wrapper.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /* Copyright (C) 2009-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/asterisk/wrapper.php
  19. * \brief File that is entry point to call an Asterisk server
  20. * \remarks To be used, an Asterisk user must be created by adding this
  21. * in /etc/asterisk/manager.conf
  22. * [dolibarr]
  23. * secret = dolibarr
  24. * deny=0.0.0.0/0.0.0.0
  25. * permit=127.0.0.1/255.255.255.0
  26. * read = system,call,log,verbose,command,agent,user
  27. * write = system,call,log,verbose,command,agent,user
  28. */
  29. if (!defined('NOREQUIRESOC')) {
  30. define('NOREQUIRESOC', '1');
  31. }
  32. if (!defined('NOREQUIRETRAN')) {
  33. define('NOREQUIRETRAN', '1');
  34. }
  35. if (!defined('NOCSRFCHECK')) {
  36. define('NOCSRFCHECK', '1');
  37. }
  38. if (!defined('NOTOKENRENEWAL')) {
  39. define('NOTOKENRENEWAL', '1');
  40. }
  41. if (!defined('NOREQUIREMENU')) {
  42. define('NOREQUIREMENU', '1');
  43. }
  44. if (!defined('NOREQUIREHTML')) {
  45. define('NOREQUIREHTML', '1');
  46. }
  47. if (!defined('NOREQUIREAJAX')) {
  48. define('NOREQUIREAJAX', '1');
  49. }
  50. /**
  51. * Empty header
  52. *
  53. * @ignore
  54. * @return void
  55. */
  56. function llxHeader()
  57. {
  58. print '<html>'."\n";
  59. print '<head>'."\n";
  60. print '<title>Asterisk redirection from Dolibarr...</title>'."\n";
  61. print '</head>'."\n";
  62. }
  63. /**
  64. * Empty footer
  65. *
  66. * @ignore
  67. * @return void
  68. */
  69. function llxFooter()
  70. {
  71. print "\n".'</html>'."\n";
  72. }
  73. require_once '../main.inc.php';
  74. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions.lib.php';
  75. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  76. // Security check
  77. if (empty($conf->clicktodial->enabled)) {
  78. accessforbidden();
  79. exit;
  80. }
  81. // Define Asterisk setup
  82. if (!isset($conf->global->ASTERISK_HOST)) {
  83. $conf->global->ASTERISK_HOST = "127.0.0.1";
  84. }
  85. if (!isset($conf->global->ASTERISK_TYPE)) {
  86. $conf->global->ASTERISK_TYPE = "SIP/";
  87. }
  88. if (!isset($conf->global->ASTERISK_INDICATIF)) {
  89. $conf->global->ASTERISK_INDICATIF = "0";
  90. }
  91. if (!isset($conf->global->ASTERISK_PORT)) {
  92. $conf->global->ASTERISK_PORT = 5038;
  93. }
  94. if ($conf->global->ASTERISK_INDICATIF == 'NONE') {
  95. $conf->global->ASTERISK_INDICATIF = '';
  96. }
  97. if (!isset($conf->global->ASTERISK_CONTEXT)) {
  98. $conf->global->ASTERISK_CONTEXT = "from-internal";
  99. }
  100. if (!isset($conf->global->ASTERISK_WAIT_TIME)) {
  101. $conf->global->ASTERISK_WAIT_TIME = "30";
  102. }
  103. if (!isset($conf->global->ASTERISK_PRIORITY)) {
  104. $conf->global->ASTERISK_PRIORITY = "1";
  105. }
  106. if (!isset($conf->global->ASTERISK_MAX_RETRY)) {
  107. $conf->global->ASTERISK_MAX_RETRY = "2";
  108. }
  109. $login = GETPOST('login', 'alphanohtml');
  110. $password = GETPOST('password', 'none');
  111. $caller = GETPOST('caller', 'alphanohtml');
  112. $called = GETPOST('called', 'alphanohtml');
  113. // IP address of Asterisk server
  114. $strHost = $conf->global->ASTERISK_HOST;
  115. // Spécifiez le type d'extension par laquelle vous poste est connecte.
  116. // ex: SIP/, IAX2/, ZAP/, etc
  117. $channel = $conf->global->ASTERISK_TYPE;
  118. // Indicatif de la ligne sortante
  119. $prefix = $conf->global->ASTERISK_INDICATIF;
  120. // Port
  121. $port = $conf->global->ASTERISK_PORT;
  122. // Context ( generalement from-internal )
  123. $strContext = $conf->global->ASTERISK_CONTEXT;
  124. // Delai d'attente avant de raccrocher
  125. $strWaitTime = $conf->global->ASTERISK_WAIT_TIME;
  126. // Priority
  127. $strPriority = $conf->global->ASTERISK_PRIORITY;
  128. // Nomber of try
  129. $strMaxRetry = $conf->global->ASTERISK_MAX_RETRY;
  130. /*
  131. * View
  132. */
  133. llxHeader();
  134. $sql = "SELECT s.nom as name FROM ".MAIN_DB_PREFIX."societe as s";
  135. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."socpeople as sp ON sp.fk_soc = s.rowid";
  136. $sql .= " WHERE s.entity IN (".getEntity('societe').")";
  137. $sql .= " AND (s.phone='".$db->escape($called)."'";
  138. $sql .= " OR sp.phone='".$db->escape($called)."'";
  139. $sql .= " OR sp.phone_perso='".$db->escape($called)."'";
  140. $sql .= " OR sp.phone_mobile='".$db->escape($called)."')";
  141. $sql .= $db->plimit(1);
  142. dol_syslog('click to dial search information with phone '.$called, LOG_DEBUG);
  143. $resql = $db->query($sql);
  144. if ($resql) {
  145. $obj = $db->fetch_object($resql);
  146. if ($obj) {
  147. $found = $obj->name;
  148. } else {
  149. $found = 'Not found';
  150. }
  151. $db->free($resql);
  152. } else {
  153. dol_print_error($db, 'Error');
  154. $found = 'Error';
  155. }
  156. $number = strtolower($called);
  157. $pos = strpos($number, "local");
  158. if (!empty($number)) {
  159. if ($pos === false) {
  160. $errno = 0;
  161. $errstr = 0;
  162. $strCallerId = "Dolibarr caller $found <".strtolower($number).">";
  163. $oSocket = @fsockopen($strHost, $port, $errno, $errstr, 10);
  164. if (!$oSocket) {
  165. print '<body>'."\n";
  166. $txt = "Failed to execute fsockopen($strHost, $port, \$errno, \$errstr, 10)<br>\n";
  167. print $txt;
  168. dol_syslog($txt, LOG_ERR);
  169. $txt = $errstr." (".$errno.")<br>\n";
  170. print $txt;
  171. dol_syslog($txt, LOG_ERR);
  172. print '</body>'."\n";
  173. } else {
  174. $txt = "Call Asterisk dialer for caller: ".$caller.", called: ".$called." clicktodiallogin: ".$login;
  175. dol_syslog($txt);
  176. print '<body onload="javascript:history.go(-1);">'."\n";
  177. print '<!-- '.$txt.' -->';
  178. fputs($oSocket, "Action: login\r\n");
  179. fputs($oSocket, "Events: off\r\n");
  180. fputs($oSocket, "Username: $login\r\n");
  181. fputs($oSocket, "Secret: $password\r\n\r\n");
  182. fputs($oSocket, "Action: originate\r\n");
  183. fputs($oSocket, "Channel: ".$channel.$caller."\r\n");
  184. fputs($oSocket, "WaitTime: $strWaitTime\r\n");
  185. fputs($oSocket, "CallerId: $strCallerId\r\n");
  186. fputs($oSocket, "Exten: ".$prefix.$number."\r\n");
  187. fputs($oSocket, "Context: $strContext\r\n");
  188. fputs($oSocket, "Priority: $strPriority\r\n\r\n");
  189. fputs($oSocket, "Action: Logoff\r\n\r\n");
  190. sleep(2);
  191. fclose($oSocket);
  192. print '</body>'."\n";
  193. }
  194. }
  195. } else {
  196. print 'Bad parameters in URL. Must be '.dol_escape_htmltag($_SERVER['PHP_SELF']).'?caller=99999&called=99999&login=xxxxx&password=xxxxx';
  197. }
  198. // End of page
  199. llxFooter();
  200. $db->close();