dolibarr  17.0.4
migrate_picture_path.php
Go to the documentation of this file.
1 #!/usr/bin/env php
2 <?php
3 /*
4  * Copyright (C) 2007-2016 Laurent Destailleur <eldy@users.sourceforge.net>
5  * Copyright (C) 2015 Jean Heimburger <http://tiaris.eu>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
27 if (!defined('NOSESSION')) {
28  define('NOSESSION', '1');
29 }
30 
31 $sapi_type = php_sapi_name();
32 $script_file = basename(__FILE__);
33 $path = __DIR__.'/';
34 
35 // Test if batch mode
36 if (substr($sapi_type, 0, 3) == 'cgi') {
37  echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
38  exit(-1);
39 }
40 
41 @set_time_limit(0); // No timeout for this script
42 define('EVEN_IF_ONLY_LOGIN_ALLOWED', 1); // Set this define to 0 if you want to lock your script when dolibarr setup is "locked to admin user only".
43 
44 // Include and load Dolibarr environment variables
45 require_once $path."../../htdocs/master.inc.php";
46 require_once DOL_DOCUMENT_ROOT."/product/class/product.class.php";
47 require_once DOL_DOCUMENT_ROOT."/core/lib/files.lib.php";
48 // After this $db, $mysoc, $langs, $conf and $hookmanager are defined (Opened $db handler to database will be closed at end of file).
49 // $user is created but empty.
50 
51 // $langs->setDefaultLang('en_US'); // To change default language of $langs
52 $langs->load("main"); // To load language file for default language
53 
54 // Global variables
55 $version = DOL_VERSION;
56 $error = 0;
57 $forcecommit = 0;
58 
59 print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
60 dol_syslog($script_file." launched with arg ".join(',', $argv));
61 
62 if (!isset($argv[1]) || $argv[1] != 'product') {
63  print "Usage: $script_file product\n";
64  exit(-1);
65 }
66 
67 print '--- start'."\n";
68 
69 // Case to migrate products path
70 if ($argv[1] == 'product') {
71  $product = new Product($db);
72 
73  $sql = "SELECT rowid as pid from ".MAIN_DB_PREFIX."product"; // Get list of all products
74  $resql = $db->query($sql);
75  if ($resql) {
76  while ($obj = $db->fetch_object($resql)) {
77  $product->fetch($obj->pid);
78  print " migrating product id=".$product->id." ref=".$product->ref."\n";
79  migrate_product_photospath($product);
80  }
81  } else {
82  print "\n sql error ".$sql;
83  exit();
84  }
85 }
86 
87 $db->close(); // Close $db database opened handler
88 
89 exit($error);
90 
97 function migrate_product_photospath($product)
98 {
99  global $conf;
100 
101  $dir = $conf->product->multidir_output[$product->entity];
102  $conf->global->PRODUCT_USE_OLD_PATH_FOR_PHOTO = 1;
103  $origin = $dir.'/'.get_exdir($product->id, 2, 0, 0, $product, 'product').$product->id."/photos";
104  $destin = $dir.'/'.dol_sanitizeFileName($product->ref);
105 
106  $error = 0;
107 
108  $origin_osencoded = dol_osencode($origin);
109  $destin_osencoded = dol_osencode($destin);
110  dol_mkdir($destin);
111 
112  if (dol_is_dir($origin)) {
113  $handle = opendir($origin_osencoded);
114  if (is_resource($handle)) {
115  while (($file = readdir($handle)) !== false) {
116  if ($file != '.' && $file != '..' && is_dir($origin_osencoded.'/'.$file)) {
117  $thumbs = opendir($origin_osencoded.'/'.$file);
118  if (is_resource($thumbs)) {
119  dol_mkdir($destin.'/'.$file);
120  while (($thumb = readdir($thumbs)) !== false) {
121  dol_move($origin.'/'.$file.'/'.$thumb, $destin.'/'.$file.'/'.$thumb);
122  }
123  // dol_delete_dir($origin.'/'.$file);
124  }
125  } else {
126  if (dol_is_file($origin.'/'.$file)) {
127  dol_move($origin.'/'.$file, $destin.'/'.$file);
128  }
129  }
130  }
131  }
132  }
133 }
Class to manage products or services.
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:745
dol_is_file($pathoffile)
Return if path is a file.
Definition: files.lib.php:481
dol_is_dir($folder)
Test if filename is a directory.
Definition: files.lib.php:451
dol_move($srcfile, $destfile, $newmask=0, $overwriteifexists=1, $testvirus=0, $indexdatabase=1)
Move a file into another name.
Definition: files.lib.php:875
dol_getmypid()
Return getmypid() or random PID when function is disabled Some web hosts disable this php function fo...
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
get_exdir($num, $level, $alpha, $withoutslash, $object, $modulepart='')
Return a path to have a the directory according to object where files are stored.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)