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