html.formsms.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. /* Copyright (C) 2005-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2010 Juanjo Menent <jmenent@2byte.es>
  4. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/core/class/html.formsms.class.php
  21. * \ingroup core
  22. * \brief Fichier de la classe permettant la generation du formulaire html d'envoi de mail unitaire
  23. */
  24. require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
  25. /**
  26. * Classe permettant la generation du formulaire d'envoi de Sms
  27. * Usage: $formsms = new FormSms($db)
  28. * $formsms->proprietes=1 ou chaine ou tableau de valeurs
  29. * $formsms->show_form() affiche le formulaire
  30. */
  31. class FormSms
  32. {
  33. /**
  34. * @var DoliDB Database handler.
  35. */
  36. public $db;
  37. public $fromid;
  38. public $fromname;
  39. public $fromsms;
  40. public $fromtype;
  41. public $replytoname;
  42. public $replytomail;
  43. public $toname;
  44. public $tomail;
  45. public $withsubstit; // Show substitution array
  46. public $withfrom;
  47. public $withto;
  48. public $withtopic;
  49. public $withbody;
  50. /**
  51. * @var int Id of company
  52. */
  53. public $withtosocid;
  54. public $withfromreadonly;
  55. public $withreplytoreadonly;
  56. public $withtoreadonly;
  57. public $withtopicreadonly;
  58. public $withcancel;
  59. public $substit = array();
  60. public $param = array();
  61. /**
  62. * @var string Error code (or message)
  63. */
  64. public $error = '';
  65. /**
  66. * @var string[] Array of error strings
  67. */
  68. public $errors = array();
  69. /**
  70. * Constructor
  71. *
  72. * @param DoliDB $db Database handler
  73. */
  74. public function __construct($db)
  75. {
  76. $this->db = $db;
  77. $this->withfrom = 1;
  78. $this->withto = 1;
  79. $this->withtopic = 1;
  80. $this->withbody = 1;
  81. $this->withfromreadonly = 1;
  82. $this->withreplytoreadonly = 1;
  83. $this->withtoreadonly = 0;
  84. $this->withtopicreadonly = 0;
  85. $this->withbodyreadonly = 0;
  86. }
  87. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  88. /**
  89. * Show the form to input an sms.
  90. *
  91. * @param string $morecss Class on first column td
  92. * @param int $showform Show form tags and submit button (recommanded is to use with value 0)
  93. * @return void
  94. */
  95. public function show_form($morecss = 'titlefield', $showform = 1)
  96. {
  97. // phpcs:enable
  98. global $conf, $langs, $user, $form;
  99. if (!is_object($form)) {
  100. $form = new Form($this->db);
  101. }
  102. // Load translation files required by the page
  103. $langs->loadLangs(array('other', 'mails', 'sms'));
  104. $soc = new Societe($this->db);
  105. if (!empty($this->withtosocid) && $this->withtosocid > 0) {
  106. $soc->fetch($this->withtosocid);
  107. }
  108. print "\n<!-- Begin form SMS -->\n";
  109. print '
  110. <script nonce="'.getNonce().'" type="text/javascript">
  111. function limitChars(textarea, limit, infodiv)
  112. {
  113. var text = textarea.value;
  114. var textlength = text.length;
  115. var info = document.getElementById(infodiv);
  116. info.innerHTML = (limit - textlength);
  117. return true;
  118. }
  119. </script>';
  120. if ($showform) {
  121. print "<form method=\"POST\" name=\"smsform\" enctype=\"multipart/form-data\" action=\"".$this->param["returnurl"]."\">\n";
  122. }
  123. print '<input type="hidden" name="token" value="'.newToken().'">';
  124. foreach ($this->param as $key => $value) {
  125. print "<input type=\"hidden\" name=\"$key\" value=\"$value\">\n";
  126. }
  127. print "<table class=\"border centpercent\">\n";
  128. // Substitution array
  129. if (!empty($this->withsubstit)) { // Unset or set ->withsubstit=0 to disable this.
  130. print "<tr><td colspan=\"2\">";
  131. $help = "";
  132. foreach ($this->substit as $key => $val) {
  133. $help .= $key.' -> '.$langs->trans($val).'<br>';
  134. }
  135. print $form->textwithpicto($langs->trans("SmsTestSubstitutionReplacedByGenericValues"), $help);
  136. print "</td></tr>\n";
  137. }
  138. // From
  139. if ($this->withfrom) {
  140. if ($this->withfromreadonly) {
  141. print '<tr><td class="titlefield '.$morecss.'">'.$langs->trans("SmsFrom");
  142. print '<input type="hidden" name="fromsms" value="'.$this->fromsms.'">';
  143. print "</td><td>";
  144. if ($this->fromtype == 'user') {
  145. $langs->load("users");
  146. $fuser = new User($this->db);
  147. $fuser->fetch($this->fromid);
  148. print $fuser->getNomUrl(1);
  149. print ' &nbsp; ';
  150. }
  151. if ($this->fromsms) {
  152. print $this->fromsms;
  153. } else {
  154. if ($this->fromtype) {
  155. $langs->load("errors");
  156. print '<span class="warning"> &lt;'.$langs->trans("ErrorNoPhoneDefinedForThisUser").'&gt; </span>';
  157. }
  158. }
  159. print "</td></tr>\n";
  160. print "</td></tr>\n";
  161. } else {
  162. print '<tr><td class="'.$morecss.'">'.$langs->trans("SmsFrom")."</td><td>";
  163. //print '<input type="text" name="fromname" size="30" value="'.$this->fromsms.'">';
  164. if ($conf->global->MAIN_SMS_SENDMODE == 'ovh') { // For backward compatibility @deprecated
  165. dol_include_once('/ovh/class/ovhsms.class.php');
  166. try {
  167. $sms = new OvhSms($this->db);
  168. if (empty($conf->global->OVHSMS_ACCOUNT)) {
  169. $resultsender = 'ErrorOVHSMS_ACCOUNT not defined';
  170. } else {
  171. $resultsender = $sms->SmsSenderList();
  172. }
  173. } catch (Exception $e) {
  174. dol_print_error('', 'Error to get list of senders: '.$e->getMessage());
  175. }
  176. } elseif (!empty($conf->global->MAIN_SMS_SENDMODE)) { // $conf->global->MAIN_SMS_SENDMODE looks like a value 'class@module'
  177. $tmp = explode('@', $conf->global->MAIN_SMS_SENDMODE);
  178. $classfile = $tmp[0];
  179. $module = (empty($tmp[1]) ? $tmp[0] : $tmp[1]);
  180. dol_include_once('/'.$module.'/class/'.$classfile.'.class.php');
  181. try {
  182. $classname = ucfirst($classfile);
  183. if (class_exists($classname)) {
  184. $sms = new $classname($this->db);
  185. $resultsender = $sms->SmsSenderList();
  186. } else {
  187. $sms = new stdClass();
  188. $sms->error = 'The SMS manager "'.$classfile.'" defined into SMS setup MAIN_SMS_SENDMODE is not found';
  189. }
  190. } catch (Exception $e) {
  191. dol_print_error('', 'Error to get list of senders: '.$e->getMessage());
  192. exit;
  193. }
  194. } else {
  195. dol_syslog("Warning: The SMS sending method has not been defined into MAIN_SMS_SENDMODE", LOG_WARNING);
  196. $resultsender = array();
  197. $resultsender[0]->number = $this->fromsms;
  198. }
  199. if (is_array($resultsender) && count($resultsender) > 0) {
  200. print '<select name="fromsms" id="fromsms" class="flat">';
  201. foreach ($resultsender as $obj) {
  202. print '<option value="'.$obj->number.'">'.$obj->number.'</option>';
  203. }
  204. print '</select>';
  205. } else {
  206. print '<span class="error">'.$langs->trans("SmsNoPossibleSenderFound");
  207. if (is_object($sms) && !empty($sms->error)) {
  208. print ' '.$sms->error;
  209. }
  210. print '</span>';
  211. }
  212. print '</td>';
  213. print "</tr>\n";
  214. }
  215. }
  216. // To (target)
  217. if ($this->withto || is_array($this->withto)) {
  218. print '<tr><td>';
  219. //$moretext=$langs->trans("YouCanUseCommaSeparatorForSeveralRecipients");
  220. $moretext = '';
  221. print $form->textwithpicto($langs->trans("SmsTo"), $moretext);
  222. print '</td><td>';
  223. if ($this->withtoreadonly) {
  224. print (!is_array($this->withto) && !is_numeric($this->withto)) ? $this->withto : "";
  225. } else {
  226. print '<input size="16" id="sendto" name="sendto" value="'.dol_escape_htmltag(!is_array($this->withto) && $this->withto != '1' ? (GETPOSTISSET("sendto") ? GETPOST("sendto") : $this->withto) : "+").'">';
  227. if (!empty($this->withtosocid) && $this->withtosocid > 0) {
  228. $liste = array();
  229. foreach ($soc->thirdparty_and_contact_phone_array() as $key => $value) {
  230. $liste[$key] = $value;
  231. }
  232. print " ".$langs->trans("or")." ";
  233. //var_dump($_REQUEST);exit;
  234. print $form->selectarray("receiver", $liste, GETPOST("receiver"), 1);
  235. }
  236. print ' <span class="opacitymedium">'.$langs->trans("SmsInfoNumero").'</span>';
  237. }
  238. print "</td></tr>\n";
  239. }
  240. // Message
  241. if ($this->withbody) {
  242. $defaultmessage = '';
  243. if ($this->param["models"] == 'body') {
  244. $defaultmessage = $this->withbody;
  245. }
  246. $defaultmessage = make_substitutions($defaultmessage, $this->substit);
  247. if (GETPOSTISSET("message")) {
  248. $defaultmessage = GETPOST("message", 'restricthtml');
  249. }
  250. $defaultmessage = str_replace('\n', "\n", $defaultmessage);
  251. print "<tr>";
  252. print '<td class="tdtop">'.$langs->trans("SmsText")."</td>";
  253. print "<td>";
  254. if ($this->withbodyreadonly) {
  255. print nl2br($defaultmessage);
  256. print '<input type="hidden" name="message" value="'.dol_escape_htmltag($defaultmessage).'">';
  257. } else {
  258. print '<textarea class="quatrevingtpercent" name="message" id="message" rows="'.ROWS_4.'" onkeyup="limitChars(this, 160, \'charlimitinfospan\')">'.$defaultmessage.'</textarea>';
  259. print '<div id="charlimitinfo" class="opacitymedium">'.$langs->trans("SmsInfoCharRemain").': <span id="charlimitinfospan">'.(160 - dol_strlen($defaultmessage)).'</span></div></td>';
  260. }
  261. print "</td></tr>\n";
  262. }
  263. print '
  264. <tr>
  265. <td>'.$langs->trans("DelayBeforeSending").':</td>
  266. <td> <input name="deferred" id="deferred" size="4" value="0"></td></tr>
  267. <tr><td>'.$langs->trans("Priority").' :</td><td>
  268. <select name="priority" id="priority" class="flat">
  269. <option value="0">high</option>
  270. <option value="1">medium</option>
  271. <option value="2" selected>low</option>
  272. <option value="3">veryLow</option>
  273. </select></td></tr>
  274. <tr><td>'.$langs->trans("Type").' :</td><td>
  275. <select name="class" id="class" class="flat">
  276. <option value="0">Flash</option>
  277. <option value="1" selected>Standard</option>
  278. <option value="2">SIM</option>
  279. <option value="3">ToolKit</option>
  280. </select></td></tr>
  281. <tr><td>'.$langs->trans("DisableStopIfSupported").' :</td><td>
  282. <select name="disablestop" id="disablestop" class="flat">
  283. <option value="0" selected>No</option>
  284. <option value="1" selected>Yes</option>
  285. </select></td></tr>';
  286. print "</table>\n";
  287. if ($showform) {
  288. print '<div class="center">';
  289. print '<input type="submit" class="button" name="sendmail" value="'.dol_escape_htmltag($langs->trans("SendSms")).'">';
  290. if ($this->withcancel) {
  291. print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  292. print '<input class="button button-cancel" type="submit" name="cancel" value="'.dol_escape_htmltag($langs->trans("Cancel")).'">';
  293. }
  294. print '</div>';
  295. print "</form>\n";
  296. }
  297. print "<!-- End form SMS -->\n";
  298. }
  299. }