dolibarr 25.0.0-alpha
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) 2024 Frédéric France <frederic.france@free.fr>
5 * Copyright (C) ---Replace with your own copyright and developer email---
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
28//if (! defined('NOREQUIREDB')) define('NOREQUIREDB', '1'); // Do not create database handler $db
29//if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER', '1'); // Do not load object $user
30//if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC', '1'); // Do not load object $mysoc
31//if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN', '1'); // Do not load object $langs
32//if (! defined('NOSCANGETFORINJECTION')) define('NOSCANGETFORINJECTION', '1'); // Do not check injection attack on GET parameters
33//if (! defined('NOSCANPOSTFORINJECTION')) define('NOSCANPOSTFORINJECTION', '1'); // Do not check injection attack on POST parameters
34//if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL', '1'); // Do not roll the Anti CSRF token (used if MAIN_SECURITY_CSRF_WITH_TOKEN is on)
35//if (! defined('NOSTYLECHECK')) define('NOSTYLECHECK', '1'); // Do not check style html tag into posted data
36//if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU', '1'); // If there is no need to load and show top and left menu
37//if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
38//if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX', '1'); // Do not load ajax.lib.php library
39//if (! defined("NOLOGIN")) define("NOLOGIN", '1'); // If this page is public (can be called outside logged session). This include the NOIPCHECK too.
40//if (! defined('NOIPCHECK')) define('NOIPCHECK', '1'); // Do not check IP defined into conf $dolibarr_main_restrict_ip
41//if (! defined("MAIN_LANG_DEFAULT")) define('MAIN_LANG_DEFAULT', 'auto'); // Force lang to a particular value
42//if (! defined("MAIN_AUTHENTICATION_MODE")) define('MAIN_AUTHENTICATION_MODE', 'aloginmodule'); // Force authentication handler
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
45if (!defined('NOSESSION')) {
46 define('NOSESSION', '1');
47} // On CLI mode, no need to use web sessions
48
49
50$sapi_type = php_sapi_name();
51$script_file = basename(__FILE__);
52$path = __DIR__.'/';
53
54// Test if batch mode
55if (substr($sapi_type, 0, 3) == 'cgi') { // @phan-suppress-current-line DolibarrForbiddenFunctionPlugin
56 echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
57 exit(1);
58}
59
60// Global variables
61$version = '1.0';
62$error = 0;
63
64
65// -------------------- START OF YOUR CODE HERE --------------------
66@set_time_limit(0); // No timeout for this script
67define('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".
68
69// Load Dolibarr environment
70$res = 0;
71// Try master.inc.php into web root detected using web root calculated from SCRIPT_FILENAME
72$tmp = empty($_SERVER['SCRIPT_FILENAME']) ? '' : $_SERVER['SCRIPT_FILENAME'];
73$tmp2 = realpath(__FILE__);
74$i = strlen($tmp) - 1; // @phan-suppress-current-line DolibarrForbiddenFunctionPlugin
75$j = strlen($tmp2) - 1; // @phan-suppress-current-line DolibarrForbiddenFunctionPlugin
76while ($i > 0 && $j > 0 && isset($tmp[$i]) && isset($tmp2[$j]) && $tmp[$i] == $tmp2[$j]) {
77 $i--;
78 $j--;
79}
80if (!$res && $i > 0 && file_exists(substr($tmp, 0, ($i + 1))."/master.inc.php")) {
81 $res = @include substr($tmp, 0, ($i + 1))."/master.inc.php"; // @phan-suppress-current-line DolibarrForbiddenFunctionPlugin
82}
83if (!$res && $i > 0 && file_exists(dirname(substr($tmp, 0, ($i + 1)))."/master.inc.php")) { // @phan-suppress-current-line DolibarrForbiddenFunctionPlugin
84 $res = @include dirname(substr($tmp, 0, ($i + 1)))."/master.inc.php"; // @phan-suppress-current-line DolibarrForbiddenFunctionPlugin
85}
86// Try master.inc.php using relative path
87if (!$res && file_exists("../master.inc.php")) {
88 $res = @include "../master.inc.php";
89}
90if (!$res && file_exists("../../master.inc.php")) {
91 $res = @include "../../master.inc.php";
92}
93if (!$res && file_exists("../../../master.inc.php")) {
94 $res = @include "../../../master.inc.php";
95}
96if (!$res) {
97 print "Include of master fails";
98 exit(1);
99}
100// After this $db, $mysoc, $langs, $conf and $hookmanager are defined (Opened $db handler to database will be closed at end of file).
101// $user is created but empty.
102
111require_once DOL_DOCUMENT_ROOT . '/core/lib/functions.lib.php';
112//$langs->setDefaultLang('en_US'); // To change default language of $langs
113$langs->load("main"); // To load language file for default language
114
115// Load user and its permissions
116$result = $user->fetch(0, 'admin'); // Load user for login 'admin'. Comment line to run as anonymous user.
117if (!($result > 0)) {
118 dol_print_error(null, $user->error);
119 exit;
120}
121$user->loadRights();
122
123$hookmanager->initHooks(array('cli'));
124
125
126/*
127 * Main
128 */
129
130print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
131if (!isset($argv[1])) { // Check parameters
132 print "Usage: ".$script_file." param1 param2 ...\n";
133 exit(1);
134}
135print '--- start'."\n";
136print 'Argument 1='.$argv[1]."\n";
137print 'Argument 2='.$argv[2]."\n";
138
139
140// Start of transaction
141$db->begin();
142
143
144// Examples for manipulating class MyObject
145//dol_include_once("/mymodule/class/myobject.class.php");
146//$myobject=new MyObject($db);
147
148// Example for inserting creating object in database
149/*
150dol_syslog($script_file." CREATE", LOG_DEBUG);
151$myobject->prop1='value_prop1';
152$myobject->prop2='value_prop2';
153$id=$myobject->create($user);
154if ($id < 0) { $error++; dol_print_error($db,$myobject->error); }
155else print "Object created with id=".$id."\n";
156*/
157
158// Example for reading object from database
159/*
160dol_syslog($script_file." FETCH", LOG_DEBUG);
161$result=$myobject->fetch($id);
162if ($result < 0) { $error; dol_print_error($db,$myobject->error); }
163else print "Object with id=".$id." loaded\n";
164*/
165
166// Example for updating object in database ($myobject must have been loaded by a fetch before)
167/*
168dol_syslog($script_file." UPDATE", LOG_DEBUG);
169$myobject->prop1='newvalue_prop1';
170$myobject->prop2='newvalue_prop2';
171$result=$myobject->update($user);
172if ($result < 0) { $error++; dol_print_error($db,$myobject->error); }
173else print "Object with id ".$myobject->id." updated\n";
174*/
175
176// Example for deleting object in database ($myobject must have been loaded by a fetch before)
177/*
178dol_syslog($script_file." DELETE", LOG_DEBUG);
179$result=$myobject->delete($user);
180if ($result < 0) { $error++; dol_print_error($db,$myobject->error); }
181else print "Object with id ".$myobject->id." deleted\n";
182*/
183
184
185// An example of a direct SQL read without using the fetch method
186/*
187$sql = "SELECT field1, field2";
188$sql.= " FROM ".$db->prefix()."myobject";
189$sql.= " WHERE field3 = 'xxx'";
190$sql.= " ORDER BY field1 ASC";
191
192dol_syslog($script_file, LOG_DEBUG);
193$resql=$db->query($sql);
194if ($resql)
195{
196 $num = $db->num_rows($resql);
197 $i = 0;
198 if ($num)
199 {
200 while ($i < $num)
201 {
202 $obj = $db->fetch_object($resql);
203 if ($obj)
204 {
205 // You can use here results
206 print $obj->field1;
207 print $obj->field2;
208 }
209 $i++;
210 }
211 }
212}
213else
214{
215 $error++;
216 dol_print_error($db);
217}
218*/
219
220
221// -------------------- END OF YOUR CODE --------------------
222
223if (!$error) {
224 $db->commit();
225 print '--- end ok'."\n";
226} else {
227 print '--- end error code='.$error."\n";
228 $db->rollback();
229}
230
231$db->close(); // Close $db database opened handler
232
233exit($error);