BEdita API doc | 4-cactus
  • Class
  • Tree
  • Deprecated
  • Download

Classes

  • Addon
  • AddressbookController
  • AddressbookShell
  • AdminController
  • Alias
  • Annotation
  • ApiAuthComponent
  • ApiBaseController
  • ApiFormatterComponent
  • ApiUploadComponent
  • ApiValidatorComponent
  • AppController
  • AppError
  • AppHelper
  • Application
  • AppModel
  • Area
  • AreasController
  • Audio
  • AuthenticationsController
  • BannedIp
  • BEAppModel
  • BEAppObjectModel
  • BeAuthComponent
  • BeAuthFacebookComponent
  • BeAuthGoogleComponent
  • BeAuthTwitterComponent
  • BeCallbackManager
  • BeConfigure
  • BeCustomPropertyComponent
  • BeditaAnnotationModel
  • BeditaBaseShell
  • BeditaCollectionModel
  • BeditaContentModel
  • BeditaExportFilter
  • BeditaImportFilter
  • BeditaObjectModel
  • BeditaProductModel
  • BeditaShell
  • BeditaSimpleObjectModel
  • BeditaSimpleStreamModel
  • BeditaStreamModel
  • BeEmbedFlashHelper
  • BeEmbedHtml5Helper
  • BeEmbedMediaHelper
  • BeErrorCode
  • BeExceptionHandler
  • BEFile
  • BeFileHandlerComponent
  • BeFormHelper
  • BeFrontHelper
  • BeHashComponent
  • BeHtmlHelper
  • BeLangTextComponent
  • BeLib
  • BeMailComponent
  • BEObject
  • BeObjectCache
  • BePaginatorToolbarHelper
  • BeSchema
  • BeSecurityComponent
  • BeSystem
  • BeSystemComponent
  • BeThumb
  • BeTimeHelper
  • BeToolbarHelper
  • BeTreeComponent
  • BeTreeHelper
  • BeUploadToObjComponent
  • BeurlHelper
  • BeVimeoComponent
  • BeYoutubeComponent
  • BuildFilterBehavior
  • CacheableBehavior
  • CacheShell
  • CallbackBehavior
  • CaptchaComponent
  • Caption
  • CaptionsBehavior
  • Card
  • CardsImportFilter
  • Category
  • CleanupDataTask
  • CleanupTask
  • Comment
  • CommentsController
  • CompactResultBehavior
  • Content
  • CreateIndexFieldsBehavior
  • DataShell
  • DataTransfer
  • DateItem
  • DbadminShell
  • DeleteDependentObjectBehavior
  • DeleteObjectBehavior
  • DeployShell
  • Document
  • DocumentsController
  • DumpModel
  • EditorialContentsShell
  • EditorNote
  • Event
  • EventLog
  • EventsController
  • FilesController
  • ForeignDependenceSaveBehavior
  • FrontendController
  • FrontendShell
  • GalleriesController
  • Gallery
  • GeoTag
  • GettextShell
  • Git
  • GravatarHelper
  • Group
  • HashJob
  • History
  • HomeController
  • Image
  • ImageInfoHelper
  • JsonExportFilter
  • JsonImportFilter
  • JsonView
  • LangText
  • Link
  • MailGroup
  • MailGroupCard
  • MailJob
  • MailLog
  • MailMessage
  • MailShell
  • MailTemplate
  • Mask
  • MediaShell
  • MigrateDumpModel
  • MigrateShell
  • Migration
  • MigrationBase
  • MigrationDateItemsShell
  • Module
  • ModulesController
  • ModuleShell
  • MultimediaController
  • NewsController
  • NewsletterController
  • NewsletterShell
  • NotifyBehavior
  • ObjectEditor
  • ObjectProperty
  • ObjectRelation
  • ObjectType
  • ObjectUser
  • PagesController
  • Permission
  • PermissionModule
  • PermsHelper
  • Product
  • Property
  • PropertyOption
  • PublicationShell
  • RedisShell
  • RelationCheck
  • RelationRepair
  • RelationShell
  • RelationStats
  • RemoveDummyNameBehavior
  • ResponseHandlerComponent
  • RestClientModel
  • Revision
  • RevisionObjectBehavior
  • SearchText
  • SearchTextSaveBehavior
  • Section
  • SectionDummy
  • SessionFilterComponent
  • SessionFilterHelper
  • ShortNews
  • SmartyView
  • SoapClientModel
  • SplitterSql
  • StatisticsController
  • Stream
  • StreamLog
  • StreamsController
  • Svn
  • TagsController
  • ThemeSmartyView
  • ThumbnailHelper
  • ThumbShell
  • TransactionComponent
  • TransactionFS
  • TranslationsController
  • Tree
  • TrHelper
  • User
  • UserProperty
  • UsersController
  • Utility
  • Version
  • Video
  • VimeoHelper
  • WebmarksController
  • XmlExportFilter
  • XmlImportFilter
  • XmlJsonConverter
  • XmlView
  • YoutubeHelper

