Beispiel #1
0
@extends('layouts.master')
@section('content')
	<div id="main" role="main">

		<?php 
$user_media_count = UserMedia::where('user_id', '=', Auth::user()->id)->where('cat', '!=', '1')->count();
if (Auth::user()->category_id == 1 && $user_media_count != 0) {
    ?>
 <p class='message' id='NoticeMessage'>
						Some of your files will be deleted in 2 days. Upgrade to premium to have unlimited torrents.
					</p>
				<?php 
}
?>
		<ul id="tiles">
		@foreach($media as $torrent)
			@if($torrent->state == "failed" || $torrent->state == "fail_free")
				<li class="media-tile failed" data-id="{{$torrent->uni_id}}" style="position: absolute; top: 0px; left: 61px; display: list-item;">
					<a href="/torrent/{{$torrent->uni_id}}">
					<img src="include/img/filetype/failed.png" height="130" width="130"></a>
					<a href="/torrent/{{$torrent->uni_id}}"><p>{{$torrent->title}}</p></a>
				</li>
			@elseif($torrent->state == "done")
					<li class="media-tile completed" data-id="{{$torrent->uni_id}}" style="position: absolute; top: 0px; left: 61px; display: list-item;">
					<a href="/torrent/{{$torrent->uni_id}}">
					@if($torrent->max_file_id != 0)
					<img src="/snaps/thumbs/{{$torrent->id}}/{{$torrent->max_file_id}}/1.jpg" height="130" width="130"></a>
					@else
					<img src="include/img/filetype/completed.png" height="130" width="130"></a>
					@endif
					<a href="/torrent/{{$torrent->uni_id}}"><p>{{$torrent->title}}</p></a>
