TinyUrlsController.php 2.4 KB

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