dolibarr 19.0.3
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 *
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 * or see https://www.gnu.org/
20 *
21 * Lots of code inspired from Dan Potter's CSMSFile class
22 */
23
37{
41 public $db;
42
46 public $error = '';
47
51 public $errors = array();
52
56 public $eol;
57
58 public $addr_from;
59 public $addr_to;
60 public $deferred;
61 public $priority;
62 public $class;
63 public $message;
64 public $nostop;
65
66 public $socid;
67 public $contact_id;
68 public $member_id;
69
70 public $fk_project;
71
72 public $deliveryreceipt;
73
74
86 public function __construct($to, $from, $msg, $deliveryreceipt = 0, $deferred = 0, $priority = 3, $class = 1)
87 {
88 global $conf;
89
90 // On definit fin de ligne
91 $this->eol = "\n";
92 if (preg_match('/^win/i', PHP_OS)) {
93 $this->eol = "\r\n";
94 }
95 if (preg_match('/^mac/i', PHP_OS)) {
96 $this->eol = "\r";
97 }
98
99 // If ending method not defined
100 if (!getDolGlobalString('MAIN_SMS_SENDMODE')) {
101 $this->error = 'No SMS Engine defined';
102 throw new Exception('No SMS Engine defined');
103 }
104
105 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);
106 dol_syslog("CSMSFile::CSMSFile: deferred=".$deferred." priority=".$priority." class=".$class, LOG_DEBUG);
107
108 // Action according to choosed sending method
109 $this->addr_from = $from;
110 $this->addr_to = $to;
111 $this->deferred = $deferred;
112 $this->priority = $priority;
113 $this->class = $class;
114 $this->deliveryreceipt = $deliveryreceipt;
115 $this->message = $msg;
116 $this->nostop = false;
117 }
118
119
125 public function sendfile()
126 {
127 global $conf;
128
129 $errorlevel = error_reporting();
130 error_reporting($errorlevel ^ E_WARNING); // Desactive warnings
131
132 $res = false;
133
134 dol_syslog("CSMSFile::sendfile addr_to=".$this->addr_to, LOG_DEBUG);
135 dol_syslog("CSMSFile::sendfile message=\n".$this->message);
136
137 $this->message = stripslashes($this->message);
138
139 if (getDolGlobalString('MAIN_SMS_DEBUG')) {
140 $this->dump_sms();
141 }
142
143 if (!getDolGlobalString('MAIN_DISABLE_ALL_SMS')) {
144 // Action according to the choosed sending method
145 if (getDolGlobalString('MAIN_SMS_SENDMODE')) {
146 $sendmode = getDolGlobalString('MAIN_SMS_SENDMODE'); // $conf->global->MAIN_SMS_SENDMODE looks like a value 'module'
147 $classmoduleofsender = getDolGlobalString('MAIN_MODULE_'.strtoupper($sendmode).'_SMS', $sendmode); // $conf->global->MAIN_MODULE_XXX_SMS looks like a value 'class@module'
148 if ($classmoduleofsender == 'ovh') {
149 $classmoduleofsender = 'ovhsms@ovh'; // For backward compatibility
150 }
151
152 $tmp = explode('@', $classmoduleofsender);
153 $classfile = $tmp[0];
154 $module = (empty($tmp[1]) ? $tmp[0] : $tmp[1]);
155 dol_include_once('/'.$module.'/class/'.strtolower($classfile).'.class.php');
156 try {
157 $classname = ucfirst($classfile);
158 if (class_exists($classname)) {
159 $sms = new $classname($this->db);
160 $sms->expe = $this->addr_from;
161 $sms->dest = $this->addr_to;
162 $sms->deferred = $this->deferred;
163 $sms->priority = $this->priority;
164 $sms->class = $this->class;
165 $sms->message = $this->message;
166 $sms->nostop = $this->nostop;
167 $sms->deliveryreceipt = $this->deliveryreceipt;
168
169 $sms->socid = $this->socid;
170 $sms->contact_id = $this->contact_id;
171 $sms->member_id = $this->member_id;
172 $sms->fk_project = $this->fk_project;
173
174 $res = $sms->SmsSend();
175
176 $this->error = $sms->error;
177 $this->errors = $sms->errors;
178 if ($res <= 0) {
179 dol_syslog("CSMSFile::sendfile: sms send error=".$this->error, LOG_ERR);
180 } else {
181 dol_syslog("CSMSFile::sendfile: sms send success with id=".$res, LOG_DEBUG);
182 //var_dump($res); // 1973128
183 if (getDolGlobalString('MAIN_SMS_DEBUG')) {
184 $this->dump_sms_result($res);
185 }
186 }
187 } else {
188 $sms = new stdClass();
189 $sms->error = 'The SMS manager "'.$classfile.'" defined into SMS setup MAIN_MODULE_'.strtoupper($sendmode).'_SMS is not found';
190 }
191 } catch (Exception $e) {
192 dol_print_error('', 'Error to get list of senders: '.$e->getMessage());
193 }
194 } else {
195 // Send sms method not correctly defined
196 // --------------------------------------
197
198 return 'Bad value for MAIN_SMS_SENDMODE constant';
199 }
200 } else {
201 $this->error = 'No sms sent. Feature is disabled by option MAIN_DISABLE_ALL_SMS';
202 dol_syslog("CSMSFile::sendfile: ".$this->error, LOG_WARNING);
203 }
204
205 error_reporting($errorlevel); // Reactive niveau erreur origine
206
207 return $res;
208 }
209
210
211 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
218 public function dump_sms()
219 {
220 // phpcs:enable
221 global $conf, $dolibarr_main_data_root;
222
223 if (@is_writeable($dolibarr_main_data_root)) { // Avoid fatal error on fopen with open_basedir
224 $outputfile = $dolibarr_main_data_root."/dolibarr_sms.log";
225 $fp = fopen($outputfile, "w");
226
227 fputs($fp, "From: ".$this->addr_from."\n");
228 fputs($fp, "To: ".$this->addr_to."\n");
229 fputs($fp, "Priority: ".$this->priority."\n");
230 fputs($fp, "Class: ".$this->class."\n");
231 fputs($fp, "Deferred: ".$this->deferred."\n");
232 fputs($fp, "DisableStop: ".$this->nostop."\n");
233 fputs($fp, "DeliveryReceipt: ".$this->deliveryreceipt."\n");
234 fputs($fp, "Message:\n".$this->message);
235
236 fclose($fp);
237 dolChmod($outputfile);
238 }
239 }
240
241 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
249 public function dump_sms_result($result)
250 {
251 // phpcs:enable
252 global $conf, $dolibarr_main_data_root;
253
254 if (@is_writeable($dolibarr_main_data_root)) { // Avoid fatal error on fopen with open_basedir
255 $outputfile = $dolibarr_main_data_root."/dolibarr_sms.log";
256 $fp = fopen($outputfile, "a+");
257
258 fputs($fp, "\nResult id=".$result);
259
260 fclose($fp);
261 dolChmod($outputfile);
262 }
263 }
264}
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.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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.
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.