OSDN Git Service

Subversion由来のタグを削除
[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  * PHP class responsible for ban-management.
13  */
14
15 class BAN {
16
17         /**
18           * Checks if a given IP is banned from commenting/voting
19           *
20           * Returns 0 when not banned, or a BANINFO object containing the
21           * message and other information of the ban
22           */
23         function isBanned($blogid, $ip) {
24                 $blogid = intval($blogid);
25                 $query = 'SELECT * FROM '.sql_table('ban').' WHERE blogid='.$blogid;
26                 $res = sql_query($query);
27                 while ($obj = sql_fetch_object($res)) {
28                         $found = strpos ($ip, $obj->iprange);
29                         if (!($found === false))
30                                 // found a match!
31                                         return new BANINFO($obj->iprange, $obj->reason);
32                 }
33                 return 0;
34         }
35
36         /**
37           * Adds a new ban to the banlist. Returns 1 on success, 0 on error
38           */
39         function addBan($blogid, $iprange, $reason) {
40                 global $manager;
41
42                 $blogid = intval($blogid);
43
44                 $param = array(
45                         'blogid'        =>  $blogid,
46                         'iprange'       => &$iprange,
47                         'reason'        => &$reason
48                 );
49                 $manager->notify('PreAddBan', $param);
50
51                 $query = 'INSERT INTO '.sql_table('ban')." (blogid, iprange, reason) VALUES "
52                            . "($blogid,'".sql_real_escape_string($iprange)."','".sql_real_escape_string($reason)."')";
53                 $res = sql_query($query);
54
55                 $param = array(
56                         'blogid'        => $blogid,
57                         'iprange'       => $iprange,
58                         'reason'        => $reason
59                 );
60                 $manager->notify('PostAddBan', $param);
61
62                 return $res ? 1 : 0;
63         }
64
65         /**
66           * Removes a ban from the banlist (correct iprange is needed as argument)
67           * Returns 1 on success, 0 on error
68           */
69         function removeBan($blogid, $iprange) {
70                 global $manager;
71                 $blogid = intval($blogid);
72
73                 $param = array(
74                         'blogid'        => $blogid,
75                         'range'         => $iprange
76                 );
77                 $manager->notify('PreDeleteBan', $param);
78
79                 $query = 'DELETE FROM '.sql_table('ban')." WHERE blogid=$blogid and iprange='" .sql_real_escape_string($iprange). "'";
80                 sql_query($query);
81
82                 $result = (sql_affected_rows() > 0);
83
84                 $param = array(
85                         'blogid'        => $blogid,
86                         'range'         => $iprange
87                 );
88                 $manager->notify('PostDeleteBan', $param);
89
90                 return $result;
91         }
92 }
93
94 class BANINFO {
95         var $iprange;
96         var $message;
97
98         function BANINFO($iprange, $message) {
99                 $this->iprange = $iprange;
100                 $this->message = $message;
101         }
102 }
103
104
105 ?>