sms.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 Frédéric France <frederic.france@netlogic.fr>
  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. */
  20. /**
  21. * \file htdocs/admin/sms.php
  22. * \brief Page to setup emails sending
  23. */
  24. require '../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  26. // Load translation files required by the page
  27. $langs->loadLangs(array("companies", "admin", "products", "sms", "other", "errors"));
  28. $action = GETPOST('action', 'aZ09');
  29. $cancel = GETPOST('cancel', 'aZ09');
  30. if (!$user->admin) {
  31. accessforbidden();
  32. }
  33. $substitutionarrayfortest = array(
  34. '__ID__' => 'TESTIdRecord',
  35. '__PHONEFROM__' => 'TESTPhoneFrom',
  36. '__PHONETO__' => 'TESTPhoneTo',
  37. '__LASTNAME__' => 'TESTLastname',
  38. '__FIRSTNAME__' => 'TESTFirstname'
  39. );
  40. /*
  41. * Actions
  42. */
  43. if ($action == 'update' && !$cancel) {
  44. dolibarr_set_const($db, "MAIN_DISABLE_ALL_SMS", GETPOST("MAIN_DISABLE_ALL_SMS", 'alphanohtml'), 'chaine', 0, '', $conf->entity);
  45. dolibarr_set_const($db, "MAIN_SMS_SENDMODE", GETPOST("MAIN_SMS_SENDMODE", 'alphanohtml'), 'chaine', 0, '', $conf->entity);
  46. dolibarr_set_const($db, "MAIN_MAIL_SMS_FROM", GETPOST("MAIN_MAIL_SMS_FROM", 'alphanohtml'), 'chaine', 0, '', $conf->entity);
  47. dolibarr_set_const($db, "MAIN_MAIL_SMS_INTL_PREFIX_DEFAULT", GETPOST("MAIN_MAIL_SMS_INTL_PREFIX_DEFAULT", 'alphanohtml'), 'chaine', 0, '', $conf->entity);
  48. header("Location: ".$_SERVER["PHP_SELF"]."?mainmenu=home&leftmenu=setup");
  49. exit;
  50. }
  51. // Send sms
  52. if ($action == 'send' && !$cancel) {
  53. $error = 0;
  54. $smsfrom = '';
  55. if (GETPOST("fromsms", 'alphanohtml')) {
  56. $smsfrom = GETPOST("fromsms", 'alphanohtml');
  57. }
  58. if (empty($smsfrom)) {
  59. $smsfrom = GETPOST("fromname", 'alphanohtml');
  60. }
  61. $sendto = GETPOST("sendto", 'alphanohtml');
  62. $body = GETPOST('message', 'alphanohtml');
  63. $deliveryreceipt = GETPOST("deliveryreceipt", 'alphanohtml');
  64. $deferred = GETPOST('deferred', 'alphanohtml');
  65. $priority = GETPOST('priority', 'alphanohtml');
  66. $class = GETPOST('class', 'alphanohtml');
  67. $errors_to = GETPOST("errorstosms", 'alphanohtml');
  68. // Create form object
  69. include_once DOL_DOCUMENT_ROOT.'/core/class/html.formsms.class.php';
  70. $formsms = new FormSms($db);
  71. if (!empty($formsms->error)) {
  72. setEventMessages($formsms->error, $formsms->errors, 'errors');
  73. $action = 'test';
  74. $error++;
  75. }
  76. if (empty($body)) {
  77. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Message")), null, 'errors');
  78. $action = 'test';
  79. $error++;
  80. }
  81. if (empty($smsfrom) || !str_replace('+', '', $smsfrom)) {
  82. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("SmsFrom")), null, 'errors');
  83. $action = 'test';
  84. $error++;
  85. }
  86. if (empty($sendto) || !str_replace('+', '', $sendto)) {
  87. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("SmsTo")), null, 'errors');
  88. $action = 'test';
  89. $error++;
  90. }
  91. if (!$error) {
  92. // Make substitutions into message
  93. complete_substitutions_array($substitutionarrayfortest, $langs);
  94. $body = make_substitutions($body, $substitutionarrayfortest);
  95. require_once DOL_DOCUMENT_ROOT.'/core/class/CSMSFile.class.php';
  96. $smsfile = new CSMSFile($sendto, $smsfrom, $body, $deliveryreceipt, $deferred, $priority, $class); // This define OvhSms->login, pass, session and account
  97. $result = $smsfile->sendfile(); // This send SMS
  98. if ($result) {
  99. setEventMessages($langs->trans("SmsSuccessfulySent", $smsfrom, $sendto), null, 'mesgs');
  100. setEventMessages($smsfile->error, $smsfile->errors, 'mesgs');
  101. } else {
  102. setEventMessages($langs->trans("ResultKo"), null, 'errors');
  103. setEventMessages($smsfile->error, $smsfile->errors, 'errors');
  104. }
  105. $action = '';
  106. }
  107. }
  108. /*
  109. * View
  110. */
  111. $form = new Form($db);
  112. $linuxlike = 1;
  113. if (preg_match('/^win/i', PHP_OS)) {
  114. $linuxlike = 0;
  115. }
  116. if (preg_match('/^mac/i', PHP_OS)) {
  117. $linuxlike = 0;
  118. }
  119. $wikihelp = 'EN:Setup Sms|FR:Paramétrage Sms|ES:Configuración Sms';
  120. llxHeader('', $langs->trans("Setup"), $wikihelp);
  121. print load_fiche_titre($langs->trans("SmsSetup"), '', 'title_setup');
  122. print '<span class="opacitymedium">'.$langs->trans("SmsDesc")."</span><br>\n";
  123. print "<br>\n";
  124. // List of sending methods
  125. $listofmethods = (is_array($conf->modules_parts['sms']) ? $conf->modules_parts['sms'] : array());
  126. asort($listofmethods);
  127. if (!count($listofmethods)) {
  128. $descnosms = $langs->trans("NoSmsEngine", '{Dolistore}');
  129. $descnosms = str_replace('{Dolistore}', '<a href="https://www.dolistore.com/search.php?orderby=position&orderway=desc&search_query=smsmanager">DoliStore</a>', $descnosms);
  130. print '<div class="warning">'.$descnosms.'</div>';
  131. }
  132. if ($action == 'edit') {
  133. print '<form method="post" action="'.$_SERVER["PHP_SELF"].'">';
  134. print '<input type="hidden" name="token" value="'.newToken().'">';
  135. print '<input type="hidden" name="action" value="update">';
  136. clearstatcache();
  137. print '<table class="noborder centpercent">';
  138. print '<tr class="liste_titre"><td>'.$langs->trans("Parameter").'</td><td>'.$langs->trans("Value").'</td></tr>';
  139. // Disable
  140. print '<tr class="oddeven"><td>'.$langs->trans("MAIN_DISABLE_ALL_SMS").'</td><td>';
  141. print $form->selectyesno('MAIN_DISABLE_ALL_SMS', getDolGlobalString('MAIN_DISABLE_ALL_SMS'), 1);
  142. print '</td></tr>';
  143. // Separator
  144. print '<tr class="oddeven"><td colspan="2">&nbsp;</td></tr>';
  145. // Method
  146. print '<tr class="oddeven"><td>'.$langs->trans("MAIN_SMS_SENDMODE").'</td><td>';
  147. if (count($listofmethods)) {
  148. print $form->selectarray('MAIN_SMS_SENDMODE', $listofmethods, $conf->global->MAIN_SMS_SENDMODE, 1);
  149. } else {
  150. print '<span class="error">'.$langs->trans("None").'</span>';
  151. }
  152. print '</td></tr>';
  153. // From
  154. print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_SMS_FROM", $langs->transnoentities("Undefined")).'</td>';
  155. print '<td><input class="flat" name="MAIN_MAIL_SMS_FROM" size="32" value="'.getDolGlobalString('MAIN_MAIL_SMS_FROM');
  156. print '"></td></tr>';
  157. // Default prefix
  158. print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_SMS_INTL_PREFIX_DEFAULT", $langs->transnoentities("Undefined")).'</td>';
  159. print '<td><input class="flat" name="MAIN_MAIL_SMS_INTL_PREFIX_DEFAULT" size="5" value="'.getDolGlobalString('MAIN_MAIL_SMS_INTL_PREFIX_DEFAULT');
  160. print '"></td></tr>';
  161. // Autocopy to
  162. /*
  163. print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_AUTOCOPY_TO").'</td>';
  164. print '<td><input class="flat" name="MAIN_MAIL_AUTOCOPY_TO" size="32" value="' . $conf->global->MAIN_MAIL_AUTOCOPY_TO;
  165. print '"></td></tr>';
  166. */
  167. print '</table>';
  168. print '<br><div class="center">';
  169. print '<input class="button button-save" type="submit" name="save" value="'.$langs->trans("Save").'"'.(!count($listofmethods) ? ' disabled' : '').'>';
  170. print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  171. print '<input class="button button-cancel" type="submit" name="cancel" value="'.$langs->trans("Cancel").'">';
  172. print '</div>';
  173. print '</form>';
  174. print '<br>';
  175. } else {
  176. print '<table class="noborder centpercent">';
  177. print '<tr class="liste_titre"><td>'.$langs->trans("Parameter").'</td><td>'.$langs->trans("Value").'</td></tr>';
  178. // Disable
  179. print '<tr class="oddeven"><td>'.$langs->trans("MAIN_DISABLE_ALL_SMS").'</td><td>'.yn(getDolGlobalString('MAIN_DISABLE_ALL_SMS')).'</td></tr>';
  180. // Separator
  181. print '<tr class="oddeven"><td colspan="2">&nbsp;</td></tr>';
  182. // Method
  183. print '<tr class="oddeven"><td>'.$langs->trans("MAIN_SMS_SENDMODE").'</td><td>';
  184. $text = empty(getDolGlobalString('MAIN_SMS_SENDMODE')) ? '' : $listofmethods[getDolGlobalString('MAIN_SMS_SENDMODE')];
  185. if (empty($text)) {
  186. $text = $langs->trans("Undefined").' '.img_warning();
  187. }
  188. print $text;
  189. print '</td></tr>';
  190. // From
  191. print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_SMS_FROM", $langs->transnoentities("Undefined")).'</td>';
  192. print '<td>'.getDolGlobalString('MAIN_MAIL_SMS_FROM');
  193. if (!empty($conf->global->MAIN_MAIL_SMS_FROM) && !isValidPhone($conf->global->MAIN_MAIL_SMS_FROM)) {
  194. print ' '.img_warning($langs->trans("ErrorBadPhone"));
  195. }
  196. print '</td></tr>';
  197. // Default prefix
  198. print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_SMS_INTL_PREFIX_DEFAULT", $langs->transnoentities("Undefined")).'</td>';
  199. print '<td>'.getDolGlobalString('MAIN_MAIL_SMS_INTL_PREFIX_DEFAULT');
  200. if (!empty($conf->global->MAIN_MAIL_SMS_INTL_PREFIX_DEFAULT) && !isValidPhone($conf->global->MAIN_MAIL_SMS_INTL_PREFIX_DEFAULT)) {
  201. print ' '.img_warning($langs->trans("ErrorBadPhone"));
  202. }
  203. print '</td></tr>';
  204. // Autocopy to
  205. /*
  206. print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_AUTOCOPY_TO").'</td>';
  207. print '<td>'.$conf->global->MAIN_MAIL_AUTOCOPY_TO;
  208. if (!empty($conf->global->MAIN_MAIL_AUTOCOPY_TO) && ! isValidEmail($conf->global->MAIN_MAIL_AUTOCOPY_TO)) print img_warning($langs->trans("ErrorBadEMail"));
  209. print '</td></tr>';
  210. */
  211. print '</table>';
  212. // Buttons for actions
  213. print '<div class="tabsAction">';
  214. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=edit">'.$langs->trans("Modify").'</a>';
  215. /*if ($conf->global->MAIN_SMS_SENDMODE != 'mail' || ! $linuxlike)
  216. {
  217. if (function_exists('fsockopen') && $port && $server)
  218. {
  219. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=testconnect">'.$langs->trans("DoTestServerAvailability").'</a>';
  220. }
  221. }
  222. else
  223. {
  224. print '<a class="butActionRefused classfortooltip" href="#" title="'.$langs->trans("FeatureNotAvailableOnLinux").'">'.$langs->trans("DoTestServerAvailability").'</a>';
  225. }*/
  226. if (count($listofmethods) && !empty($conf->global->MAIN_SMS_SENDMODE)) {
  227. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=test&amp;mode=init">'.$langs->trans("DoTestSend").'</a>';
  228. } else {
  229. print '<a class="butActionRefused classfortooltip" href="#">'.$langs->trans("DoTestSend").'</a>';
  230. }
  231. print '</div>';
  232. // Affichage formulaire de TEST simple
  233. if ($action == 'test') {
  234. print '<br>';
  235. print load_fiche_titre($langs->trans("DoTestSend"));
  236. // Cree l'objet formulaire mail
  237. include_once DOL_DOCUMENT_ROOT.'/core/class/html.formsms.class.php';
  238. $formsms = new FormSms($db);
  239. $formsms->fromtype = 'user';
  240. $formsms->fromid = $user->id;
  241. $formsms->fromsms = (GETPOSTISSET('fromsms') ? GETPOST('fromsms') : ($conf->global->MAIN_MAIL_SMS_FROM ? $conf->global->MAIN_MAIL_SMS_FROM : $user->user_mobile));
  242. $formsms->withfromreadonly = 0;
  243. $formsms->withsubstit = 0;
  244. $formsms->withfrom = 1;
  245. $formsms->withto = (GETPOSTISSET('sendto') ? GETPOST('sendto') : ($user->user_mobile ? $user->user_mobile : 1));
  246. $formsms->withbody = (GETPOSTISSET('message') ? (!GETPOST('message') ? 1 : GETPOST('message')) : $langs->trans("ThisIsATestMessage"));
  247. $formsms->withbodyreadonly = 0;
  248. $formsms->withcancel = 1;
  249. // Tableau des substitutions
  250. $formsms->substit = $substitutionarrayfortest;
  251. // Tableau des parametres complementaires du post
  252. $formsms->param["action"] = "send";
  253. $formsms->param["models"] = "body";
  254. $formsms->param["smsid"] = 0;
  255. $formsms->param["returnurl"] = $_SERVER["PHP_SELF"];
  256. $formsms->show_form();
  257. print '<br>';
  258. }
  259. }
  260. // End of page
  261. llxFooter();
  262. $db->close();