dolibarr 23.0.3
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 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
7 * Copyright (C) 2025 MDW <mdeweerd@users.noreply.github.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
29if (!defined('NOSESSION')) {
30 define('NOSESSION', '1');
31}
32
33$sapi_type = php_sapi_name();
34$script_file = basename(__FILE__);
35$path = __DIR__.'/';
36
37// Test if batch mode
38if (substr($sapi_type, 0, 3) == 'cgi') {
39 echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
40 exit(1);
41}
42
43@set_time_limit(0); // No timeout for this script
44define('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".
45
46// Include and load Dolibarr environment variables
47require_once $path."../../htdocs/master.inc.php";
48require_once DOL_DOCUMENT_ROOT.'/core/lib/functionscli.lib.php';
49require_once DOL_DOCUMENT_ROOT."/user/class/user.class.php";
50require_once DOL_DOCUMENT_ROOT."/core/lib/files.lib.php";
51// After this $db, $mysoc, $langs, $conf and $hookmanager are defined (Opened $db handler to database will be closed at end of file).
52// $user is created but empty.
59// $langs->setDefaultLang('en_US'); // To change default language of $langs
60$langs->load("main"); // To load language file for default language
61
62// Global variables
63$version = DOL_VERSION;
64$error = 0;
65$forcecommit = 0;
66
67$hookmanager->initHooks(array('cli'));
68
69
70/*
71 * Main
72 */
73
74print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
75dol_syslog($script_file." launched with arg ".implode(',', $argv));
76
77if (!isset($argv[1]) || $argv[1] != 'user') {
78 print "Usage: $script_file user\n";
79 exit(1);
80}
81
82print '--- start'."\n";
83
84// Case to migrate products path
85if ($argv[1] == 'user') {
86 $u = new User($db);
87
88 $sql = "SELECT rowid as uid from ".MAIN_DB_PREFIX."user"; // Get list of all products
89 $resql = $db->query($sql);
90 if ($resql) {
91 while ($obj = $db->fetch_object($resql)) {
92 $u->fetch($obj->uid);
93 print " migrating user id=".$u->id." ref=".$u->ref."\n";
95 }
96 } else {
97 print "\n sql error ".$sql;
98 exit();
99 }
100}
101
102$db->close(); // Close $db database opened handler
103
104exit($error);
105
106
114{
115 global $conf;
116
117 // Les fichiers implodets des users sont toujours sur l'entité 1
118 $dir = $conf->user->dir_output;
119 $origin = $dir.'/'.get_exdir($u->id, 2, 0, 0, $u, 'user');
120 $destin = $dir.'/'.$u->id;
121
122 $error = 0;
123
124 $origin_osencoded = dol_osencode($origin);
125 $destin_osencoded = dol_osencode($destin);
126 dol_mkdir($destin);
127
128 if (dol_is_dir($origin)) {
129 $handle = opendir($origin_osencoded);
130 if (is_resource($handle)) {
131 while (($file = readdir($handle)) !== false) {
132 if ($file != '.' && $file != '..' && is_dir($origin_osencoded.'/'.$file)) {
133 $thumbs = opendir($origin_osencoded.'/'.$file);
134 if (is_resource($thumbs)) {
135 dol_mkdir($destin.'/'.$file);
136 while (($thumb = readdir($thumbs)) !== false) {
137 dol_move($origin.'/'.$file.'/'.$thumb, $destin.'/'.$file.'/'.$thumb);
138 }
139 // dol_delete_dir($origin.'/'.$file);
140 }
141 } else {
142 if (dol_is_file($origin.'/'.$file)) {
143 dol_move($origin.'/'.$file, $destin.'/'.$file);
144 }
145 }
146 }
147 }
148 }
149}
Class to manage Dolibarr users.
dol_move($srcfile, $destfile, $newmask='0', $overwriteifexists=1, $testvirus=0, $indexdatabase=1, $moreinfo=array(), $entity=null)
Move a file into another name.
dol_is_file($pathoffile)
Return if path is a file.
dol_is_dir($folder)
Test if filename is a directory.
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
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)
migrate_user_filespath($u)
Migrate file from old path to new one for user $u.