Interfaces

  • ApiAuthInterface
  • MediaProviderInterface
  • UploadableInterface

Exceptions

  • BeditaAjaxException
  • BEditaAllowURLException
  • BeditaBadRequestException
  • BeditaConflictException
  • BEditaDeleteStreamObjException
  • BeditaException
  • BEditaFileExistException
  • BeditaForbiddenException
  • BeditaFrontAccessException
  • BeditaHashException
  • BEditaInfoException
  • BeditaInternalErrorException
  • BEditaIOException
  • BeditaLengthRequiredException
  • BeditaMailException
  • BEditaMediaProviderException
  • BeditaMethodNotAllowedException
  • BEditaMIMEException
  • BeditaNotFoundException
  • BeditaNotImplementedException
  • BeditaPublicationException
  • BeditaRuntimeException
  • BEditaSaveStreamObjException
  • BeditaServiceUnavailableException
  • BeditaUnauthorizedException
  • BEditaUploadPHPException
  • BEditaURLException
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 
<?php
/*-----8<--------------------------------------------------------------------
 *
 * BEdita - a semantic content management framework
 *
 * Copyright 2016 ChannelWeb Srl, Chialab Srl
 *
 * This file is part of BEdita: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * BEdita is distributed WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with BEdita (see LICENSE.LGPL).
 * If not, see <http://gnu.org/licenses/lgpl-3.0.html>.
 *
 *------------------------------------------------------------------->8-----
 */

App::import('Core', 'File');

/**
 * ApiUploadComponent class
 *
 * Used to handle upload via REST API
 *
 */
class ApiUploadComponent extends Object {

    /**
     * Controller instance
     *
     * @var Controller
     */
    public $controller = null;

    /**
     * List of components used
     *
     * @var array
     */
    public $components = array('BeFileHandler');

    /**
     * Upload quota configuration.
     *
     * Options available:
     *
     * - maxFileSize: max single file size supported (default 50 MB)
     * - maxSizeAvailable: max size available for each user (default 500 MB)
     * - maxFilesAllowed: max number of files uploadable for each user (default 500)
     *
     * @var array
     */
    protected $quota = array(
        'maxFileSize' => 52428800,  // 50 * 1024^2
        'maxSizeAvailable' => 524288000,  // 500 * 1024^2
        'maxFilesAllowed' => 500
    );

    /**
     * Initialize component (called before Controller::beforeFilter())
     *
     * @param Controller $controller
     * @return void
     */
    public function initialize(Controller $controller, array $settings = array()) {
        $this->controller = &$controller;

        $quotaConf = !empty($settings['quota']) ? $settings['quota'] : Configure::read('api.upload.quota');
        if (is_array($quotaConf)) {
            $this->quota($quotaConf);
        }

        BeLib::eventManager()->bind('Api.uploadQuota', array('Stream', 'apiUploadQuota'));
    }

    /**
     * Set quota configuration and return it.
     * If it's called without parameter return the current quota configuration.
     *
     * Replace `self::$quota` conf with the new one in `$quotaConf` passed.
     * If `$quotaConf` missing of some keys the related value already set are maintained
     *
     * @param array $quotaConf The quota configuration
     * @return array
     */
    public function quota(array $quotaConf = array()) {
        if (empty($quotaConf)) {
            return $this->quota;
        }

        $this->quota = $quotaConf + $this->quota;

        return $this->quota;
    }

    /**
     * Upload a file using local filesystem or delegating to Model::apiUpload() method if exists.
     *
     * After the file is moved to the right location
     * a row in hash_jobs is added with status "pending" and the hash string is returned.
     * That hash string must be used from client to associate a new object to the file uploaded
     *
     * @param string $originalFileName The target file name
     * @param string $objectType The object type to which the upload file refers
     * @return string
     * @throws BeditaInternalErrorException
     * @throws BeditaConflictException When file already exists
     */
    public function upload($originalFileName, $objectType) {
        $source = $this->source();
        $fileSize = $source->size();
        $originalFileName = rawurldecode($originalFileName);
        $safeFileName = $this->BeFileHandler->buildNameFromFile($originalFileName);
        $mimeType = ClassRegistry::init('Stream')->getMimeType($source->pwd(), $safeFileName);
        $hashFile = $this->BeFileHandler->getHashFile($source->pwd());

        $this->controller->ApiValidator->checkUploadable(
            $objectType,
            compact('fileSize', 'mimeType', 'originalFileName')
        );

        $this->checkQuota($fileSize);

        $objectTypeClass = Configure::read('objectTypes.' . $objectType . '.model');
        $model = ClassRegistry::init($objectTypeClass);
        if ($model instanceof UploadableInterface) {
            $targetPath = $model->apiUpload($source, array(
                'fileName' => $safeFileName,
                'hashFile' => $hashFile,
                'user' => $this->controller->ApiAuth->identify()
            ));
            $source->delete();
        } else {
            $streamId = $this->BeFileHandler->hashFileExists($hashFile);
            if ($streamId !== false) {
                throw new BeditaConflictException('The file already exists in /objects/' . $streamId);
            }
            $targetPath = $this->put($source, $safeFileName);
        }

        if (empty($targetPath)) {
            throw new BeditaInternalErrorException('Error uploading file');
        }

        return $this->generateToken(array(
            'uri' => $targetPath,
            'name' => pathinfo($targetPath, PATHINFO_BASENAME),
            'mime_type' => $mimeType,
            'file_size' => $fileSize,
            'original_name' => $originalFileName,
            'hash_file' => $hashFile,
            'object_type' => $objectType
        ));
    }

