Skip to content

Commit

Permalink
added some support for url aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
pean committed Oct 14, 2015
1 parent 0f651b2 commit 5d591c5
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 6 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"require": {
"hashids/hashids": "~1.0@dev",
"united-prototype/php-ga": "dev-composer",
"ivkos/pushbullet": "2.*",
"php": ">=5.3.2"
},
"require-dev": {
Expand Down
59 changes: 54 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 23 additions & 1 deletion src/Pean/Wash.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,13 @@ protected function identifyURL($url) {
return FALSE;
}

$url_id = $this->hashids('decode',$hash);
// Check alias first
$url_id = $this->getAlias($hash);

// If not an alias, go to hash
if($url_id === FALSE) {
$url_id = $this->hashids('decode',$hash);
}

if(preg_match('/^[\d]+$/',$url_id)) {
// Something there
Expand Down Expand Up @@ -133,6 +139,22 @@ protected function gotoURL($url, $output = 'redirect') {
}
}

protected function getAlias($alias) {
$this->db();
$sql = "select url_id from wash_aliases where alias=?";
$stmt = $this->dbh->prepare($sql);
try {
$stmt->execute(array($alias));
$url_id = $stmt->fetchColumn();
} catch (PDOException $e) {
$this->response(0,"errorMsg","Could not fetch alias: ".$e->getMessage());
}
if(!empty($url_id)) {
return $url_id;
}
return FALSE;
}

protected function getUser() {

if(empty($this->arr['token'])) {
Expand Down
7 changes: 7 additions & 0 deletions tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,10 @@ created datetime,
primary key(id),
foreign key uid(user_id) references wash_users(id) on delete cascade
)engine=InnoDB;


create table wash_aliases(
url_id int,
alias varchar(255) unique,
foreign key uid(url_id) references wash_urls(id) on delete cascade
)engine=InnoDB;
12 changes: 12 additions & 0 deletions tests/WashTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ public function testIdentifyURL() {

}

public function testGetAlias() {

$alias = 'wash';

$method = new ReflectionMethod($this->wash,'getAlias');
$method->setAccessible(TRUE);

$id = $method->invokeArgs($this->wash, array($alias));
$this->assertEquals(FALSE,$id);

}

public function testGotoURL() {
$method = new ReflectionMethod($this->wash,'gotoURL');
$method->setAccessible(TRUE);
Expand Down

0 comments on commit 5d591c5

Please sign in to comment.