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

/inc/core.php

https://github.com/idotter/wp-lifestream
PHP | 2982 lines | 2479 code | 270 blank | 233 comment | 278 complexity | 86aad731bce845433c2031cd774e5c17 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. if (!class_exists('SimplePie'))
  3. {
  4. require_once(LIFESTREAM_PATH . '/lib/simplepie.inc.php');
  5. }
  6. global $wpdb, $userdata, $lifestream;
  7. function lifestream_path_join()
  8. {
  9. $bits = func_get_args();
  10. $sep = (in_array(PHP_OS, array("WIN32", "WINNT")) ? '\\' : '/');
  11. foreach ($bits as $key=>$value) {
  12. $bits[$key] = rtrim($value, $sep);
  13. }
  14. return implode($sep, $bits);
  15. }
  16. function lifestream_array_key_pop($array, $key, $default=null)
  17. {
  18. $value = @$array[$key];
  19. unset($array[$key]);
  20. if (!$value) $value = $default;
  21. return $value;
  22. }
  23. // Returns the utf string corresponding to the unicode value (from php.net, courtesy - romans@void.lv)
  24. function lifestream_code2utf($num)
  25. {
  26. if ($num < 128) return chr($num);
  27. if ($num < 2048) return chr(($num >> 6) + 192) . chr(($num & 63) + 128);
  28. if ($num < 65536) return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
  29. if ($num < 2097152) return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
  30. return '';
  31. }
  32. function lifestream_str_startswith($string, $chunk)
  33. {
  34. return substr($string, 0, strlen($chunk)) == $chunk;
  35. }
  36. function lifestream_str_endswith($string, $chunk)
  37. {
  38. return substr($string, strlen($chunk)*-1) == $chunk;
  39. }
  40. function lifestream_get_class_constant($class, $const)
  41. {
  42. return constant(sprintf('%s::%s', $class, $const));
  43. }
  44. class Lifestream_Error extends Exception { }
  45. class Lifestream_ValidationError extends Exception { }
  46. class Lifestream_FeedFetchError extends Lifestream_Error { }
  47. class Lifestream_Event
  48. {
  49. /**
  50. * Represents a single event in the database.
  51. */
  52. function __construct(&$lifestream, $row)
  53. {
  54. $this->lifestream = $lifestream;
  55. $this->date = $row->timestamp;
  56. $this->data = array(unserialize($row->data));
  57. $this->id = $row->id;
  58. $this->timestamp = $row->timestamp;
  59. $this->total = 1;
  60. $this->is_grouped = false;
  61. $this->key = $row->key;
  62. $this->owner = $row->owner;
  63. $this->owner_id = $row->owner_id;
  64. $this->post_id = $row->post_id;
  65. $this->visible = $row->visible;
  66. $this->link = @(!empty($this->data['link']) ? $this->data['link'] : $row->link);
  67. $cls = $this->lifestream->get_feed($row->feed);
  68. $this->feed = new $cls($this->lifestream, unserialize($row->options), $row->feed_id);
  69. }
  70. function __toString()
  71. {
  72. return $this->data[0]['title'];
  73. }
  74. function get_event_display()
  75. {
  76. return $this->feed->get_event_display($this, $this->data[0]);
  77. }
  78. function get_event_link()
  79. {
  80. return $this->feed->get_event_link($this, $this->data[0]);
  81. }
  82. function get_timesince()
  83. {
  84. return $this->lifestream->timesince($this->timestamp);
  85. }
  86. function get_date()
  87. {
  88. return $this->date + LIFESTREAM_DATE_OFFSET;
  89. }
  90. /**
  91. * Returns an HTML-ready string.
  92. */
  93. function render($options=array())
  94. {
  95. return $this->feed->render($this, $options);
  96. }
  97. function get_label_instance($options=array())
  98. {
  99. if (!isset($this->_label_instance))
  100. {
  101. $this->_label_instance = $this->feed->get_label($this, $options);
  102. }
  103. return $this->_label_instance;
  104. }
  105. function get_label($options=array())
  106. {
  107. $label_inst = $this->get_label_instance($options);
  108. if (count($this->data) > 1)
  109. {
  110. if (@$options['show_owners'] || $this->lifestream->get_option('show_owners'))
  111. {
  112. $label = $label_inst->get_label_plural_user();
  113. }
  114. else
  115. {
  116. $label = $label_inst->get_label_plural();
  117. }
  118. }
  119. else
  120. {
  121. if (@$options['show_owners'] || $this->lifestream->get_option('show_owners'))
  122. {
  123. $label = $label_inst->get_label_single_user();
  124. }
  125. else
  126. {
  127. $label = $label_inst->get_label_single();
  128. }
  129. }
  130. return $label;
  131. }
  132. function get_feed_label($options=array())
  133. {
  134. $label_inst = $this->get_label_instance($options);
  135. return $label_inst->get_feed_label();
  136. }
  137. function get_url()
  138. {
  139. if (count($this->data) > 1)
  140. {
  141. // return the public url if it's grouped
  142. $url = $this->feed->get_public_url();
  143. if ($url) return $url;
  144. }
  145. else
  146. {
  147. $url = $this->data[0]['link'];
  148. if ($url) return $url;
  149. }
  150. return '#';
  151. }
  152. }
  153. class Lifestream_EventGroup extends Lifestream_Event
  154. {
  155. /**
  156. * Represents a grouped event in the database.
  157. */
  158. function __construct(&$lifestream, $row)
  159. {
  160. parent::__construct($lifestream, $row);
  161. $this->total = $row->total ? $row->total : 1;
  162. $this->data = unserialize($row->data);
  163. $this->is_grouped = true;
  164. }
  165. function get_event_display($bit)
  166. {
  167. return $this->feed->get_event_display($this, $bit);
  168. }
  169. function get_event_link($bit)
  170. {
  171. return $this->feed->get_event_link($this, $bit);
  172. }
  173. }
  174. class Lifestream
  175. {
  176. // stores all registered feeds
  177. public $feeds = array();
  178. // stores file locations to feed classes
  179. public $paths = array();
  180. // stores theme information
  181. public $themes = array();
  182. // stores icon folder names
  183. public $icons = array();
  184. // current theme
  185. public $theme = 'default';
  186. protected $paging_key = 'ls_p';
  187. protected $valid_image_types = array('image/gif' => 'gif',
  188. 'image/jpeg' => 'jpeg',
  189. 'image/png' => 'png',
  190. 'image/gif' => 'gif',
  191. 'image/x-icon' => 'ico',
  192. 'image/bmp' => 'bmp',
  193. 'image/vnd.microsoft.icon' => 'ico'
  194. );
  195. protected $valid_image_extensions = array(
  196. 'gif', 'jpg', 'jpeg', 'gif', 'png', 'ico'
  197. );
  198. function html_entity_decode($string)
  199. {
  200. static $trans_tbl;
  201. // replace numeric entities
  202. $string = preg_replace('~&#x([0-9a-f]+);~ei', 'lifestream_code2utf(hexdec("\\1"))', $string);
  203. $string = preg_replace('~&#([0-9]+);~e', 'lifestream_code2utf(\\1)', $string);
  204. // replace literal entities
  205. if (!isset($trans_tbl))
  206. {
  207. $trans_tbl = array();
  208. foreach (get_html_translation_table(HTML_ENTITIES) as $val=>$key)
  209. $trans_tbl[$key] = utf8_encode($val);
  210. }
  211. return strtr($string, $trans_tbl);
  212. }
  213. // function html_entity_decode($string)
  214. // {
  215. // $string = html_entity_decode($string, ENT_QUOTES, 'utf-8');
  216. //
  217. // $string = preg_replace('~&#x0*([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);
  218. // $string = preg_replace('~&#0*([0-9]+);~e', 'chr(\\1)', $string);
  219. //
  220. // return $string;
  221. // }
  222. function parse_nfo_file($file)
  223. {
  224. $data = array();
  225. if (!is_file($file)) return $data;
  226. $fp = file($file);
  227. foreach ($fp as $line)
  228. {
  229. if (lifestream_str_startswith('#', $line)) continue;
  230. list($key, $value) = explode(':', $line, 2);
  231. $data[strtolower($key)] = trim($value);
  232. }
  233. return $data;
  234. }
  235. function get_icon_paths()
  236. {
  237. $directories = array(
  238. lifestream_path_join(LIFESTREAM_PATH, 'icons')
  239. );
  240. if ($this->get_option('icon_dir') && $this->get_option('icon_dir') != $directories[0]) {
  241. $directories[] = $this->get_option('icon_dir');
  242. }
  243. return $directories;
  244. }
  245. function get_rss_feed_url()
  246. {
  247. $permalink = get_option('permalink_structure');
  248. if (!empty($permalink))
  249. {
  250. $url = trailingslashit(get_bloginfo('rss2_url')) . 'lifestream-feed';
  251. }
  252. else {
  253. $url = trailingslashit(get_bloginfo('url')) . 'wp-rss2.php?feed=lifestream-feed';
  254. }
  255. return $url;
  256. }
  257. /**
  258. * Find each icons/name/generic.png file.
  259. */
  260. function detect_icons()
  261. {
  262. $directories = $this->get_icon_paths();
  263. foreach ($directories as $base_dir)
  264. {
  265. if (!is_dir($base_dir)) continue;
  266. $handler = opendir($base_dir);
  267. while ($file = readdir($handler))
  268. {
  269. // ignore hidden files
  270. if (lifestream_str_startswith($file, '.')) continue;
  271. // if its not a directory we dont care
  272. $path = lifestream_path_join($base_dir, $file);
  273. if (!is_dir($path)) continue;
  274. $ext_file = lifestream_path_join($path, 'generic.png');
  275. if (is_file($ext_file))
  276. {
  277. $data = $this->parse_nfo_file(lifestream_path_join($path, 'icons.txt'));
  278. if (!$data['name']) $data['name'] = $file;
  279. $data['__path'] = $path;
  280. $data['__url'] =
  281. $this->icons[$file] = $data;
  282. }
  283. }
  284. }
  285. }
  286. function get_extension_paths()
  287. {
  288. $directories = array(
  289. lifestream_path_join(LIFESTREAM_PATH, 'extensions')
  290. );
  291. if ($this->get_option('extension_dir') && $this->get_option('extension_dir') != $directories[0]) {
  292. $directories[] = $this->get_option('extension_dir');
  293. }
  294. return $directories;
  295. }
  296. /**
  297. * Find each extension/name/extension.inc.php file.
  298. */
  299. function detect_extensions()
  300. {
  301. $lifestream =& $this;
  302. $directories = $this->get_extension_paths();
  303. foreach ($directories as $base_dir)
  304. {
  305. if (!is_dir($base_dir)) continue;
  306. $handler = opendir($base_dir);
  307. while ($file = readdir($handler))
  308. {
  309. // ignore hidden files
  310. if (lifestream_str_startswith($file, '.')) continue;
  311. $path = lifestream_path_join($base_dir, $file);
  312. // if its not a directory we dont care
  313. if (!is_dir($path)) continue;
  314. // check for extension.inc.php
  315. $ext_file = lifestream_path_join($path, 'extension.inc.php');
  316. if (is_file($ext_file))
  317. {
  318. include($ext_file);
  319. }
  320. }
  321. }
  322. }
  323. function get_theme_paths()
  324. {
  325. $directories = array(
  326. lifestream_path_join(LIFESTREAM_PATH, 'themes')
  327. );
  328. if ($this->get_option('theme_dir') && $this->get_option('theme_dir') != $directories[0]) {
  329. $directories[] = $this->get_option('theme_dir');
  330. }
  331. return $directories;
  332. }
  333. /**
  334. * Find each themes/name/theme.txt file.
  335. */
  336. function detect_themes()
  337. {
  338. $directories = $this->get_theme_paths();
  339. foreach ($directories as $base_dir)
  340. {
  341. if (!is_dir($base_dir)) continue;
  342. $handler = opendir($base_dir);
  343. while ($file = readdir($handler))
  344. {
  345. // ignore hidden files
  346. if (lifestream_str_startswith($file, '.')) continue;
  347. // if its not a directory we dont care
  348. $path = lifestream_path_join($base_dir, $file);
  349. if (!is_dir($path)) continue;
  350. // check for main.inc.php
  351. $ext_file = lifestream_path_join($path, 'theme.txt');
  352. if (is_file($ext_file))
  353. {
  354. $theme = array();
  355. $theme = $this->parse_nfo_file($ext_file);
  356. $theme['__path'] = $path;
  357. if (!array_key_exists('name', $theme)) continue;
  358. $this->themes[$file] = $theme;
  359. }
  360. }
  361. }
  362. }
  363. function get_media_url_for_icon($filename='generic.png', $iconpack='default')
  364. {
  365. $path = lifestream_path_join($this->icons[$iconpack]['__path'], $filename);
  366. if (!is_file($path))
  367. {
  368. $filename = 'generic.png';
  369. $path = lifestream_path_join(LIFESTREAM_PATH, 'icons', 'default', $filename);
  370. }
  371. return $this->get_absolute_media_url($path);
  372. }
  373. function get_icon_media_url($filename)
  374. {
  375. return $this->get_media_url_for_icon($filename, $this->get_option('icons', 'default'));
  376. }
  377. function get_theme_media_url($filename)
  378. {
  379. return $this->get_media_url_for_theme($filename, $this->get_option('theme', 'default'));
  380. }
  381. function get_media_url_for_theme($filename, $theme='default')
  382. {
  383. // base dir is now $theme['__path'] so we must abstract the web dir
  384. $path = lifestream_path_join($this->themes[$theme]['__path'], 'media', $filename);
  385. if (!is_file($path))
  386. {
  387. $path = lifestream_path_join(LIFESTREAM_PATH, 'themes', 'default', 'media', $filename);
  388. }
  389. return $this->get_absolute_media_url($path);
  390. }
  391. function get_absolute_media_url($path)
  392. {
  393. $path = str_replace(trailingslashit(WP_CONTENT_DIR), '', $path);
  394. $path = str_replace(realpath(trailingslashit(LIFESTREAM_PATH)), 'plugins/lifestream', $path);
  395. return str_replace('\\', '/', trailingslashit(WP_CONTENT_URL).$path);
  396. }
  397. function get_theme_filepath($filename)
  398. {
  399. $path = $this->get_filepath_for_theme($filename, $this->get_option('theme', 'default'));
  400. if (!is_file($path))
  401. {
  402. $path = $this->get_filepath_for_theme($filename, 'default');
  403. }
  404. return $path;
  405. }
  406. function get_filepath_for_theme($filename, $theme='default')
  407. {
  408. if (!array_key_exists($theme, $this->themes))
  409. {
  410. throw new Exception('Theme is not valid.');
  411. }
  412. return lifestream_path_join($this->themes[$theme]['__path'], $filename);
  413. }
  414. function validate_image($url)
  415. {
  416. // // Check the extension
  417. // $bits = explode('.', basename($url));
  418. // if (count($bits) > 1)
  419. // {
  420. // $ext = $bits[count($bits)-1];
  421. // return (in_array($ext, $this->valid_image_extensions));
  422. // }
  423. $handler = $this->get_option('url_handler');
  424. $use_fsock = true;
  425. if (($handler == 'auto' && function_exists('curl_init')) || $handler == 'curl')
  426. {
  427. $use_fsock = false;
  428. }
  429. $file = new SimplePie_File($url, 10, 5, null, SIMPLEPIE_USERAGENT, $use_fsock);
  430. if (!$file->success)
  431. {
  432. return false;
  433. }
  434. // Attempt to check content type
  435. if (!empty($file->headers['content-type']))
  436. {
  437. return (in_array($file->headers['content-type'], $this->valid_image_types));
  438. }
  439. // Use GD if we can
  440. if (function_exists('imagecreatefromstring'))
  441. {
  442. return (imagecreatefromstring($file->body) !== false);
  443. }
  444. // Everything has failed, we'll just let it pass
  445. return true;
  446. }
  447. // options and their default values
  448. protected $_options = array(
  449. 'day_format' => 'F jS',
  450. 'hour_format' => 'g:ia',
  451. 'number_of_items' => '50',
  452. 'date_interval' => '1 month',
  453. 'digest_title' => 'Daily Digest for %s',
  454. 'digest_body' => '%1$s',
  455. 'digest_category' => '1',
  456. 'digest_author' => '1',
  457. 'daily_digest' => '0',
  458. 'digest_interval' => 'daily',
  459. 'digest_time' => '0',
  460. 'update_interval' => '15',
  461. 'show_owners' => '0',
  462. 'use_ibox' => '1',
  463. 'show_credits' => '1',
  464. 'hide_details_default' => '1',
  465. 'url_handler' => 'auto',
  466. 'feed_items' => '10',
  467. 'truncate_length' => '128',
  468. 'theme' => 'default',
  469. 'icons' => 'default',
  470. 'extension_dir' => '',
  471. 'theme_dir' => '',
  472. 'icon_dir' => '',
  473. 'links_new_windows' => '0',
  474. 'truncate_interval' => '0',
  475. 'page_id' => '',
  476. );
  477. function __construct()
  478. {
  479. $this->path = WP_CONTENT_URL . '/plugins/lifestream';
  480. $this->_optioncache = null;
  481. add_action('admin_menu', array(&$this, 'options_menu'));
  482. add_action('wp_head', array(&$this, 'header'));
  483. add_filter('the_content', array(&$this, 'embed_callback'));
  484. add_action('init', array(&$this, 'init'));
  485. add_filter('cron_schedules', array(&$this, 'get_cron_schedules'));
  486. add_action('lifestream_digest_cron', array(&$this, 'digest_update'));
  487. add_action('lifestream_cron', array(&$this, 'update'));
  488. add_action('lifestream_cleanup', array(&$this, 'cleanup_history'));
  489. add_action('template_redirect', array($this, 'template_redirect'));
  490. register_post_type('lsevent', array('public' => false));
  491. register_activation_hook(LIFESTREAM_PLUGIN_FILE, array(&$this, 'activate'));
  492. register_deactivation_hook(LIFESTREAM_PLUGIN_FILE, array(&$this, 'deactivate'));
  493. }
  494. function truncate($string, $length=128)
  495. {
  496. if (!($length > 0)) return $string;
  497. if (strlen($string) > $length)
  498. {
  499. $string = substr($string, 0, $length-3).'...';
  500. }
  501. return $string;
  502. }
  503. // To be quite honest, WordPress should be doing this kind of magic itself.
  504. function _populate_option_cache()
  505. {
  506. if (!$this->_optioncache)
  507. {
  508. $this->_optioncache = (array)get_option('lifestream_options');
  509. if (!$this->_optioncache) $this->_optioncache = (array)$this->_options;
  510. }
  511. }
  512. /**
  513. * Fetches the value of an option. Returns `null` if the option is not set.
  514. */
  515. function get_option($option, $default=null)
  516. {
  517. $this->_populate_option_cache();
  518. if (!isset($this->_optioncache[$option])) $value = $default;
  519. else
  520. {
  521. $value = $this->_optioncache[$option];
  522. }
  523. if (empty($value)) $value = $default;
  524. return $value;
  525. }
  526. /**
  527. * Removes an option.
  528. */
  529. function delete_option($option)
  530. {
  531. $this->_populate_option_cache();
  532. unset($this->_optioncache[$option]);
  533. update_option('lifestream_options', $this->_optioncache);
  534. }
  535. /**
  536. * Updates the value of an option.
  537. */
  538. function update_option($option, $value)
  539. {
  540. $this->_populate_option_cache();
  541. $this->_optioncache[$option] = $value;
  542. update_option('lifestream_options', $this->_optioncache);
  543. }
  544. /**
  545. * Sets an option if it doesn't exist.
  546. */
  547. function add_option($option, $value)
  548. {
  549. $this->_populate_option_cache();
  550. if (!array_key_exists($option, $this->_optioncache) || $this->_optioncache[$option] === '')
  551. {
  552. $this->_optioncache[$option] = $value;
  553. update_option('lifestream_options', $this->_optioncache);
  554. }
  555. }
  556. function __($text, $params=null)
  557. {
  558. if (!is_array($params))
  559. {
  560. $params = func_get_args();
  561. $params = array_slice($params, 1);
  562. }
  563. return vsprintf(__($text, 'lifestream'), $params);
  564. }
  565. function _e($text, $params=null)
  566. {
  567. if (!is_array($params))
  568. {
  569. $params = func_get_args();
  570. $params = array_slice($params, 1);
  571. }
  572. echo vsprintf(__($text, 'lifestream'), $params);
  573. }
  574. function init()
  575. {
  576. global $wpdb;
  577. $offset = get_option('gmt_offset') * 3600;
  578. define('LIFESTREAM_DATE_OFFSET', $offset);
  579. load_plugin_textdomain('lifestream', false, 'lifestream/locales');
  580. $page = (isset($_GET['page']) ? $_GET['page'] : null);
  581. if (is_admin() && lifestream_str_startswith($page, 'lifestream'))
  582. {
  583. wp_enqueue_script('jquery');
  584. wp_enqueue_script('admin-forms');
  585. }
  586. add_feed('lifestream-feed', 'lifestream_rss_feed');
  587. $this->is_buddypress = (function_exists('bp_is_blog_page') ? true : false);
  588. // If this is an update we need to force reactivation
  589. if (LIFESTREAM_VERSION != $this->get_option('_version'))
  590. {
  591. $this->get_option('_version');
  592. $this->deactivate();
  593. $this->activate();
  594. }
  595. }
  596. function is_lifestream_event()
  597. {
  598. global $posts, $post, $wp_query;
  599. if (!$posts)
  600. {
  601. $posts = array(get_post($_GET['p'], OBJECT));
  602. $post = $posts[0];
  603. $wp_query->queried_object = $posts[0];
  604. $wp_query->queried_object_id = $posts[0]->ID;
  605. }
  606. return (is_single() && get_post_type() == 'lsevent');
  607. }
  608. function is_lifestream_home()
  609. {
  610. global $wp_query, $post;
  611. return (is_page() && $post->ID == $this->get_option('page_id'));
  612. }
  613. function template_redirect()
  614. {
  615. global $ls_template;
  616. $lifestream = $this;
  617. if ($this->is_lifestream_event())
  618. {
  619. $ls_template->get_events();
  620. include($this->get_template('event.php'));
  621. exit;
  622. }
  623. else if ($this->is_lifestream_home())
  624. {
  625. $ls_template->get_events();
  626. include($this->get_template('home.php'));
  627. exit;
  628. }
  629. }
  630. function get_template($template)
  631. {
  632. if (file_exists(TEMPLATEPATH.'/lifestream/'.$template))
  633. {
  634. return TEMPLATEPATH.'/lifestream/'.$template;
  635. }
  636. return LIFESTREAM_PATH . '/templates/'.$template;
  637. }
  638. function log_error($message, $feed_id=null)
  639. {
  640. global $wpdb;
  641. if ($feed_id)
  642. {
  643. $result = $wpdb->query($wpdb->prepare("INSERT INTO `".$wpdb->prefix."lifestream_error_log` (`feed_id`, `message`, `timestamp`) VALUES (%s, %s, %d)", $wpdb->escape($feed_id), $wpdb->escape($message), time()));
  644. }
  645. else
  646. {
  647. $result = $wpdb->query($wpdb->prepare("INSERT INTO `".$wpdb->prefix."lifestream_error_log` (`feed_id`, `message`, `timestamp`) VALUES (NULL, %s, %d)", $wpdb->escape($message), time()));
  648. }
  649. }
  650. function get_anchor_html($label, $href, $attrs=array())
  651. {
  652. // TODO: this might need to be optimized as string management is typically slow
  653. if ($this->get_option('links_new_windows') && empty($attrs['target']))
  654. {
  655. $attrs['target'] = '_blank';
  656. }
  657. $attrs['href'] = $href;
  658. $html = '<a';
  659. foreach ($attrs as $key=>$value)
  660. {
  661. $html .= ' '.$key.'="'.$value.'"';
  662. }
  663. $html .= '>'.$label.'</a>';
  664. return $html;
  665. }
  666. function get_digest_interval()
  667. {
  668. $interval = $this->get_option('digest_interval');
  669. switch ($interval)
  670. {
  671. case 'weekly':
  672. return 3600*24*7;
  673. case 'daily':
  674. return 3600*24;
  675. case 'hourly':
  676. return 3600;
  677. }
  678. }
  679. function get_cron_schedules($cron)
  680. {
  681. $interval = (int)$this->get_option('update_interval', 15);
  682. if (!($interval > 0)) $interval = 15;
  683. $cron['lifestream'] = array(
  684. 'interval' => $interval * 60,
  685. 'display' => $this->__('On Lifestream update')
  686. );
  687. $cron['lifestream_digest'] = array(
  688. 'interval' => (int)$this->get_digest_interval(),
  689. 'display' => $this->__('On Lifestream daily digest update')
  690. );
  691. return $cron;
  692. }
  693. function get_single_event($feed_type)
  694. {
  695. $events = $this->get_events(array('feed_types'=>array($feed_type), 'limit'=>1, 'break_groups'=>true));
  696. $event = $events[0];
  697. return $event;
  698. }
  699. function generate_unique_id()
  700. {
  701. return uniqid('ls_', true);
  702. }
  703. function digest_update()
  704. {
  705. global $wpdb;
  706. if ($this->get_option('daily_digest') != '1') return;
  707. $interval = $this->get_digest_interval();
  708. $options = array(
  709. 'id' => $this->generate_unique_id(),
  710. );
  711. $now = time();
  712. // If there was a previous digest, we show only events since it
  713. $from = $this->get_option('_last_digest');
  714. // Otherwise we show events within the interval period
  715. if (!$from) $from = $now - $interval;
  716. // make sure the post doesn't exist
  717. $results = $wpdb->get_results($wpdb->prepare("SELECT `post_id` FROM `".$wpdb->prefix."postmeta` WHERE `meta_key` = '_lifestream_digest_date' AND `meta_value` = %d LIMIT 0, 1", $now));
  718. if ($results) continue;
  719. $sql = $wpdb->prepare("SELECT t1.*, t2.`options` FROM `".$wpdb->prefix."lifestream_event_group` as `t1` INNER JOIN `".$wpdb->prefix."lifestream_feeds` as t2 ON t1.`feed_id` = t2.`id` WHERE t1.`timestamp` > %s AND t1.`timestamp` < %s ORDER BY t1.`timestamp` ASC", $from, $now);
  720. $results =& $wpdb->get_results($sql);
  721. $events = array();
  722. foreach ($results as &$result)
  723. {
  724. $events[] = new Lifestream_EventGroup($this, $result);
  725. }
  726. if (count($events))
  727. {
  728. ob_start();
  729. if (!include($this->get_theme_filepath('digest.inc.php'))) return;
  730. $content = sprintf($this->get_option('digest_body'), ob_get_clean(), date($this->get_option('day_format'), $now), count($events));
  731. $data = array(
  732. 'post_content' => $wpdb->escape($content),
  733. 'post_title' => $wpdb->escape(sprintf($this->get_option('digest_title'), date($this->get_option('day_format'), $now), date($this->get_option('hour_format'), $now))),
  734. 'post_date' => date('Y-m-d H:i:s', $now),
  735. 'post_category' => array($this->get_option('digest_category')),
  736. 'post_status' => 'publish',
  737. 'post_author' => $wpdb->escape($this->get_option('digest_author')),
  738. );
  739. $post_id = wp_insert_post($data);
  740. add_post_meta($post_id, '_lifestream_digest_date', $now, true);
  741. }
  742. $this->update_option('_last_digest', $now);
  743. }
  744. // page output
  745. function options_menu()
  746. {
  747. global $wpdb;
  748. wp_enqueue_script('postbox');
  749. if (function_exists('add_menu_page'))
  750. {
  751. $basename = basename(LIFESTREAM_PLUGIN_FILE);
  752. $results =& $wpdb->get_results("SELECT COUNT(*) as `count` FROM `".$wpdb->prefix."lifestream_error_log` WHERE has_viewed = 0");
  753. $errors = $results[0]->count;
  754. add_menu_page('Lifestream', 'Lifestream', 'edit_posts', $basename, array(&$this, 'options_page'));
  755. add_submenu_page($basename, $this->__('Lifestream Feeds'), $this->__('Feeds'), 'level_1', $basename, array(&$this, 'options_page'));
  756. add_submenu_page($basename, $this->__('Lifestream Events'), $this->__('Events'), 'edit_posts', 'lifestream-events.php', array(&$this, 'options_page'));
  757. add_submenu_page($basename, $this->__('Lifestream Settings'), $this->__('Settings'), 'manage_options', 'lifestream-settings.php', array(&$this, 'options_page'));
  758. add_submenu_page($basename, $this->__('Lifestream Change Log'), $this->__('Change Log'), 'edit_posts', 'lifestream-changelog.php', array(&$this, 'options_page'));
  759. add_submenu_page($basename, $this->__('Lifestream Errors'), $this->__('Errors (%d)', $errors), 'edit_posts', 'lifestream-errors.php', array(&$this, 'options_page'));
  760. add_submenu_page($basename, $this->__('Lifestream Maintenance'), $this->__('Maintenance / Debug', $errors), 'manage_options', 'lifestream-maintenance.php', array(&$this, 'options_page'));
  761. add_submenu_page($basename, $this->__('Lifestream Support Forums'), $this->__('Support Forums'), 'edit_posts', 'lifestream-forums.php', array(&$this, 'options_page'));
  762. }
  763. }
  764. function header()
  765. {
  766. echo '<script type="text/javascript" src="'.$this->path.'/lifestream.js"></script>';
  767. echo '<link rel="stylesheet" type="text/css" media="screen" href="'.$this->get_theme_media_url('lifestream.css').'"/>';
  768. }
  769. function options_page()
  770. {
  771. global $wpdb, $userdata;
  772. $wpdb->show_errors();
  773. $this->install();
  774. get_currentuserinfo();
  775. $date_format = sprintf('%s @ %s', $this->get_option('day_format'), $this->get_option('hour_format'));
  776. $basename = basename(LIFESTREAM_PLUGIN_FILE);
  777. $errors = array();
  778. $message = null;
  779. switch ($_GET['page'])
  780. {
  781. case 'lifestream-maintenance.php':
  782. if (@$_POST['resetcron'])
  783. {
  784. $this->reschedule_cron();
  785. $message = $this->__('Cron timers have been reset.');
  786. }
  787. elseif (@$_POST['restore'])
  788. {
  789. $this->restore_options();
  790. $message = $this->__('Default options have been restored.');
  791. }
  792. elseif (@$_POST['restoredb'])
  793. {
  794. $this->restore_database();
  795. $message = $this->__('Default database has been restored.');
  796. }
  797. elseif (@$_POST['fixposts'])
  798. {
  799. $new_posts = $this->upgrade_posts_to_events();
  800. $message = $this->__('There were %d new posts which had to be created.', $new_posts);
  801. }
  802. elseif (@$_POST['cleanupposts'])
  803. {
  804. $affected = $this->safe_query($wpdb->prepare("DELETE FROM `".$wpdb->prefix."posts` WHERE `post_type` = 'lsevent' AND `ID` NOT IN (SELECT `post_id` FROM `".$wpdb->prefix."lifestream_event_group` WHERE `post_id` != 0)"));
  805. $message = $this->__('There were %d unused posts which have been removed.', $affected);
  806. }
  807. break;
  808. case 'lifestream-events.php':
  809. switch ((isset($_REQUEST['op']) ? strtolower($_REQUEST['op']) : null))
  810. {
  811. case 'delete':
  812. if (!($ids = $_REQUEST['id'])) break;
  813. if (!is_array($ids)) $ids = array($ids);
  814. foreach ($ids as $id)
  815. {
  816. $result =& $wpdb->get_results($wpdb->prepare("SELECT `id`, `feed_id`, `timestamp`, `owner_id` FROM `".$wpdb->prefix."lifestream_event` WHERE `id` = %d", $id));
  817. if (!$result)
  818. {
  819. $errors[] = $this->__('The selected feed was not found.');
  820. }
  821. elseif (!current_user_can('manage_options') && $result[0]->owner_id != $userdata->ID)
  822. {
  823. $errors[] = $this->__('You do not have permission to do that.');
  824. }
  825. else
  826. {
  827. $result =& $result[0];
  828. $wpdb->query($wpdb->prepare("UPDATE `".$wpdb->prefix."lifestream_event` SET `visible` = 0 WHERE `id` = %d", $result->id));
  829. $wpdb->query($wpdb->prepare("UPDATE `".$wpdb->prefix."lifestream_event_group` SET `visible` = 0 WHERE `event_id` = %d", $result->id));
  830. // Now we have to update the batch if it exists.
  831. $group =& $wpdb->get_results($wpdb->prepare("SELECT `id` FROM `".$wpdb->prefix."lifestream_event_group` WHERE `event_id` IS NULL AND DATE(FROM_UNIXTIME(`timestamp`)) = DATE(FROM_UNIXTIME(%d)) AND `feed_id` = %d LIMIT 0, 1", $result->timestamp, $result->feed_id));
  832. if (count($group) == 1)
  833. {
  834. $group =& $group[0];
  835. $results =& $wpdb->get_results($wpdb->prepare("SELECT `data`, `link` FROM `".$wpdb->prefix."lifestream_event` WHERE `feed_id` = %d AND `visible` = 1 AND DATE(FROM_UNIXTIME(`timestamp`)) = DATE(FROM_UNIXTIME(%d))", $result->feed_id, $result->timestamp));
  836. if (count($results))
  837. {
  838. $events = array();
  839. foreach ($results as &$result)
  840. {
  841. $result->data = unserialize($result->data);
  842. $result->data['link'] = $result->link;
  843. $events[] = $result->data;
  844. }
  845. $wpdb->query($wpdb->prepare("UPDATE `".$wpdb->prefix."lifestream_event_group` SET `data` = %s, `total` = %d, `updated` = 1 WHERE `id` = %d", $wpdb->escape(serialize($events)), count($events), $group->id));
  846. }
  847. else
  848. {
  849. $this->safe_query($wpdb->prepare("DELETE FROM `".$wpdb->prefix."posts` WHERE `post_type` = 'lsevent' AND `ID` = %d", $group->post_id));
  850. $wpdb->query($wpdb->prepare("DELETE FROM `".$wpdb->prefix."lifestream_event_group` WHERE `id` = %d", $group->id));
  851. }
  852. }
  853. else
  854. {
  855. $this->safe_query($wpdb->prepare("DELETE FROM `".$wpdb->prefix."posts` WHERE `post_type` = 'lsevent' AND `ID` = %d", $result->post_id));
  856. $wpdb->query($wpdb->prepare("DELETE FROM `".$wpdb->prefix."lifestream_event_group` WHERE `event_id` = %d", $result->id));
  857. }
  858. }
  859. $message = $this->__('The selected events were hidden.');
  860. }
  861. break;
  862. }
  863. break;
  864. case 'lifestream-settings.php':
  865. if (!empty($_POST['save']))
  866. {
  867. foreach (array_keys($this->_options) as $value)
  868. {
  869. $this->update_option($value, (isset($_POST['lifestream_'.$value]) ? stripslashes($_POST['lifestream_'.$value]) : '0'));
  870. }
  871. // We need to make sure the cron runs now
  872. $this->reschedule_cron();
  873. }
  874. break;
  875. default:
  876. $feedmsgs = array();
  877. switch ((isset($_REQUEST['op']) ? strtolower($_REQUEST['op']) : null))
  878. {
  879. case 'refreshall':
  880. $results = $this->update_all($userdata->ID);
  881. foreach ($results as $id=>$result)
  882. {
  883. if (is_int($result)) $feedmsgs[$id] = $result;
  884. else $errors[] = $this->__('There was an error refreshing the selected feed: ID %s', $id);
  885. }
  886. $message = $this->__('All of your feeds have been refreshed.');
  887. break;
  888. case 'refresh':
  889. if (!$_REQUEST['id']) break;
  890. foreach ($_REQUEST['id'] as $id)
  891. {
  892. $result =& $wpdb->get_results($wpdb->prepare("SELECT * FROM `".$wpdb->prefix."lifestream_feeds` WHERE `id` = %d LIMIT 0, 1", $id));
  893. if (!$result)
  894. {
  895. $errors[] = $this->__('The selected feed was not found.');
  896. }
  897. elseif (!current_user_can('manage_options') && $result[0]->owner_id != $userdata->ID)
  898. {
  899. $errors[] = $this->__('You do not have permission to do that.');
  900. }
  901. else
  902. {
  903. $instance = Lifestream_Feed::construct_from_query_result($this, $result[0]);
  904. $msg_arr = $instance->refresh();
  905. if ($msg_arr[0] !== false)
  906. {
  907. $message = $this->__('The selected feeds and their events have been refreshed.');
  908. $feedmsgs[$instance->id] = $msg_arr[1];
  909. }
  910. else
  911. {
  912. $errors[] = $this->__('There was an error refreshing the selected feed: ID %s', $instance->id);
  913. }
  914. }
  915. }
  916. break;
  917. case 'pause':
  918. if (!$_REQUEST['id']) break;
  919. $ids = array();
  920. foreach ($_REQUEST['id'] as $id)
  921. {
  922. $ids[] = (int)$id;
  923. }
  924. if (!empty($ids))
  925. {
  926. if (current_user_can('manage_options'))
  927. {
  928. $wpdb->query($wpdb->prepare("UPDATE `".$wpdb->prefix."lifestream_feeds` SET `active` = 0 WHERE `id` IN ('%s')", implode("','", $ids)));
  929. }
  930. else
  931. {
  932. $wpdb->query($wpdb->prepare("UPDATE `".$wpdb->prefix."lifestream_feeds` SET `active` = 1 WHERE `id` IN ('%s') AND `owner_id` = %s", implode("','", $ids), $userdata->ID));
  933. }
  934. $message = $this->__('The selected feeds have been paused, and events will not be refreshed.');
  935. }
  936. break;
  937. case 'unpause':
  938. if (!$_REQUEST['id']) break;
  939. $ids = array();
  940. foreach ($_REQUEST['id'] as $id)
  941. {
  942. $ids[] = (int)$id;
  943. }
  944. if (!empty($ids))
  945. {
  946. if (current_user_can('manage_options'))
  947. {
  948. $wpdb->query($wpdb->prepare("UPDATE `".$wpdb->prefix."lifestream_feeds` SET `active` = 1 WHERE `id` IN ('%s')", implode("','", $ids)));
  949. }
  950. else
  951. {
  952. $wpdb->query($wpdb->prepare("UPDATE `".$wpdb->prefix."lifestream_feeds` SET `active` = 0 WHERE `id` IN ('%s') AND `owner_id` = %s", implode("','", $ids), $userdata->ID));
  953. }
  954. $message = $this->__('The selected feeds have been unpaused, and events will now be refreshed.');
  955. }
  956. break;
  957. case 'delete':
  958. if (!$_REQUEST['id']) break;
  959. foreach ($_REQUEST['id'] as $id)
  960. {
  961. $result =& $wpdb->get_results($wpdb->prepare("SELECT * FROM `".$wpdb->prefix."lifestream_feeds` WHERE `id` = %d LIMIT 0, 1", $id));
  962. if (!$result)
  963. {
  964. $errors[] = $this->__('The selected feed was not found.');
  965. }
  966. elseif (!current_user_can('manage_options') && $result[0]->owner_id != $userdata->ID)
  967. {
  968. $errors[] = $this->__('You do not have permission to do that.');
  969. }
  970. else
  971. {
  972. $instance = Lifestream_Feed::construct_from_query_result($this, $result[0]);
  973. $instance->delete();
  974. $message = $this->__('The selected feeds and all related events has been removed.');
  975. }
  976. }
  977. break;
  978. case 'edit':
  979. $result =& $wpdb->get_results($wpdb->prepare("SELECT * FROM `".$wpdb->prefix."lifestream_feeds` WHERE `id` = %d LIMIT 0, 1", $_GET['id']));
  980. if (!$result)
  981. {
  982. $errors[] = $this->__('The selected feed was not found.');
  983. }
  984. elseif (!current_user_can('manage_options') && $result[0]->owner_id != $userdata->ID)
  985. {
  986. $errors[] = $this->__('You do not have permission to do that.');
  987. }
  988. else
  989. {
  990. $instance = Lifestream_Feed::construct_from_query_result($this, $result[0]);
  991. $options = $instance->get_options();
  992. if (@$_POST['save'])
  993. {
  994. $values = array();
  995. foreach ($options as $option=>$option_meta)
  996. {
  997. if ($option_meta[1] && !$_POST[$option])
  998. {
  999. $errors[] = $option_meta[0].' is required.';
  1000. }
  1001. else
  1002. {
  1003. $values[$option] = stripslashes($_POST[$option]);
  1004. }
  1005. }
  1006. if ($instance->get_constant('MUST_GROUP'))
  1007. {
  1008. $values['grouped'] = 1;
  1009. }
  1010. elseif ($instance->get_constant('CAN_GROUP'))
  1011. {
  1012. $values['grouped'] = $_POST['grouped'];
  1013. }
  1014. if ($instance->get_constant('HAS_EXCERPTS'))
  1015. {
  1016. $values['excerpt'] = $_POST['excerpt'];
  1017. }
  1018. $values['feed_label'] = $_POST['feed_label'];
  1019. $values['icon_url'] = $_POST['icon_url'];
  1020. $values['auto_icon'] = @$_POST['auto_icon'];
  1021. if ($_POST['owner'] != $instance->owner_id && current_user_can('manage_options'))
  1022. {
  1023. $usero = new WP_User($_POST['owner']);
  1024. $owner = $usero->data;
  1025. $instance->owner_id = $_POST['owner'];
  1026. $instance->owner = $owner->display_name;
  1027. }
  1028. if (!count($errors))
  1029. {
  1030. $instance->options = $values;
  1031. $instance->save();
  1032. unset($_POST);
  1033. }
  1034. }
  1035. elseif (@$_POST['truncate'])
  1036. {
  1037. $instance->truncate();
  1038. $instance->refresh();
  1039. }
  1040. }
  1041. break;
  1042. case 'add':
  1043. if ($_POST)
  1044. {
  1045. $class_name = $this->get_feed($_GET['feed']);
  1046. if (!$class_name) break;
  1047. $feed = new $class_name($this);
  1048. $values = array();
  1049. $options = $feed->get_options();
  1050. foreach ($options as $option=>$option_meta)
  1051. {
  1052. if ($option_meta[1] && !$_POST[$option])
  1053. {
  1054. $errors[] = $option_meta[0].' is required.';
  1055. }
  1056. else
  1057. {
  1058. $values[$option] = stripslashes($_POST[$option]);
  1059. }
  1060. }
  1061. if ($feed->get_constant('MUST_GROUP'))
  1062. {
  1063. $values['grouped'] = 1;
  1064. }
  1065. elseif ($feed->get_constant('CAN_GROUP'))
  1066. {
  1067. $values['grouped'] = @$_POST['grouped'];
  1068. }
  1069. if ($feed->get_constant('HAS_EXCERPTS'))
  1070. {
  1071. $values['excerpt'] = $_POST['excerpt'];
  1072. }
  1073. $values['feed_label'] = $_POST['feed_label'];
  1074. $values['icon_url'] = $_POST['icon_url'];
  1075. $values['auto_icon'] = @$_POST['auto_icon'];
  1076. if (current_user_can('manage_options'))
  1077. {
  1078. $feed->owner_id = $_POST['owner'];
  1079. $usero = new WP_User($feed->owner_id);
  1080. $owner = $usero->data;
  1081. $feed->owner = $owner->display_name;
  1082. }
  1083. else
  1084. {
  1085. $feed->owner_id = $userdata->ID;
  1086. $feed->owner = $userdata->display_name;
  1087. }
  1088. $feed->options = $values;
  1089. if (!count($errors))
  1090. {
  1091. if (!($error = $feed->test()))
  1092. {
  1093. $result = $feed->save();
  1094. if ($result !== false)
  1095. {
  1096. unset($_POST);
  1097. unset($_REQUEST['op']);
  1098. $msg_arr = $feed->refresh(null, true);
  1099. if ($msg_arr[0] !== false)
  1100. {
  1101. $message = $this->__('A new %s feed was added to your Lifestream.', $feed->get_constant('NAME'));
  1102. $feedmsgs[$feed->id] = $msg_arr[1];
  1103. }
  1104. }
  1105. }
  1106. else
  1107. {
  1108. $errors[] = $error;
  1109. }
  1110. }
  1111. }
  1112. break;
  1113. }
  1114. break;
  1115. }
  1116. $lifestream = &$this;
  1117. ob_start();
  1118. ?>
  1119. <style type="text/css">
  1120. .feedlist { margin: 0; padding: 0; }
  1121. .feedlist li { list-style: none; display: inline; }
  1122. .feedlist li a { float: left; display: block; padding: 2px; margin: 1px; width: 23%; text-decoration: none; }
  1123. .feedlist li a:hover { background-color: #e9e9e9; }
  1124. .success { color: #397D33; background-color: #D1FBCA; }
  1125. .error { border-color: #E25F53; color: #E25F53; }
  1126. td.icon { padding: 7px 0 9px 10px; }
  1127. </style>
  1128. <br />
  1129. <?php
  1130. if (count($errors)) { ?>
  1131. <div id="message" class="error"><p><strong><?php $this->_e('There were errors with your request:') ?></strong></p><ul>
  1132. <?php foreach ($errors as $error) { ?>
  1133. <li><?php echo nl2br(Lifestream_Feed::parse_urls(htmlspecialchars($error))); ?></li>
  1134. <?php } ?>
  1135. </ul></div>
  1136. <?php } elseif ($message) { ?>
  1137. <div id="message" class="updated fade"><p><strong><?php echo $message; ?></strong></p></div>
  1138. <?php } ?>
  1139. <div class="wrap">
  1140. <?php
  1141. switch ($_GET['page'])
  1142. {
  1143. case 'lifestream-errors.php':
  1144. $page = (!empty($_GET['paged']) ? $_GET['paged'] : 1);
  1145. switch ((isset($_REQUEST['op']) ? strtolower($_REQUEST['op']) : null))
  1146. {
  1147. case 'clear':
  1148. $wpdb->query("DELETE FROM `".$wpdb->prefix."lifestream_error_log`");
  1149. break;
  1150. }
  1151. $start = ($page-1)*LIFESTREAM_ERRORS_PER_PAGE;
  1152. $end = $page*LIFESTREAM_ERRORS_PER_PAGE;
  1153. $wpdb->query("UPDATE `".$wpdb->prefix."lifestream_error_log` SET has_viewed = 1");
  1154. $results =& $wpdb->get_results("SELECT COUNT(*) as `count` FROM `".$wpdb->prefix."lifestream_error_log`");
  1155. $number_of_pages = ceil($results[0]->count/LIFESTREAM_EVENTS_PER_PAGE);
  1156. $results =& $wpdb->get_results($wpdb->prepare("SELECT t1.*, t2.`feed`, t2.`options` FROM `".$wpdb->prefix."lifestream_error_log` as t1 LEFT JOIN `".$wpdb->prefix."lifestream_feeds` as t2 ON t1.`feed_id` = t2.`id` ORDER BY t1.`timestamp` DESC LIMIT %d, %d", $start, $end));
  1157. include(LIFESTREAM_PATH . '/pages/errors.inc.php');
  1158. break;
  1159. case 'lifestream-maintenance.php':
  1160. include(LIFESTREAM_PATH . '/pages/maintenance.inc.php');
  1161. break;
  1162. case 'lifestream-changelog.php':
  1163. include(LIFESTREAM_PATH . '/pages/changelog.inc.php');
  1164. break;
  1165. case 'lifestream-forums.php':
  1166. include(LIFESTREAM_PATH . '/pages/forums.inc.php');
  1167. break;
  1168. case 'lifestream-settings.php':
  1169. $lifestream_digest_intervals = array(
  1170. 'weekly' => $this->__('Weekly'),
  1171. 'daily' => $this->__('Daily'),
  1172. 'hourly' => $this->__('Hourly'),
  1173. );
  1174. include(LIFESTREAM_PATH . '/pages/settings.inc.php');
  1175. break;
  1176. case 'lifestream-events.php':
  1177. $page = (!empty($_GET['paged']) ? $_GET['paged'] : 1);
  1178. $start = ($page-1)*LIFESTREAM_EVENTS_PER_PAGE;
  1179. $end = $page*LIFESTREAM_EVENTS_PER_PAGE;
  1180. if (!current_user_can('manage_options'))
  1181. {
  1182. $rows =& $wpdb->get_row($wpdb->prepare("SELECT COUNT(*) as `count` FROM `".$wpdb->prefix."lifestream_event` WHERE `owner_id` = %d", $userdata->ID));
  1183. $number_of_pages = ceil($rows->count/LIFESTREAM_EVENTS_PER_PAGE);
  1184. $rows =& $wpdb->get_results($wpdb->prepare("SELECT t1.*, t2.`feed`, t2.`options` FROM `".$wpdb->prefix."lifestream_event` as t1 JOIN `".$wpdb->prefix."lifestream_feeds` as t2 ON t1.`feed_id` = t2.`id` WHERE t1.`owner_id` = %d ORDER BY t1.`timestamp` DESC LIMIT %d, %d", $userdata->ID, $start, $end));
  1185. }
  1186. else
  1187. {
  1188. $rows =& $wpdb->get_row("SELECT COUNT(*) as `count` FROM `".$wpdb->prefix."lifestream_event`");
  1189. $number_of_pages = ceil($rows->count/LIFESTREAM_EVENTS_PER_PAGE);
  1190. $rows =& $wpdb->get_results($wpdb->prepare("SELECT t1.*, t2.`feed`, t2.`options` FROM `".$wpdb->prefix."lifestream_event` as t1 JOIN `".$wpdb->prefix."lifestream_feeds` as t2 ON t1.`feed_id` = t2.`id` ORDER BY t1.`timestamp` DESC LIMIT %d, %d", $start, $end));
  1191. }
  1192. $results = array();
  1193. foreach ($rows as $result)
  1194. {
  1195. $results[] = new Lifestream_Event($lifestream, $result);
  1196. }
  1197. unset($rows);
  1198. include(LIFESTREAM_PATH . '/pages/events.inc.php');
  1199. break;
  1200. default:
  1201. switch ((isset($_REQUEST['op']) ? strtolower($_REQUEST['op']) : null))
  1202. {
  1203. case 'edit':
  1204. include(LIFESTREAM_PATH . '/pages/edit-feed.inc.php');
  1205. break;
  1206. case 'add':
  1207. $identifier = $_GET['feed'];
  1208. $class_name = $this->get_feed($identifier);
  1209. if (!$class_name) break;
  1210. $feed = new $class_name($this);
  1211. $options = $feed->get_options();
  1212. include(LIFESTREAM_PATH . '/pages/add-feed.inc.php');
  1213. break;
  1214. default:
  1215. $page = (!empty($_GET['paged']) ? $_GET['paged'] : 1);
  1216. $start = ($page-1)*LIFESTREAM_FEEDS_PER_PAGE;
  1217. $end = $page*LIFESTREAM_FEEDS_PER_PAGE;
  1218. if (!current_user_can('manage_options'))
  1219. {
  1220. $rows =& $wpdb->get_row($wpdb->prepare("SELECT COUNT(*) as `count` FROM `".$wpdb->prefix."lifestream_feeds` WHERE `owner_id` = %d", $userdata->ID));
  1221. $number_of_pages = ceil($rows->count/LIFESTREAM_FEEDS_PER_PAGE);
  1222. $rows =& $wpdb->get_results($wpdb->prepare("SELECT t1.*, (SELECT COUNT(1) FROM `".$wpdb->prefix."lifestream_event` WHERE `feed_id` = t1.`id`) as `events` FROM `".$wpdb->prefix."lifestream_feeds` as t1 WHERE t1.`owner_id` = %d ORDER BY `id` LIMIT %d, %d", $userdata->ID, $start, $end));
  1223. }
  1224. else
  1225. {
  1226. $rows =& $wpdb->get_row("SELECT COUNT(*) as `count` FROM `".$wpdb->prefix."lifestream_feeds`");
  1227. $number_of_pages = ceil($rows->count/LIFESTREAM_FEEDS_PER_PAGE);
  1228. $rows =& $wpdb->get_results($wpdb->prepare("SELECT t1.*, (SELECT COUNT(1) FROM `".$wpdb->prefix."lifestream_event` WHERE `feed_id` = t1.`id`) as `events` FROM `".$wpdb->prefix."lifestream_feeds` as t1 ORDER BY `id` LIMIT %d, %d", $start, $end));
  1229. }
  1230. $results = array();
  1231. foreach ($rows as $result)
  1232. {
  1233. $results[] = Lifestream_Feed::construct_from_query_result($this, $result);
  1234. }
  1235. if ($results !== false)
  1236. {
  1237. include(LIFESTREAM_PATH . '/pages/feeds.inc.php');
  1238. }
  1239. break;
  1240. }
  1241. break;
  1242. }
  1243. ?>
  1244. </div>
  1245. <?php
  1246. ob_end_flush();
  1247. }
  1248. /**
  1249. * Cleans up old entries based on the `truncate_interval` setting.
  1250. */
  1251. function cleanup_history()
  1252. {
  1253. $int = $this->get_option('truncate_interval');
  1254. if (!(int)$int) return;
  1255. // the value is in days
  1256. $ts = time()-(int)$int*3600*24;
  1257. $result = $wpdb->query($wpdb->prepare("DELETE FROM `".$wpdb->prefix."lifestream_event` WHERE `timestamp` < %s", $wpdb->escape($ts)));
  1258. $this->safe_query($wpdb->prepare("DELETE FROM `".$wpdb->prefix."posts` WHERE `post_type` = 'lsevent' AND `ID` IN (SELECT `post_id` FROM `".$wpdb->prefix."lifestream_event_group` WHERE `timestamp` < %s)", $wpdb->escape($ts)));
  1259. $result = $wpdb->query($wpdb->prepare("DELETE FROM `".$wpdb->prefix."lifestream_event_group` WHERE `timestamp` < %s", $wpdb->escape($ts)));
  1260. $result = $wpdb->query($wpdb->prepare("DELETE FROM `".$wpdb->prefix."lifestream_error_log` WHERE `timestamp` < %s", $wpdb->escape($ts)));
  1261. }
  1262. /**
  1263. * Attempts to update all feeds
  1264. */
  1265. function update($user_id=null)
  1266. {
  1267. $event_arr = $this->update_all($user_id);
  1268. $events = 0;
  1269. foreach ($event_arr as $instance=>$result)
  1270. {
  1271. if (is_int($result)) $events += $result;
  1272. }
  1273. return $events;
  1274. }
  1275. function update_all($user_id=null)
  1276. {
  1277. // $user_id is not implemented yet
  1278. global $wpdb;
  1279. $this->update_option('_last_update', time());
  1280. $events = array();
  1281. $results =& $wpdb->get_results("SELECT * FROM `".$wpdb->prefix."lifestream_feeds` WHERE `active` = 1");
  1282. foreach ($results as $result)
  1283. {
  1284. $instance = Lifestream_Feed::construct_from_query_result($this, $result);
  1285. try
  1286. {
  1287. $feed_msg = $instance->refresh();
  1288. $events[$instance->id] = $feed_msg[1];
  1289. }
  1290. catch (Lifestream_FeedFetchError $ex)
  1291. {
  1292. $this->log_error($ex, $instance->id);
  1293. $events[$instance->id] = $ex;
  1294. }
  1295. }
  1296. return $events;
  1297. }
  1298. /**
  1299. * Registers a feed class with Lifestream.
  1300. * @param $class_name {Class} Should extend Lifestream_Extension.
  1301. */
  1302. function register_feed($class_name)
  1303. {
  1304. $this->feeds[lifestream_get_class_constant($class_name, 'ID')] = $class_name;
  1305. // this may be the ugliest thing ever written in PHP, thank you developers!
  1306. $rcl = new ReflectionClass($class_name);
  1307. $this->paths[$class_name] = dirname($rcl->getFileName());
  1308. unset($rcl);
  1309. }
  1310. function get_feed($class_name)
  1311. {
  1312. return $this->feeds[$class_name];
  1313. }
  1314. /**
  1315. * Similar to file_get_contents but will use curl by default.
  1316. */
  1317. function file_get_contents($url)
  1318. {
  1319. $handler = $this->get_option('url_handler');
  1320. $use_fsock = true;
  1321. if (($handler == 'auto' && function_exists('curl_init')) || $handler == 'curl')
  1322. {
  1323. $use_fsock = false;
  1324. }
  1325. $file = new SimplePie_File($url, 10, 5, null, SIMPLEPIE_USERAGENT, $use_fsock);
  1326. if (!$file->success)
  1327. {
  1328. throw new Lifestream_FeedFetchError('Failed to open url: '.$url .' ('.$file->error.')');
  1329. }
  1330. return $file->body;
  1331. }
  1332. /*
  1333. * This is a wrapper function which initiates the callback for the custom tag embedding.
  1334. */
  1335. function embed_callback($content)
  1336. {
  1337. return preg_replace_callback("|\[lifestream(?:\s+([^\]]+))?\]|i", array(&$this, 'embed_handler'), $content);
  1338. return preg_replace_callback("|<\[]lifestream(?:\s+([^>\]+]))?/?[>\]]|i", array(&$this, 'embed_handler'), $content);
  1339. }
  1340. /*
  1341. * This function handles the real meat by handing off the work to helper functions.
  1342. */
  1343. function embed_handler($matches)
  1344. {
  1345. $args = array();
  1346. if (count($matches) > 1)
  1347. {
  1348. preg_match_all("|(?:([a-z_]+)=[\"']?([a-z0-9_-\s,]+)[\"']?)\s*|i", $matches[1], $options);
  1349. for ($i=0; $i<count($options[1]); $i++)
  1350. {
  1351. if ($options[$i]) $args[$options[1][$i]] = $options[2][$i];
  1352. }
  1353. }
  1354. ob_start();
  1355. if (!empty($args['feed_ids'])) $args['feed_ids'] = explode(',', $args['feed_ids']);
  1356. if (!empty($args['user_ids'])) $args['user_ids'] = explode(',', $args['user_ids']);
  1357. if (!empty($args['feed_types'])) $args['feed_types'] = explode(',', $args['feed_types']);
  1358. lifestream($args);
  1359. return ob_get_clean();
  1360. }
  1361. /**
  1362. * Returns the duration from now until timestamp.
  1363. * @param $timestamp {Integer}
  1364. * @param $granularity {Integer}
  1365. * @param $format {String} Date format.
  1366. * @return {String}
  1367. */
  1368. function timesince($timestamp, $granularity=1, $format='Y-m-d H:i:s')
  1369. {
  1370. $difference = time() - $timestamp;
  1371. if ($difference < 0) return 'just now';
  1372. elseif ($difference < 86400*2)
  1373. {
  1374. return $this->duration($difference, $granularity) . ' ago';
  1375. }
  1376. else
  1377. {
  1378. return date($this->get_option('day_format'), $timestamp);
  1379. }
  1380. }
  1381. /**
  1382. * Returns the duration from a difference.
  1383. * @param $difference {Integer}
  1384. * @param $granularity {Integer}
  1385. * @return {String}
  1386. */
  1387. function duration($difference, $granularity=2)
  1388. {
  1389. { // if difference is over 10 days show normal time form
  1390. $periods = array(
  1391. $this->__('w') => 604800,
  1392. $this->__('d') => 86400,
  1393. $this->__('h') => 3600,
  1394. $this->__('m') => 60,
  1395. $this->__('s') => 1
  1396. );
  1397. $output = '';
  1398. foreach ($periods as $key => $value)
  1399. {
  1400. if ($difference >= $value)
  1401. {
  1402. $time = round($difference / $value);
  1403. $difference %= $value;
  1404. $output .= ($output ? ' ' : '').$time.$key;
  1405. //$output .= (($time > 1 && ($key == 'week' || $key == 'day')) ? $key.'s' : $key);
  1406. $granularity--;
  1407. }
  1408. if ($granularity == 0) break;
  1409. }
  1410. return ($output ? $output : '0 seconds');
  1411. }
  1412. }
  1413. function get_cron_task_description($name)
  1414. {
  1415. switch ($name)
  1416. {
  1417. case 'lifestream_cleanup':
  1418. return 'Cleans up old events and error messages.';
  1419. break;
  1420. case 'lifestream_cron':
  1421. return 'Updates all active feeds.';
  1422. break;
  1423. case 'lifestream_digest_cron':
  1424. return 'Creates a daily digest post if enabled.';
  1425. break;
  1426. }
  1427. }
  1428. function restore_options()
  1429. {
  1430. // default options and their values
  1431. foreach ($this->_options as $key=>$value)
  1432. {
  1433. $this->update_option($key, $value);
  1434. }
  1435. $this->update_option('extension_dir', WP_CONTENT_DIR.'/wp-lifestream/extensions/');
  1436. $this->update_option('theme_dir', WP_CONTENT_DIR.'/wp-lifestream/themes/');
  1437. $this->update_option('icon_dir', WP_CONTENT_DIR.'/wp-lifestream/icons/');
  1438. }
  1439. function restore_database()
  1440. {
  1441. global $wpdb;
  1442. $this->safe_query("DROP TABLE `".$wpdb->prefix."lifestream_event`;");
  1443. $this->safe_query("DROP TABLE `".$wpdb->prefix."lifestream_event_group`;");
  1444. $this->safe_query("DROP TABLE `".$wpdb->prefix."lifestream_feeds`;");
  1445. $this->safe_query("DROP TABLE `".$wpdb->prefix."lifestream_error_log`;");
  1446. $this->install_database();
  1447. }
  1448. function reschedule_cron()
  1449. {
  1450. wp_clear_scheduled_hook('lifestream_cron');
  1451. wp_clear_scheduled_hook('lifestream_cleanup');
  1452. wp_clear_scheduled_hook('lifestream_digest_cron');
  1453. wp_schedule_event(time()+60, 'daily', 'lifestream_cleanup');
  1454. // First lifestream cron should not happen instantly, incase we need to reschedule
  1455. wp_schedule_event(time()+60, 'lifestream', 'lifestream_cron');
  1456. // We have to calculate the time for the first digest
  1457. $digest_time = $this->get_option('digest_time');
  1458. $digest_interval = $this->get_option('digest_interval');
  1459. $time = time();
  1460. if ($digest_interval == 'hourly')
  1461. {
  1462. // Start at the next hour
  1463. $time = strtotime(date('Y-m-d H:00:00', strtotime('+1 hour', $time)));
  1464. }
  1465. else
  1466. {
  1467. // If time has already passed for today, set it for tomorrow
  1468. if (date('H') > $digest_time)
  1469. {
  1470. $time = strtotime('+1 day', $time);
  1471. }
  1472. else
  1473. {
  1474. $time = strtotime(date('Y-m-d '.$digest_time.':00:00', $time));
  1475. }
  1476. }
  1477. wp_schedule_event($time, 'lifestream_digest',

Large files files are truncated, but you can click here to view the full file