XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
center.php
Go to the documentation of this file.
1 <?php
2 //require_once XOOPS_ROOT_PATH.'/include/cp_header.php' ;
3 include_once 'admin_header.php'; //mb problem: it shows always the same "Center" tab
4 require_once XOOPS_ROOT_PATH.'/class/pagenav.php' ;
5 require_once dirname(dirname(__FILE__)).'/class/gtickets.php' ;
6 
7 //dirty trick to get navigation working with system menus
8 if(isset($_GET['num'])) {
9  $_SERVER['REQUEST_URI'] = 'admin/center.php?page=center';
10 }
11 
14 
15 // GET vars
16 $pos = empty( $_GET[ 'pos' ] ) ? 0 : intval( $_GET[ 'pos' ] ) ;
17 $num = empty( $_GET[ 'num' ] ) ? 20 : intval( $_GET[ 'num' ] ) ;
18 
19 // Table Name
20 $log_table = $db->prefix( $mydirname."_log" ) ;
21 
22 // Protector object
23 require_once dirname(dirname(__FILE__)).'/class/protector.php' ;
26 $conf = $protector->getConf() ;
27 
28 
29 //
30 // transaction stage
31 //
32 
33 if( ! empty( $_POST['action'] ) ) {
34 
35  // Ticket check
36  if ( ! $xoopsGTicket->check( true , 'protector_admin' ) ) {
37  redirect_header(XOOPS_URL.'/',3,$xoopsGTicket->getErrors());
38  }
39 
40  if( $_POST['action'] == 'update_ips' ) {
41  $error_msg = '' ;
42 
43  $lines = empty( $_POST['bad_ips'] ) ? array() : explode( "\n" , trim( $_POST['bad_ips'] ) ) ;
44  $bad_ips = array() ;
45  foreach( $lines as $line ) {
46  @list( $bad_ip , $jailed_time ) = explode( ':' , $line , 2 ) ;
47  $bad_ips[ trim( $bad_ip ) ] = empty( $jailed_time ) ? 0x7fffffff : intval( $jailed_time ) ;
48  }
49  if( ! $protector->write_file_badips( $bad_ips ) ) {
50  $error_msg .= _AM_MSG_BADIPSCANTOPEN ;
51  }
52 
53  $group1_ips = empty( $_POST['group1_ips'] ) ? array() : explode( "\n" , trim( $_POST['group1_ips'] ) ) ;
54  foreach( array_keys( $group1_ips ) as $i ) {
55  $group1_ips[$i] = trim( $group1_ips[$i] ) ;
56  }
57  $fp = @fopen( $protector->get_filepath4group1ips() , 'w' ) ;
58  if( $fp ) {
59  @flock( $fp , LOCK_EX ) ;
60  fwrite( $fp , serialize( array_unique( $group1_ips ) ) . "\n" ) ;
61  @flock( $fp , LOCK_UN ) ;
62  fclose( $fp ) ;
63  } else {
64  $error_msg .= _AM_MSG_GROUP1IPSCANTOPEN ;
65  }
66 
67  $redirect_msg = $error_msg ? $error_msg : _AM_MSG_IPFILESUPDATED ;
68  redirect_header( "center.php?page=center" , 2 , $redirect_msg ) ;
69  exit ;
70 
71  } else if( $_POST['action'] == 'delete' && isset( $_POST['ids'] ) && is_array( $_POST['ids'] ) ) {
72  // remove selected records
73  foreach( $_POST['ids'] as $lid ) {
74  $lid = intval( $lid ) ;
75  $db->query( "DELETE FROM $log_table WHERE lid='$lid'" ) ;
76  }
77  redirect_header( "center.php?page=center" , 2 , _AM_MSG_REMOVED ) ;
78  exit ;
79 
80  } else if( $_POST['action'] == 'deleteall' ) {
81  // remove all records
82  $db->query( "DELETE FROM $log_table" ) ;
83  redirect_header( "center.php?page=center" , 2 , _AM_MSG_REMOVED ) ;
84  exit ;
85 
86  } else if( $_POST['action'] == 'compactlog' ) {
87  // compactize records (removing duplicated records (ip,type)
88  $result = $db->query( "SELECT `lid`,`ip`,`type` FROM $log_table ORDER BY lid DESC" ) ;
89  $buf = array() ;
90  $ids = array() ;
91  while( list( $lid , $ip , $type ) = $db->fetchRow( $result ) ) {
92  if( isset( $buf[ $ip . $type ] ) ) {
93  $ids[] = $lid ;
94  } else {
95  $buf[ $ip . $type ] = true ;
96  }
97  }
98  $db->query( "DELETE FROM $log_table WHERE lid IN (".implode(',',$ids).")" ) ;
99  redirect_header( "center.php?page=center" , 2 , _AM_MSG_REMOVED ) ;
100  exit ;
101  }
102 }
103 
104 
105 //
106 // display stage
107 //
108 
109 // query for listing
110 $rs = $db->query( "SELECT count(lid) FROM $log_table" ) ;
111 list( $numrows ) = $db->fetchRow( $rs ) ;
112 $prs = $db->query( "SELECT l.lid, l.uid, l.ip, l.agent, l.type, l.description, UNIX_TIMESTAMP(l.timestamp), u.uname FROM $log_table l LEFT JOIN ".$db->prefix("users")." u ON l.uid=u.uid ORDER BY timestamp DESC LIMIT $pos,$num" ) ;
113 
114 // Page Navigation
115 $nav = new XoopsPageNav( $numrows , $num , $pos , 'pos' , "page=center&num=$num" ) ;
116 $nav_html = $nav->renderNav( 10 ) ;
117 
118 // Number selection
120 $num_array = array( 20 , 100 , 500 , 2000 ) ;
121 foreach( $num_array as $n ) {
122  if( $n == $num ) {
123  $num_options .= "<option value='$n' selected='selected'>$n</option>\n" ;
124  } else {
125  $num_options .= "<option value='$n'>$n</option>\n" ;
126  }
127 }
128 
129 // beggining of Output
131 include dirname(__FILE__).'/mymenu.php' ;
132 
133 // title
134 echo "<h3 style='text-align:left;'>".$xoopsModule->name()."</h3>\n" ;
135 
136 // configs writable check
137 if( ! is_writable( dirname(dirname(__FILE__)).'/configs' ) ) {
138  printf( "<p style='color:red;font-weight:bold;'>"._AM_FMT_CONFIGSNOTWRITABLE."</p>\n" , dirname(dirname(__FILE__)).'/configs' ) ;
139 }
140 
141 // bad_ips
142 $bad_ips = $protector->get_bad_ips( true ) ;
143 uksort( $bad_ips , 'protector_ip_cmp' ) ;
145 foreach( $bad_ips as $bad_ip => $jailed_time ) {
146  $line = $jailed_time ? $bad_ip . ':' . $jailed_time : $bad_ip ;
147  $line = str_replace( ':2147483647' , '' , $line ) ; // remove :0x7fffffff
148  $bad_ips4disp .= htmlspecialchars( $line , ENT_QUOTES ) . "\n" ;
149 }
150 
151 // group1_ips
152 $group1_ips = $protector->get_group1_ips() ;
153 usort( $group1_ips , 'protector_ip_cmp' ) ;
154 $group1_ips4disp = htmlspecialchars(implode("\n",$group1_ips),ENT_QUOTES) ;
155 
156 // edit configs about IP ban and IPs for group=1
157 echo "
158 <form name='ConfigForm' action='' method='POST'>
159 ".$xoopsGTicket->getTicketHtml(__LINE__,1800,'protector_admin')."
160 <input type='hidden' name='action' value='update_ips' />
161 <table width='95%' class='outer' cellpadding='4' cellspacing='1'>
162  <tr valign='top' align='left'>
163  <td class='head'>
164  "._AM_TH_BADIPS."
165  </td>
166  <td class='even'>
167  <textarea name='bad_ips' id='bad_ips' style='width:200px;height:60px;'>$bad_ips4disp</textarea>
168  <br />
169  ".htmlspecialchars($protector->get_filepath4badips())."
170  </td>
171  </tr>
172  <tr valign='top' align='left'>
173  <td class='head'>
174  "._AM_TH_GROUP1IPS."
175  </td>
176  <td class='even'>
177  <textarea name='group1_ips' id='group1_ips' style='width:200px;height:60px;'>$group1_ips4disp</textarea>
178  <br />
179  ".htmlspecialchars($protector->get_filepath4group1ips())."
180  </td>
181  </tr>
182  <tr valign='top' align='left'>
183  <td class='head'>
184  </td>
185  <td class='even'>
186  <input type='submit' value='"._GO."' />
187  </td>
188  </tr>
189 </table>
190 </form>
191 " ;
192 
193 
194 // header of log listing
195 echo "
196 <table width='95%' border='0' cellpadding='4' cellspacing='0'><tr><td>
197 <form action='' method='GET' style='margin-bottom:0px;'>
198  <table width='95%' border='0' cellpadding='4' cellspacing='0'>
199  <tr>
200  <td align='left'>
201  <select name='num' onchange='submit();'>$num_options</select>
202  <input type='submit' value='"._SUBMIT."'>
203  </td>
204  <td align='right'>
205  $nav_html
206  </td>
207  </tr>
208  </table>
209 </form>
210 <form name='MainForm' action='' method='POST' style='margin-top:0px;'>
211 ".$xoopsGTicket->getTicketHtml(__LINE__,1800,'protector_admin')."
212 <input type='hidden' name='action' value='' />
213 <table width='95%' class='outer' cellpadding='4' cellspacing='1'>
214  <tr valign='middle'>
215  <th width='5'><input type='checkbox' name='dummy' onclick=\"with(document.MainForm){for(i=0;i<length;i++){if(elements[i].type=='checkbox'){elements[i].checked=this.checked;}}}\" /></th>
216  <th>"._AM_TH_DATETIME."</th>
217  <th>"._AM_TH_USER."</th>
218  <th>"._AM_TH_IP."<br />"._AM_TH_AGENT."</th>
219  <th>"._AM_TH_TYPE."</th>
220  <th>"._AM_TH_DESCRIPTION."</th>
221  </tr>
222 " ;
223 
224 // body of log listing
225 $oddeven = 'odd' ;
226 while( list( $lid , $uid , $ip , $agent , $type , $description , $timestamp , $uname ) = $db->fetchRow( $prs ) ) {
227  $oddeven = ( $oddeven == 'odd' ? 'even' : 'odd' ) ;
228 
229  $ip = htmlspecialchars( $ip , ENT_QUOTES ) ;
230  $type = htmlspecialchars( $type , ENT_QUOTES ) ;
231  $description = htmlspecialchars( $description , ENT_QUOTES ) ;
232  $uname = htmlspecialchars( ( $uid ? $uname : _GUESTS ) , ENT_QUOTES ) ;
233 
234  // make agents shorter
235  if( preg_match( '/MSIE\s+([0-9.]+)/' , $agent , $regs ) ) {
236  $agent_short = 'IE ' . $regs[1] ;
237  } else if( stristr( $agent , 'Gecko' ) !== false ) {
238  $agent_short = strrchr( $agent , ' ' ) ;
239  } else {
240  $agent_short = substr( $agent , 0 , strpos( $agent , ' ' ) ) ;
241  }
242  $agent4disp = htmlspecialchars( $agent , ENT_QUOTES ) ;
243  $agent_desc = $agent == $agent_short ? $agent4disp : htmlspecialchars( $agent_short , ENT_QUOTES ) . "<img src='../images/dotdotdot.gif' alt='$agent4disp' title='$agent4disp' />" ;
244 
245  echo "
246  <tr>
247  <td class='$oddeven'><input type='checkbox' name='ids[]' value='$lid' /></td>
248  <td class='$oddeven'>".formatTimestamp($timestamp)."</td>
249  <td class='$oddeven'>$uname</td>
250  <td class='$oddeven'>$ip<br />$agent_desc</td>
251  <td class='$oddeven'>$type</td>
252  <td class='$oddeven' width='100%'>$description</td>
253  </tr>\n" ;
254 }
255 
256 // footer of log listing
257 echo "
258  <tr>
259  <td colspan='8' align='left'>"._AM_LABEL_REMOVE."<input type='button' value='"._AM_BUTTON_REMOVE."' onclick='if(confirm(\""._AM_JS_REMOVECONFIRM."\")){document.MainForm.action.value=\"delete\"; submit();}' /></td>
260  </tr>
261 </table>
262 <div align='right'>
263  $nav_html
264 </div>
265 <div style='clear:both;'><br /><br /></div>
266 <div align='right'>
267 "._AM_LABEL_COMPACTLOG."<input type='button' value='"._AM_BUTTON_COMPACTLOG."' onclick='if(confirm(\""._AM_JS_COMPACTLOGCONFIRM."\")){document.MainForm.action.value=\"compactlog\"; submit();}' />
268 &nbsp;
269 "._AM_LABEL_REMOVEALL."<input type='button' value='"._AM_BUTTON_REMOVEALL."' onclick='if(confirm(\""._AM_JS_REMOVEALLCONFIRM."\")){document.MainForm.action.value=\"deleteall\"; submit();}' />
270 </div>
271 </form>
272 </td></tr></table>
273 " ;
274 
276 
277 
278 function protector_ip_cmp( $a , $b )
279 {
280  $as = explode( '.' , $a ) ;
281  $aval = @$as[0] * 167777216 + @$as[1] * 65536 + @$as[2] * 256 + @$as[3] ;
282  $bs = explode( '.' , $b ) ;
283  $bval = @$bs[0] * 167777216 + @$bs[1] * 65536 + @$bs[2] * 256 + @$bs[3] ;
284 
285  return $aval > $bval ? 1 : -1 ;
286 }
287 
288 ?>