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