PageRenderTime 52ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/bookmarks_controller.php

https://github.com/Gereon93/cakemarks
PHP | 357 lines | 287 code | 47 blank | 23 comment | 41 complexity | 233466757de0776957cb7d77dd376859 MD5 | raw file
  1. <?php
  2. // Copyright (c) 2011 Martin Ueding <dev@martin-ueding.de>
  3. class BookmarksController extends AppController {
  4. var $name = 'Bookmarks';
  5. var $uses = array('Bookmark', 'Visit', 'Quote', 'Keyword');
  6. var $helpers = array('Time');
  7. /**
  8. * Lists all bookmarks.
  9. *
  10. * @author Martin Ueding <dev@martin-ueding.de>
  11. */
  12. function index() {
  13. $this->Bookmark->recursive = 0;
  14. $this->set('bookmarks', $this->paginate());
  15. }
  16. function view($id = null) {
  17. if (!$id) {
  18. $this->Session->setFlash(__('Invalid bookmark', true));
  19. $this->redirect(array('action' => 'index'));
  20. }
  21. $data = $this->Bookmark->read(null, $id);
  22. $this->set('bookmark', $data);
  23. $this->set('visits', $this->Bookmark->Visit->find('count', array(
  24. "conditions" => array("Visit.bookmark_id" => $id))));
  25. $last_visit = $this->Bookmark->Visit->find('first', array(
  26. "conditions" => array("Visit.bookmark_id" => $id),
  27. "order" => array('Visit.created DESC')));
  28. $last_visit = strtotime($last_visit['Visit']['created']);
  29. $this->set('last_visit', $last_visit);
  30. if ($data['Bookmark']['revisit'] > 0) {
  31. $this->set('next_visit', $last_visit+$data['Bookmark']['revisit']*3600);
  32. }
  33. }
  34. function add($url = null) {
  35. if (!empty($this->data)) {
  36. $this->Bookmark->create();
  37. // add page title if missing
  38. if (empty($this->data['Bookmark']['title']) && !empty($this->data['Bookmark']['url'])) {
  39. $this->data['Bookmark']['title'] = $this->_get_page_title($this->data['Bookmark']['url']);
  40. }
  41. if ($this->Bookmark->save($this->data)) {
  42. if (!empty($this->data['Keyword']['title'])) {
  43. $this->data['Bookmark']['id'] = $this->Bookmark->id;
  44. $this->Keyword->save($this->data);
  45. }
  46. $this->Session->setFlash(__('The bookmark has been saved', true));
  47. $this->redirect(array('action' => 'view', $this->Bookmark->id));
  48. }
  49. else {
  50. $this->Session->setFlash(__('The bookmark could not be saved. Please, try again.', true));
  51. }
  52. }
  53. if ($url != null) {
  54. $this->data['Bookmark']['url'] = $this->_decode_url($url);
  55. }
  56. $keywords = $this->Bookmark->Keyword->find('list', array('order' => 'Keyword.title'));
  57. $this->set(compact('keywords'));
  58. }
  59. function _decode_url($url) {
  60. $url = str_replace("__slash__", "/", $url);
  61. $url = str_replace("__colon__", ":", $url);
  62. $url = str_replace("__hash__", "#", $url);
  63. $url = str_replace("__ques__", "?", $url);
  64. $url = str_replace("__amp__", "&", $url);
  65. return $url;
  66. }
  67. function _get_page_title($url) {
  68. // append the http in case it is missing
  69. if (substr($url, 0, 4) != 'http') {
  70. $url = 'http://'.$url;
  71. }
  72. $data = file_get_contents($url);
  73. preg_match('/<title>(.*?)<\/title>/', $data, $matches);
  74. if (isset($matches[1])) {
  75. $titel = $matches[1];
  76. return $titel;
  77. }
  78. return null;
  79. }
  80. function edit($id = null) {
  81. if (!$id && empty($this->data)) {
  82. $this->Session->setFlash(__('Invalid bookmark', true));
  83. $this->redirect(array('action' => 'index'));
  84. }
  85. if (!empty($this->data)) {
  86. if ($this->Bookmark->save($this->data) &&
  87. (empty($this->data['Keyword']['title']) || $this->Keyword->save($this->data))
  88. ) {
  89. $this->Session->setFlash(__('The bookmark has been saved', true));
  90. $this->redirect(array('action' => 'view', $this->Bookmark->id));
  91. }
  92. else {
  93. $this->Session->setFlash(__('The bookmark could not be saved. Please, try again.', true));
  94. }
  95. }
  96. if (empty($this->data)) {
  97. $this->data = $this->Bookmark->read(null, $id);
  98. }
  99. $keywords = $this->Bookmark->Keyword->find('list', array('order' => 'Keyword.title'));
  100. $this->set(compact('keywords'));
  101. }
  102. function delete($id = null) {
  103. if (!$id) {
  104. $this->Session->setFlash(__('Invalid id for bookmark', true));
  105. $this->redirect(array('action'=>'index'));
  106. }
  107. if ($this->Bookmark->delete($id)) {
  108. $this->Session->setFlash(__('Bookmark deleted', true));
  109. $this->redirect(array('action'=>'index'));
  110. }
  111. $this->Session->setFlash(__('Bookmark was not deleted', true));
  112. $this->redirect(array('action' => 'index'));
  113. }
  114. function startscreen() {
  115. $limit = Configure::read("UI.Startscreen.BoxLength");
  116. $this->set('reading_list', $this->Bookmark->find('all', array('conditions' => array('Bookmark.reading_list' => 1), 'limit' => $limit)));
  117. $this->set('most_visits', $this->Bookmark->find('all', array(
  118. 'fields' => array('Bookmark.id', 'Bookmark.title', 'Bookmark.url', 'count(Bookmark.id)'),
  119. 'group' => 'cakemarks_visits.bookmark_id',
  120. 'joins' => array(
  121. array(
  122. 'table' => 'cakemarks_visits',
  123. 'conditions' => array('cakemarks_visits.bookmark_id = Bookmark.id')
  124. )
  125. ),
  126. 'limit' => $limit,
  127. 'order' => 'count(Bookmark.id) DESC',
  128. )));
  129. $this->set('newest', $this->Bookmark->find('all', array('order' => array('Bookmark.created DESC'), 'limit' => $limit)));
  130. $latest_query = '
  131. SELECT Bookmark.id, Bookmark.title, Bookmark.url, Visit.created
  132. FROM (
  133. SELECT *
  134. FROM (
  135. SELECT *
  136. FROM cakemarks_visits
  137. ORDER BY cakemarks_visits.created DESC
  138. ) sorted_visits
  139. GROUP BY bookmark_id
  140. ) Visit
  141. JOIN cakemarks_bookmarks Bookmark ON Visit.bookmark_id=Bookmark.id
  142. ORDER BY Visit.created DESC
  143. LIMIT '.$limit;
  144. $this->set('recently_visited', $this->Bookmark->query($latest_query));
  145. $revisit_query = '
  146. SELECT Bookmark.id, Bookmark.title, Bookmark.url, Bookmark.revisit, Visit.created
  147. FROM (
  148. SELECT *
  149. FROM (
  150. SELECT *
  151. FROM cakemarks_visits
  152. ORDER BY cakemarks_visits.created DESC
  153. ) sorted_visits
  154. GROUP BY bookmark_id
  155. ) Visit
  156. JOIN cakemarks_bookmarks Bookmark ON Visit.bookmark_id=Bookmark.id
  157. WHERE Visit.created IS NOT NULL
  158. && Bookmark.revisit IS NOT NULL
  159. && ADDTIME(Visit.created, MAKETIME(Bookmark.revisit, 0, 0)) < now()
  160. ORDER BY Visit.created DESC
  161. LIMIT '.$limit;
  162. $this->set('revisit', $this->Bookmark->query($revisit_query));
  163. }
  164. function sticky_keywords() {
  165. // TODO Order the bookmarks in some predictable way.
  166. return $this->Keyword->find('all', array(
  167. 'conditions' => array('Keyword.sticky' => 1),
  168. 'order' => array('Keyword.title'),
  169. ));
  170. }
  171. function stats() {
  172. $stats = array(
  173. 'bookmark_count' => $this->Bookmark->find('count'),
  174. 'quote_count' => $this->Quote->find('count'),
  175. 'visit_count' => $this->Visit->find('count'),
  176. 'keyword_count' => $this->Keyword->find('count'),
  177. );
  178. return $stats;
  179. }
  180. function visit($id) {
  181. $to_visit = $this->Bookmark->find('first', array('conditions' => array('Bookmark.id' => $id)));
  182. // Write a Visit to the DB
  183. $visit = array('Visit' => array('bookmark_id' => $id));
  184. $this->Visit->save($visit);
  185. $to_url = $to_visit['Bookmark']['url'];
  186. if (!strpos($to_url, "://")) {
  187. $to_url = "http://".$to_url;
  188. }
  189. $this->redirect($to_url);
  190. }
  191. function export() {
  192. header('Content-type: application/json');
  193. $this->layout = 'ajax';
  194. $bookmarks = $this->Bookmark->find('all');
  195. foreach ($bookmarks as $bookmark) {
  196. $current['title'] = $bookmark['Bookmark']['title'];
  197. $current['url'] = $bookmark['Bookmark']['url'];
  198. foreach ($bookmark['Keyword'] as $keyword) {
  199. $keywords[] = $keyword['title'];
  200. }
  201. if (isset($keywords)) {
  202. $current['keywords'] = $keywords;
  203. }
  204. $data[] = $current;
  205. unset($current);
  206. unset($keywords);
  207. }
  208. $this->set("data", json_encode($data));
  209. }
  210. function import() {
  211. $this->import_result['added_bookmarks'] = 0;
  212. $this->import_result['added_keywords'] = 0;
  213. $this->import_result['existing_bookmarks'] = 0;
  214. $this->import_result['existing_keywords'] = 0;
  215. if (isset($this->data['Bookmark']['json'])) {
  216. $this->_import(json_decode($this->data['Bookmark']['json'], true));
  217. $this->set('show_results', true);
  218. }
  219. else {
  220. $this->set('show_results', false);
  221. }
  222. $this->set('show_form', true);
  223. $this->set('import_result', $this->import_result);
  224. }
  225. /**
  226. * Takes an array with the format specified in the import/export document.
  227. * Each bookmark is created if it does not exist, keywords are attached to
  228. * it.
  229. *
  230. * @param input array Bookmarks and Keywords to be imported
  231. * @author Martin Ueding <dev@martin-ueding.de>
  232. */
  233. function _import($input) {
  234. if (!isset($input) || empty($input) || count($input) == 0) {
  235. return;
  236. }
  237. foreach ($input as $bookmark) {
  238. # Build a CakePHP style array.
  239. $q['Bookmark']['title'] = $bookmark['title'];
  240. $q['Bookmark']['url'] = $bookmark['url'];
  241. # If there are any keywords, add them to the array.
  242. if (isset($bookmark['keywords'])) {
  243. foreach ($bookmark['keywords'] as $keyword) {
  244. $db_keyword = $this->Keyword->find('first', array(
  245. 'conditions' => array(
  246. 'Keyword.title' => $keyword)
  247. ));
  248. if (isset($db_keyword['Keyword']['id'])) {
  249. $q['Keyword'][] = $db_keyword['Keyword']['id'];
  250. $this->import_result['existing_keywords']++;
  251. }
  252. else {
  253. $this->Keyword->create();
  254. $this->Keyword->save(array('title' => $keyword));
  255. $q['Keyword'][] = $this->Keyword->id;
  256. $this->import_result['added_keywords']++;
  257. }
  258. }
  259. }
  260. # Check whether this title/url combination already exists.
  261. $count = $this->Bookmark->find('count', array(
  262. 'conditions' => array(
  263. 'Bookmark.title' => $q['Bookmark']['title'],
  264. 'Bookmark.url' => $q['Bookmark']['url'],
  265. )
  266. ));
  267. # Go to the next bookmark if this already exists.
  268. if ($count > 0) {
  269. $this->import_result['existing_bookmarks']++;
  270. }
  271. # Add bookmark to the database.
  272. else {
  273. $this->Bookmark->create();
  274. $this->Bookmark->save($q);
  275. $this->import_result['added_bookmarks']++;
  276. }
  277. unset($q);
  278. }
  279. }
  280. function favicon($id) {
  281. $this->view = 'media';
  282. $this->Bookmark->id = $id;
  283. $url = trim(str_replace('http://', '', trim($this->Bookmark->field('url'))), '/');
  284. $url = explode('/', $url);
  285. $hash = md5($url[0]);
  286. $url = 'http://' . $url[0] . '/favicon.ico';
  287. $dir = 'cache/favico';
  288. $file = $dir.'/'.$hash;
  289. if (!file_exists($dir)) {
  290. if (!mkdir($dir, 0777, true)) {
  291. die(__('Could not create favico temp dir', true));
  292. }
  293. }
  294. if (!file_exists($file)) {
  295. $contents = @file_get_contents($url);
  296. if ($contents) {
  297. $h = fopen($file, "w");
  298. fwrite($h, $contents);
  299. fclose($h);
  300. }
  301. }
  302. if (file_exists($file)) {
  303. header('location:../../'.$file);
  304. }
  305. else {
  306. header('location:../../img/blank16.png');
  307. }
  308. }
  309. }
  310. ?>