OSDN Git Service

FIX:$manager->notify()の第二引数に変数を渡すように修正。
[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                 $data = array(
60                                 'blogid' => $blogid,
61                                 'iprange' => &$iprange,
62                                 'reason' => &$reason
63                         );
64                 $manager->notify('PreAddBan', $data);
65                 
66                 $query = 'INSERT INTO %s (blogid, iprange, reason) VALUES (%d, %s, %s)';
67                 $query = sprintf($query, sql_table('ban'), intval($blogid), DB::quoteValue($iprange), DB::quoteValue($reason));
68                 $res = DB::execute($query);
69                 
70                 $manager->notify('PostAddBan', $data);
71                 
72                 return $res !== FALSE ? 1 : 0;
73         }
74         
75         /**
76          * Removes a ban from the banlist (correct iprange is needed as argument)
77          * Returns 1 on success, 0 on error
78          */
79         public function removeBan($blogid, $iprange)
80         {
81                 global $manager;
82
83                 $data = array(
84                                 'blogid' => $blogid,
85                                 'range' => $iprange
86                         );
87                 $manager->notify('PreDeleteBan', $data);
88                 
89                 $query = 'DELETE FROM %s WHERE blogid=%d and iprange=%s';
90                 $query = sprintf($query, sql_table('ban'), intval($blogid), DB::quoteValue($iprange));
91                 $res = DB::execute($query);
92                 
93                 $manager->notify('PostDeleteBan', $data);
94                 
95                 return $res !== FALSE ? 1 : 0;
96         }
97 }
98
99 class BanInfo
100 {
101         public $iprange;
102         public $message;
103         
104         public function __construct($iprange, $message)
105         {
106                 $this->iprange = $iprange;
107                 $this->message = $message;
108                 return;
109         }
110 }