CSMSFile.class.php 8.1 KB

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