dolibarr  9.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  *
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 <http://www.gnu.org/licenses/>.
19  * or see http://www.gnu.org/
20  *
21  * Lots of code inspired from Dan Potter's CSMSFile class
22  */
23 
35 class CSMSFile
36 {
40  public $error='';
41 
42  public $addr_from;
43  public $addr_to;
44  public $deferred;
45  public $priority;
46  public $class;
47  public $message;
48  public $nostop;
49 
50 
62  function __construct($to,$from,$msg,$deliveryreceipt=0,$deferred=0,$priority=3,$class=1)
63  {
64  global $conf;
65 
66  // On definit fin de ligne
67  $this->eol="\n";
68  if (preg_match('/^win/i',PHP_OS)) $this->eol="\r\n";
69  if (preg_match('/^mac/i',PHP_OS)) $this->eol="\r";
70 
71  // If ending method not defined
72  if (empty($conf->global->MAIN_SMS_SENDMODE))
73  {
74  $this->error='No SMS Engine defined';
75  return -1;
76  }
77 
78  dol_syslog("CSMSFile::CSMSFile: MAIN_SMS_SENDMODE=".$conf->global->MAIN_SMS_SENDMODE." charset=".$conf->file->character_set_client." from=".$from.", to=".$to.", msg length=".count($msg), LOG_DEBUG);
79  dol_syslog("CSMSFile::CSMSFile: deferred=".$deferred." priority=".$priority." class=".$class, LOG_DEBUG);
80 
81  // Action according to choosed sending method
82  $this->addr_from=$from;
83  $this->addr_to=$to;
84  $this->deferred=$deferred;
85  $this->priority=$priority;
86  $this->class=$class;
87  $this->message=$msg;
88  $this->nostop=false;
89  }
90 
91 
97  function sendfile()
98  {
99  global $conf;
100 
101  $errorlevel=error_reporting();
102  error_reporting($errorlevel ^ E_WARNING); // Desactive warnings
103 
104  $res=false;
105 
106  dol_syslog("CSMSFile::sendfile addr_to=".$this->addr_to, LOG_DEBUG);
107  dol_syslog("CSMSFile::sendfile message=\n".$this->message);
108 
109  $this->message=stripslashes($this->message);
110 
111  if (! empty($conf->global->MAIN_SMS_DEBUG)) $this->dump_sms();
112 
113  if (empty($conf->global->MAIN_DISABLE_ALL_SMS))
114  {
115 
116  // Action according to choosed sending method
117  if ($conf->global->MAIN_SMS_SENDMODE == 'ovh') // Backward compatibility @deprecated
118  {
119  dol_include_once('/ovh/class/ovhsms.class.php');
120  $sms=new OvhSms($this->db);
121  $sms->expe=$this->addr_from;
122  $sms->dest=$this->addr_to;
123  $sms->message=$this->message;
124  $sms->deferred=$this->deferred;
125  $sms->priority=$this->priority;
126  $sms->class=$this->class;
127  $sms->nostop=$this->nostop;
128 
129  $res=$sms->SmsSend();
130  if ($res <= 0)
131  {
132  $this->error=$sms->error;
133  dol_syslog("CSMSFile::sendfile: sms send error=".$this->error, LOG_ERR);
134  }
135  else
136  {
137  dol_syslog("CSMSFile::sendfile: sms send success with id=".$res, LOG_DEBUG);
138  //var_dump($res); // 1973128
139  if (! empty($conf->global->MAIN_SMS_DEBUG)) $this->dump_sms_result($res);
140  }
141  }
142  else if (! empty($conf->global->MAIN_SMS_SENDMODE)) // $conf->global->MAIN_SMS_SENDMODE looks like a value 'class@module'
143  {
144  $tmp=explode('@',$conf->global->MAIN_SMS_SENDMODE);
145  $classfile=$tmp[0]; $module=(empty($tmp[1])?$tmp[0]:$tmp[1]);
146  dol_include_once('/'.$module.'/class/'.$classfile.'.class.php');
147  try
148  {
149  $classname=ucfirst($classfile);
150  $sms = new $classname($this->db);
151  $sms->expe=$this->addr_from;
152  $sms->dest=$this->addr_to;
153  $sms->deferred=$this->deferred;
154  $sms->priority=$this->priority;
155  $sms->class=$this->class;
156  $sms->message=$this->message;
157  $sms->nostop=$this->nostop;
158 
159  $res=$sms->SmsSend();
160  $this->error = $sms->error;
161  $this->errors = $sms->errors;
162  if ($res <= 0)
163  {
164  dol_syslog("CSMSFile::sendfile: sms send error=".$this->error, LOG_ERR);
165  }
166  else
167  {
168  dol_syslog("CSMSFile::sendfile: sms send success with id=".$res, LOG_DEBUG);
169  //var_dump($res); // 1973128
170  if (! empty($conf->global->MAIN_SMS_DEBUG)) $this->dump_sms_result($res);
171  }
172  }
173  catch(Exception $e)
174  {
175  dol_print_error('','Error to get list of senders: '.$e->getMessage());
176  }
177  }
178  else
179  {
180  // Send sms method not correctly defined
181  // --------------------------------------
182 
183  return 'Bad value for MAIN_SMS_SENDMODE constant';
184  }
185  }
186  else
187  {
188  $this->error='No sms sent. Feature is disabled by option MAIN_DISABLE_ALL_SMS';
189  dol_syslog("CSMSFile::sendfile: ".$this->error, LOG_WARNING);
190  }
191 
192  error_reporting($errorlevel); // Reactive niveau erreur origine
193 
194  return $res;
195  }
196 
197 
198  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
205  function dump_sms()
206  {
207  // phpcs:enable
208  global $conf,$dolibarr_main_data_root;
209 
210  if (@is_writeable($dolibarr_main_data_root)) // Avoid fatal error on fopen with open_basedir
211  {
212  $outputfile=$dolibarr_main_data_root."/dolibarr_sms.log";
213  $fp = fopen($outputfile,"w");
214 
215  fputs($fp, "From: ".$this->addr_from."\n");
216  fputs($fp, "To: ".$this->addr_to."\n");
217  fputs($fp, "Priority: ".$this->priority."\n");
218  fputs($fp, "Class: ".$this->class."\n");
219  fputs($fp, "Deferred: ".$this->deferred."\n");
220  fputs($fp, "DisableStop: ".$this->nostop."\n");
221  fputs($fp, "Message:\n".$this->message);
222 
223  fclose($fp);
224  if (! empty($conf->global->MAIN_UMASK))
225  @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
226  }
227  }
228 
229  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
237  function dump_sms_result($result)
238  {
239  // phpcs:enable
240  global $conf,$dolibarr_main_data_root;
241 
242  if (@is_writeable($dolibarr_main_data_root)) // Avoid fatal error on fopen with open_basedir
243  {
244  $outputfile=$dolibarr_main_data_root."/dolibarr_sms.log";
245  $fp = fopen($outputfile,"a+");
246 
247  fputs($fp, "\nResult id=".$result);
248 
249  fclose($fp);
250  if (! empty($conf->global->MAIN_UMASK))
251  @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
252  }
253  }
254 }
Class to send SMS Usage: $smsfile = new CSMSFile($subject,$sendto,$replyto,$message,$filepath,$mimetype,$filename,$cc,$ccc,$deliveryreceipt,$msgishtml,$errors_to); $smsfile->sendfile();.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
__construct($to, $from, $msg, $deliveryreceipt=0, $deferred=0, $priority=3, $class=1)
CSMSFile.
if(! function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
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.
dump_sms_result($result)
Write content of a SendSms result into a dump file (mode = all) Used for debugging.