Esempio n. 1
0
 /**
  * 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 = DateTime::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('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('unapproved', false) : Comments::count_by_author(User::identify()->id, Comment::status('unapproved')), 'spam_comment_count' => $user->can('manage_all_comments') ? Comments::count_total('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();
     $uinfo = $u->info;
     if (!isset($uinfo->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();
     Stack::add('admin_header_javascript', 'dashboard-js');
     $this->display('dashboard');
 }
Esempio n. 2
0
 /**
  * static delete_by_status
  * delete all the comments and commentinfo for comments with this status
  * @param mixed a comment status ID or name
  **/
 public static function delete_by_status($status)
 {
     if (!is_int($status)) {
         $status = Comment::status($status);
     }
     // first, purge all the comments
     DB::query('DELETE FROM {comments} WHERE status=?', array($status));
     // now purge any commentinfo records from those comments
     DB::query('DELETE FROM {commentinfo} WHERE comment_id NOT IN ( SELECT id FROM {comments} )');
 }
Esempio n. 3
0
 /**
  * Remove a comment type from the database
  * @param integer|string $status The type of the comment
  * @param null|integer|string $newstatus If provided, the new status to change all of the comments with the deleted status to
  */
 public static function remove_status($status, $newstatus = null)
 {
     // Delete comments of this status, delete status
     $status_id = Comment::status($status);
     if (is_null($newstatus)) {
         DB::delete(DB::table('comments'), array('status' => $status_id));
         DB::exec('DELETE FROM {commentinfo} WHERE comment_id IN (SELECT {commentinfo}.comment_id FROM {commentinfo} LEFT JOIN {comments} ON {commentinfo}.comment_id = {comments}.id WHERE {comments}.id IS NULL)');
     } else {
         DB::update(DB::table('comments'), array('status' => Comment::status($newstatus)), array('status' => $status_id));
     }
     DB::delete(DB::table('commentstatus'), array('name' => Comment::status_name($status)));
 }
Esempio n. 4
0
	/**
	 * 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;
	}
Esempio n. 5
0
 /**
  * Make sure that the state is legal.
  */
 public static function valid_state($value)
 {
     return in_array($value, array_keys(Comment::status()));
 }
Esempio n. 6
0
    echo __('Status');
    ?>
</h3>
				</div>

				<div class="panel-body" id="submitpost">
					<div id="minor-publishing">
						<div class="form-group <?php 
    echo isset($errors['status']) ? 'has-error' : '';
    ?>
">
							<?php 
    echo Form::label('status', __('Change Status'), array('class' => 'control-label'));
    ?>
							<?php 
    echo Form::select('status', Comment::status(), $post->status, array('class' => 'form-control'));
    ?>
						</div>
						<div class="form-group <?php 
    echo isset($errors['author_name']) ? 'has-error' : '';
    ?>
">
							<?php 
    echo Form::label('author_name', __('Author'), array('class' => 'control-label'));
    ?>
							<?php 
    echo Form::input('author_name', $post->user->name, array('class' => 'form-control'), 'autocomplete/user');
    ?>
						</div>

						<div class="form-group <?php 
Esempio n. 7
0
 /**
  * Handles spam deletion
  *
  * @return void
  **/
 public function action_auth_ajax_deleteall($handler)
 {
     $result = array();
     switch ($handler->handler_vars['target']) {
         case 'spam':
             if (!User::identify()->can('manage_all_comments')) {
                 Session::error(_t('You do not have permission to do that action.'));
                 break;
             }
             $total = Comments::count_total(Comment::STATUS_SPAM, FALSE);
             Comments::delete_by_status(Comment::status('spam'));
             Session::notice(sprintf(_t('Deleted all %s spam comments.'), $total));
             break;
         case 'logs':
             if (!User::identify()->can('manage_logs')) {
                 Session::error(_t('You do not have permission to do that action.'));
                 break;
             }
             $to_delete = EventLog::get(array('date' => 'any', 'nolimit' => 1));
             $count = 0;
             foreach ($to_delete as $log) {
                 $log->delete();
                 $count++;
             }
             Session::notice(sprintf(_t('Deleted all %s log entries.'), $count));
             break;
     }
     $result['messages'] = Session::messages_get(true, 'array');
     echo json_encode($result);
 }
