Dotclear

source: plugins/rateIt/_public.php @ 1307

Revision 1307, 11.3 KB checked in by JcDenis, 14 years ago (diff)

rateIt 0.5:

  • fixed PostgreSQL compatibility
Line 
1<?php
2# -- BEGIN LICENSE BLOCK ----------------------------------
3# This file is part of rateIt, a plugin for Dotclear 2.
4#
5# Copyright (c) 2009 JC Denis and contributors
6# jcdenis@gdwd.com
7#
8# Licensed under the GPL version 2.0 license.
9# A copy of this license is available in LICENSE file or at
10# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
11#
12# -- END LICENSE BLOCK ------------------------------------
13
14if (!defined('DC_RC_PATH')) return;
15
16require dirname(__FILE__).'/_widgets.php';
17
18$core->url->register('rateit','rateit','^rateit/(.+)$',
19     array('rateItPublic','file_get_contents'));
20
21if (!$core->blog->settings->rateit_active) {
22
23     $core->tpl->addBlock('rateIt',
24          array('rateItPublic','disableBlock'));
25     $core->tpl->addBlock('rateItIf',
26          array('rateItPublic','disableBlock'));
27     $core->tpl->addValue('rateItLinker',
28          array('rateItPublic','disableValue'));
29     $core->tpl->addValue('rateItTitle',
30          array('rateItPublic','disableValue'));
31     $core->tpl->addValue('rateItTotal',
32          array('rateItPublic','disableValue'));
33     $core->tpl->addValue('rateItMax',
34          array('rateItPublic','disableValue'));
35     $core->tpl->addValue('rateItMin',
36          array('rateItPublic','disableValue'));
37     $core->tpl->addValue('rateItNote',
38          array('rateItPublic','disableValue'));
39     $core->tpl->addValue('rateItFullnote',
40          array('rateItPublic','disableValue'));
41     $core->tpl->addValue('rateItQuotient',
42          array('rateItPublic','disableValue'));
43
44} else {
45     $core->addBehavior('publicHeadContent',
46          array('rateItPublic','publicHeadContent'));
47     $core->addBehavior('publicEntryAfterContent',
48          array('rateItPublic','publicEntryAfterContent'));
49
50     $core->tpl->addBlock('rateIt',
51          array('rateItPublic','rateIt'));
52     $core->tpl->addBlock('rateItIf',
53          array('rateItPublic','rateItIf'));
54     $core->tpl->addValue('rateItLinker',
55          array('rateItPublic','rateItLinker'));
56     $core->tpl->addValue('rateItTitle',
57          array('rateItPublic','rateItTitle'));
58     $core->tpl->addValue('rateItTotal',
59          array('rateItPublic','rateItTotal'));
60     $core->tpl->addValue('rateItMax',
61          array('rateItPublic','rateItMax'));
62     $core->tpl->addValue('rateItMin',
63          array('rateItPublic','rateItMin'));
64     $core->tpl->addValue('rateItQuotient',
65          array('rateItPublic','rateItQuotient'));
66     $core->tpl->addValue('rateItNote',
67          array('rateItPublic','rateItNote'));
68     $core->tpl->addValue('rateItFullnote',
69          array('rateItPublic','rateItFullnote'));
70
71     $core->url->register('rateitnow','rateitnow','^rateitnow/(.+)$',
72          array('rateItPublic','rateitnow'));
73
74     $core->url->register('rateitservice','rateitservice','^rateitservice/$',
75          array('rateItRest','service'));
76}
77
78class rateItPublic extends dcUrlHandlers
79{
80     public static function disableUrl($a)
81     {
82          self::p404(); exit;
83     }
84     public static function disableBlock($a,$b)
85     {
86          return '';
87     }
88     public static function disableValue($a)
89     {
90          return '';
91     }
92     private static function path($f)
93     {
94          $paths = explode(PATH_SEPARATOR, DC_PLUGINS_ROOT.'/rateIt/default-templates');
95          $d = array_pop($paths);
96          return $d.'/'.$f;
97     }
98
99     public static function rateitnow($args)
100     {
101          global $core;
102
103          if (!$core->blog->settings->rateit_active) {
104               self::p404();
105               exit;
106          }
107
108          if (!preg_match('#([^/]+)/([^/]+)/([^/]+)$#',$args,$m)) {
109               self::p404();
110               exit;
111          }
112
113          $voted = false;
114          $type = $m[1];
115          $id = $m[2];
116          $note = $m[3];
117
118          $ss = new rateIt($core);
119          $voted = $ss->voted($type,$id);
120          if (!$voted) {
121               $ss->set($type,$id,$note);
122               $voted = true;
123          }
124
125          if ($type='post') {
126               $post = $core->blog->getPosts(array('post_id'=>$id,'no_content'=>1));
127               if ($post->post_id) {
128                    http::redirect($core->blog->url.'post/'.$post->post_url.($voted ? '#rateit' : ''));
129               }
130          }
131
132          # --BEHAVIOR-- templateRateItRedirect
133          $core->callBehavior('templateRateItRedirect',$voted,$type,$id,$note);
134     }
135
136     public static function file_get_contents($args)
137     {
138          global $core;
139
140          if (!$core->blog->settings->rateit_active) {
141               self::p404();
142               exit;
143          }
144
145          if (!preg_match('#^(.*?)$#',$args,$m)) {
146               self::p404();
147               exit;
148          }
149
150          $f = $m[1];
151          if (strstr($f,"..") !== false) {
152               self::p404();
153               exit;
154          }
155
156          $f = self::path($f);
157          $path = dirname($f);
158          if (!is_dir($path)) {
159               self::p404();
160               exit;
161          }
162
163          $allowed_types = array('png','jpg','jpeg','gif','css','js','swf');
164          if (!file_exists($f) || !in_array(files::getExtension($f),$allowed_types)) {
165               self::p404();
166               exit;
167          }
168
169          //http::cache(array_merge(array($f),get_included_files()));
170          $type = files::getMimeType($f);
171          header('Content-Type: '.$type);
172          header('Content-Length: '.filesize($f));
173          if ($type != "text/css" || $core->blog->settings->url_scan == 'path_info') {
174               readfile($f);
175          } else {
176               echo preg_replace('#url\((?!(http:)|/)#','url('.$core->blog->url.'rateit/',file_get_contents($f));
177          }
178          exit;
179     }
180
181     private static function get_image_infos(&$core)
182     {
183          $ft = $core->blog->themes_path.$core->blog->settings->theme.'/img/rateit-stars.png';
184          $fp = dirname(__FILE__).'/default-templates/img/rateit-stars.png';
185          if (file_exists($ft)){
186               $i = getimagesize($ft);
187               return array('w'=>$i[0],'h'=>floor($i[1] /3));
188          } elseif (file_exists($fp)){
189               $i = getimagesize($fp);
190               return array('w'=>$i[0],'h'=>floor($i[1] /3));
191          } else {
192               return 16;
193          }
194     }
195
196     public static function publicHeadContent(&$core)
197     {
198          $blocs = array('rateit','rateitwidget');
199
200          # --BEHAVIOR-- publicRatingBlocsRateit
201          $core->callBehavior('publicRatingBlocsRateit',$blocs);
202
203          foreach($blocs AS $k => $v) {
204               $blocs[$k] = "'".html::escapeJS($v)."'";
205          }
206          $blocs = implode(',',$blocs);
207
208          $s = self::get_image_infos($core);
209          echo "\n".
210          '<script type="text/javascript" src="'.$core->blog->url.'rateit/js/jquery.rating.pack.js"></script>'."\n".
211          '<!-- Code CSS de jquery-rating -->'.
212          '<style type="text/css">'.
213          'div.rating-cancel,div.star-rating{float:left;width:'.($s['w']+1).'px;height:'.$s['h'].'px;text-indent:-999em;cursor:pointer;display:block;background:transparent;overflow:hidden} '.
214          'div.rating-cancel,div.rating-cancel a{background:url('.$core->blog->url.'rateit/img/delete.png) no-repeat 0 -16px} '.
215          'div.star-rating,div.star-rating a{background:url('.$core->blog->url.'rateit/img/rateit-stars.png) no-repeat 0 0px} '.
216          'div.rating-cancel a,div.star-rating a{display:block;width:'.$s.'px;height:100%;background-position:0 0px;border:0} '.
217          'div.star-rating-on a{background-position:0 -'.$s['h'].'px!important} '.
218          'div.star-rating-hover a{background-position:0 -'.($s['h']*2).'px} '.
219          'div.star-rating-readonly a{cursor:default !important} '.
220          'div.star-rating{background:transparent!important;overflow:hidden!important} '.
221          '</style>'.
222          '<script type="text/javascript" src="'.$core->blog->url.'rateit/js/rateit.js"></script>'."\n".
223          "<style type=\"text/css\">\n@import url(".$core->blog->url."rateit/rateit.css);\n</style>\n".
224          '<script type="text/javascript">'."\n".
225          "//<![CDATA[\n".
226          "rateIt.prototype.blocs = [".$blocs."];\n".
227          "rateIt.prototype.blog_uid = '".html::escapeJS($core->blog->uid)."';\n".
228          "rateIt.prototype.enable_cookie = '".($core->blog->settings->rateit_userident > 0 ? '1' : '0')."';\n".
229          "rateIt.prototype.image_size = '".$s['h']."';\n".
230          "rateIt.prototype.service_url = '".html::escapeJS($core->blog->url.'rateitservice/')."';\n".
231          "rateIt.prototype.msg_thanks = '".html::escapeJS($core->blog->settings->rateit_msgthanks)."';\n".
232          "\n//]]>\n".
233          "</script>\n";
234     }
235
236     public static function publicEntryAfterContent($core,$_ctx,$force=false)
237     {
238          if (!$core->blog->settings->rateit_poststpl && !$force) return;
239
240          $f = 'tpl/rateit.html';
241          $f = self::path($f);
242          $d = dirname($f);
243          $core->tpl->setPath($core->tpl->getPath(),$d);
244
245          if ('' != ($fc = $core->tpl->getData('rateit.html'))) {
246               echo $fc;
247          }
248     }
249
250     public static function rateIt($attr,$content)
251     {
252          return 
253          '<?php'."\n".
254          '$rateit_params = new ArrayObject();'."\n".
255          '$rateit_params->type = "";'."\n".
256          '$rateit_params->id = 0;'."\n".
257          'if ($_ctx->exists("posts")) {'."\n".
258          '    $rateit_params->type = "post";'."\n".
259          '    $rateit_params->id = $_ctx->posts->post_id;'."\n".
260          '}'."\n".
261
262          # --BEHAVIOR-- templateRateIt
263          '$core->callBehavior("templateRateIt",$rateit_params);'."\n".
264
265          '$rateit_type = $rateit_params->type ;'."\n".
266          '$rateit_id = $rateit_params->id ;'."\n".
267          '$rateIt = new rateIt($core);'."\n".
268          '$rateit_voted= $rateIt->voted($rateit_type,$rateit_id);'."\n".
269          '$_ctx->rateIt = $rateIt->get($rateit_type,$rateit_id);'."\n".
270          '?>'."\n".$content."\n".
271          '<?php'."\n".
272          'unset($rateit_type,$rateit_id,$rateit_voted);'."\n".
273          '$_ctx->rateIt = null;'."\n".
274          '?>';
275     }
276
277     public static function rateItIf($attr,$content)
278     {
279          $res =
280          '<?php $star_if = 0;'."\n";
281          if (isset($attr['has_vote'])) {
282               $res .= $attr['has_vote'] == 1 ?
283                     'if ($_ctx->rateIt->total > 0) { $star_if = 1; }' :
284                     'if ($_ctx->rateIt->total == 0) { $star_if = 1; }';
285          }
286
287          $res .=
288          'if ($star_if == 1) { ?>'.$content.'<?php } ?>'."\n";
289
290          return $res;
291     }
292
293     public static function rateItTitle($attr)
294     {
295          global $core,$_ctx;
296          $f = $core->tpl->getFilters($attr);
297
298          $title = '';
299          if ($_ctx->exists("posts"))
300               $title = __('Rate this entry');
301
302          # --BEHAVIOR-- templateRateItTitle
303          $call_title = $core->callBehavior('templateRateItTitle',$title);
304          if (!empty($call_title))
305               $title = $call_title;
306
307          return '<?php echo '.sprintf($f,"'$title'").'; ?>';
308     }
309
310     public static function rateItLinker($attr)
311     {
312          global $core;
313          $f = $core->tpl->getFilters($attr);
314          return 
315          '<?php '."\n".
316          'echo \'<form class="rateit-linker" id="rateit-linker-\'.$rateit_type.\'-\'.$rateit_id.\'" action="'.$core->blog->url.'rateitnow/\'.$rateit_type.\'/\'.$rateit_id.\'/" method="post"><p>\';'."\n".
317          'for($i=0;$i<$_ctx->rateIt->quotient;$i++){'."\n".
318          '    $dis = $rateit_voted ?'."\n".
319          '         \' disabled="disabled"\' : \'\';'."\n".
320          '    $chk = $_ctx->rateIt->note > $i && $_ctx->rateIt->note <= $i+1 ? '.
321          '         \' checked="checked"\' : \'\';'."\n".
322          '    echo \'<input name="rateit-\'.$rateit_type.\'-\'.$rateit_id.\'" class="rateit-\'.$rateit_type.\'-\'.$rateit_id.\'" type="radio" value="\'.($i+1).\'"\'.$chk.$dis.\'/>'."\n".
323          '\'; } ?>'."\n".
324          '<input type="submit" name="submit" value="'.__('Vote').'"/>'."\n".
325          '</p></form>';
326     }
327
328     public static function rateItFullnote($attr)
329     {         global $core;
330          $f = $core->tpl->getFilters($attr);
331          return '<?php echo \'<span id="rateit-fullnote-\'.$rateit_type.\'-\'.$rateit_id.\'"  class="rateit-fullnote">\'.'.sprintf($f,'$_ctx->rateIt->note."/".$_ctx->rateIt->quotient').'.\'</span>\'; ?>';
332     }
333
334     public static function rateItQuotient($attr)
335     {
336          return self::rateItValue($attr,'quotient');
337     }
338
339     public static function rateItTotal($attr)
340     {
341          return self::rateItValue($attr,'total');
342     }
343
344     public static function rateItMax($attr)
345     {
346          return self::rateItValue($attr,'max');
347     }
348
349     public static function rateItMin($attr)
350     {
351          return self::rateItValue($attr,'min');
352     }
353
354     public static function rateItNote($attr)
355     {
356          return self::rateItValue($attr,'note');
357     }
358
359     private static function rateItValue($a,$r)
360     {
361          global $core;
362          $f = $core->tpl->getFilters($a);
363          return '<?php echo \'<span id="rateit-'.$r.'-\'.$rateit_type.\'-\'.$rateit_id.\'"  class="rateit-'.$r.'">\'.'.sprintf($f,'$_ctx->rateIt->'.$r).'.\'</span>\'; ?>';
364     }
365}
366
367?>
Note: See TracBrowser for help on using the repository browser.

Sites map