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