sms.php 11 KB

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