<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Paste;
use App\Log;
use App\User;
use \Input;
use DB;
use Carbon\Carbon;

class CustomController extends Controller
{
    private function jsonError($msg = null){
        $r = (is_null($msg) ? ['status' => 'err'] : ['status' => 'err', 'msg' => $msg]);
        return response()->json($r);
    }

    public function iplogger($link) { // this is only sent to the opps in very rare situations
        $file = fopen(env('IP_LOG_PATH'), 'a');
        fwrite($file, "LINK \"$link\": (" . Carbon::now() . ") - IP " . $_SERVER['HTTP_X_REAL_IP'] . " - USERAGENT " . $_SERVER['HTTP_USER_AGENT'] . "\n");
        fclose($file);
        return abort(404);
    }


    public function meowbahh() {
        $myUser = newGetUser();
        $paste = Paste::find(155142);
        $comments = ($myUser != null && $myUser->isDeveloper(true) ? DB::table('comments')->where('doxId', $paste->id)->orderBy('date', 'desc')->get() : DB::table('comments')->where(['doxId' => $paste->id, 'hidden' => 0])->orderBy('date', 'desc')->get());

        // if(!$myUser || (!$myUser->isAdmin() && !in_array(strtolower($myUser->username), ['cloaked', 'solarwindsadmin', 'synqronize']))){
        //     return abort(403);
        // }

        if(isset($_SERVER['HTTP_USER_AGENT'])){ // increment views
            $cx = $_SERVER[env('REAL_IP_HEADER')];
            $cx = substr($cx, strlen($cx) - 6, strlen($cx));
            $ua = md5($cx.$_SERVER['HTTP_USER_AGENT']);
            if(DB::table('views')->where(['ua' => $ua, 'id' => $paste->id])->count() < 1){
                $paste->increment('views');
                DB::table('views')->insert(['ua' => $ua, 'id' => $paste->id, 'date' => floor(microtime(true) * 1000)]);
            }
        }

        $rate = null;
        if(!is_null($myUser)){
            $rate = DB::table('paste_ratings')->where(['pid' => $paste->id, 'uid' => $myUser->id])->first();
            if(!is_null($rate)){
                $rate = $rate->rating;
            }
        }
        $originalPasteContent = Paste::where('id', 153223)->firstOrFail()->content;
        return view('custom.meowbahh', ['myUser' => $myUser, 'paste' => $paste, 'comments' => $comments, 'originalPasteContent' => $originalPasteContent, 'rate' => $rate]);
    }

    public function yuri() {
        $myUser = newGetUser();
        $paste = Paste::find(357811);
        $comments = ($myUser != null && $myUser->isDeveloper(true) ? DB::table('comments')->where('doxId', $paste->id)->orderBy('date', 'desc')->get() : DB::table('comments')->where(['doxId' => $paste->id, 'hidden' => 0])->orderBy('date', 'desc')->get());

        // if(!$myUser || (!$myUser->isAdmin() && !in_array(strtolower($myUser->username), ['cloaked', 'solarwindsadmin', 'synqronize']))){
        //     return abort(403);
        // }

        if(isset($_SERVER['HTTP_USER_AGENT'])){ // increment views
            $cx = $_SERVER[env('REAL_IP_HEADER')];
            $cx = substr($cx, strlen($cx) - 6, strlen($cx));
            $ua = md5($cx.$_SERVER['HTTP_USER_AGENT']);
            if(DB::table('views')->where(['ua' => $ua, 'id' => $paste->id])->count() < 1){
                $paste->increment('views');
                DB::table('views')->insert(['ua' => $ua, 'id' => $paste->id, 'date' => floor(microtime(true) * 1000)]);
            }
        }

        $rate = null;
        if(!is_null($myUser)){
            $rate = DB::table('paste_ratings')->where(['pid' => $paste->id, 'uid' => $myUser->id])->first();
            if(!is_null($rate)){
                $rate = $rate->rating;
            }
        }
        $originalPasteContent = Paste::where('id', 357811)->firstOrFail()->content;
        return view('custom.yuri', ['myUser' => $myUser, 'paste' => $paste, 'comments' => $comments, 'originalPasteContent' => $originalPasteContent, 'rate' => $rate]);
    }
	
	    public function jahzuko() {
        $myUser = newGetUser();
        $paste = Paste::find(460040);
        $comments = ($myUser != null && $myUser->isDeveloper(true) ? DB::table('comments')->where('doxId', $paste->id)->orderBy('date', 'desc')->get() : DB::table('comments')->where(['doxId' => $paste->id, 'hidden' => 0])->orderBy('date', 'desc')->get());

        // if(!$myUser || (!$myUser->isAdmin() && !in_array(strtolower($myUser->username), ['cloaked', 'solarwindsadmin', 'synqronize']))){
        //     return abort(403);
        // }

        $reiko_pic = User::find(279988)->profile_pic ?? 'No profile picture available.';
        $operator_pic = User::find(12113)->profile_pic ?? 'No profile picture available.';
        $convict_pic = User::find(352054)->profile_pic ?? 'No profile picture available.';

        if(isset($_SERVER['HTTP_USER_AGENT'])){ // increment views
            $cx = $_SERVER[env('REAL_IP_HEADER')];
            $cx = substr($cx, strlen($cx) - 6, strlen($cx));
            $ua = md5($cx.$_SERVER['HTTP_USER_AGENT']);
            if(DB::table('views')->where(['ua' => $ua, 'id' => $paste->id])->count() < 1){
                $paste->increment('views');
                DB::table('views')->insert(['ua' => $ua, 'id' => $paste->id, 'date' => floor(microtime(true) * 1000)]);
            }
        }

        $rate = null;
        if(!is_null($myUser)){
            $rate = DB::table('paste_ratings')->where(['pid' => $paste->id, 'uid' => $myUser->id])->first();
            if(!is_null($rate)){
                $rate = $rate->rating;
            }
        }
        $originalPasteContent = Paste::where('id', 460040)->firstOrFail()->content;
            return view('custom.jahzuko', [
                'myUser' => $myUser, 
                'paste' => $paste, 
                'comments' => $comments, 
                'originalPasteContent' => $originalPasteContent, 
                'rate' => $rate,
                'reiko_pic' => $reiko_pic,
                'operator_pic' => $operator_pic,
                'convict_pic' => $convict_pic
            ]);
        }


}



// created by Brenton, twitter.com/brenton_xxx t.me/brenton