Ejemplo n.º 1
0
 /**
  * Show the form for editing the specified resource.
  *
  * @param  int  $id
  * @return Response
  */
 public function edit($id)
 {
     $media = $this->media->find($id);
     if (is_null($media)) {
         return Redirect::route('media.index');
     }
     return View::make('media.edit', compact('media'));
 }
Ejemplo n.º 2
0
 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;
     }
 }