Zionetrix::Git
Repositories
Help
Report an Issue
manage-asterisk-blacklist
Code
Commits
Branches
Tags
Search
Tree:
7146eff
Branches
Tags
master
manage-asterisk-blacklist
include.php
Initial commit
Benjamin Renard
commited
7146eff
at 2016-07-21 18:04:07
include.php
Blame
History
Raw
<?php /** * Links : * * PAMI : https://github.com/marcelog/PAMI * * log4php : https://logging.apache.org/log4php * * AMI : http://ofps.oreilly.com/titles/9781449332426/asterisk-AMI.html **/ require_once 'PAMI/Autoloader/Autoloader.php'; PAMI\Autoloader\Autoloader::register(); use PAMI\Client\Impl\ClientImpl as PamiClient; use PAMI\Message\Action\CoreShowChannelsAction; use PAMI\Message\Action\SIPPeersAction; use PAMI\Message\Action\QueueStatusAction; use PAMI\Message\Action\MailboxCountAction; use PAMI\Message\Action\OriginateAction; use PAMI\Message\Action\HangupAction; use PAMI\Message\Action\CommandAction; use PAMI\Message\Action\DBGetAction; use PAMI\Message\Action\DBPutAction; use PAMI\Message\Action\DBDelAction; class MyAMI { private static $opts; private static $client; private static $started; function configure($opts) { self::$opts = $opts; } function start() { if (self :: $started) return True; if (self::$opts) { try { self :: initAMI(); self :: $client->open(); self :: $started=True; return True; } catch(Exception $e) { self :: log_error('Error connecting to AMI : '.$e->getMessage()); } } return; } function initAMI() { self :: initLog(); self::$client = new PamiClient(self::$opts['ami']); } function initLog() { require('log4php/Logger.php'); Logger::configure(array( 'rootLogger' => array( 'appenders' => array('default'), ), 'appenders' => array( 'default' => array( 'class' => 'LoggerAppenderFile', 'layout' => array( 'class' => 'LoggerLayoutSimple' ), 'params' => array( 'file' => self :: $opts["logFile"], 'append' => true ), 'threshold' => (self :: $opts["debug"]?"DEBUG":"WARN") ) ) )); } function stop() { try { self :: $client->close(); return True; } catch (Exception $e) { self :: log_error('Error closing connection to AMI : '.$e->getMessage()); } return; } function listSIPPeers() { if (!self :: start()) { return; } $response = self :: $client->send(new SIPPeersAction()); if ($response->isSuccess()) { $peers=array(); foreach($response->getEvents() as $event) { if ($event->getName() == "PeerEntry") { $peers[$event->getObjectName()]=$event->getKeys(); } } return $peers; } else { self :: log_action_error('listSIPPeers',$response->getMessage()); } return; } function listChannels() { if (!self :: start()) { return; } $response = self :: $client->send(new CoreShowChannelsAction()); if ($response->isSuccess()) { $channels=array(); foreach($response->getEvents() as $event) { if ($event->getName() == "CoreShowChannel") { $channels[$event->getChannel()]=$event->getKeys(); } } return $channels; } else { self :: log_action_error('listChannels',$response->getMessage()); } return; } function listQueues() { if (!self :: start()) { return; } $response = self :: $client->send(new QueueStatusAction()); if ($response->isSuccess()) { $queues=array(); foreach($response->getEvents() as $event) { if ($event->getName() == "QueueParams") { $queues[$event->getQueue()]=array( 'params' => $event->getKeys(), 'members' => array(), 'calls' => array() ); } elseif ($event->getName() == "QueueMember") { $queues[$event->getQueue()]['members'][]=$event->getKeys(); } elseif ($event->getName() == "QueueEntry") { $queues[$event->getKey('Queue')]['calls'][]=$event->getKeys(); } } return $queues; } else { self :: log_action_error('listQueues',$response->getMessage()); } return; } function dbGetTree() { if (!self :: start()) { return; } $response = self :: $client->send(new CommandAction('database show')); if ($response->isSuccess()) { $tree=array(); foreach (explode("\n",$response->getRawContent()) as $line) { if (preg_match('/^\/([^ ]*) *: (.*) *$/',$line,$m)) { $ret=&$tree; $keys=explode('/',$m[1]); for ($i=0;$i<(count($keys)-1);$i++) { if (!is_array($ret[$keys[$i]])) { $ret[$keys[$i]]=array(); } $ret=&$ret[$keys[$i]]; } $ret[$keys[(count($keys)-1)]]=trim($m[2]); } } return $tree; } else { self :: log_action_error('getDBTree',$response->getMessage()); } return; } function dbGet($family,$key) { if (!self :: start()) { return; } $response = self :: $client->send(new DBGetAction($family, $key)); if ($response->isSuccess()) { return $response->getKeys(); } else { self :: log_action_error('dbGet',$response->getMessage()); } return; } function dbPut($family,$key,$value) { if (!self :: start()) { return; } $response = self :: $client->send(new DBPutAction($family, $key, $value)); if ($response->isSuccess()) { return true; } else { self :: log_action_error('dbPut',$response->getMessage()); } return; } function dbDel($family,$key) { if (!self :: start()) { return; } $response = self :: $client->send(new DBDelAction($family, $key)); if ($response->isSuccess()) { return true; } else { self :: log_action_error('dbDel',$response->getMessage()); } return; } function mailboxInfos($mailbox) { if (!self :: start()) { return; } $response = self :: $client->send(new MailboxCountAction($mailbox)); if ($response->isSuccess()) { return $response->getKeys(); } else { self :: log_action_error('mailboxInfos',$response->getMessage()); } return; } function originate($channel,$params) { if (!self :: start()) { return; } $act=new OriginateAction($channel); if (is_array($params)) { foreach($params as $key => $val) { switch($key) { case 'context': $act -> setContext($val); ;; case 'extension': $act -> setExtension($val); ;; case 'priority': $act -> setPriority($val); ;; case 'callerid': $act -> setCallerId($val); ;; } } } $response = self :: $client->send($act); if ($response->isSuccess()) { return True; } else { self :: log_action_error('mailboxInfos',$response->getMessage()); } return; } function channelHangup($channel) { if (!self :: start()) { return; } $response = self :: $client->send(new HangupAction($channel)); if ($response->isSuccess()) { return True; } else { self :: log_action_error('channelHangup',$response->getMessage()); } return; } function log_action_error($action,$msg) { self :: log_error("Asterisk return error during $action : $msg"); } function log_error($msg) { error_log("MyAMI Error : $msg"); die("$msg"); } }