├── ATOM.php ├── COPYING ├── Feed.php ├── Item.php ├── README.md ├── RSS1.php ├── RSS2.php ├── composer.json └── examples ├── example_atom.php ├── example_minimum.php ├── example_rss1.php └── example_rss2.php /ATOM.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * This file is part of the "Universal Feed Writer" project. 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU General Public License 20 | * along with this program. If not, see . 21 | */ 22 | 23 | /** 24 | * Wrapper for creating ATOM feeds 25 | * 26 | * @package UniversalFeedWriter 27 | */ 28 | class ATOM extends Feed 29 | { 30 | /** 31 | * {@inheritdoc} 32 | */ 33 | public function __construct() 34 | { 35 | parent::__construct(Feed::ATOM); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 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 | 635 | Copyright (C) 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 . 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 | Copyright (C) 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 | . 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 | . 675 | -------------------------------------------------------------------------------- /Feed.php: -------------------------------------------------------------------------------- 1 | 8 | * Copyright (C) 2010-2015 Michael Bemmerl 9 | * 10 | * This file is part of the "Universal Feed Writer" project. 11 | * 12 | * This program is free software: you can redistribute it and/or modify 13 | * it under the terms of the GNU General Public License as published by 14 | * the Free Software Foundation, either version 3 of the License, or 15 | * (at your option) any later version. 16 | * 17 | * This program is distributed in the hope that it will be useful, 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 | * GNU General Public License for more details. 21 | * 22 | * You should have received a copy of the GNU General Public License 23 | * along with this program. If not, see . 24 | */ 25 | 26 | // RSS 0.90 Officially obsoleted by 1.0 27 | // RSS 0.91, 0.92, 0.93 and 0.94 Officially obsoleted by 2.0 28 | // So, define constants for RSS 1.0, RSS 2.0 and ATOM 29 | 30 | /** 31 | * Universal Feed Writer class 32 | * 33 | * Generate RSS 1.0, RSS2.0 and ATOM Feeds 34 | * 35 | * @package UniversalFeedWriter 36 | * @author Anis uddin Ahmad 37 | * @link http://www.ajaxray.com/projects/rss 38 | */ 39 | abstract class Feed 40 | { 41 | const RSS1 = 'RSS 1.0'; 42 | const RSS2 = 'RSS 2.0'; 43 | const ATOM = 'ATOM'; 44 | 45 | /** 46 | * Collection of all channel elements 47 | */ 48 | private $channels = array(); 49 | 50 | /** 51 | * Collection of items as object of \FeedWriter\Item class. 52 | */ 53 | private $items = array(); 54 | 55 | /** 56 | * Store some other version wise data 57 | */ 58 | private $data = array(); 59 | 60 | /** 61 | * The tag names which have to encoded as CDATA 62 | */ 63 | private $CDATAEncoding = array(); 64 | 65 | /** 66 | * Collection of XML namespaces 67 | */ 68 | private $namespaces = array(); 69 | 70 | /** 71 | * Contains the format of this feed. 72 | */ 73 | private $version = null; 74 | 75 | /** 76 | * Constructor 77 | * 78 | * If no version is given, a feed in RSS 2.0 format will be generated. 79 | * 80 | * @param constant the version constant (RSS1/RSS2/ATOM). 81 | */ 82 | protected function __construct($version = Feed::RSS2) 83 | { 84 | $this->version = $version; 85 | 86 | // Setting default encoding 87 | $this->encoding = 'utf-8'; 88 | 89 | // Setting default value for essential channel elements 90 | $this->channels['title'] = $version . ' Feed'; 91 | $this->channels['link'] = 'http://www.ajaxray.com/blog'; 92 | 93 | // Add some default XML namespaces 94 | $this->namespaces['content'] = 'http://purl.org/rss/1.0/modules/content/'; 95 | $this->namespaces['wfw'] = 'http://wellformedweb.org/CommentAPI/'; 96 | $this->namespaces['atom'] = 'http://www.w3.org/2005/Atom'; 97 | $this->namespaces['rdf'] = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; 98 | $this->namespaces['rss1'] = 'http://purl.org/rss/1.0/'; 99 | $this->namespaces['dc'] = 'http://purl.org/dc/elements/1.1/'; 100 | $this->namespaces['sy'] = 'http://purl.org/rss/1.0/modules/syndication/'; 101 | 102 | // Tag names to encode in CDATA 103 | $this->addCDATAEncoding(array('description', 'content:encoded', 'summary')); 104 | } 105 | 106 | // Start # public functions --------------------------------------------- 107 | 108 | /** 109 | * Set the URLs for feed pagination. 110 | * 111 | * See RFC 5005, chapter 3. At least one page URL must be specified. 112 | * 113 | * @param string The URL to the next page of this feed. Optional. 114 | * @param string The URL to the previous page of this feed. Optional. 115 | * @param string The URL to the first page of this feed. Optional. 116 | * @param string The URL to the last page of this feed. Optional. 117 | * @link http://tools.ietf.org/html/rfc5005#section-3 118 | * @return self 119 | */ 120 | public function setPagination($nextURL = null, $previousURL = null, $firstURL = null, $lastURL = null) 121 | { 122 | if (empty($nextURL) && empty($previousURL) && empty($firstURL) && empty($lastURL)) 123 | die('At least one URL must be specified for pagination to work.'); 124 | 125 | if (!empty($nextURL)) 126 | $this->setAtomLink($nextURL, 'next'); 127 | 128 | if (!empty($previousURL)) 129 | $this->setAtomLink($previousURL, 'previous'); 130 | 131 | if (!empty($firstURL)) 132 | $this->setAtomLink($firstURL, 'first'); 133 | 134 | if (!empty($lastURL)) 135 | $this->setAtomLink($lastURL, 'last'); 136 | 137 | return $this; 138 | } 139 | 140 | /** 141 | * Add a channel element indicating the program used to generate the feed. 142 | * 143 | * @return self 144 | */ 145 | public function addGenerator() 146 | { 147 | if ($this->version == Feed::ATOM) 148 | $this->setChannelElement('atom:generator', 'FeedWriter', array('uri' => 'https://github.com/mibe/FeedWriter')); 149 | else if ($this->version == Feed::RSS2) 150 | $this->setChannelElement('generator', 'FeedWriter'); 151 | else 152 | die('The generator element is not supported in RSS1 feeds.'); 153 | 154 | return $this; 155 | } 156 | 157 | /** 158 | * Add a XML namespace to the internal list of namespaces. After that, 159 | * custom channel elements can be used properly to generate a valid feed. 160 | * 161 | * @access public 162 | * @param string namespace prefix 163 | * @param string namespace name (URI) 164 | * @return self 165 | * @link http://www.w3.org/TR/REC-xml-names/ 166 | */ 167 | public function addNamespace($prefix, $uri) 168 | { 169 | $this->namespaces[$prefix] = $uri; 170 | 171 | return $this; 172 | } 173 | 174 | /** 175 | * Add a channel element to the feed. 176 | * 177 | * @access public 178 | * @param string name of the channel tag 179 | * @param string content of the channel tag 180 | * @param array array of element attributes with attribute name as array key 181 | * @param bool TRUE if this element can appear multiple times 182 | * @return self 183 | */ 184 | public function setChannelElement($elementName, $content, $attributes = null, $multiple = false) 185 | { 186 | $entity['content'] = $content; 187 | $entity['attributes'] = $attributes; 188 | 189 | if ($multiple === TRUE) 190 | $this->channels[$elementName][] = $entity; 191 | else 192 | $this->channels[$elementName] = $entity; 193 | 194 | return $this; 195 | } 196 | 197 | /** 198 | * Set multiple channel elements from an array. Array elements 199 | * should be 'channelName' => 'channelContent' format. 200 | * 201 | * @access public 202 | * @param array array of channels 203 | * @return self 204 | */ 205 | public function setChannelElementsFromArray($elementArray) 206 | { 207 | if (!is_array($elementArray)) 208 | return; 209 | 210 | foreach ($elementArray as $elementName => $content) { 211 | $this->setChannelElement($elementName, $content); 212 | } 213 | 214 | return $this; 215 | } 216 | 217 | /** 218 | * Get the appropriate MIME type string for the current feed. 219 | * 220 | * @access public 221 | * @return string The MIME type string. 222 | */ 223 | public function getMIMEType() 224 | { 225 | switch ($this->version) { 226 | case Feed::RSS2 : $mimeType = "application/rss+xml"; 227 | break; 228 | case Feed::RSS1 : $mimeType = "application/rdf+xml"; 229 | break; 230 | case Feed::ATOM : $mimeType = "application/atom+xml"; 231 | break; 232 | default : $mimeType = "text/xml"; 233 | } 234 | 235 | return $mimeType; 236 | } 237 | 238 | /** 239 | * Print the actual RSS/ATOM file 240 | * 241 | * Sets a Content-Type header and echoes the contents of the feed. 242 | * Should only be used in situations where direct output is desired; 243 | * if you need to pass a string around, use generateFeed() instead. 244 | * 245 | * @access public 246 | * @param bool FALSE if the specific feed media type should be sent. 247 | * @return void 248 | */ 249 | public function printFeed($useGenericContentType = false) 250 | { 251 | $contentType = "application/rss+xml"; 252 | 253 | if (!$useGenericContentType) { 254 | $contentType = $this->getMIMEType(); 255 | } 256 | 257 | header("Content-Type: " . $contentType . "; charset=" . $this->encoding); 258 | echo $this->generateFeed(); 259 | } 260 | 261 | /** 262 | * Generate the feed. 263 | * 264 | * @access public 265 | * @return string The complete feed XML. 266 | */ 267 | public function generateFeed() 268 | { 269 | return $this->makeHeader() 270 | . $this->makeChannels() 271 | . $this->makeItems() 272 | . $this->makeFooter(); 273 | } 274 | 275 | /** 276 | * Create a new Item. 277 | * 278 | * @access public 279 | * @return Item instance of Item class 280 | */ 281 | public function createNewItem() 282 | { 283 | $Item = new Item($this->version); 284 | 285 | return $Item; 286 | } 287 | 288 | /** 289 | * Add one or more tags to the list of CDATA encoded tags 290 | * 291 | * @access public 292 | * @param array An array of tag names that are merged into the list of tags which should be encoded as CDATA 293 | * @return self 294 | */ 295 | public function addCDATAEncoding(array $tags) 296 | { 297 | $this->CDATAEncoding = array_merge($this->CDATAEncoding, $tags); 298 | 299 | return $this; 300 | } 301 | 302 | /** 303 | * Get list of CDATA encoded properties 304 | * 305 | * @access public 306 | * @return array Return an array of CDATA properties that are to be encoded as CDATA 307 | */ 308 | public function getCDATAEncoding() 309 | { 310 | return $this->CDATAEncoding; 311 | } 312 | 313 | /** 314 | * Remove tags from the list of CDATA encoded tags 315 | * 316 | * @access public 317 | * @param array An array of tag names that should be removed. 318 | * @return void 319 | */ 320 | public function removeCDATAEncoding(array $tags) 321 | { 322 | // Call array_values to re-index the array. 323 | $this->CDATAEncoding = array_values(array_diff($this->CDATAEncoding, $tags)); 324 | } 325 | 326 | /** 327 | * Add a FeedItem to the main class 328 | * 329 | * @access public 330 | * @param Item instance of Item class 331 | * @return self 332 | */ 333 | public function addItem(Item $feedItem) 334 | { 335 | if ($feedItem->getVersion() != $this->version) 336 | die('Feed type mismatch: This instance can handle ' . $this->version . ' feeds only, but item with type ' . $feedItem->getVersion() . ' given.'); 337 | 338 | $this->items[] = $feedItem; 339 | 340 | return $this; 341 | } 342 | 343 | // Wrapper functions ------------------------------------------------------------------- 344 | 345 | /** 346 | * Set the 'encoding' attribute in the XML prolog. 347 | * 348 | * @access public 349 | * @param string value of 'encoding' attribute 350 | * @return self 351 | */ 352 | public function setEncoding($encoding) 353 | { 354 | $this->encoding = $encoding; 355 | 356 | return $this; 357 | } 358 | 359 | /** 360 | * Set the 'title' channel element 361 | * 362 | * @access public 363 | * @param string value of 'title' channel tag 364 | * @return self 365 | */ 366 | public function setTitle($title) 367 | { 368 | return $this->setChannelElement('title', $title); 369 | } 370 | 371 | /** 372 | * Set the date when the ATOM feed was lastly updated. 373 | * 374 | * This adds the 'updated' element to the feed. The value of the date parameter 375 | * can be either an instance of the DateTime class, an integer containing a UNIX 376 | * timestamp or a string which is parseable by PHP's 'strtotime' function. 377 | * 378 | * Not supported in RSS1 feeds. 379 | * 380 | * @access public 381 | * @param DateTime|int|string Date which should be used. 382 | * @return self 383 | */ 384 | public function setDate($date) 385 | { 386 | if ($this->version == Feed::RSS1) 387 | die('The publication date is not supported in RSS1 feeds.'); 388 | 389 | // The feeds have different date formats. 390 | $format = $this->version == Feed::ATOM ? \DATE_ATOM : \DATE_RSS; 391 | 392 | if ($date instanceof DateTime) 393 | $date = $date->format($format); 394 | else if(is_numeric($date) && $date >= 0) 395 | $date = date($format, $date); 396 | else if (is_string($date)) 397 | $date = date($format, strtotime($date)); 398 | else 399 | die('The given date was not an instance of DateTime, a UNIX timestamp or a date string.'); 400 | 401 | if ($this->version == Feed::ATOM) 402 | $this->setChannelElement('updated', $date); 403 | else 404 | $this->setChannelElement('lastBuildDate', $date); 405 | 406 | return $this; 407 | } 408 | 409 | /** 410 | * Set the 'description' channel element 411 | * 412 | * @access public 413 | * @param string value of 'description' channel tag 414 | * @return self 415 | */ 416 | public function setDescription($description) 417 | { 418 | if ($this->version != Feed::ATOM) 419 | $this->setChannelElement('description', $description); 420 | 421 | return $this; 422 | } 423 | 424 | /** 425 | * Set the 'link' channel element 426 | * 427 | * @access public 428 | * @param string value of 'link' channel tag 429 | * @return self 430 | */ 431 | public function setLink($link) 432 | { 433 | if ($this->version == Feed::ATOM) 434 | $this->setChannelElement('link', '', array('href' => $link)); 435 | else 436 | $this->setChannelElement('link', $link); 437 | 438 | return $this; 439 | } 440 | 441 | /** 442 | * Set custom 'link' channel elements. 443 | * 444 | * In ATOM feeds, only one link with alternate relation and the same combination of 445 | * type and hreflang values. 446 | * 447 | * @access public 448 | * @param string URI of this link 449 | * @param string relation type of the resource 450 | * @param string MIME type of the target resource 451 | * @param string language of the resource 452 | * @param string human-readable information about the resource 453 | * @param int length of the resource in bytes 454 | * @link https://www.iana.org/assignments/link-relations/link-relations.xml 455 | * @link https://tools.ietf.org/html/rfc4287#section-4.2.7 456 | * @return self 457 | */ 458 | public function setAtomLink($href, $rel = null, $type = null, $hreflang = null, $title = null, $length = null) 459 | { 460 | $data = array('href' => $href); 461 | 462 | if ($rel != null) { 463 | if (!is_string($rel) || empty($rel)) 464 | die('rel parameter must be a string and a valid relation identifier.'); 465 | 466 | $data['rel'] = $rel; 467 | } 468 | if ($type != null) { 469 | // Regex used from RFC 4287, page 41 470 | if (!is_string($type) || preg_match('/.+\/.+/', $type) != 1) 471 | die('type parameter must be a string and a MIME type.'); 472 | 473 | $data['type'] = $type; 474 | } 475 | if ($hreflang != null) { 476 | // Regex used from RFC 4287, page 41 477 | if (!is_string($hreflang) || preg_match('/[A-Za-z]{1,8}(-[A-Za-z0-9]{1,8})*/', $hreflang) != 1) 478 | die('hreflang parameter must be a string and a valid language code.'); 479 | 480 | $data['hreflang'] = $hreflang; 481 | } 482 | if ($title != null) { 483 | if (!is_string($title) || empty($title)) 484 | die('title parameter must be a string and not empty.'); 485 | 486 | $data['title'] = $title; 487 | } 488 | if ($length != null) { 489 | if (!is_int($length) || $length < 0) 490 | die('length parameter must be a positive integer.'); 491 | 492 | $data['length'] = (string) $length; 493 | } 494 | 495 | // ATOM spec. has some restrictions on atom:link usage 496 | // See RFC 4287, page 12 (4.1.1) 497 | if ($this->version == Feed::ATOM) { 498 | foreach ($this->channels as $key => $value) { 499 | if ($key != 'atom:link') 500 | continue; 501 | 502 | // $value is an array , so check every element 503 | foreach ($value as $linkItem) { 504 | // Only one link with relation alternate and same hreflang & type is allowed. 505 | if (@$linkItem['rel'] == 'alternate' && @$linkItem['hreflang'] == $hreflang && @$linkItem['type'] == $type) 506 | die('The feed must not contain more than one link element with a relation of "alternate"' 507 | . ' that has the same combination of type and hreflang attribute values.'); 508 | } 509 | } 510 | } 511 | 512 | return $this->setChannelElement('atom:link', '', $data, true); 513 | } 514 | 515 | /** 516 | * Set an 'atom:link' channel element with relation=self attribute. 517 | * Needs the full URL to this feed. 518 | * 519 | * @link http://www.rssboard.org/rss-profile#namespace-elements-atom-link 520 | * @access public 521 | * @param string URL to this feed 522 | * @return self 523 | */ 524 | public function setSelfLink($url) 525 | { 526 | return $this->setAtomLink($url, 'self', $this->getMIMEType()); 527 | } 528 | 529 | /** 530 | * Set the 'image' channel element 531 | * 532 | * @access public 533 | * @param string title of image 534 | * @param string link url of the image 535 | * @param string path url of the image 536 | * @return self 537 | */ 538 | public function setImage($title, $link, $url) 539 | { 540 | return $this->setChannelElement('image', array('title'=>$title, 'link'=>$link, 'url'=>$url)); 541 | } 542 | 543 | /** 544 | * Set the 'about' channel element. Only for RSS 1.0 545 | * 546 | * @access public 547 | * @param string value of 'about' channel tag 548 | * @return self 549 | */ 550 | public function setChannelAbout($url) 551 | { 552 | $this->data['ChannelAbout'] = $url; 553 | 554 | return $this; 555 | } 556 | 557 | /** 558 | * Generate an UUID. 559 | * 560 | * The UUID is based on an MD5 hash. If no key is given, a unique ID as the input 561 | * for the MD5 hash is generated. 562 | * 563 | * @author Anis uddin Ahmad 564 | * @access public 565 | * @param string optional key on which the UUID is generated 566 | * @param string an optional prefix 567 | * @return string the formated UUID 568 | */ 569 | public static function uuid($key = null, $prefix = '') 570 | { 571 | $key = ($key == null) ? uniqid(rand()) : $key; 572 | $chars = md5($key); 573 | $uuid = substr($chars,0,8) . '-'; 574 | $uuid .= substr($chars,8,4) . '-'; 575 | $uuid .= substr($chars,12,4) . '-'; 576 | $uuid .= substr($chars,16,4) . '-'; 577 | $uuid .= substr($chars,20,12); 578 | 579 | return $prefix . $uuid; 580 | } 581 | 582 | /** 583 | * Replace invalid XML characters. 584 | * 585 | * @link http://www.phpwact.org/php/i18n/charsets#xml See utf8_for_xml() function 586 | * @link http://www.w3.org/TR/REC-xml/#charsets 587 | * @link https://github.com/mibe/FeedWriter/issues/30 588 | * 589 | * @access public 590 | * @param string string which should be filtered 591 | * @param string replace invalid characters with this string 592 | * @return string the filtered string 593 | */ 594 | public static function filterInvalidXMLChars($string, $replacement = '_') // default to '\x{FFFD}' ??? 595 | { 596 | $result = preg_replace('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]+/u', $replacement, $string); 597 | 598 | // Did the PCRE replace failed because of bad UTF-8 data? 599 | // If yes, try a non-multibyte regex and without the UTF-8 mode enabled. 600 | if ($result == NULL && preg_last_error() == PREG_BAD_UTF8_ERROR) 601 | $result = preg_replace('/[^\x09\x0a\x0d\x20-\xFF]+/', $replacement, $string); 602 | 603 | // In case the regex replacing failed completely, return the whole unfiltered string. 604 | if ($result == NULL) 605 | $result = $string; 606 | 607 | return $result; 608 | } 609 | // End # public functions ---------------------------------------------- 610 | 611 | // Start # private functions ---------------------------------------------- 612 | 613 | /** 614 | * Returns all used XML namespace prefixes in this instance. 615 | * This includes all channel elements and feed items. 616 | * Unfortunately some namespace prefixes are not included, 617 | * because they are hardcoded, e.g. rdf. 618 | * 619 | * @access private 620 | * @return array Array with namespace prefix as value. 621 | */ 622 | private function getNamespacePrefixes() 623 | { 624 | $prefixes = array(); 625 | 626 | // Get all tag names from channel elements... 627 | $tags = array_keys($this->channels); 628 | 629 | // ... and now all names from feed items 630 | foreach ($this->items as $item) 631 | $tags = array_merge($tags, array_keys($item->getElements())); 632 | 633 | // Look for prefixes in those tag names 634 | foreach ($tags as $tag) { 635 | $elements = explode(':', $tag); 636 | 637 | if (count($elements) != 2) 638 | continue; 639 | 640 | $prefixes[] = $elements[0]; 641 | } 642 | 643 | return array_unique($prefixes); 644 | } 645 | 646 | /** 647 | * Returns the XML header and root element, depending on the feed type. 648 | * 649 | * @access private 650 | * @return string The XML header of the feed. 651 | */ 652 | private function makeHeader() 653 | { 654 | $out = 'encoding.'" ?>' . PHP_EOL; 655 | 656 | $prefixes = $this->getNamespacePrefixes(); 657 | $attributes = array(); 658 | $tagName = ''; 659 | $defaultNamespace = ''; 660 | 661 | if ($this->version == Feed::RSS2) { 662 | $tagName = 'rss'; 663 | $attributes['version'] = '2.0'; 664 | } elseif ($this->version == Feed::RSS1) { 665 | $tagName = 'rdf:RDF'; 666 | $prefixes[] = 'rdf'; 667 | $defaultNamespace = $this->namespaces['rss1']; 668 | } elseif ($this->version == Feed::ATOM) { 669 | $tagName = 'feed'; 670 | $defaultNamespace = $this->namespaces['atom']; 671 | 672 | // Ugly hack to remove the 'atom' value from the prefixes array. 673 | $prefixes = array_flip($prefixes); 674 | unset($prefixes['atom']); 675 | $prefixes = array_flip($prefixes); 676 | } 677 | 678 | // Iterate through every namespace prefix and add it to the element attributes. 679 | foreach ($prefixes as $prefix) { 680 | if (!isset($this->namespaces[$prefix])) 681 | die('Unknown XML namespace prefix: \'' . $prefix . '\'. Use the addNamespace method to add support for this prefix.'); 682 | else 683 | $attributes['xmlns:' . $prefix] = $this->namespaces[$prefix]; 684 | } 685 | 686 | // Include default namepsace, if required 687 | if (!empty($defaultNamespace)) 688 | $attributes['xmlns'] = $defaultNamespace; 689 | 690 | $out .= $this->makeNode($tagName, '', $attributes, true); 691 | 692 | return $out; 693 | } 694 | 695 | /** 696 | * Closes the open tags at the end of file 697 | * 698 | * @access private 699 | * @return string The XML footer of the feed. 700 | */ 701 | private function makeFooter() 702 | { 703 | if ($this->version == Feed::RSS2) { 704 | return '' . PHP_EOL . ''; 705 | } elseif ($this->version == Feed::RSS1) { 706 | return ''; 707 | } elseif ($this->version == Feed::ATOM) { 708 | return ''; 709 | } 710 | } 711 | 712 | /** 713 | * Creates a single node in XML format 714 | * 715 | * @access private 716 | * @param string name of the tag 717 | * @param mixed tag value as string or array of nested tags in 'tagName' => 'tagValue' format 718 | * @param array Attributes (if any) in 'attrName' => 'attrValue' format 719 | * @param string True if the end tag should be omitted. Defaults to false. 720 | * @return string formatted xml tag 721 | */ 722 | private function makeNode($tagName, $tagContent, $attributes = null, $omitEndTag = false) 723 | { 724 | $nodeText = ''; 725 | $attrText = ''; 726 | 727 | if (is_array($attributes) && count($attributes) > 0) { 728 | foreach ($attributes as $key => $value) { 729 | $value = self::filterInvalidXMLChars($value); 730 | $value = htmlspecialchars($value); 731 | $attrText .= " $key=\"$value\""; 732 | } 733 | } 734 | 735 | if (is_array($tagContent) && $this->version == Feed::RSS1) { 736 | $attrText = ' rdf:parseType="Resource"'; 737 | } 738 | 739 | $attrText .= (in_array($tagName, $this->CDATAEncoding) && $this->version == Feed::ATOM) ? ' type="html"' : ''; 740 | $nodeText .= "<{$tagName}{$attrText}>"; 741 | $nodeText .= (in_array($tagName, $this->CDATAEncoding)) ? ' $value) { 745 | if (is_array($value)) { 746 | $nodeText .= PHP_EOL; 747 | foreach ($value as $subValue) { 748 | $nodeText .= $this->makeNode($key, $subValue); 749 | } 750 | } else if (is_string($value)) { 751 | $nodeText .= $this->makeNode($key, $value); 752 | } else { 753 | throw new \RuntimeException("Unknown node-value type for $key"); 754 | } 755 | } 756 | } else { 757 | $tagContent = self::filterInvalidXMLChars($tagContent); 758 | $nodeText .= (in_array($tagName, $this->CDATAEncoding)) ? $this->sanitizeCDATA($tagContent) : htmlspecialchars($tagContent); 759 | } 760 | 761 | $nodeText .= (in_array($tagName, $this->CDATAEncoding)) ? ']]>' : ''; 762 | 763 | if (!$omitEndTag) 764 | $nodeText .= ""; 765 | 766 | $nodeText .= PHP_EOL; 767 | 768 | return $nodeText; 769 | } 770 | 771 | /** 772 | * Make the channels. 773 | * 774 | * @access private 775 | * @return string The feed header as XML containing all the feed metadata. 776 | */ 777 | private function makeChannels() 778 | { 779 | $out = ''; 780 | 781 | //Start channel tag 782 | switch ($this->version) { 783 | case Feed::RSS2: 784 | $out .= '' . PHP_EOL; 785 | break; 786 | case Feed::RSS1: 787 | $out .= (isset($this->data['ChannelAbout']))? "data['ChannelAbout']}\">" : "channels['link']}\">"; 788 | break; 789 | } 790 | 791 | //Print Items of channel 792 | foreach ($this->channels as $key => $value) { 793 | // In ATOM feeds, strip all ATOM namespace prefixes from the tag name. They are not needed here, 794 | // because the ATOM namespace name is set as default namespace. 795 | if ($this->version == Feed::ATOM && strncmp($key, 'atom', 4) == 0) { 796 | $key = substr($key, 5); 797 | } 798 | 799 | // The channel element can occur multiple times, when the key 'content' is not in the array. 800 | if (!isset($value['content'])) { 801 | // If this is the case, iterate through the array with the multiple elements. 802 | foreach ($value as $singleElement) { 803 | $out .= $this->makeNode($key, $singleElement['content'], $singleElement['attributes']); 804 | } 805 | } else { 806 | $out .= $this->makeNode($key, $value['content'], $value['attributes']); 807 | } 808 | } 809 | 810 | if ($this->version == Feed::RSS1) { 811 | //RSS 1.0 have special tag with channel 812 | $out .= "" . PHP_EOL . "" . PHP_EOL; 813 | foreach ($this->items as $item) { 814 | $thisItems = $item->getElements(); 815 | $out .= "" . PHP_EOL; 816 | } 817 | $out .= "" . PHP_EOL . "" . PHP_EOL . "" . PHP_EOL; 818 | } else if ($this->version == Feed::ATOM) { 819 | // ATOM feeds have a unique feed ID. This is generated from the 'link' channel element. 820 | $out .= $this->makeNode('id', Feed::uuid($this->channels['link']['attributes']['href'], 'urn:uuid:')); 821 | } 822 | 823 | return $out; 824 | } 825 | 826 | /** 827 | * Prints formatted feed items 828 | * 829 | * @access private 830 | * @return string The XML of every feed item. 831 | */ 832 | private function makeItems() 833 | { 834 | $out = ''; 835 | 836 | foreach ($this->items as $item) { 837 | $thisItems = $item->getElements(); 838 | 839 | // the argument is printed as rdf:about attribute of item in rss 1.0 840 | $out .= $this->startItem($thisItems['link']['content']); 841 | 842 | foreach ($thisItems as $feedItem) { 843 | $name = $feedItem['name']; 844 | 845 | // Strip all ATOM namespace prefixes from tags when feed is an ATOM feed. 846 | // Not needed here, because the ATOM namespace name is used as default namespace. 847 | if ($this->version == Feed::ATOM && strncmp($name, 'atom', 4) == 0) 848 | $name = substr($name, 5); 849 | 850 | $out .= $this->makeNode($name, $feedItem['content'], $feedItem['attributes']); 851 | } 852 | $out .= $this->endItem(); 853 | } 854 | 855 | return $out; 856 | } 857 | 858 | /** 859 | * Make the starting tag of channels 860 | * 861 | * @access private 862 | * @param string The vale of about tag which is used for RSS 1.0 only. 863 | * @return string The starting XML tag of an feed item. 864 | */ 865 | private function startItem($about = false) 866 | { 867 | $out = ''; 868 | 869 | if ($this->version == Feed::RSS2) { 870 | $out .= '' . PHP_EOL; 871 | } elseif ($this->version == Feed::RSS1) { 872 | if ($about) { 873 | $out .= "" . PHP_EOL; 874 | } else { 875 | throw new \Exception("link element is not set - It's required for RSS 1.0 to be used as the about attribute of the item tag."); 876 | } 877 | } elseif ($this->version == Feed::ATOM) { 878 | $out .= "" . PHP_EOL; 879 | } 880 | 881 | return $out; 882 | } 883 | 884 | /** 885 | * Closes feed item tag 886 | * 887 | * @access private 888 | * @return string The ending XML tag of an feed item. 889 | */ 890 | private function endItem() 891 | { 892 | if ($this->version == Feed::RSS2 || $this->version == Feed::RSS1) { 893 | return '' . PHP_EOL; 894 | } elseif ($this->version == Feed::ATOM) { 895 | return '' . PHP_EOL; 896 | } 897 | } 898 | 899 | /** 900 | * Sanitizes data which will be later on returned as CDATA in the feed. 901 | * 902 | * A "]]>" respectively "", "]]>", $text); 912 | $text = str_replace(" 8 | * Copyright (C) 2010-2013, 2015 Michael Bemmerl 9 | * 10 | * This file is part of the "Universal Feed Writer" project. 11 | * 12 | * This program is free software: you can redistribute it and/or modify 13 | * it under the terms of the GNU General Public License as published by 14 | * the Free Software Foundation, either version 3 of the License, or 15 | * (at your option) any later version. 16 | * 17 | * This program is distributed in the hope that it will be useful, 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 | * GNU General Public License for more details. 21 | * 22 | * You should have received a copy of the GNU General Public License 23 | * along with this program. If not, see . 24 | */ 25 | 26 | /** 27 | * Universal Feed Writer 28 | * 29 | * Item class - Used as feed element in Feed class 30 | * 31 | * @package UniversalFeedWriter 32 | * @author Anis uddin Ahmad 33 | * @link http://www.ajaxray.com/projects/rss 34 | */ 35 | class Item 36 | { 37 | /** 38 | * Collection of feed item elements 39 | */ 40 | private $elements = array(); 41 | 42 | /** 43 | * Contains the format of this feed. 44 | */ 45 | private $version; 46 | 47 | /** 48 | * Is used as a suffix when multiple elements have the same name. 49 | **/ 50 | private $_cpt = 0; 51 | 52 | /** 53 | * Constructor 54 | * 55 | * @param constant (RSS1/RSS2/ATOM) RSS2 is default. 56 | */ 57 | public function __construct($version = Feed::RSS2) 58 | { 59 | $this->version = $version; 60 | } 61 | 62 | /** 63 | * Return an unique number 64 | * 65 | * @access private 66 | * @return int 67 | **/ 68 | private function cpt() 69 | { 70 | return $this->_cpt++; 71 | } 72 | 73 | /** 74 | * Add an element to elements array 75 | * 76 | * @access public 77 | * @param string The tag name of an element 78 | * @param string The content of tag 79 | * @param array Attributes (if any) in 'attrName' => 'attrValue' format 80 | * @param boolean Specifies if an already existing element is overwritten. 81 | * @param boolean Specifies if multiple elements of the same name are allowed. 82 | * @return self 83 | */ 84 | public function addElement($elementName, $content, $attributes = null, $overwrite = FALSE, $allowMultiple = FALSE) 85 | { 86 | $key = $elementName; 87 | 88 | // return if element already exists & if overwriting is disabled 89 | // & if multiple elements are not allowed. 90 | if (isset($this->elements[$elementName]) && !$overwrite) { 91 | if (!$allowMultiple) 92 | return; 93 | 94 | $key .= '-' . $this->cpt(); 95 | } 96 | 97 | $this->elements[$key]['name'] = $elementName; 98 | $this->elements[$key]['content'] = $content; 99 | $this->elements[$key]['attributes'] = $attributes; 100 | 101 | return $this; 102 | } 103 | 104 | /** 105 | * Set multiple feed elements from an array. 106 | * Elements which have attributes cannot be added by this method 107 | * 108 | * @access public 109 | * @param array array of elements in 'tagName' => 'tagContent' format. 110 | * @return self 111 | */ 112 | public function addElementArray($elementArray) 113 | { 114 | if (!is_array($elementArray)) 115 | return; 116 | 117 | foreach ($elementArray as $elementName => $content) { 118 | $this->addElement($elementName, $content); 119 | } 120 | 121 | return $this; 122 | } 123 | 124 | /** 125 | * Return the collection of elements in this feed item 126 | * 127 | * @access public 128 | * @return array All elements of this item. 129 | */ 130 | public function getElements() 131 | { 132 | return $this->elements; 133 | } 134 | 135 | /** 136 | * Return the type of this feed item 137 | * 138 | * @access public 139 | * @return string The feed type, as defined in Feed.php 140 | */ 141 | public function getVersion() 142 | { 143 | return $this->version; 144 | } 145 | 146 | // Wrapper functions ------------------------------------------------------ 147 | 148 | /** 149 | * Set the 'description' element of feed item 150 | * 151 | * @access public 152 | * @param string The content of 'description' or 'summary' element 153 | * @return self 154 | */ 155 | public function setDescription($description) 156 | { 157 | $tag = ($this->version == Feed::ATOM) ? 'summary' : 'description'; 158 | 159 | return $this->addElement($tag, $description); 160 | } 161 | 162 | /** 163 | * Set the 'content' element of the feed item 164 | * For ATOM feeds only 165 | * 166 | * @access public 167 | * @param string Content for the item (i.e., the body of a blog post). 168 | * @return self 169 | */ 170 | public function setContent($content) 171 | { 172 | if ($this->version != Feed::ATOM) 173 | die('The content element is supported in ATOM feeds only.'); 174 | 175 | return $this->addElement('content', $content, array('type' => 'html')); 176 | } 177 | 178 | /** 179 | * Set the 'title' element of feed item 180 | * 181 | * @access public 182 | * @param string The content of 'title' element 183 | * @return self 184 | */ 185 | public function setTitle($title) 186 | { 187 | return $this->addElement('title', $title); 188 | } 189 | 190 | /** 191 | * Set the 'date' element of the feed item. 192 | * 193 | * The value of the date parameter can be either an instance of the 194 | * DateTime class, an integer containing a UNIX timestamp or a string 195 | * which is parseable by PHP's 'strtotime' function. 196 | * 197 | * @access public 198 | * @param DateTime|int|string Date which should be used. 199 | * @return self 200 | */ 201 | public function setDate($date) 202 | { 203 | if (!is_numeric($date)) { 204 | if ($date instanceof DateTime) 205 | $date = $date->getTimestamp(); 206 | else { 207 | $date = strtotime($date); 208 | 209 | if ($date === FALSE) 210 | die('The given date string was not parseable.'); 211 | } 212 | } elseif ($date < 0) 213 | die('The given date is not an UNIX timestamp.'); 214 | 215 | if ($this->version == Feed::ATOM) { 216 | $tag = 'updated'; 217 | $value = date(\DATE_ATOM, $date); 218 | } elseif ($this->version == Feed::RSS2) { 219 | $tag = 'pubDate'; 220 | $value = date(\DATE_RSS, $date); 221 | } else { 222 | $tag = 'dc:date'; 223 | $value = date("Y-m-d", $date); 224 | } 225 | 226 | return $this->addElement($tag, $value); 227 | } 228 | 229 | /** 230 | * Set the 'link' element of feed item 231 | * 232 | * @access public 233 | * @param string The content of 'link' element 234 | * @return void 235 | */ 236 | public function setLink($link) 237 | { 238 | if ($this->version == Feed::RSS2 || $this->version == Feed::RSS1) { 239 | $this->addElement('link', $link); 240 | } else { 241 | $this->addElement('link','',array('href'=>$link)); 242 | $this->addElement('id', Feed::uuid($link,'urn:uuid:')); 243 | } 244 | 245 | return $this; 246 | } 247 | 248 | /** 249 | * Attach a external media to the feed item. 250 | * Not supported in RSS 1.0 feeds. 251 | * 252 | * See RFC 4288 for syntactical correct MIME types. 253 | * 254 | * Note that you should avoid the use of more than one enclosure in one item, 255 | * since some RSS aggregators don't support it. 256 | * 257 | * @access public 258 | * @param string The URL of the media. 259 | * @param integer The length of the media. 260 | * @param string The MIME type attribute of the media. 261 | * @param boolean Specifies, if multiple enclosures are allowed 262 | * @return self 263 | * @link https://tools.ietf.org/html/rfc4288 264 | */ 265 | public function addEnclosure($url, $length, $type, $multiple = TRUE) 266 | { 267 | if ($this->version == Feed::RSS1) 268 | die('Media attachment is not supported in RSS1 feeds.'); 269 | 270 | // the length parameter should be set to 0 if it can't be determined 271 | // see http://www.rssboard.org/rss-profile#element-channel-item-enclosure 272 | if (!is_numeric($length) || $length < 0) 273 | die('The length parameter must be an integer and greater or equals to zero.'); 274 | 275 | // Regex used from RFC 4287, page 41 276 | if (!is_string($type) || preg_match('/.+\/.+/', $type) != 1) 277 | die('type parameter must be a string and a MIME type.'); 278 | 279 | $attributes = array('length' => $length, 'type' => $type); 280 | 281 | if ($this->version == Feed::RSS2) { 282 | $attributes['url'] = $url; 283 | $this->addElement('enclosure', '', $attributes, FALSE, $multiple); 284 | } else { 285 | $attributes['href'] = $url; 286 | $attributes['rel'] = 'enclosure'; 287 | $this->addElement('atom:link', '', $attributes, FALSE, $multiple); 288 | } 289 | 290 | return $this; 291 | } 292 | 293 | /** 294 | * Alias of addEnclosure, for backward compatibility. Using only this 295 | * method ensures that the 'enclosure' element will be present only once. 296 | * 297 | * @access public 298 | * @param string The URL of the media. 299 | * @param integer The length of the media. 300 | * @param string The MIME type attribute of the media. 301 | * @return self 302 | * @link https://tools.ietf.org/html/rfc4288 303 | * @deprecated Use the addEnclosure method instead. 304 | * 305 | **/ 306 | public function setEnclosure($url, $length, $type) 307 | { 308 | return $this->addEnclosure($url, $length, $type, false); 309 | } 310 | 311 | /** 312 | * Set the 'author' element of feed item. 313 | * Not supported in RSS 1.0 feeds. 314 | * 315 | * @access public 316 | * @param string The author of this item 317 | * @param string Optional email address of the author 318 | * @param string Optional URI related to the author 319 | * @return self 320 | */ 321 | public function setAuthor($author, $email = null, $uri = null) 322 | { 323 | switch ($this->version) { 324 | case Feed::RSS1: die('The author element is not supported in RSS1 feeds.'); 325 | break; 326 | case Feed::RSS2: 327 | if ($email != null) 328 | $author = $email . ' (' . $author . ')'; 329 | 330 | $this->addElement('author', $author); 331 | break; 332 | case Feed::ATOM: 333 | $elements = array('name' => $author); 334 | 335 | // Regex from RFC 4287 page 41 336 | if ($email != null && preg_match('/.+@.+/', $email) == 1) 337 | $elements['email'] = $email; 338 | 339 | if ($uri != null) 340 | $elements['uri'] = $uri; 341 | 342 | $this->addElement('author', $elements); 343 | break; 344 | } 345 | 346 | return $this; 347 | } 348 | 349 | /** 350 | * Set the unique identifier of the feed item 351 | * 352 | * On ATOM feeds, the identifier must begin with an valid URI scheme. 353 | * 354 | * @access public 355 | * @param string The unique identifier of this item 356 | * @param boolean The value of the 'isPermaLink' attribute in RSS 2 feeds. 357 | * @return self 358 | */ 359 | public function setId($id, $permaLink = false) 360 | { 361 | if ($this->version == Feed::RSS2) { 362 | if (!is_bool($permaLink)) 363 | die('The permaLink parameter must be boolean.'); 364 | 365 | $permaLink = $permaLink ? 'true' : 'false'; 366 | 367 | $this->addElement('guid', $id, array('isPermaLink' => $permaLink)); 368 | } elseif ($this->version == Feed::ATOM) { 369 | // Check if the given ID is an valid URI scheme (see RFC 4287 4.2.6) 370 | // The list of valid schemes was generated from http://www.iana.org/assignments/uri-schemes 371 | // by using only permanent or historical schemes. 372 | $validSchemes = array('aaa', 'aaas', 'about', 'acap', 'acct', 'afs', 'cap', 'cid', 'coap', 'coaps', 'crid', 'data', 'dav', 'dict', 'dns', 'dtn', 'dvb', 'example', 'fax', 'file', 'filesystem', 'ftp', 'geo', 'go', 'gopher', 'h323', 'ham', 'http', 'https', 'iax', 'icap', 'icon', 'im', 'imap', 'info', 'ipn', 'ipp', 'ipps', 'iris', 'iris.beep', 'iris.lwz', 'iris.xpc', 'iris.xpcs', 'jabber', 'jms', 'ldap', 'mailserver', 'mailto', 'mid', 'modem', 'msrp', 'msrps', 'mtqp', 'mupdate', 'news', 'nfs', 'ni', 'nih', 'nntp', 'opaquelocktoken', 'pack', 'pkcs11', 'pop', 'pres', 'prospero', 'reload', 'rsync', 'rtsp', 'rtsps', 'rtspu', 'service', 'session', 'shttp', 'sieve', 'sip', 'sips', 'sms', 'snews', 'snmp', 'soap.beep', 'soap.beeps', 'stun', 'stuns', 'tag', 'tel', 'telnet', 'tftp', 'thismessage', 'tip', 'tn3270', 'turn', 'turns', 'tv', 'urn', 'vemmi', 'videotex', 'wais', 'ws', 'wss', 'xcon', 'xcon-userid', 'xmlrpc.beep', 'xmlrpc.beeps', 'xmpp', 'z39.50', 'z39.50r', 'z39.50s'); 373 | $found = FALSE; 374 | $checkId = strtolower($id); 375 | 376 | foreach($validSchemes as $scheme) 377 | if (strrpos($checkId, $scheme . ':', -strlen($checkId)) !== FALSE) 378 | { 379 | $found = TRUE; 380 | break; 381 | } 382 | 383 | if (!$found) 384 | die("The ID must begin with an IANA-registered URI scheme."); 385 | 386 | $this->addElement('id', $id, NULL, TRUE); 387 | } else 388 | die('A unique ID is not supported in RSS1 feeds.'); 389 | 390 | return $this; 391 | } 392 | 393 | } // end of class Item 394 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Generate RSS 1.0, RSS 2.0 or ATOM formatted feeds 2 | ==================================================== 3 | 4 | This package can be used to generate feeds in either RSS 1.0, RSS 2.0 or ATOM 5 | formats. 6 | 7 | Applications can create feed writer object, several feed item objects, set 8 | several types of properties of either feeds and feed items, and add items to 9 | the feed. 10 | 11 | Once a feed is fully composed with its items, the feed writer class can generate 12 | the necessary XML structure to describe the feed in the RSS or ATOM formats. 13 | The feed is generated as part of the current feed output. 14 | 15 | 16 | Requirements 17 | --------------- 18 | 19 | PHP >= 5.3 20 | 21 | If you don't have 5.3 available on your system, there's a version supporting 22 | PHP >= 5.0 in the "legacy-php-5.0" branch. 23 | 24 | 25 | Documentation 26 | -------------- 27 | 28 | The documentation can be found in the "gh-pages" branch or on GitHub Pages: 29 | http://mibe.github.io/FeedWriter/ 30 | 31 | Check **/examples** directory for usages examples. 32 | 33 | 34 | Authors 35 | ---------- 36 | (in chronological order) 37 | 38 | - [Anis uddin Ahmad](https://github.com/ajaxray) 39 | - [Michael Bemmerl](https://github.com/mibe) 40 | - Phil Freo 41 | - Paul Ferrett 42 | - Brennen Bearnes 43 | - Michael Robinson 44 | - Baptiste Fontaine 45 | - Kristián Valentín 46 | - Brandtley McMinn 47 | - Julian Bogdani 48 | -------------------------------------------------------------------------------- /RSS1.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * This file is part of the "Universal Feed Writer" project. 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU General Public License 20 | * along with this program. If not, see . 21 | */ 22 | 23 | /** 24 | * Wrapper for creating RSS1 feeds 25 | * 26 | * @package UniversalFeedWriter 27 | */ 28 | class RSS1 extends Feed 29 | { 30 | /** 31 | * {@inheritdoc} 32 | */ 33 | public function __construct() 34 | { 35 | parent::__construct(Feed::RSS1); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /RSS2.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * This file is part of the "Universal Feed Writer" project. 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU General Public License 20 | * along with this program. If not, see . 21 | */ 22 | 23 | /** 24 | * Wrapper for creating RSS2 feeds 25 | * 26 | * @package UniversalFeedWriter 27 | */ 28 | class RSS2 extends Feed 29 | { 30 | /** 31 | * {@inheritdoc} 32 | */ 33 | public function __construct() 34 | { 35 | parent::__construct(Feed::RSS2); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mibe/feedwriter", 3 | "description": "Generate feeds in either RSS 1.0, RSS 2.0 or ATOM formats", 4 | "keywords": ["RSS", "RSS 1.0", "RSS 2.0", "RSS2", "ATOM", "feed"], 5 | "homepage": "https://github.com/mibe/FeedWriter", 6 | "type": "library", 7 | "license": "GPL-3.0", 8 | "authors": [ 9 | { 10 | "name": "Anis uddin Ahmad", 11 | "email": "anis.programmer@gmail.com" 12 | }, 13 | { 14 | "name": "Michael Bemmerl", 15 | "email": "mail@mx-server.de" 16 | }, 17 | { 18 | "name": "Phil Freo" 19 | }, 20 | { 21 | "name": "Paul Ferrett" 22 | }, 23 | { 24 | "name": "Brennen Bearnes" 25 | }, 26 | { 27 | "name": "Michael Robinson", 28 | "email": "mike@pagesofinterest.net" 29 | }, 30 | { 31 | "name": "Baptiste Fontaine" 32 | }, 33 | { 34 | "name": "Kristián Valentín" 35 | }, 36 | { 37 | "name": "Brandtley McMinn" 38 | }, 39 | { 40 | "name": "Julian Bogdani" 41 | } 42 | ], 43 | "minimum-stability": "dev", 44 | "autoload": { 45 | "classmap": [ 46 | "ATOM.php", 47 | "Feed.php", 48 | "Item.php", 49 | "RSS1.php", 50 | "RSS2.php" 51 | ] 52 | }, 53 | "require" : { 54 | "php": ">=5.3.0" 55 | }, 56 | "extra": { 57 | "branch-alias": { 58 | "dev-master": "1.0.x-dev" 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /examples/example_atom.php: -------------------------------------------------------------------------------- 1 | 15 | * 16 | * This file is part of the "Universal Feed Writer" project. 17 | * 18 | * This program is free software: you can redistribute it and/or modify 19 | * it under the terms of the GNU General Public License as published by 20 | * the Free Software Foundation, either version 3 of the License, or 21 | * (at your option) any later version. 22 | * 23 | * This program is distributed in the hope that it will be useful, 24 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 25 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 26 | * GNU General Public License for more details. 27 | * 28 | * You should have received a copy of the GNU General Public License 29 | * along with this program. If not, see . 30 | */ 31 | 32 | // IMPORTANT : No need to add id for feed or channel. It will be automatically created from link. 33 | 34 | //Creating an instance of ATOM class. 35 | $TestFeed = new ATOM; 36 | 37 | //Setting the channel elements 38 | //Use wrapper functions for common elements 39 | $TestFeed->setTitle('Testing the RSS writer class'); 40 | $TestFeed->setLink('http://www.ajaxray.com/rss2/channel/about'); 41 | $TestFeed->setDate(new DateTime()); 42 | 43 | //For other channel elements, use setChannelElement() function 44 | $TestFeed->setChannelElement('author', array('name'=>'Anis uddin Ahmad')); 45 | 46 | //You can add additional link elements, e.g. to a PubSubHubbub server with custom relations. 47 | $TestFeed->setSelfLink('http://example.com/myfeed'); 48 | $TestFeed->setAtomLink('http://pubsubhubbub.appspot.com', 'hub'); 49 | 50 | //Adding a feed. Generally this portion will be in a loop and add all feeds. 51 | 52 | //Create an empty Item 53 | $newItem = $TestFeed->createNewItem(); 54 | 55 | //Add elements to the feed item 56 | //Use wrapper functions to add common feed elements 57 | $newItem->setTitle('The first feed'); 58 | $newItem->setLink('http://www.yahoo.com'); 59 | $newItem->setDate(time()); 60 | $newItem->setAuthor('Anis uddin Ahmad', 'anis@example.invalid'); 61 | $newItem->setEnclosure('http://upload.wikimedia.org/wikipedia/commons/4/49/En-us-hello-1.ogg', 11779, 'audio/ogg'); 62 | 63 | //Internally changed to "summary" tag for ATOM feed 64 | $newItem->setDescription('This is a test of adding CDATA encoded description by the php Universal Feed Writer class'); 65 | $newItem->setContent('

hi.

This is the content for the entry.

'); 66 | 67 | //Now add the feed item 68 | $TestFeed->addItem($newItem); 69 | 70 | //OK. Everything is done. Now generate the feed. 71 | $TestFeed->printFeed(); 72 | -------------------------------------------------------------------------------- /examples/example_minimum.php: -------------------------------------------------------------------------------- 1 | 15 | * 16 | * This file is part of the "Universal Feed Writer" project. 17 | * 18 | * This program is free software: you can redistribute it and/or modify 19 | * it under the terms of the GNU General Public License as published by 20 | * the Free Software Foundation, either version 3 of the License, or 21 | * (at your option) any later version. 22 | * 23 | * This program is distributed in the hope that it will be useful, 24 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 25 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 26 | * GNU General Public License for more details. 27 | * 28 | * You should have received a copy of the GNU General Public License 29 | * along with this program. If not, see . 30 | */ 31 | 32 | //Creating an instance of RSS2 class. 33 | $TestFeed = new RSS2; 34 | 35 | //Setting the channel elements 36 | //Use wrapper functions for common channel elements 37 | $TestFeed->setTitle('Testing & Checking the RSS writer class'); 38 | $TestFeed->setLink('http://www.ajaxray.com'); 39 | $TestFeed->setDescription('This is a test of creating a RSS 2.0 feed Universal Feed Writer'); 40 | 41 | //Image title and link must match with the 'title' and 'link' channel elements for valid RSS 2.0 42 | $TestFeed->setImage('Testing & Checking the RSS writer class','http://www.ajaxray.com','http://www.rightbrainsolution.com/_resources/img/logo.png'); 43 | 44 | //Let's add some feed items: Create two empty Item instances 45 | $itemOne = $TestFeed->createNewItem(); 46 | $itemTwo = $TestFeed->createNewItem(); 47 | 48 | //Add item details 49 | $itemOne->setTitle('The title of the first entry.'); 50 | $itemOne->setLink('http://www.google.de'); 51 | $itemOne->setDate(time()); 52 | $itemOne->setDescription('And here\'s the description of the entry.'); 53 | 54 | $itemTwo->setTitle('Lorem ipsum'); 55 | $itemTwo->setLink('http://www.example.com'); 56 | $itemTwo->setDate(1234567890); 57 | $itemTwo->setDescription('Lorem ipsum dolor sit amet, consectetur, adipisci velit'); 58 | 59 | //Now add the feed item 60 | $TestFeed->addItem($itemOne); 61 | $TestFeed->addItem($itemTwo); 62 | 63 | //OK. Everything is done. Now generate the feed. 64 | $TestFeed->printFeed(); 65 | -------------------------------------------------------------------------------- /examples/example_rss1.php: -------------------------------------------------------------------------------- 1 | 15 | * 16 | * This file is part of the "Universal Feed Writer" project. 17 | * 18 | * This program is free software: you can redistribute it and/or modify 19 | * it under the terms of the GNU General Public License as published by 20 | * the Free Software Foundation, either version 3 of the License, or 21 | * (at your option) any later version. 22 | * 23 | * This program is distributed in the hope that it will be useful, 24 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 25 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 26 | * GNU General Public License for more details. 27 | * 28 | * You should have received a copy of the GNU General Public License 29 | * along with this program. If not, see . 30 | */ 31 | 32 | //Creating an instance of RSS1 class. 33 | $TestFeed = new RSS1; 34 | 35 | //Setting the channel elements 36 | //Use wrapper functions for common elements 37 | //For other optional channel elements, use setChannelElement() function 38 | $TestFeed->setTitle('Testing the RSS writer class'); 39 | $TestFeed->setLink('http://www.ajaxray.com/rss2/channel/about'); 40 | $TestFeed->setDescription('This is test of creating a RSS 1.0 feed by Universal Feed Writer'); 41 | 42 | //It's important for RSS 1.0 43 | $TestFeed->setChannelAbout('http://www.ajaxray.com/rss2/channel/about'); 44 | 45 | //Adding a feed. Generally this portion will be in a loop and add all feeds. 46 | 47 | //Create an empty FeedItem 48 | $newItem = $TestFeed->createNewItem(); 49 | 50 | //Add elements to the feed item 51 | //Use wrapper functions to add common feed elements 52 | $newItem->setTitle('The first feed'); 53 | $newItem->setLink('http://www.yahoo.com'); 54 | //The parameter is a timestamp for setDate() function 55 | $newItem->setDate(time()); 56 | $newItem->setDescription('This is test of adding CDATA encoded description by the php Universal Feed Writer class'); 57 | //Use core addElement() function for other supported optional elements 58 | $newItem->addElement('dc:subject', 'Nothing but test'); 59 | 60 | //Now add the feed item 61 | $TestFeed->addItem($newItem); 62 | 63 | //Adding multiple elements from array 64 | //Elements which have an attribute cannot be added by this way 65 | $newItem = $TestFeed->createNewItem(); 66 | $newItem->addElementArray(array('title'=>'The 2nd feed', 'link'=>'http://www.google.com', 'description'=>'This is a test of the FeedWriter class')); 67 | $TestFeed->addItem($newItem); 68 | 69 | //OK. Everything is done. Now generate the feed. 70 | $TestFeed->printFeed(); 71 | -------------------------------------------------------------------------------- /examples/example_rss2.php: -------------------------------------------------------------------------------- 1 | 15 | * Copyright (C) 2013 Michael Bemmerl 16 | * 17 | * This file is part of the "Universal Feed Writer" project. 18 | * 19 | * This program is free software: you can redistribute it and/or modify 20 | * it under the terms of the GNU General Public License as published by 21 | * the Free Software Foundation, either version 3 of the License, or 22 | * (at your option) any later version. 23 | * 24 | * This program is distributed in the hope that it will be useful, 25 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 26 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 27 | * GNU General Public License for more details. 28 | * 29 | * You should have received a copy of the GNU General Public License 30 | * along with this program. If not, see . 31 | */ 32 | 33 | // Creating an instance of RSS2 class. 34 | $TestFeed = new RSS2; 35 | 36 | // Setting some basic channel elements. These three elements are mandatory. 37 | $TestFeed->setTitle('Testing & Checking the Feed Writer project'); 38 | $TestFeed->setLink('https://github.com/mibe/FeedWriter'); 39 | $TestFeed->setDescription('This is just an example how to use the Feed Writer project in your code.'); 40 | 41 | // Image title and link must match with the 'title' and 'link' channel elements for RSS 2.0, 42 | // which were set above. 43 | $TestFeed->setImage('Testing & Checking the Feed Writer project', 'https://github.com/mibe/FeedWriter', 'https://upload.wikimedia.org/wikipedia/commons/thumb/d/d9/Rss-feed.svg/256px-Rss-feed.svg.png'); 44 | 45 | // Use core setChannelElement() function for other optional channel elements. 46 | // See http://www.rssboard.org/rss-specification#optionalChannelElements 47 | // for other optional channel elements. Here the language code for American English and 48 | $TestFeed->setChannelElement('language', 'en-US'); 49 | 50 | // The date when this feed was lastly updated. The publication date is also set. 51 | $TestFeed->setDate(date(DATE_RSS, time())); 52 | $TestFeed->setChannelElement('pubDate', date(\DATE_RSS, strtotime('2013-04-06'))); 53 | 54 | // By using arrays as channelElement values, can be set element like this 55 | // 56 | // Saturday 57 | // Sunday 58 | // 59 | // Thanks to - Peter Fargaš 60 | $TestFeed->setChannelElement("skipDays", array('day'=>['Saturday','Sunday'])); 61 | 62 | // You can add additional link elements, e.g. to a PubSubHubbub server with custom relations. 63 | // It's recommended to provide a backlink to the feed URL. 64 | $TestFeed->setSelfLink('http://example.com/myfeed'); 65 | $TestFeed->setAtomLink('http://pubsubhubbub.appspot.com', 'hub'); 66 | 67 | // You can add more XML namespaces for more custom channel elements which are not defined 68 | // in the RSS 2 specification. Here the 'creativeCommons' element is used. There are much more 69 | // available. Have a look at this list: http://feedvalidator.org/docs/howto/declare_namespaces.html 70 | $TestFeed->addNamespace('creativeCommons', 'http://backend.userland.com/creativeCommonsRssModule'); 71 | $TestFeed->setChannelElement('creativeCommons:license', 'http://www.creativecommons.org/licenses/by/1.0'); 72 | 73 | // If you want you can also add a line to publicly announce that you used 74 | // this fine piece of software to generate the feed. ;-) 75 | $TestFeed->addGenerator(); 76 | 77 | // Here we are done setting up the feed. What's next is adding some feed items. 78 | 79 | // Create a new feed item. 80 | $newItem = $TestFeed->createNewItem(); 81 | 82 | // Add basic elements to the feed item 83 | // These are again mandatory for a valid feed. 84 | $newItem->setTitle('Hello World!'); 85 | $newItem->setLink('http://www.example.com'); 86 | $newItem->setDescription('This is a test of adding a description by the Feed Writer classes. It\'s automatically CDATA encoded.'); 87 | 88 | // The following method calls add some optional elements to the feed item. 89 | 90 | // Let's set the publication date of this item. You could also use a UNIX timestamp or 91 | // an instance of PHP's DateTime class. 92 | $newItem->setDate('2013-04-07 00:50:30'); 93 | 94 | // You can also attach a media object to a feed item. You just need the URL, the byte length 95 | // and the MIME type of the media. Here's a quirk: The RSS2 spec says "The url must be an http url.". 96 | // Other schemes like ftp, https, etc. produce an error in feed validators. 97 | $newItem->setEnclosure('http://upload.wikimedia.org/wikipedia/commons/4/49/En-us-hello-1.ogg', 11779, 'audio/ogg'); 98 | 99 | // If you want you can set the name (and email address) of the author of this feed item. 100 | $newItem->setAuthor('Anis uddin Ahmad', 'admin@ajaxray.com'); 101 | 102 | // You can set a globally unique identifier. This can be a URL or any other string. 103 | // If you set permaLink to true, the identifier must be an URL. The default of the 104 | // permaLink parameter is false. 105 | $newItem->setId('http://example.com/URL/to/article', true); 106 | 107 | // Use the addElement() method for other optional elements. 108 | // This here will add the 'source' element. The second parameter is the value of the element 109 | // and the third is an array containing the element attributes. 110 | $newItem->addElement('source', 'Mike\'s page', array('url' => 'http://www.example.com')); 111 | 112 | // Now add the feed item to the main feed. 113 | $TestFeed->addItem($newItem); 114 | 115 | // Another method to add feeds items is by using an array which contains key-value pairs 116 | // of every item element. Elements which have attributes cannot be added by this way. 117 | $newItem = $TestFeed->createNewItem(); 118 | $newItem->addElementArray(array('title'=> 'The 2nd item', 'link' => 'http://www.google.com', 'description' => 'Just another test.')); 119 | $TestFeed->addItem($newItem); 120 | 121 | // OK. Everything is done. Now generate the feed. 122 | // Then do anything (e,g cache, save, attach, print) you want with the feed in $myFeed. 123 | $myFeed = $TestFeed->generateFeed(); 124 | 125 | // If you want to send the feed directly to the browser, use the printFeed() method. 126 | $TestFeed->printFeed(); --------------------------------------------------------------------------------