Example #1
0
	/**
	* 系统入口
	* 并且支持配置自动加载路径
	* @return void
	*/
	public static function start($appDirectory,$conf='config.php', $debug=false){
		//var_dump( $_SERVER);
		// 设定错误和异常处理
		//register_shutdown_function(array('YYK','fatalError'));
		self::$debug = $debug;
		set_error_handler(array('YYK','appError'), E_ALL);
		set_exception_handler(array('YYK','appException'));
		//取当前框架路径
		self::$YYK_path = pathinfo(__FILE__, PATHINFO_DIRNAME);
		//取当前项目路径
		self::$APP_path = realpath( $appDirectory);
		//self::$APP_path = pathinfo(realpath( $appDirectory), PATHINFO_DIRNAME);//pathinfo($_SERVER["SCRIPT_FILENAME"], PATHINFO_DIRNAME);

		//加载常用方法
		include self::$YYK_path. DIRECTORY_SEPARATOR . 'common.php';

		if (file_exists(self::$APP_path. DIRECTORY_SEPARATOR . 'common.php')) {
			//加载项目自定义方法
			include self::$APP_path. DIRECTORY_SEPARATOR . 'common.php';
		}
		//加载默认配置
		/*
		$tmp = array();
		if (file_exists(self::$YYK_path. DIRECTORY_SEPARATOR . 'config.php')) {
		$tmp = include self::$YYK_path. DIRECTORY_SEPARATOR . 'config.php';
		}
		*/

		//加载用户配置
		$conf = empty($conf) ? "config.php" : $conf;
		if (file_exists(self::$APP_path. DIRECTORY_SEPARATOR . $conf)){
			self::$config = include self::$APP_path. DIRECTORY_SEPARATOR . $conf;
		}

		//合并配置
		/*
		self::$config = array_merge($tmp, self::$config);
		unset($tmp);
		*/

		// 注册AUTOLOAD方法
		//加载用户控制器类
		spl_autoload_register(array('YYK', 'appCtrl'));

		//加载用户模型类
		spl_autoload_register(array('YYK', 'appModel'));

		//加载YYK核心类
		spl_autoload_register(array('YYK', 'YYKClass'));

		//加载框架扩展类

		//加载第三方类
		spl_autoload_register(array('YYK', 'otherClass'));

		//启动session
		session_start();

		//设置时区
		date_default_timezone_set(self::$config['timeZone']);

		//启用smart static server 智能选择静态服务器
		/**/
		if (isset(self::$config['cacheRedis']) && count(self::$config['cacheRedis'])>0 && isset(self::$config['s3']) && count(self::$config['s3'])>0){
			$mem = CacheRedis::create();
			$s3_key = 's3_' . ip2long(getIp());
			$country = $mem->get($s3_key);

			if (!$country) {
				if($ipInfo = getIpInfo(getIP())){
					$country = $ipInfo['country_id'];
				}
			}

			if (isset(self::$config['s3'][$country]) && is_array(self::$config['s3'][$country]) ) {
				self::$config['tplConst'] =array_merge( self::$config['tplConst'], self::$config['s3'][$country]);
			}

			$mem->set($s3_key, $country);
			$mem->expire($s3_key, 600);
		}
		
		//路由分析
		$requestRoute = self::parseRoute();
		self::$ctrlName = $requestRoute[0];
		self::$methodName = $requestRoute[1];

		//启用语言包
		if (isset(self::$config['useMultiLang']) && self::$config['useMultiLang']==true ) {
			if (isset($_GET['l'])) {
				setLang($_GET['l']);
				self::$defaultLang = $_GET['l'];
			}
			else{
				setLang(getLang());
				self::$defaultLang = getLang();
			}
		}


		//检查是否开启了模板静态缓存
		$staticCacheRoute = strstr($_SERVER['REQUEST_URI'], self::$ctrlName . DIRECTORY_SEPARATOR . self::$methodName);
		$staticCacheType = false;

		if (isset( self::$config['staticCache'][$staticCacheRoute] )) {
			$staticCacheType = $staticCacheType = isset(self::$config['staticCacheType']) ? self::$config['staticCacheType'] : 'FS';

			$staticCacheKey = 'staticCache' . str_replace(DIRECTORY_SEPARATOR, '_', self::$APP_path) . str_replace(DIRECTORY_SEPARATOR, '_', $staticCacheRoute);
			
			header('Server: Microsoft-IIS/8.0');
			header('X-Powered-By: ASP.NET');
			$content ='';
			switch(strtolower($staticCacheType)) {
				case 'redis':
					$redis = CacheRedis::create();
					$cacheTtl = $redis->ttl($staticCacheKey);
					$cacheTtl = $cacheTtl>0 ? $cacheTtl : 0;
					$cacheModifyTime   = time() -( self::$config['staticCache'][$staticCacheRoute] - $cacheTtl);
					$requestModifyTime= isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])-3600*8 : 0 ;
					$cacheExpire = $cacheModifyTime + self::$config['staticCache'][$staticCacheRoute];
					if ($cacheExpire > time()) {
						if ($cacheModifyTime <= $requestModifyTime ) {
							header("HTTP/1.1 304 Not Modified");
							header("Date: " . date('D, d M Y H:i:s', time()) . ' GMT');
							header("Last-Modified: " . date('D, d M Y H:i:s', $cacheModifyTime) . ' GMT');
							header("Cache-Control: max-age=" . $cacheTtl);
							header("Expires: ".date('D, d M Y H:i:s', $cacheExpire) . ' GMT');
							exit(0);
						}
						$content = $redis->get($staticCacheKey);
					}
					break;

				case 'memcache':
					$memcache = CacheMem::create();
					$content =  $memcache->get($staticCacheKey);
					$cacheModifyTime = substr($content, 0, strpos($content, ','));
					$cacheTtl = self::$config['staticCache'][$staticCacheRoute] - ( time()-$cacheModifyTime );
					$cacheTtl = $cacheTtl>0 ? $cacheTtl : 0;
					$requestModifyTime= isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])-3600*8 : 0 ;
					$cacheExpire = $cacheModifyTime + self::$config['staticCache'][$staticCacheRoute];
					if ($cacheExpire > time()) {
						if ($cacheModifyTime <= $requestModifyTime ) {
							header("HTTP/1.1 304 Not Modified");
							header("Date: " . date('D, d M Y H:i:s', time()) . ' GMT');
							header("Last-Modified: " . date('D, d M Y H:i:s', $cacheModifyTime) . ' GMT');
							header("Cache-Control: max-age=" . $cacheTtl);
							header("Expires: ".date('D, d M Y H:i:s', $cacheExpire) . ' GMT');
							exit(0);
						}
						$content = substr($content, strpos($content, ',')+1);
					}
					break;

				default:	//默认文件系统缓存
					$tplCache = self::$APP_path . DIRECTORY_SEPARATOR . 'html' . DIRECTORY_SEPARATOR . self::$ctrlName . DIRECTORY_SEPARATOR . self::$methodName . '.html';
					if (!file_exists($tplCache)) {
						break;
					}
					$cacheModifyTime = filemtime($tplCache);
					$cacheTtl = self::$config['staticCache'][$staticCacheRoute] - ( time()-$cacheModifyTime );
					$cacheTtl = $cacheTtl>0 ? $cacheTtl : 0;
					$requestModifyTime= isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])-3600*8 : 0 ;
					$cacheExpire = $cacheModifyTime + self::$config['staticCache'][$staticCacheRoute];
					//echo $cacheModifyTime . "<br>";
					//echo $requestModifyTime . "<br>";
					//echo time();
					if ($cacheExpire > time()) {
						if ($cacheModifyTime <= $requestModifyTime ) {
							header("HTTP/1.1 304 Not Modified");
							header("Date: " . date('D, d M Y H:i:s', time()) . ' GMT');
							header("Last-Modified: " . date('D, d M Y H:i:s', $cacheModifyTime) . ' GMT');
							header("Cache-Control: max-age=" . $cacheTtl);
							header("Expires: ".date('D, d M Y H:i:s', $cacheExpire) . ' GMT');
							exit(0);
						}
						else{
							$content = file_get_contents($tplCache);
						}
					}

					break;
			}

			if (strlen($content)>0) {
				header("Date: " . date('D, d M Y H:i:s', time()) . ' GMT');
				header("Last-Modified: " . date('D, d M Y H:i:s', $cacheModifyTime) . ' GMT');
				header("Cache-Control: max-age=" . $cacheTtl);
				header("Expires: ".date('D, d M Y H:i:s', $cacheExpire) . ' GMT');
				echo $content;exit(0);
			}
		}
		self::$ctrlName = $requestRoute[0] . 'Ctrl';

		//检查路由是否存在
		if (class_exists(self::$ctrlName)){
			$C = new self::$ctrlName();
			if (method_exists($C, self::$methodName)){
				self::$ctrlName = $requestRoute[0];
				call_user_func(array($C, self::$methodName));
			}
			else{
				//E( 'None method:'.self::$methodName);
				_404();
			}
		}
		else{
			//控制器不存在
			//E( 'None Controller:' . self::$ctrlName);
			_404();
		}
	}
