mymodule.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #!/usr/bin/env php
  2. <?php
  3. /* Copyright (C) 2007-2023 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) ---Put here your own copyright and developer email---
  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. /**
  20. * \file htdocs/modulebuilder/template/scripts/mymodule.php
  21. * \ingroup mymodule
  22. * \brief This file is a command line script for module MyModule. You can execute it with:
  23. * php mymodule/scripts/mymodule.php
  24. */
  25. //if (! defined('NOREQUIREDB')) define('NOREQUIREDB', '1'); // Do not create database handler $db
  26. //if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER', '1'); // Do not load object $user
  27. //if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC', '1'); // Do not load object $mysoc
  28. //if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN', '1'); // Do not load object $langs
  29. //if (! defined('NOSCANGETFORINJECTION')) define('NOSCANGETFORINJECTION', '1'); // Do not check injection attack on GET parameters
  30. //if (! defined('NOSCANPOSTFORINJECTION')) define('NOSCANPOSTFORINJECTION', '1'); // Do not check injection attack on POST parameters
  31. //if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL', '1'); // Do not roll the Anti CSRF token (used if MAIN_SECURITY_CSRF_WITH_TOKEN is on)
  32. //if (! defined('NOSTYLECHECK')) define('NOSTYLECHECK', '1'); // Do not check style html tag into posted data
  33. //if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU', '1'); // If there is no need to load and show top and left menu
  34. //if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
  35. //if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX', '1'); // Do not load ajax.lib.php library
  36. //if (! defined("NOLOGIN")) define("NOLOGIN", '1'); // If this page is public (can be called outside logged session). This include the NOIPCHECK too.
  37. //if (! defined('NOIPCHECK')) define('NOIPCHECK', '1'); // Do not check IP defined into conf $dolibarr_main_restrict_ip
  38. //if (! defined("MAIN_LANG_DEFAULT")) define('MAIN_LANG_DEFAULT', 'auto'); // Force lang to a particular value
  39. //if (! defined("MAIN_AUTHENTICATION_MODE")) define('MAIN_AUTHENTICATION_MODE', 'aloginmodule'); // Force authentication handler
  40. //if (! defined('CSRFCHECK_WITH_TOKEN')) define('CSRFCHECK_WITH_TOKEN', '1'); // Force use of CSRF protection with tokens even for GET
  41. //if (! defined('NOBROWSERNOTIF')) define('NOBROWSERNOTIF', '1'); // Disable browser notification
  42. if (!defined('NOSESSION')) {
  43. define('NOSESSION', '1');
  44. } // On CLI mode, no need to use web sessions
  45. $sapi_type = php_sapi_name();
  46. $script_file = basename(__FILE__);
  47. $path = __DIR__.'/';
  48. // Test if batch mode
  49. if (substr($sapi_type, 0, 3) == 'cgi') {
  50. echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
  51. exit(-1);
  52. }
  53. // Global variables
  54. $version = '1.0';
  55. $error = 0;
  56. // -------------------- START OF YOUR CODE HERE --------------------
  57. @set_time_limit(0); // No timeout for this script
  58. 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".
  59. // Load Dolibarr environment
  60. $res = 0;
  61. // Try master.inc.php into web root detected using web root calculated from SCRIPT_FILENAME
  62. $tmp = empty($_SERVER['SCRIPT_FILENAME']) ? '' : $_SERVER['SCRIPT_FILENAME']; $tmp2 = realpath(__FILE__); $i = strlen($tmp) - 1; $j = strlen($tmp2) - 1;
  63. while ($i > 0 && $j > 0 && isset($tmp[$i]) && isset($tmp2[$j]) && $tmp[$i] == $tmp2[$j]) {
  64. $i--;
  65. $j--;
  66. }
  67. if (!$res && $i > 0 && file_exists(substr($tmp, 0, ($i + 1))."/master.inc.php")) {
  68. $res = @include substr($tmp, 0, ($i + 1))."/master.inc.php";
  69. }
  70. if (!$res && $i > 0 && file_exists(dirname(substr($tmp, 0, ($i + 1)))."/master.inc.php")) {
  71. $res = @include dirname(substr($tmp, 0, ($i + 1)))."/master.inc.php";
  72. }
  73. // Try master.inc.php using relative path
  74. if (!$res && file_exists("../master.inc.php")) {
  75. $res = @include "../master.inc.php";
  76. }
  77. if (!$res && file_exists("../../master.inc.php")) {
  78. $res = @include "../../master.inc.php";
  79. }
  80. if (!$res && file_exists("../../../master.inc.php")) {
  81. $res = @include "../../../master.inc.php";
  82. }
  83. if (!$res) {
  84. print "Include of master fails";
  85. exit(-1);
  86. }
  87. // After this $db, $mysoc, $langs, $conf and $hookmanager are defined (Opened $db handler to database will be closed at end of file).
  88. // $user is created but empty.
  89. //$langs->setDefaultLang('en_US'); // To change default language of $langs
  90. $langs->load("main"); // To load language file for default language
  91. // Load user and its permissions
  92. $result = $user->fetch('', 'admin'); // Load user for login 'admin'. Comment line to run as anonymous user.
  93. if (!($result > 0)) {
  94. dol_print_error('', $user->error);
  95. exit;
  96. }
  97. $user->getrights();
  98. print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
  99. if (!isset($argv[1])) { // Check parameters
  100. print "Usage: ".$script_file." param1 param2 ...\n";
  101. exit(-1);
  102. }
  103. print '--- start'."\n";
  104. print 'Argument 1='.$argv[1]."\n";
  105. print 'Argument 2='.$argv[2]."\n";
  106. // Start of transaction
  107. $db->begin();
  108. // Examples for manipulating class MyObject
  109. //dol_include_once("/mymodule/class/myobject.class.php");
  110. //$myobject=new MyObject($db);
  111. // Example for inserting creating object in database
  112. /*
  113. dol_syslog($script_file." CREATE", LOG_DEBUG);
  114. $myobject->prop1='value_prop1';
  115. $myobject->prop2='value_prop2';
  116. $id=$myobject->create($user);
  117. if ($id < 0) { $error++; dol_print_error($db,$myobject->error); }
  118. else print "Object created with id=".$id."\n";
  119. */
  120. // Example for reading object from database
  121. /*
  122. dol_syslog($script_file." FETCH", LOG_DEBUG);
  123. $result=$myobject->fetch($id);
  124. if ($result < 0) { $error; dol_print_error($db,$myobject->error); }
  125. else print "Object with id=".$id." loaded\n";
  126. */
  127. // Example for updating object in database ($myobject must have been loaded by a fetch before)
  128. /*
  129. dol_syslog($script_file." UPDATE", LOG_DEBUG);
  130. $myobject->prop1='newvalue_prop1';
  131. $myobject->prop2='newvalue_prop2';
  132. $result=$myobject->update($user);
  133. if ($result < 0) { $error++; dol_print_error($db,$myobject->error); }
  134. else print "Object with id ".$myobject->id." updated\n";
  135. */
  136. // Example for deleting object in database ($myobject must have been loaded by a fetch before)
  137. /*
  138. dol_syslog($script_file." DELETE", LOG_DEBUG);
  139. $result=$myobject->delete($user);
  140. if ($result < 0) { $error++; dol_print_error($db,$myobject->error); }
  141. else print "Object with id ".$myobject->id." deleted\n";
  142. */
  143. // An example of a direct SQL read without using the fetch method
  144. /*
  145. $sql = "SELECT field1, field2";
  146. $sql.= " FROM ".MAIN_DB_PREFIX."myobject";
  147. $sql.= " WHERE field3 = 'xxx'";
  148. $sql.= " ORDER BY field1 ASC";
  149. dol_syslog($script_file, LOG_DEBUG);
  150. $resql=$db->query($sql);
  151. if ($resql)
  152. {
  153. $num = $db->num_rows($resql);
  154. $i = 0;
  155. if ($num)
  156. {
  157. while ($i < $num)
  158. {
  159. $obj = $db->fetch_object($resql);
  160. if ($obj)
  161. {
  162. // You can use here results
  163. print $obj->field1;
  164. print $obj->field2;
  165. }
  166. $i++;
  167. }
  168. }
  169. }
  170. else
  171. {
  172. $error++;
  173. dol_print_error($db);
  174. }
  175. */
  176. // -------------------- END OF YOUR CODE --------------------
  177. if (!$error) {
  178. $db->commit();
  179. print '--- end ok'."\n";
  180. } else {
  181. print '--- end error code='.$error."\n";
  182. $db->rollback();
  183. }
  184. $db->close(); // Close $db database opened handler
  185. exit($error);