dolibarr 25.0.0-alpha
sms.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2007-2011 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2009 Regis Houssin <regis.houssin@inodbox.com>
4 * Copyright (C) 2013 Juanjo Menent <jmenent@2byte.es>
5 * Copyright (C) 2020-2025 Frédéric France <frederic.france@free.fr>
6 * Copyright (C) 2026 MDW <mdeweerd@users.noreply.github.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
27// Load Dolibarr environment
28require '../main.inc.php';
29require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
30
39// Load translation files required by the page
40$langs->loadLangs(array("companies", "admin", "products", "sms", "other", "errors"));
41
42$action = GETPOST('action', 'aZ09');
43$cancel = GETPOST('cancel', 'alpha');
44
45if (!$user->admin) {
47}
48
49$substitutionarrayfortest = array(
50 '__ID__' => 'TESTIdRecord',
51 '__PHONEFROM__' => 'TESTPhoneFrom',
52 '__PHONETO__' => 'TESTPhoneTo',
53 '__LASTNAME__' => 'TESTLastname',
54 '__FIRSTNAME__' => 'TESTFirstname'
55);
56
57
58/*
59 * Actions
60 */
61
62if ($action == 'update' && !$cancel) {
63 dolibarr_set_const($db, "MAIN_DISABLE_ALL_SMS", GETPOST("MAIN_DISABLE_ALL_SMS", 'alphanohtml'), 'chaine', 0, '', $conf->entity);
64
65 dolibarr_set_const($db, "MAIN_SMS_SENDMODE", GETPOST("MAIN_SMS_SENDMODE", 'alphanohtml'), 'chaine', 0, '', $conf->entity);
66
67 dolibarr_set_const($db, "MAIN_SMS_FROM", GETPOST("MAIN_SMS_FROM", 'alphanohtml'), 'chaine', 0, '', $conf->entity);
68
69 header("Location: ".$_SERVER["PHP_SELF"]."?mainmenu=home&leftmenu=setup");
70 exit;
71}
72
73
74// Send sms
75if ($action == 'send' && !$cancel) {
76 $error = 0;
77
78 $smsfrom = '';
79 if (GETPOST("fromsms", 'alphanohtml')) {
80 $smsfrom = GETPOST("fromsms", 'alphanohtml');
81 }
82 if (empty($smsfrom)) {
83 $smsfrom = GETPOST("fromname", 'alphanohtml');
84 }
85 $sendto = GETPOST("sendto", 'alphanohtml');
86 $body = GETPOST('message', 'alphanohtml');
87 $deliveryreceipt = GETPOSTINT("deliveryreceipt");
88 $deferred = GETPOSTINT('deferred');
89 $priority = GETPOSTINT('priority');
90 $class = GETPOSTINT('class');
91 $errors_to = GETPOST("errorstosms", 'alphanohtml');
92
93 // Create form object
94 include_once DOL_DOCUMENT_ROOT.'/core/class/html.formsms.class.php';
95 $formsms = new FormSms($db);
96
97 if (!empty($formsms->error)) {
98 setEventMessages($formsms->error, $formsms->errors, 'errors');
99 $action = 'test';
100 $error++;
101 }
102 if (empty($body)) {
103 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Message")), null, 'errors');
104 $action = 'test';
105 $error++;
106 }
107 if (empty($smsfrom) || !str_replace('+', '', $smsfrom)) {
108 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("SmsFrom")), null, 'errors');
109 $action = 'test';
110 $error++;
111 }
112 if (empty($sendto) || !str_replace('+', '', $sendto)) {
113 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("SmsTo")), null, 'errors');
114 $action = 'test';
115 $error++;
116 }
117 if (!$error) {
118 // Make substitutions into message
119 complete_substitutions_array($substitutionarrayfortest, $langs);
120 $body = make_substitutions($body, $substitutionarrayfortest);
121
122 require_once DOL_DOCUMENT_ROOT.'/core/class/CSMSFile.class.php';
123 try {
124 $smsfile = new CSMSFile($sendto, $smsfrom, $body, $deliveryreceipt, $deferred, $priority, $class); // This define OvhSms->login, pass, session and account
125 } catch (Exception $e) {
126 $error++;
127 setEventMessages($e->getMessage(), null, 'error');
128 }
129 if (!$error) {
130 $result = $smsfile->sendfile(); // This send SMS
131 if (!$result) {
132 $error++;
133 setEventMessages($smsfile->error, $smsfile->errors, 'mesgs');
134 }
135 }
136
137 if (!$error) {
138 setEventMessages($langs->trans("SmsSuccessfulySent", $smsfrom, $sendto), null, 'mesgs');
139 } else {
140 setEventMessages($langs->trans("ResultKo"), null, 'errors');
141 }
142
143 $action = '';
144 }
145}
146
147
148
149/*
150 * View
151 */
152
153$form = new Form($db);
154
155$linuxlike = 1;
156if (preg_match('/^win/i', PHP_OS)) {
157 $linuxlike = 0;
158}
159if (preg_match('/^mac/i', PHP_OS)) {
160 $linuxlike = 0;
161}
162
163$wikihelp = 'EN:Setup Sms|FR:Paramétrage Sms|ES:Configuración Sms';
164llxHeader('', $langs->trans("Setup"), $wikihelp, '', 0, 0, '', '', '', 'mod-admin page-sms');
165
166print load_fiche_titre($langs->trans("SmsSetup"), '', 'title_setup');
167
168print '<span class="opacitymedium">'.$langs->trans("SmsDesc")."</span><br>\n";
169print "<br>\n";
170print "<br>\n";
171
172// List of sending methods
173$listofmethods = (is_array($conf->modules_parts['sms']) ? $conf->modules_parts['sms'] : array());
174asort($listofmethods);
175
176if (!count($listofmethods)) {
177 $descnosms = $langs->trans("NoSmsEngine", '{Dolistore}');
178 $descnosms = str_replace('{Dolistore}', '<a href="https://www.dolistore.com/index.php?controller=search&orderby=position&orderway=desc&website=marketplace&search_query=smsmanager">DoliStore</a>', $descnosms);
179 print '<div class="warning">'.$descnosms.'</div>';
180}
181
182if ($action == 'edit') {
183 print '<form method="post" action="'.dolBuildUrl($_SERVER["PHP_SELF"]).'">';
184 print '<input type="hidden" name="token" value="'.newToken().'">';
185 print '<input type="hidden" name="action" value="update">';
186
187 clearstatcache();
188
189 print '<table class="noborder centpercent">';
190 print '<tr class="liste_titre"><td>'.$langs->trans("Parameter").'</td><td></td></tr>';
191
192 // Disable
193 print '<tr class="oddeven"><td>'.$langs->trans("MAIN_DISABLE_ALL_SMS").'</td><td>';
194 print $form->selectyesno('MAIN_DISABLE_ALL_SMS', getDolGlobalString('MAIN_DISABLE_ALL_SMS'), 1);
195 print '</td></tr>';
196
197 // Separator
198 print '<tr class="oddeven"><td colspan="2">&nbsp;</td></tr>';
199
200 // Method
201 print '<tr class="oddeven"><td>'.$langs->trans("MAIN_SMS_SENDMODE").'</td><td>';
202 if (count($listofmethods)) {
203 print $form->selectarray('MAIN_SMS_SENDMODE', $listofmethods, getDolGlobalString('MAIN_SMS_SENDMODE'), 1);
204 } else {
205 print '<span class="error">'.$langs->trans("None").'</span>';
206 }
207 print '</td></tr>';
208
209 // From
210 print '<tr class="oddeven"><td>'.$langs->trans("MAIN_SMS_FROM", $langs->transnoentities("Undefined")).'</td>';
211 print '<td><input class="flat" name="MAIN_SMS_FROM" size="32" value="'.getDolGlobalString('MAIN_SMS_FROM');
212 print '"></td></tr>';
213
214 print '</table>';
215
216 print '<div class="center">';
217 print '<input class="button button-save" type="submit" name="save" value="'.$langs->trans("Save").'"'.(!count($listofmethods) ? ' disabled' : '').'>';
218 print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
219 print '<input class="button button-cancel" type="submit" name="cancel" value="'.$langs->trans("Cancel").'">';
220 print '</div>';
221
222 print '</form>';
223 print '<br>';
224} else {
225 print '<table class="noborder centpercent">';
226 print '<tr class="liste_titre"><td>'.$langs->trans("Parameter").'</td><td></td></tr>';
227
228 // Disable
229 print '<tr class="oddeven"><td>'.$langs->trans("MAIN_DISABLE_ALL_SMS").'</td><td>'.yn(getDolGlobalString('MAIN_DISABLE_ALL_SMS')).'</td></tr>';
230
231 // Separator
232 print '<tr class="oddeven"><td colspan="2">&nbsp;</td></tr>';
233
234 // Method
235 print '<tr class="oddeven"><td>'.$langs->trans("MAIN_SMS_SENDMODE").'</td><td>';
236 $text = getDolGlobalString('MAIN_SMS_SENDMODE') ? $listofmethods[getDolGlobalString('MAIN_SMS_SENDMODE')] : '';
237 if (empty($text)) {
238 $text = $langs->trans("Undefined").' '.img_warning();
239 }
240 print $text;
241 print '</td></tr>';
242
243 // From
244 print '<tr class="oddeven"><td>'.$langs->trans("MAIN_SMS_FROM", $langs->transnoentities("Undefined")).'</td>';
245 print '<td>'.getDolGlobalString('MAIN_SMS_FROM');
246 if (getDolGlobalString('MAIN_SMS_FROM') && !isValidPhone(getDolGlobalString('MAIN_SMS_FROM'))) {
247 print ' '.img_warning($langs->trans("ErrorBadPhone"));
248 }
249 print '</td></tr>';
250
251 print '</table>';
252
253
254 // Buttons for actions
255
256 print '<div class="tabsAction">';
257
258 if ($action != 'test') {
259 print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=edit">'.$langs->trans("Modify").'</a>';
260
261 if (count($listofmethods) && getDolGlobalString('MAIN_SMS_SENDMODE')) {
262 print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=test&mode=init&token='.newToken().'">'.$langs->trans("DoTestSend").'</a>';
263 } else {
264 print '<a class="butActionRefused classfortooltip" href="#">'.$langs->trans("DoTestSend").'</a>';
265 }
266 }
267
268 print '</div>';
269
270
271 // Display simple test form
272 if ($action == 'test') {
273 print '<br>';
274 print load_fiche_titre($langs->trans("DoTestSend"));
275
276 // Create SMS form object
277 include_once DOL_DOCUMENT_ROOT.'/core/class/html.formsms.class.php';
278 $formsms = new FormSms($db);
279 $formsms->fromtype = 'user';
280 $formsms->fromid = $user->id;
281 $formsms->fromsms = (GETPOSTISSET('fromsms') ? GETPOST('fromsms') : getDolGlobalString('MAIN_SMS_FROM', $user->user_mobile));
282 $formsms->withfromreadonly = 0;
283 $formsms->withsubstit = 0;
284 $formsms->withfrom = 1;
285 $formsms->withto = (GETPOSTISSET('sendto') ? GETPOST('sendto') : ($user->user_mobile ? $user->user_mobile : 1));
286 $formsms->withbody = (GETPOSTISSET('message') ? (!GETPOST('message') ? 1 : GETPOST('message')) : $langs->trans("ThisIsATestMessage"));
287 $formsms->withbodyreadonly = 0;
288 $formsms->withcancel = 1;
289 // Tableau des substitutions
290 $formsms->substit = $substitutionarrayfortest;
291 // Tableau des parameters complementaires du post
292 $formsms->param["action"] = "send";
293 $formsms->param["models"] = "body";
294 $formsms->param["smsid"] = 0;
295 $formsms->param["returnurl"] = $_SERVER["PHP_SELF"];
296
297 $formsms->show_form();
298
299 print '<br>';
300 }
301}
302
303// End of page
304llxFooter();
305$db->close();
dolibarr_set_const($db, $name, $value, $type='chaine', $visible=0, $note='', $entity=1)
Insert a parameter (key,value) into database (delete old key then insert it again).
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:91
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:73
Class to send SMS Usage: $smsfile = new CSMSFile($subject,$sendto,$replyto,$message,...
Class to manage generation of HTML components Only common components must be here.
Class for generating the SMS sending form Usage: $formsms = new FormSms($db) $formsms->proprietes=1 o...
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
img_warning($titlealt='default', $moreatt='', $morecss='pictowarning')
Show warning logo.
newToken()
Return the value of token currently saved into session with name 'newtoken'.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
isValidPhone($phone)
Return true if phone number syntax is ok TODO Decide what to do with this.
yn($yesno, $format=1, $color=0)
Return yes or no in current language.
complete_substitutions_array(&$substitutionarray, $outputlangs, $object=null, $parameters=null, $callfunc="completesubstitutionarray")
Complete the $substitutionarray with more entries coming from external module that had set the "subst...
make_substitutions($text, $substitutionarray, $outputlangs=null, $converttextinhtmlifnecessary=0)
Make substitution into a text string, replacing keys with vals from $substitutionarray (oldval=>newva...
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='', $morecssonpicto='widthpictotitle')
Load a title with picto.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.