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