[原创]SS FrameWork For PHP 4.0 (专注于extjs和flex的PHP MVC核心框架)

9 篇文章 0 订阅
6 篇文章 0 订阅
这是我以前写的PHP MVC 框架核心, 麻雀虽小五脏俱全,完整代码详见附件 SS FrameWork For PHP 4.0.rar

该框架主要用于php与extjs(json),flex(xml)的交互操作, 简单而实用,不像zend 那么臃肿(zend的功能你能用到20%已经很好了)

由于采用了ant, build前请下载JRE, 并设置好JAVA_HOME,CLASSPATH和PATH(或者放到jre目录), 然后点击install.bat,最后将build目录就是你要找的目录

======================================
几个核心代码:

index.php

<?php
require( 'define.php' );

/**
* SS FrameWork 3.0 For PHP
* @author 影枫.爪哇 <mail@shadowsnow.com>
* @copyright 2009-2011 skanion.iteye.com
* @version 3.0
* @since 2011.09.14
*/
$ctrl=isset($_REQUEST['c']) ? trim($_REQUEST['c']) : 'index';
$action=isset($_REQUEST['a']) ? ($_REQUEST['a']) : 'index';
ss_conf('db');
ss_conf('web');
ss_sys('db');
//db::getInstance()->query('SET NAMES UTF8');
ss_sys('id');
ss_sys('ip');
ss_sys('chklogin');
ss_sys('cache');
chklogin::init();
ip::setIp();
ss_model($ctrl);
ss_call_ctrl($ctrl,$action);


define.php

<?php
define('SS', 'ShadowSnow');
define('ACCESS', 'Access Declined.');
define('DS', DIRECTORY_SEPARATOR);
define('WEBROOT', dirname(__FILE__) .DS);
define('CLASSES',WEBROOT . 'classes' .DS);
define('FUN', CLASSES . 'function' .DS);
define('INC', CLASSES . 'inc' .DS);
define('MODEL', CLASSES . 'model' .DS);
define('SYS', CLASSES . 'system' .DS);
define('CONF', WEBROOT . 'config' .DS);
define('CTRL', WEBROOT . 'controller' .DS);
define('VIEW', WEBROOT . 'view' .DS);
define('CACHE', WEBROOT . 'cache' .DS);
define('PUB', WEBROOT . 'public' .DS);
define('IMGAE', PUB . 'image' .DS);
define('JS', PUB . 'js' .DS);
define('CSS', PUB . 'css' .DS);
define('PAGE', PUB . 'page' .DS);
define('SWF', PUB . 'swf' .DS);
define('URL', 'http://'. $_SERVER['SERVER_NAME']. '/');
define('JsonURL', 'http://'. $_SERVER['SERVER_NAME']. '/index.php');
//date_default_timezone_set("Etc/GMT-8");
date_default_timezone_set('Asia/Shanghai');
//echo date_default_timezone_get();
error_reporting(E_ALL);
ini_set('display_errors','On');
require( FUN. 'autoload.fun.php');
ss_conf('chk_lang');
ss_conf('lang_' . chk_lang::$lang);
ss_fun('main');
ss_fun('filter');
ss_sys('ss');
session_start();


db.sys.php:

<?php

defined('SS') or exit('Access Declined.');