    /**
     * Given the file size of file to upload
     * it checks if the upload quota configuration is exceeded.
     *
     * @param int $fileSize The file size of file to upload
     * @return void
     */
    public function checkQuota($fileSize) {
        if ($fileSize > $this->quota['maxFileSize']) {
            $megaByte = round($this->quota['maxFileSize']/(1024*1024), 2);
            throw new BeditaBadRequestException('File size exceeds the maximum allowed (' . $megaByte . ' MB)', array(
                'errorCode' => array(
                    'UPLOAD_MAX_FILESIZE_EXCEEDED' => array(
                        'max_file_size' => $this->quota['maxFileSize'],
                        'file_size' => $fileSize
                    )
                )
            ));
        }

        $objecTypesQuota = $this->quotaUsed();
        if ($objecTypesQuota === false) {
            throw new BeditaInternalErrorException('Error calculating the quota available');
        }

        // init total quota used with file to upload
        $quotaUsed = array('size' => (int)$fileSize, 'number' => 1);

        foreach ($objecTypesQuota as $type => $data) {
            $quotaUsed['size'] += (int)$data['size'];
            $quotaUsed['number'] += (int)$data['number'];
        }

        if ($quotaUsed['size'] > $this->quota['maxSizeAvailable']) {
            $megaByte = round($this->quota['maxSizeAvailable']/(1024*1024), 2);
            throw new BeditaForbiddenException('Allowed quota of ' . $megaByte . ' MB exceeded', array(
                'errorCode' => array(
                    'UPLOAD_QUOTA_EXCEEDED' => array(
                        'quota_available' => $this->quota['maxSizeAvailable'],
                        'quota_used' => $quotaUsed['size'] - $fileSize,
                        'file_size' => $fileSize
                    )
                )
            ));
        }

        if ($quotaUsed['number'] > $this->quota['maxFilesAllowed']) {
            throw new BeditaForbiddenException('Allowed number of ' . $this->quota['maxFilesAllowed'] . ' files exceeded.', array(
                'errorCode' => array(
                    'UPLOAD_FILES_LIMIT_EXCEEDED' => array(
                        'max_files_allowed' => $this->quota['maxFilesAllowed']
                    )
                )
            ));
        }
    }

    /**
     * Return the upload quota occupied divided by object type.
     *
     * Event triggered:
     *
     * - Api.uploadQuota: pass to listener a list of objects uploadable and the authenticated user
     *
     *   The listener should be calculate the size occupied (in bytes) from all files linked to a specific object type
     *   and the total number of files.
     *   It should be return an array with object types as keys and the above information.
     *   For example:
     *
     *   ```
     *   array(
     *       'image' => array(
     *           'size' => 12345678,
     *           'number' => 256
     *        ),
     *        'video' => array(
     *           'size' => 123456789,
     *           'number' => 15
     *        ),
     *   )
     *   ```
     *
     *   The listener should be merge its results with `$event->result` to avoid to delete other results
     *   calculated from another listener.
     *
     *   If the listener returns false or stop the event propagation the method return false.
     *
     * @return array|false It returns false if the quota calculation fails
     */
    public function quotaUsed() {
        $eventData = array(
            'uploadableObjects' => $this->controller->ApiValidator->uploadableObjects(),
            'user' => $this->controller->ApiAuth->identify()
        );
        $event = BeLib::eventManager()->trigger('Api.uploadQuota', $eventData);

        if ($event->result === false || !empty($event->stopped)) {
            return false;
        }

        return is_array($event->result) ? $event->result : array();
    }

