├── LICENSE ├── README.md ├── RtmpClient.class.php ├── RtmpMessage.class.php ├── RtmpOperation.class.php ├── RtmpPacket.class.php ├── RtmpSocket.class.php ├── RtmpStream.class.php ├── SabreAMF ├── AMF0 │ ├── Const.php │ ├── Deserializer.php │ └── Serializer.php ├── AMF3 │ ├── AbstractMessage.php │ ├── AcknowledgeMessage.php │ ├── CommandMessage.php │ ├── Const.php │ ├── Deserializer.php │ ├── ErrorMessage.php │ ├── RemotingMessage.php │ ├── Serializer.php │ └── Wrapper.php ├── ArrayCollection.php ├── ByteArray.php ├── CallbackServer.php ├── ChangeLog ├── ClassMapper.php ├── ClassNotFoundException.php ├── Client.php ├── Const.php ├── Deserializer.php ├── DetailException.php ├── Externalized.php ├── ITypedObject.php ├── InputStream.php ├── InvalidAMFException.php ├── LICENCE ├── Message.php ├── OutputStream.php ├── README ├── RecordSet.php ├── Serializer.php ├── Server.php ├── TypedObject.php └── UndefinedMethodException.php ├── composer.json ├── debug.php └── main.php /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. {http://fsf.org/} 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see {http://www.gnu.org/licenses/}. 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | php-rtmp-client Copyright (C) 2013 qwantix 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | {http://www.gnu.org/licenses/}. 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | {http://www.gnu.org/philosophy/why-not-lgpl.html}. 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | php-rtmp-client 2 | =============== 3 | 4 | A Rtmp client for PHP 5 | -------------------------------------------------------------------------------- /RtmpClient.class.php: -------------------------------------------------------------------------------- 1 | client = $client; 37 | } 38 | 39 | /** 40 | * Connect 41 | * 42 | * @param string $host 43 | * @param string $application 44 | * @param int $port 45 | */ 46 | public function connect($host,$application,$port = 1935, $connectParams=null) 47 | { 48 | $this->close(); 49 | 50 | $this->host = $host; 51 | $this->application = $application; 52 | $this->port = $port; 53 | 54 | 55 | 56 | if($this->initSocket()) 57 | { 58 | $aReadSockets = array($this->socket); 59 | $this->handshake(); 60 | $this->send_ConnectPacket($connectParams); 61 | } 62 | } 63 | /** 64 | * Close connection 65 | * 66 | */ 67 | public function close() 68 | { 69 | $this->socket && $this->socket->close(); 70 | $this->chunkSizeR = $this->chunkSizeW = 128; 71 | } 72 | /** 73 | * Call remote procedure (RPC) 74 | * 75 | * @param string $procedureName 76 | * @param array $args array of arguments, null if not args 77 | * @param callback $handler 78 | * 79 | * @return mixed result of RPC 80 | */ 81 | public function call($procedureName,array $args = null,$handler = null) 82 | { 83 | return $this->sendOperation(new RtmpOperation(new RtmpMessage($procedureName,null,$args), $handler)); 84 | } 85 | 86 | public function __call($name, $arguments) 87 | { 88 | return $this->call($name, $arguments); 89 | } 90 | 91 | 92 | //------------------------------------ 93 | // Socket 94 | //------------------------------------ 95 | private function initSocket() 96 | { 97 | $this->socket = new RtmpSocket(); 98 | return $this->socket->connect($this->host, $this->port); 99 | } 100 | private function socketRead($length) 101 | { 102 | return $this->socket->read($length); 103 | } 104 | private function socketWrite(RtmpStream $data, $n = -1) 105 | { 106 | return $this->socket->write($data, $n); 107 | } 108 | 109 | //------------------------------------- 110 | 111 | private $listening = false; 112 | /** 113 | * listen socket 114 | * 115 | * @return mixed last result 116 | */ 117 | private function listen() 118 | { 119 | if($this->listening) 120 | return; 121 | if(!$this->socket) 122 | return; 123 | $this->listening = true; 124 | $stop = false; 125 | $return = null; 126 | while (!$stop) 127 | { 128 | if($p = $this->readPacket()) 129 | { 130 | switch($p->type) 131 | { 132 | case 0x01; //Chunk size 133 | $this->handle_setChunkSize($p); 134 | break; 135 | case 0x03: //Bytes Read 136 | 137 | break; 138 | case 0x04: //Ping 139 | unset($this->operations[$p->chunkStreamId]); 140 | break; 141 | case 0x05: //Window Acknowledgement Size 142 | $this->handle_windowAcknowledgementSize($p); 143 | break; 144 | case 0x06: //Peer BW 145 | $this->handle_setPeerBandwidth($p); 146 | break; 147 | case 0x08: //Audio Data 148 | 149 | break; 150 | case 0x09: //Video Data 151 | 152 | break; 153 | case 0x12: //Notify 154 | 155 | break; 156 | 157 | case RtmpPacket::TYPE_INVOKE_AMF0: 158 | case RtmpPacket::TYPE_INVOKE_AMF3: //Invoke 159 | $return = $this->handle_invoke($p); 160 | if(sizeof($this->operations) == 0) 161 | $stop = true; 162 | break; 163 | case 0x16: 164 | 165 | break; 166 | case 0x36: //agregate 167 | 168 | break; 169 | default: 170 | 171 | break; 172 | } 173 | } 174 | usleep(1); 175 | } 176 | $this->listening = false; 177 | return $return; 178 | } 179 | /** 180 | * Previous packet 181 | * @internal 182 | * 183 | * @var RtmpPacket 184 | */ 185 | private $prevReadingPacket = array(); 186 | /** 187 | * Read packet 188 | * 189 | * @return RtmpPacket 190 | */ 191 | private function readPacket() 192 | { 193 | $p = new RtmpPacket(); 194 | 195 | $header = $this->socketRead(1)->readTinyInt(); 196 | 197 | $p->chunkType = (($header & 0xc0) >> 6); 198 | $p->chunkStreamId = $header & 0x3f; 199 | 200 | switch($p->chunkStreamId) 201 | { 202 | case 0: //range of 64-319, second byte + 64 203 | $p->chunkStreamId = 64 + $this->socketRead(1)->readTinyInt(); 204 | break; 205 | case 1: //range of 64-65599,thrid byte * 256 + second byte + 64 206 | $p->chunkStreamId = 64 + $this->socketRead(1)->readTinyInt() + $this->socketRead(1)->readTinyInt()*256; 207 | break; 208 | case 2: 209 | break; 210 | default: //range of 3-63 211 | // complete stream ids 212 | } 213 | switch($p->chunkType) 214 | { 215 | case RtmpPacket::CHUNK_TYPE_3: 216 | $p->timestamp = $this->prevReadingPacket[$p->chunkStreamId]->timestamp; 217 | case RtmpPacket::CHUNK_TYPE_2: 218 | $p->length = $this->prevReadingPacket[$p->chunkStreamId]->length; 219 | $p->type = $this->prevReadingPacket[$p->chunkStreamId]->type; 220 | case RtmpPacket::CHUNK_TYPE_1: 221 | $p->streamId = $this->prevReadingPacket[$p->chunkStreamId]->streamId; 222 | case RtmpPacket::CHUNK_TYPE_0: 223 | break; 224 | } 225 | $this->prevReadingPacket[$p->chunkStreamId] = $p; 226 | $headerSize = RtmpPacket::$SIZES[$p->chunkType]; 227 | 228 | if($headerSize == RtmpPacket::MAX_HEADER_SIZE) 229 | $p->hasAbsTimestamp = true; 230 | 231 | //If not operation exists, create it 232 | if(!isset($this->operations[$p->chunkStreamId])) 233 | $this->operations[$p->chunkStreamId] = new RtmpOperation(); 234 | 235 | if($this->operations[$p->chunkStreamId]->getResponse()) 236 | { 237 | //Operation chunking.... 238 | $p = $this->operations[$p->chunkStreamId]->getResponse()->getPacket(); 239 | $headerSize = 0; //no header 240 | } 241 | else 242 | { 243 | //Create response from packet 244 | $this->operations[$p->chunkStreamId]->createResponse($p); 245 | } 246 | 247 | 248 | $headerSize--; 249 | $header; 250 | if($headerSize>0) 251 | $header = $this->socketRead($headerSize); 252 | 253 | if($headerSize >= 3) 254 | $p->timestamp = $header->readInt24(); 255 | if($headerSize >= 6) 256 | { 257 | $p->length = $header->readInt24(); 258 | 259 | $p->bytesRead = 0; 260 | $p->free(); 261 | } 262 | if($headerSize > 6) 263 | $p->type = $header->readTinyInt(); 264 | 265 | if($headerSize == 11) 266 | $p->streamId = $header->readInt32LE(); 267 | 268 | 269 | $nToRead = $p->length - $p->bytesRead; 270 | $nChunk = $this->chunkSizeR; 271 | if($nToRead < $nChunk) 272 | $nChunk = $nToRead; 273 | 274 | if($p->payload == null) 275 | $p->payload = ""; 276 | $p->payload .= $this->socketRead($nChunk)->flush(); 277 | if($p->bytesRead + $nChunk != strlen($p->payload)) 278 | throw new Exception("Read failed, have read ".strlen($p->payload)." of ".($p->bytesRead + $nChunk)); 279 | $p->bytesRead += $nChunk; 280 | 281 | if($p->isReady()) 282 | return $p; 283 | 284 | return null; 285 | 286 | 287 | } 288 | 289 | /** 290 | * Previous packet 291 | * @internal 292 | * 293 | * @var RtmpPacket 294 | */ 295 | private $prevSendingPacket = array(); 296 | /** 297 | * Send packet 298 | * 299 | * @param RtmpPacket $packet 300 | * @return bool 301 | */ 302 | private function sendPacket(RtmpPacket $packet) 303 | { 304 | 305 | if(!$packet->length) 306 | $packet->length = strlen($packet->payload); 307 | if(isset($this->prevSendingPacket[$packet->chunkStreamId])) 308 | { 309 | if($packet->length == $this->prevSendingPacket[$packet->chunkStreamId]->length) 310 | $packet->chunkType = RtmpPacket::CHUNK_TYPE_2; 311 | else 312 | $packet->chunkType = RtmpPacket::CHUNK_TYPE_1; 313 | } 314 | if($packet->chunkType > 3) //sanity 315 | throw new Exception("sanity failed!! tring to send header of type: 0x%02x"); 316 | 317 | $this->prevSendingPacket[$packet->chunkStreamId] = $packet; 318 | 319 | $headerSize = RtmpPacket::$SIZES[$packet->chunkType]; 320 | //Initialize header 321 | $header = new RtmpStream(); 322 | $header->writeByte($packet->chunkType << 6 | $packet->chunkStreamId); 323 | if($headerSize > 1) 324 | { 325 | $packet->timestamp = time(); 326 | $header->writeInt24($packet->timestamp); 327 | } 328 | if($headerSize > 4) 329 | { 330 | $header->writeInt24($packet->length); 331 | $header->writeByte($packet->type); 332 | } 333 | if($headerSize > 8) 334 | $header->writeInt32LE($packet->streamId); 335 | 336 | // Send header 337 | $this->socketWrite($header); 338 | 339 | $headerSize = $packet->length; 340 | $buffer = new RtmpStream($packet->payload); 341 | 342 | 343 | while($headerSize) 344 | { 345 | $chunkSize = $packet->type == RtmpPacket::TYPE_INVOKE_AMF0 || $packet->type == RtmpPacket::TYPE_INVOKE_AMF3 ? $this->chunkSizeW : $packet->length; 346 | if($headerSize < $this->chunkSizeW) 347 | $chunkSize = $headerSize; 348 | 349 | if(!$this->socketWrite($buffer, $chunkSize)) 350 | throw new Exception("Socket write error (write : $chunkSize)"); 351 | 352 | $headerSize -= $chunkSize; 353 | //$buffer = substr($buffer,$chunkSize); 354 | 355 | if($headerSize > 0) 356 | { 357 | $sep = (0xc0 | $packet->chunkStreamId); 358 | if(!$this->socketWrite(new RtmpStream(chr($sep)),1)) 359 | return false; 360 | } 361 | 362 | } 363 | return true; 364 | 365 | } 366 | 367 | protected function sendOperation(RtmpOperation $op) 368 | { 369 | $this->operations[$op->getChunkStreamID()] = $op; 370 | $this->sendPacket($op->getCall()->getPacket()); 371 | return $this->listen(); 372 | } 373 | 374 | 375 | //------------------------------------ 376 | // RTMP Methods 377 | //------------------------------------ 378 | /** 379 | * Perform handshake 380 | * 381 | */ 382 | private function handshake() 383 | { 384 | /// Send C0, the version 385 | $stream = new RtmpStream(); 386 | 387 | $stream->writeByte("\x03"); //"\x03"; 388 | $this->socketWrite($stream); 389 | 390 | /// Send C1 391 | $ctime = time(); 392 | $stream->writeInt32($ctime); //Time 393 | $stream->write("\x80\x00\x03\x02"); //Zero zone? Flex put : 0x80 0x00 0x03 0x02, maybe new handshake style? 394 | 395 | $crandom = ""; 396 | for($i=0; $iwrite($crandom); 400 | $this->socketWrite($stream); 401 | 402 | ///Read S0 403 | $s0 = $this->socketRead(1)->readTinyInt(); 404 | if($s0 != 0x03) 405 | throw new Exception("Packet version ".$s0." not supported"); 406 | ///Read S1 407 | $s1 = $this->socketRead(self::RTMP_SIG_SIZE); 408 | 409 | ///Send C2 410 | $c2 = new RtmpStream(); 411 | $c2->writeInt32($s1->readInt32()); 412 | $s1->readInt32(); 413 | $c2->writeInt32($ctime); 414 | $raw = $s1->readRaw(); 415 | $c2->write($raw); 416 | $this->socketWrite($c2); 417 | 418 | ///Read S2 419 | $resp = $this->socketRead(self::RTMP_SIG_SIZE); 420 | 421 | //TODO check integrity 422 | 423 | return true; 424 | 425 | } 426 | 427 | private function send_ConnectPacket($connectParams=null) 428 | { 429 | $this->sendOperation( 430 | new RtmpOperation(new RtmpMessage("connect",(object)array( 431 | "app" => $this->application, 432 | "flashVer" => "LNX 10,0,32,18", 433 | "swfUrl" => null, 434 | "tcUrl" => "rtmp://$this->host:$this->port/$this->application", 435 | "fpad" => false, 436 | "capabilities" => 0.0, 437 | "audioCodecs" => 0x01, 438 | "videoCodecs" => 0xFF, 439 | "videoFunction" => 0, 440 | "pageUrl" => null, 441 | "objectEncoding" => 0x03 442 | ), $connectParams), array($this,"onConnect")) 443 | ); 444 | } 445 | private function send_SetChunkSize() 446 | { 447 | 448 | } 449 | private function send_AbortMessage() 450 | { 451 | 452 | } 453 | private function send_Acknowledgement() 454 | { 455 | 456 | } 457 | private function send_UserControlMessage() 458 | { 459 | 460 | } 461 | private function send_WindowAcknowledgementSize() 462 | { 463 | 464 | } 465 | private function send_SetPeerBandwidth() 466 | { 467 | 468 | } 469 | private function handle_windowAcknowledgementSize(RtmpPacket $p) 470 | { 471 | $s = new RtmpStream($p->payload); 472 | $size = $s->readInt32(); 473 | //TODO 474 | unset($this->operations[$p->chunkStreamId]); 475 | } 476 | private function handle_setPeerBandwidth(RtmpPacket $p) 477 | { 478 | $s = new RtmpStream($p->payload); 479 | $size = $s->readInt32(); 480 | $limitType = $s->readTinyInt(); 481 | //TODO 482 | unset($this->operations[$p->chunkStreamId]); 483 | } 484 | private function handle_setChunkSize(RtmpPacket $p) 485 | { 486 | $s = new RtmpStream($p->payload); 487 | $this->chunkSizeR = $s->readInt32(); 488 | unset($this->operations[$p->chunkStreamId]); 489 | } 490 | 491 | private function handle_invoke(RtmpPacket $p) 492 | { 493 | $op = $this->operations[$p->chunkStreamId]; 494 | $op->getResponse()->decode($p); 495 | 496 | if($op->getCall() && $op->getResponse()->isResponseCommand() ) 497 | { 498 | //Result 499 | unset($this->operations[$p->chunkStreamId]); 500 | $op->invokeHandler(); 501 | $data = $op->getResponse()->arguments instanceof SabreAMF_AMF3_Wrapper ? $op->getResponse()->arguments->getData() : $op->getResponse()->arguments; 502 | if($op->getResponse()->isError()) 503 | { 504 | $data = (object)$data; 505 | throw new Exception($data->description . (isset($data->application)&&!empty($data->application)?" (Application specific message: {$data->application})":'')); 506 | } 507 | return $data; 508 | } 509 | else 510 | { 511 | //Remote invoke from server 512 | $h = $op->getResponse()->commandName; 513 | if($this->client) 514 | $h = array($this->client,$h); 515 | is_callable($h) && call_user_func_array($h,$op->getResponse()->arguments); 516 | $op->clearResponse(); 517 | return; 518 | } 519 | 520 | 521 | } 522 | 523 | 524 | //------------------------------------ 525 | // Internal handlers 526 | //------------------------------------ 527 | /** 528 | * On connect handler 529 | * @internal 530 | * @param RtmpMessage $m 531 | * 532 | */ 533 | public function onConnect(RtmpOperation $m) 534 | { 535 | $this->connected = true; 536 | unset($this->prevSendingPacket[$m->getResponse()->getPacket()->chunkStreamId]); 537 | 538 | } 539 | } 540 | -------------------------------------------------------------------------------- /RtmpMessage.class.php: -------------------------------------------------------------------------------- 1 | commandName = $commandName; 22 | $this->commandObject = $commandObject; 23 | $this->arguments = $arguments; 24 | 25 | } 26 | /** 27 | * getPacket 28 | * 29 | * @return RtmpPacket 30 | */ 31 | public function getPacket() 32 | { 33 | return $this->packet; 34 | } 35 | public function setPacket($packet) 36 | { 37 | $this->packet = $packet; 38 | } 39 | /** 40 | * Encode Message 41 | * 42 | * @param int $amfVersion 43 | * @return RtmpPacket 44 | */ 45 | public function encode() 46 | { 47 | $amfVersion = 3; //Using AMF3 48 | 49 | //Increment transaction id 50 | $this->transactionId = self::$currentTransactionID++; 51 | 52 | //Create packet 53 | $p = new RtmpPacket(); 54 | if($this->commandName == "connect") 55 | { 56 | $this->transactionId = 1; 57 | $amfVersion = 0; //Connect packet must be in AMF0 58 | } 59 | $p->chunkStreamId = 3; 60 | $p->streamId = 0; 61 | $p->chunkType = RtmpPacket::CHUNK_TYPE_0; 62 | $p->type = $amfVersion == 0 ? RtmpPacket::TYPE_INVOKE_AMF0 : RtmpPacket::TYPE_INVOKE_AMF3 ; //Invoke 63 | 64 | //Encoding payload 65 | $stream = new SabreAMF_OutputStream(); 66 | $serializer = new SabreAMF_AMF0_Serializer($stream); 67 | $serializer->writeAMFData($this->commandName); 68 | $serializer->writeAMFData($this->transactionId); 69 | 70 | $serializer->writeAMFData($this->commandObject); 71 | if($this->arguments != null) 72 | foreach($this->arguments as $arg) 73 | $serializer->writeAMFData($arg); 74 | $p->payload = ''; 75 | if($amfVersion == 3) 76 | $p->payload = "\x00"; //XXX: put empty bytes in amf3 mode...I don't know why..*/ 77 | $p->payload .= $stream->getRawData(); 78 | 79 | $this->packet = $p; 80 | 81 | return $p; 82 | } 83 | public function decode(RtmpPacket $p) 84 | { 85 | $this->packet = $p; 86 | $amfVersion = $p->type == RtmpPacket::TYPE_INVOKE_AMF0?0:3; 87 | if($amfVersion==3 && $p->payload{0}==chr(0)) 88 | { 89 | $p->payload = substr($p->payload,1); 90 | $amfVersion = 0; 91 | } 92 | 93 | $stream = new SabreAMF_InputStream($p->payload); 94 | $deserializer = $amfVersion == 0 ? new SabreAMF_AMF0_Deserializer($stream) : new SabreAMF_AMF3_Deserializer($stream); 95 | 96 | $this->commandName = $deserializer->readAMFData(); 97 | $this->transactionId = $deserializer->readAMFData(); 98 | $this->commandObject = $deserializer->readAMFData(); 99 | try 100 | { 101 | $this->arguments = $deserializer->readAMFData(); 102 | } 103 | catch(Exception $e) 104 | { 105 | //if not exists InputStream throw exeception 106 | $this->arguments = null; 107 | } 108 | if(($this->commandName == "_error") || (is_array($this->arguments) && !empty($this->arguments) && isset($this->arguments['level']) && ($this->arguments['level']=='error'))) 109 | $this->_isError = true; 110 | } 111 | private $_isError = false; 112 | public function isError() 113 | { 114 | return $this->_isError; 115 | } 116 | /** 117 | * Return if message is a response message 118 | * 119 | * @return bool 120 | */ 121 | public function isResponseCommand() 122 | { 123 | return $this->commandName == "_result" || $this->commandName == "_error"; 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /RtmpOperation.class.php: -------------------------------------------------------------------------------- 1 | call = $call; 15 | $call->encode(); 16 | $this->chunkStreamID = $call->getPacket()->chunkStreamId; 17 | } 18 | $this->handler = $handler; 19 | } 20 | 21 | public function getChunkStreamID() 22 | { 23 | return $this->chunkStreamID; 24 | } 25 | 26 | /** 27 | * getCall 28 | * 29 | * @return RtmpMessage 30 | */ 31 | public function getCall() 32 | { 33 | return $this->call; 34 | } 35 | 36 | /** 37 | * getResponse 38 | * 39 | * @return RtmpMessage 40 | */ 41 | public function getResponse() 42 | { 43 | return $this->response; 44 | } 45 | public function clearResponse() 46 | { 47 | $this->response = null; 48 | } 49 | /** 50 | * CReate response from packet 51 | * 52 | * @param RtmpPacket $packet 53 | */ 54 | public function createResponse(RtmpPacket $packet) 55 | { 56 | $this->response = new RtmpMessage(); 57 | $this->response->setPacket($packet); 58 | } 59 | 60 | public function invokeHandler() 61 | { 62 | is_callable($this->handler) && call_user_func($this->handler,$this); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /RtmpPacket.class.php: -------------------------------------------------------------------------------- 1 | chunkType = 0; 41 | $this->chunkStreamId = 0; 42 | $this->timestamp = 0; 43 | $this->length = 0; 44 | $this->type = 0; 45 | $this->streamId = 0; 46 | $this->hasAbsTimestamp = false; 47 | $this->bytesRead = 0; 48 | $this->payload = null; 49 | } 50 | 51 | public function free() 52 | { 53 | $this->payload = null; 54 | } 55 | 56 | public function isReady() 57 | { 58 | return $this->bytesRead == $this->length; 59 | } 60 | } 61 | 62 | -------------------------------------------------------------------------------- /RtmpSocket.class.php: -------------------------------------------------------------------------------- 1 | close(); 26 | $this->host = $host; 27 | $this->port = $port; 28 | if (($this->socket = socket_create(AF_INET, SOCK_STREAM, 0)) == false) 29 | throw new Exception("Unable to create socket."); 30 | if (!socket_connect($this->socket, $this->host, $this->port)) 31 | throw new Exception("Could not connect to $this->host:$this->port"); 32 | return $this->socket != null; 33 | } 34 | /** 35 | * Close socket 36 | * 37 | */ 38 | public function close() 39 | { 40 | $this->socket && socket_close($this->socket); 41 | } 42 | /** 43 | * Read socket 44 | * 45 | * @param int $length 46 | * @return RtmpStream 47 | */ 48 | public function read($length) 49 | { 50 | $buff = ""; 51 | $t = time(); 52 | do 53 | { 54 | $recv = ""; 55 | $recv = socket_read($this->socket, $length - strlen($buff), PHP_BINARY_READ); 56 | if($recv === false) 57 | throw new Exception("Could not read socket"); 58 | 59 | if($recv != "") 60 | $buff .= $recv; 61 | 62 | if(time() > $t + $this->timeout) 63 | throw new Exception("Timeout, could not read socket"); 64 | } 65 | while($recv != "" && strlen($buff) < $length); 66 | $this->recvBuffer = substr($buff,$length); 67 | return new RtmpStream(substr($buff,0,$length)); 68 | } 69 | /** 70 | * Write data 71 | * 72 | * @param RtmpStream $data 73 | * @param int $n 74 | * @return bool 75 | */ 76 | public function write(RtmpStream $data, $n = -1) 77 | { 78 | $buffer = $data->flush($n); 79 | $n = strlen($buffer); 80 | while($n>0) 81 | { 82 | $nBytes = socket_write($this->socket,$buffer,$n); 83 | if($nBytes === false) 84 | { 85 | $this->close(); 86 | return false; 87 | } 88 | 89 | if($nBytes == 0) 90 | break; 91 | 92 | $n -= $nBytes; 93 | $buffer = substr($buffer, $nBytes); 94 | } 95 | return true; 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /RtmpStream.class.php: -------------------------------------------------------------------------------- 1 | _data = $data; 10 | } 11 | 12 | public function reset() 13 | { 14 | $this->_index = 0; 15 | } 16 | 17 | public function flush($length = -1) 18 | { 19 | if($length == -1) 20 | { 21 | $d = $this->_data; 22 | $this->_data = ""; 23 | } 24 | else 25 | { 26 | $d = substr($this->_data,0,$length); 27 | $this->_data = substr($this->_data,$length); 28 | } 29 | $this->_index = 0; 30 | return $d; 31 | } 32 | public function dump() 33 | { 34 | return $this->_data; 35 | } 36 | public function begin() 37 | { 38 | $this->_index = 0; 39 | return $this; 40 | } 41 | public function move($pos) 42 | { 43 | $this->_index = max(array(0,min(array($pos,strlen($data))))); 44 | return $this; 45 | } 46 | public function end() 47 | { 48 | $this->_index = strlen($this->_data); 49 | return $this; 50 | } 51 | public function push($data) 52 | { 53 | $this->_data .= $data; 54 | return $this; 55 | } 56 | //-------------------------------- 57 | // Writer 58 | //-------------------------------- 59 | 60 | public function writeByte($value) 61 | { 62 | $this->_data .= is_int($value)?chr($value):$value; 63 | $this->_index++; 64 | } 65 | 66 | public function writeInt16($value) 67 | { 68 | $this->_data .= pack("s",$value); 69 | $this->_index += 2; 70 | } 71 | public function writeInt24($value) 72 | { 73 | $this->_data .= substr(pack("N",$value),1); 74 | $this->_index += 3; 75 | } 76 | public function writeInt32($value) 77 | { 78 | $this->_data .= pack("N",$value); 79 | $this->_index += 4; 80 | } 81 | public function writeInt32LE($value) 82 | { 83 | $this->_data .= pack("V",$value); 84 | $this->_index += 4; 85 | } 86 | public function write($value) 87 | { 88 | $this->_data .= $value; 89 | $this->_index += strlen($value); 90 | } 91 | //------------------------------- 92 | // Reader 93 | //------------------------------- 94 | 95 | public function readByte() 96 | { 97 | return ($this->_data[$this->_index++]); 98 | } 99 | public function readTinyInt() 100 | { 101 | return ord($this->readByte()); 102 | } 103 | public function readInt16() 104 | { 105 | return $this->read("s",2); 106 | } 107 | public function readInt24() 108 | { 109 | $m = unpack("N","\x00".substr($this->_data,$this->_index,3)); 110 | $this->_index += 3; 111 | return $m[1]; 112 | } 113 | public function readInt32() 114 | { 115 | return $this->read("N",4); 116 | } 117 | public function readInt32LE() 118 | { 119 | return $this->read("V",4); 120 | } 121 | 122 | public function readRaw($length = 0) 123 | { 124 | if($length == 0) 125 | $length = strlen($this->_data) - $this->_index; 126 | $datas = substr($this->_data,$this->_index,$length); 127 | $this->_index += $length; 128 | return $datas; 129 | } 130 | private function read($type, $size) 131 | { 132 | $m = unpack("$type",substr($this->_data,$this->_index,$size)); 133 | $this->_index += $size; 134 | return $m[1]; 135 | } 136 | 137 | 138 | } 139 | -------------------------------------------------------------------------------- /SabreAMF/AMF0/Const.php: -------------------------------------------------------------------------------- 1 | refList = array(); 51 | 52 | if (is_null($settype)) { 53 | $settype = $this->stream->readByte(); 54 | } 55 | 56 | switch ($settype) { 57 | 58 | case SabreAMF_AMF0_Const::DT_NUMBER : return $this->stream->readDouble(); 59 | case SabreAMF_AMF0_Const::DT_BOOL : return $this->stream->readByte()==true; 60 | case SabreAMF_AMF0_Const::DT_STRING : return $this->readString(); 61 | case SabreAMF_AMF0_Const::DT_OBJECT : return $this->readObject(); 62 | case SabreAMF_AMF0_Const::DT_NULL : return null; 63 | case SabreAMF_AMF0_Const::DT_UNDEFINED : return null; 64 | case SabreAMF_AMF0_Const::DT_REFERENCE : return $this->readReference(); 65 | case SabreAMF_AMF0_Const::DT_MIXEDARRAY : return $this->readMixedArray(); 66 | case SabreAMF_AMF0_Const::DT_ARRAY : return $this->readArray(); 67 | case SabreAMF_AMF0_Const::DT_DATE : return $this->readDate(); 68 | case SabreAMF_AMF0_Const::DT_LONGSTRING : return $this->readLongString(); 69 | case SabreAMF_AMF0_Const::DT_UNSUPPORTED : return null; 70 | case SabreAMF_AMF0_Const::DT_XML : return $this->readLongString(); 71 | case SabreAMF_AMF0_Const::DT_TYPEDOBJECT : return $this->readTypedObject(); 72 | case SabreAMF_AMF0_Const::DT_AMF3 : return $this->readAMF3Data(); 73 | default : throw new Exception('Unsupported type: 0x' . strtoupper(str_pad(dechex($settype),2,0,STR_PAD_LEFT))); return false; 74 | 75 | } 76 | 77 | } 78 | 79 | /** 80 | * readObject 81 | * 82 | * @return object 83 | */ 84 | public function readObject() { 85 | 86 | $object = array(); 87 | $this->refList[] =& $object; 88 | while (true) { 89 | $key = $this->readString(); 90 | $vartype = $this->stream->readByte(); 91 | if ($vartype==SabreAMF_AMF0_Const::DT_OBJECTTERM) break; 92 | $object[$key] = $this->readAmfData($vartype); 93 | } 94 | if (defined('SABREAMF_OBJECT_AS_ARRAY')) { 95 | $object = (object)$object; 96 | } 97 | return $object; 98 | 99 | } 100 | 101 | /** 102 | * readReference 103 | * 104 | * @return object 105 | */ 106 | public function readReference() { 107 | 108 | $refId = $this->stream->readInt(); 109 | if (isset($this->refList[$refId])) { 110 | return $this->refList[$refId]; 111 | } else { 112 | throw new Exception('Invalid reference offset: ' . $refId); 113 | return false; 114 | } 115 | 116 | } 117 | 118 | 119 | /** 120 | * readArray 121 | * 122 | * @return array 123 | */ 124 | public function readArray() { 125 | 126 | $length = $this->stream->readLong(); 127 | $arr = array(); 128 | $this->refList[]&=$arr; 129 | while($length--) $arr[] = $this->readAMFData(); 130 | return $arr; 131 | 132 | } 133 | 134 | /** 135 | * readMixedArray 136 | * 137 | * @return array 138 | */ 139 | public function readMixedArray() { 140 | 141 | $highestIndex = $this->stream->readLong(); 142 | return $this->readObject(); 143 | 144 | } 145 | 146 | /** 147 | * readString 148 | * 149 | * @return string 150 | */ 151 | public function readString() { 152 | 153 | $strLen = $this->stream->readInt(); 154 | return $this->stream->readBuffer($strLen); 155 | 156 | } 157 | 158 | /** 159 | * readLongString 160 | * 161 | * @return string 162 | */ 163 | public function readLongString() { 164 | 165 | $strLen = $this->stream->readLong(); 166 | return $this->stream->readBuffer($strLen); 167 | 168 | } 169 | 170 | /** 171 | * 172 | * readDate 173 | * 174 | * @return int 175 | */ 176 | public function readDate() { 177 | 178 | // Unix timestamp in seconds. We strip the millisecond part 179 | $timestamp = floor($this->stream->readDouble() / 1000); 180 | 181 | // we are ignoring the timezone 182 | $timezoneOffset = $this->stream->readInt(); 183 | //if ($timezoneOffset > 720) $timezoneOffset = ((65536 - $timezoneOffset)); 184 | //$timezoneOffset=($timezoneOffset * 60) - date('Z'); 185 | 186 | $dateTime = new DateTime('@' . $timestamp); 187 | 188 | return $dateTime; 189 | 190 | } 191 | 192 | /** 193 | * readTypedObject 194 | * 195 | * @return object 196 | */ 197 | public function readTypedObject() { 198 | 199 | $classname = $this->readString(); 200 | 201 | $isMapped = false; 202 | 203 | if ($classname = $this->getLocalClassName($classname)) { 204 | $rObject = new $classname(); 205 | $isMapped = true; 206 | } else { 207 | $rObject = new SabreAMF_TypedObject($classname,null); 208 | } 209 | $this->refList[] =& $rObject; 210 | 211 | $props = array(); 212 | while (true) { 213 | $key = $this->readString(); 214 | $vartype = $this->stream->readByte(); 215 | if ($vartype==SabreAMF_AMF0_Const::DT_OBJECTTERM) break; 216 | $props[$key] = $this->readAmfData($vartype); 217 | } 218 | 219 | if ($isMapped) { 220 | foreach($props as $k=>$v) 221 | $rObject->$k = $v; 222 | } else { 223 | $rObject->setAMFData($props); 224 | } 225 | 226 | return $rObject; 227 | 228 | } 229 | 230 | /** 231 | * readAMF3Data 232 | * 233 | * @return SabreAMF_AMF3_Wrapper 234 | */ 235 | public function readAMF3Data() { 236 | 237 | $amf3Deserializer = new SabreAMF_AMF3_Deserializer($this->stream); 238 | return new SabreAMF_AMF3_Wrapper($amf3Deserializer->readAMFData()); 239 | 240 | } 241 | 242 | 243 | } 244 | 245 | 246 | -------------------------------------------------------------------------------- /SabreAMF/AMF0/Serializer.php: -------------------------------------------------------------------------------- 1 | 65536) $type = SabreAMF_AMF0_Const::DT_LONGSTRING; 51 | 52 | // Normal string 53 | if (!$type && is_string($data)) $type = SabreAMF_AMF0_Const::DT_STRING; 54 | 55 | // Checking if its an array 56 | if (!$type && is_array($data)) { 57 | 58 | // Looping through the array to see if there are any 59 | // non-numeric keys 60 | foreach(array_keys($data) as $key) { 61 | if (!is_numeric($key)) { 62 | // There's a non-numeric key.. we'll make it a mixed 63 | // array 64 | $type = SabreAMF_AMF0_Const::DT_MIXEDARRAY; 65 | break; 66 | } 67 | } 68 | 69 | // Pure array 70 | if (!$type) $type = SabreAMF_AMF0_Const::DT_ARRAY; 71 | } 72 | 73 | // Its an object 74 | if (!$type && is_object($data)) { 75 | 76 | // If its an AMF3 wrapper.. we treat it as such 77 | if ($data instanceof SabreAMF_AMF3_Wrapper) $type = SabreAMF_AMF0_Const::DT_AMF3; 78 | 79 | else if ($data instanceof DateTime) $type = SabreAMF_AMF0_Const::DT_DATE; 80 | 81 | // We'll see if its registered in the classmapper 82 | else if ($this->getRemoteClassName(get_class($data))) $type = SabreAMF_AMF0_Const::DT_TYPEDOBJECT; 83 | 84 | // Otherwise.. check if it its an TypedObject 85 | else if ($data instanceof SabreAMF_ITypedObject) $type = SabreAMF_AMF0_Const::DT_TYPEDOBJECT; 86 | 87 | // If everything else fails, its a general object 88 | else $type = SabreAMF_AMF0_Const::DT_OBJECT; 89 | } 90 | 91 | // If everything failed, throw an exception 92 | if ($type===false) { 93 | throw new Exception('Unhandled data-type: ' . gettype($data)); 94 | return null; 95 | } 96 | } else $type = $forcetype; 97 | 98 | $this->stream->writeByte($type); 99 | 100 | switch ($type) { 101 | 102 | case SabreAMF_AMF0_Const::DT_NUMBER : return $this->stream->writeDouble($data); 103 | case SabreAMF_AMF0_Const::DT_BOOL : return $this->stream->writeByte($data==true); 104 | case SabreAMF_AMF0_Const::DT_STRING : return $this->writeString($data); 105 | case SabreAMF_AMF0_Const::DT_OBJECT : return $this->writeObject($data); 106 | case SabreAMF_AMF0_Const::DT_NULL : return true; 107 | case SabreAMF_AMF0_Const::DT_MIXEDARRAY : return $this->writeMixedArray($data); 108 | case SabreAMF_AMF0_Const::DT_ARRAY : return $this->writeArray($data); 109 | case SabreAMF_AMF0_Const::DT_DATE : return $this->writeDate($data); 110 | case SabreAMF_AMF0_Const::DT_LONGSTRING : return $this->writeLongString($data); 111 | case SabreAMF_AMF0_Const::DT_TYPEDOBJECT : return $this->writeTypedObject($data); 112 | case SabreAMF_AMF0_Const::DT_AMF3 : return $this->writeAMF3Data($data); 113 | default : throw new Exception('Unsupported type: ' . gettype($data)); return false; 114 | 115 | } 116 | 117 | } 118 | 119 | /** 120 | * writeMixedArray 121 | * 122 | * @param array $data 123 | * @return void 124 | */ 125 | public function writeMixedArray($data) { 126 | 127 | $this->stream->writeLong(0); 128 | foreach($data as $key=>$value) { 129 | $this->writeString($key); 130 | $this->writeAMFData($value); 131 | } 132 | $this->writeString(''); 133 | $this->stream->writeByte(SabreAMF_AMF0_Const::DT_OBJECTTERM); 134 | 135 | } 136 | 137 | /** 138 | * writeArray 139 | * 140 | * @param array $data 141 | * @return void 142 | */ 143 | public function writeArray($data) { 144 | 145 | if (!count($data)) { 146 | $this->stream->writeLong(0); 147 | } else { 148 | end($data); 149 | $last = key($data); 150 | $this->stream->writeLong($last+1); 151 | for($i=0;$i<=$last;$i++) { 152 | $item = isset($data[$i])?$data[$i]:NULL; 153 | $this->writeAMFData($item); 154 | } 155 | } 156 | 157 | } 158 | 159 | /** 160 | * writeObject 161 | * 162 | * @param object $data 163 | * @return void 164 | */ 165 | public function writeObject($data) { 166 | 167 | foreach($data as $key=>$value) { 168 | $this->writeString($key); 169 | $this->writeAmfData($value); 170 | } 171 | $this->writeString(''); 172 | $this->stream->writeByte(SabreAMF_AMF0_Const::DT_OBJECTTERM); 173 | return true; 174 | 175 | } 176 | 177 | /** 178 | * writeString 179 | * 180 | * @param string $string 181 | * @return void 182 | */ 183 | public function writeString($string) { 184 | 185 | $this->stream->writeInt(strlen($string)); 186 | $this->stream->writeBuffer($string); 187 | 188 | } 189 | 190 | /** 191 | * writeLongString 192 | * 193 | * @param string $string 194 | * @return void 195 | */ 196 | public function writeLongString($string) { 197 | 198 | $this->stream->writeLong(strlen($string)); 199 | $this->stream->writeBuffer($string); 200 | 201 | } 202 | /** 203 | * writeTypedObject 204 | * 205 | * @param object $data 206 | * @return void 207 | */ 208 | public function writeTypedObject($data) { 209 | 210 | if ($data instanceof SabreAMF_ITypedObject) { 211 | $classname = $data->getAMFClassName(); 212 | $data = $data->getAMFData(); 213 | } else $classname = $this->getRemoteClassName(get_class($data)); 214 | 215 | $this->writeString($classname); 216 | return $this->writeObject($data); 217 | 218 | } 219 | 220 | 221 | /** 222 | * writeAMF3Data 223 | * 224 | * @param mixed $data 225 | * @return void 226 | */ 227 | public function writeAMF3Data(SabreAMF_AMF3_Wrapper $data) { 228 | 229 | $serializer = new SabreAMF_AMF3_Serializer($this->stream); 230 | return $serializer->writeAMFData($data->getData()); 231 | 232 | } 233 | 234 | /** 235 | * Writes a date object 236 | * 237 | * @param DateTime $data 238 | * @return void 239 | */ 240 | public function writeDate(DateTime $data) { 241 | 242 | $this->stream->writeDouble($data->format('U')*1000); 243 | 244 | // empty timezone 245 | $this->stream->writeInt(0); 246 | } 247 | 248 | } 249 | 250 | 251 | -------------------------------------------------------------------------------- /SabreAMF/AMF3/AbstractMessage.php: -------------------------------------------------------------------------------- 1 | messageId = $this->generateRandomId(); 35 | $this->clientId = $this->generateRandomId(); 36 | $this->destination = null; 37 | $this->body = null; 38 | $this->timeToLive = 0; 39 | $this->timestamp = time() . '00'; 40 | $this->headers = new STDClass(); 41 | 42 | if ($message) { 43 | $this->correlationId = $message->messageId; 44 | } 45 | 46 | } 47 | 48 | } 49 | 50 | 51 | -------------------------------------------------------------------------------- /SabreAMF/AMF3/CommandMessage.php: -------------------------------------------------------------------------------- 1 | stream->readByte(); 65 | } 66 | 67 | switch ($settype) { 68 | 69 | case SabreAMF_AMF3_Const::DT_UNDEFINED : return null; 70 | case SabreAMF_AMF3_Const::DT_NULL : return null; 71 | case SabreAMF_AMF3_Const::DT_BOOL_FALSE : return false; 72 | case SabreAMF_AMF3_Const::DT_BOOL_TRUE : return true; 73 | case SabreAMF_AMF3_Const::DT_INTEGER : return $this->readInt(); 74 | case SabreAMF_AMF3_Const::DT_NUMBER : return $this->stream->readDouble(); 75 | case SabreAMF_AMF3_Const::DT_STRING : return $this->readString(); 76 | case SabreAMF_AMF3_Const::DT_XML : return $this->readString(); 77 | case SabreAMF_AMF3_Const::DT_DATE : return $this->readDate(); 78 | case SabreAMF_AMF3_Const::DT_ARRAY : return $this->readArray(); 79 | case SabreAMF_AMF3_Const::DT_OBJECT : return $this->readObject(); 80 | case SabreAMF_AMF3_Const::DT_XMLSTRING : return $this->readXMLString(); 81 | case SabreAMF_AMF3_Const::DT_BYTEARRAY : return $this->readByteArray(); 82 | default : throw new Exception('Unsupported type: 0x' . strtoupper(str_pad(dechex($settype),2,0,STR_PAD_LEFT))); return false; 83 | 84 | 85 | } 86 | 87 | } 88 | 89 | 90 | /** 91 | * readObject 92 | * 93 | * @return object 94 | */ 95 | public function readObject() { 96 | 97 | $objInfo = $this->readU29(); 98 | $storedObject = ($objInfo & 0x01)==0; 99 | $objInfo = $objInfo >> 1; 100 | 101 | if ($storedObject) { 102 | 103 | $objectReference = $objInfo; 104 | if (!isset($this->storedObjects[$objectReference])) { 105 | 106 | throw new Exception('Object reference #' . $objectReference . ' not found'); 107 | 108 | } else { 109 | 110 | $rObject = $this->storedObjects[$objectReference]; 111 | 112 | } 113 | 114 | } else { 115 | 116 | $storedClass = ($objInfo & 0x01)==0; 117 | $objInfo= $objInfo >> 1; 118 | 119 | // If this is a stored class.. we have the info 120 | if ($storedClass) { 121 | 122 | $classReference = $objInfo; 123 | if (!isset($this->storedClasses[$classReference])) { 124 | 125 | throw new Exception('Class reference #' . $classReference . ' not found'); 126 | 127 | } else { 128 | 129 | $encodingType = $this->storedClasses[$classReference]['encodingType']; 130 | $propertyNames = $this->storedClasses[$classReference]['propertyNames']; 131 | $className = $this->storedClasses[$classReference]['className']; 132 | 133 | } 134 | 135 | } else { 136 | 137 | $className = $this->readString(); 138 | $encodingType = $objInfo & 0x03; 139 | $propertyNames = array(); 140 | $objInfo = $objInfo >> 2; 141 | 142 | } 143 | 144 | //ClassMapping magic 145 | if ($className) { 146 | 147 | if ($localClassName = $this->getLocalClassName($className)) { 148 | 149 | $rObject = new $localClassName(); 150 | 151 | } else { 152 | 153 | $rObject = new SabreAMF_TypedObject($className,array()); 154 | 155 | } 156 | } else { 157 | 158 | $rObject = new STDClass(); 159 | 160 | } 161 | 162 | $this->storedObjects[] =& $rObject; 163 | 164 | if ($encodingType & SabreAMF_AMF3_Const::ET_EXTERNALIZED) { 165 | 166 | if (!$storedClass) { 167 | $this->storedClasses[] = array('className' => $className,'encodingType'=>$encodingType,'propertyNames'=>$propertyNames); 168 | } 169 | if ($rObject instanceof SabreAMF_Externalized) { 170 | $rObject->readExternal($this->readAMFData()); 171 | } elseif ($rObject instanceof SabreAMF_TypedObject) { 172 | $rObject->setAMFData(array('externalizedData'=>$this->readAMFData())); 173 | } else { 174 | $rObject->externalizedData = $this->readAMFData(); 175 | } 176 | //$properties['externalizedData'] = $this->readAMFData(); 177 | 178 | } else { 179 | 180 | if ($encodingType & SabreAMF_AMF3_Const::ET_SERIAL) { 181 | 182 | if (!$storedClass) { 183 | $this->storedClasses[] = array('className' => $className,'encodingType'=>$encodingType,'propertyNames'=>$propertyNames); 184 | } 185 | $properties = array(); 186 | do { 187 | $propertyName = $this->readString(); 188 | if ($propertyName!="") { 189 | $propertyNames[] = $propertyName; 190 | $properties[$propertyName] = $this->readAMFData(); 191 | } 192 | } while ($propertyName!=""); 193 | 194 | 195 | } else { 196 | if (!$storedClass) { 197 | $propertyCount = $objInfo; 198 | for($i=0;$i<$propertyCount;$i++) { 199 | 200 | $propertyNames[] = $this->readString(); 201 | 202 | } 203 | $this->storedClasses[] = array('className' => $className,'encodingType'=>$encodingType,'propertyNames'=>$propertyNames); 204 | 205 | } 206 | 207 | $properties = array(); 208 | foreach($propertyNames as $propertyName) { 209 | 210 | $properties[$propertyName] = $this->readAMFData(); 211 | 212 | } 213 | 214 | } 215 | 216 | if ($rObject instanceof SabreAMF_TypedObject) { 217 | $rObject->setAMFData($properties); 218 | } else { 219 | foreach($properties as $k=>$v) if ($k) $rObject->$k = $v; 220 | } 221 | 222 | } 223 | 224 | } 225 | return $rObject; 226 | 227 | } 228 | 229 | /** 230 | * readArray 231 | * 232 | * @return array 233 | */ 234 | public function readArray() { 235 | 236 | $arrId = $this->readU29(); 237 | if (($arrId & 0x01)==0) { 238 | $arrId = $arrId >> 1; 239 | if ($arrId>=count($this->storedObjects)) { 240 | throw new Exception('Undefined array reference: ' . $arrId); 241 | return false; 242 | } 243 | return $this->storedObjects[$arrId]; 244 | } 245 | $arrId = $arrId >> 1; 246 | 247 | $data = array(); 248 | $this->storedObjects[] &= $data; 249 | 250 | $key = $this->readString(); 251 | 252 | while($key!="") { 253 | $data[$key] = $this->readAMFData(); 254 | $key = $this->readString(); 255 | } 256 | 257 | for($i=0;$i<$arrId;$i++) { 258 | $data[] = $this->readAMFData(); 259 | } 260 | 261 | return $data; 262 | 263 | } 264 | 265 | 266 | /** 267 | * readString 268 | * 269 | * @return string 270 | */ 271 | public function readString() { 272 | 273 | $strref = $this->readU29(); 274 | 275 | if (($strref & 0x01) == 0) { 276 | $strref = $strref >> 1; 277 | if ($strref>=count($this->storedStrings)) { 278 | throw new Exception('Undefined string reference: ' . $strref); 279 | return false; 280 | } 281 | return $this->storedStrings[$strref]; 282 | } else { 283 | $strlen = $strref >> 1; 284 | $str = $this->stream->readBuffer($strlen); 285 | if ($str != "") $this->storedStrings[] = $str; 286 | return $str; 287 | } 288 | 289 | } 290 | 291 | 292 | /** 293 | * readString 294 | * 295 | * @return string 296 | */ 297 | public function readXMLString() { 298 | 299 | $strref = $this->readU29(); 300 | 301 | $strlen = $strref >> 1; 302 | $str = $this->stream->readBuffer($strlen); 303 | return simplexml_load_string($str); 304 | 305 | } 306 | 307 | /** 308 | * readString 309 | * 310 | * @return string 311 | */ 312 | public function readByteArray() { 313 | 314 | $strref = $this->readU29(); 315 | 316 | $strlen = $strref >> 1; 317 | $str = $this->stream->readBuffer($strlen); 318 | return new SabreAMF_ByteArray($str); 319 | 320 | } 321 | 322 | /** 323 | * readU29 324 | * 325 | * @return int 326 | */ 327 | public function readU29() { 328 | 329 | $count = 1; 330 | $u29 = 0; 331 | 332 | $byte = $this->stream->readByte(); 333 | 334 | while((($byte & 0x80) != 0) && $count < 4) { 335 | $u29 <<= 7; 336 | $u29 |= ($byte & 0x7f); 337 | $byte = $this->stream->readByte(); 338 | $count++; 339 | } 340 | 341 | if ($count < 4) { 342 | $u29 <<= 7; 343 | $u29 |= $byte; 344 | } else { 345 | // Use all 8 bits from the 4th byte 346 | $u29 <<= 8; 347 | $u29 |= $byte; 348 | } 349 | 350 | return $u29; 351 | 352 | } 353 | 354 | /** 355 | * readInt 356 | * 357 | * @return int 358 | */ 359 | public function readInt() { 360 | 361 | $int = $this->readU29(); 362 | // if int and has the sign bit set 363 | // Check if the integer is an int 364 | // and is signed 365 | if (($int & 0x18000000) != 0) { 366 | $int ^= 0x1fffffff; 367 | $int *= -1; 368 | $int -= 1; 369 | } 370 | 371 | return $int; 372 | 373 | } 374 | 375 | /** 376 | * readDate 377 | * 378 | * @return int 379 | */ 380 | public function readDate() { 381 | $dateref = $this->readU29(); 382 | if (($dateref & 0x01) == 0) { 383 | $dateref = $dateref >> 1; 384 | if ($dateref>=count($this->storedObjects)) { 385 | throw new Exception('Undefined date reference: ' . $dateref); 386 | return false; 387 | } 388 | return $this->storedObjects[$dateref]; 389 | } 390 | 391 | $timestamp = floor($this->stream->readDouble() / 1000); 392 | 393 | $dateTime = new DateTime('@' . $timestamp); 394 | 395 | $this->storedObjects[] = $dateTime; 396 | return $dateTime; 397 | } 398 | 399 | 400 | } 401 | 402 | 403 | -------------------------------------------------------------------------------- /SabreAMF/AMF3/ErrorMessage.php: -------------------------------------------------------------------------------- 1 | messageId = $this->generateRandomId(); 44 | $this->clientId = $this->generateRandomId(); 45 | $this->destination = null; 46 | $this->body = null; 47 | $this->timeToLive = 0; 48 | $this->timestamp = time() . '00'; 49 | $this->headers = new STDClass(); 50 | 51 | } 52 | } 53 | 54 | 55 | -------------------------------------------------------------------------------- /SabreAMF/AMF3/Serializer.php: -------------------------------------------------------------------------------- 1 | 0xFFFFFFF || $data < -268435456) { 45 | $type = SabreAMF_AMF3_Const::DT_NUMBER; 46 | } else { 47 | $type = SabreAMF_AMF3_Const::DT_INTEGER; 48 | } 49 | } 50 | if (!$type && is_float($data)) $type = SabreAMF_AMF3_Const::DT_NUMBER; 51 | if (!$type && is_int($data)) $type = SabreAMF_AMF3_Const::DT_INTEGER; 52 | if (!$type && is_string($data)) $type = SabreAMF_AMF3_Const::DT_STRING; 53 | if (!$type && is_array($data)) $type = SabreAMF_AMF3_Const::DT_ARRAY; 54 | if (!$type && is_object($data)) { 55 | 56 | if ($data instanceof SabreAMF_ByteArray) 57 | $type = SabreAMF_AMF3_Const::DT_BYTEARRAY; 58 | elseif ($data instanceof DateTime) 59 | $type = SabreAMF_AMF3_Const::DT_DATE; 60 | else 61 | $type = SabreAMF_AMF3_Const::DT_OBJECT; 62 | 63 | 64 | } 65 | if ($type===false) { 66 | throw new Exception('Unhandled data-type: ' . gettype($data)); 67 | return null; 68 | } 69 | if ($type == SabreAMF_AMF3_Const::DT_INTEGER && ($data > 268435455 || $data < -268435456)) { 70 | $type = SabreAMF_AMF3_Const::DT_NUMBER; 71 | } 72 | } else $type = $forcetype; 73 | 74 | $this->stream->writeByte($type); 75 | 76 | switch ($type) { 77 | 78 | case SabreAMF_AMF3_Const::DT_NULL : break; 79 | case SabreAMF_AMF3_Const::DT_BOOL_FALSE : break; 80 | case SabreAMF_AMF3_Const::DT_BOOL_TRUE : break; 81 | case SabreAMF_AMF3_Const::DT_INTEGER : $this->writeInt($data); break; 82 | case SabreAMF_AMF3_Const::DT_NUMBER : $this->stream->writeDouble($data); break; 83 | case SabreAMF_AMF3_Const::DT_STRING : $this->writeString($data); break; 84 | case SabreAMF_AMF3_Const::DT_DATE : $this->writeDate($data); break; 85 | case SabreAMF_AMF3_Const::DT_ARRAY : $this->writeArray($data); break; 86 | case SabreAMF_AMF3_Const::DT_OBJECT : $this->writeObject($data); break; 87 | case SabreAMF_AMF3_Const::DT_BYTEARRAY : $this->writeByteArray($data); break; 88 | default : throw new Exception('Unsupported type: ' . gettype($data)); return null; 89 | 90 | } 91 | 92 | } 93 | 94 | /** 95 | * writeObject 96 | * 97 | * @param mixed $data 98 | * @return void 99 | */ 100 | public function writeObject($data) { 101 | 102 | $encodingType = SabreAMF_AMF3_Const::ET_PROPLIST; 103 | if ($data instanceof SabreAMF_ITypedObject) { 104 | 105 | $classname = $data->getAMFClassName(); 106 | $data = $data->getAMFData(); 107 | 108 | } else if (!$classname = $this->getRemoteClassName(get_class($data))) { 109 | 110 | 111 | $classname = ''; 112 | 113 | } else { 114 | 115 | if ($data instanceof SabreAMF_Externalized) { 116 | 117 | $encodingType = SabreAMF_AMF3_Const::ET_EXTERNALIZED; 118 | 119 | } 120 | 121 | } 122 | 123 | 124 | $objectInfo = 0x03; 125 | $objectInfo |= $encodingType << 2; 126 | 127 | switch($encodingType) { 128 | 129 | case SabreAMF_AMF3_Const::ET_PROPLIST : 130 | 131 | $propertyCount=0; 132 | foreach($data as $k=>$v) { 133 | $propertyCount++; 134 | } 135 | 136 | $objectInfo |= ($propertyCount << 4); 137 | 138 | 139 | $this->writeInt($objectInfo); 140 | $this->writeString($classname); 141 | foreach($data as $k=>$v) { 142 | 143 | $this->writeString($k); 144 | 145 | } 146 | foreach($data as $k=>$v) { 147 | 148 | $this->writeAMFData($v); 149 | 150 | } 151 | break; 152 | 153 | case SabreAMF_AMF3_Const::ET_EXTERNALIZED : 154 | 155 | $this->writeInt($objectInfo); 156 | $this->writeString($classname); 157 | $this->writeAMFData($data->writeExternal()); 158 | break; 159 | } 160 | 161 | } 162 | 163 | /** 164 | * writeInt 165 | * 166 | * @param int $int 167 | * @return void 168 | */ 169 | public function writeInt($int) { 170 | 171 | // Note that this is simply a sanity check of the conversion algorithm; 172 | // when live this sanity check should be disabled (overflow check handled in this.writeAMFData). 173 | /*if ( ( ( $int & 0x70000000 ) != 0 ) && ( ( $int & 0x80000000 ) == 0 ) ) 174 | throw new Exception ( 'Integer overflow during Int32 to AMF3 conversion' );*/ 175 | 176 | if ( ( $int & 0xffffff80 ) == 0 ) 177 | { 178 | $this->stream->writeByte ( $int & 0x7f ); 179 | 180 | return; 181 | } 182 | 183 | if ( ( $int & 0xffffc000 ) == 0 ) 184 | { 185 | $this->stream->writeByte ( ( $int >> 7 ) | 0x80 ); 186 | $this->stream->writeByte ( $int & 0x7f ); 187 | 188 | return; 189 | } 190 | 191 | if ( ( $int & 0xffe00000 ) == 0 ) 192 | { 193 | $this->stream->writeByte ( ( $int >> 14 ) | 0x80 ); 194 | $this->stream->writeByte ( ( $int >> 7 ) | 0x80 ); 195 | $this->stream->writeByte ( $int & 0x7f ); 196 | 197 | return; 198 | } 199 | 200 | $this->stream->writeByte ( ( $int >> 22 ) | 0x80 ); 201 | $this->stream->writeByte ( ( $int >> 15 ) | 0x80 ); 202 | $this->stream->writeByte ( ( $int >> 8 ) | 0x80 ); 203 | $this->stream->writeByte ( $int & 0xff ); 204 | 205 | return; 206 | } 207 | 208 | public function writeByteArray(SabreAMF_ByteArray $data) { 209 | 210 | $this->writeString($data->getData()); 211 | 212 | } 213 | 214 | /** 215 | * writeString 216 | * 217 | * @param string $str 218 | * @return void 219 | */ 220 | public function writeString($str) { 221 | 222 | $strref = strlen($str) << 1 | 0x01; 223 | $this->writeInt($strref); 224 | $this->stream->writeBuffer($str); 225 | 226 | } 227 | 228 | /** 229 | * writeArray 230 | * 231 | * @param array $arr 232 | * @return void 233 | */ 234 | public function writeArray(array $arr) { 235 | 236 | end($arr); 237 | 238 | // We need to split up strings an numeric array keys 239 | $num = array(); 240 | $string = array(); 241 | foreach($arr as $k=>$v) { 242 | if (is_int($k)) $num[] = $v; else $string[$k] = $v; 243 | } 244 | 245 | unset($arr); 246 | 247 | // Writing the length for the numeric keys in the array 248 | $arrLen = count($num); 249 | $arrId = ($arrLen << 1) | 0x01; 250 | 251 | $this->writeInt($arrId); 252 | 253 | foreach($string as $key=>$v) { 254 | $this->writeString($key); 255 | $this->writeAMFData($v); 256 | } 257 | $this->writeString(""); 258 | 259 | foreach($num as $v) { 260 | $this->writeAMFData($v); 261 | } 262 | 263 | } 264 | 265 | /** 266 | * Writes a date object 267 | * 268 | * @param DateTime $data 269 | * @return void 270 | */ 271 | public function writeDate(DateTime $data) { 272 | 273 | // We're always sending actual date objects, never references 274 | $this->writeInt(0x01); 275 | $this->stream->writeDouble($data->format('U')*1000); 276 | 277 | } 278 | 279 | } 280 | 281 | 282 | -------------------------------------------------------------------------------- /SabreAMF/AMF3/Wrapper.php: -------------------------------------------------------------------------------- 1 | setData($data); 33 | 34 | } 35 | 36 | 37 | /** 38 | * getData 39 | * 40 | * @return mixed 41 | */ 42 | public function getData() { 43 | 44 | return $this->data; 45 | 46 | } 47 | 48 | /** 49 | * setData 50 | * 51 | * @param mixed $data 52 | * @return void 53 | */ 54 | public function setData($data) { 55 | 56 | $this->data = $data; 57 | 58 | } 59 | 60 | 61 | } 62 | 63 | 64 | -------------------------------------------------------------------------------- /SabreAMF/ArrayCollection.php: -------------------------------------------------------------------------------- 1 | data = new ArrayObject($data); 38 | 39 | } 40 | 41 | /** 42 | * This is used by SabreAMF when this object is unserialized (from AMF3) 43 | * 44 | * @param array $data 45 | * @return void 46 | */ 47 | function readExternal($data) { 48 | 49 | $this->data = new ArrayObject($data); 50 | 51 | } 52 | 53 | /** 54 | * This is used by SabreAMF when this object is serialized 55 | * 56 | * @return array 57 | */ 58 | function writeExternal() { 59 | 60 | return iterator_to_array($this->data); 61 | 62 | } 63 | 64 | /** 65 | * implemented from IteratorAggregate 66 | * 67 | * @return ArrayObject 68 | */ 69 | function getIterator() { 70 | 71 | return $this->data; 72 | 73 | } 74 | 75 | /** 76 | * implemented from ArrayAccess 77 | * 78 | * @param mixed $offset 79 | * @return bool 80 | */ 81 | function offsetExists($offset) { 82 | 83 | return isset($this->data[$offset]); 84 | 85 | } 86 | 87 | /** 88 | * Implemented from ArrayAccess 89 | * 90 | * @param mixed $offset 91 | * @return mixed 92 | */ 93 | function offsetGet($offset) { 94 | 95 | return $this->data[$offset]; 96 | 97 | } 98 | 99 | /** 100 | * Implemented from ArrayAccess 101 | * 102 | * @param mixed $offset 103 | * @param mixed $value 104 | * @return void 105 | */ 106 | function offsetSet($offset,$value) { 107 | 108 | if (!is_null($offset)) { 109 | $this->data[$offset] = $value; 110 | } else { 111 | $this->data[] = $value; 112 | } 113 | 114 | } 115 | 116 | /** 117 | * Implemented from ArrayAccess 118 | * 119 | * @param mixed $offset 120 | * @return void 121 | */ 122 | function offsetUnset($offset) { 123 | 124 | unset($this->data[$offset]); 125 | 126 | } 127 | 128 | /** 129 | * Implemented from Countable 130 | * 131 | * @return int 132 | */ 133 | function count() { 134 | 135 | return count($this->data); 136 | 137 | } 138 | 139 | } 140 | 141 | 142 | -------------------------------------------------------------------------------- /SabreAMF/ByteArray.php: -------------------------------------------------------------------------------- 1 | data = $data; 30 | 31 | } 32 | 33 | /** 34 | * getData 35 | * 36 | * @return string 37 | */ 38 | function getData() { 39 | 40 | return $this->data; 41 | 42 | } 43 | 44 | /** 45 | * setData 46 | * 47 | * @param string $data 48 | * @return void 49 | */ 50 | function setData($data) { 51 | 52 | $this->data = $data; 53 | 54 | } 55 | 56 | } 57 | 58 | 59 | -------------------------------------------------------------------------------- /SabreAMF/CallbackServer.php: -------------------------------------------------------------------------------- 1 | operation) { 54 | 55 | case SabreAMF_AMF3_CommandMessage::CLIENT_PING_OPERATION : 56 | $response = new SabreAMF_AMF3_AcknowledgeMessage($request); 57 | break; 58 | case SabreAMF_AMF3_CommandMessage::LOGIN_OPERATION : 59 | $authData = base64_decode($request->body); 60 | if ($authData) { 61 | $authData = explode(':',$authData,2); 62 | if (count($authData)==2) { 63 | $this->authenticate($authData[0],$authData[1]); 64 | } 65 | } 66 | $response = new SabreAMF_AMF3_AcknowledgeMessage($request); 67 | $response->body = true; 68 | break; 69 | case SabreAMF_AMF3_CommandMessage::DISCONNECT_OPERATION : 70 | $response = new SabreAMF_AMF3_AcknowledgeMessage($request); 71 | break; 72 | default : 73 | throw new Exception('Unsupported CommandMessage operation: ' . $request->operation); 74 | 75 | } 76 | return $response; 77 | 78 | } 79 | 80 | /** 81 | * authenticate 82 | * 83 | * @param string $username 84 | * @param string $password 85 | * @return void 86 | */ 87 | protected function authenticate($username,$password) { 88 | 89 | if (is_callable($this->onAuthenticate)) { 90 | call_user_func($this->onAuthenticate,$username,$password); 91 | } 92 | 93 | } 94 | 95 | /** 96 | * invokeService 97 | * 98 | * @param string $service 99 | * @param string $method 100 | * @param array $data 101 | * @return mixed 102 | */ 103 | protected function invokeService($service,$method,$data) { 104 | 105 | if (is_callable($this->onInvokeService)) { 106 | return call_user_func_array($this->onInvokeService,array($service,$method,$data)); 107 | } else { 108 | throw new Exception('onInvokeService is not defined or not callable'); 109 | } 110 | 111 | } 112 | 113 | 114 | /** 115 | * exec 116 | * 117 | * @return void 118 | */ 119 | public function exec() { 120 | 121 | // First we'll be looping through the headers to see if there's anything we reconize 122 | 123 | foreach($this->getRequestHeaders() as $header) { 124 | 125 | switch($header['name']) { 126 | 127 | // We found a credentials headers, calling the authenticate method 128 | case 'Credentials' : 129 | $this->authenticate($header['data']['userid'],$header['data']['password']); 130 | break; 131 | 132 | } 133 | 134 | } 135 | 136 | foreach($this->getRequests() as $request) { 137 | 138 | // Default AMFVersion 139 | $AMFVersion = 0; 140 | 141 | $response = null; 142 | 143 | try { 144 | 145 | if (is_array($request['data']) && isset($request['data'][0]) && $request['data'][0] instanceof SabreAMF_AMF3_AbstractMessage) { 146 | $request['data'] = $request['data'][0]; 147 | } 148 | 149 | // See if we are dealing with the AMF3 messaging system 150 | if (is_object($request['data']) && $request['data'] instanceof SabreAMF_AMF3_AbstractMessage) { 151 | 152 | $AMFVersion = 3; 153 | 154 | // See if we are dealing with a CommandMessage 155 | if ($request['data'] instanceof SabreAMF_AMF3_CommandMessage) { 156 | 157 | // Handle the command message 158 | $response = $this->handleCommandMessage($request['data']); 159 | } 160 | 161 | // Is this maybe a RemotingMessage ? 162 | if ($request['data'] instanceof SabreAMF_AMF3_RemotingMessage) { 163 | 164 | // Yes 165 | $response = new SabreAMF_AMF3_AcknowledgeMessage($request['data']); 166 | $response->body = $this->invokeService($request['data']->source,$request['data']->operation,$request['data']->body); 167 | 168 | } 169 | 170 | } else { 171 | 172 | // We are dealing with AMF0 173 | $service = substr($request['target'],0,strrpos($request['target'],'.')); 174 | $method = substr(strrchr($request['target'],'.'),1); 175 | 176 | $response = $this->invokeService($service,$method,$request['data']); 177 | 178 | } 179 | 180 | $status = SabreAMF_Const::R_RESULT; 181 | 182 | } catch (Exception $e) { 183 | 184 | // We got an exception somewhere, ignore anything that has happened and send back 185 | // exception information 186 | 187 | if ($e instanceof SabreAMF_DetailException) { 188 | $detail = $e->getDetail(); 189 | } else { 190 | $detail = ''; 191 | } 192 | 193 | switch($AMFVersion) { 194 | case SabreAMF_Const::AMF0 : 195 | $response = array( 196 | 'description' => $e->getMessage(), 197 | 'detail' => $detail, 198 | 'line' => $e->getLine(), 199 | 'code' => $e->getCode()?$e->getCode():get_class($e), 200 | ); 201 | break; 202 | case SabreAMF_Const::AMF3 : 203 | $response = new SabreAMF_AMF3_ErrorMessage($request['data']); 204 | $response->faultString = $e->getMessage(); 205 | $response->faultCode = $e->getCode(); 206 | $response->faultDetail = $detail; 207 | break; 208 | 209 | } 210 | $status = SabreAMF_Const::R_STATUS; 211 | } 212 | 213 | $this->setResponse($request['response'],$status,$response); 214 | 215 | } 216 | $this->sendResponse(); 217 | 218 | } 219 | 220 | } 221 | 222 | 223 | -------------------------------------------------------------------------------- /SabreAMF/ChangeLog: -------------------------------------------------------------------------------- 1 | SabreAMF 1.3 - 2009-06-27 2 | * Fixed: Issue 6: Invalid command operation 12 3 | * Fixed: AMF3 deserialization improvements on 64bit machines and various 4 | other bugs (thanks Asbjorn) 5 | * Fixed: Issue 11: AMF0 array reference bugs fixed 6 | * Fixed: AMF0 classmapping bugs 7 | * Fixed: Bug in recordset object notation 8 | * New: If invalid AMF is received, we send back a more specific 9 | exception (thanks Asbjorn) 10 | * New: Drupal integration module (separate download) (thanks Ionut) 11 | 12 | Older: 13 | * Changelogs to follow, if anyone is interested 14 | -------------------------------------------------------------------------------- /SabreAMF/ClassMapper.php: -------------------------------------------------------------------------------- 1 | 'SabreAMF_AMF3_RemotingMessage', 26 | 'flex.messaging.messages.CommandMessage' => 'SabreAMF_AMF3_CommandMessage', 27 | 'flex.messaging.messages.AcknowledgeMessage' => 'SabreAMF_AMF3_AcknowledgeMessage', 28 | 'flex.messaging.messages.ErrorMessage' => 'SabreAMF_AMF3_ErrorMessage', 29 | 'flex.messaging.io.ArrayCollection' => 'SabreAMF_ArrayCollection' 30 | ); 31 | 32 | /** 33 | * Assign this callback to intercept calls to getLocalClass 34 | * 35 | * @var callback 36 | */ 37 | static public $onGetLocalClass; 38 | 39 | /** 40 | * Assign this callback to intercept calls to getRemoteClass 41 | * 42 | * @var callback 43 | */ 44 | static public $onGetRemoteClass; 45 | 46 | /** 47 | * The Constructor 48 | * 49 | * We make the constructor private so the class cannot be initialized 50 | * 51 | * @return void 52 | */ 53 | private function __construct() { } 54 | 55 | /** 56 | * Register a new class to be mapped 57 | * 58 | * @param string $remoteClass 59 | * @param string $localClass 60 | * @return void 61 | */ 62 | static public function registerClass($remoteClass,$localClass) { 63 | 64 | self::$maps[$remoteClass] = $localClass; 65 | 66 | } 67 | 68 | /** 69 | * Get the local classname for a remote class 70 | * 71 | * This method will return FALSE when the class is not found 72 | * 73 | * @param string $remoteClass 74 | * @return mixed 75 | */ 76 | static public function getLocalClass($remoteClass) { 77 | 78 | $localClass = false; 79 | $cb = false; 80 | $localClass=(isset(self::$maps[$remoteClass]))?self::$maps[$remoteClass]:false; 81 | if (!$localClass && is_callable(self::$onGetLocalClass)) { 82 | $cb = true; 83 | $localClass = call_user_func(self::$onGetLocalClass,$remoteClass); 84 | } 85 | if (!$localClass) return false; 86 | if (!is_string($localClass) && $cb) { 87 | throw new Exception('Classname received from onGetLocalClass should be a string or return false. ' . gettype($localClass) . ' was returned'); 88 | } 89 | if (!class_exists($localClass)) { 90 | throw new Exception('Class ' . $localClass . ' is not defined'); 91 | } 92 | return $localClass; 93 | 94 | } 95 | 96 | /** 97 | * Get the remote classname for a local class 98 | * 99 | * This method will return FALSE when the class is not found 100 | * 101 | * @param string $localClass 102 | * @return mixed 103 | */ 104 | static public function getRemoteClass($localClass) { 105 | 106 | $remoteClass = false; 107 | $cb = false; 108 | $remoteClass = array_search($localClass,self::$maps); 109 | if (!$remoteClass && is_callable(self::$onGetRemoteClass)) { 110 | $cb = true; 111 | $remoteClass = call_user_func(self::$onGetRemoteClass,$localClass); 112 | } 113 | if (!$remoteClass) return false; 114 | if (!is_string($remoteClass) && $cb) { 115 | throw new Exception('Classname received from onGetRemoteClass should be a string or return false. ' . gettype($remoteClass) . ' was returned'); 116 | } 117 | return $remoteClass; 118 | 119 | } 120 | 121 | } 122 | 123 | 124 | -------------------------------------------------------------------------------- /SabreAMF/ClassNotFoundException.php: -------------------------------------------------------------------------------- 1 | message = "Could not locate class " . $classname; 32 | $this->code = "Server.Processing"; 33 | 34 | // Call parent class constructor 35 | parent::__construct( $this->message ); 36 | } 37 | 38 | public function getDetail() { 39 | 40 | return "Please check that the given servicename is correct and that the class exists."; 41 | 42 | } 43 | 44 | } 45 | 46 | ?> 47 | -------------------------------------------------------------------------------- /SabreAMF/Client.php: -------------------------------------------------------------------------------- 1 | endPoint = $endPoint; 84 | 85 | $this->amfRequest = new SabreAMF_Message(); 86 | $this->amfOutputStream = new SabreAMF_OutputStream(); 87 | 88 | } 89 | 90 | 91 | /** 92 | * sendRequest 93 | * 94 | * sendRequest sends the request to the server. It expects the servicepath and methodname, and the parameters of the methodcall 95 | * 96 | * @param string $servicePath The servicepath (e.g.: myservice.mymethod) 97 | * @param array $data The parameters you want to send 98 | * @return mixed 99 | */ 100 | public function sendRequest($servicePath,$data) { 101 | 102 | // We're using the FLEX Messaging framework 103 | if($this->encoding & SabreAMF_Const::FLEXMSG) { 104 | 105 | 106 | // Setting up the message 107 | $message = new SabreAMF_AMF3_RemotingMessage(); 108 | $message->body = $data; 109 | 110 | // We need to split serviceName.methodName into separate variables 111 | $service = explode('.',$servicePath); 112 | $method = array_pop($service); 113 | $service = implode('.',$service); 114 | $message->operation = $method; 115 | $message->source = $service; 116 | 117 | $data = $message; 118 | } 119 | 120 | $this->amfRequest->addBody(array( 121 | 122 | // If we're using the flex messaging framework, target is specified as the string 'null' 123 | 'target' => $this->encoding & SabreAMF_Const::FLEXMSG?'null':$servicePath, 124 | 'response' => '/1', 125 | 'data' => $data 126 | )); 127 | 128 | $this->amfRequest->serialize($this->amfOutputStream); 129 | 130 | // The curl request 131 | $ch = curl_init($this->endPoint); 132 | curl_setopt($ch,CURLOPT_POST,1); 133 | curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1); 134 | curl_setopt($ch,CURLOPT_TIMEOUT,20); 135 | curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-type: ' . SabreAMF_Const::MIMETYPE)); 136 | curl_setopt($ch,CURLOPT_POSTFIELDS,$this->amfOutputStream->getRawData()); 137 | if ($this->httpProxy) { 138 | curl_setopt($ch,CURLOPT_PROXY,$this->httpProxy); 139 | } 140 | $result = curl_exec($ch); 141 | 142 | if (curl_errno($ch)) { 143 | throw new Exception('CURL error: ' . curl_error($ch)); 144 | false; 145 | } else { 146 | curl_close($ch); 147 | } 148 | 149 | $this->amfInputStream = new SabreAMF_InputStream($result); 150 | $this->amfResponse = new SabreAMF_Message(); 151 | $this->amfResponse->deserialize($this->amfInputStream); 152 | 153 | $this->parseHeaders(); 154 | 155 | foreach($this->amfResponse->getBodies() as $body) { 156 | 157 | if (strpos($body['target'],'/1')===0) return $body['data'] ; 158 | 159 | } 160 | 161 | } 162 | 163 | /** 164 | * addHeader 165 | * 166 | * Add a header to the client request 167 | * 168 | * @param string $name 169 | * @param bool $required 170 | * @param mixed $data 171 | * @return void 172 | */ 173 | public function addHeader($name,$required,$data) { 174 | 175 | $this->amfRequest->addHeader(array('name'=>$name,'required'=>$required==true,'data'=>$data)); 176 | 177 | } 178 | 179 | /** 180 | * setCredentials 181 | * 182 | * @param string $username 183 | * @param string $password 184 | * @return void 185 | */ 186 | public function setCredentials($username,$password) { 187 | 188 | $this->addHeader('Credentials',false,(object)array('userid'=>$username,'password'=>$password)); 189 | 190 | } 191 | 192 | /** 193 | * setHttpProxy 194 | * 195 | * @param mixed $httpProxy 196 | * @return void 197 | */ 198 | public function setHttpProxy($httpProxy) { 199 | $this->httpProxy = $httpProxy; 200 | } 201 | 202 | /** 203 | * parseHeaders 204 | * 205 | * @return void 206 | */ 207 | private function parseHeaders() { 208 | 209 | foreach($this->amfResponse->getHeaders() as $header) { 210 | 211 | switch($header['name']) { 212 | 213 | case 'ReplaceGatewayUrl' : 214 | if (is_string($header['data'])) { 215 | $this->endPoint = $header['data']; 216 | } 217 | break; 218 | 219 | } 220 | 221 | 222 | } 223 | 224 | } 225 | 226 | /** 227 | * Change the AMF encoding (0 or 3) 228 | * 229 | * @param int $encoding 230 | * @return void 231 | */ 232 | public function setEncoding($encoding) { 233 | 234 | $this->encoding = $encoding; 235 | $this->amfRequest->setEncoding($encoding & SabreAMF_Const::AMF3); 236 | 237 | } 238 | 239 | } 240 | 241 | 242 | 243 | -------------------------------------------------------------------------------- /SabreAMF/Const.php: -------------------------------------------------------------------------------- 1 | stream = $stream; 40 | 41 | } 42 | 43 | /** 44 | * readAMFData 45 | * 46 | * Starts reading an AMF block from the stream 47 | * 48 | * @param mixed $settype 49 | * @return mixed 50 | */ 51 | public abstract function readAMFData($settype = null); 52 | 53 | 54 | /** 55 | * getLocalClassName 56 | * 57 | * @param string $remoteClass 58 | * @return mixed 59 | */ 60 | protected function getLocalClassName($remoteClass) { 61 | 62 | return SabreAMF_ClassMapper::getLocalClass($remoteClass); 63 | 64 | } 65 | 66 | } 67 | 68 | 69 | -------------------------------------------------------------------------------- /SabreAMF/DetailException.php: -------------------------------------------------------------------------------- 1 | rawData = $data; 44 | 45 | } 46 | 47 | /** 48 | * &readBuffer 49 | * 50 | * @param int $length 51 | * @return mixed 52 | */ 53 | public function &readBuffer($length) { 54 | 55 | if ($length+$this->cursor > strlen($this->rawData)) { 56 | throw new Exception('Buffer underrun at position: '. $this->cursor . '. Trying to fetch '. $length . ' bytes'); 57 | return false; 58 | } 59 | $data = substr($this->rawData,$this->cursor,$length); 60 | $this->cursor+=$length; 61 | return $data; 62 | 63 | } 64 | 65 | /** 66 | * readByte 67 | * 68 | * @return int 69 | */ 70 | public function readByte() { 71 | 72 | return ord($this->readBuffer(1)); 73 | 74 | } 75 | 76 | /** 77 | * readInt 78 | * 79 | * @return int 80 | */ 81 | public function readInt() { 82 | 83 | $block = $this->readBuffer(2); 84 | $int = unpack("n",$block); 85 | return $int[1]; 86 | 87 | } 88 | 89 | 90 | /** 91 | * readDouble 92 | * 93 | * @return float 94 | */ 95 | public function readDouble() { 96 | 97 | $double = $this->readBuffer(8); 98 | 99 | $testEndian = unpack("C*",pack("S*",256)); 100 | $bigEndian = !$testEndian[1]==1; 101 | 102 | if ($bigEndian) $double = strrev($double); 103 | $double = unpack("d",$double); 104 | return $double[1]; 105 | } 106 | 107 | /** 108 | * readLong 109 | * 110 | * @return int 111 | */ 112 | public function readLong() { 113 | 114 | $block = $this->readBuffer(4); 115 | $long = unpack("N",$block); 116 | return $long[1]; 117 | } 118 | 119 | /** 120 | * readInt24 121 | * 122 | * return int 123 | */ 124 | public function readInt24() { 125 | 126 | $block = chr(0) . $this->readBuffer(3); 127 | $long = unpack("N",$block); 128 | return $long[1]; 129 | 130 | } 131 | 132 | } 133 | 134 | 135 | 136 | -------------------------------------------------------------------------------- /SabreAMF/InvalidAMFException.php: -------------------------------------------------------------------------------- 1 | 10 | * @licence http://www.freebsd.org/copyright/license.html BSD License (4 Clause) 11 | */ 12 | 13 | /** 14 | * Detailed exception 15 | */ 16 | require_once dirname(__FILE__) . '/DetailException.php'; 17 | 18 | /** 19 | * In valid AMF exception 20 | * 21 | * @uses SabreAMF_DetailException 22 | */ 23 | class SabreAMF_InvalidAMFException extends Exception implements SabreAMF_DetailException { 24 | 25 | /** 26 | * Constructor 27 | */ 28 | public function __construct() { 29 | // Specific message to ClassException 30 | $this->message = "No valid AMF request received"; 31 | $this->code = "Server.Processing"; 32 | 33 | // Call parent class constructor 34 | parent::__construct( $this->message ); 35 | } 36 | 37 | public function getDetail() { 38 | 39 | return "Please check that you are calling this page with Flash and AMF."; 40 | 41 | } 42 | 43 | } 44 | 45 | ?> 46 | -------------------------------------------------------------------------------- /SabreAMF/LICENCE: -------------------------------------------------------------------------------- 1 | Copyright (C) 2006-2009 Rooftop Solutions. 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, 5 | are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, 8 | this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright notice, 10 | this list of conditions and the following disclaimer in the documentation 11 | and/or other materials provided with the distribution. 12 | * Neither the name of the SabreAMF nor the names of its contributors 13 | may be used to endorse or promote products derived from this software 14 | without specific prior written permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 20 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 | POSSIBILITY OF SUCH DAMAGE. 27 | -------------------------------------------------------------------------------- /SabreAMF/Message.php: -------------------------------------------------------------------------------- 1 | outputStream = $stream; 61 | $stream->writeByte(0x00); 62 | $stream->writeByte($this->encoding); 63 | $stream->writeInt(count($this->headers)); 64 | 65 | foreach($this->headers as $header) { 66 | 67 | $serializer = new SabreAMF_AMF0_Serializer($stream); 68 | $serializer->writeString($header['name']); 69 | $stream->writeByte($header['required']==true); 70 | $stream->writeLong(-1); 71 | $serializer->writeAMFData($header['data']); 72 | } 73 | 74 | $stream->writeInt(count($this->bodies)); 75 | 76 | 77 | foreach($this->bodies as $body) { 78 | $serializer = new SabreAMF_AMF0_Serializer($stream); 79 | $serializer->writeString($body['target']); 80 | $serializer->writeString($body['response']); 81 | $stream->writeLong(-1); 82 | 83 | switch($this->encoding) { 84 | 85 | case SabreAMF_Const::AMF0 : 86 | $serializer->writeAMFData($body['data']); 87 | break; 88 | case SabreAMF_Const::AMF3 : 89 | $serializer->writeAMFData(new SabreAMF_AMF3_Wrapper($body['data'])); 90 | break; 91 | 92 | } 93 | 94 | } 95 | 96 | } 97 | 98 | /** 99 | * deserialize 100 | * 101 | * This method deserializes a request. It requires an SabreAMF_InputStream with valid AMF data. After 102 | * deserialization the contents of the request can be found through the getBodies and getHeaders methods 103 | * 104 | * @param SabreAMF_InputStream $stream 105 | * @return void 106 | */ 107 | public function deserialize(SabreAMF_InputStream $stream) { 108 | 109 | $this->headers = array(); 110 | $this->bodies = array(); 111 | 112 | $this->InputStream = $stream; 113 | 114 | $stream->readByte(); 115 | 116 | $this->clientType = $stream->readByte(); 117 | 118 | $deserializer = new SabreAMF_AMF0_Deserializer($stream); 119 | 120 | $totalHeaders = $stream->readInt(); 121 | 122 | for($i=0;$i<$totalHeaders;$i++) { 123 | 124 | $header = array( 125 | 'name' => $deserializer->readString(), 126 | 'required' => $stream->readByte()==true 127 | ); 128 | $stream->readLong(); 129 | $header['data'] = $deserializer->readAMFData(null,true); 130 | $this->headers[] = $header; 131 | 132 | } 133 | 134 | $totalBodies = $stream->readInt(); 135 | 136 | for($i=0;$i<$totalBodies;$i++) { 137 | 138 | try { 139 | $target = $deserializer->readString(); 140 | } catch (Exception $e) { 141 | // Could not fetch next body.. this happens with some versions of AMFPHP where the body 142 | // count isn't properly set. If this happens we simply stop decoding 143 | break; 144 | } 145 | 146 | $body = array( 147 | 'target' => $target, 148 | 'response' => $deserializer->readString(), 149 | 'length' => $stream->readLong(), 150 | 'data' => $deserializer->readAMFData(null,true) 151 | ); 152 | 153 | if (is_object($body['data']) && $body['data'] instanceof SabreAMF_AMF3_Wrapper) { 154 | $body['data'] = $body['data']->getData(); 155 | $this->encoding = SabreAMF_Const::AMF3; 156 | } else if (is_array($body['data']) && isset($body['data'][0]) && is_object($body['data'][0]) && $body['data'][0] instanceof SabreAMF_AMF3_Wrapper) { 157 | $body['data'] = $body['data'][0]->getData(); 158 | $this->encoding = SabreAMF_Const::AMF3; 159 | } 160 | 161 | $this->bodies[] = $body; 162 | 163 | } 164 | 165 | 166 | } 167 | 168 | /** 169 | * getClientType 170 | * 171 | * Returns the ClientType for the request. Check SabreAMF_Const for possible (known) values 172 | * 173 | * @return int 174 | */ 175 | public function getClientType() { 176 | 177 | return $this->clientType; 178 | 179 | } 180 | 181 | /** 182 | * getBodies 183 | * 184 | * Returns the bodies int the message 185 | * 186 | * @return array 187 | */ 188 | public function getBodies() { 189 | 190 | return $this->bodies; 191 | 192 | } 193 | 194 | /** 195 | * getHeaders 196 | * 197 | * Returns the headers in the message 198 | * 199 | * @return array 200 | */ 201 | public function getHeaders() { 202 | 203 | return $this->headers; 204 | 205 | } 206 | 207 | /** 208 | * addBody 209 | * 210 | * Adds a body to the message 211 | * 212 | * @param mixed $body 213 | * @return void 214 | */ 215 | public function addBody($body) { 216 | 217 | $this->bodies[] = $body; 218 | 219 | } 220 | 221 | /** 222 | * addHeader 223 | * 224 | * Adds a message header 225 | * 226 | * @param mixed $header 227 | * @return void 228 | */ 229 | public function addHeader($header) { 230 | 231 | $this->headers[] = $header; 232 | 233 | } 234 | 235 | /** 236 | * setEncoding 237 | * 238 | * @param int $encoding 239 | * @return void 240 | */ 241 | public function setEncoding($encoding) { 242 | 243 | $this->encoding = $encoding; 244 | 245 | } 246 | 247 | /** 248 | * getEncoding 249 | * 250 | * @return int 251 | */ 252 | public function getEncoding() { 253 | 254 | return $this->encoding; 255 | 256 | } 257 | 258 | } 259 | 260 | 261 | -------------------------------------------------------------------------------- /SabreAMF/OutputStream.php: -------------------------------------------------------------------------------- 1 | rawData.=$str; 31 | } 32 | 33 | /** 34 | * writeByte 35 | * 36 | * @param int $byte 37 | * @return void 38 | */ 39 | public function writeByte($byte) { 40 | 41 | $this->rawData.=pack('c',$byte); 42 | 43 | } 44 | 45 | /** 46 | * writeInt 47 | * 48 | * @param int $int 49 | * @return void 50 | */ 51 | public function writeInt($int) { 52 | 53 | $this->rawData.=pack('n',$int); 54 | 55 | } 56 | 57 | /** 58 | * writeDouble 59 | * 60 | * @param float $double 61 | * @return void 62 | */ 63 | public function writeDouble($double) { 64 | 65 | $bin = pack("d",$double); 66 | $testEndian = unpack("C*",pack("S*",256)); 67 | $bigEndian = !$testEndian[1]==1; 68 | if ($bigEndian) $bin = strrev($bin); 69 | $this->rawData.=$bin; 70 | 71 | } 72 | 73 | /** 74 | * writeLong 75 | * 76 | * @param int $long 77 | * @return void 78 | */ 79 | public function writeLong($long) { 80 | 81 | $this->rawData.=pack("N",$long); 82 | 83 | 84 | } 85 | 86 | /** 87 | * getRawData 88 | * 89 | * @return string 90 | */ 91 | public function getRawData() { 92 | 93 | return $this->rawData; 94 | 95 | } 96 | 97 | 98 | } 99 | 100 | 101 | 102 | -------------------------------------------------------------------------------- /SabreAMF/README: -------------------------------------------------------------------------------- 1 | SabreAMF v1.3 2 | 3 | Copyright (C) 2006-2009 Rooftop Solutions. All rights reserved. 4 | 5 | Documentation, bug reports, downloads: 6 | http://code.google.com/p/sabreamf/ 7 | 8 | SabreAMF is distributed under a BSD licence. Please check the LICENCE file for more information. 9 | 10 | Main Author: 11 | Evert Pot (http://www.rooftopsolutions.nl) 12 | 13 | Bugfixes, suggestions, improvements: 14 | Karl von Randow (http://xk72.com/blog) 15 | Renaun Erickson (http://renaun.com/blog) 16 | Asbjørn Sloth Tønnesen (http://lila.io/) 17 | 18 | Drupal integration: 19 | Ionut Stoica 20 | 21 | Thanks to: 22 | Kevin Langdon, author of ServiceCapture for making the first big steps in decoding the AMF3 spec and helping out when i got stuck 23 | The AMFPHP team for their excellent piece of software. 24 | The OSFlash community for opening up the Flash Platform. 25 | PyAMF project 26 | -------------------------------------------------------------------------------- /SabreAMF/RecordSet.php: -------------------------------------------------------------------------------- 1 | (object)array( 53 | 'totalCount' => $this->count(), 54 | 'initialData' => $this->getData(), 55 | 'cursor' => 1, 56 | 'serviceName' => false, 57 | 'columnNames' => $this->getColumnNames(), 58 | 'version' => 1, 59 | 'id' => false, 60 | ) 61 | ); 62 | 63 | 64 | } 65 | 66 | } 67 | 68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /SabreAMF/Serializer.php: -------------------------------------------------------------------------------- 1 | stream = $stream; 40 | 41 | } 42 | 43 | /** 44 | * writeAMFData 45 | * 46 | * @param mixed $data 47 | * @param int $forcetype 48 | * @return mixed 49 | */ 50 | public abstract function writeAMFData($data,$forcetype=null); 51 | 52 | /** 53 | * getStream 54 | * 55 | * @return SabreAMF_OutputStream 56 | */ 57 | public function getStream() { 58 | 59 | return $this->stream; 60 | 61 | } 62 | 63 | /** 64 | * getRemoteClassName 65 | * 66 | * @param string $localClass 67 | * @return mixed 68 | */ 69 | protected function getRemoteClassName($localClass) { 70 | 71 | return SabreAMF_ClassMapper::getRemoteClass($localClass); 72 | 73 | } 74 | 75 | } 76 | 77 | 78 | -------------------------------------------------------------------------------- /SabreAMF/Server.php: -------------------------------------------------------------------------------- 1 | readInput(); 77 | 78 | //file_put_contents($dump.'/' . md5($data),$data); 79 | 80 | $this->amfInputStream = new SabreAMF_InputStream($data); 81 | 82 | $this->amfRequest = new SabreAMF_Message(); 83 | $this->amfOutputStream = new SabreAMF_OutputStream(); 84 | $this->amfResponse = new SabreAMF_Message(); 85 | 86 | $this->amfRequest->deserialize($this->amfInputStream); 87 | 88 | } 89 | 90 | /** 91 | * getRequests 92 | * 93 | * Returns the requests that are made to the gateway. 94 | * 95 | * @return array 96 | */ 97 | public function getRequests() { 98 | 99 | return $this->amfRequest->getBodies(); 100 | 101 | } 102 | 103 | /** 104 | * setResponse 105 | * 106 | * Send a response back to the client (based on a request you got through getRequests) 107 | * 108 | * @param string $target This parameter should contain the same as the 'response' item you got through getRequests. This connects the request to the response 109 | * @param int $responsetype Set as either SabreAMF_Const::R_RESULT or SabreAMF_Const::R_STATUS, depending on if the call succeeded or an error was produced 110 | * @param mixed $data The result data 111 | * @return void 112 | */ 113 | public function setResponse($target,$responsetype,$data) { 114 | 115 | 116 | switch($responsetype) { 117 | 118 | case SabreAMF_Const::R_RESULT : 119 | $target = $target.='/onResult'; 120 | break; 121 | case SabreAMF_Const::R_STATUS : 122 | $target = $target.='/onStatus'; 123 | break; 124 | case SabreAMF_Const::R_DEBUG : 125 | $target = '/onDebugEvents'; 126 | break; 127 | } 128 | return $this->amfResponse->addBody(array('target'=>$target,'response'=>'','data'=>$data)); 129 | 130 | } 131 | 132 | /** 133 | * sendResponse 134 | * 135 | * Sends the responses back to the client. Call this after you answered all the requests with setResponse 136 | * 137 | * @return void 138 | */ 139 | public function sendResponse() { 140 | 141 | header('Content-Type: ' . SabreAMF_Const::MIMETYPE); 142 | $this->amfResponse->setEncoding($this->amfRequest->getEncoding()); 143 | $this->amfResponse->serialize($this->amfOutputStream); 144 | echo($this->amfOutputStream->getRawData()); 145 | 146 | } 147 | 148 | /** 149 | * addHeader 150 | * 151 | * Add a header to the server response 152 | * 153 | * @param string $name 154 | * @param bool $required 155 | * @param mixed $data 156 | * @return void 157 | */ 158 | public function addHeader($name,$required,$data) { 159 | 160 | $this->amfResponse->addHeader(array('name'=>$name,'required'=>$required==true,'data'=>$data)); 161 | 162 | } 163 | 164 | /** 165 | * getRequestHeaders 166 | * 167 | * returns the request headers 168 | * 169 | * @return void 170 | */ 171 | public function getRequestHeaders() { 172 | 173 | return $this->amfRequest->getHeaders(); 174 | 175 | } 176 | 177 | /** 178 | * setInputFile 179 | * 180 | * returns the true/false depended on wheater the stream is readable 181 | * 182 | * @param string $stream New input stream 183 | * 184 | * @author Asbjørn Sloth Tønnesen 185 | * @return bool 186 | */ 187 | static public function setInputFile($stream) { 188 | 189 | if (!is_readable($stream)) return false; 190 | 191 | self::$dataInputStream = $stream; 192 | return true; 193 | 194 | } 195 | 196 | /** 197 | * setInputString 198 | * 199 | * Returns the true/false depended on wheater the string was accepted. 200 | * That a string is accepted by this method, does NOT mean that it is a valid AMF request. 201 | * 202 | * @param string $string New input string 203 | * 204 | * @author Asbjørn Sloth Tønnesen 205 | * @return bool 206 | */ 207 | static public function setInputString($string) { 208 | 209 | if (!(is_string($string) && strlen($string) > 0)) 210 | throw new SabreAMF_InvalidAMFException(); 211 | 212 | self::$dataInputStream = null; 213 | self::$dataInputData = $string; 214 | return true; 215 | 216 | } 217 | 218 | /** 219 | * readInput 220 | * 221 | * Reads the input from stdin unless it has been overwritten 222 | * with setInputFile or setInputString. 223 | * 224 | * @author Asbjørn Sloth Tønnesen 225 | * @return string Binary string containing the AMF data 226 | */ 227 | protected function readInput() { 228 | 229 | if (is_null(self::$dataInputStream)) return self::$dataInputData; 230 | 231 | $data = file_get_contents(self::$dataInputStream); 232 | if (!$data) throw new SabreAMF_InvalidAMFException(); 233 | 234 | return $data; 235 | 236 | } 237 | 238 | } 239 | 240 | 241 | -------------------------------------------------------------------------------- /SabreAMF/TypedObject.php: -------------------------------------------------------------------------------- 1 | setAMFClassName($classname); 23 | $this->setAMFData($data); 24 | 25 | } 26 | 27 | /** 28 | * getAMFClassName 29 | * 30 | * @return string 31 | */ 32 | public function getAMFClassName() { 33 | 34 | return $this->amfClassName; 35 | 36 | } 37 | 38 | /** 39 | * getAMFData 40 | * 41 | * @return mixed 42 | */ 43 | public function getAMFData() { 44 | 45 | return $this->amfData; 46 | 47 | } 48 | 49 | /** 50 | * setAMFClassName 51 | * 52 | * @param string $classname 53 | * @return void 54 | */ 55 | public function setAMFClassName($classname) { 56 | 57 | $this->amfClassName = $classname; 58 | 59 | } 60 | 61 | /** 62 | * setAMFData 63 | * 64 | * @param mixed $data 65 | * @return void 66 | */ 67 | public function setAMFData($data) { 68 | 69 | $this->amfData = $data; 70 | 71 | } 72 | 73 | } 74 | 75 | 76 | -------------------------------------------------------------------------------- /SabreAMF/UndefinedMethodException.php: -------------------------------------------------------------------------------- 1 | message = "Undefined method '$method' in class $class"; 32 | $this->code = "Server.Processing"; 33 | 34 | // Call parent class constructor 35 | parent::__construct( $this->message ); 36 | 37 | } 38 | 39 | public function getDetail() { 40 | 41 | return "Check to ensure that the method is defined, and that it is spelled correctly."; 42 | 43 | } 44 | 45 | 46 | } 47 | 48 | ?> 49 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "qwantix/php-rtmp-client", 3 | "type": "library", 4 | "description": "A Rtmp client for PHP", 5 | "authors": [ 6 | { 7 | "name": "Brice Dauzats", 8 | "email": "qwantix@gmail.com" 9 | } 10 | ], 11 | "require": { 12 | "php": ">=5.0.0" 13 | }, 14 | "license": "GPL-v3" 15 | } -------------------------------------------------------------------------------- /debug.php: -------------------------------------------------------------------------------- 1 | connect("localhost","myApp"); 6 | $result = $client->call("myMethod"); 7 | var_dump($result); 8 | 9 | --------------------------------------------------------------------------------