From 14627abb2709066fdf7aee6ce289adeba7a99671 Mon Sep 17 00:00:00 2001 From: Alexandre SPANGARO Date: Sat, 29 Sep 2018 15:28:03 +0200 Subject: [PATCH 01/11] FIX Title problem on admin RSS module --- htdocs/admin/external_rss.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/htdocs/admin/external_rss.php b/htdocs/admin/external_rss.php index e8683dfc4ee..2862a94c207 100644 --- a/htdocs/admin/external_rss.php +++ b/htdocs/admin/external_rss.php @@ -242,10 +242,10 @@ if ($resql) { $obj = $db->fetch_object($resql); - preg_match('/^([0-9]+)/i',$obj->note,$reg); + preg_match('/^([0-9]+)/i',$obj->note,$reg); $idrss = $reg[1]; - $keyrssurl="EXTERNAL_RSS_URLRSS_".$idrss; - $keyrsstitle="EXTERNAL_RSS_URLRSS_".$idrss; + $keyrsstitle="EXTERNAL_RSS_TITLE_".$idrss; + $keyrssurl="EXTERNAL_RSS_URLRSS_".$idrss; //print "x".$idrss; $rssparser=new RssParser($db); From 75b1cac33faa25539074adbbcc24f125f05de352 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 1 Oct 2018 01:10:42 +0200 Subject: [PATCH 02/11] FIX Pagination stats --- htdocs/product/stats/card.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/product/stats/card.php b/htdocs/product/stats/card.php index 3982ea7b87e..dd12d9d7bf2 100644 --- a/htdocs/product/stats/card.php +++ b/htdocs/product/stats/card.php @@ -75,9 +75,9 @@ if (empty($search_year)) $search_year=$currentyear; $form = new Form($db); $htmlother = new FormOther($db); - $object = new Product($db); -if (! $id) + +if (! $id && empty($ref)) { llxHeader("",$langs->trans("ProductStatistics")); From d66b2322c2f7776c3c7107c981872f5aba40a2bd Mon Sep 17 00:00:00 2001 From: Ferran Marcet Date: Tue, 2 Oct 2018 09:37:44 +0200 Subject: [PATCH 03/11] FIX Can't create shipping if have shipping line's extrafields --- htdocs/expedition/card.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/expedition/card.php b/htdocs/expedition/card.php index 26b2d729f82..0b03f8fc608 100644 --- a/htdocs/expedition/card.php +++ b/htdocs/expedition/card.php @@ -1576,7 +1576,7 @@ if ($action == 'create') $srcLine = new OrderLine($db); $srcLine->fetch_optionals($line->id); // fetch extrafields also available in orderline $line = new ExpeditionLigne($db); - $line->fetch_optionals($line->id); + //$line->fetch_optionals($line->id); $line->array_options = array_merge($line->array_options, $srcLine->array_options); print ''; print $line->showOptionals($extrafieldsline, 'edit', array('style'=>$bc[$var], 'colspan'=>$colspan),$indiceAsked); From e83149a344d95bbcd1eab2f33316bb1556eb7b8d Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Tue, 2 Oct 2018 22:34:41 +0200 Subject: [PATCH 04/11] Fix deletion of credit card when no Oauth set --- .../interface_80_modStripe_Stripe.class.php | 23 ++++++------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/htdocs/core/triggers/interface_80_modStripe_Stripe.class.php b/htdocs/core/triggers/interface_80_modStripe_Stripe.class.php index 0e8147ce458..3f724e5b096 100644 --- a/htdocs/core/triggers/interface_80_modStripe_Stripe.class.php +++ b/htdocs/core/triggers/interface_80_modStripe_Stripe.class.php @@ -138,7 +138,7 @@ class InterfaceStripe if ($action == 'COMPANY_MODIFY') { dol_syslog("Trigger '" . $this->name . "' for action '$action' launched by " . __FILE__ . ". id=" . $object->id); - $stripeacc = $stripe->getStripeAccount($service); // No need of network access for this + $stripeacc = $stripe->getStripeAccount($service); // No need of network access for this. May return '' if no Oauth defined. if ($object->client != 0) { $customer = $stripe->customerStripe($object, $stripeacc, $servicestatus); // This make a network request @@ -167,7 +167,7 @@ class InterfaceStripe if ($action == 'COMPANY_DELETE') { dol_syslog("Trigger '" . $this->name . "' for action '$action' launched by " . __FILE__ . ". id=" . $object->id); - $stripeacc = $stripe->getStripeAccount($service); // No need of network access for this + $stripeacc = $stripe->getStripeAccount($service); // No need of network access for this. May return '' if no Oauth defined. $customer = $stripe->customerStripe($object, $stripeacc, $servicestatus); if ($customer) @@ -191,15 +191,10 @@ class InterfaceStripe if (! empty($object->stripe_card_ref)) { - $stripeacc = $stripe->getStripeAccount($service); // No need of network access for this + $stripeacc = $stripe->getStripeAccount($service); // No need of network access for this. May return '' if no Oauth defined. $stripecu = $stripe->getStripeCustomerAccount($object->fk_soc); // No need of network access for this - if (empty($stripeacc)) - { - $ok = -1; - $this->error = "Stripe API keys are not defined into Stripe module setup for mode ".$service; - } - elseif ($stripecu) + if ($stripecu) { // Get customer (required to get a card) if (empty($stripeacc)) { // If the Stripe connect account not set, we use common API usage @@ -231,14 +226,10 @@ class InterfaceStripe if (! empty($object->stripe_card_ref)) { - $stripeacc = $stripe->getStripeAccount($service); // No need of network access for this + $stripeacc = $stripe->getStripeAccount($service); // No need of network access for this. May return '' if no Oauth defined. $stripecu = $stripe->getStripeCustomerAccount($object->fk_soc); // No need of network access for this - if (empty($stripeacc)) - { - $ok = -1; - $this->error = "Stripe API keys are not defined into Stripe module setup for mode ".$service; - } - elseif ($stripecu) + + if ($stripecu) { // Get customer (required to get a card) if (empty($stripeacc)) { // If the Stripe connect account not set, we use common API usage From 587766b39b835d029c5f3f1ecd12b3028d0ad9af Mon Sep 17 00:00:00 2001 From: gauthier Date: Wed, 3 Oct 2018 10:38:47 +0200 Subject: [PATCH 05/11] FIX : wrong function name --- htdocs/core/lib/pdf.lib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/core/lib/pdf.lib.php b/htdocs/core/lib/pdf.lib.php index 1d6c26caecd..36f1997ac85 100644 --- a/htdocs/core/lib/pdf.lib.php +++ b/htdocs/core/lib/pdf.lib.php @@ -440,7 +440,7 @@ function pdf_build_address($outputlangs,$sourcecompany,$targetcompany='',$target // Contact on a thirdparty that is a different thirdparty than the thirdparty of object if ($targetcontact->socid > 0 && $targetcontact->socid != $targetcompany->id) { - $targetcontact->fetch_thirparty(); + $targetcontact->fetch_thirdparty(); $companytouseforaddress = $targetcontact->thirdparty; } From 583ea055671f27eaf845b8c9b5a7061e65ef816a Mon Sep 17 00:00:00 2001 From: atm-greg Date: Wed, 3 Oct 2018 12:13:53 +0200 Subject: [PATCH 06/11] fix missing letter... --- htdocs/core/lib/pdf.lib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/core/lib/pdf.lib.php b/htdocs/core/lib/pdf.lib.php index 66caf2ea23a..31c6b613e24 100644 --- a/htdocs/core/lib/pdf.lib.php +++ b/htdocs/core/lib/pdf.lib.php @@ -438,7 +438,7 @@ function pdf_build_address($outputlangs,$sourcecompany,$targetcompany='',$target // Contact on a thirdparty that is a different thirdparty than the thirdparty of object if ($targetcontact->socid > 0 && $targetcontact->socid != $targetcompany->id) { - $targetcontact->fetch_thirparty(); + $targetcontact->fetch_thirdparty(); $companytouseforaddress = $targetcontact->thirdparty; } From 8ee7a48583378758cb7c5aa19be25e0eb0207b01 Mon Sep 17 00:00:00 2001 From: aplose Date: Wed, 3 Oct 2018 15:15:46 +0200 Subject: [PATCH 07/11] Fix issue #9434 : no array_options available when calling api : I replace the deprecated code by the fetch_optionnals() recommanded call. --- htdocs/commande/class/commande.class.php | 12 ++++++++---- htdocs/compta/facture/class/facture.class.php | 12 +++++++----- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/htdocs/commande/class/commande.class.php b/htdocs/commande/class/commande.class.php index 9007490d421..6677a9f08de 100644 --- a/htdocs/commande/class/commande.class.php +++ b/htdocs/commande/class/commande.class.php @@ -1677,10 +1677,11 @@ class Commande extends CommonOrder // Retrieve all extrafields for invoice // fetch optionals attributes and labels - require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php'; - $extrafields=new ExtraFields($this->db); - $extralabels=$extrafields->fetch_name_optionals_label($this->table_element,true); - $this->fetch_optionals($this->id,$extralabels); +// require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php'; +// $extrafields=new ExtraFields($this->db); +// $extralabels=$extrafields->fetch_name_optionals_label($this->table_element,true); +// $this->fetch_optionals($this->id,$extralabels); + $this->fetch_optionals(); $this->db->free($result); @@ -1882,6 +1883,9 @@ class Commande extends CommonOrder $line->multicurrency_total_tva = $objp->multicurrency_total_tva; $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc; + $line->fetch_optionals(); + + $this->lines[$i] = $line; $i++; diff --git a/htdocs/compta/facture/class/facture.class.php b/htdocs/compta/facture/class/facture.class.php index bc7a14c0c45..9bf6d4b876c 100644 --- a/htdocs/compta/facture/class/facture.class.php +++ b/htdocs/compta/facture/class/facture.class.php @@ -1353,10 +1353,12 @@ class Facture extends CommonInvoice // Retrieve all extrafield for invoice // fetch optionals attributes and labels - require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php'; - $extrafields=new ExtraFields($this->db); - $extralabels=$extrafields->fetch_name_optionals_label($this->table_element,true); - $this->fetch_optionals($this->id,$extralabels); +// require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php'; +// $extrafields=new ExtraFields($this->db); +// $extralabels=$extrafields->fetch_name_optionals_label($this->table_element,true); +// $this->fetch_optionals($this->id,$extralabels); + $this->fetch_optionals(); + /* * Lines @@ -1477,7 +1479,7 @@ class Facture extends CommonInvoice $line->multicurrency_total_tva = $objp->multicurrency_total_tva; $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc; - // TODO Fetch optional like done in fetch line of facture_rec ? + $line->fetch_optionals(); $this->lines[$i] = $line; From ef7ed9786609cca1f277edf54e6036c74c0e4944 Mon Sep 17 00:00:00 2001 From: florian HENRY Date: Wed, 3 Oct 2018 15:40:58 +0200 Subject: [PATCH 08/11] fix ressource list with extrafields --- htdocs/resource/list.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/htdocs/resource/list.php b/htdocs/resource/list.php index 8ef3168ee96..cce22025a9a 100644 --- a/htdocs/resource/list.php +++ b/htdocs/resource/list.php @@ -1,7 +1,7 @@ * Copyright (C) 2018 Nicolas ZABOURI - * + * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or @@ -242,14 +242,14 @@ print "\n"; if ($ret) { - foreach ($object->lines as $obj) + foreach ($object->lines as $ressource) { print ''; if (! empty($arrayfields['t.ref']['checked'])) { print ''; - print $obj->getNomUrl(5); + print $ressource->getNomUrl(5); print ''; if (! $i) $totalarray['nbfield']++; } @@ -257,20 +257,20 @@ if ($ret) if (! empty($arrayfields['ty.label']['checked'])) { print ''; - print $obj->type_label; + print $ressource->type_label; print ''; if (! $i) $totalarray['nbfield']++; } // Extra fields - $obj = (Object) $resource->array_options; + $obj = (Object) $ressource->array_options; include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_print_fields.tpl.php'; print ''; - print ''; + print ''; print img_edit(); print ''; print ' '; - print ''; + print ''; print img_delete(); print ''; print ''; From b8c88a214449d5129fd4c479249cfa7c2373d10d Mon Sep 17 00:00:00 2001 From: florian HENRY Date: Wed, 3 Oct 2018 15:43:00 +0200 Subject: [PATCH 09/11] rename var --- htdocs/resource/list.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/htdocs/resource/list.php b/htdocs/resource/list.php index cce22025a9a..85be267613d 100644 --- a/htdocs/resource/list.php +++ b/htdocs/resource/list.php @@ -242,14 +242,14 @@ print "\n"; if ($ret) { - foreach ($object->lines as $ressource) + foreach ($object->lines as $resource) { print ''; if (! empty($arrayfields['t.ref']['checked'])) { print ''; - print $ressource->getNomUrl(5); + print $resource->getNomUrl(5); print ''; if (! $i) $totalarray['nbfield']++; } @@ -257,20 +257,20 @@ if ($ret) if (! empty($arrayfields['ty.label']['checked'])) { print ''; - print $ressource->type_label; + print $resource->type_label; print ''; if (! $i) $totalarray['nbfield']++; } // Extra fields - $obj = (Object) $ressource->array_options; + $obj = (Object) $resource->array_options; include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_print_fields.tpl.php'; print ''; - print ''; + print ''; print img_edit(); print ''; print ' '; - print ''; + print ''; print img_delete(); print ''; print ''; From 7a94f4fc00a4427f2a0c09f18f1de279a6abca32 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Thu, 4 Oct 2018 11:34:46 +0200 Subject: [PATCH 10/11] Add more logs --- htdocs/public/payment/paymentok.php | 40 ++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/htdocs/public/payment/paymentok.php b/htdocs/public/payment/paymentok.php index 34209877bf3..91c97098f33 100644 --- a/htdocs/public/payment/paymentok.php +++ b/htdocs/public/payment/paymentok.php @@ -201,34 +201,50 @@ if (! empty($conf->paypal->enabled)) $resArray=getDetails($onlinetoken); //var_dump($resarray); - dol_syslog("We call DoExpressCheckoutPayment token=".$onlinetoken." paymentType=".$paymentType." currencyCodeType=".$currencyCodeType." payerID=".$payerID." ipaddress=".$ipaddress." FinalPaymentAmt=".$FinalPaymentAmt." fulltag=".$fulltag, LOG_DEBUG, 0, '_payment'); - $resArray=confirmPayment($onlinetoken, $paymentType, $currencyCodeType, $payerID, $ipaddress, $FinalPaymentAmt, $fulltag); - $ack = strtoupper($resArray["ACK"]); if ($ack=="SUCCESS" || $ack=="SUCCESSWITHWARNING") { + // Nothing to do + dol_syslog("Call to GetExpressCheckoutDetails return ".$ack); + } + else + { + dol_syslog("Call to GetExpressCheckoutDetails return error: ".json_encode($resArray), LOG_WARNING); + } + + dol_syslog("We call DoExpressCheckoutPayment token=".$onlinetoken." paymentType=".$paymentType." currencyCodeType=".$currencyCodeType." payerID=".$payerID." ipaddress=".$ipaddress." FinalPaymentAmt=".$FinalPaymentAmt." fulltag=".$fulltag, LOG_DEBUG, 0, '_payment'); + $resArray2=confirmPayment($onlinetoken, $paymentType, $currencyCodeType, $payerID, $ipaddress, $FinalPaymentAmt, $fulltag); + //var_dump($resarray); + + $ack = strtoupper($resArray2["ACK"]); + if ($ack=="SUCCESS" || $ack=="SUCCESSWITHWARNING") + { + dol_syslog("Call to GetExpressCheckoutDetails return ".$ack); + $object->source = $source; $object->ref = $ref; $object->payerID = $payerID; $object->fulltag = $fulltag; - $object->resArray = $resArray; + $object->resArray = $resArray2; // resArray was built from a string like that // TOKEN=EC%2d1NJ057703V9359028&TIMESTAMP=2010%2d11%2d01T11%3a40%3a13Z&CORRELATIONID=1efa8c6a36bd8&ACK=Success&VERSION=56&BUILD=1553277&TRANSACTIONID=9B994597K9921420R&TRANSACTIONTYPE=expresscheckout&PAYMENTTYPE=instant&ORDERTIME=2010%2d11%2d01T11%3a40%3a12Z&AMT=155%2e57&FEEAMT=5%2e54&TAXAMT=0%2e00&CURRENCYCODE=EUR&PAYMENTSTATUS=Completed&PENDINGREASON=None&REASONCODE=None - $PAYMENTSTATUS=urldecode($resArray["PAYMENTSTATUS"]); // Should contains 'Completed' - $TRANSACTIONID=urldecode($resArray["TRANSACTIONID"]); - $TAXAMT=urldecode($resArray["TAXAMT"]); - $NOTE=urldecode($resArray["NOTE"]); + $PAYMENTSTATUS=urldecode($resArray2["PAYMENTSTATUS"]); // Should contains 'Completed' + $TRANSACTIONID=urldecode($resArray2["TRANSACTIONID"]); + $TAXAMT=urldecode($resArray2["TAXAMT"]); + $NOTE=urldecode($resArray2["NOTE"]); $ispaymentok=true; } else { + dol_syslog("Call to DoExpressCheckoutPayment return error: ".json_encode($resArray2), LOG_WARNING); + //Display a user friendly Error on the page using any of the following error information returned by PayPal - $ErrorCode = urldecode($resArray["L_ERRORCODE0"]); - $ErrorShortMsg = urldecode($resArray["L_SHORTMESSAGE0"]); - $ErrorLongMsg = urldecode($resArray["L_LONGMESSAGE0"]); - $ErrorSeverityCode = urldecode($resArray["L_SEVERITYCODE0"]); + $ErrorCode = urldecode($resArray2["L_ERRORCODE0"]); + $ErrorShortMsg = urldecode($resArray2["L_SHORTMESSAGE0"]); + $ErrorLongMsg = urldecode($resArray2["L_LONGMESSAGE0"]); + $ErrorSeverityCode = urldecode($resArray2["L_SEVERITYCODE0"]); } } else From 75c968b4ae07ba424a19fb2a61ba4d5d2e17be58 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Thu, 4 Oct 2018 13:10:48 +0200 Subject: [PATCH 11/11] FIX Backup of database without mysqladmin available from cron. --- htdocs/admin/tools/export.php | 173 --------------------- htdocs/core/class/utils.class.php | 240 ++++++++++++++++++++++++++++-- 2 files changed, 224 insertions(+), 189 deletions(-) diff --git a/htdocs/admin/tools/export.php b/htdocs/admin/tools/export.php index f3d2057395f..e10b5530f52 100644 --- a/htdocs/admin/tools/export.php +++ b/htdocs/admin/tools/export.php @@ -235,176 +235,3 @@ $time_end = time(); $db->close(); - - -// MYSQL NO BINARIES (only php) -/** Backup the db OR just a table without mysqldump binary (does not require any exec permission) - * Author: David Walsh (http://davidwalsh.name/backup-mysql-database-php) - * Updated and enhanced by Stephen Larroque (lrq3000) and by the many commentators from the blog - * Note about foreign keys constraints: for Dolibarr, since there are a lot of constraints and when imported the tables will be inserted in the dumped order, not in constraints order, then we ABSOLUTELY need to use SET FOREIGN_KEY_CHECKS=0; when importing the sql dump. - * Note2: db2SQL by Howard Yeend can be an alternative, by using SHOW FIELDS FROM and SHOW KEYS FROM we could generate a more precise dump (eg: by getting the type of the field and then precisely outputting the right formatting - in quotes, numeric or null - instead of trying to guess like we are doing now). - * - * @param string $outputfile Output file name - * @param string $tables Table name or '*' for all - * @return int <0 if KO, >0 if OK - */ -function backup_tables($outputfile, $tables='*') -{ - global $db, $langs; - global $errormsg; - - // Set to UTF-8 - if (is_a($db, 'DoliDBMysqli')) { - /** @var DoliDBMysqli $db */ - $db->db->set_charset('utf8'); - } else { - /** @var DoliDB $db */ - $db->query('SET NAMES utf8'); - $db->query('SET CHARACTER SET utf8'); - } - - //get all of the tables - if ($tables == '*') - { - $tables = array(); - $result = $db->query('SHOW FULL TABLES WHERE Table_type = \'BASE TABLE\''); - while($row = $db->fetch_row($result)) - { - $tables[] = $row[0]; - } - } - else - { - $tables = is_array($tables) ? $tables : explode(',',$tables); - } - - //cycle through - $handle = fopen($outputfile, 'w+'); - if (fwrite($handle, '') === false) - { - $langs->load("errors"); - dol_syslog("Failed to open file ".$outputfile,LOG_ERR); - $errormsg=$langs->trans("ErrorFailedToWriteInDir"); - return -1; - } - - // Print headers and global mysql config vars - $sqlhead = ''; - $sqlhead .= "-- ".$db::LABEL." dump via php with Dolibarr ".DOL_VERSION." --- --- Host: ".$db->db->host_info." Database: ".$db->database_name." --- ------------------------------------------------------ --- Server version ".$db->db->server_info." - -/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; -/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; -/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; -/*!40101 SET NAMES utf8 */; -/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; -/*!40103 SET TIME_ZONE='+00:00' */; -/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; -/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; -/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; -/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; - -"; - - if (GETPOST("nobin_disable_fk")) $sqlhead .= "SET FOREIGN_KEY_CHECKS=0;\n"; - //$sqlhead .= "SET SQL_MODE=\"NO_AUTO_VALUE_ON_ZERO\";\n"; - if (GETPOST("nobin_use_transaction")) $sqlhead .= "SET AUTOCOMMIT=0;\nSTART TRANSACTION;\n"; - - fwrite($handle, $sqlhead); - - $ignore = ''; - if (GETPOST("nobin_sql_ignore")) $ignore = 'IGNORE '; - $delayed = ''; - if (GETPOST("nobin_delayed")) $delayed = 'DELAYED '; - - // Process each table and print their definition + their datas - foreach($tables as $table) - { - // Saving the table structure - fwrite($handle, "\n--\n-- Table structure for table `".$table."`\n--\n"); - - if (GETPOST("nobin_drop")) fwrite($handle,"DROP TABLE IF EXISTS `".$table."`;\n"); // Dropping table if exists prior to re create it - fwrite($handle,"/*!40101 SET @saved_cs_client = @@character_set_client */;\n"); - fwrite($handle,"/*!40101 SET character_set_client = utf8 */;\n"); - $resqldrop=$db->query('SHOW CREATE TABLE '.$table); - $row2 = $db->fetch_row($resqldrop); - if (empty($row2[1])) - { - fwrite($handle, "\n-- WARNING: Show create table ".$table." return empy string when it should not.\n"); - } - else - { - fwrite($handle,$row2[1].";\n"); - //fwrite($handle,"/*!40101 SET character_set_client = @saved_cs_client */;\n\n"); - - // Dumping the data (locking the table and disabling the keys check while doing the process) - fwrite($handle, "\n--\n-- Dumping data for table `".$table."`\n--\n"); - if (!GETPOST("nobin_nolocks")) fwrite($handle, "LOCK TABLES `".$table."` WRITE;\n"); // Lock the table before inserting data (when the data will be imported back) - if (GETPOST("nobin_disable_fk")) fwrite($handle, "ALTER TABLE `".$table."` DISABLE KEYS;\n"); - else fwrite($handle, "/*!40000 ALTER TABLE `".$table."` DISABLE KEYS */;\n"); - - $sql='SELECT * FROM '.$table; - $result = $db->query($sql); - while($row = $db->fetch_row($result)) - { - // For each row of data we print a line of INSERT - fwrite($handle,'INSERT '.$delayed.$ignore.'INTO `'.$table.'` VALUES ('); - $columns = count($row); - for($j=0; $j<$columns; $j++) { - // Processing each columns of the row to ensure that we correctly save the value (eg: add quotes for string - in fact we add quotes for everything, it's easier) - if ($row[$j] == null && !is_string($row[$j])) { - // IMPORTANT: if the field is NULL we set it NULL - $row[$j] = 'NULL'; - } elseif(is_string($row[$j]) && $row[$j] == '') { - // if it's an empty string, we set it as an empty string - $row[$j] = "''"; - } elseif(is_numeric($row[$j]) && !strcmp($row[$j], $row[$j]+0) ) { // test if it's a numeric type and the numeric version ($nb+0) == string version (eg: if we have 01, it's probably not a number but rather a string, else it would not have any leading 0) - // if it's a number, we return it as-is -// $row[$j] = $row[$j]; - } else { // else for all other cases we escape the value and put quotes around - $row[$j] = addslashes($row[$j]); - $row[$j] = preg_replace("#\n#", "\\n", $row[$j]); - $row[$j] = "'".$row[$j]."'"; - } - } - fwrite($handle,implode(',', $row).");\n"); - } - if (GETPOST("nobin_disable_fk")) fwrite($handle, "ALTER TABLE `".$table."` ENABLE KEYS;\n"); // Enabling back the keys/index checking - if (!GETPOST("nobin_nolocks")) fwrite($handle, "UNLOCK TABLES;\n"); // Unlocking the table - fwrite($handle,"\n\n\n"); - } - } - - /* Backup Procedure structure*/ - /* - $result = $db->query('SHOW PROCEDURE STATUS'); - if ($db->num_rows($result) > 0) - { - while ($row = $db->fetch_row($result)) { $procedures[] = $row[1]; } - foreach($procedures as $proc) - { - fwrite($handle,"DELIMITER $$\n\n"); - fwrite($handle,"DROP PROCEDURE IF EXISTS '$name'.'$proc'$$\n"); - $resqlcreateproc=$db->query("SHOW CREATE PROCEDURE '$proc'"); - $row2 = $db->fetch_row($resqlcreateproc); - fwrite($handle,"\n".$row2[2]."$$\n\n"); - fwrite($handle,"DELIMITER ;\n\n"); - } - } - */ - /* Backup Procedure structure*/ - - // Write the footer (restore the previous database settings) - $sqlfooter="\n\n"; - if (GETPOST("nobin_use_transaction")) $sqlfooter .= "COMMIT;\n"; - if (GETPOST("nobin_disable_fk")) $sqlfooter .= "SET FOREIGN_KEY_CHECKS=1;\n"; - $sqlfooter.="\n\n-- Dump completed on ".date('Y-m-d G-i-s'); - fwrite($handle, $sqlfooter); - - fclose($handle); - - return 1; -} diff --git a/htdocs/core/class/utils.class.php b/htdocs/core/class/utils.class.php index 3a7a79122c9..b0b641eca57 100644 --- a/htdocs/core/class/utils.class.php +++ b/htdocs/core/class/utils.class.php @@ -176,14 +176,14 @@ class Utils * @param int $usedefault 1=Use default backup profile (Set this to 1 when used as cron) * @param string $file 'auto' or filename to build * @param int $keeplastnfiles Keep only last n files (not used yet) + * @param int $execmethod 0=Use default method (that is 1 by default), 1=Use the PHP 'exec', 2=Use the 'popen' method * @return int 0 if OK, < 0 if KO (this function is used also by cron so only 0 is OK) */ - function dumpDatabase($compression='none', $type='auto', $usedefault=1, $file='auto', $keeplastnfiles=0) + function dumpDatabase($compression='none', $type='auto', $usedefault=1, $file='auto', $keeplastnfiles=0, $execmethod=0) { global $db, $conf, $langs, $dolibarr_main_data_root; global $dolibarr_main_db_name, $dolibarr_main_db_host, $dolibarr_main_db_user, $dolibarr_main_db_port, $dolibarr_main_db_pass; - $langs->load("admin"); dol_syslog("Utils::dumpDatabase type=".$type." compression=".$compression." file=".$file, LOG_DEBUG); @@ -291,21 +291,58 @@ class Utils if ($handle) { + $execmethod=1; + if (! empty($conf->global->MAIN_EXEC_USE_POPEN)) $execmethod=$conf->global->MAIN_EXEC_USE_POPEN; + if (empty($execmethod)) $execmethod=1; + $ok=0; - dol_syslog("Run command ".$fullcommandcrypted); - $handlein = popen($fullcommandclear, 'r'); - $i=0; - while (!feof($handlein)) + dol_syslog("Run command with method ".$execmethod." with ".$fullcommandcrypted); + + // TODO Replace with executeCLI function + if ($execmethod == 1) { - $i++; // output line number - $read = fgets($handlein); - // Exclude warning line we don't want - if ($i == 1 && preg_match('/Warning.*Using a password/i', $read)) continue; - fwrite($handle,$read); - if (preg_match('/'.preg_quote('-- Dump completed').'/i',$read)) $ok=1; - elseif (preg_match('/'.preg_quote('SET SQL_NOTES=@OLD_SQL_NOTES').'/i',$read)) $ok=1; + exec($fullcommandclear, $readt, $retval); + $result = $retval; + + if ($retval != 0) + { + $langs->load("errors"); + dol_syslog("Datadump retval after exec=".$retval, LOG_ERR); + $error = 'Error '.$retval; + $ok=0; + } + else + { + $i=0; + if (!empty($readt)) + foreach($readt as $key=>$read) + { + $i++; // output line number + if ($i == 1 && preg_match('/Warning.*Using a password/i', $read)) continue; + fwrite($handle,$read); + if (preg_match('/'.preg_quote('-- Dump completed').'/i',$read)) $ok=1; + elseif (preg_match('/'.preg_quote('SET SQL_NOTES=@OLD_SQL_NOTES').'/i',$read)) $ok=1; + } + } } - pclose($handlein); + if ($execmethod == 2) // With this method, there is no way to get the return code, only output + { + $handlein = popen($fullcommandclear, 'r'); + $i=0; + while (!feof($handlein)) + { + $i++; // output line number + $read = fgets($handlein); + // Exclude warning line we don't want + if ($i == 1 && preg_match('/Warning.*Using a password/i', $read)) continue; + fwrite($handle,$read); + if (preg_match('/'.preg_quote('-- Dump completed').'/i',$read)) $ok=1; + elseif (preg_match('/'.preg_quote('SET SQL_NOTES=@OLD_SQL_NOTES').'/i',$read)) $ok=1; + } + pclose($handlein); + + } + if ($compression == 'none') fclose($handle); if ($compression == 'gz') gzclose($handle); @@ -370,13 +407,13 @@ class Utils if ($compression == 'gz' or $compression == 'bz') { - backup_tables($outputfiletemp); + $this->backup_tables($outputfiletemp); dol_compress_file($outputfiletemp, $outputfile, $compression); unlink($outputfiletemp); } else { - backup_tables($outputfile); + $this->backup_tables($outputfile); } $this->output = ""; @@ -782,4 +819,175 @@ class Utils $this->output = 'Archive log files (keeping last SYSLOG_FILE_SAVES='.$nbSaves.' files) done.'; return 0; } + + /** Backup the db OR just a table without mysqldump binary, with PHP only (does not require any exec permission) + * Author: David Walsh (http://davidwalsh.name/backup-mysql-database-php) + * Updated and enhanced by Stephen Larroque (lrq3000) and by the many commentators from the blog + * Note about foreign keys constraints: for Dolibarr, since there are a lot of constraints and when imported the tables will be inserted in the dumped order, not in constraints order, then we ABSOLUTELY need to use SET FOREIGN_KEY_CHECKS=0; when importing the sql dump. + * Note2: db2SQL by Howard Yeend can be an alternative, by using SHOW FIELDS FROM and SHOW KEYS FROM we could generate a more precise dump (eg: by getting the type of the field and then precisely outputting the right formatting - in quotes, numeric or null - instead of trying to guess like we are doing now). + * + * @param string $outputfile Output file name + * @param string $tables Table name or '*' for all + * @return int <0 if KO, >0 if OK + */ + function backup_tables($outputfile, $tables='*') + { + global $db, $langs; + global $errormsg; + + // Set to UTF-8 + if (is_a($db, 'DoliDBMysqli')) { + /** @var DoliDBMysqli $db */ + $db->db->set_charset('utf8'); + } else { + /** @var DoliDB $db */ + $db->query('SET NAMES utf8'); + $db->query('SET CHARACTER SET utf8'); + } + + //get all of the tables + if ($tables == '*') + { + $tables = array(); + $result = $db->query('SHOW FULL TABLES WHERE Table_type = \'BASE TABLE\''); + while($row = $db->fetch_row($result)) + { + $tables[] = $row[0]; + } + } + else + { + $tables = is_array($tables) ? $tables : explode(',',$tables); + } + + //cycle through + $handle = fopen($outputfile, 'w+'); + if (fwrite($handle, '') === false) + { + $langs->load("errors"); + dol_syslog("Failed to open file ".$outputfile,LOG_ERR); + $errormsg=$langs->trans("ErrorFailedToWriteInDir"); + return -1; + } + + // Print headers and global mysql config vars + $sqlhead = ''; + $sqlhead .= "-- ".$db::LABEL." dump via php with Dolibarr ".DOL_VERSION." +-- +-- Host: ".$db->db->host_info." Database: ".$db->database_name." +-- ------------------------------------------------------ +-- Server version ".$db->db->server_info." + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40101 SET NAMES utf8 */; +/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; +/*!40103 SET TIME_ZONE='+00:00' */; +/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; +/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; +/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; +/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; + +"; + + if (GETPOST("nobin_disable_fk")) $sqlhead .= "SET FOREIGN_KEY_CHECKS=0;\n"; + //$sqlhead .= "SET SQL_MODE=\"NO_AUTO_VALUE_ON_ZERO\";\n"; + if (GETPOST("nobin_use_transaction")) $sqlhead .= "SET AUTOCOMMIT=0;\nSTART TRANSACTION;\n"; + + fwrite($handle, $sqlhead); + + $ignore = ''; + if (GETPOST("nobin_sql_ignore")) $ignore = 'IGNORE '; + $delayed = ''; + if (GETPOST("nobin_delayed")) $delayed = 'DELAYED '; + + // Process each table and print their definition + their datas + foreach($tables as $table) + { + // Saving the table structure + fwrite($handle, "\n--\n-- Table structure for table `".$table."`\n--\n"); + + if (GETPOST("nobin_drop")) fwrite($handle,"DROP TABLE IF EXISTS `".$table."`;\n"); // Dropping table if exists prior to re create it + fwrite($handle,"/*!40101 SET @saved_cs_client = @@character_set_client */;\n"); + fwrite($handle,"/*!40101 SET character_set_client = utf8 */;\n"); + $resqldrop=$db->query('SHOW CREATE TABLE '.$table); + $row2 = $db->fetch_row($resqldrop); + if (empty($row2[1])) + { + fwrite($handle, "\n-- WARNING: Show create table ".$table." return empy string when it should not.\n"); + } + else + { + fwrite($handle,$row2[1].";\n"); + //fwrite($handle,"/*!40101 SET character_set_client = @saved_cs_client */;\n\n"); + + // Dumping the data (locking the table and disabling the keys check while doing the process) + fwrite($handle, "\n--\n-- Dumping data for table `".$table."`\n--\n"); + if (!GETPOST("nobin_nolocks")) fwrite($handle, "LOCK TABLES `".$table."` WRITE;\n"); // Lock the table before inserting data (when the data will be imported back) + if (GETPOST("nobin_disable_fk")) fwrite($handle, "ALTER TABLE `".$table."` DISABLE KEYS;\n"); + else fwrite($handle, "/*!40000 ALTER TABLE `".$table."` DISABLE KEYS */;\n"); + + $sql='SELECT * FROM '.$table; + $result = $db->query($sql); + while($row = $db->fetch_row($result)) + { + // For each row of data we print a line of INSERT + fwrite($handle,'INSERT '.$delayed.$ignore.'INTO `'.$table.'` VALUES ('); + $columns = count($row); + for($j=0; $j<$columns; $j++) { + // Processing each columns of the row to ensure that we correctly save the value (eg: add quotes for string - in fact we add quotes for everything, it's easier) + if ($row[$j] == null && !is_string($row[$j])) { + // IMPORTANT: if the field is NULL we set it NULL + $row[$j] = 'NULL'; + } elseif(is_string($row[$j]) && $row[$j] == '') { + // if it's an empty string, we set it as an empty string + $row[$j] = "''"; + } elseif(is_numeric($row[$j]) && !strcmp($row[$j], $row[$j]+0) ) { // test if it's a numeric type and the numeric version ($nb+0) == string version (eg: if we have 01, it's probably not a number but rather a string, else it would not have any leading 0) + // if it's a number, we return it as-is + // $row[$j] = $row[$j]; + } else { // else for all other cases we escape the value and put quotes around + $row[$j] = addslashes($row[$j]); + $row[$j] = preg_replace("#\n#", "\\n", $row[$j]); + $row[$j] = "'".$row[$j]."'"; + } + } + fwrite($handle,implode(',', $row).");\n"); + } + if (GETPOST("nobin_disable_fk")) fwrite($handle, "ALTER TABLE `".$table."` ENABLE KEYS;\n"); // Enabling back the keys/index checking + if (!GETPOST("nobin_nolocks")) fwrite($handle, "UNLOCK TABLES;\n"); // Unlocking the table + fwrite($handle,"\n\n\n"); + } + } + + /* Backup Procedure structure*/ + /* + $result = $db->query('SHOW PROCEDURE STATUS'); + if ($db->num_rows($result) > 0) + { + while ($row = $db->fetch_row($result)) { $procedures[] = $row[1]; } + foreach($procedures as $proc) + { + fwrite($handle,"DELIMITER $$\n\n"); + fwrite($handle,"DROP PROCEDURE IF EXISTS '$name'.'$proc'$$\n"); + $resqlcreateproc=$db->query("SHOW CREATE PROCEDURE '$proc'"); + $row2 = $db->fetch_row($resqlcreateproc); + fwrite($handle,"\n".$row2[2]."$$\n\n"); + fwrite($handle,"DELIMITER ;\n\n"); + } + } + */ + /* Backup Procedure structure*/ + + // Write the footer (restore the previous database settings) + $sqlfooter="\n\n"; + if (GETPOST("nobin_use_transaction")) $sqlfooter .= "COMMIT;\n"; + if (GETPOST("nobin_disable_fk")) $sqlfooter .= "SET FOREIGN_KEY_CHECKS=1;\n"; + $sqlfooter.="\n\n-- Dump completed on ".date('Y-m-d G-i-s'); + fwrite($handle, $sqlfooter); + + fclose($handle); + + return 1; + } } \ No newline at end of file