dolibarr  13.0.2
migrate_picture_path.php
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 <http://www.gnu.org/licenses/>.
19  */
20 
27 if (!defined('NOSESSION')) define('NOSESSION', '1');
28 
29 $sapi_type = php_sapi_name();
30 $script_file = basename(__FILE__);
31 $path = __DIR__.'/';
32 
33 // Test if batch mode
34 if (substr($sapi_type, 0, 3) == 'cgi') {
35  echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
36  exit(-1);
37 }
38 
39 @set_time_limit(0); // No timeout for this script
40 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".
41 
42 // Include and load Dolibarr environment variables
43 require_once $path."../../htdocs/master.inc.php";
44 require_once DOL_DOCUMENT_ROOT."/user/class/user.class.php";
45 require_once DOL_DOCUMENT_ROOT."/core/lib/files.lib.php";
46 // After this $db, $mysoc, $langs, $conf and $hookmanager are defined (Opened $db handler to database will be closed at end of file).
47 // $user is created but empty.
48 
49 // $langs->setDefaultLang('en_US'); // To change default language of $langs
50 $langs->load("main"); // To load language file for default language
51 
52 // Global variables
53 $version = DOL_VERSION;
54 $error = 0;
55 $forcecommit = 0;
56 
57 print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
58 dol_syslog($script_file." launched with arg ".join(',', $argv));
59 
60 if (!isset($argv[1]) || $argv[1] != 'user') {
61  print "Usage: $script_file user\n";
62  exit(-1);
63 }
64 
65 print '--- start'."\n";
66 
67 // Case to migrate products path
68 if ($argv[1] == 'user') {
69  $u = new User($db);
70 
71  $sql = "SELECT rowid as uid from ".MAIN_DB_PREFIX."user"; // Get list of all products
72  $resql = $db->query($sql);
73  if ($resql) {
74  while ($obj = $db->fetch_object($resql)) {
75  $u->fetch($obj->uid);
76  print " migrating user id=".$u->id." ref=".$u->ref."\n";
77  migrate_user_filespath($u);
78  }
79  } else {
80  print "\n sql error ".$sql;
81  exit();
82  }
83 }
84 
85 $db->close(); // Close $db database opened handler
86 
87 exit($error);
88 
89 
96 function migrate_user_filespath($u)
97 {
98  global $conf;
99 
100  // Les fichiers joints des users sont toujours sur l'entité 1
101  $dir = $conf->user->dir_output;
102  $origin = $dir.'/'.get_exdir($u->id, 2, 0, 0, $u, 'user');
103  $destin = $dir.'/'.$u->id;
104 
105  $error = 0;
106 
107  $origin_osencoded = dol_osencode($origin);
108  $destin_osencoded = dol_osencode($destin);
109  dol_mkdir($destin);
110 
111  if (dol_is_dir($origin)) {
112  $handle = opendir($origin_osencoded);
113  if (is_resource($handle)) {
114  while (($file = readdir($handle)) !== false) {
115  if ($file != '.' && $file != '..' && is_dir($origin_osencoded.'/'.$file)) {
116  $thumbs = opendir($origin_osencoded.'/'.$file);
117  if (is_resource($thumbs)) {
118  dol_mkdir($destin.'/'.$file);
119  while (($thumb = readdir($thumbs)) !== false) {
120  dol_move($origin.'/'.$file.'/'.$thumb, $destin.'/'.$file.'/'.$thumb);
121  }
122  // dol_delete_dir($origin.'/'.$file);
123  }
124  } else {
125  if (dol_is_file($origin.'/'.$file)) {
126  dol_move($origin.'/'.$file, $destin.'/'.$file);
127  }
128  }
129  }
130  }
131  }
132 }
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
Class to manage Dolibarr users.
Definition: user.class.php:44
dol_getmypid()
Return getmypid() or random PID when function is disabled Some web hosts disable this php function fo...
dol_is_dir($folder)
Test if filename is a directory.
Definition: files.lib.php:432
dol_move($srcfile, $destfile, $newmask=0, $overwriteifexists=1, $testvirus=0, $indexdatabase=1)
Move a file into another name.
Definition: files.lib.php:817
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
get_exdir($num, $level, $alpha, $withoutslash, $object, $modulepart= '')
Return a path to have a the directory according to object where files are stored. ...
dol_is_file($pathoffile)
Return if path is a file.
Definition: files.lib.php:457
print
Draft customers invoices.
Definition: index.php:89
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_mkdir($dir, $dataroot= '', $newmask=null)
Creation of a directory (this can create recursive subdir)