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; } }
/** * FileAPI upload controller (example) */ include './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') { 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'])) {
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; }
include './FileAPI.class.php'; if (!empty($_SERVER['HTTP_ORIGIN'])) { 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'); } if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') { exit; } if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST') { $files = FileAPI::getFiles(); $images = array(); fetchImages($files, $images); $jsonp = isset($_REQUEST['callback']) ? trim($_REQUEST['callback']) : null; $json = json_encode(array('images' => $images, 'data' => print_r(array('_REQUEST' => $_REQUEST, '_FILES' => FileAPI::getFiles()), true))); if (empty($jsonp)) { echo $json; } else { echo '<script type="text/javascript">' . '(function(ctx,jsonp){' . 'if(ctx&&ctx[jsonp]){' . 'ctx[jsonp](200, "OK", "' . addslashes($json) . '")' . '}' . '})(this.parent, "' . $jsonp . '")' . '</script>'; } 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) { $content = file_get_contents($filename); $images[$name] = 'data:' . $mime . ';base64,' . base64_encode($content);