1 | <?php /* -*- tab-width: 5; indent-tabs-mode: t; c-basic-offset: 5 -*- */ |
---|
2 | /***************************************************************\ |
---|
3 | * This is 'Carnaval', a plugin for Dotclear 2 * |
---|
4 | * * |
---|
5 | * Copyright (c) 2007-2008 * |
---|
6 | * Osku and contributors. * |
---|
7 | * * |
---|
8 | * This is an open source software, distributed under the GNU * |
---|
9 | * General Public License (version 2) terms and conditions. * |
---|
10 | * * |
---|
11 | * You should have received a copy of the GNU General Public * |
---|
12 | * License along with 'Carnaval' (see COPYING.txt); * |
---|
13 | * if not, write to the Free Software Foundation, Inc., * |
---|
14 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
---|
15 | \***************************************************************/ |
---|
16 | if (!defined('DC_CONTEXT_ADMIN')) { return; } |
---|
17 | |
---|
18 | class dcCarnaval |
---|
19 | { |
---|
20 | private static $blog; |
---|
21 | private static $con; |
---|
22 | private static $table; |
---|
23 | |
---|
24 | public static $found; // Avoid multiple SQL requests |
---|
25 | |
---|
26 | public static function cssPath() |
---|
27 | { |
---|
28 | global $core; |
---|
29 | return path::real($core->blog->public_path).'/carnaval-css'; |
---|
30 | } |
---|
31 | |
---|
32 | public static function cssURL() |
---|
33 | { |
---|
34 | global $core; |
---|
35 | return $core->blog->settings->public_url.'/carnaval-css'; |
---|
36 | } |
---|
37 | |
---|
38 | public static function init(&$blog) |
---|
39 | { |
---|
40 | self::$blog =& $blog; |
---|
41 | self::$con =& $blog->con; |
---|
42 | self::$table = $blog->prefix.'carnaval'; |
---|
43 | |
---|
44 | self::$found = array( |
---|
45 | 'comments'=>array(), |
---|
46 | 'pings'=>array() |
---|
47 | ); |
---|
48 | } |
---|
49 | |
---|
50 | public static function getClasses($params=array()) |
---|
51 | { |
---|
52 | $strReq = |
---|
53 | 'SELECT class_id, comment_author, comment_author_mail, '. |
---|
54 | 'comment_author_site, comment_class, '. |
---|
55 | 'comment_text_color, comment_background_color '. |
---|
56 | 'FROM '.self::$table.' '. |
---|
57 | "WHERE blog_id = '".self::$con->escape(self::$blog->id)."' "; |
---|
58 | |
---|
59 | if (isset($params['class_id'])) { |
---|
60 | $strReq .= 'AND class_id = '.(integer) $params['class_id'].' '; |
---|
61 | } |
---|
62 | if (isset($params['mail'])) { |
---|
63 | $strReq .= 'AND comment_author_mail <> \'\' '. |
---|
64 | 'AND comment_author_mail = \''. |
---|
65 | self::$con->escape($params['mail']).'\''; |
---|
66 | } |
---|
67 | if (isset($params['site'])) { |
---|
68 | $strReq .= 'AND comment_author_site <> \'\' '. |
---|
69 | 'AND \''.self::$con->escape($params['site']).'\' '. |
---|
70 | 'LIKE CONCAT(comment_author_site,\'%\')'; |
---|
71 | } |
---|
72 | return self::$con->select($strReq); |
---|
73 | } |
---|
74 | |
---|
75 | public static function getClass($id) |
---|
76 | { |
---|
77 | return self::getClasses(array('class_id'=>$id)); |
---|
78 | } |
---|
79 | |
---|
80 | public static function addClass($author,$mail,$site='',$text,$backg,$class) |
---|
81 | { |
---|
82 | $cur = self::$con->openCursor(self::$table); |
---|
83 | |
---|
84 | $cur->blog_id = (string) self::$blog->id; |
---|
85 | $cur->comment_author = (string) $author; |
---|
86 | $cur->comment_author_mail = (string) $mail; |
---|
87 | $cur->comment_author_site = (string) $site; |
---|
88 | $cur->comment_class = (string) $class; |
---|
89 | $cur->comment_text_color = (string) $text; |
---|
90 | $cur->comment_background_color = (string) $backg; |
---|
91 | |
---|
92 | if ($cur->comment_author == '') { |
---|
93 | throw new Exception(__('You must provide a name')); |
---|
94 | } |
---|
95 | if ($cur->comment_class == '') { |
---|
96 | throw new Exception(__('You must provide a CSS Class')); |
---|
97 | } |
---|
98 | if ($cur->comment_author_mail == '' && $cur->comment_author_site == '') { |
---|
99 | throw new Exception(__('You must provide an e-mail or a web site adress')); |
---|
100 | } |
---|
101 | |
---|
102 | $strReq = 'SELECT MAX(class_id) FROM '.self::$table; |
---|
103 | |
---|
104 | $rs = self::$con->select($strReq); |
---|
105 | $cur->class_id = (integer) $rs->f(0) + 1; |
---|
106 | $cur->insert(); |
---|
107 | |
---|
108 | self::$blog->triggerBlog(); |
---|
109 | } |
---|
110 | |
---|
111 | public static function updateClass($id,$author,$mail='',$site='',$text,$backg,$class='') |
---|
112 | { |
---|
113 | $cur = self::$con->openCursor(self::$table); |
---|
114 | $cur->comment_author = $author; |
---|
115 | $cur->comment_author_mail = $mail; |
---|
116 | $cur->comment_author_site = $site; |
---|
117 | $cur->comment_class = $class; |
---|
118 | $cur->comment_text_color = $text; |
---|
119 | $cur->comment_background_color = $backg; |
---|
120 | |
---|
121 | if ($cur->comment_author == '') { |
---|
122 | throw new Exception(__('You must provide a name')); |
---|
123 | } |
---|
124 | if ($cur->comment_class == '') { |
---|
125 | throw new Exception(__('You must provide a CSS Class')); |
---|
126 | } |
---|
127 | if ($cur->comment_author_mail == '' && $cur->comment_author_site == '') { |
---|
128 | throw new Exception(__('You must provide an email or a website address')); |
---|
129 | } |
---|
130 | |
---|
131 | $cur->update('WHERE class_id = '.(integer) $id. |
---|
132 | " AND blog_id = '".self::$con->escape(self::$blog->id)."'"); |
---|
133 | |
---|
134 | self::$blog->triggerBlog(); |
---|
135 | } |
---|
136 | |
---|
137 | |
---|
138 | public static function delClass($id) |
---|
139 | { |
---|
140 | $id = (integer) $id; |
---|
141 | |
---|
142 | $strReq = 'DELETE FROM '.self::$table.' '. |
---|
143 | "WHERE blog_id = '".self::$con->escape(self::$blog->id)."' ". |
---|
144 | 'AND class_id = '.$id.' '; |
---|
145 | |
---|
146 | self::$con->execute($strReq); |
---|
147 | self::$blog->triggerBlog(); |
---|
148 | } |
---|
149 | |
---|
150 | public static function getCommentClass($mail) |
---|
151 | { |
---|
152 | if (isset(self::$found['comments'][$mail])) { |
---|
153 | return self::$found['comments'][$mail]; |
---|
154 | } |
---|
155 | |
---|
156 | $rs = self::getClasses(array('mail'=>$mail)); |
---|
157 | self::$found['comments'][$mail] = |
---|
158 | $rs->isEmpty() ? '' : ' '.$rs->comment_class; |
---|
159 | |
---|
160 | return self::$found['comments'][$mail]; |
---|
161 | } |
---|
162 | |
---|
163 | public static function getPingClass($site) |
---|
164 | { |
---|
165 | if (isset(self::$found['pings'][$site])) { |
---|
166 | return self::$found['pings'][$site]; |
---|
167 | } |
---|
168 | |
---|
169 | $rs = self::getClasses(array('site'=>$site)); |
---|
170 | self::$found['pings'][$site] = |
---|
171 | $rs->isEmpty() ? '' : ' '.$rs->comment_class; |
---|
172 | |
---|
173 | return self::$found['pings'][$site]; |
---|
174 | } |
---|
175 | } |
---|
176 | class carnavalConfig |
---|
177 | { |
---|
178 | public static function adjustColor($c) |
---|
179 | { |
---|
180 | if ($c === '') { |
---|
181 | return ''; |
---|
182 | } |
---|
183 | |
---|
184 | $c = strtoupper($c); |
---|
185 | |
---|
186 | if (preg_match('/^[A-F0-9]{3,6}$/',$c)) { |
---|
187 | $c = '#'.$c; |
---|
188 | } |
---|
189 | |
---|
190 | if (preg_match('/^#[A-F0-9]{6}$/',$c)) { |
---|
191 | return $c; |
---|
192 | } |
---|
193 | |
---|
194 | if (preg_match('/^#[A-F0-9]{3,}$/',$c)) { |
---|
195 | return '#'.substr($c,1,1).substr($c,1,1).substr($c,2,1).substr($c,2,1).substr($c,3,1).substr($c,3,1); |
---|
196 | } |
---|
197 | |
---|
198 | return ''; |
---|
199 | } |
---|
200 | |
---|
201 | public static function imagesPath() |
---|
202 | { |
---|
203 | global $core; |
---|
204 | return path::real($core->blog->public_path).'/carnaval-images'; |
---|
205 | } |
---|
206 | |
---|
207 | public static function imagesURL() |
---|
208 | { |
---|
209 | global $core; |
---|
210 | return $core->blog->settings->public_url.'/carnaval-images'; |
---|
211 | } |
---|
212 | |
---|
213 | public static function canWriteImages($create=false) |
---|
214 | { |
---|
215 | global $core; |
---|
216 | |
---|
217 | $public = path::real($core->blog->public_path); |
---|
218 | $imgs = self::imagesPath(); |
---|
219 | |
---|
220 | if (!function_exists('imagecreatetruecolor') || !function_exists('imagepng') || !function_exists('imagecreatefrompng')) { |
---|
221 | return false; |
---|
222 | } |
---|
223 | |
---|
224 | if (!is_dir($public)) { |
---|
225 | return false; |
---|
226 | } |
---|
227 | |
---|
228 | if (!is_dir($imgs)) { |
---|
229 | if (!is_writable($public)) { |
---|
230 | return false; |
---|
231 | } |
---|
232 | if ($create) { |
---|
233 | files::makeDir($imgs); |
---|
234 | } |
---|
235 | return true; |
---|
236 | } |
---|
237 | |
---|
238 | if (!is_writable($imgs)) { |
---|
239 | return false; |
---|
240 | } |
---|
241 | |
---|
242 | return true; |
---|
243 | } |
---|
244 | |
---|
245 | |
---|
246 | public static function createImages($color,$name) |
---|
247 | { |
---|
248 | if (!self::canWriteImages(true)) { |
---|
249 | throw new Exception(__('Unable to create images.')); |
---|
250 | } |
---|
251 | |
---|
252 | $comment_t = dirname(__FILE__).'/../../plugins/blowupConfig/alpha-img/comment-t.png'; |
---|
253 | $comment_b = dirname(__FILE__).'/../../plugins/blowupConfig/alpha-img/comment-b.png'; |
---|
254 | |
---|
255 | $cval_comment_t = $name.'-comment-t.png'; |
---|
256 | $cval_comment_b = $name.'-comment-b.png'; |
---|
257 | |
---|
258 | self::dropImage($cval_comment_t); |
---|
259 | self::dropImage($cval_comment_b); |
---|
260 | |
---|
261 | $color = self::adjustColor($color); |
---|
262 | |
---|
263 | self::commentImages($color,$comment_t,$comment_b,$cval_comment_t,$cval_comment_b); |
---|
264 | |
---|
265 | } |
---|
266 | |
---|
267 | protected static function commentImages($comment_color,$comment_t,$comment_b,$dest_t,$dest_b) |
---|
268 | { |
---|
269 | $comment_color = sscanf($comment_color,'#%2X%2X%2X'); |
---|
270 | |
---|
271 | $d_comment_t = imagecreatetruecolor(500,25); |
---|
272 | $fill = imagecolorallocate($d_comment_t,$comment_color[0],$comment_color[1],$comment_color[2]); |
---|
273 | imagefill($d_comment_t,0,0,$fill); |
---|
274 | |
---|
275 | $s_comment_t = imagecreatefrompng($comment_t); |
---|
276 | imagealphablending($s_comment_t,true); |
---|
277 | imagecopy($d_comment_t,$s_comment_t,0,0,0,0,500,25); |
---|
278 | |
---|
279 | imagepng($d_comment_t,self::imagesPath().'/'.$dest_t); |
---|
280 | imagedestroy($d_comment_t); |
---|
281 | imagedestroy($s_comment_t); |
---|
282 | |
---|
283 | $d_comment_b = imagecreatetruecolor(500,7); |
---|
284 | $fill = imagecolorallocate($d_comment_b,$comment_color[0],$comment_color[1],$comment_color[2]); |
---|
285 | imagefill($d_comment_b,0,0,$fill); |
---|
286 | |
---|
287 | $s_comment_b = imagecreatefrompng($comment_b); |
---|
288 | imagealphablending($s_comment_b,true); |
---|
289 | imagecopy($d_comment_b,$s_comment_b,0,0,0,0,500,7); |
---|
290 | |
---|
291 | imagepng($d_comment_b,self::imagesPath().'/'.$dest_b); |
---|
292 | imagedestroy($d_comment_b); |
---|
293 | imagedestroy($s_comment_b); |
---|
294 | } |
---|
295 | |
---|
296 | public static function dropImage($img) |
---|
297 | { |
---|
298 | $img = path::real(self::imagesPath().'/'.$img); |
---|
299 | if (is_writable(dirname($img))) { |
---|
300 | @unlink($img); |
---|
301 | @unlink(dirname($img).'/.'.basename($img,'.png').'_sq.jpg'); |
---|
302 | @unlink(dirname($img).'/.'.basename($img,'.png').'_m.jpg'); |
---|
303 | @unlink(dirname($img).'/.'.basename($img,'.png').'_s.jpg'); |
---|
304 | @unlink(dirname($img).'/.'.basename($img,'.png').'_sq.jpg'); |
---|
305 | @unlink(dirname($img).'/.'.basename($img,'.png').'_t.jpg'); |
---|
306 | } |
---|
307 | } |
---|
308 | } |
---|
309 | ?> |
---|