Example #2
0
 public function getHandleProviderCallback($driver, Request $request)
 {
     if ($request->input('error')) {
         return redirect()->route('auth.signup');
     }
     $user = Socialite::driver($driver)->user();
     $id = $user->getId();
     $nick_name = $user->getNickname();
     $first_name = $user->getName();
     $last_name = null;
     $email = $user->getEmail();
     $avatar = $user->getAvatar();
     if (!$email) {
         notify()->flash('Sorry ' . $first_name . '!', 'error', ['text' => 'Your ' . $driver . ' account have not email. Please login your social network with email address.']);
         return redirect()->route('auth.signup');
     }
     $url = null;
     if ($driver == "linkedin") {
         if (isset($user->user['publicProfileUrl'])) {
             $url = $user->user['publicProfileUrl'];
         }
         if (isset($user->user['firstName'])) {
             $first_name = $user->user['firstName'];
         }
         if (isset($user->user['lastName'])) {
             $last_name = $user->user['lastName'];
         }
     }
     if ($driver == "google") {
         if (isset($user->user['url'])) {
             $url = $user->user['url'];
         }
         if (isset($user->user['name']['givenName'])) {
             $first_name = $user->user['name']['givenName'];
         }
         if (isset($user->user['name']['familyName'])) {
             $last_name = $user->user['name']['familyName'];
         }
     }
     if ($driver == "facebook") {
         $url = "http://www.facebook.com/" . $user->getId();
         if (isset($user->user['first_name'])) {
             $first_name = $user->user['first_name'];
         }
         if (isset($user->user['last_name'])) {
             $last_name = $user->user['last_name'];
         }
     }
     $user = User::where('email', $email)->first();
     if (!$user) {
         //register new account
         $random_password = str_random(6);
         $ipInfo = getIpInfo($request->getClientIp());
         $arrayUser = ['email' => $email, 'username' => "u-" . uniqid(), 'first_name' => $first_name, 'last_name' => $last_name, 'password' => bcrypt($random_password), 'active' => true, $driver . '_id' => $id, $driver . '_url' => $url, 'ban' => false];
         if ($ipInfo) {
             if (isset($ipInfo['city'])) {
                 $arrayUser['city'] = $ipInfo['city'];
             }
             if (isset($ipInfo['city'])) {
                 $arrayUser['location'] = $ipInfo['city'];
             }
             if (isset($ipInfo['country'])) {
                 $arrayUser['country'] = $ipInfo['country'];
             }
             if (isset($ipInfo['countryCode'])) {
                 $arrayUser['country_code'] = $ipInfo['countryCode'];
             }
             if (isset($ipInfo['lat'])) {
                 $arrayUser['lat'] = $ipInfo['lat'];
             }
             if (isset($ipInfo['lon'])) {
                 $arrayUser['lon'] = $ipInfo['lon'];
             }
             if (isset($ipInfo['region'])) {
                 $arrayUser['region'] = $ipInfo['region'];
             }
             if (isset($ipInfo['regionName'])) {
                 $arrayUser['regionName'] = $ipInfo['regionName'];
             }
             if (isset($ipInfo['timezone'])) {
                 $arrayUser['timezone'] = $ipInfo['timezone'];
             }
         } else {
             $arrayUser['timezone'] = "UTC";
         }
         $user = User::create($arrayUser);
         $user->permissions()->create(['is_admin' => false, 'is_superadmin' => false]);
         Mail::send('emails.auth.signup', ['user' => $user, 'password' => $random_password], function ($message) use($user) {
             $message->subject('Thanks for registering.')->to($user->email);
         });
         notify()->flash('Welcome', 'success', ['text' => 'You are now signed in.']);
     }
     Auth::login($user, true);
     if (!Auth::user()->getSocialId($driver)) {
         Auth::user()->update([$driver . '_id' => $id, $driver . '_url' => $url]);
     }
     if (!Auth::user()->active) {
         Auth::user()->update(['active' => true, 'active_hash' => null, 'active_at' => Carbon::now()->toDateTimeString()]);
     }
     return redirect()->route('home');
 }