Пример #1
0
 public function shorten()
 {
     $randomize = substr(str_shuffle(str_repeat("abcdefghijklmnopqrstuvwxyz0123456789", 5)), 0, 5);
     $input = Request::input('url');
     $url = App\Url::where('url', $input)->first();
     if (!$url) {
         $new_url = new App\Url();
         $new_url->url = $input;
         $new_url->short = $randomize;
         $new_url->save();
         View::share('shortend', $new_url);
     } else {
         View::share('shortend', $url);
     }
     return view('shortend');
 }
Пример #2
0
 /**
  * Add url to database.
  *
  * @return Url
  */
 public function addurl(Request $request)
 {
     $url = $request->input('long_url');
     $hash_url = hash('adler32', $url);
     $results = Url::where('hash', '=', $hash_url)->get();
     if (empty($results[0])) {
         $iurl = new Url();
         $iurl->hash = $hash_url;
         $iurl->long_url = base64_encode($url);
         $iurl->ip = $request->ip();
         $iurl->save();
         return sprintf('%s/%s', url('/'), $hash_url);
     } else {
         return sprintf('%s/%s', url('/'), $results[0]->hash);
     }
 }
Пример #3
0
 public function shorten(Request $request)
 {
     $longUrl = parse_url($request->url, PHP_URL_SCHEME) ? $request->url : 'http://' . $request->url;
     if (filter_var($longUrl, FILTER_VALIDATE_URL) === false) {
         return view('pages.url.index', ['error' => 'Unable to shorten that link. It is not a valid url.']);
     }
     $url = Url::where('long_url', '=', $longUrl)->first();
     if ($url == null) {
         $ln = 2;
         $generatedToken = null;
         while ($generatedToken == null && $ln < 10) {
             $tempToken = $this->generateToken($ln);
             $url = Url::where('token', '=', $tempToken)->first();
             if ($url == null) {
                 $generatedToken = $tempToken;
             } else {
                 $ln++;
             }
         }
         if ($generatedToken != null) {
             $url = new Url();
             $url->token = $generatedToken;
             $url->long_url = $longUrl;
             $url->save();
             $success['token'] = $url->token;
             $success['long_url'] = $url->long_url;
             $success['short_url'] = preg_replace('#(http|https|ftp)://(www\\.?)?#i', '', $request->root()) . '/' . $url->token;
             $success['filtered_url'] = preg_replace('#(http|https|ftp)://(www\\.?)?#i', '', $url->long_url);
         } else {
             $error = 'Something went wrong and it\'s my fault. Please try again.';
             return view('pages.url.index', ['error' => $error]);
         }
     } else {
         $success['token'] = $url->token;
         $success['long_url'] = $url->long_url;
         $success['short_url'] = preg_replace('#(http|https|ftp)://(www\\.?)?#i', '', $request->root()) . '/' . $url->token;
         $success['filtered_url'] = preg_replace('#(http|https|ftp)://(www\\.?)?#i', '', $url->long_url);
     }
     //return redirect()->route('pages.url.index');
     return view('pages.url.index', ['success' => $success]);
 }
Пример #4
0
 /**
  * Show the form for creating a new resource.
  *
  * @return Response
  */
 public function create(Request $request)
 {
     $v = \Validator::make($request->all(), ['url' => 'required|url']);
     if ($v->fails()) {
         return redirect()->back()->withErrors($v->errors());
     } else {
         $new_url = new Url();
         $new_url->long_url = $request->url;
         while (true) {
             $generated = \Illuminate\Support\Str::random(4);
             $url_validation = \Validator::make([], [$generated => 'unique:urls']);
             if (!$url_validation->fails()) {
                 $new_url->short_url = \Illuminate\Support\Str::random(4);
                 break;
             }
         }
         $new_url->save();
         $stat = Stat::where('links_created', '>=', 0)->first();
         $stat->links_created = $stat->links_created + 1;
         $stat->save();
     }
     return redirect('/')->with('url', $new_url->short_url);
 }