Added Feature #16641: Here comes FAL - beta2 version
[Packages/TYPO3.CMS.git] / typo3 / sysext / fal / classes / dataprovider / class.tx_fal_plupload_dataprovider.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010 FAL development team <fal@wmdb.de>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * File Abtraction Layer dataprovider for pluploader
30 *
31 * @author FAL development team <fal@wmdb.de>
32 * @package TYPO3
33 * @subpackage tx_fal
34 * @version $Id: $
35 */
36
37
38
39 require_once(PATH_typo3 . 'classes/class.typo3_tcefile.php');
40
41 class tx_fal_plupload_dataprovider extends TYPO3_tcefile {
42
43 /**
44 * Performing the file admin action:
45 * Initializes the objects, setting permissions, sending data to object.
46 *
47 * @return void
48 */
49 public function main() {
50 // Initializing:
51 $this->fileProcessor = t3lib_div::makeInstance('tx_fal_extfilefunc');
52 $this->fileProcessor->init($GLOBALS['FILEMOUNTS'], $GLOBALS['TYPO3_CONF_VARS']['BE']['fileExtensions']);
53 $this->fileProcessor->init_actionPerms($GLOBALS['BE_USER']->getFileoperationPermissions());
54 $this->fileProcessor->dontCheckForUnique = ($this->overwriteExistingFiles ? 1 : 0);
55
56 // Checking referer / executing:
57 $refInfo = parse_url(t3lib_div::getIndpEnv('HTTP_REFERER'));
58 $httpHost = t3lib_div::getIndpEnv('TYPO3_HOST_ONLY');
59 if ($httpHost != $refInfo['host']
60 && $this->vC != $GLOBALS['BE_USER']->veriCode()
61 && !$GLOBALS['TYPO3_CONF_VARS']['SYS']['doNotCheckReferer']
62 && $GLOBALS['CLIENT']['BROWSER'] != 'flash') {
63 $this->fileProcessor->writeLog(0, 2, 1, 'Referer host "%s" and server host "%s" did not match!', array($refInfo['host'], $httpHost));
64 } else {
65 $this->fileProcessor->start($this->file);
66 $this->fileData = $this->fileProcessor->processData();
67 }
68 }
69
70 /**
71 * Handles the actual process from within the ajaxExec function
72 * therefore, it does exactly the same as the real typo3/tce_file.php
73 * but without calling the "finish" method, thus makes it simpler to deal with the
74 * actual return value
75 *
76 *
77 * @param string $params always empty.
78 * @param string $ajaxObj The Ajax object used to return content and set content types
79 * @return void
80 */
81 public function processAjaxRequest(array $params, TYPO3AJAX $ajaxObj) {
82 $this->init();
83 $this->processBinaryStream();
84 $this->main();
85
86 $response = array();
87 $error = null;
88 foreach ($this->fileData['upload'] as $uploaded) {
89 // $uploaded would contain the absolute file path, if it worked
90 if ($uploaded === null) {
91 $error = $this->fileProcessor->lastError;
92 break;
93 }
94 }
95 if (strlen($error)) {
96 $response['message'] = $error;
97 $response['success'] = FALSE;
98 } else {
99 $response['result'] = $this->fileData;
100 if ($this->redirect) {
101 $response['redirect'] = $this->redirect;
102 }
103 $response['success'] = TRUE;
104 }
105 $ajaxObj->setContentFormat('plain');
106 $ajaxObj->setContent(array(json_encode($response)));
107 }
108
109 /**
110 * File upload can be as multipart or as binary stream.
111 *
112 * This method first checks, if upload is not a multipart upload and then
113 * converts the received data into the expected structures.
114 *
115 * @return void
116 */
117 protected function processBinaryStream() {
118
119 // Look for the content type header
120 if (isset($_SERVER['HTTP_CONTENT_TYPE'])) {
121 $contentType = $_SERVER['HTTP_CONTENT_TYPE'];
122 }
123
124 if (isset($_SERVER['CONTENT_TYPE'])) {
125 $contentType = $_SERVER['CONTENT_TYPE'];
126 }
127
128 if (strpos($contentType, "multipart") === FALSE) {
129 $tempFile = tempnam('/tmp', 'upload');
130
131 // ppen temp file
132 $out = fopen($tempFile, 'wb');
133 if ($out) {
134 // read binary input stream and append it to temp file
135 $in = fopen("php://input", "rb");
136
137 if ($in) {
138 while ($buff = fread($in, 4096))
139 fwrite($out, $buff);
140 } else
141 die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
142 fclose($out);
143 $_FILES['upload_1'] = array(
144 'name' => $_GET['file']['upload']['1']['name'],
145 'tmp_name' => $tempFile,
146 'size' => filesize($tempFile),
147 'relativeTarget' => $_GET['file']['upload']['1']['relativeTarget'],
148 );
149 } else
150 die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
151 }
152 }
153
154 }
155
156 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/fal/classes/dataprovider/class.tx_fal_plupload_dataprovider.php'])) {
157 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/fal/classes/dataprovider/class.tx_fal_plupload_dataprovider.php']);
158 }
159 ?>