class db_base
{

var $host = "";
var $database = "";
var $user = "";
var $password = "";
var $port = "";
var $socket = "";
var $record = array();
var $insert_id;
var $errno = 0;
var $error = "";
var $type = "mysql";
var $sql = "";
var $link = false;
var $result = false;
var $locked = false;
var $helper_db;
var $select = array();
var $from = '';
var $join = array() ;
var $on = array();
var $where = array();
var $orwhere = array();
var $order = array();
var $limit = '';
var $pconnect = false;
var $prepared_statement = false;


private static $Singleton;

public function __construct()
{
$this->set_config();
}

public static function GetInstance()
{
if(!(self::$Singleton instanceof db)){
self::$Singleton=new db();
}
return self::$Singleton;
}

public function set_config()
{
if (class_exists('db_config')){
$this->type = db_config::$db_type;
$this->host = db_config::$db_host;
$this->database = db_config::$db_name;
$this->user = db_config::$db_user;
$this->password = db_config::$db_pass;
$this->port = db_config::$db_port;
$this->socket = db_config::$db_socket;
}
}

public function set_parameters($type, $host, $database, $user, $pass, $port=3306, $socket='')
{
$this->type = $type;
$this->host = $host;
$this->database = $database;
$this->user = $user;
$this->password = $pass;
$this->port = $port;
$this->socket = $socket;
}

public function connect(){}

function free() {}

function clear()
{
$this->select = array();
$this->from = '';
$this->join = array();
$this->on = array();
$this->where = array();
$this->orwhere = array();
$this->order = array();
$this->limit = '';
}

public function selects($select=array())
{
if(!is_array($select)) $select=array($select);
if (count($select) > 0) $this->select = $select;
}

public function froms($from= '')
{
if ($from) $this->from = $from;
}

public function joins($join=array())
{
if(!is_array($join)) $join=array($join);
if (count($join) > 0) $this->join = $join;
}

public function ons($on=array())
{
if(!is_array($on)) $on=array($on);
if (count($on) > 0) $this->on = $on;
}

public function wheres($where=array())
{
if(!is_array($where)) $where=array($where);
if (count($where) > 0) $this->where = $where;
}

public function orwheres($orwhere=array())
{
if(!is_array($orwhere)) $orwhere=array($orwhere);
if (count($orwhere) > 0) $this->orwhere = $orwhere;
}

public function orders($order=array())
{
if(!is_array($order)) $order=array($order);
if (count($order) > 0) $this->order = $order;
}

public function limits($limit='')
{
if($limit) $this->limit= $limit . '';
}

public function getQuerySql()
{

$sql = '';
if(count($this->select) > 0){
$sql .= "SELECT " . implode(',', $this->select);
}
else{
$sql .= "SELECT * ";
}

if($this->from){
$sql .= " FROM ". $this->from ." ";
}
else{
return false;
}

if( count($this->on)> 0){
$ons = "";
$i = 0;
foreach ($this->on as $key => $value){
$sql .= " LEFT JOIN ".$this->join[$i]." ON " .$key. " = " .$value. " ";
$i++;
}
}


$isWhere = false;
if(count($this->where) > 0){
$isWhere = true;
$sql .= " WHERE ";
$ands = '';
foreach ($this->where as $key => $value){
if (!is_numeric($key)){
$ands .= " AND " .$key. " = '" .$value. "' ";
}
else{
$ands .= " AND " .$value. " ";
}
}
if (substr($ands,0,4) == ' AND') $ands = substr($ands,4);
$sql .= $ands;
}

if(count($this->orwhere) > 0){
if(!$isWhere) $sql .= " WHERE ";
$ors = '';
foreach ($this->orwhere as $key => $value){
if (!is_numeric($key)){
$ors .= " OR " .$key. " = '" .$value. "' ";
}
else{
$ors .= " OR " .$value. " ";
}
}
if (substr($ors,0,3) == ' OR' && !$isWhere) $ors = substr($ors,3);
$sql .= $ors;
}

if(count($this->order) > 0){
$sql .= " ORDER BY " . implode(',', $this->order);
}

if($this->limit){
$sql .= ' LIMIT '.$this->limit .' ';
}

return $sql;
}

public function querySql($sql){}

public function query($sql = '')
{
$this->connect();
$this->free();
if ($sql){
$this->result = $this->querySql($sql);
if(!$this->result) $this->result = false;
$this->sql = $sql;
return $this->result;
}
else{
$sql = $this->getQuerySql();
$this->result = $this->querySql($sql);
if(!$this->result) $this->result = false;
$this->clear();
$this->sql = $sql;
return $this->result;
}
}

public function querys($sql = '')
{
return $this->query($sql);
}

public function next_record($type = MYSQL_ASSOC) {}


public function lock($table, $mode = "write")
{
$query = "lock tables ";
if(is_array($table)) {
while(list($key,$value) = each($table)) {
if(is_int($key)) $key = $mode;
if(strpos($value, ",")) {
$query .= str_replace(",", " $key, ", $value) . " $key, ";
} else {
$query .= "$value $key, ";
}
}
$query = substr($query, 0, -2);
} elseif(strpos($table, ",")) {
$query .= str_replace(",", " $mode, ", $table) . " $mode";
} else {
$query .= "$table $mode";
}
if(!$this->query($query)) {
$this->halt("lock() failed.");
return false;
}
$this->locked = true;
return true;
}

public function unlock()
{
$this->locked = false;
if(!$this->query("unlock tables")) {
$this->halt("unlock() failed.");
return false;
}
return true;
}

public function f($name)
{
if (isset($this->record[$name])) {
return $this->record[$name];
}
else{
return false;
}
}

public function p($name)
{
if (isset($this->record[$name])) {
print $this->record[$name];
}
}

public function nextid($table)
{
$sql = "select " .$table. "_id from " .$table. " order by " .$table. "_id desc";
if(!$this->query($sql)) {
$this->halt('query failed in nextid: '.$sql);
return 0;
}

if(!$this->next_record()) {
return 1;
}
else {
return $this->f($table. "_id") + 1;
}
}

public function insert_id() {}


public function num_rows() {}

public function affected_rows() {}

public function num_fields() {}


public function update_row($table, $index, $fields)
{
$this->connect();
if(!is_array($fields))
{
$this->halt('Invalid update row called');
return false;
}
if(!is_array($index))
{
$index = array($index);
}

$field_types='';
$index_types='';
$count=0;
$indexes=array();

foreach($fields as $key => $value)
{
if(!in_array($key, $index))
{
if (!is_numeric($key)){
$updates[] = "`$key`='".$this->escape($value)."'";
}
else{
$updates[] = $value;
}

}
}
if(isset($updates))
{
$sql = "UPDATE `$table` SET ".implode(',',$updates)." WHERE ";

$indexes=array();
foreach($index as $subindex)
{
$indexes[]="`$subindex`='".$this->escape($fields[$subindex])."'";
}
$sql .= implode(' AND ', $indexes);
$this->query($sql);
return $this->affected_rows();
}

return false;
}

public function insert_row($table, $fields,$replace='')
{
$this->connect();
if(!is_array($fields))
{
$this->halt('Invalid insert row called');
return false;
}

foreach($fields as $key => $value)
{
$field_names[] = $key;
if(is_numeric($value)){
$field_values[] = $value;
}
else{
$field_values[] = "'".$value."'";
}
//$field_values[] = $this->escape($value);
}
if(isset($field_names))
{
$sql = $replace ? 'REPLACE' : 'INSERT';
$sql .= " INTO `$table` (`".implode('`,`', $field_names)."`) VALUES ";
$sql .= "(".implode(",", $field_values).")";
//$sql .= "('".implode("','", $field_values)."')";
//tip($sql);
$this->query($sql);
return $this->affected_rows();
}else
{
$this->halt('Error insering row');
}
return false;
}

public function delete_row($table, $where = array())
{
$this->connect();
if(!is_array($where)) $where=array($where);
if(count($where) > 0){
$where_ = '';
foreach ($where as $key => $value){
if (!is_numeric($key)){
$where_ .= " AND " .$key. " = '" .$value. "' ";
}
else{
$where_ .= " AND " .$value. " ";
}
}
if (substr($where_,0,4) == ' AND') $where_ = substr($where_,4);
$where_ = ' WHERE '. $where_;
}

$sql = "DELETE FROM " .$table;
$sql .= $where_;
$this->query($sql);
return $this->affected_rows();
}

public function replace_row($table, $fields)
{
return $this->insert_row($table, $fields, 'REPLACE');
}

public function escape($value, $trim=true){}


protected function set_log($level, $message)
{
$messages = str_split($message, 500);
for ($i = 0; $i < count($messages); $i ++) {
syslog($level, $messages[$i]);
}
}

protected function halt($msg)
{

if ($this->locked) {
$this->unlock();
}

$this->set_log(LOG_DEBUG, sprintf("Database Error: [ %s ]\n\nMySQL Error: [ %s (%s) ]",
$msg,
$this->errno,
$this->error));

tip(sprintf("Database Error: [ %s ]\n\nMySQL Error: [ %s (%s) ]",
$msg,
$this->errno,
$this->error));
}

public function close(){}


public function found_rows()
{
if(!isset($this->helper_db)){
$this->helper_db = new db();
}
$this->helper_db->query("SELECT FOUND_ROWS() as found;");
$this->helper_db->next_record();
return $this->helper_db->f('found');
}


public function table_exists($table_name)
{
if(!isset($this->tables)){
$this->tables=array();
$this->query('SHOW TABLES');
while($r=$this->next_record(MYSQL_NUM)){
$this->tables[]=$r[0];
}
}
return in_array($table_name, $this->tables);
}
}

