/**
  * @test
  */
 public function creates_token_for_url()
 {
     $token = $this->shortener->make('https://phpunit.de/manual/4.8/en/appendixes.assertions.html');
     $this->assertRegExp('/^[a-z0-9]{5}$/i', $token);
     $result = Url::where('token', $token)->first();
     $this->assertNotNull($result);
 }
 public function see_if_count_was_incremented()
 {
     $this->shortener->make('https://phpunit.de/manual/4.8/en/appendixes.assertions.html', 'phpunit');
     $this->resolver('phpunit');
     $result = Url::where('token', 'phpunit')->first();
     $this->assertEquals('1', $result['visits']);
 }
예제 #3
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);
     }
 }
예제 #4
0
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     //
     DB::table('urls')->delete();
     Url::create(['token' => 'Aa', 'long_url' => 'https://www.google.com/']);
     Url::create(['token' => 'aA', 'long_url' => 'https://github.com/']);
 }
 public function short_url($short_url)
 {
     $res = Url::where('short_url', $short_url)->first();
     //dd($res->url);
     if (is_null($res)) {
         return redirect('url');
     }
     return redirect($res->url);
 }
예제 #6
0
 public function redirect($short)
 {
     $redirect = App\Url::where('short', $short)->first();
     if ($redirect) {
         return redirect($redirect->url);
     } else {
         return view('404');
     }
 }
예제 #7
0
 public static function get_short_url()
 {
     $short_url = base_convert(rand(1000, 99999), 10, 36);
     $res = Url::where('short_url', $short_url)->first();
     if (!empty($res)) {
         static::get_short_url();
     } else {
         return $short_url;
     }
 }
예제 #8
0
 public function handleShortcode($shortCode)
 {
     $url = Url::where('short_url', $shortCode)->get();
     if (count($url) > 0) {
         return Redirect::to($url[0]['original']['long_url'], 302);
     } else {
         Session::flash('message', 'This Short Url Is Not Valid!');
         Session::flash('alert-class', 'alert-danger');
         return Redirect::to('urls');
     }
 }
예제 #9
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]);
 }
예제 #10
0
 public function shortenlink(Request $request)
 {
     $input = Request::get('link');
     $count = Url::where('long_url', $input)->count();
     if ($count) {
         $url = Url::where('long_url', $input)->get();
         return 'long~' . $url;
     } else {
         $url = Url::where('short_url', $input)->get();
         return 'short~' . $url;
     }
 }
예제 #11
0
function generateName()
{
    $chars = str_split('abcdefghijklmnopqrstuvwxyz' . 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' . '0123456789');
    // Find a filename.
    do {
        $name = '';
        foreach (array_rand($chars, 4) as $k) {
            $name .= $chars[$k];
        }
        $url = Url::where('name', '=', $name)->first();
    } while (!empty($url));
    return $name;
}
예제 #12
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);
 }
 /**
  * Create token for a url.
  *
  * @param $longUrl
  * @param null $customToken
  * @return string
  */
 public function make($longUrl, $customToken = null)
 {
     if ($customToken) {
         if ($this->exists('token', $customToken)) {
             throw new DuplicateTokenException();
         }
         $token = $customToken;
     } else {
         $duplicate = $this->exists('url', $longUrl);
         if ($duplicate) {
             return $duplicate['token'];
         }
         $token = $this->token();
     }
     Url::create(['token' => $token, 'url' => $longUrl]);
     return $token;
 }
예제 #14
0
 /**
  * @return @data list
  */
 public function getInfolist()
 {
     $urls = Url::all();
     $leads = Lead::all();
     return view('app.data-list', compact('urls', 'leads'));
 }
예제 #15
0
 public function redirect($url)
 {
     $find = Url::where(['short_url' => $url])->first();
     return redirect()->to("http://" . $find);
     // return redirectTo($find);
 }
 /**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function store(Request $request)
 {
     $hashids = new Hashids('smpx', 6);
     $hashed_url = $hashids->encode(125);
     $url = new Url();
     $url->long_url = $request["long_url"];
     $url->short_url = config('app.url') + '/' + $hashed_url;
     $url->is_active = true;
     $url->clicks = 0;
     $urlInstance = $url->create();
     echo $urlInstance->toJson(), "hoeee";
     return;
 }
예제 #17
0
 /**
  * Returns the country analytics
  */
 public function countryAnalytics(Request $request, $id, $rangeFrom, $rangeTo, $unit)
 {
     $url = Url::find($id);
     if (empty($url)) {
         echo 'Not Found';
         return;
     }
     Utility::getUnit($unit);
     $countryData = $url->hits()->whereBetween('created_at', array($rangeFrom, $rangeTo))->select(\DB::raw('count(*) as count, country_iso_code, country'))->groupBy('country_iso_code')->get();
     echo json_encode($countryData);
     return;
 }
예제 #18
0
 /**
  * Get all the user urls.
  *
  * @param int $userId
  * @return \Illuminate\Http\JsonResponse
  */
 public function showUserUrls($userId)
 {
     $urls = Url::where(['user_id' => $userId])->paginate(20);
     return $this->respondWithCollection($urls, new UrlTransformer(), 'urls');
 }
예제 #19
0
 /**
  * Redirect the shor url the the long/original url
  * redirect the long url to the view 
  * 
  */
 public function redirect($code)
 {
     $id = $this->_getIndex($code);
     $row = Url::where('id', '=', $id)->first();
     if ($row) {
         return Redirect::to($row->url);
     } else {
         return Redirect::to('/invalid')->with('url', 'Invalid');
     }
 }
예제 #20
0
 /**
  * Fetches the url by it's token.
  * @param $token
  * @return mixed
  */
 public function make($token)
 {
     $result = Url::where('token', $token)->firstOrFail();
     $result->incrementVisits();
     return $result['url'];
 }