Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 6 commits
  • 14 files changed
  • 0 commit comments
  • 1 contributor
View
91 include/Controller.php
@@ -0,0 +1,91 @@
+<?php
+abstract class Controller
+{
+ /**
+ * Controller name
+ */
+ protected $name;
+
+ /**
+ * Action to render
+ */
+ protected $action;
+
+ /**
+ * User input data
+ */
+ protected $data;
+
+ public function __construct()
+ {
+ $this->loadModel($this->name);
+ }
+
+ protected function loadModel($model = null)
+ {
+ $model_class = "Model_${model}";
+
+ try {
+ $obj = new $model_class();
+ } catch (RuntimeException $e) {
+ die("Database error.");
+ }
+
+ $this->$model = $obj;
+ }
+
+ public function getCacheName($data = null)
+ {
+ if (empty($data)) {
+ return sprintf(
+ '%s-%s',
+ $this->name,
+ $this->action
+ );
+ }
+
+ ksort($data);
+ $data = implode('-', $data);
+
+ return sprintf(
+ '%s-%s-%s',
+ $this->name,
+ $this->action,
+ $data
+ );
+ }
+
+ public function setAction($action = null)
+ {
+ $this->action = $action;
+ }
+
+ public function setData($data = array())
+ {
+ $this->data = $data;
+ }
+
+ public function render()
+ {
+ ob_start();
+
+ try {
+ $viewData = call_user_func(array($this, $this->action));
+ extract($viewData);
+
+ $template = sprintf(
+ '%s/index.tpl',
+ $GLOBALS['BX_config']['theme']
+ );
+ include TEMPLATE_DIR . $template;
+
+ } catch (Exception $e) {
+ die("Just come back later.");
+ }
+
+ $page = ob_get_contents();
+ ob_end_clean();
+
+ return $page;
+ }
+}
View
60 include/Controller/Feed.php
@@ -0,0 +1,60 @@
+<?php
+class Controller_Feed extends Controller
+{
+ protected $name = 'Feed';
+
+ public function atom()
+ {
+ $this->Feed->setType('atom');
+ return $this->feed();
+ }
+
+ public function rss()
+ {
+ $this->Feed->setType('rss');
+ return $this->feed();
+ }
+
+ protected function feed()
+ {
+ /**
+ * If secred hash is set, password protect feed
+ */
+ if (defined('SECRET_FEED_HASH') &&
+ (SECRET_FEED_HASH != '')
+ ) {
+ if ($this->data['hash'] != SECRET_FEED_HASH) {
+ die('Denied.');
+ }
+ }
+
+ try {
+ $this->loadModel('Planet');
+ $results = $this->Planet->getEntries();
+
+ /**
+ * Retrieve entries
+ */
+ foreach ($results as $result) {
+
+ $entry = $this->Feed->createEntry();
+
+ $entry->setTitle($result['title']);
+ $entry->setLink($result['link']);
+ $entry->setDescription($result['description']);
+ $entry->setGuid($result['guid']);
+ $entry->setSource(array(
+ 'title' => $result['title'],
+ 'url' => $result['link'],
+ ));
+
+ $this->Feed->addEntry($entry);
+ }
+
+ echo $this->Feed;
+
+ } catch (Exception $e) {
+ die('Feed fail.');
+ }
+ }
+}
View
119 include/Controller/Planet.php
@@ -0,0 +1,119 @@
+<?php
+/**
+ * @author Till Klampaeckel <till@php.net>
+ */
+class Controller_Planet extends Controller
+{
+ protected $name = 'Planet';
+
+ public function index()
+ {
+ $this->setData(array(
+ 'page' => 0,
+ 'query' => null
+ ));
+ return $this->page();
+ }
+
+ /**
+ * page action
+ *
+ * @param int $from The start key.
+ * @param string $query Search query.
+ *
+ * @return array
+ */
+ public function page()
+ {
+ $data = array_merge(
+ array(
+ 'page' => 0,
+ 'query' => null
+ ),
+ $this->data
+ );
+ extract($data);
+
+ $return = array();
+ $return['blogs'] = $this->Planet->getBlogs();
+ $return['BX_config'] = $GLOBALS['BX_config'];
+
+ if (empty($query)) {
+ $query = null;
+ $return['nav'] = $this->Planet->getNavigation($page);
+ } else {
+ $return['nav'] = array('prev' => null, 'next' => null);
+ }
+
+ $return['entries'] = $this->Planet->getEntries(
+ 'default', $page, $query
+ );
+
+ return $return;
+ }
+
+ public function search()
+ {
+ return $this->page();
+ }
+
+ /**
+ * List OPML feed
+ *
+ * @return void
+ */
+ public function opml()
+ {
+ header('Content-Type: text/x-opml');
+ header('Content-Type: text/xml');
+ $project = PROJECT_NAME_HR;
+ $title = PROJECT_NAME_HR . ' feed list';
+ $created = date('r');
+ echo <<<XML
+<?xml version="1.0" encoding="utf-8" ?>
+<opml version="2.0">
+ <head>
+ <title>{$title}</title>
+ <dateCreated>{$created}</dateCreated>
+ </head>
+ <body>
+XML;
+ foreach ($this->Planet->getFeeds('default') as $data) {
+ echo '<outline type="rss" text="'
+ . htmlspecialchars($data['title'])
+ . '" htmlUrl="'
+ . htmlspecialchars($data['blogurl'])
+ . '" xmlUrl="'
+ . htmlspecialchars($data['feedurl'])
+ . '" />' . "\n";
+ }
+ echo <<< XML
+ </body>
+</opml>
+XML;
+ exit(0);
+ }
+
+ public function setData($data = array())
+ {
+ if (array_key_exists('query', $data)) {
+ $data['query'] = trim($data['query']);
+ }
+ if (array_key_exists('page', $data)) {
+ $data['page'] = (int)($data['page']);
+ }
+
+ $this->data = $data;
+ }
+
+ public function getCacheName()
+ {
+ $data = $this->data;
+
+ if (array_key_exists('query', $data)) {
+ $data['query'] = md5($data['query']);
+ }
+
+ return parent::getCacheName($data);
+ }
+}
View
11 include/Controller/Submit.php
@@ -0,0 +1,11 @@
+<?php
+class Controller_Submit extends Controller
+{
+ public function index()
+ {
+ }
+
+ public function submit()
+ {
+ }
+}
View
3  include/Model.php
@@ -0,0 +1,3 @@
+<?php
+class Model {
+}
View
14 include/PlanetPEAR/Feed.php → include/Model/Feed.php
@@ -4,7 +4,7 @@
* @package Planet-PHP
* @author Till Klampaeckel <till@php.net>
*/
-class PlanetPEAR_Feed
+class Model_Feed extends Model
{
/**
* @var array $entries
@@ -24,7 +24,7 @@ class PlanetPEAR_Feed
/**
* @param string $type
*
- * @return PlanetPEAR_Feed
+ * @return Model_Feed
*
* @uses self::setType()
* @uses self::reset()
@@ -113,11 +113,11 @@ protected function getFeedDefinition()
/**
* Add another entry to this feed.
*
- * @param PlanetPEAR_Feed_Entry $entry
+ * @param Model_Feed_Entry $entry
*
- * return PlanetPEAR_Feed
+ * return Model_Feed
*/
- public function addEntry(PlanetPEAR_Feed_Entry $entry)
+ public function addEntry(Model_Feed_Entry $entry)
{
$arrEntry = $entry->toArray();
$this->entries[] = $arrEntry;
@@ -126,11 +126,11 @@ public function addEntry(PlanetPEAR_Feed_Entry $entry)
}
/**
- * @return PlanetPEAR_Feed_Entry
+ * @return Model_Feed_Entry
*/
public function createEntry()
{
- $entry = new PlanetPEAR_Feed_Entry();
+ $entry = new Model_Feed_Entry();
return $entry;
}
View
2  include/PlanetPEAR/Feed/Entry.php → include/Model/Feed/Entry.php
@@ -1,5 +1,5 @@
<?php
-class PlanetPEAR_Feed_Entry
+class Model_Feed_Entry extends Model
{
protected $data = array(
View
82 include/PlanetPEAR.php → include/Model/Planet.php
@@ -1,16 +1,11 @@
<?php
-class PlanetPEAR
+class Model_Planet extends Model
{
protected $db; // MDB2_Common
protected $queryRestriction; // no idea
protected $tally = 10; // blog entries per page
/**
- * Request data.
- */
- protected $controller, $action, $from, $query;
-
- /**
* @param MDB2_Common $db Optional MDB2 object.
*
* @return $this
@@ -22,25 +17,13 @@ public function __construct(MDB2_Common $db = null)
} else {
$this->db = MDB2::connect($GLOBALS['BX_config']['dsn']);
}
- }
-
- /**
- * Render the template.
- *
- * @param string $tpl The filename.
- * @param array $vars The template vars.
- *
- * @return boolean
- */
- public function render($tpl, array $vars)
- {
- extract($vars);
- //var_dump($entries);
-
- $file = TEMPLATE_DIR . $tpl;
-
- return include $file;
+ if (MDB2::isError($this->db)) {
+ throw new RuntimeException(
+ $this->db->getUserInfo(),
+ $this->db->getCode()
+ );
+ }
}
/**
@@ -81,24 +64,6 @@ public function getBlogs($section = 'default', $forceAll = false)
return $res;
}
- public function getCacheName()
- {
- if ($this->isQuery()) {
- return 'search-' . md5($this->query);
- }
- return sprintf(
- '%s-%s-%s',
- $this->controller,
- $this->action,
- $this->from
- );
- }
-
- public function getController()
- {
- return $this->controller;
- }
-
/**
* Fetch and return a list of all feeds
*
@@ -242,37 +207,4 @@ public function getNavigation($startKey)
return $navigation;
}
-
-
- public function isQuery()
- {
- if (!empty($this->query)) {
- return true;
- }
- return false;
- }
-
- public function setAction($action)
- {
- $this->action = strtolower($action);
- return $this;
- }
-
- public function setController($controller)
- {
- $this->controller = ucfirst(strtolower($controller));
- return $this;
- }
-
- public function setFrom($from)
- {
- $this->from = $from;
- return $this;
- }
-
- public function setQuery($query)
- {
- $this->query = trim($query);
- return $this;
- }
}
View
10 include/PlanetPEAR/Controller/Base.php
@@ -1,10 +0,0 @@
-<?php
-abstract class PlanetPEAR_Controller_Base
-{
- protected $planet;
-
- public function __construct(PlanetPEAR $planet)
- {
- $this->planet = $planet;
- }
-}
View
69 include/PlanetPEAR/Controller/Index.php
@@ -1,69 +0,0 @@
-<?php
-/**
- * @author Till Klampaeckel <till@php.net>
- */
-class PlanetPEAR_Controller_Index extends PlanetPEAR_Controller_Base
-{
- protected $data;
- protected $planet;
-
- public function index()
- {
- return $this->page(0, null);
- }
-
- /**
- * page action
- *
- * @param int $from The start key.
- * @param string $query Search query.
- *
- * @return array
- */
- public function page($from, $query)
- {
- if (empty($query)) {
- $query = null;
- }
- $this->data['entries'] = $this->planet->getEntries('default', $from, $query);
-
- return $this->data;
- }
-
- /**
- * List OPML feed
- *
- * @return void
- */
- public function opml()
- {
- header('Content-Type: text/x-opml');
- header('Content-Type: text/xml');
- $project = PROJECT_NAME_HR;
- $title = PROJECT_NAME_HR . ' feed list';
- $created = date('r');
- echo <<<XML
-<?xml version="1.0" encoding="utf-8" ?>
-<opml version="2.0">
- <head>
- <title>{$title}</title>
- <dateCreated>{$created}</dateCreated>
- </head>
- <body>
-XML;
- foreach ($this->planet->getFeeds('default') as $data) {
- echo '<outline type="rss" text="'
- . htmlspecialchars($data['title'])
- . '" htmlUrl="'
- . htmlspecialchars($data['blogurl'])
- . '" xmlUrl="'
- . htmlspecialchars($data['feedurl'])
- . '" />' . "\n";
- }
- echo <<< XML
- </body>
-</opml>
-XML;
- exit(0);
- }
-}
View
3  public/.htaccess
@@ -15,9 +15,6 @@ RewriteEngine On
RewriteRule ^theme/(.*)$ theme-router.php?r=$1 [L]
RewriteRule ^favicon\.ico$ theme-router.php?r=favicon.ico [L]
RewriteRule ^opensearch\.xml$ theme-router.php?r=opensearch.xml [L]
-RewriteRule ^atom$ feed.php?type=atom [QSA,L]
-RewriteRule ^rss$ feed.php?type=rss [QSA,L]
-RewriteRule ^search/(.*)$ router.php?search=$1 [L]
RewriteRule ^index/(.*)$ $1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
View
47 public/feed.php
@@ -1,47 +0,0 @@
-<?php
-/**
- * Configuration file.
- */
-require_once dirname(dirname(__FILE__)).'/config/config.inc.php';
-
-/**
- * If secred hash is set, password protect feed
- */
-if (defined('SECRET_FEED_HASH')) {
- if (SECRET_FEED_HASH != '') {
- if ($_GET['hash'] != SECRET_FEED_HASH) {
- die('Denied.');
- }
- }
-}
-
-try {
- $planet = new PlanetPEAR;
- $results = $planet->getEntries();
-
- $planetFeed = new PlanetPEAR_Feed($_GET['type']);
-
- /**
- * Retrieve entries
- */
- foreach ($results as $result) {
-
- $entry = $planetFeed->createEntry();
-
- $entry->setTitle($result['title']);
- $entry->setLink($result['link']);
- $entry->setDescription($result['description']);
- $entry->setGuid($result['guid']);
- $entry->setSource(array(
- 'title' => $result['title'],
- 'url' => $result['link'],
- ));
-
- $planetFeed->addEntry($entry);
- }
-
- echo $planetFeed;
-
-} catch (Exception $e) {
- die('Feed fail.');
-}
View
85 public/router.php
@@ -8,80 +8,49 @@
try {
$m = Net_URL_Mapper::getInstance();
- $m->connect('opml', array('controller' => 'index', 'action' => 'opml'));
- $m->connect('index', array('controller' => 'index', 'action' => 'index'));
- $m->connect('index/:from', array('controller' => 'index', 'action' => 'page'));
-
- $match = $m->match($_SERVER['REQUEST_URI']);
-
- if ($match === null) {
- $match = array('controller' => 'index', 'action' => 'index');
- }
+ $m->connect('opml', array('controller' => 'planet', 'action' => 'opml'));
+ $m->connect('index', array('controller' => 'planet', 'action' => 'index', 'from' => 0));
+ $m->connect('index/:page', array('controller' => 'planet', 'action' => 'page'));
+ $m->connect('search/:query', array('controller' => 'planet', 'action' => 'page'));
+ $m->connect('submit', array('controller' => 'submit', 'action' => 'index'));
+ $m->connect('admin', array('controller' => 'admin', 'action' => 'index'));
+ $m->connect('atom', array('controller' => 'feed', 'action' => 'atom'));
+ $m->connect('atom/:hash', array('controller' => 'feed', 'action' => 'atom'));
+ $m->connect('rss', array('controller' => 'feed', 'action' => 'rss'));
+ $m->connect('rss/:hash', array('controller' => 'feed', 'action' => 'rss'));
+
+ $route = $m->match($_SERVER['REQUEST_URI']);
} catch (Net_URL_Mapper_Exception $e) {
die("Something went wrong.");
}
-$query = (string) @$_GET['search'];
-if (!empty($query)) {
- $match['action'] = 'page';
-} else {
- $query = null;
+if ($route === null) {
+ $route = array('controller' => 'planet', 'action' => 'index');
}
-if (!isset($match['from'])) {
- $from = 0;
-} else {
- $from = (int) $match['from'];
-}
+$c = "Controller_".ucfirst(strtolower($route['controller']));
+$controller = new $c();
-$planet = new PlanetPEAR;
-$planet->setController($match['controller']);
-$planet->setAction($match['action']);
-$planet->setFrom($from);
-$planet->setQuery($query);
+$controller->setAction(strtolower($route['action']));
+$controller->setData(array_diff_key(
+ $route,
+ array('controller' => null, 'action' => null)
+));
-$cacheName = $planet->getCacheName();
-$cacheFile = BX_TEMP_DIR . '/' . $cacheName;
+$cacheFile = BX_TEMP_DIR . '/' . $controller->getCacheName();
if (!file_exists($cacheFile) || PLANET_DEBUG) {
- ob_start();
-
- $controller = 'PlanetPEAR_Controller_' . $planet->getController();
- $controllerObj = new $controller($planet);
-
- try {
- $viewData = call_user_func_array(array($controllerObj, $match['action']), array($from, $query));
-
- $viewData['blogs'] = $planet->getBlogs();
- $viewData['BX_config'] = $BX_config;
-
- if ($planet->isQuery() === false) {
- $viewData['nav'] = $planet->getNavigation($from);
- } else {
- $viewData['nav'] = array('prev' => null, 'next' => null);
- }
-
- $planet->render(
- sprintf(
- '%s/index.tpl',
- $BX_config['theme']
- ),
- $viewData
- );
-
- } catch (Exception $e) {
- die("Just come back later.");
- }
- $page = ob_get_contents();
- ob_end_clean();
-
$fp = fopen($cacheFile, 'w');
if ($fp) {
fwrite($fp, $page);
fclose($fp);
}
- echo $page;
+
+ echo $controller->render();
+
} else {
+
echo file_get_contents($cacheFile);
+
}
View
2  scripts/aggregate.php
@@ -32,7 +32,7 @@
*/
$agg->aggregateAllBlogs($blogId);
if ($agg->isNew() === true) {
- foreach (glob(BX_TEMP_DIR . 'Index-*') as $file) {
+ foreach (glob(BX_TEMP_DIR . 'Planet-*') as $file) {
unlink($file);
}
}

No commit comments for this range

Something went wrong with that request. Please try again.