dolibarr 21.0.0-beta
CSMSFile.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2000-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2003 Jean-Louis Bergamo <jlb@j1b.org>
4 * Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
5 * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
6 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 * or see https://www.gnu.org/
21 *
22 * Lots of code inspired from Dan Potter's CSMSFile class
23 */
24
38{
42 public $db;
43
47 public $error = '';
48
52 public $errors = array();
53
57 public $eol;
58
62 public $addr_from;
63
67 public $addr_to;
71 public $deferred;
75 public $priority;
79 public $class;
83 public $message;
87 public $nostop;
88
92 public $socid;
96 public $contact_id;
100 public $member_id;
101
105 public $fk_project;
106
110 public $deliveryreceipt;
111
112
124 public function __construct($to, $from, $msg, $deliveryreceipt = 0, $deferred = 0, $priority = 3, $class = 1)
125 {
126 global $conf;
127
128 // Define the line ending (TODO: Why not use PHP_EOL?)
129 $this->eol = "\n";
130 if (preg_match('/^win/i', PHP_OS)) {
131 $this->eol = "\r\n";
132 }
133 if (preg_match('/^mac/i', PHP_OS)) {
134 $this->eol = "\r";
135 }
136
137 // If SMS sending method not defined
138 if (!getDolGlobalString('MAIN_SMS_SENDMODE')) {
139 $this->error = 'No SMS Engine defined';
140 throw new Exception('No SMS Engine defined');
141 }
142
143 dol_syslog("CSMSFile::CSMSFile: MAIN_SMS_SENDMODE=".getDolGlobalString('MAIN_SMS_SENDMODE')." charset=".$conf->file->character_set_client." from=".$from.", to=".$to.", msg length=".strlen($msg), LOG_DEBUG);
144 dol_syslog("CSMSFile::CSMSFile: deferred=".$deferred." priority=".$priority." class=".$class, LOG_DEBUG);
145
146 // Action according to chosen sending method
147 $this->addr_from = $from;
148 $this->addr_to = $to;
149 $this->deferred = $deferred;
150 $this->priority = $priority;
151 $this->class = $class;
152 $this->deliveryreceipt = $deliveryreceipt;
153 $this->message = $msg;
154 $this->nostop = false;
155 }
156
157
163 public function sendfile()
164 {
165 $errorlevel = error_reporting();
166 error_reporting($errorlevel ^ E_WARNING); // Disable warnings
167
168 $res = false;
169
170 dol_syslog("CSMSFile::sendfile addr_to=".$this->addr_to, LOG_DEBUG);
171 dol_syslog("CSMSFile::sendfile message=\n".$this->message);
172
173 $this->message = stripslashes($this->message);
174
175 if (getDolGlobalString('MAIN_SMS_DEBUG')) {
176 $this->dump_sms();
177 }
178
179 if (!getDolGlobalString('MAIN_DISABLE_ALL_SMS')) {
180 // Action according to the chose sending method
181 if (getDolGlobalString('MAIN_SMS_SENDMODE')) {
182 $sendmode = getDolGlobalString('MAIN_SMS_SENDMODE'); // $conf->global->MAIN_SMS_SENDMODE looks like a value 'module'
183 $classmoduleofsender = getDolGlobalString('MAIN_MODULE_'.strtoupper($sendmode).'_SMS', $sendmode); // $conf->global->MAIN_MODULE_XXX_SMS looks like a value 'class@module'
184 if ($classmoduleofsender == 'ovh') {
185 $classmoduleofsender = 'ovhsms@ovh'; // For backward compatibility
186 }
187
188 $tmp = explode('@', $classmoduleofsender);
189 $classfile = $tmp[0];
190 $module = (empty($tmp[1]) ? $tmp[0] : $tmp[1]);
191 dol_include_once('/'.$module.'/class/'.strtolower($classfile).'.class.php');
192 try {
193 $classname = ucfirst($classfile);
194
195 dol_syslog("CSMSFile::sendfile: try to include class ".$classname);
196
197 if (class_exists($classname)) {
198 $sms = new $classname($this->db);
199 '@phan-var-force OvhSms $sms'; // Using original for analysis
200
201 $sms->expe = $this->addr_from;
202 $sms->dest = $this->addr_to;
203 $sms->deferred = $this->deferred;
204 $sms->priority = $this->priority;
205 $sms->class = $this->class;
206 $sms->message = $this->message;
207 $sms->nostop = $this->nostop;
208 $sms->deliveryreceipt = $this->deliveryreceipt;
209
210 $sms->socid = $this->socid;
211 $sms->contact_id = $this->contact_id;
212 $sms->member_id = $this->member_id;
213 $sms->fk_project = $this->fk_project;
214
215 $res = $sms->SmsSend();
216
217 $this->error = $sms->error;
218 $this->errors = $sms->errors;
219 if ($res <= 0) {
220 dol_syslog("CSMSFile::sendfile: sms send error=".$this->error, LOG_ERR);
221 if (getDolGlobalString('MAIN_SMS_DEBUG')) {
222 $this->dump_sms_result($res);
223 }
224 $res = false;
225 } else {
226 dol_syslog("CSMSFile::sendfile: sms send success with id=".$res, LOG_DEBUG);
227 //var_dump($res); // 1973128
228 if (getDolGlobalString('MAIN_SMS_DEBUG')) {
229 $this->dump_sms_result($res);
230 }
231 }
232 } else {
233 $this->error = 'The SMS manager "'.$classfile.'" defined into SMS setup MAIN_MODULE_'.strtoupper($sendmode).'_SMS is not found';
234 }
235 } catch (Exception $e) {
236 dol_print_error(null, 'Error to get list of senders: '.$e->getMessage());
237 }
238 } else {
239 // Send sms method not correctly defined
240 // --------------------------------------
241 $this->error = 'Bad value for MAIN_SMS_SENDMODE constant';
242 $res = false;
243 }
244 } else {
245 $this->error = 'No sms sent. Feature is disabled by option MAIN_DISABLE_ALL_SMS';
246 dol_syslog("CSMSFile::sendfile: ".$this->error, LOG_WARNING);
247 }
248
249 error_reporting($errorlevel); // Reactive niveau erreur origine
250
251 return $res;
252 }
253
254
255 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
262 public function dump_sms()
263 {
264 // phpcs:enable
265 global $conf, $dolibarr_main_data_root;
266
267 if (@is_writable($dolibarr_main_data_root)) { // Avoid fatal error on fopen with open_basedir
268 $outputfile = $dolibarr_main_data_root."/dolibarr_sms.log";
269 $fp = fopen($outputfile, "w");
270
271 fwrite($fp, "From: ".$this->addr_from."\n");
272 fwrite($fp, "To: ".$this->addr_to."\n");
273 fwrite($fp, "Priority: ".$this->priority."\n");
274 fwrite($fp, "Class: ".$this->class."\n");
275 fwrite($fp, "Deferred: ".$this->deferred."\n");
276 fwrite($fp, "DisableStop: ".((string) (int) $this->nostop)."\n");
277 fwrite($fp, "DeliveryReceipt: ".$this->deliveryreceipt."\n");
278 fwrite($fp, "Message:\n".$this->message);
279
280 fclose($fp);
281 dolChmod($outputfile);
282 }
283 }
284
285 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
293 public function dump_sms_result($result)
294 {
295 // phpcs:enable
296 global $dolibarr_main_data_root;
297
298 if (@is_writable($dolibarr_main_data_root)) { // Avoid fatal error on fopen with open_basedir
299 $outputfile = $dolibarr_main_data_root."/dolibarr_sms.log";
300 $fp = fopen($outputfile, "a+");
301
302 fwrite($fp, "\nResult of SmsSend = ".$result);
303
304 fclose($fp);
305 dolChmod($outputfile);
306 }
307 }
308}
Class to send SMS Usage: $smsfile = new CSMSFile($subject,$sendto,$replyto,$message,...
dump_sms_result($result)
Write content of a SendSms result into a dump file (mode = all) Used for debugging.
sendfile()
Send SMS that was prepared by constructor.
dump_sms()
Write content of a SendSms request into a dump file (mode = all) Used for debugging.
__construct($to, $from, $msg, $deliveryreceipt=0, $deferred=0, $priority=3, $class=1)
CSMSFile.
dolChmod($filepath, $newmask='')
Change mod of a file.
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79