+

Last commit for include.php: 7146effad4b1d46f0edd888b00be72bbd6de7eaa

Initial commit

Benjamin Renard [2016-07-21 18:04:07]
Initial commit
  1. <?php
  2.  
  3. /**
  4.  * Links :
  5.  * * PAMI : https://github.com/marcelog/PAMI
  6.  * * log4php : https://logging.apache.org/log4php
  7.  * * AMI : http://ofps.oreilly.com/titles/9781449332426/asterisk-AMI.html
  8.  **/
  9.  
  10. require_once 'PAMI/Autoloader/Autoloader.php';
  11.  
  12. PAMI\Autoloader\Autoloader::register();
  13.  
  14. use PAMI\Client\Impl\ClientImpl as PamiClient;
  15. use PAMI\Message\Action\CoreShowChannelsAction;
  16. use PAMI\Message\Action\SIPPeersAction;
  17. use PAMI\Message\Action\QueueStatusAction;
  18. use PAMI\Message\Action\MailboxCountAction;
  19. use PAMI\Message\Action\OriginateAction;
  20. use PAMI\Message\Action\HangupAction;
  21. use PAMI\Message\Action\CommandAction;
  22. use PAMI\Message\Action\DBGetAction;
  23. use PAMI\Message\Action\DBPutAction;
  24. use PAMI\Message\Action\DBDelAction;
  25.  
  26. class MyAMI {
  27.  
  28. private static $opts;
  29.  
  30. private static $client;
  31.  
  32. private static $started;
  33.  
  34. function configure($opts) {
  35. self::$opts = $opts;
  36. }
  37.  
  38. function start() {
  39. if (self :: $started)
  40. return True;
  41. if (self::$opts) {
  42. try {
  43. self :: initAMI();
  44. self :: $client->open();
  45. self :: $started=True;
  46. return True;
  47. }
  48. catch(Exception $e) {
  49. self :: log_error('Error connecting to AMI : '.$e->getMessage());
  50. }
  51. }
  52. return;
  53. }
  54.  
  55. function initAMI() {
  56. self :: initLog();
  57.  
  58. self::$client = new PamiClient(self::$opts['ami']);
  59. }
  60.  
  61. function initLog() {
  62. require('log4php/Logger.php');
  63.  
  64. Logger::configure(array(
  65. 'rootLogger' => array(
  66. 'appenders' => array('default'),
  67. ),
  68. 'appenders' => array(
  69. 'default' => array(
  70. 'class' => 'LoggerAppenderFile',
  71. 'layout' => array(
  72. 'class' => 'LoggerLayoutSimple'
  73. ),
  74. 'params' => array(
  75. 'file' => self :: $opts["logFile"],
  76. 'append' => true
  77. ),
  78. 'threshold' => (self :: $opts["debug"]?"DEBUG":"WARN")
  79. )
  80. )
  81. ));
  82. }
  83.  
  84. function stop() {
  85. try {
  86. self :: $client->close();
  87. return True;
  88. }
  89. catch (Exception $e) {
  90. self :: log_error('Error closing connection to AMI : '.$e->getMessage());
  91. }
  92. return;
  93. }
  94.  
  95. function listSIPPeers() {
  96. if (!self :: start()) {
  97. return;
  98. }
  99.  
  100. $response = self :: $client->send(new SIPPeersAction());
  101. if ($response->isSuccess()) {
  102. $peers=array();
  103. foreach($response->getEvents() as $event) {
  104. if ($event->getName() == "PeerEntry") {
  105. $peers[$event->getObjectName()]=$event->getKeys();
  106. }
  107. }
  108. return $peers;
  109. }
  110. else {
  111. self :: log_action_error('listSIPPeers',$response->getMessage());
  112. }
  113. return;
  114. }
  115.  
  116. function listChannels() {
  117. if (!self :: start()) {
  118. return;
  119. }
  120.  
  121. $response = self :: $client->send(new CoreShowChannelsAction());
  122. if ($response->isSuccess()) {
  123. $channels=array();
  124. foreach($response->getEvents() as $event) {
  125. if ($event->getName() == "CoreShowChannel") {
  126. $channels[$event->getChannel()]=$event->getKeys();
  127. }
  128. }
  129. return $channels;
  130. }
  131. else {
  132. self :: log_action_error('listChannels',$response->getMessage());
  133. }
  134. return;
  135. }
  136.  
  137. function listQueues() {
  138. if (!self :: start()) {
  139. return;
  140. }
  141.  
  142. $response = self :: $client->send(new QueueStatusAction());
  143. if ($response->isSuccess()) {
  144. $queues=array();
  145. foreach($response->getEvents() as $event) {
  146. if ($event->getName() == "QueueParams") {
  147. $queues[$event->getQueue()]=array(
  148. 'params' => $event->getKeys(),
  149. 'members' => array(),
  150. 'calls' => array()
  151. );
  152. }
  153. elseif ($event->getName() == "QueueMember") {
  154. $queues[$event->getQueue()]['members'][]=$event->getKeys();
  155. }
  156. elseif ($event->getName() == "QueueEntry") {
  157. $queues[$event->getKey('Queue')]['calls'][]=$event->getKeys();
  158. }
  159. }
  160. return $queues;
  161. }
  162. else {
  163. self :: log_action_error('listQueues',$response->getMessage());
  164. }
  165. return;
  166. }
  167.  
  168. function dbGetTree() {
  169.  
  170. if (!self :: start()) {
  171. return;
  172. }
  173.  
  174. $response = self :: $client->send(new CommandAction('database show'));
  175. if ($response->isSuccess()) {
  176. $tree=array();
  177. foreach (explode("\n",$response->getRawContent()) as $line) {
  178. if (preg_match('/^\/([^ ]*) *: (.*) *$/',$line,$m)) {
  179. $ret=&$tree;
  180. $keys=explode('/',$m[1]);
  181. for ($i=0;$i<(count($keys)-1);$i++) {
  182. if (!is_array($ret[$keys[$i]])) {
  183. $ret[$keys[$i]]=array();
  184. }
  185. $ret=&$ret[$keys[$i]];
  186. }
  187. $ret[$keys[(count($keys)-1)]]=trim($m[2]);
  188. }
  189. }
  190. return $tree;
  191. }
  192. else {
  193. self :: log_action_error('getDBTree',$response->getMessage());
  194. }
  195. return;
  196. }
  197.  
  198. function dbGet($family,$key) {
  199. if (!self :: start()) {
  200. return;
  201. }
  202.  
  203. $response = self :: $client->send(new DBGetAction($family, $key));
  204. if ($response->isSuccess()) {
  205. return $response->getKeys();
  206. }
  207. else {
  208. self :: log_action_error('dbGet',$response->getMessage());
  209. }
  210. return;
  211. }
  212.  
  213. function dbPut($family,$key,$value) {
  214. if (!self :: start()) {
  215. return;
  216. }
  217.  
  218. $response = self :: $client->send(new DBPutAction($family, $key, $value));
  219. if ($response->isSuccess()) {
  220. return true;
  221. }
  222. else {
  223. self :: log_action_error('dbPut',$response->getMessage());
  224. }
  225. return;
  226. }
  227.  
  228. function dbDel($family,$key) {
  229. if (!self :: start()) {
  230. return;
  231. }
  232.  
  233. $response = self :: $client->send(new DBDelAction($family, $key));
  234. if ($response->isSuccess()) {
  235. return true;
  236. }
  237. else {
  238. self :: log_action_error('dbDel',$response->getMessage());
  239. }
  240. return;
  241. }
  242.  
  243. function mailboxInfos($mailbox) {
  244. if (!self :: start()) {
  245. return;
  246. }
  247.  
  248. $response = self :: $client->send(new MailboxCountAction($mailbox));
  249. if ($response->isSuccess()) {
  250. return $response->getKeys();
  251. }
  252. else {
  253. self :: log_action_error('mailboxInfos',$response->getMessage());
  254. }
  255. return;
  256. }
  257.  
  258. function originate($channel,$params) {
  259. if (!self :: start()) {
  260. return;
  261. }
  262.  
  263. $act=new OriginateAction($channel);
  264. if (is_array($params)) {
  265. foreach($params as $key => $val) {
  266. switch($key) {
  267. case 'context':
  268. $act -> setContext($val);
  269. ;;
  270. case 'extension':
  271. $act -> setExtension($val);
  272. ;;
  273. case 'priority':
  274. $act -> setPriority($val);
  275. ;;
  276. case 'callerid':
  277. $act -> setCallerId($val);
  278. ;;
  279. }
  280. }
  281. }
  282.  
  283. $response = self :: $client->send($act);
  284. if ($response->isSuccess()) {
  285. return True;
  286. }
  287. else {
  288. self :: log_action_error('mailboxInfos',$response->getMessage());
  289. }
  290. return;
  291. }
  292.  
  293. function channelHangup($channel) {
  294. if (!self :: start()) {
  295. return;
  296. }
  297.  
  298. $response = self :: $client->send(new HangupAction($channel));
  299. if ($response->isSuccess()) {
  300. return True;
  301. }
  302. else {
  303. self :: log_action_error('channelHangup',$response->getMessage());
  304. }
  305. return;
  306. }
  307.  
  308. function log_action_error($action,$msg) {
  309. self :: log_error("Asterisk return error during $action : $msg");
  310. }
  311.  
  312. function log_error($msg) {
  313. error_log("MyAMI Error : $msg");
  314. die("$msg");
  315. }
  316. }