/** * Short URL via Ajax * * @return array Data * @throws BadRequestHttpException If model not validated */ public function actionShort() { $model = new Link(); if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post()) && $model->save()) { $model->short_code = base_convert($model->id, 20, 36); $model->save(false); Yii::$app->response->format = Response::FORMAT_JSON; return ['shortUrl' => Url::to(['site/go', 'shortCode' => $model->short_code], true)]; } throw new BadRequestHttpException('Bad parameters'); }
public function generateLink() { $link = new Link(); $link->email = $this->email; $link->generateToken(); return $link->save(false) && empty($this->getErrors()) ? $link : null; }
public static function checkOrShorten($long_url) { $link = new Link(); $link->user_id = NULL; $link->long_url = $long_url; //logged in handle if (Auth::check()) { $user_id = Auth::user()->id; $link->user_id = Auth::user()->id; $query = DB::select("\r\n \tSELECT \r\n \t\tshort_url\r\n \tFROM \r\n \t\tlinks \r\n \tWHERE \r\n \t\tlong_url = '{$long_url}' \r\n \tAND \r\n \t\tuser_id = '{$user_id}'\r\n "); if ($query) { $link->short_url = $query; } } //While the short url is null, keep going until a new shortened url is found. while (is_null($link->short_url)) { $link->short_url = str_random(4); $query = DB::select("SELECT short_url FROM links WHERE short_url = '" . $link->short_url . "'"); if (!$query) { $context = stream_context_create(array('http' => array('follow_location' => false))); $html = file_get_contents($long_url, true, $context); if (strlen($html) > 0) { preg_match("/\\<title\\>(.*)\\<\\/title\\>/", $html, $title); $link->page_title = $title[1]; } } else { $link->short_url = NULL; } //Saves the built object to the database. $link->save(); DB::table('users_links')->insert(array('link_id' => $link->id, 'user_id' => $link->user_id, 'privacy' => 0)); } return $link; }
/** * Store a newly created resource in storage. * * @return Response */ public function store(LinkRequest $request) { // $input = $request->all(); $kelas = new Link($input); if ($kelas->save()) { return response()->json(array('success' => TRUE)); } }
public function actionAdd() { $model = new Link(); if ($model->load(Yii::$app->getRequest()->post()) && $model->save()) { return $this->redirect(['index']); } else { return $this->render('add', ['model' => $model]); } }
public function store($inputs) { $link = new Link(); $link->url = urldecode($inputs['url']); $link->photo_url = $this->cleanPhotoUrl($inputs['photo_url']); $link->title = $inputs['title']; $link->save(); return $link; }
public function testSaveLink() { $link = new Link(); $link->email = "*****@*****.**"; $link->generateToken(); $token = $link->token; $this->assertTrue($link->save()); $this->tester->seeInDatabase('links', ['email' => '*****@*****.**', 'token' => $token]); }
/** * Update friend link, new when don't exists. * * @param $inputs */ public function updateFriend($inputs) { if (isset($inputs['id'])) { $friend = Link::find($inputs['id']); } else { $friend = new Link(); } $friend->name = $inputs['name']; $friend->link = $inputs['link']; $friend->save(); }
/** * Creates a new Link model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate($id = null) { $model = new Link(); if ($id) { $model->block_id = (int) $id; } if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['block/index']); } else { return $this->render('create', ['model' => $model]); } }
/** * Creates a new Link model. * If creation is successful, the browser will be redirected to the 'view' page. * @param integer $menuId * @return mixed */ public function actionCreate($menuId) { $model = new Link(); if ($model->load(Yii::$app->request->post())) { $model->menu_id = $menuId; if ($model->save()) { return $this->redirect(['menu/view', 'id' => $model->menu_id]); } } else { return $this->render('create', ['model' => $model, 'menu' => Menu::findOne($menuId)]); } }
public function actionAjaxaddlinkt($id) { $count = Link::find()->where(['admin_id' => Yii::$app->user->id])->andWhere(['link_type' => 1])->andWhere(['link_id' => $id])->count(); if ($count == 0) { $link = new Link(); $link->link_type = 1; $link->link_id = $id; $link->admin_id = Yii::$app->user->id; $link->time = time(); if ($link->save()) { echo 'турнир добавлен'; } else { echo 'не сохранилось...'; } } else { echo 'уже был добавлен...'; } }
public static function createLink($long_url, $is_secret = false, $custom_ending = null, $link_ip = '127.0.0.1', $creator = false, $return_object = false) { /** * Given parameters needed to create a link, generate appropriate ending and * return formatted link. * * @param string $custom_ending * @param boolean (optional) $is_secret * @param string (optional) $custom_ending * @param string $link_ip * @param string $creator * @return string $formatted_link */ $is_already_short = LinkHelper::checkIfAlreadyShortened($long_url); if ($is_already_short) { throw new \Exception('Sorry, but your link already looks like a shortened URL.'); } if (!$is_secret && !$custom_ending && ($existing_link = LinkHelper::longLinkExists($long_url))) { // if link is not specified as secret, is non-custom, and // already exists in Polr, lookup the value and return return self::formatLink($existing_link); } if ($custom_ending) { // has custom ending $ending_conforms = LinkHelper::validateEnding($custom_ending); if (!$ending_conforms) { throw new \Exception('Sorry, but custom endings can only contain alphanumeric characters'); } $ending_in_use = LinkHelper::linkExists($custom_ending); if ($ending_in_use) { throw new \Exception('Sorry, but this URL ending is already in use.'); } $link_ending = $custom_ending; } else { // no custom ending $link_ending = LinkHelper::findSuitableEnding(); } $link = new Link(); $link->short_url = $link_ending; $link->long_url = $long_url; $link->ip = $link_ip; $link->is_custom = $custom_ending != null; if ($creator) { // if user is logged in, save user as creator $link->creator = $creator; } if ($is_secret) { $rand_bytes_num = intval(env('POLR_SECRET_BYTES')); $secret_key = CryptoHelper::generateRandomHex($rand_bytes_num); $link->secret_key = $secret_key; } else { $secret_key = false; } $link->save(); $formatted_link = self::formatLink($link_ending, $secret_key); if ($return_object) { return $link; } return $formatted_link; }