Source of file Item.php
Size: 11,856 Bytes - Last Modified: 2017-08-13T05:38:53+02:00
C:/xampp/htdocs/PodTube/src/Feeds/Item.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 | <?php namespace FeedWriter; use DateTime; /* * Copyright (C) 2008 Anis uddin Ahmad <anisniit@gmail.com> * Copyright (C) 2010-2013, 2015-2016 Michael Bemmerl <mail@mx-server.de> * * This file is part of the "Universal Feed Writer" project. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ /** * Universal Feed Writer * * Item class - Used as feed element in Feed class * * @package UniversalFeedWriter * @author Anis uddin Ahmad <anisniit@gmail.com> * @link http://www.ajaxray.com/projects/rss */ class Item { /** * Collection of feed item elements */ private $elements = []; /** * Contains the format of this feed. */ private $version; /** * Is used as a suffix when multiple elements have the same name. **/ private $_cpt = 0; /** * Constructor * * @param string $version * @internal param string $RSS2 is default. */ public function __construct($version = Feed::RSS2){ $this->version = $version; } /** * Return an unique number * * @access private * @return int **/ private function cpt(){ return $this->_cpt++; } /** * Add an element to elements array * * @access public * @param string The tag name of an element * @param string The content of tag * @param array Attributes (if any) in 'attrName' => 'attrValue' format * @param boolean Specifies if an already existing element is overwritten. * @param boolean Specifies if multiple elements of the same name are allowed. * @return self */ public function addElement($elementName, $content, $attributes = null, $overwrite = false, $allowMultiple = false){ $key = $elementName; // return if element already exists & if overwriting is disabled // & if multiple elements are not allowed. if(isset($this->elements[$elementName]) && !$overwrite){ if(!$allowMultiple){ return null; } $key .= '-' . $this->cpt(); } $this->elements[$key]['name'] = $elementName; $this->elements[$key]['content'] = $content; $this->elements[$key]['attributes'] = $attributes; return $this; } /** * Set multiple feed elements from an array. * Elements which have attributes cannot be added by this method * * @access public * @param array array of elements in 'tagName' => 'tagContent' format. * @return self */ public function addElementArray(array $elementArray){ foreach($elementArray as $elementName => $content){ $this->addElement($elementName, $content); } return $this; } /** * Return the collection of elements in this feed item * * @access public * @return array All elements of this item. */ public function getElements(){ return $this->elements; } /** * Return the type of this feed item * * @access public * @return string The feed type, as defined in Feed.php */ public function getVersion(){ return $this->version; } // Wrapper functions ------------------------------------------------------ /** * Set the 'description' element of feed item * * @access public * @param string The content of 'description' or 'summary' element * @return self */ public function setDescription($description){ $tag = ($this->version == Feed::ATOM) ? 'summary' : 'description'; return $this->addElement($tag, $description); } /** * Set the 'content' element of the feed item * For ATOM feeds only * * @access public * @param string Content for the item (i.e., the body of a blog post). * @return self */ public function setContent($content){ if($this->version != Feed::ATOM){ die('The content element is supported in ATOM feeds only.'); } return $this->addElement('content', $content, array('type' => 'html')); } /** * Set the 'title' element of feed item * * @access public * @param string The content of 'title' element * @return self */ public function setTitle($title){ return $this->addElement('title', $title); } /** * Set the 'date' element of the feed item. * * The value of the date parameter can be either an instance of the * DateTime class, an integer containing a UNIX timestamp or a string * which is parseable by PHP's 'strtotime' function. * * @access public * @param DateTime|int|string Date which should be used. * @return self */ public function setDate($date){ if(!is_numeric($date)){ if($date instanceof DateTime){ $date = $date->getTimestamp(); } else{ $date = strtotime($date); if($date === false){ die('The given date string was not parseable.'); } } } else if($date < 0){ die('The given date is not an UNIX timestamp.'); } if($this->version == Feed::ATOM){ $tag = 'updated'; $value = date(\DATE_ATOM, $date); } else if($this->version == Feed::RSS2){ $tag = 'pubDate'; $value = date(\DATE_RSS, $date); } else{ $tag = 'dc:date'; $value = date("Y-m-d", $date); } return $this->addElement($tag, $value); } /** * Set the 'link' element of feed item * * @access public * @param string The content of 'link' element * @return \FeedWriter\Item */ public function setLink($link){ if($this->version == Feed::RSS2 || $this->version == Feed::RSS1){ $this->addElement('link', $link); } else{ $this->addElement('link', '', array('href' => $link)); $this->addElement('id', Feed::uuid($link, 'urn:uuid:')); } return $this; } /** * Attach a external media to the feed item. * Not supported in RSS 1.0 feeds. * * See RFC 4288 for syntactical correct MIME types. * * Note that you should avoid the use of more than one enclosure in one item, * since some RSS aggregators don't support it. * * @access public * @param string The URL of the media. * @param integer The length of the media. * @param string The MIME type attribute of the media. * @param boolean Specifies, if multiple enclosures are allowed * @return self * @link https://tools.ietf.org/html/rfc4288 */ public function addEnclosure($url, $length, $type, $multiple = true){ if($this->version == Feed::RSS1){ die('Media attachment is not supported in RSS1 feeds.'); } // the length parameter should be set to 0 if it can't be determined // see http://www.rssboard.org/rss-profile#element-channel-item-enclosure if(!is_numeric($length) || $length < 0){ die('The length parameter must be an integer and greater or equals to zero.'); } // Regex used from RFC 4287, page 41 if(!is_string($type) || preg_match('/.+\/.+/', $type) != 1){ die('type parameter must be a string and a MIME type.'); } $attributes = array('length' => $length, 'type' => $type); if($this->version == Feed::RSS2){ $attributes['url'] = $url; $this->addElement('enclosure', '', $attributes, false, $multiple); } else{ $attributes['href'] = $url; $attributes['rel'] = 'enclosure'; $this->addElement('atom:link', '', $attributes, false, $multiple); } return $this; } /** * Alias of addEnclosure, for backward compatibility. Using only this * method ensures that the 'enclosure' element will be present only once. * * @access public * @param string The URL of the media. * @param integer The length of the media. * @param string The MIME type attribute of the media. * @return self * @link https://tools.ietf.org/html/rfc4288 * @deprecated Use the addEnclosure method instead. * **/ public function setEnclosure($url, $length, $type){ return $this->addEnclosure($url, $length, $type, false); } /** * Set the 'author' element of feed item. * Not supported in RSS 1.0 feeds. * * @access public * @param string The author of this item * @param string Optional email address of the author * @param string Optional URI related to the author * @return self */ public function setAuthor($author, $email = null, $uri = null){ if($this->version == Feed::RSS1){ die('The author element is not supported in RSS1 feeds.'); } else if($this->version == Feed::RSS2){ if($email != null){ $author = $email . ' (' . $author . ')'; } $this->addElement('author', $author); } else if($this->version == Feed::ATOM){ $elements = array('name' => $author); // Regex from RFC 4287 page 41 if($email != null && preg_match('/.+@.+/', $email) == 1){ $elements['email'] = $email; } if($uri != null){ $elements['uri'] = $uri; } $this->addElement('author', $elements); } return $this; } /** * Set the unique identifier of the feed item * * On ATOM feeds, the identifier must begin with an valid URI scheme. * * @access public * @param string The unique identifier of this item * @param boolean The value of the 'isPermaLink' attribute in RSS 2 feeds. * @return self */ public function setId($id, $permaLink = false){ if($this->version == Feed::RSS2){ if(!is_bool($permaLink)){ die('The permaLink parameter must be boolean.'); } $permaLink = $permaLink ? 'true' : 'false'; $this->addElement('guid', $id, array('isPermaLink' => $permaLink)); } else if($this->version == Feed::ATOM){ // Check if the given ID is an valid URI scheme (see RFC 4287 4.2.6) // The list of valid schemes was generated from http://www.iana.org/assignments/uri-schemes // by using only permanent or historical schemes. $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'); $found = false; $checkId = strtolower($id); foreach($validSchemes as $scheme){ if(strrpos($checkId, $scheme . ':', -strlen($checkId)) !== false){ $found = true; break; } } if(!$found){ die("The ID must begin with an IANA-registered URI scheme."); } $this->addElement('id', $id, null, true); } else{ die('A unique ID is not supported in RSS1 feeds.'); } return $this; } } |