wrapper.php 6.4 KB

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