2010-12-23 12:26:25 +01:00
|
|
|
<?
|
2011-02-14 17:49:52 +01:00
|
|
|
/**
|
|
|
|
* Copyright ou © ou Copr. Clément Desmidt - Shikiryu, 14/02/2011
|
|
|
|
*
|
|
|
|
* shikiryu+url [ at ] gmail [ dot ] com
|
|
|
|
*
|
2011-02-14 18:10:49 +01:00
|
|
|
* Ce logiciel est un programme informatique servant à faire un raccourci d'url et stats.
|
2011-02-14 17:49:52 +01:00
|
|
|
*
|
2011-02-14 18:10:49 +01:00
|
|
|
* Il est sous license : http://creativecommons.org/licenses/by-nc-sa/2.0/fr/
|
2011-02-14 17:49:52 +01:00
|
|
|
* ---------------------------------------
|
|
|
|
* Copyright or © or Copr. Clément Desmidt - Shikiryu, 14/02/2011
|
|
|
|
*
|
|
|
|
* shikiryu+url [ at ] gmail [ dot ] com
|
|
|
|
*
|
2011-02-14 18:10:49 +01:00
|
|
|
* This software is a computer program whose purpose is to url shortening and stats.
|
2011-02-14 17:49:52 +01:00
|
|
|
*
|
2011-02-14 18:10:49 +01:00
|
|
|
* It's under this license : http://creativecommons.org/licenses/by-nc-sa/2.0/fr/
|
2011-02-14 17:49:52 +01:00
|
|
|
*/
|
2010-12-23 12:26:25 +01:00
|
|
|
include 'XMLSQL.php';
|
|
|
|
class ShortURL extends XMLSQL{
|
|
|
|
|
|
|
|
const DATABASE = "db/database.xml";
|
2011-01-04 12:26:21 +01:00
|
|
|
const STATE_ALREADY_EXIST = "This shortcut already exists. ";
|
|
|
|
const STATE_FIELD_MISSING = "Don't leave any field blank ! ";
|
|
|
|
const STATE_ERROR = "Error. ";
|
|
|
|
const STATE_CREATED = "Shortcut created ";
|
2010-12-23 12:26:25 +01:00
|
|
|
public $_debug = false;
|
|
|
|
|
|
|
|
public function __construct($path = ''){
|
|
|
|
parent::__construct($path.self::DATABASE);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function shortThisUrl($longUrl, $shortName){
|
|
|
|
if($this->pkAlreadyExists($shortName, 'url')){
|
|
|
|
return self::STATE_ALREADY_EXIST;
|
|
|
|
}else{
|
2011-01-04 11:57:00 +01:00
|
|
|
return $this->insert(array('url'=>$longUrl,'hit'=>'0'), rawurlencode($shortName))->into('url')->query();
|
2010-12-23 12:26:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function findThisUrl($shortName){
|
|
|
|
if($this->pkAlreadyExists(rawurlencode($shortName), 'url')){
|
2011-01-04 11:57:00 +01:00
|
|
|
$this->_incrementStatFor($shortName);
|
2010-12-23 12:26:25 +01:00
|
|
|
return $this->select(array('url'))->from('url')->where(rawurlencode($shortName))->query();
|
|
|
|
}else{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function extractEverything(){
|
|
|
|
return $this->select()->from('url')->query();
|
|
|
|
}
|
|
|
|
|
2011-01-04 11:57:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Considering the table with $shortname already exist
|
|
|
|
*/
|
|
|
|
private function _incrementStatFor($shortName){
|
|
|
|
$currentHit = $this->select(array('hit'))->from('url')->where(rawurlencode($shortName))->query();
|
|
|
|
$currentHit = $currentHit[0];
|
|
|
|
return $this->update('url')->set(array('hit'=>$currentHit+1))->where(rawurlencode($shortName))->query();
|
|
|
|
}
|
|
|
|
|
2010-12-23 12:26:25 +01:00
|
|
|
}
|