PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/core/class/CSMSFile.class.php

https://bitbucket.org/speedealing/speedealing
PHP | 244 lines | 142 code | 33 blank | 69 comment | 15 complexity | 0812577e5f76897602f1403e8c5cca97 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  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. * @return int
  53. */
  54. function __construct($to,$from,$msg,$deliveryreceipt=0,$deferred=0,$priority=3,$class=1)
  55. {
  56. global $conf;
  57. // On definit fin de ligne
  58. $this->eol="\n";
  59. if (preg_match('/^win/i',PHP_OS)) $this->eol="\r\n";
  60. if (preg_match('/^mac/i',PHP_OS)) $this->eol="\r";
  61. // If ending method not defined
  62. if (empty($conf->global->MAIN_SMS_SENDMODE))
  63. {
  64. $this->error='No SMS Engine defined';
  65. return -1;
  66. }
  67. 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);
  68. dol_syslog("CSMSFile::CSMSFile: deferred=".$deferred." priority=".$priority." class=".$class, LOG_DEBUG);
  69. // Action according to choosed sending method
  70. $this->addr_from=$from;
  71. $this->addr_to=$to;
  72. $this->deferred=$deferred;
  73. $this->priority=$priority;
  74. $this->class=$class;
  75. $this->message=$msg;
  76. }
  77. /**
  78. * Send mail that was prepared by constructor
  79. *
  80. * @return boolean True if mail sent, false otherwise
  81. */
  82. function sendfile()
  83. {
  84. global $conf;
  85. $errorlevel=error_reporting();
  86. error_reporting($errorlevel ^ E_WARNING); // Desactive warnings
  87. $res=false;
  88. dol_syslog("CSMSFile::sendfile addr_to=".$this->addr_to, LOG_DEBUG);
  89. dol_syslog("CSMSFile::sendfile message=\n".$this->message);
  90. $this->message=stripslashes($this->message);
  91. if (! empty($conf->global->MAIN_SMS_DEBUG)) $this->dump_sms();
  92. if (empty($conf->global->MAIN_DISABLE_ALL_SMS))
  93. {
  94. // Action according to choosed sending method
  95. if ($conf->global->MAIN_SMS_SENDMODE == 'ovh') // Backward compatibility @deprecated
  96. {
  97. dol_include_once('/ovh/class/ovhsms.class.php');
  98. $sms=new OvhSms($this->db);
  99. $sms->expe=$this->addr_from;
  100. $sms->dest=$this->addr_to;
  101. $sms->message=$this->message;
  102. $sms->deferred=$this->deferred;
  103. $sms->priority=$this->priority;
  104. $sms->class=$this->class;
  105. $res=$sms->SmsSend();
  106. if ($res <= 0)
  107. {
  108. $this->error=$sms->error;
  109. dol_syslog("CSMSFile::sendfile: sms send error=".$this->error, LOG_ERR);
  110. }
  111. else
  112. {
  113. dol_syslog("CSMSFile::sendfile: sms send success with id=".$res, LOG_DEBUG);
  114. //var_dump($res); // 1973128
  115. $this->dump_sms_result($res);
  116. }
  117. }
  118. else if (! empty($conf->global->MAIN_SMS_SENDMODE)) // $conf->global->MAIN_SMS_SENDMODE looks like a value 'class@module'
  119. {
  120. $tmp=explode('@',$conf->global->MAIN_SMS_SENDMODE);
  121. $classfile=$tmp[0]; $module=(empty($tmp[1])?$tmp[0]:$tmp[1]);
  122. dol_include_once('/'.$module.'/class/'.$classfile.'.class.php');
  123. try
  124. {
  125. $classname=ucfirst($classfile);
  126. $sms = new $classname($this->db);
  127. $sms->expe=$this->addr_from;
  128. $sms->dest=$this->addr_to;
  129. $sms->deferred=$this->deferred;
  130. $sms->priority=$this->priority;
  131. $sms->class=$this->class;
  132. $sms->message=$this->message;
  133. $res=$sms->SmsSend();
  134. if ($res <= 0)
  135. {
  136. $this->error=$sms->error;
  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 mail method not correctly defined
  154. // --------------------------------------
  155. return 'Bad value for MAIN_SMS_SENDMODE constant';
  156. }
  157. }
  158. else
  159. {
  160. $this->error='No mail 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 SMTP 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 SMTP request 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. }
  211. ?>