OSDN Git Service

FIX: NP_MediaUtilsをNucleus 3.65/PHP5.4/MySQL5.5で動作するよう修正
[nucleus-jp/nucleus-plugins.git] / trunk / NP_Blacklist / NP_Blacklist.php
1 <?php
2
3 /**
4   * NP_Blacklist(JP) ($Revision: 1.14 $)
5   * by hsur ( http://blog.cles.jp/np_cles )
6   * $Id: NP_Blacklist.php,v 1.14 2008-06-09 10:33:40 hsur Exp $
7   *
8   * Based on NP_Blacklist 0.98
9   * by xiffy
10   * http://forum.nucleuscms.org/viewtopic.php?t=5300
11 */
12
13 /*
14   * Copyright (C) 2005-2008 cles All rights reserved.
15   *
16   * This program is free software; you can redistribute it and/or
17   * modify it under the terms of the GNU General Public License
18   * as published by the Free Software Foundation; either version 2
19   * of the License, or (at your option) any later version.
20   * 
21   * This program is distributed in the hope that it will be useful,
22   * but WITHOUT ANY WARRANTY; without even the implied warranty of
23   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24   * GNU General Public License for more details.
25   * 
26   * You should have received a copy of the GNU General Public License
27   * along with this program; if not, write to the Free Software
28   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
29 */
30
31 require_once(dirname(__FILE__)."/blacklist/blacklist_lib.php");
32
33 class NP_Blacklist extends NucleusPlugin {
34         function getName() {
35                 return 'Blacklist(JP)';
36         }
37         function getAuthor() {
38                 return 'xiffy + hsur';
39         }
40         function getURL() {
41                 return 'http://blog.cles.jp/np_cles/category/31/subcatid/11';
42         }
43         function getVersion() {
44                 return '1.3.1';
45         }
46         function getDescription() {
47                 return '[$Revision: 1.14 $]<br />'.NP_BLACKLIST_description;
48         }
49         function supportsFeature($what) {
50                 switch ($what) {
51                         case 'SqlTablePrefix' :
52                                 return 1;
53                         default :
54                                 return 0;
55                 }
56         }
57
58         function install() {
59                 // create some options
60                 $this->createOption('enabled', NP_BLACKLIST_enabled, 'yesno', 'yes');
61                 $this->createOption('redirect', NP_BLACKLIST_redirect, 'text', '');
62                 $this->createOption('ipblock', NP_BLACKLIST_ipblock, 'yesno', 'yes');
63                 $this->createOption('ipthreshold', NP_BLACKLIST_ipthreshold, 'text', '10');
64                 $this->createOption('SkipNameResolve', NP_BLACKLIST_SkipNameResolve, 'yesno', 'yes');
65
66                 $this->_initSettings();
67         }
68
69         function unInstall() {
70         }
71
72         function getPluginOption($name) {
73                 return $this->getOption($name);
74         }
75
76         function getEventList() {
77                 $this->_initSettings();
78                 return array ('QuickMenu', 'SpamCheck');
79         }
80
81         function hasAdminArea() {
82                 return 1;
83         }
84
85         function init() {
86                 // include language file for this plugin 
87                 $language = ereg_replace('[\\|/]', '', getLanguageName());
88                 if (file_exists($this->getDirectory().'language/'.$language.'.php'))
89                         @ include_once ($this->getDirectory().'language/'.$language.'.php');
90                 else
91                         @ include_once ($this->getDirectory().'language/english.php');
92                 $this->resultCache = false;
93         }
94
95         function event_QuickMenu(& $data) {
96                 global $member, $nucleus, $blogid;
97                 // only show to admins
98                 if (preg_match("/MD$/", $nucleus['version'])) {
99                         $isblogadmin = $member->isBlogAdmin(-1);
100                 } else {
101                         $isblogadmin = $member->isBlogAdmin($blogid);
102                 }
103                 if (!($member->isLoggedIn() && ($member->isAdmin() | $isblogadmin)))
104                         return;
105                 array_push($data['options'], array ('title' => NP_BLACKLIST_name, 'url' => $this->getAdminURL(), 'tooltip' => NP_BLACKLIST_nameTips,));
106         }
107
108         // handle SpamCheck event
109         function event_SpamCheck(& $data) {
110                 if (isset ($data['spamcheck']['result']) && $data['spamcheck']['result'] == true) {
111                         // Already checked... and is spam
112                         return;
113                 }
114
115                 if (!isset ($data['spamcheck']['return'])) {
116                         $data['spamcheck']['return'] = true;
117                 }
118
119                 // for SpamCheck API 2.0 compatibility
120                 if (!$data['spamcheck']['data']) {
121                         switch (strtolower($data['spamcheck']['type'])) {
122                                 case 'comment' :
123                                         $data['spamcheck']['data'] = $data['spamcheck']['body']."\n";
124                                         $data['spamcheck']['data'] .= $data['spamcheck']['author']."\n";
125                                         $data['spamcheck']['data'] .= $data['spamcheck']['url']."\n";
126                                         $data['spamcheck']['data'] .= $data['spamcheck']['email']."\n";
127                                         break;
128                                 case 'trackback' :
129                                         $data['spamcheck']['data'] = $data['spamcheck']['title']."\n";
130                                         $data['spamcheck']['data'] .= $data['spamcheck']['excerpt']."\n";
131                                         $data['spamcheck']['data'] .= $data['spamcheck']['blogname']."\n";
132                                         $data['spamcheck']['data'] .= $data['spamcheck']['url'];
133                                         break;
134                                 case 'referer' :
135                                         $data['spamcheck']['data'] = $data['spamcheck']['url'];
136                                         break;
137                         }
138                 }
139                 $ipblock = ($data['spamcheck']['ipblock'] == true ) || ($data['spamcheck']['live'] == true);
140
141                 // Check for spam
142                 $result = $this->blacklist($data['spamcheck']['type'], $data['spamcheck']['data'], $ipblock);
143
144                 if ($result) {
145                         // Spam found
146                         // logging !
147                         pbl_logspammer($data['spamcheck']['type'].': '.$result);
148                         if (isset ($data['spamcheck']['return']) && $data['spamcheck']['return'] == true) {
149                                 // Return to caller
150                                 $data['spamcheck']['result'] = true;
151                                 $data['spamcheck']['plugin'] = $this->getName();
152                                 $data['spamcheck']['message'] = 'Marked as spam by NP_Blacklist';
153                                 return;
154                         } else {
155                                 $this->_redirect($this->getOption('redirect'));
156                         }
157                 }
158         }
159
160         function blacklist($type, $testString, $ipblock = true) {
161                 global $member;
162                 if ($this->resultCache)
163                         return $this->resultCache.'[Cached]';
164
165                 if ($member->isLoggedIn()) {
166                         return '';
167                 }
168
169                 if ($this->getOption('enabled') == 'yes') {
170                         // update the blacklist first file
171                         //pbl_updateblacklist($this->getOption('update'),false);
172                         if ($ipblock) {
173                                 $ipblock = ($this->getOption('ipblock') == 'yes') ? true : false;
174                         }
175
176                         $result = '';
177                         if ($ipblock || $testString != '') {
178                                 $result = pbl_checkforspam($testString, $ipblock, $this->getOption('ipthreshold'), true);
179                         }
180
181                         if ($result) {
182                                 $this->resultCache = $result;
183                         }
184
185                         return $result;
186                 }
187         }
188
189         function _redirect($url) {
190                 if (!$url) {
191                         header("HTTP/1.0 403 Forbidden");
192                         header("Status: 403 Forbidden");
193
194                         include (dirname(__FILE__).'/blacklist/blocked.txt');
195                 } else {
196                         $url = preg_replace('|[^a-z0-9-~+_.?#=&;,/:@%]|i', '', $url);
197                         header('Location: '.$url);
198                 }
199                 exit;
200         }
201
202         function _initSettings() {
203                 $settingsDir = dirname(__FILE__).'/blacklist/settings/';
204                 $settings = array (
205                         'blacklist.log', 
206                         'blockip.pbl', 
207                         'whiteip.pbl',
208                         'matched.pbl', 
209                         'blacklist.pbl', 
210                         'blacklist.txt', 
211                         'suspects.pbl',
212                         'personal_blacklist.pbl',
213                 );
214
215                 // setup settings
216                 if ($this->_is_writable($settingsDir)) {
217                         // setup distfile\r                      foreach (glob($settingsDir.'*.dist') as $distfile) {
218                                 $userFile = substr($distfile, 0, strlen($distfile)-5);
219                                 if (!file_exists($userFile)) {
220                                         if (copy($distfile, $userFile)) {
221                                                 @chmod($userFile, 0666);
222                                                 $this->_warn("'$userFile' ".NP_BLACKLIST_isCreated);
223                                         } else {
224                                                 $this->_warn("'$userFile' ".NP_BLACKLIST_canNotCreate);
225                                         }
226                                 }
227                         }
228                         
229                         foreach ($settings as $setting) {
230                                 @touch($settingsDir.$setting);
231                         }
232                 }
233
234                 // check settings       
235                 foreach ($settings as $setting) {
236                         $this->_is_writable($settingsDir.$setting);
237                 }
238
239                 // setup and check cache dir
240                 $cacheDir = NP_BLACKLIST_CACHE_DIR;
241                 $this->_is_writable($cacheDir);
242         }
243
244         function _is_writable($file) {
245                 $ret = is_writable($file);
246                 if (!$ret) {
247                         $this->_warn("'$file' ".NP_BLACKLIST_isNotWritable);
248                 }
249                 return $ret;
250         }
251
252         function _warn($msg) {
253                 ACTIONLOG :: add(WARNING, 'Blacklist: '.$msg);
254         }
255
256 }