/** * @return array * @throws Xapp_XFile_Exception */ public function put() { xapp_import('xapp.Path.Utils'); xapp_import('xapp.Utils.SystemTextEncoding'); $vars = array_merge($_GET, $_POST); $dstIn = '/'; $mount = '/'; if (array_key_exists('dstDir', $vars)) { $dstIn = XApp_Path_Utils::decodeSecureMagic($vars['dstDir']); } if (array_key_exists('mount', $vars)) { $mount = preg_replace('@[/\\\\]@', '', XApp_Path_Utils::decodeSecureMagic($vars['mount'])); } if ($dstIn === '.') { $dstIn = '/'; } $vfs = $this->getFileSystem($mount); $destination = $vfs->toRealPath(XApp_Path_Utils::normalizePath($mount . DIRECTORY_SEPARATOR . $dstIn)); $errors = array(); if (!$this->isLocal($mount, $this->getFSResources())) { return $this->putRemote($mount, $destination); } //writable check if (!is_writable($destination)) { throw new Xapp_XFile_Exception(XAPP_TEXT_FORMATTED('DIRECTORY_NOT_WRITEABLE', array($destination), 55100)); } //parse files $fileVars = $_FILES; foreach ($fileVars as $boxName => $boxData) { if (substr($boxName, 0, 9) != "userfile_") { continue; } $err = self::parseFileDataErrors($boxData); if ($err != null) { $errorMessage = $err[1]; $errors[] = XAPP_TEXT_FORMATTED('Error with upload %s', array($errorMessage)); continue; } //basic sanitize $userfile_name = $boxData["name"]; $userfile_name = XApp_Path_Utils::sanitizeEx(XApp_SystemTextEncoding::fromPostedFileName($userfile_name), XApp_Path_Utils::SANITIZE_HTML_STRICT); $userfile_name = substr($userfile_name, 0, 128); //rename if needed! $autorename = xapp_get_option(self::AUTO_RENAME); if ($autorename) { $userfile_name = self::autoRenameForDest($destination, $userfile_name); } /*** * file extension check */ $ext = pathinfo(strtolower($userfile_name), PATHINFO_EXTENSION); $allowable = explode(',', xapp_get_option(self::UPLOAD_EXTENSIONS, $this)); if ($ext == '' || $ext == false || !in_array($ext, $allowable)) { $errors[] = XAPP_TEXT_FORMATTED('UPLOAD_EXTENSIONS_NOT_ALLOWED', array($userfile_name, $ext)); xapp_clog('file not allowed'); continue; } try { //no need anymore if (file_exists($destination . "/" . $userfile_name)) { } } catch (Exception $e) { $errorMessage = $e->getMessage(); $errors[] = XAPP_TEXT_FORMATTED('UPLOAD_UNKOWN_ERROR', array($userfile_name, $errorMessage)); break; } if (isset($boxData["input_upload"])) { try { $input = fopen("php://input", "r"); $output = fopen("{$destination}/" . $userfile_name, "w"); $sizeRead = 0; while ($sizeRead < intval($boxData["size"])) { $chunk = fread($input, 4096); $sizeRead += strlen($chunk); fwrite($output, $chunk, strlen($chunk)); } fclose($input); fclose($output); } catch (Exception $e) { $errorMessage = $e->getMessage(); $errors[] = XAPP_TEXT_FORMATTED('UPLOAD_UNKOWN_ERROR', array($userfile_name, $errorMessage)); break; } } else { $result = @move_uploaded_file($boxData["tmp_name"], "{$destination}/" . $userfile_name); if (!$result) { $realPath = $destination . DIRECTORY_SEPARATOR . $userfile_name; $result = move_uploaded_file($boxData["tmp_name"], $realPath); } if (!$result) { $errors[] = XAPP_TEXT_FORMATTED('UPLOAD_UNKOWN_ERROR', array($userfile_name)); break; } } } return $errors; }
public function rename($mount, $filePath, $filename_new, $dest = null, &$errors) { $filename_new = XApp_Path_Utils::sanitizeEx(XApp_SystemTextEncoding::magicDequote($filename_new), XApp_Path_Utils::SANITIZE_HTML_STRICT); $filename_new = substr($filename_new, 0, xapp_get_option(self::NODENAME_MAX_LENGTH, $this)); $old = $this->toRealPath($mount . DIRECTORY_SEPARATOR . $filePath); if (!is_writable($old)) { $errors[] = XAPP_TEXT_FORMATTED('FILE_NOT_WRITEABLE', array($old), 55100); return; } if ($dest == null) { $new = dirname($old) . "/" . $filename_new; } else { $new = $this->toRealPath($mount . DIRECTORY_SEPARATOR . $dest); } if ($filename_new == "" && $dest == null) { $errors[] = XAPP_TEXT_FORMATTED('DIRECTORY_NOT_WRITEABLE', array($old), 55100); return; } if (file_exists($new)) { $errors[] = XAPP_TEXT_FORMATTED('FILE_EXISTS', array($filename_new), 55100); } if (!file_exists($old)) { $errors[] = XAPP_TEXT_FORMATTED('CAN_NOT_FIND_FILE', array(basename($filePath)), 55100); return; } rename($old, $new); }
public function get($mount, $relativePath, $attachment = false, $options = array()) { $relativePath = XApp_Path_Utils::sanitizeEx($relativePath); $mount = XApp_Path_Utils::normalizePath(XApp_Path_Utils::sanitizeEx($mount), true, true); $mount = str_replace('/', '', $mount); $fsPath = XApp_Path_Utils::sanitizeEx($mount . '://' . $relativePath); $content = $this->getMountManager()->read($fsPath); if ($content) { $options = array_merge($options, array(XApp_File_Utils::OPTION_AS_ATTACHMENT => $attachment)); return $this->send(basename($relativePath), $content, $options); } }