/** * Sets up the fixture, for example, opens a network connection. * This method is called before a test is executed. * * @access protected */ protected function module_setup() { $user = User::get_by_name( 'posts_test' ); if ( !$user ) { $user = User::create( array ( 'username'=>'posts_test', 'email'=>'*****@*****.**', 'password'=>md5('q' . rand( 0,65535 ) ), ) ); } $this->user = $user; $post = Post::create( array( 'title' => 'Test Post', 'content' => 'These tests expect there to be at least one post.', 'user_id' => $user->id, 'status' => Post::status( 'published' ), 'content_type' => Post::type( 'entry' ), ) ); $this->post_id = $post->id; $this->paramarray = array( 'id' => 'foofoo', 'post_id' => $this->post_id, 'name' => 'test', 'email' => '*****@*****.**', 'url' => 'http://example.org', 'ip' => ip2long('127.0.0.1'), 'content' => 'test content', 'status' => Comment::STATUS_UNAPPROVED, 'date' => HabariDateTime::date_create(), 'type' => Comment::COMMENT ); $this->comment = Comment::create( $this->paramarray ); }
/** * When a post is published, add a cron entry to do pinging * * @param Post $post A post object whose status has been set to published */ public function action_post_status_published($post) { if ($post->status == Post::status('published') && $post->pubdate <= HabariDateTime::date_create()) { CronTab::add_single_cron('ping update sites', array('Autopinger', 'ping_sites'), HabariDateTime::date_create()->int, 'Ping update sites.'); EventLog::log('Crontab added', 'info', 'default', null, null); } }
public static function check_posts($nolimit = false) { $autoclosed = array(); $age_in_days = Options::get('autoclose__age_in_days'); if (is_null($age_in_days)) { return; } $age_in_days = abs(intval($age_in_days)); $search = array('content_type' => 'entry', 'before' => HabariDateTime::date_create()->modify('-' . $age_in_days . ' days'), 'nolimit' => true, 'status' => 'published'); if (!$nolimit) { $search['after'] = HabariDateTime::date_create()->modify('-' . ($age_in_days + 30) . ' days'); } $posts = Posts::get($search); foreach ($posts as $post) { if (!$post->info->comments_disabled && !$post->info->comments_autoclosed) { $post->info->comments_disabled = true; $post->info->comments_autoclosed = true; $post->info->commit(); $autoclosed[] = sprintf('<a href="%s">%s</a>', $post->permalink, htmlspecialchars($post->title)); } } if (count($autoclosed)) { if (count($autoclosed) > 5) { Session::notice(sprintf(_t('Comments autoclosed for: %s and %d other posts', 'autoclose'), implode(', ', array_slice($autoclosed, 0, 5)), count($autoclosed) - 5)); } else { Session::notice(sprintf(_t('Comments autoclosed for: %s', 'autoclose'), implode(', ', $autoclosed))); } } else { Session::notice(sprintf(_t('Found no posts older than %d days with comments enabled.', 'autoclose'), $age_in_days)); } return true; }
/** * Handles get requests for the dashboard * @todo update check should probably be cron'd and cached, not re-checked every load */ public function get_dashboard() { // Not sure how best to determine this yet, maybe set an option on install, maybe do this: $firstpostdate = DB::get_value('SELECT min(pubdate) FROM {posts} WHERE status = ?', array(Post::status('published'))); $this->theme->active_time = HabariDateTime::date_create($firstpostdate); // get the active theme, so we can check it // @todo this should be worked into the main Update::check() code for registering beacons $active_theme = Themes::get_active(); $active_theme = $active_theme->name . ':' . $active_theme->version; // check to see if we have updates to display $this->theme->updates = Options::get('updates_available', array()); // collect all the stats we display on the dashboard $this->theme->stats = array('author_count' => Users::get(array('count' => 1)), 'page_count' => Posts::get(array('count' => 1, 'content_type' => Post::type('page'), 'status' => Post::status('published'))), 'entry_count' => Posts::get(array('count' => 1, 'content_type' => Post::type('entry'), 'status' => Post::status('published'))), 'comment_count' => Comments::count_total(Comment::STATUS_APPROVED, false), 'tag_count' => Tags::vocabulary()->count_total(), 'page_draft_count' => Posts::get(array('count' => 1, 'content_type' => Post::type('page'), 'status' => Post::status('draft'), 'user_id' => User::identify()->id)), 'entry_draft_count' => Posts::get(array('count' => 1, 'content_type' => Post::type('entry'), 'status' => Post::status('draft'), 'user_id' => User::identify()->id)), 'unapproved_comment_count' => User::identify()->can('manage_all_comments') ? Comments::count_total(Comment::STATUS_UNAPPROVED, false) : Comments::count_by_author(User::identify()->id, Comment::STATUS_UNAPPROVED), 'spam_comment_count' => User::identify()->can('manage_all_comments') ? Comments::count_total(Comment::STATUS_SPAM, false) : Comments::count_by_author(User::identify()->id, Comment::STATUS_SPAM), 'user_entry_scheduled_count' => Posts::get(array('count' => 1, 'content_type' => Post::type('any'), 'status' => Post::status('scheduled'), 'user_id' => User::identify()->id))); $this->fetch_dashboard_modules(); // check for first run $u = User::identify(); if (!isset($u->info->experience_level)) { $this->theme->first_run = true; $u->info->experience_level = 'user'; $u->info->commit(); } else { $this->theme->first_run = false; } $this->display('dashboard'); }
/** * Sets up the fixture, for example, opens a network connection. * This method is called before a test is executed. * * @access protected */ protected function setUp() { $this->post_id = Post::get()->id; $this->paramarray = array('id' => 'foofoo', 'post_id' => $this->post_id, 'name' => 'test', 'email' => '*****@*****.**', 'url' => 'http://example.org', 'ip' => ip2long('127.0.0.1'), 'content' => 'test content', 'status' => Comment::STATUS_UNAPPROVED, 'date' => HabariDateTime::date_create(), 'type' => Comment::COMMENT); $this->comment = new Comment($this->paramarray); $this->comment->insert(); }
public function theme_monthly_archives_links_list($theme, $full_names = TRUE, $show_counts = TRUE, $type = 'entry', $status = 'published') { $results = Posts::get(array('content_type' => $type, 'status' => $status, 'month_cts' => 1)); $archives[] = ''; foreach ($results as $result) { // add leading zeros $result->month = str_pad($result->month, 2, 0, STR_PAD_LEFT); // what format do we want to show the month in? if ($full_names) { $display_month = HabariDateTime::date_create()->set_date($result->year, $result->month, 1)->get('F'); } else { $display_month = HabariDateTime::date_create()->set_date($result->year, $result->month, 1)->get('M'); } // do we want to show the count of posts? if ($show_counts) { $count = ' (' . $result->ct . ')'; } else { $count = ''; } $archives[] = '<li>'; $archives[] = '<a href="' . URL::get('display_entries_by_date', array('year' => $result->year, 'month' => $result->month)) . '" title="View entries in ' . $display_month . '/' . $result->year . '">' . $display_month . ' ' . $result->year . ' ' . $count . '</a>'; $archives[] = '</li>'; } $archives[] = ''; return implode("\n", $archives); }
public function act_request() { // @todo limit this to GUIDs POST'd $plugins = Posts::get(array('content_type' => 'addon', 'nolimit' => true, 'status' => Post::status('published'))); $xml = new SimpleXMLElement('<updates></updates>'); foreach ($plugins as $plugin) { // if we don't have any versions, skip this plugin if (empty($plugin->info->versions)) { //continue; } // create the beacon's node $beacon = $xml->addChild('beacon'); $beacon['id'] = $plugin->info->guid; $beacon['name'] = $plugin->title; $beacon['url'] = $plugin->permalink; $beacon['type'] = $plugin->info->type; foreach ($plugin->info->versions as $version) { // @todo limit this to only versions older than the one POST'd $update = $beacon->addChild('update', $version['description']); $update['severity'] = $version['severity']; $update['version'] = $version['version']; $update['habari_version'] = $version['habari_version']; $update['url'] = $version['url']; $update['date'] = HabariDateTime::date_create($version->date)->format('c'); } } // spit out the xml ob_clean(); // clean the output buffer header('Content-type: application/xml'); echo $xml->asXML(); }
public function filter_post_field_load($value, $key) { switch ($key) { case 'event_start': case 'event_end': return HabariDateTime::date_create($value)->text_format('{M} {j}, {Y} {g}:{i}{a}'); } return $value; }
public function action_plugin_activation($file = '') { if (Plugins::id_from_file($file) == Plugins::id_from_file(__FILE__)) { if (Options::get('database_optimizer__frequency') == null) { Options::set('database_optimizer__frequency', 'weekly'); } // add a cronjob to kick off next and optimize our db now CronTab::add_single_cron('optimize database tables initial', 'optimize_database', HabariDateTime::date_create(time()), 'Optimizes database tables.'); $this->create_cron(); } }
/** * Handles POST requests from the options admin page */ public function post_options() { $option_items = array(); $timezones = DateTimeZone::listIdentifiers(); $timezones = array_merge(array('' => ''), array_combine(array_values($timezones), array_values($timezones))); $option_items[_t('Name & Tagline')] = array('title' => array('label' => _t('Site Name'), 'type' => 'text', 'helptext' => ''), 'tagline' => array('label' => _t('Site Tagline'), 'type' => 'text', 'helptext' => ''), 'about' => array('label' => _t('About'), 'type' => 'textarea', 'helptext' => '')); $option_items[_t('Publishing')] = array('pagination' => array('label' => _t('Items per Page'), 'type' => 'text', 'helptext' => ''), 'atom_entries' => array('label' => _t('Entries to show in Atom feed'), 'type' => 'text', 'helptext' => ''), 'comments_require_id' => array('label' => _t('Require Comment Author Info'), 'type' => 'checkbox', 'helptext' => ''), 'spam_percentage' => array('label' => _t('Comment SPAM Threshold'), 'type' => 'text', 'helptext' => _t('The likelihood a comment is considered SPAM, in percent.'))); $option_items[_t('Time & Date')] = array('timezone' => array('label' => _t('Time Zone'), 'type' => 'select', 'selectarray' => $timezones, 'helptext' => _t('Current Date Time: %s', array(HabariDateTime::date_create()->format()))), 'dateformat' => array('label' => _t('Date Format'), 'type' => 'text', 'helptext' => _t('Current Date: %s', array(HabariDateTime::date_create()->date))), 'timeformat' => array('label' => _t('Time Format'), 'type' => 'text', 'helptext' => _t('Current Time: %s', array(HabariDateTime::date_create()->time)))); $option_items[_t('Language')] = array('locale' => array('label' => _t('Locale'), 'type' => 'select', 'selectarray' => array_merge(array('' => 'default'), array_combine(HabariLocale::list_all(), HabariLocale::list_all())), 'helptext' => _t('International language code')), 'system_locale' => array('label' => _t('System Locale'), 'type' => 'text', 'helptext' => _t('The appropriate locale code for your server'))); $option_items[_t('Troubleshooting')] = array('log_min_severity' => array('label' => _t('Minimum Severity'), 'type' => 'select', 'selectarray' => LogEntry::list_severities(), 'helptext' => _t('Only log entries with a this or higher severity.')), 'log_backtraces' => array('label' => _t('Log Backtraces'), 'type' => 'checkbox', 'helptext' => _t('Logs error backtraces to the log table\'s data column. Can drastically increase log size!'))); /*$option_items[_t('Presentation')] = array( 'encoding' => array( 'label' => _t('Encoding'), 'type' => 'select', 'selectarray' => array( 'UTF-8' => 'UTF-8' ), 'helptext' => '', ), );*/ $option_items = Plugins::filter('admin_option_items', $option_items); $form = new FormUI('Admin Options'); $tab_index = 3; foreach ($option_items as $name => $option_fields) { $fieldset = $form->append('wrapper', Utils::slugify(_u($name)), $name); $fieldset->class = 'container settings'; $fieldset->append('static', $name, '<h2>' . htmlentities($name, ENT_COMPAT, 'UTF-8') . '</h2>'); foreach ($option_fields as $option_name => $option) { $field = $fieldset->append($option['type'], $option_name, $option_name, $option['label']); $field->template = 'optionscontrol_' . $option['type']; $field->class = 'item clear'; if ($option['type'] == 'select' && isset($option['selectarray'])) { $field->options = $option['selectarray']; } $field->tabindex = $tab_index; $tab_index++; if (isset($option['helptext'])) { $field->helptext = $option['helptext']; } else { $field->helptext = ''; } } } /* @todo: filter for additional options from plugins * We could either use existing config forms and simply extract * the form controls, or we could create something different */ $submit = $form->append('submit', 'apply', _t('Apply'), 'admincontrol_submit'); $submit->tabindex = $tab_index; $form->on_success(array($this, 'form_options_success')); $this->theme->form = $form->get(); $this->theme->option_names = array_keys($option_items); $this->theme->display('options'); }
public function action_atom_add_post($xml, $post) { $link = $xml->addChild('link'); $link->addAttribute('rel', 'replies'); //type="application/atom+xml" is default, could be omitted //$link->addAttribute('type', 'application/atom+xml'); $link->addAttribute('href', URL::get('atom_feed_entry_comments', array('slug' => $post->slug))); $link->addAttribute('thr:count', $post->comments->approved->count, 'http://purl.org/syndication/thread/1.0'); if ($post->comments->approved->count > 0) { $link->addAttribute('thr:updated', HabariDateTime::date_create(end($post->comments->approved)->date)->get(HabariDateTime::ATOM), 'http://purl.org/syndication/thread/1.0'); } $xml->addChild('thr:total', $post->comments->approved->count, 'http://purl.org/syndication/thread/1.0'); }
public function test_create_post() { $tags = array('one', 'two', 'THREE'); $params = array('title' => 'A post title', 'content' => 'Some great content. Really.', 'user_id' => $this->user->id, 'status' => Post::status('published'), 'content_type' => Post::type('entry'), 'tags' => 'one, two, THREE', 'pubdate' => HabariDateTime::date_create(time())); $post = Post::create($params); $this->assertType('Post', $post, 'Post should be created.'); // Check the post's id is set. $this->assertGreaterThan(0, (int) $post->id, 'The Post id should be greater than zero'); // Check the post's tags are usable. $this->assertEquals(count($post->tags), count($tags), 'All tags should have been created.'); foreach ($post->tags as $tag_slug => $tag_text) { $this->assertEquals($tag_slug, Utils::slugify($tag_text), 'Tags key should be slugified tag.'); } }
protected function setUp() { set_time_limit(0); $this->posts = array(); $user = User::get_by_name('posts_test'); if (!$user) { $user = User::create(array('username' => 'posts_test', 'email' => '*****@*****.**', 'password' => md5('q' . rand(0, 65535)))); } $time = time() - 160; $this->tag_sets = array(array('one'), array('two'), array('one', 'two'), array('three')); foreach ($this->tag_sets as $tags) { $time = $time - rand(3600, 3600 * 36); $this->posts[] = Post::create(array('title' => $this->get_title(), 'content' => $this->get_content(1, 3, 'some', array('ol' => 1, 'ul' => 1), 'cat'), 'user_id' => $user->id, 'status' => Post::status('published'), 'content_type' => Post::type('entry'), 'tags' => $tags, 'pubdate' => HabariDateTime::date_create($time))); } }
protected static function fetch_backtype($url) { $backtype = array(); $cacheName = "backtype-{$url}"; if (Cache::has($cacheName)) { foreach (Cache::get($cacheName) as $cachedBacktype) { $cachedBacktype->date = HabariDateTime::date_create($cachedBacktype->date); $backtype[] = $cachedBacktype; } return $backtype; } $connectData = json_decode(file_get_contents("http://api.backtype.com/comments/connect.json?url={$url}&key=key&itemsperpage=10000")); if (isset($connectData->comments)) { foreach ($connectData->comments as $dat) { $comment = new StdClass(); switch ($dat->entry_type) { case 'tweet': $comment->id = 'backtype-twitter-' . $dat->tweet_id; $comment->url = 'http://twitter.com/' . $dat->tweet_from_user . '/status/' . $dat->tweet_id; $comment->name = '@' . $dat->tweet_from_user . ' (via Backtype: Twitter)'; $comment->content_out = InputFilter::filter($dat->tweet_text); $comment->date = $dat->tweet_created_at; break; case 'comment': $comment->id = 'backtype-comment-' . $dat->comment->id; $comment->url = $dat->comment->url; $comment->name = $dat->author->name . ' (via Backtype: ' . InputFilter::filter($dat->blog->title) . ')'; $comment->content_out = InputFilter::filter($dat->comment->content); $comment->date = $dat->comment->date; break; } if (!$comment) { continue; } $comment->status = Comment::STATUS_APPROVED; $comment->type = Comment::TRACKBACK; $comment->email = null; $backtype[] = $comment; } } Cache::set($cacheName, $backtype); return $backtype; }
/** * Return the title for the page * @return String the title. */ public function the_title($head = false) { $title = ''; //check against the matched rule switch ($this->matched_rule->name) { case 'display_404': $title = 'Error 404'; break; case 'display_entry': $title .= $this->post->title; break; case 'display_page': $title .= $this->post->title; break; case 'display_search': $title .= 'Search for ' . ucfirst($this->criteria); break; case 'display_entries_by_tag': $title .= ucfirst($this->tag) . ' Tag'; break; case 'display_entries_by_date': $title .= 'Archive for '; $archive_date = new HabariDateTime(); if (empty($date_array['day'])) { if (empty($date_array['month'])) { //Year only $archive_date->set_date($this->year, 1, 1); $title .= $archive_date->format('Y'); break; } //year and month only $archive_date->set_date($this->year, $this->month, 1); $title .= $archive_date->format('F Y'); break; } $archive_date->set_date($this->year, $this->month, $this->day); $title .= $archive_date->format('F jS, Y'); break; case 'display_home': return Options::get('title'); break; } return $title; }
public function test_delete_content_type() { Post::add_new_type( 'test_type' ); $params = array( 'title' => 'A post title', 'content' => 'Some great content. Really.', 'user_id' => $this->user->id, 'status' => Post::status('published'), 'content_type' => Post::type('test_type'), 'pubdate' => HabariDateTime::date_create( time() ), ); $post = Post::create($params); $this->assert_true( 'test_type' == $post->typename, "Post content type should be 'test_type'." ); $this->assert_false( Post::delete_post_type( 'test_type' ), "Post still exists with the content type 'test_type'" ); $post->delete(); $this->assert_true( Post::delete_post_type( 'test_type' ), "No posts exist with the content type 'test_type'" ); }
/** * Handles asyncronous cron calls. * * @todo next_cron should be the actual next run time and update it when new * crons are added instead of just maxing out at one day.. */ function act_poll_cron() { Utils::check_request_method(array('GET', 'HEAD', 'POST')); $time = doubleval($this->handler_vars['time']); if ($time != Options::get('cron_running')) { return; } // allow script to run for 10 minutes set_time_limit(600); $time = HabariDateTime::date_create(); $crons = DB::get_results('SELECT * FROM {crontab} WHERE start_time <= ? AND next_run <= ?', array($time->sql, $time->sql), 'CronJob'); if ($crons) { foreach ($crons as $cron) { $cron->execute(); } } // set the next run time to the lowest next_run OR a max of one day. $next_cron = DB::get_value('SELECT next_run FROM {crontab} ORDER BY next_run ASC LIMIT 1', array()); Options::set('next_cron', min(intval($next_cron), $time->modify('+1 day')->int)); Options::set('cron_running', false); }
private function update_tweetbacks(Post $post) { // Get the lastest tweetback in database $tweetbacks = $post->comments->tweetbacks; if ($tweetbacks->count > 0) { $tweet_url = explode('/', $tweetbacks[0]->url); $since_id = end($tweet_url); } else { $since_id = 0; } // Get short urls $aliases = array_filter((array) ShortURL::get($post)); //$aliases[] = $post->permalink; // Do not include original permalink, because Twitter Search has character limit, too. // Construct request URL $url = 'http://search.twitter.com/search.json?'; $url .= http_build_query(array('ors' => implode(' ', $aliases), 'rpp' => 50, 'since_id' => $since_id), '', '&'); // Get JSON content $call = new RemoteRequest($url); $call->set_timeout(5); $result = $call->execute(); if (Error::is_error($result)) { throw Error::raise(_t('Unable to contact Twitter.', $this->class_name)); } $response = $call->get_response_body(); // Decode JSON $obj = json_decode($response); if (isset($obj->results) && is_array($obj->results)) { $obj = $obj->results; } else { throw Error::raise(_t('Response is not correct, Twitter server may be down or API is changed.', $this->class_name)); } // Store new tweetbacks into database foreach ($obj as $tweet) { Comment::create(array('post_id' => $post->id, 'name' => $tweet->from_user, 'url' => sprintf('http://twitter.com/%1$s/status/%2$d', $tweet->from_user, $tweet->id), 'content' => $tweet->text, 'status' => Comment::STATUS_APPROVED, 'date' => HabariDateTime::date_create($tweet->created_at), 'type' => Comment::TWEETBACK)); } }
/** * Handles get requests for the dashboard * @todo update check should probably be cron'd and cached, not re-checked every load */ public function get_dashboard() { // Not sure how best to determine this yet, maybe set an option on install, maybe do this: $firstpostdate = DB::get_value('SELECT min(pubdate) FROM {posts} WHERE status = ?', array(Post::status('published'))); if ($firstpostdate) { $this->theme->active_time = HabariDateTime::date_create($firstpostdate); } // check to see if we have updates to display $this->theme->updates = Options::get('updates_available', array()); // collect all the stats we display on the dashboard $user = User::identify(); $this->theme->stats = array('author_count' => Users::get(array('count' => 1)), 'post_count' => Posts::get(array('count' => 1, 'content_type' => Post::type('any'), 'status' => Post::status('published'))), 'comment_count' => Comments::count_total(Comment::STATUS_APPROVED, false), 'tag_count' => Tags::vocabulary()->count_total(), 'user_draft_count' => Posts::get(array('count' => 1, 'content_type' => Post::type('any'), 'status' => Post::status('draft'), 'user_id' => $user->id)), 'unapproved_comment_count' => User::identify()->can('manage_all_comments') ? Comments::count_total(Comment::STATUS_UNAPPROVED, false) : Comments::count_by_author(User::identify()->id, Comment::STATUS_UNAPPROVED), 'spam_comment_count' => $user->can('manage_all_comments') ? Comments::count_total(Comment::STATUS_SPAM, false) : Comments::count_by_author($user->id, Comment::STATUS_SPAM), 'user_scheduled_count' => Posts::get(array('count' => 1, 'content_type' => Post::type('any'), 'status' => Post::status('scheduled'), 'user_id' => $user->id))); // check for first run $u = User::identify(); if (!isset($u->info->experience_level)) { $this->theme->first_run = true; $u->info->experience_level = 'user'; $u->info->commit(); } else { $this->theme->first_run = false; } $this->get_additem_form(); $this->display('dashboard'); }
/** * function update_scheduled_posts_cronjob * * Creates or recreates the cronjob to publish * scheduled posts. It is called whenever a post * is updated or created * */ public static function update_scheduled_posts_cronjob() { $min_time = DB::get_value('SELECT MIN(pubdate) FROM {posts} WHERE status = ?', array(Post::status('scheduled'))); CronTab::delete_cronjob('publish_scheduled_posts'); if ($min_time) { CronTab::add_single_cron('publish_scheduled_posts', array('Posts', 'publish_scheduled_posts'), $min_time, 'Next run: ' . HabariDateTime::date_create($min_time)->get('c')); } }
?> </span> <a href="<?php URL::out('admin', array('page' => 'comments', 'status' => $comment->status, 'year' => $comment->date->year, 'month' => $comment->date->mon)); ?> " title="<?php _e('Search for other comments from %s', array($comment->date->format('M, Y'))); ?> "><?php $comment->date->out(HabariDateTime::get_default_date_format()); ?> </a></span> <span class="time pct10 dim"><?php _e('at'); ?> <span><?php $comment->date->out(HabariDateTime::get_default_time_format()); ?> </span></span> <ul class="dropbutton"> <?php foreach ($comment->menu as $act_id => $action) { $url = str_replace('__commentid__', $comment->id, $action['url']); ?> <li class="<?php echo $act_id; if (isset($action['nodisplay']) && $action['nodisplay'] == true) { echo ' nodisplay'; } ?> "><a href="<?php
/** * function get * Returns requested comments * @param array An associated array of parameters, or a querystring * @return array An array of Comment objects, one for each query result * * <code> * $comments = comments::get( array ( "author" => "skippy" ) ); * $comments = comments::get( array ( "slug" => "first-post", "status" => "1", "orderby" => "date ASC" ) ); * </code> * **/ public static function get($paramarray = array()) { $params = array(); $fns = array('get_results', 'get_row', 'get_value'); $select = ''; // what to select -- by default, everything foreach (Comment::default_fields() as $field => $value) { $select .= '' == $select ? "{comments}.{$field}" : ", {comments}.{$field}"; } // defaults $orderby = 'date DESC'; $limit = Options::get('pagination'); // Put incoming parameters into the local scope $paramarray = Utils::get_params($paramarray); // Transact on possible multiple sets of where information that is to be OR'ed if (isset($paramarray['where']) && is_array($paramarray['where'])) { $wheresets = $paramarray['where']; } else { $wheresets = array(array()); } $wheres = array(); $joins = array(); if (isset($paramarray['where']) && is_string($paramarray['where'])) { $wheres[] = $paramarray['where']; } else { foreach ($wheresets as $paramset) { // safety mechanism to prevent empty queries $where = array('1=1'); $paramset = array_merge((array) $paramarray, (array) $paramset); if (isset($paramset['id']) && (is_numeric($paramset['id']) || is_array($paramset['id']))) { if (is_numeric($paramset['id'])) { $where[] = "{comments}.id= ?"; $params[] = $paramset['id']; } else { if (is_array($paramset['id']) && !empty($paramset['id'])) { $id_list = implode(',', $paramset['id']); // Clean up the id list - remove all non-numeric or comma information $id_list = preg_replace("/[^0-9,]/", "", $id_list); // You're paranoid, ringmaster! :P $limit = count($paramset['id']); $where[] = '{comments}.id IN (' . addslashes($id_list) . ')'; } } } if (isset($paramset['status']) && FALSE !== $paramset['status']) { if (is_array($paramset['status'])) { $paramset['status'] = array_diff($paramset['status'], array('any')); array_walk($paramset['status'], create_function('&$a,$b', '$a = Comment::status( $a );')); $where[] = "{comments}.status IN (" . Utils::placeholder_string(count($paramset['status'])) . ")"; $params = array_merge($params, $paramset['status']); } else { $where[] = "{comments}.status= ?"; $params[] = Comment::status($paramset['status']); } } if (isset($paramset['type']) && FALSE !== $paramset['type']) { if (is_array($paramset['type'])) { $paramset['type'] = array_diff($paramset['type'], array('any')); array_walk($paramset['type'], create_function('&$a,$b', '$a = Comment::type( $a );')); $where[] = "type IN (" . Utils::placeholder_string(count($paramset['type'])) . ")"; $params = array_merge($params, $paramset['type']); } else { $where[] = "type= ?"; $params[] = Comment::type($paramset['type']); } } if (isset($paramset['name'])) { $where[] = "name= ?"; $params[] = $paramset['name']; } if (isset($paramset['email'])) { $where[] = "email= ?"; $params[] = $paramset['email']; } if (isset($paramset['url'])) { $where[] = "url= ?"; $params[] = $paramset['url']; } if (isset($paramset['post_id'])) { $where[] = "{comments}.post_id= ?"; $params[] = $paramset['post_id']; } if (isset($paramset['ip'])) { $where[] = "ip= ?"; $params[] = $paramset['ip']; } /* do searching */ if (isset($paramset['post_author'])) { $joins['posts'] = ' INNER JOIN {posts} ON {comments}.post_id = {posts}.id'; if (is_array($paramset['post_author'])) { $where[] = "{posts}.user_id IN (" . implode(',', array_fill(0, count($paramset['post_author']), '?')) . ")"; $params = array_merge($params, $paramset['post_author']); } else { $where[] = '{posts}.user_id = ?'; $params[] = (string) $paramset['post_author']; } } if (isset($paramset['criteria'])) { if (isset($paramset['criteria_fields'])) { // Support 'criteria_fields' => 'author,ip' rather than 'criteria_fields' => array( 'author', 'ip' ) if (!is_array($paramset['criteria_fields']) && is_string($paramset['criteria_fields'])) { $paramset['criteria_fields'] = explode(',', $paramset['criteria_fields']); } } else { $paramset['criteria_fields'] = array('content'); } $paramset['criteria_fields'] = array_unique($paramset['criteria_fields']); preg_match_all('/(?<=")([\\p{L}\\p{N}]+[^"]*)(?=")|([\\p{L}\\p{N}]+)/u', $paramset['criteria'], $matches); $where_search = array(); foreach ($matches[0] as $word) { foreach ($paramset['criteria_fields'] as $criteria_field) { $where_search[] .= "({comments}.{$criteria_field} LIKE CONCAT('%',?,'%'))"; $params[] = $word; } } if (count($where_search) > 0) { $where[] = '(' . implode(" \nOR\n ", $where_search) . ')'; } } /* * Build the pubdate * If we've got the day, then get the date. * If we've got the month, but no date, get the month. * If we've only got the year, get the whole year. * @todo Ensure that we've actually got all the needed parts when we query on them * @todo Ensure that the value passed in is valid to insert into a SQL date (ie '04' and not '4') */ if (isset($paramset['day'])) { /* Got the full date */ $where[] = 'date BETWEEN ? AND ?'; $startDate = sprintf('%d-%02d-%02d', $paramset['year'], $paramset['month'], $paramset['day']); $startDate = HabariDateTime::date_create($startDate); $params[] = $startDate->sql; $params[] = $startDate->modify('+1 day')->sql; } elseif (isset($paramset['month'])) { $where[] = 'date BETWEEN ? AND ?'; $startDate = sprintf('%d-%02d-%02d', $paramset['year'], $paramset['month'], 1); $startDate = HabariDateTime::date_create($startDate); $params[] = $startDate->sql; $params[] = $startDate->modify('+1 month')->sql; } elseif (isset($paramset['year'])) { $where[] = 'date BETWEEN ? AND ?'; $startDate = sprintf('%d-%02d-%02d', $paramset['year'], 1, 1); $startDate = HabariDateTime::date_create($startDate); $params[] = $startDate->sql; $params[] = $startDate->modify('+1 year')->sql; } // Concatenate the WHERE clauses if (count($where) > 0) { $wheres[] = ' (' . implode(' AND ', $where) . ') '; } } } // Only show comments to which the current user has permission to read the associated post if (isset($paramset['ignore_permissions'])) { $master_perm_where = ''; } else { // This set of wheres will be used to generate a list of comment_ids that this user can read $perm_where = array(); $perm_where_denied = array(); $params_where = array(); $where = array(); // every condition here will require a join with the posts table $joins['posts'] = 'INNER JOIN {posts} ON {comments}.post_id={posts}.id'; // Get the tokens that this user is granted or denied access to read $read_tokens = isset($paramset['read_tokens']) ? $paramset['read_tokens'] : ACL::user_tokens(User::identify(), 'read', true); $deny_tokens = isset($paramset['deny_tokens']) ? $paramset['deny_tokens'] : ACL::user_tokens(User::identify(), 'deny', true); // If a user can read his own posts, let him if (User::identify()->can('own_posts', 'read')) { $perm_where['own_posts_id'] = '{posts}.user_id = ?'; $params_where[] = User::identify()->id; } // If a user can read any post type, let him if (User::identify()->can('post_any', 'read')) { $perm_where = array('post_any' => '(1=1)'); $params_where = array(); } else { // If a user can read specific post types, let him $permitted_post_types = array(); foreach (Post::list_active_post_types() as $name => $posttype) { if (User::identify()->can('post_' . Utils::slugify($name), 'read')) { $permitted_post_types[] = $posttype; } } if (count($permitted_post_types) > 0) { $perm_where[] = '{posts}.content_type IN (' . implode(',', $permitted_post_types) . ')'; } // If a user can read posts with specific tokens, let him see comments on those posts if (count($read_tokens) > 0) { $joins['post_tokens__allowed'] = ' LEFT JOIN {post_tokens} pt_allowed ON {posts}.id= pt_allowed.post_id AND pt_allowed.token_id IN (' . implode(',', $read_tokens) . ')'; $perm_where['perms_join_null'] = 'pt_allowed.post_id IS NOT NULL'; } } // If a user is denied access to all posts, do so if (User::identify()->cannot('post_any')) { $perm_where_denied = array('(0=1)'); } else { // If a user is denied read access to specific post types, deny him $denied_post_types = array(); foreach (Post::list_active_post_types() as $name => $posttype) { if (User::identify()->cannot('post_' . Utils::slugify($name))) { $denied_post_types[] = $posttype; } } if (count($denied_post_types) > 0) { $perm_where_denied[] = '{posts}.content_type NOT IN (' . implode(',', $denied_post_types) . ')'; } } // If there are granted permissions to check, add them to the where clause if (count($perm_where) == 0 && !isset($joins['post_tokens__allowed'])) { // You have no grants. You get no comments. $where['perms_granted'] = '(0=1)'; } elseif (count($perm_where) > 0) { $where['perms_granted'] = ' (' . implode(' OR ', $perm_where) . ') '; $params = array_merge($params, $params_where); } if (count($deny_tokens) > 0) { $joins['post_tokens__denied'] = ' LEFT JOIN {post_tokens} pt_denied ON {posts}.id= pt_denied.post_id AND pt_denied.token_id IN (' . implode(',', $deny_tokens) . ')'; $perm_where_denied['perms_join_null'] = 'pt_denied.post_id IS NULL'; } // If there are denied permissions to check, add them to the where clause if (count($perm_where_denied) > 0) { $where['perms_denied'] = ' (' . implode(' AND ', $perm_where_denied) . ') '; } $master_perm_where = implode(' AND ', $where); } // Get any full-query parameters $possible = array('page', 'fetch_fn', 'count', 'month_cts', 'nolimit', 'limit', 'offset', 'orderby'); foreach ($possible as $varname) { if (isset($paramarray[$varname])) { ${$varname} = $paramarray[$varname]; } } if (isset($page) && is_numeric($page)) { $offset = (intval($page) - 1) * intval($limit); } if (isset($fetch_fn)) { if (!in_array($fetch_fn, $fns)) { $fetch_fn = $fns[0]; } } else { $fetch_fn = $fns[0]; } // is a count being request? if (isset($count)) { $select = "COUNT( 1 )"; $fetch_fn = 'get_value'; $orderby = ''; } // is a count of comments by month being requested? $groupby = ''; if (isset($month_cts)) { $select = 'MONTH(FROM_UNIXTIME(date)) AS month, YEAR(FROM_UNIXTIME(date)) AS year, COUNT({comments}.id) AS ct'; $groupby = 'year, month'; $orderby = 'year, month'; } if (isset($limit)) { $limit = " LIMIT {$limit}"; if (isset($offset)) { $limit .= " OFFSET {$offset}"; } } if (isset($nolimit) || isset($month_cts)) { $limit = ''; } // Build the final SQL statement $query = ' SELECT DISTINCT ' . $select . ' FROM {comments} ' . implode(' ', $joins); if (count($wheres) > 0) { $query .= ' WHERE (' . implode(" \nOR\n ", $wheres) . ')'; $query .= $master_perm_where == '' ? '' : ' AND (' . $master_perm_where . ')'; } elseif ($master_perm_where != '') { $query .= ' WHERE (' . $master_perm_where . ')'; } $query .= $groupby == '' ? '' : ' GROUP BY ' . $groupby; $query .= ($orderby == '' ? '' : ' ORDER BY ' . $orderby) . $limit; //Utils::debug( $query, $params ); DB::set_fetch_mode(PDO::FETCH_CLASS); DB::set_fetch_class('Comment'); $results = DB::$fetch_fn($query, $params, 'Comment'); if ('get_results' != $fetch_fn) { // return the results return $results; } elseif (is_array($results)) { $c = __CLASS__; $return_value = new $c($results); $return_value->get_param_cache = $paramarray; return $return_value; } }
public function form_publish_success( FormUI $form ) { $post_id = 0; if ( isset( $this->handler_vars['id'] ) ) { $post_id = intval( $this->handler_vars['id'] ); } // If an id has been passed in, we're updating an existing post, otherwise we're creating one if ( 0 !== $post_id ) { $post = Post::get( array( 'id' => $post_id, 'status' => Post::status( 'any' ) ) ); // Verify that the post hasn't already been updated since the form was loaded if ( $post->modified != $form->modified->value ) { Session::notice( _t( 'The post %1$s was updated since you made changes. Please review those changes before overwriting them.', array( sprintf( '<a href="%1$s">\'%2$s\'</a>', $post->permalink, Utils::htmlspecialchars( $post->title ) ) ) ) ); Utils::redirect( URL::get( 'admin', 'page=publish&id=' . $post->id ) ); exit; } // REFACTOR: this is duplicated in the insert code below, move it outside of the conditions // Don't try to update form values that have been removed by plugins $expected = array('title', 'tags', 'content'); foreach ( $expected as $field ) { if ( isset( $form->$field ) ) { $post->$field = $form->$field->value; } } if ( $form->newslug->value == '' && $post->status == Post::status( 'published' ) ) { Session::notice( _t( 'A post slug cannot be empty. Keeping old slug.' ) ); } elseif ( $form->newslug->value != $form->slug->value ) { $post->slug = $form->newslug->value; } // REFACTOR: the permissions checks should go before any of this other logic // sorry, we just don't allow changing posts you don't have rights to if ( ! ACL::access_check( $post->get_access(), 'edit' ) ) { Session::error( _t( 'You don\'t have permission to edit that post' ) ); $this->get_blank(); } // sorry, we just don't allow changing content types to types you don't have rights to $user = User::identify(); $type = 'post_' . Post::type_name( $form->content_type->value ); if ( $form->content_type->value != $post->content_type && ( $user->cannot( $type ) || ! $user->can_any( array( 'own_posts' => 'edit', 'post_any' => 'edit', $type => 'edit' ) ) ) ) { Session::error( _t( 'Changing content types is not allowed' ) ); $this->get_blank(); } $post->content_type = $form->content_type->value; // if not previously published and the user wants to publish now, change the pubdate to the current date/time unless a date has been explicitly set if ( ( $post->status != Post::status( 'published' ) ) && ( $form->status->value == Post::status( 'published' ) ) && ( HabariDateTime::date_create( $form->pubdate->value )->int == $form->updated->value ) ) { $post->pubdate = HabariDateTime::date_create(); } // else let the user change the publication date. // If previously published and the new date is in the future, the post will be unpublished and scheduled. Any other status, and the post will just get the new pubdate. // This will result in the post being scheduled for future publication if the date/time is in the future and the new status is published. else { $post->pubdate = HabariDateTime::date_create( $form->pubdate->value ); } $minor = $form->minor_edit->value && ( $post->status != Post::status( 'draft' ) ); $post->status = $form->status->value; } else { // REFACTOR: don't do this here, it's duplicated in Post::create() $post = new Post(); // check the user can create new posts of the set type. $user = User::identify(); $type = 'post_' . Post::type_name( $form->content_type->value ); if ( ACL::user_cannot( $user, $type ) || ( ! ACL::user_can( $user, 'post_any', 'create' ) && ! ACL::user_can( $user, $type, 'create' ) ) ) { Session::error( _t( 'Creating that post type is denied' ) ); $this->get_blank(); } // REFACTOR: why is this on_success here? We don't even display a form $form->on_success( array( $this, 'form_publish_success' ) ); if ( HabariDateTime::date_create( $form->pubdate->value )->int != $form->updated->value ) { $post->pubdate = HabariDateTime::date_create( $form->pubdate->value ); } $postdata = array( 'slug' => $form->newslug->value, 'user_id' => User::identify()->id, 'pubdate' => $post->pubdate, 'status' => $form->status->value, 'content_type' => $form->content_type->value, ); // Don't try to add form values that have been removed by plugins $expected = array( 'title', 'tags', 'content' ); foreach ( $expected as $field ) { if ( isset( $form->$field ) ) { $postdata[$field] = $form->$field->value; } } $minor = false; // REFACTOR: consider using new Post( $postdata ) instead and call ->insert() manually $post = Post::create( $postdata ); } $post->info->comments_disabled = !$form->comments_enabled->value; // REFACTOR: admin should absolutely not have a hook for this here Plugins::act( 'publish_post', $post, $form ); // REFACTOR: we should not have to update a post we just created, this should be moved to the post-update functionality above and only called if changes have been made // alternately, perhaps call ->update() or ->insert() as appropriate here, so things that apply to each operation (like comments_disabled) can still be included once outside the conditions above $post->update( $minor ); $permalink = ( $post->status != Post::status( 'published' ) ) ? $post->permalink . '?preview=1' : $post->permalink; Session::notice( sprintf( _t( 'The post %1$s has been saved as %2$s.' ), sprintf( '<a href="%1$s">\'%2$s\'</a>', $permalink, Utils::htmlspecialchars( $post->title ) ), Post::status_name( $post->status ) ) ); Utils::redirect( URL::get( 'admin', 'page=publish&id=' . $post->id ) ); }
/** * function __set * Overrides QueryRecord __set to implement custom object properties * @param string Name of property to return * @return mixed The requested field value */ public function __set($name, $value) { switch ($name) { case 'pubdate': case 'updated': case 'modified': if (!$value instanceof HabariDateTime) { $value = HabariDateTime::date_create($value); } break; case 'tags': if ($value instanceof Terms) { return $this->tags_object = $value; } elseif (is_array($value)) { return $this->tags_object = new Terms($value); } else { return $this->tags_object = Terms::parse($value, 'Term', Tags::vocabulary()); } case 'status': return $this->setstatus($value); } return parent::__set($name, $value); }
public static function trim() { // allow an option to be set to override the log retention - in days $retention = Options::get('log_retention', 14); // default to 14 days // make it into the string we'll use $retention = '-' . intval($retention) . ' days'; // Trim the log table down $date = HabariDateTime::date_create()->modify($retention); return DB::query('DELETE FROM {log} WHERE timestamp < ?', array($date->sql)); }
/** * Receive a Pingback via XMLRPC * @param array $params An array of XMLRPC parameters from the remote call * @return string The success state of the pingback */ public function xmlrpc_pingback__ping( $params ) { try { list( $source_uri, $target_uri )= $params; // This should really be done by an Habari core function $target_parse = InputFilter::parse_url( $target_uri ); $target_stub = $target_parse['path']; $base_url = Site::get_path( 'base', true ); if ( '/' != $base_url) { $target_stub = str_replace( $base_url, '', $target_stub ); } $target_stub = trim( $target_stub, '/' ); if ( strpos( $target_stub, '?' ) !== false ) { list( $target_stub, $query_string )= explode( '?', $target_stub ); } // Can this be used as a target? $target_slug = URL::parse( $target_stub )->named_arg_values['slug']; if ( $target_slug === false ) { throw new XMLRPCException( 33 ); } // Does the target exist? $target_post = Post::get( array( 'slug' => $target_slug ) ); if ( $target_post === false ) { throw new XMLRPCException( 32 ); } // Is comment allowed? if ( $target_post->info->comments_disabled ) { throw new XMLRPCException( 33 ); } // Is this Pingback already registered? if ( Comments::get( array( 'post_id' => $target_post->id, 'url' => $source_uri, 'type' => Comment::PINGBACK ) )->count() > 0 ) { throw new XMLRPCException( 48 ); } // Retrieve source contents try { $rr = new RemoteRequest( $source_uri ); $rr->execute(); if ( ! $rr->executed() ) { throw new XMLRPCException( 16 ); } $source_contents = $rr->get_response_body(); $headers = $rr->get_response_headers(); } catch ( XMLRPCException $e ) { // catch our special type of exception and re-throw it throw $e; } catch ( Exception $e ) { throw new XMLRPCException( -32300 ); } // Encoding is converted into internal encoding. // First, detect the source string's encoding $habari_encoding = strtoupper( MultiByte::hab_encoding() ); $source_encoding = 'Windows-1252'; // Is the charset in the headers? if ( isset( $headers['Content-Type'] ) && strpos( $headers['Content-Type'], 'charset' ) !== false ) { // This regex should be changed to meet the HTTP spec at some point if ( preg_match("/charset[\x09\x0A\x0C\x0D\x20]*=[\x09\x0A\x0C\x0D\x20]*('?)([A-Za-z0-9\-\_]+)\1/i", $headers['Content-Type'], $matches ) ) { $source_encoding = strtoupper( $matches[2] ); } } // Can we tell the charset from the stream itself? else if ( ( $enc = MultiByte::detect_bom_encoding( $source_contents ) ) !== false ) { $source_encoding = $enc; } // Is the charset in a meta tag? else if ( preg_match( "/<meta[^>]+charset[\x09\x0A\x0C\x0D\x20]*=[\x09\x0A\x0C\x0D\x20]*([\"']?)([A-Za-z0-9\-\_]+)\1/i", $source_contents, $matches ) ) { $source_encoding = strtoupper( $matches[2] ); if (in_array($source_encoding, array("UTF-16", "UTF-16BE", "UTF-16LE"))) { $source_encoding = "UTF-8"; } } // Then, convert the string $ret = MultiByte::convert_encoding( $source_contents, $habari_encoding, $source_encoding ); if ( $ret !== false ) { $source_contents = $ret; } // Find the page's title preg_match( '/<title>(.*)<\/title>/is', $source_contents, $matches ); $source_title = $matches[1]; // Find the reciprocal links and their context preg_match( '/<body[^>]*>(.+)<\/body>/is', $source_contents, $matches ); $source_contents_filtered = preg_replace( '/\s{2,}/is', ' ', strip_tags( $matches[1], '<a>' ) ); // Get rid of all the non-recriprocal links $ht = new HTMLTokenizer( trim( $source_contents_filtered ) ); $set = $ht->parse(); $all_links = $set->slice( 'a', array() ); $keep_links = $set->slice( 'a', array( 'href' => $target_uri ) ); $bad_links = array_diff( $all_links, $keep_links ); foreach( $bad_links as $link ) { $link->tokenize_replace( '' ); $set->replace_slice( $link ); } $source_contents_filtered = (string)$set; // Get the excerpt if ( !preg_match( '%.{0,100}?<a[^>]*?href\\s*=\\s*("|\'|)' . $target_uri . '\\1[^>]*?'.'>(.+?)</a>.{0,100}%s', $source_contents_filtered, $source_excerpt ) ) { throw new XMLRPCException( 17 ); } /** Sanitize Data */ $source_excerpt = '…' . InputFilter::filter( $source_excerpt[0] ) . '…'; $source_title = InputFilter::filter($source_title); $source_uri = InputFilter::filter($source_uri); /* Sanitize the URL */ if (!empty($source_uri)) { $parsed = InputFilter::parse_url( $source_uri ); if ( $parsed['is_relative'] ) { // guess if they meant to use an absolute link $parsed = InputFilter::parse_url( 'http://' . $source_uri ); if ( ! $parsed['is_error'] ) { $source_uri = InputFilter::glue_url( $parsed ); } else { // disallow relative URLs $source_uri = ''; } } if ( $parsed['is_pseudo'] || ( $parsed['scheme'] !== 'http' && $parsed['scheme'] !== 'https' ) ) { // allow only http(s) URLs $source_uri = ''; } else { // reconstruct the URL from the error-tolerant parsing // http:moeffju.net/blog/ -> http://moeffju.net/blog/ $source_uri = InputFilter::glue_url( $parsed ); } } // Add a new pingback comment $pingback = new Comment( array( 'post_id' => $target_post->id, 'name' => $source_title, 'email' => '', 'url' => $source_uri, 'ip' => Utils::get_ip(), 'content' => $source_excerpt, 'status' => Comment::STATUS_UNAPPROVED, 'date' => HabariDateTime::date_create(), 'type' => Comment::PINGBACK, ) ); $pingback->insert(); // Respond to the Pingback return 'The pingback has been registered'; } catch ( XMLRPCException $e ) { $e->output_fault_xml(); } }
protected function get_hash($commentid) { return substr(md5($commentid . $_SERVER['REMOTE_ADDR'] . Options::get('GUID') . HabariDateTime::date_create()->yday), 0, 6); }
/** * Outputs cached blocvk image. * * @param AjaxHandler $handler The AjaxHandler hadling the request. */ public function action_auth_ajax_piwik_graph(AjaxHandler $handler) { $api_url = urldecode($handler->handler_vars->raw('id')); $expires = HabariDateTime::date_create('midnight + 1 day')->int - time(); if (!Cache::has('piwik_graphs_' . $api_url)) { // Cache until midnight. Cache::set('piwik_graphs_' . $api_url, base64_encode(RemoteRequest::get_contents($api_url)), $expires); } // Serve the cached image. header('content-type: image/png'); header('Expires: ' . gmdate("D, d M Y H:i:s", time() + $expires) . ' GMT'); echo base64_decode(Cache::get('piwik_graphs_' . $api_url)); }
</a> <ul class="comment-authors"> <?php foreach ($post['comments'] as $comment) { ?> <li><a style="color:<?php echo $comment['color']; ?> " href="<?php echo $comment['comment']->post->permalink; ?> #comment-<?php echo $comment['comment']->id; ?> " title="<?php printf(_t('Posted at %1$s', 'binadamu'), HabariDateTime::date_create($comment['comment']->date)->get('g:m a \\o\\n F jS, Y')); ?> "><?php echo $comment['comment']->name; ?> </a></li> <?php } ?> </ul> </li> <?php } ?> </ul> </li>
/** * Output an Atom collection of comments based on the supplied parameters. * * @param array $params An array of parameters passed to Comments::get() to retrieve comments */ function get_comments( $params = array() ) { $comments = null; $comments_count = null; // Assign self link. $self = ''; // Assign alternate link. $alternate = ''; $updated = HabariDateTime::date_create(); // Check if this is a feed for a single post if ( isset( $params['slug'] ) || isset( $params['id'] ) ) { if ( isset( $params['slug'] ) ) { $post = Post::get( array( 'slug' => $params['slug'] ) ); } elseif ( isset( $params['id'] ) ) { $post = Post::get( array( 'id' => $params['id'] ) ); } // If the post doesn't exist, send a 404 if ( !$post instanceOf Post ) { header( 'HTTP/1.1 404 Not Found', true, 404 ); die('The post could not be found'); } $comments = $post->comments->approved; $comments_count = count( $comments ); $content_type = Post::type_name( $post->content_type ); $self = URL::get( "atom_feed_{$content_type}_comments", $post, false ); $alternate = URL::get( "display_{$content_type}", $post, false ); if ( $comments_count ) { $updated = $comments[$comments_count - 1]->date; } } else { $self = URL::get( 'atom_feed_comments' ); $alternate = URL::get( 'display_home' ); $params['status'] = Comment::STATUS_APPROVED; $comments = Comments::get( $params ); $comments_count = Comments::count_total( Comment::status( 'approved' ) ); if ( $comments_count ) { $updated = $comments[0]->date; } } $id = isset( $params['slug'] ) ? $params['slug'] : 'atom_comments'; $xml = $this->create_atom_wrapper( $alternate, $self, $id, $updated ); $xml = $this->add_pagination_links( $xml, $comments_count ); $xml = $this->add_comments( $xml, $comments ); Plugins::act( 'atom_get_comments', $xml, $params, $this->handler_vars ); $xml = $xml->asXML(); ob_clean(); header( 'Content-Type: application/atom+xml' ); print $xml; }