slacken 13 년 전
커밋
1bba94fd00
8개의 변경된 파일275개의 추가작업 그리고 0개의 파일을 삭제
  1. 9 0
      config.yaml
  2. 105 0
      include/controller.php
  3. 10 0
      include/lib.php
  4. 15 0
      include/start.php
  5. 40 0
      include/storage.php
  6. 32 0
      include/view.php
  7. 53 0
      index.php
  8. 11 0
      views/welcome.php

+ 9 - 0
config.yaml

@@ -0,0 +1,9 @@
+name: mysaecdn
+version: 1
+handle:
+- compress: if ( out_header["Content-type"]=="text/html" ) compress
+- compress: if ( out_header["Content-type"]=="text/javascript" ) compress
+- compress: if ( out_header["Content-type"]=="text/css" ) compress
+- compress: if ( out_header["Content-type"]=="image/jpeg" ) compress
+- compress: if ( out_header["Content-type"]=="image/png" ) compress
+- rewrite: if ( !is_dir() && !is_file() && path ~ "^(.*)$" ) goto "index.php?q=$1"

+ 105 - 0
include/controller.php

@@ -0,0 +1,105 @@
+<?php
+
+if ( ! defined('BASE_PATH')) exit('No direct script access allowed');
+
+class controller{
+	
+	public $content_type;
+	
+	public $succeed ;
+	
+	public $error_type;
+	
+	public function __construct($request = ''){
+		
+		$this->content_type = 'text/plain';
+		$this->error_type = 0;
+		$this->succeed = TRUE;
+		
+		$request = ltrim($request,'/');
+		
+		//是否为SAE环境
+		if(!IS_SAE || !class_exists('SaeStorage')){//
+			$this->error_type = 2;
+			$this->succeed = FALSE;
+		}
+		
+		//请求为空
+		elseif($request === ''){
+			
+			//显示欢迎页面
+			if(WELCOME_DOC){
+				view::show('welcome');
+				return ;
+			}else{
+				$this->error_type = 1;
+				$this->succeed = FALSE;
+			}
+		}
+		
+		else{
+			//匹配文件后缀
+			$temp = array();
+			if(preg_match('/\.(jpg|jpeg|png|pdf|gif|css|js|zip)$/i', $request,$temp)===1){//暂时先就这几种
+				//http://en.wikipedia.org/wiki/Internet_media_type#List_of_common_media_types
+				switch($temp[1]){
+					case 'jpg':{$this->content_type="image/jpeg";}break;
+					case 'gif':{$this->content_type="image/gif";}break;
+					case 'png':{$this->content_type="image/png";}break;
+					case 'css':{$this->content_type="text/css";}break;
+					case 'js':{$this->content_type="text/javascript";}break;
+				}
+			}
+		}
+		
+		//开始处理
+		$this->handle($request);
+		
+	}
+	/**
+	 * 获取内容并输出
+	 * 如果stroage里面不存在,则从URL里面获取
+	 * */
+	private function handle($filename){
+		$content = '';
+		if($this->succeed){
+			$storage = new storage();
+			if($storage->exists($filename)){
+				$content = $storage->read($filename);
+			}else{
+				$content = @file_get_contents(BASE_URL.$filename);
+				$storage->write($filename, $content);
+			}
+			if(empty($content)){
+				$this->error_type = 3;
+				$this->succeed = FALSE;
+			}
+		}
+		//显示内容
+		$this->render($content);
+	}
+	
+	
+	/**
+	 * 输出结果,包括缓存控制等
+	 * */
+	private function render($content=''){
+		if(!$this->succeed){
+			$this->error();
+			return ;
+		}else{
+			header("Expires: " . date("D, j M Y H:i:s GMT", time()+2592000));//缓存一月
+			header('Content-type: '.$this->content_type);
+			echo $content;
+		}
+	}
+	
+	/**
+	 * 处理错误
+	 * */
+	private function error(){
+		echo "<strong>something seems wrong.</strong>";
+	}
+	
+	
+}