Esempio n. 8
0
 public function action_auth_ajax_wp_import_comments()
 {
     // get the values post'd in
     $inputs = $_POST->filter_keys(array('db_name', 'db_host', 'db_user', 'db_pass', 'db_prefix', 'category_import', 'import_index'));
     $inputs = $inputs->getArrayCopy();
     // make sure we have all our default values
     $inputs = array_merge($this->default_values, $inputs);
     // get the wpdb
     $wpdb = $this->wp_connect($inputs['db_host'], $inputs['db_name'], $inputs['db_user'], $inputs['db_pass']);
     // if we couldn't connect, error out
     if (!$wpdb) {
         EventLog::log(_t('Failed to import from "%s"', array($inputs['db_name'])));
         Session::error(_t('Failed to import from "%s"', array($inputs['db_name'])));
         echo '<p>' . _t('Failed to connect using the given database connection details.') . '</p>';
     }
     // we connected just fine, let's get moving!
     // begin a transaction. if we error out at any point, we want to roll back to before import began
     DB::begin_transaction();
     // fetch the number of comments from the wordpress database so we can batch things up
     $num_comments = $wpdb->get_value('select count(comment_id) from ' . $inputs['db_prefix'] . 'comments');
     // figure out the LIMIT we're at
     $min = $inputs['import_index'] * IMPORT_BATCH;
     $max = min($min + IMPORT_BATCH, $num_comments);
     // for display only
     echo '<p>' . _t('Importing comments %1$d - %2$d of %3$d.', array($min, $max, $num_comments)) . '</p>';
     // get all the imported users so we can link old comment authors to new comment authors
     $users = DB::get_results('select user_id, value from {userinfo} where name = :name', array(':name' => 'wp_id'));
     // create an easy user map of old ID -> new ID
     $user_map = array();
     foreach ($users as $info) {
         $user_map[$info->value] = $info->user_id;
     }
     // get all the imported posts so we can link old post IDs to new post IDs
     $posts = DB::get_results('select post_id, value from {postinfo} where name = :name', array(':name' => 'wp_id'));
     // create an easy post map of old ID -> new ID
     $post_map = array();
     foreach ($posts as $info) {
         $post_map[$info->value] = $info->post_id;
     }
     // get all the comment IDs we've imported so far to make sure we don't duplicate any
     $comment_map = DB::get_column('select value from {commentinfo} where name = :name', array(':name' => 'wp_id'));
     // now we're ready to start importing comments
     $comments = $wpdb->get_results('select comment_id, comment_post_id, comment_author, comment_author_email, comment_author_url, comment_author_ip, comment_date, comment_content, comment_karma, comment_approved, comment_agent, comment_type, comment_parent, user_id from ' . $inputs['db_prefix'] . 'comments order by comment_id asc limit ' . $min . ', ' . IMPORT_BATCH);
     foreach ($comments as $comment) {
         // if this post is already in the list we've imported, skip it
         if (in_array($comment->id, $comment_map)) {
             continue;
         }
         // if the post this comment belongs to is not in the list of imported posts, skip it
         if (!isset($post_map[$comment->comment_post_id])) {
             continue;
         }
         // create the new comment
         $c = new Comment(array('content' => MultiByte::convert_encoding($comment->comment_content), 'name' => MultiByte::convert_encoding($comment->comment_author), 'email' => MultiByte::convert_encoding($comment->comment_author_email), 'url' => MultiByte::convert_encoding($comment->comment_author_url), 'date' => HabariDateTime::date_create($comment->comment_date), 'post_id' => $post_map[$comment->comment_post_id]));
         // figure out the comment type
         switch ($comment->comment_type) {
             case 'pingback':
                 $c->type = Comment::type('pingback');
                 break;
             case 'trackback':
                 $c->type = Comment::type('trackback');
                 break;
             default:
             case 'comment':
                 $c->type = Comment::type('comment');
                 break;
         }
         // figure out the comment status
         switch ($comment->comment_approved) {
             case '1':
                 $c->status = Comment::status('approved');
                 break;
             case '':
             case '0':
                 $c->status = Comment::status('unapproved');
                 break;
             case 'spam':
                 $c->status = Comment::status('spam');
                 break;
             default:
                 // Comment::status() returns false if it doesn't recognize the status type
                 $status = Comment::status($comment->comment_status);
                 // store in a temp value because if you try and set ->status to an invalid value the Comment class freaks
                 if ($status == false) {
                     // we're not importing statuses we don't recognize - continue 2 to break out of the switch and the loop and continue to the next comment
                     continue 2;
                 } else {
                     $c->status = $status;
                 }
                 break;
         }
         // save the old comment ID in info
         $c->info->wp_id = $comment->comment_id;
         // save the old post ID in info
         $c->info->wp_post_id = $comment->comment_post_id;
         // save the old comment karma - but only if it is something
         if ($comment->comment_karma != '0') {
             $c->info->wp_karma = $comment->comment_karma;
         }
         // save the old comment user agent - but only if it is something
         if ($comment->comment_agent != '') {
             $c->info->wp_agent = $comment->comment_agent;
         }
         // now that we've got all the pieces in place, save the comment
         try {
             $c->insert();
         } catch (Exception $e) {
             EventLog::log($e->getMessage(), 'err');
             echo '<p class="error">' . _t('There was an error importing comment ID %d. See the EventLog for the error message.', array($comment->comment_id));
             echo '<p>' . _t('Rolling back changes&hellip;') . '</p>';
             // rollback all changes before we return so the import hasn't changed anything yet
             DB::rollback();
             // and return so they don't get AJAX to send them on to the next step
             return false;
         }
     }
     // if we've finished without an error, commit the import
     DB::commit();
     if ($max < $num_comments) {
         // if there are more posts to import
         // get the next ajax url
         $ajax_url = URL::get('auth_ajax', array('context' => 'wp_import_comments'));
         // bump the import index by one so we get a new batch next time
         $inputs['import_index']++;
     } else {
         // display the completed message!
         EventLog::log(_t('Import completed from "%s"', array($inputs['db_name'])));
         echo '<p>' . _t('Import is complete.') . '</p>';
         return;
     }
     // and spit out ajax to send them to the next step - posts!
     echo $this->get_ajax($ajax_url, $inputs);
 }
