CSMSFile.class.php 8.5 KB

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