Ejemplo n.º 1
0
 public function redirect()
 {
     $token = $this->getParam('token');
     $url_id = Hasher::decode($token);
     $query = $this->getService('DB')->prepare('SELECT * from urls WHERE id = :url_id');
     $query->bindParam(':url_id', $url_id);
     $query->execute();
     $result = $query->fetch(\PDO::FETCH_ASSOC);
     return $result ? $this->getResponse()->withRedirect($result['url']) : $this->getResponse()->withStatus(404)->write('Not Found');
 }
Ejemplo n.º 2
0
 public function abridge()
 {
     $url = $this->getParam('url');
     if (empty($url)) {
         return $this->output(false, 'No URL is provided');
     }
     if (!filter_var($url, FILTER_VALIDATE_URL)) {
         return $this->output(false, 'The given URL is malformed');
     }
     if (strlen($url) > self::MAX_URL_LEN) {
         return $this->output(false, 'The given URL exceeds the maximum length');
     }
     $query = $this->getService('DB')->prepare('INSERT INTO urls (url) VALUES (:url) ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id)');
     $query->bindParam(':url', $url);
     $query->execute();
     $url_id = $this->getService('DB')->lastInsertId();
     if (!$url_id) {
         return $this->output(false, 'Unable to generate a token for the given URL');
     }
     $token = Hasher::encode($url_id);
     return $this->output(true, 'OK', $token);
 }
Ejemplo n.º 3
0
 public function testDecode()
 {
     $this->assertEquals(1, Hasher::decode('zlwrymre'));
     $this->assertFalse(Hasher::decode(''));
 }