Dotclear

source: plugins/wfwcomment/class.dc.wfwcomment.php @ 1009

Revision 1009, 6.6 KB checked in by pep, 15 years ago (diff)

wfwcomment : initial import in DC Lab

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ----------------------------------
3#
4# This file is part of WFWComment, a plugin for DotClear2.
5#
6# Copyright (c) 2006-2009 Pep and contributors.
7# Licensed under the GPL version 2.0 license.
8# See LICENSE file or
9# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10#
11# -- END LICENSE BLOCK ------------------------------------
12
13class dcWFWComment
14{
15     public $core;
16
17     public function __construct(&$core)
18     {
19          $this->core =& $core;
20          $this->con =& $this->core->con;
21     }
22
23     public function receive($post_id)
24     {
25          header('Content-Type: text/plain; charset=UTF-8');
26
27          $rawdata = file_get_contents("php://input");
28          if (empty($rawdata)) {
29               http::head(400,'Bad request');
30               echo "Missing data";
31               return;
32          }
33
34          $post_id = (integer) $post_id;
35          $is_tb_like = true;
36          $err = false;
37          $msg = '';
38
39          // Looking for <autor> or <dc:author>.
40          // If found, incoming data wouln't be treated as a trackback
41          if (preg_match('!<author[^>]*>(.*)</author[^>]*>!i', $rawdata, $match)) {
42               $mail = $match[1];
43          }
44
45          if (preg_match('!<dc:creator[^>]*>(.*)</dc:creator[^>]*>!i', $rawdata, $match)) {
46               if (preg_match('!^(.*)\((.*)\)!i', $match[1], $names)) {
47                    $name = $names[2];
48                    $mail = $names[1];
49               } else {
50                    $name = $match[1];
51               }
52               $is_tb_like = false;
53          }
54
55          // Got <link> ?
56          if (preg_match('!<link[^>]*>(.*)</link[^>]*>!i', $rawdata, $match)) {
57               $link = $match[1];
58          }
59
60          // Got <title> ?
61          if (preg_match('!<title[^>]*>(.*)</title[^>]*>!i', $rawdata, $match)) {
62               $title = $match[1];
63          }
64
65          // Got <source> ?
66          if (preg_match('!<source[^>]*>(.*)</source[^>]*>!i', $rawdata, $match)) {
67               $blog_name = $match[1];
68          }
69
70          // Got <description> ?
71          if (preg_match('!<description[^>]*>(.*)</description[^>]*>!ims', $rawdata, $match)) {
72               if (preg_match('/^<!\[CDATA\[(.*)\]\]>/ims', $match[1], $cdata)) {
73                    $content = $cdata[1];
74               } else {
75                    $content = $match[1];
76               }
77          }
78
79          $name = !empty($name) ? $name : '';
80          $mail = !empty($mail) ? $mail : '';
81          $title = !empty($title) ? $title : '';
82          $content = !empty($content) ? $content : '';
83          $link = !empty($link) ? $link : '';
84          $blog_name = !empty($blog_name) ? $blog_name : '';
85
86          $charset = '';
87          $comment = '';
88
89          if ($this->core->blog === null) {
90               $err = true;
91               $msg = 'No blog.';
92          }
93          elseif ($is_tb_like) {
94               if ($link == '') {
95                    $err = true;
96                    $msg = 'URL parameter is requiered.';
97               }
98               elseif ($blog_name == '') {
99                    $err = true;
100                    $msg = 'Blog name is requiered.';
101               }
102          }
103          else {
104               if (empty($name)) {
105                    $err = true;
106                    $msg = 'You must provide a name.';
107               }
108               elseif (!text::isEmail($mail)) {
109                    $err = true;
110                    $msg = 'You must provide a valid email adress.';
111               }
112          }
113
114          if (!$err) {
115               $post = $this->core->blog->getPosts(array('post_id'=>$post_id));
116
117               if ($post->isEmpty()) {
118                    $err = true;
119                    $msg = 'No such post.';
120               }
121               elseif ($is_tb_like && !$post->trackbacksActive()) {
122                    $err = true;
123                    $msg = 'Trackbacks are not allowed for this post or weblog.';
124               }
125               elseif (!$post->commentsActive()) {
126                    $err = true;
127                    $msg = 'Comments are not allowed for this post or weblog.';
128               }
129          }
130
131          if (!$err)
132          {
133               $charset = $this->getCharsetFromRequest();
134
135               if (!$charset) {
136                    $charset = mb_detect_encoding($title.' '.$content.' '.$blog_name.' '.$name,
137                    'UTF-8,ISO-8859-1,ISO-8859-2,ISO-8859-3,'.
138                    'ISO-8859-4,ISO-8859-5,ISO-8859-6,ISO-8859-7,ISO-8859-8,'.
139                    'ISO-8859-9,ISO-8859-10,ISO-8859-13,ISO-8859-14,ISO-8859-15');
140               }
141
142
143               $link = trim(html::clean($link));
144               $mail = trim(html::clean($mail));
145
146               $cur = $this->core->con->openCursor($this->core->prefix.'comment');
147
148               if ($is_tb_like) {
149                    if (strtolower($charset) != 'utf-8') {
150                         $title = iconv($charset,'UTF-8',$title);
151                         $content = iconv($charset,'UTF-8',$content);
152                         $blog_name = iconv($charset,'UTF-8',$blog_name);
153                    }
154
155                    $title = trim(html::clean($title));
156                    $title = html::decodeEntities($title);
157                    $title = html::escapeHTML($title);
158                    $title = text::cutString($title,60);
159
160                    $content = trim(html::clean($content));
161                    $content = html::decodeEntities($content);
162                    $content = preg_replace('/\s+/ms',' ',$content);
163                    $content = text::cutString($content,252);
164                    $content = html::escapeHTML($content).'...';
165
166                    $blog_name = trim(html::clean($blog_name));
167                    $blog_name = html::decodeEntities($blog_name);
168                    $blog_name = html::escapeHTML($blog_name);
169                    $blog_name = text::cutString($blog_name,60);
170
171                    if (!$blog_name) {
172                         $blog_name = 'Anonymous blog';
173                    }
174
175                    $comment =
176                    "<!-- TB -->\n".
177                    '<p><strong>'.($title ? $title : $blog_name)."</strong></p>\n".
178                    '<p>'.$content.'</p>';
179
180                    $cur->comment_author = $blog_name;
181                    $cur->comment_site = $link;
182                    $cur->comment_content = $comment;
183                    $cur->post_id = $post_id;
184                    $cur->comment_trackback = 1;
185                    $cur->comment_status = $this->core->blog->settings->comments_pub ? 1 : -1;
186                    $cur->comment_ip = http::realIP();
187
188                    $before_behavior = 'publicBeforeTrackbackCreate';
189                    $after_behavior  = 'publicAfterTrackbackCreate';
190               }
191               else {
192                    if (strtolower($charset) != 'utf-8') {
193                         $name = iconv($charset,'UTF-8',$name);
194                         $content = iconv($charset,'UTF-8',$content);
195                    }
196
197                    $name = trim(html::clean($name));
198                    $name = html::decodeEntities($name);
199                    $name = html::escapeHTML($name);
200                    $name = text::cutString($name,60);
201
202                    $content = trim(html::clean($content));
203                    $content = html::decodeEntities($content);
204                    $content = preg_replace('/\s+/ms',' ',$content);
205                    $content = html::escapeHTML($content);
206
207                    $cur->comment_author = $name;
208                    $cur->comment_site = $link;
209                    $cur->comment_email = $mail;
210                    $cur->comment_content = $content;
211                    $cur->post_id = $post_id;
212                    $cur->comment_status = $this->core->blog->settings->comments_pub ? 1 : -1;
213                    $cur->comment_ip = http::realIP();
214
215                    $before_behavior = 'publicBeforeCommentCreate';
216                    $after_behavior  = 'publicAfterCommentCreate';
217               }
218
219               try
220               {
221                    # --BEHAVIOR-- publicBeforeCommentCreate / publicBeforeTrackbackCreate
222                    $this->core->callBehavior($before_behavior,$cur);
223
224                    $comment_id = $this->core->blog->addComment($cur);
225
226                    # --BEHAVIOR-- publicAfterCommentCreate / publicAfterTrackbackCreate
227                    $this->core->callBehavior($after_behavior,$cur,$comment_id);
228               }
229               catch (Exception $e) {
230                    $err = true;
231                    $msg = 'Something went wrong : '.$e->getMessage();
232               }
233          }
234
235          if ($err) {
236               http::head(412,'Precondition failed');
237               if ($msg) {
238                    echo $msg;
239               }
240          }
241     }
242
243     private function getCharsetFromRequest()
244     {
245          if (isset($_SERVER['CONTENT_TYPE']))
246          {
247               if (preg_match('|charset=([a-zA-Z0-9-]+)|',$_SERVER['CONTENT_TYPE'],$m)) {
248                    return $m[1];
249               }
250          }
251
252          return null;
253     }
254}
255?>
Note: See TracBrowser for help on using the repository browser.

Sites map