dolibarr  7.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@capnetworks.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 {
37  var $error='';
38 
39  var $addr_from;
40  var $addr_to;
41  var $deferred;
42  var $priority;
43  var $class;
44  var $message;
45  var $nostop;
46 
47 
59  function __construct($to,$from,$msg,$deliveryreceipt=0,$deferred=0,$priority=3,$class=1)
60  {
61  global $conf;
62 
63  // On definit fin de ligne
64  $this->eol="\n";
65  if (preg_match('/^win/i',PHP_OS)) $this->eol="\r\n";
66  if (preg_match('/^mac/i',PHP_OS)) $this->eol="\r";
67 
68  // If ending method not defined
69  if (empty($conf->global->MAIN_SMS_SENDMODE))
70  {
71  $this->error='No SMS Engine defined';
72  return -1;
73  }
74 
75  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);
76  dol_syslog("CSMSFile::CSMSFile: deferred=".$deferred." priority=".$priority." class=".$class, LOG_DEBUG);
77 
78  // Action according to choosed sending method
79  $this->addr_from=$from;
80  $this->addr_to=$to;
81  $this->deferred=$deferred;
82  $this->priority=$priority;
83  $this->class=$class;
84  $this->message=$msg;
85  $this->nostop=false;
86  }
87 
88 
94  function sendfile()
95  {
96  global $conf;
97 
98  $errorlevel=error_reporting();
99  error_reporting($errorlevel ^ E_WARNING); // Desactive warnings
100 
101  $res=false;
102 
103  dol_syslog("CSMSFile::sendfile addr_to=".$this->addr_to, LOG_DEBUG);
104  dol_syslog("CSMSFile::sendfile message=\n".$this->message);
105 
106  $this->message=stripslashes($this->message);
107 
108  if (! empty($conf->global->MAIN_SMS_DEBUG)) $this->dump_sms();
109 
110  if (empty($conf->global->MAIN_DISABLE_ALL_SMS))
111  {
112 
113  // Action according to choosed sending method
114  if ($conf->global->MAIN_SMS_SENDMODE == 'ovh') // Backward compatibility @deprecated
115  {
116  dol_include_once('/ovh/class/ovhsms.class.php');
117  $sms=new OvhSms($this->db);
118  $sms->expe=$this->addr_from;
119  $sms->dest=$this->addr_to;
120  $sms->message=$this->message;
121  $sms->deferred=$this->deferred;
122  $sms->priority=$this->priority;
123  $sms->class=$this->class;
124  $sms->nostop=$this->nostop;
125 
126  $res=$sms->SmsSend();
127  if ($res <= 0)
128  {
129  $this->error=$sms->error;
130  dol_syslog("CSMSFile::sendfile: sms send error=".$this->error, LOG_ERR);
131  }
132  else
133  {
134  dol_syslog("CSMSFile::sendfile: sms send success with id=".$res, LOG_DEBUG);
135  //var_dump($res); // 1973128
136  if (! empty($conf->global->MAIN_SMS_DEBUG)) $this->dump_sms_result($res);
137  }
138  }
139  else if (! empty($conf->global->MAIN_SMS_SENDMODE)) // $conf->global->MAIN_SMS_SENDMODE looks like a value 'class@module'
140  {
141  $tmp=explode('@',$conf->global->MAIN_SMS_SENDMODE);
142  $classfile=$tmp[0]; $module=(empty($tmp[1])?$tmp[0]:$tmp[1]);
143  dol_include_once('/'.$module.'/class/'.$classfile.'.class.php');
144  try
145  {
146  $classname=ucfirst($classfile);
147  $sms = new $classname($this->db);
148  $sms->expe=$this->addr_from;
149  $sms->dest=$this->addr_to;
150  $sms->deferred=$this->deferred;
151  $sms->priority=$this->priority;
152  $sms->class=$this->class;
153  $sms->message=$this->message;
154  $sms->nostop=$this->nostop;
155 
156  $res=$sms->SmsSend();
157  $this->error = $sms->error;
158  $this->errors = $sms->errors;
159  if ($res <= 0)
160  {
161  dol_syslog("CSMSFile::sendfile: sms send error=".$this->error, LOG_ERR);
162  }
163  else
164  {
165  dol_syslog("CSMSFile::sendfile: sms send success with id=".$res, LOG_DEBUG);
166  //var_dump($res); // 1973128
167  if (! empty($conf->global->MAIN_SMS_DEBUG)) $this->dump_sms_result($res);
168  }
169  }
170  catch(Exception $e)
171  {
172  dol_print_error('','Error to get list of senders: '.$e->getMessage());
173  }
174  }
175  else
176  {
177  // Send sms method not correctly defined
178  // --------------------------------------
179 
180  return 'Bad value for MAIN_SMS_SENDMODE constant';
181  }
182  }
183  else
184  {
185  $this->error='No sms sent. Feature is disabled by option MAIN_DISABLE_ALL_SMS';
186  dol_syslog("CSMSFile::sendfile: ".$this->error, LOG_WARNING);
187  }
188 
189  error_reporting($errorlevel); // Reactive niveau erreur origine
190 
191  return $res;
192  }
193 
194 
201  function dump_sms()
202  {
203  global $conf,$dolibarr_main_data_root;
204 
205  if (@is_writeable($dolibarr_main_data_root)) // Avoid fatal error on fopen with open_basedir
206  {
207  $outputfile=$dolibarr_main_data_root."/dolibarr_sms.log";
208  $fp = fopen($outputfile,"w");
209 
210  fputs($fp, "From: ".$this->addr_from."\n");
211  fputs($fp, "To: ".$this->addr_to."\n");
212  fputs($fp, "Priority: ".$this->priority."\n");
213  fputs($fp, "Class: ".$this->class."\n");
214  fputs($fp, "Deferred: ".$this->deferred."\n");
215  fputs($fp, "DisableStop: ".$this->nostop."\n");
216  fputs($fp, "Message:\n".$this->message);
217 
218  fclose($fp);
219  if (! empty($conf->global->MAIN_UMASK))
220  @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
221  }
222  }
223 
231  function dump_sms_result($result)
232  {
233  global $conf,$dolibarr_main_data_root;
234 
235  if (@is_writeable($dolibarr_main_data_root)) // Avoid fatal error on fopen with open_basedir
236  {
237  $outputfile=$dolibarr_main_data_root."/dolibarr_sms.log";
238  $fp = fopen($outputfile,"a+");
239 
240  fputs($fp, "\nResult id=".$result);
241 
242  fclose($fp);
243  if (! empty($conf->global->MAIN_UMASK))
244  @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
245  }
246  }
247 
248 }
249 
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.
sendfile()
Send sms that was prepared by constructor.
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Return a prefix to use for this Dolibarr instance, for session/cookie names or email id...
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.