/** * 构造函数 * * @access public * * @param array $params 数据库连接参数,如主机名,数据库用户名,密码等 * * @return boolean */ public function __construct($options = null) { if (!extension_loaded('redis')) { Controller::halt('The redis extension to be loaded!'); } //当参数为空时,程序则自动加载配置文件中数据库连接参数 if (!$options || !is_array($options)) { $options = Configure::get('redis'); if (!$options) { $options = array(); } } $options += $this->_defaultOptions; //连接数据库 $this->_Redis = new Redis(); $connect = !$options['persistent'] ? 'connect' : 'pconnect'; $return = $this->_Redis->{$connect}($options['host'], $options['port'], $options['expire']); if ($return && $options['password']) { $return = $this->_Redis->auth($options['password']); } if ($return && $options['database']) { $return = $this->_Redis->select($options['database']); } return $return; }
/** * http下载文件 * * Reads a file and send a header to force download it. * * @access public * * @param string $file 文件路径 * @param string $rename 文件重命名后的名称 * * @return void */ public static function render($file, $rename = null) { //参数分析 if (!$file) { return false; } if (headers_sent()) { return false; } //分析文件是否存在 if (!is_file($file)) { Controller::showMsg('Error 404:The file not found!'); } //分析文件名 $filename = !$rename ? basename($file) : $rename; header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header("Content-Disposition: attachment; filename=\"{$filename}\""); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); ob_clean(); flush(); readfile($file); exit; }
/** * 构造方法 * * 用于初始化运行环境,或对基本变量进行赋值 * * @access public * * @param array $params 数据库连接参数,如主机名,数据库用户名,密码等 * * @return boolean */ public function __construct($params = array()) { if (!extension_loaded('mongo')) { Controller::halt('The mongo extension to be loaded!'); } //参数分析 if (!$params || !is_array($params)) { //加载数据库配置文件. $params = Configure::get('mongo'); } $params = is_array($params) ? $params + $this->_defaultConfig : $this->_defaultConfig; if (!isset($params['dbname']) || !$params['dbname']) { Controller::halt('The file of MongoDB config is error, dbname is not found!'); } try { //实例化mongo $this->_mongo = new Mongo($params['dsn'], $params['option']); //连接mongo数据库 $this->_dbLink = $this->_mongo->selectDB($params['dbname']); //用户登录 if (isset($params['username']) && isset($params['password'])) { $this->_dbLink->authenticate($params['username'], $params['password']); } return true; } catch (Exception $exception) { //抛出异常信息 throw new DoitException('MongoDb connect error!<br/>' . $exception->getMessage(), $exception->getCode()); } }
/** * 连接FTP服务器 * * @access public * * @param string $server FTP服务器地址 * @param integer $port FTP服务器端口 * @param string $username FTP用户名 * @param string $password FTP密码 * * @return boolean */ public function connect($server, $port = 21, $username, $password) { //参数分析 if (!$server || !$username || !$password) { return false; } try { $this->_linkId = @ftp_connect($server, $port); if (!@ftp_login($this->_linkId, $username, $password)) { Controller::showMsg('Ftp Server 登陆失败'); } //打开被动模拟 ftp_pasv($this->_linkId, 1); return true; } catch (Exception $exception) { //抛出异常信息 throw new DoitException('Ftp server connect error!<br/>' . $exception->getMessage(), $exception->getCode()); } }
/** * 项目文件的自动加载 * * doitPHP系统自动加载核心类库文件(core目录内的文件)及运行所需的controller文件、model文件、widget文件等 * * 注:并非程序初始化时将所有的controller,model等文件都统统加载完,再执行其它。 * 理解本函数前一定要先理解AutoLoad的作用。 * 当程序运行时发现所需的文件没有找到时,AutoLoad才会被激发,按照loadClass()的程序设计来完成对该文件的加载 * * @access public * * @param string $className 所需要加载的类的名称,注:不含后缀名 * * @return void */ public static function loadClass($className) { //doitPHP核心类文件的加载分析 if (substr($className, 0, 8) == 'doitphp\\') { $className = substr($className, 8); $filePath = DOIT_ROOT . DS . str_replace('\\', DS, $className) . '.php'; } else { $filePath = BASE_PATH . DS . (strpos($className, '\\') !== false ? str_replace(array('\\', '_'), DS, $className) : str_replace('_', DS, $className)) . '.php'; if (!is_file($filePath)) { //根据配置文件设置,加载文件 if (self::_loadImportConfigFile($className)) { return true; } //当文件不存在时,提示错误信息 Controller::halt('The File: ' . $filePath . ' is not found !', 'Normal'); } } Doit::loadFile($filePath); return true; }
/** * 构造方法 * * @access public * * @param array $params 数据库连接参数,如主机名,数据库用户名,密码等 * * @return boolean */ public function __construct($options = null) { //分析memcached扩展模块的加载 if (!extension_loaded('memcached')) { Controller::halt('The memcached extension to be loaded before use!'); } //获取Memcache服务器连接参数 if (!$options || !is_array($options)) { $options = Configure::get('memcached'); } if (is_array($options) && $options) { $this->_defaultOptions = $options + $this->_defaultOptions; } if (!$this->_defaultOptions['servers']) { $this->_defaultOptions['servers'][] = $this->_defaultServer; } $this->_Memcached = new Memcached(); foreach ($this->_defaultOptions['servers'] as $server) { $server += array('host' => '127.0.0.1', 'port' => 11211, 'persistent' => true); $this->_Memcached->addServer($server['host'], $server['port'], $this->_defaultOptions['persistent']); } return true; }
/** * 根据图片原来的宽和高的比例,自适应性处理缩略图的宽度和高度 * * @access protected * @return boolean */ protected function _handleImageSize() { //当没有所生成的图片的宽度和高度设置时. if (!$this->_width || !$this->_height) { Controller::halt('You do not set the image height size or width size!'); } $perW = $this->_width / $this->_imageWidth; $perH = $this->_height / $this->_imageHeight; if (ceil($this->_imageHeight * $perW) > $this->_height) { $this->_widthNew = ceil($this->_imageWidth * $perH); $this->_heightNew = $this->_height; } else { $this->_widthNew = $this->_width; $this->_heightNew = ceil($this->_imageHeight * $perW); } return true; }
/** * 读取名字为$name的zip包的内容 * * @access public * * @param string $name 所读取的zip文件的路径 * * @return array */ public function readZip($name) { //参数分析 if (!$name) { return false; } if (!is_file($name)) { Controller::halt("The File: {$name} is not found!"); } // File information $size = filesize($name); // Read file $fh = fopen($name, "rb"); $filedata = fread($fh, $size); fclose($fh); // Break into sections $filesecta = explode("PK", $filedata); // ZIP Comment $unpackeda = unpack('x16/v1length', $filesecta[1]); $comment = substr($filesecta[1], 18, $unpackeda['length']); $comment = str_replace(array("\r\n", "\r"), "\n", $comment); // CR + LF and CR -> LF // Cut entries from the central directory $filesecta = explode("PK", $filedata); $filesecta = explode("PK", $filesecta[0]); array_shift($filesecta); // Removes empty entry/signature $files = array(); foreach ($filesecta as $filedata) { // CRC:crc, FD:file date, FT: file time, CM: compression method, GPF: general purpose flag, VN: version needed, CS: compressed size, UCS: uncompressed size, FNL: filename length $entrya = array(); $unpackeda = unpack("v1version/v1general_purpose/v1compress_method/v1file_time/v1file_date/V1crc/V1size_compressed/V1size_uncompressed/v1filename_length", $filedata); // Check for value block after compressed data if ($unpackeda['general_purpose'] & 0x8) { $unpackeda2 = unpack("V1crc/V1size_compressed/V1size_uncompressed", substr($filedata, -12)); $unpackeda['crc'] = $unpackeda2['crc']; $unpackeda['size_compressed'] = $unpackeda2['size_uncompressed']; $unpackeda['size_uncompressed'] = $unpackeda2['size_uncompressed']; unset($unpackeda2); } $entrya['name'] = substr($filedata, 26, $unpackeda['filename_length']); if (substr($entrya['name'], -1) == "/") { continue; } $entrya['dir'] = dirname($entrya['name']); $entrya['dir'] = $entrya['dir'] == "." ? "" : $entrya['dir']; $entrya['name'] = basename($entrya['name']); $files[] = $entrya; } return $files; }
/** * 处理jquery 插件 lazyload的调用代码 * * @access public * * @param string $tag 图片的选择器标签 * @param string $options 图片类型(非图片格式类型) * * @return string */ public static function lazyload($tag = 'img', $options = 'small') { //选择AJAX加载类型图片 $imageName = self::_parseAjaxImage($options); return "<script type=\"text/javascript\">\$(document).ready(function(){\$('" . $tag . "').lazyload({placeholder:'" . Controller::getAssetUrl('doit/images') . '/' . $imageName . "',effect:'fadeIn'});});</script>\r"; }
/** * 获取当前项目asset目录的url * * @access public * * @param string $dirName asset目录的子目录名 * * @return string */ public static function getAssetUrl($dirName = null) { return Controller::getAssetUrl($dirName); }
/** * 视图中加载pager的CSS文件 * * 注:本方法虽与loadCss()功能一样,不过本方法是供在视图中使用的,主要用在ajax分页时。如:Pagination::loadCssFile(); * * @access public * * @param string $styleName * * @return string */ public static function loadCssFile($styleName = 'classic') { //设置分页CSS样式 switch ($styleName) { case 'classic': $_styleFile = 'doitphp_pagelist_classic.min.css'; break; default: $_styleFile = 'doitphp_pagelist_default.min.css'; } $cssFile = Controller::getBaseUrl() . '/assets/doit/images/' . $_styleFile; return "<link rel=\"stylesheet\" type=\"text/css\" href=\"" . $cssFile . "\"/>\r"; }
/** * 静态加载文件(相当于PHP函数require_once) * * include 以$fileName为名的php文件,如果加载了,这里将不再加载. * @param string $filePath 文件路径,注:含后缀名 * @return boolean */ public static function loadFile($filePath) { //参数分析 if (!$filePath) { return false; } //判断文件有没有加载过,加载过的直接返回true if (!isset(self::$_incFiles[$filePath])) { //分析文件是不是真实存在,若文件不存在,则只能... if (!is_file($filePath)) { //当所要加载的文件不存在时,错误提示 Controller::halt('The File: ' . $filePath . ' is not found!', 'Normal'); } include_once $filePath; self::$_incFiles[$filePath] = true; } return self::$_incFiles[$filePath]; }
/** * 文件重命名或移动文件 * * @access public * * @param string $sourceFile 源文件 * @param string $destFile 新文件名或路径 * * @return boolean */ public static function moveFile($sourceFile, $destFile) { //参数分析 if (!$sourceFile || !$destFile) { return false; } //文件及目录分析 if (!is_file($sourceFile)) { Controller::halt('The file:' . $sourceFile . ' is not found!'); } self::_parseDir(dirname($destFile), true); return rename($sourceFile, $destFile); }
/** * 显示验证码. * * @access public * * @param string $imageUrl 验证码的背影图片路径 * * @return void */ public function show($imageUrl = null) { //当前面没有session_start()调用时. Session::start(); if (!is_null($imageUrl)) { $this->_imageUrl = trim($imageUrl); } $this->_image = !function_exists('imagecreatetruecolor') ? imagecreate($this->_width, $this->_height) : imagecreatetruecolor($this->_width, $this->_height); //当有背景图片存在时 if ($this->_imageUrl) { //初始化图片信息. list($imageWidth, $imageHeight, $type) = getimagesize($this->_imageUrl); //分析图片的格式 switch ($type) { case 1: $image = imagecreatefromgif($this->_imageUrl); $this->_type = 'gif'; break; case 2: $image = imagecreatefromjpeg($this->_imageUrl); $this->_type = 'jpg'; break; case 3: $image = imagecreatefrompng($this->_imageUrl); $this->_type = 'png'; break; case 4: $image = imagecreatefromwbmp($this->_imageUrl); $this->_type = 'bmp'; break; } //背景 $srcX = $imageWidth > $this->_width ? mt_rand(0, $imageWidth - $this->_width) : 0; $srcY = $imageHeight > $this->_height ? mt_rand(0, $imageHeight - $this->_height) : 0; imagecopymerge($this->_image, $image, 0, 0, $srcX, $srcY, $this->_width, $this->_height, 100); imagedestroy($image); //边框 $borderColor = imagecolorallocate($this->_image, 255, 255, 255); imagerectangle($this->_image, 1, 1, $this->_width - 2, $this->_height - 2, $borderColor); } else { //定义图片类型 $this->_type = 'png'; //背景 $bgColorArray = !$this->_bgColor ? array(255, 255, 255) : $this->_bgColor; $back_color = imagecolorallocate($this->_image, $bgColorArray[0], $bgColorArray[1], $bgColorArray[2]); imagefilledrectangle($this->_image, 0, 0, $this->_width - 1, $this->_height - 1, $back_color); //边框 $borderColor = imagecolorallocate($this->_image, 238, 238, 238); imagerectangle($this->_image, 0, 0, $this->_width - 1, $this->_height - 1, $borderColor); } //获取验证码内容. $this->getCaptchaContent(); //验证码中含有汉字 if (!preg_match('~[\\x{4e00}-\\x{9fa5}]+~u', $this->_textContent)) { //计算验证码的位数 $codeStrlen = strlen($this->_textContent); //每位验证码所占用的图片宽度 $perWidth = ceil(($this->_width - 10) / $codeStrlen); for ($i = 0; $i < $codeStrlen; $i++) { //获取单个字符 $textContent = $this->_textContent[$i]; $bbox = imagettfbbox($this->_fontSize, 0, $this->_fontName, $textContent); $fontW = $bbox[2] - $bbox[0]; $fontH = abs($bbox[7] - $bbox[1]); $fontX = ceil(($perWidth - $fontW) / 2) + $perWidth * $i + 5; $min_y = $fontH + 5; $max_y = $this->_height - 5; $fontY = rand($min_y, $max_y); $fontColor = !$this->_fontColor ? imagecolorallocate($this->_image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)) : imagecolorallocate($this->_image, $this->_fontColor[0], $this->_fontColor[1], $this->_fontColor[2]); imagettftext($this->_image, $this->_fontSize, 0, $fontX, $fontY, $fontColor, $this->_fontName, $textContent); } } else { //分析验证码的位置 $bbox = imagettfbbox($this->_fontSize, 0, $this->_fontName, $this->_textContent); $fontW = $bbox[2] - $bbox[0]; $fontH = abs($bbox[7] - $bbox[1]); $fontX = ceil(($this->_width - $fontW) / 2) + 5; $min_y = $fontH + 5; $max_y = $this->_height - 5; $fontY = rand($min_y, $max_y); $fontColor = !$this->_fontColor ? imagecolorallocate($this->_image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)) : imagecolorallocate($this->_image, $this->_fontColor[0], $this->_fontColor[1], $this->_fontColor[2]); imagettftext($this->_image, $this->_fontSize, 0, $fontX, $fontY, $fontColor, $this->_fontName, $this->_textContent); } //干扰线 for ($i = 0; $i < 5; $i++) { $line_color = imagecolorallocate($this->_image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); imageline($this->_image, mt_rand(2, $this->_width - 3), mt_rand(2, $this->_height - 3), mt_rand(2, $this->_width - 3), mt_rand(2, $this->_height - 3), $line_color); } //将显示的验证码赋值给session. Session::set($this->_sessionName, $this->_textContent); //当有headers内容输出时. if (headers_sent()) { Controller::halt('headers already sent'); } //显示图片,根据背景图片的格式显示相应格式的图片. switch ($this->_type) { case 'gif': header('Content-type:image/gif'); imagegif($this->_image); break; case 'jpg': header('Content-type:image/jpeg'); imagejpeg($this->_image); break; case 'png': header('Content-type:image/png'); imagepng($this->_image); break; case 'bmp': header('Content-type:image/wbmp'); imagewbmp($this->_image); break; } imagedestroy($this->_image); }