PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/asterix14/dolibarr
PHP | 233 lines | 137 code | 33 blank | 63 comment | 15 complexity | 9a6ba63a9924f083f210e9ae59e99d4d MD5 | raw file
Possible License(s): LGPL-2.0
  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-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  5. * Copyright (C) 2005-2009 Regis Houssin <regis@dolibarr.fr>
  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 2 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 CSMSFile
  30. * \brief Class to send SMS
  31. * \remarks Usage: $smsfile = new CSMSFile($subject,$sendto,$replyto,$message,$filepath,$mimetype,$filename,$cc,$ccc,$deliveryreceipt,$msgishtml,$errors_to);
  32. * \remarks $smsfile->sendfile();
  33. */
  34. class CSMSFile
  35. {
  36. var $error='';
  37. var $message;
  38. var $addr_from;
  39. var $addr_to;
  40. var $deliveryreceipt;
  41. var $deferred;
  42. var $priority;
  43. var $class;
  44. /**
  45. * CSMSFile
  46. * @param to Recipients SMS
  47. * @param from Sender SMS
  48. * @param msg Message
  49. * @param deliveryreceipt Ask a delivery receipt
  50. * @param deferred Deferred or not
  51. * @param priority Priority
  52. * @param class Class
  53. */
  54. function CSMSFile($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: deliveryreceipt=".$deliveryreceipt." 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->message=$msg;
  73. $this->deliveryreceipt=$deliveryreceipt;
  74. $this->deferred=$deferred;
  75. $this->priority=$priority;
  76. $this->class=$class;
  77. }
  78. /**
  79. * Send mail that was prepared by constructor
  80. *
  81. * @return boolean True if mail sent, false otherwise
  82. */
  83. function sendfile()
  84. {
  85. global $conf;
  86. $errorlevel=error_reporting();
  87. error_reporting($errorlevel ^ E_WARNING); // Desactive warnings
  88. $res=false;
  89. dol_syslog("CSMSFile::sendfile addr_to=".$this->addr_to, LOG_DEBUG);
  90. dol_syslog("CSMSFile::sendfile message=\n".$this->message);
  91. $this->message=stripslashes($this->message);
  92. if (! empty($conf->global->MAIN_SMS_DEBUG)) $this->dump_sms();
  93. if (empty($conf->global->MAIN_DISABLE_ALL_SMS))
  94. {
  95. // Action according to choosed sending method
  96. if ($conf->global->MAIN_SMS_SENDMODE == 'ovh') // Backward compatibility @deprecated
  97. {
  98. dol_include_once('/ovh/class/ovhsms.class.php');
  99. $sms=new OvhSms($this->db);
  100. $sms->expe=$this->addr_from;
  101. $sms->dest=$this->addr_to;
  102. $sms->message=$this->message;
  103. $sms->deferred=$this->deferred;
  104. $sms->priority=$this->priority;
  105. $sms->class=$this->class;
  106. $res=$sms->SmsSend();
  107. if ($res <= 0)
  108. {
  109. $this->error=$sms->error;
  110. dol_syslog("CSMSFile::sendfile: sms send error=".$this->error, LOG_ERR);
  111. }
  112. else
  113. {
  114. dol_syslog("CSMSFile::sendfile: sms send success with id=".$res, LOG_DEBUG);
  115. //var_dump($res); // 1973128
  116. $this->dump_sms_result($res);
  117. }
  118. }
  119. else if (! empty($conf->global->MAIN_SMS_SENDMODE)) // $conf->global->MAIN_SMS_SENDMODE looks like a value 'class@module'
  120. {
  121. $tmp=explode('@',$conf->global->MAIN_SMS_SENDMODE);
  122. $classfile=$tmp[0]; $module=(empty($tmp[1])?$tmp[0]:$tmp[1]);
  123. dol_include_once('/'.$module.'/class/'.$classfile.'.class.php');
  124. try
  125. {
  126. $classname=ucfirst($classfile);
  127. $sms = new $classname($this->db);
  128. $sms->expe=$this->addr_from;
  129. $sms->dest=$this->addr_to;
  130. $sms->message=$this->message;
  131. $sms->deferred=$this->deferred;
  132. $sms->priority=$this->priority;
  133. $sms->class=$this->class;
  134. $res=$sms->SmsSend();
  135. if ($res <= 0)
  136. {
  137. $this->error=$sms->error;
  138. dol_syslog("CSMSFile::sendfile: sms send error=".$this->error, LOG_ERR);
  139. }
  140. else
  141. {
  142. dol_syslog("CSMSFile::sendfile: sms send success with id=".$res, LOG_DEBUG);
  143. //var_dump($res); // 1973128
  144. $this->dump_sms_result($res);
  145. }
  146. }
  147. catch(Exception $e)
  148. {
  149. dol_print_error('','Error to get list of senders: '.$e->getMessage());
  150. }
  151. }
  152. else
  153. {
  154. // Send mail method not correctly defined
  155. // --------------------------------------
  156. return 'Bad value for MAIN_SMS_SENDMODE constant';
  157. }
  158. }
  159. else
  160. {
  161. $this->error='No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_SMS';
  162. dol_syslog("CSMSFile::sendfile: ".$this->error, LOG_WARNING);
  163. }
  164. error_reporting($errorlevel); // Reactive niveau erreur origine
  165. return $res;
  166. }
  167. /**
  168. * Write content of a SMTP request into a dump file (mode = all)
  169. * Used for debugging.
  170. */
  171. function dump_sms()
  172. {
  173. global $conf,$dolibarr_main_data_root;
  174. if (@is_writeable($dolibarr_main_data_root)) // Avoid fatal error on fopen with open_basedir
  175. {
  176. $fp = fopen($dolibarr_main_data_root."/dolibarr_sms.log","w");
  177. fputs($fp, $this->message);
  178. fclose($fp);
  179. if (! empty($conf->global->MAIN_UMASK))
  180. @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
  181. }
  182. }
  183. /**
  184. * Write content of a SMTP request into a dump file (mode = all)
  185. * Used for debugging.
  186. */
  187. function dump_sms_result($result)
  188. {
  189. global $conf,$dolibarr_main_data_root;
  190. if (@is_writeable($dolibarr_main_data_root)) // Avoid fatal error on fopen with open_basedir
  191. {
  192. $fp = fopen($dolibarr_main_data_root."/dolibarr_sms.log","a+");
  193. fputs($fp, "\nResult id=".$result);
  194. fclose($fp);
  195. if (! empty($conf->global->MAIN_UMASK))
  196. @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
  197. }
  198. }
  199. }
  200. ?>