Overview

Namespaces

  • Alpha
    • Controller
      • Front
    • Exception
    • Model
      • Type
    • Task
    • Util
      • Backup
      • Cache
      • Code
        • Highlight
        • Metric
      • Config
      • Convertor
      • Email
      • Extension
      • Feed
      • File
      • Graph
      • Helper
      • Http
        • Filter
        • Session
      • Image
      • Logging
      • Search
      • Security
    • View
      • Renderer
        • Html
        • Json
      • Widget

Classes

  • ActiveRecordController
  • ArticleController
  • AttachmentController
  • CacheController
  • Controller
  • DEnumController
  • ExcelController
  • FeedController
  • GenSecureQueryStringController
  • ImageController
  • IndexController
  • InstallController
  • ListActiveRecordsController
  • LogController
  • LoginController
  • LogoutController
  • MetricController
  • PhpinfoController
  • RecordSelectorController
  • SearchController
  • SequenceController
  • TagController

Interfaces

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