Alpha Framework alpha--controller
[ class tree: alpha--controller ] [ index: alpha--controller ] [ all elements ]

Source for file ViewAttachment.php

Documentation is available at ViewAttachment.php

  1. <?php
  2.  
  3. // include the config file
  4. if(!isset($config)) {
  5.     require_once '../util/AlphaConfig.inc';
  6.     $config AlphaConfig::getInstance();
  7. }
  8.  
  9. require_once $config->get('sysRoot').'alpha/util/Logger.inc';
  10. require_once $config->get('sysRoot').'alpha/util/AlphaFileUtil.inc';
  11. require_once $config->get('sysRoot').'alpha/controller/AlphaController.inc';
  12. require_once $config->get('sysRoot').'alpha/controller/AlphaControllerInterface.inc';
  13. require_once $config->get('sysRoot').'alpha/exceptions/IllegalArguementException.inc';
  14. require_once $config->get('sysRoot').'alpha/view/AlphaView.inc';
  15.  
  16. /**
  17.  * 
  18.  * Controller used to view (download) an attachment file on an ArticleObject
  19.  * 
  20.  * @package alpha::controller
  21.  * @since 1.0
  22.  * @author John Collins <dev@alphaframework.org>
  23.  * @version $Id: ViewAttachment.php 1341 2011-03-17 15:02:02Z johnc $
  24.  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
  25.  * @copyright Copyright (c) 2011, John Collins (founder of Alpha Framework).
  26.  *  All rights reserved.
  27.  * 
  28.  *  <pre>
  29.  *  Redistribution and use in source and binary forms, with or
  30.  *  without modification, are permitted provided that the
  31.  *  following conditions are met:
  32.  * 
  33.  *  * Redistributions of source code must retain the above
  34.  *    copyright notice, this list of conditions and the
  35.  *    following disclaimer.
  36.  *  * Redistributions in binary form must reproduce the above
  37.  *    copyright notice, this list of conditions and the
  38.  *    following disclaimer in the documentation and/or other
  39.  *    materials provided with the distribution.
  40.  *  * Neither the name of the Alpha Framework nor the names
  41.  *    of its contributors may be used to endorse or promote
  42.  *    products derived from this software without specific
  43.  *    prior written permission.
  44.  *   
  45.  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  46.  *  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  47.  *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  48.  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  49.  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  50.  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  51.  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  52.  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  53.  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  54.  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  55.  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  56.  *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  57.  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  58.  *  </pre>
  59.  *  
  60.  */
  61. class ViewAttachment extends AlphaController implements AlphaControllerInterface{    
  62.     /**
  63.      * Trace logger
  64.      * 
  65.      * @var Logger 
  66.      * @since 1.0
  67.      */
  68.     private static $logger null;
  69.     
  70.     /**
  71.      * The constructor
  72.      * 
  73.      * @since 1.0
  74.      */
  75.     public function __construct({
  76.         self::$logger new Logger('ViewAttachment');
  77.         self::$logger->debug('>>__construct()');
  78.         
  79.         // ensure that the super class constructor is called, indicating the rights group
  80.         parent::__construct('Public');
  81.         
  82.         self::$logger->debug('<<__construct');
  83.     }
  84.     
  85.     /**
  86.      * Handle GET requests
  87.      * 
  88.      * @param array $params 
  89.      * @since 1.0
  90.      * @throws ResourceNotFoundException
  91.      */
  92.     public function doGET($params{
  93.         self::$logger->debug('>>doGET($params=['.var_export($paramstrue).'])');
  94.         
  95.         global $config;
  96.         
  97.         try {
  98.             if(isset($params['dir']&& isset($params['filename'])) {
  99.                 $filePath $params['dir'].'/'.$params['filename'];
  100.                 
  101.                 if(file_exists($filePath)) {
  102.                     self::$logger->info('Downloading the file ['.$params['filename'].'] from the folder ['.$params['dir'].']');
  103.                     
  104.                     $pathParts pathinfo($filePath);
  105.                     $mimeType AlphaFileUtil::getMIMETypeByExtension($pathParts['extension']);
  106.                     header('Content-Type: '.$mimeType);
  107.                     header('Content-Disposition: attachment; filename="'.$pathParts['basename'].'"');
  108.                     header('Content-Length: '.filesize($filePath));
  109.                     
  110.                     readfile($filePath);
  111.                     
  112.                     self::$logger->debug('<<doGET');
  113.                     exit;
  114.                 }else{
  115.                     self::$logger->error('Could not access article attachment file ['.$filePath.'] as it does not exist!');
  116.                     throw new IllegalArguementException('File not found');
  117.                 }
  118.             }else{
  119.                 self::$logger->error('Could not access article attachment as dir and/or filename were not provided!');
  120.                 throw new IllegalArguementException('File not found');
  121.             }
  122.         }catch(IllegalArguementException $e{
  123.             self::$logger->error($e->getMessage());
  124.             throw new ResourceNotFoundException($e->getMessage());
  125.         }
  126.         
  127.         self::$logger->debug('<<doGET');
  128.     }
  129.     
  130.     /**
  131.      * Handle POST requests
  132.      * 
  133.      * @param array $params 
  134.      */
  135.     public function doPOST($params{
  136.         self::$logger->debug('>>doPOST($params=['.var_export($paramstrue).'])');
  137.         
  138.         self::$logger->debug('<<doPOST');
  139.     }
  140. }
  141.  
  142. // now build the new controller if this file is called directly
  143. if ('ViewAttachment.php' == basename($_SERVER['PHP_SELF'])) {
  144.     $controller new ViewAttachment();
  145.     
  146.     if(!empty($_POST)) {            
  147.         $controller->doPOST($_POST);
  148.     }else{
  149.         $controller->doGET($_GET);
  150.     }
  151. }
  152.  
  153. ?>

Documentation generated on Thu, 17 Mar 2011 16:45:08 +0000 by phpDocumentor 1.4.3