/** * Add info on the user who uploaded the file and the date it was uploaded, and create thumb if it doesn't exist */ protected function get_file_object($file_name) { // Create the thumb if it doesn't exist $thumb = $this->options['upload_dir'] . 'thumb/' . $file_name; if (!file_exists(jQueryUpload::thumbFilename($thumb))) { $this->create_scaled_image($file_name, $this->options['image_versions']['thumbnail']); } // Call the parent method to create the file object $file = parent::get_file_object($file_name); // Add the meta data to the object if (is_object($file)) { $meta = $this->options['upload_dir'] . 'meta/' . $file_name; $file->info = $file->desc = ""; // If the meta data file exists, extract and render the content if (is_file($meta)) { $data = unserialize(file_get_contents($meta)); $file->info = self::renderData($data); $file->desc = array_key_exists(2, $data) ? $data[2] : ''; } elseif (is_link($this->options['upload_dir'] . $file_name)) { $title = Title::newFromText($file_name, NS_FILE); if (is_object($title) && $title->exists()) { list($uid, $ts, $file->desc) = self::getUploadedFileInfo($title); $file->info = self::renderData(array($uid, wfTimestamp(TS_UNIX, $ts))); } } } return $file; }
public static function onRegistration() { global $wgExtensionFunctions, $wgAPIModules; // Register our API Ajax handler $wgAPIModules['jqu'] = 'ApijQueryUpload'; // Create a singleton instance self::$instance = new self(); $wgExtensionFunctions[] = array(self::$instance, 'setup'); // If the query-string arg mwaction is supplied, rename action and change mwaction to action // - this hack was required because the jQueryUpload module uses the name "action" too if (array_key_exists('mwaction', $_REQUEST)) { self::$action = array_key_exists('action', $_REQUEST) ? $_REQUEST['action'] : false; $_REQUEST['action'] = $_GET['action'] = $_POST['action'] = 'jqu'; } }
public function execute() { global $wgScriptPath, $wgUploadDirectory, $wgRequest, $wgFileExtensions; $params = $this->extractRequestParams(); $thumb = $params['thumb']; $path = $params['path']; $name = $params['name']; // So that meaningful errors can be sent back to the client error_reporting(E_ALL | E_STRICT); // But turn off error output as warnings can break the json syntax ini_set("display_errors", "off"); header('Pragma: no-cache'); header('Cache-Control: no-store, no-cache, must-revalidate'); // If a file name is supplied, then this is a file or thumbnail request if ($name) { global $wgUser; // Only return the file if the user is logged in if (!$wgUser->isLoggedIn()) { ApiBase::dieDebug(__METHOD__, 'Not logged in'); } // Get the file or thumb location if ($thumb) { $name = jQueryUpload::thumbFilename("thumb/{$name}"); } $file = "{$wgUploadDirectory}/jquery_upload_files/{$path}/{$name}"; // Set the headers, output the file and bail header("Content-Length: " . filesize($file)); header("Content-Disposition: inline; filename=\"{$name}\""); //header( "Content-Transfer-Encoding: binary" ); IE was not rendering PDF's inline with this header included header("Pragma: private"); $this->getResult()->addValue(null, 'file', $file); $this->getResult()->addValue(null, 'mime', mime_content_type($file)); return; } header('Content-Disposition: inline; filename="files.json"'); header('X-Content-Type-Options: nosniff'); header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Methods: OPTIONS, HEAD, GET, POST, PUT, DELETE'); header('Access-Control-Allow-Headers: X-File-Name, X-File-Type, X-File-Size'); // Process the rename and desc text inputs added to the upload form rows if (array_key_exists('upload_rename_from', $_REQUEST) && array_key_exists('files', $_FILES)) { foreach ($_REQUEST['upload_rename_from'] as $i => $from) { if (false !== ($j = array_search($from, $_FILES['files']['name']))) { $ext = pathinfo($from, PATHINFO_EXTENSION); $name = $_REQUEST['upload_rename_to'][$i] . ".{$ext}"; $_FILES['files']['name'][$j] = $name; jQueryUpload::$desc[$name] = $_REQUEST['upload_desc'][$i]; } } } // Get the file locations $dir = "{$wgUploadDirectory}/jquery_upload_files/{$path}"; if ($path) { $dir .= '/'; } $thm = $dir . 'thumb/'; $meta = $dir . 'meta/'; // Set the initial options for the upload file object $url = "{$wgScriptPath}/api.php?action=jqu"; if ($path) { $url .= "&path={$path}"; } $upload_options = array('script_url' => $url, 'upload_dir' => $dir, 'upload_url' => "{$url}&name=", 'accept_file_types' => '/(' . implode('|', $wgFileExtensions) . ')/i', 'delete_type' => 'POST', 'max_file_size' => 50000000, 'image_versions' => array('thumbnail' => array('upload_dir' => $thm, 'upload_url' => "{$url}&thumb=1&name=", 'max_width' => 80, 'max_height' => 80))); // jQueryUpload module expects the action parameter to be populated with it's own action, not the API action $_REQUEST['action'] = jQueryUpload::$action; // Create the file upload object $upload_handler = new MWUploadHandler($upload_options); // Buffer the output so we the API formatter can send it ob_start(); // Call the appropriate method based on the request switch ($_SERVER['REQUEST_METHOD']) { case 'OPTIONS': break; case 'HEAD': case 'GET': $upload_handler->get(); break; case 'POST': // Create the directories if they don't exist (we do it here so they're not created for every dir read) if (!is_dir("{$wgUploadDirectory}/jquery_upload_files")) { mkdir("{$wgUploadDirectory}/jquery_upload_files"); } if (!is_dir($dir)) { mkdir($dir); } if (!is_dir($thm)) { mkdir($thm); } if (!is_dir($meta)) { mkdir($meta); } $upload_handler->post(); break; default: header('HTTP/1.1 405 Method Not Allowed'); } // Store the buffered output in the result $this->getResult()->addValue(null, 'text', ob_get_contents()); ob_end_clean(); // API needs a MIME type set, so we'll populate it from the existing header $mime = preg_match('%^Content-type: (.+?)$%m', implode("\n", headers_list()), $m) ? $m[1] : 'text/plain'; $this->getResult()->addValue(null, 'mime', $mime); return; }