/** * @return array */ public function upload() { include_once MODX_BASE_PATH . 'assets/plugins/simplefiles/lib/FileAPI.class.php'; $errorCode = 0; if (!empty($_SERVER['HTTP_ORIGIN'])) { // Enable CORS header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']); header('Access-Control-Allow-Methods: POST, GET, OPTIONS'); header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Range, Content-Disposition, Content-Type'); } if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') { $this->isExit = true; return; } if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST') { $dir = $this->params['folder'] . $this->rid . "/"; $flag = $this->FS->makeDir($dir, $this->modx->config['new_folder_permissions']); if (!$flag) { $this->modx->logEvent(0, 3, "Cannot create {$dir} .", 'SimpleFiles'); die; } $uploadDir = $this->params['folder'] . 'upload/' . $this->rid . '/'; $this->FS->makeDir($uploadDir, $this->modx->config['new_folder_permissions']); $filename = end(explode('filename=', $_SERVER['HTTP_CONTENT_DISPOSITION'])); $content_range_header = $_SERVER['HTTP_CONTENT_RANGE']; $content_range = $content_range_header ? preg_split('/[^0-9]+/', $content_range_header) : null; $size = $content_range ? $content_range[3] : null; $partfile = MODX_BASE_PATH . $uploadDir . $filename . '.part'; $in = @fopen("php://input", "rb"); if (!$content_range[1] && $this->FS->fileSize($partfile) > $content_range[2]) { $out = @fopen($partfile, "wb"); } else { $out = fopen($partfile, "ab"); } while ($buff = fread($in, 4096)) { @fwrite($out, $buff); } @fclose($out); @fclose($in); if ($size && $size == $this->FS->fileSize($partfile)) { $name = $this->data->stripName(urldecode($filename)); $name = $this->FS->getInexistantFilename($dir . $name, true); $ext = end(explode('.', $name)); if (in_array($ext, explode(',', $this->params['allowedFiles']))) { if ($this->FS->moveFile($partfile, $name)) { $this->data->create(array('sf_file' => $this->FS->relativePath($name), 'sf_rid' => $this->rid, 'sf_type' => 'file', 'sf_properties' => json_encode(array('filename' => $this->FS->takeFileName($name), 'basename' => $this->FS->takeFileBasename($name), 'mime' => $this->FS->takeFileMIME($name), 'ext' => $this->FS->takeFileExt($name))), 'sf_title' => preg_replace('/\\.[^.\\s]{2,4}$/', '', urldecode($filename)), 'sf_size' => $size))->save(); } else { $errorCode = 101; } } else { $errorCode = 7; } $this->FS->rmDir($this->params['folder'] . 'upload/' . $this->rid); } // Server response: "HTTP/1.1 200 OK" $this->isExit = true; $this->output = \FileAPI::makeResponse(array('status' => \FileAPI::OK, 'statusText' => 'OK', 'body' => array('data' => array('errorCode' => $errorCode)))); return; } }
public function upload() { $out = array(); include_once MODX_BASE_PATH . 'assets/plugins/simplegallery/lib/FileAPI.class.php'; if (!empty($_SERVER['HTTP_ORIGIN'])) { // Enable CORS header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']); header('Access-Control-Allow-Methods: POST, GET, OPTIONS'); header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Range, Content-Disposition, Content-Type'); } if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') { $this->isExit = true; return; } if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST') { $files = \FileAPI::getFiles(); // Retrieve File List $dir = $this->params['folder'] . $this->rid . "/"; $flag = $this->FS->makeDir($dir, $this->modx->config['new_folder_permissions']); if ($files['sg_files']['error'] == UPLOAD_ERR_OK) { $tmp_name = $files["sg_files"]["tmp_name"]; $name = $this->data->stripName($_FILES["sg_files"]["name"]); $name = $this->FS->getInexistantFilename($dir . $name, true); $ext = $this->FS->takeFileExt($name); if (in_array($ext, array('png', 'jpg', 'gif', 'jpeg'))) { if (@move_uploaded_file($tmp_name, $name)) { $options = "w={$this->modx->config['maxImageWidth']}&h={$this->modx->config['maxImageHeight']}&q=96&ar=x&f={$ext}"; if (@$this->data->makeThumb('', $this->FS->relativePath($name), $options)) { $info = getimagesize($name); $properties = array('width' => $info[0], 'height' => $info[1], 'size' => filesize($name)); $this->data->create(array('sg_image' => $this->FS->relativePath($name), 'sg_rid' => $this->rid, 'sg_title' => preg_replace('/\\.[^.\\s]{2,4}$/', '', $_FILES["sg_files"]["name"]), 'sg_properties' => $properties))->save(true); } else { @unlink($name); $files['sg_files']['error'] = 100; } } } else { $files['sg_files']['error'] = 101; } } //fetchImages($files, $images); $json = array('data' => array('_REQUEST' => $_REQUEST, '_FILES' => $files)); // JSONP callback name $jsonp = isset($_REQUEST['callback']) ? trim($_REQUEST['callback']) : null; // Server response: "HTTP/1.1 200 OK" $this->isExit = true; $this->output = \FileAPI::makeResponse(array('status' => \FileAPI::OK, 'statusText' => 'OK', 'body' => $json), $jsonp); return $out; } }
function serviceAcceptHtml5File() { if (!empty($_SERVER['HTTP_ORIGIN'])) { // Enable CORS header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']); header('Access-Control-Allow-Methods: POST, GET, OPTIONS'); header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Range, Content-Disposition, Content-Type'); header('Access-Control-Allow-Credentials: true'); } if (strtoupper($_SERVER['REQUEST_METHOD']) != 'POST') { exit; } require_once BX_DIRECTORY_PATH_PLUGINS . 'file-api/server/FileAPI.class.php'; $aFiles = FileAPI::getFiles(); // Retrieve File List $aReady = array(); $aImages = array(); // JSONP callback name $sJsonp = isset($_REQUEST['callback']) ? trim($_REQUEST['callback']) : null; // upload files and return error messages or uploaded file IDs $this->performAcceptHtml5File($aFiles, $aReady); // Fetch all image-info from files list $this->fetchImagesForAcceptHtml5File($aFiles, $aImages); // JSON-data for server response $aJson = array('files' => $aReady, 'images' => $aImages, 'data' => array('_REQUEST' => $_REQUEST, '_FILES' => $aFiles)); // Server response: "HTTP/1.1 200 OK" FileAPI::makeResponse(array('status' => FileAPI::OK, 'statusText' => 'OK', 'body' => $aJson), $sJsonp); exit; }
} if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') { exit; } if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST') { $files = FileAPI::getFiles(); // Retrieve File List $images = array(); // Fetch all image-info from files list fetchImages($files, $images); // JSONP callback name $jsonp = isset($_REQUEST['callback']) ? trim($_REQUEST['callback']) : null; // JSON-data for server response $json = array('images' => $images, 'data' => array('_REQUEST' => $_REQUEST, '_FILES' => $files)); // Server response: "HTTP/1.1 200 OK" FileAPI::makeResponse(array('status' => FileAPI::OK, 'statusText' => 'OK', 'body' => $json), $jsonp); exit; } function fetchImages($files, &$images, $name = 'file') { if (isset($files['tmp_name'])) { $filename = $files['tmp_name']; list($mime) = explode(';', @mime_content_type($filename)); if (strpos($mime, 'image') !== false) { $size = getimagesize($filename); $base64 = base64_encode(file_get_contents($filename)); $images[$name] = array('width' => $size[0], 'height' => $size[1], 'mime' => $mime, 'size' => filesize($filename), 'dataURL' => 'data:' . $mime . ';base64,' . $base64); } } else { foreach ($files as $name => $file) { fetchImages($file, $images, $name);