1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK ---------------------------------- |
---|
3 | # |
---|
4 | # This file is part of zoneclearFeedServer, a plugin for Dotclear 2. |
---|
5 | # |
---|
6 | # Copyright (c) 2009-2013 Jean-Christian Denis, BG 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')){return;} |
---|
16 | |
---|
17 | class zoneclearFeedServer |
---|
18 | { |
---|
19 | public static $nethttp_timeout = 5; |
---|
20 | public static $nethttp_agent = 'zoneclearFeedServer - http://zoneclear.org'; |
---|
21 | public static $nethttp_maxredirect = 2; |
---|
22 | |
---|
23 | public $core; |
---|
24 | public $con; |
---|
25 | private $blog; |
---|
26 | private $table; |
---|
27 | private $lock = null; |
---|
28 | |
---|
29 | public function __construct($core) |
---|
30 | { |
---|
31 | $this->core = $core; |
---|
32 | $this->con = $core->con; |
---|
33 | $this->blog = $core->con->escape($core->blog->id); |
---|
34 | $this->table = $core->prefix.'zc_feed'; |
---|
35 | } |
---|
36 | |
---|
37 | # Short openCursor |
---|
38 | public function openCursor() |
---|
39 | { |
---|
40 | return $this->con->openCursor($this->table); |
---|
41 | } |
---|
42 | |
---|
43 | # Update record |
---|
44 | public function updFeed($id,$cur) |
---|
45 | { |
---|
46 | $this->con->writeLock($this->table); |
---|
47 | |
---|
48 | try |
---|
49 | { |
---|
50 | $id = (integer) $id; |
---|
51 | |
---|
52 | if ($id < 1) |
---|
53 | { |
---|
54 | throw new Exception(__('No such ID')); |
---|
55 | } |
---|
56 | $cur->feed_upddt = date('Y-m-d H:i:s'); |
---|
57 | |
---|
58 | $cur->update("WHERE feed_id = ".$id." AND blog_id = '".$this->blog."' "); |
---|
59 | $this->con->unlock(); |
---|
60 | $this->trigger(); |
---|
61 | } |
---|
62 | catch (Exception $e) |
---|
63 | { |
---|
64 | $this->con->unlock(); |
---|
65 | throw $e; |
---|
66 | } |
---|
67 | |
---|
68 | # --BEHAVIOR-- zoneclearFeedServerAfterUpdFeed |
---|
69 | $this->core->callBehavior('zoneclearFeedServerAfterUpdFeed',$cur,$id); |
---|
70 | } |
---|
71 | |
---|
72 | # Add record |
---|
73 | public function addFeed($cur) |
---|
74 | { |
---|
75 | $this->con->writeLock($this->table); |
---|
76 | |
---|
77 | try |
---|
78 | { |
---|
79 | $cur->feed_id = $this->getNextId(); |
---|
80 | $cur->blog_id = $this->blog; |
---|
81 | $cur->feed_creadt = date('Y-m-d H:i:s'); |
---|
82 | $cur->feed_upddt = date('Y-m-d H:i:s'); |
---|
83 | |
---|
84 | //add getFeedCursor here |
---|
85 | |
---|
86 | $cur->insert(); |
---|
87 | $this->con->unlock(); |
---|
88 | $this->trigger(); |
---|
89 | } |
---|
90 | catch (Exception $e) |
---|
91 | { |
---|
92 | $this->con->unlock(); |
---|
93 | throw $e; |
---|
94 | } |
---|
95 | |
---|
96 | # --BEHAVIOR-- zoneclearFeedServerAfterAddFeed |
---|
97 | $this->core->callBehavior('zoneclearFeedServerAfterAddFeed',$cur); |
---|
98 | |
---|
99 | return $cur->feed_id; |
---|
100 | } |
---|
101 | |
---|
102 | # Quick enable / disable feed |
---|
103 | public function enableFeed($id,$enable=true,$time=null) |
---|
104 | { |
---|
105 | try |
---|
106 | { |
---|
107 | $id = (integer) $id; |
---|
108 | |
---|
109 | if ($id < 1) |
---|
110 | { |
---|
111 | throw new Exception(__('No such ID')); |
---|
112 | } |
---|
113 | $cur = $this->openCursor(); |
---|
114 | $this->con->writeLock($this->table); |
---|
115 | |
---|
116 | $cur->feed_upddt = date('Y-m-d H:i:s'); |
---|
117 | |
---|
118 | $cur->feed_status = (integer) $enable; |
---|
119 | if (null !== $time) |
---|
120 | { |
---|
121 | $cur->feed_upd_last = (integer) $time; |
---|
122 | } |
---|
123 | |
---|
124 | $cur->update("WHERE feed_id = ".$id." AND blog_id = '".$this->blog."' "); |
---|
125 | $this->con->unlock(); |
---|
126 | $this->trigger(); |
---|
127 | } |
---|
128 | catch (Exception $e) |
---|
129 | { |
---|
130 | $this->con->unlock(); |
---|
131 | throw $e; |
---|
132 | } |
---|
133 | |
---|
134 | # --BEHAVIOR-- zoneclearFeedServerAfterEnableFeed |
---|
135 | $this->core->callBehavior('zoneclearFeedServerAfterEnableFeed',$id,$enable,$time); |
---|
136 | } |
---|
137 | |
---|
138 | # Delete record (this not deletes post) |
---|
139 | public function delFeed($id) |
---|
140 | { |
---|
141 | $id = (integer) $id; |
---|
142 | |
---|
143 | if ($id < 1) |
---|
144 | { |
---|
145 | throw new Exception(__('No such ID')); |
---|
146 | } |
---|
147 | |
---|
148 | # --BEHAVIOR-- zoneclearFeedServerBeforeDelFeed |
---|
149 | $this->core->callBehavior('zoneclearFeedServerBeforeDelFeed',$id); |
---|
150 | |
---|
151 | $this->con->execute( |
---|
152 | 'DELETE FROM '.$this->table.' '. |
---|
153 | 'WHERE feed_id = '.$id.' '. |
---|
154 | "AND blog_id = '".$this->blog."' " |
---|
155 | ); |
---|
156 | $this->trigger(); |
---|
157 | } |
---|
158 | |
---|
159 | # Get related posts |
---|
160 | public function getPostsByFeed($params=array(),$count_only=false) |
---|
161 | { |
---|
162 | if (!isset($params['feed_id'])) |
---|
163 | { |
---|
164 | return null; |
---|
165 | } |
---|
166 | |
---|
167 | $params['from'] = |
---|
168 | 'LEFT JOIN '.$this->core->prefix.'meta F '. |
---|
169 | 'ON P.post_id = F.post_id '; |
---|
170 | $params['sql'] = |
---|
171 | "AND P.blog_id = '".$this->blog."' ". |
---|
172 | "AND F.meta_type = 'zoneclearfeed_id' ". |
---|
173 | "AND F.meta_id = '".$this->con->escape($params['feed_id'])."' "; |
---|
174 | |
---|
175 | unset($params['feed_id']); |
---|
176 | |
---|
177 | return $this->core->blog->getPosts($params,$count_only); |
---|
178 | } |
---|
179 | |
---|
180 | # get record |
---|
181 | public function getFeeds($params=array(),$count_only=false) |
---|
182 | { |
---|
183 | if ($count_only) |
---|
184 | { |
---|
185 | $strReq = 'SELECT count(Z.feed_id) '; |
---|
186 | } |
---|
187 | else |
---|
188 | { |
---|
189 | $content_req = ''; |
---|
190 | if (!empty($params['columns']) && is_array($params['columns'])) |
---|
191 | { |
---|
192 | $content_req .= implode(', ',$params['columns']).', '; |
---|
193 | } |
---|
194 | |
---|
195 | $strReq = |
---|
196 | 'SELECT Z.feed_id, Z.feed_creadt, Z.feed_upddt, Z.feed_type, '. |
---|
197 | 'Z.blog_id, Z.cat_id, '. |
---|
198 | 'Z.feed_upd_int, Z.feed_upd_last, Z.feed_status, '. |
---|
199 | $content_req. |
---|
200 | 'LOWER(Z.feed_name) as lowername, Z.feed_name, Z.feed_desc, '. |
---|
201 | 'Z.feed_url, Z.feed_feed, Z.feed_get_tags, '. |
---|
202 | 'Z.feed_tags, Z.feed_owner, Z.feed_tweeter, Z.feed_lang, '. |
---|
203 | 'Z.feed_nb_out, Z.feed_nb_in, '. |
---|
204 | 'C.cat_title, C.cat_url, C.cat_desc '; |
---|
205 | } |
---|
206 | |
---|
207 | $strReq .= |
---|
208 | 'FROM '.$this->table.' Z '. |
---|
209 | 'LEFT OUTER JOIN '.$this->core->prefix.'category C ON Z.cat_id = C.cat_id '; |
---|
210 | |
---|
211 | if (!empty($params['from'])) |
---|
212 | { |
---|
213 | $strReq .= $params['from'].' '; |
---|
214 | } |
---|
215 | |
---|
216 | $strReq .= "WHERE Z.blog_id = '".$this->blog."' "; |
---|
217 | |
---|
218 | if (isset($params['feed_type'])) |
---|
219 | { |
---|
220 | $strReq .= "AND Z.feed_type = '".$this->con->escape($params['type'])."' "; |
---|
221 | } |
---|
222 | else |
---|
223 | { |
---|
224 | $strReq .= "AND Z.feed_type = 'feed' "; |
---|
225 | } |
---|
226 | |
---|
227 | if (!empty($params['feed_id'])) |
---|
228 | { |
---|
229 | if (is_array($params['feed_id'])) |
---|
230 | { |
---|
231 | array_walk($params['feed_id'],create_function('&$v,$k','if($v!==null){$v=(integer)$v;}')); |
---|
232 | } |
---|
233 | else |
---|
234 | { |
---|
235 | $params['feed_id'] = array((integer) $params['feed_id']); |
---|
236 | } |
---|
237 | $strReq .= 'AND Z.feed_id '.$this->con->in($params['feed_id']); |
---|
238 | } |
---|
239 | |
---|
240 | if (isset($params['feed_feed'])) |
---|
241 | { |
---|
242 | $strReq .= "AND Z.feed_feed = '".$this->con->escape($params['feed_feed'])."' "; |
---|
243 | } |
---|
244 | if (isset($params['feed_url'])) |
---|
245 | { |
---|
246 | $strReq .= "AND Z.feed_url = '".$this->con->escape($params['feed_url'])."' "; |
---|
247 | } |
---|
248 | if (isset($params['feed_status'])) |
---|
249 | { |
---|
250 | $strReq .= "AND Z.feed_status = ".((integer) $params['feed_status'])." "; |
---|
251 | } |
---|
252 | |
---|
253 | if (!empty($params['sql'])) |
---|
254 | { |
---|
255 | $strReq .= $params['sql'].' '; |
---|
256 | } |
---|
257 | |
---|
258 | if (!$count_only) |
---|
259 | { |
---|
260 | if (!empty($params['order'])) |
---|
261 | { |
---|
262 | $strReq .= 'ORDER BY '.$this->con->escape($params['order']).' '; |
---|
263 | } |
---|
264 | else |
---|
265 | { |
---|
266 | $strReq .= 'ORDER BY Z.feed_upddt DESC '; |
---|
267 | } |
---|
268 | } |
---|
269 | |
---|
270 | if (!$count_only && !empty($params['limit'])) |
---|
271 | { |
---|
272 | $strReq .= $this->con->limit($params['limit']); |
---|
273 | } |
---|
274 | |
---|
275 | $rs = $this->con->select($strReq); |
---|
276 | $rs->zc = $this; |
---|
277 | |
---|
278 | return $rs; |
---|
279 | } |
---|
280 | |
---|
281 | # Get next table id |
---|
282 | private function getNextId() |
---|
283 | { |
---|
284 | return $this->con->select('SELECT MAX(feed_id) FROM '.$this->table)->f(0) + 1; |
---|
285 | } |
---|
286 | |
---|
287 | # Lock a file to see if an update is ongoing |
---|
288 | public function lockUpdate() |
---|
289 | { |
---|
290 | try |
---|
291 | { |
---|
292 | # Need flock function |
---|
293 | if (!function_exists('flock')) |
---|
294 | { |
---|
295 | throw New Exception("Can't call php function named flock"); |
---|
296 | } |
---|
297 | # Cache writable ? |
---|
298 | if (!is_writable(DC_TPL_CACHE)) |
---|
299 | { |
---|
300 | throw new Exception("Can't write in cache fodler"); |
---|
301 | } |
---|
302 | # Set file path |
---|
303 | $f_md5 = md5($this->blog); |
---|
304 | $cached_file = sprintf('%s/%s/%s/%s/%s.txt', |
---|
305 | DC_TPL_CACHE, |
---|
306 | 'periodical', |
---|
307 | substr($f_md5,0,2), |
---|
308 | substr($f_md5,2,2), |
---|
309 | $f_md5 |
---|
310 | ); |
---|
311 | # Real path |
---|
312 | $cached_file = path::real($cached_file,false); |
---|
313 | # Make dir |
---|
314 | if (!is_dir(dirname($cached_file))) |
---|
315 | { |
---|
316 | files::makeDir(dirname($cached_file),true); |
---|
317 | } |
---|
318 | # Make file |
---|
319 | if (!file_exists($cached_file)) |
---|
320 | { |
---|
321 | !$fp = @fopen($cached_file, 'w'); |
---|
322 | if ($fp === false) |
---|
323 | { |
---|
324 | throw New Exception("Can't create file"); |
---|
325 | } |
---|
326 | fwrite($fp,'1',strlen('1')); |
---|
327 | fclose($fp); |
---|
328 | } |
---|
329 | # Open file |
---|
330 | if (!($fp = @fopen($cached_file, 'r+'))) |
---|
331 | { |
---|
332 | throw New Exception("Can't open file"); |
---|
333 | } |
---|
334 | # Lock file |
---|
335 | if (!flock($fp,LOCK_EX)) |
---|
336 | { |
---|
337 | throw New Exception("Can't lock file"); |
---|
338 | } |
---|
339 | $this->lock = $fp; |
---|
340 | return true; |
---|
341 | } |
---|
342 | catch (Exception $e) |
---|
343 | { |
---|
344 | throw $e; |
---|
345 | } |
---|
346 | return false; |
---|
347 | } |
---|
348 | |
---|
349 | public function unlockUpdate() |
---|
350 | { |
---|
351 | @fclose($this->lock); |
---|
352 | $this->lock = null; |
---|
353 | } |
---|
354 | |
---|
355 | # Check and add/update post related to record if needed |
---|
356 | public function checkFeedsUpdate($id=null,$throw=false) |
---|
357 | { |
---|
358 | # Limit to one update at a time |
---|
359 | try |
---|
360 | { |
---|
361 | $this->lockUpdate(); |
---|
362 | } |
---|
363 | catch (Exception $e) |
---|
364 | { |
---|
365 | if ($throw) throw $e; |
---|
366 | return false; |
---|
367 | } |
---|
368 | |
---|
369 | dt::setTZ($this->core->blog->settings->system->blog_timezone); |
---|
370 | $time = time(); |
---|
371 | $s = $this->core->blog->settings->zoneclearFeedServer; |
---|
372 | |
---|
373 | # All feeds or only one (from admin) |
---|
374 | $f = !$id ? |
---|
375 | $this->getFeeds(array('feed_status'=>1,'order'=>'feed_upd_last ASC')) : |
---|
376 | $this->getFeeds(array('feed_id'=>$id)); |
---|
377 | |
---|
378 | # No feed |
---|
379 | if ($f->isEmpty()) return false; |
---|
380 | |
---|
381 | # Set feeds user |
---|
382 | $this->enableUser($s->zoneclearFeedServer_user); |
---|
383 | |
---|
384 | $updates = false; |
---|
385 | $loop_mem = array(); |
---|
386 | |
---|
387 | $limit = abs((integer) $s->zoneclearFeedServer_update_limit); |
---|
388 | if ($limit < 1) $limit = 10; |
---|
389 | $i = 0; |
---|
390 | |
---|
391 | $cur_post = $this->con->openCursor($this->core->prefix.'post'); |
---|
392 | $cur_meta = $this->con->openCursor($this->core->prefix.'meta'); |
---|
393 | |
---|
394 | while($f->fetch()) |
---|
395 | { |
---|
396 | # Check if feed need update |
---|
397 | if ($id || $i < $limit && $f->feed_status == 1 |
---|
398 | && $time > $f->feed_upd_last + $f->feed_upd_int) |
---|
399 | { |
---|
400 | $i++; |
---|
401 | $feed = self::readFeed($f->feed_feed); |
---|
402 | |
---|
403 | # Nothing to parse |
---|
404 | if (!$feed) |
---|
405 | { |
---|
406 | # Keep active empty feed or disable it ? |
---|
407 | if (!$s->zoneclearFeedServer_keep_empty_feed) |
---|
408 | { |
---|
409 | $this->enableFeed($f->feed_id,false); |
---|
410 | } |
---|
411 | $i++; |
---|
412 | } |
---|
413 | # Not updated since last visit |
---|
414 | elseif (!$id && '' != $feed->pubdate && strtotime($feed->pubdate) < $f->feed_upd_last) |
---|
415 | { |
---|
416 | # Set update time of this feed |
---|
417 | $this->enableFeed($f->feed_id,true,$time); |
---|
418 | $i++; |
---|
419 | } |
---|
420 | else |
---|
421 | { |
---|
422 | # Set update time of this feed |
---|
423 | $this->enableFeed($f->feed_id,$f->feed_status,$time); |
---|
424 | |
---|
425 | $this->con->begin(); |
---|
426 | |
---|
427 | foreach ($feed->items as $item) |
---|
428 | { |
---|
429 | $item_TS = $item->TS ? $item->TS : $time; |
---|
430 | |
---|
431 | // I found that mercurial atom feed did not repect standard |
---|
432 | $item_link = @$item->link; |
---|
433 | if (!$item_link) |
---|
434 | { |
---|
435 | $item_link = @$item->guid; |
---|
436 | } |
---|
437 | # Unknow feed item link |
---|
438 | if (!$item_link) continue; |
---|
439 | |
---|
440 | $item_link = $this->con->escape($item_link); |
---|
441 | $is_new_published_entry = false; |
---|
442 | |
---|
443 | # Not updated since last visit |
---|
444 | if (!$id && $item_TS < $f->feed_upd_last) continue; |
---|
445 | |
---|
446 | # Fix loop twin |
---|
447 | if (in_array($item_link,$loop_mem)) continue; |
---|
448 | $loop_mem[] = $item_link; |
---|
449 | |
---|
450 | # Check if entry exists |
---|
451 | $old_post = $this->con->select( |
---|
452 | 'SELECT P.post_id, P.post_status '. |
---|
453 | 'FROM '.$this->core->prefix.'post P '. |
---|
454 | 'INNER JOIN '.$this->core->prefix.'meta M '. |
---|
455 | 'ON P.post_id = M.post_id '. |
---|
456 | "WHERE blog_id='".$this->blog."' ". |
---|
457 | "AND meta_type = 'zoneclearfeed_url' ". |
---|
458 | "AND meta_id = '".$item_link."' " |
---|
459 | ); |
---|
460 | |
---|
461 | # Prepare entry cursor |
---|
462 | $cur_post->clean(); |
---|
463 | $cur_post->post_dt = date('Y-m-d H:i:s',$item_TS); |
---|
464 | if ($f->cat_id) $cur_post->cat_id = $f->cat_id; |
---|
465 | $post_content = $item->content ? $item->content : $item->description; |
---|
466 | $cur_post->post_content = html::absoluteURLs($post_content,$feed->link); |
---|
467 | $cur_post->post_title = $item->title ? $item->title : text::cutString(html::clean($cur_post->post_content),60); |
---|
468 | $creator = $item->creator ? $item->creator : $f->feed_owner; |
---|
469 | |
---|
470 | try |
---|
471 | { |
---|
472 | # Create entry |
---|
473 | if ($old_post->isEmpty()) |
---|
474 | { |
---|
475 | # Post |
---|
476 | $cur_post->user_id = $this->core->auth->userID(); |
---|
477 | $cur_post->post_format = 'xhtml'; |
---|
478 | $cur_post->post_status = (integer) $s->zoneclearFeedServer_post_status_new; |
---|
479 | $cur_post->post_open_comment = 0; |
---|
480 | $cur_post->post_open_tb = 0; |
---|
481 | |
---|
482 | # --BEHAVIOR-- zoneclearFeedServerBeforePostCreate |
---|
483 | $this->core->callBehavior('zoneclearFeedServerBeforePostCreate',$cur_post); |
---|
484 | |
---|
485 | $post_id = $this->core->auth->sudo(array($this->core->blog,'addPost'),$cur_post); |
---|
486 | |
---|
487 | # --BEHAVIOR-- zoneclearFeedServerAfterPostCreate |
---|
488 | $this->core->callBehavior('zoneclearFeedServerAfterPostCreate',$cur_post,$post_id); |
---|
489 | |
---|
490 | # Auto tweet new post |
---|
491 | if ($cur_post->post_status == 1) |
---|
492 | { |
---|
493 | $is_new_published_entry = true; |
---|
494 | } |
---|
495 | } |
---|
496 | # Update entry |
---|
497 | else |
---|
498 | { |
---|
499 | $post_id = $old_post->post_id; |
---|
500 | |
---|
501 | # --BEHAVIOR-- zoneclearFeedServerBeforePostUpdate |
---|
502 | $this->core->callBehavior('zoneclearFeedServerBeforePostUpdate',$cur_post,$post_id); |
---|
503 | |
---|
504 | $this->core->auth->sudo(array($this->core->blog,'updPost'),$post_id,$cur_post); |
---|
505 | |
---|
506 | # Quick delete old meta |
---|
507 | $this->con->execute( |
---|
508 | 'DELETE FROM '.$this->core->prefix.'meta '. |
---|
509 | 'WHERE post_id = '.$post_id.' '. |
---|
510 | "AND meta_type LIKE 'zoneclearfeed_%' " |
---|
511 | ); |
---|
512 | # Delete old tags |
---|
513 | $this->core->auth->sudo(array($this->core->meta,'delPostMeta'),$post_id,'tag'); |
---|
514 | |
---|
515 | # --BEHAVIOR-- zoneclearFeedServerAfterPostUpdate |
---|
516 | $this->core->callBehavior('zoneclearFeedServerAfterPostUpdate',$cur_post,$post_id); |
---|
517 | } |
---|
518 | |
---|
519 | # Quick add new meta |
---|
520 | $meta = new ArrayObject(); |
---|
521 | $meta->tweeter = $f->feed_tweeter; |
---|
522 | |
---|
523 | $cur_meta->clean(); |
---|
524 | $cur_meta->post_id = $post_id; |
---|
525 | $cur_meta->meta_type = 'zoneclearfeed_url'; |
---|
526 | $cur_meta->meta_id = $meta->url = $item_link; |
---|
527 | $cur_meta->insert(); |
---|
528 | |
---|
529 | $cur_meta->clean(); |
---|
530 | $cur_meta->post_id = $post_id; |
---|
531 | $cur_meta->meta_type = 'zoneclearfeed_author'; |
---|
532 | $cur_meta->meta_id = $meta->author = $creator; |
---|
533 | $cur_meta->insert(); |
---|
534 | |
---|
535 | $cur_meta->clean(); |
---|
536 | $cur_meta->post_id = $post_id; |
---|
537 | $cur_meta->meta_type = 'zoneclearfeed_site'; |
---|
538 | $cur_meta->meta_id = $meta->site = $f->feed_url; |
---|
539 | $cur_meta->insert(); |
---|
540 | |
---|
541 | $cur_meta->clean(); |
---|
542 | $cur_meta->post_id = $post_id; |
---|
543 | $cur_meta->meta_type = 'zoneclearfeed_sitename'; |
---|
544 | $cur_meta->meta_id = $meta->sitename = $f->feed_name; |
---|
545 | $cur_meta->insert(); |
---|
546 | |
---|
547 | $cur_meta->clean(); |
---|
548 | $cur_meta->post_id = $post_id; |
---|
549 | $cur_meta->meta_type = 'zoneclearfeed_id'; |
---|
550 | $cur_meta->meta_id = $meta->id = $f->feed_id; |
---|
551 | $cur_meta->insert(); |
---|
552 | |
---|
553 | # Add new tags |
---|
554 | $tags = $this->core->meta->splitMetaValues($f->feed_tags); |
---|
555 | if ($f->feed_get_tags) |
---|
556 | { |
---|
557 | $tags = array_merge($tags,$item->subject); |
---|
558 | $tags = array_unique($tags); |
---|
559 | } |
---|
560 | $formated_tags = array(); |
---|
561 | foreach ($tags as $tag) |
---|
562 | { |
---|
563 | # Change tags case |
---|
564 | switch((integer) $s->zoneclearFeedServer_tag_case) |
---|
565 | { |
---|
566 | case 3: $tag = strtoupper($tag); break; |
---|
567 | case 2: $tag = strtolower($tag); break; |
---|
568 | case 1: $tag = ucfirst(strtolower($tag)); break; |
---|
569 | default: /* do nothing*/ break; |
---|
570 | } |
---|
571 | if (!in_array($tag,$formated_tags)) |
---|
572 | { |
---|
573 | $formated_tags[] = $tag; |
---|
574 | $this->core->auth->sudo(array($this->core->meta,'setPostMeta'),$post_id,'tag',dcMeta::sanitizeMetaID($tag)); |
---|
575 | } |
---|
576 | } |
---|
577 | $meta->tags = $formated_tags; |
---|
578 | |
---|
579 | # --BEHAVIOR-- zoneclearFeedServerAfterFeedUpdate |
---|
580 | $this->core->callBehavior('zoneclearFeedServerAfterFeedUpdate',$this->core,$is_new_published_entry,$cur_post,$meta); |
---|
581 | |
---|
582 | } |
---|
583 | catch (Exception $e) |
---|
584 | { |
---|
585 | $this->con->rollback(); |
---|
586 | $this->enableUser(false); |
---|
587 | $this->unlockUpdate(); |
---|
588 | throw $e; |
---|
589 | } |
---|
590 | $updates = true; |
---|
591 | } |
---|
592 | $this->con->commit(); |
---|
593 | } |
---|
594 | } |
---|
595 | } |
---|
596 | $this->enableUser(false); |
---|
597 | $this->unlockUpdate(); |
---|
598 | return true; |
---|
599 | } |
---|
600 | |
---|
601 | # Set permission to update post table |
---|
602 | public function enableUser($enable=false) |
---|
603 | { |
---|
604 | # Enable |
---|
605 | if ($enable) |
---|
606 | { |
---|
607 | if (!$this->core->auth->checkUser($enable)) |
---|
608 | { |
---|
609 | throw new Exception('Unable to set user'); |
---|
610 | } |
---|
611 | } |
---|
612 | # Disable |
---|
613 | else |
---|
614 | { |
---|
615 | $this->core->auth = null; |
---|
616 | $this->core->auth = new dcAuth($this->core); |
---|
617 | } |
---|
618 | } |
---|
619 | |
---|
620 | # Read and parse external feeds |
---|
621 | public static function readFeed($f) |
---|
622 | { |
---|
623 | try |
---|
624 | { |
---|
625 | $feed_reader = new feedReader; |
---|
626 | $feed_reader->setCacheDir(DC_TPL_CACHE); |
---|
627 | $feed_reader->setTimeout(self::$nethttp_timeout); |
---|
628 | $feed_reader->setMaxRedirects(self::$nethttp_maxredirect); |
---|
629 | $feed_reader->setUserAgent(self::$nethttp_agent); |
---|
630 | return $feed_reader->parse($f); |
---|
631 | } |
---|
632 | catch (Exception $e) {} |
---|
633 | |
---|
634 | return null; |
---|
635 | } |
---|
636 | |
---|
637 | # Trigger blog |
---|
638 | private function trigger() |
---|
639 | { |
---|
640 | $this->core->blog->triggerBlog(); |
---|
641 | } |
---|
642 | |
---|
643 | # Check if an URL is well formed |
---|
644 | public static function validateURL($url) |
---|
645 | { |
---|
646 | if (false === strpos($url,'http://') && false === strpos($url,'https://')) |
---|
647 | { |
---|
648 | return false; |
---|
649 | } |
---|
650 | return true; |
---|
651 | } |
---|
652 | |
---|
653 | public static function absoluteURL($root,$url) |
---|
654 | { |
---|
655 | $host = preg_replace('|^([a-z]{3,}://)(.*?)/(.*)$|','$1$2',$root); |
---|
656 | |
---|
657 | $parse = parse_url($url); |
---|
658 | if (empty($parse['scheme'])) |
---|
659 | { |
---|
660 | if (strpos($url,'/') === 0) |
---|
661 | { |
---|
662 | $url = $host.$url; |
---|
663 | } |
---|
664 | elseif (strpos($url,'#') === 0) |
---|
665 | { |
---|
666 | $url = $root.$url; |
---|
667 | } |
---|
668 | elseif (preg_match('|/$|',$root)) |
---|
669 | { |
---|
670 | $url = $root.$url; |
---|
671 | } |
---|
672 | else |
---|
673 | { |
---|
674 | $url = dirname($root).'/'.$url; |
---|
675 | } |
---|
676 | } |
---|
677 | return $url; |
---|
678 | } |
---|
679 | |
---|
680 | public static function getAllStatus() |
---|
681 | { |
---|
682 | return array( |
---|
683 | __('disabled') => '0', |
---|
684 | __('enabled') => '1' |
---|
685 | ); |
---|
686 | } |
---|
687 | |
---|
688 | public static function getAllUpdateInterval() |
---|
689 | { |
---|
690 | return array( |
---|
691 | __('every hour') => 3600, |
---|
692 | __('every two hours') => 7200, |
---|
693 | __('two times per day') => 43200, |
---|
694 | __('every day') => 86400, |
---|
695 | __('every two days') => 172800, |
---|
696 | __('every week') => 604800 |
---|
697 | ); |
---|
698 | } |
---|
699 | |
---|
700 | public function getAllBlogAdmins() |
---|
701 | { |
---|
702 | $admins = array(); |
---|
703 | |
---|
704 | # Get super admins |
---|
705 | $rs = $this->con->select( |
---|
706 | 'SELECT user_id, user_super, user_name, user_firstname, user_displayname '. |
---|
707 | 'FROM '.$this->con->escapeSystem($this->core->prefix.'user').' '. |
---|
708 | 'WHERE user_super = 1 AND user_status = 1 ' |
---|
709 | ); |
---|
710 | |
---|
711 | if (!$rs->isEmpty()) |
---|
712 | { |
---|
713 | while ($rs->fetch()) |
---|
714 | { |
---|
715 | $user_cn = dcUtils::getUserCN($rs->user_id, $rs->user_name, $rs->user_firstname, $rs->user_displayname); |
---|
716 | $admins[$user_cn.' (super admin)'] = $rs->user_id; |
---|
717 | } |
---|
718 | } |
---|
719 | |
---|
720 | # Get admins |
---|
721 | $rs = $this->con->select( |
---|
722 | 'SELECT U.user_id, U.user_super, U.user_name, U.user_firstname, U.user_displayname '. |
---|
723 | 'FROM '.$this->con->escapeSystem($this->core->prefix.'user').' U '. |
---|
724 | 'LEFT JOIN '.$this->con->escapeSystem($this->core->prefix.'permissions').' P '. |
---|
725 | 'ON U.user_id=P.user_id '. |
---|
726 | 'WHERE U.user_status = 1 '. |
---|
727 | "AND P.blog_id = '".$this->blog."' ". |
---|
728 | "AND P.permissions LIKE '%|admin|%' " |
---|
729 | ); |
---|
730 | |
---|
731 | if (!$rs->isEmpty()) |
---|
732 | { |
---|
733 | while ($rs->fetch()) |
---|
734 | { |
---|
735 | $user_cn = dcUtils::getUserCN($rs->user_id, $rs->user_name, $rs->user_firstname, $rs->user_displayname); |
---|
736 | $admins[$user_cn.' (admin)'] = $rs->user_id; |
---|
737 | } |
---|
738 | } |
---|
739 | |
---|
740 | return $admins; |
---|
741 | } |
---|
742 | |
---|
743 | # Get list of urls where entries could be hacked |
---|
744 | public static function getPublicUrlTypes($core) |
---|
745 | { |
---|
746 | $types = array(); |
---|
747 | |
---|
748 | # --BEHAVIOR-- zoneclearFeedServerPublicUrlTypes |
---|
749 | $core->callBehavior('zoneclearFeedServerPublicUrlTypes',$types); |
---|
750 | |
---|
751 | $types[__('home page')] = 'default'; |
---|
752 | $types[__('post pages')] = 'post'; |
---|
753 | $types[__('tags pages')] = 'tag'; |
---|
754 | $types[__('archives pages')] = 'archive'; |
---|
755 | $types[__('category pages')] = 'category'; |
---|
756 | $types[__('entries feed')] = 'feed'; |
---|
757 | |
---|
758 | return $types; |
---|
759 | } |
---|
760 | |
---|
761 | # Take care about tweakurls (thanks Mathieu M.) |
---|
762 | // not a good way but job is done |
---|
763 | public static function tweakurlsAfterPostCreate($cur,$id) |
---|
764 | { |
---|
765 | global $core; |
---|
766 | $cur->post_url = tweakUrls::tweakBlogURL($cur->post_url); |
---|
767 | $core->auth->sudo(array($core->blog,'updPost'),$id,$cur); |
---|
768 | } |
---|
769 | } |
---|
770 | ?> |
---|