first commit

This commit is contained in:
2014-03-26 14:18:20 +08:00
commit 41681f1f18
31 changed files with 4496 additions and 0 deletions

83
lib/BaseController.class.php Executable file
View File

@ -0,0 +1,83 @@
<?php
class BaseController{
public $tplParams = array();
public $layout = NULL;
public $layoutTpl = true;
function __construct(){
//if token is expires clear it
if( $_SESSION['expiresTime'] && $_SESSION['expiresTime'] <= time() ){
$_SESSION = array();
}
if(isset($_GET['tokenString'])){
$tokenString=explode(".", $_GET['tokenString']);
$token= json_decode(base64_decode($tokenString[1]) , true );
}
if($token['oauth_token']){
Weibo::init( c('akey') , c('skey') ,$tokenInfo['oauth_token'] );
Weibo::setIp('202.110.0.5');
$res = Weibo::show_user_by_id( $tokenInfo['user_id'] );
if( $res && !isset( $res['error_code'] ) ){
foreach ($res as $key => $value) {
$_SESSION[$key] = $value;
}
$_SESSION['__SINA']['TOKEN'] = $tokenInfo['oauth_token'];
$_SESSION['expiresTime'] = $tokenInfo['expires'] + time();
}
}else{
Weibo::init( c('akey') , c('skey') ,$_SESSION['__SINA']['TOKEN'] );
Weibo::setIp('202.110.0.5');
}
//init weibo Class
}
protected function _add( $key , $value = NULL ){
if( $value == NULL && is_array($key) ){
$this->tplParams += $key;
}else{
$this->tplParams[$key] = $value;
}
}
protected function showMessage( $message , $type = 'notice' , $show = true ){
$message = '<message type="'.$type.'">'.$message.'</message>';
if( $show ){
echo $message;
}else{
return $message;
}
}
protected function render( $tplName = NULL , $data = array() ){
if( $data && is_array( $data ) ){
$data = array_merge( $data , $this->tplParams );
}else{
$data = $this->tplParams;
}
if( !$tplName ){
$tpl = $tplName?$tplName:get_class($this);
if( Url::getMethod() != c('default_method') ){
$tpl .= '_'.Url::getMethod();
}
}else{
$tpl = $tplName;
}
render( $data , $tpl , $this->layout , $this->layoutTpl );
}
protected function getAjax( $method , $extra = array() ){
if( !method_exists( $this , $method ) ) die('Ajax method is not exists' );
//save info
$url = Url::getUrl();
$extra['method'] = $method;
$GLOBALS['__isAjax'] = true;
Url::setUrl( Url::make($extra) );
ob_start();
$this->$method();
$html = ob_get_clean();
//replace back
unset($GLOBALS['__isAjax']);
Url::setUrl( $url );
return $html;
}
}

99
lib/app.function.php Executable file
View File

@ -0,0 +1,99 @@
<?php
function checkLogin(){
if( !isLogin() ){
$back = $_SERVER['REQUEST_URI']?:'/';
if( v('ajax') && isset( $_SERVER['HTTP_REFERER'])&&$_SERVER['HTTP_REFERER'] ){
$back = $_SERVER['HTTP_REFERER'];
}
$state = NULL;
if( IFCookie::isNeedFix() ){
$sid = session_id();
$state = $sid;
$back .= '&'.IFCookie::SESSIONSTR.'='.$sid;
}
$_SESSION['backUrl'] = $back;
$aurl = Weibo::getAuthorizeURL( c('callback') , 'code' , $state );
header("Location: ".$aurl);
exit;
}
}
function isLogin(){
if( isset( $_GET['viewer'] ) ){
if( $_GET['viewer'] == '' || $_GET['viewer'] != $_SESSION['id'] ){
$_SESSION = array();
}
}
if( $_SESSION['id'] ){
return true;
}else{
return false;
}
}
function getPager( $page , $page_all , $old = array() ,$custom = array() ){
if( $page_all <= 1 ){
return '';
}
if( $custom && is_array($custom) ){
$custom = '&'.http_build_query($custom);
}elseif( $custom ){
$custom = '&'.$custom;
}else{
$custom = NULL;
}
$pre = $next = $middle = '';
$left_num = $right_num = 0;
$left_more = $right_more = false;
if( $page > 1 ){
$pre = '<li class="pref" ><a href="'.Url::make( 'page='.($page - 1).$custom , $old ).'" title="上一页">上一页</a></li>';
}
if( $page < $page_all ){
$next = '<li class="next" ><a href="'.Url::make( 'page='.($page + 1).$custom , $old ). '" title="下一页">下一页</a></li>';
}
if( $page_all <= 11 ){
/* 小于等于 11 页 */
$left_num = $page - 1;
$right_num = $page_all - $page;
}else{
if( $page > 6 ){
$left_more = true;
if( $page < $page_all - 5 ){
$right_more = true;
$left_num = 4;
$right_num = 4;
}else{
$right_more = false;
$right_num = $page_all - $page;
$left_num = 9 - $right_num;
}
}else{
$left_num = $page - 1;
$left_more = false;
$right_num = 9 - $left_num;
$right_more = true;
}
}
if( $left_more ){
$middle .= '<li><a href="'.Url::make( 'page=1'.$custom , $old ).'">1</a></li>';
$middle .= '<li><a>...</a></li>';
}
for($j=$left_num;$j>0;$j--){
$i = $page - $j;
if( $i <= 0 ){
continue;
}
$middle .= '<li><a href="'.Url::make( 'page='.$i.$custom , $old ). '">' . $i .'</a></li>';
}
$middle .= '<li class="cur"><a>' . $page . '</a></li>';
for($j=1;$j<=$right_num;$j++){
$i = $page + $j;
if( $i > $page_all ){
continue;
}
$middle .= '<li><a href="'.Url::make( 'page='.$i.$custom , $old ).'">' . $i .'</a></li>';
}
if( $right_more ){
$middle .= '<li><a>...</a></li>';
$middle .= '<li><a href="'.Url::make( 'page='.$page_all.$custom , $old ).'">' . $page_all .'</a></li>';
}
return $pre.$middle.$next;
}