wrapper.php 6.1 KB

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