Example #1
0
 public function onBeforeFileAction(MOXMAN_Core_FileActionEventArgs $args)
 {
     switch ($args->getAction()) {
         case MOXMAN_Core_FileActionEventArgs::COPY:
         case MOXMAN_Core_FileActionEventArgs::ADD:
             if (!isset($args->getData()->thumb)) {
                 if (MOXMAN_Core_FileActionEventArgs::ADD) {
                     $file = $args->getFile();
                 } else {
                     $file = $args->getTargetFile();
                 }
                 $parentFile = $file->getParentFile();
                 // If not set, probably a folder?
                 if (isset($args->getData()->fileSize)) {
                     $fileSize = $args->getData()->fileSize;
                 } else {
                     $fileSize = 0;
                 }
                 // Recalculate quota after uploading a new file
                 $dirs = MOXMAN::getUserStorage()->get("quota.dirs", new stdClass());
                 $this->recalcQuota($parentFile, $dirs);
                 MOXMAN::getUserStorage()->put("quota.dirs", $dirs);
                 $quotaSize = $this->parseSize($file->getConfig()->get("quota.size", "-1"));
                 // Check if size of the root directory exceeds the configured max size
                 $rootFile = $file->getFileSystem()->getRootFile();
                 if (isset($dirs->{$rootFile->getPublicPath()}) && $dirs->{$rootFile->getPublicPath()}->dsize + $fileSize > $quotaSize) {
                     throw new MOXMAN_Exception("Quota exceeded when adding file: " . $file->getPublicPath() . " (" . $this->formatSize($dirs->{$rootFile->getPublicPath()}->dsize + $fileSize) . " > " . $this->formatSize($quotaSize) . ").");
                 }
             }
             break;
     }
 }
Example #2
0
 public function onFileAction(MOXMAN_Core_FileActionEventArgs $args)
 {
     if ($args->isAction("add") && (!isset($args->getData()->thumb) || !$args->getData()->thumb)) {
         $this->add($args->getFile()->getPublicPath());
     }
     if ($args->isAction("delete")) {
         $this->remove((object) array("paths" => array($args->getFile()->getPublicPath())));
     }
 }
 /**
  * Executes the command logic with the specified RPC parameters.
  *
  * @param Object $params Command parameters sent from client.
  * @return Object Result object to be passed back to client.
  */
 public function execute($params)
 {
     $file = MOXMAN::getFile($params->path);
     $url = parse_url($params->url);
     $config = $file->getConfig();
     if ($config->get('general.demo')) {
         throw new MOXMAN_Exception("This action is restricted in demo mode.", MOXMAN_Exception::DEMO_MODE);
     }
     if ($file->exists()) {
         throw new MOXMAN_Exception("To file already exist: " . $file->getPublicPath(), MOXMAN_Exception::FILE_EXISTS);
     }
     if (!$file->canWrite()) {
         throw new MOXMAN_Exception("No write access to file: " . $toFile->getPublicPath(), MOXMAN_Exception::NO_WRITE_ACCESS);
     }
     $filter = MOXMAN_Vfs_BasicFileFilter::createFromConfig($config);
     if ($filter->accept($file, false) !== MOXMAN_Vfs_IFileFilter::ACCEPTED) {
         throw new MOXMAN_Exception("Invalid file name for: " . $file->getPublicPath(), MOXMAN_Exception::INVALID_FILE_NAME);
     }
     $port = "";
     if (isset($url["port"])) {
         $port = ":" . $url["port"];
     }
     $query = "";
     if (isset($url["query"])) {
         $query = "?" . $url["query"];
     }
     $path = $url["path"] . $query;
     $host = $url["scheme"] . "://" . $url["host"] . $port;
     $httpClient = new MOXMAN_Http_HttpClient($host);
     $request = $httpClient->createRequest($path);
     $response = $request->send();
     // Read file into ram
     // TODO: This should not happen if we know the file size
     $content = "";
     while (($chunk = $response->read()) != "") {
         $content .= $chunk;
     }
     $httpClient->close();
     // Fire before file action add event
     $args = new MOXMAN_Core_FileActionEventArgs("add", $file);
     $args->getData()->fileSize = strlen($content);
     MOXMAN::getPluginManager()->get("core")->fire("BeforeFileAction", $args);
     $file = $args->getFile();
     $stream = $file->open(MOXMAN_Vfs_IFileStream::WRITE);
     $stream->write($content);
     $stream->close();
     $args = new MOXMAN_Core_FileActionEventArgs("add", $file);
     MOXMAN::getPluginManager()->get("core")->fire("FileAction", $args);
     return parent::fileToJson($file);
 }
