Just another url shortener http://shikiryu.alwaysdata.net/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
2.1 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. <?
  2. /**
  3. * Copyright ou © ou Copr. Clément Desmidt - Shikiryu, 14/02/2011
  4. *
  5. * shikiryu+url [ at ] gmail [ dot ] com
  6. *
  7. * Ce logiciel est un programme informatique servant à faire un raccourci d'url et stats.
  8. *
  9. * Il est sous license : http://creativecommons.org/licenses/by-nc-sa/2.0/fr/
  10. * ---------------------------------------
  11. * Copyright or © or Copr. Clément Desmidt - Shikiryu, 14/02/2011
  12. *
  13. * shikiryu+url [ at ] gmail [ dot ] com
  14. *
  15. * This software is a computer program whose purpose is to url shortening and stats.
  16. *
  17. * It's under this license : http://creativecommons.org/licenses/by-nc-sa/2.0/fr/
  18. */
  19. include 'XMLSQL.php';
  20. class ShortURL extends XMLSQL{
  21. const DATABASE = "db/database.xml";
  22. const STATE_ALREADY_EXIST = "This shortcut already exists. ";
  23. const STATE_FIELD_MISSING = "Don't leave any field blank ! ";
  24. const STATE_ERROR = "Error. ";
  25. const STATE_CREATED = "Shortcut created ";
  26. public $_debug = false;
  27. public function __construct($path = ''){
  28. parent::__construct($path.self::DATABASE);
  29. }
  30. public function shortThisUrl($longUrl, $shortName){
  31. if($this->pkAlreadyExists($shortName, 'url') || file_exists($shortName)){
  32. return self::STATE_ALREADY_EXIST;
  33. }else{
  34. return $this->insert(array('url'=>$longUrl,'hit'=>'0'), rawurlencode($shortName))->into('url')->query();
  35. }
  36. }
  37. public function findThisUrl($shortName){
  38. if($this->pkAlreadyExists(rawurlencode($shortName), 'url')){
  39. $this->_incrementStatFor($shortName);
  40. return $this->select(array('url'))->from('url')->where(rawurlencode($shortName))->query();
  41. }else{
  42. return;
  43. }
  44. }
  45. public function extractEverything(){
  46. return $this->select()->from('url')->query();
  47. }
  48. /**
  49. * Considering the table with $shortname already exist
  50. */
  51. private function _incrementStatFor($shortName){
  52. $currentHit = $this->select(array('hit'))->from('url')->where(rawurlencode($shortName))->query();
  53. $currentHit = $currentHit[0];
  54. return $this->update('url')->set(array('hit'=>$currentHit+1))->where(rawurlencode($shortName))->query();
  55. }
  56. }