Advertisement
Tyler_Elric

BlogAPI.php

Nov 6th, 2012
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.85 KB | None | 0 0
  1. <?php
  2. require_once '../lib/Slim/Slim.php';
  3. require_once '../lib/blog.php';
  4. require_once '../lib/user.php';
  5.  
  6. \Slim\Slim::registerAutoloader();
  7.  
  8. date_default_timezone_set('utc');
  9.  
  10. function getBlog($app,$elevate=false){return new Blog($app);}
  11.  
  12. function baseURL($extend,$b=null){
  13.     static $base;
  14.     if(!isset($base))
  15.         $base = $b===null?".":$b;
  16.     return "$base/$extend";
  17. }
  18.  
  19. $app = new \Slim\Slim();
  20.  
  21. function getTags($posts){
  22.     $ret=array();
  23.     foreach($posts as $post)
  24.         foreach($post['meta']['tag'] as $tag)
  25.             array_push($ret,$tag);
  26.     return $ret;
  27. }
  28.  
  29. function iterationCount($arr){
  30.     $used = array();
  31.     foreach($arr as $obj){
  32.         if(array_key_exists($obj,$used))
  33.             $used[$obj]+=1;
  34.         else $used[$obj]=1;
  35.     }
  36.     $tags = array_keys($used);
  37.     usort($tags,function($a,$b) use($used){
  38.         if($used[$a]==$used[$b]) return 0;
  39.         return ($used[$a]<$used[$b])?-1:1;
  40.     });
  41.     return $tags;
  42. }
  43.  
  44. function setPostLinks($posts)
  45. {
  46.     foreach($posts as &$post)
  47.     {
  48.         $post['link']=baseUrl("#!posts/{$post['id']}");
  49.     }
  50.     return $posts;
  51. }
  52.  
  53. function renderPost($post)
  54. {
  55.     ?>
  56.     <div class='post' id=<?php echo "post-{$post['id']}"; ?>>
  57.         <h1><a href=<?php echo "'{$post['link']}'"; ?>>
  58.             <?php echo $post['title']; ?>
  59.         </a></h1>
  60.         <div class='content'><?php echo $post['content']; ?></div>
  61.         <div class='meta'>
  62.             <div class='tags'><?php foreach($post['meta']['tag'] as $tag): ?>
  63.                 <span class='padding'><span class='tag'><?php echo $tag; ?></span></span>
  64.             <?php endforeach; ?></div>
  65.             <div class='info'>
  66.                 Written by
  67.                 <span class='author'><?php echo $post['meta']['author']; ?></span>
  68.                 on
  69.                 <span class='date'><?php echo $post['meta']['date']; ?></span>
  70.             </div>
  71.         </div>
  72.     </div>
  73. <?php }
  74.  
  75. $app->get("/posts/:id",function($postID) use($app){
  76.     baseUrl("","..");
  77.     $blog = getBlog($app);
  78.     $posts = setPostLinks($blog->posts("id=".mysql_real_escape_string($postID)));
  79.     if(count($posts)==1)
  80.     {
  81.         $post = $posts[0];?>
  82.         <html>
  83.             <head>
  84.                 <title><?php echo $post['title'];?></title>
  85.                 <meta name="description" content=<?php echo "'".$post['description']."'"; ?> />
  86.                 <meta name="keywords" content=<?php echo "'".implode(" ",$post['meta']['tag'])."'"; ?> />
  87.             </head>
  88.             <body><?php renderPost($post); ?></body>
  89.         </html>
  90.     <?php }
  91.     else $app->pass();
  92. });
  93.  
  94. $app->get("/posts",function() use($app){
  95.     baseUrl("",".");
  96.     $since = $app->request()->params('since');
  97.     if($since!==null)
  98.         $since = "modified >= FROM_UNIXTIME($since)";
  99.     else $since = "true";
  100.     $blog = getBlog($app);
  101.     $posts = setPostLinks($blog->posts($since));
  102.     $postCount = count($posts);
  103.     if($app->request()->isAjax()||isset($_GET['ajax']))
  104.     {
  105.         $app->response()['Content-Type'] = 'text/json';
  106.         echo renderJSON($posts);
  107.     }
  108.     else{ ?>
  109.         <html>
  110.             <head>
  111.                 <title><?php echo ($postCount<1?"Posts":$posts[0]['title']); ?></title>
  112.                 <meta name="description" content=<?php echo "'".($postCount>=1?$posts[0]['description']:"")."'"; ?> />
  113.                 <meta name="keywords" content=<?php
  114.                     $tags = null;
  115.                     if($postCount>1)
  116.                         $tags = array_slice(iterationCount(getTags($posts)),0,10);
  117.                     elseif($postCount>0)
  118.                         $tags = array_slice($posts[0]['meta']['tag'],0,10);
  119.                     else $tags = array();
  120.                     echo "'".implode(" ",$tags)."'";
  121.                 ?> />
  122.             </head>
  123.             <body><?php foreach($posts as $post) renderPost($post); ?></body>
  124.         </html>
  125.     <?php }
  126. });
  127.  
  128. $app->get("/tags",function() use($app){
  129.     $blog = getBlog($app);
  130.     $tags = $blog ->tags();
  131.     if($app->request()->isAjax())
  132.         $app->render('json',array("response"=>$posts));
  133.     else {?>
  134.         <html>
  135.             <head>
  136.                 <title>Popular Tags</title>
  137.             </head>
  138.             <body>
  139.                 <table>
  140.                     <tr><th>Tag</th><th>Frequency</th>
  141.                     <?php foreach($tags as $tag): ?><tr>
  142.                         <td><?php echo $tag['tag']; ?></td>
  143.                         <td><?php echo $tag['quantity']; ?></td>
  144.                     </tr><?php endforeach; ?>
  145.                 </table>
  146.             </body>
  147.         </html>
  148.     <?php }
  149. });
  150.  
  151. $app->get("/",function() use($app){
  152.     $data = $app->request()->params('_escaped_fragment_');
  153.     if($data!==null)
  154.         $app->redirect($data);
  155. });
  156.  
  157. $app->run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement