├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── composer.lock ├── phpcs.xml ├── phpcs └── PHPAMI │ └── ruleset.xml └── src └── Ami.php /.gitignore: -------------------------------------------------------------------------------- 1 | /composer.phar 2 | /vendor 3 | /bin 4 | /.idea 5 | /vbin 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 2.1, February 1999 3 | 4 | Copyright (C) 1991, 1999 Free Software Foundation, Inc. 5 | 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | [This is the first released version of the Lesser GPL. It also counts 10 | as the successor of the GNU Library Public License, version 2, hence 11 | the version number 2.1.] 12 | 13 | Preamble 14 | 15 | The licenses for most software are designed to take away your 16 | freedom to share and change it. By contrast, the GNU General Public 17 | Licenses are intended to guarantee your freedom to share and change 18 | free software--to make sure the software is free for all its users. 19 | 20 | This license, the Lesser General Public License, applies to some 21 | specially designated software packages--typically libraries--of the 22 | Free Software Foundation and other authors who decide to use it. You 23 | can use it too, but we suggest you first think carefully about whether 24 | this license or the ordinary General Public License is the better 25 | strategy to use in any particular case, based on the explanations below. 26 | 27 | When we speak of free software, we are referring to freedom of use, 28 | not price. Our General Public Licenses are designed to make sure that 29 | you have the freedom to distribute copies of free software (and charge 30 | for this service if you wish); that you receive source code or can get 31 | it if you want it; that you can change the software and use pieces of 32 | it in new free programs; and that you are informed that you can do 33 | these things. 34 | 35 | To protect your rights, we need to make restrictions that forbid 36 | distributors to deny you these rights or to ask you to surrender these 37 | rights. These restrictions translate to certain responsibilities for 38 | you if you distribute copies of the library or if you modify it. 39 | 40 | For example, if you distribute copies of the library, whether gratis 41 | or for a fee, you must give the recipients all the rights that we gave 42 | you. You must make sure that they, too, receive or can get the source 43 | code. If you link other code with the library, you must provide 44 | complete object files to the recipients, so that they can relink them 45 | with the library after making changes to the library and recompiling 46 | it. And you must show them these terms so they know their rights. 47 | 48 | We protect your rights with a two-step method: (1) we copyright the 49 | library, and (2) we offer you this license, which gives you legal 50 | permission to copy, distribute and/or modify the library. 51 | 52 | To protect each distributor, we want to make it very clear that 53 | there is no warranty for the free library. Also, if the library is 54 | modified by someone else and passed on, the recipients should know 55 | that what they have is not the original version, so that the original 56 | author's reputation will not be affected by problems that might be 57 | introduced by others. 58 | 59 | Finally, software patents pose a constant threat to the existence of 60 | any free program. We wish to make sure that a company cannot 61 | effectively restrict the users of a free program by obtaining a 62 | restrictive license from a patent holder. Therefore, we insist that 63 | any patent license obtained for a version of the library must be 64 | consistent with the full freedom of use specified in this license. 65 | 66 | Most GNU software, including some libraries, is covered by the 67 | ordinary GNU General Public License. This license, the GNU Lesser 68 | General Public License, applies to certain designated libraries, and 69 | is quite different from the ordinary General Public License. We use 70 | this license for certain libraries in order to permit linking those 71 | libraries into non-free programs. 72 | 73 | When a program is linked with a library, whether statically or using 74 | a shared library, the combination of the two is legally speaking a 75 | combined work, a derivative of the original library. The ordinary 76 | General Public License therefore permits such linking only if the 77 | entire combination fits its criteria of freedom. The Lesser General 78 | Public License permits more lax criteria for linking other code with 79 | the library. 80 | 81 | We call this license the "Lesser" General Public License because it 82 | does Less to protect the user's freedom than the ordinary General 83 | Public License. It also provides other free software developers Less 84 | of an advantage over competing non-free programs. These disadvantages 85 | are the reason we use the ordinary General Public License for many 86 | libraries. However, the Lesser license provides advantages in certain 87 | special circumstances. 88 | 89 | For example, on rare occasions, there may be a special need to 90 | encourage the widest possible use of a certain library, so that it becomes 91 | a de-facto standard. To achieve this, non-free programs must be 92 | allowed to use the library. A more frequent case is that a free 93 | library does the same job as widely used non-free libraries. In this 94 | case, there is little to gain by limiting the free library to free 95 | software only, so we use the Lesser General Public License. 96 | 97 | In other cases, permission to use a particular library in non-free 98 | programs enables a greater number of people to use a large body of 99 | free software. For example, permission to use the GNU C Library in 100 | non-free programs enables many more people to use the whole GNU 101 | operating system, as well as its variant, the GNU/Linux operating 102 | system. 103 | 104 | Although the Lesser General Public License is Less protective of the 105 | users' freedom, it does ensure that the user of a program that is 106 | linked with the Library has the freedom and the wherewithal to run 107 | that program using a modified version of the Library. 108 | 109 | The precise terms and conditions for copying, distribution and 110 | modification follow. Pay close attention to the difference between a 111 | "work based on the library" and a "work that uses the library". The 112 | former contains code derived from the library, whereas the latter must 113 | be combined with the library in order to run. 114 | 115 | GNU LESSER GENERAL PUBLIC LICENSE 116 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 117 | 118 | 0. This License Agreement applies to any software library or other 119 | program which contains a notice placed by the copyright holder or 120 | other authorized party saying it may be distributed under the terms of 121 | this Lesser General Public License (also called "this License"). 122 | Each licensee is addressed as "you". 123 | 124 | A "library" means a collection of software functions and/or data 125 | prepared so as to be conveniently linked with application programs 126 | (which use some of those functions and data) to form executables. 127 | 128 | The "Library", below, refers to any such software library or work 129 | which has been distributed under these terms. A "work based on the 130 | Library" means either the Library or any derivative work under 131 | copyright law: that is to say, a work containing the Library or a 132 | portion of it, either verbatim or with modifications and/or translated 133 | straightforwardly into another language. (Hereinafter, translation is 134 | included without limitation in the term "modification".) 135 | 136 | "Source code" for a work means the preferred form of the work for 137 | making modifications to it. For a library, complete source code means 138 | all the source code for all modules it contains, plus any associated 139 | interface definition files, plus the scripts used to control compilation 140 | and installation of the library. 141 | 142 | Activities other than copying, distribution and modification are not 143 | covered by this License; they are outside its scope. The act of 144 | running a program using the Library is not restricted, and output from 145 | such a program is covered only if its contents constitute a work based 146 | on the Library (independent of the use of the Library in a tool for 147 | writing it). Whether that is true depends on what the Library does 148 | and what the program that uses the Library does. 149 | 150 | 1. You may copy and distribute verbatim copies of the Library's 151 | complete source code as you receive it, in any medium, provided that 152 | you conspicuously and appropriately publish on each copy an 153 | appropriate copyright notice and disclaimer of warranty; keep intact 154 | all the notices that refer to this License and to the absence of any 155 | warranty; and distribute a copy of this License along with the 156 | Library. 157 | 158 | You may charge a fee for the physical act of transferring a copy, 159 | and you may at your option offer warranty protection in exchange for a 160 | fee. 161 | 162 | 2. You may modify your copy or copies of the Library or any portion 163 | of it, thus forming a work based on the Library, and copy and 164 | distribute such modifications or work under the terms of Section 1 165 | above, provided that you also meet all of these conditions: 166 | 167 | a) The modified work must itself be a software library. 168 | 169 | b) You must cause the files modified to carry prominent notices 170 | stating that you changed the files and the date of any change. 171 | 172 | c) You must cause the whole of the work to be licensed at no 173 | charge to all third parties under the terms of this License. 174 | 175 | d) If a facility in the modified Library refers to a function or a 176 | table of data to be supplied by an application program that uses 177 | the facility, other than as an argument passed when the facility 178 | is invoked, then you must make a good faith effort to ensure that, 179 | in the event an application does not supply such function or 180 | table, the facility still operates, and performs whatever part of 181 | its purpose remains meaningful. 182 | 183 | (For example, a function in a library to compute square roots has 184 | a purpose that is entirely well-defined independent of the 185 | application. Therefore, Subsection 2d requires that any 186 | application-supplied function or table used by this function must 187 | be optional: if the application does not supply it, the square 188 | root function must still compute square roots.) 189 | 190 | These requirements apply to the modified work as a whole. If 191 | identifiable sections of that work are not derived from the Library, 192 | and can be reasonably considered independent and separate works in 193 | themselves, then this License, and its terms, do not apply to those 194 | sections when you distribute them as separate works. But when you 195 | distribute the same sections as part of a whole which is a work based 196 | on the Library, the distribution of the whole must be on the terms of 197 | this License, whose permissions for other licensees extend to the 198 | entire whole, and thus to each and every part regardless of who wrote 199 | it. 200 | 201 | Thus, it is not the intent of this section to claim rights or contest 202 | your rights to work written entirely by you; rather, the intent is to 203 | exercise the right to control the distribution of derivative or 204 | collective works based on the Library. 205 | 206 | In addition, mere aggregation of another work not based on the Library 207 | with the Library (or with a work based on the Library) on a volume of 208 | a storage or distribution medium does not bring the other work under 209 | the scope of this License. 210 | 211 | 3. You may opt to apply the terms of the ordinary GNU General Public 212 | License instead of this License to a given copy of the Library. To do 213 | this, you must alter all the notices that refer to this License, so 214 | that they refer to the ordinary GNU General Public License, version 2, 215 | instead of to this License. (If a newer version than version 2 of the 216 | ordinary GNU General Public License has appeared, then you can specify 217 | that version instead if you wish.) Do not make any other change in 218 | these notices. 219 | 220 | Once this change is made in a given copy, it is irreversible for 221 | that copy, so the ordinary GNU General Public License applies to all 222 | subsequent copies and derivative works made from that copy. 223 | 224 | This option is useful when you wish to copy part of the code of 225 | the Library into a program that is not a library. 226 | 227 | 4. You may copy and distribute the Library (or a portion or 228 | derivative of it, under Section 2) in object code or executable form 229 | under the terms of Sections 1 and 2 above provided that you accompany 230 | it with the complete corresponding machine-readable source code, which 231 | must be distributed under the terms of Sections 1 and 2 above on a 232 | medium customarily used for software interchange. 233 | 234 | If distribution of object code is made by offering access to copy 235 | from a designated place, then offering equivalent access to copy the 236 | source code from the same place satisfies the requirement to 237 | distribute the source code, even though third parties are not 238 | compelled to copy the source along with the object code. 239 | 240 | 5. A program that contains no derivative of any portion of the 241 | Library, but is designed to work with the Library by being compiled or 242 | linked with it, is called a "work that uses the Library". Such a 243 | work, in isolation, is not a derivative work of the Library, and 244 | therefore falls outside the scope of this License. 245 | 246 | However, linking a "work that uses the Library" with the Library 247 | creates an executable that is a derivative of the Library (because it 248 | contains portions of the Library), rather than a "work that uses the 249 | library". The executable is therefore covered by this License. 250 | Section 6 states terms for distribution of such executables. 251 | 252 | When a "work that uses the Library" uses material from a header file 253 | that is part of the Library, the object code for the work may be a 254 | derivative work of the Library even though the source code is not. 255 | Whether this is true is especially significant if the work can be 256 | linked without the Library, or if the work is itself a library. The 257 | threshold for this to be true is not precisely defined by law. 258 | 259 | If such an object file uses only numerical parameters, data 260 | structure layouts and accessors, and small macros and small inline 261 | functions (ten lines or less in length), then the use of the object 262 | file is unrestricted, regardless of whether it is legally a derivative 263 | work. (Executables containing this object code plus portions of the 264 | Library will still fall under Section 6.) 265 | 266 | Otherwise, if the work is a derivative of the Library, you may 267 | distribute the object code for the work under the terms of Section 6. 268 | Any executables containing that work also fall under Section 6, 269 | whether or not they are linked directly with the Library itself. 270 | 271 | 6. As an exception to the Sections above, you may also combine or 272 | link a "work that uses the Library" with the Library to produce a 273 | work containing portions of the Library, and distribute that work 274 | under terms of your choice, provided that the terms permit 275 | modification of the work for the customer's own use and reverse 276 | engineering for debugging such modifications. 277 | 278 | You must give prominent notice with each copy of the work that the 279 | Library is used in it and that the Library and its use are covered by 280 | this License. You must supply a copy of this License. If the work 281 | during execution displays copyright notices, you must include the 282 | copyright notice for the Library among them, as well as a reference 283 | directing the user to the copy of this License. Also, you must do one 284 | of these things: 285 | 286 | a) Accompany the work with the complete corresponding 287 | machine-readable source code for the Library including whatever 288 | changes were used in the work (which must be distributed under 289 | Sections 1 and 2 above); and, if the work is an executable linked 290 | with the Library, with the complete machine-readable "work that 291 | uses the Library", as object code and/or source code, so that the 292 | user can modify the Library and then relink to produce a modified 293 | executable containing the modified Library. (It is understood 294 | that the user who changes the contents of definitions files in the 295 | Library will not necessarily be able to recompile the application 296 | to use the modified definitions.) 297 | 298 | b) Use a suitable shared library mechanism for linking with the 299 | Library. A suitable mechanism is one that (1) uses at run time a 300 | copy of the library already present on the user's computer system, 301 | rather than copying library functions into the executable, and (2) 302 | will operate properly with a modified version of the library, if 303 | the user installs one, as long as the modified version is 304 | interface-compatible with the version that the work was made with. 305 | 306 | c) Accompany the work with a written offer, valid for at 307 | least three years, to give the same user the materials 308 | specified in Subsection 6a, above, for a charge no more 309 | than the cost of performing this distribution. 310 | 311 | d) If distribution of the work is made by offering access to copy 312 | from a designated place, offer equivalent access to copy the above 313 | specified materials from the same place. 314 | 315 | e) Verify that the user has already received a copy of these 316 | materials or that you have already sent this user a copy. 317 | 318 | For an executable, the required form of the "work that uses the 319 | Library" must include any data and utility programs needed for 320 | reproducing the executable from it. However, as a special exception, 321 | the materials to be distributed need not include anything that is 322 | normally distributed (in either source or binary form) with the major 323 | components (compiler, kernel, and so on) of the operating system on 324 | which the executable runs, unless that component itself accompanies 325 | the executable. 326 | 327 | It may happen that this requirement contradicts the license 328 | restrictions of other proprietary libraries that do not normally 329 | accompany the operating system. Such a contradiction means you cannot 330 | use both them and the Library together in an executable that you 331 | distribute. 332 | 333 | 7. You may place library facilities that are a work based on the 334 | Library side-by-side in a single library together with other library 335 | facilities not covered by this License, and distribute such a combined 336 | library, provided that the separate distribution of the work based on 337 | the Library and of the other library facilities is otherwise 338 | permitted, and provided that you do these two things: 339 | 340 | a) Accompany the combined library with a copy of the same work 341 | based on the Library, uncombined with any other library 342 | facilities. This must be distributed under the terms of the 343 | Sections above. 344 | 345 | b) Give prominent notice with the combined library of the fact 346 | that part of it is a work based on the Library, and explaining 347 | where to find the accompanying uncombined form of the same work. 348 | 349 | 8. You may not copy, modify, sublicense, link with, or distribute 350 | the Library except as expressly provided under this License. Any 351 | attempt otherwise to copy, modify, sublicense, link with, or 352 | distribute the Library is void, and will automatically terminate your 353 | rights under this License. However, parties who have received copies, 354 | or rights, from you under this License will not have their licenses 355 | terminated so long as such parties remain in full compliance. 356 | 357 | 9. You are not required to accept this License, since you have not 358 | signed it. However, nothing else grants you permission to modify or 359 | distribute the Library or its derivative works. These actions are 360 | prohibited by law if you do not accept this License. Therefore, by 361 | modifying or distributing the Library (or any work based on the 362 | Library), you indicate your acceptance of this License to do so, and 363 | all its terms and conditions for copying, distributing or modifying 364 | the Library or works based on it. 365 | 366 | 10. Each time you redistribute the Library (or any work based on the 367 | Library), the recipient automatically receives a license from the 368 | original licensor to copy, distribute, link with or modify the Library 369 | subject to these terms and conditions. You may not impose any further 370 | restrictions on the recipients' exercise of the rights granted herein. 371 | You are not responsible for enforcing compliance by third parties with 372 | this License. 373 | 374 | 11. If, as a consequence of a court judgment or allegation of patent 375 | infringement or for any other reason (not limited to patent issues), 376 | conditions are imposed on you (whether by court order, agreement or 377 | otherwise) that contradict the conditions of this License, they do not 378 | excuse you from the conditions of this License. If you cannot 379 | distribute so as to satisfy simultaneously your obligations under this 380 | License and any other pertinent obligations, then as a consequence you 381 | may not distribute the Library at all. For example, if a patent 382 | license would not permit royalty-free redistribution of the Library by 383 | all those who receive copies directly or indirectly through you, then 384 | the only way you could satisfy both it and this License would be to 385 | refrain entirely from distribution of the Library. 386 | 387 | If any portion of this section is held invalid or unenforceable under any 388 | particular circumstance, the balance of the section is intended to apply, 389 | and the section as a whole is intended to apply in other circumstances. 390 | 391 | It is not the purpose of this section to induce you to infringe any 392 | patents or other property right claims or to contest validity of any 393 | such claims; this section has the sole purpose of protecting the 394 | integrity of the free software distribution system which is 395 | implemented by public license practices. Many people have made 396 | generous contributions to the wide range of software distributed 397 | through that system in reliance on consistent application of that 398 | system; it is up to the author/donor to decide if he or she is willing 399 | to distribute software through any other system and a licensee cannot 400 | impose that choice. 401 | 402 | This section is intended to make thoroughly clear what is believed to 403 | be a consequence of the rest of this License. 404 | 405 | 12. If the distribution and/or use of the Library is restricted in 406 | certain countries either by patents or by copyrighted interfaces, the 407 | original copyright holder who places the Library under this License may add 408 | an explicit geographical distribution limitation excluding those countries, 409 | so that distribution is permitted only in or among countries not thus 410 | excluded. In such case, this License incorporates the limitation as if 411 | written in the body of this License. 412 | 413 | 13. The Free Software Foundation may publish revised and/or new 414 | versions of the Lesser General Public License from time to time. 415 | Such new versions will be similar in spirit to the present version, 416 | but may differ in detail to address new problems or concerns. 417 | 418 | Each version is given a distinguishing version number. If the Library 419 | specifies a version number of this License which applies to it and 420 | "any later version", you have the option of following the terms and 421 | conditions either of that version or of any later version published by 422 | the Free Software Foundation. If the Library does not specify a 423 | license version number, you may choose any version ever published by 424 | the Free Software Foundation. 425 | 426 | 14. If you wish to incorporate parts of the Library into other free 427 | programs whose distribution conditions are incompatible with these, 428 | write to the author to ask for permission. For software which is 429 | copyrighted by the Free Software Foundation, write to the Free 430 | Software Foundation; we sometimes make exceptions for this. Our 431 | decision will be guided by the two goals of preserving the free status 432 | of all derivatives of our free software and of promoting the sharing 433 | and reuse of software generally. 434 | 435 | NO WARRANTY 436 | 437 | 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO 438 | WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. 439 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR 440 | OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY 441 | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE 442 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 443 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE 444 | LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME 445 | THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 446 | 447 | 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN 448 | WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY 449 | AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU 450 | FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR 451 | CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE 452 | LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING 453 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A 454 | FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF 455 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 456 | DAMAGES. 457 | 458 | END OF TERMS AND CONDITIONS 459 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # phpami 2 | Asterisk Manager Interface (AMI) for PHP 5.4+ supporting legacy v1.4 to v13 with no thrills. 3 | 4 | This is a fork and update of the AMI portion of the now defunc [PHPAGI](http://phpagi.sourceforge.net/) PHP 4.4 project which has not been updated since 2010. 5 | 6 | We have kept the simplicity of a single class with command methods, adding a packagist entry and namespaces. This is a breaking change as class and method names have changed to follow modern standards, however they are a 1:1 mapping so updating your code should be straight forward. 7 | 8 | ## Alternatives 9 | 10 | If you are dealing with only Asterisk v13+ servers, consider using [PAMI](http://marcelog.github.io/PAMI/) instead, which uses modern design paterns with observer-listener pattern. It no longer supports legacy Asterisk versions like v1.4 however. It may also see less development than the [NAMI](http://marcelog.github.io/Nami/) library for NodeJS. 11 | 12 | ## Updates 13 | 14 | The project is considered in a usable state and feature complete. 15 | 16 | This project is used in corporate applications. As such, the authors are unlikely to update it on a regular basis, but instead when the corporate applications that use it run into problems. You should expect updates in the 5-10yr range. 17 | 18 | Issues and PRs will be monitored, and we will continue to work with the community to provide updates as they are contributed. 19 | 20 | ## Installing via Composer 21 | 22 | The recommended way to install PHPAMI is through 23 | [Composer](http://getcomposer.org). 24 | 25 | ```bash 26 | # Install Composer 27 | curl -sS https://getcomposer.org/installer | php 28 | ``` 29 | 30 | Next, run the Composer command to install the latest stable version: 31 | 32 | ```bash 33 | composer.phar require ofbeaton/phpami 34 | ``` 35 | 36 | After installing, you can now use it in your code: 37 | 38 | ```php 39 | $ami = new \PHPAMI\Ami(); 40 | if ($ami->connect('localhost:5038', 'myuser', 'mysecret', 'off') === false) { 41 | throw new \RuntimeException('Could not connect to Asterisk Management Interface.'); 42 | } 43 | 44 | // // if you have a looping of command function 45 | // // set allowTimeout flag to true 46 | // $ami->allowTimeout(); 47 | 48 | // $result contains the output from the command 49 | $result = $ami->command('core show channels'); 50 | 51 | $ami->disconnect(); 52 | ``` 53 | 54 | ## License 55 | 56 | This software is distributed under the LGPL 2.1 License. Please see [License file](LICENSE) for more information. 57 | 58 | This library is a fork of the now defunc [PHPAGI](http://phpagi.sourceforge.net/) project by Matthew Asham. See [Fork release](https://github.com/ofbeaton/phpami/releases/tag/0.1) for more details. 59 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ofbeaton/phpami", 3 | "description": "Asterisk Manager Interface (AMI) for PHP 5.4+ supporting legacy v1.4 to v13 with no thrills.", 4 | "keywords": [ 5 | "Asterisk", 6 | "astx", 7 | "AMI", 8 | "phpami", 9 | "phpagi", 10 | "pami", 11 | "pagi" 12 | ], 13 | "homepage": "https://github.com/ofbeaton/phpami", 14 | "license": "LGPL-2.1-only", 15 | "authors": [ 16 | { 17 | "name": "Finlay Beaton", 18 | "email": "ofbeaton@gmail.com" 19 | }, 20 | { 21 | "name": "Phpami Community", 22 | "homepage": "https://github.com/ofbeaton/phpami/graphs/contributors" 23 | }, 24 | { 25 | "name": "PHPAGI Community (pre-fork)", 26 | "homepage": "http://sourceforge.net/p/phpagi/_members/" 27 | } 28 | ], 29 | "require": { 30 | "php": ">=5.4.0" 31 | }, 32 | "require-dev": { 33 | "ofbeaton/granite-php": "^6.0", 34 | "squizlabs/php_codesniffer": "^3.4" 35 | }, 36 | "config": { 37 | "bin-dir": "vbin" 38 | }, 39 | "autoload": { 40 | "psr-4": { 41 | "PHPAMI\\": "src" 42 | } 43 | }, 44 | "autoload-dev": { 45 | "psr-4": { 46 | "PHPAMI\\Tests\\": "tests" 47 | } 48 | }, 49 | "extra": { 50 | "branch-alias": { 51 | "dev-master": "1.0-dev" 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /composer.lock: -------------------------------------------------------------------------------- 1 | { 2 | "_readme": [ 3 | "This file locks the dependencies of your project to a known state", 4 | "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", 5 | "This file is @generated automatically" 6 | ], 7 | "content-hash": "4c85890cfd4b93685a90d1784f890253", 8 | "packages": [], 9 | "packages-dev": [ 10 | { 11 | "name": "ofbeaton/granite-php", 12 | "version": "6.9.0", 13 | "source": { 14 | "type": "git", 15 | "url": "https://github.com/ofbeaton/granite-php.git", 16 | "reference": "1767dfb3dd7f9a0026c007cd68ad7ffeb51d4b82" 17 | }, 18 | "dist": { 19 | "type": "zip", 20 | "url": "https://api.github.com/repos/ofbeaton/granite-php/zipball/1767dfb3dd7f9a0026c007cd68ad7ffeb51d4b82", 21 | "reference": "1767dfb3dd7f9a0026c007cd68ad7ffeb51d4b82", 22 | "shasum": "" 23 | }, 24 | "require-dev": { 25 | "jakub-onderka/php-console-highlighter": "^0.4.0", 26 | "jakub-onderka/php-parallel-lint": "^1.0", 27 | "squizlabs/php_codesniffer": "^3.4" 28 | }, 29 | "suggest": { 30 | "jakub-onderka/php-console-highlighter": "Highlight syntax for php-parallel-lint", 31 | "jakub-onderka/php-parallel-lint": "speed up `php -l` check", 32 | "squizlabs/php_codesniffer": "check and fix granite code style" 33 | }, 34 | "type": "library", 35 | "extra": { 36 | "branch-alias": { 37 | "dev-master": "4.0-dev" 38 | } 39 | }, 40 | "notification-url": "https://packagist.org/downloads/", 41 | "license": [ 42 | "MIT" 43 | ], 44 | "authors": [ 45 | { 46 | "name": "Finlay Beaton", 47 | "email": "ofbeaton@gmail.com" 48 | }, 49 | { 50 | "name": "Granite Community", 51 | "homepage": "https://github.com/ofbeaton/granite-php/graphs/contributors" 52 | } 53 | ], 54 | "description": "Opinionated PHP Coding Style enforcement for PHP", 55 | "homepage": "https://github.com/ofbeaton/granite-php", 56 | "keywords": [ 57 | "Code style", 58 | "Coding Style", 59 | "PHP_CodeSniffer", 60 | "code sniffer", 61 | "lint" 62 | ], 63 | "time": "2019-03-21T21:17:59+00:00" 64 | }, 65 | { 66 | "name": "squizlabs/php_codesniffer", 67 | "version": "3.4.2", 68 | "source": { 69 | "type": "git", 70 | "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", 71 | "reference": "b8a7362af1cc1aadb5bd36c3defc4dda2cf5f0a8" 72 | }, 73 | "dist": { 74 | "type": "zip", 75 | "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/b8a7362af1cc1aadb5bd36c3defc4dda2cf5f0a8", 76 | "reference": "b8a7362af1cc1aadb5bd36c3defc4dda2cf5f0a8", 77 | "shasum": "" 78 | }, 79 | "require": { 80 | "ext-simplexml": "*", 81 | "ext-tokenizer": "*", 82 | "ext-xmlwriter": "*", 83 | "php": ">=5.4.0" 84 | }, 85 | "require-dev": { 86 | "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0" 87 | }, 88 | "bin": [ 89 | "bin/phpcs", 90 | "bin/phpcbf" 91 | ], 92 | "type": "library", 93 | "extra": { 94 | "branch-alias": { 95 | "dev-master": "3.x-dev" 96 | } 97 | }, 98 | "notification-url": "https://packagist.org/downloads/", 99 | "license": [ 100 | "BSD-3-Clause" 101 | ], 102 | "authors": [ 103 | { 104 | "name": "Greg Sherwood", 105 | "role": "lead" 106 | } 107 | ], 108 | "description": "PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.", 109 | "homepage": "https://github.com/squizlabs/PHP_CodeSniffer", 110 | "keywords": [ 111 | "phpcs", 112 | "standards" 113 | ], 114 | "time": "2019-04-10T23:49:02+00:00" 115 | } 116 | ], 117 | "aliases": [], 118 | "minimum-stability": "stable", 119 | "stability-flags": [], 120 | "prefer-stable": false, 121 | "prefer-lowest": false, 122 | "platform": { 123 | "php": ">=5.4.0" 124 | }, 125 | "platform-dev": [] 126 | } 127 | -------------------------------------------------------------------------------- /phpcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | The default command line arguments 4 | 5 | src 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /phpcs/PHPAMI/ruleset.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | The coding standard for the PHPAMI project. 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /src/Ami.php: -------------------------------------------------------------------------------- 1 | and others, PHPAGI (pre-fork) 4 | * All Rights Reserved. 5 | * 6 | * This software is released under the terms of the GNU Lesser General Public License v2.1 7 | * A copy of which is available from http://www.gnu.org/copyleft/lesser.html 8 | */ 9 | 10 | namespace PHPAMI; 11 | 12 | /** 13 | * Asterisk Manager class 14 | * 15 | * @link http://www.voip-info.org/wiki-Asterisk+config+manager.conf 16 | * @link http://www.voip-info.org/wiki-Asterisk+manager+API 17 | * @example examples/sip_show_peer.php Get information about a sip peer 18 | */ 19 | class Ami 20 | { 21 | /** 22 | * Location of the asterisk directory. 23 | */ 24 | const AST_CONFIG_DIR = '/etc/asterisk/'; 25 | 26 | /** 27 | * Default configuration file for PHPAMI, in the asterisk directory. 28 | */ 29 | const DEFAULT_PHPAMI_CONFIG = '/etc/asterisk/phpami.conf'; 30 | 31 | /** 32 | * Default configuration file for PHPAGI, in the asterisk directory. 33 | * 34 | * Included for backwards compatibility with PHPAGI library. 35 | */ 36 | const DEFAULT_PHPAGI_CONFIG = '/etc/asterisk/phpagi.conf'; 37 | 38 | /** 39 | * Log level. 40 | */ 41 | const LOG_FATAL = 0; 42 | 43 | /** 44 | * Log level. 45 | */ 46 | const LOG_ERROR = 1; 47 | 48 | /** 49 | * Log level. 50 | */ 51 | const LOG_WARN = 2; 52 | 53 | /** 54 | * Log level. 55 | */ 56 | const LOG_INFO = 3; 57 | 58 | /** 59 | * Log level. 60 | */ 61 | const LOG_DEBUG = 4; 62 | 63 | /** 64 | * Log level. 65 | */ 66 | const LOG_TRACE = 5; 67 | 68 | /** 69 | * Config variables 70 | * 71 | * @var array 72 | */ 73 | public $config; 74 | 75 | /** 76 | * Socket 77 | * 78 | * @var resource|null 79 | */ 80 | public $socket = null; 81 | 82 | /** 83 | * Server we are connected to 84 | * 85 | * @var string 86 | */ 87 | public $server; 88 | 89 | /** 90 | * Port on the server we are connected to 91 | * 92 | * @var integer 93 | */ 94 | public $port; 95 | 96 | /** 97 | * @var int Used in waitResponse function to prevent looping when true 98 | */ 99 | private $allowTimeout; 100 | 101 | /** 102 | * Event Handlers 103 | * 104 | * @var array 105 | */ 106 | private $eventHandlers; 107 | 108 | /** 109 | * Whether we're successfully logged in 110 | * 111 | * @var boolean 112 | */ 113 | private $loggedIn = false; 114 | 115 | /** 116 | * @var int Log level. 117 | */ 118 | private $logLevel = self::LOG_ERROR; 119 | 120 | 121 | /** 122 | * Constructor 123 | * 124 | * @param string|array $config Name of the config file to parse. 125 | * @param array $optconfig Array of configuration vars and vals, stuffed into $this->config['asmanager']. 126 | */ 127 | public function __construct($config = null, array $optconfig = []) 128 | { 129 | // load config 130 | if (is_string($config) === true && file_exists($config) === true) { 131 | $this->config = parse_ini_file($config, true); 132 | } elseif (file_exists(self::DEFAULT_PHPAMI_CONFIG) === true) { 133 | $this->config = parse_ini_file(self::DEFAULT_PHPAMI_CONFIG, true); 134 | } elseif (file_exists(self::DEFAULT_PHPAGI_CONFIG) === true) { 135 | $this->config = parse_ini_file(self::DEFAULT_PHPAGI_CONFIG, true); 136 | } 137 | 138 | // If optconfig is specified, stuff vals and vars into 'asmanager' config array. 139 | foreach ($optconfig as $var => $val) { 140 | $this->config['asmanager'][$var] = $val; 141 | } 142 | 143 | // add default values to config for uninitialized values 144 | if (isset($this->config['asmanager']['server']) === false) { 145 | $this->config['asmanager']['server'] = 'localhost'; 146 | } 147 | 148 | if (isset($this->config['asmanager']['port']) === false) { 149 | $this->config['asmanager']['port'] = 5038; 150 | } 151 | 152 | if (isset($this->config['asmanager']['username']) === false) { 153 | $this->config['asmanager']['username'] = 'phpagi'; 154 | } 155 | 156 | if (isset($this->config['asmanager']['secret']) === false) { 157 | $this->config['asmanager']['secret'] = 'phpagi'; 158 | } 159 | }//end __construct() 160 | 161 | 162 | /** 163 | * Send a request 164 | * 165 | * @param string $action To send. 166 | * @param array $parameters To attach. 167 | * 168 | * @return array of parameters, empty if invalid socket resource 169 | */ 170 | public function sendRequest($action, array $parameters = []) 171 | { 172 | if (!is_resource($this->socket)) { 173 | return []; 174 | } 175 | 176 | $req = 'Action: '.$action."\r\n"; 177 | foreach ($parameters as $var => $val) { 178 | if (is_array($val) === true) { // only supported by Asterisk > 1.4 179 | foreach ($val as $k => $v) { 180 | $req .= $var.': '.$k.'='.$v."\r\n"; 181 | } 182 | } else { 183 | $req .= $var.': '.$val."\r\n"; 184 | } 185 | } 186 | 187 | $req .= "\r\n"; 188 | fwrite($this->socket, $req); 189 | $response = $this->waitResponse(); 190 | 191 | return $response; 192 | }//end sendRequest() 193 | 194 | /** 195 | * Set global allowTimeout flag 196 | * it will prevent looping waitResponse function 197 | * 198 | * @param boolean $value 199 | * @return void 200 | */ 201 | public function allowTimeout($value = true) 202 | { 203 | $this->allowTimeout = boolval($value); 204 | }//end allowTimeout() 205 | 206 | /** 207 | * Wait for a response 208 | * 209 | * If a request was just sent, this will return the response. 210 | * Otherwise, it will loop forever, handling events. 211 | * 212 | * @param boolean $allowTimeout If the socket times out, return an empty array. 213 | * 214 | * @return array of parameters, empty on timeout or invalid socket resource 215 | */ 216 | public function waitResponse($allowTimeout = false) 217 | { 218 | if (!is_resource($this->socket)) { 219 | return []; 220 | } 221 | 222 | $allowTimeout = $this->allowTimeout ?: $allowTimeout; 223 | 224 | // make sure we haven't already timed out 225 | $info = stream_get_meta_data($this->socket); 226 | if (feof($this->socket) === true || $info['timed_out'] === true) { 227 | return []; 228 | } 229 | 230 | $timeout = false; 231 | do { 232 | $type = null; 233 | $parameters = []; 234 | 235 | $buffer = trim(fgets($this->socket, 4096)); 236 | while ($buffer !== false && $buffer !== '') { 237 | $a = strpos($buffer, ':'); 238 | if ($a !== false) { 239 | if (count($parameters) === 0) { // first line in a response? 240 | $type = strtolower(substr($buffer, 0, $a)); 241 | if (substr($buffer, ($a + 2)) === 'Follows') { 242 | // A follows response means there is a miltiline field that follows. 243 | $parameters['data'] = ''; 244 | $buff = fgets($this->socket, 4096); 245 | $info = stream_get_meta_data($this->socket); 246 | while (substr($buff, 0, 6) !== '--END ' 247 | && $info['timed_out'] !== true 248 | && $info['eof'] !== true 249 | ) { 250 | $parameters['data'] .= $buff; 251 | $buff = fgets($this->socket, 4096); 252 | $info = stream_get_meta_data($this->socket); 253 | } 254 | } 255 | } 256 | 257 | // store parameter in $parameters 258 | $parameters[substr($buffer, 0, $a)] = substr($buffer, ($a + 2)); 259 | }//end if 260 | 261 | $buffer = trim(fgets($this->socket, 4096)); 262 | 263 | //asterisk 18 data format. Each Output is placed in an array. Grab all outputs and put them into one array with 'data' key so that that format is the same as Asterisk 13. 264 | 265 | //( 266 | // [Response] => Success 267 | // [Message] => Command output follows 268 | // [Output] => PJSIP/SBC-000c984a!default!8006319010!33!Up!Set!__TRANSFER_CONTEXT=internals!Anonymous!!!3!932!!1686577740.11883886 269 | //) 270 | 271 | if(substr($buffer, 0, 6) == 'Output'){ 272 | 273 | $parameters['data'] =''; 274 | $value = ''; 275 | 276 | while($buffer != ''){ 277 | $value .= substr($buffer, 8); 278 | $value .="\r\n"; //later the string will be split into an array by "\n" in Functions.php 279 | $buffer = trim(fgets($this->socket, 4096)); 280 | } 281 | 282 | $parameters['data'] = $value; 283 | 284 | } 285 | 286 | }//end while 287 | 288 | // process response 289 | switch ($type) { 290 | case '': 291 | /* 292 | Timeout or connection failure occurred. If not timed_out assume 293 | connection failure and set timeout=true to exit while loop. 294 | */ 295 | $info = stream_get_meta_data($this->socket); 296 | $timeout = ($info['timed_out'] === true) ? $allowTimeout : true; 297 | break; 298 | 299 | case 'event': 300 | $this->processEvent($parameters); 301 | break; 302 | 303 | case 'response': 304 | // nothing to process here 305 | break; 306 | 307 | default: 308 | $this->log('Unhandled response packet from Manager: '.print_r($parameters, true), self::LOG_ERROR); 309 | break; 310 | } 311 | } while ($type !== 'response' && $timeout === false); 312 | return $parameters; 313 | }//end waitResponse() 314 | 315 | 316 | /** 317 | * Empty receive buffer 318 | * 319 | * @return flushed buffer 320 | */ 321 | public function flush() 322 | { 323 | $buffer = fgets($this->socket, 4096); 324 | $info = stream_get_meta_data($this->socket); 325 | while ($info['timed_out'] !== true && $info['eof'] !== true) { 326 | $buffer .= fgets($this->socket, 4096); 327 | $info = stream_get_meta_data($this->socket); 328 | } 329 | 330 | return $buffer; 331 | }//end flush() 332 | 333 | 334 | /** 335 | * Connect to Asterisk 336 | * 337 | * @param string|null $server Hostname to connect to. Recommend FQDN. 338 | * @param string|null $username Username to authenticate with. 339 | * @param string|null $secret Password for the user. 340 | * @param boolean|string $events Toggle or filter events. 341 | * 342 | * @return boolean true on success 343 | * 344 | * @example examples/sip_show_peer.php Get information about a sip peer 345 | */ 346 | public function connect($server = null, $username = null, $secret = null, $events = true) 347 | { 348 | // use config if not specified 349 | if ($server === null) { 350 | $server = $this->config['asmanager']['server']; 351 | } 352 | 353 | if ($username === null) { 354 | $username = $this->config['asmanager']['username']; 355 | } 356 | 357 | if ($secret === null) { 358 | $secret = $this->config['asmanager']['secret']; 359 | } 360 | 361 | // get port from server if specified 362 | if (strpos($server, ':') !== false) { 363 | $c = explode(':', $server); 364 | $this->server = $c[0]; 365 | $this->port = $c[1]; 366 | } else { 367 | $this->server = $server; 368 | $this->port = $this->config['asmanager']['port']; 369 | } 370 | 371 | // connect the socket 372 | $errno = null; 373 | $errstr = null; 374 | // TODO: Convert this to a custom error handler to silence the error instead. 375 | $this->socket = @fsockopen($this->server, $this->port, $errno, $errstr); 376 | if ($this->socket === false) { 377 | $this->log( 378 | 'Unable to connect to manager '.$this->server.':'.$this->port.' ('.$errno.'): '.$errstr, 379 | self::LOG_FATAL 380 | ); 381 | return false; 382 | } 383 | 384 | // set a 2 second stream timeout 385 | stream_set_timeout($this->socket, 2); 386 | 387 | // read the header 388 | $str = fgets($this->socket); 389 | $info = stream_get_meta_data($this->socket); 390 | // note: else: don't $this->log($str) until someone looks to see why it mangles the logging 391 | if ($str === false || $info['timed_out'] === true) { 392 | // a problem. 393 | $this->log('Asterisk Manager header not received.', self::LOG_FATAL); 394 | return false; 395 | } 396 | 397 | // login 398 | $res = $this->sendRequest('login', ['Username' => $username, 'Secret' => $secret]); 399 | if ($res['Response'] !== 'Success') { 400 | $this->loggedIn = false; 401 | $this->log('Failed to login.', self::LOG_FATAL); 402 | $this->disconnect(); 403 | return false; 404 | } 405 | 406 | $this->loggedIn = true; 407 | 408 | // default state is to get all events, only send if changed 409 | if ($events !== true && $events !== 'on') { 410 | $this->events($events); 411 | } 412 | 413 | return true; 414 | }//end connect() 415 | 416 | 417 | /** 418 | * Disconnect 419 | * 420 | * @example examples/sip_show_peer.php Get information about a sip peer 421 | * 422 | * @return void 423 | */ 424 | public function disconnect() 425 | { 426 | if ($this->loggedIn === true) { 427 | $this->logoff(); 428 | } 429 | 430 | if (is_resource($this->socket)) { 431 | fclose($this->socket); 432 | } 433 | }//end disconnect() 434 | 435 | 436 | /** 437 | * Set Absolute Timeout 438 | * 439 | * Hangup a channel after a certain time. 440 | * 441 | * @param string $channel Channel name to hangup. 442 | * @param integer $timeout Maximum duration of the call (sec). 443 | * 444 | * @return array of parameters 445 | * 446 | * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+AbsoluteTimeout 447 | */ 448 | public function absoluteTimeout($channel, $timeout) 449 | { 450 | $result = $this->sendRequest('AbsoluteTimeout', ['Channel' => $channel, 'Timeout' => $timeout]); 451 | return $result; 452 | }//end absoluteTimeout() 453 | 454 | 455 | /** 456 | * Change monitoring filename of a channel 457 | * 458 | * @param string $channel The channel to record. 459 | * @param string $file The new name of the file created in the monitor spool directory. 460 | * 461 | * @return array of parameters 462 | * 463 | * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+ChangeMonitor 464 | */ 465 | public function changeMonitor($channel, $file) 466 | { 467 | $result = $this->sendRequest('ChangeMontior', ['Channel' => $channel, 'File' => $file]); 468 | return $result; 469 | }//end changeMonitor() 470 | 471 | 472 | /** 473 | * Execute Command 474 | * 475 | * @param string $command The command to execute. 476 | * @param string $actionId Message matching variable. 477 | * 478 | * @return array of parameters 479 | * 480 | * @example examples/sip_show_peer.php Get information about a sip peer 481 | * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+Command 482 | * @link http://www.voip-info.org/wiki-Asterisk+CLI 483 | */ 484 | public function command($command, $actionId = null) 485 | { 486 | $parameters = ['Command' => $command]; 487 | if ($actionId !== null) { 488 | $parameters['ActionID'] = $actionId; 489 | } 490 | 491 | $result = $this->sendRequest('Command', $parameters); 492 | return $result; 493 | }//end command() 494 | 495 | 496 | /** 497 | * Enable/Disable sending of events to this manager 498 | * 499 | * @param string|boolean $eventmask Is either 'on', 'off', or 'system,call,log'. 500 | * 501 | * @return array of parameters 502 | * 503 | * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+Events 504 | */ 505 | public function events($eventmask) 506 | { 507 | if ($eventmask === true) { 508 | $eventmask = 'on'; 509 | } elseif ($eventmask === false) { 510 | $eventmask = 'off'; 511 | } 512 | 513 | $result = $this->sendRequest('Events', ['EventMask' => $eventmask]); 514 | return $result; 515 | }//end events() 516 | 517 | 518 | /** 519 | * Check Extension Status 520 | * 521 | * @param string $exten Extension to check state on. 522 | * @param string $context Context for extension. 523 | * @param string $actionId Message matching variable. 524 | * 525 | * @return array of parameters 526 | * 527 | * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+ExtensionState 528 | */ 529 | public function extensionState($exten, $context, $actionId = null) 530 | { 531 | $parameters = [ 532 | 'Exten' => $exten, 533 | 'Context' => $context, 534 | ]; 535 | if ($actionId !== null) { 536 | $parameters['ActionID'] = $actionId; 537 | } 538 | 539 | $result = $this->sendRequest('ExtensionState', $parameters); 540 | return $result; 541 | }//end extensionState() 542 | 543 | 544 | /** 545 | * Gets a Channel Variable 546 | * 547 | * @param string $channel Channel to read variable from. 548 | * @param string $variable To retrieve. 549 | * @param string $actionId Message matching variable. 550 | * 551 | * @return array of parameters 552 | * 553 | * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+GetVar 554 | * @link http://www.voip-info.org/wiki-Asterisk+variables 555 | */ 556 | public function getVar($channel, $variable, $actionId = null) 557 | { 558 | $parameters = [ 559 | 'Channel' => $channel, 560 | 'Variable' => $variable, 561 | ]; 562 | if ($actionId !== null) { 563 | $parameters['ActionID'] = $actionId; 564 | } 565 | 566 | $result = $this->sendRequest('GetVar', $parameters); 567 | return $result; 568 | }//end getVar() 569 | 570 | 571 | /** 572 | * Hangup Channel 573 | * 574 | * @param string $channel The channel name to be hungup. 575 | * 576 | * @return array of parameters 577 | * 578 | * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+Hangup 579 | */ 580 | public function hangup($channel) 581 | { 582 | $result = $this->sendRequest('Hangup', ['Channel' => $channel]); 583 | return $result; 584 | }//end hangup() 585 | 586 | 587 | /** 588 | * List IAX Peers 589 | * 590 | * @return array of parameters 591 | * 592 | * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+IAXpeers 593 | */ 594 | public function iaxPeers() 595 | { 596 | $result = $this->sendRequest('IAXPeers'); 597 | return $result; 598 | }//end iaxPeers() 599 | 600 | 601 | /** 602 | * List available manager commands 603 | * 604 | * @param string $actionId Message matching variable. 605 | * 606 | * @return array of parameters 607 | * 608 | * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+ListCommands 609 | */ 610 | public function listCommands($actionId = null) 611 | { 612 | if ($actionId !== null) { 613 | $result = $this->sendRequest('ListCommands', ['ActionID' => $actionId]); 614 | } else { 615 | $result = $this->sendRequest('ListCommands'); 616 | } 617 | 618 | return $result; 619 | }//end listCommands() 620 | 621 | 622 | /** 623 | * Logoff Manager 624 | * 625 | * @return array of parameters 626 | * 627 | * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+Logoff 628 | */ 629 | public function logoff() 630 | { 631 | $result = $this->sendRequest('Logoff'); 632 | return $result; 633 | }//end logoff() 634 | 635 | 636 | /** 637 | * Check Mailbox Message Count 638 | * 639 | * Returns number of new and old messages. 640 | * Message: Mailbox Message Count 641 | * Mailbox: 642 | * NewMessages: 643 | * OldMessages: 644 | * 645 | * @param string $mailbox Full mailbox ID @. 646 | * @param string $actionId Message matching variable. 647 | * 648 | * @return array of parameters 649 | * 650 | * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+MailboxCount 651 | */ 652 | public function mailboxCount($mailbox, $actionId = null) 653 | { 654 | $parameters = ['Mailbox' => $mailbox]; 655 | if ($actionId !== null) { 656 | $parameters['ActionID'] = $actionId; 657 | } 658 | 659 | $results = $this->sendRequest('MailboxCount', $parameters); 660 | return $results; 661 | }//end mailboxCount() 662 | 663 | 664 | /** 665 | * Check Mailbox 666 | * 667 | * Returns number of messages. 668 | * Message: Mailbox Status 669 | * Mailbox: 670 | * Waiting: 671 | * 672 | * @param string $mailbox Full mailbox ID @. 673 | * @param string $actionId Message matching variable. 674 | * 675 | * @return array of parameters 676 | * 677 | * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+MailboxStatus 678 | */ 679 | public function mailboxStatus($mailbox, $actionId = null) 680 | { 681 | $parameters = ['Mailbox' => $mailbox]; 682 | if ($actionId !== null) { 683 | $parameters['ActionID'] = $actionId; 684 | } 685 | 686 | $result = $this->sendRequest('MailboxStatus', $parameters); 687 | return $result; 688 | }//end mailboxStatus() 689 | 690 | 691 | /** 692 | * Monitor a channel 693 | * 694 | * @param string $channel To monitor. 695 | * @param string $file File. 696 | * @param string $format Format. 697 | * @param boolean $mix Mix. 698 | * 699 | * @return array of parameters 700 | * 701 | * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+Monitor 702 | */ 703 | public function monitor($channel, $file = null, $format = null, $mix = null) 704 | { 705 | $parameters = ['Channel' => $channel]; 706 | if ($file !== null) { 707 | $parameters['File'] = $file; 708 | } 709 | 710 | if ($format !== null) { 711 | $parameters['Format'] = $format; 712 | } 713 | 714 | if ($file !== null) { 715 | if ($mix === true) { 716 | $parameters['Mix'] = 'true'; 717 | } else { 718 | $parameters['Mix'] = 'false'; 719 | } 720 | } 721 | 722 | $result = $this->sendRequest('Monitor', $parameters); 723 | return $result; 724 | }//end monitor() 725 | 726 | 727 | /** 728 | * Originate Call 729 | * 730 | * @param string $channel Channel name to call. 731 | * @param string $exten Extension to use (requires 'Context' and 'Priority'). 732 | * @param string $context Context to use (requires 'Exten' and 'Priority'). 733 | * @param string $priority Priority to use (requires 'Exten' and 'Context'). 734 | * @param string $application Application to use. 735 | * @param string $data Data to use (requires 'Application'). 736 | * @param integer $timeout How long to wait for call to be answered (in ms). 737 | * @param string $callerid Caller ID to be set on the outgoing channel. 738 | * @param string $variable Channel variable to set (VAR1=value1|VAR2=value2). 739 | * @param string $account Account code. 740 | * @param boolean $async True fast origination. 741 | * @param string $actionId Message matching variable. 742 | * 743 | * @return array of parameters 744 | * 745 | * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+Originate 746 | */ 747 | public function originate( 748 | $channel, 749 | $exten = null, 750 | $context = null, 751 | $priority = null, 752 | $application = null, 753 | $data = null, 754 | $timeout = null, 755 | $callerid = null, 756 | $variable = null, 757 | $account = null, 758 | $async = null, 759 | $actionId = null 760 | ) { 761 | $parameters = ['Channel' => $channel]; 762 | 763 | if ($exten !== null) { 764 | $parameters['Exten'] = $exten; 765 | } 766 | 767 | if ($context !== null) { 768 | $parameters['Context'] = $context; 769 | } 770 | 771 | if ($priority !== null) { 772 | $parameters['Priority'] = $priority; 773 | } 774 | 775 | if ($application !== null) { 776 | $parameters['Application'] = $application; 777 | } 778 | 779 | if ($data !== null) { 780 | $parameters['Data'] = $data; 781 | } 782 | 783 | if ($timeout !== null) { 784 | $parameters['Timeout'] = $timeout; 785 | } 786 | 787 | if ($callerid !== null) { 788 | $parameters['CallerID'] = $callerid; 789 | } 790 | 791 | if ($variable !== null) { 792 | $parameters['Variable'] = $variable; 793 | } 794 | 795 | if ($account !== null) { 796 | $parameters['Account'] = $account; 797 | } 798 | 799 | if ($async !== null) { 800 | if ($async === true) { 801 | $parameters['Async'] = 'true'; 802 | } else { 803 | $parameters['Async'] = 'false'; 804 | } 805 | } 806 | 807 | if ($actionId !== null) { 808 | $parameters['ActionID'] = $actionId; 809 | } 810 | 811 | $result = $this->sendRequest('Originate', $parameters); 812 | return $result; 813 | }//end originate() 814 | 815 | 816 | /** 817 | * List parked calls 818 | * 819 | * @param string $actionId Message matching variable. 820 | * 821 | * @return array of parameters 822 | * 823 | * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+ParkedCalls 824 | */ 825 | public function parkedCalls($actionId = null) 826 | { 827 | if ($actionId !== null) { 828 | $result = $this->sendRequest('ParkedCalls', ['ActionID' => $actionId]); 829 | } else { 830 | $result = $this->sendRequest('ParkedCalls'); 831 | } 832 | 833 | return $result; 834 | }//end parkedCalls() 835 | 836 | 837 | /** 838 | * Ping 839 | * 840 | * @return array of parameters 841 | * 842 | * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+Ping 843 | */ 844 | public function ping() 845 | { 846 | $result = $this->sendRequest('Ping'); 847 | return $result; 848 | }//end ping() 849 | 850 | 851 | /** 852 | * Queue Add 853 | * 854 | * @param string $queue Queue. 855 | * @param string $interface Interface. 856 | * @param integer $penalty Penalty. 857 | * 858 | * @return array of parameters 859 | * 860 | * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+QueueAdd 861 | */ 862 | public function queueAdd($queue, $interface, $penalty = 0) 863 | { 864 | $parameters = [ 865 | 'Queue' => $queue, 866 | 'Interface' => $interface, 867 | ]; 868 | if ($penalty !== 0) { 869 | $parameters['Penalty'] = $penalty; 870 | } 871 | 872 | $result = $this->sendRequest('QueueAdd', $parameters); 873 | return $result; 874 | }//end queueAdd() 875 | 876 | 877 | /** 878 | * Queue Remove 879 | * 880 | * @param string $queue Queue. 881 | * @param string $interface Interface. 882 | * 883 | * @return array of parameters 884 | * 885 | * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+QueueRemove 886 | */ 887 | public function queueRemove($queue, $interface) 888 | { 889 | $result = $this->sendRequest('QueueRemove', ['Queue' => $queue, 'Interface' => $interface]); 890 | return $result; 891 | }//end queueRemove() 892 | 893 | 894 | /** 895 | * Queues 896 | * 897 | * @return array of parameters 898 | * 899 | * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+Queues 900 | */ 901 | public function queues() 902 | { 903 | $result = $this->sendRequest('Queues'); 904 | return $result; 905 | }//end queues() 906 | 907 | 908 | /** 909 | * Queue Status 910 | * 911 | * @param string $actionId Message matching variable. 912 | * 913 | * @return array of parameters 914 | * 915 | * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+QueueStatus 916 | */ 917 | public function queueStatus($actionId = null) 918 | { 919 | if ($actionId !== null) { 920 | $result = $this->sendRequest('QueueStatus', ['ActionID' => $actionId]); 921 | } else { 922 | $result = $this->sendRequest('QueueStatus'); 923 | } 924 | 925 | return $result; 926 | }//end queueStatus() 927 | 928 | 929 | /** 930 | * Redirect 931 | * 932 | * @param string $channel Channel. 933 | * @param string $extrachannel Extra Channel. 934 | * @param string $exten Exten. 935 | * @param string $context Context. 936 | * @param string $priority Priority. 937 | * 938 | * @return array of parameters 939 | * 940 | * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+Redirect 941 | */ 942 | public function redirect($channel, $extrachannel, $exten, $context, $priority) 943 | { 944 | $result = $this->sendRequest( 945 | 'Redirect', 946 | [ 947 | 'Channel' => $channel, 948 | 'ExtraChannel' => $extrachannel, 949 | 'Exten' => $exten, 950 | 'Context' => $context, 951 | 'Priority' => $priority, 952 | ] 953 | ); 954 | 955 | return $result; 956 | }//end redirect() 957 | 958 | 959 | /** 960 | * Set the CDR UserField 961 | * 962 | * @param string $userfield User field. 963 | * @param string $channel Channel. 964 | * @param string $append Append. 965 | * 966 | * @return array of parameters 967 | * 968 | * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+SetCDRUserField 969 | */ 970 | public function setCdrUserField($userfield, $channel, $append = null) 971 | { 972 | $parameters = [ 973 | 'UserField' => $userfield, 974 | 'Channel' => $channel, 975 | ]; 976 | if ($append !== null) { 977 | $parameters['Append'] = $append; 978 | } 979 | 980 | $result = $this->sendRequest('SetCDRUserField', $parameters); 981 | return $result; 982 | }//end setCdrUserField() 983 | 984 | 985 | /** 986 | * Set Channel Variable 987 | * 988 | * @param string $channel Channel to set variable for. 989 | * @param string $variable Name. 990 | * @param string $value Value. 991 | * 992 | * @return array of parameters 993 | * 994 | * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+SetVar 995 | */ 996 | public function setVar($channel, $variable, $value) 997 | { 998 | $result = $this->sendRequest('SetVar', ['Channel' => $channel, 'Variable' => $variable, 'Value' => $value]); 999 | return $result; 1000 | }//end setVar() 1001 | 1002 | 1003 | /** 1004 | * Channel Status 1005 | * 1006 | * @param string $channel Channel. 1007 | * @param string $actionId Message matching variable. 1008 | * 1009 | * @return array of parameters 1010 | * 1011 | * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+Status 1012 | */ 1013 | public function status($channel, $actionId = null) 1014 | { 1015 | $parameters = ['Channel' => $channel]; 1016 | if ($actionId !== null) { 1017 | $parameters['ActionID'] = $actionId; 1018 | } 1019 | 1020 | $result = $this->sendRequest('Status', $parameters); 1021 | return $result; 1022 | }//end status() 1023 | 1024 | 1025 | /** 1026 | * Stop monitoring a channel 1027 | * 1028 | * @param string $channel Channel. 1029 | * 1030 | * @return array of parameters 1031 | * 1032 | * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+StopMonitor 1033 | */ 1034 | public function stopMonitor($channel) 1035 | { 1036 | $result = $this->sendRequest('StopMonitor', ['Channel' => $channel]); 1037 | return $result; 1038 | }//end stopMonitor() 1039 | 1040 | 1041 | /** 1042 | * Dial over Zap channel while offhook 1043 | * 1044 | * @param string $zapchannel Zap Channel. 1045 | * @param string $number Number. 1046 | * 1047 | * @return array of parameters 1048 | * 1049 | * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+ZapDialOffhook 1050 | */ 1051 | public function zapDialOffhook($zapchannel, $number) 1052 | { 1053 | $result = $this->sendRequest('ZapDialOffhook', ['ZapChannel' => $zapchannel, 'Number' => $number]); 1054 | return $result; 1055 | }//end zapDialOffhook() 1056 | 1057 | 1058 | /** 1059 | * Toggle Zap channel Do Not Disturb status OFF 1060 | * 1061 | * @param string $zapchannel Zap Channel. 1062 | * 1063 | * @return array of parameters 1064 | * 1065 | * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+ZapDNDoff 1066 | */ 1067 | public function zapDndOff($zapchannel) 1068 | { 1069 | $result = $this->sendRequest('ZapDNDoff', ['ZapChannel' => $zapchannel]); 1070 | return $result; 1071 | }//end zapDndOff() 1072 | 1073 | 1074 | /** 1075 | * Toggle Zap channel Do Not Disturb status ON 1076 | * 1077 | * @param string $zapchannel Zap Channel. 1078 | * 1079 | * @return array of parameters 1080 | * 1081 | * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+ZapDNDon 1082 | */ 1083 | public function zapDndOn($zapchannel) 1084 | { 1085 | $result = $this->sendRequest('ZapDNDon', ['ZapChannel' => $zapchannel]); 1086 | return $result; 1087 | }//end zapDndOn() 1088 | 1089 | 1090 | /** 1091 | * Hangup Zap Channel 1092 | * 1093 | * @param string $zapchannel Zap Channel. 1094 | * 1095 | * @return array of parameters 1096 | * 1097 | * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+ZapHangup 1098 | */ 1099 | public function zapHangup($zapchannel) 1100 | { 1101 | $result = $this->sendRequest('ZapHangup', ['ZapChannel' => $zapchannel]); 1102 | return $result; 1103 | }//end zapHangup() 1104 | 1105 | 1106 | /** 1107 | * Transfer Zap Channel 1108 | * 1109 | * @param string $zapchannel Zap Channel. 1110 | * 1111 | * @return array of parameters 1112 | * 1113 | * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+ZapTransfer 1114 | */ 1115 | public function zapTransfer($zapchannel) 1116 | { 1117 | $result = $this->sendRequest('ZapTransfer', ['ZapChannel' => $zapchannel]); 1118 | return $result; 1119 | }//end zapTransfer() 1120 | 1121 | 1122 | /** 1123 | * Zap Show Channels 1124 | * 1125 | * @param string $actionId Message matching variable. 1126 | * 1127 | * @return array of parameters 1128 | * 1129 | * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+ZapShowChannels 1130 | */ 1131 | public function zapShowChannels($actionId = null) 1132 | { 1133 | if ($actionId !== null) { 1134 | $result = $this->sendRequest('ZapShowChannels', ['ActionID' => $actionId]); 1135 | } else { 1136 | $result = $this->sendRequest('ZapShowChannels'); 1137 | } 1138 | 1139 | return $result; 1140 | }//end zapShowChannels() 1141 | 1142 | 1143 | /** 1144 | * Log a message 1145 | * 1146 | * @param string $message Message to log. 1147 | * @param integer $level From 1 to 4. 1148 | * 1149 | * @return void 1150 | */ 1151 | public function log($message, $level = self::LOG_INFO) 1152 | { 1153 | if ($level <= $this->logLevel) { 1154 | error_log(date('r').' - '.$message); 1155 | } 1156 | }//end log() 1157 | 1158 | 1159 | /** 1160 | * @param integer $level Log Level to use. 1161 | * 1162 | * @return void 1163 | * @throws \InvalidArgumentException Invalid Log level. 1164 | * 1165 | * @since 2015-07-25 1166 | */ 1167 | public function setLogLevel($level) 1168 | { 1169 | if ($level < self::LOG_FATAL || $level > self::LOG_TRACE) { 1170 | throw new \InvalidArgumentException('Invalid Log Level'); 1171 | } 1172 | 1173 | $this->logLevel = $level; 1174 | }//end setLogLevel() 1175 | 1176 | 1177 | /** 1178 | * Add event handler 1179 | * 1180 | * Known Events include ( http://www.voip-info.org/wiki-asterisk+manager+events ) 1181 | * Link - Fired when two voice channels are linked together and voice data exchange commences. 1182 | * Unlink - Fired when a link between two voice channels is discontinued, for example, just before call completion. 1183 | * Newexten - 1184 | * Hangup - 1185 | * Newchannel - 1186 | * Newstate - 1187 | * Reload - Fired when the "RELOAD" console command is executed. 1188 | * Shutdown - 1189 | * ExtensionStatus - 1190 | * Rename - 1191 | * Newcallerid - 1192 | * Alarm - 1193 | * AlarmClear - 1194 | * Agentcallbacklogoff - 1195 | * Agentcallbacklogin - 1196 | * Agentlogoff - 1197 | * MeetmeJoin - 1198 | * MessageWaiting - 1199 | * join - 1200 | * leave - 1201 | * AgentCalled - 1202 | * ParkedCall - Fired after ParkedCalls 1203 | * Cdr - 1204 | * ParkedCallsComplete - 1205 | * QueueParams - 1206 | * QueueMember - 1207 | * QueueStatusEnd - 1208 | * Status - 1209 | * StatusComplete - 1210 | * ZapShowChannels - Fired after ZapShowChannels 1211 | * ZapShowChannelsComplete - 1212 | * 1213 | * @param string $event Type or * for default handler. 1214 | * @param string $callback Function. 1215 | * 1216 | * @return boolean sucess 1217 | */ 1218 | public function addEventHandler($event, $callback) 1219 | { 1220 | $event = strtolower($event); 1221 | if (isset($this->eventHandlers[$event]) === true) { 1222 | $this->log($event.' handler is already defined, not over-writing.', self::LOG_ERROR); 1223 | return false; 1224 | } 1225 | 1226 | $this->eventHandlers[$event] = $callback; 1227 | return true; 1228 | }//end addEventHandler() 1229 | 1230 | 1231 | /** 1232 | * Process event 1233 | * 1234 | * @param array $parameters Parameters. 1235 | * 1236 | * @return mixed result of event handler or false if no handler was found 1237 | */ 1238 | public function processEvent(array $parameters) 1239 | { 1240 | $ret = false; 1241 | $e = strtolower($parameters['Event']); 1242 | $this->log('Got event: '.$e, self::LOG_INFO); 1243 | 1244 | $handler = ''; 1245 | if (isset($this->eventHandlers[$e]) === true) { 1246 | $handler = $this->eventHandlers[$e]; 1247 | } elseif (isset($this->eventHandlers['*']) === true) { 1248 | $handler = $this->eventHandlers['*']; 1249 | } 1250 | 1251 | if (function_exists($handler) === true) { 1252 | $this->log('Execute handler: '.$handler, self::LOG_DEBUG); 1253 | $ret = $handler($e, $parameters, $this->server, $this->port); 1254 | } else { 1255 | $this->log('No event handler for event: '.$e, self::LOG_DEBUG); 1256 | } 1257 | 1258 | return $ret; 1259 | }//end processEvent() 1260 | 1261 | 1262 | /** 1263 | * @param string $input To split. 1264 | * 1265 | * @return array of output lines 1266 | * @since 2015-07-26 1267 | */ 1268 | public function split($input) 1269 | { 1270 | $output = preg_split('/[\t\n]+/', $input); 1271 | return $output; 1272 | }//end split() 1273 | }//end class 1274 | --------------------------------------------------------------------------------