<?php
/**************************************************
*  Created:  2010-06-08
*
*  文件说明
*
*  @Xweibo (C)1996-2099 SINA Inc.
*  @Author zhenquan <zhenquan@staff.sina.com.cn>
*
***************************************************/
include('action.abs.php');
class admin extends action {

		function admin() {
			parent :: action();
		}

		function default_action() {
			$this->login();
		}

		function index() {
			$url = WB_UPGRADE_CHK_URL . '?p=std&v=' . WB_VERSION;
			$json = file_get_contents($url);
			//$json = '{"ver":"1.2","img":"http:\/\/timg.sjs.sinajs.cn\/miniblog2style\/images\/common\/logo_714beta.png","text":"\u60a8\u5f53\u524d\u4f7f\u7528\u7684\u65b0\u6d6a\u5fae\u535a\u63d2\u4ef6\u6709\u91cd\u8981\u66f4\u65b0\uff0c\u66f4\u65b0\u81f31.3\u7248\u672c\uff1a<br><ul><li>\u65b0\u589e\u7ad9\u5185\u5fae\u535a\u529f\u80fd\u3002<\/li><li>\u4f18\u5316\u8d34\u5b50\u5206\u4eab\u5230\u5fae\u535a\u903b\u8f91\u3002<\/li><li>\u63d0\u4f9b\u66f4\u5f3a\u5927\u7684\u63d2\u4ef6\u5347\u7ea7\u8bbe\u7f6e\u3002<\/li><\/ul><br>\u5efa\u8bae\u60a8\u7acb\u5373\u4e0b\u8f7d\u5b89\u88c5\u6700\u65b0\u7248\u672c\u3002","downurl":"http:\/\/www.xtalk.cn\/"}';
			$json = json_decode($json);
			if ((string)$json->ver > (string)WB_VERSION) {
				TPL::assign('u_tips', $json);
			}
			$this->_display('index');
		}

		/**
		 * 得到用户授权
		 */
		function tokenCallback(){
			$wb	= APP::N('weibo');
			//设置之前request token
			$wb->setToken($_SESSION['admin_oauth_keys1']['oauth_token'], $_SESSION['admin_oauth_keys1']['oauth_token_secret']);
			$last_key = $wb->getAccessToken(V('r:oauth_verifier')) ;
			if ($last_key && !$last_key['error_code']) {
				$data = array('oauth_token'=>$last_key['oauth_token'],'oauth_token_secret' => $last_key['oauth_token_secret']);
				// 写入session
				$AdminUser = APP::N('adminUser', 'admin_id');

				$AdminUser->setInfo($data);
				// 记录入数据库
				$config = APP::N('sysConfig');
				$config->set($data);
				$config->save();

				header('Location:' . APP::mkModuleUrl('admin/admin.index', '', 'admin.php'));
				exit;
			} else {
			}
		}

		/**
		 * 用户登录
		 */
		function login() {
			session_start();
			if ($this->_isPost()) {
				$user = trim(V('p:username'));
				$passwd = trim(V('p:password'));
				$verify_code = strtolower(V('p:verify_code'));
				
				if (empty($user) || empty($passwd)) {
					exit('{"state":"401", "msg":"账号或密码错误"}');
					//$this->_error('错误的帐号密码');
				}
				$db = APP :: ADP('db');
				$db->setTable(T_ADMIN_USER);
				$sql = 'SELECT *FROM ' . $db->getTable(T_ADMIN_USER) . ' WHERE `name`="' . mysql_escape_string($user) . '"';
				$rs = $db->getRow($sql);
				if (!$rs || $rs['pwd'] != md5($passwd)) {
					exit('{"state":"401", "msg":"账号或密码错误"}');
					//$this->_error('错误的帐号密码');
				}
				
				$autocode = APP :: N('authCode');
				if (!$autocode->checkAuthcode($verify_code)) {
					exit('{"state":"402", "msg":"验证码错误"}');
					//$this->_error('错误的验证码');
				}

				if ($rs['state'] != '0') {
					exit('{"state":"403", "msg":"输入的帐号被禁用"}');
					//$this->_error('输入的帐号被禁用');
				}

				$AdminUser = APP::N('adminUser', 'admin_id');
				$session_data = array(
						'admin_id' => $rs['id'],
						'name' => $rs['name'],
						'real_name' => $rs['real_name'],
						'state' => $rs['state'],
						'add_time' => $rs['add_time']
						);
				$AdminUser->setInfo($session_data);
				exit('{"state":"200"}');
				//$this->_redirect('index');
			}
			$this->_display('login');
		}
		
		/**
		 * 退出登录
		 */
		function logout() {
			$AdminUser = APP::N('adminUser', 'admin_id');
			$session_data = array(
							'admin_id',
							'name',
							'real_name',
							'state',
							'add_time',
							'oauth_token',
							'oauth_token_secret',
							'accessToken'
							);
			$AdminUser->delInfo($session_data);
			APP :: redirect('admin/admin.login', 2);
		}

		/**
		 * 绘制验证码
		 */
		function authcode() {
			header("Cache-Control: no-cache, must-revalidate");
			header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
			$autocode = APP :: N('authCode');
			$autocode->setImage(array('type'=>'png','width'=>70,'height'=>25));
			$autocode->paint();
		}
}
