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 | |
---|
14 | if (!defined('DC_RC_PATH')) return; |
---|
15 | |
---|
16 | require dirname(__FILE__).'/_widgets.php'; |
---|
17 | |
---|
18 | $core->url->register('rateit','rateit','^rateit/(.+)$', |
---|
19 | array('rateItPublic','file_get_contents')); |
---|
20 | |
---|
21 | if (!$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 | |
---|
78 | class 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 dirname(&$f) |
---|
93 | { |
---|
94 | $d = array_pop(explode(PATH_SEPARATOR, DC_PLUGINS_ROOT.'/rateIt/default-templates')); |
---|
95 | $f = $d.'/'.$f; |
---|
96 | return dirname($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 | $path = self::dirname($f); |
---|
157 | if (!is_dir($path)) { |
---|
158 | self::p404(); |
---|
159 | exit; |
---|
160 | } |
---|
161 | |
---|
162 | $allowed_types = array('png','jpg','jpeg','gif','css','js','swf'); |
---|
163 | if (!file_exists($f) || !in_array(files::getExtension($f),$allowed_types)) { |
---|
164 | self::p404(); |
---|
165 | exit; |
---|
166 | } |
---|
167 | |
---|
168 | //http::cache(array_merge(array($f),get_included_files())); |
---|
169 | $type = files::getMimeType($f); |
---|
170 | header('Content-Type: '.$type); |
---|
171 | header('Content-Length: '.filesize($f)); |
---|
172 | if ($type != "text/css" || $core->blog->settings->url_scan == 'path_info') { |
---|
173 | readfile($f); |
---|
174 | } else { |
---|
175 | echo preg_replace('#url\((?!(http:)|/)#','url('.$core->blog->url.'rateit/',file_get_contents($f)); |
---|
176 | } |
---|
177 | exit; |
---|
178 | } |
---|
179 | |
---|
180 | private static function get_image_infos(&$core) |
---|
181 | { |
---|
182 | $ft = $core->blog->themes_path.$core->blog->settings->theme.'/img/rateit-stars.png'; |
---|
183 | $fp = dirname(__FILE__).'/default-templates/img/rateit-stars.png'; |
---|
184 | if (file_exists($ft)){ |
---|
185 | $i = getimagesize($ft); |
---|
186 | return array('w'=>$i[0],'h'=>floor($i[1] /3)); |
---|
187 | } elseif (file_exists($fp)){ |
---|
188 | $i = getimagesize($fp); |
---|
189 | return array('w'=>$i[0],'h'=>floor($i[1] /3)); |
---|
190 | } else { |
---|
191 | return 16; |
---|
192 | } |
---|
193 | } |
---|
194 | |
---|
195 | public static function publicHeadContent(&$core) |
---|
196 | { |
---|
197 | $blocs = array('rateit','rateitwidget'); |
---|
198 | |
---|
199 | # --BEHAVIOR-- publicRatingBlocsRateit |
---|
200 | $core->callBehavior('publicRatingBlocsRateit',$blocs); |
---|
201 | |
---|
202 | foreach($blocs AS $k => $v) { |
---|
203 | $blocs[$k] = "'".html::escapeJS($v)."'"; |
---|
204 | } |
---|
205 | $blocs = implode(',',$blocs); |
---|
206 | |
---|
207 | $s = self::get_image_infos($core); |
---|
208 | echo "\n". |
---|
209 | '<script type="text/javascript" src="'.$core->blog->url.'rateit/js/jquery.rating.pack.js"></script>'."\n". |
---|
210 | '<!-- Code CSS de jquery-rating -->'. |
---|
211 | '<style type="text/css">'. |
---|
212 | '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} '. |
---|
213 | 'div.rating-cancel,div.rating-cancel a{background:url('.$core->blog->url.'rateit/img/delete.png) no-repeat 0 -16px} '. |
---|
214 | 'div.star-rating,div.star-rating a{background:url('.$core->blog->url.'rateit/img/rateit-stars.png) no-repeat 0 0px} '. |
---|
215 | 'div.rating-cancel a,div.star-rating a{display:block;width:'.$s.'px;height:100%;background-position:0 0px;border:0} '. |
---|
216 | 'div.star-rating-on a{background-position:0 -'.$s['h'].'px!important} '. |
---|
217 | 'div.star-rating-hover a{background-position:0 -'.($s['h']*2).'px} '. |
---|
218 | 'div.star-rating-readonly a{cursor:default !important} '. |
---|
219 | 'div.star-rating{background:transparent!important;overflow:hidden!important} '. |
---|
220 | '</style>'. |
---|
221 | '<script type="text/javascript" src="'.$core->blog->url.'rateit/js/rateit.js"></script>'."\n". |
---|
222 | "<style type=\"text/css\">\n@import url(".$core->blog->url."rateit/rateit.css);\n</style>\n". |
---|
223 | '<script type="text/javascript">'."\n". |
---|
224 | "//<![CDATA[\n". |
---|
225 | "rateIt.prototype.blocs = [".$blocs."];\n". |
---|
226 | "rateIt.prototype.blog_uid = '".html::escapeJS($core->blog->uid)."';\n". |
---|
227 | "rateIt.prototype.enable_cookie = '".($core->blog->settings->rateit_userident > 0 ? '1' : '0')."';\n". |
---|
228 | "rateIt.prototype.image_size = '".$s['h']."';\n". |
---|
229 | "rateIt.prototype.service_url = '".html::escapeJS($core->blog->url.'rateitservice/')."';\n". |
---|
230 | "rateIt.prototype.msg_thanks = '".html::escapeJS($core->blog->settings->rateit_msgthanks)."';\n". |
---|
231 | "\n//]]>\n". |
---|
232 | "</script>\n"; |
---|
233 | } |
---|
234 | |
---|
235 | public static function publicEntryAfterContent($core,$_ctx,$force=false) |
---|
236 | { |
---|
237 | if (!$core->blog->settings->rateit_poststpl && !$force) return; |
---|
238 | |
---|
239 | $f = 'tpl/rateit.html'; |
---|
240 | $d = self::dirname($f); |
---|
241 | $core->tpl->setPath($core->tpl->getPath(),$d); |
---|
242 | |
---|
243 | if ('' != ($fc = $core->tpl->getData('rateit.html'))) { |
---|
244 | echo $fc; |
---|
245 | } |
---|
246 | } |
---|
247 | |
---|
248 | public static function rateIt($attr,$content) |
---|
249 | { |
---|
250 | return |
---|
251 | '<?php'."\n". |
---|
252 | '$rateit_params = new ArrayObject();'."\n". |
---|
253 | '$rateit_params->type = "";'."\n". |
---|
254 | '$rateit_params->id = 0;'."\n". |
---|
255 | 'if ($_ctx->exists("posts")) {'."\n". |
---|
256 | ' $rateit_params->type = "post";'."\n". |
---|
257 | ' $rateit_params->id = $_ctx->posts->post_id;'."\n". |
---|
258 | '}'."\n". |
---|
259 | |
---|
260 | # --BEHAVIOR-- templateRateIt |
---|
261 | '$core->callBehavior("templateRateIt",$rateit_params);'."\n". |
---|
262 | |
---|
263 | '$rateit_type = $rateit_params->type ;'."\n". |
---|
264 | '$rateit_id = $rateit_params->id ;'."\n". |
---|
265 | '$rateIt = new rateIt($core);'."\n". |
---|
266 | '$rateit_voted= $rateIt->voted($rateit_type,$rateit_id);'."\n". |
---|
267 | '$_ctx->rateIt = $rateIt->get($rateit_type,$rateit_id);'."\n". |
---|
268 | '?>'."\n".$content."\n". |
---|
269 | '<?php'."\n". |
---|
270 | 'unset($rateit_type,$rateit_id,$rateit_voted);'."\n". |
---|
271 | '$_ctx->rateIt = null;'."\n". |
---|
272 | '?>'; |
---|
273 | } |
---|
274 | |
---|
275 | public static function rateItIf($attr,$content) |
---|
276 | { |
---|
277 | $res = |
---|
278 | '<?php $star_if = 0;'."\n"; |
---|
279 | if (isset($attr['has_vote'])) { |
---|
280 | $res .= $attr['has_vote'] == 1 ? |
---|
281 | 'if ($_ctx->rateIt->total > 0) { $star_if = 1; }' : |
---|
282 | 'if ($_ctx->rateIt->total == 0) { $star_if = 1; }'; |
---|
283 | } |
---|
284 | |
---|
285 | $res .= |
---|
286 | 'if ($star_if == 1) { ?>'.$content.'<?php } ?>'."\n"; |
---|
287 | |
---|
288 | return $res; |
---|
289 | } |
---|
290 | |
---|
291 | public static function rateItTitle($attr) |
---|
292 | { |
---|
293 | global $core,$_ctx; |
---|
294 | $f = $core->tpl->getFilters($attr); |
---|
295 | |
---|
296 | $title = ''; |
---|
297 | if ($_ctx->exists("posts")) |
---|
298 | $title = __('Rate this entry'); |
---|
299 | |
---|
300 | # --BEHAVIOR-- templateRateItTitle |
---|
301 | $call_title = $core->callBehavior('templateRateItTitle',$title); |
---|
302 | if (!empty($call_title)) |
---|
303 | $title = $call_title; |
---|
304 | |
---|
305 | return '<?php echo '.sprintf($f,"'$title'").'; ?>'; |
---|
306 | } |
---|
307 | |
---|
308 | public static function rateItLinker($attr) |
---|
309 | { |
---|
310 | global $core; |
---|
311 | $f = $core->tpl->getFilters($attr); |
---|
312 | return |
---|
313 | '<?php '."\n". |
---|
314 | '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". |
---|
315 | 'for($i=0;$i<$_ctx->rateIt->quotient;$i++){'."\n". |
---|
316 | ' $dis = $rateit_voted ?'."\n". |
---|
317 | ' \' disabled="disabled"\' : \'\';'."\n". |
---|
318 | ' $chk = $_ctx->rateIt->note > $i && $_ctx->rateIt->note <= $i+1 ? '. |
---|
319 | ' \' checked="checked"\' : \'\';'."\n". |
---|
320 | ' echo \'<input name="rateit-\'.$rateit_type.\'-\'.$rateit_id.\'" class="rateit-\'.$rateit_type.\'-\'.$rateit_id.\'" type="radio" value="\'.($i+1).\'"\'.$chk.$dis.\'/>'."\n". |
---|
321 | '\'; } ?>'."\n". |
---|
322 | '<input type="submit" name="submit" value="'.__('Vote').'"/>'."\n". |
---|
323 | '</p></form>'; |
---|
324 | } |
---|
325 | |
---|
326 | public static function rateItFullnote($attr) |
---|
327 | { global $core; |
---|
328 | $f = $core->tpl->getFilters($attr); |
---|
329 | return '<?php echo \'<span id="rateit-fullnote-\'.$rateit_type.\'-\'.$rateit_id.\'" class="rateit-fullnote">\'.'.sprintf($f,'$_ctx->rateIt->note."/".$_ctx->rateIt->quotient').'.\'</span>\'; ?>'; |
---|
330 | } |
---|
331 | |
---|
332 | public static function rateItQuotient($attr) |
---|
333 | { |
---|
334 | return self::rateItValue($attr,'quotient'); |
---|
335 | } |
---|
336 | |
---|
337 | public static function rateItTotal($attr) |
---|
338 | { |
---|
339 | return self::rateItValue($attr,'total'); |
---|
340 | } |
---|
341 | |
---|
342 | public static function rateItMax($attr) |
---|
343 | { |
---|
344 | return self::rateItValue($attr,'max'); |
---|
345 | } |
---|
346 | |
---|
347 | public static function rateItMin($attr) |
---|
348 | { |
---|
349 | return self::rateItValue($attr,'min'); |
---|
350 | } |
---|
351 | |
---|
352 | public static function rateItNote($attr) |
---|
353 | { |
---|
354 | return self::rateItValue($attr,'note'); |
---|
355 | } |
---|
356 | |
---|
357 | private static function rateItValue($a,$r) |
---|
358 | { |
---|
359 | global $core; |
---|
360 | $f = $core->tpl->getFilters($a); |
---|
361 | return '<?php echo \'<span id="rateit-'.$r.'-\'.$rateit_type.\'-\'.$rateit_id.\'" class="rateit-'.$r.'">\'.'.sprintf($f,'$_ctx->rateIt->'.$r).'.\'</span>\'; ?>'; |
---|
362 | } |
---|
363 | } |
---|
364 | |
---|
365 | ?> |
---|