migrate_picture_path.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. /**
  21. * \file scripts/user/migrate_picture_path.php
  22. * \ingroup scripts
  23. * \brief Migrate pictures from old system prior to 3.7 to new path for 3.7+
  24. */
  25. if (!defined('NOSESSION')) {
  26. define('NOSESSION', '1');
  27. }
  28. $sapi_type = php_sapi_name();
  29. $script_file = basename(__FILE__);
  30. $path = __DIR__.'/';
  31. // Test if batch mode
  32. if (substr($sapi_type, 0, 3) == 'cgi') {
  33. echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
  34. exit(-1);
  35. }
  36. @set_time_limit(0); // No timeout for this script
  37. 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".
  38. // Include and load Dolibarr environment variables
  39. require_once $path."../../htdocs/master.inc.php";
  40. require_once DOL_DOCUMENT_ROOT."/user/class/user.class.php";
  41. require_once DOL_DOCUMENT_ROOT."/core/lib/files.lib.php";
  42. // After this $db, $mysoc, $langs, $conf and $hookmanager are defined (Opened $db handler to database will be closed at end of file).
  43. // $user is created but empty.
  44. // $langs->setDefaultLang('en_US'); // To change default language of $langs
  45. $langs->load("main"); // To load language file for default language
  46. // Global variables
  47. $version = DOL_VERSION;
  48. $error = 0;
  49. $forcecommit = 0;
  50. print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
  51. dol_syslog($script_file." launched with arg ".join(',', $argv));
  52. if (!isset($argv[1]) || $argv[1] != 'user') {
  53. print "Usage: $script_file user\n";
  54. exit(-1);
  55. }
  56. print '--- start'."\n";
  57. // Case to migrate products path
  58. if ($argv[1] == 'user') {
  59. $u = new User($db);
  60. $sql = "SELECT rowid as uid from ".MAIN_DB_PREFIX."user"; // Get list of all products
  61. $resql = $db->query($sql);
  62. if ($resql) {
  63. while ($obj = $db->fetch_object($resql)) {
  64. $u->fetch($obj->uid);
  65. print " migrating user id=".$u->id." ref=".$u->ref."\n";
  66. migrate_user_filespath($u);
  67. }
  68. } else {
  69. print "\n sql error ".$sql;
  70. exit();
  71. }
  72. }
  73. $db->close(); // Close $db database opened handler
  74. exit($error);
  75. /**
  76. * Migrate file from old path to new one for user $u
  77. *
  78. * @param User $u Object user
  79. * @return void
  80. */
  81. function migrate_user_filespath($u)
  82. {
  83. global $conf;
  84. // Les fichiers joints des users sont toujours sur l'entité 1
  85. $dir = $conf->user->dir_output;
  86. $origin = $dir.'/'.get_exdir($u->id, 2, 0, 0, $u, 'user');
  87. $destin = $dir.'/'.$u->id;
  88. $error = 0;
  89. $origin_osencoded = dol_osencode($origin);
  90. $destin_osencoded = dol_osencode($destin);
  91. dol_mkdir($destin);
  92. if (dol_is_dir($origin)) {
  93. $handle = opendir($origin_osencoded);
  94. if (is_resource($handle)) {
  95. while (($file = readdir($handle)) !== false) {
  96. if ($file != '.' && $file != '..' && is_dir($origin_osencoded.'/'.$file)) {
  97. $thumbs = opendir($origin_osencoded.'/'.$file);
  98. if (is_resource($thumbs)) {
  99. dol_mkdir($destin.'/'.$file);
  100. while (($thumb = readdir($thumbs)) !== false) {
  101. dol_move($origin.'/'.$file.'/'.$thumb, $destin.'/'.$file.'/'.$thumb);
  102. }
  103. // dol_delete_dir($origin.'/'.$file);
  104. }
  105. } else {
  106. if (dol_is_file($origin.'/'.$file)) {
  107. dol_move($origin.'/'.$file, $destin.'/'.$file);
  108. }
  109. }
  110. }
  111. }
  112. }
  113. }