PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/banned/importBans.php

http://esglobalban.googlecode.com/
PHP | 217 lines | 156 code | 29 blank | 32 comment | 23 complexity | 71d2d6e09f09789bb057deab8195aec6 MD5 | raw file
  1. <?php
  2. /*
  3. This file is part of GlobalBan.
  4. Written by Stefan Jonasson <soynuts@unbuinc.net>
  5. Copyright 2008 Stefan Jonasson
  6. GlobalBan is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. GlobalBan is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with GlobalBan. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. include_once(ROOTDIR."/include/database/class.BanQueries.php");
  18. include_once(ROOTDIR."/include/database/class.ServerQueries.php");
  19. include_once(ROOTDIR."/include/database/class.ReasonQueries.php");
  20. // Import bans from banned_users.cfg file
  21. if($fullPower) {
  22. $bansAdded = false;
  23. $reasonQueries = new ReasonQueries();
  24. // Get list of ban reasons
  25. $banReasons = $reasonQueries->getReasonList();
  26. // If this is set, then that means a server is being added
  27. if(isset($_POST['submitImport'])) {
  28. // Parse input and add bans
  29. //$bans = $_POST['bans'];
  30. $reason = $_POST['reason']; // Reason Id
  31. $tempName = $_FILES['cfgfile']['tmp_name']; // Temp name of when it is uploaded
  32. if(is_uploaded_file($tempName)) {
  33. $bans = "";
  34. $f = fopen($tempName, 'r' );
  35. while( $data = fread( $f, 4096 ) ) { $bans .= $data; }
  36. fclose( $f );
  37. echo $bans;
  38. $banLines = explode("\n", $bans);
  39. $banQueries = new BanQueries();
  40. $serverQueries = new ServerQueries();
  41. // Constant Variables
  42. $serverId = $serverQueries->getFirstServer(); // We need to assign it to a valid server for it to display
  43. $nameOfBanned = ""; // Name is unknown
  44. $banner = ""; // Don't know who banner is, so leave empty
  45. $count = 0;
  46. $failed = 0;
  47. foreach($banLines as $banLine) {
  48. $ban = explode(" ", $banLine);
  49. // 0 = banid (discard)
  50. // 1 = length
  51. // 2 = steamid
  52. $length = $ban[1]; // Length of ban in minutes
  53. $steamId = $ban[2]; // Steam ID of banned
  54. $timeScale = "minutes";
  55. $banId = -1;
  56. if($steamId != "") {
  57. if($length > 0) {
  58. $lengthInSec = $length*60; // Convert to seconds
  59. $expireDate = time() + $lengthInSec; // Expire date
  60. // Add the new ban non-perma ban
  61. $banId = $banQueries->addBan($steamId, $length, $timeScale, $expireDate, $reason, $banner, 0, $nameOfBanned, $serverId, null, '');
  62. } else {
  63. // Add perma ban
  64. $banId = $banQueries->addBan($steamId, $length, $timeScale, time(), $reason, $banner, 0, $nameOfBanned, $serverId, null, '');
  65. }
  66. if($banId > 0) {
  67. $count++;
  68. } else {
  69. $failed++;
  70. }
  71. }
  72. }
  73. $bansAdded = true;
  74. }
  75. }
  76. // The XML form was submitted
  77. if(isset($_POST['submitXMLImport'])) {
  78. $reason = $_POST['reason']; // Reason Id
  79. $tempName = $_FILES['file']['tmp_name']; // Temp name of when it is uploaded
  80. $count = 0;
  81. $alreadyAdded = 0;
  82. $failed = 0;
  83. if(is_uploaded_file($tempName)) {
  84. $banQueries = new BanQueries();
  85. $xml = "";
  86. $f = fopen($tempName, 'r' );
  87. while( $data = fread( $f, 4096 ) ) { $xml .= $data; }
  88. fclose( $f );
  89. preg_match_all("/\<Ban\>(.*?)\<\/Ban\>/s", $xml, $banBlocks);
  90. foreach($banBlocks[1] as $block ) {
  91. preg_match_all( "/\<SteamID\>(.*?)\<\/SteamID\>/", $block, $steamId);
  92. preg_match_all( "/\<IP\>(.*?)\<\/IP\>/", $block, $ip);
  93. preg_match_all( "/\<Name\>(.*?)\<\/Name\>/", $block, $name);
  94. preg_match_all( "/\<Length\>(.*?)\<\/Length\>/", $block, $length);
  95. preg_match_all( "/\<TimeScale\>(.*?)\<\/TimeScale\>/", $block, $timeScale);
  96. preg_match_all( "/\<AddDate\>(.*?)\<\/AddDate\>/", $block, $addDate);
  97. preg_match_all( "/\<ExpireDate\>(.*?)\<\/ExpireDate\>/", $block, $expireDate);
  98. preg_match_all( "/\<Webpage\>(.*?)\<\/Webpage\>/", $block, $webpage);
  99. $id = $banQueries->importBan($steamId[1][0], $name[1][0], $length[1][0], $timeScale[1][0], $addDate[1][0], $expireDate[1][0], $webpage[1][0], $reason, $ip[1][0]);
  100. if($id > 0) {
  101. $count++;
  102. } else {
  103. $alreadyAdded++;
  104. }
  105. }
  106. $bansAdded = true;
  107. }
  108. }
  109. ?>
  110. <?php
  111. // Display that the changes were successful
  112. if($bansAdded) {
  113. ?><h5 class="error"><?=$count?> steam IDs imported to ban table.<br/>
  114. <?php
  115. if($alreadyAdded > 0) {
  116. echo $alreadyAdded . " of the imports already exist in the database.";
  117. }
  118. if($failed > 0) {
  119. echo "Failed to add ".$failed." bans as they already exist.";
  120. }
  121. ?>
  122. </h5><?php
  123. }
  124. ?>
  125. <div class="tborder">
  126. <div id="tableHead">
  127. <div><b>Import Bans</b</div>
  128. </div>
  129. <form action="index.php?page=importBans" method="POST" enctype="multipart/form-data">
  130. <table class="bordercolor" width="100%" cellspacing="1" cellpadding="5" border="0" style="margin-top: 1px;">
  131. <tr class="rowColor2">
  132. <th style="text-align: left;">Import from banned_users.cfg</th>
  133. <th style="text-align: left;">Import from GlobalBan XML file</th>
  134. </tr>
  135. <tr class="rowColor1">
  136. <td>Locate the appropriate banned_users.cfg with the browse button below.</td>
  137. <td>Locate the appropriate banned_users.xml with the browse button below.</td>
  138. </tr>
  139. <tr class="rowColor2">
  140. <td width="1%" nowrap>Reason to apply to all imports:
  141. <select name="reason">
  142. <?php
  143. if(count($banReasons > 0)) {
  144. for($i=0; $i<count($banReasons);$i++) {
  145. $reason = $banReasons[$i];
  146. ?><option value="<?=$reason->getId()?>"><?=$reason->getReason()?></option><?php
  147. }
  148. } else {
  149. ?><option value="-1">Reason List Empty, Populate before proceeding</option><?php
  150. }
  151. ?>
  152. </select>
  153. </td>
  154. <td width="1%" nowrap>Reason to apply to all imports:
  155. <select name="reason">
  156. <?php
  157. if(count($banReasons > 0)) {
  158. for($i=0; $i<count($banReasons);$i++) {
  159. $reason = $banReasons[$i];
  160. ?><option value="<?=$reason->getId()?>"><?=$reason->getReason()?></option><?php
  161. }
  162. } else {
  163. ?><option value="-1">Reason List Empty, Populate before proceeding</option><?php
  164. }
  165. ?>
  166. </select>
  167. </td>
  168. </tr>
  169. <tr class="rowColor1">
  170. <td><input id="file" name="cfgfile" size="40" type="file" /></td>
  171. <td><input id="file" name="file" size="40" type="file" /></td>
  172. </tr>
  173. <tr class="rowColor2">
  174. <td><input type="submit" name="submitImport" value="Import CFG Bans"/></td>
  175. <td><input type="submit" name="submitXMLImport" value="Import XML Bans"/></td>
  176. </tr>
  177. </table>
  178. </form>
  179. </div>
  180. <br/>
  181. <h5>Note: This process may take some time if the import is large.</h5>
  182. <?php
  183. }
  184. ?>