+ 10 - 0
include/lib.php

@@ -0,0 +1,10 @@
+<?php
+if ( ! defined('BASE_PATH')) exit('No direct script access allowed');
+/**
+ * 公共函数
+ * */
+class lib{
+	
+	
+
+}

+ 15 - 0
include/start.php

@@ -0,0 +1,15 @@
+<?php
+//
+if ( ! defined('BASE_PATH')) exit('No direct script access allowed');
+
+/**
+ * 自动加载
+ * */
+function includeloader($class){
+	$path = BASE_PATH."include/{$class}.php";
+	if (is_readable($path)) require $path;
+}
+
+spl_autoload_register('includeloader');
+
+new controller(isset($_GET['q'])?$_GET['q']:'');

+ 40 - 0
include/storage.php

@@ -0,0 +1,40 @@
+<?php
+
+if ( ! defined('BASE_PATH')) exit('No direct script access allowed');
+
+/**
+ * 封装SAE storage
+ * */
+class storage{
+	
+	public $instance;
+	
+	public $domain;
+	
+	public function __construct(){
+		
+		$this->domain = DOMAIN;
+		$this->instance = new SaeStorage(SAE_ACCESSKEY,SAE_SECRETKEY);
+	}
+	
+	public function exists($filename){
+		return $this->instance->fileExists($this->domain,$filename);
+	}
+	//这里是效率瓶颈啊!!
+	public function read($filename){
+		return $this->instance->read($this->domain,$filename);
+	}
+	
+	public function write($name,$content){
+		return $this->instance->write($this->domain,$name,$content);
+	}
+	
+	public function url($name){
+		return $this->instance->getUrl($this->domain,$name);
+	}
+	
+	public function error(){
+		return $this->instance->error();
+	}
+	
+}

+ 32 - 0
include/view.php

@@ -0,0 +1,32 @@
+<?php
+
+if ( ! defined('BASE_PATH')) exit('No direct script access allowed');
+
+class view{
+	
+    public function __construct(){
+        ob_start();
+    }
+    
+    public function render(){}
+    
+    public function finish(){
+        $content = ob_get_clean();
+        return $content;
+    }
+    
+    /**
+     * 直接显示
+     * */
+	public static function show($page, $data = array()){
+		$page = BASE_PATH.'views/'.trim($page,'/').'.php';
+		if(is_readable($page)){
+			ob_start();
+			
+	    	include $page;
+	    	ob_end_flush();
+		}
+	}
+	
+}
+?>

+ 53 - 0
index.php

@@ -0,0 +1,53 @@
+<?php
+
+
+/**
+ * 设置源静态文件的根目录的URL地址
+ * */
+define('STATIC_URL','http://www.baidu.com/');
+
+/**
+ * SAE storage的domain
+ * */
+define('DOMAIN','cdn');
+
+/**
+ * 空请求时是否显示文档
+ * */
+define('WELCOME_DOC',TRUE);
+
+/**
+ * 运行环境:development/testing/production
+ * */
+define('ENVIRONMENT','development');
+
+//========================================================
+
+
+if (defined('ENVIRONMENT'))
+{
+	switch (ENVIRONMENT)
+	{
+		case 'development':
+			error_reporting(E_ALL);
+		break;
+	
+		case 'testing':
+		case 'production':
+			error_reporting(0);
+		break;
+
+		default:
+			exit('The application environment is not set correctly.');
+	}
+}
+
+//本地根目录
+define('BASE_PATH',dirname(__FILE__).'/');
+
+define('BASE_URL', rtrim(STATIC_URL,'/').'/');
+
+define('IS_SAE', defined('SAE_SECRETKEY'));
+
+require_once BASE_PATH.'include/start.php';
+

+ 11 - 0
views/welcome.php

@@ -0,0 +1,11 @@
+<?php if ( ! defined('BASE_PATH')) exit('No direct script access allowed');?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
+<html>
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+<title>说明文档</title>
+</head>
+<body>
+这是说明文档啦!!稍候奉上
+</body>
+</html>