1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Upload.php - Upload Trait |
5
|
|
|
* |
6
|
|
|
* The Jaxon class uses a modular plug-in system to facilitate the processing |
7
|
|
|
* of special Ajax requests made by a PHP page. |
8
|
|
|
* It generates Javascript that the page must include in order to make requests. |
9
|
|
|
* It handles the output of response commands (see <Jaxon\Response\Response>). |
10
|
|
|
* Many flags and settings can be adjusted to effect the behavior of the Jaxon class |
11
|
|
|
* as well as the client-side javascript. |
12
|
|
|
* |
13
|
|
|
* @package jaxon-core |
14
|
|
|
* @author Thierry Feuzeu <[email protected]> |
15
|
|
|
* @copyright 2017 Thierry Feuzeu <[email protected]> |
16
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
17
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Jaxon\Traits; |
21
|
|
|
|
22
|
|
|
use Jaxon\Plugin\Manager as PluginManager; |
23
|
|
|
use Jaxon\Request\Manager as RequestManager; |
24
|
|
|
use Jaxon\Response\Manager as ResponseManager; |
25
|
|
|
|
26
|
|
|
use Jaxon\Utils\URI; |
27
|
|
|
use Exception; |
28
|
|
|
use Closure; |
29
|
|
|
|
30
|
|
|
trait Upload |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Check if uploaded files are available |
34
|
|
|
* |
35
|
|
|
* @return boolean |
36
|
|
|
*/ |
37
|
|
|
public function hasUploadedFiles() |
38
|
|
|
{ |
39
|
|
|
if(($xUploadPlugin = $this->getPluginManager()->getRequestPlugin(self::FILE_UPLOAD)) == null) |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
return false; |
42
|
|
|
} |
43
|
|
|
return $xUploadPlugin->canProcessRequest(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Check uploaded files validity and move them to the user dir |
48
|
|
|
* |
49
|
|
|
* @return boolean |
50
|
|
|
*/ |
51
|
|
|
public function saveUploadedFiles() |
52
|
|
|
{ |
53
|
|
|
try |
54
|
|
|
{ |
55
|
|
|
if(($xUploadPlugin = $this->getPluginManager()->getRequestPlugin(self::FILE_UPLOAD)) == null) |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
throw new Exception($this->trans('errors.upload.plugin')); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
elseif(!$xUploadPlugin->canProcessRequest()) |
60
|
|
|
{ |
61
|
|
|
throw new Exception($this->trans('errors.upload.request')); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
// Save uploaded files |
64
|
|
|
$sKey = $xUploadPlugin->saveUploadedFiles(); |
65
|
|
|
$sResponse = '{"code": "success", "upl": "' . $sKey . '"}'; |
66
|
|
|
$return = true; |
67
|
|
|
} |
68
|
|
|
catch(Exception $e) |
69
|
|
|
{ |
70
|
|
|
$sResponse = '{"code": "error", "msg": "' . addslashes($e->getMessage()) . '"}'; |
71
|
|
|
$return = false; |
72
|
|
|
} |
73
|
|
|
// Send the response back to the browser |
74
|
|
|
echo '<script>var res = ', $sResponse, '; </script>'; |
75
|
|
|
if(($this->getOption('core.process.exit'))) |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
exit(); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
return $return; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the uploaded files |
84
|
|
|
* |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
public function getUploadedFiles() |
88
|
|
|
{ |
89
|
|
|
if(($xUploadPlugin = $this->getPluginManager()->getRequestPlugin(self::FILE_UPLOAD)) == null) |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
return []; |
92
|
|
|
} |
93
|
|
|
return $xUploadPlugin->getUploadedFiles(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Filter uploaded file name |
98
|
|
|
* |
99
|
|
|
* @param Closure $fFileFilter The closure which filters filenames |
100
|
|
|
* |
101
|
|
|
* @return void |
102
|
|
|
*/ |
103
|
|
|
public function setUploadFileFilter(Closure $fFileFilter) |
104
|
|
|
{ |
105
|
|
|
if(($xUploadPlugin = $this->getPluginManager()->getRequestPlugin(self::FILE_UPLOAD)) == null) |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
return; |
108
|
|
|
} |
109
|
|
|
$xUploadPlugin->setFileFilter($fFileFilter); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.