OSDN Git Service

MERGE: リビジョン1873〜1893。skinnable-masterのマージ
[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 1812 2012-05-01 14:59:07Z 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         public function isBanned($blogid, $ip)
29         {
30                 $blogid = intval($blogid);
31                 $query = sprintf('SELECT * FROM %s WHERE blogid=%d', sql_table('ban'), intval($blogid));
32                 $res = DB::getResult($query);
33                 foreach ( $res as $row )
34                 {
35                         $found = i18n::strpos ($ip, $row['iprange']);
36                         if ( $found !== false )
37                         {
38                                 // found a match!
39                                 return new BanInfo($row['iprange'], $row['reason']);
40                         }
41                 }
42                 return 0;
43         }
44
45         /**
46          * Ban::addBan()
47          * Adds a new ban to the banlist. Returns 1 on success, 0 on error
48          * 
49          * @param       Integer $blogid ID for weblog
50          * @param       String  $iprange        IP range
51          * @param       String  $reason reason for banning
52          * @return      Boolean
53          * 
54          */
55         public function addBan($blogid, $iprange, $reason)
56         {
57                 global $manager;
58                 
59                 $manager->notify(
60                         'PreAddBan',
61                         array(
62                                 'blogid' => $blogid,
63                                 'iprange' => &$iprange,
64                                 'reason' => &$reason
65                         )
66                 );
67                 
68                 $query = 'INSERT INTO %s (blogid, iprange, reason) VALUES (%d, %s, %s)';
69                 $query = sprintf($query, sql_table('ban'), intval($blogid), DB::quoteValue($iprange), DB::quoteValue($reason));
70                 $res = DB::execute($query);
71                 
72                 $manager->notify(
73                         'PostAddBan',
74                         array(
75                                 'blogid' => $blogid,
76                                 'iprange' => $iprange,
77                                 'reason' => $reason
78                         )
79                 );
80                 
81                 return $res !== FALSE ? 1 : 0;
82         }
83         
84         /**
85          * Removes a ban from the banlist (correct iprange is needed as argument)
86          * Returns 1 on success, 0 on error
87          */
88         public function removeBan($blogid, $iprange)
89         {
90                 global $manager;
91                 
92                 $manager->notify(
93                         'PreDeleteBan',
94                         array(
95                                 'blogid' => $blogid,
96                                 'range' => $iprange
97                         )
98                 );
99                 
100                 $query = 'DELETE FROM %s WHERE blogid=%d and iprange=%s';
101                 $query = sprintf($query, sql_table('ban'), intval($blogid), DB::quoteValue($iprange));
102                 $res = DB::execute($query);
103                 
104                 $manager->notify(
105                         'PostDeleteBan',
106                         array(
107                                 'blogid' => $blogid,
108                                 'range' => $iprange
109                         )
110                 );
111                 
112                 return $res !== FALSE ? 1 : 0;
113         }
114 }
115
116 class BanInfo
117 {
118         public $iprange;
119         public $message;
120         
121         public function __construct($iprange, $message)
122         {
123                 $this->iprange = $iprange;
124                 $this->message = $message;
125                 return;
126         }
127 }