Advertisement
Guest User

lib/RSS2Writer.php

a guest
Jan 27th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 18.92 KB | None | 0 0
  1.     <?php
  2.         //--------------------------------------------------------------------------------
  3.         //  RSS2Writer (v2.1) and sample controller
  4.         //
  5.         //  (c) Copyright Daniel Soutter
  6.         // 
  7.         //  Blog: http://www.code-tips.com/
  8.         // 
  9.         //  This script may be used or developed at your own risk as long as it is
  10.         //  referenced appropriately.
  11.         //
  12.         //  This script may be used or developed at your own risk as long as it is
  13.         //  referenced appropriately.
  14.         //  Please post any comments or suggestions for improvement to
  15.         //  my blog (above).
  16.         //
  17.         //  For technical information about this Php RSS2Writer (v2.1) class, see:
  18.         //  http://www.web-resource.org/web-programming/free-php-scripts/RSS2Writer/
  19.         //
  20.         //  For usage instructions for the Php RSS2Writer (v2.1) class, see:
  21.         //  http://www.code-tips.com/2010/01/php-rss2writer-v20-generate-rss-20-feed.html
  22.         //--------------------------------------------------------------------------------
  23.  
  24.     class RSS2Writer
  25.     {
  26.         //Declare variables
  27.         var $xml;               // Used to store the rss xml
  28.         var $indent;
  29.         var $useCDATA = false;
  30.        
  31.         var $feedData = Array("title" => "", "description" => "", "link" => "");
  32.        
  33.         var $channelData = Array();
  34.         var $channelCategories = Array();
  35.         var $channelImage = null;
  36.         var $channelCloud = null;
  37.        
  38.        
  39.         var $itemsArray = Array();
  40.         var $nsArray = Array();
  41.        
  42.        
  43.         /*********************************************************************************
  44.         * Class Constructor
  45.         *
  46.         * Description:  Creates an instance of the XMLWriter and starts an xml 1.0
  47.         *               document.  Starts required RSS 2.0 elements (rss, channel)
  48.         *  
  49.         * Paramaters:   String $title           -   Channel title
  50.         *               String $description     -   Channel description
  51.         *               String $link            -   link to channel (unique)
  52.         *               Int $indent             -   Xml indent level
  53.         *               Boolean $useDATA        -   use CDATA for feed title, content
  54.         *
  55.         * Returns:      Void
  56.         **********************************************************************************/
  57.         function __construct($title, $description, $link, $indent = 6, $useCDATA = false)   //Constructor
  58.         {
  59.        
  60.             $this->feedData['title'] = $title;
  61.             $this->feedData['description'] = $description;
  62.             $this->feedData['link'] = $link;
  63.             $this->indent = $indent;
  64.             $this->feedData['useCDATA'] = $useCDATA;
  65.         }
  66.  
  67.         function addNamespace($ns, $url) {
  68.             $this->nsArray[$ns] = $url;
  69.         }
  70.        
  71.         /*********************************************************************************
  72.         * function addItem
  73.         *
  74.         * Description:  Add an item to the feed.
  75.         *  
  76.         * Paramaters:   String $title           -   Item title
  77.         *               String $description     -   Item description
  78.         *               String $link            -   link to item (unique)
  79.         *               2D Array $optionalElements[][]     
  80.                                                 -    Array[n] = Array("elementName" => (String), "value" => (String))
  81.         *
  82.         * Returns:      Void
  83.         **********************************************************************************/
  84.         function addItem($title = null, $description = null, $link = null, $guid = null)
  85.         {
  86.             if($guid == null) {
  87.                 $guid = $link;
  88.             }
  89.             $this->itemsArray[] = Array("title" => $title, "description" => $description, "link" => $link, "guid" => $guid, "optionalElements" => Array(), "itemCategories" => Array());
  90.         }
  91.  
  92.        
  93.        
  94.         /*********************************************************************************
  95.         * function addElement (Optional)
  96.         *
  97.         * Description:  Generic function to add any an Optional element to the Chanel
  98.         *               or most recent item
  99.         *  
  100.         * Paramaters:   String $elementName         -   Name of the element to add
  101.         *               String $val                 -   Value of the element
  102.         *               2D Array: $attributes[n]    -   Array(AttributeName, AttributeValue)
  103.         *
  104.         * Returns:      Void
  105.         **********************************************************************************/
  106.         function addElement($elementName, $val = null, $attributes = Array()){
  107.        
  108.             if (count($this->itemsArray) > 0)
  109.             {
  110.                 //Add to most recent item
  111.                 $this->itemsArray[count($this->itemsArray)-1]['optionalElements'][] = Array("elementName" => $elementName, "value" => $val, "attributes" => $attributes);
  112.             }
  113.             else
  114.             {
  115.                 //Add to channel
  116.                 $this->channelData[] = Array("elementName" => $elementName, "value" => $val, "attributes" => $attributes);
  117.             }
  118.  
  119.         }
  120.        
  121.        
  122.         /*********************************************************************************
  123.         * function addCategory
  124.         *
  125.         * Description:  Generic function to add categories to the channel and/or items.
  126.         *               Call this function before adding items to the feed
  127.         *               to add categories to the channel.  Call after adding an item
  128.         *               to assign categories/tags to individual feed items.
  129.         *
  130.         *               You can add multiple categories to a channel or item.  This
  131.         *               function will need to be called once to add each category.
  132.         *  
  133.         * Paramaters:   Array $categories [][]  -   Multidimensional array of categories
  134.         *                                           and optional domain.  Array details:
  135.         *                                               [n][0] = category name,
  136.         *                                               [n][1] = domain (or null)
  137.         * Returns:      Void
  138.         **********************************************************************************/
  139.         function addCategory($categoryName, $domain = null) //
  140.         {
  141.             if (count($this->itemsArray) > 0)
  142.             {
  143.                 //Add category to most recent item
  144.                 $this->itemsArray[count($this->itemsArray)-1]['itemCategories'][] = Array($categoryName, $domain);
  145.             }
  146.             else
  147.             {
  148.                 //Add category to Channel
  149.                 $this->channelCategories[] = Array($categoryName, $domain);
  150.             }
  151.  
  152.         }
  153.        
  154.        
  155.         /*********************************************************************************
  156.         * function channelImage (Optional)
  157.         *
  158.         * Description:  Add an image to the channel.
  159.         *              
  160.         *  
  161.         * Paramaters:   String $title           -   Channel title
  162.         *               String $link            -   link to send to if clicked
  163.         *               String $url             -   url to image
  164.         *               String $width           -   image width
  165.         *               String $height          -   image height
  166.         *
  167.         * Returns:      Void
  168.         **********************************************************************************/
  169.         function channelImage($title, $link, $url, $width, $height) //All Strings
  170.         {
  171.             $this->channelImage = Array($title, $link, $url, $width, $height);
  172.  
  173.         }
  174.        
  175.        
  176.         /*********************************************************************************
  177.         * function channelCloud (Optional)
  178.         *
  179.         * Description:  Add cloud connectivity settings to the channel
  180.         *              
  181.         *  
  182.         * Paramaters:   String $domain          -   cloud domain/server
  183.         *               String $port            -   cloud port
  184.         *               String $path            -   path to feed data
  185.         *               String $regProcedure
  186.         *               String $protocol   
  187.         *
  188.         * Returns:      Void
  189.         **********************************************************************************/
  190.         function channelCloud($domain, $port = '80', $path, $regProcedure = 'pingMe', $protocol = 'soap')
  191.         {
  192.             $this->channelCloud = Array($domain, $port, $path, $regProcedure, $protocol);
  193.  
  194.         }
  195.            
  196.        
  197.         /*********************************************************************************
  198.         * function getXML
  199.         *
  200.         * Description:  Generate and return the full rss feed xml
  201.         *  
  202.         * Paramaters:   none
  203.         *
  204.         * Returns:      Returns the rss feed xml as a String
  205.         **********************************************************************************/
  206.         function getXML()
  207.         {  
  208.        
  209.             //Set the default timezone
  210.             @date_default_timezone_set("GMT");
  211.            
  212.             //Create the xml write object
  213.             $writer = new XMLWriter();
  214.            
  215.             //XMLWriter Output method:
  216.             //------------------------------------------------------------------------------------------
  217.             $writer->openMemory();                  //  Xml stored in memory (store in variable, output
  218.                                                     //  to file, print/echo to user, etc.
  219.                                                    
  220.             //$this->$writer->openURI('php://output');      //  Send xml to browser/user
  221.             //-----------------------------------------------------------------------------------------
  222.        
  223.            
  224.             //XML Version
  225.             $writer->startDocument('1.0', 'UTF-8');
  226.            
  227.             //Indent level
  228.             $writer->setIndent($this->indent);
  229.  
  230.             //Create first element / main block (Xml type - RSS 2.0)
  231.             $writer->startElement('rss');
  232.             //Start RSS--------------------------------------------------------------------------------
  233.             //*****************************************************************************************
  234.             //RSS attribute(s)
  235.             $writer->writeAttribute('version', '2.0');  
  236.             foreach($this->nsArray as $ns => $url) {
  237.                 $writer->writeAttribute('xmlns:'.$ns, $url);
  238.             }
  239.            
  240.             $writer->startElement("channel");
  241.             //Start Channel------------------------------------------------------------------------
  242.            
  243.             //Required Channel Elements
  244.             //---------------------------------------------------------
  245.             $writer->writeElement('title', $this->feedData{'title'});
  246.             $writer->writeElement('description', $this->feedData['description']);
  247.             $writer->writeElement('link', $this->feedData['link']);
  248.            
  249.            
  250.            
  251.             //Optional Channel Elements
  252.             //---------------------------------------------------------
  253.  
  254.        
  255.             foreach ($this->channelCategories as $category)
  256.             {
  257.                 //Category block
  258.                 $writer->startElement('category');
  259.                     if($category[1] != null) //category has an associated domain
  260.                         $writer->writeAttribute('domain', $category[1]);
  261.                     $writer->text($category[0]);  //Category Name
  262.                 $writer->endElement();
  263.             }
  264.  
  265.            
  266.             if($this->channelCloud != null)
  267.             {
  268.                 //Cloud block - Allow registration with a cloud to recieve notification of feed updates
  269.                
  270.                 $writer->startElement('cloud');
  271.                     $writer->writeAttribute('domain', $this->channelCloud[0]);
  272.                     $writer->writeAttribute('port', $this->channelCloud[1]);
  273.                     $writer->writeAttribute('path', $this->channelCloud[2]);
  274.                     $writer->writeAttribute('registerProcedure', $this->channelCloud[3]);
  275.                     $writer->writeAttribute('protocol', $this->channelCloud[4]);
  276.                 $writer->endElement();
  277.             }
  278.  
  279.            
  280.             if($this->channelImage != null)
  281.             {
  282.                 //Channel Image (Optional)
  283.                
  284.                 $writer->startElement('image');
  285.                     $writer->writeElement('title', $this->channelImage[0]);
  286.                     $writer->writeElement('link', $this->channelImage[1]);
  287.                     $writer->writeElement('url', $this->channelImage[2]);
  288.                     $writer->writeElement('width', $this->channelImage[3]);
  289.                     $writer->writeElement('height', $this->channelImage[4]);
  290.                 $writer->endElement();
  291.             }
  292.            
  293.            
  294.  
  295.             foreach ($this->channelData as $element)
  296.             {
  297.                 //Other Optional Elements
  298.                 $writer->startElement($element['elementName']);
  299.                
  300.                 foreach ($element['attributes'] as $attribute)
  301.                     $writer->writeAttribute($attribute[0], $attribute[1]);
  302.                    
  303.                 if ($element['value'] != null)
  304.                     $writer->text($element['value']);  //Element Value
  305.                
  306.                 $writer->endElement();
  307.  
  308.             }
  309.  
  310.            
  311.             //Output the items
  312.             foreach ($this->itemsArray as $item)
  313.             {
  314.                 $writer->startElement("item");
  315.                 //Start Item-----------------------------------------------------------------------
  316.                
  317.                 if($this->useCDATA)
  318.                 {
  319.                     /*=============Changes By abasit83 v2.1 ===============*/
  320.                     $writer->startElement("title");
  321.                     $writer->writeCData($item['title']);
  322.                     $writer->endElement(); 
  323.                    
  324.                     $writer->startElement("link"); 
  325.                     $writer->writeCData($item['link']);
  326.                     $writer->endElement(); 
  327.                    
  328.                     $writer->startElement("guid"); 
  329.                     $writer->writeCData($item['guid']);
  330.                     $writer->endElement(); 
  331.                     /*=============END Changes By abasit83===============*/
  332.                 }
  333.                 else
  334.                 {
  335.                     $writer->writeElement('title', $item['title']);
  336.                     $writer->writeElement('link', $item['link']);
  337.                     $writer->writeElement('guid', $item['guid']);
  338.                 }
  339.                                                
  340.                 foreach ($item['optionalElements'] as $element)
  341.                 {
  342.                     /*=============Changes By kgraefe v2.1 ================*/
  343.                     //$writer->writeElement($element['elementName'], $element['value']);
  344.                     $writer->startElement($element['elementName']);
  345.                    
  346.                     foreach ($element['attributes'] as $attribute)
  347.                         $writer->writeAttribute($attribute[0], $attribute[1]);
  348.                        
  349.                     if ($element['value'] != null)
  350.                         $writer->text($element['value']);  //Element Value
  351.                    
  352.                     $writer->endElement();
  353.                     /*=============END Changes By kgraefe==================*/
  354.                 }
  355.                
  356.                 foreach ($item['itemCategories'] as $category)
  357.                 {
  358.                     //Category block
  359.                     $writer->startElement('category');
  360.                         if($category[1] != null) //category has an associated domain
  361.                             $writer->writeAttribute('domain', $category[1]);
  362.                         $writer->text($category[0]);  //Category Name
  363.                     $writer->endElement();
  364.                 }
  365.                
  366.                 //Item Content
  367.                 if($this->useCDATA)
  368.                 {
  369.                     /*=============Changes By abasit83 v2.1===============*/
  370.                     $writer->startElement("description");  
  371.                     $writer->writeCData($item['description']);
  372.                     $writer->endElement(); 
  373.                     /*=============END Changes By abasit83===============*/
  374.                 }
  375.                 else
  376.                 {
  377.                     $writer->writeElement('description', $item['description']);
  378.                 }
  379.                 $writer->endElement();
  380.                 //End Item ------------------------------------------------------------------------
  381.             }
  382.        
  383.             /*
  384.             $writer->startElement('atom:link');
  385.                 $writer->writeAttribute('href', $this->feedData['link']);
  386.                 $writer->writeAttribute('rel', 'self');
  387.                 $writer->writeAttribute('type', 'application/rss+xml');
  388.             $writer->endElement();
  389.             */
  390.        
  391.             $writer->endElement();
  392.             //End channel -------------------------------------------------------------------------
  393.                
  394.             // End rss
  395.             $writer->endElement();
  396.             //-----------------------------------------------------------------------------------------
  397.             //*****************************************************************************************
  398.  
  399.             //End Xml Document
  400.             $writer->endDocument();
  401.  
  402.             $this->xml = $writer->outputMemory(true);
  403.  
  404.             return $this->xml;
  405.  
  406.         }
  407.        
  408.        
  409.         /*********************************************************************************
  410.         * function getXMLFiltered: Comming in the next version (3.0)
  411.         *
  412.         * Description:  generates and returns the rss feed xml filtered by the categories passed to the function.
  413.         *               The resulting RSS feed will only contain items which have one or more of the
  414.         *               specified categories.
  415.         *  
  416.         * Paramaters:   $categories: Array("category_name1", "category_name2", "category_nam3")
  417.         *
  418.         * Returns:      Returns the rss feed xml as a String
  419.         **********************************************************************************/
  420.         function getXMLFiltered($categories)
  421.         {  
  422.        
  423.         }
  424.        
  425.  
  426.         /*********************************************************************************
  427.         * function writeToFile
  428.         *
  429.         * Description:  Writes the generated rss xml to a file
  430.         *  
  431.         * Paramaters:   String $fileName        -   Filename to save the rss feed xml
  432.         *               Array $categories (Optional) - write a filtered RSS Feed to a file
  433.         *
  434.         * Returns:      Void
  435.         **********************************************************************************/
  436.         function writeToFile($fileName, $categories = null)
  437.         {
  438.             $this->closeDocument();
  439.            
  440.             $fh = fopen($fileName, 'w') or die("can't open file");
  441.            
  442.             if(!$categories == null)
  443.                 fwrite($fh, $this->getXML());
  444.             else
  445.             {
  446.                 fwrite($fh, $this->getXML());
  447.                 //fwrite($fh, $this->getXMLFiltered($categories));
  448.             }
  449.            
  450.             fclose($fh);
  451.         }
  452.        
  453.        
  454.        
  455.        
  456.        
  457.        
  458.        
  459.         //  Note:
  460.         //--------------------------------------------------------------------------------
  461.         //  The following functions are no longer in use.  They remain in the class for
  462.         //  cases where they may be used, but can be removed if not required.  Any versions
  463.         //  of the RSS2Writer class published after this one (v2.0) will not include the
  464.         //  functions below.
  465.         //--------------------------------------------------------------------------------
  466.        
  467.         /*********************************************************************************
  468.         * function closeItem - No longer in use
  469.         *
  470.         * Description:  Closes an item element only if the current has been left open.
  471.         *  
  472.         * Paramaters:   none
  473.         *
  474.         * Returns:      Void
  475.         **********************************************************************************/
  476.         function closeItem()
  477.         {
  478.             if($this->itemOpen)
  479.             {
  480.                 $this->xml .= '</item>
  481.    ';//end item tag with new line
  482.                 $this->itemOpen = false;
  483.             }
  484.         }
  485.        
  486.        
  487.        
  488.         /*********************************************************************************
  489.         * private function closeDocument - No longer in use
  490.         *
  491.         * Description:  Closes the Channel and rss elements as well as the document
  492.         *  
  493.         * Paramaters:   none
  494.         *
  495.         * Returns:      Void
  496.         **********************************************************************************/
  497.         private function closeDocument()
  498.         {
  499.             //Create the xml write object
  500.             $writer = new XMLWriter();
  501.             $writer->openMemory();
  502.             $writer->setIndent(4);
  503.            
  504.             // Start the xml elements which requiring closing (allow endElement() function to work)
  505.             $writer->startElement('rss');
  506.             $writer->startElement('channel');
  507.             $writer->text('.');
  508.            
  509.             //Flush the current xml to remove start tags, but allow correct elements to be closed.
  510.             $writer->flush();
  511.            
  512.             $writer->endElement();
  513.             //End channel -------------------------------------------------------------------------
  514.                
  515.             // End rss
  516.             $writer->endElement();
  517.             //-----------------------------------------------------------------------------------------
  518.             //*****************************************************************************************
  519.  
  520.             //End Xml Document
  521.             $writer->endDocument();
  522.  
  523.             //$writer->flush();
  524.             $this->xml .= $writer->outputMemory(true);
  525.         }
  526.        
  527.        
  528.     }
  529.     ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement