wrapper.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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('NOTOKENRENEWAL')) {
  36. define('NOTOKENRENEWAL', '1');
  37. }
  38. if (!defined('NOREQUIREMENU')) {
  39. define('NOREQUIREMENU', '1');
  40. }
  41. if (!defined('NOREQUIREHTML')) {
  42. define('NOREQUIREHTML', '1');
  43. }
  44. if (!defined('NOREQUIREAJAX')) {
  45. define('NOREQUIREAJAX', '1');
  46. }
  47. /**
  48. * Empty header
  49. *
  50. * @ignore
  51. * @return void
  52. */
  53. function llxHeader()
  54. {
  55. print '<html>'."\n";
  56. print '<head>'."\n";
  57. print '<title>Asterisk redirection from Dolibarr...</title>'."\n";
  58. print '</head>'."\n";
  59. }
  60. /**
  61. * Empty footer
  62. *
  63. * @ignore
  64. * @return void
  65. */
  66. function llxFooter()
  67. {
  68. print "\n".'</html>'."\n";
  69. }
  70. require_once '../main.inc.php';
  71. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions.lib.php';
  72. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  73. // Security check
  74. if (empty($conf->clicktodial->enabled)) {
  75. accessforbidden();
  76. exit;
  77. }
  78. // Define Asterisk setup
  79. if (!isset($conf->global->ASTERISK_HOST)) {
  80. $conf->global->ASTERISK_HOST = "127.0.0.1";
  81. }
  82. if (!isset($conf->global->ASTERISK_TYPE)) {
  83. $conf->global->ASTERISK_TYPE = "SIP/";
  84. }
  85. if (!isset($conf->global->ASTERISK_INDICATIF)) {
  86. $conf->global->ASTERISK_INDICATIF = "0";
  87. }
  88. if (!isset($conf->global->ASTERISK_PORT)) {
  89. $conf->global->ASTERISK_PORT = 5038;
  90. }
  91. if (getDolGlobalString('ASTERISK_INDICATIF') == 'NONE') {
  92. $conf->global->ASTERISK_INDICATIF = '';
  93. }
  94. if (!isset($conf->global->ASTERISK_CONTEXT)) {
  95. $conf->global->ASTERISK_CONTEXT = "from-internal";
  96. }
  97. if (!isset($conf->global->ASTERISK_WAIT_TIME)) {
  98. $conf->global->ASTERISK_WAIT_TIME = "30";
  99. }
  100. if (!isset($conf->global->ASTERISK_PRIORITY)) {
  101. $conf->global->ASTERISK_PRIORITY = "1";
  102. }
  103. if (!isset($conf->global->ASTERISK_MAX_RETRY)) {
  104. $conf->global->ASTERISK_MAX_RETRY = "2";
  105. }
  106. $login = GETPOST('login', 'alphanohtml');
  107. $password = GETPOST('password', 'none');
  108. $caller = GETPOST('caller', 'alphanohtml');
  109. $called = GETPOST('called', 'alphanohtml');
  110. // IP address of Asterisk server
  111. $strHost = $conf->global->ASTERISK_HOST;
  112. // Specify the type of extension through which your extension is connected.
  113. // ex: SIP/, IAX2/, ZAP/, etc
  114. $channel = $conf->global->ASTERISK_TYPE;
  115. // Outgoing call sign
  116. $prefix = $conf->global->ASTERISK_INDICATIF;
  117. // Asterisk Port
  118. $port = $conf->global->ASTERISK_PORT;
  119. // Context ( generalement from-internal )
  120. $strContext = $conf->global->ASTERISK_CONTEXT;
  121. // Waiting time before hanging up
  122. $strWaitTime = $conf->global->ASTERISK_WAIT_TIME;
  123. // Priority
  124. $strPriority = $conf->global->ASTERISK_PRIORITY;
  125. // Number of call attempts
  126. $strMaxRetry = $conf->global->ASTERISK_MAX_RETRY;
  127. /*
  128. * View
  129. */
  130. llxHeader();
  131. $sql = "SELECT s.nom as name FROM ".MAIN_DB_PREFIX."societe as s";
  132. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."socpeople as sp ON sp.fk_soc = s.rowid";
  133. $sql .= " WHERE s.entity IN (".getEntity('societe').")";
  134. $sql .= " AND (s.phone='".$db->escape($called)."'";
  135. $sql .= " OR sp.phone='".$db->escape($called)."'";
  136. $sql .= " OR sp.phone_perso='".$db->escape($called)."'";
  137. $sql .= " OR sp.phone_mobile='".$db->escape($called)."')";
  138. $sql .= $db->plimit(1);
  139. dol_syslog('click to dial search information with phone '.$called, LOG_DEBUG);
  140. $resql = $db->query($sql);
  141. if ($resql) {
  142. $obj = $db->fetch_object($resql);
  143. if ($obj) {
  144. $found = $obj->name;
  145. } else {
  146. $found = 'Not found';
  147. }
  148. $db->free($resql);
  149. } else {
  150. dol_print_error($db, 'Error');
  151. $found = 'Error';
  152. }
  153. $number = strtolower($called);
  154. $pos = strpos($number, "local");
  155. if (!empty($number)) {
  156. if ($pos === false) {
  157. $errno = 0;
  158. $errstr = 0;
  159. $strCallerId = "Dolibarr caller $found <".strtolower($number).">";
  160. $oSocket = @fsockopen($strHost, $port, $errno, $errstr, 10);
  161. if (!$oSocket) {
  162. print '<body>'."\n";
  163. $txt = "Failed to execute fsockopen($strHost, $port, \$errno, \$errstr, 10)<br>\n";
  164. print $txt;
  165. dol_syslog($txt, LOG_ERR);
  166. $txt = $errstr." (".$errno.")<br>\n";
  167. print $txt;
  168. dol_syslog($txt, LOG_ERR);
  169. print '</body>'."\n";
  170. } else {
  171. $txt = "Call Asterisk dialer for caller: ".$caller.", called: ".$called." clicktodiallogin: ".$login;
  172. dol_syslog($txt);
  173. print '<body onload="history.go(-1);">'."\n";
  174. print '<!-- '.$txt.' -->';
  175. fputs($oSocket, "Action: login\r\n");
  176. fputs($oSocket, "Events: off\r\n");
  177. fputs($oSocket, "Username: $login\r\n");
  178. fputs($oSocket, "Secret: $password\r\n\r\n");
  179. fputs($oSocket, "Action: originate\r\n");
  180. fputs($oSocket, "Channel: ".$channel.$caller."\r\n");
  181. fputs($oSocket, "WaitTime: $strWaitTime\r\n");
  182. fputs($oSocket, "CallerId: $strCallerId\r\n");
  183. fputs($oSocket, "Exten: ".$prefix.$number."\r\n");
  184. fputs($oSocket, "Context: $strContext\r\n");
  185. fputs($oSocket, "Priority: $strPriority\r\n\r\n");
  186. fputs($oSocket, "Action: Logoff\r\n\r\n");
  187. sleep(2);
  188. fclose($oSocket);
  189. print '</body>'."\n";
  190. }
  191. }
  192. } else {
  193. print 'Bad parameters in URL. Must be '.dol_escape_htmltag($_SERVER['PHP_SELF']).'?caller=99999&called=99999&login=xxxxx&password=xxxxx';
  194. }
  195. // End of page
  196. llxFooter();
  197. $db->close();