summaryrefslogtreecommitdiff
path: root/framework/Web/UI/ActiveControls/TActiveFileUpload.php
blob: f20983b5f4b6bc607093add1b6122394c8572650 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
<?php
/**
 * TActiveFileUpload.php
 * 
 * @author Bradley Booms <Bradley.Booms@nsighttel.com>
 * @author Christophe Boulain <Christophe.Boulain@gmail.com>
 * @author Gabor Berczi <gabor.berczi@devworx.hu> (issue 349 remote vulnerability fix)
 * @version $Id$
 * @package System.Web.UI.ActiveControls
 */

/**
 * Load TActiveControlAdapter and TFileUpload.
 */
Prado::using('System.Web.UI.ActiveControls.TActiveControlAdapter');
Prado::using('System.Web.UI.WebControls.TFileUpload');

/**
 * TActiveFileUpload
 * 
 * TActiveFileUpload displays a file upload field on a page. Upon postback,
 * the text entered into the field will be treated as the name of the file
 * that will be uploaded to the server. The property {@link getHasFile HasFile}
 * indicates whether the file upload is successful. If successful, the file
 * may be obtained by calling {@link saveAs} to save it at a specified place.
 * You can use {@link getFileName FileName}, {@link getFileType FileType},
 * {@link getFileSize FileSize} to get the original client-side file name,
 * the file mime type, and the file size information. If the upload is not
 * successful, {@link getErrorCode ErrorCode} contains the error code
 * describing the cause of failure.
 *
 * TActiveFileUpload raises {@link onFileUpload OnFileUpload} event if a file is uploaded
 * (whether it succeeds or not).
 * 
 * TActiveFileUpload actually does a postback in a hidden IFrame, and then does a callback.
 * This callback then raises the {@link onFileUpload OnFileUpload} event. After the postback
 * a status icon is displayed; either a green checkmark if the upload is successful,
 * or a red x if there was an error.
 *  
 * @author Bradley Booms <Bradley.Booms@nsighttel.com>
 * @author Christophe Boulain <Christophe.Boulain@gmail.com>
 * @package System.Web.UI.ActiveControls
 * @version $Id$
 */
class TActiveFileUpload extends TFileUpload implements IActiveControl, ICallbackEventHandler, INamingContainer 
{
	
	const SCRIPT_PATH = 'prado/activefileupload';
	
	/**
	 * @var THiddenField a flag to tell which component is doing the callback.
	 */
	private $_flag;
	/**
	 * @var TImage that spins to show that the file is being uploaded.
	 */
	private $_busy;
	/**
	 * @var TImage that shows a green check mark for completed upload.
	 */
	private $_success;
	/**
	 * @var TImage that shows a red X for incomplete upload.
	 */
	private $_error;
	/**
	 * @var TInlineFrame used to submit the data in an "asynchronous" fashion.
	 */
	private $_target;

	
	/**
	 * Creates a new callback control, sets the adapter to
	 * TActiveControlAdapter. If you override this class, be sure to set the
	 * adapter appropriately by, for example, by calling this constructor.
	 */
	public function __construct(){
		parent::__construct();
		$this->setAdapter(new TActiveControlAdapter($this));
	}
	
	
	/**
	 * @param string asset file in the self::SCRIPT_PATH directory.
	 * @return string asset file url.
	 */
	protected function getAssetUrl($file='')
	{
		$base = $this->getPage()->getClientScript()->getPradoScriptAssetUrl();
		return $base.'/'.self::SCRIPT_PATH.'/'.$file;
	}
	
	
	/**
	 * This method is invoked when a file is uploaded.
	 * If you override this method, be sure to call the parent implementation to ensure
	 * the invocation of the attached event handlers.
	 * @param TEventParameter event parameter to be passed to the event handlers
	 */
	public function onFileUpload($param){
		if ($this->_flag->getValue() && $this->getPage()->getIsPostBack()){
			// save the file so that it will persist past the end of this return.
			$localName = str_replace('\\', '/', tempnam(Prado::getPathOfNamespace($this->getTempPath()),''));
			parent::saveAs($localName);
			
			$filename=addslashes($this->getFileName());

			$params = new TActiveFileUploadCallbackParams;
			$params->localName = $localName;
			$params->fileName = $filename;
			$params->fileSize = $this->getFileSize();
			$params->fileType = $this->getFileType();
			$params->errorCode = $this->getErrorCode();

			// return some javascript to display a completion status.
			echo <<<EOS
<script language="Javascript">
	Options = new Object();
	Options.clientID = '{$this->getClientID()}';
	Options.targetID = '{$this->_target->getUniqueID()}';
	Options.fileName = '{$params->fileName}';
	Options.fileSize = '{$params->fileSize}';
	Options.fileType = '{$params->fileType}';
	Options.errorCode = '{$params->errorCode}';
	Options.callbackToken = '{$this->pushParamsAndGetToken($params)}';
	parent.Prado.WebUI.TActiveFileUpload.onFileUpload(Options);
</script>
EOS;
			exit();
		}
	}
	
