OSDN Git Service

CHANGE: SQL92標準に合わせてINSERT文を変更。
[nucleus-jp/nucleus-next.git] / nucleus / libs / BAN.php
1 <?php
2 /*
3  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4  * Copyright (C) 2002-2009 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-2009 The Nucleus Group
17  * @version $Id: BAN.php 1525 2011-06-21 10:20:19Z sakamocchi $
18  */
19
20 class BAN {
21
22         /**
23           * Checks if a given IP is banned from commenting/voting
24           *
25           * Returns 0 when not banned, or a BANINFO object containing the
26           * message and other information of the ban
27           */
28         function isBanned($blogid, $ip) {
29                 $blogid = intval($blogid);
30                 $query = 'SELECT * FROM '.sql_table('ban').' WHERE blogid='.$blogid;
31                 $res = sql_query($query);
32                 while ($obj = sql_fetch_object($res)) {
33                         $found = i18n::strpos ($ip, $obj->iprange);
34                         if (!($found === false))
35                                 // found a match!
36                                         return new BANINFO($obj->iprange, $obj->reason);
37                 }
38                 return 0;
39         }
40
41         /**
42          * BAN::addBan()
43          * Adds a new ban to the banlist. Returns 1 on success, 0 on error
44          * 
45          * @param       Integer $blogid ID for weblog
46          * @param       String  $iprange        IP range
47          * @param       String  $reason reason for banning
48          * @return      Boolean
49          * 
50          */
51         function addBan($blogid, $iprange, $reason)
52         {
53                 global $manager;
54                 
55                 $blogid = intval($blogid);
56                 
57                 $manager->notify(
58                         'PreAddBan',
59                         array(
60                                 'blogid' => $blogid,
61                                 'iprange' => &$iprange,
62                                 'reason' => &$reason
63                         )
64                 );
65                 
66                 $query = "INSERT INTO %s (blogid, iprange, reason) VALUES (%d, '%s', '%s')";
67                 $query = sprintf($query, sql_table('ban'), $blogid, sql_real_escape_string($iprange), sql_real_escape_string($reason));
68                 $res = sql_query($query);
69                 
70                 $manager->notify(
71                         'PostAddBan',
72                         array(
73                                 'blogid' => $blogid,
74                                 'iprange' => $iprange,
75                                 'reason' => $reason
76                         )
77                 );
78                 return $res ? 1 : 0;
79         }
80         
81         /**
82           * Removes a ban from the banlist (correct iprange is needed as argument)
83           * Returns 1 on success, 0 on error
84           */
85         function removeBan($blogid, $iprange) {
86                 global $manager;
87                 $blogid = intval($blogid);
88
89                 $manager->notify('PreDeleteBan', array('blogid' => $blogid, 'range' => $iprange));
90
91                 $query = 'DELETE FROM '.sql_table('ban')." WHERE blogid=$blogid and iprange='" .sql_real_escape_string($iprange). "'";
92                 sql_query($query);
93
94                 $result = (sql_affected_rows() > 0);
95
96                 $manager->notify('PostDeleteBan', array('blogid' => $blogid, 'range' => $iprange));
97
98                 return $result;
99         }
100 }
101
102 class BANINFO {
103         var $iprange;
104         var $message;
105
106         function BANINFO($iprange, $message) {
107                 $this->iprange = $iprange;
108                 $this->message = $message;
109         }
110 }
111
112
113 ?>