public static function checkExtBlackList($file, $black_list) { if (count($black_list) == 0) { return -1; } $fileArr = explode(chr(46), $file["name"]); $lastIndex = count($fileArr) - 1; $fileExt = chr(46) . $fileArr[$lastIndex]; if (!in_array(strtolower($fileExt), $black_list)) { return 1; } else { self::$error = "This file type is not allowed on this server."; return -2; } }
/** * adds a resource to the database. This method requires a FileUpload parameter and it * will take care of processing the upload file and so on. If the file is already in disk and we'd * like to add it, please check GalleryResources::addResourceFromDisk() * This method will also take care of extracting the metadata from the file and generating the * thumbnail in the required format, according to our configuration. * * @param ownerId * @param albumId * @param description * @param upload A FileUpload object * @see FileUpload * @see GalleryResources::addResourceFromDisk() * @return It will return one of the following constants: * - GALLERY_ERROR_RESOURCE_TOO_BIG * - GALLERY_ERROR_RESOURCE_FORBIDDEN_EXTENSION * - GALLERY_ERROR_QUOTA_EXCEEDED * - GALLERY_ERROR_ADDING_RESOURCE * - GALLERY_ERROR_UPLOADS_NOT_ENABLED * or the identifier of the resource that was just added if the operation succeeded. */ function addResource($ownerId, $albumId, $description, $upload) { // check if quotas are enabled, and if this file would make us go // over the quota if (GalleryResourceQuotas::isBlogOverResourceQuota($ownerId, $upload->getSize())) { return GALLERY_ERROR_QUOTA_EXCEEDED; } // first of all, validate the file using the // upload validator class. It can return // UPLOAD_VALIDATOR_ERROR_UPLOAD_TOO_BIG (-1) // or // UPLOAD_VALIDATOR_ERROR_FORBIDDEN_EXTENSION (-2) // in case the file is not valid. $uploadValidator = new UploadValidator(); $error = $uploadValidator->validate($upload); if ($error < 0) { return $error; } // get the metadata $getId3 = new GetID3(); $metadata = $getId3->analyze($upload->getTmpName()); // nifty helper method from the getid3 package getid3_lib::CopyTagsToComments($metadata); $resourceType = $this->_getResourceType($upload->getFileName(), $metadata); // set the flags $flags = 0; if ($resourceType == GALLERY_RESOURCE_IMAGE) { $flags = $flags | GALLERY_RESOURCE_PREVIEW_AVAILABLE; } $info = $this->_filterMetadata($metadata, $resourceType); // add the record to the database $fileName = $upload->getFileName(); $resourceId = $this->addResourceToDatabase($ownerId, $albumId, $description, $flags, $resourceType, $filePath, $fileName, $info); if (!$resourceId) { return false; } // and finally move the file to the right place in disk // move the file to disk $storage = new GalleryResourceStorage(); $resFile = $storage->storeUpload($resourceId, $ownerId, $upload); // if the file cannot be read, we will also remove the record from the // database so that we don't screw up $fileReadable = File::isReadable($resFile); if (!$resFile || $resFile < 0 || !$fileReadable) { // if something went wrong, we should not keep the record in the db $query = "DELETE FROM " . $this->getPrefix() . "gallery_resources WHERE id = {$resourceId}"; $this->Execute($query); return $resFile; } // and finally, we can generate the thumbnail only if the file is an image, of course :) if ($resourceType == GALLERY_RESOURCE_IMAGE) { $this->generateResourceThumbnail($resFile, $resourceId, $ownerId); $this->generateResourceMediumSizeThumbnail($resFile, $resourceId, $ownerId); } // return the id of the resource we just added return $resourceId; }