Exemplo n.º 1
0
 /**
  * Handle Ban getting from Server
  *
  * @param InputRequest $request
  */
 public function handlebans(InputRequest $request)
 {
     $successResponse = false;
     \Log::info($request->ban);
     if (!$request->has('ban') || !$request->has('key') || $request->get('key') != env('SERVER_QUERY_KEY')) {
         printf("ERROR: Invalid Response!");
         exit(0);
     }
     $banList = $request->get('ban');
     foreach ($banList as $ban) {
         $ban = explode(" \$ ", $ban);
         $type = $ban[0];
         if ($type == "ban") {
             $ip_address = $ban[1];
             $name = $ban[2];
             $name = str_replace('(VIEW)', '', $name);
             $name = str_replace('(SPEC)', '', $name);
             $admin = $ban[3];
             $admin = str_replace('(VIEW)', '', $admin);
             $admin = str_replace('(SPEC)', '', $admin);
             $admin_ip = $ban[4];
             $reason = $ban[5] == "" ? null : $ban[5];
             $banQ = Ban::findOrNullByIP($ip_address);
             // If no ban for this IP at Server then do add Ban.
             if ($banQ == null) {
                 $player_country_id = 0;
                 $geoip = \App::make('geoip');
                 try {
                     $ip_without_mask = str_replace('*', '0', $ip_address);
                     if ($player_geoip = $geoip->city($ip_without_mask)) {
                         $player_isoCode = $player_geoip->country->isoCode;
                         $country = Country::where('countryCode', 'LIKE', $player_isoCode)->first();
                         /**
                          * Country returned is not in Countrie table
                          */
                         if ($country == null) {
                             $player_country_id = 0;
                         } else {
                             $player_country_id = $country->id;
                         }
                     }
                 } catch (\Exception $e) {
                     switch ($e) {
                         case $e instanceof \InvalidArgumentException:
                             $player_country_id = 0;
                             break;
                         case $e instanceof \GeoIp2\Exception\AddressNotFoundException:
                             $player_country_id = 0;
                             break;
                         default:
                             $player_country_id = 0;
                             break;
                     }
                 }
                 $newBan = Ban::create(['name' => $name, 'ip_address' => $ip_address, 'server_id' => 1, 'country_id' => $player_country_id, 'reason' => $reason, 'admin_name' => $admin, 'admin_ip' => $admin_ip, 'status' => true]);
             } else {
                 if (!$banQ->status) {
                     $banQ->name = $name;
                     $banQ->status = true;
                     $banQ->banned_till = null;
                     $banQ->reason = $ban[5] == "" ? $banQ->reason : $ban[5];
                     $banQ->updated_by = $admin;
                     $banQ->updated_by_site = false;
                     $banQ->save();
                 }
             }
             $successResponse = true;
         } else {
             if ($type == "unban") {
                 $ip_address = $ban[1];
                 $admin = $ban[2];
                 $admin = str_replace('(VIEW)', '', $admin);
                 $admin = str_replace('(SPEC)', '', $admin);
                 $banQ = Ban::findOrNullByIP($ip_address);
                 if ($banQ && $banQ->status) {
                     $banQ->status = false;
                     $banQ->banned_till = Carbon::now();
                     $banQ->updated_by = $admin;
                     $banQ->updated_by_site = false;
                     $banQ->save();
                 }
                 $successResponse = true;
             }
         }
     }
     if ($successResponse) {
         print "SUCCESS";
         exit(0);
     }
 }