new Block object, block expiry, optional sysop blocks
[lhc/web/wiklou.git] / includes / Block.php
1 <?
2 # Blocks and bans object
3
4 #TODO: This could be used everywhere, but it isn't.
5
6 # All the functions in this class assume the object is either explicitly
7 # loaded or filled. It is not load-on-demand.
8
9 # To use delete(), you only need to fill $mAddress
10
11 class Block
12 {
13 var $mAddress, $mUser, $mBy, $mReason, $mTimestamp;
14
15 function Block( $address = "", $user = "", $by = 0, $reason = "", $timestamp = "" )
16 {
17 $this->mAddress = $address;
18 $this->mUser = $user;
19 $this->mBy = $by;
20 $this->mReason = $reason;
21 $this->mTimestamp = $timestamp;
22 }
23
24 /*static*/ function newFromDB( $address, $user = 0, $killExpired = true )
25 {
26 $ban = new Block();
27 $ban->load( $address, $user, $killExpired );
28 return $ban;
29 }
30
31 function clear()
32 {
33 $mAddress = $mReason = $mTimestamp = "";
34 $mUser = $mBy = 0;
35 }
36
37 # Get a ban from the DB, with either the given address or the given username
38 function load( $address, $user = 0, $killExpired = true )
39 {
40 $fname = "Block::load";
41 $ret = false;
42 $killed = false;
43
44 if ( 0 == $user ) {
45 $sql = "SELECT * FROM ipblocks WHERE ipb_address='$address'";
46 } else {
47 $sql = "SELECT * FROM ipblocks WHERE (ipb_address='$address' OR ipb_user={$user})";
48 }
49
50
51 $res = wfQuery( $sql, $fname );
52 if ( 0 == wfNumRows( $res ) ) {
53 # User is not blocked
54 $this->clear();
55 } else {
56 # Get first block
57 $row = wfFetchObject( $res );
58 $this->initFromRow( $row );
59
60 if ( $killExpired ) {
61
62 # If requested, delete expired rows
63 do {
64 $killed = $this->deleteIfExpired();
65 $row = wfFetchObject( $res );
66 } while ( $killed && $row );
67
68 # If there were any left after the killing finished, return true
69 if ( $row == false ) {
70 $ret = false;
71 $this->clear();
72 } else {
73 $ret = true;
74 }
75 } else {
76 $ret = true;
77 }
78 }
79 wfFreeResult( $res );
80 return $ret;
81 }
82
83 function initFromRow( $row )
84 {
85 $this->mAddress = $row->ipb_address;
86 $this->mReason = $row->ipb_reason;
87 $this->mTimestamp = $row->ipb_timestamp;
88 $this->mUser = $row->ipb_user;
89 $this->mBy = $row->ipb_by;
90 }
91
92 # Callback with a Block object for every block
93 /*static*/ function enumBlocks( $callback, $tag, $killExpired = true )
94 {
95 $sql = "SELECT * FROM ipblocks ORDER BY ipb_timestamp";
96 $res = wfQuery( $sql, "Block::enumBans" );
97 $block = new Block();
98
99 while ( $row = wfFetchObject( $res ) ) {
100 $block->initFromRow( $row );
101 if ( $killExpired ) {
102 if ( !$block->deleteIfExpired() ) {
103 $callback( $block, $tag );
104 }
105 } else {
106 $callback( $block, $tag );
107 }
108 }
109 wfFreeResult( $res );
110 }
111
112 function delete()
113 {
114 wfQuery( "DELETE FROM ipblocks WHERE ipb_address='{$this->mAddress}'",
115 "Block::delete" );
116 }
117
118 function insert()
119 {
120 $sql = "INSERT INTO ipblocks (ipb_address, ipb_user, ipb_by, " .
121 "ipb_reason, ipb_timestamp ) VALUES ('{$this->mAddress}', {$this->mUser}, " .
122 "{$this->mBy}, '" . wfStrencode( $this->mReason ) . "','{$this->mTimestamp}')";
123 wfQuery( $sql, "Block::insert" );
124 }
125
126 function deleteIfExpired()
127 {
128 if ( $this->isExpired() ) {
129 $this->delete();
130 return true;
131 } else {
132 return false;
133 }
134 }
135
136 function isExpired()
137 {
138 global $wgIPBlockExpiration, $wgUserBlockExpiration;
139
140 $period = $this->mUser ? $wgUserBlockExpiration : $wgIPBlockExpiration;
141
142 # Period==0 means no expiry
143 if ( !$period ) {
144 return false;
145 }
146 $expiry = wfTimestamp2Unix( $this->mTimestamp ) + $period;
147 $now = wfTimestamp2Unix( wfTimestampNow() );
148 if ( $now > $expiry ) {
149 return true;
150 } else {
151 return false;
152 }
153 }
154
155 function isValid()
156 {
157 return $this->mAddress != "";
158 }
159
160
161 function updateTimestamp()
162 {
163 $sql = "UPDATE ipblocks SET ipb_timestamp='" . wfTimestampNow() . "' WHERE ipb_address='{$this->mAddress}'";
164 wfQuery( "UPDATE ipblocks SET ipb_timestamp='" . wfTimestampNow() .
165 "' WHERE ipb_address='{$this->mAddress}'", "Block::updateTimestamp" );
166 }
167 }
168 ?>