1 | <?php defined('SYSPATH') OR die('No direct script access.'); |
||
12 | class Kohana_Upload_Util { |
||
13 | |||
14 | 4 | public static function download($url, $directory, $filename = NULL) |
|
15 | { |
||
16 | 4 | $url = str_replace(' ', '%20', $url); |
|
17 | |||
18 | 4 | if ( ! Valid::url($url)) |
|
19 | return FALSE; |
||
20 | |||
21 | 4 | $curl = curl_init($url); |
|
22 | 4 | $file = Upload_Util::combine($directory, uniqid()); |
|
23 | |||
24 | 4 | $handle = fopen($file, 'w'); |
|
25 | 4 | $headers = new HTTP_Header(); |
|
26 | |||
27 | 4 | curl_setopt($curl, CURLOPT_FILE, $handle); |
|
28 | 4 | curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); |
|
29 | 4 | curl_setopt($curl, CURLOPT_HEADERFUNCTION, array($headers, 'parse_header_string')); |
|
30 | |||
31 | 4 | if (curl_exec($curl) === FALSE OR curl_getinfo($curl, CURLINFO_HTTP_CODE) !== 200) |
|
32 | { |
||
33 | 1 | fclose($handle); |
|
34 | 1 | unlink($file); |
|
35 | |||
36 | 1 | throw new Kohana_Exception('Curl: Download Error: :error, status :status on url :url', array(':url' => $url, ':status' => curl_getinfo($curl, CURLINFO_HTTP_CODE), ':error' => curl_error($curl))); |
|
37 | } |
||
38 | |||
39 | 3 | fclose($handle); |
|
40 | |||
41 | 3 | if ($filename === NULL) |
|
42 | { |
||
43 | 3 | if ( ! isset($headers['content-disposition']) |
|
44 | 3 | OR ! ($filename = Upload_Util::filename_from_content_disposition($headers['content-disposition']))) |
|
45 | { |
||
46 | 2 | $mime_type = curl_getinfo($curl, CURLINFO_CONTENT_TYPE); |
|
47 | 2 | $url = urldecode(curl_getinfo($curl, CURLINFO_EFFECTIVE_URL)); |
|
48 | |||
49 | 2 | $filename = Upload_Util::filename_from_url($url, $mime_type); |
|
50 | } |
||
51 | } |
||
52 | |||
53 | 3 | $filename = substr(pathinfo($filename, PATHINFO_FILENAME), 0, 60).'.'.pathinfo($filename, PATHINFO_EXTENSION); |
|
54 | |||
55 | 3 | $result_file = Upload_Util::combine($directory, $filename); |
|
56 | |||
57 | 3 | rename($file, $result_file); |
|
58 | |||
59 | 3 | return is_file($result_file) ? $filename : FALSE; |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * Move the contents of the stream to a specified directory with a given name |
||
64 | * |
||
65 | * @param string $stream |
||
66 | * @param string $directory |
||
|
|||
67 | * @param string $filename |
||
68 | */ |
||
69 | 1 | public static function stream_copy_to_file($stream, $file) |
|
70 | { |
||
71 | 1 | $stream_handle = fopen($stream, "r"); |
|
72 | 1 | $result_handle = fopen($file, 'w'); |
|
73 | |||
74 | 1 | $transfered_bytes = stream_copy_to_stream($stream_handle, $result_handle); |
|
75 | |||
76 | 1 | if ( (int) $transfered_bytes <= 0) |
|
77 | throw new Kohana_Exception('No data was transfered from :stream to :file ', array(':stream' => $stream, ':file' => Debug::path($file))); |
||
78 | |||
79 | 1 | fclose($stream_handle); |
|
80 | 1 | fclose($result_handle); |
|
81 | 1 | } |
|
82 | |||
83 | /** |
||
84 | * recursively delete directory |
||
85 | * |
||
86 | * @param string $directory |
||
87 | * @return boolean |
||
88 | */ |
||
89 | 17 | public static function rmdir($directory) |
|
90 | { |
||
91 | 17 | if ( ! is_dir($directory)) |
|
92 | 1 | return FALSE; |
|
93 | |||
94 | 16 | $files = array_diff(scandir($directory), array('.', '..')); |
|
95 | |||
96 | 16 | foreach ($files as $file) |
|
97 | { |
||
98 | 15 | $current = $directory.DIRECTORY_SEPARATOR.$file; |
|
99 | |||
100 | 15 | if (is_dir($current)) |
|
101 | { |
||
102 | 2 | Upload_Util::rmdir($current); |
|
103 | } |
||
104 | else |
||
105 | { |
||
106 | 15 | unlink($current); |
|
107 | } |
||
108 | } |
||
109 | 16 | return rmdir($directory); |
|
110 | } |
||
111 | |||
112 | /** |
||
113 | * Method to make a filename safe for writing on the filesystem, removing all strange characters |
||
114 | * @param string $filename |
||
115 | * @return string |
||
116 | */ |
||
117 | 13 | static public function sanitize($filename, $separator = '-') |
|
133 | |||
134 | /** |
||
135 | * Check if a file looks like a filename ("file.ext") |
||
136 | * @param string $filename |
||
137 | * @return boolean |
||
138 | */ |
||
139 | 16 | public static function is_filename($filename) |
|
143 | |||
144 | /** |
||
145 | * Return possible filenames from a given url. |
||
146 | * Filenames can be in the query or the url of the file itself |
||
147 | * |
||
148 | * @param string $url |
||
149 | * @return array |
||
150 | */ |
||
151 | 14 | public static function filenames_candidates_from_url($url) |
|
164 | |||
165 | /** |
||
166 | * Create a filename path from function arguments with / based on the operating system |
||
167 | * @code |
||
168 | * $filename = file::combine('usr','local','bin'); // will be "user/local/bin" |
||
169 | * @endcode |
||
170 | * @return string |
||
171 | * @author Ivan Kerin |
||
172 | */ |
||
173 | 31 | public static function combine() |
|
184 | |||
185 | /** |
||
186 | * Detirmine the filename from the url |
||
187 | * @param string $url |
||
188 | * @param string $mime_type |
||
189 | * @return string |
||
190 | */ |
||
191 | 9 | public static function filename_from_url($url, $mime_type = NULL) |
|
208 | |||
209 | 9 | public static function filename_from_content_disposition($content_disposition) |
|
210 | { |
||
211 | 9 | if (preg_match('/filename="?(.*?)"?$/', $content_disposition, $matches)) |
|
212 | 7 | return $matches[1]; |
|
213 | |||
214 | 2 | return NULL; |
|
215 | } |
||
216 | |||
217 | /** |
||
218 | * Perform transformations on an image and store it at a different location (or overwrite existing) |
||
219 | * |
||
220 | * @param string $from |
||
221 | * @param string $to |
||
222 | * @param array $transformations |
||
223 | */ |
||
224 | 3 | public static function transform_image($from, $to, array $transformations = array()) |
|
245 | } |
||
246 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.