dolibarr/htdocs/asterisk/wrapper.php

196 lines
6.3 KiB
PHP
Raw Normal View History

2009-03-22 23:32:22 +01:00
<?php
2010-07-22 23:48:56 +02:00
/* Copyright (C) 2009-2010 Laurent Destailleur <eldy@users.sourceforge.net>
2009-03-22 23:32:22 +01:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
2009-03-22 23:32:22 +01:00
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2011-08-03 02:45:22 +02:00
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2009-03-22 23:32:22 +01:00
*/
/**
* \file htdocs/asterisk/wrapper.php
* \brief File that is entry point to call an Asterisk server
2009-03-22 23:32:22 +01:00
* \remarks To be used, an Asterisk user must be created by adding this
* in /etc/asterisk/manager.conf
2009-03-22 23:32:22 +01:00
* [dolibarr]
* secret = dolibarr
* deny=0.0.0.0/0.0.0.0
* permit=127.0.0.1/255.255.255.0
* read = system,call,log,verbose,command,agent,user
* write = system,call,log,verbose,command,agent,user
*/
if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC', '1');
if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN', '1');
if (! defined('NOCSRFCHECK')) define('NOCSRFCHECK', '1');
if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL', '1');
if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU', '1');
if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML', '1');
if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX', '1');
2010-07-27 01:50:09 +02:00
2011-08-28 03:54:20 +02:00
/**
* Empty header
*
* @return void
2011-08-28 03:54:20 +02:00
*/
function llxHeader()
{
print '<html>'."\n";
print '<head>'."\n";
print '<title>Asterisk redirection from Dolibarr...</title>'."\n";
print '</head>'."\n";
2010-07-27 01:50:09 +02:00
}
2011-08-28 03:54:20 +02:00
/**
* Empty footer
*
* @return void
2011-08-28 03:54:20 +02:00
*/
function llxFooter()
{
print "\n".'</html>'."\n";
2011-02-05 16:56:06 +01:00
}
2010-07-22 23:48:56 +02:00
require_once '../main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/functions.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
2009-03-22 23:32:22 +01:00
2010-07-22 23:48:56 +02:00
// Security check
2012-09-15 11:21:22 +02:00
if (empty($conf->clicktodial->enabled))
2010-07-27 01:50:09 +02:00
{
accessforbidden();
exit;
}
2009-03-22 23:32:22 +01:00
2010-07-27 01:50:09 +02:00
// Define Asterisk setup
if (! isset($conf->global->ASTERISK_HOST)) $conf->global->ASTERISK_HOST="127.0.0.1";
if (! isset($conf->global->ASTERISK_TYPE)) $conf->global->ASTERISK_TYPE="SIP/";
if (! isset($conf->global->ASTERISK_INDICATIF)) $conf->global->ASTERISK_INDICATIF="0";
2010-08-26 14:23:45 +02:00
if (! isset($conf->global->ASTERISK_PORT)) $conf->global->ASTERISK_PORT=5038;
2010-07-27 01:50:09 +02:00
if ($conf->global->ASTERISK_INDICATIF=='NONE') $conf->global->ASTERISK_INDICATIF='';
if (! isset($conf->global->ASTERISK_CONTEXT)) $conf->global->ASTERISK_CONTEXT="from-internal";
if (! isset($conf->global->ASTERISK_WAIT_TIME)) $conf->global->ASTERISK_WAIT_TIME="30";
if (! isset($conf->global->ASTERISK_PRIORITY)) $conf->global->ASTERISK_PRIORITY="1";
if (! isset($conf->global->ASTERISK_MAX_RETRY)) $conf->global->ASTERISK_MAX_RETRY="2";
2009-03-22 23:32:22 +01:00
2016-12-18 13:21:09 +01:00
$login = GETPOST('login');
$password = GETPOST('password');
$caller = GETPOST('caller');
$called = GETPOST('called');
2009-03-22 23:32:22 +01:00
2011-08-28 03:54:20 +02:00
// IP address of Asterisk server
2009-03-22 23:32:22 +01:00
$strHost = $conf->global->ASTERISK_HOST;
2011-08-28 03:54:20 +02:00
// Spécifiez le type d'extension par laquelle vous poste est connecte.
// ex: SIP/, IAX2/, ZAP/, etc
2009-03-22 23:32:22 +01:00
$channel = $conf->global->ASTERISK_TYPE;
2011-08-28 03:54:20 +02:00
// Indicatif de la ligne sortante
2010-07-27 01:50:09 +02:00
$prefix = $conf->global->ASTERISK_INDICATIF;
2011-08-28 03:54:20 +02:00
// Port
2010-07-27 01:50:09 +02:00
$port = $conf->global->ASTERISK_PORT;
2011-08-28 03:54:20 +02:00
// Context ( generalement from-internal )
$strContext = $conf->global->ASTERISK_CONTEXT;
2011-08-28 03:54:20 +02:00
// Delai d'attente avant de raccrocher
$strWaitTime = $conf->global->ASTERISK_WAIT_TIME;
2011-08-28 03:54:20 +02:00
// Priority
$strPriority = $conf->global->ASTERISK_PRIORITY;
2011-08-28 03:54:20 +02:00
// Nomber of try
$strMaxRetry = $conf->global->ASTERISK_MAX_RETRY;
2009-03-22 23:32:22 +01:00
2010-07-22 23:48:56 +02:00
/*
* View
*/
2010-07-27 01:50:09 +02:00
llxHeader();
2009-03-22 23:32:22 +01:00
2015-10-05 16:38:26 +02:00
$sql = "SELECT s.nom as name FROM ".MAIN_DB_PREFIX."societe as s";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."socpeople as sp ON sp.fk_soc = s.rowid";
$sql.= " WHERE s.entity IN (".getEntity('societe').")";
2015-10-05 16:38:26 +02:00
$sql.= " AND (s.phone='".$db->escape($called)."'";
$sql.= " OR sp.phone='".$db->escape($called)."'";
$sql.= " OR sp.phone_perso='".$db->escape($called)."'";
$sql.= " OR sp.phone_mobile='".$db->escape($called)."')";
$sql.= $db->plimit(1);
dol_syslog('click to dial search information with phone '.$called, LOG_DEBUG);
$resql = $db->query($sql);
if ($resql)
{
$obj = $db->fetch_object($resql);
if ($obj)
{
$found = $obj->name;
} else {
$found = $notfound;
}
$db->free($resql);
}
else
{
dol_print_error($db, 'Error');
2015-10-05 16:38:26 +02:00
$found = 'Error';
}
2011-08-28 03:54:20 +02:00
$number=strtolower($called);
$pos=strpos($number, "local");
2009-03-22 23:32:22 +01:00
if (! empty($number))
{
2011-08-28 03:54:20 +02:00
if ($pos===false)
{
$errno=0;
$errstr=0;
2015-10-05 16:38:26 +02:00
$strCallerId = "Dolibarr call $found <".strtolower($number).">";
2011-08-28 03:54:20 +02:00
$oSocket = @fsockopen($strHost, $port, $errno, $errstr, 10);
if (!$oSocket)
{
print '<body>'."\n";
$txt="Failed to execute fsockopen($strHost, $port, \$errno, \$errstr, 10)<br>\n";
print $txt;
dol_syslog($txt, LOG_ERR);
2011-08-28 03:54:20 +02:00
$txt=$errstr." (".$errno.")<br>\n";
print $txt;
dol_syslog($txt, LOG_ERR);
2011-08-28 03:54:20 +02:00
print '</body>'."\n";
}
else
{
$txt="Call Asterisk dialer for caller: ".$caller.", called: ".$called." clicktodiallogin: ".$login;
dol_syslog($txt);
print '<body onload="javascript:history.go(-1);">'."\n";
print '<!-- '.$txt.' -->';
fputs($oSocket, "Action: login\r\n");
fputs($oSocket, "Events: off\r\n");
fputs($oSocket, "Username: $login\r\n");
fputs($oSocket, "Secret: $password\r\n\r\n");
fputs($oSocket, "Action: originate\r\n");
fputs($oSocket, "Channel: ".$channel.$caller."\r\n");
fputs($oSocket, "WaitTime: $strWaitTime\r\n");
fputs($oSocket, "CallerId: $strCallerId\r\n");
fputs($oSocket, "Exten: ".$prefix.$number."\r\n");
fputs($oSocket, "Context: $strContext\r\n");
fputs($oSocket, "Priority: $strPriority\r\n\r\n");
fputs($oSocket, "Action: Logoff\r\n\r\n");
sleep(2);
fclose($oSocket);
print '</body>'."\n";
}
}
2009-03-22 23:32:22 +01:00
}
2010-07-27 01:50:09 +02:00
else {
2011-02-05 16:51:28 +01:00
print 'Bad parameters in URL. Must be '.$_SERVER['PHP_SELF'].'?caller=99999&called=99999&login=xxxxx&password=xxxxx';
2010-07-27 01:50:09 +02:00
}
2018-07-29 11:33:52 +02:00
// End of page
2011-02-05 16:56:06 +01:00
llxFooter();
$db->close();