CSMSFile.class.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. /**
  24. * \file htdocs/core/class/CSMSFile.class.php
  25. * \brief File of class to send sms
  26. * \author Laurent Destailleur.
  27. */
  28. /**
  29. * Class to send SMS
  30. * Usage: $smsfile = new CSMSFile($subject,$sendto,$replyto,$message,$filepath,$mimetype,$filename,$cc,$ccc,$deliveryreceipt,$msgishtml,$errors_to);
  31. * $smsfile->sendfile();
  32. */
  33. class CSMSFile
  34. {
  35. var $error='';
  36. var $addr_from;
  37. var $addr_to;
  38. var $deferred;
  39. var $priority;
  40. var $class;
  41. var $message;
  42. /**
  43. * CSMSFile
  44. *
  45. * @param string $to Recipients SMS
  46. * @param string $from Sender SMS
  47. * @param string $msg Message
  48. * @param int $deliveryreceipt Not used
  49. * @param int $deferred Deferred or not
  50. * @param int $priority Priority
  51. * @param int $class Class
  52. */
  53. function __construct($to,$from,$msg,$deliveryreceipt=0,$deferred=0,$priority=3,$class=1)
  54. {
  55. global $conf;
  56. // On definit fin de ligne
  57. $this->eol="\n";
  58. if (preg_match('/^win/i',PHP_OS)) $this->eol="\r\n";
  59. if (preg_match('/^mac/i',PHP_OS)) $this->eol="\r";
  60. // If ending method not defined
  61. if (empty($conf->global->MAIN_SMS_SENDMODE))
  62. {
  63. $this->error='No SMS Engine defined';
  64. return -1;
  65. }
  66. 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);
  67. dol_syslog("CSMSFile::CSMSFile: deferred=".$deferred." priority=".$priority." class=".$class, LOG_DEBUG);
  68. // Action according to choosed sending method
  69. $this->addr_from=$from;
  70. $this->addr_to=$to;
  71. $this->deferred=$deferred;
  72. $this->priority=$priority;
  73. $this->class=$class;
  74. $this->message=$msg;
  75. }
  76. /**
  77. * Send sms that was prepared by constructor
  78. *
  79. * @return boolean True if sms sent, false otherwise
  80. */
  81. function sendfile()
  82. {
  83. global $conf;
  84. $errorlevel=error_reporting();
  85. error_reporting($errorlevel ^ E_WARNING); // Desactive warnings
  86. $res=false;
  87. dol_syslog("CSMSFile::sendfile addr_to=".$this->addr_to, LOG_DEBUG);
  88. dol_syslog("CSMSFile::sendfile message=\n".$this->message);
  89. $this->message=stripslashes($this->message);
  90. if (! empty($conf->global->MAIN_SMS_DEBUG)) $this->dump_sms();
  91. if (empty($conf->global->MAIN_DISABLE_ALL_SMS))
  92. {
  93. // Action according to choosed sending method
  94. if ($conf->global->MAIN_SMS_SENDMODE == 'ovh') // Backward compatibility @deprecated
  95. {
  96. dol_include_once('/ovh/class/ovhsms.class.php');
  97. $sms=new OvhSms($this->db);
  98. $sms->expe=$this->addr_from;
  99. $sms->dest=$this->addr_to;
  100. $sms->message=$this->message;
  101. $sms->deferred=$this->deferred;
  102. $sms->priority=$this->priority;
  103. $sms->class=$this->class;
  104. $res=$sms->SmsSend();
  105. if ($res <= 0)
  106. {
  107. $this->error=$sms->error;
  108. dol_syslog("CSMSFile::sendfile: sms send error=".$this->error, LOG_ERR);
  109. }
  110. else
  111. {
  112. dol_syslog("CSMSFile::sendfile: sms send success with id=".$res, LOG_DEBUG);
  113. //var_dump($res); // 1973128
  114. $this->dump_sms_result($res);
  115. }
  116. }
  117. else if (! empty($conf->global->MAIN_SMS_SENDMODE)) // $conf->global->MAIN_SMS_SENDMODE looks like a value 'class@module'
  118. {
  119. $tmp=explode('@',$conf->global->MAIN_SMS_SENDMODE);
  120. $classfile=$tmp[0]; $module=(empty($tmp[1])?$tmp[0]:$tmp[1]);
  121. dol_include_once('/'.$module.'/class/'.$classfile.'.class.php');
  122. try
  123. {
  124. $classname=ucfirst($classfile);
  125. $sms = new $classname($this->db);
  126. $sms->expe=$this->addr_from;
  127. $sms->dest=$this->addr_to;
  128. $sms->deferred=$this->deferred;
  129. $sms->priority=$this->priority;
  130. $sms->class=$this->class;
  131. $sms->message=$this->message;
  132. $res=$sms->SmsSend();
  133. $this->error = $sms->error;
  134. $this->errors = $sms->errors;
  135. if ($res <= 0)
  136. {
  137. dol_syslog("CSMSFile::sendfile: sms send error=".$this->error, LOG_ERR);
  138. }
  139. else
  140. {
  141. dol_syslog("CSMSFile::sendfile: sms send success with id=".$res, LOG_DEBUG);
  142. //var_dump($res); // 1973128
  143. $this->dump_sms_result($res);
  144. }
  145. }
  146. catch(Exception $e)
  147. {
  148. dol_print_error('','Error to get list of senders: '.$e->getMessage());
  149. }
  150. }
  151. else
  152. {
  153. // Send sms method not correctly defined
  154. // --------------------------------------
  155. return 'Bad value for MAIN_SMS_SENDMODE constant';
  156. }
  157. }
  158. else
  159. {
  160. $this->error='No sms sent. Feature is disabled by option MAIN_DISABLE_ALL_SMS';
  161. dol_syslog("CSMSFile::sendfile: ".$this->error, LOG_WARNING);
  162. }
  163. error_reporting($errorlevel); // Reactive niveau erreur origine
  164. return $res;
  165. }
  166. /**
  167. * Write content of a SendSms request into a dump file (mode = all)
  168. * Used for debugging.
  169. *
  170. * @return void
  171. */
  172. function dump_sms()
  173. {
  174. global $conf,$dolibarr_main_data_root;
  175. if (@is_writeable($dolibarr_main_data_root)) // Avoid fatal error on fopen with open_basedir
  176. {
  177. $outputfile=$dolibarr_main_data_root."/dolibarr_sms.log";
  178. $fp = fopen($outputfile,"w");
  179. fputs($fp, "From: ".$this->addr_from."\n");
  180. fputs($fp, "To: ".$this->addr_to."\n");
  181. fputs($fp, "Priority: ".$this->priority."\n");
  182. fputs($fp, "Class: ".$this->class."\n");
  183. fputs($fp, "Deferred: ".$this->deferred."\n");
  184. fputs($fp, "Message:\n".$this->message);
  185. fclose($fp);
  186. if (! empty($conf->global->MAIN_UMASK))
  187. @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
  188. }
  189. }
  190. /**
  191. * Write content of a SendSms result into a dump file (mode = all)
  192. * Used for debugging.
  193. *
  194. * @param int $result Result of sms sending
  195. * @return void
  196. */
  197. function dump_sms_result($result)
  198. {
  199. global $conf,$dolibarr_main_data_root;
  200. if (@is_writeable($dolibarr_main_data_root)) // Avoid fatal error on fopen with open_basedir
  201. {
  202. $outputfile=$dolibarr_main_data_root."/dolibarr_sms.log";
  203. $fp = fopen($outputfile,"a+");
  204. fputs($fp, "\nResult id=".$result);
  205. fclose($fp);
  206. if (! empty($conf->global->MAIN_UMASK))
  207. @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
  208. }
  209. }
  210. }