/** * insertPostTags * * @param $post_id * @param $post_data */ public function insertPostTags($post_id, $tags) { $post_id = (int) $post_id; $tag_map = array(); $tags_table = Solar::factory('Foresmo_Model_Tags'); $existing_tags = $tags_table->fetchAllAsArray(); foreach ($existing_tags as $existing_tag) { foreach ($tags as $tag) { if (strtolower($tag) == strtolower($existing_tag['tag'])) { $tag_map[$tag] = $existing_tag['id']; } } } foreach ($tags as $tag) { if (array_key_exists($tag, $tag_map)) { $data = array('post_id' => $post_id, 'tag_id' => $tag_map[$tag]); $this->insert($data); } else { $data = array('tag' => $tag, 'tag_slug' => Foresmo::makeSlug($tag)); $last_insert_id = $tags_table->insert($data); $data = array('post_id' => $post_id, 'tag_id' => $last_insert_id); $this->insert($data); } } }
/** * processAdminRequest * * handle module admin request, and return output * * @param string $name module name * @param array $data request data: POST, GET, PARAMS(from url) * * @return mixed; */ public function processAdminRequest($name, $data) { $module = $this->loadModule($name); if (method_exists($module, 'admin_request')) { try { $module->admin_request($data); if (isset($module->output)) { Foresmo::escape($module->output); return $module->output; } else { return null; } } catch (Exception $e) { return null; } } return null; }
/** * updateContentTags * Update Tags for a post/page * @param $id * @param $tags */ public function updateContentTags($id, $tags) { if ($id != (int) $id) { return false; } $id = (int) $id; $tag_map = array(); $tags_table = Solar::factory('Foresmo_Model_Tags'); $existing_tags = $tags_table->fetchAllAsArray(); $content_tags = $tags_table->fetchTagsByID($id); foreach ($existing_tags as $existing_tag) { foreach ($tags as $tag) { if (Foresmo::makeSlug($tag) == $existing_tag['tag_slug']) { $t = $existing_tag['tag_slug']; $tag_map[$t] = $existing_tag['id']; } } } // find diff in tags to update and existing tags for content $ct_slugs = array(); $tag_slugs = array(); foreach ($content_tags as $t) { $ct_slugs[] = $t['tag_slug']; } foreach ($tags as $tag) { $tag_slugs[] = Foresmo::makeSlug($tag); } // delete tags if necessary $to_delete = array_diff($ct_slugs, $tag_slugs); $delete_tag_ids = array(); foreach ($to_delete as $del_tag) { $delete_tag_ids[] = $tags_table->fetchTagIdBySlug($del_tag); } if (!empty($to_delete)) { $this->deleteContentTagsById($id, $delete_tag_ids); } // add tags foreach ($tag_slugs as $tag) { if (array_key_exists($tag, $tag_map) && !$this->hasTag($id, $tag_map[$tag])) { $data = array('post_id' => $id, 'tag_id' => $tag_map[$tag]); $this->insert($data); } elseif (!array_key_exists($tag, $tag_map)) { $data = array('tag' => $tag, 'tag_slug' => Foresmo::makeSlug($tag)); $last_insert_id = $tags_table->insert($data); $data = array('post_id' => $id, 'tag_id' => $last_insert_id); $this->insert($data); } } }
/** * addContent * New blog post/page * * @return void */ public function addContent() { $errors = array(); if (!isset($this->_post['post_title']) || $this->validate('validateBlank', $this->_post['post_title'])) { $errors[] = 'Title cannot be blank.'; } if (!isset($this->_post['post_content']) || $this->validate('validateBlank', $this->_post['post_title'])) { $errors[] = 'Content cannot be blank.'; } $this->_post['post_slug'] = Foresmo::makeSlug($this->_post['post_title']); if (in_array(strtolower($this->_post['post_slug']), $this->_restricted_names)) { $errors[] = 'The slug for this post/page "' . $this->_post['post_slug'] . '" is restricted. Please choose a different slug/title'; } if (count($errors) > 0) { $message = implode('<br/>', $errors); $this->success = false; $this->message = $message; return; } if (!isset($this->_post['post_excerpt']) || $this->validate('validateBlank', $this->_post['post_excerpt'])) { $this->_post['post_excerpt'] = Foresmo::makeExcerpt($this->_post['post_content'], 60, '...'); } $last_insert_id = $this->_model->posts->insertContent($this->_post); if (!$this->validate('validateBlank', $this->_post['post_tags'])) { $tags = explode(',', rtrim(trim($this->_post['post_tags']), ',')); foreach ($tags as $key => $tag) { $tags[$key] = trim($tag); } $this->_model->posts_tags->insertContentTags($last_insert_id, $tags); } if (isset($this->_post['post_comments_disabled']) && $this->_post['post_comments_disabled'] == 'true') { $this->_model->post_info->insertCommentsDisabled($last_insert_id, true); } else { $this->_model->post_info->insertCommentsDisabled($last_insert_id, false); } if ((int) $this->_post['post_type'] == 1) { $message = "Successly created new post! <a href=\"/{$this->_post['post_slug']}\">View post</a>."; } elseif ((int) $this->_post['post_type'] == 2) { $message = "Successly created new page! <a href=\"/{$this->_post['post_slug']}\">View page</a>."; } $this->success = true; $this->data = array('id' => $last_insert_id); $this->message = $message; }
/** * _getClientFloodCount * Check cache to see if IP requires rate limiting * If exceeds max attempts, inject delay * */ protected function _getClientFloodCount() { $ip = Foresmo::getIP(); $count = 0; if ($ip != '0.0.0.0') { $key = 'foresmo_flood_control_' . $ip; $life = $this->_cache->_config['life']; $this->_cache->_config['life'] = self::FLOODCONTROL_CACHE_CLIENT; $count = $this->_cache->fetch($key); $this->_cache->_config['life'] = $life; if ($count !== false && is_numeric($count)) { $count = (int) $count; } else { $count = 0; } } return $count; }
/** * fetchContentValue * Fetch Column for post/page * * @param int $content_id * @param string $col * @return mixed */ public function fetchContentValue($content_id, $col) { $result = $this->fetchValue(array('cols' => array($col), 'where' => array('id = ?' => $content_id))); Foresmo::escape($result); return $result; }
/** * fetchRecentComments * Fetch recent comments * * @param int $limit limit (default 10) * @return array result set */ public function fetchRecentComments($limit = null) { if (is_null($limit)) { $limit = 10; } $limit = (int) $limit; $results = $this->fetchAllAsArray(array('where' => array('type = ?' => array(0)), 'eager' => array('commentinfo', 'posts'), 'order' => array('id DESC'), 'limit' => array($limit))); Foresmo::dateFilter($results); Foresmo::sanitize($results); return $results; }
/** * ajax_blog_install * This ajax action handles blog installation * * @param $post_data * @return string */ public function ajax_blog_install($post_data) { if ($this->installed) { return 'Blog is already installed!'; } if (!empty($post_data['db_type'])) { $db_type = ucfirst($post_data['db_type']); $adapter = 'Solar_Sql_Adapter_' . $db_type; } else { return 'DB Type cannot be blank!'; } Solar_Config::set('Solar_Sql', 'adapter', $adapter); Solar_Config::set($adapter, 'host', $post_data['db_host']); Solar_Config::set($adapter, 'user', $post_data['db_username']); Solar_Config::set($adapter, 'pass', $post_data['db_password']); Solar_Config::set($adapter, 'name', $post_data['db_name']); Solar_Config::set($adapter, 'prefix', $post_data['db_prefix']); $adapter = Solar::factory($adapter); try { $adapter->connect(); } catch (Exception $e) { return 'Cannot connect to database! Please ensure valid DB info.'; } $this->random_str = Foresmo::randomString(18); $config_file = Solar::$system . '/config/Solar.config.php'; $config_content = $this->_getConfigContent($post_data); if (($handle = @fopen($config_file, 'w')) !== false) { if (@fwrite($handle, $config_content) === false) { fclose($handle); return "Cannot write to: {$config_file}. Please set the permissions to 777 for this file."; } else { fclose($handle); } } else { return "Could not open {$config_file}, please ensure that this file exists and is writable."; } $schema = Solar::$system . '/source/foresmo/Foresmo/Schemas/' . $db_type . '.php'; $schema_sql = Solar_File::load($schema); $schema_sql = str_replace('[prefix]', $post_data['db_prefix'], $schema_sql); try { $adapter->query($schema_sql); } catch (Exception $e) { // tables already exist? } $errors = array(); $matches = array(); $ret_str = ''; $post_data['blog_user'] = trim($post_data['blog_user']); if (empty($post_data['blog_password']) == true || empty($post_data['blog_password2']) == true || empty($post_data['blog_user']) == true || empty($post_data['blog_title']) == true || empty($post_data['blog_email']) == true) { $errors[] = 'No fields should be left blank!'; } preg_match('/^([.0-9a-z_-]+)@(([0-9a-z-]+\\.)+[0-9a-z]{2,4})$/i', $post_data['blog_email'], $matches); if (count($matches) == 0) { $errors[] = 'Not a valid email address.'; } if (strlen($post_data['blog_password']) < 7) { $errors[] = 'The user password must be seven characters or more'; } if ($post_data['blog_password'] !== $post_data['blog_password2']) { $errors[] = 'The user password fields did not match!'; } if (count($errors) > 0) { $ret_str .= '<p class="error"><b>Validation Errors:</b></p>'; foreach ($errors as $error) { $ret_str .= '<span class="error">' . $error . '</span><br />'; } return $ret_str; } $username = $post_data['blog_user']; $password = $post_data['blog_password']; $password = md5($this->random_str . $password); $email = trim($post_data['blog_email']); $table = $post_data['db_prefix'] . 'groups'; $data = array('name' => 'Admin'); $adapter->insert($table, $data); $last_insert_id = $adapter->lastInsertId($table, 'id'); $permissions = array(); $table = $post_data['db_prefix'] . 'permissions'; $data = array('name' => 'create_post'); $adapter->insert($table, $data); $permissions[] = $adapter->lastInsertId($table, 'id'); $data = array('name' => 'edit_post'); $adapter->insert($table, $data); $permissions[] = $adapter->lastInsertId($table, 'id'); $data = array('name' => 'delete_post'); $adapter->insert($table, $data); $permissions[] = $adapter->lastInsertId($table, 'id'); $data = array('name' => 'create_page'); $adapter->insert($table, $data); $permissions[] = $adapter->lastInsertId($table, 'id'); $data = array('name' => 'edit_page'); $adapter->insert($table, $data); $permissions[] = $adapter->lastInsertId($table, 'id'); $data = array('name' => 'delete_page'); $adapter->insert($table, $data); $permissions[] = $adapter->lastInsertId($table, 'id'); $table = $post_data['db_prefix'] . 'groups_permissions'; foreach ($permissions as $permission) { $data = array('group_id' => $last_insert_id, 'permission_id' => (int) $permission); $adapter->insert($table, $data); } $table = $post_data['db_prefix'] . 'users'; $data = array('group_id' => $last_insert_id, 'username' => $username, 'password' => $password, 'email' => strtolower($email)); $adapter->insert($table, $data); $table = $post_data['db_prefix'] . 'options'; $data = array('name' => 'blog_installed', 'type' => 1, 'value' => time()); $adapter->insert($table, $data); $data = array('name' => 'blog_theme', 'type' => 0, 'value' => 'default'); $adapter->insert($table, $data); $data = array('name' => 'blog_title', 'type' => 0, 'value' => $post_data['blog_title']); $adapter->insert($table, $data); $data = array('name' => 'blog_date_format', 'type' => 0, 'value' => 'F j, Y, g:ia'); $adapter->insert($table, $data); $data = array('name' => 'blog_timezone', 'type' => 0, 'value' => '-4:00'); $adapter->insert($table, $data); $data = array('name' => 'blog_posts_per_page', 'type' => 0, 'value' => 10); $adapter->insert($table, $data); $data = array('name' => 'blog_comment_link_limit', 'type' => 0, 'value' => 3); $adapter->insert($table, $data); $table = $post_data['db_prefix'] . 'posts'; $data = array('slug' => 'my-first-post', 'content_type' => 1, 'title' => 'My first post!', 'content' => "Welcome to {$post_data['blog_title']}. Look forward to new blog posts soon!", 'user_id' => 1, 'status' => 1, 'pubdate' => time(), 'modified' => time()); $adapter->insert($table, $data); $table = $post_data['db_prefix'] . 'comments'; $data = array('post_id' => 1, 'name' => 'Foresmo', 'email' => '*****@*****.**', 'url' => 'http://foresmo.com', 'ip' => sprintf("%u", ip2long('192.168.0.1')), 'content' => 'Congratulations!', 'status' => 1, 'date' => time(), 'type' => 0); $adapter->insert($table, $data); $table = $post_data['db_prefix'] . 'tags'; $data = array('tag' => 'Foresmo', 'tag_slug' => 'foresmo'); $adapter->insert($table, $data); $table = $post_data['db_prefix'] . 'posts_tags'; $data = array('post_id' => 1, 'tag_id' => 1); $adapter->insert($table, $data); $table = $post_data['db_prefix'] . 'modules'; $data = array('name' => 'Pages', 'enabled' => 1); $adapter->insert($table, $data); $data = array('name' => 'Search', 'enabled' => 1); $adapter->insert($table, $data); $data = array('name' => 'Calendar', 'enabled' => 1); $adapter->insert($table, $data); $data = array('name' => 'Tags', 'enabled' => 1); $adapter->insert($table, $data); $data = array('name' => 'Links', 'enabled' => 1); $adapter->insert($table, $data); $data = array('name' => 'Archives', 'enabled' => 1); $adapter->insert($table, $data); $data = array('name' => 'Flickr', 'enabled' => 0); $adapter->insert($table, $data); $data = array('name' => 'Twitter', 'enabled' => 0); $adapter->insert($table, $data); $data = array('name' => 'Sections', 'enabled' => 0); $adapter->insert($table, $data); $table = $post_data['db_prefix'] . 'module_info'; $data = array('module_id' => 3, 'name' => 'start_of_week', 'type' => 0, 'value' => 0); $adapter->insert($table, $data); if ($db_type == 'Mysql') { $data = array('module_id' => 2, 'name' => 'search_adapter', 'type' => 0, 'value' => 'mysql'); } else { $data = array('module_id' => 2, 'name' => 'search_adapter', 'type' => 0, 'value' => 'default'); } $adapter->insert($table, $data); $data = array('module_id' => 2, 'name' => 'search_adapter_settings', 'type' => 0, 'value' => 'a:5:{s:7:"Default";a:0:{}s:6:"Google";a:0:{}s:5:"Mysql";a:0:{}s:6:"Lucene";a:0:{}s:5:"Sphinx";a:0:{}}'); $adapter->insert($table, $data); return 'Foresmo installed! Click <a href="/">here</a> to check it out! Also, don\'t forget to change the permissions of the config back to read only.'; }
/** * fetchSpam * Fetch all spam comments * * @return array result set */ public function fetchSpam() { $results = $this->fetchAllAsArray(array('where' => array('comments.status = ?' => array(2)), 'eager' => array('commentinfo', 'post'), 'order' => array('id DESC'))); Foresmo::dateFilter($results); Foresmo::escape($results); return $results; }
/** * actionSettings * Admin/settings action/page * * @return void * * @access public * @since .09 */ public function actionSettings() { $post_data = $this->_request->post(); if (isset($post_data['submit'])) { foreach ($post_data as $key => $value) { switch ($key) { case 'blog_title': if (trim($value) != '') { $this->_model->options->updateOption('blog_title', $value); } break; case 'blog_date_format': if (!isset($post_data['blog_date_format_preset']) && trim($value) != '') { $this->_model->options->updateOption('blog_date_format', $value); } break; case 'blog_date_format_preset': if (trim($value) != '') { $this->_model->options->updateOption('blog_date_format', $value); } break; case 'blog_timezone': if (trim($value) != '') { $this->_model->options->updateOption('blog_timezone', $value); ini_set('date.timezone', $value); } break; } } } $this->data = $this->_model->options->fetchAllOptions(false); $this->timezones = Foresmo::fetchTimeZones(); $this->timezone_current = date_default_timezone_get(); }
/** * _setup * * Set variables used throughout the app here. */ protected function _setup() { if (Solar_Config::get('Foresmo', 'dev')) { xdebug_start_trace('/var/www/foresmo/tmp/trace'); } if (!isset($this->session)) { $this->session = Solar::factory('Solar_Session', array('class' => 'Foresmo_App')); } $adapter = Solar_Config::get('Solar_Sql', 'adapter'); $adapter = Solar::factory($adapter); try { $adapter->connect(); } catch (Exception $e) { $this->connect = false; // should display an error page and die. } if ($this->connect) { $this->_adapter = $adapter; $this->installed = (bool) Solar_Config::get('Foresmo', 'installed'); if (!$this->installed && $this->_controller != 'install') { $this->_redirect('/install'); } $this->web_root = Solar::$system . '/content/'; $this->_model = Solar_Registry::get('model_catalog'); $cache_settings = Solar_Config::get('Foresmo', 'cache'); if (isset($cache_settings['adapter'])) { $this->_model->_config['cache'] = $cache_settings; $this->_cache = Solar::factory('Solar_Cache', $cache_settings); } $results = $this->_model->options->fetchBlogOptions(); foreach ($results as $result) { switch ($result['name']) { case 'blog_theme': $this->blog_theme = $result['value']; break; case 'blog_admin_theme': $this->blog_admin_theme = $result['value']; break; case 'blog_theme_options': $this->blog_theme_options = unserialize($result['value']); break; case 'blog_admin_theme_options': $this->blog_admin_theme_options = unserialize($result['value']); break; case 'blog_title': $this->blog_title = $result['value']; break; case 'blog_posts_per_page': $this->_model->posts->posts_per_page = (int) $result['value']; break; case 'blog_comment_link_limit': $this->_model->comments->link_count_limit = (int) $result['value']; break; } } $this->page_title = $this->blog_title; $time_info = Foresmo::getTimeInfo(); Foresmo::$date_format = $time_info['blog_date_format']; Foresmo::$timezone = $time_info['blog_timezone']; $this->_model->posts->published_posts_count = $this->_model->posts->fetchPublishedPostsCount(); $this->_setPagesCount(); $this->_layout_default = $this->blog_theme; $this->_setToken(); $this->_modules = Solar::factory('Foresmo_Modules', array('model' => $this->_model)); $this->enabled_modules_data = $this->_modules->getEnabledModulesData(); $this->_registerModuleHooks(); } }
/** * fetchPostsByTag * Fetch all posts with status of 1 (published) with specific tag(s) * with all it's pertitent associated data (tags, comments, * postinfo) as an array * * @param array $tags list of tags * * @param string $oper AND / OR * * @return array */ public function fetchPostsByTag($tags, $oper = 'AND') { if (!$tags || empty($tags) || $oper != 'AND' && $oper != 'OR') { return array(); } $where_stmt = 'status = ? AND content_type = ?'; $where_values = array(1, 1); $join = array(); $count = count($tags); for ($i = 0; $i < $count; $i++) { $where_values[] = $tags[$i]; if ($oper == 'AND') { $tc = $i + 1; $where_stmt .= " AND tags{$tc}.tag_slug = ?"; if ($tc == 1) { $join[] = array('type' => "inner", 'name' => "{$this->_config['prefix']}posts_tags AS posts_tags{$tc}", 'cond' => "posts_tags{$tc}.post_id = {$this->_config['prefix']}posts.id"); } else { $join[] = array('type' => "inner", 'name' => "{$this->_config['prefix']}posts_tags AS posts_tags{$tc}", 'cond' => "posts_tags{$tc}.post_id = posts_tags{$i}.post_id"); } $join[] = array('type' => "inner", 'name' => "{$this->_config['prefix']}tags AS tags{$tc}", 'cond' => "posts_tags{$tc}.tag_id = tags{$tc}.id"); } } if ($oper == 'OR') { $join[] = array('type' => "inner", 'name' => "{$this->_config['prefix']}posts_tags AS posts_tags1", 'cond' => "posts_tags1.post_id = {$this->_config['prefix']}posts.id"); $join[] = array('type' => "inner", 'name' => "{$this->_config['prefix']}tags AS tags1", 'cond' => "posts_tags1.tag_id = tags1.id"); $where_stmt .= ' AND tags1.tag_slug IN (' . rtrim(str_repeat('?,', $count), ',') . ')'; } $where = array($where_stmt => $where_values); $results = $this->fetchAllAsArray(array('distinct' => true, 'where' => $where, 'order' => array('id DESC'), 'join' => $join, 'eager' => array('comments' => array('eager' => array('commentinfo')), 'tags', 'postinfo', 'users'))); Foresmo::dateFilter($results); Foresmo::sanitize($results); return $results; }