Example #4
0
 /** @ignore */
 private function copyFile($fromFile, $toFile)
 {
     $config = $toFile->getConfig();
     if ($config->get('general.demo')) {
         throw new MOXMAN_Exception("This action is restricted in demo mode.", MOXMAN_Exception::DEMO_MODE);
     }
     if (!$fromFile->exists()) {
         throw new MOXMAN_Exception("From file doesn't exist: " . $fromFile->getPublicPath(), MOXMAN_Exception::FILE_DOESNT_EXIST);
     }
     if (!$toFile->canWrite()) {
         throw new MOXMAN_Exception("No write access to file: " . $toFile->getPublicPath(), MOXMAN_Exception::NO_WRITE_ACCESS);
     }
     $filter = MOXMAN_Vfs_BasicFileFilter::createFromConfig($config);
     if ($filter->accept($fromFile, $fromFile->isFile()) !== MOXMAN_Vfs_BasicFileFilter::ACCEPTED) {
         throw new MOXMAN_Exception("Invalid file name for: " . $fromFile->getPublicPath(), MOXMAN_Exception::INVALID_FILE_NAME);
     }
     // Fire before file action event
     $args = new MOXMAN_Core_FileActionEventArgs(MOXMAN_Core_FileActionEventArgs::COPY, $fromFile);
     $args->setTargetFile($toFile);
     $args->getData()->fileSize = $fromFile->getSize();
     MOXMAN::getPluginManager()->get("core")->fire("BeforeFileAction", $args);
     $fromFile = $args->getFile();
     $toFile = $args->getTargetFile();
     // To file exists generate unique name
     $fileName = $toFile->getName();
     $ext = MOXMAN_Util_PathUtils::getExtension($fileName);
     for ($i = 2; $toFile->exists(); $i++) {
         if ($toFile->isFile() && $ext) {
             $toFile = MOXMAN::getFile($toFile->getParent(), basename($fileName, '.' . $ext) . '_' . $i . '.' . $ext);
         } else {
             $toFile = MOXMAN::getFile($toFile->getParent(), $fileName . '_' . $i);
         }
     }
     $fromFile->copyTo($toFile);
     $this->fireTargetFileAction(MOXMAN_Core_FileActionEventArgs::COPY, $fromFile, $toFile);
     return $toFile;
 }
