OSDN Git Service

FIX: NP_ThumbnailをNucleus 3.65/PHP5.4/MySQL5.5で動作するよう修正
[nucleus-jp/nucleus-plugins.git] / NP_RejectASCIIComment / tags / NP_RejectASCIIComment.php
1 <?php
2
3 /**
4   * REJECT 'ONLY ASCII' COMMENT AND TRACKBACK PLUGIN FOR NucleusCMS
5   * PHP versions 4 and 5
6   *
7   * This program is free software; you can redistribute it and/or
8   * modify it under the terms of the GNU General Public License
9   * as published by the Free Software Foundation; either version 2
10   * of the License, or (at your option) any later version.
11   * (see nucleus/documentation/index.html#license for more info)
12   * 
13   * 
14   * @author     shizuki
15   * @copyright  2006-2007 shizuki
16   * @license    http://www.gnu.org/licenses/gpl.txt
17   *             GNU GENERAL PUBLIC LICENSE Version 2, June 1991
18   * @version    0.3
19   * @link       http://shizuki.kinezumi.net
20   *
21   * 0.3  Release version
22   * 0.2  supports SpamCheck API 2
23   *      supports NP_TrackBack
24   * 0.1  initial
25   *
26   **/
27
28 class NP_RejectASCIIComment extends NucleusPlugin
29 {
30
31         // name of plugin
32         function getName()
33         {
34                 $retData = 'Reject ASCII Comment and TrackBack';
35                 return $retData;
36         }
37
38         // author of plugin
39         function getAuthor()
40         {
41                 $retData = 'shizuki';
42                 return $retData;
43         }
44
45         // an URL to the plugin website
46         function getURL()
47         {
48                 $retData = 'http://shizuki.kinezumi.net';
49                 return $retData;
50         }
51
52         // version of the plugin
53         function getVersion()
54         {
55                 $retData = '0.3';
56                 return $retData;
57         }
58
59         // a description to be shown on the installed plugins listing
60         function getDescription()
61         {
62                 $retData = _REJECT_ASCII_DESCRIPTION;
63                 if (_CHARSET != 'UTF-8') {
64                         mb_convert_encoding($retData, _CHARSET, 'UTF-8');
65                 }
66                 return $retData;
67         }
68
69         function getEventList()
70         {
71                 return array(
72                                          'SpamCheck',
73                                          'ValidateForm'
74                                         );
75         }
76
77         function install()
78         {
79                 $optionData = _REJECT_ASCII_HOOK;
80                 if (_CHARSET != 'UTF-8') {
81                         mb_convert_encoding($optionData, _CHARSET, 'UTF-8');
82                 }
83                 $this->createBlogOption('hook',  $optionData, 'yesno', 'no');
84                 $optionData = _REJECT_ASCII_DELTB;
85                 if (_CHARSET != 'UTF-8') {
86                         mb_convert_encoding($optionData, _CHARSET, 'UTF-8');
87                 }
88                 $this->createBlogOption('deltb', $optionData, 'yesno', 'no');
89         }
90
91         function supportsFeature ($what)
92         {
93                 switch ($what)
94                 {
95                         case 'SqlTablePrefix':
96                                 return 1;
97                         default:
98                                 return 0;
99                 }
100         }
101
102         function init()
103         {
104                 global $admin;
105                 $langFile = $this->getDirectory() . 'japanese.php';
106                 if (file_exists($langFile)) {
107                         include_once($langFile);
108                 }
109         }
110
111         function event_SpamCheck(&$data)
112         {
113                 global $DIR_PLUGINS, $member;
114
115                 $spamCheck = $data['spamcheck'];
116                 if ($spamCheck['result'] == TRUE) {
117                         return FALSE;
118                 }
119
120                 if ($member->isLoggedIn()) {
121                         return FALSE;
122                 }
123
124                 $type     = (!empty($spamCheck['type'])) ? $spamCheck['type'] : 'post';
125                 $encoding = $this->_detect_encoding($excerpt);
126                 switch ($type) {
127                         case 'comment':
128                                 $checkData = $spamCheck['body'];
129                                 $item_id   = intval($spamCheck['id']);
130                                 break;
131                         case 'trackback':
132                                 $checkData = $spamCheck['excerpt'] . $spamCheck['title'] . $spamCheck['blogname'];
133                                 $item_id   = intval($spamCheck['id']);
134                                 break;
135                         default:
136                 }
137                 $bid       = intval(getBlogIDFromItemID($item_id));
138                 $bname     = getBlogNameFromID($bid);
139                 $inque     = 'SELECT ititle as result FROM %s WHERE inumber = %d';
140                 $inque     = sprintf($inque, sql_table('item'), $item_id);
141                 $iname     = quickQuery($inque);
142                 $checkData = str_replace($bname, '', $checkData);
143                 $checkData = str_replace($iname, '', $checkData);
144                 $checkData = ereg_replace("\r|\n","",$checkData);
145                 $checkData = mb_convert_encoding($checkData, 'UTF-8', $encoding);
146                 if ($checkData && !preg_match('/[\x80-\xff]/', $checkData)) {
147                         $checkType = array(
148                                                            'comment'   => _REJECT_ASCII_COMMENT,
149                                                            'trackback' => _REJECT_ASCII_TRACKBACK,
150                                                           );
151                         $info      = _REJECT_ASCII_INFOHEAD . $item_id . $checkType[$type];
152                         if (_CHARSET != 'UTF-8') {
153                                 mb_convert_encoding($info, _CHARSET, 'UTF-8');
154                         }
155 //                      ACTIONLOG :: add(INFO, 'RejectASCII: ' . $info . shorten(strip_tags($checkData), 50, '...') . ')');
156                         $data['spamcheck']['result'] = TRUE;
157                         if ($type == 'trackback' && $this->getBlogOption($bid, 'deltb') == 'yes') {
158                                 header("Location: " . createItemLink($item_id));
159                                 exit();
160                         }
161                         return TRUE;
162                 } else {
163                         return FALSE;
164                 }
165         }
166
167         function event_ValidateForm(&$data) {
168                 global $DIR_PLUGINS, $member;
169                 if (getNucleusVersion() >= '330' || $data['type'] != 'comment') {
170                         return TRUE;
171                 }
172                 $item_id = intval($data['comment']['itemid']);
173                 $blog_id = intval(getBlogIDFromItemID($item_id));
174
175                 if ($this->getBlogOption($blog_id, 'hook') == 'no') {
176                         return TRUE;
177                 }
178
179                 if ($member->isLoggedIn()) {
180                         return TRUE;
181                 }
182
183                 $spamcheck = array(
184                                                    'type' => 'comment',
185                                                    'id'   => $item_id,
186                                                    'user' => $data['comment']['user'],
187                                                    'body' => $data['comment']['body'],
188                                                   );
189
190                 $param     = array(
191                                                    'spamcheck' => &$spamcheck
192                                                   );
193                 $this->event_SpamCheck($param);
194
195                 if (isset($spamcheck['result']) && $spamcheck['result'] == TRUE) {
196                         $data['error'] = -1;
197                         header("Location: " . createItemLink($item_id));
198                         exit();
199                 }
200
201                 return true;
202         }
203
204         function _detect_encoding($string)
205         {
206                 if (preg_match("/;\s*charset=([^\n]+)/is", serverVar("CONTENT_TYPE"), $regs)) {
207                         $encoding =  strtoupper(trim($regs[1]));
208                 } else {
209                         $encoding = '';
210                 }
211                 $mbstrInput = strtolower(ini_get("mbstring.http_input"));
212                 if (!empty($encoding) && (mb_http_input('P') == '' || $mbstrInput == 'pass')) {
213                         return $encoding;
214                 } else {
215                         if (_CHARSET == 'UTF-8') {
216                                 $encChars = 'UTF-8,EUC-JP,SJIS,ISO-8859-1,ASCII,JIS';
217                         } else {
218                                 $encChars = 'EUC-JP,UTF-8,SJIS,ISO-8859-1,ASCII,JIS';
219                         }
220                         $encoding = mb_detect_encoding($string, $encChars);
221                 }
222                 return ($encoding) ? $encoding : 'ISO-8859-1';
223         }
224
225 }
226