Beispiel #2
0
Route::post('test3', function () {
    $media = Media::where('title', 'LIKE', '%.meta%')->where('state', '=', 'done')->get();
    foreach ($media as $m) {
        $mediafiles = MediaLike::where('media_id', '=', $m->id)->get();
        foreach ($mediafiles as $mf) {
            $mf->delete();
        }
        $userm = UserMedia::where('media_id', '=', $m->id)->get();
        foreach ($userm as $mm) {
            $mm->delete();
        }
        $m->delete();
    }
});
Route::get('test4', function () {
    $media_users = UserMedia::where('media_id', '=', '11')->get();
    foreach ($media_users as $media_user) {
        $user = User::where('id', '=', $media_user->user_id)->first();
        $user_cat = Category::where('id', '=', $user->category_id)->first();
        echo $user_cat->max_add;
    }
});
Route::post('test9', function () {
    $update = Media::where('hash', '=', 'A88EF509425F827998A4FD84AF2D46E020F6510B')->first();
    $url = 'http://77.247.178.109/rt/plugins/httprpc/action.php';
    $myvars = 'mode=list-get&hash=' . $update->hash;
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $myvars);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
Beispiel #3
0
 public function usersMedia()
 {
     return UserMedia::where('media_id', '=', $this->id)->get();
 }
Beispiel #4
0
 /**
  * Execute the console command.
  *
  * @return void
  */
 public function fire()
 {
     $time_start = microtime(true);
     $media = Media::where('state', '!=', 'done')->where('state', '!=', 'failed')->where('state', '!=', 'max_pause')->where('state', '!=', 'process')->where('state', '!=', 'fail_free')->where('state', '!=', 'delete')->get();
     foreach ($media as $update) {
         try {
             if ($update->state === "put_pause") {
                 echo "start";
                 $url = 'http://s01.okaydrive.com/rt/plugins/httprpc/action.php';
                 $myvars = 'mode=start&hash=' . $update->hash;
                 $ch = curl_init($url);
                 $username = '******';
                 $password = '******';
                 curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
                 curl_setopt($ch, CURLOPT_POST, 1);
                 curl_setopt($ch, CURLOPT_POSTFIELDS, $myvars);
                 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                 curl_setopt($ch, CURLOPT_HEADER, 0);
                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                 $response = curl_exec($ch);
                 $torrent_start = json_decode($response, true);
                 $update->state = "put_start";
                 $update->save();
             }
             $now = new DateTime();
             $now_stamp = $now->format('Y-m-d H:i:s');
             $diff = strtotime($now_stamp) - strtotime($update->featured_at);
             $diff_in_minss = $diff / 60;
             if ($diff_in_minss > 60 && $update->cat == 1 && $update->featured_at != "0000-00-00 00:00:00") {
                 $media_users = UserMedia::where('media_id', '=', $update->id)->get();
                 $del = true;
                 foreach ($media_users as $media_user) {
                     $user = User::where('id', '=', $media_user->user_id)->first();
                     if ($user->category_id == 1) {
                     } else {
                         $del = false;
                         $update->cat = $user->category_id;
                         $update->save();
                     }
                 }
                 if ($del) {
                     $url = 'http://s01.okaydrive.com/rt/plugins/httprpc/action.php';
                     $input_xml = '<methodCall><methodName>system.multicall</methodName><params><param><value><array><data><value><struct><member><name>methodName</name><value><string>d.set_custom5</string></value></member><member><name>params</name><value><array><data><value><string>' . $update->hash . '</string></value><value><string>1</string></value></data></array></value></member></struct></value><value><struct><member><name>methodName</name><value><string>d.delete_tied</string></value></member><member><name>params</name><value><array><data><value><string>' . $update->hash . '</string></value></data></array></value></member></struct></value><value><struct><member><name>methodName</name><value><string>d.erase</string></value></member><member><name>params</name><value><array><data><value><string>' . $update->hash . '</string></value></data></array></value></member></struct></value></data></array></value></param></params></methodCall>';
                     $ch = curl_init($url);
                     $username = '******';
                     $password = '******';
                     curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
                     curl_setopt($ch, CURLOPT_POST, 1);
                     curl_setopt($ch, CURLOPT_POSTFIELDS, $input_xml);
                     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                     curl_setopt($ch, CURLOPT_HEADER, 0);
                     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
                     curl_setopt($ch, CURLOPT_TIMEOUT, 4);
                     //timeout in seconds
                     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                     $response = curl_exec($ch);
                     $report = new UserFlag();
                     $report->type = 'check';
                     $report->media_id = $update->id;
                     $report->res = 'took more than an hour' . $response;
                     $report->save();
                     $update->state = 'fail_free';
                     $update->save();
                     $user_report = UserFlag::where('media_id', '=', $update->id)->where('type', '=', 'error')->first();
                     if (!isset($user_report->id)) {
                         $report = new UserFlag();
                         $report->type = 'error';
                         $report->media_id = $update->id;
                         $report->res = $response;
                         $report->save();
                     }
                 }
             } else {
                 $url = 'http://s01.okaydrive.com/rt/plugins/httprpc/action.php';
                 $myvars = 'mode=list-get&hash=' . $update->hash;
                 $ch = curl_init($url);
                 $username = '******';
                 $password = '******';
                 curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
                 curl_setopt($ch, CURLOPT_POST, 1);
                 curl_setopt($ch, CURLOPT_POSTFIELDS, $myvars);
                 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                 curl_setopt($ch, CURLOPT_HEADER, 0);
                 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
                 curl_setopt($ch, CURLOPT_TIMEOUT, 4);
                 //timeout in seconds
                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                 $response = curl_exec($ch);
                 $tor = json_decode($response, true);
                 if (count($tor != 0) && $tor != false) {
                     echo $tor[0];
                     echo "files begin";
                     if ($update->files()->count() == 0 && $update->file_try < 3 && $update->state == "downloading" || $update->files()->count() == 0 && $update->file_try == 3 && $update->state == "downloading" && $this->percent($tor[9], $tor[6]) > 0) {
                         echo "files";
                         if ($tor[3] != 0) {
                             if (strpos($update->title, '.meta') !== false) {
                                 echo "in";
                             } else {
                                 if (!empty($tor[26])) {
                                     $url = 'http://s01.okaydrive.com/rt/plugins/httprpc/action.php';
                                     $myvars = 'mode=fls&hash=' . $tor[0];
                                     $ch = curl_init($url);
                                     $username = '******';
                                     $password = '******';
                                     curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
                                     curl_setopt($ch, CURLOPT_POST, 1);
                                     curl_setopt($ch, CURLOPT_POSTFIELDS, $myvars);
                                     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                                     curl_setopt($ch, CURLOPT_HEADER, 0);
                                     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
                                     curl_setopt($ch, CURLOPT_TIMEOUT, 5);
                                     //timeout in seconds
                                     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                                     $response = curl_exec($ch);
                                     $torrent_files = json_decode($response, true);
                                     $files = $torrent_files;
                                     if (!empty($files)) {
                                         $ignore_first_folder = true;
                                         $id = 1;
                                         $paths = array();
                                         foreach ($files as $file) {
                                             if ($file[0] != $tor[0] . '.meta' && isset($file[0]) && !empty($file[0])) {
                                                 if ($tor[34] != 0) {
                                                     $fd = parse_url(basename(str_replace("#", "", $tor[26])) . '/' . str_replace("#", "", $file[0]));
                                                 } else {
                                                     $fd = parse_url(str_replace("#", "", $file[0]));
                                                 }
                                                 if (isset($fd['path'])) {
                                                     $path_parts = pathinfo($fd['path']);
                                                 } else {
                                                     $path_parts = pathinfo($file["name"]);
                                                 }
                                                 $dirs = explode("/", $path_parts['dirname']);
                                                 for ($i = 0; $i <= count($dirs); $i++) {
                                                     if (isset($dirs[$i]) && $dirs[$i] != '.') {
                                                         $full_path = $this->fullpath($dirs, $i);
                                                         if (array_key_exists($full_path, $paths)) {
                                                         } else {
                                                             $paths[$full_path]["id"] = $id;
                                                             $paths[$full_path]["name"] = $dirs[$i];
                                                             $prev_path = $this->fullpath($dirs, $i - 1);
                                                             if (!isset($paths[$prev_path]["id"])) {
                                                                 $pv_p = 0;
                                                             } else {
                                                                 $pv_p = $paths[$prev_path]["id"];
                                                             }
                                                             $new_folder = new MediaFlag();
                                                             $new_folder->name = $dirs[$i];
                                                             $new_folder->folder_id = $id;
                                                             $new_folder->in = $pv_p;
                                                             $new_folder->media_id = $update->id;
                                                             $new_folder->save();
                                                             $id++;
                                                         }
                                                     } elseif (isset($dirs[$i]) && $dirs[$i] == '.') {
                                                         //echo $path_parts["basename"].' 0';
                                                         $new_file = new MediaLike();
                                                         if ($tor[34] != 0) {
                                                             $new_file->path = basename(str_replace("#", "", $tor[26])) . '/' . str_replace("#", "", $file[0]);
                                                         } else {
                                                             $new_file->path = str_replace("#", "", $file[0]);
                                                         }
                                                         $new_file->name = $path_parts["basename"];
                                                         $new_file->type = $this->getExt($new_file->path);
                                                         $new_file->in = 0;
                                                         $new_file->size = $file[3];
                                                         $new_file->media_id = $update->id;
                                                         //$like->user_id = Auth::user()->id;
                                                         $new_file->save();
                                                         $ignore_first_folder = false;
                                                     } else {
                                                         if (isset($dirs[$i - 1]) && $dirs[$i - 1] != '.') {
                                                             $full_path = $this->fullpath($dirs, $i - 1);
                                                             //echo $path_parts["basename"].' '.$paths[$full_path]["id"];
                                                             $new_file = new MediaLike();
                                                             if ($tor[34] != 0) {
                                                                 $new_file->path = basename(str_replace("#", "", $tor[26])) . '/' . str_replace("#", "", $file[0]);
                                                             } else {
                                                                 $new_file->path = $file[0];
                                                             }
                                                             $new_file->type = $this->getExt($new_file->path);
                                                             $new_file->name = $path_parts["basename"];
                                                             $new_file->in = $paths[$full_path]["id"];
                                                             $new_file->size = $file[3];
                                                             $new_file->media_id = $update->id;
                                                             //$like->user_id = Auth::user()->id;
                                                             $new_file->save();
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                         $update["ignore_first"] = $ignore_first_folder;
                                         $update->save();
                                     }
                                     $update->file_try = $update->file_try + 1;
                                     $update->save();
                                 }
                             }
                         }
                     }
                     $info = $tor;
                     echo "max check begin";
                     $max_pause = false;
                     if (strpos($update->title, '.meta') !== false) {
                         echo "in";
                     } else {
                         if ($tor[5] != $update->hash . '.meta' && $info[29] != 0 && $info[6] != null && $info[6] != 0 && !empty($info[6]) && $update->max_check == 0) {
                             $max_pause = true;
                             $media_users = UserMedia::where('media_id', '=', $update->id)->get();
                             echo $info[6] . 'size';
                             foreach ($media_users as $media_user) {
                                 $user = User::where('id', '=', $media_user->user_id)->first();
                                 $user_cat = Category::where('id', '=', $user->category_id)->first();
                                 if ($info[6] > $user_cat->max_add) {
                                     $media_user->max_error = true;
                                     $media_user->save();
                                 } else {
                                     $max_pause = false;
                                 }
                             }
                             if ($max_pause) {
                                 $update->state = "max_pause";
                                 $update->save();
                                 $info["status"] = 7;
                                 $url = 'http://s01.okaydrive.com/rt/plugins/httprpc/action.php';
                                 $input_xml = '<methodCall><methodName>system.multicall</methodName><params><param><value><array><data><value><struct><member><name>methodName</name><value><string>d.set_custom5</string></value></member><member><name>params</name><value><array><data><value><string>' . $update->hash . '</string></value><value><string>1</string></value></data></array></value></member></struct></value><value><struct><member><name>methodName</name><value><string>d.delete_tied</string></value></member><member><name>params</name><value><array><data><value><string>' . $update->hash . '</string></value></data></array></value></member></struct></value><value><struct><member><name>methodName</name><value><string>d.erase</string></value></member><member><name>params</name><value><array><data><value><string>' . $update->hash . '</string></value></data></array></value></member></struct></value></data></array></value></param></params></methodCall>';
                                 $ch = curl_init($url);
                                 $username = '******';
                                 $password = '******';
                                 curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
                                 curl_setopt($ch, CURLOPT_POST, 1);
                                 curl_setopt($ch, CURLOPT_POSTFIELDS, $input_xml);
                                 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                                 curl_setopt($ch, CURLOPT_HEADER, 0);
                                 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
                                 curl_setopt($ch, CURLOPT_TIMEOUT, 4);
                                 //timeout in seconds
                                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                                 $response = curl_exec($ch);
                                 $report = new UserFlag();
                                 $report->type = 'check';
                                 $report->media_id = $update->id;
                                 $report->res = 'max pause' . $response;
                                 $report->save();
                             }
                             $update->max_check = 1;
                             $update->save();
                         }
                     }
                     echo "max check end";
                     if ($max_pause) {
                     } else {
                         $state = 'stop';
                         if ($info[1] != 0) {
                             $state = "downloading";
                             if ($info[4] == 0 || $info[29] == 0) {
                                 $state = "paused";
                             }
                         } elseif ($info[24] != 0) {
                             $state = "hashing";
                         } elseif ($info[2] != 0) {
                             $state = "checking";
                         }
                         if ($info[9] >= $info[6]) {
                             $state = "done";
                         }
                         if ($update->title != $info[5]) {
                             if ($info[5] == '') {
                                 $info[5] = 'no name field';
                             }
                             $update->title = $info[5];
                         }
                         if ($update->state == "put_start" && $state == 'stop') {
                             $url = 'http://s01.okaydrive.com/rt/plugins/httprpc/action.php';
                             $myvars = 'mode=start&hash=' . $tor[0];
                             $ch = curl_init($url);
                             $username = '******';
                             $password = '******';
                             curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
                             curl_setopt($ch, CURLOPT_POST, 1);
                             curl_setopt($ch, CURLOPT_POSTFIELDS, $myvars);
                             curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                             curl_setopt($ch, CURLOPT_HEADER, 0);
                             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                             $response = curl_exec($ch);
                             $torrent_start = json_decode($response, true);
                             $update->state = "downloading";
                             $update->status = 4;
                             $update->save();
                         } elseif ($update->state == "downloading" && $state == 'stop') {
                             $url = 'http://s01.okaydrive.com/rt/plugins/httprpc/action.php';
                             $myvars = 'mode=start&hash=' . $tor[0];
                             $ch = curl_init($url);
                             $username = '******';
                             $password = '******';
                             curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
                             curl_setopt($ch, CURLOPT_POST, 1);
                             curl_setopt($ch, CURLOPT_POSTFIELDS, $myvars);
                             curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                             curl_setopt($ch, CURLOPT_HEADER, 0);
                             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                             $response = curl_exec($ch);
                             $torrent_start = json_decode($response, true);
                         } elseif ($update->state == "downloading" && $state == 'paused') {
                             $url = 'http://s01.okaydrive.com/rt/plugins/httprpc/action.php';
                             $myvars = 'mode=start&hash=' . $tor[0];
                             $ch = curl_init($url);
                             $username = '******';
                             $password = '******';
                             curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
                             curl_setopt($ch, CURLOPT_POST, 1);
                             curl_setopt($ch, CURLOPT_POSTFIELDS, $myvars);
                             curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                             curl_setopt($ch, CURLOPT_HEADER, 0);
                             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                             $response = curl_exec($ch);
                             $torrent_start = json_decode($response, true);
                         }
                         if ($info[29] == 0 && $info[1] == 1 && $info[2] == 1) {
                             $state = 'hashing';
                         }
                         switch ($state) {
                             case "hashing":
                                 $update->state = "hashing";
                                 $update->save();
                                 break;
                             case "checking":
                                 $update->state = "check";
                                 $update->status = 2;
                                 $update->save();
                                 break;
                             case "paused":
                             case "downloading":
                                 $update->state = "downloading";
                                 $update->status = 4;
                                 $update->save();
                                 break;
                             case "done":
                                 if ($tor[5] == $update->hash . '.meta' || $update->title == $update->hash . '.meta') {
                                     $report = new UserFlag();
                                     $report->media_id = $update->id;
                                     $report->save();
                                 } else {
                                     $loc = '/home/mfs/Downloads/transmission/completed/' . $update->id;
                                     $url = 'http://s01.okaydrive.com/rt/plugins/datadir/action.php';
                                     $myvars = 'move_fastresume=0&move_datafiles=1&move_addpath=1&hash=' . $tor[0] . '&datadir=' . $loc;
                                     $ch = curl_init($url);
                                     $username = '******';
                                     $password = '******';
                                     curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
                                     curl_setopt($ch, CURLOPT_POST, 1);
                                     curl_setopt($ch, CURLOPT_POSTFIELDS, $myvars);
                                     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                                     curl_setopt($ch, CURLOPT_HEADER, 0);
                                     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                                     $response = curl_exec($ch);
                                     $torrent_move = json_decode($response, true);
                                     $process = shell_exec('nohup php /opt/nginx/html/artisan sys:process --hash=' . $update->hash . ' > /dev/null 2>&1 & echo $!');
                                     $update->state = "process";
                                     $update->status = 5;
                                     $update->save();
                                     break;
                                 }
                         }
                         if ($update->state == "put_start" || $update->state == "put_pause" || strpos($update->title, '.meta') !== false || $update->featured_at != "0000-00-00 00:00:00") {
                             echo "in";
                         } else {
                             if ($update->state == "downloading") {
                                 $now = new DateTime();
                                 $now_stamp = $now->format('Y-m-d H:i:s');
                                 $update->featured_at = $now_stamp;
                                 $update->save();
                             }
                         }
                         if ($update->state == "hashing") {
                             $update->perc = $this->percent($info[25], $info[8]);
                         } else {
                             $update->perc = $this->percent($info[9], $info[6]);
                         }
                         $update->upsd = $info[13];
                         if ($info[13] == 0) {
                             $update->eta = $info[6] - $info[9];
                         } else {
                             $update->eta = ($info[6] - $info[9]) / $info[13];
                         }
                         $update->peersc = $info[18];
                         $update->peersa = $info[19];
                         $update->size = $info[6];
                         if ($update->title != $info[5]) {
                             if ($info[5] == '') {
                                 $info[5] = 'no name field';
                             }
                             $update->title = $info[5];
                         }
                         $update->save();
                     }
                     echo uniqid();
                 }
             }
         } catch (Exception $e) {
             echo 'Message: ' . $e->getMessage();
             $report = new UserFlag();
             $report->type = 'check-all-error';
             $report->media_id = $update->id;
             $report->res = $e->getMessage();
             $report->save();
         }
     }
     echo "end";
 }
 public function uploadFile()
 {
     $requestsPerHour = 60;
     $key = sprintf('api:%s', Request::getClientIp());
     $get_data = DB::table('limit')->where('ip', $key)->first();
     if (isset($get_data->ip)) {
         $count = $get_data->count;
         $count++;
         DB::table('limit')->where('ip', $key)->update(array('count' => $count));
     } else {
         DB::table('limit')->insert(array('user_id' => Auth::user()->id, 'ip' => $key, 'count' => 0));
     }
     $count = UserMedia::where('user_id', '=', Auth::user()->id)->where('is_deleted', '=', '0')->where('cat', '=', '1')->count();
     //if($count >= 2 && Auth::user()->category_id == 1){
     //	$response = Response::json(array('result'=>false, 'location' => false,'error'=>'Free accounts are only allowed 2 torrents per account.' ));
     //	$response->header('Content-Type', 'application/json');
     //	return $response;
     //}
     $user_media = UserMedia::where('user_id', '=', Auth::user()->id)->where('is_deleted', '=', '0')->get();
     if (count($user_media) != 0) {
         $uma = array();
         foreach ($user_media as $um) {
             array_push($uma, $um->media_id);
         }
         $media_count = Media::whereIn('id', $uma)->where('state', '!=', 'done')->where('state', '!=', 'max_pause')->where('state', '!=', 'failed')->where('state', '!=', 'process')->where('state', '!=', 'stop')->count();
         if ($media_count >= 1 && Auth::user()->category_id == 1) {
             $response = Response::json(array('result' => false, 'location' => false, 'error' => 'Free accounts are only allowed 1 active torrent per account.'));
             $response->header('Content-Type', 'application/json');
             return $response;
         }
         if ($media_count >= 10) {
             $response = Response::json(array('result' => false, 'location' => false, 'error' => 'Your account is only allowed 10 active torrents.'));
             $response->header('Content-Type', 'application/json');
             return $response;
         }
     }
     if (Auth::user()->category_id == 1) {
         $status = true;
         $useage = Auth::user()->used_bytes;
         if ($useage > Auth::user()->avl_bytes) {
             $status = false;
         }
         if (Auth::user()->avl_bytes - $useage < 104857600) {
             $status = false;
         }
         date_default_timezone_set('Pacific/Auckland');
         $ip_date = date("Y-m-d");
         $ip_bytes = DataIp::where('ip', '=', $_SERVER['REMOTE_ADDR'])->where('date', '=', $ip_date)->sum('bytes');
         if (1073741824 - $ip_bytes < 104857600) {
             $status = false;
         }
         if (!$status) {
             $response = Response::json(array('result' => false, 'location' => false, 'error' => 'Low bandwidth left on your account. Upgrade your account to premium.'));
             $response->header('Content-Type', 'application/json');
             return $response;
         }
     }
     require '/opt/nginx/html/vendor/upload.php';
     $upload_directory = '/opt/nginx/html/public/cache/tmp';
     $allowed_extensions = array('torrent');
     $max_size = 1048576;
     $uploader = new FileUpload('file');
     $ext = $uploader->getExtension();
     if (empty($ext)) {
         $response = Response::json(array('result' => false, 'location' => false, 'error' => 'Invalid file type.'));
         $response->header('Content-Type', 'application/json');
         return $response;
     }
     $filename = uniqid(uniqid(), true) . '.' . $ext;
     $uploader->newFileName = $filename;
     $uploader->sizeLimit = $max_size;
     $result = $uploader->handleUpload($upload_directory, $allowed_extensions);
     $errors = $uploader->getErrorMsg();
     if (!empty($errors)) {
         $response = Response::json(array('result' => false, 'location' => false, 'error' => $uploader->getErrorMsg()));
         $response->header('Content-Type', 'application/json');
         return $response;
     }
     $file = $uploader->getSavedFile();
     $url = 'http://s01.okaydrive.com/rt/php/addtorrent2.php';
     $myvars = 'torrents_start_stopped=1&url=https://okaydrive.com/cache/tmp/' . $filename;
     $ch = curl_init($url);
     $username = '******';
     $password = '******';
     curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
     curl_setopt($ch, CURLOPT_POST, 1);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $myvars);
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
     curl_setopt($ch, CURLOPT_HEADER, 0);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     $response = curl_exec($ch);
     $json = json_decode($response, true);
     $uni_id = '';
     if ($json["result"] == "success") {
         $hash = $json["hash"];
         $mediaexist = Media::where('hash', '=', $hash)->first();
         if (isset($mediaexist->id)) {
             if ($mediaexist->state == 'done' || $mediaexist->state == 'failed') {
                 $url = 'http://s01.okaydrive.com/rt/plugins/httprpc/action.php';
                 $myvars = 'mode=remove&hash=' . $mediaexist->hash;
                 $ch = curl_init($url);
                 $username = '******';
                 $password = '******';
                 curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
                 curl_setopt($ch, CURLOPT_POST, 1);
                 curl_setopt($ch, CURLOPT_POSTFIELDS, $myvars);
                 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                 curl_setopt($ch, CURLOPT_HEADER, 0);
                 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
                 curl_setopt($ch, CURLOPT_TIMEOUT, 4);
                 //timeout in seconds
                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                 $response = curl_exec($ch);
                 $removed = json_decode($response, true);
             }
             $userHasMedia = UserMedia::where('user_id', '=', Auth::user()->id)->where('media_id', '=', $mediaexist->id)->first();
             if (!isset($userHasMedia->id)) {
                 if ($mediaexist->size > Auth::user()->category()->max_add) {
                     $response = Response::json(array('result' => false, 'location' => false, 'error' => 'Max torrent size allowed for Free accounts reached.'));
                     $response->header('Content-Type', 'application/json');
                     return $response;
                 }
                 if ($mediaexist->state == 'max_pause') {
                     $mediaexist->state = 'put_pause';
                     $mediaexist->save();
                 }
                 if ($mediaexist->state == 'fail_free' && $mediaexist->user_id != Auth::user()->user_id) {
                     $mediaexist->state = 'put_pause';
                     $mediaexist->save();
                 }
                 if ($mediaexist->state == 'delete') {
                     $mediaexist->state = 'put_pause';
                     $mediaexist->save();
                 }
                 $newMedia = new UserMedia();
                 $newMedia->user_id = Auth::user()->id;
                 $newMedia->cat = Auth::user()->category_id;
                 $newMedia->media_id = $mediaexist->id;
                 $newMedia->uni_id = uniqid(rand(), true);
                 $newMedia->save();
                 $uni_id = $newMedia->uni_id;
                 $res = 'cache';
             } else {
                 if ($mediaexist->state == 'max_pause') {
                     $mediaexist->state = 'put_pause';
                     $mediaexist->save();
                 }
                 if ($mediaexist->state == 'fail_free' && $mediaexist->user_id != Auth::user()->user_id) {
                     $mediaexist->state = 'put_pause';
                     $mediaexist->save();
                 }
                 if ($mediaexist->state == 'delete') {
                     $mediaexist->state = 'put_pause';
                     $mediaexist->save();
                 }
                 if ($userHasMedia->is_deleted) {
                     $userHasMedia->is_deleted = false;
                     $userHasMedia->save();
                 }
                 $uni_id = $userHasMedia->uni_id;
                 $res = 'has';
             }
         } else {
             sleep(4);
             $url = 'http://s01.okaydrive.com/rt/plugins/httprpc/action.php';
             $myvars = 'mode=info&hash=' . $hash;
             $ch = curl_init($url);
             $username = '******';
             $password = '******';
             curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
             curl_setopt($ch, CURLOPT_POST, 1);
             curl_setopt($ch, CURLOPT_POSTFIELDS, $myvars);
             curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
             curl_setopt($ch, CURLOPT_HEADER, 0);
             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
             $response = curl_exec($ch);
             $torrent_info = json_decode($response, true);
             if (empty($torrent_info[0]) || !isset($torrent_info[0])) {
                 $torrent_info[0] = $hash;
             }
             $inputTorrent["hash"] = $hash;
             $inputTorrent["title"] = $torrent_info[0];
             $inputTorrent["state"] = 'put_pause';
             $inputTorrent["user_id"] = Auth::user()->id;
             $inputTorrent["source"] = 'https://okaydrive.com/cache/tmp/' . $filename;
             $inputTorrent["cat"] = Auth::user()->category_id;
             $new_media = $this->media->create($inputTorrent);
             $newMedia = new UserMedia();
             $newMedia->user_id = Auth::user()->id;
             $newMedia->cat = Auth::user()->category_id;
             $newMedia->media_id = $new_media->id;
             $newMedia->uni_id = uniqid(rand(), true);
             $newMedia->save();
             $uni_id = $newMedia->uni_id;
             sleep(1);
             $url = 'http://s01.okaydrive.com/rt/plugins/httprpc/action.php';
             $myvars = 'mode=fls&hash=' . $hash;
             $ch = curl_init($url);
             $username = '******';
             $password = '******';
             curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
             curl_setopt($ch, CURLOPT_POST, 1);
             curl_setopt($ch, CURLOPT_POSTFIELDS, $myvars);
             curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
             curl_setopt($ch, CURLOPT_HEADER, 0);
             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
             $response = curl_exec($ch);
             $torrent_files = json_decode($response, true);
             $files = $torrent_files;
             $totalSize = $torrent_info[1];
             if (!empty($totalSize)) {
                 if ($totalSize > Auth::user()->category()->max_add) {
                     $newMedia->delete();
                     $new_media->delete();
                     $response = Response::json(array('result' => false, 'location' => false, 'error' => 'Max torrent size allowed for Free accounts reached.'));
                     $response->header('Content-Type', 'application/json');
                     return $response;
                 } else {
                     $new_media["size"] = $totalSize;
                 }
             }
             if (!empty($files) && !empty($torrent_info[2])) {
                 $ignore_first_folder = true;
                 $id = 1;
                 $paths = array();
                 foreach ($files as $file) {
                     if ($file[0] != $hash . '.meta') {
                         if ($torrent_info[3] != 0) {
                             $fd = parse_url(basename($torrent_info[2]) . '/' . $file[0]);
                         } else {
                             $fd = parse_url($file[0]);
                         }
                         $path_parts = pathinfo($fd['path']);
                         $dirs = explode("/", $path_parts['dirname']);
                         for ($i = 0; $i <= count($dirs); $i++) {
                             if (isset($dirs[$i]) && $dirs[$i] != '.') {
                                 $full_path = $this->fullpath($dirs, $i);
                                 if (array_key_exists($full_path, $paths)) {
                                 } else {
                                     $paths[$full_path]["id"] = $id;
                                     $paths[$full_path]["name"] = $dirs[$i];
                                     $prev_path = $this->fullpath($dirs, $i - 1);
                                     if (!isset($paths[$prev_path]["id"])) {
                                         $pv_p = 0;
                                     } else {
                                         $pv_p = $paths[$prev_path]["id"];
                                     }
                                     $new_folder = new MediaFlag();
                                     $new_folder->name = $dirs[$i];
                                     $new_folder->folder_id = $id;
                                     $new_folder->in = $pv_p;
                                     $new_folder->media_id = $new_media->id;
                                     $new_folder->save();
                                     $id++;
                                 }
                             } elseif (isset($dirs[$i]) && $dirs[$i] == '.') {
                                 //echo $path_parts["basename"].' 0';
                                 $new_file = new MediaLike();
                                 if ($torrent_info[3] != 0) {
                                     $new_file->path = basename($torrent_info[2]) . '/' . $file[0];
                                 } else {
                                     $new_file->path = $file[0];
                                 }
                                 $new_file->type = $this->getExt($new_file->path);
                                 $new_file->name = $path_parts["basename"];
                                 $new_file->in = 0;
                                 $new_file->size = $file[3];
                                 $new_file->media_id = $new_media->id;
                                 //$like->user_id = Auth::user()->id;
                                 $new_file->save();
                                 $ignore_first_folder = false;
                             } else {
                                 if (isset($dirs[$i - 1]) && $dirs[$i - 1] != '.') {
                                     $full_path = $this->fullpath($dirs, $i - 1);
                                     //echo $path_parts["basename"].' '.$paths[$full_path]["id"];
                                     $new_file = new MediaLike();
                                     if ($torrent_info[3] != 0) {
                                         $new_file->path = basename($torrent_info[2]) . '/' . $file[0];
                                     } else {
                                         $new_file->path = $file[0];
                                     }
                                     $new_file->type = $this->getExt($new_file->path);
                                     $new_file->name = $path_parts["basename"];
                                     $new_file->in = $paths[$full_path]["id"];
                                     $new_file->size = $file[3];
                                     $new_file->media_id = $new_media->id;
                                     //$like->user_id = Auth::user()->id;
                                     $new_file->save();
                                 }
                             }
                         }
                     }
                 }
                 $new_media["ignore_first"] = $ignore_first_folder;
             }
             $new_media->save();
             $res = 'added';
         }
     } else {
         $error = "Could not add the torrent, please check your input.";
     }
     //$new_media = $this->media->create($input);
     if (isset($error)) {
         $response = Response::json(array('result' => false, 'location' => false, 'error' => $error));
         $response->header('Content-Type', 'application/json');
         return $response;
     } else {
         $response = Response::json(array('result' => true, 'location' => '/torrent/' . $uni_id, 'torrent' => $res));
         $response->header('Content-Type', 'application/json');
         return $response;
     }
 }