/** * 获取客户端IP地址, * 如果开发者的开发环境ip和服务器在同一个内网内,则获取的是局域网内的ip地址 * @access public * @author songdengtao <http://www.songdengtao.cn/> * @return string */ public static function getClientIp() { if (false === \Pandaphp::get('isCli')) { $client = ''; if (isset($_SERVER['HTTP_CLIENT_IP'])) { $client = $_SERVER['HTTP_CLIENT_IP']; } $forward = ''; if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $forward = $_SERVER['HTTP_X_FORWARDED_FOR']; } $remote = $_SERVER['REMOTE_ADDR']; if (filter_var($client, FILTER_VALIDATE_IP)) { $ip = $client; } elseif (filter_var($forward, FILTER_VALIDATE_IP)) { $ip = $forward; } else { $ip = $remote; } return $ip; } else { if (isset($_SERVER['SSH_CLIENT'])) { return strstr($_SERVER['SSH_CLIENT'], ' ', true); } else { return ''; } } }
public function display($template = '') { if (method_exists($this, '_beforeDisplay')) { // 执行显示模板之前需要的操作 $this->_beforeDisplay(); } $appPath = \Pandaphp::get('appPath'); if (empty($template) || 0 !== strpos($template, $appPath)) { $viewDirname = \Pandaphp::shell('Config::get', 'dirname_view'); $templateFilePath = $appPath . MODULE_NAME . '/' . $viewDirname . '/'; $templateTheme = \Pandaphp::shell('Config::get', 'template_theme'); if ($templateTheme) { $templateFilePath .= $templateTheme . '/'; } $templateFilePath .= lcfirst(CONTROLLER_NAME) . '-' . ACTION_NAME; $templateFilePath .= \Pandaphp::shell('Config::get', 'template_suffix'); } else { $templateFilePath = $template; } if (\Pandaphp::shell('File::isExist', $templateFilePath)) { View::display($templateFilePath); } else { \Pandaphp::shell('Error::halt', $templateFilePath . ' 模板不存在!'); } }
/** * 错误输出 * * debug状态下,谁都可以看到错误信息 * developer状态下,只有开发者才能开间错误信息,开发者[指定的ip和帐号] * 其他的不能看到错误信息 * * @access public * @param mixed $errdata 错误信息 * @param string $errpage 错误页面URL * @author songdengtao <http://www.songdengtao.cn> * * @return void */ public static function halt($errdata, $errpage = '') { $e = []; $ip = ''; $developerIps = []; $isDebug = \Pandaphp::get('debug'); $isCli = \Pandaphp::get('isCli'); $isOpen = true; if (false === $isCli) { $ip = \Pandaphp::shell('Ip::getClientIp'); $developerIps = \Pandaphp::shell('Config::get', 'developer_ips'); if (!in_array($ip, $developerIps)) { $isOpen = false; } } if ($isOpen || $isDebug || $isCli) { if (!is_array($errdata)) { $trace = debug_backtrace(); $e['message'] = '<b style="color:#FF9900;">[ pandaphp_error ] :</b><br> ' . $errdata; $e['file'] = $trace[1]['file']; $e['line'] = $trace[1]['line']; ob_start(); debug_print_backtrace(); $e['trace'] = ob_get_clean(); } else { $e = $errdata; } if ($isCli) { $errstr = $e['message']; if (\Pandaphp::get('isWin')) { $errstr = iconv('UTF-8', 'gbk', $e['message']); } $errstr .= PHP_EOL; $errstr .= 'FILE:' . $e['file'] . PHP_EOL; $errstr .= 'LILE:' . $e['line'] . PHP_EOL; $errstr .= $e['trace']; die($errstr); } } else { if (in_array($ip, $developerIps)) { $message = $errdata['message']; } else { $message = is_array($errdata) ? $errdata['message'] : $errdata; $errorMsg = \Pandaphp::shell('Config::get', 'error_message'); $message = $isDebug ? $message : $errorMsg; } $e['message'] = $message; } if (empty($errpage)) { $errpage = \Pandaphp::shell('Config::get', 'error_page'); $errpage = \Pandaphp::get('webroot') . $errpage; } if (\Pandaphp::shell('File::isExist', $errpage, true)) { include $errpage; exit; } else { static::defaultErrorPrint($e, $isDebug); } die($e); }
private static function _isRuntimesExistAndWritable() { $runtimesPath = \Pandaphp::get('runtimePath'); $isExist = \Pandaphp::shell('Dir::isExist', $runtimesPath); $isWriteable = \Pandaphp::shell('Dir::isWritable', $runtimesPath); return $isExist && $isWriteable; }
public function __construct() { parent::__construct(); $ds = '/'; $module = MODULE_NAME; $leftDelimiter = \Pandaphp::shell('Config::get', 'template_smarty_left_delimiter'); $this->left_delimiter = $leftDelimiter; $rightDelimiter = \Pandaphp::shell('Config::get', 'template_smarty_right_delimiter'); $this->right_delimiter = $rightDelimiter; // 设置模板文件目录 $viewDirname = \Pandaphp::shell('Config::get', 'dirname_view'); $theme = \Pandaphp::shell('Config::get', 'template_theme'); $theme = $theme ? $theme . $ds : ''; $templateDir = \Pandaphp::get('appPath') . $module . $ds . $viewDirname . $ds . $theme; $this->setTemplateDir($templateDir); // 设置编译后的模板文件目录] $runtimeDir = \Pandaphp::get('runtimePath'); $compileDir = $runtimeDir . $module . $ds . 'template_complie' . $ds; $this->setCompileDir($compileDir); // 设置缓存的模板文件目录 $cacheDir = $runtimeDir . $module . $ds . 'template_cache' . $ds; $this->setCacheDir($cacheDir); $isTemplateCache = \Pandaphp::shell('Config::get', 'template_cache_on'); $this->setCaching($isTemplateCache); $templateCacheLifetime = \Pandaphp::shell('Config::get', 'template_cache_lifetime'); $this->setCacheLifetime($templateCacheLifetime); }
private static function _getRouterInstance() { if (is_null(static::$_routerInstance)) { $class = 'pandaphp\\router\\'; $class .= \Pandaphp::get('isCli') ? 'CliRouter' : 'CgiRouter'; static::$_routerInstance = new $class(); } return static::$_routerInstance; }
/** * 解析请求的URI,获得PANDAURI * @access public * @author songdengtao <http://www.songdengtao.cn/> * @return string */ public function getPandaUri() { $queryVar = \Pandaphp::get('httpQueryStr'); $inputData = \Pandaphp::shell('Input::get'); $queryStr = ''; if (isset($inputData[$queryVar])) { $mdlCtrlActStr = $inputData[$queryVar]; if (\Pandaphp::shell('Str::isEnd', $mdlCtrlActStr, $this->urlHtmlSuffix)) { $queryStr .= str_replace($this->urlHtmlSuffix, '', $mdlCtrlActStr); } else { $queryStr .= $mdlCtrlActStr; } $queryStr = str_replace('/index.php', '', $queryStr) . '/'; } $subDomain = \Pandaphp::shell('Http::getSubDomain'); $isDomainDeploy = $this->urlDomainDeployOn && (isset($this->urlDomainDeployMapping[$subDomain]) || empty($subDomain)); if ($isDomainDeploy) { if (empty($subDomain)) { $subDomain = 'www'; } $runtimeModule = $this->urlDomainDeployMapping[$subDomain]; } else { if (isset($inputData[$queryVar])) { $runtimeModule = substr($queryStr, 1, strpos(trim($queryStr, '/'), '/')); } else { $runtimeModule = $this->defaultModule; } } \Pandaphp::set('runtimeModule', $runtimeModule); $this->defaultController = \Pandaphp::shell('Config::get', 'default_controller'); $this->defaultAction = \Pandaphp::shell('Config::get', 'default_action'); if (isset($inputData[$queryVar])) { $queryStr = '/' . $runtimeModule . $queryStr; unset($inputData[$queryVar]); } else { $queryStr .= '/' . $runtimeModule . '/' . $this->defaultController . '/' . $this->defaultAction . '/'; } foreach ($inputData as $key => $val) { $queryStr .= $key . '/' . $val . '/'; } return $queryStr; }
/** * 上传单张文件 * @access public * @param array $imageFile $_FILES[filename] * @author songdengtao <http://www.songdengtao.cn> * @return array */ public function uploadOne(array $imageFile = []) { if (empty($imageFile)) { return ['stat' => 4, 'msg' => '请选择文件']; } $checkFileErrorRes = $this->_CheckFileError($imageFile['error']); if ($checkFileErrorRes['stat'] !== 0) { return $checkFileErrorRes; } //原文件名 $fileName = strip_tags($imageFile['name']); //服务器上临时文件名 $tempName = $imageFile['tmp_name']; //文件大小 $fileSize = $imageFile['size']; //检查文件名 if (!$fileName) { return ['stat' => 4, 'msg' => '请选择文件']; } // 检查上传的目录是否存在 if (!@is_dir($this->savePath)) { return ['stat' => 9, 'msg' => '上传目录不存在']; } //检查目录写权限 if (!@is_writable($this->savePath)) { return ['stat' => 10, 'msg' => '上传目录没有写权限']; } //检查是否已上传 if (!@is_uploaded_file($tempName)) { return ['stat' => 8, 'msg' => '文件上传失败']; } //检查文件大小 if ($this->maxSize !== 0 && $fileSize > $this->maxSize) { return ['stat' => 11, 'msg' => '上传文件大小超过系统限制']; } //获得文件扩展名 $tempArr = explode(".", $fileName); $fileExt = strtolower(trim(array_pop($tempArr))); //检查扩展名 if (!in_array($fileExt, $this->exts)) { return ['stat' => 12, 'msg' => '上传文件扩展名是不允许的扩展名:' . $fileExt]; } // 检查mime $mime = \Pandaphp::shell('Image::getFileMimeType', $tempName); if (!in_array($mime, $this->mimes)) { return ['stat' => 13, 'msg' => '文件的MIME是不允许的MIME:' . $mime]; } //创建文件夹 if ($this->autoSub) { // 子目录保存 $subDirName = date('Ymd'); if (function_exists($this->nameSub[0])) { $subDirName = $this->nameSub[0]($this->nameSub[1]); } $dirpath = $this->savePath . $subDirName . '/'; } else { $dirpath = $this->savePath; } if (!\Pandaphp::shell('Dir::mkdir', $dirpath, true)) { return ['stat' => 14, 'msg' => '创建文件保存目录和子目录失败,请检查权限或者手动创建']; } // 创建新的文件 if ($this->saveExt) { $fileExt = $this->saveExt; } $newFileName = date("YmdHis") . '_' . rand(10000, 99999); if (function_exists($this->saveName[0])) { $newFileName = $this->saveName[0]($this->saveName[1]); } $newFile = $newFileName . '.' . $fileExt; // 如果重名 if (file_exists($newFile) && !$this->overwrite) { $newFile = $newFileName . '_' . rand(10000, 99999) . $fileExt; } //移动文件 $newFilePath = $dirpath . $newFile; if (move_uploaded_file($tempName, $newFilePath) === false) { return ['stat' => 8, 'msg' => '文件上传失败']; } @chmod($newFilePath, 0644); return ['stat' => 0, 'msg' => '文件上传成功', 'data' => ['name' => $newFileName, 'ext' => $fileExt, 'size' => $fileSize, 'path' => $newFilePath, 'src' => str_replace(\Pandaphp::get('webroot'), '/', $newFilePath)]]; }
/** * 获取所有的配置文件的路径 * @access private * @param string $filename 配置文件名 * @author songdengtao <http://www.songdengtao.cn/> * @return array */ private function _GetConfigFilePaths($filename = '') { $paths = []; if (!empty($filename)) { $paths[] = $this->configDirPath . $filename . $this->configFileExt; } $runtimeModule = \Pandaphp::get('runtimeModule'); if (!empty($runtimeModule)) { $paths[] = $this->configDirPath . strtolower($runtimeModule) . $this->configFileExt; } $paths[] = $this->configDirPath . $this->systemConfigFilename . $this->configFileExt; $paths[] = $this->globalConfigDirPath . $this->globalConfigFilename . $this->configFileExt; return $paths; }
/** * 404页面 * @access public * @param string $msg 展示的信息 * @param string $_404PageUrl 404页面URL * @author songdengtao <http://www.songdengtao.cn> * @return void */ public static function _404($msg = '', $_404PageUrl = '') { if (empty($_404PageUrl)) { $_404PageUrl = \Pandaphp::shell('Config::get', '404'); $_404PageUrl = \Pandaphp::get('webroot') . $_404PageUrl; } if (\Pandaphp::shell('File::isExist', $_404PageUrl)) { require_once $_404PageUrl; } else { \Pandaphp::shell('Error::halt', $msg); } #todo 读秒跳转 exit; }
/** * 获取module,controller, action * @access public * @param string $pandaUri * @author songdengtao <http://www.songdengtao.cn/> * @return array */ public function getMdlCtrlActGET($pandaUri = '') { $ret = ['module' => \Pandaphp::get('runtimeModule'), 'controller' => $this->defaultController, 'action' => $this->defaultAction, '_GET' => []]; if (!empty($pandaUri)) { $pandaUriArr = explode('/', trim($pandaUri, '/')); array_shift($pandaUriArr); if (count($pandaUriArr) >= 1) { $ret['controller'] = ucwords(array_shift($pandaUriArr)); } if (count($pandaUriArr) >= 1) { $ret['action'] = array_shift($pandaUriArr); } $ret['_GET'] = $pandaUriArr; } else { \Pandaphp::shell('Error::halt', '请检查默认的模块,控制器,操作是否空或不存在!'); } if (!empty($ret['_GET'])) { $queryArr = $ret['_GET']; $keyArr = []; $valArr = []; foreach ($queryArr as $key => $val) { if ($key % 2 === 0) { $keyArr[] = $val; } else { $valArr[] = $val; } } $GET = []; foreach ($keyArr as $k => $v) { if (isset($valArr[$k]) && false === is_null($valArr[$k])) { $GET[$v] = $valArr[$k]; } } $ret['_GET'] = $GET; } $ret['_GET'] = (array) $ret['_GET']; return \Pandaphp::shell('Filter::inputFilter', $ret); }