/** * 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'); }
public function test_delete_block() { $params = array('title' => $this->title, 'type' => $this->type); $block = new Block($params); $block->insert(); $count = DB::get_value('SELECT count(*) FROM {blocks}'); $block->delete(); $this->assert_equal($count - 1, DB::get_value('SELECT count(*) FROM {blocks}'), 'Count of blocks should decrease by one'); }
/** * Removes a logging type from the lookup table * * @param string $type The type of the error * @param string $module The module of the error */ public static function unregister_type($type = 'default', $module = null) { $id = DB::get_value("SELECT id FROM {log_types} WHERE module = ? and type = ?", array(self::get_module($module), $type)); if ($id) { if (!DB::exists('{log}', array('type_id' => $id))) { DB::delete('{log_types}', array('id' => $id)); } } }
function test_createduplicategroup() { // Can I create two groups with the same name? $group = UserGroup::create('name=new dupe group'); $group2 = UserGroup::create('name=new dupe group'); assert($group2 instanceof UserGroup); $this->assert_true(DB::get_value('SELECT count(*) FROM {groups} WHERE name = ?', array('new dupe group')) == 1, 'Was able to create two groups with the same name.'); $group->delete(); $group2->delete(); }
public function test_delete() { $group = UserGroup::get("new test group"); Plugins::register(array($this, 'filter_usergroup_delete_allow'), 'filter', 'usergroup_delete_allow'); $this->assert_true($group instanceof UserGroup, 'Could not retrieve group named "new test group".'); $this->allow_filter = false; $group->delete(); $this->assert_false(DB::get_value('SELECT count(*) FROM {groups} WHERE name = ?', array('new test group')) == 0, 'Was able to delete a group despite not being allowed to do so.'); $this->allow_filter = true; $group->delete(); $this->assert_true(DB::get_value('SELECT count(*) FROM {groups} WHERE name = ?', array('new test group')) == 0, 'Was not able to delete a created group.'); $group = UserGroup::get("new test group"); $this->assert_false($group instanceof UserGroup, 'Was able to retrieve (deleted) group named "new test group".'); }
/** * Add a new cron job to the DB. * * @see CronJob * @param array $paramarray A paramarray of cron job feilds. * @return \Habari\CronJob */ static function add_cron($paramarray) { // Delete any existing job with this same name if ($job = CronTab::get_cronjob($paramarray['name'])) { $job->delete(); } $cron = new CronJob($paramarray); $result = $cron->insert(); //If the new cron should run earlier than the others, rest next_cron to its strat time. $next_cron = DB::get_value('SELECT next_run FROM {crontab} ORDER BY next_run ASC LIMIT 1', array()); if (intval(Options::get('next_cron')) > intval($next_cron)) { Options::set('next_cron', $next_cron); } return $result ? $cron : false; }
/** * Add this block to a particular area in the theme * * @param string $area The name of the area to add to * @param integer $order The position of the block within the area * @param string $scope The scope id into which to add this block */ public function add_to_area($area, $order = null, $scope = null) { if (is_null($scope)) { $scope = 0; } if (is_null($order) || !is_int($order)) { $order = DB::get_value('SELECT max(display_order) + 1 FROM {blocks_areas} WHERE area = :area AND scope_id = :scope', array('area' => $area, 'scope' => $scope)); if (is_null($order)) { $order = 1; } } else { DB::query('UPDATE {blocks_areas} SET display_order = display_order + 1 WHERE area = :area AND scope_id = :scope AND display_order >= :order', array('area' => $area, 'scope' => $scope, 'order' => $order)); } // If the block isn't saved in the database, insert it. if (!$this->id) { $this->insert(); } $result = DB::query('INSERT INTO {blocks_areas} (block_id, area, scope_id, display_order) VALUES (:block_id, :area, :scope_id, :display_order)', array('block_id' => $this->id, 'area' => $area, 'scope_id' => $scope, 'display_order' => $order)); }
/** * 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 (intval($firstpostdate) !== 0) { $firstpostdate = time() - $firstpostdate; } $this->theme->active_time = array('years' => floor($firstpostdate / 31556736), 'months' => floor($firstpostdate % 31556736 / 2629728), 'days' => round($firstpostdate % 2629728 / 86400)); // get the active theme, so we can check it $active_theme = Themes::get_active(); $active_theme = $active_theme->name . ':' . $active_theme->version; // if the active plugin list has changed, expire the updates cache if (Cache::has('dashboard_updates') && Cache::get('dashboard_updates_plugins') != Options::get('active_plugins')) { Cache::expire('dashboard_updates'); } // if the theme version has changed, expire the updates cache if (Cache::has('dashboard_updates') && Cache::get('dashboard_updates_theme') != $active_theme) { Cache::expire('dashboard_updates'); } /* * Check for updates to core and any hooked plugins * cache the output so we don't make a request every load but can still display updates */ if (Cache::has('dashboard_updates')) { $this->theme->updates = Cache::get('dashboard_updates'); } else { $updates = Update::check(); if (!Error::is_error($updates)) { Cache::set('dashboard_updates', $updates); $this->theme->updates = $updates; // cache the set of plugins we just used to check for Cache::set('dashboard_updates_plugins', Options::get('active_plugins')); // cache the active theme we just used to check for Cache::set('dashboard_updates_theme', $active_theme); } else { $this->theme->updates = array(); } } $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::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'); }
/** * Displays blocks associated to the specified area and current scope. * * @param Theme $theme The theme with which this area will be output * @param string $area The area to which blocks will be output * @param string $context The area of context within the theme that could adjust the template used * @param string $scope Used to force a specific scope * @return string the output of all the blocks */ public function theme_area($theme, $area, $context = null, $scope = null) { // This array would normally come from the database via: $scopes = $this->get_scopes($area); $active_scope = 0; foreach ($scopes as $scope_id => $scope_object) { if (is_null($scope) && $this->check_scope_criteria($scope_object->criteria) || $scope == $scope_object->name) { $scope_block_count = DB::get_value('SELECT count( *) FROM {blocks_areas} ba WHERE ba.scope_id = ?', array($scope_object->id)); if ($scope_block_count > 0) { $active_scope = $scope_object->id; } break; } } $area_blocks = $this->get_blocks($area, $active_scope, $theme); $this->area = $area; if (isset($context)) { $this->context[] = $context; } // This is the block wrapper fallback template list $fallback = array($area . '.blockwrapper', 'blockwrapper', 'content'); if (!is_null($context)) { array_unshift($fallback, $context . '.blockwrapper'); array_unshift($fallback, $context . '.' . $area . '.blockwrapper'); } $output = ''; $i = 0; foreach ($area_blocks as $block_instance_id => $block) { // Temporarily set some values into the block $block->_area = $area; $block->_instance_id = $block_instance_id; $block->_area_index = $i++; $block->_fallback = $fallback; $hook = 'block_content_' . $block->type; Plugins::act($hook, $block, $this); Plugins::act('block_content', $block, $this); $block->_content = implode('', $this->content_return($block, $context)); if (trim($block->_content) == '') { unset($area_blocks[$block_instance_id]); } } // Potentially render each block inside of a wrapper. reset($area_blocks); $firstkey = key($area_blocks); end($area_blocks); $lastkey = key($area_blocks); foreach ($area_blocks as $block_instance_id => $block) { $block->_first = $block_instance_id == $firstkey; $block->_last = $block_instance_id == $lastkey; // Set up the theme for the wrapper $this->block = $block; $this->content = $block->_content; // This pattern renders the block inside the wrapper template only if a matching template exists $newoutput = $this->display_fallback($fallback, 'fetch'); if ($newoutput === false) { $output .= $block->_content; } else { $output .= $newoutput; } // Remove temporary values from the block so they're not saved to the database unset($block->_area); unset($block->_instance_id); unset($block->_area_index); unset($block->_first); unset($block->_last); } if (trim($output) != '') { // This is the area fallback template list $fallback = array($context . '.area.' . $area, $context . '.area', 'area.' . $area, 'area'); $this->content = $output; $newoutput = $this->display_fallback($fallback, 'fetch'); if ($newoutput !== false) { $output = $newoutput; } } $this->area = ''; if (isset($context)) { array_pop($this->context); } return $output; }
/** * Add a new cron job to the DB. * * @see CronJob * @param array $paramarray A paramarray of cron job feilds. */ static function add_cron($paramarray) { $cron = new CronJob($paramarray); $result = $cron->insert(); //If the new cron should run earlier than the others, rest next_cron to its strat time. $next_cron = DB::get_value('SELECT next_run FROM {crontab} ORDER BY next_run ASC LIMIT 1', array()); if (intval(Options::get('next_cron')) > intval($next_cron)) { Options::set('next_cron', $next_cron); } return $result; }
public function test_object_type() { $name = 'unit_test'; Vocabulary::add_object_type( $name ); $sql_id = DB::get_value( "SELECT id FROM {object_types} WHERE name = :vocab_name", array( 'vocab_name' => $name ) ); $id = Vocabulary::object_type_id( $name ); $this->assert_equal( $sql_id, $id, 'The sql id should equal the id returned.' ); DB::delete( '{object_types}', array( 'name' => $name ) ); }
$collect = array('threadBuy' => array()); $callbacks = array('pw_bbs_posts' => '_callbackPosts', 'pw_bbs_threads_buy' => '_callbackThreadsBuy'); list($_srcTable, $_subT) = getSubTable('pw_posts', $db_plist); $nextId = transferDataByPk($_srcTable, $associateFields, 'pid', $lastId, $limit, $callbacks); if ($collect['threadBuy']) { $sql = 'INSERT INTO pw_bbs_threads_buy (`tid`, `pid`, `created_userid`, `created_time`, `ctype`, `cost`) VALUES %s'; $targetDb->query(sprintf($sql, implode(',', $collect['threadBuy']))); } //分表 calculatePercent("SELECT COUNT(*) FROM {$_srcTable}", "SELECT COUNT(*) FROM {$_srcTable} WHERE pid <= {$nextId}"); $db_plist && ($_subTMessage = $_srcTable); //分表跳转 refreshSubTableTo('truncateGThreadsTmp', $db_plist, $_subT); } elseif ('truncateGThreadsTmp' == $action) { //转移群组数据到版块 "群组升级"" 分类 "群组话题" 版块 $data = $targetDb->get_value("SELECT count(*) FROM tmp_group_to_thread"); if ($data == 0) { refreshTo('favors'); } $sql = "INSERT INTO pw_bbs_forum (`parentid`, `type`, `name`, `hassub`, `isshow`, `created_time`) VALUES (0, 'category', '群组升级', 1, 1, {$timestamp})"; $targetDb->query($sql); $categoryid = $targetDb->insert_id(); $sql = "INSERT INTO pw_bbs_forum (`parentid`, `type`, `name`, `isshow`, `newtime`, `created_time`) VALUES ({$categoryid}, 'forum', '群组话题', 1, 3, {$timestamp})"; $targetDb->query($sql); $fid = $targetDb->insert_id(); $targetDb->query(sprintf("REPLACE INTO tmp_group_to_thread SET tid = 0, cid = %d, fid = %d", $categoryid, $fid)); refreshTo('truncateGThreadsTmpDo'); } elseif ('truncateGThreadsTmpDo' == $action) { $data = $targetDb->get_one("SELECT * FROM tmp_group_to_thread WHERE tid=0"); if (!$data) { refreshTo('favors');
/** * function act_comment_insert_before * This function is executed when the action "comment_insert_before" * is invoked from a Comment object. * The parent class, Plugin, handles registering the action * and hook name using the name of the function to determine * where it will be applied. * You can still register functions as hooks without using * this method, but boy, is it handy. * @param Comment The comment that will be processed before storing it in the database. **/ function action_comment_insert_before ( $comment ) { // This plugin ignores non-comments if ($comment->type != Comment::COMMENT) { return; } $spamcheck = array(); // <script> is bad, mmmkay? $comment->content = InputFilter::filter($comment->content); // first, check the commenter's name // if it's only digits, then we can discard this comment if ( preg_match( "/^\d+$/", $comment->name ) ) { $comment->status = Comment::STATUS_SPAM; $spamcheck[] = _t('Commenters with numeric names are spammy.'); } // now look at the comment text // if it's digits only, discard it $textonly = strip_tags( $comment->content ); if ( preg_match( "/^\d+$/", $textonly ) ) { $comment->status = Comment::STATUS_SPAM; $spamcheck[] = _t('Comments that are only numeric are spammy.'); } // is the content whitespaces only? if ( preg_match( "/\A\s+\z/", $textonly ) ) { $comment->status = Comment::STATUS_SPAM; $spamcheck[] = _t('Comments that are only whitespace characters are spammy.'); } // is the content the single word "array"? if ( 'array' == strtolower( $textonly ) ) { $comment->status = Comment::STATUS_SPAM; $spamcheck[] = _t('Comments that are only "array" are spammy.'); } // is the content the same as the name? if ( strtolower( $textonly ) == strtolower( $comment->name ) ) { $comment->status = Comment::STATUS_SPAM; $spamcheck[] = _t('Comments that consist of only the commenters name are spammy.'); } // a lot of spam starts with "<strong>some text...</strong>" if ( preg_match( "#^<strong>[^.]+\.\.\.</strong>#", $comment->content ) ) { $comment->status = Comment::STATUS_SPAM; $spamcheck[] = _t('Comments that start with strong text are spammy.'); } // are there more than 3 URLs posted? If so, it's almost certainly spam if ( preg_match_all( "#https?://#", strtolower( $comment->content ), $matches, PREG_SET_ORDER ) > 3 ) { $comment->status = Comment::STATUS_SPAM; $spamcheck[] = _t('There is a 3 URL limit in comments.'); } // are there more than 3 URLencoded characters in the content? if ( preg_match_all( "/%[0-9a-f]{2}/", strtolower( $comment->content ), $matches, PREG_SET_ORDER ) > 3 ) { $comment->status = Comment::STATUS_SPAM; $spamcheck[] = _t('There is a 3 URL-encoded character limit in comments.'); } // Was the tcount high enough? /* // This only works with special javascript running on comment form if ( empty($handlervars['tcount']) || $handlervars['tcount'] < 10 ) { $comment->status = Comment::STATUS_SPAM; $spamcheck[] = _t('Commenter did not actually type content.'); } */ // We don't allow bbcode here, silly if ( stripos($comment->content, '[url=') !== false ) { $comment->status = Comment::STATUS_SPAM; $spamcheck[] = _t('We do not accept BBCode here.'); } // Must have less than half link content $nonacontent = strip_tags(preg_replace('/<a.*?<\/a/i', '', $comment->content)); $text_length = strlen( $textonly ); if ( strlen($nonacontent) / ( $text_length == 0 ? 1 : $text_length) < 0.5 ) { $comment->status = Comment::STATUS_SPAM; $spamcheck[] = _t('Too much text that is a link compared to that which is not.'); } // Only do db checks if it's not already spam if ($comment->status != Comment::STATUS_SPAM) { $spams = DB::get_value('SELECT count(*) FROM ' . DB::table('comments') . ' WHERE status = ? AND ip = ?', array(Comment::STATUS_SPAM, $comment->ip)); // If you've already got two spams on your IP address, all you ever do is spam if ($spams > 1) { $comment->status = Comment::STATUS_SPAM; $spamcheck[] = sprintf(_t('Too many existing spams from this IP: %s'), $comment->ip); } } // Any commenter that takes longer than the session timeout is automatically moderated if (!isset($_SESSION['comments_allowed']) || ! in_array(Controller::get_var('ccode'), $_SESSION['comments_allowed'])) { $comment->status = Comment::STATUS_UNAPPROVED; $spamcheck[] = _t("The commenter's session timed out."); } if ( isset($comment->info->spamcheck) && is_array($comment->info->spamcheck)) { $comment->info->spamcheck = array_unique(array_merge($comment->info->spamcheck, $spamcheck)); } else { $comment->info->spamcheck = $spamcheck; } // otherwise everything looks good // so continue processing the comment return; }
/** * Moves a term within the vocabulary. Returns a Term object. null parameters append the term to the end of any hierarchies. * @return Term The Term object moved **/ public function move_term($term, $target_term = null, $before = FALSE) { // We assume that the arguments passed are valid terms. Check them before calling this. // If there are terms in the vocabulary, work out the reference point if (!$this->is_empty()) { $source_left = $term->mptt_left; $source_right = $term->mptt_right; $range = $source_right - $source_left + 1; $nodes_moving = $range / 2; // parent and descendants DB::begin_transaction(); // Move the source nodes out of the way by making mptt_left and mptt_right negative $source_to_temp = $source_right + 1; // move the terms so that the rightmost one's mptt_right is -1 $params = array('displacement' => $source_to_temp, 'vocab_id' => $this->id, 'range_left' => $source_left, 'range_right' => $source_right); $res = DB::query(' UPDATE {terms} SET mptt_left = mptt_left - :displacement, mptt_right = mptt_right - :displacement WHERE vocabulary_id = :vocab_id AND mptt_left BETWEEN :range_left AND :range_right ', $params); if (!$res) { DB::rollback(); return FALSE; } // Close the gap in the tree created by moving those nodes out $params = array('range' => $range, 'vocab_id' => $this->id, 'source_left' => $source_left); $res = DB::query('UPDATE {terms} SET mptt_left=mptt_left-:range WHERE vocabulary_id=:vocab_id AND mptt_left > :source_left', $params); if (!$res) { DB::rollback(); return FALSE; } $res = DB::query('UPDATE {terms} SET mptt_right=mptt_right-:range WHERE vocabulary_id=:vocab_id AND mptt_right > :source_left', $params); if (!$res) { DB::rollback(); return FALSE; } // Determine the insertion point mptt_target if ($this->hierarchical) { if (null == $target_term) { // If no target is specified, put the new term after the last term $mptt_target = DB::get_value('SELECT MAX(mptt_right) FROM {terms} WHERE vocabulary_id=?', array($this->id)) + 1; } else { if (FALSE == $before) { $mptt_target = DB::get_value('SELECT mptt_right FROM {terms} WHERE vocabulary_id=? AND id = ?', array($this->id, $target_term->id)); } else { $mptt_target = DB::get_value('SELECT mptt_left FROM {terms} WHERE vocabulary_id=? AND id = ?', array($this->id, $target_term->id)); } } } else { // vocabulary is not hierarchical if (FALSE != $before) { $mptt_target = DB::get_value('SELECT mptt_left FROM {terms} WHERE vocabulary_id=? AND id = ?', array($this->id, $target_term->id)); } else { $mptt_target = DB::get_value('SELECT MAX(mptt_right) FROM {terms} WHERE vocabulary_id=?', array($this->id)) + 1; } } $temp_left = $source_left - $source_to_temp; $temp_to_target = $mptt_target - $temp_left; // Create space in the tree for the insertion $params = array('vocab_id' => $this->id, 'range' => $range, 'mptt_target' => $mptt_target); $res = DB::query('UPDATE {terms} SET mptt_left=mptt_left+:range WHERE vocabulary_id=:vocab_id AND mptt_left >= :mptt_target', $params); if (!$res) { DB::rollback(); return FALSE; } $res = DB::query('UPDATE {terms} SET mptt_right=mptt_right+:range WHERE vocabulary_id=:vocab_id AND mptt_right >= :mptt_target', $params); if (!$res) { DB::rollback(); return FALSE; } // Move the temp nodes into the space created for them $params = array('vocab_id' => $this->id, 'temp_to_target' => $temp_to_target); $res = DB::query('UPDATE {terms} SET mptt_left=mptt_left+:temp_to_target WHERE vocabulary_id=:vocab_id AND mptt_left < 0', $params); if (!$res) { DB::rollback(); return FALSE; } $res = DB::query('UPDATE {terms} SET mptt_right=mptt_right+:temp_to_target WHERE vocabulary_id=:vocab_id AND mptt_right < 0', $params); if (!$res) { DB::rollback(); return FALSE; } // Success! DB::commit(); // @todo: need to return the updated term return $term; } return FALSE; }
/** * Returns the number of times the most used tag is used. * * @return int The number of times the most used tag is used. **/ public static function max_count() { return DB::get_value('SELECT count( t2.object_id ) AS max FROM {terms} t, {object_terms} t2 WHERE t2.term_id = t.id AND t.vocabulary_id = ? GROUP BY t.id ORDER BY max DESC LIMIT 1', array(Tags::vocabulary()->id)); }
public function internal_check($url) { $check = DB::get_value('SELECT value FROM {postinfo} WHERE name=?', array($url)); if ($check) { return $check; } }
/** * Handles AJAX requests from the dashboard */ public function ajax_dashboard($handler_vars) { Utils::check_request_method(array('POST')); $this->create_theme(); $this->get_additem_form(); $available_modules = Plugins::filter('dashboard_block_list', array()); $user_id = User::identify()->id; $dashboard_area = 'dashboard_' . $user_id; switch ($handler_vars['action']) { case 'updateModules': $modules = $_POST['moduleOrder']; $order = 0; foreach ($modules as $module) { $order++; DB::query('UPDATE {blocks_areas} SET display_order = :display_order WHERE block_id = :id AND area = :dashboardarea', array('display_order' => $order, 'id' => $module, 'dashboardarea' => $dashboard_area)); } $ar = new AjaxResponse(200, _t('Modules updated.')); break; case 'addModule': $type = $handler_vars['module_name']; $title = $available_modules[$type]; $block = new Block(array('title' => $title, 'type' => $type)); $block->insert(); $max_display_order = DB::get_value('SELECT max(display_order) FROM {blocks_areas} WHERE area = :dashboardarea and scope_id = 0;', array('dashboardarea' => $dashboard_area)); $max_display_order++; DB::query('INSERT INTO {blocks_areas} (block_id, area, scope_id, display_order) VALUES (:block_id, :dashboardarea, 0, :display_order)', array('block_id' => $block->id, 'display_order' => $max_display_order, 'dashboardarea' => $dashboard_area)); $ar = new AjaxResponse(200, _t('Added module %s.', array($title))); $ar->html('modules', $this->theme->fetch('dashboard_modules')); break; case 'removeModule': $block_id = $handler_vars['moduleid']; DB::delete('{blocks}', array('id' => $block_id)); DB::delete('{blocks_areas}', array('block_id' => $block_id)); $ar = new AjaxResponse(200, _t('Removed module.')); $ar->html('modules', $this->theme->fetch('dashboard_modules')); break; case 'configModule': $block_id = $handler_vars['moduleid']; $block = DB::get_row('SELECT * FROM {blocks} b WHERE b.id = :id', array('id' => $block_id), 'Block'); /** Block $block */ $form = $block->get_form(); $form->_ajax = true; $form->set_option('success_message', _t('Module Configuration Saved.') . '<script type="text/javascript">window.setTimeout(function(){$(".form_message").fadeOut();}, 2000);</script>'); $control_id = new FormControlHidden('moduleid', 'null:null'); $control_id->value = $block->id; $control_id->id = 'moduleid'; $form->append($control_id); $control_action = new FormControlHidden('action', 'null:null'); $control_action->value = 'configModule'; $control_action->id = 'action'; $form->append($control_action); $form->out(); $form_id = $form->name; exit; break; } $ar->out(); }
/** * Return the log entry's event module. * * <code>$log->module</code> * * @return string Human-readable event module */ public function get_event_module() { $module = DB::get_value( 'SELECT module FROM {log_types} WHERE id=' . $this->type_id ); return $module ? $module : _t( 'Unknown' ); }
/** * Get posts by info * - has:info => a post info key or array of post info keys, which should be present * - all:info => a post info key and value pair or array of post info key and value pairs, which should all be present and match * - not:all:info => a post info key and value pair or array of post info key and value pairs, to exclude if all are present and match * - any:info => a post info key and value pair or array of post info key and value pairs, any of which can match * - not:any:info => a post info key and value pair or array of post info key and value pairs, to exclude if any are present and match */ public function test_get_posts_by_info() { // setup $informationless_post = Post::create(array('title' => 'This is a Post without information', 'content' => 'The real point of this post is to make sure that there is at least one countable post without info for the sake of testing.', 'user_id' => $this->user->id, 'status' => Post::status('published'), 'content_type' => Post::type('entry'), 'pubdate' => DateTime::date_create(time()))); $seven_things = array("one", "two", "red", "blue", "black", "old", "new"); // create some posts with info for ($i = 1; $i < 42; $i++) { $post = Post::create(array('title' => 'This Post has Info', 'content' => 'If this were really a post, would it have such useless information?', 'user_id' => $this->user->id, 'status' => Post::status('published'), 'content_type' => Post::type('entry'), 'pubdate' => DateTime::date_create(time()))); $post->info->testing_info = 1; $post->info->{$seven_things}[$i % 7] = 1; $post->info->i = $i; $post->info->commit(); } // has:info $count = DB::get_value("SELECT COUNT(*) FROM {posts}\n\t\t\t\tLEFT JOIN {postinfo} pi1 ON\n\t\t\t\t\t{posts}.id = pi1.post_id AND\n\t\t\t\t\tpi1.name = 'red'\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tpi1.name <> ''\n\t\t"); $count_info_posts = Posts::get(array('ignore_permissions' => true, 'has:info' => 'testing_info', 'count' => 'DISTINCT {posts}.id', 'nolimit' => 1)); $this->assert_not_equal(Posts::count_total(), $count_info_posts); $count_posts = Posts::get(array('ignore_permissions' => true, 'has:info' => array('red'), 'count' => 'DISTINCT {posts}.id', 'nolimit' => 1)); $this->assert_equal($count_posts, $count); $count = DB::get_value("SELECT COUNT(*) FROM {posts}\n\t\t\t\tLEFT JOIN {postinfo} pi1 ON\n\t\t\t\t\t{posts}.id = pi1.post_id AND\n\t\t\t\t\tpi1.name = 'testing_info'\n\t\t\t\tLEFT JOIN {postinfo} pi2 ON\n\t\t\t\t\t{posts}.id = pi2.post_id AND\n\t\t\t\t\tpi2.name = 'red'\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tpi1.name <> '' OR\n\t\t\t\t\t\tpi2.name <> ''\n\t\t"); $count_posts = Posts::get(array('ignore_permissions' => true, 'has:info' => array('testing_info', 'red'), 'count' => 1, 'nolimit' => 1)); $this->assert_equal($count_posts, $count); // $query = Posts::get( array( 'has:info' => array( 'testing_info', 'red' ), 'nolimit' => 1, 'fetch_fn' => 'get_query' ) ); // Utils::debug( $query );die(); // all:info $count = DB::get_value("SELECT COUNT(*) FROM {posts}\n\t\t\t\tLEFT JOIN {postinfo} pi1 ON\n\t\t\t\t\t{posts}.id = pi1.post_id AND\n\t\t\t\t\tpi1.name = 'blue' AND pi1.value = 1\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tpi1.name <> ''\n\t\t"); $count_posts = Posts::get(array('ignore_permissions' => true, 'all:info' => array('blue' => 1), 'count' => 1, 'nolimit' => 1)); $this->assert_equal($count_posts, $count); $count = DB::get_value("SELECT COUNT(*) FROM {posts}\n\t\t\t\tLEFT JOIN {postinfo} pi1 ON\n\t\t\t\t\t{posts}.id = pi1.post_id AND\n\t\t\t\t\tpi1.name = 'blue' AND pi1.value = 1\n\t\t\t\tLEFT JOIN {postinfo} pi2 ON\n\t\t\t\t\t{posts}.id = pi2.post_id AND\n\t\t\t\t\tpi2.name = 'two' AND pi2.value = 1\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tpi1.name <> '' AND\n\t\t\t\t\t\tpi2.name <> ''\n\t\t"); $count_posts = Posts::get(array('ignore_permissions' => true, 'all:info' => array('blue' => true, 'two' => true), 'count' => 1, 'nolimit' => 1)); $this->assert_equal($count_posts, $count); // any:info $count = DB::get_value("SELECT COUNT(*) FROM {posts}\n\t\t\t\tLEFT JOIN {postinfo} pi1 ON\n\t\t\t\t\t{posts}.id = pi1.post_id AND\n\t\t\t\t\tpi1.name = 'black' AND\n\t\t\t\t\tpi1.value = 1\n\t\t\t\tLEFT JOIN {postinfo} pi2 ON\n\t\t\t\t\t{posts}.id = pi2.post_id AND\n\t\t\t\t\tpi2.name = 'blue' AND\n\t\t\t\t\tpi2.value = 1\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tpi1.name <> '' OR\n\t\t\t\t\t\tpi2.name <> ''\n\t\t"); $count_posts = Posts::get(array('ignore_permissions' => true, 'any:info' => array('black' => 1, 'blue' => 1), 'count' => 1, 'nolimit' => 1)); $this->assert_equal($count_posts, $count); $count = Posts::get(array('ignore_permissions' => true, 'all:info' => array('black' => 1), 'count' => 1, 'nolimit' => 1)) + Posts::get(array('all:info' => array('blue' => true), 'count' => 1, 'nolimit' => 1)); $this->assert_equal($count_posts, $count); $count = DB::get_value("SELECT COUNT(*) FROM {posts}\n\t\t\t\tLEFT JOIN {postinfo} pi1 ON\n\t\t\t\t\t{posts}.id = pi1.post_id AND\n\t\t\t\t\tpi1.name = 'i' AND\n\t\t\t\t\tpi1.value IN ( 0,1,2,3,4,5 )\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tpi1.name <> ''\n\t\t"); $params = array('ignore_permissions' => true, 'any:info' => array('i' => array(1, 2, 3, 4, 5)), 'count' => 1, 'nolimit' => 1); //$this->output(Posts::get(array_merge($params, array('fetch_fn' => 'get_query')))); $count_posts = Posts::get($params); $this->assert_equal($count_posts, $count); // not:all:info $count = DB::get_value("SELECT COUNT(*) FROM {posts} WHERE\n\t\t\t\t{posts}.id NOT IN (\n\t\t\t\t\tSELECT post_id FROM {postinfo}\n\t\t\t\t\t\tWHERE ( name = 'testing_info' AND value = 1 )\n\t\t\t\t\t\tGROUP BY post_id\n\t\t\t\t\t\tHAVING COUNT(*) = 1\n\t\t\t\t)\n\t\t"); $count_posts = Posts::get(array('ignore_permissions' => true, 'not:all:info' => array('testing_info' => 1), 'nolimit' => 1, 'count' => 1)); $this->assert_equal($count_posts, $count, _t('not:all:info expected %d, got %d', array($count, $count_posts))); $count = DB::get_value("SELECT COUNT(*) FROM {posts} WHERE\n\t\t\t\t{posts}.id NOT IN (\n\t\t\t\t\tSELECT post_id FROM {postinfo}\n\t\t\t\t\t\tWHERE ( name = 'one' AND value = 1 )\n\t\t\t\t\t\tGROUP BY post_id\n\t\t\t\t\t\tHAVING COUNT(*) = 1\n\t\t\t\t)\n\t\t"); $count_posts = Posts::get(array('ignore_permissions' => true, 'not:all:info' => array('one' => 1), 'count' => 1, 'nolimit' => 1)); $this->assert_equal($count_posts, $count); $count = DB::get_value("SELECT COUNT(*) FROM {posts} WHERE\n\t\t\t\t{posts}.id NOT IN (\n\t\t\t\t\tSELECT post_id FROM {postinfo}\n\t\t\t\t\t\tWHERE ( name = 'old' AND value = 1 OR\n\t\t\t\t\t\t name = 'new' AND value = 1 )\n\t\t\t\t\t\tGROUP BY post_id\n\t\t\t\t\t\tHAVING COUNT(*) = 2\n\t\t\t\t)\n\t\t"); $count_posts = Posts::get(array('ignore_permissions' => true, 'not:all:info' => array('old' => 1, 'new' => 1), 'count' => 1, 'nolimit' => 1)); $this->assert_equal($count_posts, $count); // not:any:info $count = DB::get_value("SELECT COUNT(*) FROM {posts} WHERE\n\t\t\t\t{posts}.id NOT IN (\n\t\t\t\t\tSELECT post_id FROM {postinfo}\n\t\t\t\t\t\tWHERE ( {postinfo}.name = 'two' AND {postinfo}.value = 1 )\n\t\t\t\t)\n\t\t"); $count_posts = Posts::get(array('ignore_permissions' => true, 'not:any:info' => array('two' => 1), 'count' => 1, 'nolimit' => 1)); $this->assert_equal($count_posts, $count); $count = DB::get_value("SELECT COUNT(*) FROM {posts} WHERE\n\t\t\t\t{posts}.id NOT IN (\n\t\t\t\t\tSELECT post_id FROM {postinfo}\n\t\t\t\t\t\tWHERE ( {postinfo}.name = 'black' AND {postinfo}.value = 1 OR\n\t\t\t\t\t\t {postinfo}.name = 'blue' AND {postinfo}.value = 1 )\n\t\t\t\t)\n\t\t"); $count_posts = Posts::get(array('ignore_permissions' => true, 'not:any:info' => array('black' => 1, 'blue' => 1), 'count' => 1, 'nolimit' => 1)); $this->assert_equal($count_posts, $count); // $query = Posts::get( array( 'not:any:info' => array( 'comments_disabled' => 1, 'html_title' => 'Chili, The Breakfast of Champions' ), 'nolimit' => 1, 'fetch_fn' => 'get_query' ) ); // Utils::debug( $query );die(); // teardown Posts::get(array('ignore_permissions' => true, 'has:info' => 'testing_info', 'nolimit' => 1))->delete(); $informationless_post->delete(); }
/** * Find the count of objects of a given type associated with this Term. * * @param $type string. The name of the object type for which the associations are wanted. * @return Array of object ids associated with this term for the given type. */ public function object_count( $type ) { $type_id = Vocabulary::object_type_id( $type ); $result = DB::get_value( "SELECT count(object_id) FROM {object_terms} WHERE term_id = ? AND object_type_id = ?", array( $this->id, $type_id ) ); return $result; }
/** * Grant a permission to a user * @param integer $user_id The user ID * @param integer $token_id The name or ID of the permission token to grant * @param string $access The kind of access to assign the group * @return Result of the DB query */ public static function grant_user($user_id, $token_id, $access = 'full') { $token_id = self::token_id($token_id); $access_mask = DB::get_value('SELECT access_mask FROM {user_token_permissions} WHERE user_id=? AND token_id=?', array($user_id, $token_id)); if ($access_mask === false) { $permission_bit = 0; // default is 'deny' (bitmask 0) } $bitmask = self::get_bitmask($access_mask); if ($access == 'full') { $bitmask->value = $bitmask->full; } elseif ($access == 'deny') { $bitmask->value = 0; } else { $bitmask->{$access} = true; } $result = DB::update('{user_token_permissions}', array('access_mask' => $bitmask->value), array('user_id' => $user_id, 'token_id' => $token_id)); ACL::clear_caches(); return $result; }
/** * Returns the number of times the least used tag is used. * * @return int The number of times the least used tag is used. **/ public function min_count() { return DB::get_value('SELECT count( t2.object_id ) AS min FROM {terms} t, {object_terms} t2 WHERE t2.term_id = t.id AND t.vocabulary_id = ? GROUP BY t.id ORDER BY min ASC LIMIT 1', array($this->id)); }
/** * 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. This only works on host with safe mode DISABLED if (!ini_get('safe_mode')) { set_time_limit(600); } $time = DateTime::create(); $crons = DB::get_results('SELECT * FROM {crontab} WHERE start_time <= ? AND next_run <= ? AND active != ?', array($time->sql, $time->sql, 0), '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} WHERE active != ? ORDER BY next_run ASC LIMIT 1', array(0)); Options::set('next_cron', min(intval($next_cron), $time->modify('+1 day')->int)); Options::set('cron_running', false); }
/** * Execute and return the first row of this query * @return object The result object, a QueryRecord or the specified class */ public function value() { return DB::get_value($this->get(), $this->params()); }
/** * 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')); } }
/** * Creates a basic RSS-format XML structure with channel and items elements * @return SimpleXMLElement The requested RSS document */ public function create_rss_wrapper($feed_name) { $itunes = Options::get(Podcast::OPTIONS_PREFIX . "{$feed_name}_itunes"); $xml = new SimpleXMLElement('<' . '?xml version="1.0" encoding="UTF-8" ?' . '><rss></rss>'); $xml->addAttribute('xmlns:xmlns:atom', 'http://www.w3.org/2005/Atom'); $xml->addAttribute('xmlns:xmlns:itunes', 'http://www.itunes.com/dtds/podcast-1.0.dtd'); $xml->addAttribute('version', '2.0'); $channel = $xml->addChild('channel'); $title = $channel->addChild('title', isset($itunes['title']) ? $itunes['title'] : Options::get('title')); $link = $channel->addChild('link', Site::get_url('habari')); $atom_link = $channel->addChild('xmlns:atom:link'); $atom_link->addAttribute('href', $this->current_url); $atom_link->addAttribute('rel', 'self'); $atom_link->addAttribute('type', 'application/rss+xml'); $lang = $channel->addChild('language', strlen(Options::get('locale')) ? Options::get('locale') : 'en-us'); if ($tagline = Options::get('tagline')) { $description = $channel->addChild('description', $tagline); } $max_time = DB::get_value("SELECT MAX(pubdate) FROM {posts} WHERE status = ? AND content_type = ? AND id in ( SELECT post_id from {postinfo} where name = ? )", array(Post::status('published'), Post::type('podcast'), $feed_name)); $pubDate = $channel->addChild('lastBuildDate', HabariDateTime::date_create($max_time)->get('r')); $generator = $channel->addChild('generator', 'Habari ' . Version::get_habariversion() . ' http://habariproject.org/'); $itunes_author = $channel->addChild('xmlns:itunes:author', $itunes['author']); $itunes_subtitle = $channel->addChild('xmlns:itunes:subtitle', $itunes['subtitle']); $itunes_summary = $channel->addChild('xmlns:itunes:summary', $itunes['summary']); $itunes_summary = $channel->addChild('description', $itunes['summary']); $itunes_owner = $channel->addChild('xmlns:itunes:owner'); $itunes_owner_name = $itunes_owner->addChild('xmlns:itunes:name', $itunes['owner_name']); $itunes_owner_email = $itunes_owner->addChild('xmlns:itunes:email', $itunes['owner_email']); $itunes_explicit = $channel->addChild('xmlns:itunes:explicit', $itunes['explicit']); $itunes_image = $channel->addChild('xmlns:itunes:image'); $itunes_image->addAttribute('href', $itunes['image']); if (trim($itunes['main_category']) != '') { $itunes_category = $channel->addChild('xmlns:itunes:category'); $categories = explode(':', $itunes['main_category']); $itunes_category->addAttribute('text', $categories[0]); if (isset($categories[1])) { $child = $itunes_category->addChild('xmlns:itunes:category'); $child->addAttribute('text', $categories[1]); } } if (trim($itunes['category_2']) != '') { $itunes_category = $channel->addChild('xmlns:itunes:category'); $categories = explode(':', $itunes['category_2']); $itunes_category->addAttribute('text', $categories[0]); if (isset($categories[1])) { $child = $itunes_category->addChild('xmlns:itunes:category'); $child->addAttribute('text', $categories[1]); } } if (trim($itunes['category_3']) != '') { $itunes_category = $channel->addChild('xmlns:itunes:category'); $categories = explode(':', $itunes['category_3']); $itunes_category->addAttribute('text', $categories[0]); if (isset($categories[1])) { $child = $itunes_category->addChild('xmlns:itunes:category'); $child->addAttribute('text', $categories[1]); } } $itunes_block = $channel->addChild('xmlns:itunes:block', $itunes['block'] ? 'Yes' : 'No'); if (strlen($itunes['redirect'])) { $itunes_redirect = $channel->addChild('xmlns:itunes:new-feed-url', $itunes['redirect']); } Plugins::act('podcast_create_wrapper', $xml); return $xml; }
public static function detatch_from_post($tag_id, $post_id) { Plugins::act('tag_detatch_from_post_before', $tag_id, $post_id); $result = DB::query('DELETE FROM {tag2post} WHERE tag_id = ? AND post_id = ?', array($tag_id, $post_id)); // should we delete the tag if it's the only one left? $count = DB::get_value('SELECT COUNT(tag_id) FROM {tag2post} WHERE tag_id = ?', array($tag_id)); if ($count == 0) { $delete = true; $delete = Plugins::filter('tag_detach_from_post_delete_empty_tag', $delete, $tag_id); if ($delete) { DB::query('DELETE FROM {tags} WHERE id = ?', array($tag_id)); } } Plugins::act('tag_detatch_from_post_after', $tag_id, $post_id, $result); return $result; }
/** * Given a group's name, return its ID * @param string a group's name * @return int the group's ID, or false if the group doesn't exist */ public static function id( $name ) { $check_field = is_numeric( $name ) ? 'id' : 'name'; $id = DB::get_value( "SELECT id FROM {groups} WHERE {$check_field}=?", array( $name ) ); return $id; // get_value returns false if no record is returned }
/** * Returns the number of times the most used tag is used. * * @return int The number of times the most used tag is used. **/ public static function max_count() { return DB::get_value('SELECT count( t2.post_id ) AS max FROM {tags} t, {tag2post} t2 WHERE t2.tag_id = t.id GROUP BY t.id ORDER BY max DESC LIMIT 1'); }
public function add_template_vars() { $this->add_template('formcontrol_text', dirname(__FILE__) . '/forms/formcontrol_text.php', true); $this->add_template('formcontrol_textarea', dirname(__FILE__) . '/forms/formcontrol_textarea.php', true); $this->assign('recent_comments', Comments::get(array('limit' => 5, 'status' => Comment::STATUS_APPROVED, 'orderby' => 'date DESC'))); $this->assign('recent_posts', Posts::get(array('limit' => 5, 'orderby' => 'pubdate DESC', 'content_type' => 1, 'status' => 2))); if ('' != Controller::get_var('tag')) { $tag_text = DB::get_value('SELECT tag_text FROM {tags} WHERE tag_slug=?', array(Controller::get_var('tag'))); $this->assign('tag_text', $tag_text); } if (!$this->template_engine->assigned('pages')) { $this->assign('pages', Posts::get(array('content_type' => 'page', 'status' => Post::status('published'), 'nolimit' => 1))); } if (!$this->template_engine->assigned('user')) { $this->assign('user', User::identify()); } if (!$this->template_engine->assigned('page')) { $page = Controller::get_var('page'); $this->assign('page', isset($page) ? $page : 1); } parent::add_template_vars(); }