Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once '../lib/Slim/Slim.php';
- require_once '../lib/blog.php';
- require_once '../lib/user.php';
- \Slim\Slim::registerAutoloader();
- date_default_timezone_set('utc');
- function getBlog($app,$elevate=false){return new Blog($app);}
- function baseURL($extend,$b=null){
- static $base;
- if(!isset($base))
- $base = $b===null?".":$b;
- return "$base/$extend";
- }
- $app = new \Slim\Slim();
- function getTags($posts){
- $ret=array();
- foreach($posts as $post)
- foreach($post['meta']['tag'] as $tag)
- array_push($ret,$tag);
- return $ret;
- }
- function iterationCount($arr){
- $used = array();
- foreach($arr as $obj){
- if(array_key_exists($obj,$used))
- $used[$obj]+=1;
- else $used[$obj]=1;
- }
- $tags = array_keys($used);
- usort($tags,function($a,$b) use($used){
- if($used[$a]==$used[$b]) return 0;
- return ($used[$a]<$used[$b])?-1:1;
- });
- return $tags;
- }
- function setPostLinks($posts)
- {
- foreach($posts as &$post)
- {
- $post['link']=baseUrl("#!posts/{$post['id']}");
- }
- return $posts;
- }
- function renderPost($post)
- {
- ?>
- <div class='post' id=<?php echo "post-{$post['id']}"; ?>>
- <h1><a href=<?php echo "'{$post['link']}'"; ?>>
- <?php echo $post['title']; ?>
- </a></h1>
- <div class='content'><?php echo $post['content']; ?></div>
- <div class='meta'>
- <div class='tags'><?php foreach($post['meta']['tag'] as $tag): ?>
- <span class='padding'><span class='tag'><?php echo $tag; ?></span></span>
- <?php endforeach; ?></div>
- <div class='info'>
- Written by
- <span class='author'><?php echo $post['meta']['author']; ?></span>
- on
- <span class='date'><?php echo $post['meta']['date']; ?></span>
- </div>
- </div>
- </div>
- <?php }
- $app->get("/posts/:id",function($postID) use($app){
- baseUrl("","..");
- $blog = getBlog($app);
- $posts = setPostLinks($blog->posts("id=".mysql_real_escape_string($postID)));
- if(count($posts)==1)
- {
- $post = $posts[0];?>
- <html>
- <head>
- <title><?php echo $post['title'];?></title>
- <meta name="description" content=<?php echo "'".$post['description']."'"; ?> />
- <meta name="keywords" content=<?php echo "'".implode(" ",$post['meta']['tag'])."'"; ?> />
- </head>
- <body><?php renderPost($post); ?></body>
- </html>
- <?php }
- else $app->pass();
- });
- $app->get("/posts",function() use($app){
- baseUrl("",".");
- $since = $app->request()->params('since');
- if($since!==null)
- $since = "modified >= FROM_UNIXTIME($since)";
- else $since = "true";
- $blog = getBlog($app);
- $posts = setPostLinks($blog->posts($since));
- $postCount = count($posts);
- if($app->request()->isAjax()||isset($_GET['ajax']))
- {
- $app->response()['Content-Type'] = 'text/json';
- echo renderJSON($posts);
- }
- else{ ?>
- <html>
- <head>
- <title><?php echo ($postCount<1?"Posts":$posts[0]['title']); ?></title>
- <meta name="description" content=<?php echo "'".($postCount>=1?$posts[0]['description']:"")."'"; ?> />
- <meta name="keywords" content=<?php
- $tags = null;
- if($postCount>1)
- $tags = array_slice(iterationCount(getTags($posts)),0,10);
- elseif($postCount>0)
- $tags = array_slice($posts[0]['meta']['tag'],0,10);
- else $tags = array();
- echo "'".implode(" ",$tags)."'";
- ?> />
- </head>
- <body><?php foreach($posts as $post) renderPost($post); ?></body>
- </html>
- <?php }
- });
- $app->get("/tags",function() use($app){
- $blog = getBlog($app);
- $tags = $blog ->tags();
- if($app->request()->isAjax())
- $app->render('json',array("response"=>$posts));
- else {?>
- <html>
- <head>
- <title>Popular Tags</title>
- </head>
- <body>
- <table>
- <tr><th>Tag</th><th>Frequency</th>
- <?php foreach($tags as $tag): ?><tr>
- <td><?php echo $tag['tag']; ?></td>
- <td><?php echo $tag['quantity']; ?></td>
- </tr><?php endforeach; ?>
- </table>
- </body>
- </html>
- <?php }
- });
- $app->get("/",function() use($app){
- $data = $app->request()->params('_escaped_fragment_');
- if($data!==null)
- $app->redirect($data);
- });
- $app->run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement