CSMSFile.class.php 7.9 KB

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