PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/system/application/libraries/sweetcron.php

https://code.google.com/p/lifepress/
PHP | 370 lines | 270 code | 43 blank | 57 comment | 77 complexity | a6457a717cad1ee2254d2c054e1e347b MD5 | raw file
  1. <?php if (!defined('BASEPATH')) exit('No direct access allowed.');
  2. /**
  3. * Sweetcron
  4. *
  5. * Self-hosted lifestream software based on the CodeIgniter framework.
  6. *
  7. * Copyright (c) 2008, Yongfook.com & Edible, Inc.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without modification, are
  11. * permitted provided that the following conditions are met:
  12. *
  13. * * Redistributions of source code must retain the above copyright notice, this list of
  14. * conditions and the following disclaimer.
  15. *
  16. * * Redistributions in binary form must reproduce the above copyright notice, this list
  17. * of conditions and the following disclaimer in the documentation and/or other materials
  18. * provided with the distribution.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  21. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  22. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
  23. * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  25. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. * @package Sweetcron
  31. * @copyright 2008, Yongfook.com & Edible, Inc.
  32. * @author Yongfook
  33. * @link http://sweetcron/ Sweetcron
  34. * @license http://www.opensource.org/licenses/gpl-2.0.php GPL License
  35. */
  36. class Sweetcron {
  37. function __construct() {
  38. $this->CI =& get_instance();
  39. $this->CI->config->set_item('lifepress_release', 'Heathcliff');
  40. $this->CI->config->set_item('sweetcron_version', '0.1');
  41. }
  42. function fetch_items()
  43. {
  44. if(!ini_get('safe_mode')){
  45. //requires php to not be running in "safe mode"
  46. set_time_limit(0);
  47. }
  48. //soz
  49. $option->option_name = 'last_fetch';
  50. $option->option_value = time();
  51. $this->CI->option_model->add_option($option);
  52. $feeds = $this->CI->feed_model->get_active_feeds();
  53. if ($feeds) {
  54. foreach ($feeds as $feed) {
  55. $this->CI->simplepie->set_feed_url($feed->feed_url);
  56. $this->CI->simplepie->enable_cache(FALSE);
  57. $this->CI->simplepie->init();
  58. $items = $this->CI->simplepie->get_items();
  59. $this->add_new_items($items, $feed);
  60. }
  61. }
  62. }
  63. function add_new_items($items, $feed)
  64. {
  65. foreach ($items as $item) {
  66. $new->item_data = array();
  67. $new->item_data['title'] = $item->get_title();
  68. $new->item_data['permalink'] = $item->get_permalink();
  69. $new->item_data['content'] = $item->get_content();
  70. $new->item_data['enclosures'] = $item->get_enclosures();
  71. $new->item_data['categories'] = $item->get_categories();
  72. $new->item_data['tags'] = $this->get_tags($new->item_data);
  73. $new->item_data['image'] = $this->get_image($item->get_content());
  74. //build out clean item
  75. $new->item_status = 'publish';
  76. $new->item_date = strtotime($item->get_date());
  77. $new->item_title = $this->CI->input->xss_clean(trim(strip_tags($item->get_title())));
  78. $new->item_permalink = $item->get_permalink();
  79. $new->item_content = $this->CI->input->xss_clean(trim(strip_tags($item->get_content())));
  80. $new->item_name = url_title($new->item_title);
  81. $new->item_feed_id = $feed->feed_id;
  82. $new = $this->extend('pre_db', $feed->feed_domain, $new, $item);
  83. //and add
  84. $this->CI->item_model->add_item($new);
  85. }
  86. }
  87. function pseudo_cron()
  88. {
  89. if ($this->CI->config->item('cron_type') == 'pseudo') {
  90. //time in seconds between each pseudo cron
  91. //if you want more frequent cron updates it's better to rely on "true cron" and to turn off pseudo cron
  92. $interval = 1800; //1800 = 30 minutes
  93. if (($this->CI->config->item('last_access') - $this->CI->config->item('last_fetch')) > $interval) {
  94. $this->fetch_items();
  95. }
  96. }
  97. }
  98. function extend($method = 'pre_db', $feed_domain = NULL, $item = NULL, $simplepie_object = NULL)
  99. {
  100. //we can extend what sweetcron does at various points in the import / output process by using plugin architecture
  101. //see system/applications/plugins for example plugins
  102. if ($feed_domain && $item) {
  103. $class = str_replace('.', '_', $feed_domain);
  104. $plugin = BASEPATH.'application/plugins/'.$class.'.php';
  105. if (file_exists($plugin)) {
  106. //check if already loaded
  107. if (!method_exists($class, $method)) {
  108. include(BASEPATH.'application/plugins/'.$class.'.php');
  109. }
  110. $plugin = new $class;
  111. return $plugin->$method($item, $simplepie_object);
  112. } else {
  113. return $item;
  114. }
  115. }
  116. }
  117. function get_tags($data)
  118. {
  119. $tags = '';
  120. $other_tags = '';
  121. //attempt to pull from enclosures
  122. if (isset($data['enclosures'][0]->categories[0]->term)) {
  123. $tags = html_entity_decode($data['enclosures'][0]->categories[0]->term);
  124. $tags = explode(' ', $tags);
  125. }
  126. //attempt to pull from categories
  127. if (isset($data['categories'][0]->term)) {
  128. foreach ($data['categories'] as $category) {
  129. //sometimes a tag is an ugly url that I don't think we want...
  130. if (substr($category->term, 0, 7) != 'http://') {
  131. $other_tags[] = html_entity_decode($category->term);
  132. }
  133. }
  134. }
  135. $tags_count = count($tags);
  136. $other_tags_count = count($other_tags);
  137. //lets go with whichever has the most...
  138. if ($tags_count > $other_tags_count) {
  139. $tags = $tags;
  140. } else {
  141. $tags = $other_tags;
  142. }
  143. //clean before returning...
  144. return $tags;
  145. }
  146. function get_image($html) {
  147. //kudos to:
  148. //http://zytzagoo.net/blog/2008/01/23/extracting-images-from-html-using-regular-expressions/
  149. if (stripos($html, '<img') !== false) {
  150. $imgsrc_regex = '#<\s*img [^\>]*src\s*=\s*(["\'])(.*?)\1#im';
  151. preg_match($imgsrc_regex, $html, $matches);
  152. unset($imgsrc_regex);
  153. unset($html);
  154. if (is_array($matches) && !empty($matches)) {
  155. return $matches[2];
  156. } else {
  157. return false;
  158. }
  159. } else {
  160. return false;
  161. }
  162. }
  163. function is_current_page($page_name)
  164. {
  165. //just a simple tab highlighter for the admin panel
  166. if ($this->CI->uri->segment(2) == strtolower($page_name)) {
  167. return true;
  168. }
  169. }
  170. function integrity_check()
  171. {
  172. if (!$this->CI->db->table_exists('feeds') || !$this->CI->db->table_exists('items') || !$this->CI->db->table_exists('options') || !$this->CI->db->table_exists('tags') || !$this->CI->db->table_exists('tag_relationships') || !$this->CI->db->table_exists('users')) {
  173. if (file_exists(BASEPATH.'../.htaccess')) {
  174. die('Whoo Hoo! Almost there - now just run the <a href="'.$this->CI->config->item('base_url').'admin/install'.'">install script</a>.');
  175. } else {
  176. die('Looks like you are missing an .htaccess file...<br />For instructions on creating one, please see <a href="http://code.google.com/p/sweetcron/wiki/Installation">the installation documentation</a>');
  177. }
  178. }
  179. }
  180. function install_check()
  181. {
  182. if ($this->CI->db->table_exists('feeds') || $this->CI->db->table_exists('items') || $this->CI->db->table_exists('options') || $this->CI->db->table_exists('tags') || $this->CI->db->table_exists('tag_relationships') || $this->CI->db->table_exists('users')) {
  183. die('Sweetcron is already (or partially) installed. If you wish to reinstall, please clear your database first.');
  184. }
  185. }
  186. function compatibility_check()
  187. {
  188. //checks php version
  189. if (version_compare(PHP_VERSION, '5.0.0', '<')) {
  190. die('Sorry, Sweetcron is for PHP5 and above. Your version of PHP is lower than that. Time to upgrade?');
  191. }
  192. }
  193. function do_search()
  194. {
  195. if ($this->CI->input->post('query')) {
  196. //strip out stuff and send to page
  197. $query = urlencode($this->CI->input->post('query'));
  198. header('Location: '.$this->CI->config->item('base_url').'items/search/'.$query);
  199. } else {
  200. show_error('You must type some keywords to search');
  201. }
  202. }
  203. function get_single_item_page($item_id = NULL)
  204. {
  205. if ($item_id) {
  206. //remove query string (some 3rd party commenting services refer back after commenting)
  207. $item_id = explode('?', $item_id);
  208. $item_id = $item_id[0];
  209. $data->item = $this->CI->item_model->get_public_item_by_id($item_id);
  210. $data->page_name = $data->item->get_title();
  211. $data->popular_tags = $this->CI->tag_model->get_all_tags('count', 50);
  212. $data->all_tags = $this->CI->tag_model->get_all_tags('count');
  213. $this->CI->load->view('themes/'.$this->CI->config->item('theme').'/_header', $data);
  214. $this->CI->load->view('themes/'.$this->CI->config->item('theme').'/single', $data);
  215. $this->CI->load->view('themes/'.$this->CI->config->item('theme').'/_footer', $data);
  216. }
  217. }
  218. function query_items($type = 'site', $query = NULL, $offset = 0, $limit = 10)
  219. {
  220. return $this->get_items_page($type, NULL, TRUE, $query, NULL, TRUE, $offset, $limit);
  221. }
  222. function get_items_page($type = 'index', $current_page_num = 1, $public = FALSE, $query = NULL, $rss_filter = NULL, $query_items = NULL, $offset = NULL, $limit = NULL)
  223. {
  224. //return raw items for query_items()
  225. if ($query_items) {
  226. if ($type == 'site') {
  227. return $this->CI->item_model->get_items_by_feed_domain($offset, $limit, $query, $public);
  228. } elseif ($type == 'tag') {
  229. return $this->CI->item_model->get_items_by_tag($offset, $limit, $query, $public);
  230. } elseif ($type == 'search') {
  231. return $this->CI->item_model->get_items_by_search($offset, $limit, $query, $public);
  232. }
  233. exit();
  234. }
  235. $data->blog_posts = $this->CI->item_model->get_items_by_feed_domain(0, 10, 'sweetcron', $public);
  236. $data->active_feeds = $this->CI->feed_model->get_active_feeds(TRUE);
  237. $data->popular_tags = $this->CI->tag_model->get_all_tags('count', 50);
  238. $data->all_tags = $this->CI->tag_model->get_all_tags('count');
  239. $data->page_type = $type;
  240. if ($type == 'rss_feed') {
  241. if (!$rss_filter) {
  242. $data->page_name = '';
  243. $data->items = $this->CI->item_model->get_all_items(0, 20, $public);
  244. } elseif ($rss_filter == 'tag') {
  245. $data->page_name = '- tagged with '.$query;
  246. $data->items = $this->CI->item_model->get_items_by_tag(0, 20, $query, $public);
  247. } elseif ($rss_filter == 'search') {
  248. $data->page_name = '- search for '.$query;
  249. $data->items = $this->CI->item_model->get_items_by_search(0, 20, $query, $public);
  250. } elseif ($rss_filter == 'site') {
  251. $data->page_name = '- imported from '.$query;
  252. $data->items = $this->CI->item_model->get_items_by_feed_domain(0, 20, $query, $public);
  253. }
  254. $this->CI->load->view('themes/'.$this->CI->config->item('theme').'/rss_feed', $data);
  255. } elseif ($type == 'static_page') {
  256. $page = $this->CI->uri->segment(2);
  257. $data->page_name = ucwords($page);
  258. if (file_exists(BASEPATH.'application/views/themes/'.$this->CI->config->item('theme').'/'.$page.'.php')) {
  259. $this->CI->load->view('themes/'.$this->CI->config->item('theme').'/_header', $data);
  260. $this->CI->load->view('themes/'.$this->CI->config->item('theme').'/'.$page, $data);
  261. $this->CI->load->view('themes/'.$this->CI->config->item('theme').'/_footer', $data);
  262. } else {
  263. show_404();
  264. }
  265. } else {
  266. $this->CI->page->SetItemsPerPage($this->CI->config->item('per_page'));
  267. $this->CI->page->SetQueryStringVar('page');
  268. $this->CI->page->SetLinksFormat('&lsaquo;',' ','&rsaquo;');
  269. $this->CI->page->SetLinksToDisplay(10);
  270. $this->CI->page->SetCurrentPage($current_page_num);
  271. if ($public) {
  272. $admin = '';
  273. } else {
  274. $admin = 'admin/';
  275. }
  276. //conditionals depending on page type
  277. if ($type == 'index') {
  278. $data->page_name = 'Home';
  279. $this->CI->page->SetItemCount($this->CI->item_model->count_all_items($public));
  280. if ($public) {
  281. $this->CI->page->SetLinksHref($this->CI->config->item('base_url').$admin);
  282. } else {
  283. $this->CI->page->SetLinksHref($this->CI->config->item('base_url').$admin.'items/');
  284. }
  285. $data->items = $this->CI->item_model->get_all_items($this->CI->page->GetOffset(), $this->CI->page->GetSqlLimit(), $public);
  286. } elseif ($type == 'search') {
  287. $data->page_name = 'Items Search';
  288. $this->CI->page->SetItemCount($this->CI->item_model->count_items_by_search($query, $public));
  289. $this->CI->page->SetLinksHref($this->CI->config->item('base_url').$admin.'items/search/'.$query.'/');
  290. $data->items = $this->CI->item_model->get_items_by_search($this->CI->page->GetOffset(), $this->CI->page->GetSqlLimit(), $query, $public);
  291. } elseif ($type == 'tag') {
  292. $data->page_name = 'Items Tag';
  293. $this->CI->page->SetItemCount($this->CI->item_model->count_items_by_tag($query, $public));
  294. $this->CI->page->SetLinksHref($this->CI->config->item('base_url').$admin.'items/tag/'.$query.'/');
  295. $data->items = $this->CI->item_model->get_items_by_tag($this->CI->page->GetOffset(), $this->CI->page->GetSqlLimit(), $query, $public);
  296. } elseif ($type == 'site') {
  297. $data->page_name = 'Items Site';
  298. $this->CI->page->SetItemCount($this->CI->item_model->count_items_by_feed_domain($query, $public));
  299. $this->CI->page->SetLinksHref($this->CI->config->item('base_url').$admin.'items/site/'.$query.'/');
  300. $data->items = $this->CI->item_model->get_items_by_feed_domain($this->CI->page->GetOffset(), $this->CI->page->GetSqlLimit(), $query, $public);
  301. }
  302. $data->page_query = $query;
  303. if ($query && $type == 'search') {
  304. $data->query = $query;
  305. }
  306. if ($query && $type == 'tag') {
  307. $data->tag = urldecode($query);
  308. }
  309. if ($query && $type == 'site') {
  310. $data->site = $query;
  311. }
  312. //load view
  313. if ($public) {
  314. $data->pages = $this->CI->page->GetPageLinks();
  315. $this->CI->load->view('themes/'.$this->CI->config->item('theme').'/_header', $data);
  316. $this->CI->load->view('themes/'.$this->CI->config->item('theme').'/items', $data);
  317. $this->CI->load->view('themes/'.$this->CI->config->item('theme').'/_footer', $data);
  318. } else {
  319. $data->pages = $this->CI->page->GetPageLinks();
  320. $this->CI->load->view('admin/_header', $data);
  321. $this->CI->load->view('admin/items', $data);
  322. $this->CI->load->view('admin/_footer');
  323. }
  324. }
  325. }
  326. }
  327. ?>