TinyUrlsController.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. App::uses('ToolsAppController', 'Tools.Controller');
  3. /*
  4. Apply this route (/Config/routes.php):
  5. Router::connect('/s/:id',
  6. array('plugin'=>'tools', 'controller'=>'tiny_urls', 'action'=>'go'),
  7. array('id'=>'[0-9a-zA-Z]+'));
  8. Result:
  9. /domain/s/ID
  10. */
  11. class TinyUrlsController extends ToolsAppController {
  12. //public $uses = array('Tools.TinyUrl');
  13. public function beforeFilter() {
  14. parent::beforeFilter();
  15. if (isset($this->Auth)) {
  16. $this->Auth->allow('go');
  17. }
  18. }
  19. /****************************************************************************************
  20. * ADMIN functions
  21. ****************************************************************************************/
  22. /**
  23. * Main redirect function
  24. */
  25. public function go() {
  26. if (empty($this->request->params['id'])) {
  27. throw new NotFoundException();
  28. }
  29. $entry = $this->TinyUrl->translate($this->request->params['id']);
  30. if (empty($entry)) {
  31. throw new NotFoundException();
  32. }
  33. //$message = $entry['TinyInt']['flash_message'];
  34. $url = $entry['TinyUrl']['target'];
  35. if (!empty($message)) {
  36. $type = !empty($entry['TinyUrl']['flash_type']) ? $entry['TinyUrl']['flash_type'] : 'success';
  37. $this->Common->flashMessage($message, $type);
  38. }
  39. $this->TinyUrl->up($entry['TinyUrl']['id'], array('field' => 'used', 'modify' => true, 'timestampField' => 'last_used'));
  40. return $this->redirect($url, 301);
  41. }
  42. public function admin_index() {
  43. //TODO
  44. if ($this->Common->isPosted()) {
  45. $this->TinyUrl->set($this->request->data);
  46. if ($this->TinyUrl->validates()) {
  47. $id = $this->TinyUrl->generate($this->TinyUrl->data['TinyUrl']['url']);
  48. $this->Common->flashMessage('New Key: ' . h($id), 'success');
  49. $url = $this->TinyUrl->urlByKey($id);
  50. $this->set(compact('url'));
  51. $this->request->data = array();
  52. }
  53. }
  54. $tinyUrls = $this->TinyUrl->find('count', array('conditions' => array()));
  55. $this->set(compact('tinyUrls'));
  56. }
  57. public function admin_listing() {
  58. }
  59. public function admin_reset() {
  60. if (!$this->Common->isPosted()) {
  61. throw new MethodNotAllowedException();
  62. }
  63. $this->TinyUrl->truncate();
  64. $this->Common->flashMessage(__('Done'), 'success');
  65. return $this->Common->autoRedirect(array('action' => 'index'));
  66. }
  67. }