Example #5
0
 /**
  * Executes the command logic with the specified RPC parameters.
  *
  * @param Object $params Command parameters sent from client.
  * @return Object Result object to be passed back to client.
  */
 public function execute($params)
 {
     $fromFile = MOXMAN::getFile($params->from);
     $toFile = MOXMAN::getFile($params->to);
     $config = $toFile->getConfig();
     if ($config->get('general.demo')) {
         throw new MOXMAN_Exception("This action is restricted in demo mode.", MOXMAN_Exception::DEMO_MODE);
     }
     if (!$fromFile->exists()) {
         throw new MOXMAN_Exception("From file doesn't exist: " . $fromFile->getPublicPath(), MOXMAN_Exception::FILE_DOESNT_EXIST);
     }
     if (!$toFile->canWrite()) {
         throw new MOXMAN_Exception("No write access to file: " . $toFile->getPublicPath(), MOXMAN_Exception::NO_WRITE_ACCESS);
     }
     $paths = array();
     $fileSystemManager = MOXMAN::getFileSystemManager();
     $zipArchive = new ZipArchive();
     $localTempFilePath = null;
     $result = array();
     if ($fromFile instanceof MOXMAN_Vfs_Local_File) {
         $res = $zipArchive->open($fromFile->getPath());
     } else {
         $localTempFilePath = $fileSystemManager->getLocalTempPath($fromFile);
         $fromFile->exportTo($localTempFilePath);
         $res = $zipArchive->open($localTempFilePath);
     }
     if ($res) {
         for ($i = 0; $i < $zipArchive->numFiles; $i++) {
             $stat = $zipArchive->statIndex($i);
             $paths[] = $stat["name"];
         }
         $filter = MOXMAN_Vfs_BasicFileFilter::createFromConfig($config);
         $fileSystem = $toFile->getFileSystem();
         foreach ($paths as $path) {
             $isFile = !preg_match('/\\/$/', $path);
             $toPath = MOXMAN_Util_PathUtils::combine($toFile->getPath(), iconv('cp437', 'UTF-8', $path));
             $targetFile = MOXMAN::getFile($toPath);
             if ($filter->accept($targetFile, $isFile) === MOXMAN_Vfs_IFileFilter::ACCEPTED) {
                 if ($isFile) {
                     $content = $zipArchive->getFromName($path);
                     // Fire before file action add event
                     $args = new MOXMAN_Core_FileActionEventArgs("add", $targetFile);
                     $args->getData()->fileSize = strlen($content);
                     MOXMAN::getPluginManager()->get("core")->fire("BeforeFileAction", $args);
                     $targetFile = $args->getFile();
                     $targetFile = $this->mkdirs($targetFile, true);
                     $stream = $targetFile->open(MOXMAN_Vfs_IFileStream::WRITE);
                     $stream->write($content);
                     $stream->close();
                     //echo "Create file: ". $targetFile->getPublicPath() ."\n";
                     $this->fireFileAction(MOXMAN_Core_FileActionEventArgs::ADD, $targetFile);
                 } else {
                     $targetFile = $this->mkdirs($targetFile);
                 }
                 $result[] = $this->fileToJson($targetFile);
             }
         }
         $zipArchive->close();
         if ($localTempFilePath) {
             $fileSystemManager->removeLocalTempFile($fromFile);
         }
     }
     return $result;
 }
