      1: <?php
      2:
      3: // include the config file
      4: if(!isset($config)) {
      5:     require_once '../util/AlphaConfig.inc';
      6:     $config = AlphaConfig::getInstance();
      7:
      8:     require_once $config->get('app.root').'alpha/util/AlphaAutoLoader.inc';
      9: }
     10:
     11: /**
     12:  * Controller for viewing news feeds
     13:  *
     14:  * @package alpha::controller
     15:  * @since 1.0
     16:  * @author John Collins <dev@alphaframework.org>
     17:  * @version $Id: ViewFeed.php 1654 2013-03-08 17:13:14Z alphadevx $
     18:  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
     19:  * @copyright Copyright (c) 2013, John Collins (founder of Alpha Framework).
     20:  * All rights reserved.
     21:  *
     22:  * <pre>
     23:  * Redistribution and use in source and binary forms, with or
     24:  * without modification, are permitted provided that the
     25:  * following conditions are met:
     26:  *
     27:  * * Redistributions of source code must retain the above
     28:  *   copyright notice, this list of conditions and the
     29:  *   following disclaimer.
     30:  * * Redistributions in binary form must reproduce the above
     31:  *   copyright notice, this list of conditions and the
     32:  *   following disclaimer in the documentation and/or other
     33:  *   materials provided with the distribution.
     34:  * * Neither the name of the Alpha Framework nor the names
     35:  *   of its contributors may be used to endorse or promote
     36:  *   products derived from this software without specific
     37:  *   prior written permission.
     38:  *
     39:  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
     40:  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
     41:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     42:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     43:  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     44:  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     45:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     46:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     47:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     48:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     49:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     50:  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     51:  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     52:  * </pre>
     53:  *
     54:  */
     55: class ViewFeed extends AlphaController implements AlphaControllerInterface {
     56:     /**
     57:      * The name of the BO to render as a feed
     58:      *
     59:      * @var string
     60:      * @since 1.0
     61:      */
     62:     private $BOName;
     63:
     64:     /**
     65:      * The type of feed to render (RSS, RSS2 or Atom)
     66:      *
     67:      * @var string
     68:      * @since 1.0
     69:      */
     70:     private $type;
     71:
     72:     /**
     73:      * The title of the feed
     74:      *
     75:      * @var string
     76:      * @since 1.0
     77:      */
     78:     protected $title;
     79:
     80:     /**
     81:      * The description of the feed
     82:      *
     83:      * @var string
     84:      * @since 1.0
     85:      */
     86:     protected $description;
     87:
     88:     /**
     89:      * The BO to feed field mappings
     90:      *
     91:      * @var array
     92:      * @since 1.0
     93:      */
     94:     protected $fieldMappings;
     95:
     96:     /**
     97:      * The BO field name to sort the feed by (descending), default is OID
     98:      *
     99:      * @var string
    100:      * @since 1.0
    101:      */
    102:     private $sortBy = 'OID';
    103:
    104:     /**
    105:      * Trace logger
    106:      *
    107:      * @var Logger
    108:      * @since 1.0
    109:      */
    110:     private static $logger = null;
    111:
    112:     /**
    113:      * constructor to set up the object
    114:      *
    115:      * @since 1.0
    116:      */
    117:     public function __construct() {
    118:         self::$logger = new Logger('ViewFeed');
    119:         self::$logger->debug('>>__construct()');
    120:
    121:         global $config;
    122:
    123:         // ensure that the super class constructor is called, indicating the rights group
    124:         parent::__construct('Public');
    125:
    126:         self::$logger->debug('<<__construct');
    127:     }
    128:
    129:     /**
    130:      * Handle GET requests
    131:      *
    132:      * @param array $params
    133:      * @since 1.0
    134:      * @throws ResourceNotFoundException
    135:      */
    136:     public function doGET($params) {
    137:         self::$logger->debug('>>doGET($params=['.var_export($params, true).'])');
    138:
    139:         global $config;
    140:
    141:         try {
    142:             if (isset($params['bo'])) {
    143:                 $BOName = $params['bo'];
    144:             }else{
    145:                 throw new IllegalArguementException('BO not specified to generate feed!');
    146:             }
    147:
    148:             if (isset($params['type'])) {
    149:                 $type = $params['type'];
    150:             }else{
    151:                 throw new IllegalArguementException('No feed type specified to generate feed!');
    152:             }
    153:
    154:             $this->BOName = $BOName;
    155:             $this->type = $type;
    156:
    157:             $this->setup();
    158:
    159:             switch($type) {
    160:                 case 'RSS2':
    161:                     $feed = new RSS2($BOName, $this->title, str_replace('&', '&amp;', $_SERVER['REQUEST_URI']), $this->description);
    162:                     $feed->setFieldMappings($this->fieldMappings[0], $this->fieldMappings[1], $this->fieldMappings[2], $this->fieldMappings[3]);
    163:                 break;
    164:                 case 'RSS':
    165:                     $feed = new RSS($BOName, $this->title, str_replace('&', '&amp;', $_SERVER['REQUEST_URI']), $this->description);
    166:                     $feed->setFieldMappings($this->fieldMappings[0], $this->fieldMappings[1], $this->fieldMappings[2], $this->fieldMappings[3]);
    167:                 break;
    168:                 case 'Atom':
    169:                     $feed = new Atom($BOName, $this->title, str_replace('&', '&amp;', $_SERVER['REQUEST_URI']), $this->description);
    170:                     $feed->setFieldMappings($this->fieldMappings[0], $this->fieldMappings[1], $this->fieldMappings[2], $this->fieldMappings[3],
    171:                         $this->fieldMappings[4]);
    172:                     if($config->get('feeds.atom.author') != '')
    173:                         $feed->addAuthor($config->get('feeds.atom.author'));
    174:                 break;
    175:             }
    176:
    177:             // now add the twenty last items (from newest to oldest) to the feed, and render
    178:             $feed->loadBOs(20, $this->sortBy);
    179:             echo $feed->render();
    180:
    181:             // log the request for this news feed
    182:             $feedLog = new LogFile($config->get('app.file.store.dir').'logs/feeds.log');
    183:             $feedLog->writeLine(array($this->BOName, $this->type, date("Y-m-d H:i:s"), $_SERVER['HTTP_USER_AGENT'], $_SERVER['REMOTE_ADDR']));
    184:         }catch(IllegalArguementException $e) {
    185:             self::$logger->error($e->getMessage());
    186:             throw new ResourceNotFoundException($e->getMessage());
    187:         }
    188:
    189:         self::$logger->debug('<<doGet');
    190:     }
    191:
    192:     /**
    193:      * Method to handle POST requests
    194:      *
    195:      * @param array $params
    196:      * @since 1.0
    197:      */
    198:     public function doPOST($params) {
    199:         self::$logger->debug('>>doPOST($params=['.var_export($params, true).'])');
    200:
    201:         self::$logger->debug('<<doPOST');
    202:     }
    203:
    204:     /**
    205:      * setup the feed title, field mappings and description based on common BO types
    206:      */
    207:     protected function setup() {
    208:         self::$logger->debug('>>setup()');
    209:
    210:         global $config;
    211:
    212:         $bo = new $this->BOName;
    213:
    214:         if($bo instanceof ArticleObject) {
    215:             $this->title = 'Latest articles from '.$config->get('app.title');
    216:             $this->description = 'News feed containing all of the details on the latest articles published on '.$config->get('app.title').'.';
    217:             $this->fieldMappings = array('title', 'URL', 'description', 'created_ts', 'OID');
    218:             $this->sortBy = 'created_ts';
    219:         }
    220:
    221:         self::$logger->debug('<<setup');
    222:     }
    223: }
    224:
    225: // now build the new controller
    226: if(basename($_SERVER['PHP_SELF']) == 'ViewFeed.php') {
    227:     $controller = new ViewFeed();
    228:
    229:     if(!empty($_POST)) {
    230:         $controller->doPOST($_REQUEST);
    231:     }else{
    232:         $controller->doGET($_GET);
    233:     }
    234: }
    235:
    236: ?>