Esempio n. 9
0
 /**
  * Activity Sparkline
  *
  * Handle activity sparkline block output
  *
  * @param Block $block The block instance to be configured
  * @param Theme $theme The active theme
  */
 public function action_block_content_activity_sparkline($block, $theme)
 {
     // Number of days to show; make this configurable
     $n_days = $block->field_load('sparkline_days');
     //
     $i = 0;
     $days = array();
     while ($i < $n_days) {
         $days[] = HabariDateTime::date_create()->modify('-' . $i . ' days');
         $i++;
     }
     $days = array_reverse($days);
     // Utils::debug( $days );
     $day_stats = array();
     foreach ($days as $day) {
         // $posts = $theme->get_posts();
         $posts = Posts::get(array('year' => $day->format('Y'), 'month' => $day->format('m'), 'day' => $day->format('d'), 'limit' => 5));
         $posts = count($posts);
         // $posts = 90;
         $comments = Comments::get(array('year' => $day->format('Y'), 'month' => $day->format('m'), 'day' => $day->format('d'), 'status' => Comment::status('approved'), 'nolimit' => true));
         $comments = count($comments);
         // $comments = 5;
         if ($posts > 0) {
             $posts = 5;
         }
         $day_stats[] = array('posts' => $posts, 'comments' => $comments, 'date' => $day);
         //
     }
     $block->days = $day_stats;
 }
Esempio n. 10
0
 public function upgrade_db_post_5112()
 {
     $this->create_base_comment_types();
     // Throw the existing values out far to avoid collisions
     DB::query('UPDATE {comments} SET status = status + 30, type = type + 30');
     // Update statuses
     $updates = array('unapproved' => 0, 'approved' => 1, 'spam' => 2, 'deleted' => 3);
     foreach ($updates as $name => $oldvalue) {
         DB::query('UPDATE {comments} SET status = :newstatus WHERE status = :oldstatus', array('newstatus' => Comment::status($name), 'oldstatus' => 30 + $oldvalue));
     }
     // Update types
     $updates = array('comment' => 0, 'pingback' => 1, 'trackback' => 2);
     foreach ($updates as $name => $oldvalue) {
         DB::query('UPDATE {comments} SET type = :newtype WHERE type = :oldtype', array('newtype' => Comment::type($name), 'oldtype' => 30 + $oldvalue));
     }
 }
Esempio n. 11
0
 private function get_comment_status()
 {
     $rand = mt_rand(1, 10);
     if ($rand > 0 && $rand <= 5) {
         // give approved the highest probability
         return Comment::status('approved');
     } else {
         if ($rand > 5 && $rand <= 6) {
             // next up is spam
             return Comment::status('spam');
         } else {
             if ($rand > 6 && $rand <= 8) {
                 // unapproved
                 return Comment::status('unapproved');
             } else {
                 // finally, deleted
                 return Comment::status('deleted');
             }
         }
     }
 }
 /**
  * 垃圾评论 
  */
 public function spamcomment()
 {
     $id = (int) $this->_get("id");
     if ($id <= 0) {
         $this->error("参数有误!");
     }
     $r = M("Comments")->where(array("id" => $id))->find();
     if ($r) {
         $Comment = new Comment();
         $status = $Comment->status($id);
         if ($status["status"]) {
             $this->success("状态转换成功!");
         } else {
             $this->error($status['info']);
         }
     } else {
         $this->error("该评论不存在!");
     }
 }
