1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK ---------------------------------- |
---|
3 | # |
---|
4 | # This file is part of cinecturlink2, a plugin for Dotclear 2. |
---|
5 | # |
---|
6 | # Copyright (c) 2009-2013 Jean-Christian Denis and contributors |
---|
7 | # contact@jcdenis.fr http://jcd.lv |
---|
8 | # |
---|
9 | # Licensed under the GPL version 2.0 license. |
---|
10 | # A copy of this license is available in LICENSE file or at |
---|
11 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
12 | # |
---|
13 | # -- END LICENSE BLOCK ------------------------------------ |
---|
14 | |
---|
15 | if (!defined('DC_RC_PATH')) { |
---|
16 | |
---|
17 | return null; |
---|
18 | } |
---|
19 | |
---|
20 | /** |
---|
21 | * @ingroup DC_PLUGIN_CINECTURLINK2 |
---|
22 | * @brief Share media you like - main methods. |
---|
23 | * @since 2.6 |
---|
24 | */ |
---|
25 | class cinecturlink2 |
---|
26 | { |
---|
27 | /** @var dcCore dcCore instance */ |
---|
28 | public $core; |
---|
29 | /** @var dbLayer dbLayer instance */ |
---|
30 | public $con; |
---|
31 | /** @var string Cinecturlink table name */ |
---|
32 | public $table; |
---|
33 | /** @var string Blog ID */ |
---|
34 | public $blog; |
---|
35 | |
---|
36 | /** |
---|
37 | * Contructor |
---|
38 | * |
---|
39 | * @param dcCore $core dcCore instance |
---|
40 | */ |
---|
41 | public function __construct(dcCore $core) |
---|
42 | { |
---|
43 | $core->blog->settings->addNamespace('cinecturlink2'); |
---|
44 | |
---|
45 | $this->core = $core; |
---|
46 | $this->con = $core->con; |
---|
47 | $this->table = $core->prefix.'cinecturlink2'; |
---|
48 | $this->blog = $core->con->escape($core->blog->id); |
---|
49 | } |
---|
50 | |
---|
51 | /** |
---|
52 | * Get links |
---|
53 | * |
---|
54 | * @param array $params Query params |
---|
55 | * @param boolean $count_only Count only result |
---|
56 | * @return record record instance |
---|
57 | */ |
---|
58 | public function getLinks($params=array(), $count_only=false) |
---|
59 | { |
---|
60 | if ($count_only) { |
---|
61 | $strReq = 'SELECT count(L.link_id) '; |
---|
62 | } |
---|
63 | else { |
---|
64 | $content_req = ''; |
---|
65 | if (!empty($params['columns']) && is_array($params['columns'])) { |
---|
66 | $content_req .= implode(', ', $params['columns']).', '; |
---|
67 | } |
---|
68 | |
---|
69 | $strReq = |
---|
70 | 'SELECT L.link_id, L.blog_id, L.cat_id, L.user_id, L.link_type, '. |
---|
71 | $content_req. |
---|
72 | 'L.link_creadt, L.link_upddt, L.link_note, L.link_count, '. |
---|
73 | 'L.link_title, L.link_desc, L.link_author, '. |
---|
74 | 'L.link_lang, L.link_url, L.link_img, '. |
---|
75 | 'U.user_name, U.user_firstname, U.user_displayname, U.user_email, '. |
---|
76 | 'U.user_url, '. |
---|
77 | 'C.cat_title, C.cat_desc '; |
---|
78 | } |
---|
79 | |
---|
80 | $strReq .= |
---|
81 | 'FROM '.$this->table.' L '. |
---|
82 | 'INNER JOIN '.$this->core->prefix.'user U ON U.user_id = L.user_id '. |
---|
83 | 'LEFT OUTER JOIN '.$this->table.'_cat C ON L.cat_id = C.cat_id '; |
---|
84 | |
---|
85 | if (!empty($params['from'])) { |
---|
86 | $strReq .= $params['from'].' '; |
---|
87 | } |
---|
88 | |
---|
89 | $strReq .= "WHERE L.blog_id = '".$this->blog."' "; |
---|
90 | |
---|
91 | if (isset($params['link_type'])) { |
---|
92 | if (is_array($params['link_type']) && !empty($params['link_type'])) { |
---|
93 | $strReq .= 'AND L.link_type '.$this->con->in($params['link_type']); |
---|
94 | } |
---|
95 | elseif ($params['link_type'] != '') { |
---|
96 | $strReq .= "AND L.link_type = '".$this->con->escape($params['link_type'])."' "; |
---|
97 | } |
---|
98 | } |
---|
99 | else { |
---|
100 | $strReq .= "AND L.link_type = 'cinecturlink' "; |
---|
101 | } |
---|
102 | |
---|
103 | if (!empty($params['link_id'])) { |
---|
104 | if (is_array($params['link_id'])) { |
---|
105 | array_walk($params['link_id'], create_function('&$v,$k', 'if($v!==null){$v=(integer)$v;}')); |
---|
106 | } |
---|
107 | else { |
---|
108 | $params['link_id'] = array((integer) $params['link_id']); |
---|
109 | } |
---|
110 | $strReq .= 'AND L.link_id '.$this->con->in($params['link_id']); |
---|
111 | } |
---|
112 | |
---|
113 | if (!empty($params['cat_id'])) { |
---|
114 | if (is_array($params['cat_id'])) { |
---|
115 | array_walk($params['cat_id'], create_function('&$v,$k', 'if($v!==null){$v=(integer)$v;}')); |
---|
116 | } |
---|
117 | else { |
---|
118 | $params['cat_id'] = array((integer) $params['cat_id']); |
---|
119 | } |
---|
120 | $strReq .= 'AND L.cat_id '.$this->con->in($params['cat_id']); |
---|
121 | } |
---|
122 | if (!empty($params['cat_title'])) { |
---|
123 | $strReq .= "AND C.cat_title = '".$this->con->escape($params['cat_title'])."' "; |
---|
124 | } |
---|
125 | |
---|
126 | if (!empty($params['link_title'])) { |
---|
127 | $strReq .= "AND L.link_title = '".$this->con->escape($params['link_title'])."' "; |
---|
128 | } |
---|
129 | |
---|
130 | if (!empty($params['link_lang'])) { |
---|
131 | $strReq .= "AND L.link_lang = '".$this->con->escape($params['link_lang'])."' "; |
---|
132 | } |
---|
133 | |
---|
134 | if (!empty($params['sql'])) { |
---|
135 | $strReq .= $params['sql'].' '; |
---|
136 | } |
---|
137 | |
---|
138 | if (!$count_only) { |
---|
139 | if (!empty($params['order'])) { |
---|
140 | $strReq .= 'ORDER BY '.$this->con->escape($params['order']).' '; |
---|
141 | } |
---|
142 | else { |
---|
143 | $strReq .= 'ORDER BY L.link_upddt DESC '; |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | if (!$count_only && !empty($params['limit'])) { |
---|
148 | $strReq .= $this->con->limit($params['limit']); |
---|
149 | } |
---|
150 | |
---|
151 | return $this->con->select($strReq); |
---|
152 | } |
---|
153 | |
---|
154 | /** |
---|
155 | * Add link |
---|
156 | * |
---|
157 | * @param cursor $cur cursor instance |
---|
158 | */ |
---|
159 | public function addLink(cursor $cur) |
---|
160 | { |
---|
161 | $this->con->writeLock($this->table); |
---|
162 | |
---|
163 | try { |
---|
164 | if ($cur->link_title == '') { |
---|
165 | throw new Exception(__('No link title')); |
---|
166 | } |
---|
167 | if ($cur->link_desc == '') { |
---|
168 | throw new Exception(__('No link description')); |
---|
169 | } |
---|
170 | if ('' == $cur->link_note) { |
---|
171 | $cur->link_note = 10; |
---|
172 | } |
---|
173 | if (0 > $cur->link_note || $cur->link_note > 20) { |
---|
174 | $cur->link_note = 10; |
---|
175 | } |
---|
176 | |
---|
177 | $cur->link_id = $this->getNextLinkId(); |
---|
178 | $cur->blog_id = $this->blog; |
---|
179 | $cur->user_id = $this->core->auth->userID(); |
---|
180 | $cur->link_creadt = date('Y-m-d H:i:s'); |
---|
181 | $cur->link_upddt = date('Y-m-d H:i:s'); |
---|
182 | $cur->link_pos = 0; |
---|
183 | $cur->link_count = 0; |
---|
184 | $cur->insert(); |
---|
185 | $this->con->unlock(); |
---|
186 | } |
---|
187 | catch (Exception $e) { |
---|
188 | $this->con->unlock(); |
---|
189 | throw $e; |
---|
190 | } |
---|
191 | $this->trigger(); |
---|
192 | |
---|
193 | # --BEHAVIOR-- cinecturlink2AfterAddLink |
---|
194 | $this->core->callBehavior('cinecturlink2AfterAddLink', $cur); |
---|
195 | |
---|
196 | return $cur->link_id; |
---|
197 | } |
---|
198 | |
---|
199 | /** |
---|
200 | * Update link |
---|
201 | * |
---|
202 | * @param integer $id Link ID |
---|
203 | * @param cursor $cur cursor instance |
---|
204 | * @param boolean $behavior Call related behaviors |
---|
205 | */ |
---|
206 | public function updLink($id, cursor $cur, $behavior=true) |
---|
207 | { |
---|
208 | $id = (integer) $id; |
---|
209 | |
---|
210 | if (empty($id)) { |
---|
211 | throw new Exception(__('No such link ID')); |
---|
212 | } |
---|
213 | |
---|
214 | $cur->link_upddt = date('Y-m-d H:i:s'); |
---|
215 | |
---|
216 | $cur->update("WHERE link_id = ".$id." AND blog_id = '".$this->blog."' "); |
---|
217 | $this->trigger(); |
---|
218 | |
---|
219 | if ($behavior) { |
---|
220 | # --BEHAVIOR-- cinecturlink2AfterUpdLink |
---|
221 | $this->core->callBehavior('cinecturlink2AfterUpdLink', $cur, $id); |
---|
222 | } |
---|
223 | } |
---|
224 | |
---|
225 | /** |
---|
226 | * Delete link |
---|
227 | * |
---|
228 | * @param integer $id Link ID |
---|
229 | */ |
---|
230 | public function delLink($id) |
---|
231 | { |
---|
232 | $id = (integer) $id; |
---|
233 | |
---|
234 | if (empty($id)) { |
---|
235 | throw new Exception(__('No such link ID')); |
---|
236 | } |
---|
237 | |
---|
238 | # --BEHAVIOR-- cinecturlink2BeforeDelLink |
---|
239 | $this->core->callBehavior('cinecturlink2BeforeDelLink', $id); |
---|
240 | |
---|
241 | $this->con->execute( |
---|
242 | 'DELETE FROM '.$this->table.' '. |
---|
243 | 'WHERE link_id = '.$id.' '. |
---|
244 | "AND blog_id = '".$this->blog."' " |
---|
245 | ); |
---|
246 | |
---|
247 | $this->trigger(); |
---|
248 | } |
---|
249 | |
---|
250 | /** |
---|
251 | * Get next link ID |
---|
252 | * |
---|
253 | * @return integer Next link ID |
---|
254 | */ |
---|
255 | private function getNextLinkId() |
---|
256 | { |
---|
257 | return $this->con->select( |
---|
258 | 'SELECT MAX(link_id) FROM '.$this->table.' ' |
---|
259 | )->f(0) + 1; |
---|
260 | } |
---|
261 | |
---|
262 | /** |
---|
263 | * Get categories |
---|
264 | * |
---|
265 | * @param array $params Query params |
---|
266 | * @param boolean $count_only Count only result |
---|
267 | * @return record record instance |
---|
268 | */ |
---|
269 | public function getCategories($params=array(), $count_only=false) |
---|
270 | { |
---|
271 | if ($count_only) { |
---|
272 | $strReq = 'SELECT count(C.cat_id) '; |
---|
273 | } |
---|
274 | else { |
---|
275 | $content_req = ''; |
---|
276 | if (!empty($params['columns']) && is_array($params['columns'])) { |
---|
277 | $content_req .= implode(', ', $params['columns']).', '; |
---|
278 | } |
---|
279 | |
---|
280 | $strReq = |
---|
281 | 'SELECT C.cat_id, C.blog_id, C.cat_title, C.cat_desc, '. |
---|
282 | $content_req. |
---|
283 | 'C.cat_pos, C.cat_creadt, C.cat_upddt '; |
---|
284 | } |
---|
285 | |
---|
286 | $strReq .= 'FROM '.$this->table.'_cat C '; |
---|
287 | |
---|
288 | if (!empty($params['from'])) { |
---|
289 | $strReq .= $params['from'].' '; |
---|
290 | } |
---|
291 | |
---|
292 | $strReq .= "WHERE C.blog_id = '".$this->blog."' "; |
---|
293 | |
---|
294 | if (!empty($params['cat_id'])) { |
---|
295 | if (is_array($params['cat_id'])) { |
---|
296 | array_walk($params['cat_id'], create_function('&$v,$k', 'if($v!==null){$v=(integer)$v;}')); |
---|
297 | } |
---|
298 | else { |
---|
299 | $params['cat_id'] = array((integer) $params['cat_id']); |
---|
300 | } |
---|
301 | $strReq .= 'AND C.cat_id '.$this->con->in($params['cat_id']); |
---|
302 | } |
---|
303 | |
---|
304 | if (!empty($params['cat_title'])) { |
---|
305 | $strReq .= "AND C.cat_title = '".$this->con->escape($params['cat_title'])."' "; |
---|
306 | } |
---|
307 | |
---|
308 | if (!empty($params['sql'])) { |
---|
309 | $strReq .= $params['sql'].' '; |
---|
310 | } |
---|
311 | |
---|
312 | if (!$count_only) { |
---|
313 | if (!empty($params['order'])) { |
---|
314 | $strReq .= 'ORDER BY '.$this->con->escape($params['order']).' '; |
---|
315 | } |
---|
316 | else { |
---|
317 | $strReq .= 'ORDER BY cat_pos ASC '; |
---|
318 | } |
---|
319 | } |
---|
320 | |
---|
321 | if (!$count_only && !empty($params['limit'])) { |
---|
322 | $strReq .= $this->con->limit($params['limit']); |
---|
323 | } |
---|
324 | |
---|
325 | return $this->con->select($strReq); |
---|
326 | } |
---|
327 | |
---|
328 | /** |
---|
329 | * Add category |
---|
330 | * |
---|
331 | * @param cursor $cur cursor instance |
---|
332 | * @return integer New category ID |
---|
333 | */ |
---|
334 | public function addCategory(cursor $cur) |
---|
335 | { |
---|
336 | $this->con->writeLock($this->table.'_cat'); |
---|
337 | |
---|
338 | try { |
---|
339 | if ($cur->cat_title == '') { |
---|
340 | throw new Exception(__('No category title')); |
---|
341 | } |
---|
342 | if ($cur->cat_desc == '') { |
---|
343 | throw new Exception(__('No category description')); |
---|
344 | } |
---|
345 | |
---|
346 | $cur->cat_id = $this->getNextCatId(); |
---|
347 | $cur->cat_pos = $this->getNextCatPos(); |
---|
348 | $cur->blog_id = $this->blog; |
---|
349 | $cur->cat_creadt = date('Y-m-d H:i:s'); |
---|
350 | $cur->cat_upddt = date('Y-m-d H:i:s'); |
---|
351 | $cur->insert(); |
---|
352 | $this->con->unlock(); |
---|
353 | } |
---|
354 | catch (Exception $e) { |
---|
355 | $this->con->unlock(); |
---|
356 | throw $e; |
---|
357 | } |
---|
358 | $this->trigger(); |
---|
359 | |
---|
360 | return $cur->cat_id; |
---|
361 | } |
---|
362 | |
---|
363 | /** |
---|
364 | * Update category |
---|
365 | * |
---|
366 | * @param integer $id Category ID |
---|
367 | * @param cursor $cur cursor instance |
---|
368 | */ |
---|
369 | public function updCategory($id, cursor $cur) |
---|
370 | { |
---|
371 | $id = (integer) $id; |
---|
372 | |
---|
373 | if (empty($id)) { |
---|
374 | throw new Exception(__('No such category ID')); |
---|
375 | } |
---|
376 | |
---|
377 | $cur->cat_upddt = date('Y-m-d H:i:s'); |
---|
378 | |
---|
379 | $cur->update("WHERE cat_id = ".$id." AND blog_id = '".$this->blog."' "); |
---|
380 | $this->trigger(); |
---|
381 | } |
---|
382 | |
---|
383 | /** |
---|
384 | * Delete category |
---|
385 | * |
---|
386 | * @param integer $id Category ID |
---|
387 | */ |
---|
388 | public function delCategory($id) |
---|
389 | { |
---|
390 | $id = (integer) $id; |
---|
391 | |
---|
392 | if (empty($id)) { |
---|
393 | throw new Exception(__('No such category ID')); |
---|
394 | } |
---|
395 | |
---|
396 | $this->con->execute( |
---|
397 | 'DELETE FROM '.$this->table.'_cat '. |
---|
398 | 'WHERE cat_id = '.$id.' '. |
---|
399 | "AND blog_id = '".$this->blog."' " |
---|
400 | ); |
---|
401 | |
---|
402 | # Update link cat to NULL |
---|
403 | $cur = $this->con->openCursor($this->table); |
---|
404 | $cur->cat_id = NULL; |
---|
405 | $cur->link_upddt = date('Y-m-d H:i:s'); |
---|
406 | $cur->update("WHERE cat_id = ".$id." AND blog_id = '".$this->blog."' "); |
---|
407 | |
---|
408 | $this->trigger(); |
---|
409 | } |
---|
410 | |
---|
411 | /** |
---|
412 | * Get next category ID |
---|
413 | * |
---|
414 | * @return integer Next category ID |
---|
415 | */ |
---|
416 | private function getNextCatId() |
---|
417 | { |
---|
418 | return $this->con->select( |
---|
419 | 'SELECT MAX(cat_id) FROM '.$this->table.'_cat ' |
---|
420 | )->f(0) + 1; |
---|
421 | } |
---|
422 | |
---|
423 | /** |
---|
424 | * Get next category position |
---|
425 | * |
---|
426 | * @return integer Next category position |
---|
427 | */ |
---|
428 | private function getNextCatPos() |
---|
429 | { |
---|
430 | return $this->con->select( |
---|
431 | 'SELECT MAX(cat_pos) FROM '.$this->table.'_cat '. |
---|
432 | "WHERE blog_id = '".$this->blog."' " |
---|
433 | )->f(0) + 1; |
---|
434 | } |
---|
435 | |
---|
436 | /** |
---|
437 | * Trigger event |
---|
438 | */ |
---|
439 | private function trigger() |
---|
440 | { |
---|
441 | $this->core->blog->triggerBlog(); |
---|
442 | } |
---|
443 | |
---|
444 | /** |
---|
445 | * Check if a directory exists and is writable |
---|
446 | * |
---|
447 | * @param string $root Root |
---|
448 | * @param string $folder Folder to create into root folder |
---|
449 | * @param boolean $throw Throw exception or not |
---|
450 | * @return boolean True if exists and writable |
---|
451 | */ |
---|
452 | public static function test_folder($root, $folder, $throw=false) |
---|
453 | { |
---|
454 | if (!is_dir($root.'/'.$folder)) { |
---|
455 | if (!is_dir($root) || !is_writable($root) || !mkdir($root.'/'.$folder)) { |
---|
456 | if ($throw) { |
---|
457 | throw new Exception(__('Failed to create public folder for images.')); |
---|
458 | } |
---|
459 | |
---|
460 | return false; |
---|
461 | } |
---|
462 | } |
---|
463 | |
---|
464 | return true; |
---|
465 | } |
---|
466 | } |
---|