	/**
	 * @return string the path where the uploaded file will be stored temporarily, in namespace format
	 * default "Application.runtime.*"
	 */
	public function getTempPath(){
		return $this->getViewState('TempPath', 'Application.runtime.*');
	}
	
	/**
	 * @param string the path where the uploaded file will be stored temporarily in namespace format
	 * default "Application.runtime.*"
	 */
	public function setTempPath($value){
		$this->setViewState('TempPath',$value,'Application.runtime.*');
	}
	
	/**
	 * @return boolean a value indicating whether an automatic callback to the server will occur whenever the user modifies the text in the TTextBox control and then tabs out of the component. Defaults to true.
	 * Note: When set to false, you will need to trigger the callback yourself.
	 */
	public function getAutoPostBack(){
		return $this->getViewState('AutoPostBack', true);
	}
	
	/**
	 * @param boolean a value indicating whether an automatic callback to the server will occur whenever the user modifies the text in the TTextBox control and then tabs out of the component. Defaults to true.
	 * Note: When set to false, you will need to trigger the callback yourself.
	 */
	public function setAutoPostBack($value){
		$this->setViewState('AutoPostBack',TPropertyValue::ensureBoolean($value),true);
	}
	
	/**
	 * @return string A chuck of javascript that will need to be called if {{@link getAutoPostBack AutoPostBack} is set to false}
	 */	
	public function getCallbackJavascript(){
		return "Prado.WebUI.TActiveFileUpload.fileChanged(\"{$this->getClientID()}\")";
	}
	
	/**
	 * @throws TInvalidDataValueException if the {@link getTempPath TempPath} is not writable.
	 */
	public function onInit($sender)
	{
		parent::onInit($sender);

		if (!Prado::getApplication()->getCache())
		  if (!Prado::getApplication()->getSecurityManager())
			throw new Exception('TActiveFileUpload needs either an application level cache or a security manager to work securely');

		if (!is_writable(Prado::getPathOfNamespace($this->getTempPath()))){
			throw new TInvalidDataValueException("activefileupload_temppath_invalid", $this->getTempPath());
		}
	}
	
	/**
	 * Raises <b>OnFileUpload</b> event.
	 * 
	 * This method is required by {@link ICallbackEventHandler} interface. 
	 * This method is mainly used by framework and control developers.
	 * @param TCallbackEventParameter the event parameter
	 */
 	public function raiseCallbackEvent($param)
	{
 		$cp = $param->getCallbackParameter();
		if ($key = $cp->targetID == $this->_target->getUniqueID())
		{	
			$params = $this->popParamsByToken($cp->callbackToken);
		
			$_FILES[$key]['name'] = $params->fileName;
			$_FILES[$key]['size'] = intval($params->fileSize);
			$_FILES[$key]['type'] = $params->fileType;
			$_FILES[$key]['error'] = intval($params->errorCode);
			$_FILES[$key]['tmp_name'] = $params->localName;

			$this->loadPostData($key, null);
			
			$this->raiseEvent('OnFileUpload', $this, $param);
		}
	}

	protected function pushParamsAndGetToken(TActiveFileUploadCallbackParams $params)
	{
		if ($cache = Prado::getApplication()->getCache())
			{
				// this is the most secure method, file info can't be forged from client side, no matter what
				$token = md5('TActiveFileUpload::Params::'.$this->ClientID.'::'+rand(1000*1000,9999*1000));
				$cache->set($token, serialize($params), 5*60); // expire in 5 minutes - the callback should arrive back in seconds, actually
			}
		else
		if ($mgr = Prado::getApplication()->getSecurityManager())
			{
				// this is a less secure method, file info can be still forged from client side, but only if attacker knows the secret application key
				$token = base64_encode($mgr->encrypt(serialize($params)));
			}
		else
			throw new Exception('TActiveFileUpload needs either an application level cache or a security manager to work securely');
			
		return $token;
	}
	
	protected function popParamsByToken($token)
	{
		if ($cache = Prado::getApplication()->getCache())
			{
				$v = $cache->get($token);
				assert($v!='');
				$cache->delete($token); // remove it from cache so it can't be used again and won't take up space either
				$params = unserialize($v);
			}
		else
		if ($mgr = Prado::getApplication()->getSecurityManager())
			{
				$v = $mgr->decrypt(base64_decode($token));
				$params = unserialize($v);
			}
		else
			throw new Exception('TActiveFileUpload needs either an application level cache or a security manager to work securely');
 
		assert($params instanceof TActiveFileUploadCallbackParams);
		
		return $params;
	}

