rendered paste body<?phpif(!defined('IN_VALKYRIE')) { exit;}class IndexController extends PageController { public $routes = array( // overloadable action to work with __call '*' => '/*/(str)a/(str)b/(str)c/(str)d', // deal with paginating the blog posts 'page' => '/page/(int)page_num', ); public $pre_filters = array('options', 'user'); // default action, show the first few posts public function index(Registry $registry, Request $request, Response $response) { $request['page_num'] = 1; $this->page($registry, $request, $response); } // looking on another page of posts public function page(Registry $registry, Request $request, Response $response) { // how many posts are there? No need for the burden of finders // or records here $num_posts = $registry->dba->getValue("SELECT COUNT(ID) FROM wp_posts WHERE post_type='post' AND post_status='publish'"); // calculate the number of pages $num_pages = @ceil($num_posts / $registry['posts_per_page']); // pagination using the Pager library $response['posts_pager'] = $registry->pager ->numPages($num_pages) ->currentPage($request['page_num']) ->showRightOfCurrent(3) ->showLeftOfCurrent(3) ->setPath('/page/%page%/'); // template stuff $response['posts'] = $this->getFinder('posts')->findAllVisible($request['page_num'], $registry['posts_per_page']); $response['main'] = 'posts.html'; } // allow for variable route names as per defined in getRoutes from // the database pages that were added, eg: wordpress' "about" page public function __call($fn, array $args = array()) { // need to bring them in this way when using __call list($registry, $request, $response) = $args; // finder for posts model definition $posts_finder = $this->getFinder('posts'); // permalink / archive / single post if(ctype_digit($request['a'])) { // single posts if($request['d'] !== NULL) { $post = $posts_finder->findAllVisible(1, 0, array( 'post_name' => $request['d'], )); if($post === NULL) { $this->setNotFound(); } $response['post'] = $post; $response['main'] = 'post.html'; // archives } else { $posts = $posts_finder->findAllVisible(1, 0, array( 'day' => $request['c'], 'month' => $request['b'], 'year' => $request['a'], )); $time = mktime(0, 0, 0, $request['b'], $request['c'], $request['a']); $date = ""; // make a nice looking date, eg: Jan 1, 2007 if($request['b']) { $date .= date("M ", $time); } if($request['c']) { $date .= date("j, ", $time); } if($request['a']) { $date .= date("Y", $time); } $response['archive_date'] = $date; $response['archive_posts'] = $posts; $response['main'] = 'archives.html'; } // page / category } else { // cached info, uses same query results as main menu $pages = $posts_finder->findVisiblePages(); // this page exists if(isset($pages[$request['a']])) { $response['post'] = $posts_finder->findPage($request['a']); $response['main'] = 'post.html'; // check if this is a category, or sub category } else { $finder = $this->getFinder('categories'); // acount for second-level sub category $category_name = $request['b'] !== NULL ? $request['b'] : $request['a']; // get the category $category = $finder->findBy('category_nicename', $category_name); if($category === NULL) { $this->setNotFound(); } // get all categories from cached results so that we can get this category's // permalink using the proper nested category nice names $categories = $finder->findAllNames(); $category->permalink = $categories[$category->getId()]['nice_name']; // get the posts in this category through a relation $response['archive_posts'] = $posts_finder->findAllVisible(1, 0, array( 'category' => $category->getId(), )); $response['category'] = $category; $response['main'] = 'category.html'; } } } /** * RSS 2.0 feed. */ public function feed(Registry $registry, Request $request, Response $response) { @header('Content-type: text/xml; charset=' . $registry['blog_charset'], true); require_once VK_APP_DIR .'/functions.php'; $posts = $this->getFinder('posts')->findAllVisible(1, 10, array()); $response['last_modified'] = $posts->current()->getRssDate(); $response['posts'] = $posts; $response->setBase('rss2.html'); } /** * Log the user in. */ public function login(Registry $registry, Request $request, Response $response) { if(!$registry->user->login($request['name'], $request['pass'], (bool)(int)$request['rememberme'])) { $response['main'] = 'errors/error_login.html'; return; } // assuming we're logged in, try to log us in through wordpress $data = array( 'redirect_to' => vk_path('/'), 'log' => $request['name'], 'pwd' => $request['pass'], 'rememberme' => $request['rememberme'], ); $data = urlencode($data); $host = vk_url(); $fp = fsockopen($host, 80, $error_no, $error_str); if($fp) { $header = "POST wp-login.php?action=login HTTP/1.1\r\n"; $header.= "Host: ". $host ."\r\n"; $header.= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0\r\n"; $header.= "Content-Type: application/x-www-form-urlencoded\r\n"; $header.= "Content-Length: ".strlen($data)."\r\n"; $header.= "Connection: close\r\n\r\n"; $header.= $data; fwrite($fp, $header); } fclose($fp); // redirect us! header("Location: ". $request->server('HTTP_REFERER')); } /** * Log the user out. */ public function logout(Registry $registry, Request $request, Response $response) { $registry->user->logout(); // logout through wordpress as well header("Location: wp-login.php?action=logout"); }}