1 | <?php defined('SYSPATH') OR die('No direct script access.'); |
||
12 | abstract class Kohana_Upload_Source { |
||
13 | |||
14 | const TYPE_URL = 'url'; |
||
15 | const TYPE_STREAM = 'stream'; |
||
16 | const TYPE_TEMP = 'temp'; |
||
17 | const TYPE_FILE = 'file'; |
||
18 | const TYPE_UPLOAD = 'upload'; |
||
19 | |||
20 | /** |
||
21 | * Determine if the source is a valid one |
||
22 | * @param mixed $source |
||
23 | * @return boolean |
||
24 | */ |
||
25 | 12 | public static function valid($source) |
|
26 | { |
||
27 | 12 | return Upload_Source::guess_type($source) !== FALSE; |
|
28 | } |
||
29 | |||
30 | /** |
||
31 | * Store any native errors occurred during upload |
||
32 | */ |
||
33 | protected $_error; |
||
34 | |||
35 | 13 | public function error($error = NULL) |
|
45 | |||
46 | /** |
||
47 | * Guess the type of the source: |
||
48 | * |
||
49 | * - Upload_Source::TYPE_URL |
||
50 | * - Upload_Source::TYPE_STREAM |
||
51 | * - Upload_Source::TYPE_TEMP |
||
52 | * - Upload_Source::TYPE_FILE |
||
53 | * - Upload_Source::TYPE_UPLOAD |
||
54 | * - FALSE |
||
55 | * |
||
56 | * @param mixed $source |
||
57 | * @return string|boolean |
||
58 | */ |
||
59 | 32 | public static function guess_type($source) |
|
60 | { |
||
61 | 32 | if (is_array($source)) |
|
62 | 32 | { |
|
63 | 2 | return (Upload::valid($source) AND $source['error'] !== UPLOAD_ERR_NO_FILE) ? Upload_Source::TYPE_UPLOAD : FALSE; |
|
64 | } |
||
65 | 30 | elseif ($source == 'php://input') |
|
66 | { |
||
67 | 1 | return Upload_Source::TYPE_STREAM; |
|
68 | } |
||
69 | 29 | elseif (Valid::url($source)) |
|
70 | { |
||
71 | 5 | return Upload_Source::TYPE_URL; |
|
72 | } |
||
73 | 25 | elseif (substr_count($source, DIRECTORY_SEPARATOR) === 1) |
|
74 | { |
||
75 | 7 | return Upload_Source::TYPE_TEMP; |
|
76 | } |
||
77 | 18 | elseif (is_file($source)) |
|
78 | { |
||
79 | 16 | return Upload_Source::TYPE_FILE; |
|
80 | } |
||
81 | else |
||
82 | { |
||
83 | 3 | return FALSE; |
|
84 | } |
||
85 | } |
||
86 | |||
87 | 22 | public static function factory($data) |
|
88 | { |
||
89 | 22 | return new Upload_Source($data); |
|
90 | } |
||
91 | |||
92 | protected $_filename; |
||
93 | protected $_data = NULL; |
||
94 | protected $_type = NULL; |
||
95 | protected $_copied = FALSE; |
||
96 | |||
97 | 22 | public function __construct($data) |
|
98 | { |
||
99 | 22 | $this->_data = $data; |
|
100 | |||
101 | 22 | if (($type = Upload_Source::guess_type($data))) |
|
102 | 22 | { |
|
103 | 22 | $this->_type = $type; |
|
104 | 22 | } |
|
105 | 22 | } |
|
106 | |||
107 | 22 | public function type() |
|
108 | { |
||
109 | 22 | return $this->_type; |
|
110 | } |
||
111 | |||
112 | 16 | public function data() |
|
113 | { |
||
114 | 16 | return $this->_data; |
|
115 | } |
||
116 | |||
117 | 14 | public function copied() |
|
118 | { |
||
119 | 14 | return $this->_copied; |
|
120 | } |
||
121 | |||
122 | 14 | public function filename($filename = NULL) |
|
123 | { |
||
124 | 14 | if ($filename !== NULL) |
|
125 | 14 | { |
|
126 | 14 | $this->_filename = $filename; |
|
127 | 14 | return $this; |
|
128 | } |
||
129 | 14 | return $this->_filename; |
|
130 | } |
||
131 | |||
132 | /** |
||
133 | * move the uploaded file to a specified location, throw an exception on upload error (with appropriate error message) |
||
134 | * Return the filename |
||
135 | * |
||
136 | * @param array $data |
||
137 | * @param string $directory |
||
138 | * @return string |
||
139 | */ |
||
140 | 5 | public static function process_type_upload(array $data, $directory) |
|
163 | |||
164 | /** |
||
165 | * Check if the file can be operated with. |
||
166 | * |
||
167 | * @param string $file |
||
168 | */ |
||
169 | 14 | public static function valid_file($file) |
|
173 | |||
174 | /** |
||
175 | * Copy the source data to a directory |
||
176 | * |
||
177 | * @param string $directory |
||
178 | * @return Upload_Source $this |
||
179 | */ |
||
180 | 14 | public function copy_to($directory) |
|
233 | } |
||
234 |