import-users.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #!/usr/bin/env php
  2. <?php
  3. /* Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2016 Juanjo Menent <jmenent@2byte.es>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. *
  19. * WARNING, THIS WILL LOAD MASS DATA ON YOUR INSTANCE
  20. */
  21. /**
  22. * \file dev/initdata/import-users.php
  23. * \brief Script example to insert thirdparties from a csv file.
  24. * To purge data, you can have a look at purge-data.php
  25. */
  26. // Test si mode batch
  27. $sapi_type = php_sapi_name();
  28. $script_file = basename(__FILE__);
  29. $path=dirname(__FILE__).'/';
  30. if (substr($sapi_type, 0, 3) == 'cgi') {
  31. echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
  32. exit;
  33. }
  34. // Recupere root dolibarr
  35. $path=preg_replace('/import-users.php/i', '', $_SERVER["PHP_SELF"]);
  36. require $path."../../htdocs/master.inc.php";
  37. include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  38. include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  39. $delimiter=',';
  40. $enclosure='"';
  41. $linelength=10000;
  42. $escape='/';
  43. // Global variables
  44. $version=DOL_VERSION;
  45. $confirmed=1;
  46. $error=0;
  47. /*
  48. * Main
  49. */
  50. @set_time_limit(0);
  51. print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
  52. dol_syslog($script_file." launched with arg ".implode(',', $argv));
  53. $mode = $argv[1];
  54. $filepath = $argv[2];
  55. $filepatherr = $filepath.'.err';
  56. //$defaultlang = empty($argv[3])?'en_US':$argv[3];
  57. $startlinenb = empty($argv[3]) ? 1 : $argv[3];
  58. $endlinenb = empty($argv[4]) ? 0 : $argv[4];
  59. if (empty($mode) || ! in_array($mode, array('test','confirm','confirmforced')) || empty($filepath)) {
  60. print "Usage: $script_file (test|confirm|confirmforced) filepath.csv [startlinenb] [endlinenb]\n";
  61. print "Usage: $script_file test myfilepath.csv 2 1002\n";
  62. print "\n";
  63. exit(-1);
  64. }
  65. if (! file_exists($filepath)) {
  66. print "Error: File ".$filepath." not found.\n";
  67. print "\n";
  68. exit(-1);
  69. }
  70. $ret=$user->fetch('', 'admin');
  71. if (! $ret > 0) {
  72. print 'A user with login "admin" and all permissions must be created to use this script.'."\n";
  73. exit;
  74. }
  75. $user->getrights();
  76. // Ask confirmation
  77. if (! $confirmed) {
  78. print "Hit Enter to continue or CTRL+C to stop...\n";
  79. $input = trim(fgets(STDIN));
  80. }
  81. // Open input and output files
  82. $fhandle = fopen($filepath, 'r');
  83. if (! $fhandle) {
  84. print 'Error: Failed to open file '.$filepath."\n";
  85. exit(1);
  86. }
  87. $fhandleerr = fopen($filepatherr, 'w');
  88. if (! $fhandleerr) {
  89. print 'Error: Failed to open file '.$filepatherr."\n";
  90. exit(1);
  91. }
  92. //$langs->setDefaultLang($defaultlang);
  93. $db->begin();
  94. $i=0;
  95. $nboflines=0;
  96. while ($fields=fgetcsv($fhandle, $linelength, $delimiter, $enclosure, $escape)) {
  97. $i++;
  98. $errorrecord=0;
  99. if ($startlinenb && $i < $startlinenb) {
  100. continue;
  101. }
  102. if ($endlinenb && $i > $endlinenb) {
  103. continue;
  104. }
  105. $nboflines++;
  106. $object = new User($db);
  107. $object->statut = 1;
  108. $tmp=explode(' ', $fields[3], 2);
  109. $object->firstname = trim($tmp[0]);
  110. $object->lastname = trim($tmp[1]);
  111. if ($object->lastname) {
  112. $object->login = strtolower(substr($object->firstname, 0, 1)) . strtolower(substr($object->lastname, 0));
  113. } else {
  114. $object->login=strtolower($object->firstname);
  115. }
  116. $object->login=preg_replace('/ /', '', $object->login);
  117. $object->password = 'init';
  118. print "Process line nb ".$i.", login ".$object->login;
  119. $ret=$object->create($user);
  120. if ($ret < 0) {
  121. print " - Error in create result code = ".$ret." - ".$object->errorsToString();
  122. $errorrecord++;
  123. } else {
  124. print " - Creation OK with login ".$object->login." - id = ".$ret;
  125. }
  126. print "\n";
  127. if ($errorrecord) {
  128. fwrite($fhandleerr, 'Error on record nb '.$i." - ".$object->errorsToString()."\n");
  129. $error++; // $errorrecord will be reset
  130. }
  131. }
  132. // commit or rollback
  133. print "Nb of lines qualified: ".$nboflines."\n";
  134. print "Nb of errors: ".$error."\n";
  135. if ($mode != 'confirmforced' && ($error || $mode != 'confirm')) {
  136. print "Rollback any changes.\n";
  137. $db->rollback();
  138. } else {
  139. print "Commit all changes.\n";
  140. $db->commit();
  141. }
  142. $db->close();
  143. fclose($fhandle);
  144. fclose($fhandleerr);
  145. exit($error);