Esempio n. 13
0
 public function test_status_action()
 {
     $comment_status_actions = array(Comment::status('unapproved') => _t('Unapprove'), Comment::status('approved') => _t('Approve'), Comment::status('spam') => _t('Spam'));
     foreach ($comment_status_actions as $status => $action) {
         $this->assert_equal($action, Comment::status_action($status));
     }
 }
Esempio n. 14
0
 /**
  * Add a comment to the site
  *
  * @param mixed $post A Post object instance or Post object id
  * @param string $name The commenter's name
  * @param string $email The commenter's email address
  * @param string $url The commenter's website URL
  * @param string $content The comment content
  * @param array $extra An associative array of extra values that should be considered
  */
 function add_comment($post, $name = null, $email = null, $url = null, $content = null, $extra = null)
 {
     if (is_numeric($post)) {
         $post = Post::get(array('id' => $post));
     }
     if (!$post instanceof Post) {
         // Not sure what you're trying to pull here, but that's no good
         header('HTTP/1.1 403 Forbidden', true, 403);
         die;
     }
     /* Sanitize data */
     foreach (array('name', 'url', 'email', 'content') as $k) {
         ${$k} = InputFilter::filter(${$k});
     }
     // there should never be any HTML in the name, so do some extra filtering on it
     $name = strip_tags(html_entity_decode($name, ENT_QUOTES, 'UTF-8'));
     /* Sanitize the URL */
     if (!empty($url)) {
         $parsed = InputFilter::parse_url($url);
         if ($parsed['is_relative']) {
             // guess if they meant to use an absolute link
             $parsed = InputFilter::parse_url('http://' . $url);
             if (!$parsed['is_error']) {
                 $url = InputFilter::glue_url($parsed);
             } else {
                 // disallow relative URLs
                 $url = '';
             }
         }
         if ($parsed['is_pseudo'] || $parsed['scheme'] !== 'http' && $parsed['scheme'] !== 'https') {
             // allow only http(s) URLs
             $url = '';
         } else {
             // reconstruct the URL from the error-tolerant parsing
             // http:moeffju.net/blog/ -> http://moeffju.net/blog/
             $url = InputFilter::glue_url($parsed);
         }
     }
     /* Create comment object*/
     $comment = new Comment(array('post_id' => $post->id, 'name' => $name, 'email' => $email, 'url' => $url, 'ip' => Utils::get_ip(), 'content' => $content, 'status' => Comment::status('approved'), 'date' => DateTime::create(), 'type' => Comment::type('comment')));
     // Should this really be here or in a default filter?
     // In any case, we should let plugins modify the status after we set it here.
     $user = User::identify();
     if ($user->loggedin && $comment->email == $user->email) {
         $comment->status = 'approved';
     }
     // Allow themes to work with comment hooks
     Themes::create();
     // Allow plugins to change comment data and add commentinfo based on plugin-added form fields
     Plugins::act('comment_accepted', $comment, $this->handler_vars, $extra);
     $spam_rating = 0;
     $spam_rating = Plugins::filter('spam_filter', $spam_rating, $comment, $this->handler_vars, $extra);
     if ($spam_rating >= Options::get('spam_percentage', 100)) {
         $comment->status = 'spam';
     }
     $comment->insert();
     $anchor = '';
     // If the comment was saved
     if ($comment->id && $comment->status != 'spam') {
         $anchor = '#comment-' . $comment->id;
         // store in the user's session that this comment is pending moderation
         if ($comment->status == 'unapproved') {
             Session::notice(_t('Your comment is pending moderation.'), 'comment_' . $comment->id);
         }
         // if no cookie exists, we should set one
         // but only if the user provided some details
         $cookie_name = 'comment_' . Options::get('public-GUID');
         // build the string we store for the cookie
         $cookie_content = implode('#', array($comment->name, $comment->email, $comment->url));
         // if the user is not logged in and there is no cookie OR the cookie differs from the current set
         if (User::identify()->loggedin == false && (!isset($_COOKIE[$cookie_name]) || $_COOKIE[$cookie_name] != $cookie_content)) {
             // update the cookie
             setcookie($cookie_name, $cookie_content, time() + DateTime::YEAR, Site::get_path('base', true));
         }
     }
     // Return the commenter to the original page.
     Utils::redirect($post->permalink . $anchor);
 }