PageRenderTime 72ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/app/Controller/TwitsController.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 255 lines | 139 code | 36 blank | 80 comment | 30 complexity | 5a23619e346e6ffd06c1d3d3a695f66a MD5 | raw file
  1. <?php
  2. /* $
  3. Document : fktwitter
  4. Created on : Dec 26, 2011, 7:12:34 PM
  5. Author : udeshika
  6. Description: fktwitter
  7. */
  8. class TwitsController extends AppController {
  9. public $name = 'Twits';
  10. public $helpers = array('Html', 'Form', 'Js', 'Custom');
  11. public $components = array('Session');
  12. /**
  13. * get all twits everyone else X
  14. * @access public
  15. * @param
  16. * @return posts array pass to the view
  17. */
  18. public function index() {
  19. if ($this->check_users()) {
  20. $results = $this->Twit->query("SELECT PO.* , US.username ,US.fname ,US.lname ,US.image FROM `twits` as PO LEFT
  21. JOIN users as US ON US.username = PO.username WHERE PO.username!='" . $this->Session->read('User') . "' ORDER BY UNIX_TIMESTAMP(PO.created) DESC");
  22. $this->set('posts', $results);
  23. } else {
  24. $this->redirect('../users/login');
  25. }
  26. }
  27. /**
  28. * get all twits everyone else
  29. * @access public
  30. * @param $user login user or all user profile $user name
  31. * @return posts array pass to the view, userdata array returns profile data
  32. */
  33. public function stream($user='') {
  34. if ($this->check_users()) {
  35. if ($user == 'everyone') {
  36. $results = $this->Twit->query("SELECT PO.* , US.username ,US.fname ,US.lname ,US.image FROM `twits` as PO LEFT
  37. JOIN users as US ON US.username = PO.username WHERE PO.username!='" . $this->Session->read('User') . "' ORDER BY UNIX_TIMESTAMP(PO.created) DESC");
  38. } else {
  39. $results = $this->Twit->query("SELECT PO.* , US.username ,US.fname ,US.lname ,US.image FROM `twits` as PO LEFT
  40. JOIN users as US ON US.username = PO.username WHERE US.username='" . $user . "' OR LOCATE ('@" . $user . "', PO.twitt) > 0 ORDER BY UNIX_TIMESTAMP(PO.created) DESC");
  41. $user_results = $this->Twit->query("SELECT US.* FROM `users` as US WHERE US.username='" . $user . "'");
  42. $this->set('userdata', $user_results);
  43. }
  44. //$this->set('sidecontent_for_layout', 'sidecontent');
  45. $this->set('posts', $results);
  46. } else {
  47. $this->redirect('../users/login');
  48. }
  49. }
  50. /**
  51. * get last insert twitt using AJAX
  52. * @access public
  53. * @param last insert id $id
  54. * @return posts array pass to the view
  55. */
  56. public function getstream($id='') {
  57. if ($this->check_users()) {
  58. $this->layout = '';
  59. if ($id > 0) {
  60. $results = $this->Twit->query("SELECT PO.* , US.username ,US.fname ,US.lname ,US.image FROM `twits` as PO LEFT
  61. JOIN users as US ON US.username = PO.username WHERE PO.id='" . $id . "'");
  62. } else {
  63. $results = $this->Twit->query("SELECT PO.* , US.username ,US.fname ,US.lname ,US.image FROM `twits` as PO LEFT
  64. JOIN users as US ON US.username = PO.username ORDER BY PO.created DESC");
  65. }
  66. $this->set('posts', $results);
  67. } else {
  68. $this->redirect('../users/login');
  69. }
  70. }
  71. /**
  72. * All status stream refreshed every 2 second using AJAX
  73. * @access public
  74. * @param select data acording to last twitt ($id) and username ($user)
  75. * @return posts array pass to the view
  76. */
  77. public function refreshe($id='', $user='') {
  78. if ($this->check_users()) {
  79. $this->layout = '';
  80. if ($user != '') {
  81. if ($id > 0) {
  82. $results = $this->Twit->query("SELECT PO.* , US.username ,US.fname ,US.lname ,US.image FROM `twits` as PO LEFT
  83. JOIN users as US ON US.username = PO.username WHERE PO.id >'" . $id . "' AND
  84. (US.username='" . $user . "' OR LOCATE ('@" . $user . "', PO.twitt) > 0 ) ORDER BY UNIX_TIMESTAMP(PO.created) DESC");
  85. $this->set('posts', $results);
  86. }
  87. } else {
  88. if ($id > 0) {
  89. $results = $this->Twit->query("SELECT PO.* , US.username ,US.fname ,US.lname ,US.image FROM `twits` as PO LEFT
  90. JOIN users as US ON US.username = PO.username WHERE PO.username!='" . $this->Session->read('User') . "' AND PO.id >'" . $id . "' ORDER BY UNIX_TIMESTAMP(PO.created) DESC");
  91. $this->set('posts', $results);
  92. }
  93. }
  94. } else {
  95. $this->redirect('../users/login');
  96. }
  97. }
  98. /**
  99. * more info twitt via jq ajax
  100. * @access public
  101. * @param int twitt $id
  102. * @return posts array pass to the view
  103. */
  104. public function view($id) {
  105. $this->layout = '';
  106. $results = $this->Twit->query("SELECT PO.* , US.username ,US.fname ,US.lname ,US.image FROM `twits` as PO LEFT
  107. JOIN users as US ON US.username = PO.username WHERE PO.id='".$id."'");
  108. $this->set('posts', $results);
  109. }
  110. /**
  111. * add new twitt via jq ajax
  112. * @access public
  113. * @param url convert to shorten and post will save
  114. * @return redirect twitt strim
  115. */
  116. public function ajaxadd() {
  117. if ($this->request->is('post')) {
  118. $this->layout = '';
  119. $this->request->data['twitt'] = $this->set_url($this->request->data['twitt']);
  120. if ($this->Twit->save($this->request->data)) {
  121. echo $this->Twit->getLastInsertId();
  122. // echo 'Your post has been saved.';
  123. } else {
  124. echo 'Unable to add your post.';
  125. }
  126. }
  127. }
  128. /**
  129. * add new twitt
  130. * @access public
  131. * @param url convert to shorten and post will save
  132. * @return redirect twitt strim
  133. */
  134. public function add() {
  135. if ($this->check_users()) {
  136. if ($this->request->is('post')) {
  137. $this->request->data['twitt'] = $this->set_url($this->request->data['twitt']);
  138. if ($this->Twit->save($this->request->data)) {
  139. $this->Session->setFlash('Your post has been saved.');
  140. $this->redirect('stream/everyone');
  141. } else {
  142. echo 'Unable to add your post.';
  143. }
  144. }
  145. } else {
  146. $this->redirect('../users/login');
  147. }
  148. }
  149. /**
  150. * delete twitter
  151. * @access public
  152. * @param delete twitt acording to $id
  153. * @return redirect to user profile
  154. */
  155. function delete($id) {
  156. if ($this->check_users()) {
  157. if ($this->request->is('get')) {
  158. throw new MethodNotAllowedException();
  159. }
  160. if ($this->Twit->delete($id)) {
  161. $this->Session->setFlash('The twitter with id: ' . $id . ' has been deleted.');
  162. $this->redirect(array('action' => 'stream/' . $this->Session->read('User') . ''));
  163. }
  164. } else {
  165. $this->redirect('../users/login');
  166. }
  167. }
  168. /**
  169. * string replase with <a href= and shortener url
  170. * @access public
  171. * @param string $text replase
  172. * @return text
  173. */
  174. public function set_url($text) {
  175. $urlset = $this->geturls($text);
  176. foreach ($urlset[0] as $k => $url) {
  177. $url_shortener = $this->random_alpha(5);
  178. $text = str_replace($url, '<a href="' . $this->base . '/t/url/' . $url_shortener . '">' . $url . '</a>', $text);
  179. $this->insert_url($url, $url_shortener);
  180. }
  181. return $text;
  182. }
  183. /**
  184. * preg_match_all urls
  185. * @access public
  186. * @param string $text to $matches array
  187. * @return array
  188. */
  189. public function geturls($text) {
  190. $matches = array();
  191. preg_match_all('/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[.\!\/\\w]*))?)/', $text, $matches);
  192. return $matches;
  193. }
  194. /**
  195. * Inserts a URL to the database and returns the whole dataset
  196. * @access public
  197. * @param string $url Url to shorten
  198. * @return array Array with all available data inside, or empty one
  199. */
  200. function insert_url($url, $shortener_url) {
  201. $ret = array();
  202. $sql = 'INSERT INTO url_redirect (url,shortener_url)
  203. VALUES (
  204. \'' . $url . '\',
  205. \'' . $shortener_url . '\');';
  206. $results = $this->Twit->query($sql);
  207. }
  208. /**
  209. * randum 5 letter str
  210. * @access public
  211. * @param $alphNums all alph numeric values,$length length of new url
  212. * @return pass new shorten url
  213. */
  214. function random_alpha($length) {
  215. $alphNums = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  216. $newString = str_shuffle(str_repeat($alphNums, rand(1, $length)));
  217. return substr($newString, rand(0, strlen($newString) - $length), $length);
  218. }
  219. }