	/**
	 * Publish the javascript
	 */
	public function onPreRender($param){
		parent::onPreRender($param);
		$this->getPage()->getClientScript()->registerPradoScript('effects');
		$this->getPage()->getClientScript()->registerPradoScript('activefileupload');
	}
	
	
	public function createChildControls(){
		$this->_flag = Prado::createComponent('THiddenField');
		$this->_flag->setID('Flag');
		$this->getControls()->add($this->_flag);
		
		$this->_busy = Prado::createComponent('TImage');
		$this->_busy->setID('Busy');
		$this->_busy->setImageUrl($this->getAssetUrl('ActiveFileUploadIndicator.gif'));
		$this->_busy->setStyle("display:none");
		$this->getControls()->add($this->_busy);
		
		$this->_success = Prado::createComponent('TImage');
		$this->_success->setID('Success');
		$this->_success->setImageUrl($this->getAssetUrl('ActiveFileUploadComplete.png'));
		$this->_success->setStyle("display:none");
		$this->getControls()->add($this->_success);
		
		$this->_error = Prado::createComponent('TImage');
		$this->_error->setID('Error');
		$this->_error->setImageUrl($this->getAssetUrl('ActiveFileUploadError.png'));
		$this->_error->setStyle("display:none");
		$this->getControls()->add($this->_error);
		
		$this->_target = Prado::createComponent('TInlineFrame');
		$this->_target->setID('Target');
		$this->_target->setFrameUrl($this->getAssetUrl('ActiveFileUploadBlank.html'));
		$this->_target->setStyle("width:0px; height:0px;");
		$this->_target->setShowBorder(false);
		$this->getControls()->add($this->_target);
	}
	
	
	/**
	 * Removes localfile on ending of the callback.  
	 */
	public function onUnload($param){
		if ($this->getPage()->getIsCallback() && 
			$this->getHasFile() && 
			file_exists($this->getLocalName())){
				unlink($this->getLocalName());
		}
		parent::onUnload($param);
	}

	/**
	 * @return TBaseActiveCallbackControl standard callback control options.
	 */
	public function getActiveControl(){
		return $this->getAdapter()->getBaseActiveControl();
	}

	/**
	 * Adds ID attribute, and renders the javascript for active component.
	 * @param THtmlWriter the writer used for the rendering purpose
	 */
	public function addAttributesToRender($writer){
		parent::addAttributesToRender($writer);
		$writer->addAttribute('id',$this->getClientID());
		
		$this->getActiveControl()->registerCallbackClientScript($this->getClientClassName(),$this->getClientOptions());
	}

	/**
	 * @return string corresponding javascript class name for this control.
	 */
	protected function getClientClassName(){
		return 'Prado.WebUI.TActiveFileUpload';
	}

	/**
	 * Gets the client side options for this control.
	 * @return array (	inputID => input client ID,
	 * 					flagID => flag client ID,
	 * 					targetName => target unique ID,
	 * 					formID => form client ID,
	 * 					indicatorID => upload indicator client ID,
	 * 					completeID => complete client ID,
	 * 					errorID => error client ID)
	 */
	protected function getClientOptions(){
		$options['ID'] = $this->getClientID();
		$options['EventTarget'] = $this->getUniqueID();
		
		$options['inputID'] = $this->getClientID();
		$options['flagID'] = $this->_flag->getClientID();
		$options['targetID'] = $this->_target->getUniqueID();
		$options['formID'] = $this->getPage()->getForm()->getClientID();
		$options['indicatorID'] = $this->_busy->getClientID();
		$options['completeID'] = $this->_success->getClientID();
		$options['errorID'] = $this->_error->getClientID();
		$options['autoPostBack'] = $this->getAutoPostBack();
		return $options;
	}

	/**
	 * Saves the uploaded file.
	 * @param string the file name used to save the uploaded file
	 * @param boolean whether to delete the temporary file after saving.
	 * If true, you will not be able to save the uploaded file again.
	 * @return boolean true if the file saving is successful
	 */
	public function saveAs($fileName,$deleteTempFile=true){
		if (($this->getErrorCode()===UPLOAD_ERR_OK) && (file_exists($this->getLocalName()))){
			if ($deleteTempFile)
				return rename($this->getLocalName(),$fileName);
			else
				return copy($this->getLocalName(),$fileName);
		} else
			return false;
	}

	/**
	 * @return TImage the image displayed when an upload 
	 * 		completes successfully.
	 */
	public function getSuccessImage(){
		$this->ensureChildControls();
		return $this->_success;
	}
	
	/**
	 * @return TImage the image displayed when an upload 
	 * 		does not complete successfully.
	 */
	public function getErrorImage(){
		$this->ensureChildControls();
		return $this->_error;
	}
	
	/**
	 * @return TImage the image displayed when an upload 
	 * 		is in progress.
	 */
	public function getBusyImage(){
		$this->ensureChildControls();
		return $this->_busy;
	}
}

class TActiveFileUploadCallbackParams
{
	public $localName;
	public $fileName;
	public $fileSize;
	public $fileType;
	public $errorCode;
}