    /**
     * Read from php://input, put the content in a temporary file and return the File instance
     *
     * @return File
     * @throws BeditaBadRequestException, BeditaInternalErrorException, BeditaLengthRequiredException
     */
    public function source() {
        $contentLength = env('CONTENT_LENGTH');
        if (empty($contentLength)) {
            throw new BeditaLengthRequiredException('Missing or invalid Content-Length in request headers');
        }

        $inputData = file_get_contents('php://input');
        if (empty($inputData)) {
            throw new BeditaBadRequestException('Missing file to upload');
        }

        $tmpFileName = tempnam(sys_get_temp_dir(), 'bedita-api-upload-');
        if ($tmpFileName === false) {
            throw new BeditaInternalErrorException('Error creating temporary file');
        }

        $source = new File($tmpFileName);
        if ($source->write($inputData) === false) {
            $source->delete();
            throw new BeditaInternalErrorException('Error writing input data in temporary file');
        }
        clearstatcache();

        if ($source->size() != $contentLength) {
            throw new BeditaBadRequestException('Content-Length header does not match file size');
        }

        return $source;
    }

    /**
     * Copy the source file in a target path obtained starting from `$targetName`
     * and returning the target path.
     * Once the file is successfully copied it is removed.
     *
     * @param File $source The source File instance
     * @param string $targetName The file target name
     * @return string
     * @throws BeditaInternalErrorException
     */
    public function put(File $source, $targetName) {
        $targetPath = $this->BeFileHandler->getPathTargetFile($targetName);

        if ($this->BeFileHandler->putFile($source->pwd(), $targetPath) === false) {
            throw new BeditaInternalErrorException('Error during upload operation');
        }
        $source->delete();

        return $targetPath;
    }

    /**
     * Save an upload token in hash_jobs table and return it.
     *
     * @throws BeditaInternalErrorException
     * @param array $params An array of parameters to save in `hash_jobs.params`
     * @return string
     */
    protected function generateToken(array $params = array()) {
        $user = $this->controller->ApiAuth->identify();
        $hashJob = ClassRegistry::init('HashJob');
        $uploadToken = $hashJob->generateHash(true);
        $data = array(
            'service_type' => 'api_upload',
            'user_id' => $user['id'],
            'hash' => $uploadToken,
            'expired' => date('Y-m-d H:i:s', strtotime('+30 minutes')),
        );
        $data += $params;
        if (!$hashJob->save($data)) {
            throw new BeditaInternalErrorException('Error during upload operation');
        }

        return $uploadToken;
    }

    /**
     * Given an upload token it returns the related data
     *
     * @param string $uploadToken The upload token
     * @param string $objectType The object type related to the upload operation
     * @return array
     */
    public function uploadedFileData($uploadToken, $objectType) {
        $uploadData = $this->validateToken($uploadToken);

        if ($objectType != $uploadData['object_type']) {
            throw new BeditaBadRequestException('upload_token refers to different object type');
        }

        $uploadData = array_intersect_key($uploadData, array_flip(
            array('uri', 'name', 'mime_type', 'file_size', 'original_name', 'hash_file')
        ));

        $objectTypeClass = Configure::read('objectTypes.' . $objectType . '.model');
        $model = ClassRegistry::init($objectTypeClass);
        if ($model instanceof UploadableInterface) {
            return $model->apiUploadTransformData($uploadData);
        }

        if ($objectType == 'image') {
            $this->BeFileHandler->setImageData($uploadData);
        }

        return $uploadData;
    }

    /**
     * Validate an `$uploadToken` and return data related
     *
     * @param string $uploadToken The upload token to validate
     * @return array
     * @throws BeditaBadRequestException When the token doesn't exists or is not valid anymore
     */
    public function validateToken($uploadToken) {
        $user = $this->controller->ApiAuth->identify();
        $hashJob = ClassRegistry::init('HashJob');
        $hashRow = $hashJob->find('first', array(
            'conditions' => array(
                'service_type' => 'api_upload',
                'hash' => $uploadToken,
                'user_id' => $user['id']
            )
        ));

        if (empty($hashRow)) {
            throw new BeditaBadRequestException('upload_token ' . $uploadToken . ' not exists');
        }

        if ($hashRow['HashJob']['status'] != 'pending') {
            throw new BeditaBadRequestException('Invalid upload_token. Its status is ' . $hashRow['HashJob']['status']);
        }

        return $hashRow['HashJob'];
    }

    /**
     * Remove `$uploadToken`
     *
     * @param string $uploadToken The upload token to remove
     * @return bool
     */
    public function removeToken($uploadToken) {
        if (empty($uploadToken)) {
            return false;
        }

        $user = $this->controller->ApiAuth->identify();
        $hashJob = ClassRegistry::init('HashJob');
        $hashId = $hashJob->field('id', array(
            'service_type' => 'api_upload',
            'hash' => $uploadToken,
            'user_id' => $user['id']
        ));

        if (!$hashId) {
            return false;
        }

        return $hashJob->delete($hashId);
    }
}
BEdita API doc | 4-cactus API documentation generated by ApiGen