dolibarr 20.0.0
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;
68 public $deferred;
69 public $priority;
70 public $class;
71 public $message;
75 public $nostop;
76
77 public $socid;
78 public $contact_id;
79 public $member_id;
80
81 public $fk_project;
82
83 public $deliveryreceipt;
84
85
97 public function __construct($to, $from, $msg, $deliveryreceipt = 0, $deferred = 0, $priority = 3, $class = 1)
98 {
99 global $conf;
100
101 // Define the line ending (TODO: Why not use PHP_EOL?)
102 $this->eol = "\n";
103 if (preg_match('/^win/i', PHP_OS)) {
104 $this->eol = "\r\n";
105 }
106 if (preg_match('/^mac/i', PHP_OS)) {
107 $this->eol = "\r";
108 }
109
110 // If SMS sending method not defined
111 if (!getDolGlobalString('MAIN_SMS_SENDMODE')) {
112 $this->error = 'No SMS Engine defined';
113 throw new Exception('No SMS Engine defined');
114 }
115
116 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);
117 dol_syslog("CSMSFile::CSMSFile: deferred=".$deferred." priority=".$priority." class=".$class, LOG_DEBUG);
118
119 // Action according to chosen sending method
120 $this->addr_from = $from;
121 $this->addr_to = $to;
122 $this->deferred = $deferred;
123 $this->priority = $priority;
124 $this->class = $class;
125 $this->deliveryreceipt = $deliveryreceipt;
126 $this->message = $msg;
127 $this->nostop = false;
128 }
129
130
136 public function sendfile()
137 {
138 $errorlevel = error_reporting();
139 error_reporting($errorlevel ^ E_WARNING); // Disable warnings
140
141 $res = false;
142
143 dol_syslog("CSMSFile::sendfile addr_to=".$this->addr_to, LOG_DEBUG);
144 dol_syslog("CSMSFile::sendfile message=\n".$this->message);
145
146 $this->message = stripslashes($this->message);
147
148 if (getDolGlobalString('MAIN_SMS_DEBUG')) {
149 $this->dump_sms();
150 }
151
152 if (!getDolGlobalString('MAIN_DISABLE_ALL_SMS')) {
153 // Action according to the chose sending method
154 if (getDolGlobalString('MAIN_SMS_SENDMODE')) {
155 $sendmode = getDolGlobalString('MAIN_SMS_SENDMODE'); // $conf->global->MAIN_SMS_SENDMODE looks like a value 'module'
156 $classmoduleofsender = getDolGlobalString('MAIN_MODULE_'.strtoupper($sendmode).'_SMS', $sendmode); // $conf->global->MAIN_MODULE_XXX_SMS looks like a value 'class@module'
157 if ($classmoduleofsender == 'ovh') {
158 $classmoduleofsender = 'ovhsms@ovh'; // For backward compatibility
159 }
160
161 $tmp = explode('@', $classmoduleofsender);
162 $classfile = $tmp[0];
163 $module = (empty($tmp[1]) ? $tmp[0] : $tmp[1]);
164 dol_include_once('/'.$module.'/class/'.strtolower($classfile).'.class.php');
165 try {
166 $classname = ucfirst($classfile);
167
168 dol_syslog("CSMSFile::sendfile: try to include class ".$classname);
169
170 if (class_exists($classname)) {
171 $sms = new $classname($this->db);
172
173 $sms->expe = $this->addr_from;
174 $sms->dest = $this->addr_to;
175 $sms->deferred = $this->deferred;
176 $sms->priority = $this->priority;
177 $sms->class = $this->class;
178 $sms->message = $this->message;
179 $sms->nostop = $this->nostop;
180 $sms->deliveryreceipt = $this->deliveryreceipt;
181
182 $sms->socid = $this->socid;
183 $sms->contact_id = $this->contact_id;
184 $sms->member_id = $this->member_id;
185 $sms->fk_project = $this->fk_project;
186
187 $res = $sms->SmsSend();
188
189 $this->error = $sms->error;
190 $this->errors = $sms->errors;
191 if ($res <= 0) {
192 dol_syslog("CSMSFile::sendfile: sms send error=".$this->error, LOG_ERR);
193 if (getDolGlobalString('MAIN_SMS_DEBUG')) {
194 $this->dump_sms_result($res);
195 }
196 $res = false;
197 } else {
198 dol_syslog("CSMSFile::sendfile: sms send success with id=".$res, LOG_DEBUG);
199 //var_dump($res); // 1973128
200 if (getDolGlobalString('MAIN_SMS_DEBUG')) {
201 $this->dump_sms_result($res);
202 }
203 }
204 } else {
205 $this->error = 'The SMS manager "'.$classfile.'" defined into SMS setup MAIN_MODULE_'.strtoupper($sendmode).'_SMS is not found';
206 }
207 } catch (Exception $e) {
208 dol_print_error(null, 'Error to get list of senders: '.$e->getMessage());
209 }
210 } else {
211 // Send sms method not correctly defined
212 // --------------------------------------
213 $this->error = 'Bad value for MAIN_SMS_SENDMODE constant';
214 $res = false;
215 }
216 } else {
217 $this->error = 'No sms sent. Feature is disabled by option MAIN_DISABLE_ALL_SMS';
218 dol_syslog("CSMSFile::sendfile: ".$this->error, LOG_WARNING);
219 }
220
221 error_reporting($errorlevel); // Reactive niveau erreur origine
222
223 return $res;
224 }
225
226
227 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
234 public function dump_sms()
235 {
236 // phpcs:enable
237 global $conf, $dolibarr_main_data_root;
238
239 if (@is_writable($dolibarr_main_data_root)) { // Avoid fatal error on fopen with open_basedir
240 $outputfile = $dolibarr_main_data_root."/dolibarr_sms.log";
241 $fp = fopen($outputfile, "w");
242
243 fwrite($fp, "From: ".$this->addr_from."\n");
244 fwrite($fp, "To: ".$this->addr_to."\n");
245 fwrite($fp, "Priority: ".$this->priority."\n");
246 fwrite($fp, "Class: ".$this->class."\n");
247 fwrite($fp, "Deferred: ".$this->deferred."\n");
248 fwrite($fp, "DisableStop: ".((string) (int) $this->nostop)."\n");
249 fwrite($fp, "DeliveryReceipt: ".$this->deliveryreceipt."\n");
250 fwrite($fp, "Message:\n".$this->message);
251
252 fclose($fp);
253 dolChmod($outputfile);
254 }
255 }
256
257 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
265 public function dump_sms_result($result)
266 {
267 // phpcs:enable
268 global $dolibarr_main_data_root;
269
270 if (@is_writable($dolibarr_main_data_root)) { // Avoid fatal error on fopen with open_basedir
271 $outputfile = $dolibarr_main_data_root."/dolibarr_sms.log";
272 $fp = fopen($outputfile, "a+");
273
274 fwrite($fp, "\nResult of SmsSend = ".$result);
275
276 fclose($fp);
277 dolChmod($outputfile);
278 }
279 }
280}
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 dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.