wrapper.php 6.3 KB

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