PageRenderTime 103ms CodeModel.GetById 26ms RepoModel.GetById 3ms 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
  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', 'lifestream_digest_cron');
  1478. }
  1479. function deactivate()
  1480. {
  1481. wp_clear_scheduled_hook('lifestream_cron');
  1482. wp_clear_scheduled_hook('lifestream_cleanup');
  1483. wp_clear_scheduled_hook('lifestream_digest_cron');
  1484. }
  1485. /**
  1486. * Initializes the plug-in upon activation.
  1487. */
  1488. function activate()
  1489. {
  1490. global $wpdb, $userdata;
  1491. get_currentuserinfo();
  1492. // Options/database install
  1493. $this->install();
  1494. // Add a feed for this blog
  1495. $results =& $wpdb->get_results("SELECT COUNT(*) as `count` FROM `".$wpdb->prefix."lifestream_feeds`");
  1496. if (!$results[0]->count)
  1497. {
  1498. $rss_url = get_bloginfo('rss2_url');
  1499. $options = array('url' => $rss_url);
  1500. $feed = new Lifestream_BlogFeed($this, $options);
  1501. $feed->owner = $userdata->display_name;
  1502. $feed->owner_id = $userdata->ID;
  1503. $feed->save(false);
  1504. }
  1505. // Cron job for the update
  1506. $this->reschedule_cron();
  1507. }
  1508. function credits()
  1509. {
  1510. return 'Powered by <a href="http://www.enthropia.com/labs/wp-lifestream/">Lifestream</a>.';
  1511. }
  1512. /**
  1513. * Adds/updates the options on plug-in activation.
  1514. */
  1515. function install($allow_database_install=true)
  1516. {
  1517. global $wpdb, $userdata;
  1518. $version = (string)$this->get_option('_version', 0);
  1519. if ($allow_database_install)
  1520. {
  1521. $this->install_database($version);
  1522. }
  1523. if (version_compare($version, '0.95', '<'))
  1524. {
  1525. foreach ($this->_options as $key=>$value)
  1526. {
  1527. $ovalue = get_option('lifestream_' . $key);
  1528. if (!$ovalue)
  1529. {
  1530. $value = $value;
  1531. }
  1532. else
  1533. {
  1534. delete_option('lifestream_' . $key);
  1535. }
  1536. $this->add_option($key, $value);
  1537. }
  1538. }
  1539. // default options and their values
  1540. foreach ($this->_options as $key=>$value)
  1541. {
  1542. $this->add_option($key, $value);
  1543. }
  1544. // because these are based off of the WP_CONTENT_DIR they cannot be included in $_options
  1545. $this->add_option('extension_dir', WP_CONTENT_DIR.'/wp-lifestream/extensions/');
  1546. $this->add_option('theme_dir', WP_CONTENT_DIR.'/wp-lifestream/themes/');
  1547. $this->add_option('icon_dir', WP_CONTENT_DIR.'/wp-lifestream/icons/');
  1548. if (!$this->get_option('page_id'))
  1549. {
  1550. get_currentuserinfo();
  1551. // First let's see if they have a legacy page:
  1552. $results = $wpdb->get_results($wpdb->prepare("SELECT `ID` FROM `".$wpdb->prefix."posts` WHERE `post_type` = 'page' AND `post_content` LIKE '%%[lifestream]%%' AND `post_author` = %d AND `post_status` != 'trash' LIMIT 2", $userdata->ID));
  1553. if (count($results) == 2)
  1554. {
  1555. // Now we're in trouble
  1556. }
  1557. elseif (count($results) == 1)
  1558. {
  1559. $this->update_option('page_id', $results[0]->ID);
  1560. }
  1561. else
  1562. {
  1563. $post = array(
  1564. 'post_title' => 'Lifestream',
  1565. 'post_content' => 'This is just a placeholder for your Lifestream. You may modify the title, categories, and anything else, but the page\'s content has no effect on your Lifestream.',
  1566. 'post_status' => 'publish',
  1567. 'post_author' => $userdata->ID,
  1568. 'post_type' => 'page',
  1569. // should we insert the feed types into the tags?
  1570. // 'tags_input' => ''
  1571. );
  1572. $post_id = wp_insert_post($post);
  1573. $this->update_option('page_id', $post_id);
  1574. }
  1575. }
  1576. if (version_compare($version, LIFESTREAM_VERSION, '=')) return;
  1577. $this->update_option('_version', LIFESTREAM_VERSION);
  1578. }
  1579. /**
  1580. * Executes a MySQL query with exception handling.
  1581. */
  1582. function safe_query($sql)
  1583. {
  1584. global $wpdb;
  1585. $result = $wpdb->query($sql);
  1586. if ($result === false)
  1587. {
  1588. if ($wpdb->error)
  1589. {
  1590. $reason = $wpdb->error->get_error_message();
  1591. }
  1592. else
  1593. {
  1594. $reason = $this->__('Unknown SQL Error');
  1595. }
  1596. $this->log_error($reason);
  1597. throw new Lifestream_Error($reason);
  1598. }
  1599. return $result;
  1600. }
  1601. /**
  1602. * Initializes the database if it's not already present.
  1603. */
  1604. function install_database($version=0)
  1605. {
  1606. global $wpdb, $userdata;
  1607. get_currentuserinfo();
  1608. $this->safe_query("CREATE TABLE IF NOT EXISTS `".$wpdb->prefix."lifestream_event` (
  1609. `id` int(10) unsigned NOT NULL auto_increment,
  1610. `feed_id` int(10) unsigned NOT NULL,
  1611. `post_id` int(10) unsigned default 0 NOT NULL,
  1612. `feed` varchar(32) NOT NULL,
  1613. `link` varchar(200) NOT NULL,
  1614. `data` blob NOT NULL,
  1615. `visible` tinyint(1) default 1 NOT NULL,
  1616. `timestamp` int(11) NOT NULL,
  1617. `version` int(11) default 0 NOT NULL,
  1618. `key` char(16) NOT NULL,
  1619. `group_key` char(32) NOT NULL,
  1620. `owner` varchar(128) NOT NULL,
  1621. `owner_id` int(10) unsigned NOT NULL,
  1622. PRIMARY KEY (`id`),
  1623. INDEX `feed` (`feed`),
  1624. UNIQUE `feed_id` (`feed_id`, `group_key`, `owner_id`, `link`)
  1625. );");
  1626. $this->safe_query("CREATE TABLE IF NOT EXISTS `".$wpdb->prefix."lifestream_event_group` (
  1627. `id` int(10) unsigned NOT NULL auto_increment,
  1628. `feed_id` int(10) unsigned NOT NULL,
  1629. `event_id` int(10) unsigned NOT NULL,
  1630. `post_id` int(10) unsigned default 0 NOT NULL,
  1631. `feed` varchar(32) NOT NULL,
  1632. `data` blob NOT NULL,
  1633. `total` int(10) unsigned default 1 NOT NULL,
  1634. `updated` tinyint(1) default 0 NOT NULL,
  1635. `visible` tinyint(1) default 1 NOT NULL,
  1636. `timestamp` int(11) NOT NULL,
  1637. `version` int(11) default 0 NOT NULL,
  1638. `key` char(16) NOT NULL,
  1639. `group_key` char(32) NOT NULL,
  1640. `owner` varchar(128) NOT NULL,
  1641. `owner_id` int(10) unsigned NOT NULL,
  1642. PRIMARY KEY (`id`),
  1643. INDEX `feed` (`feed`),
  1644. INDEX `feed_id` (`feed_id`, `group_key`, `owner_id`, `timestamp`)
  1645. );");
  1646. $this->safe_query("CREATE TABLE IF NOT EXISTS `".$wpdb->prefix."lifestream_feeds` (
  1647. `id` int(10) unsigned NOT NULL auto_increment,
  1648. `feed` varchar(32) NOT NULL,
  1649. `options` text default NULL,
  1650. `timestamp` int(11) NOT NULL,
  1651. `active` tinyint(1) default 1 NOT NULL,
  1652. `owner` varchar(128) NOT NULL,
  1653. `owner_id` int(10) unsigned NOT NULL,
  1654. `version` int(11) default 0 NOT NULL,
  1655. INDEX `owner_id` (`owner_id`),
  1656. PRIMARY KEY (`id`)
  1657. );");
  1658. $this->safe_query("CREATE TABLE IF NOT EXISTS `".$wpdb->prefix."lifestream_error_log` (
  1659. `id` int(10) unsigned NOT NULL auto_increment,
  1660. `message` varchar(255) NOT NULL,
  1661. `trace` text NULL,
  1662. `feed_id` int(10) unsigned NULL,
  1663. `timestamp` int(11) NOT NULL,
  1664. `has_viewed` tinyint(1) default 0 NOT NULL,
  1665. INDEX `feed_id` (`feed_id`, `has_viewed`),
  1666. INDEX `has_viewed` (`has_viewed`),
  1667. PRIMARY KEY (`id`)
  1668. );");
  1669. if (!$version) return;
  1670. // URGENT TODO: we need to solve alters when the column already exists due to WP issues
  1671. if (version_compare($version, '0.5', '<'))
  1672. {
  1673. // Old wp-cron built-in stuff
  1674. wp_clear_scheduled_hook('Lifestream_Hourly');
  1675. // Upgrade them to version 0.5
  1676. $wpdb->query("ALTER IGNORE TABLE `".$wpdb->prefix."lifestream_event_group` ADD `version` INT(11) NOT NULL DEFAULT '0' AFTER `timestamp`, ADD `key` CHAR( 16 ) NOT NULL AFTER `version`;");
  1677. $wpdb->query("ALTER IGNORE TABLE `".$wpdb->prefix."lifestream_event` ADD `version` INT(11) NOT NULL DEFAULT '0' AFTER `timestamp`, ADD `key` CHAR( 16 ) NOT NULL AFTER `version`;");
  1678. }
  1679. if (version_compare($version, '0.6', '<'))
  1680. {
  1681. $wpdb->query("ALTER IGNORE TABLE `".$wpdb->prefix."lifestream_event_group` ADD `owner` VARCHAR(128) NOT NULL AFTER `key`, ADD `owner_id` int(10) unsigned NOT NULL AFTER `owner`;");
  1682. $wpdb->query("ALTER IGNORE TABLE `".$wpdb->prefix."lifestream_event` ADD `owner` VARCHAR(128) NOT NULL AFTER `key`, ADD `owner_id` int(10) unsigned NOT NULL AFTER `owner`;");
  1683. $wpdb->query("ALTER IGNORE TABLE `".$wpdb->prefix."lifestream_feeds` ADD `owner` VARCHAR(128) NOT NULL AFTER `timestamp`, ADD `owner_id` int(10) unsigned NOT NULL AFTER `owner`;");
  1684. $wpdb->query("ALTER IGNORE TABLE `".$wpdb->prefix."lifestream_event` DROP INDEX `feed_id`, ADD UNIQUE `feed_id` (`feed_id` , `key` , `owner_id` , `link` );");
  1685. $wpdb->query("ALTER IGNORE TABLE `".$wpdb->prefix."lifestream_event_group` DROP INDEX `feed_id`, ADD INDEX `feed_id` (`feed_id` , `key` , `timestamp` , `owner_id`);");
  1686. $wpdb->query("ALTER IGNORE TABLE `".$wpdb->prefix."lifestream_feeds` ADD INDEX `owner_id` (`owner_id`);");
  1687. $this->safe_query($wpdb->prepare("UPDATE `".$wpdb->prefix."lifestream_feeds` SET `owner` = %s, `owner_id` = %d", $userdata->display_name, $userdata->ID));
  1688. $this->safe_query($wpdb->prepare("UPDATE `".$wpdb->prefix."lifestream_event` SET `owner` = %s, `owner_id` = %d", $userdata->display_name, $userdata->ID));
  1689. $this->safe_query($wpdb->prepare("UPDATE `".$wpdb->prefix."lifestream_event_group` SET `owner` = %s, `owner_id` = %d", $userdata->display_name, $userdata->ID));
  1690. }
  1691. if (version_compare($version, '0.81', '<'))
  1692. {
  1693. $wpdb->query("ALTER IGNORE TABLE `".$wpdb->prefix."lifestream_event` ADD `feed` VARCHAR(32) NOT NULL AFTER `feed_id`");
  1694. $this->safe_query("UPDATE IGNORE `".$wpdb->prefix."lifestream_event` as t1 set t1.`feed` = (SELECT t2.`feed` FROM `".$wpdb->prefix."lifestream_feeds` as t2 WHERE t1.`feed_id` = t2.`id`)");
  1695. }
  1696. if (version_compare($version, '0.84', '<'))
  1697. {
  1698. $wpdb->query("ALTER IGNORE TABLE `".$wpdb->prefix."lifestream_event` ADD INDEX ( `feed` )");
  1699. $wpdb->query("ALTER IGNORE TABLE `".$wpdb->prefix."lifestream_event_group` ADD INDEX ( `feed` )");
  1700. }
  1701. if (version_compare($version, '0.90', '<'))
  1702. {
  1703. $wpdb->query("ALTER IGNORE TABLE `".$wpdb->prefix."lifestream_feeds` ADD `version` int(11) default 0 NOT NULL AFTER `owner_id`");
  1704. }
  1705. if (version_compare($version, '0.99.6.0', '<'))
  1706. {
  1707. $wpdb->query("ALTER IGNORE TABLE `".$wpdb->prefix."lifestream_feeds` ADD `active` tinyint(1) default 1 NOT NULL AFTER `timestamp`");
  1708. }
  1709. if (version_compare($version, '0.99.9.4', '<'))
  1710. {
  1711. $wpdb->query("UPDATE `".$wpdb->prefix."lifestream_feeds` SET `active` = 1");
  1712. }
  1713. if (version_compare($version, '0.99.9.7', '<'))
  1714. {
  1715. $wpdb->query("ALTER IGNORE TABLE `".$wpdb->prefix."lifestream_event` ADD `group_key` char(32) NOT NULL AFTER `key`, DROP KEY `feed_id`, ADD UNIQUE `feed_id` (`feed_id`, `group_key`, `owner_id`, `link`)");
  1716. $wpdb->query("ALTER IGNORE TABLE `".$wpdb->prefix."lifestream_event_group` ADD `group_key` char(32) NOT NULL AFTER `key`, DROP KEY `feed_id`, ADD INDEX `feed_id` (`feed_id`, `group_key`, `owner_id`, `timestamp`)");
  1717. $wpdb->query("UPDATE `".$wpdb->prefix."lifestream_event` SET `group_key` = md5(`key`)");
  1718. $wpdb->query("UPDATE `".$wpdb->prefix."lifestream_event_group` SET `group_key` = md5(`key`)");
  1719. }
  1720. if (version_compare($version, '0.99.9.7.1', '<'))
  1721. {
  1722. $wpdb->query("ALTER IGNORE TABLE `".$wpdb->prefix."lifestream_event` ADD `post_id` int(10) unsigned default 0 NOT NULL AFTER `feed_id`");
  1723. $wpdb->query("ALTER IGNORE TABLE `".$wpdb->prefix."lifestream_event_group` ADD `post_id` int(10) unsigned default 0 NOT NULL AFTER `feed_id`");
  1724. $this->upgrade_posts_to_events();
  1725. }
  1726. }
  1727. /**
  1728. * Imports all events as custom posts in WordPress 2.9.
  1729. */
  1730. function upgrade_posts_to_events()
  1731. {
  1732. $new_events = 0;
  1733. $offset = 0;
  1734. $events = $this->get_events(array(
  1735. 'offset' => $offset,
  1736. 'post_ids' => array(0),
  1737. ));
  1738. while ($events)
  1739. {
  1740. foreach ($events as &$event)
  1741. {
  1742. $this->create_post_for_event($event);
  1743. $new_events += 1;
  1744. }
  1745. $offset += 50;
  1746. $events = $this->get_events(array(
  1747. 'offset' => $offset,
  1748. ));
  1749. }
  1750. return $new_events;
  1751. }
  1752. function create_post_for_event($event)
  1753. {
  1754. global $wpdb;
  1755. // TODO: find a better title
  1756. $post = array(
  1757. 'post_title' => 'Lifestream Event',
  1758. 'post_content' => '',
  1759. 'post_status' => 'publish',
  1760. 'post_author' => $event->owner_id,
  1761. 'post_type' => 'lsevent',
  1762. // should we insert the feed types into the tags?
  1763. // 'tags_input' => ''
  1764. 'post_date' => date('Y-m-d H:i:s', $event->timestamp),
  1765. );
  1766. $post_id = wp_insert_post($post);
  1767. $event->post_id = $post_id;
  1768. $event_list = array();
  1769. if ($event->is_grouped)
  1770. {
  1771. $wpdb->query($wpdb->prepare("UPDATE `".$wpdb->prefix."lifestream_event_group` set `post_id` = %d WHERE `id` = %d", $event->post_id, $event->id));
  1772. foreach ($event->data as $event)
  1773. {
  1774. // TODO: append to $event_list
  1775. }
  1776. }
  1777. else
  1778. {
  1779. $event_list[] = $event;
  1780. }
  1781. // TODO: process event list and update post ids
  1782. }
  1783. function get_page_from_request()
  1784. {
  1785. return (!empty($_GET[$this->paging_key]) ? $_GET[$this->paging_key] : 1);
  1786. }
  1787. function get_next_page_url($page=null)
  1788. {
  1789. if (!$page) $page = $this->get_page_from_request();
  1790. if (strpos($_SERVER['QUERY_STRING'], '?') !== false) {
  1791. $url = str_replace('&'.$this->paging_key.'='.$page, '', $_SERVER['QUERY_STRING']);
  1792. return $url.'&'.$this->paging_key.'='.($page+1);
  1793. }
  1794. return '?'.$this->paging_key.'='.($page+1);
  1795. }
  1796. function get_previous_page_url($page=null)
  1797. {
  1798. if (!$page) $page = $this->get_page_from_request();
  1799. if (strpos($_SERVER['QUERY_STRING'], '?') !== false) {
  1800. $url = str_replace('&'.$this->paging_key.'='.$page, '', $_SERVER['QUERY_STRING']);
  1801. return $url.'&'.$this->paging_key.'='.($page-1);
  1802. }
  1803. return '?'.$this->paging_key.'='.($page-1);
  1804. }
  1805. /**
  1806. * Gets recent events from the lifestream.
  1807. * @param {Array} $_ An array of keyword args.
  1808. */
  1809. function get_events($_=array())
  1810. {
  1811. global $wpdb;
  1812. $defaults = array(
  1813. // number of events
  1814. 'event_ids' => array(),
  1815. 'post_ids' => array(),
  1816. 'limit' => $this->get_option('number_of_items'),
  1817. // offset of events (e.g. pagination)
  1818. 'offset' => 0,
  1819. // array of feed ids
  1820. 'feed_ids' => array(),
  1821. // array of user ids
  1822. 'user_ids' => array(),
  1823. // array of feed type identifiers
  1824. 'feed_types' => array(),
  1825. // interval for date cutoff (see mysql INTERVAL)
  1826. 'date_interval' => $this->get_option('date_interval'),
  1827. // start date of events
  1828. 'start_date' => -1,
  1829. // end date
  1830. 'end_date' => -1,
  1831. // minimum number of events in group
  1832. 'event_total_min' => -1,
  1833. // maximum
  1834. 'event_total_max' => -1,
  1835. // break groups into single events
  1836. 'break_groups' => false,
  1837. );
  1838. $_ = array_merge($defaults, $_);
  1839. # If any arguments are invalid we bail out
  1840. // Old-style
  1841. if (!empty($_['number_of_results'])) $_['limit'] = $_['number_of_results'];
  1842. if (!((int)$_['limit'] > 0)) return false;
  1843. if (!((int)$_['offset'] >= 0)) return false;
  1844. if (!preg_match('/[\d]+ (month|day|year|hour|second|microsecond|week|quarter)s?/', $_['date_interval'])) $_['date_interval'] = -1;
  1845. else $_['date_interval'] = rtrim($_['date_interval'], 's');
  1846. $_['feed_ids'] = (array)$_['feed_ids'];
  1847. $_['event_ids'] = (array)$_['event_ids'];
  1848. $_['post_ids'] = (array)$_['post_ids'];
  1849. $_['user_ids'] = (array)$_['user_ids'];
  1850. $_['feed_types'] = (array)$_['feed_types'];
  1851. $where = array('t1.`visible` = 1');
  1852. if (count($_['feed_ids']))
  1853. {
  1854. foreach ($_['feed_ids'] as $key=>$value)
  1855. {
  1856. $_['feed_ids'][$key] = $wpdb->escape($value);
  1857. }
  1858. $where[] = 't1.`feed_id` IN ('.implode(', ', $_['feed_ids']).')';
  1859. }
  1860. elseif (count($_['feed_types']))
  1861. {
  1862. foreach ($_['feed_types'] as $key=>$value)
  1863. {
  1864. $_['feed_types'][$key] = $wpdb->escape($value);
  1865. }
  1866. $where[] = 't1.`feed` IN ("'.implode('", "', $_['feed_types']).'")';
  1867. }
  1868. if (count($_['event_ids']))
  1869. {
  1870. foreach ($_['event_ids'] as $key=>$value)
  1871. {
  1872. $_['event_ids'][$key] = $wpdb->escape($value);
  1873. }
  1874. $where[] = 't1.`id` IN ('.implode(', ', $_['event_ids']).')';
  1875. }
  1876. elseif (count($_['post_ids']))
  1877. {
  1878. foreach ($_['post_ids'] as $key=>$value)
  1879. {
  1880. $_['post_ids'][$key] = $wpdb->escape($value);
  1881. }
  1882. $where[] = 't1.`post_id` IN ('.implode(', ', $_['post_ids']).')';
  1883. }
  1884. if (count($_['user_ids']))
  1885. {
  1886. foreach ($_['user_ids'] as $key=>$value)
  1887. {
  1888. $_['user_ids'][$key] = $wpdb->escape($value);
  1889. }
  1890. $where[] = 't1.`owner_id` IN ('.implode(', ', $_['user_ids']).')';
  1891. }
  1892. if ($_['event_total_max'] > -1)
  1893. {
  1894. $where[] = sprintf('t1.`total` <= %d', $_['event_total_max']);
  1895. }
  1896. if ($_['event_total_min'] > -1)
  1897. {
  1898. $where[] = sprintf('t1.`total` >= %d', $_['event_total_min']);
  1899. }
  1900. if ($_['date_interval'] !== -1)
  1901. {
  1902. $where[] = sprintf('t1.`timestamp` > UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL %s))', $_['date_interval']);
  1903. }
  1904. if ($_['start_date'] !== -1)
  1905. {
  1906. if (!is_int($_['start_date'])) $_['start_date'] = strtotime($_['start_date']);
  1907. $where[] = sprintf('t1.`timestamp` >= %s', $_['start_date']);
  1908. }
  1909. if ($_['end_date'] !== -1)
  1910. {
  1911. if (!is_int($_['end_date'])) $_['end_date'] = strtotime($_['end_date']);
  1912. $where[] = sprintf('t1.`timestamp` <= %s', $_['end_date']);
  1913. }
  1914. if ($_['break_groups'])
  1915. {
  1916. // we select from lifestream_event vs grouped
  1917. $table = 'lifestream_event';
  1918. $cls = 'Lifestream_Event';
  1919. }
  1920. else
  1921. {
  1922. $table = 'lifestream_event_group';
  1923. $cls = 'Lifestream_EventGroup';
  1924. }
  1925. $sql = sprintf("SELECT t1.*, t2.`options` FROM `".$wpdb->prefix.$table."` as `t1` INNER JOIN `".$wpdb->prefix."lifestream_feeds` as t2 ON t1.`feed_id` = t2.`id` WHERE t1.`visible` = 1 AND (%s) ORDER BY t1.`timestamp` DESC LIMIT %d, %d", implode(') AND (', $where), $_['offset'], $_['limit']);
  1926. $results =& $wpdb->get_results($sql);
  1927. $events = array();
  1928. foreach ($results as &$result)
  1929. {
  1930. if (array_key_exists($result->feed, $this->feeds))
  1931. {
  1932. $events[] = new $cls($this, $result);
  1933. }
  1934. }
  1935. return $events;
  1936. }
  1937. }
  1938. $lifestream = new Lifestream();
  1939. function lifestream_get_single_event($feed_type)
  1940. {
  1941. global $lifestream;
  1942. return $lifestream->get_single_event($feed_type);
  1943. }
  1944. require_once(LIFESTREAM_PATH . '/inc/labels.php');
  1945. abstract class Lifestream_Extension
  1946. {
  1947. /**
  1948. * Represents a feed object in the database.
  1949. */
  1950. public $options;
  1951. public static $builtin = false;
  1952. public static $absolute_path = __FILE__;
  1953. // The ID must be a-z, 0-9, _, and - characters. It also must be unique.
  1954. const ID = 'generic';
  1955. const NAME = 'Generic';
  1956. const AUTHOR = 'David Cramer';
  1957. const URL = '';
  1958. const DESCRIPTION = '';
  1959. // Can this feed be grouped?
  1960. const CAN_GROUP = true;
  1961. // Can this feed have a label?
  1962. const MUST_GROUP = false;
  1963. // Labels used in rendering each event
  1964. // params: feed name, event descriptor
  1965. const LABEL = 'Lifestream_Label';
  1966. // The version is so you can manage data in the database for old versions.
  1967. const VERSION = 2;
  1968. const MEDIA = 'automatic';
  1969. const HAS_EXCERPTS = false;
  1970. /**
  1971. * Instantiates this object through a feed database object.
  1972. */
  1973. public static function construct_from_query_result(&$lifestream, $row)
  1974. {
  1975. $class = $lifestream->get_feed($row->feed);
  1976. if (!$class)
  1977. {
  1978. $class = 'Lifestream_InvalidExtension';
  1979. }
  1980. if (!empty($row->options)) $options = unserialize($row->options);
  1981. else $options = null;
  1982. $instance = new $class($lifestream, $options, $row->id, $row);
  1983. $instance->date = $row->timestamp;
  1984. return $instance;
  1985. }
  1986. function __construct(&$lifestream, $options=array(), $id=null, $row=null)
  1987. {
  1988. $this->lifestream = $lifestream;
  1989. $this->options = $options;
  1990. $this->id = $id;
  1991. if ($row)
  1992. {
  1993. $this->active = $row->active;
  1994. $this->owner = $row->owner;
  1995. $this->owner_id = $row->owner_id;
  1996. $this->_owner_id = $row->owner_id;
  1997. $this->version = $row->version;
  1998. $this->events = (int)@($row->events);
  1999. $this->feed = $row->feed;
  2000. }
  2001. else
  2002. {
  2003. $this->version = $this->get_constant('VERSION');
  2004. }
  2005. }
  2006. function __toInt()
  2007. {
  2008. return $this->id;
  2009. }
  2010. function __toString()
  2011. {
  2012. return $this->get_url();
  2013. }
  2014. function get_event_excerpt(&$event, &$bit)
  2015. {
  2016. if (!isset($this->option['excerpts']))
  2017. {
  2018. // default legacy value
  2019. $this->update_option('excerpt', 1);
  2020. }
  2021. if ($this->get_option('excerpt') > 0)
  2022. {
  2023. $excerpt = $this->get_event_description($event, $bit);
  2024. }
  2025. if ($this->get_option('excerpt') == 1)
  2026. {
  2027. $excerpt = $this->lifestream->truncate($excerpt, $this->lifestream->get_option('truncate_length'));
  2028. }
  2029. return $excerpt;
  2030. }
  2031. function has_excerpt(&$event, &$bit)
  2032. {
  2033. if (!isset($this->option['excerpts']))
  2034. {
  2035. // default legacy value
  2036. $this->update_option('excerpt', 1);
  2037. }
  2038. return ($this->get_option('excerpt') > 0 && $this->get_event_description($event, $bit));
  2039. }
  2040. /**
  2041. * Returns the description (also used in excerpts) for this
  2042. * event.
  2043. * @return {String} event description
  2044. */
  2045. function get_event_description(&$event, &$bit)
  2046. {
  2047. return $bit['description'];
  2048. }
  2049. function get_event_display(&$event, &$bit)
  2050. {
  2051. return $bit['title'];
  2052. }
  2053. function get_event_link(&$event, &$bit)
  2054. {
  2055. return $bit['link'];
  2056. }
  2057. function get_feed_display()
  2058. {
  2059. return $this->__toString();
  2060. }
  2061. function get_icon_name()
  2062. {
  2063. return 'icon.png';
  2064. }
  2065. function get_icon_url()
  2066. {
  2067. // TODO: clean this up to use the new Lifestream::get_media methods
  2068. if ($this->get_option('icon_url'))
  2069. {
  2070. return $this->get_option('icon_url');
  2071. }
  2072. $path = trailingslashit($this->lifestream->paths[get_class($this)]);
  2073. $root = trailingslashit(dirname(__FILE__));
  2074. if ($path == $root)
  2075. {
  2076. $path = $this->lifestream->icons[$this->lifestream->get_option('icons', 'default')]['__path'];
  2077. $icon_path = lifestream_path_join($path, $this->get_constant('ID').'.png');
  2078. if (!is_file($icon_path))
  2079. {
  2080. $icon_path = lifestream_path_join($path, 'generic.png');
  2081. }
  2082. $path = $icon_path;
  2083. }
  2084. else
  2085. {
  2086. $path = lifestream_path_join($path, $this->get_icon_name());
  2087. }
  2088. return $this->lifestream->get_absolute_media_url($path);
  2089. }
  2090. function get_public_url()
  2091. {
  2092. return $this->get_constant('URL');
  2093. }
  2094. function get_image_url($row, $item)
  2095. {
  2096. return is_array($item['image']) ? $item['image']['url'] : $item['image'];
  2097. }
  2098. function get_thumbnail_url($row, $item)
  2099. {
  2100. // Array checks are for backwards compatbility
  2101. return is_array(@$item['thumbnail']) ? $item['thumbnail']['url'] : @$item['thumbnail'];
  2102. }
  2103. function get_public_name()
  2104. {
  2105. if ($this->get_option('feed_label'))
  2106. {
  2107. return $this->get_option('feed_label');
  2108. }
  2109. return $this->get_constant('NAME');
  2110. }
  2111. /**
  2112. * Returns a constant attached to this class.
  2113. * @param {string} $constant
  2114. * @return {string | integer} $value
  2115. */
  2116. function get_constant($constant)
  2117. {
  2118. return constant(sprintf('%s::%s', get_class($this), $constant));
  2119. }
  2120. /**
  2121. * Returns an array of available options.
  2122. * @return {array} Available options.
  2123. */
  2124. function get_options()
  2125. {
  2126. return array(
  2127. // key => array(label, required, default value, choices)
  2128. 'url' => array($this->lifestream->__('Feed URL:'), true, '', ''),
  2129. );
  2130. }
  2131. /**
  2132. * Fetches the value of an option. Returns `null` if the option is not set.
  2133. */
  2134. function get_option($option, $default=null)
  2135. {
  2136. if (!isset($this->options[$option]))
  2137. {
  2138. $value = $default;
  2139. }
  2140. else {
  2141. $value = $this->options[$option];
  2142. if (!$value) $value = $default;
  2143. }
  2144. if (empty($value)) $value = null;
  2145. return $value;
  2146. }
  2147. /**
  2148. * Removes an option.
  2149. */
  2150. function delete_option($option)
  2151. {
  2152. unset($this->options[$option]);
  2153. }
  2154. /**
  2155. * Updates the value of an option.
  2156. */
  2157. function update_option($option, $value)
  2158. {
  2159. $this->options[$option] = $value;
  2160. }
  2161. /**
  2162. * Sets an option if it doesn't exist.
  2163. */
  2164. function add_option($option, $value)
  2165. {
  2166. if (!array_key_exists($option, $this->options) || $this->options[$option] === '')
  2167. {
  2168. $this->options[$option] = $value;
  2169. }
  2170. }
  2171. function truncate()
  2172. {
  2173. global $wpdb;
  2174. if ($this->id)
  2175. {
  2176. $wpdb->query($wpdb->prepare("DELETE FROM `".$wpdb->prefix."lifestream_event` WHERE `feed_id` = %d", $this->id));
  2177. $this->lifestream->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 `feed_id` = %d)", $this->id));
  2178. $wpdb->query($wpdb->prepare("DELETE FROM `".$wpdb->prefix."lifestream_event_group` WHERE `feed_id` = %d", $this->id));
  2179. }
  2180. }
  2181. function save($validate=true)
  2182. {
  2183. global $wpdb;
  2184. $this->save_options($validate);
  2185. // If it has an ID it means it already exists.
  2186. if ($this->id)
  2187. {
  2188. $result = $wpdb->query($wpdb->prepare("UPDATE `".$wpdb->prefix."lifestream_feeds` set `options` = %s, `owner` = %s, `owner_id` = %d WHERE `id` = %d", serialize($this->options), $this->owner, $this->owner_id, $this->id));
  2189. if ($this->_owner_id && $this->_owner_id != $this->owner_id)
  2190. {
  2191. $wpdb->query($wpdb->prepare("UPDATE `".$wpdb->prefix."lifestream_event` SET `owner` = %s, `owner_id` = %d WHERE `feed_id` = %d", $this->owner, $this->owner_id, $this->id));
  2192. $wpdb->query($wpdb->prepare("UPDATE `".$wpdb->prefix."lifestream_event_group` SET `owner` = %s, `owner_id` = %d WHERE `feed_id` = %d", $this->owner, $this->owner_id, $this->id));
  2193. }
  2194. }
  2195. else
  2196. {
  2197. $result = $wpdb->query($wpdb->prepare("INSERT INTO `".$wpdb->prefix."lifestream_feeds` (`feed`, `options`, `timestamp`, `owner`, `owner_id`, `version`) VALUES (%s, %s, %d, %s, %d, %d)", $this->get_constant('ID'), serialize($this->options), time(), $this->owner, $this->owner_id, $this->get_constant('VERSION')));
  2198. $this->id = $wpdb->insert_id;
  2199. }
  2200. return $result;
  2201. }
  2202. function delete()
  2203. {
  2204. global $wpdb;
  2205. $this->lifestream->safe_query($wpdb->prepare("DELETE FROM `".$wpdb->prefix."lifestream_feeds` WHERE `id` = %d", $this->id));
  2206. $this->lifestream->safe_query($wpdb->prepare("DELETE FROM `".$wpdb->prefix."lifestream_event` WHERE `feed_id` = %d", $this->id));
  2207. $this->lifestream->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 `feed_id` = %d)", $this->id));
  2208. $this->lifestream->safe_query($wpdb->prepare("DELETE FROM `".$wpdb->prefix."lifestream_event_group` WHERE `feed_id` = %d", $this->id));
  2209. $this->lifestream->safe_query($wpdb->prepare("DELETE FROM `".$wpdb->prefix."lifestream_error_log` WHERE `feed_id` = %d", $this->id));
  2210. $this->id = null;
  2211. }
  2212. /**
  2213. * Called upon saving options to handle additional data management.
  2214. */
  2215. function save_options($validate=true) { }
  2216. /**
  2217. * Validates the feed. A success has no return value.
  2218. */
  2219. function test()
  2220. {
  2221. try
  2222. {
  2223. $this->save_options($validate=true);
  2224. $this->fetch();
  2225. }
  2226. catch (Lifestream_Error $ex)
  2227. {
  2228. return $ex->getMessage();
  2229. }
  2230. }
  2231. function refresh($urls=null, $initial=false)
  2232. {
  2233. global $wpdb;
  2234. date_default_timezone_set('UTC');
  2235. if (!$this->id) return array(false, $this->lifestream->__('Feed has not yet been saved.'));
  2236. $grouped = array();
  2237. $ungrouped = array();
  2238. $total = 0;
  2239. try
  2240. {
  2241. $items = $this->fetch($urls, $initial);
  2242. }
  2243. catch (Lifestream_Error $ex)
  2244. {
  2245. $this->lifestream->log_error($ex, $this->id);
  2246. return array(false, $ex);
  2247. }
  2248. if (!$items) return array(false, $this->lifestream->__('Feed result was empty.'));
  2249. if (!$initial) $default_timestamp = time();
  2250. else $default_timestamp = 0;
  2251. foreach ($items as $item_key=>&$item)
  2252. {
  2253. // We need to set the default timestamp if no dates are set
  2254. $date = lifestream_array_key_pop($item, 'date');
  2255. $key = lifestream_array_key_pop($item, 'key');
  2256. $group_key = md5(lifestream_array_key_pop($item, 'group_key', $key));
  2257. if (!($date > 0)) $date = $default_timestamp;
  2258. if ($this->version == 2)
  2259. {
  2260. if ($item['guid']) $link_key = md5(lifestream_array_key_pop($item, 'guid'));
  2261. else $link_key = md5($item['link'] . $item['title']);
  2262. }
  2263. elseif ($this->version == 1)
  2264. {
  2265. $link_key = md5($item['link'] . $item['title']);
  2266. }
  2267. else
  2268. {
  2269. $link_key = $item['link'];
  2270. }
  2271. $affected = $wpdb->query($wpdb->prepare("INSERT IGNORE INTO `".$wpdb->prefix."lifestream_event` (`feed_id`, `feed`, `link`, `data`, `timestamp`, `version`, `key`, `group_key`, `owner`, `owner_id`) VALUES (%d, %s, %s, %s, %d, %d, %s, %s, %s, %d)", $this->id, $this->get_constant('ID'), $link_key, serialize($item), $date, $this->get_constant('VERSION'), $key, $group_key, $this->owner, $this->owner_id));
  2272. if ($affected)
  2273. {
  2274. $item['id'] = $wpdb->insert_id;
  2275. $item['date'] = $date;
  2276. $item['key'] = $key;
  2277. $item['group_key'] = $group_key;
  2278. $total += 1;
  2279. $label = $this->get_label_class($key);
  2280. if ($this->get_option('grouped') && $this->get_constant('CAN_GROUP') && constant(sprintf('%s::%s', $label, 'CAN_GROUP')))
  2281. {
  2282. if (!array_key_exists($group_key, $grouped)) $grouped[$group_key] = array();
  2283. $grouped[$group_key][date('m d Y', $date)] = $date;
  2284. }
  2285. else
  2286. {
  2287. $ungrouped[] = $item;
  2288. }
  2289. }
  2290. else
  2291. {
  2292. unset($items[$item_key]);
  2293. }
  2294. }
  2295. // Grouping them by key
  2296. foreach ($grouped as $group_key=>$dates)
  2297. {
  2298. // Grouping them by date
  2299. foreach ($dates as $date_key=>$date)
  2300. {
  2301. // Get all of the current events for this date
  2302. // (including the one we affected just now)
  2303. $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)) AND `group_key` = %s", $this->id, $date, $group_key));
  2304. $events = array();
  2305. foreach ($results as &$result)
  2306. {
  2307. $result->data = unserialize($result->data);
  2308. if (!$result->data['link']) $result->data['link'] = $result->link;
  2309. $events[] = $result->data;
  2310. }
  2311. // First let's see if the group already exists in the database
  2312. $group =& $wpdb->get_results($wpdb->prepare("SELECT `id` FROM `".$wpdb->prefix."lifestream_event_group` WHERE `feed_id` = %d AND DATE(FROM_UNIXTIME(`timestamp`)) = DATE(FROM_UNIXTIME(%d)) AND `group_key` = %s LIMIT 0, 1", $this->id, $date, $group_key));
  2313. if (count($group) == 1)
  2314. {
  2315. $group =& $group[0];
  2316. $wpdb->query($wpdb->prepare("UPDATE `".$wpdb->prefix."lifestream_event_group` SET `data` = %s, `total` = %d, `updated` = 1, `timestamp` = %d WHERE `id` = %d", serialize($events), count($events), $date, $group->id));
  2317. }
  2318. else
  2319. {
  2320. $wpdb->query($wpdb->prepare("INSERT INTO `".$wpdb->prefix."lifestream_event_group` (`feed_id`, `feed`, `data`, `total`, `timestamp`, `version`, `key`, `group_key`, `owner`, `owner_id`, `post_id`) VALUES(%d, %s, %s, %d, %d, %d, %s, %s, %s, %d, 0)", $this->id, $this->get_constant('ID'), serialize($events), count($events), $date, $this->get_constant('VERSION'), $key, $group_key, $this->owner, $this->owner_id));
  2321. }
  2322. }
  2323. }
  2324. foreach ($ungrouped as &$item)
  2325. {
  2326. $date = lifestream_array_key_pop($item, 'date');
  2327. $key = lifestream_array_key_pop($item, 'key');
  2328. $group_key = lifestream_array_key_pop($item, 'group_key');
  2329. $wpdb->query($wpdb->prepare("INSERT INTO `".$wpdb->prefix."lifestream_event_group` (`feed_id`, `feed`, `event_id`, `data`, `timestamp`, `total`, `version`, `key`, `group_key`, `owner`, `owner_id`, `post_id`) VALUES(%d, %s, %d, %s, %d, 1, %d, %s, %s, %s, %d, 0)", $this->id, $this->get_constant('ID'), $item['id'], serialize(array($item)), $date, $this->get_constant('VERSION'), $key, $group_key, $this->owner, $this->owner_id));
  2330. }
  2331. $wpdb->query($wpdb->prepare("UPDATE `".$wpdb->prefix."lifestream_feeds` SET `timestamp` = UNIX_TIMESTAMP() WHERE `id` = %d", $this->id));
  2332. unset($items, $ungrouped);
  2333. $this->lifestream->upgrade_posts_to_events();
  2334. return array(true, $total);
  2335. }
  2336. /**
  2337. * Processes a row and returns an array of data dictionaries.
  2338. * @return {Array} Array of data dictionaries.
  2339. */
  2340. function yield_many()
  2341. {
  2342. $args = func_get_args();
  2343. $data = call_user_func_array(array(&$this, 'yield'), $args);
  2344. return array($data);
  2345. }
  2346. /**
  2347. * Processes a row and returns a data dictionary.
  2348. * Should at the very least return title and link keys.
  2349. * @abstract
  2350. * @return {Array} Data dictionary.
  2351. */
  2352. //abstract function yield();
  2353. abstract function fetch();
  2354. function get_id($event, $uniq_id='')
  2355. {
  2356. return 'ls-'.$event->id.'-'.$uniq_id;
  2357. }
  2358. function render_item($row, $item)
  2359. {
  2360. $thumbnail = $this->get_thumbnail_url($row, $item);
  2361. if (!empty($thumbnail) && $this->get_constant('MEDIA') == 'automatic')
  2362. {
  2363. $image = $this->get_image_url($row, $item);
  2364. if ($this->lifestream->get_option('use_ibox') == '1' && !empty($image))
  2365. {
  2366. // change it to be large size images
  2367. $ibox = ' rel="ibox&target=\''.htmlspecialchars($image).'\'"';
  2368. }
  2369. else $ibox = '';
  2370. return sprintf('<a href="%s"'.$ibox.' class="photo" title="%s"><img src="%s" width="50" alt="%s"/></a>', htmlspecialchars($item['link']), htmlspecialchars($item['title']), htmlspecialchars($thumbnail), htmlspecialchars($item['title']));
  2371. }
  2372. return sprintf('<a href="%s">%s</a>', htmlspecialchars($item['link']), htmlspecialchars($item['title']));
  2373. }
  2374. function get_label_class($key)
  2375. {
  2376. return $this->get_constant('LABEL');
  2377. }
  2378. function get_label($event, $options=array())
  2379. {
  2380. $cls = $this->get_label_class($event->key);
  2381. return new $cls($this, $event, $options);
  2382. }
  2383. function render($event, $options=array())
  2384. {
  2385. $lifestream = $this->lifestream;
  2386. $label_inst = $event->get_label_instance($options);
  2387. if ($event->is_grouped && count($event->data) == 1 && $this->get_constant('MUST_GROUP')) $visible = true;
  2388. else $visible = isset($options['show_details']) ? !empty($options['show_details']) : null;
  2389. if ($visible === null) $visible = !$this->lifestream->get_option('hide_details_default');
  2390. $filename = $label_inst->get_template();
  2391. require($this->lifestream->get_theme_filepath('templates/'.$filename.'.inc.php'));
  2392. }
  2393. function get_events($limit=50, $offset=0)
  2394. {
  2395. global $wpdb;
  2396. if (!$this->id) return false;
  2397. if (!($limit > 0) || !($offset >= 0)) return false;
  2398. $results =& $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.`feed_id` = %d ORDER BY t1.`timestamp` DESC LIMIT %d, %d", $this->id, $offset, $limit));
  2399. $events = array();
  2400. foreach ($results as &$result)
  2401. {
  2402. $events[] = new Lifestream_Event($this->lifestream, $result);
  2403. }
  2404. return $events;
  2405. }
  2406. }
  2407. class Lifestream_InvalidExtension extends Lifestream_Extension
  2408. {
  2409. const NAME = '(The extension could not be found)';
  2410. function get_url()
  2411. {
  2412. return $this->feed;
  2413. }
  2414. function fetch()
  2415. {
  2416. return;
  2417. }
  2418. }
  2419. /**
  2420. * Generic RSS/Atom feed extension.
  2421. */
  2422. class Lifestream_Feed extends Lifestream_Extension
  2423. {
  2424. function save_options($validate=true)
  2425. {
  2426. $urls = $this->get_url();
  2427. if (!is_array($urls)) $urls = array($urls);
  2428. $url = $urls[0];
  2429. if (is_array($url)) $url = $url[0];
  2430. $feed = new SimplePie();
  2431. $feed->enable_cache(false);
  2432. if ($validate)
  2433. {
  2434. $data = $this->lifestream->file_get_contents($url);
  2435. $feed->set_raw_data($data);
  2436. $feed->enable_order_by_date(false);
  2437. $feed->force_feed(true);
  2438. $success = $feed->init();
  2439. }
  2440. if ($this->get_option('auto_icon') && ($url = $feed->get_favicon()))
  2441. {
  2442. if ($this->lifestream->validate_image($url))
  2443. {
  2444. $this->update_option('icon_url', $url);
  2445. }
  2446. else
  2447. {
  2448. $this->update_option('icon_url', '');
  2449. }
  2450. }
  2451. // elseif ($this->get_option('icon_url'))
  2452. // {
  2453. // if (!$this->lifestream->validate_image($this->get_option('icon_url')))
  2454. // {
  2455. // throw new Lifestream_Error($this->lifestream->__('The icon url is not a valid image.'));
  2456. // }
  2457. // }
  2458. parent::save_options();
  2459. }
  2460. /**
  2461. * Fetches all current events from this extension.
  2462. * @return {Array} List of events.
  2463. */
  2464. function fetch($urls=null, $initial=false)
  2465. {
  2466. if (!$urls) $urls = $this->get_url();
  2467. if (!is_array($urls)) $urls = array($urls);
  2468. $items = array();
  2469. foreach ($urls as $url_data)
  2470. {
  2471. if (is_array($url_data))
  2472. {
  2473. // url, key
  2474. list($url, $key) = $url_data;
  2475. }
  2476. else
  2477. {
  2478. $url = $url_data;
  2479. $key = '';
  2480. }
  2481. $feed = new SimplePie();
  2482. $feed->enable_cache(false);
  2483. $data = $this->lifestream->file_get_contents($url);
  2484. $feed->set_raw_data($data);
  2485. $feed->enable_order_by_date(false);
  2486. $feed->force_feed(true);
  2487. $success = $feed->init();
  2488. if (!$success)
  2489. {
  2490. $sample = substr($data, 0, 150);
  2491. throw new Lifestream_FeedFetchError("Error fetching feed from {$url} ({$feed->error()})....\n\n{$sample}");
  2492. }
  2493. $feed->handle_content_type();
  2494. foreach ($feed->get_items() as $row)
  2495. {
  2496. $rows =& $this->yield_many($row, $url, $key);
  2497. foreach ($rows as $row)
  2498. {
  2499. if (!$row) continue;
  2500. if (!$row['key']) $row['key'] = $key;
  2501. if (count($row)) $items[] = $row;
  2502. }
  2503. }
  2504. $feed->__destruct();
  2505. unset($feed);
  2506. }
  2507. return $items;
  2508. }
  2509. function yield($row, $url, $key)
  2510. {
  2511. // date and link are required
  2512. // the rest of the data will be serialized into a `data` field
  2513. // and is pulled out and used on the render($row) method
  2514. $title = $row->get_title();
  2515. if (!$title) return false;
  2516. $data = array(
  2517. 'date' => $row->get_date('U'),
  2518. 'link' => $this->lifestream->html_entity_decode($row->get_link()),
  2519. 'title' => $this->lifestream->html_entity_decode($title),
  2520. 'description' => $this->lifestream->html_entity_decode($row->get_description()),
  2521. 'key' => $key,
  2522. 'guid' => $row->get_id(),
  2523. );
  2524. if ($enclosure = $row->get_enclosure())
  2525. {
  2526. if ($thumbnail = $enclosure->get_thumbnail())
  2527. {
  2528. $data['thumbnail'] = $thumbnail;
  2529. }
  2530. if ($image = $enclosure->get_medium())
  2531. {
  2532. $data['image'] = $image;
  2533. }
  2534. elseif ($image = $enclosure->get_link())
  2535. {
  2536. $data['image'] = $image;
  2537. }
  2538. if (!$data['key']) $data['key'] = 'photo';
  2539. }
  2540. return $data;
  2541. }
  2542. function get_url()
  2543. {
  2544. return $this->get_option('url');
  2545. }
  2546. function parse_urls($text)
  2547. {
  2548. # match http(s):// urls
  2549. $text = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w\=/\~_\.\%\-]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>', $text);
  2550. # match www urls
  2551. $text = preg_replace('@((?<!http://)www\.([-\w\.]+)+(:\d+)?(/([\w/\=\~_\.\%\-]*(\?\S+)?)?)?)@', '<a href="http://$1">$1</a>', $text);
  2552. # match email@address
  2553. $text = preg_replace('/\b([A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4})\b/i', '<a href="mailto:$1">$1</a>', $text);
  2554. return $text;
  2555. }
  2556. }
  2557. /**
  2558. * You need to pass a thumbnail item in yield() for PhotoFeed item's
  2559. */
  2560. class Lifestream_PhotoFeed extends Lifestream_Feed
  2561. {
  2562. const LABEL = 'Lifestream_PhotoLabel';
  2563. const MUST_GROUP = true;
  2564. }
  2565. class Lifestream_GenericFeed extends Lifestream_Feed {
  2566. const DESCRIPTION = 'The generic feed can handle both feeds with images (in enclosures), as well as your standard text based RSS and Atom feeds.';
  2567. function get_options()
  2568. {
  2569. return array(
  2570. 'url' => array($this->lifestream->__('Feed URL:'), true, '', ''),
  2571. );
  2572. }
  2573. function get_public_url()
  2574. {
  2575. return $this->get_option('url');
  2576. }
  2577. function get_label($event, $options)
  2578. {
  2579. if ($event->key == 'photo') $cls = Lifestream_PhotoFeed::LABEL;
  2580. else $cls = $this->get_constant('LABEL');
  2581. return new $cls($this, $event, $options);
  2582. }
  2583. }
  2584. $lifestream->register_feed('Lifestream_GenericFeed');
  2585. /**
  2586. * Outputs the recent lifestream events.
  2587. * @param {Array} $args An array of keyword args.
  2588. */
  2589. function lifestream($args=array())
  2590. {
  2591. global $lifestream;
  2592. setlocale(LC_ALL, WPLANG);
  2593. $_ = func_get_args();
  2594. $defaults = array(
  2595. 'id' => $lifestream->generate_unique_id(),
  2596. 'limit' => $lifestream->get_option('number_of_items'),
  2597. );
  2598. if (@$_[0] && !is_array($_[0]))
  2599. {
  2600. // old style
  2601. $_ = array(
  2602. 'limit' => @$_[0],
  2603. 'feed_ids' => @$_[1],
  2604. 'date_interval' => @$_[2],
  2605. 'user_ids' => @$_[4],
  2606. );
  2607. foreach ($_ as $key=>$value)
  2608. {
  2609. if ($value == null) unset($_[$key]);
  2610. }
  2611. }
  2612. else
  2613. {
  2614. $_ = $args;
  2615. }
  2616. $page = $lifestream->get_page_from_request();
  2617. $defaults['offset'] = ($page-1)*(!empty($_['limit']) ? $_['limit'] : $defaults['limit']);
  2618. $_ = array_merge($defaults, $_);
  2619. $limit = $_['limit'];
  2620. $_['limit'] = $_['limit'] + 1;
  2621. $options =& $_;
  2622. // TODO: offset
  2623. //$offset = $lifestream->get_option('lifestream_timezone');
  2624. $events = call_user_func(array(&$lifestream, 'get_events'), $_);
  2625. $has_next_page = (count($events) > $limit);
  2626. if ($has_next_page) {
  2627. $events = array_slice($events, 0, $limit);
  2628. }
  2629. $has_prev_page = ($page > 1);
  2630. $has_paging = ($has_next_page || $has_prev_page);
  2631. $show_metadata = empty($options['hide_metadata']);
  2632. require($lifestream->get_theme_filepath('main.inc.php'));
  2633. echo '<!-- Powered by Lifestream (version: '.LIFESTREAM_VERSION.'; theme: '.$lifestream->get_option('theme', 'default').'; iconset: '.$lifestream->get_option('icons', 'default').') -->';
  2634. if ($lifestream->get_option('show_credits') == '1')
  2635. {
  2636. echo '<p class="lifestream_credits"><small>'.$lifestream->credits().'</small></p>';
  2637. }
  2638. }
  2639. function lifestream_sidebar_widget($_=array())
  2640. {
  2641. global $lifestream;
  2642. setlocale(LC_ALL, WPLANG);
  2643. $defaults = array(
  2644. 'limit' => 10,
  2645. 'break_groups' => true,
  2646. 'show_details' => false,
  2647. );
  2648. $_ = array_merge($defaults, $_);
  2649. $_['id'] = $lifestream->generate_unique_id();
  2650. $options =& $_;
  2651. // TODO: offset
  2652. //$offset = $lifestream->get_option('lifestream_timezone');
  2653. $events = call_user_func(array(&$lifestream, 'get_events'), $_);
  2654. $show_metadata = empty($options['hide_metadata']);
  2655. require($lifestream->get_theme_filepath('sidebar.inc.php'));
  2656. }
  2657. function lifestream_register_feed($class_name)
  2658. {
  2659. global $lifestream;
  2660. $lifestream->register_feed($class_name);
  2661. }
  2662. // built-in feeds
  2663. //include(LIFESTREAM_PATH . '/inc/extensions.php');
  2664. // legacy local_feeds
  2665. // PLEASE READ extensions/README
  2666. @include(LIFESTREAM_PATH . '/local_feeds.inc.php');
  2667. // detect external extensions in extensions/
  2668. $lifestream->detect_extensions();
  2669. $lifestream->detect_themes();
  2670. $lifestream->detect_icons();
  2671. // sort once
  2672. ksort($lifestream->feeds);
  2673. // Require more of the codebase
  2674. require_once(LIFESTREAM_PATH . '/inc/widget.php');
  2675. require_once(LIFESTREAM_PATH . '/inc/syndicate.php');
  2676. require_once(LIFESTREAM_PATH . '/inc/template.php');
  2677. ?>