if (class_exists('db_config')){
if(db_config::$db_type == 'mysqli' && function_exists('mysqli_close')){
ss_sys('mysqli');
}
else{
ss_sys('mysql');
}
}



autoload.fun.php

<?php

defined('SS') or exit('Access Declined.');

function ss_ctrl($controller)
{
if(!empty($controller)){
$file = CTRL . $controller . '.ctrl.php';
if (is_file($file)) {
require_once($file);
}
}
}

function ss_sys($class)
{

$file = SYS . $class . '.sys.php';
if (is_file($file)) {
require_once($file);
}
}

function ss_model($class)
{
$file = MODEL . $class . '.class.php';
if (is_file($file)) {
require_once($file);
}
}

function ss_fun($function)
{
$file = FUN . $function . '.fun.php';
if (is_file($file)) {
require_once($file);
}
}

function ss_inc($inc)
{
$file = INC . $inc . '.inc.php';
if (is_file($file)) {
require_once($file);
}
}

function ss_view($view)
{
$file = VIEW . $view . '.view.php';
if (is_file($file)) {
require_once($file);
}

}

function ss_conf($config)
{
$file = CONF . $config . '.conf.php';
if (is_file($file)) {
require_once($file);
}

}

function ss_cache($cache)
{
$file = CACHE . $cache . '.cache.php';
if (is_file($file)) {
require_once($file); }

}

