<?php
	/**
	 * 用于本系统的重构CONTROLLER基类
	 * @desc 添加权限验证功能
	 * @author [Alone] alonedistian@gmail.com
	 * 
	 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	 * 关于权限验证方式的说明:
	 * 		根据用户当前访问的URI验证用户是否具有指定权限
	 * 		Example:/role/mod/1:首先验证用户是否具有[role]权限,
	 * 		如果不具有该权限则进一步验证用户是否具有[role_mod]权限,
	 * 		如果用户也不具有该权限则判断用户不能进行接下来的操作。
	 */
	class CTRL_CONTROLLER extends CORE_CONTROLLER {
		protected $a_imgTypes;
		
		function __before()
		{
			parent::__before();
			//echo "Controller.php ".SITE_URL;
			//echo $_REQUEST['ctrl'];
			//die();
			//if( $_REQUEST['ctrl'] != 'interface' && ( $_REQUEST['ctrl'] != 'admin' && $_REQUEST['act'] != 'login' ) ){
			if( $_REQUEST['ctrl'] != 'index' || $_REQUEST['act'] != 'login' ){
				$_obj_admin			= new MODEL_ADMIN();
				//check login
				//echo "no interface admin login";
				if( !$_obj_admin->checkLogin() ){
					echo "<script type='text/javascript'>window.top.location='".SITE_URL."/admin/login?src={$_SERVER['REQUEST_URI']}';</script>";
					exit;
				}
				//}else{
				    //check power
				    //var_dump($_REQUEST['ctrl']);
					//if( $_REQUEST['ctrl'] != 'index' ){
					/*/if( $_REQUEST['ctrl'] != 'index' ){
						$_obj_admin		= new MODEL_ADMIN();
						//var_dump($_obj_admin);
						
						//get_class_methods($_obj_admin);
						//var_dump(get_class_methods($_obj_admin));						
						//var_dump($_obj_admin->checkPower( $_REQUEST['ctrl'] ));
						
						if( !$_obj_admin->checkPower( $_REQUEST['ctrl'] ) && !$_obj_admin->checkPower( "{$_REQUEST['ctrl']}_{$_REQUEST['act']}" ) )
							$this->showMessage( false, 'Not enought Power!' );
					}*/
				//}
			}
			
			$this->a_imgTypes['image/pjpeg']	= 'jpg';
			$this->a_imgTypes['image/jpeg']	= 'jpg';
			$this->a_imgTypes['image/gif']	= 'gif';
			$this->a_imgTypes['image/png']	= 'png';
			$this->a_imgTypes['image/bmp']	= 'bmp';
			
			$this->assign( 'website_domain', C('WEBSITE_DOMAIN'));
			
		}//function
		
		public function postFile( $s_type, $i_width = null, $i_height = null, $s_aimName = null ){
			$_SERVER['HTTP_X_REQUESTED_WITH']	= 'xmlhttprequest';
			$_o_toolSenndFile					= new TOOL_SENDFILE( CORE_CONFIG::get( 'IMGSERVER_UPD_URL' ) );
			$_s_inputName						= $_REQUEST['key'];
			
			if( empty( $_s_inputName ) )
				$this->showMessage( false, 'Please select a file!' );
			if( empty( $_FILES[$_s_inputName] ) )
				$this->showMessage( false, 'Please select a file!' );
			$_a_fileInfo						= $_FILES[$_s_inputName];
			$_v_result							= $_o_toolSenndFile->sendFile( $_a_fileInfo['tmp_name'], 'file', array( 'file_name' => $s_aimName, 'from' => $s_type, 'type' => $_a_fileInfo['type'] ) );
			
			if( empty( $_v_result ) )
				$this->showMessage( false, "File upload faild!" );
			$_a_result							= unserialize( $_v_result );
			if( empty( $_a_result ) || empty( $_a_result['result'] ) )
				$this->showMessage( false, empty( $_a_result['msg'] ) ? 'File upload faild!' : $_v_result );

			$_s_filePath						= $_a_result['msg'];
			$_s_dirName							= dirname( $_s_filePath );
			$_s_baseName						= basename( $_s_filePath );
			switch ( $s_type ){
				case 'firmware' :
					break;
				default :
				/**
				 * 修改图片大小
				 * @desc 通过文件名,使图片服务器自动生成指定尺寸的图片
				 */
				$_a_baseName						= explode( '.', $_s_baseName );
				$_a_baseName[0]						.= "_{$i_width}_{$i_height}";
				$_s_baseName						= implode( '.', $_a_baseName );
			}
			$_a_result							= array();
			$_a_result['url']					= str_replace( '\\', '/', $_s_dirName. DS . $_s_baseName );
			$_a_result['size']					= $_FILES[$_s_inputName]['size'];
			$this->showMessage( true, $_a_result );
			exit;
			
		}
		
		/**
		 * 图片上传功能封装
		 * @desc 改功能提供AJAX使用,用于根据条件将图片上传到指定位置并使用JSON方式返回上传后到文件名
		 */
		public function uploadImg( $s_aimPath = '', $i_width = null, $i_height = null, $s_synPath = null ){
			$_SERVER['HTTP_X_REQUESTED_WITH']	= 'xmlhttprequest';
			
			if( !$s_aimPath )
				$s_aimPath			= APP_PATH . CHANNEL_PIC_PATH;

			if( !file_exists( $s_aimPath ) ){
				$this->showMessage( false, "System image save path[{$s_aimPath}] dose not exists!", array(), true );
			}else if( !is_writable( $s_aimPath ) ){
				$this->showMessage( false, 'Not enought System power to upload images!', array(), true );
			}
			
			$_s_inputfileName		= $_REQUEST['key'];
			if( empty( $_s_inputfileName ) )
				$this->showMessage( false, 'Please select a file!', array(), true );
			
			if( !empty( $_FILES[$_s_inputfileName] ) ){
				$_arr_picInfo				= $_FILES[$_s_inputfileName];
				if( isset( $this->a_imgTypes[$_arr_picInfo['type']] ) ){
					$_str_aimName			= md5( $_arr_picInfo['tmp_name'] ) . ".{$this->a_imgTypes[$_arr_picInfo['type']]}";

					if( !is_null( $i_width ) || !is_null( $i_height ) ){
						$_void_result		= TOOL_IMAGE::imgResize( $_arr_picInfo['tmp_name'], $i_width, $i_height );
						if( !$_void_result )
							$this->showMessage( false, 'File resize faild!', array(), true );
					}
					copy( $_arr_picInfo['tmp_name'], $s_aimPath. DS . $_str_aimName );
					$_arr_picInfo['defpic']	= $_str_aimName;
				}else
					$this->showMessage( false, 'File type undefined!', array(), true );
			}
			
			$_a_result						= array();
			$_a_result['url']				= $_str_aimName;
			$_a_result['size']				= $_FILES[$_s_inputfileName]['size'];
			if( $s_synPath ){
				copy( $s_aimPath. DS . $_str_aimName, $s_synPath . $_str_aimName );
			}
			$this->showMessage( true, $_a_result, array(), true );
		}//function
		
		public function createSearchSelectForm( $arr_paras, $str_key, $str_value, $bool_withAll = true ){
			$_str_requestValue					= empty( $_REQUEST[$str_key] ) ? "" : $_REQUEST[$str_key];
			if( $bool_withAll )
				$_str_result					= "<option value=''>ALL</option>";
			else
				$_str_result					= "";
			if( !empty( $arr_paras ) ){
				foreach( $arr_paras as $_arr_value ){
					$_str_result				.= "<option value='{$_arr_value[$str_key]}' " . 
													( ( $_str_requestValue && ( $_arr_value[$str_key] == $_str_requestValue ) ) ? "selected=true" : '' ) .
													">{$_arr_value[$str_value]}</option>";
				}
			}
			return "<select name='{$str_key}'>{$_str_result}</select>";
		}
		
		public function createEnum( $a_paras, $s_name, $s_value ){
			$_a_tmp				= array();
			if( !empty( $a_paras ) ){
				foreach( $a_paras as $_a_data )
					$_a_tmp[]	= array( 'name' => $_a_data[$s_name], 'value' => $_a_data[$s_value] );
				return $_a_tmp;
			}
			return array();
		}
		
		/**
		 * 获取语言设置
		 */
		public function getLanguages(){
			$_obj_language			= new MODEL_LANGUAGE();
			return $_obj_language->getLanguages();
		}
		
		/**
		 * 将语言设置信息格式化为CHECKBOX
		 * @param $arr_languageSet
		 */
		public function createLanguageCheckBox( $arr_languageSet ){
			if( empty( $arr_languageSet ) )
				return array();
			$_arr_result					= array();
			foreach( $arr_languageSet as $_arr_languageInfo ){
				$_arr_languageInfo['html']	= "<input type='checkbox' name='language_id[]' title='{$_arr_languageInfo['language_name']}' value='{$_arr_languageInfo['language_id']}'" . ( !empty( $_arr_languageInfo['checked'] ) ? 'checked=true' : '' ) . "/>{$_arr_languageInfo['language_key']}"; 
				$_arr_result[]				= $_arr_languageInfo;
			}				
			return $_arr_result;
		}
	
		
		public function showMessage( $b_result, $s_message, $a_href = array(), $b_stop = true ){
			$_a_hrefs				= array();
			if( !empty( $a_href ) ){
				foreach( $a_href as $_s_name => $_s_value ){
					$_a_hrefs[]		= array( 'name' => $_s_name, 'link' => $_s_value );
				}
			}
			$_a_result	= array(
									 'result'	=> $b_result,
									 'msg'		=> $s_message,
									 'hrefs'	=> $_a_hrefs,
									);
			
			if( isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && strtolower( $_SERVER['HTTP_X_REQUESTED_WITH'] ) == 'xmlhttprequest' )
				echo json_encode( $_a_result );
			else{
				$this->assign( 'result', $_a_result );
				$this->display( 'common/msg.tpl' );
			}//if
			if( $b_stop )
				exit;
		}//function
	}
	/**
	 * Finish
	 * o._.o
	 */
?>

  

相关文章: