dolibarr  19.0.0-dev
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 
36 class CSMSFile
37 {
41  public $error = '';
42 
43  public $addr_from;
44  public $addr_to;
45  public $deferred;
46  public $priority;
47  public $class;
48  public $message;
49  public $nostop;
50 
51  public $socid;
52  public $contact_id;
53  public $member_id;
54 
55  public $fk_project;
56 
57 
69  public function __construct($to, $from, $msg, $deliveryreceipt = 0, $deferred = 0, $priority = 3, $class = 1)
70  {
71  global $conf;
72 
73  // On definit fin de ligne
74  $this->eol = "\n";
75  if (preg_match('/^win/i', PHP_OS)) {
76  $this->eol = "\r\n";
77  }
78  if (preg_match('/^mac/i', PHP_OS)) {
79  $this->eol = "\r";
80  }
81 
82  // If ending method not defined
83  if (empty($conf->global->MAIN_SMS_SENDMODE)) {
84  $this->error = 'No SMS Engine defined';
85  throw new Exception('No SMS Engine defined');
86  }
87 
88  dol_syslog("CSMSFile::CSMSFile: MAIN_SMS_SENDMODE=".$conf->global->MAIN_SMS_SENDMODE." charset=".$conf->file->character_set_client." from=".$from.", to=".$to.", msg length=".strlen($msg), LOG_DEBUG);
89  dol_syslog("CSMSFile::CSMSFile: deferred=".$deferred." priority=".$priority." class=".$class, LOG_DEBUG);
90 
91  // Action according to choosed sending method
92  $this->addr_from = $from;
93  $this->addr_to = $to;
94  $this->deferred = $deferred;
95  $this->priority = $priority;
96  $this->class = $class;
97  $this->message = $msg;
98  $this->nostop = false;
99  }
100 
101 
107  public function sendfile()
108  {
109  global $conf;
110 
111  $errorlevel = error_reporting();
112  error_reporting($errorlevel ^ E_WARNING); // Desactive warnings
113 
114  $res = false;
115 
116  dol_syslog("CSMSFile::sendfile addr_to=".$this->addr_to, LOG_DEBUG);
117  dol_syslog("CSMSFile::sendfile message=\n".$this->message);
118 
119  $this->message = stripslashes($this->message);
120 
121  if (!empty($conf->global->MAIN_SMS_DEBUG)) {
122  $this->dump_sms();
123  }
124 
125  if (empty($conf->global->MAIN_DISABLE_ALL_SMS)) {
126  // Action according to choosed sending method
127  if ($conf->global->MAIN_SMS_SENDMODE == 'ovh') { // Backward compatibility @deprecated
128  dol_include_once('/ovh/class/ovhsms.class.php');
129  $sms = new OvhSms($this->db);
130  $sms->expe = $this->addr_from;
131  $sms->dest = $this->addr_to;
132  $sms->message = $this->message;
133  $sms->deferred = $this->deferred;
134  $sms->priority = $this->priority;
135  $sms->class = $this->class;
136  $sms->nostop = $this->nostop;
137 
138  $sms->socid = $this->socid;
139  $sms->contact_id = $this->contact_id;
140  $sms->member_id = $this->member_id;
141  $sms->project = $this->fk_project;
142 
143  $res = $sms->SmsSend();
144 
145  if ($res <= 0) {
146  $this->error = $sms->error;
147  dol_syslog("CSMSFile::sendfile: sms send error=".$this->error, LOG_ERR);
148  } else {
149  dol_syslog("CSMSFile::sendfile: sms send success with id=".$res, LOG_DEBUG);
150  //var_dump($res); // 1973128
151  if (!empty($conf->global->MAIN_SMS_DEBUG)) {
152  $this->dump_sms_result($res);
153  }
154  }
155  } elseif (!empty($conf->global->MAIN_SMS_SENDMODE)) { // $conf->global->MAIN_SMS_SENDMODE looks like a value 'class@module'
156  $tmp = explode('@', $conf->global->MAIN_SMS_SENDMODE);
157  $classfile = $tmp[0];
158  $module = (empty($tmp[1]) ? $tmp[0] : $tmp[1]);
159  dol_include_once('/'.$module.'/class/'.$classfile.'.class.php');
160  try {
161  $classname = ucfirst($classfile);
162  $sms = new $classname($this->db);
163  $sms->expe = $this->addr_from;
164  $sms->dest = $this->addr_to;
165  $sms->deferred = $this->deferred;
166  $sms->priority = $this->priority;
167  $sms->class = $this->class;
168  $sms->message = $this->message;
169  $sms->nostop = $this->nostop;
170 
171  $sms->socid = $this->socid;
172  $sms->contact_id = $this->contact_id;
173  $sms->member_id = $this->member_id;
174  $sms->fk_project = $this->fk_project;
175 
176  $res = $sms->SmsSend();
177 
178  $this->error = $sms->error;
179  $this->errors = $sms->errors;
180  if ($res <= 0) {
181  dol_syslog("CSMSFile::sendfile: sms send error=".$this->error, LOG_ERR);
182  } else {
183  dol_syslog("CSMSFile::sendfile: sms send success with id=".$res, LOG_DEBUG);
184  //var_dump($res); // 1973128
185  if (!empty($conf->global->MAIN_SMS_DEBUG)) {
186  $this->dump_sms_result($res);
187  }
188  }
189  } catch (Exception $e) {
190  dol_print_error('', 'Error to get list of senders: '.$e->getMessage());
191  }
192  } else {
193  // Send sms method not correctly defined
194  // --------------------------------------
195 
196  return 'Bad value for MAIN_SMS_SENDMODE constant';
197  }
198  } else {
199  $this->error = 'No sms sent. Feature is disabled by option MAIN_DISABLE_ALL_SMS';
200  dol_syslog("CSMSFile::sendfile: ".$this->error, LOG_WARNING);
201  }
202 
203  error_reporting($errorlevel); // Reactive niveau erreur origine
204 
205  return $res;
206  }
207 
208 
209  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
216  public function dump_sms()
217  {
218  // phpcs:enable
219  global $conf, $dolibarr_main_data_root;
220 
221  if (@is_writeable($dolibarr_main_data_root)) { // Avoid fatal error on fopen with open_basedir
222  $outputfile = $dolibarr_main_data_root."/dolibarr_sms.log";
223  $fp = fopen($outputfile, "w");
224 
225  fputs($fp, "From: ".$this->addr_from."\n");
226  fputs($fp, "To: ".$this->addr_to."\n");
227  fputs($fp, "Priority: ".$this->priority."\n");
228  fputs($fp, "Class: ".$this->class."\n");
229  fputs($fp, "Deferred: ".$this->deferred."\n");
230  fputs($fp, "DisableStop: ".$this->nostop."\n");
231  fputs($fp, "Message:\n".$this->message);
232 
233  fclose($fp);
234  dolChmod($outputfile);
235  }
236  }
237 
238  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
246  public function dump_sms_result($result)
247  {
248  // phpcs:enable
249  global $conf, $dolibarr_main_data_root;
250 
251  if (@is_writeable($dolibarr_main_data_root)) { // Avoid fatal error on fopen with open_basedir
252  $outputfile = $dolibarr_main_data_root."/dolibarr_sms.log";
253  $fp = fopen($outputfile, "a+");
254 
255  fputs($fp, "\nResult id=".$result);
256 
257  fclose($fp);
258  dolChmod($outputfile);
259  }
260  }
261 }
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...
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
dolChmod($filepath, $newmask='')
Change mod of a file.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.