OSDN Git Service

コピーライトの期間表記を「2002-2012」に更新
[nucleus-jp/nucleus-jp-ancient.git] / nucleus / libs / BAN.php
1 <?php
2 /*
3  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4  * Copyright (C) 2002-2012 The Nucleus Group
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  * (see nucleus/documentation/index.html#license for more info)
11  */
12 /**
13  * PHP class responsible for ban-management.
14  *
15  * @license http://nucleuscms.org/license.txt GNU General Public License
16  * @copyright Copyright (C) 2002-2012 The Nucleus Group
17  * @version $Id$
18  * $NucleusJP: BAN.php,v 1.6 2006/07/20 08:01:52 kimitake Exp $
19  */
20
21 class BAN {
22
23         /**
24           * Checks if a given IP is banned from commenting/voting
25           *
26           * Returns 0 when not banned, or a BANINFO object containing the
27           * message and other information of the ban
28           */
29         function isBanned($blogid, $ip) {
30                 $blogid = intval($blogid);
31                 $query = 'SELECT * FROM '.sql_table('ban').' WHERE blogid='.$blogid;
32                 $res = sql_query($query);
33                 while ($obj = sql_fetch_object($res)) {
34                         $found = strpos ($ip, $obj->iprange);
35                         if (!($found === false))
36                                 // found a match!
37                                         return new BANINFO($obj->iprange, $obj->reason);
38                 }
39                 return 0;
40         }
41
42         /**
43           * Adds a new ban to the banlist. Returns 1 on success, 0 on error
44           */
45         function addBan($blogid, $iprange, $reason) {
46                 global $manager;
47
48                 $blogid = intval($blogid);
49
50                 $param = array(
51                         'blogid'        =>  $blogid,
52                         'iprange'       => &$iprange,
53                         'reason'        => &$reason
54                 );
55                 $manager->notify('PreAddBan', $param);
56
57                 $query = 'INSERT INTO '.sql_table('ban')." (blogid, iprange, reason) VALUES "
58                            . "($blogid,'".sql_real_escape_string($iprange)."','".sql_real_escape_string($reason)."')";
59                 $res = sql_query($query);
60
61                 $param = array(
62                         'blogid'        => $blogid,
63                         'iprange'       => $iprange,
64                         'reason'        => $reason
65                 );
66                 $manager->notify('PostAddBan', $param);
67
68                 return $res ? 1 : 0;
69         }
70
71         /**
72           * Removes a ban from the banlist (correct iprange is needed as argument)
73           * Returns 1 on success, 0 on error
74           */
75         function removeBan($blogid, $iprange) {
76                 global $manager;
77                 $blogid = intval($blogid);
78
79                 $param = array(
80                         'blogid'        => $blogid,
81                         'range'         => $iprange
82                 );
83                 $manager->notify('PreDeleteBan', $param);
84
85                 $query = 'DELETE FROM '.sql_table('ban')." WHERE blogid=$blogid and iprange='" .sql_real_escape_string($iprange). "'";
86                 sql_query($query);
87
88                 $result = (sql_affected_rows() > 0);
89
90                 $param = array(
91                         'blogid'        => $blogid,
92                         'range'         => $iprange
93                 );
94                 $manager->notify('PostDeleteBan', $param);
95
96                 return $result;
97         }
98 }
99
100 class BANINFO {
101         var $iprange;
102         var $message;
103
104         function BANINFO($iprange, $message) {
105                 $this->iprange = $iprange;
106                 $this->message = $message;
107         }
108 }
109
110
111 ?>