Example #6
0
 /**
  * Applies formats to an image.
  *
  * @param MOXMAN_Vfs_IFile $file File to generate images for.
  */
 public function applyFormat(MOXMAN_Vfs_IFile $file)
 {
     if (!$file->exists() || !MOXMAN_Media_ImageAlter::canEdit($file)) {
         return;
     }
     $config = $file->getConfig();
     $format = $config->get("autoformat.rules", "");
     $quality = $config->get("autoformat.jpeg_quality", 90);
     // @codeCoverageIgnoreStart
     if (!$format) {
         return;
     }
     // @codeCoverageIgnoreEnd
     $chunks = preg_split('/,/', $format, 0, PREG_SPLIT_NO_EMPTY);
     $imageInfo = MOXMAN_Media_MediaInfo::getInfo($file);
     $width = $imageInfo["width"];
     $height = $imageInfo["height"];
     foreach ($chunks as $chunk) {
         $parts = explode('=', $chunk);
         $actions = array();
         $fileName = preg_replace('/\\..+$/', '', $file->getName());
         $extension = preg_replace('/^.+\\./', '', $file->getName());
         $targetWidth = $newWidth = $width;
         $targetHeight = $newHeight = $height;
         $items = explode('|', $parts[0]);
         foreach ($items as $item) {
             switch ($item) {
                 case "gif":
                 case "jpg":
                 case "jpeg":
                 case "png":
                     $extension = $item;
                     break;
                 default:
                     $matches = array();
                     if (preg_match('/\\s?([0-9|\\*]+)\\s?x([0-9|\\*]+)\\s?/', $item, $matches)) {
                         $actions[] = "resize";
                         $targetWidth = $matches[1];
                         $targetHeight = $matches[2];
                         if ($targetWidth == '*') {
                             // Width is omitted
                             $targetWidth = floor($width / ($height / $targetHeight));
                         }
                         if ($targetHeight == '*') {
                             // Height is omitted
                             $targetHeight = floor($height / ($width / $targetWidth));
                         }
                     }
             }
         }
         // Scale it
         if ($targetWidth != $width || $targetHeight != $height) {
             $scale = min($targetWidth / $width, $targetHeight / $height);
             $newWidth = $scale > 1 ? $width : floor($width * $scale);
             $newHeight = $scale > 1 ? $height : floor($height * $scale);
         }
         // Build output path
         $outPath = $parts[1];
         $outPath = str_replace("%f", $fileName, $outPath);
         $outPath = str_replace("%e", $extension, $outPath);
         $outPath = str_replace("%ow", "" . $width, $outPath);
         $outPath = str_replace("%oh", "" . $height, $outPath);
         $outPath = str_replace("%tw", "" . $targetWidth, $outPath);
         $outPath = str_replace("%th", "" . $targetHeight, $outPath);
         $outPath = str_replace("%w", "" . $newWidth, $outPath);
         $outPath = str_replace("%h", "" . $newHeight, $outPath);
         $outFile = MOXMAN::getFileSystemManager()->getFile($file->getParent(), $outPath);
         // Make dirs
         $parents = array();
         $parent = $outFile->getParentFile();
         while ($parent) {
             if ($parent->exists()) {
                 break;
             }
             $parents[] = $parent;
             $parent = $parent->getParentFile();
         }
         for ($i = count($parents) - 1; $i >= 0; $i--) {
             $parents[$i]->mkdir();
             $args = new MOXMAN_Core_FileActionEventArgs(MOXMAN_Core_FileActionEventArgs::ADD, $parents[$i]);
             $args->getData()->format = true;
             MOXMAN::getPluginManager()->get("core")->fire("FileAction", $args);
         }
         if (count($actions) > 0) {
             foreach ($actions as $action) {
                 switch ($action) {
                     case 'resize':
                         $imageAlter = new MOXMAN_Media_ImageAlter();
                         $tempFilePath = MOXMAN::getFileSystemManager()->getLocalTempPath($file);
                         $imageAlter->load($file->exportTo($tempFilePath));
                         $imageAlter->resize($newWidth, $newHeight);
                         $outFileTempPath = MOXMAN::getFileSystemManager()->getLocalTempPath($outFile);
                         $imageAlter->save($outFileTempPath, $quality);
                         $outFile->importFrom($outFileTempPath);
                         $args = new MOXMAN_Core_FileActionEventArgs(MOXMAN_Core_FileActionEventArgs::ADD, $outFile);
                         $args->getData()->format = true;
                         MOXMAN::getPluginManager()->get("core")->fire("FileAction", $args);
                         break;
                 }
             }
         } else {
             $imageAlter = new MOXMAN_Media_ImageAlter();
             $tempFilePath = MOXMAN::getFileSystemManager()->getLocalTempPath($file);
             $imageAlter->load($file->exportTo($tempFilePath));
             $outFileTempPath = MOXMAN::getFileSystemManager()->getLocalTempPath($outFile);
             $imageAlter->save($outFileTempPath, $quality);
             $outFile->importFrom($outFileTempPath);
             $args = new MOXMAN_Core_FileActionEventArgs(MOXMAN_Core_FileActionEventArgs::ADD, $outFile);
             $args->getData()->format = true;
             MOXMAN::getPluginManager()->get("core")->fire("FileAction", $args);
         }
     }
 }
Example #7
0
 /** @ignore */
 private function fireFileAction($action, $file, $data = array())
 {
     $args = new MOXMAN_Core_FileActionEventArgs($action, $file);
     $args->getData()->thumb = true;
     return MOXMAN::getPluginManager()->get("core")->fire("FileAction", $args);
 }
