1 | <?php |
---|
2 | # ***** BEGIN LICENSE BLOCK ***** |
---|
3 | # |
---|
4 | # This file is part of DL Manager, a plugin for Dotclear 2 |
---|
5 | # Copyright (C) 2008,2010 Moe (http://gniark.net/) and Tomtom (http://blog.zenstyle.fr) |
---|
6 | # |
---|
7 | # DL Manager is free software; you can redistribute it and/or |
---|
8 | # modify it under the terms of the GNU General Public License v2.0 |
---|
9 | # as published by the Free Software Foundation. |
---|
10 | # |
---|
11 | # DL Manager is distributed in the hope that it will be useful, |
---|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | # GNU General Public License for more details. |
---|
15 | # |
---|
16 | # You should have received a copy of the GNU General Public License |
---|
17 | # along with this program; if not, write to the Free Software Foundation, |
---|
18 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
19 | # |
---|
20 | # Images are from Silk Icons : http://www.famfamfam.com/lab/icons/silk/ |
---|
21 | # |
---|
22 | # ***** END LICENSE BLOCK ***** |
---|
23 | |
---|
24 | if (!defined('DC_RC_PATH')) {return;} |
---|
25 | |
---|
26 | /** |
---|
27 | @ingroup Download manager |
---|
28 | @brief General class |
---|
29 | */ |
---|
30 | class dlManager |
---|
31 | { |
---|
32 | /** |
---|
33 | return list of subdirectories |
---|
34 | @return <b>array</b> Subdirectories |
---|
35 | */ |
---|
36 | public static function listDirs($in_jail=false,$empty_value=false) |
---|
37 | { |
---|
38 | global $core; |
---|
39 | |
---|
40 | $dirs = array(); |
---|
41 | |
---|
42 | # empty default value |
---|
43 | if ($empty_value) |
---|
44 | { |
---|
45 | $dirs = array(''=> ''); |
---|
46 | } |
---|
47 | |
---|
48 | try |
---|
49 | { |
---|
50 | if (!is_object($core->media)) |
---|
51 | { |
---|
52 | $core->media = new dcMedia($core); |
---|
53 | } |
---|
54 | # from gallery/gal.php |
---|
55 | foreach ($core->media->getRootDirs() as $v) |
---|
56 | { |
---|
57 | $path = $v->relname; |
---|
58 | if ($in_jail) |
---|
59 | { |
---|
60 | if (self::inJail($path)) |
---|
61 | { |
---|
62 | $dirs[$path] = $path; |
---|
63 | } |
---|
64 | } |
---|
65 | else |
---|
66 | { |
---|
67 | $dirs[$path] = $path; |
---|
68 | } |
---|
69 | } |
---|
70 | } |
---|
71 | catch (Exception $e) |
---|
72 | { |
---|
73 | $core->error->add($e->getMessage()); |
---|
74 | } |
---|
75 | |
---|
76 | return($dirs); |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | get sort values |
---|
81 | @param empty_value <b>boolean</b> Add an empty value in the array |
---|
82 | @return <b>array</b> sort values |
---|
83 | */ |
---|
84 | public static function getSortValues($empty_value=false) |
---|
85 | { |
---|
86 | $array = (($empty_value === true) ? array('' => '') : array()); |
---|
87 | |
---|
88 | # from /dotclear/admin/media.php |
---|
89 | return(array_merge($array,array( |
---|
90 | __('By names, ascendant') => 'name-asc', |
---|
91 | __('By names, descendant') => 'name-desc', |
---|
92 | __('By dates, ascendant') => 'date-asc', |
---|
93 | __('By dates, descendant') => 'date-desc' |
---|
94 | ))); |
---|
95 | } |
---|
96 | |
---|
97 | /** |
---|
98 | return DL Manager URL |
---|
99 | @return <b>string</b> URL |
---|
100 | */ |
---|
101 | public static function pageURL() |
---|
102 | { |
---|
103 | global $core; |
---|
104 | |
---|
105 | return ($core->blog->url.$core->url->getBase('media')); |
---|
106 | } |
---|
107 | |
---|
108 | /** |
---|
109 | make BreadCrumb |
---|
110 | @param dir <b>string</b> path directory |
---|
111 | @return <b>record</b> BreadCrumb |
---|
112 | */ |
---|
113 | public static function breadCrumb($dir) |
---|
114 | { |
---|
115 | # BreadCrumb |
---|
116 | $base_url = self::pageURL().'/'; |
---|
117 | $dirs = explode('/',$dir); |
---|
118 | $path = ''; |
---|
119 | |
---|
120 | $breadCrumb = array(); |
---|
121 | |
---|
122 | foreach ($dirs as $dir) |
---|
123 | { |
---|
124 | $dir = trim($dir); |
---|
125 | |
---|
126 | if (!empty($dir)) |
---|
127 | { |
---|
128 | $path = (($path == '') ? $dir : $path.'/'.$dir); |
---|
129 | $breadCrumb[] = array( |
---|
130 | 'name' => $dir, |
---|
131 | 'url' => $base_url.$path |
---|
132 | ); |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | return(staticRecord::newFromArray($breadCrumb)); |
---|
137 | } |
---|
138 | |
---|
139 | /** |
---|
140 | test if a file or a directory is in "jail" |
---|
141 | @param path <b>string</b> path |
---|
142 | @return <b>boolean</b> BreadCrumb |
---|
143 | */ |
---|
144 | public static function inJail($path) |
---|
145 | { |
---|
146 | global $core; |
---|
147 | |
---|
148 | $core->blog->settings->addNamespace('dlManager'); |
---|
149 | $root = $core->blog->settings->dlManager->dlmanager_root; |
---|
150 | |
---|
151 | if (!empty($root) && (strpos($path,$root) !== 0)) |
---|
152 | { |
---|
153 | return false; |
---|
154 | } |
---|
155 | |
---|
156 | return true; |
---|
157 | } |
---|
158 | |
---|
159 | /** |
---|
160 | get a static record for items |
---|
161 | @param array <b>array</b> path directory |
---|
162 | @return <b>record</b> Items |
---|
163 | */ |
---|
164 | public static function getItems($array) |
---|
165 | { |
---|
166 | global $core; |
---|
167 | |
---|
168 | $count_dl = unserialize($core->blog->settings->dlmanager_count_dl); |
---|
169 | if (!is_array($count_dl)) |
---|
170 | { |
---|
171 | $count_dl = array(); |
---|
172 | } |
---|
173 | |
---|
174 | $items = array(); |
---|
175 | |
---|
176 | foreach ($array as $k => $v) |
---|
177 | { |
---|
178 | $dl = '0'; |
---|
179 | if ($core->blog->settings->dlmanager_counter) |
---|
180 | { |
---|
181 | if ((isset($v->media_id)) |
---|
182 | && (array_key_exists($v->media_id,$count_dl))) |
---|
183 | { |
---|
184 | $dl = $count_dl[$v->media_id]; |
---|
185 | } |
---|
186 | } |
---|
187 | $items[] = array( |
---|
188 | 'dir_url' => (isset($v->dir_url) ? $v->dir_url : ''), |
---|
189 | 'relname' => (isset($v->relname) ? $v->relname : ''), |
---|
190 | 'media_type' => (isset($v->media_type) ? $v->media_type : ''), |
---|
191 | 'media_title' => (isset($v->media_title) ? $v->media_title : ''), |
---|
192 | 'size' => (isset($v->size) ? $v->size : ''), |
---|
193 | 'file' => (isset($v->file) ? $v->file : ''), |
---|
194 | 'file_url' => (isset($v->file_url) ? $v->file_url : ''), |
---|
195 | 'media_id' => (isset($v->media_id) ? $v->media_id : ''), |
---|
196 | 'basename' => (isset($v->basename) ? $v->basename : ''), |
---|
197 | 'extension' => (isset($v->extension) ? $v->extension : ''), |
---|
198 | 'type' => (isset($v->type) ? $v->type : ''), |
---|
199 | 'media_type' => (isset($v->media_type) ? $v->media_type : ''), |
---|
200 | 'media_dtstr' => (isset($v->media_dtstr) ? $v->media_dtstr : ''), |
---|
201 | 'media_thumb' => (isset($v->media_thumb) ? $v->media_thumb : ''), |
---|
202 | 'media_meta' => (isset($v->media_meta) ? $v->media_meta : ''), |
---|
203 | 'count_dl' => $dl, |
---|
204 | ); |
---|
205 | } |
---|
206 | |
---|
207 | return(staticRecord::newFromArray($items)); |
---|
208 | } |
---|
209 | |
---|
210 | /** |
---|
211 | get zip content (files) of a media item |
---|
212 | @param array <b>fileItem</b> File item |
---|
213 | @return <b>record</b> Files |
---|
214 | */ |
---|
215 | public static function getZipContent($item) |
---|
216 | { |
---|
217 | global $core; |
---|
218 | |
---|
219 | $files = array(); |
---|
220 | |
---|
221 | $content = $core->media->getZipContent($item); |
---|
222 | |
---|
223 | foreach ($content as $file => $v) |
---|
224 | { |
---|
225 | $files[] = array('file' => $file); |
---|
226 | } |
---|
227 | |
---|
228 | return(staticRecord::newFromArray($files)); |
---|
229 | } |
---|
230 | |
---|
231 | /** |
---|
232 | get image metadata a media item |
---|
233 | @param array <b>fileItem</b> File item |
---|
234 | @return <b>record</b> Image metadata |
---|
235 | */ |
---|
236 | public static function getImageMeta($item) |
---|
237 | { |
---|
238 | global $core; |
---|
239 | |
---|
240 | $meta = array(); |
---|
241 | |
---|
242 | foreach ($item->media_meta as $k => $v) |
---|
243 | { |
---|
244 | if (!empty($v)) |
---|
245 | { |
---|
246 | $meta[] = array( |
---|
247 | 'name' => $k , |
---|
248 | 'value' => $v |
---|
249 | ); |
---|
250 | } |
---|
251 | } |
---|
252 | |
---|
253 | return(staticRecord::newFromArray($meta)); |
---|
254 | } |
---|
255 | |
---|
256 | /** |
---|
257 | find entries containing this media |
---|
258 | @param path <b>string</b> path |
---|
259 | @return <b>boolean</b> BreadCrumb |
---|
260 | */ |
---|
261 | public static function findPosts($id) |
---|
262 | { |
---|
263 | global $core; |
---|
264 | |
---|
265 | $file = $core->media->getFile($id); |
---|
266 | |
---|
267 | # from /dotclear/admin/media_item.php |
---|
268 | $params = array( |
---|
269 | 'post_type' => '', |
---|
270 | 'from' => 'LEFT OUTER JOIN '.$core->prefix.'post_media PM ON P.post_id = PM.post_id ', |
---|
271 | 'sql' => 'AND ('. |
---|
272 | 'PM.media_id = '.(integer) $id.' '. |
---|
273 | "OR post_content_xhtml LIKE '%".$core->con->escape($file->relname)."%' ". |
---|
274 | "OR post_excerpt_xhtml LIKE '%".$core->con->escape($file->relname)."%' " |
---|
275 | ); |
---|
276 | |
---|
277 | if ($file->media_image) |
---|
278 | { # We look for thumbnails too |
---|
279 | $media_root = $core->blog->host.path::clean($core->blog->settings->public_url).'/'; |
---|
280 | foreach ($file->media_thumb as $v) { |
---|
281 | $v = preg_replace('/^'.preg_quote($media_root,'/').'/','',$v); |
---|
282 | $params['sql'] .= "OR post_content_xhtml LIKE '%".$core->con->escape($v)."%' "; |
---|
283 | $params['sql'] .= "OR post_excerpt_xhtml LIKE '%".$core->con->escape($v)."%' "; |
---|
284 | } |
---|
285 | } |
---|
286 | |
---|
287 | $params['sql'] .= ') '; |
---|
288 | |
---|
289 | $rs = $core->blog->getPosts($params); |
---|
290 | |
---|
291 | return $rs; |
---|
292 | } |
---|
293 | } |
---|