“博客迷”(BlogMi)源码阅读[1]

最近在学习PHP语言,想做一个博客程序.于是下载了一个博客程序的源码来研究.这个程序比较小巧,通过阅读可以快速掌握编写一个博客的方法.

阅读顺序是从第一个文件index.php开始.源码如下:

本文件的思路是:

1.包含核心文件: 

require_once dirname(__FILE__).'/files/core.php';

核心文件包含了自定义的函数和全局变量.还有一个404页面函数.

2.通过正则表达式来确定请求类型.

3.根据请求的正文类型来进行查询.

require 'files/posts/index/publish.php';

4.查询完毕后通过模板进行输出.

if ($mc_get_type != 'rss') require 'files/theme/v/index.php';
else require 'files/rss.php';

  1 <?php
  2 require_once dirname(__FILE__).'/files/core.php';
  3 $mc_post_per_page = 2;
  4 $qs = $_SERVER['QUERY_STRING'];
  5 if (preg_match('|^post/([a-z0-5]{6})$|', $qs, $matches)) {
  6   $mc_get_type = 'post';
  7   $mc_get_name = $matches[1];
  8 }
  9 else if (preg_match('|^tag/([^/]+)/(\?page=([0-9]+)){0,1}$|', $qs, $matches)) {
 10   $mc_get_type = 'tag';
 11   $mc_get_name = urldecode($matches[1]);
 12   $mc_page_num = isset($matches[2]) ? $matches[3] : 1;
 13 }
 14 else if (preg_match('|^date/([0-9]{4}-[0-9]{2})/(\?page=([0-9]+)){0,1}$|', $qs, $matches)) {
 15   $mc_get_type = 'date';
 16   $mc_get_name = urldecode($matches[1]);
 17   $mc_page_num = isset($matches[2]) ? $matches[3] : 1;
 18 }
 19 else if (preg_match('|^archive/$|', $qs, $matches)) {
 20   $mc_get_type = 'archive';
 21 }
 22 else if ($qs == 'rss/') {
 23   $mc_get_type = 'rss';
 24   $mc_get_name = '';
 25   $mc_page_num = isset($_GET['page']) ? $_GET['page'] : 1;
 26 }
 27 else if (preg_match('|^(([-a-zA-Z0-5]+/)+)$|', $qs, $matches)) {
 28   $mc_get_type = 'page';
 29   $mc_get_name = substr($matches[1], 0, -1);
 30 } else {
 31   $mc_get_type = 'index';
 32   $mc_get_name = '';
 33   $mc_page_num = isset($_GET['page']) ? $_GET['page'] : 1;
 34 }
 35 if ($mc_get_type == 'post') {
 36   require 'files/posts/index/publish.php';
 37   if (array_key_exists($mc_get_name, $mc_posts)) {
 38     $mc_post_id = $mc_get_name;
 39     $mc_post = $mc_posts[$mc_post_id];
 40     $mc_data = unserialize(file_get_contents('files/posts/data/'.$mc_post_id.'.dat'));
 41   }
 42   else {
 43     mc_404();
 44   }
 45 }
 46 else if ($mc_get_type == 'tag') {
 47   require 'files/posts/index/publish.php';
 48   $mc_post_ids = array_keys($mc_posts);
 49   $mc_post_count = count($mc_post_ids);
 50   $mc_tag_posts = array();
 51   for ($i = 0; $i < $mc_post_count; $i ++) {
 52     $id = $mc_post_ids[$i];
 53     $post = $mc_posts[$id];
 54     if (in_array($mc_get_name, $post['tags'])) {
 55       $mc_tag_posts[$id] = $post;
 56     }
 57   }
 58   $mc_posts = $mc_tag_posts;
 59   $mc_post_ids = array_keys($mc_posts);
 60   $mc_post_count = count($mc_post_ids);
 61 }
 62 else if ($mc_get_type == 'date') {
 63   require 'files/posts/index/publish.php';
 64   $mc_post_ids = array_keys($mc_posts);
 65   $mc_post_count = count($mc_post_ids);
 66   $mc_date_posts = array();
 67   for ($i = 0; $i < $mc_post_count; $i ++) {
 68     $id = $mc_post_ids[$i];
 69     $post = $mc_posts[$id];
 70     if (strpos($post['date'], $mc_get_name) === 0) {
 71       $mc_date_posts[$id] = $post;
 72     }
 73   }
 74   $mc_posts = $mc_date_posts;
 75   $mc_post_ids = array_keys($mc_posts);
 76   $mc_post_count = count($mc_post_ids);
 77 }
 78 else if ($mc_get_type == 'archive') {
 79   require 'files/posts/index/publish.php';
 80   $mc_post_ids = array_keys($mc_posts);
 81   $mc_post_count = count($mc_post_ids);
 82   $tags_array = array();
 83   $date_array = array();
 84   for ($i = 0; $i < $mc_post_count; $i ++) {
 85     $post_id = $mc_post_ids[$i];
 86     $post = $mc_posts[$post_id];
 87     $date_array[] = substr($post['date'], 0, 7);
 88     $tags_array = array_merge($tags_array, $post['tags']);
 89   }
 90   $mc_tags  = array_values(array_unique($tags_array));
 91   $mc_dates = array_values(array_unique($date_array));
 92 }
 93 else if ($mc_get_type == 'page') {
 94   require 'files/pages/index/publish.php';
 95   if (array_key_exists($mc_get_name, $mc_pages)) {
 96     $mc_post_id = $mc_get_name;
 97     $mc_post = $mc_pages[$mc_post_id];
 98     $mc_data = unserialize(file_get_contents('files/pages/data/'.$mc_post['file'].'.dat'));
 99   }
100   else {
101     mc_404();
102   }
103 }
104 else {
105   require 'files/posts/index/publish.php';
106   $mc_post_ids = array_keys($mc_posts);
107   $mc_post_count = count($mc_post_ids);
108 }
109 if ($mc_get_type != 'rss')
110   require 'files/theme/v/index.php';
111 else
112   require 'files/rss.php';
113 ?>

 

posted @ 2016-04-13 18:21  夜梦多离殇  阅读(312)  评论(0编辑  收藏  举报