Example #8
0
 /**
  * Process a request using the specified context.
  *
  * @param MOXMAN_Http_Context $httpContext Context instance to pass to use for the handler.
  */
 public function processRequest(MOXMAN_Http_Context $httpContext)
 {
     $tempFilePath = null;
     $chunkFilePath = null;
     $request = $httpContext->getRequest();
     $response = $httpContext->getResponse();
     try {
         // Check if the user is authenticated or not
         if (!MOXMAN::getAuthManager()->isAuthenticated()) {
             if (!isset($json->method) || !preg_match('/^(login|logout)$/', $json->method)) {
                 $exception = new MOXMAN_Exception("Access denied by authenticator(s).", 10);
                 $exception->setData(array("login_url" => MOXMAN::getConfig()->get("authenticator.login_page")));
                 throw $exception;
             }
         }
         $file = MOXMAN::getFile($request->get("path"));
         $config = $file->getConfig();
         if ($config->get('general.demo')) {
             throw new MOXMAN_Exception("This action is restricted in demo mode.", MOXMAN_Exception::DEMO_MODE);
         }
         $maxSizeBytes = preg_replace("/[^0-9.]/", "", $config->get("upload.maxsize"));
         if (strpos(strtolower($config->get("upload.maxsize")), "k") > 0) {
             $maxSizeBytes = round(floatval($maxSizeBytes) * 1024);
         }
         if (strpos(strtolower($config->get("upload.maxsize")), "m") > 0) {
             $maxSizeBytes = round(floatval($maxSizeBytes) * 1024 * 1024);
         }
         function generateRandomString($length = 10)
         {
             $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
             $charactersLength = strlen($characters);
             $randomString = '';
             for ($i = 0; $i < $length; $i++) {
                 $randomString .= $characters[rand(0, $charactersLength - 1)];
             }
             return $randomString;
         }
         $filename = generateRandomString() . '.' . MOXMAN_Util_PathUtils::getExtension($request->get("name"));
         $id = $request->get("id");
         $loaded = intval($request->get("loaded", "0"));
         $total = intval($request->get("total", "-1"));
         $file = MOXMAN::getFile($file->getPath(), $filename);
         // Generate unique id for first chunk
         // TODO: We should cleanup orphan ID:s if upload fails etc
         if ($loaded == 0) {
             $id = uniqid();
         }
         // Setup path to temp file based on id
         $tempFilePath = MOXMAN_Util_PathUtils::combine(MOXMAN_Util_PathUtils::getTempDir(), "mcupload_" . $id . "." . MOXMAN_Util_PathUtils::getExtension($file->getName()));
         $chunkFilePath = MOXMAN_Util_PathUtils::combine(MOXMAN_Util_PathUtils::getTempDir(), "mcupload_chunk_" . $id . "." . MOXMAN_Util_PathUtils::getExtension($file->getName()));
         if (!$file->canWrite()) {
             throw new MOXMAN_Exception("No write access to path: " . $file->getPublicPath(), MOXMAN_Exception::NO_WRITE_ACCESS);
         }
         if ($total > $maxSizeBytes) {
             throw new MOXMAN_Exception("File size to large: " . $file->getPublicPath(), MOXMAN_Exception::FILE_SIZE_TO_LARGE);
         }
         // Operations on first chunk
         if ($loaded == 0) {
             // Fire before file action add event
             $args = new MOXMAN_Core_FileActionEventArgs("add", $file);
             $args->getData()->fileSize = $total;
             MOXMAN::getPluginManager()->get("core")->fire("BeforeFileAction", $args);
             $file = $args->getFile();
             if ($file->exists()) {
                 if (!$config->get("upload.overwrite") && !$request->get("overwrite")) {
                     throw new MOXMAN_Exception("Target file exists: " . $file->getPublicPath(), MOXMAN_Exception::FILE_EXISTS);
                 } else {
                     MOXMAN::getPluginManager()->get("core")->deleteThumbnail($file);
                     $file->delete();
                 }
             }
             $filter = MOXMAN_Vfs_CombinedFileFilter::createFromConfig($config, "upload");
             if ($filter->accept($file) !== MOXMAN_Vfs_CombinedFileFilter::ACCEPTED) {
                 throw new MOXMAN_Exception("Invalid file name for: " . $file->getPublicPath(), MOXMAN_Exception::INVALID_FILE_NAME);
             }
         }
         $blobSize = 0;
         $inputFile = $request->getFile("file");
         if (!$inputFile) {
             throw new MOXMAN_Exception("No input file specified.");
         }
         if ($loaded === 0) {
             // Check if we should mock or not
             if (defined('PHPUNIT')) {
                 if (!copy($inputFile['tmp_name'], $tempFilePath)) {
                     throw new MOXMAN_Exception("Could not move the uploaded temp file.");
                 }
             } else {
                 if (!move_uploaded_file($inputFile['tmp_name'], $tempFilePath)) {
                     throw new MOXMAN_Exception("Could not move the uploaded temp file.");
                 }
             }
             $blobSize = filesize($tempFilePath);
         } else {
             // Check if we should mock or not
             if (defined('PHPUNIT')) {
                 if (!copy($inputFile['tmp_name'], $chunkFilePath)) {
                     throw new MOXMAN_Exception("Could not move the uploaded temp file.");
                 }
             } else {
                 if (!move_uploaded_file($inputFile['tmp_name'], $chunkFilePath)) {
                     throw new MOXMAN_Exception("Could not move the uploaded temp file.");
                 }
             }
             $in = fopen($chunkFilePath, 'r');
             if ($in) {
                 $out = fopen($tempFilePath, 'a');
                 if ($out) {
                     while ($buff = fread($in, 8192)) {
                         $blobSize += strlen($buff);
                         fwrite($out, $buff);
                     }
                     fclose($out);
                 }
                 fclose($in);
             }
             unlink($chunkFilePath);
         }
         // Import file when all chunks are complete
         if ($total == -1 || $loaded + $blobSize == $total) {
             clearstatcache();
             // Check if file is valid on last chunk we also check on first chunk but not in the onces in between
             $filter = MOXMAN_Vfs_CombinedFileFilter::createFromConfig($config, "upload");
             if ($filter->accept($file) !== MOXMAN_Vfs_CombinedFileFilter::ACCEPTED) {
                 throw new MOXMAN_Exception("Invalid file name for: " . $file->getPublicPath(), MOXMAN_Exception::INVALID_FILE_NAME);
             }
             // Resize the temporary blob
             if ($config->get("upload.autoresize") && preg_match('/gif|jpe?g|png/i', MOXMAN_Util_PathUtils::getExtension($tempFilePath)) === 1) {
                 $size = getimagesize($tempFilePath);
                 $maxWidth = $config->get('upload.max_width');
                 $maxHeight = $config->get('upload.max_height');
                 if ($size[0] > $maxWidth || $size[1] > $maxHeight) {
                     $imageAlter = new MOXMAN_Media_ImageAlter();
                     $imageAlter->load($tempFilePath);
                     $imageAlter->resize($maxWidth, $maxHeight, true);
                     $imageAlter->save($tempFilePath, $config->get("upload.autoresize_jpeg_quality"));
                 }
             }
             // Create thumbnail and upload then import local blob
             MOXMAN::getPluginManager()->get("core")->createThumbnail($file, $tempFilePath);
             $file->importFrom($tempFilePath);
             unlink($tempFilePath);
             $args = new MOXMAN_Core_FileActionEventArgs("add", $file);
             MOXMAN::getPluginManager()->get("core")->fire("FileAction", $args);
             // In case file is modified
             $file = $args->getFile();
             $result = MOXMAN_Core_Plugin::fileToJson($file, true);
         } else {
             $result = $id;
         }
         $response->sendJson(array("jsonrpc" => "2.0", "result" => $result, "id" => null));
     } catch (Exception $e) {
         if ($tempFilePath && file_exists($tempFilePath)) {
             unlink($tempFilePath);
         }
         if ($chunkFilePath && file_exists($chunkFilePath)) {
             unlink($chunkFilePath);
         }
         MOXMAN::dispose();
         // Closes any open file systems/connections
         $message = $e->getMessage();
         $data = null;
         // Add file and line number when running in debug mode
         // @codeCoverageIgnoreStart
         if (MOXMAN::getConfig()->get("general.debug")) {
             $message .= " " . $e->getFile() . " (" . $e->getLine() . ")";
         }
         // @codeCoverageIgnoreEnd
         // Grab the data from the exception
         if ($e instanceof MOXMAN_Exception && !$data) {
             $data = $e->getData();
         }
         // Json encode error response
         $response->sendJson((object) array("jsonrpc" => "2.0", "error" => array("code" => $e->getCode(), "message" => $message, "data" => $data), "id" => null));
     }
 }