Dotclear

source: plugins/zoneclearFeedServer/_public.php @ 3139

Revision 3139, 17.7 KB checked in by JcDenis, 10 years ago (diff)
  • Required Dotclear 2.5
  • Fixed admin pages titles and messages and typo
  • Added Favorites icon
  • Added new 'homeonly' option on widgets
  • Fixed https protocol
  • Added option to keep active empty feeds
  • Added option to transform imported tags
Line 
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
15if (!defined('DC_RC_PATH')){return;}
16
17# Namespace for settings
18$core->blog->settings->addNamespace('zoneclearFeedServer');
19
20# Widgets
21require_once dirname(__FILE__).'/_widgets.php';
22
23$core->addBehavior('coreBlogGetPosts',array('zoneclearFeedServerPublicBehaviors','coreBlogGetPosts'));
24
25if (!$core->blog->settings->zoneclearFeedServer->zoneclearFeedServer_active)
26{
27     return;
28}
29if (1 == $core->blog->settings->zoneclearFeedServer->zoneclearFeedServer_bhv_pub_upd)
30{
31     $core->addBehavior('publicBeforeDocument',array('zoneclearFeedServerPublicBehaviors','publicDocument'));
32}
33elseif (2 == $core->blog->settings->zoneclearFeedServer->zoneclearFeedServer_bhv_pub_upd)
34{
35     $core->addBehavior('publicAfterDocument',array('zoneclearFeedServerPublicBehaviors','publicAfterDocument'));
36}
37elseif (3 == $core->blog->settings->zoneclearFeedServer->zoneclearFeedServer_bhv_pub_upd)
38{
39     $core->addBehavior('publicHeadContent',array('zoneclearFeedServerPublicBehaviors','publicHeadContent'));
40}
41
42# Take care about tweakurls (thanks Mathieu M.)
43if (version_compare($core->plugins->moduleInfo('tweakurls','version'),'0.8','>=')) {
44     $core->addbehavior('zoneclearFeedServerAfterPostCreate',array('zoneclearFeedServer','tweakurlsAfterPostCreate'));
45}
46
47$core->tpl->addBlock('zcFeeds',array('zoneclearFeedServerTpl','Feeds'));
48$core->tpl->addValue('zcFeedsCount',array('zoneclearFeedServerTpl','FeedsCount'));
49$core->tpl->addValue('zcFeedsEntriesCount',array('zoneclearFeedServerTpl','FeedsEntriesCount'));
50$core->tpl->addBlock('zcFeedsFooter',array('zoneclearFeedServerTpl','FeedsFooter'));
51$core->tpl->addBlock('zcFeedsHeader',array('zoneclearFeedServerTpl','FeedsHeader'));
52$core->tpl->addValue('zcFeedEntriesCount',array('zoneclearFeedServerTpl','FeedEntriesCount'));
53$core->tpl->addValue('zcFeedCategory',array('zoneclearFeedServerTpl','FeedCategory'));
54$core->tpl->addValue('zcFeedCategoryID',array('zoneclearFeedServerTpl','FeedCategoryID'));
55$core->tpl->addValue('zcFeedCategoryURL',array('zoneclearFeedServerTpl','FeedCategoryURL'));
56$core->tpl->addValue('zcFeedCategoryShortURL',array('zoneclearFeedServerTpl','FeedCategoryShortURL'));
57$core->tpl->addValue('zcFeedID',array('zoneclearFeedServerTpl','FeedID'));
58$core->tpl->addBlock('zcFeedIf',array('zoneclearFeedServerTpl','FeedIf'));
59$core->tpl->addValue('zcFeedIfFirst',array('zoneclearFeedServerTpl','FeedIfFirst'));
60$core->tpl->addValue('zcFeedIfOdd',array('zoneclearFeedServerTpl','FeedIfOdd'));
61$core->tpl->addValue('zcFeedLang',array('zoneclearFeedServerTpl','FeedLang'));
62$core->tpl->addValue('zcFeedName',array('zoneclearFeedServerTpl','FeedName'));
63$core->tpl->addValue('zcFeedOwner',array('zoneclearFeedServerTpl','FeedOwner'));
64$core->tpl->addValue('zcFeedDesc',array('zoneclearFeedServerTpl','FeedDesc'));
65$core->tpl->addValue('zcFeedSiteURL',array('zoneclearFeedServerTpl','FeedSiteURL'));
66$core->tpl->addValue('zcFeedFeedURL',array('zoneclearFeedServerTpl','FeedFeedURL'));
67
68class zoneclearFeedServerPublicBehaviors
69{
70     # Remember others post extension
71     public static function coreBlogGetPosts($rs)
72     {
73          $GLOBALS['beforeZcFeedRsExt'] = $rs->extensions();
74          $rs->extend('zoneclearFeedServerPosts');
75     }
76
77     # Update feeds after contents
78     public static function publicAfterDocument($core)
79     {
80          # Limit feeds update to home page et feed page
81          # Like publishScheduledEntries
82          if (!in_array($core->url->type,array('default','feed'))) return;
83         
84          self::publicDocument($core);
85     }
86
87     # Generic behavior for before and after public content
88     public static function publicDocument($core)
89     {
90          $zc = new zoneclearFeedServer($core);
91          $zc->checkFeedsUpdate();
92          return;
93     }
94
95     # Update feeds by an Ajax request (background)
96     public static function publicHeadContent($core,$_ctx)
97     {
98          # Limit update to home page
99          if ($core->url->type != 'default') return;
100         
101          $blog_url = html::escapeJS($core->blog->url.$core->url->getBase('zoneclearFeedsPage').'/zcfsupd');
102          $blog_id = html::escapeJS($core->blog->id);
103         
104          echo
105          "\n<!-- JS for zoneclearFeedServer --> \n".
106          "<script type=\"text/javascript\" src=\"".
107               $core->blog->url.$core->url->getBase('zoneclearFeedsPage').'/zcfsupd.js">'.
108          "</script> \n".
109          "<script type=\"text/javascript\"> \n".
110          "//<![CDATA[\n".
111          " \$(function(){if(!document.getElementById){return;} ".
112          " $('body').zoneclearFeedServer({blog_url:'".$blog_url."',blog_id:'".$blog_id."'}); ".
113          " })\n".
114          "//]]>\n".
115          "</script>\n";
116     }
117}
118
119class zoneclearFeedServerPosts extends rsExtPost
120{
121     public static function zcFeed($rs,$info)
122     {
123          $p = array(
124               'post_id'=>$rs->post_id,
125               'meta_type'=>'zoneclearfeed_'.$info,
126               'limit'=>1
127          );
128          $meta = $rs->core->meta->getMetadata($p);
129         
130          return $meta->isEmpty() ? null : $meta->meta_id;
131     }
132     
133     public static function zcFeedBrother($type,$args)
134     {
135          if (!empty($GLOBALS['beforeZcFeedRsExt'][$type]))
136          {
137               $func = $GLOBALS['beforeZcFeedRsExt'][$type];
138          }
139          elseif (is_callable('rsExtPostPublic',$type))
140          {
141               $func = array('rsExtPostPublic',$type);
142          }
143          else
144          {
145               $func = array('rsExtPost',$type);
146          }
147          return call_user_func_array($func,$args);
148     }
149     
150     public static function getAuthorLink($rs)
151     {
152          $author = $rs->zcFeed('author');
153          $site = $rs->zcFeed('site');
154          $sitename = $rs->zcFeed('sitename');
155         
156          return ($author && $sitename) ?
157               $author.' (<a href="'.$site.'">'.$sitename.'</a>)' :
158               self::zcFeedBrother('getAuthorLink',array(&$rs));
159     }
160     
161     public static function getAuthorCN($rs)
162     {
163          $author = $rs->zcFeed('author');
164          return $author ? 
165               $author : 
166               self::zcFeedBrother('getAuthorCN',array(&$rs));
167     }
168     
169     public static function getURL($rs)
170     {
171          $url = $rs->zcFeed('url');
172          $types = @unserialize($rs->core->blog->settings->zoneclearFeedServer->zoneclearFeedServer_post_title_redir);
173          $full = is_array($types) && in_array($rs->core->url->type,$types);
174         
175          return $url && $full ? 
176               zoneclearFeedServer::absoluteURL($rs->zcFeed('site'),$url) : 
177               self::zcFeedBrother('getURL',array(&$rs));
178     }
179     
180     public static function getContent($rs,$absolute_urls=false)
181     {
182          $url = $rs->zcFeed('url');
183          $sitename = $rs->zcFeed('sitename');
184          $content = self::zcFeedBrother('getContent',array(&$rs,$absolute_urls));
185         
186          if ($url && $sitename && $rs->post_type == 'post')
187          {
188               $types = @unserialize($rs->core->blog->settings->zoneclearFeedServer->zoneclearFeedServer_post_full_tpl);
189               
190               if (is_array($types) && in_array($rs->core->url->type,$types))
191               {
192                    return $content .
193                    '<p class="zoneclear-original"><em>'.
194                    sprintf(__('Original post on <a href="%s">%s</a>'),$url,$sitename).
195                    '</em></p>';
196               }
197               else
198               {
199                    $content = context::remove_html($content);
200                    $content = context::cut_string($content,350);     
201                    $content = html::escapeHTML($content);
202                   
203                    return
204                    '<p>'.$content.'... '.
205                    '<em><a href="'.self::getURL($rs).'" title="'.__('Read more details about this feed').'">'.__('Continue reading').'</a></em></p>';
206               }
207          }
208          else
209          {
210               return $content;
211          }
212     }
213}
214
215class zoneclearFeedServerURL extends dcUrlHandlers
216{
217     public static function zcFeedsPage($args)
218     {
219          global $core, $_ctx;
220          $s = $core->blog->settings->zoneclearFeedServer;
221         
222          # Not active
223          if (!$s->zoneclearFeedServer_active)
224          {
225               self::p404();
226               return;
227          }
228         
229          # Update feeds (from ajax or other post resquest)
230          if ($args == '/zcfsupd' && 3 == $s->zoneclearFeedServer_bhv_pub_upd)
231          {
232               $msg = '';
233               if (!empty($_POST['blogId']) && html::escapeJS($core->blog->id) == $_POST['blogId'])
234               {
235                    try
236                    {
237                         $zc = new zoneclearFeedServer($core);
238                         if ($zc->checkFeedsUpdate())
239                         {
240                              $msg = '<status>ok</status><message>Feeds updated successfully</message>';
241                         }
242                    }
243                    catch (Exception $e) {}
244               }
245               if (empty($msg))
246               {
247                    $msg = '<status>failed</status><message>Failed to update feeds</message>';
248               }
249               
250               header('Content-Type: application/xml; charset=UTF-8');
251               echo 
252               '<?xml version="1.0" encoding="utf-8"?>'."\n".
253               '<response><rsp>'."\n".
254               $msg."\n".
255               '</rsp></response>';
256               exit(1);
257          }
258          # Server js
259          elseif ($args == '/zcfsupd.js' && 3 == $s->zoneclearFeedServer_bhv_pub_upd)
260          {
261               $core->tpl->setPath($core->tpl->getPath(),dirname(__FILE__).'/default-templates');
262               self::serveDocument('zcfsupd.js','text/javascript',false,false);
263               return;
264          }
265          # Server feeds description page
266          elseif (in_array($args,array('','/')) && $s->zoneclearFeedServer_pub_active)
267          {
268               $core->tpl->setPath($core->tpl->getPath(),dirname(__FILE__).'/default-templates');
269               self::serveDocument('zcfeeds.html');
270               return;
271          }
272          # Unknow
273          else
274          {
275               self::p404();
276               return;
277          }
278          return;
279     }
280}
281
282class zoneclearFeedServerTpl
283{
284     public static function Feeds($a,$c)
285     {
286          $lastn = -1;
287          $p = '';
288          if (isset($a['lastn']))
289          {
290               $lastn = abs((integer) $a['lastn'])+0;
291               $p .= "\$zcfs_params['limit'] = ".$lastn.";\n";
292          }
293          if (isset($a['cat_id']))
294          {
295               $p .= "@\$zcfs_params['sql'] .= 'AND Z.cat_id = ".addslashes($a['cat_id'])." ';\n";
296          }
297          if (isset($a['no_category']))
298          {
299               $p .= "@\$zcfs_params['sql'] .= 'AND Z.cat_id IS NULL ';\n";
300          }
301          if (!empty($a['site_url']))
302          {
303               $p .= "\$zcfs_params['feed_url'] = '".addslashes($a['site_url'])."';\n";
304          }
305          if (isset($a['feed_status']))
306          {
307               $p .= "\$zcfs_params['feed_status'] = ".((integer) $a['feed_status']).";\n";
308          }
309          else
310          {
311               $p .= "\$zcfs_params['feed_status'] = 1;\n";
312          }
313          if (!empty($a['feed_url']))
314          {
315               $p .= "\$zcfs_params['feed_feed'] = '".addslashes($a['feed_url'])."';\n";
316          }
317          if (isset($a['feed_owner']))
318          {
319               $p .= "@\$zcfs_params['sql'] .= \"AND Z.feed_owner = '".addslashes($a['author'])."' \";\n";
320          }
321         
322          $sortby = 'feed_creadt';
323          $order = 'desc';
324          if (isset($a['sortby']))
325          {
326               switch ($a['sortby'])
327               {
328                    case 'name': $sortby = 'lowername'; break;
329                    case 'owner' : $sortby = 'feed_owner'; break;
330                    case 'date' : $sortby = 'feed_dt'; break;
331                    case 'update' : $sortby = 'feed_upddt'; break;
332                    case 'id' : $sortby = 'feed_id'; break;
333               }
334          }
335          if (isset($a['order']) && preg_match('/^(desc|asc)$/i',$a['order']))
336          {
337               $order = $a['order'];
338          }
339          $p .= "\$zcfs_params['order'] = '".$sortby." ".$order."';\n";
340         
341          return 
342          '<?php '.$p.
343          '$_ctx->feeds_params = $zcfs_params;'."\n".
344          '$zcfs = new zoneclearFeedServer($core);'."\n".
345          '$_ctx->feeds = $zcfs->getFeeds($zcfs_params); unset($zcfs_params,$zcfs);'."\n".
346          "?>\n".
347          '<?php while ($_ctx->feeds->fetch()) : ?>'.$c.'<?php endwhile; '.
348          '$_ctx->feeds = null; $_ctx->feeds_params = null; ?>';
349     }
350     
351     public static function FeedIf($a,$c)
352     {
353          $if = array();
354         
355          $operator = isset($a['operator']) ? self::getOperator($a['operator']) : '&&';
356         
357          if (isset($a['type']))
358          {
359               $type = trim($a['type']);
360               $type = !empty($type) ? $type : 'feed';
361               $if[] = '$_ctx->feeds->feed_type == "'.addslashes($type).'"';
362          }
363          if (isset($a['site_url']))
364          {
365               $url = trim($a['feed_url']);
366               if (substr($url,0,1) == '!')
367               {
368                    $url = substr($url,1);
369                    $if[] = '$_ctx->feeds->feed_url != "'.addslashes($url).'"';
370               }
371               else
372               {
373                    $if[] = '$_ctx->feeds->feed_url == "'.addslashes($url).'"';
374               }
375          }
376          if (isset($a['feed_url']))
377          {
378               $url = trim($a['feed_feed']);
379               if (substr($url,0,1) == '!') 
380               {
381                    $url = substr($url,1);
382                    $if[] = '$_ctx->feeds->feed_feed != "'.addslashes($url).'"';
383               }
384               else
385               {
386                    $if[] = '$_ctx->feeds->feed_feed == "'.addslashes($url).'"';
387               }
388          }
389          if (isset($a['category']))
390          {
391               $category = addslashes(trim($a['category']));
392               if (substr($category,0,1) == '!')
393               {
394                    $category = substr($category,1);
395                    $if[] = '($_ctx->feeds->cat_url != "'.$category.'")';
396               }
397               else
398               {
399                    $if[] = '($_ctx->feeds->cat_url == "'.$category.'")';
400               }
401          }
402          if (isset($a['first']))
403          {
404               $sign = (boolean) $a['first'] ? '=' : '!';
405               $if[] = '$_ctx->feeds->index() '.$sign.'= 0';
406          }
407          if (isset($a['odd']))
408          {
409               $sign = (boolean) $a['odd'] ? '=' : '!';
410               $if[] = '($_ctx->feeds->index()+1)%2 '.$sign.'= 1';
411          }
412          if (isset($a['has_category']))
413          {
414               $sign = (boolean) $a['has_category'] ? '' : '!';
415               $if[] = $sign.'$_ctx->feeds->cat_id';
416          }
417          if (isset($a['has_description']))
418          {
419               $sign = (boolean) $a['has_description'] ? '' : '!';
420               $if[] = $sign.'$_ctx->feeds->feed_desc';
421          }
422         
423          if (!empty($if))
424          {
425               return '<?php if('.implode(' '.$operator.' ',$if).') : ?>'.$c.'<?php endif; ?>';
426          }
427          else
428          {
429               return $c;
430          }
431     }
432     
433     public static function FeedIfFirst($a)
434     {
435          $ret = isset($a['return']) ? $a['return'] : 'first';
436          $ret = html::escapeHTML($ret);
437         
438          return
439          '<?php if ($_ctx->feeds->index() == 0) { '.
440          "echo '".addslashes($ret)."'; } ?>";
441     }
442     
443     public static function FeedIfOdd($a)
444     {
445          $ret = isset($a['return']) ? $a['return'] : 'odd';
446          $ret = html::escapeHTML($ret);
447         
448          return
449          '<?php if (($_ctx->feeds->index()+1)%2 == 1) { '.
450          "echo '".addslashes($ret)."'; } ?>";
451     }
452     
453     public static function FeedDesc($a)
454     {
455          return self::getValue($a,'$_ctx->feeds->feed_desc');
456     }
457     
458     public static function FeedOwner($a)
459     {
460          return self::getValue($a,'$_ctx->feeds->feed_owner');
461     }
462     
463     public static function FeedCategory($a)
464     {
465          return self::getValue($a,'$_ctx->feeds->cat_title');
466     }
467     
468     public static function FeedCategoryID($a)
469     {
470          return self::getValue($a,'$_ctx->feeds->cat_id');
471     }
472     
473     public static function FeedCategoryURL($a)
474     {
475          return self::getValue($a,'$core->blog->url.$core->url->getBase(\'category\').\'/\'.html::sanitizeURL($_ctx->feeds->cat_url)');
476     }
477     
478     public static function FeedCategoryShortURL($a)
479     {
480          return self::getValue($a,'$_ctx->feeds->cat_url');
481     }
482     
483     public static function FeedID($a)
484     {
485          return self::getValue($a,'$_ctx->feeds->feed_id');
486     }
487         
488     public static function FeedLang($a)
489     {
490          $f = $GLOBALS['core']->tpl->getFilters($a);
491          if (!empty($a['full']))
492          {
493               return '<?php $langs = l10n::getISOcodes(); if (isset($langs[$_ctx->feeds->feed_lang])) { echo '.sprintf($f,'$langs[$_ctx->feeds->feed_lang]').'; } else { echo '.sprintf($f,'$_ctx->feeds->feed_lang').'; } unset($langs); ?>';
494          }
495          else
496          {
497               return '<?php echo '.sprintf($f,'$_ctx->feeds->feed_lang').'; ?>';
498          }
499     }
500     
501     public static function FeedName($a)
502     {
503          return self::getValue($a,'$_ctx->feeds->feed_name');
504     }
505     
506     public static function FeedSiteURL($a)
507     {
508          return self::getValue($a,'$_ctx->feeds->feed_url');
509     }
510     
511     public static function FeedFeedURL($a)
512     {
513          return self::getValue($a,'$_ctx->feeds->feed_feed');
514     }
515     
516     public static function FeedsHeader($a,$c)
517     {
518          return "<?php if (\$_ctx->feeds->isStart()) : ?>".$c."<?php endif; ?>";
519     }
520     
521     public static function FeedsFooter($a,$c)
522     {
523          return "<?php if (\$_ctx->feeds->isEnd()) : ?>".$c."<?php endif; ?>";
524     }
525     
526     public static function FeedsCount($a)
527     {
528          $none = 'no sources';
529          $one = 'one source';
530          $more = '%d sources';
531         
532          if (isset($a['none']))
533          {
534               $none = addslashes($a['none']);
535          }
536          if (isset($a['one']))
537          {
538               $one = addslashes($a['one']);
539          }
540          if (isset($a['more']))
541          {
542               $more = addslashes($a['more']);
543          }
544         
545          return
546          "<?php \$fcount = \$_ctx->feeds->count(); \n".
547          "if (\$fcount == 0) {\n".
548          "  printf(__('".$none."'),\$fcount);\n".
549          "} elseif (\$fcount == 1) {\n".
550          "  printf(__('".$one."'),\$fcount);\n".
551          "} else {\n".
552          "  printf(__('".$more."'),\$fcount);\n".
553          "} unset(\$fcount); ?>";
554     }
555     
556     public static function FeedsEntriesCount($a)
557     {
558          $none = 'no entries';
559          $one = 'one entry';
560          $more = '%d entries';
561         
562          if (isset($a['none']))
563          {
564               $none = addslashes($a['none']);
565          }
566          if (isset($a['one']))
567          {
568               $one = addslashes($a['one']);
569          }
570          if (isset($a['more']))
571          {
572               $more = addslashes($a['more']);
573          }
574         
575          return
576          "<?php \$fcount = 0; \$allfeeds = \$_ctx->feeds->zc->getFeeds(); \n".
577          "if (!\$allfeeds->isEmpty()) { \n".
578          " while (\$allfeeds->fetch()) { ".
579          \$fcount += (integer) \$_ctx->feeds->zc->getPostsByFeed(array('feed_id'=>\$allfeeds->feed_id),true)->f(0); ".
580          " } \n".
581          "} \n".
582          "if (\$fcount == 0) {\n".
583          "  printf(__('".$none."'),\$fcount);\n".
584          "} elseif (\$fcount == 1) {\n".
585          "  printf(__('".$one."'),\$fcount);\n".
586          "} else {\n".
587          "  printf(__('".$more."'),\$fcount);\n".
588          "} unset(\$allfeeds,\$fcount); ?>";
589     }
590     
591     public static function FeedEntriesCount($a)
592     {
593          $none = 'no entries';
594          $one = 'one entry';
595          $more = '%d entries';
596         
597          if (isset($a['none']))
598          {
599               $none = addslashes($a['none']);
600          }
601          if (isset($a['one']))
602          {
603               $one = addslashes($a['one']);
604          }
605          if (isset($a['more']))
606          {
607               $more = addslashes($a['more']);
608          }
609         
610          return
611          "<?php \$fcount = \$_ctx->feeds->zc->getPostsByFeed(array('feed_id'=>\$_ctx->feeds->feed_id),true)->f(0); \n".
612          "if (\$fcount == 0) {\n".
613          "  printf(__('".$none."'),\$fcount);\n".
614          "} elseif (\$fcount == 1) {\n".
615          "  printf(__('".$one."'),\$fcount);\n".
616          "} else {\n".
617          "  printf(__('".$more."'),\$fcount);\n".
618          "} unset(\$fcount); ?>";
619     }
620     
621     protected static function getValue($a,$v)
622     {
623          return '<?php echo '.sprintf($GLOBALS['core']->tpl->getFilters($a),$v).'; ?>';
624     }
625     
626     protected static function getOperator($op)
627     {
628          switch (strtolower($op))
629          {
630               case 'or':
631               case '||':
632                    return '||';
633               case 'and':
634               case '&&':
635               default:
636                    return '&&';
637          }
638     }
639}
640?>
Note: See TracBrowser for help on using the repository browser.

Sites map