function ss_swf($swf)
{
$file = SWF . $swf . '.php';
if (is_file($file)) {
require_once($file);
}
}

function ss_call_ctrl($ctrl= 'index',$action = 'index')
{
ss_sys('ctrlbase');
$action .= 'Action';
ss_ctrl($ctrl);
$ctrlClass = $ctrl.'Ctrl';
if (class_exists($ctrlClass)){
$reflectionClass=new ReflectionClass($ctrlClass);
call_user_func($ctrlClass.'::init');
if ($reflectionClass->hasMethod($action)){
$reflectionMethod=$reflectionClass->getMethod($action);
if($reflectionMethod->isStatic()){
call_user_func($ctrlClass.'::'.$action);
}
else{
call_user_func(array($ctrlClass,$action));
}
}

}

}

spl_autoload_register('ss_model');



cache.sys.php

<?php

defined('SS') or exit('Access Declined.');
define('CACHE_TIME',db_config::$db_cache);
define('CACHE_CLEAR_TIME',db_config::$db_cache_clear);
define('CACHE_CLEAR_SWITCH',db_config::$db_cache_switch);


class cache
{
private static $Singleton;
public static function GetInstance()
{
if(!(self::$Singleton instanceof self)){
self::$Singleton=new self();
}
return self::$Singleton;
}

public static function get_cache_name($name,$minute = false)
{
if (!$minute) {
$minute = CACHE_TIME;
}
$hour = date('Ymd-H');
$minute = ceil(intval(date('i')) / $minute) * $minute;
return CACHE. $name . '[' . $hour .$minute .'].cache.php';
}

public static function auto($name, $data,$minute = false)
{
$content = self::get($name,$minute);
if($content){
return $content;
}
else{
return self::set($name,$data,$minute);
}
}


public static function get($name,$minute = false)
{
if (CACHE_CLEAR_SWITCH == 'on'){
self::clear();
$cache = self::get_cache_name($name,$minute);
if (is_file($cache)){
return unserialize(file_get_contents($cache));
}
}
return false;
}


public static function set($name, $data,$minute = false)
{
if (CACHE_CLEAR_SWITCH == 'on'){
$cache = self::get_cache_name($name,$minute);
$file = @fopen($cache, 'w');
@fwrite($file,serialize($data));
@fclose($file);
}
return $data;
}

public static function clear()
{
$cache = CACHE. 'log.cache.php';
$time = time();
if(is_file($cache)){
if ( ($time - intval(file_get_contents($cache))) < CACHE_CLEAR_TIME* 60 ){
return true;
}
}

self::deldir(CACHE);
$file = @fopen($cache, 'w');
@fwrite($file, $time);
@fclose($file);
return false;
}

public static function del($name,$minute = false)
{
@unlink(self::get_cache_name($name,$minute));
}


public static function deldir($dir)
{
self::delfile($dir);
}

public static function delfile($dir)
{

if (is_dir($dir)) {
if ($dh = @opendir($dir)) {
while (($file = @readdir($dh)) !== false) {
if ($file!="." && $file!=".." && $file!="log.cache.php") {
$fullpath=$dir."/".$file;
if(!is_dir($fullpath)) {
@unlink($fullpath);

} else {
self::delfile($fullpath);
@rmdir($fullpath);
}
}
}
@closedir($dh);
}
}

}

private function __clone(){}
public function __construct(){}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值