Repository: Aoiujz/WechatSDK Branch: master Commit: 9a60328f38af Files: 382 Total size: 2.9 MB Directory structure: gitextract_dabn5tus/ ├── .gitignore ├── .htaccess ├── Application/ │ ├── Common/ │ │ ├── Common/ │ │ │ └── index.html │ │ ├── Conf/ │ │ │ ├── config.php │ │ │ └── index.html │ │ └── index.html │ ├── Home/ │ │ ├── Common/ │ │ │ └── index.html │ │ ├── Conf/ │ │ │ ├── config.php │ │ │ └── index.html │ │ ├── Controller/ │ │ │ ├── IndexController.class.php │ │ │ └── index.html │ │ ├── Model/ │ │ │ └── index.html │ │ ├── View/ │ │ │ └── index.html │ │ └── index.html │ ├── README.md │ └── index.html ├── Public/ │ └── README.md ├── ThinkPHP/ │ ├── Common/ │ │ └── functions.php │ ├── Conf/ │ │ ├── convention.php │ │ └── debug.php │ ├── LICENSE.txt │ ├── Lang/ │ │ ├── en-us.php │ │ ├── pt-br.php │ │ ├── zh-cn.php │ │ └── zh-tw.php │ ├── Library/ │ │ ├── Behavior/ │ │ │ ├── AgentCheckBehavior.class.php │ │ │ ├── BorisBehavior.class.php │ │ │ ├── BrowserCheckBehavior.class.php │ │ │ ├── BuildLiteBehavior.class.php │ │ │ ├── CheckActionRouteBehavior.class.php │ │ │ ├── CheckLangBehavior.class.php │ │ │ ├── ChromeShowPageTraceBehavior.class.php │ │ │ ├── ContentReplaceBehavior.class.php │ │ │ ├── CronRunBehavior.class.php │ │ │ ├── FireShowPageTraceBehavior.class.php │ │ │ ├── ParseTemplateBehavior.class.php │ │ │ ├── ReadHtmlCacheBehavior.class.php │ │ │ ├── RobotCheckBehavior.class.php │ │ │ ├── ShowPageTraceBehavior.class.php │ │ │ ├── ShowRuntimeBehavior.class.php │ │ │ ├── TokenBuildBehavior.class.php │ │ │ ├── UpgradeNoticeBehavior.class.php │ │ │ └── WriteHtmlCacheBehavior.class.php │ │ ├── Com/ │ │ │ ├── Wechat.class.php │ │ │ ├── WechatAuth.class.php │ │ │ └── WechatCrypt.class.php │ │ ├── Org/ │ │ │ ├── Net/ │ │ │ │ ├── Http.class.php │ │ │ │ └── IpLocation.class.php │ │ │ └── Util/ │ │ │ ├── ArrayList.class.php │ │ │ ├── CodeSwitch.class.php │ │ │ ├── Date.class.php │ │ │ ├── Rbac.class.php │ │ │ ├── Stack.class.php │ │ │ └── String.class.php │ │ ├── Think/ │ │ │ ├── App.class.php │ │ │ ├── Auth.class.php │ │ │ ├── Behavior.class.php │ │ │ ├── Build.class.php │ │ │ ├── Cache/ │ │ │ │ └── Driver/ │ │ │ │ ├── Apachenote.class.php │ │ │ │ ├── Apc.class.php │ │ │ │ ├── Db.class.php │ │ │ │ ├── Eaccelerator.class.php │ │ │ │ ├── File.class.php │ │ │ │ ├── Memcache.class.php │ │ │ │ ├── Memcached.class.php │ │ │ │ ├── Memcachesae.class.php │ │ │ │ ├── Redis.class.php │ │ │ │ ├── Shmop.class.php │ │ │ │ ├── Sqlite.class.php │ │ │ │ ├── Wincache.class.php │ │ │ │ └── Xcache.class.php │ │ │ ├── Cache.class.php │ │ │ ├── Controller/ │ │ │ │ ├── HproseController.class.php │ │ │ │ ├── JsonRpcController.class.php │ │ │ │ ├── RestController.class.php │ │ │ │ ├── RpcController.class.php │ │ │ │ └── YarController.class.php │ │ │ ├── Controller.class.php │ │ │ ├── Crypt/ │ │ │ │ └── Driver/ │ │ │ │ ├── Base64.class.php │ │ │ │ ├── Crypt.class.php │ │ │ │ ├── Des.class.php │ │ │ │ ├── Think.class.php │ │ │ │ └── Xxtea.class.php │ │ │ ├── Crypt.class.php │ │ │ ├── Db/ │ │ │ │ ├── Driver/ │ │ │ │ │ ├── Firebird.class.php │ │ │ │ │ ├── Mongo.class.php │ │ │ │ │ ├── Mysql.class.php │ │ │ │ │ ├── Oracle.class.php │ │ │ │ │ ├── Pgsql.class.php │ │ │ │ │ ├── Sqlite.class.php │ │ │ │ │ └── Sqlsrv.class.php │ │ │ │ ├── Driver.class.php │ │ │ │ └── Lite.class.php │ │ │ ├── Db.class.php │ │ │ ├── Dispatcher.class.php │ │ │ ├── Exception.class.php │ │ │ ├── Hook.class.php │ │ │ ├── Image/ │ │ │ │ └── Driver/ │ │ │ │ ├── GIF.class.php │ │ │ │ ├── Gd.class.php │ │ │ │ └── Imagick.class.php │ │ │ ├── Image.class.php │ │ │ ├── Log/ │ │ │ │ └── Driver/ │ │ │ │ ├── File.class.php │ │ │ │ └── Sae.class.php │ │ │ ├── Log.class.php │ │ │ ├── Model/ │ │ │ │ ├── AdvModel.class.php │ │ │ │ ├── MergeModel.class.php │ │ │ │ ├── MongoModel.class.php │ │ │ │ ├── RelationModel.class.php │ │ │ │ └── ViewModel.class.php │ │ │ ├── Model.class.php │ │ │ ├── Page.class.php │ │ │ ├── Route.class.php │ │ │ ├── Session/ │ │ │ │ └── Driver/ │ │ │ │ ├── Db.class.php │ │ │ │ ├── Memcache.class.php │ │ │ │ └── Mysqli.class.php │ │ │ ├── Storage/ │ │ │ │ └── Driver/ │ │ │ │ ├── File.class.php │ │ │ │ └── Sae.class.php │ │ │ ├── Storage.class.php │ │ │ ├── Template/ │ │ │ │ ├── Driver/ │ │ │ │ │ ├── Ease.class.php │ │ │ │ │ ├── Lite.class.php │ │ │ │ │ ├── Mobile.class.php │ │ │ │ │ ├── Smart.class.php │ │ │ │ │ └── Smarty.class.php │ │ │ │ ├── TagLib/ │ │ │ │ │ ├── Cx.class.php │ │ │ │ │ └── Html.class.php │ │ │ │ └── TagLib.class.php │ │ │ ├── Template.class.php │ │ │ ├── Think.class.php │ │ │ ├── Upload/ │ │ │ │ └── Driver/ │ │ │ │ ├── Bcs/ │ │ │ │ │ ├── bcs.class.php │ │ │ │ │ ├── mimetypes.class.php │ │ │ │ │ └── requestcore.class.php │ │ │ │ ├── Bcs.class.php │ │ │ │ ├── Ftp.class.php │ │ │ │ ├── Local.class.php │ │ │ │ ├── Qiniu/ │ │ │ │ │ └── QiniuStorage.class.php │ │ │ │ ├── Qiniu.class.php │ │ │ │ ├── Sae.class.php │ │ │ │ └── Upyun.class.php │ │ │ ├── Upload.class.php │ │ │ ├── Verify.class.php │ │ │ └── View.class.php │ │ └── Vendor/ │ │ ├── Boris/ │ │ │ ├── Boris.php │ │ │ ├── CLIOptionsHandler.php │ │ │ ├── ColoredInspector.php │ │ │ ├── Config.php │ │ │ ├── DumpInspector.php │ │ │ ├── EvalWorker.php │ │ │ ├── ExportInspector.php │ │ │ ├── Inspector.php │ │ │ ├── ReadlineClient.php │ │ │ └── ShallowParser.php │ │ ├── EaseTemplate/ │ │ │ ├── template.core.php │ │ │ └── template.ease.php │ │ ├── Hprose/ │ │ │ ├── HproseClassManager.php │ │ │ ├── HproseClient.php │ │ │ ├── HproseCommon.php │ │ │ ├── HproseFormatter.php │ │ │ ├── HproseHttpClient.php │ │ │ ├── HproseHttpServer.php │ │ │ ├── HproseIO.php │ │ │ ├── HproseIOStream.php │ │ │ ├── HproseReader.php │ │ │ ├── HproseTags.php │ │ │ └── HproseWriter.php │ │ ├── README.txt │ │ ├── SmartTemplate/ │ │ │ ├── class.smarttemplate.php │ │ │ ├── class.smarttemplatedebugger.php │ │ │ └── class.smarttemplateparser.php │ │ ├── Smarty/ │ │ │ ├── Smarty.class.php │ │ │ ├── SmartyBC.class.php │ │ │ ├── debug.tpl │ │ │ ├── plugins/ │ │ │ │ ├── block.textformat.php │ │ │ │ ├── function.counter.php │ │ │ │ ├── function.cycle.php │ │ │ │ ├── function.fetch.php │ │ │ │ ├── function.html_checkboxes.php │ │ │ │ ├── function.html_image.php │ │ │ │ ├── function.html_options.php │ │ │ │ ├── function.html_radios.php │ │ │ │ ├── function.html_select_date.php │ │ │ │ ├── function.html_select_time.php │ │ │ │ ├── function.html_table.php │ │ │ │ ├── function.mailto.php │ │ │ │ ├── function.math.php │ │ │ │ ├── modifier.capitalize.php │ │ │ │ ├── modifier.date_format.php │ │ │ │ ├── modifier.debug_print_var.php │ │ │ │ ├── modifier.escape.php │ │ │ │ ├── modifier.regex_replace.php │ │ │ │ ├── modifier.replace.php │ │ │ │ ├── modifier.spacify.php │ │ │ │ ├── modifier.truncate.php │ │ │ │ ├── modifiercompiler.cat.php │ │ │ │ ├── modifiercompiler.count_characters.php │ │ │ │ ├── modifiercompiler.count_paragraphs.php │ │ │ │ ├── modifiercompiler.count_sentences.php │ │ │ │ ├── modifiercompiler.count_words.php │ │ │ │ ├── modifiercompiler.default.php │ │ │ │ ├── modifiercompiler.escape.php │ │ │ │ ├── modifiercompiler.from_charset.php │ │ │ │ ├── modifiercompiler.indent.php │ │ │ │ ├── modifiercompiler.lower.php │ │ │ │ ├── modifiercompiler.noprint.php │ │ │ │ ├── modifiercompiler.string_format.php │ │ │ │ ├── modifiercompiler.strip.php │ │ │ │ ├── modifiercompiler.strip_tags.php │ │ │ │ ├── modifiercompiler.to_charset.php │ │ │ │ ├── modifiercompiler.unescape.php │ │ │ │ ├── modifiercompiler.upper.php │ │ │ │ ├── modifiercompiler.wordwrap.php │ │ │ │ ├── outputfilter.trimwhitespace.php │ │ │ │ ├── shared.escape_special_chars.php │ │ │ │ ├── shared.literal_compiler_param.php │ │ │ │ ├── shared.make_timestamp.php │ │ │ │ ├── shared.mb_str_replace.php │ │ │ │ ├── shared.mb_unicode.php │ │ │ │ ├── shared.mb_wordwrap.php │ │ │ │ └── variablefilter.htmlspecialchars.php │ │ │ └── sysplugins/ │ │ │ ├── smarty_cacheresource.php │ │ │ ├── smarty_cacheresource_custom.php │ │ │ ├── smarty_cacheresource_keyvaluestore.php │ │ │ ├── smarty_config_source.php │ │ │ ├── smarty_internal_cacheresource_file.php │ │ │ ├── smarty_internal_compile_append.php │ │ │ ├── smarty_internal_compile_assign.php │ │ │ ├── smarty_internal_compile_block.php │ │ │ ├── smarty_internal_compile_break.php │ │ │ ├── smarty_internal_compile_call.php │ │ │ ├── smarty_internal_compile_capture.php │ │ │ ├── smarty_internal_compile_config_load.php │ │ │ ├── smarty_internal_compile_continue.php │ │ │ ├── smarty_internal_compile_debug.php │ │ │ ├── smarty_internal_compile_eval.php │ │ │ ├── smarty_internal_compile_extends.php │ │ │ ├── smarty_internal_compile_for.php │ │ │ ├── smarty_internal_compile_foreach.php │ │ │ ├── smarty_internal_compile_function.php │ │ │ ├── smarty_internal_compile_if.php │ │ │ ├── smarty_internal_compile_include.php │ │ │ ├── smarty_internal_compile_include_php.php │ │ │ ├── smarty_internal_compile_insert.php │ │ │ ├── smarty_internal_compile_ldelim.php │ │ │ ├── smarty_internal_compile_nocache.php │ │ │ ├── smarty_internal_compile_private_block_plugin.php │ │ │ ├── smarty_internal_compile_private_function_plugin.php │ │ │ ├── smarty_internal_compile_private_modifier.php │ │ │ ├── smarty_internal_compile_private_object_block_function.php │ │ │ ├── smarty_internal_compile_private_object_function.php │ │ │ ├── smarty_internal_compile_private_print_expression.php │ │ │ ├── smarty_internal_compile_private_registered_block.php │ │ │ ├── smarty_internal_compile_private_registered_function.php │ │ │ ├── smarty_internal_compile_private_special_variable.php │ │ │ ├── smarty_internal_compile_rdelim.php │ │ │ ├── smarty_internal_compile_section.php │ │ │ ├── smarty_internal_compile_setfilter.php │ │ │ ├── smarty_internal_compile_while.php │ │ │ ├── smarty_internal_compilebase.php │ │ │ ├── smarty_internal_config.php │ │ │ ├── smarty_internal_config_file_compiler.php │ │ │ ├── smarty_internal_configfilelexer.php │ │ │ ├── smarty_internal_configfileparser.php │ │ │ ├── smarty_internal_data.php │ │ │ ├── smarty_internal_debug.php │ │ │ ├── smarty_internal_filter_handler.php │ │ │ ├── smarty_internal_function_call_handler.php │ │ │ ├── smarty_internal_get_include_path.php │ │ │ ├── smarty_internal_nocache_insert.php │ │ │ ├── smarty_internal_parsetree.php │ │ │ ├── smarty_internal_resource_eval.php │ │ │ ├── smarty_internal_resource_extends.php │ │ │ ├── smarty_internal_resource_file.php │ │ │ ├── smarty_internal_resource_php.php │ │ │ ├── smarty_internal_resource_registered.php │ │ │ ├── smarty_internal_resource_stream.php │ │ │ ├── smarty_internal_resource_string.php │ │ │ ├── smarty_internal_smartytemplatecompiler.php │ │ │ ├── smarty_internal_template.php │ │ │ ├── smarty_internal_templatebase.php │ │ │ ├── smarty_internal_templatecompilerbase.php │ │ │ ├── smarty_internal_templatelexer.php │ │ │ ├── smarty_internal_templateparser.php │ │ │ ├── smarty_internal_utility.php │ │ │ ├── smarty_internal_write_file.php │ │ │ ├── smarty_resource.php │ │ │ ├── smarty_resource_custom.php │ │ │ ├── smarty_resource_recompiled.php │ │ │ ├── smarty_resource_uncompiled.php │ │ │ └── smarty_security.php │ │ ├── TemplateLite/ │ │ │ ├── class.compiler.php │ │ │ ├── class.config.php │ │ │ ├── class.template.php │ │ │ └── internal/ │ │ │ ├── compile.compile_config.php │ │ │ ├── compile.compile_custom_block.php │ │ │ ├── compile.compile_custom_function.php │ │ │ ├── compile.compile_if.php │ │ │ ├── compile.generate_compiler_debug_output.php │ │ │ ├── compile.include.php │ │ │ ├── compile.parse_is_expr.php │ │ │ ├── compile.section_start.php │ │ │ ├── debug.tpl │ │ │ ├── template.build_dir.php │ │ │ ├── template.config_loader.php │ │ │ ├── template.destroy_dir.php │ │ │ ├── template.fetch_compile_include.php │ │ │ └── template.generate_debug_output.php │ │ ├── jsonRPC/ │ │ │ ├── jsonRPCClient.php │ │ │ └── jsonRPCServer.php │ │ ├── phpRPC/ │ │ │ ├── bigint.php │ │ │ ├── compat.php │ │ │ ├── dhparams/ │ │ │ │ ├── 1024.dhp │ │ │ │ ├── 128.dhp │ │ │ │ ├── 1536.dhp │ │ │ │ ├── 160.dhp │ │ │ │ ├── 192.dhp │ │ │ │ ├── 2048.dhp │ │ │ │ ├── 256.dhp │ │ │ │ ├── 3072.dhp │ │ │ │ ├── 4096.dhp │ │ │ │ ├── 512.dhp │ │ │ │ ├── 768.dhp │ │ │ │ └── 96.dhp │ │ │ ├── dhparams.php │ │ │ ├── pecl/ │ │ │ │ └── xxtea/ │ │ │ │ ├── CREDITS │ │ │ │ ├── INSTALL │ │ │ │ ├── LICENSE │ │ │ │ ├── README │ │ │ │ ├── config.m4 │ │ │ │ ├── config.w32 │ │ │ │ ├── php_xxtea.c │ │ │ │ ├── php_xxtea.dsp │ │ │ │ ├── php_xxtea.h │ │ │ │ ├── php_xxtea.sln │ │ │ │ ├── php_xxtea.vcproj │ │ │ │ ├── test/ │ │ │ │ │ └── test.php │ │ │ │ ├── xxtea.c │ │ │ │ └── xxtea.h │ │ │ ├── phprpc_client.php │ │ │ ├── phprpc_date.php │ │ │ ├── phprpc_server.php │ │ │ └── xxtea.php │ │ └── spyc/ │ │ ├── COPYING │ │ ├── README.md │ │ ├── Spyc.php │ │ ├── composer.json │ │ ├── examples/ │ │ │ ├── yaml-dump.php │ │ │ └── yaml-load.php │ │ ├── php4/ │ │ │ ├── 5to4.php │ │ │ ├── spyc.php4 │ │ │ └── test.php4 │ │ ├── spyc.yaml │ │ └── tests/ │ │ ├── DumpTest.php │ │ ├── IndentTest.php │ │ ├── ParseTest.php │ │ ├── RoundTripTest.php │ │ ├── comments.yaml │ │ ├── failing1.yaml │ │ ├── indent_1.yaml │ │ └── quotes.yaml │ ├── Mode/ │ │ ├── Api/ │ │ │ ├── App.class.php │ │ │ ├── Controller.class.php │ │ │ ├── Dispatcher.class.php │ │ │ └── functions.php │ │ ├── Lite/ │ │ │ ├── App.class.php │ │ │ ├── Controller.class.php │ │ │ ├── Dispatcher.class.php │ │ │ ├── Model.class.php │ │ │ ├── View.class.php │ │ │ ├── convention.php │ │ │ └── functions.php │ │ ├── Sae/ │ │ │ └── convention.php │ │ ├── api.php │ │ ├── common.php │ │ ├── lite.php │ │ └── sae.php │ ├── ThinkPHP.php │ └── Tpl/ │ ├── dispatch_jump.tpl │ ├── page_trace.tpl │ └── think_exception.tpl ├── index.php ├── readme.md ├── requirements.apt └── tsuru.yml ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ /Runtime/ /php/ ================================================ FILE: .htaccess ================================================ Options +FollowSymlinks RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L] ================================================ FILE: Application/Common/Common/index.html ================================================ ================================================ FILE: Application/Common/Conf/config.php ================================================ '配置值' ); ================================================ FILE: Application/Common/Conf/index.html ================================================ ================================================ FILE: Application/Common/index.html ================================================ ================================================ FILE: Application/Home/Common/index.html ================================================ ================================================ FILE: Application/Home/Conf/config.php ================================================ '配置值' ); ================================================ FILE: Application/Home/Conf/index.html ================================================ ================================================ FILE: Application/Home/Controller/IndexController.class.php ================================================ // +---------------------------------------------------------------------- namespace Home\Controller; use Think\Controller; use Com\Wechat; use Com\WechatAuth; class IndexController extends Controller{ /** * 微信消息接口入口 * 所有发送到微信的消息都会推送到该操作 * 所以,微信公众平台后台填写的api地址则为该操作的访问地址 */ public function index($id = ''){ //调试 try{ $appid = 'wx58aebef2023e68cd'; //AppID(应用ID) $token = 'E9E05045F594065909D2B5554A8F34CE'; //微信后台填写的TOKEN $crypt = 'q6FPCUoCQWaOiR3UUe5RfQu8A7hlJcMW4BnNyH9z2il'; //消息加密KEY(EncodingAESKey) /* 加载微信SDK */ $wechat = new Wechat($token, $appid, $crypt); /* 获取请求信息 */ $data = $wechat->request(); if($data && is_array($data)){ /** * 你可以在这里分析数据,决定要返回给用户什么样的信息 * 接受到的信息类型有10种,分别使用下面10个常量标识 * Wechat::MSG_TYPE_TEXT //文本消息 * Wechat::MSG_TYPE_IMAGE //图片消息 * Wechat::MSG_TYPE_VOICE //音频消息 * Wechat::MSG_TYPE_VIDEO //视频消息 * Wechat::MSG_TYPE_SHORTVIDEO //视频消息 * Wechat::MSG_TYPE_MUSIC //音乐消息 * Wechat::MSG_TYPE_NEWS //图文消息(推送过来的应该不存在这种类型,但是可以给用户回复该类型消息) * Wechat::MSG_TYPE_LOCATION //位置消息 * Wechat::MSG_TYPE_LINK //连接消息 * Wechat::MSG_TYPE_EVENT //事件消息 * * 事件消息又分为下面五种 * Wechat::MSG_EVENT_SUBSCRIBE //订阅 * Wechat::MSG_EVENT_UNSUBSCRIBE //取消订阅 * Wechat::MSG_EVENT_SCAN //二维码扫描 * Wechat::MSG_EVENT_LOCATION //报告位置 * Wechat::MSG_EVENT_CLICK //菜单点击 */ //记录微信推送过来的数据 file_put_contents('./data.json', json_encode($data)); /* 响应当前请求(自动回复) */ //$wechat->response($content, $type); /** * 响应当前请求还有以下方法可以使用 * 具体参数格式说明请参考文档 * * $wechat->replyText($text); //回复文本消息 * $wechat->replyImage($media_id); //回复图片消息 * $wechat->replyVoice($media_id); //回复音频消息 * $wechat->replyVideo($media_id, $title, $discription); //回复视频消息 * $wechat->replyMusic($title, $discription, $musicurl, $hqmusicurl, $thumb_media_id); //回复音乐消息 * $wechat->replyNews($news, $news1, $news2, $news3); //回复多条图文消息 * $wechat->replyNewsOnce($title, $discription, $url, $picurl); //回复单条图文消息 * */ //执行Demo $this->demo($wechat, $data); } } catch(\Exception $e){ file_put_contents('./error.json', json_encode($e->getMessage())); } } /** * DEMO * @param Object $wechat Wechat对象 * @param array $data 接受到微信推送的消息 */ private function demo($wechat, $data){ switch ($data['MsgType']) { case Wechat::MSG_TYPE_EVENT: switch ($data['Event']) { case Wechat::MSG_EVENT_SUBSCRIBE: $wechat->replyText('欢迎您关注麦当苗儿公众平台!回复“文本”,“图片”,“语音”,“视频”,“音乐”,“图文”,“多图文”查看相应的信息!'); break; case Wechat::MSG_EVENT_UNSUBSCRIBE: //取消关注,记录日志 break; default: $wechat->replyText("欢迎访问麦当苗儿公众平台!您的事件类型:{$data['Event']},EventKey:{$data['EventKey']}"); break; } break; case Wechat::MSG_TYPE_TEXT: switch ($data['Content']) { case '文本': $wechat->replyText('欢迎访问麦当苗儿公众平台,这是文本回复的内容!'); break; case '图片': //$media_id = $this->upload('image'); $media_id = '1J03FqvqN_jWX6xe8F-VJr7QHVTQsJBS6x4uwKuzyLE'; $wechat->replyImage($media_id); break; case '语音': //$media_id = $this->upload('voice'); $media_id = '1J03FqvqN_jWX6xe8F-VJgisW3vE28MpNljNnUeD3Pc'; $wechat->replyVoice($media_id); break; case '视频': //$media_id = $this->upload('video'); $media_id = '1J03FqvqN_jWX6xe8F-VJn9Qv0O96rcQgITYPxEIXiQ'; $wechat->replyVideo($media_id, '视频标题', '视频描述信息。。。'); break; case '音乐': //$thumb_media_id = $this->upload('thumb'); $thumb_media_id = '1J03FqvqN_jWX6xe8F-VJrjYzcBAhhglm48EhwNoBLA'; $wechat->replyMusic( 'Wakawaka!', 'Shakira - Waka Waka, MaxRNB - Your first R/Hiphop source', 'http://wechat.zjzit.cn/Public/music.mp3', 'http://wechat.zjzit.cn/Public/music.mp3', $thumb_media_id ); //回复音乐消息 break; case '图文': $wechat->replyNewsOnce( "全民创业蒙的就是你,来一盆冷水吧!", "全民创业已经如火如荼,然而创业是一个非常自我的过程,它是一种生活方式的选择。从外部的推动有助于提高创业的存活率,但是未必能够提高创新的成功率。第一次创业的人,至少90%以上都会以失败而告终。创业成功者大部分年龄在30岁到38岁之间,而且创业成功最高的概率是第三次创业。", "http://www.topthink.com/topic/11991.html", "http://yun.topthink.com/Uploads/Editor/2015-07-30/55b991cad4c48.jpg" ); //回复单条图文消息 break; case '多图文': $news = array( "全民创业蒙的就是你,来一盆冷水吧!", "全民创业已经如火如荼,然而创业是一个非常自我的过程,它是一种生活方式的选择。从外部的推动有助于提高创业的存活率,但是未必能够提高创新的成功率。第一次创业的人,至少90%以上都会以失败而告终。创业成功者大部分年龄在30岁到38岁之间,而且创业成功最高的概率是第三次创业。", "http://www.topthink.com/topic/11991.html", "http://yun.topthink.com/Uploads/Editor/2015-07-30/55b991cad4c48.jpg" ); //回复单条图文消息 $wechat->replyNews($news, $news, $news, $news, $news); break; default: $wechat->replyText("欢迎访问麦当苗儿公众平台!您输入的内容是:{$data['Content']}"); break; } break; default: # code... break; } } /** * 资源文件上传方法 * @param string $type 上传的资源类型 * @return string 媒体资源ID */ private function upload($type){ $appid = 'wx58aebef2023e68cd'; $appsecret = 'bf818ec2fb49c20a478bbefe9dc88c60'; $token = session("token"); if($token){ $auth = new WechatAuth($appid, $appsecret, $token); } else { $auth = new WechatAuth($appid, $appsecret); $token = $auth->getAccessToken(); session(array('expire' => $token['expires_in'])); session("token", $token['access_token']); } switch ($type) { case 'image': $filename = './Public/image.jpg'; $media = $auth->materialAddMaterial($filename, $type); break; case 'voice': $filename = './Public/voice.mp3'; $media = $auth->materialAddMaterial($filename, $type); break; case 'video': $filename = './Public/video.mp4'; $discription = array('title' => '视频标题', 'introduction' => '视频描述'); $media = $auth->materialAddMaterial($filename, $type, $discription); break; case 'thumb': $filename = './Public/music.jpg'; $media = $auth->materialAddMaterial($filename, $type); break; default: return ''; } if($media["errcode"] == 42001){ //access_token expired session("token", null); $this->upload($type); } return $media['media_id']; } } ================================================ FILE: Application/Home/Controller/index.html ================================================ ================================================ FILE: Application/Home/Model/index.html ================================================ ================================================ FILE: Application/Home/View/index.html ================================================ ================================================ FILE: Application/Home/index.html ================================================ ================================================ FILE: Application/README.md ================================================ 项目目录 ================================================ FILE: Application/index.html ================================================ ================================================ FILE: Public/README.md ================================================ 资源文件目录 ================================================ FILE: ThinkPHP/Common/functions.php ================================================ // +---------------------------------------------------------------------- /** * Think 系统函数库 */ /** * 获取和设置配置参数 支持批量定义 * @param string|array $name 配置变量 * @param mixed $value 配置值 * @param mixed $default 默认值 * @return mixed */ function C($name=null, $value=null,$default=null) { static $_config = array(); // 无参数时获取所有 if (empty($name)) { return $_config; } // 优先执行设置获取或赋值 if (is_string($name)) { if (!strpos($name, '.')) { $name = strtoupper($name); if (is_null($value)) return isset($_config[$name]) ? $_config[$name] : $default; $_config[$name] = $value; return null; } // 二维数组设置和获取支持 $name = explode('.', $name); $name[0] = strtoupper($name[0]); if (is_null($value)) return isset($_config[$name[0]][$name[1]]) ? $_config[$name[0]][$name[1]] : $default; $_config[$name[0]][$name[1]] = $value; return null; } // 批量设置 if (is_array($name)){ $_config = array_merge($_config, array_change_key_case($name,CASE_UPPER)); return null; } return null; // 避免非法参数 } /** * 加载配置文件 支持格式转换 仅支持一级配置 * @param string $file 配置文件名 * @param string $parse 配置解析方法 有些格式需要用户自己解析 * @return array */ function load_config($file,$parse=CONF_PARSE){ $ext = pathinfo($file,PATHINFO_EXTENSION); switch($ext){ case 'php': return include $file; case 'ini': return parse_ini_file($file); case 'yaml': return yaml_parse_file($file); case 'xml': return (array)simplexml_load_file($file); case 'json': return json_decode(file_get_contents($file), true); default: if(function_exists($parse)){ return $parse($file); }else{ E(L('_NOT_SUPPORT_').':'.$ext); } } } /** * 解析yaml文件返回一个数组 * @param string $file 配置文件名 * @return array */ if (!function_exists('yaml_parse_file')) { function yaml_parse_file($file) { vendor('spyc.Spyc'); return Spyc::YAMLLoad($file); } } /** * 抛出异常处理 * @param string $msg 异常消息 * @param integer $code 异常代码 默认为0 * @throws Think\Exception * @return void */ function E($msg, $code=0) { throw new Think\Exception($msg, $code); } /** * 记录和统计时间(微秒)和内存使用情况 * 使用方法: * * G('begin'); // 记录开始标记位 * // ... 区间运行代码 * G('end'); // 记录结束标签位 * echo G('begin','end',6); // 统计区间运行时间 精确到小数后6位 * echo G('begin','end','m'); // 统计区间内存使用情况 * 如果end标记位没有定义,则会自动以当前作为标记位 * 其中统计内存使用需要 MEMORY_LIMIT_ON 常量为true才有效 * * @param string $start 开始标签 * @param string $end 结束标签 * @param integer|string $dec 小数位或者m * @return mixed */ function G($start,$end='',$dec=4) { static $_info = array(); static $_mem = array(); if(is_float($end)) { // 记录时间 $_info[$start] = $end; }elseif(!empty($end)){ // 统计时间和内存使用 if(!isset($_info[$end])) $_info[$end] = microtime(TRUE); if(MEMORY_LIMIT_ON && $dec=='m'){ if(!isset($_mem[$end])) $_mem[$end] = memory_get_usage(); return number_format(($_mem[$end]-$_mem[$start])/1024); }else{ return number_format(($_info[$end]-$_info[$start]),$dec); } }else{ // 记录时间和内存使用 $_info[$start] = microtime(TRUE); if(MEMORY_LIMIT_ON) $_mem[$start] = memory_get_usage(); } return null; } /** * 获取和设置语言定义(不区分大小写) * @param string|array $name 语言变量 * @param mixed $value 语言值或者变量 * @return mixed */ function L($name=null, $value=null) { static $_lang = array(); // 空参数返回所有定义 if (empty($name)) return $_lang; // 判断语言获取(或设置) // 若不存在,直接返回全大写$name if (is_string($name)) { $name = strtoupper($name); if (is_null($value)){ return isset($_lang[$name]) ? $_lang[$name] : $name; }elseif(is_array($value)){ // 支持变量 $replace = array_keys($value); foreach($replace as &$v){ $v = '{$'.$v.'}'; } return str_replace($replace,$value,isset($_lang[$name]) ? $_lang[$name] : $name); } $_lang[$name] = $value; // 语言定义 return null; } // 批量定义 if (is_array($name)) $_lang = array_merge($_lang, array_change_key_case($name, CASE_UPPER)); return null; } /** * 添加和获取页面Trace记录 * @param string $value 变量 * @param string $label 标签 * @param string $level 日志级别 * @param boolean $record 是否记录日志 * @return void|array */ function trace($value='[think]',$label='',$level='DEBUG',$record=false) { return Think\Think::trace($value,$label,$level,$record); } /** * 编译文件 * @param string $filename 文件名 * @return string */ function compile($filename) { $content = php_strip_whitespace($filename); $content = trim(substr($content, 5)); // 替换预编译指令 $content = preg_replace('/\/\/\[RUNTIME\](.*?)\/\/\[\/RUNTIME\]/s', '', $content); if(0===strpos($content,'namespace')){ $content = preg_replace('/namespace\s(.*?);/','namespace \\1{',$content,1); }else{ $content = 'namespace {'.$content; } if ('?>' == substr($content, -2)) $content = substr($content, 0, -2); return $content.'}'; } /** * 获取模版文件 格式 资源://模块@主题/控制器/操作 * @param string $template 模版资源地址 * @param string $layer 视图层(目录)名称 * @return string */ function T($template='',$layer=''){ // 解析模版资源地址 if(false === strpos($template,'://')){ $template = 'http://'.str_replace(':', '/',$template); } $info = parse_url($template); $file = $info['host'].(isset($info['path'])?$info['path']:''); $module = isset($info['user'])?$info['user'].'/':MODULE_NAME.'/'; $extend = $info['scheme']; $layer = $layer?$layer:C('DEFAULT_V_LAYER'); // 获取当前主题的模版路径 $auto = C('AUTOLOAD_NAMESPACE'); if($auto && isset($auto[$extend])){ // 扩展资源 $baseUrl = $auto[$extend].$module.$layer.'/'; }elseif(C('VIEW_PATH')){ // 改变模块视图目录 $baseUrl = C('VIEW_PATH'); }elseif(defined('TMPL_PATH')){ // 指定全局视图目录 $baseUrl = TMPL_PATH.$module; }else{ $baseUrl = APP_PATH.$module.$layer.'/'; } // 获取主题 $theme = substr_count($file,'/')<2 ? C('DEFAULT_THEME') : ''; // 分析模板文件规则 $depr = C('TMPL_FILE_DEPR'); if('' == $file) { // 如果模板文件名为空 按照默认规则定位 $file = CONTROLLER_NAME . $depr . ACTION_NAME; }elseif(false === strpos($file, '/')){ $file = CONTROLLER_NAME . $depr . $file; }elseif('/' != $depr){ $file = substr_count($file,'/')>1 ? substr_replace($file,$depr,strrpos($file,'/'),1) : str_replace('/', $depr, $file); } return $baseUrl.($theme?$theme.'/':'').$file.C('TMPL_TEMPLATE_SUFFIX'); } /** * 获取输入参数 支持过滤和默认值 * 使用方法: * * I('id',0); 获取id参数 自动判断get或者post * I('post.name','','htmlspecialchars'); 获取$_POST['name'] * I('get.'); 获取$_GET * * @param string $name 变量的名称 支持指定类型 * @param mixed $default 不存在的时候默认值 * @param mixed $filter 参数过滤方法 * @param mixed $datas 要获取的额外数据源 * @return mixed */ function I($name,$default='',$filter=null,$datas=null) { static $_PUT = null; if(strpos($name,'/')){ // 指定修饰符 list($name,$type) = explode('/',$name,2); }elseif(C('VAR_AUTO_STRING')){ // 默认强制转换为字符串 $type = 's'; } if(strpos($name,'.')) { // 指定参数来源 list($method,$name) = explode('.',$name,2); }else{ // 默认为自动判断 $method = 'param'; } switch(strtolower($method)) { case 'get' : $input =& $_GET; break; case 'post' : $input =& $_POST; break; case 'put' : if(is_null($_PUT)){ parse_str(file_get_contents('php://input'), $_PUT); } $input = $_PUT; break; case 'param' : switch($_SERVER['REQUEST_METHOD']) { case 'POST': $input = $_POST; break; case 'PUT': if(is_null($_PUT)){ parse_str(file_get_contents('php://input'), $_PUT); } $input = $_PUT; break; default: $input = $_GET; } break; case 'path' : $input = array(); if(!empty($_SERVER['PATH_INFO'])){ $depr = C('URL_PATHINFO_DEPR'); $input = explode($depr,trim($_SERVER['PATH_INFO'],$depr)); } break; case 'request' : $input =& $_REQUEST; break; case 'session' : $input =& $_SESSION; break; case 'cookie' : $input =& $_COOKIE; break; case 'server' : $input =& $_SERVER; break; case 'globals' : $input =& $GLOBALS; break; case 'data' : $input =& $datas; break; default: return null; } if(''==$name) { // 获取全部变量 $data = $input; $filters = isset($filter)?$filter:C('DEFAULT_FILTER'); if($filters) { if(is_string($filters)){ $filters = explode(',',$filters); } foreach($filters as $filter){ $data = array_map_recursive($filter,$data); // 参数过滤 } } }elseif(isset($input[$name])) { // 取值操作 $data = $input[$name]; $filters = isset($filter)?$filter:C('DEFAULT_FILTER'); if($filters) { if(is_string($filters)){ if(0 === strpos($filters,'/')){ if(1 !== preg_match($filters,(string)$data)){ // 支持正则验证 return isset($default) ? $default : null; } }else{ $filters = explode(',',$filters); } }elseif(is_int($filters)){ $filters = array($filters); } if(is_array($filters)){ foreach($filters as $filter){ if(function_exists($filter)) { $data = is_array($data) ? array_map_recursive($filter,$data) : $filter($data); // 参数过滤 }else{ $data = filter_var($data,is_int($filter) ? $filter : filter_id($filter)); if(false === $data) { return isset($default) ? $default : null; } } } } } if(!empty($type)){ switch(strtolower($type)){ case 'a': // 数组 $data = (array)$data; break; case 'd': // 数字 $data = (int)$data; break; case 'f': // 浮点 $data = (float)$data; break; case 'b': // 布尔 $data = (boolean)$data; break; case 's': // 字符串 default: $data = (string)$data; } } }else{ // 变量默认值 $data = isset($default)?$default:null; } is_array($data) && array_walk_recursive($data,'think_filter'); return $data; } function array_map_recursive($filter, $data) { $result = array(); foreach ($data as $key => $val) { $result[$key] = is_array($val) ? array_map_recursive($filter, $val) : call_user_func($filter, $val); } return $result; } /** * 设置和获取统计数据 * 使用方法: * * N('db',1); // 记录数据库操作次数 * N('read',1); // 记录读取次数 * echo N('db'); // 获取当前页面数据库的所有操作次数 * echo N('read'); // 获取当前页面读取次数 * * @param string $key 标识位置 * @param integer $step 步进值 * @param boolean $save 是否保存结果 * @return mixed */ function N($key, $step=0,$save=false) { static $_num = array(); if (!isset($_num[$key])) { $_num[$key] = (false !== $save)? S('N_'.$key) : 0; } if (empty($step)){ return $_num[$key]; }else{ $_num[$key] = $_num[$key] + (int)$step; } if(false !== $save){ // 保存结果 S('N_'.$key,$_num[$key],$save); } return null; } /** * 字符串命名风格转换 * type 0 将Java风格转换为C的风格 1 将C风格转换为Java的风格 * @param string $name 字符串 * @param integer $type 转换类型 * @return string */ function parse_name($name, $type=0) { if ($type) { return ucfirst(preg_replace_callback('/_([a-zA-Z])/', function($match){return strtoupper($match[1]);}, $name)); } else { return strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_")); } } /** * 优化的require_once * @param string $filename 文件地址 * @return boolean */ function require_cache($filename) { static $_importFiles = array(); if (!isset($_importFiles[$filename])) { if (file_exists_case($filename)) { require $filename; $_importFiles[$filename] = true; } else { $_importFiles[$filename] = false; } } return $_importFiles[$filename]; } /** * 区分大小写的文件存在判断 * @param string $filename 文件地址 * @return boolean */ function file_exists_case($filename) { if (is_file($filename)) { if (IS_WIN && APP_DEBUG) { if (basename(realpath($filename)) != basename($filename)) return false; } return true; } return false; } /** * 导入所需的类库 同java的Import 本函数有缓存功能 * @param string $class 类库命名空间字符串 * @param string $baseUrl 起始路径 * @param string $ext 导入的文件扩展名 * @return boolean */ function import($class, $baseUrl = '', $ext=EXT) { static $_file = array(); $class = str_replace(array('.', '#'), array('/', '.'), $class); if (isset($_file[$class . $baseUrl])) return true; else $_file[$class . $baseUrl] = true; $class_strut = explode('/', $class); if (empty($baseUrl)) { if ('@' == $class_strut[0] || MODULE_NAME == $class_strut[0]) { //加载当前模块的类库 $baseUrl = MODULE_PATH; $class = substr_replace($class, '', 0, strlen($class_strut[0]) + 1); }elseif ('Common' == $class_strut[0]) { //加载公共模块的类库 $baseUrl = COMMON_PATH; $class = substr($class, 7); }elseif (in_array($class_strut[0],array('Think','Org','Behavior','Com','Vendor')) || is_dir(LIB_PATH.$class_strut[0])) { // 系统类库包和第三方类库包 $baseUrl = LIB_PATH; }else { // 加载其他模块的类库 $baseUrl = APP_PATH; } } if (substr($baseUrl, -1) != '/') $baseUrl .= '/'; $classfile = $baseUrl . $class . $ext; if (!class_exists(basename($class),false)) { // 如果类不存在 则导入类库文件 return require_cache($classfile); } return null; } /** * 基于命名空间方式导入函数库 * load('@.Util.Array') * @param string $name 函数库命名空间字符串 * @param string $baseUrl 起始路径 * @param string $ext 导入的文件扩展名 * @return void */ function load($name, $baseUrl='', $ext='.php') { $name = str_replace(array('.', '#'), array('/', '.'), $name); if (empty($baseUrl)) { if (0 === strpos($name, '@/')) {//加载当前模块函数库 $baseUrl = MODULE_PATH.'Common/'; $name = substr($name, 2); } else { //加载其他模块函数库 $array = explode('/', $name); $baseUrl = APP_PATH . array_shift($array).'/Common/'; $name = implode('/',$array); } } if (substr($baseUrl, -1) != '/') $baseUrl .= '/'; require_cache($baseUrl . $name . $ext); } /** * 快速导入第三方框架类库 所有第三方框架的类库文件统一放到 系统的Vendor目录下面 * @param string $class 类库 * @param string $baseUrl 基础目录 * @param string $ext 类库后缀 * @return boolean */ function vendor($class, $baseUrl = '', $ext='.php') { if (empty($baseUrl)) $baseUrl = VENDOR_PATH; return import($class, $baseUrl, $ext); } /** * 实例化模型类 格式 [资源://][模块/]模型 * @param string $name 资源地址 * @param string $layer 模型层名称 * @return Think\Model */ function D($name='',$layer='') { if(empty($name)) return new Think\Model; static $_model = array(); $layer = $layer? : C('DEFAULT_M_LAYER'); if(isset($_model[$name.$layer])) return $_model[$name.$layer]; $class = parse_res_name($name,$layer); if(class_exists($class)) { $model = new $class(basename($name)); }elseif(false === strpos($name,'/')){ // 自动加载公共模块下面的模型 if(!C('APP_USE_NAMESPACE')){ import('Common/'.$layer.'/'.$class); }else{ $class = '\\Common\\'.$layer.'\\'.$name.$layer; } $model = class_exists($class)? new $class($name) : new Think\Model($name); }else { Think\Log::record('D方法实例化没找到模型类'.$class,Think\Log::NOTICE); $model = new Think\Model(basename($name)); } $_model[$name.$layer] = $model; return $model; } /** * 实例化一个没有模型文件的Model * @param string $name Model名称 支持指定基础模型 例如 MongoModel:User * @param string $tablePrefix 表前缀 * @param mixed $connection 数据库连接信息 * @return Think\Model */ function M($name='', $tablePrefix='',$connection='') { static $_model = array(); if(strpos($name,':')) { list($class,$name) = explode(':',$name); }else{ $class = 'Think\\Model'; } $guid = (is_array($connection)?implode('',$connection):$connection).$tablePrefix . $name . '_' . $class; if (!isset($_model[$guid])) $_model[$guid] = new $class($name,$tablePrefix,$connection); return $_model[$guid]; } /** * 解析资源地址并导入类库文件 * 例如 module/controller addon://module/behavior * @param string $name 资源地址 格式:[扩展://][模块/]资源名 * @param string $layer 分层名称 * @param integer $level 控制器层次 * @return string */ function parse_res_name($name,$layer,$level=1){ if(strpos($name,'://')) {// 指定扩展资源 list($extend,$name) = explode('://',$name); }else{ $extend = ''; } if(strpos($name,'/') && substr_count($name, '/')>=$level){ // 指定模块 list($module,$name) = explode('/',$name,2); }else{ $module = defined('MODULE_NAME') ? MODULE_NAME : '' ; } $array = explode('/',$name); if(!C('APP_USE_NAMESPACE')){ $class = parse_name($name, 1); import($module.'/'.$layer.'/'.$class.$layer); }else{ $class = $module.'\\'.$layer; foreach($array as $name){ $class .= '\\'.parse_name($name, 1); } // 导入资源类库 if($extend){ // 扩展资源 $class = $extend.'\\'.$class; } } return $class.$layer; } /** * 用于实例化访问控制器 * @param string $name 控制器名 * @param string $path 控制器命名空间(路径) * @return Think\Controller|false */ function controller($name,$path=''){ $layer = C('DEFAULT_C_LAYER'); if(!C('APP_USE_NAMESPACE')){ $class = parse_name($name, 1).$layer; import(MODULE_NAME.'/'.$layer.'/'.$class); }else{ $class = ( $path ? basename(ADDON_PATH).'\\'.$path : MODULE_NAME ).'\\'.$layer; $array = explode('/',$name); foreach($array as $name){ $class .= '\\'.parse_name($name, 1); } $class .= $layer; } if(class_exists($class)) { return new $class(); }else { return false; } } /** * 实例化多层控制器 格式:[资源://][模块/]控制器 * @param string $name 资源地址 * @param string $layer 控制层名称 * @param integer $level 控制器层次 * @return Think\Controller|false */ function A($name,$layer='',$level=0) { static $_action = array(); $layer = $layer? : C('DEFAULT_C_LAYER'); $level = $level? : ($layer == C('DEFAULT_C_LAYER')?C('CONTROLLER_LEVEL'):1); if(isset($_action[$name.$layer])) return $_action[$name.$layer]; $class = parse_res_name($name,$layer,$level); if(class_exists($class)) { $action = new $class(); $_action[$name.$layer] = $action; return $action; }else { return false; } } /** * 远程调用控制器的操作方法 URL 参数格式 [资源://][模块/]控制器/操作 * @param string $url 调用地址 * @param string|array $vars 调用参数 支持字符串和数组 * @param string $layer 要调用的控制层名称 * @return mixed */ function R($url,$vars=array(),$layer='') { $info = pathinfo($url); $action = $info['basename']; $module = $info['dirname']; $class = A($module,$layer); if($class){ if(is_string($vars)) { parse_str($vars,$vars); } return call_user_func_array(array(&$class,$action.C('ACTION_SUFFIX')),$vars); }else{ return false; } } /** * 处理标签扩展 * @param string $tag 标签名称 * @param mixed $params 传入参数 * @return void */ function tag($tag, &$params=NULL) { \Think\Hook::listen($tag,$params); } /** * 执行某个行为 * @param string $name 行为名称 * @param string $tag 标签名称(行为类无需传入) * @param Mixed $params 传入的参数 * @return void */ function B($name, $tag='',&$params=NULL) { if(''==$tag){ $name .= 'Behavior'; } return \Think\Hook::exec($name,$tag,$params); } /** * 去除代码中的空白和注释 * @param string $content 代码内容 * @return string */ function strip_whitespace($content) { $stripStr = ''; //分析php源码 $tokens = token_get_all($content); $last_space = false; for ($i = 0, $j = count($tokens); $i < $j; $i++) { if (is_string($tokens[$i])) { $last_space = false; $stripStr .= $tokens[$i]; } else { switch ($tokens[$i][0]) { //过滤各种PHP注释 case T_COMMENT: case T_DOC_COMMENT: break; //过滤空格 case T_WHITESPACE: if (!$last_space) { $stripStr .= ' '; $last_space = true; } break; case T_START_HEREDOC: $stripStr .= "<<'; } else { $output = $label . print_r($var, true); } } else { ob_start(); var_dump($var); $output = ob_get_clean(); if (!extension_loaded('xdebug')) { $output = preg_replace('/\]\=\>\n(\s+)/m', '] => ', $output); $output = '
' . $label . htmlspecialchars($output, ENT_QUOTES) . '
'; } } if ($echo) { echo($output); return null; }else return $output; } /** * 设置当前页面的布局 * @param string|false $layout 布局名称 为false的时候表示关闭布局 * @return void */ function layout($layout) { if(false !== $layout) { // 开启布局 C('LAYOUT_ON',true); if(is_string($layout)) { // 设置新的布局模板 C('LAYOUT_NAME',$layout); } }else{// 临时关闭布局 C('LAYOUT_ON',false); } } /** * URL组装 支持不同URL模式 * @param string $url URL表达式,格式:'[模块/控制器/操作#锚点@域名]?参数1=值1&参数2=值2...' * @param string|array $vars 传入的参数,支持数组和字符串 * @param string|boolean $suffix 伪静态后缀,默认为true表示获取配置值 * @param boolean $domain 是否显示域名 * @return string */ function U($url='',$vars='',$suffix=true,$domain=false) { // 解析URL $info = parse_url($url); $url = !empty($info['path'])?$info['path']:ACTION_NAME; if(isset($info['fragment'])) { // 解析锚点 $anchor = $info['fragment']; if(false !== strpos($anchor,'?')) { // 解析参数 list($anchor,$info['query']) = explode('?',$anchor,2); } if(false !== strpos($anchor,'@')) { // 解析域名 list($anchor,$host) = explode('@',$anchor, 2); } }elseif(false !== strpos($url,'@')) { // 解析域名 list($url,$host) = explode('@',$info['path'], 2); } // 解析子域名 if(isset($host)) { $domain = $host.(strpos($host,'.')?'':strstr($_SERVER['HTTP_HOST'],'.')); }elseif($domain===true){ $domain = $_SERVER['HTTP_HOST']; if(C('APP_SUB_DOMAIN_DEPLOY') ) { // 开启子域名部署 $domain = $domain=='localhost'?'localhost':'www'.strstr($_SERVER['HTTP_HOST'],'.'); // '子域名'=>array('模块[/控制器]'); foreach (C('APP_SUB_DOMAIN_RULES') as $key => $rule) { $rule = is_array($rule)?$rule[0]:$rule; if(false === strpos($key,'*') && 0=== strpos($url,$rule)) { $domain = $key.strstr($domain,'.'); // 生成对应子域名 $url = substr_replace($url,'',0,strlen($rule)); break; } } } } // 解析参数 if(is_string($vars)) { // aaa=1&bbb=2 转换成数组 parse_str($vars,$vars); }elseif(!is_array($vars)){ $vars = array(); } if(isset($info['query'])) { // 解析地址里面参数 合并到vars parse_str($info['query'],$params); $vars = array_merge($params,$vars); } // URL组装 $depr = C('URL_PATHINFO_DEPR'); $urlCase = C('URL_CASE_INSENSITIVE'); if($url) { if(0=== strpos($url,'/')) {// 定义路由 $route = true; $url = substr($url,1); if('/' != $depr) { $url = str_replace('/',$depr,$url); } }else{ if('/' != $depr) { // 安全替换 $url = str_replace('/',$depr,$url); } // 解析模块、控制器和操作 $url = trim($url,$depr); $path = explode($depr,$url); $var = array(); $varModule = C('VAR_MODULE'); $varController = C('VAR_CONTROLLER'); $varAction = C('VAR_ACTION'); $var[$varAction] = !empty($path)?array_pop($path):ACTION_NAME; $var[$varController] = !empty($path)?array_pop($path):CONTROLLER_NAME; if($maps = C('URL_ACTION_MAP')) { if(isset($maps[strtolower($var[$varController])])) { $maps = $maps[strtolower($var[$varController])]; if($action = array_search(strtolower($var[$varAction]),$maps)){ $var[$varAction] = $action; } } } if($maps = C('URL_CONTROLLER_MAP')) { if($controller = array_search(strtolower($var[$varController]),$maps)){ $var[$varController] = $controller; } } if($urlCase) { $var[$varController] = parse_name($var[$varController]); } $module = ''; if(!empty($path)) { $var[$varModule] = implode($depr,$path); }else{ if(C('MULTI_MODULE')) { if(MODULE_NAME != C('DEFAULT_MODULE') || !C('MODULE_ALLOW_LIST')){ $var[$varModule]= MODULE_NAME; } } } if($maps = C('URL_MODULE_MAP')) { if($_module = array_search(strtolower($var[$varModule]),$maps)){ $var[$varModule] = $_module; } } if(isset($var[$varModule])){ $module = $var[$varModule]; unset($var[$varModule]); } } } if(C('URL_MODEL') == 0) { // 普通模式URL转换 $url = __APP__.'?'.C('VAR_MODULE')."={$module}&".http_build_query(array_reverse($var)); if($urlCase){ $url = strtolower($url); } if(!empty($vars)) { $vars = http_build_query($vars); $url .= '&'.$vars; } }else{ // PATHINFO模式或者兼容URL模式 if(isset($route)) { $url = __APP__.'/'.rtrim($url,$depr); }else{ $module = (defined('BIND_MODULE') && BIND_MODULE==$module )? '' : $module; $url = __APP__.'/'.($module?$module.MODULE_PATHINFO_DEPR:'').implode($depr,array_reverse($var)); } if($urlCase){ $url = strtolower($url); } if(!empty($vars)) { // 添加参数 foreach ($vars as $var => $val){ if('' !== trim($val)) $url .= $depr . $var . $depr . urlencode($val); } } if($suffix) { $suffix = $suffix===true?C('URL_HTML_SUFFIX'):$suffix; if($pos = strpos($suffix, '|')){ $suffix = substr($suffix, 0, $pos); } if($suffix && '/' != substr($url,-1)){ $url .= '.'.ltrim($suffix,'.'); } } } if(isset($anchor)){ $url .= '#'.$anchor; } if($domain) { $url = (is_ssl()?'https://':'http://').$domain.$url; } return $url; } /** * 渲染输出Widget * @param string $name Widget名称 * @param array $data 传入的参数 * @return void */ function W($name, $data=array()) { return R($name,$data,'Widget'); } /** * 判断是否SSL协议 * @return boolean */ function is_ssl() { if(isset($_SERVER['HTTPS']) && ('1' == $_SERVER['HTTPS'] || 'on' == strtolower($_SERVER['HTTPS']))){ return true; }elseif(isset($_SERVER['SERVER_PORT']) && ('443' == $_SERVER['SERVER_PORT'] )) { return true; } return false; } /** * URL重定向 * @param string $url 重定向的URL地址 * @param integer $time 重定向的等待时间(秒) * @param string $msg 重定向前的提示信息 * @return void */ function redirect($url, $time=0, $msg='') { //多行URL地址支持 $url = str_replace(array("\n", "\r"), '', $url); if (empty($msg)) $msg = "系统将在{$time}秒之后自动跳转到{$url}!"; if (!headers_sent()) { // redirect if (0 === $time) { header('Location: ' . $url); } else { header("refresh:{$time};url={$url}"); echo($msg); } exit(); } else { $str = ""; if ($time != 0) $str .= $msg; exit($str); } } /** * 缓存管理 * @param mixed $name 缓存名称,如果为数组表示进行缓存设置 * @param mixed $value 缓存值 * @param mixed $options 缓存参数 * @return mixed */ function S($name,$value='',$options=null) { static $cache = ''; if(is_array($options)){ // 缓存操作的同时初始化 $type = isset($options['type'])?$options['type']:''; $cache = Think\Cache::getInstance($type,$options); }elseif(is_array($name)) { // 缓存初始化 $type = isset($name['type'])?$name['type']:''; $cache = Think\Cache::getInstance($type,$name); return $cache; }elseif(empty($cache)) { // 自动初始化 $cache = Think\Cache::getInstance(); } if(''=== $value){ // 获取缓存 return $cache->get($name); }elseif(is_null($value)) { // 删除缓存 return $cache->rm($name); }else { // 缓存数据 if(is_array($options)) { $expire = isset($options['expire'])?$options['expire']:NULL; }else{ $expire = is_numeric($options)?$options:NULL; } return $cache->set($name, $value, $expire); } } /** * 快速文件数据读取和保存 针对简单类型数据 字符串、数组 * @param string $name 缓存名称 * @param mixed $value 缓存值 * @param string $path 缓存路径 * @return mixed */ function F($name, $value='', $path=DATA_PATH) { static $_cache = array(); $filename = $path . $name . '.php'; if ('' !== $value) { if (is_null($value)) { // 删除缓存 if(false !== strpos($name,'*')){ return false; // TODO }else{ unset($_cache[$name]); return Think\Storage::unlink($filename,'F'); } } else { Think\Storage::put($filename,serialize($value),'F'); // 缓存数据 $_cache[$name] = $value; return null; } } // 获取缓存数据 if (isset($_cache[$name])) return $_cache[$name]; if (Think\Storage::has($filename,'F')){ $value = unserialize(Think\Storage::read($filename,'F')); $_cache[$name] = $value; } else { $value = false; } return $value; } /** * 根据PHP各种类型变量生成唯一标识号 * @param mixed $mix 变量 * @return string */ function to_guid_string($mix) { if (is_object($mix)) { return spl_object_hash($mix); } elseif (is_resource($mix)) { $mix = get_resource_type($mix) . strval($mix); } else { $mix = serialize($mix); } return md5($mix); } /** * XML编码 * @param mixed $data 数据 * @param string $root 根节点名 * @param string $item 数字索引的子节点名 * @param string $attr 根节点属性 * @param string $id 数字索引子节点key转换的属性名 * @param string $encoding 数据编码 * @return string */ function xml_encode($data, $root='think', $item='item', $attr='', $id='id', $encoding='utf-8') { if(is_array($attr)){ $_attr = array(); foreach ($attr as $key => $value) { $_attr[] = "{$key}=\"{$value}\""; } $attr = implode(' ', $_attr); } $attr = trim($attr); $attr = empty($attr) ? '' : " {$attr}"; $xml = ""; $xml .= "<{$root}{$attr}>"; $xml .= data_to_xml($data, $item, $id); $xml .= ""; return $xml; } /** * 数据XML编码 * @param mixed $data 数据 * @param string $item 数字索引时的节点名称 * @param string $id 数字索引key转换为的属性名 * @return string */ function data_to_xml($data, $item='item', $id='id') { $xml = $attr = ''; foreach ($data as $key => $val) { if(is_numeric($key)){ $id && $attr = " {$id}=\"{$key}\""; $key = $item; } $xml .= "<{$key}{$attr}>"; $xml .= (is_array($val) || is_object($val)) ? data_to_xml($val, $item, $id) : $val; $xml .= ""; } return $xml; } /** * session管理函数 * @param string|array $name session名称 如果为数组则表示进行session设置 * @param mixed $value session值 * @return mixed */ function session($name='',$value='') { $prefix = C('SESSION_PREFIX'); if(is_array($name)) { // session初始化 在session_start 之前调用 if(isset($name['prefix'])) C('SESSION_PREFIX',$name['prefix']); if(C('VAR_SESSION_ID') && isset($_REQUEST[C('VAR_SESSION_ID')])){ session_id($_REQUEST[C('VAR_SESSION_ID')]); }elseif(isset($name['id'])) { session_id($name['id']); } if('common' == APP_MODE){ // 其它模式可能不支持 ini_set('session.auto_start', 0); } if(isset($name['name'])) session_name($name['name']); if(isset($name['path'])) session_save_path($name['path']); if(isset($name['domain'])) ini_set('session.cookie_domain', $name['domain']); if(isset($name['expire'])) { ini_set('session.gc_maxlifetime', $name['expire']); ini_set('session.cookie_lifetime', $name['expire']); } if(isset($name['use_trans_sid'])) ini_set('session.use_trans_sid', $name['use_trans_sid']?1:0); if(isset($name['use_cookies'])) ini_set('session.use_cookies', $name['use_cookies']?1:0); if(isset($name['cache_limiter'])) session_cache_limiter($name['cache_limiter']); if(isset($name['cache_expire'])) session_cache_expire($name['cache_expire']); if(isset($name['type'])) C('SESSION_TYPE',$name['type']); if(C('SESSION_TYPE')) { // 读取session驱动 $type = C('SESSION_TYPE'); $class = strpos($type,'\\')? $type : 'Think\\Session\\Driver\\'. ucwords(strtolower($type)); $hander = new $class(); session_set_save_handler( array(&$hander,"open"), array(&$hander,"close"), array(&$hander,"read"), array(&$hander,"write"), array(&$hander,"destroy"), array(&$hander,"gc")); } // 启动session if(C('SESSION_AUTO_START')) session_start(); }elseif('' === $value){ if(''===$name){ // 获取全部的session return $prefix ? $_SESSION[$prefix] : $_SESSION; }elseif(0===strpos($name,'[')) { // session 操作 if('[pause]'==$name){ // 暂停session session_write_close(); }elseif('[start]'==$name){ // 启动session session_start(); }elseif('[destroy]'==$name){ // 销毁session $_SESSION = array(); session_unset(); session_destroy(); }elseif('[regenerate]'==$name){ // 重新生成id session_regenerate_id(); } }elseif(0===strpos($name,'?')){ // 检查session $name = substr($name,1); if(strpos($name,'.')){ // 支持数组 list($name1,$name2) = explode('.',$name); return $prefix?isset($_SESSION[$prefix][$name1][$name2]):isset($_SESSION[$name1][$name2]); }else{ return $prefix?isset($_SESSION[$prefix][$name]):isset($_SESSION[$name]); } }elseif(is_null($name)){ // 清空session if($prefix) { unset($_SESSION[$prefix]); }else{ $_SESSION = array(); } }elseif($prefix){ // 获取session if(strpos($name,'.')){ list($name1,$name2) = explode('.',$name); return isset($_SESSION[$prefix][$name1][$name2])?$_SESSION[$prefix][$name1][$name2]:null; }else{ return isset($_SESSION[$prefix][$name])?$_SESSION[$prefix][$name]:null; } }else{ if(strpos($name,'.')){ list($name1,$name2) = explode('.',$name); return isset($_SESSION[$name1][$name2])?$_SESSION[$name1][$name2]:null; }else{ return isset($_SESSION[$name])?$_SESSION[$name]:null; } } }elseif(is_null($value)){ // 删除session if(strpos($name,'.')){ list($name1,$name2) = explode('.',$name); if($prefix){ unset($_SESSION[$prefix][$name1][$name2]); }else{ unset($_SESSION[$name1][$name2]); } }else{ if($prefix){ unset($_SESSION[$prefix][$name]); }else{ unset($_SESSION[$name]); } } }else{ // 设置session if(strpos($name,'.')){ list($name1,$name2) = explode('.',$name); if($prefix){ $_SESSION[$prefix][$name1][$name2] = $value; }else{ $_SESSION[$name1][$name2] = $value; } }else{ if($prefix){ $_SESSION[$prefix][$name] = $value; }else{ $_SESSION[$name] = $value; } } } return null; } /** * Cookie 设置、获取、删除 * @param string $name cookie名称 * @param mixed $value cookie值 * @param mixed $option cookie参数 * @return mixed */ function cookie($name='', $value='', $option=null) { // 默认设置 $config = array( 'prefix' => C('COOKIE_PREFIX'), // cookie 名称前缀 'expire' => C('COOKIE_EXPIRE'), // cookie 保存时间 'path' => C('COOKIE_PATH'), // cookie 保存路径 'domain' => C('COOKIE_DOMAIN'), // cookie 有效域名 'secure' => C('COOKIE_SECURE'), // cookie 启用安全传输 'httponly' => C('COOKIE_HTTPONLY'), // httponly设置 ); // 参数设置(会覆盖黙认设置) if (!is_null($option)) { if (is_numeric($option)) $option = array('expire' => $option); elseif (is_string($option)) parse_str($option, $option); $config = array_merge($config, array_change_key_case($option)); } if(!empty($config['httponly'])){ ini_set("session.cookie_httponly", 1); } // 清除指定前缀的所有cookie if (is_null($name)) { if (empty($_COOKIE)) return null; // 要删除的cookie前缀,不指定则删除config设置的指定前缀 $prefix = empty($value) ? $config['prefix'] : $value; if (!empty($prefix)) {// 如果前缀为空字符串将不作处理直接返回 foreach ($_COOKIE as $key => $val) { if (0 === stripos($key, $prefix)) { setcookie($key, '', time() - 3600, $config['path'], $config['domain'],$config['secure'],$config['httponly']); unset($_COOKIE[$key]); } } } return null; }elseif('' === $name){ // 获取全部的cookie return $_COOKIE; } $name = $config['prefix'] . str_replace('.', '_', $name); if ('' === $value) { if(isset($_COOKIE[$name])){ $value = $_COOKIE[$name]; if(0===strpos($value,'think:')){ $value = substr($value,6); return array_map('urldecode',json_decode(MAGIC_QUOTES_GPC?stripslashes($value):$value,true)); }else{ return $value; } }else{ return null; } } else { if (is_null($value)) { setcookie($name, '', time() - 3600, $config['path'], $config['domain'],$config['secure'],$config['httponly']); unset($_COOKIE[$name]); // 删除指定cookie } else { // 设置cookie if(is_array($value)){ $value = 'think:'.json_encode(array_map('urlencode',$value)); } $expire = !empty($config['expire']) ? time() + intval($config['expire']) : 0; setcookie($name, $value, $expire, $config['path'], $config['domain'],$config['secure'],$config['httponly']); $_COOKIE[$name] = $value; } } return null; } /** * 加载动态扩展文件 * @var string $path 文件路径 * @return void */ function load_ext_file($path) { // 加载自定义外部文件 if($files = C('LOAD_EXT_FILE')) { $files = explode(',',$files); foreach ($files as $file){ $file = $path.'Common/'.$file.'.php'; if(is_file($file)) include $file; } } // 加载自定义的动态配置文件 if($configs = C('LOAD_EXT_CONFIG')) { if(is_string($configs)) $configs = explode(',',$configs); foreach ($configs as $key=>$config){ $file = is_file($config)? $config : $path.'Conf/'.$config.CONF_EXT; if(is_file($file)) { is_numeric($key)?C(load_config($file)):C($key,load_config($file)); } } } } /** * 获取客户端IP地址 * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字 * @param boolean $adv 是否进行高级模式获取(有可能被伪装) * @return mixed */ function get_client_ip($type = 0,$adv=false) { $type = $type ? 1 : 0; static $ip = NULL; if ($ip !== NULL) return $ip[$type]; if($adv){ if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); $pos = array_search('unknown',$arr); if(false !== $pos) unset($arr[$pos]); $ip = trim($arr[0]); }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; }elseif (isset($_SERVER['REMOTE_ADDR'])) { $ip = $_SERVER['REMOTE_ADDR']; } }elseif (isset($_SERVER['REMOTE_ADDR'])) { $ip = $_SERVER['REMOTE_ADDR']; } // IP地址合法验证 $long = sprintf("%u",ip2long($ip)); $ip = $long ? array($ip, $long) : array('0.0.0.0', 0); return $ip[$type]; } /** * 发送HTTP状态 * @param integer $code 状态码 * @return void */ function send_http_status($code) { static $_status = array( // Informational 1xx 100 => 'Continue', 101 => 'Switching Protocols', // Success 2xx 200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content', // Redirection 3xx 300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Moved Temporarily ', // 1.1 303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy', // 306 is deprecated but reserved 307 => 'Temporary Redirect', // Client Error 4xx 400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Timeout', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Long', 415 => 'Unsupported Media Type', 416 => 'Requested Range Not Satisfiable', 417 => 'Expectation Failed', // Server Error 5xx 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Timeout', 505 => 'HTTP Version Not Supported', 509 => 'Bandwidth Limit Exceeded' ); if(isset($_status[$code])) { header('HTTP/1.1 '.$code.' '.$_status[$code]); // 确保FastCGI模式下正常 header('Status:'.$code.' '.$_status[$code]); } } function think_filter(&$value){ // TODO 其他安全过滤 // 过滤查询特殊字符 if(preg_match('/^(EXP|NEQ|GT|EGT|LT|ELT|OR|XOR|LIKE|NOTLIKE|NOT BETWEEN|NOTBETWEEN|BETWEEN|NOTIN|NOT IN|IN)$/i',$value)){ $value .= ' '; } } // 不区分大小写的in_array实现 function in_array_case($value,$array){ return in_array(strtolower($value),array_map('strtolower',$array)); } ================================================ FILE: ThinkPHP/Conf/convention.php ================================================ // +---------------------------------------------------------------------- /** * ThinkPHP惯例配置文件 * 该文件请不要修改,如果要覆盖惯例配置的值,可在应用配置文件中设定和惯例不符的配置项 * 配置名称大小写任意,系统会统一转换成小写 * 所有配置参数都可以在生效前动态改变 */ defined('THINK_PATH') or exit(); return array( /* 应用设定 */ 'APP_USE_NAMESPACE' => true, // 应用类库是否使用命名空间 'APP_SUB_DOMAIN_DEPLOY' => false, // 是否开启子域名部署 'APP_SUB_DOMAIN_RULES' => array(), // 子域名部署规则 'APP_DOMAIN_SUFFIX' => '', // 域名后缀 如果是com.cn net.cn 之类的后缀必须设置 'ACTION_SUFFIX' => '', // 操作方法后缀 'MULTI_MODULE' => true, // 是否允许多模块 如果为false 则必须设置 DEFAULT_MODULE 'MODULE_DENY_LIST' => array('Common','Runtime'), 'CONTROLLER_LEVEL' => 1, 'APP_AUTOLOAD_LAYER' => 'Controller,Model', // 自动加载的应用类库层 关闭APP_USE_NAMESPACE后有效 'APP_AUTOLOAD_PATH' => '', // 自动加载的路径 关闭APP_USE_NAMESPACE后有效 /* Cookie设置 */ 'COOKIE_EXPIRE' => 0, // Cookie有效期 'COOKIE_DOMAIN' => '', // Cookie有效域名 'COOKIE_PATH' => '/', // Cookie路径 'COOKIE_PREFIX' => '', // Cookie前缀 避免冲突 'COOKIE_SECURE' => false, // Cookie安全传输 'COOKIE_HTTPONLY' => '', // Cookie httponly设置 /* 默认设定 */ 'DEFAULT_M_LAYER' => 'Model', // 默认的模型层名称 'DEFAULT_C_LAYER' => 'Controller', // 默认的控制器层名称 'DEFAULT_V_LAYER' => 'View', // 默认的视图层名称 'DEFAULT_LANG' => 'zh-cn', // 默认语言 'DEFAULT_THEME' => '', // 默认模板主题名称 'DEFAULT_MODULE' => 'Home', // 默认模块 'DEFAULT_CONTROLLER' => 'Index', // 默认控制器名称 'DEFAULT_ACTION' => 'index', // 默认操作名称 'DEFAULT_CHARSET' => 'utf-8', // 默认输出编码 'DEFAULT_TIMEZONE' => 'PRC', // 默认时区 'DEFAULT_AJAX_RETURN' => 'JSON', // 默认AJAX 数据返回格式,可选JSON XML ... 'DEFAULT_JSONP_HANDLER' => 'jsonpReturn', // 默认JSONP格式返回的处理方法 'DEFAULT_FILTER' => 'htmlspecialchars', // 默认参数过滤方法 用于I函数... /* 数据库设置 */ 'DB_TYPE' => '', // 数据库类型 'DB_HOST' => '', // 服务器地址 'DB_NAME' => '', // 数据库名 'DB_USER' => '', // 用户名 'DB_PWD' => '', // 密码 'DB_PORT' => '', // 端口 'DB_PREFIX' => '', // 数据库表前缀 'DB_PARAMS' => array(), // 数据库连接参数 'DB_DEBUG' => TRUE, // 数据库调试模式 开启后可以记录SQL日志 'DB_FIELDS_CACHE' => true, // 启用字段缓存 'DB_CHARSET' => 'utf8', // 数据库编码默认采用utf8 'DB_DEPLOY_TYPE' => 0, // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器) 'DB_RW_SEPARATE' => false, // 数据库读写是否分离 主从式有效 'DB_MASTER_NUM' => 1, // 读写分离后 主服务器数量 'DB_SLAVE_NO' => '', // 指定从服务器序号 /* 数据缓存设置 */ 'DATA_CACHE_TIME' => 0, // 数据缓存有效期 0表示永久缓存 'DATA_CACHE_COMPRESS' => false, // 数据缓存是否压缩缓存 'DATA_CACHE_CHECK' => false, // 数据缓存是否校验缓存 'DATA_CACHE_PREFIX' => '', // 缓存前缀 'DATA_CACHE_TYPE' => 'File', // 数据缓存类型,支持:File|Db|Apc|Memcache|Shmop|Sqlite|Xcache|Apachenote|Eaccelerator 'DATA_CACHE_PATH' => TEMP_PATH,// 缓存路径设置 (仅对File方式缓存有效) 'DATA_CACHE_KEY' => '', // 缓存文件KEY (仅对File方式缓存有效) 'DATA_CACHE_SUBDIR' => false, // 使用子目录缓存 (自动根据缓存标识的哈希创建子目录) 'DATA_PATH_LEVEL' => 1, // 子目录缓存级别 /* 错误设置 */ 'ERROR_MESSAGE' => '页面错误!请稍后再试~',//错误显示信息,非调试模式有效 'ERROR_PAGE' => '', // 错误定向页面 'SHOW_ERROR_MSG' => false, // 显示错误信息 'TRACE_MAX_RECORD' => 100, // 每个级别的错误信息 最大记录数 /* 日志设置 */ 'LOG_RECORD' => false, // 默认不记录日志 'LOG_TYPE' => 'File', // 日志记录类型 默认为文件方式 'LOG_LEVEL' => 'EMERG,ALERT,CRIT,ERR',// 允许记录的日志级别 'LOG_FILE_SIZE' => 2097152, // 日志文件大小限制 'LOG_EXCEPTION_RECORD' => false, // 是否记录异常信息日志 /* SESSION设置 */ 'SESSION_AUTO_START' => true, // 是否自动开启Session 'SESSION_OPTIONS' => array(), // session 配置数组 支持type name id path expire domain 等参数 'SESSION_TYPE' => '', // session hander类型 默认无需设置 除非扩展了session hander驱动 'SESSION_PREFIX' => '', // session 前缀 //'VAR_SESSION_ID' => 'session_id', //sessionID的提交变量 /* 模板引擎设置 */ 'TMPL_CONTENT_TYPE' => 'text/html', // 默认模板输出类型 'TMPL_ACTION_ERROR' => THINK_PATH.'Tpl/dispatch_jump.tpl', // 默认错误跳转对应的模板文件 'TMPL_ACTION_SUCCESS' => THINK_PATH.'Tpl/dispatch_jump.tpl', // 默认成功跳转对应的模板文件 'TMPL_EXCEPTION_FILE' => THINK_PATH.'Tpl/think_exception.tpl',// 异常页面的模板文件 'TMPL_DETECT_THEME' => false, // 自动侦测模板主题 'TMPL_TEMPLATE_SUFFIX' => '.html', // 默认模板文件后缀 'TMPL_FILE_DEPR' => '/', //模板文件CONTROLLER_NAME与ACTION_NAME之间的分割符 // 布局设置 'TMPL_ENGINE_TYPE' => 'Think', // 默认模板引擎 以下设置仅对使用Think模板引擎有效 'TMPL_CACHFILE_SUFFIX' => '.php', // 默认模板缓存后缀 'TMPL_DENY_FUNC_LIST' => 'echo,exit', // 模板引擎禁用函数 'TMPL_DENY_PHP' => false, // 默认模板引擎是否禁用PHP原生代码 'TMPL_L_DELIM' => '{', // 模板引擎普通标签开始标记 'TMPL_R_DELIM' => '}', // 模板引擎普通标签结束标记 'TMPL_VAR_IDENTIFY' => 'array', // 模板变量识别。留空自动判断,参数为'obj'则表示对象 'TMPL_STRIP_SPACE' => true, // 是否去除模板文件里面的html空格与换行 'TMPL_CACHE_ON' => true, // 是否开启模板编译缓存,设为false则每次都会重新编译 'TMPL_CACHE_PREFIX' => '', // 模板缓存前缀标识,可以动态改变 'TMPL_CACHE_TIME' => 0, // 模板缓存有效期 0 为永久,(以数字为值,单位:秒) 'TMPL_LAYOUT_ITEM' => '{__CONTENT__}', // 布局模板的内容替换标识 'LAYOUT_ON' => false, // 是否启用布局 'LAYOUT_NAME' => 'layout', // 当前布局名称 默认为layout // Think模板引擎标签库相关设定 'TAGLIB_BEGIN' => '<', // 标签库标签开始标记 'TAGLIB_END' => '>', // 标签库标签结束标记 'TAGLIB_LOAD' => true, // 是否使用内置标签库之外的其它标签库,默认自动检测 'TAGLIB_BUILD_IN' => 'cx', // 内置标签库名称(标签使用不必指定标签库名称),以逗号分隔 注意解析顺序 'TAGLIB_PRE_LOAD' => '', // 需要额外加载的标签库(须指定标签库名称),多个以逗号分隔 /* URL设置 */ 'URL_CASE_INSENSITIVE' => true, // 默认false 表示URL区分大小写 true则表示不区分大小写 'URL_MODEL' => 1, // URL访问模式,可选参数0、1、2、3,代表以下四种模式: // 0 (普通模式); 1 (PATHINFO 模式); 2 (REWRITE 模式); 3 (兼容模式) 默认为PATHINFO 模式 'URL_PATHINFO_DEPR' => '/', // PATHINFO模式下,各参数之间的分割符号 'URL_PATHINFO_FETCH' => 'ORIG_PATH_INFO,REDIRECT_PATH_INFO,REDIRECT_URL', // 用于兼容判断PATH_INFO 参数的SERVER替代变量列表 'URL_REQUEST_URI' => 'REQUEST_URI', // 获取当前页面地址的系统变量 默认为REQUEST_URI 'URL_HTML_SUFFIX' => 'html', // URL伪静态后缀设置 'URL_DENY_SUFFIX' => 'ico|png|gif|jpg', // URL禁止访问的后缀设置 'URL_PARAMS_BIND' => true, // URL变量绑定到Action方法参数 'URL_PARAMS_BIND_TYPE' => 0, // URL变量绑定的类型 0 按变量名绑定 1 按变量顺序绑定 'URL_PARAMS_FILTER' => false, // URL变量绑定过滤 'URL_PARAMS_FILTER_TYPE'=> '', // URL变量绑定过滤方法 如果为空 调用DEFAULT_FILTER 'URL_ROUTER_ON' => false, // 是否开启URL路由 'URL_ROUTE_RULES' => array(), // 默认路由规则 针对模块 'URL_MAP_RULES' => array(), // URL映射定义规则 /* 系统变量名称设置 */ 'VAR_MODULE' => 'm', // 默认模块获取变量 'VAR_ADDON' => 'addon', // 默认的插件控制器命名空间变量 'VAR_CONTROLLER' => 'c', // 默认控制器获取变量 'VAR_ACTION' => 'a', // 默认操作获取变量 'VAR_AJAX_SUBMIT' => 'ajax', // 默认的AJAX提交变量 'VAR_JSONP_HANDLER' => 'callback', 'VAR_PATHINFO' => 's', // 兼容模式PATHINFO获取变量例如 ?s=/module/action/id/1 后面的参数取决于URL_PATHINFO_DEPR 'VAR_TEMPLATE' => 't', // 默认模板切换变量 'VAR_AUTO_STRING' => false, // 输入变量是否自动强制转换为字符串 如果开启则数组变量需要手动传入变量修饰符获取变量 'HTTP_CACHE_CONTROL' => 'private', // 网页缓存控制 'CHECK_APP_DIR' => true, // 是否检查应用目录是否创建 'FILE_UPLOAD_TYPE' => 'Local', // 文件上传方式 'DATA_CRYPT_TYPE' => 'Think', // 数据加密方式 ); ================================================ FILE: ThinkPHP/Conf/debug.php ================================================ // +---------------------------------------------------------------------- /** * ThinkPHP 默认的调试模式配置文件 */ defined('THINK_PATH') or exit(); // 调试模式下面默认设置 可以在应用配置目录下重新定义 debug.php 覆盖 return array( 'LOG_RECORD' => true, // 进行日志记录 'LOG_EXCEPTION_RECORD' => true, // 是否记录异常信息日志 'LOG_LEVEL' => 'EMERG,ALERT,CRIT,ERR,WARN,NOTIC,INFO,DEBUG,SQL', // 允许记录的日志级别 'DB_FIELDS_CACHE' => false, // 字段缓存信息 'DB_DEBUG' => true, // 开启调试模式 记录SQL日志 'TMPL_CACHE_ON' => false, // 是否开启模板编译缓存,设为false则每次都会重新编译 'TMPL_STRIP_SPACE' => false, // 是否去除模板文件里面的html空格与换行 'SHOW_ERROR_MSG' => true, // 显示错误信息 'URL_CASE_INSENSITIVE' => false, // URL区分大小写 ); ================================================ FILE: ThinkPHP/LICENSE.txt ================================================ ThinkPHP遵循Apache2开源协议发布,并提供免费使用。 版权所有Copyright © 2006-2014 by ThinkPHP (http://thinkphp.cn) All rights reserved。 ThinkPHP® 商标和著作权所有者为上海顶想信息科技有限公司。 Apache Licence是著名的非盈利开源组织Apache采用的协议。 该协议和BSD类似,鼓励代码共享和尊重原作者的著作权, 允许代码修改,再作为开源或商业软件发布。需要满足 的条件: 1. 需要给代码的用户一份Apache Licence ; 2. 如果你修改了代码,需要在被修改的文件中说明; 3. 在延伸的代码中(修改和有源代码衍生的代码中)需要 带有原来代码中的协议,商标,专利声明和其他原来作者规 定需要包含的说明; 4. 如果再发布的产品中包含一个Notice文件,则在Notice文 件中需要带有本协议内容。你可以在Notice中增加自己的 许可,但不可以表现为对Apache Licence构成更改。 具体的协议参考:http://www.apache.org/licenses/LICENSE-2.0 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ================================================ FILE: ThinkPHP/Lang/en-us.php ================================================ // +---------------------------------------------------------------------- /** * ThinkPHP English language package */ return array( /* core language package */ '_MODULE_NOT_EXIST_' => "Module can't be loaded", '_CONTROLLER_NOT_EXIST_' => "Controller can't be loaded", '_ERROR_ACTION_' => 'Illegal Action', '_LANGUAGE_NOT_LOAD_' => "Can't load language package", '_TEMPLATE_NOT_EXIST_' => "Template doesn't exist", '_MODULE_' => 'Module', '_ACTION_' => 'Action', '_MODEL_NOT_EXIST_' => "Model can't be loaded", '_VALID_ACCESS_' => 'No access', '_XML_TAG_ERROR_' => 'XML tag syntax errors', '_DATA_TYPE_INVALID_' => 'Illegal data objects!', '_OPERATION_WRONG_' => 'Operation error occurs', '_NOT_LOAD_DB_' => 'Unable to load the database', '_NO_DB_DRIVER_' => 'Unable to load database driver', '_NOT_SUPPORT_DB_' => 'The system is temporarily not support database', '_NO_DB_CONFIG_' => 'Not define the database configuration', '_NOT_SUPPORT_' => 'The system does not support', '_CACHE_TYPE_INVALID_' => 'Unable to load the cache type', '_FILE_NOT_WRITABLE_' => 'Directory (file) is not writable', '_METHOD_NOT_EXIST_' => 'The method you requested does not exist!', '_CLASS_NOT_EXIST_' => 'Instantiating a class does not exist!', '_CLASS_CONFLICT_' => 'Class name conflicts', '_TEMPLATE_ERROR_' => 'Template Engine errors', '_CACHE_WRITE_ERROR_' => 'Cache file write failed!', '_TAGLIB_NOT_EXIST_' => 'Tag library is not defined', '_OPERATION_FAIL_' => 'Operation failed!', '_OPERATION_SUCCESS_' => 'Operation succeed!', '_SELECT_NOT_EXIST_' => 'Record does not exist!', '_EXPRESS_ERROR_' => 'Expression errors', '_TOKEN_ERROR_' => "Form's token errors", '_RECORD_HAS_UPDATE_' => 'Record has been updated', '_NOT_ALLOW_PHP_' => 'PHP codes are not allowed in the template', '_PARAM_ERROR_' => 'Parameter error or undefined', '_ERROR_QUERY_EXPRESS_' => 'Query express error', ); ================================================ FILE: ThinkPHP/Lang/pt-br.php ================================================ // +---------------------------------------------------------------------- /** * ThinkPHP Portuguese language package */ return array( /* core language package */ '_MODULE_NOT_EXIST_' => "Módulo não pode ser carregado", '_CONTROLLER_NOT_EXIST_' => "Controller não pode ser carregado", '_ERROR_ACTION_' => 'Ação ilegal', '_LANGUAGE_NOT_LOAD_' => "Não é possível carregar pacote da linguagem", '_TEMPLATE_NOT_EXIST_' => "Template não existe", '_MODULE_' => 'Módulo', '_ACTION_' => 'Ação', '_MODEL_NOT_EXIST_' => "Modelo não pode ser carregado", '_VALID_ACCESS_' => 'Sem acesso', '_XML_TAG_ERROR_' => 'Erro de sintaxe - XML tag', '_DATA_TYPE_INVALID_' => 'Tipos de dados ilegais!', '_OPERATION_WRONG_' => 'Erro na operação', '_NOT_LOAD_DB_' => 'Impossível carregar banco de dados', '_NO_DB_DRIVER_' => 'Impossível carregar driver do bando de dados', '_NOT_SUPPORT_DB_' => 'Temporariamente sem suporte ao banco', '_NO_DB_CONFIG_' => 'Não define a configuração do banco', '_NOT_SUPPORT_' => 'O sistema não suporta', '_CACHE_TYPE_INVALID_' => 'Impossível carregar o tipo de cache', '_FILE_NOT_WRITABLE_' => 'Diretório (arquivo) não pode ser escrito', '_METHOD_NOT_EXIST_' => 'O método solicitado não existe!', '_CLASS_NOT_EXIST_' => 'Não existe instância da classe', '_CLASS_CONFLICT_' => 'Conflitos com nome da classe', '_TEMPLATE_ERROR_' => 'Erros na contrução do template', '_CACHE_WRITE_ERROR_' => 'Escrita do arquivo de cache falhou!', '_TAGLIB_NOT_EXIST_' => 'Biblioteca da tag não foi definida', '_OPERATION_FAIL_' => 'Operação falhou!', '_OPERATION_SUCCESS_' => 'Operação bem sucessida!', '_SELECT_NOT_EXIST_' => 'Gravação não existe!', '_EXPRESS_ERROR_' => 'Erros de expressão', '_TOKEN_ERROR_' => 'Erro no token do formulário', '_RECORD_HAS_UPDATE_' => 'Gravação não foi atualizada', '_NOT_ALLOW_PHP_' => 'Código PHP não é permitido no template', '_PARAM_ERROR_' => 'Parâmetro errado ou indefinido', '_ERROR_QUERY_EXPRESS_' => 'Erros na expressão da query', ); ================================================ FILE: ThinkPHP/Lang/zh-cn.php ================================================ // +---------------------------------------------------------------------- /** * ThinkPHP 简体中文语言包 */ return array( /* 核心语言变量 */ '_MODULE_NOT_EXIST_' => '无法加载模块', '_CONTROLLER_NOT_EXIST_' => '无法加载控制器', '_ERROR_ACTION_' => '非法操作', '_LANGUAGE_NOT_LOAD_' => '无法加载语言包', '_TEMPLATE_NOT_EXIST_' => '模板不存在', '_MODULE_' => '模块', '_ACTION_' => '操作', '_MODEL_NOT_EXIST_' => '模型不存在或者没有定义', '_VALID_ACCESS_' => '没有权限', '_XML_TAG_ERROR_' => 'XML标签语法错误', '_DATA_TYPE_INVALID_' => '非法数据对象!', '_OPERATION_WRONG_' => '操作出现错误', '_NOT_LOAD_DB_' => '无法加载数据库', '_NO_DB_DRIVER_' => '无法加载数据库驱动', '_NOT_SUPPORT_DB_' => '系统暂时不支持数据库', '_NO_DB_CONFIG_' => '没有定义数据库配置', '_NOT_SUPPORT_' => '系统不支持', '_CACHE_TYPE_INVALID_' => '无法加载缓存类型', '_FILE_NOT_WRITABLE_' => '目录(文件)不可写', '_METHOD_NOT_EXIST_' => '方法不存在!', '_CLASS_NOT_EXIST_' => '实例化一个不存在的类!', '_CLASS_CONFLICT_' => '类名冲突', '_TEMPLATE_ERROR_' => '模板引擎错误', '_CACHE_WRITE_ERROR_' => '缓存文件写入失败!', '_TAGLIB_NOT_EXIST_' => '标签库未定义', '_OPERATION_FAIL_' => '操作失败!', '_OPERATION_SUCCESS_' => '操作成功!', '_SELECT_NOT_EXIST_' => '记录不存在!', '_EXPRESS_ERROR_' => '表达式错误', '_TOKEN_ERROR_' => '表单令牌错误', '_RECORD_HAS_UPDATE_' => '记录已经更新', '_NOT_ALLOW_PHP_' => '模板禁用PHP代码', '_PARAM_ERROR_' => '参数错误或者未定义', '_ERROR_QUERY_EXPRESS_' => '错误的查询条件', ); ================================================ FILE: ThinkPHP/Lang/zh-tw.php ================================================ // +---------------------------------------------------------------------- /** * ThinkPHP 繁体中文語言包 */ return array( /* 核心語言變數 */ '_MODULE_NOT_EXIST_' => '無法載入模組', '_CONTROLLER_NOT_EXIST_' => '無法載入控制器', '_ERROR_ACTION_' => '非法操作', '_LANGUAGE_NOT_LOAD_' => '無法載入語言包', '_TEMPLATE_NOT_EXIST_' => '模板不存在', '_MODULE_' => '模組', '_ACTION_' => '操作', '_MODEL_NOT_EXIST_' => '模型不存在或者沒有定義', '_VALID_ACCESS_' => '沒有權限', '_XML_TAG_ERROR_' => 'XML標籤語法錯誤', '_DATA_TYPE_INVALID_' => '非法資料物件!', '_OPERATION_WRONG_' => '操作出現錯誤', '_NOT_LOAD_DB_' => '無法載入資料庫', '_NO_DB_DRIVER_' => '無法載入資料庫驅動', '_NOT_SUPPORT_DB_' => '系統暫時不支援資料庫', '_NO_DB_CONFIG_' => '沒有定義資料庫設定', '_NOT_SUPPORT_' => '系統不支援', '_CACHE_TYPE_INVALID_' => '無法載入快取類型', '_FILE_NOT_WRITABLE_' => '目錄(檔案)不可寫', '_METHOD_NOT_EXIST_' => '方法不存在!', '_CLASS_NOT_EXIST_' => '實例化一個不存在的類別!', '_CLASS_CONFLICT_' => '類別名稱衝突', '_TEMPLATE_ERROR_' => '模板引擎錯誤', '_CACHE_WRITE_ERROR_' => '快取檔案寫入失敗!', '_TAGLIB_NOT_EXIST_' => '標籤庫未定義', '_OPERATION_FAIL_' => '操作失敗!', '_OPERATION_SUCCESS_' => '操作成功!', '_SELECT_NOT_EXIST_' => '記錄不存在!', '_EXPRESS_ERROR_' => '運算式錯誤', '_TOKEN_ERROR_' => '表單權限錯誤', '_RECORD_HAS_UPDATE_' => '記錄已經更新', '_NOT_ALLOW_PHP_' => '模板禁用PHP代碼', '_PARAM_ERROR_' => '參數錯誤或者未定義', '_ERROR_QUERY_EXPRESS_' => '錯誤的查詢條件', ); ================================================ FILE: ThinkPHP/Library/Behavior/AgentCheckBehavior.class.php ================================================ // +---------------------------------------------------------------------- namespace Behavior; /** * 行为扩展:代理检测 */ class AgentCheckBehavior { public function run(&$params) { // 代理访问检测 $limitProxyVisit = C('LIMIT_PROXY_VISIT',null,true); if($limitProxyVisit && ($_SERVER['HTTP_X_FORWARDED_FOR'] || $_SERVER['HTTP_VIA'] || $_SERVER['HTTP_PROXY_CONNECTION'] || $_SERVER['HTTP_USER_AGENT_VIA'])) { // 禁止代理访问 exit('Access Denied'); } } } ================================================ FILE: ThinkPHP/Library/Behavior/BorisBehavior.class.php ================================================ // +---------------------------------------------------------------------- namespace Behavior; use Think\Think; /** * Boris行为扩展 */ class BorisBehavior { public function run(&$params) { if(IS_CLI){ if(!function_exists('pcntl_signal')) E("pcntl_signal not working.\nRepl mode based on Linux OS or PHP for OS X(http://php-osx.liip.ch/)\n"); Think::addMap(array( 'Boris\Boris' => VENDOR_PATH . 'Boris/Boris.php', 'Boris\Config' => VENDOR_PATH . 'Boris/Config.php', 'Boris\CLIOptionsHandler' => VENDOR_PATH . 'Boris/CLIOptionsHandler.php', 'Boris\ColoredInspector' => VENDOR_PATH . 'Boris/ColoredInspector.php', 'Boris\DumpInspector' => VENDOR_PATH . 'Boris/DumpInspector.php', 'Boris\EvalWorker' => VENDOR_PATH . 'Boris/EvalWorker.php', 'Boris\ExportInspector' => VENDOR_PATH . 'Boris/ExportInspector.php', 'Boris\Inspector' => VENDOR_PATH . 'Boris/Inspector.php', 'Boris\ReadlineClient' => VENDOR_PATH . 'Boris/ReadlineClient.php', 'Boris\ShallowParser' => VENDOR_PATH . 'Boris/ShallowParser.php', )); $boris = new \Boris\Boris(">>> "); $config = new \Boris\Config(); $config->apply($boris, true); $options = new \Boris\CLIOptionsHandler(); $options->handle($boris); $boris->onStart(sprintf("echo 'REPL MODE FOR THINKPHP \nTHINKPHP_VERSION: %s, PHP_VERSION: %s, BORIS_VERSION: %s\n';", THINK_VERSION, PHP_VERSION, $boris::VERSION)); $boris->start(); } } } ================================================ FILE: ThinkPHP/Library/Behavior/BrowserCheckBehavior.class.php ================================================ // +---------------------------------------------------------------------- namespace Behavior; /** * 浏览器防刷新检测 */ class BrowserCheckBehavior { public function run(&$params) { if($_SERVER['REQUEST_METHOD'] == 'GET') { // 启用页面防刷新机制 $guid = md5($_SERVER['PHP_SELF']); // 浏览器防刷新的时间间隔(秒) 默认为10 $refleshTime = C('LIMIT_REFLESH_TIMES',null,10); // 检查页面刷新间隔 if(cookie('_last_visit_time_'.$guid) && cookie('_last_visit_time_'.$guid)>time()-$refleshTime) { // 页面刷新读取浏览器缓存 header('HTTP/1.1 304 Not Modified'); exit; }else{ // 缓存当前地址访问时间 cookie('_last_visit_time_'.$guid, $_SERVER['REQUEST_TIME']); //header('Last-Modified:'.(date('D,d M Y H:i:s',$_SERVER['REQUEST_TIME']-C('LIMIT_REFLESH_TIMES'))).' GMT'); } } } } ================================================ FILE: ThinkPHP/Library/Behavior/BuildLiteBehavior.class.php ================================================ // +---------------------------------------------------------------------- namespace Behavior; // 创建Lite运行文件 // 可以替换框架入口文件运行 // 建议绑定位置app_init class BuildLiteBehavior { public function run(&$params) { if(!defined('BUILD_LITE_FILE')) return ; $litefile = C('RUNTIME_LITE_FILE',null,RUNTIME_PATH.'lite.php'); if(is_file($litefile)) return; $defs = get_defined_constants(TRUE); $content = 'namespace {$GLOBALS[\'_beginTime\'] = microtime(TRUE);'; if(MEMORY_LIMIT_ON) { $content .= '$GLOBALS[\'_startUseMems\'] = memory_get_usage();'; } // 生成数组定义 unset($defs['user']['BUILD_LITE_FILE']); $content .= $this->buildArrayDefine($defs['user']).'}'; // 读取编译列表文件 $filelist = is_file(CONF_PATH.'lite.php')? include CONF_PATH.'lite.php': array( THINK_PATH.'Common/functions.php', COMMON_PATH.'Common/function.php', CORE_PATH . 'Think'.EXT, CORE_PATH . 'Hook'.EXT, CORE_PATH . 'App'.EXT, CORE_PATH . 'Dispatcher'.EXT, CORE_PATH . 'Log'.EXT, CORE_PATH . 'Log/Driver/File'.EXT, CORE_PATH . 'Route'.EXT, CORE_PATH . 'Controller'.EXT, CORE_PATH . 'View'.EXT, CORE_PATH . 'Storage'.EXT, CORE_PATH . 'Storage/Driver/File'.EXT, CORE_PATH . 'Exception'.EXT, BEHAVIOR_PATH . 'ParseTemplateBehavior'.EXT, BEHAVIOR_PATH . 'ContentReplaceBehavior'.EXT, ); // 编译文件 foreach ($filelist as $file){ if(is_file($file)) { $content .= compile($file); } } // 处理Think类的start方法 $content = preg_replace('/\$runtimefile = RUNTIME_PATH(.+?)(if\(APP_STATUS)/','\2',$content,1); $content .= "\nnamespace { Think\Think::addMap(".var_export(\Think\Think::getMap(),true).");"; $content .= "\nL(".var_export(L(),true).");\nC(".var_export(C(),true).');Think\Hook::import('.var_export(\Think\Hook::get(),true).');Think\Think::start();}'; // 生成运行Lite文件 file_put_contents($litefile,strip_whitespace(' $val) { $key = strtoupper($key); $content .= 'defined(\'' . $key . '\') or '; if (is_int($val) || is_float($val)) { $content .= "define('" . $key . "'," . $val . ');'; } elseif (is_bool($val)) { $val = ($val) ? 'true' : 'false'; $content .= "define('" . $key . "'," . $val . ');'; } elseif (is_string($val)) { $content .= "define('" . $key . "','" . addslashes($val) . "');"; } $content .= "\n"; } return $content; } } ================================================ FILE: ThinkPHP/Library/Behavior/CheckActionRouteBehavior.class.php ================================================ // +---------------------------------------------------------------------- namespace Behavior; /** * 系统行为扩展:操作路由检测 */ class CheckActionRouteBehavior { // 行为扩展的执行入口必须是run public function run(&$config){ // 优先检测是否存在PATH_INFO $regx = trim($_SERVER['PATH_INFO'],'/'); if(empty($regx)) return ; // 路由定义文件优先于config中的配置定义 // 路由处理 $routes = $config['routes']; if(!empty($routes)) { $depr = C('URL_PATHINFO_DEPR'); // 分隔符替换 确保路由定义使用统一的分隔符 $regx = str_replace($depr,'/',$regx); $regx = substr_replace($regx,'',0,strlen(__URL__)); foreach ($routes as $rule=>$route){ if(0===strpos($rule,'/') && preg_match($rule,$regx,$matches)) { // 正则路由 return C('ACTION_NAME',$this->parseRegex($matches,$route,$regx)); }else{ // 规则路由 $len1 = substr_count($regx,'/'); $len2 = substr_count($rule,'/'); if($len1>=$len2) { if('$' == substr($rule,-1,1)) {// 完整匹配 if($len1 != $len2) { continue; }else{ $rule = substr($rule,0,-1); } } $match = $this->checkUrlMatch($regx,$rule); if($match) return C('ACTION_NAME',$this->parseRule($rule,$route,$regx)); } } } } } // 检测URL和规则路由是否匹配 private function checkUrlMatch($regx,$rule) { $m1 = explode('/',$regx); $m2 = explode('/',$rule); $match = true; // 是否匹配 foreach ($m2 as $key=>$val){ if(':' == substr($val,0,1)) {// 动态变量 if(strpos($val,'\\')) { $type = substr($val,-1); if('d'==$type && !is_numeric($m1[$key])) { $match = false; break; } }elseif(strpos($val,'^')){ $array = explode('|',substr(strstr($val,'^'),1)); if(in_array($m1[$key],$array)) { $match = false; break; } } }elseif(0 !== strcasecmp($val,$m1[$key])){ $match = false; break; } } return $match; } // 解析规范的路由地址 // 地址格式 操作?参数1=值1&参数2=值2... private function parseUrl($url) { $var = array(); if(false !== strpos($url,'?')) { // 操作?参数1=值1&参数2=值2... $info = parse_url($url); $path = $info['path']; parse_str($info['query'],$var); }else{ // 操作 $path = $url; } $var[C('VAR_ACTION')] = $path; return $var; } // 解析规则路由 // '路由规则'=>'操作?额外参数1=值1&额外参数2=值2...' // '路由规则'=>array('操作','额外参数1=值1&额外参数2=值2...') // '路由规则'=>'外部地址' // '路由规则'=>array('外部地址','重定向代码') // 路由规则中 :开头 表示动态变量 // 外部地址中可以用动态变量 采用 :1 :2 的方式 // 'news/:month/:day/:id'=>array('News/read?cate=1','status=1'), // 'new/:id'=>array('/new.php?id=:1',301), 重定向 private function parseRule($rule,$route,$regx) { // 获取路由地址规则 $url = is_array($route)?$route[0]:$route; // 获取URL地址中的参数 $paths = explode('/',$regx); // 解析路由规则 $matches = array(); $rule = explode('/',$rule); foreach ($rule as $item){ if(0===strpos($item,':')) { // 动态变量获取 if($pos = strpos($item,'^') ) { $var = substr($item,1,$pos-1); }elseif(strpos($item,'\\')){ $var = substr($item,1,-2); }else{ $var = substr($item,1); } $matches[$var] = array_shift($paths); }else{ // 过滤URL中的静态变量 array_shift($paths); } } if(0=== strpos($url,'/') || 0===strpos($url,'http')) { // 路由重定向跳转 if(strpos($url,':')) { // 传递动态参数 $values = array_values($matches); $url = preg_replace('/:(\d+)/e','$values[\\1-1]',$url); } header("Location: $url", true,(is_array($route) && isset($route[1]))?$route[1]:301); exit; }else{ // 解析路由地址 $var = $this->parseUrl($url); // 解析路由地址里面的动态参数 $values = array_values($matches); foreach ($var as $key=>$val){ if(0===strpos($val,':')) { $var[$key] = $values[substr($val,1)-1]; } } $var = array_merge($matches,$var); // 解析剩余的URL参数 if($paths) { preg_replace('@(\w+)\/([^\/]+)@e', '$var[strtolower(\'\\1\')]=strip_tags(\'\\2\');', implode('/',$paths)); } // 解析路由自动传入参数 if(is_array($route) && isset($route[1])) { parse_str($route[1],$params); $var = array_merge($var,$params); } $action = $var[C('VAR_ACTION')]; unset($var[C('VAR_ACTION')]); $_GET = array_merge($var,$_GET); return $action; } } // 解析正则路由 // '路由正则'=>'[分组/模块/操作]?参数1=值1&参数2=值2...' // '路由正则'=>array('[分组/模块/操作]?参数1=值1&参数2=值2...','额外参数1=值1&额外参数2=值2...') // '路由正则'=>'外部地址' // '路由正则'=>array('外部地址','重定向代码') // 参数值和外部地址中可以用动态变量 采用 :1 :2 的方式 // '/new\/(\d+)\/(\d+)/'=>array('News/read?id=:1&page=:2&cate=1','status=1'), // '/new\/(\d+)/'=>array('/new.php?id=:1&page=:2&status=1','301'), 重定向 private function parseRegex($matches,$route,$regx) { // 获取路由地址规则 $url = is_array($route)?$route[0]:$route; $url = preg_replace('/:(\d+)/e','$matches[\\1]',$url); if(0=== strpos($url,'/') || 0===strpos($url,'http')) { // 路由重定向跳转 header("Location: $url", true,(is_array($route) && isset($route[1]))?$route[1]:301); exit; }else{ // 解析路由地址 $var = $this->parseUrl($url); // 解析剩余的URL参数 $regx = substr_replace($regx,'',0,strlen($matches[0])); if($regx) { preg_replace('@(\w+)\/([^,\/]+)@e', '$var[strtolower(\'\\1\')]=strip_tags(\'\\2\');', $regx); } // 解析路由自动传入参数 if(is_array($route) && isset($route[1])) { parse_str($route[1],$params); $var = array_merge($var,$params); } $action = $var[C('VAR_ACTION')]; unset($var[C('VAR_ACTION')]); $_GET = array_merge($var,$_GET); } return $action; } } ================================================ FILE: ThinkPHP/Library/Behavior/CheckLangBehavior.class.php ================================================ // +---------------------------------------------------------------------- namespace Behavior; /** * 语言检测 并自动加载语言包 */ class CheckLangBehavior { // 行为扩展的执行入口必须是run public function run(&$params){ // 检测语言 $this->checkLanguage(); } /** * 语言检查 * 检查浏览器支持语言,并自动加载语言包 * @access private * @return void */ private function checkLanguage() { // 不开启语言包功能,仅仅加载框架语言文件直接返回 if (!C('LANG_SWITCH_ON',null,false)){ return; } $langSet = C('DEFAULT_LANG'); $varLang = C('VAR_LANGUAGE',null,'l'); $langList = C('LANG_LIST',null,'zh-cn'); // 启用了语言包功能 // 根据是否启用自动侦测设置获取语言选择 if (C('LANG_AUTO_DETECT',null,true)){ if(isset($_GET[$varLang])){ $langSet = $_GET[$varLang];// url中设置了语言变量 cookie('think_language',$langSet,3600); }elseif(cookie('think_language')){// 获取上次用户的选择 $langSet = cookie('think_language'); }elseif(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){// 自动侦测浏览器语言 preg_match('/^([a-z\d\-]+)/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches); $langSet = $matches[1]; cookie('think_language',$langSet,3600); } if(false === stripos($langList,$langSet)) { // 非法语言参数 $langSet = C('DEFAULT_LANG'); } } // 定义当前语言 define('LANG_SET',strtolower($langSet)); // 读取框架语言包 $file = THINK_PATH.'Lang/'.LANG_SET.'.php'; if(LANG_SET != C('DEFAULT_LANG') && is_file($file)) L(include $file); // 读取应用公共语言包 $file = LANG_PATH.LANG_SET.'.php'; if(is_file($file)) L(include $file); // 读取模块语言包 $file = MODULE_PATH.'Lang/'.LANG_SET.'.php'; if(is_file($file)) L(include $file); // 读取当前控制器语言包 $file = MODULE_PATH.'Lang/'.LANG_SET.'/'.strtolower(CONTROLLER_NAME).'.php'; if (is_file($file)) L(include $file); } } ================================================ FILE: ThinkPHP/Library/Behavior/ChromeShowPageTraceBehavior.class.php ================================================ // +---------------------------------------------------------------------- // $Id$ /** * 将Trace信息输出到chrome浏览器的控制器,从而不影响ajax效果和页面的布局。 * 使用前,你需要先安装 chrome log 这个插件: http://craig.is/writing/chrome-logger。 * 定义应用的tags.php文件 Application/Common/Conf/tags.php, * * array( * 'Behavior\ChromeShowPageTrace' * ) * ); * * 如果trace信息没有正常输出,请查看您的日志。 * 这是通过http headers和chrome通信,所以要保证在输出trace信息之前不能有 * headers输出,你可以在入口文件第一行加入代码 ob_start(); 或者配置output_buffering * */ namespace Behavior; use Think\Log; /** * 系统行为扩展 页面Trace显示输出 */ class ChromeShowPageTraceBehavior { protected $tracePageTabs = array('BASE'=>'基本','FILE'=>'文件','INFO'=>'流程','ERR|NOTIC'=>'错误','SQL'=>'SQL','DEBUG'=>'调试'); // 行为扩展的执行入口必须是run public function run(&$params){ if(C('SHOW_PAGE_TRACE')) $this->showTrace(); } /** * 显示页面Trace信息 * @access private */ private function showTrace() { // 系统默认显示信息 $files = get_included_files(); $info = array(); foreach ($files as $key=>$file){ $info[] = $file.' ( '.number_format(filesize($file)/1024,2).' KB )'; } $trace = array(); $base = array( '请求信息' => date('Y-m-d H:i:s',$_SERVER['REQUEST_TIME']).' '.$_SERVER['SERVER_PROTOCOL'].' '.$_SERVER['REQUEST_METHOD'].' : '.__SELF__, '运行时间' => $this->showTime(), '吞吐率' => number_format(1/G('beginTime','viewEndTime'),2).'req/s', '内存开销' => MEMORY_LIMIT_ON?number_format((memory_get_usage() - $GLOBALS['_startUseMems'])/1024,2).' kb':'不支持', '查询信息' => N('db_query').' queries '.N('db_write').' writes ', '文件加载' => count(get_included_files()), '缓存信息' => N('cache_read').' gets '.N('cache_write').' writes ', '配置加载' => count(c()), '会话信息' => 'SESSION_ID='.session_id(), ); // 读取应用定义的Trace文件 $traceFile = COMMON_PATH.'Conf/trace.php'; if(is_file($traceFile)) { $base = array_merge($base,include $traceFile); } $debug = trace(); $tabs = C('TRACE_PAGE_TABS',null,$this->tracePageTabs); foreach ($tabs as $name=>$title){ switch(strtoupper($name)) { case 'BASE':// 基本信息 $trace[$title] = $base; break; case 'FILE': // 文件信息 $trace[$title] = $info; break; default:// 调试信息 $name = strtoupper($name); if(strpos($name,'|')) {// 多组信息 $array = explode('|',$name); $result = array(); foreach($array as $name){ $result += isset($debug[$name])?$debug[$name]:array(); } $trace[$title] = $result; }else{ $trace[$title] = isset($debug[$name])?$debug[$name]:''; } } } chrome_debug('TRACE信息:'.__SELF__,'group'); //输出日志 foreach($trace as $title=>$log){ '错误'==$title?chrome_debug($title,'group'):chrome_debug($title,'groupCollapsed'); foreach($log as $i=>$logstr){ chrome_debug($i.'.'.$logstr,'log'); } chrome_debug('','groupEnd'); } chrome_debug('','groupEnd'); if($save = C('PAGE_TRACE_SAVE')) { // 保存页面Trace日志 if(is_array($save)) {// 选择选项卡保存 $tabs = C('TRACE_PAGE_TABS',null,$this->tracePageTabs); $array = array(); foreach ($save as $tab){ $array[] = $tabs[$tab]; } } $content = date('[ c ]').' '.get_client_ip().' '.$_SERVER['REQUEST_URI']."\r\n"; foreach ($trace as $key=>$val){ if(!isset($array) || in_array($key,$array)) { $content .= '[ '.$key." ]\r\n"; if(is_array($val)) { foreach ($val as $k=>$v){ $content .= (!is_numeric($k)?$k.':':'').print_r($v,true)."\r\n"; } }else{ $content .= print_r($val,true)."\r\n"; } $content .= "\r\n"; } } error_log(str_replace('
',"\r\n",$content), 3,LOG_PATH.date('y_m_d').'_trace.log'); } unset($files,$info,$base); } /** * 获取运行时间 */ private function showTime() { // 显示运行时间 G('beginTime',$GLOBALS['_beginTime']); G('viewEndTime'); // 显示详细运行时间 return G('beginTime','viewEndTime').'s ( Load:'.G('beginTime','loadTime').'s Init:'.G('loadTime','initTime').'s Exec:'.G('initTime','viewStartTime').'s Template:'.G('viewStartTime','viewEndTime').'s )'; } } if(!function_exists('chrome_debug')){ //ChromePhp 输出trace的函数 function chrome_debug($msg,$type='trace',$trace_level=1){ if('trace'==$type){ ChromePhp::groupCollapsed($msg); $traces=debug_backtrace(false); $traces=array_reverse($traces); $max=count($traces)-$trace_level; for($i=0;$i<$max;$i++){ $trace=$traces[$i]; $fun=isset($trace['class'])?$trace['class'].'::'.$trace['function']:$trace['function']; $file=isset($trace['file'])?$trace['file']:'unknown file'; $line=isset($trace['line'])?$trace['line']:'unknown line'; $trace_msg='#'.$i.' '.$fun.' called at ['.$file.':'.$line.']'; if(!empty($trace['args'])){ ChromePhp::groupCollapsed($trace_msg); ChromePhp::log($trace['args']); ChromePhp::groupEnd(); }else{ ChromePhp::log($trace_msg); } } ChromePhp::groupEnd(); }else{ if(method_exists('Behavior\ChromePhp',$type)){ //支持type trace,warn,log,error,group, groupCollapsed, groupEnd等 call_user_func(array('Behavior\ChromePhp',$type),$msg); }else{ //如果type不为trace,warn,log等,则为log的标签 call_user_func_array(array('Behavior\ChromePhp','log'),func_get_args()); } } } /** * Server Side Chrome PHP debugger class * * @package ChromePhp * @author Craig Campbell */ class ChromePhp{ /** * @var string */ const VERSION = '4.1.0'; /** * @var string */ const HEADER_NAME = 'X-ChromeLogger-Data'; /** * @var string */ const BACKTRACE_LEVEL = 'backtrace_level'; /** * @var string */ const LOG = 'log'; /** * @var string */ const WARN = 'warn'; /** * @var string */ const ERROR = 'error'; /** * @var string */ const GROUP = 'group'; /** * @var string */ const INFO = 'info'; /** * @var string */ const GROUP_END = 'groupEnd'; /** * @var string */ const GROUP_COLLAPSED = 'groupCollapsed'; /** * @var string */ const TABLE = 'table'; /** * @var string */ protected $_php_version; /** * @var int */ protected $_timestamp; /** * @var array */ protected $_json = array( 'version' => self::VERSION, 'columns' => array('log', 'backtrace', 'type'), 'rows' => array() ); /** * @var array */ protected $_backtraces = array(); /** * @var bool */ protected $_error_triggered = false; /** * @var array */ protected $_settings = array( self::BACKTRACE_LEVEL => 1 ); /** * @var ChromePhp */ protected static $_instance; /** * Prevent recursion when working with objects referring to each other * * @var array */ protected $_processed = array(); /** * constructor */ private function __construct() { $this->_php_version = phpversion(); $this->_timestamp = $this->_php_version >= 5.1 ? $_SERVER['REQUEST_TIME'] : time(); $this->_json['request_uri'] = $_SERVER['REQUEST_URI']; } /** * gets instance of this class * * @return ChromePhp */ public static function getInstance() { if (self::$_instance === null) { self::$_instance = new self(); } return self::$_instance; } /** * logs a variable to the console * * @param mixed $data,... unlimited OPTIONAL number of additional logs [...] * @return void */ public static function log() { $args = func_get_args(); return self::_log('', $args); } /** * logs a warning to the console * * @param mixed $data,... unlimited OPTIONAL number of additional logs [...] * @return void */ public static function warn() { $args = func_get_args(); return self::_log(self::WARN, $args); } /** * logs an error to the console * * @param mixed $data,... unlimited OPTIONAL number of additional logs [...] * @return void */ public static function error() { $args = func_get_args(); return self::_log(self::ERROR, $args); } /** * sends a group log * * @param string value */ public static function group() { $args = func_get_args(); return self::_log(self::GROUP, $args); } /** * sends an info log * * @param mixed $data,... unlimited OPTIONAL number of additional logs [...] * @return void */ public static function info() { $args = func_get_args(); return self::_log(self::INFO, $args); } /** * sends a collapsed group log * * @param string value */ public static function groupCollapsed() { $args = func_get_args(); return self::_log(self::GROUP_COLLAPSED, $args); } /** * ends a group log * * @param string value */ public static function groupEnd() { $args = func_get_args(); return self::_log(self::GROUP_END, $args); } /** * sends a table log * * @param string value */ public static function table() { $args = func_get_args(); return self::_log(self::TABLE, $args); } /** * internal logging call * * @param string $type * @return void */ protected static function _log($type, array $args) { // nothing passed in, don't do anything if (count($args) == 0 && $type != self::GROUP_END) { return; } $logger = self::getInstance(); $logger->_processed = array(); $logs = array(); foreach ($args as $arg) { $logs[] = $logger->_convert($arg); } $backtrace = debug_backtrace(false); $level = $logger->getSetting(self::BACKTRACE_LEVEL); $backtrace_message = 'unknown'; if (isset($backtrace[$level]['file']) && isset($backtrace[$level]['line'])) { $backtrace_message = $backtrace[$level]['file'] . ' : ' . $backtrace[$level]['line']; } $logger->_addRow($logs, $backtrace_message, $type); } /** * converts an object to a better format for logging * * @param Object * @return array */ protected function _convert($object) { // if this isn't an object then just return it if (!is_object($object)) { return $object; } //Mark this object as processed so we don't convert it twice and it //Also avoid recursion when objects refer to each other $this->_processed[] = $object; $object_as_array = array(); // first add the class name $object_as_array['___class_name'] = get_class($object); // loop through object vars $object_vars = get_object_vars($object); foreach ($object_vars as $key => $value) { // same instance as parent object if ($value === $object || in_array($value, $this->_processed, true)) { $value = 'recursion - parent object [' . get_class($value) . ']'; } $object_as_array[$key] = $this->_convert($value); } $reflection = new ReflectionClass($object); // loop through the properties and add those foreach ($reflection->getProperties() as $property) { // if one of these properties was already added above then ignore it if (array_key_exists($property->getName(), $object_vars)) { continue; } $type = $this->_getPropertyKey($property); if ($this->_php_version >= 5.3) { $property->setAccessible(true); } try { $value = $property->getValue($object); } catch (ReflectionException $e) { $value = 'only PHP 5.3 can access private/protected properties'; } // same instance as parent object if ($value === $object || in_array($value, $this->_processed, true)) { $value = 'recursion - parent object [' . get_class($value) . ']'; } $object_as_array[$type] = $this->_convert($value); } return $object_as_array; } /** * takes a reflection property and returns a nicely formatted key of the property name * * @param ReflectionProperty * @return string */ protected function _getPropertyKey(ReflectionProperty $property) { $static = $property->isStatic() ? ' static' : ''; if ($property->isPublic()) { return 'public' . $static . ' ' . $property->getName(); } if ($property->isProtected()) { return 'protected' . $static . ' ' . $property->getName(); } if ($property->isPrivate()) { return 'private' . $static . ' ' . $property->getName(); } } /** * adds a value to the data array * * @var mixed * @return void */ protected function _addRow(array $logs, $backtrace, $type) { // if this is logged on the same line for example in a loop, set it to null to save space if (in_array($backtrace, $this->_backtraces)) { $backtrace = null; } // for group, groupEnd, and groupCollapsed // take out the backtrace since it is not useful if ($type == self::GROUP || $type == self::GROUP_END || $type == self::GROUP_COLLAPSED) { $backtrace = null; } if ($backtrace !== null) { $this->_backtraces[] = $backtrace; } $row = array($logs, $backtrace, $type); $this->_json['rows'][] = $row; $this->_writeHeader($this->_json); } protected function _writeHeader($data) { header(self::HEADER_NAME . ': ' . $this->_encode($data)); } /** * encodes the data to be sent along with the request * * @param array $data * @return string */ protected function _encode($data) { return base64_encode(utf8_encode(json_encode($data))); } /** * adds a setting * * @param string key * @param mixed value * @return void */ public function addSetting($key, $value) { $this->_settings[$key] = $value; } /** * add ability to set multiple settings in one call * * @param array $settings * @return void */ public function addSettings(array $settings) { foreach ($settings as $key => $value) { $this->addSetting($key, $value); } } /** * gets a setting * * @param string key * @return mixed */ public function getSetting($key) { if (!isset($this->_settings[$key])) { return null; } return $this->_settings[$key]; } } } ================================================ FILE: ThinkPHP/Library/Behavior/ContentReplaceBehavior.class.php ================================================ // +---------------------------------------------------------------------- namespace Behavior; /** * 系统行为扩展:模板内容输出替换 */ class ContentReplaceBehavior { // 行为扩展的执行入口必须是run public function run(&$content){ $content = $this->templateContentReplace($content); } /** * 模板内容替换 * @access protected * @param string $content 模板内容 * @return string */ protected function templateContentReplace($content) { // 系统默认的特殊变量替换 $replace = array( '__ROOT__' => __ROOT__, // 当前网站地址 '__APP__' => __APP__, // 当前应用地址 '__MODULE__' => __MODULE__, '__ACTION__' => __ACTION__, // 当前操作地址 '__SELF__' => __SELF__, // 当前页面地址 '__CONTROLLER__'=> __CONTROLLER__, '__URL__' => __CONTROLLER__, '__PUBLIC__' => __ROOT__.'/Public',// 站点公共目录 ); // 允许用户自定义模板的字符串替换 if(is_array(C('TMPL_PARSE_STRING')) ) $replace = array_merge($replace,C('TMPL_PARSE_STRING')); $content = str_replace(array_keys($replace),array_values($replace),$content); return $content; } } ================================================ FILE: ThinkPHP/Library/Behavior/CronRunBehavior.class.php ================================================ // +---------------------------------------------------------------------- namespace Behavior; /** * 自动执行任务 */ class CronRunBehavior { public function run(&$params) { // 锁定自动执行 $lockfile = RUNTIME_PATH.'cron.lock'; if(is_writable($lockfile) && filemtime($lockfile) > $_SERVER['REQUEST_TIME'] - C('CRON_MAX_TIME',null,60)) { return ; } else { touch($lockfile); } set_time_limit(1000); ignore_user_abort(true); // 载入cron配置文件 // 格式 return array( // 'cronname'=>array('filename',intervals,nextruntime),... // ); if(is_file(RUNTIME_PATH.'~crons.php')) { $crons = include RUNTIME_PATH.'~crons.php'; }elseif(is_file(COMMON_PATH.'Conf/crons.php')){ $crons = include COMMON_PATH.'Conf/crons.php'; } if(isset($crons) && is_array($crons)) { $update = false; $log = array(); foreach ($crons as $key=>$cron){ if(empty($cron[2]) || $_SERVER['REQUEST_TIME']>=$cron[2]) { // 到达时间 执行cron文件 G('cronStart'); include COMMON_PATH.'Cron/'.$cron[0].'.php'; G('cronEnd'); $_useTime = G('cronStart','cronEnd', 6); // 更新cron记录 $cron[2] = $_SERVER['REQUEST_TIME']+$cron[1]; $crons[$key] = $cron; $log[] = "Cron:$key Runat ".date('Y-m-d H:i:s')." Use $_useTime s\n"; $update = true; } } if($update) { // 记录Cron执行日志 \Think\Log::write(implode('',$log)); // 更新cron文件 $content = ""; file_put_contents(RUNTIME_PATH.'~crons.php',$content); } } // 解除锁定 unlink($lockfile); return ; } } ================================================ FILE: ThinkPHP/Library/Behavior/FireShowPageTraceBehavior.class.php ================================================ // +---------------------------------------------------------------------- // $Id$ /** * 将Trace信息输出到火狐的firebug,从而不影响ajax效果和页面的布局。 * 使用前,你需要先在火狐浏览器上安装firebug和firePHP两个插件。 * 定义应用的tags.php文件, * * array( * 'FireShowPageTrace' * ) * ); * * 再将此文件放到应用的Behavior文件夹中即可 * 如果trace信息没有正常输出,请查看您的日志。 * firePHP,是通过http headers和firebug通讯的,所以要保证在输出trace信息之前不能有 * headers输出,你可以在入口文件第一行加入代码 ob_start(); 或者配置output_buffering * */ namespace Behavior; /** * 系统行为扩展 页面Trace显示输出 */ class FireShowPageTraceBehavior { protected $tracePagTabs = array('BASE'=>'基本','FILE'=>'文件','INFO'=>'流程','ERR|NOTIC'=>'错误','SQL'=>'SQL','DEBUG'=>'调试'); // 行为扩展的执行入口必须是run public function run(&$params){ if(C('FIRE_SHOW_PAGE_TRACE',null,true)) $this->showTrace(); } /** * 显示页面Trace信息 * @access private */ private function showTrace() { // 系统默认显示信息 $files = get_included_files(); $info = array(); foreach ($files as $key=>$file){ $info[] = $file.' ( '.number_format(filesize($file)/1024,2).' KB )'; } $trace = array(); $base = array( '请求信息'=> date('Y-m-d H:i:s',$_SERVER['REQUEST_TIME']).' '.$_SERVER['SERVER_PROTOCOL'].' '.$_SERVER['REQUEST_METHOD'].' : '.__SELF__, '运行时间'=> $this->showTime(), '内存开销'=> MEMORY_LIMIT_ON?number_format((memory_get_usage() - $GLOBALS['_startUseMems'])/1024,2).' kb':'不支持', '查询信息'=> N('db_query').' queries '.N('db_write').' writes ', '文件加载'=> count(get_included_files()), '缓存信息'=> N('cache_read').' gets '.N('cache_write').' writes ', '配置加载'=> count(c()), '会话信息'=> 'SESSION_ID='.session_id(), ); // 读取应用定义的Trace文件 $traceFile = CONF_PATH.'trace.php'; if(is_file($traceFile)) { $base = array_merge($base,include $traceFile); } $debug = trace(); $tabs = C('TRACE_PAGE_TABS',null,$this->tracePagTabs); foreach ($tabs as $name=>$title){ switch(strtoupper($name)) { case 'BASE':// 基本信息 $trace[$title] = $base; break; case 'FILE': // 文件信息 $trace[$title] = $info; break; default:// 调试信息 if(strpos($name,'|')) {// 多组信息 $array = explode('|',$name); $result = array(); foreach($array as $name){ $result += isset($debug[$name])?$debug[$name]:array(); } $trace[$title] = $result; }else{ $trace[$title] = isset($debug[$name])?$debug[$name]:''; } } } foreach ($trace as $key=>$val){ if(!is_array($val) && empty($val)) $val=array(); if(is_array($val)){ $fire=array( array('','') ); foreach($val as $k=>$v){ $fire[]=array($k,$v); } fb(array($key,$fire),FirePHP::TABLE); }else{ fb($val,$key); } } unset($files,$info,$log,$base); } /** * 获取运行时间 */ private function showTime() { // 显示运行时间 G('beginTime',$GLOBALS['_beginTime']); G('viewEndTime'); // 显示详细运行时间 return G('beginTime','viewEndTime').'s ( Load:'.G('beginTime','loadTime').'s Init:'.G('loadTime','initTime').'s Exec:'.G('initTime','viewStartTime').'s Template:'.G('viewStartTime','viewEndTime').'s )'; } } function fb() { $instance = FirePHP::getInstance(true); $args = func_get_args(); return call_user_func_array(array($instance,'fb'),$args); } class FB { /** * Enable and disable logging to Firebug * * @see FirePHP->setEnabled() * @param boolean $Enabled TRUE to enable, FALSE to disable * @return void */ public static function setEnabled($Enabled) { $instance = FirePHP::getInstance(true); $instance->setEnabled($Enabled); } /** * Check if logging is enabled * * @see FirePHP->getEnabled() * @return boolean TRUE if enabled */ public static function getEnabled() { $instance = FirePHP::getInstance(true); return $instance->getEnabled(); } /** * Specify a filter to be used when encoding an object * * Filters are used to exclude object members. * * @see FirePHP->setObjectFilter() * @param string $Class The class name of the object * @param array $Filter An array or members to exclude * @return void */ public static function setObjectFilter($Class, $Filter) { $instance = FirePHP::getInstance(true); $instance->setObjectFilter($Class, $Filter); } /** * Set some options for the library * * @see FirePHP->setOptions() * @param array $Options The options to be set * @return void */ public static function setOptions($Options) { $instance = FirePHP::getInstance(true); $instance->setOptions($Options); } /** * Get options for the library * * @see FirePHP->getOptions() * @return array The options */ public static function getOptions() { $instance = FirePHP::getInstance(true); return $instance->getOptions(); } /** * Log object to firebug * * @see http://www.firephp.org/Wiki/Reference/Fb * @param mixed $Object * @return true * @throws Exception */ public static function send() { $instance = FirePHP::getInstance(true); $args = func_get_args(); return call_user_func_array(array($instance,'fb'),$args); } /** * Start a group for following messages * * Options: * Collapsed: [true|false] * Color: [#RRGGBB|ColorName] * * @param string $Name * @param array $Options OPTIONAL Instructions on how to log the group * @return true */ public static function group($Name, $Options=null) { $instance = FirePHP::getInstance(true); return $instance->group($Name, $Options); } /** * Ends a group you have started before * * @return true * @throws Exception */ public static function groupEnd() { return self::send(null, null, FirePHP::GROUP_END); } /** * Log object with label to firebug console * * @see FirePHP::LOG * @param mixes $Object * @param string $Label * @return true * @throws Exception */ public static function log($Object, $Label=null) { return self::send($Object, $Label, FirePHP::LOG); } /** * Log object with label to firebug console * * @see FirePHP::INFO * @param mixes $Object * @param string $Label * @return true * @throws Exception */ public static function info($Object, $Label=null) { return self::send($Object, $Label, FirePHP::INFO); } /** * Log object with label to firebug console * * @see FirePHP::WARN * @param mixes $Object * @param string $Label * @return true * @throws Exception */ public static function warn($Object, $Label=null) { return self::send($Object, $Label, FirePHP::WARN); } /** * Log object with label to firebug console * * @see FirePHP::ERROR * @param mixes $Object * @param string $Label * @return true * @throws Exception */ public static function error($Object, $Label=null) { return self::send($Object, $Label, FirePHP::ERROR); } /** * Dumps key and variable to firebug server panel * * @see FirePHP::DUMP * @param string $Key * @param mixed $Variable * @return true * @throws Exception */ public static function dump($Key, $Variable) { return self::send($Variable, $Key, FirePHP::DUMP); } /** * Log a trace in the firebug console * * @see FirePHP::TRACE * @param string $Label * @return true * @throws Exception */ public static function trace($Label) { return self::send($Label, FirePHP::TRACE); } /** * Log a table in the firebug console * * @see FirePHP::TABLE * @param string $Label * @param string $Table * @return true * @throws Exception */ public static function table($Label, $Table) { return self::send($Table, $Label, FirePHP::TABLE); } } if (!defined('E_STRICT')) { define('E_STRICT', 2048); } if (!defined('E_RECOVERABLE_ERROR')) { define('E_RECOVERABLE_ERROR', 4096); } if (!defined('E_DEPRECATED')) { define('E_DEPRECATED', 8192); } if (!defined('E_USER_DEPRECATED')) { define('E_USER_DEPRECATED', 16384); } /** * Sends the given data to the FirePHP Firefox Extension. * The data can be displayed in the Firebug Console or in the * "Server" request tab. * * For more information see: http://www.firephp.org/ * * @copyright Copyright (C) 2007-2009 Christoph Dorn * @author Christoph Dorn * @license http://www.opensource.org/licenses/bsd-license.php * @package FirePHPCore */ class FirePHP { /** * FirePHP version * * @var string */ const VERSION = '0.3'; // @pinf replace '0.3' with '%%package.version%%' /** * Firebug LOG level * * Logs a message to firebug console. * * @var string */ const LOG = 'LOG'; /** * Firebug INFO level * * Logs a message to firebug console and displays an info icon before the message. * * @var string */ const INFO = 'INFO'; /** * Firebug WARN level * * Logs a message to firebug console, displays an warning icon before the message and colors the line turquoise. * * @var string */ const WARN = 'WARN'; /** * Firebug ERROR level * * Logs a message to firebug console, displays an error icon before the message and colors the line yellow. Also increments the firebug error count. * * @var string */ const ERROR = 'ERROR'; /** * Dumps a variable to firebug's server panel * * @var string */ const DUMP = 'DUMP'; /** * Displays a stack trace in firebug console * * @var string */ const TRACE = 'TRACE'; /** * Displays an exception in firebug console * * Increments the firebug error count. * * @var string */ const EXCEPTION = 'EXCEPTION'; /** * Displays an table in firebug console * * @var string */ const TABLE = 'TABLE'; /** * Starts a group in firebug console * * @var string */ const GROUP_START = 'GROUP_START'; /** * Ends a group in firebug console * * @var string */ const GROUP_END = 'GROUP_END'; /** * Singleton instance of FirePHP * * @var FirePHP */ protected static $instance = null; /** * Flag whether we are logging from within the exception handler * * @var boolean */ protected $inExceptionHandler = false; /** * Flag whether to throw PHP errors that have been converted to ErrorExceptions * * @var boolean */ protected $throwErrorExceptions = true; /** * Flag whether to convert PHP assertion errors to Exceptions * * @var boolean */ protected $convertAssertionErrorsToExceptions = true; /** * Flag whether to throw PHP assertion errors that have been converted to Exceptions * * @var boolean */ protected $throwAssertionExceptions = false; /** * Wildfire protocol message index * * @var int */ protected $messageIndex = 1; /** * Options for the library * * @var array */ protected $options = array('maxDepth' => 10, 'maxObjectDepth' => 5, 'maxArrayDepth' => 5, 'useNativeJsonEncode' => true, 'includeLineNumbers' => true); /** * Filters used to exclude object members when encoding * * @var array */ protected $objectFilters = array( 'firephp' => array('objectStack', 'instance', 'json_objectStack'), 'firephp_test_class' => array('objectStack', 'instance', 'json_objectStack') ); /** * A stack of objects used to detect recursion during object encoding * * @var object */ protected $objectStack = array(); /** * Flag to enable/disable logging * * @var boolean */ protected $enabled = true; /** * The insight console to log to if applicable * * @var object */ protected $logToInsightConsole = null; /** * When the object gets serialized only include specific object members. * * @return array */ public function __sleep() { return array('options','objectFilters','enabled'); } /** * Gets singleton instance of FirePHP * * @param boolean $AutoCreate * @return FirePHP */ public static function getInstance($AutoCreate = false) { if ($AutoCreate===true && !self::$instance) { self::init(); } return self::$instance; } /** * Creates FirePHP object and stores it for singleton access * * @return FirePHP */ public static function init() { return self::setInstance(new self()); } /** * Set the instance of the FirePHP singleton * * @param FirePHP $instance The FirePHP object instance * @return FirePHP */ public static function setInstance($instance) { return self::$instance = $instance; } /** * Set an Insight console to direct all logging calls to * * @param object $console The console object to log to * @return void */ public function setLogToInsightConsole($console) { if(is_string($console)) { if(get_class($this)!='FirePHP_Insight' && !is_subclass_of($this, 'FirePHP_Insight')) { throw new Exception('FirePHP instance not an instance or subclass of FirePHP_Insight!'); } $this->logToInsightConsole = $this->to('request')->console($console); } else { $this->logToInsightConsole = $console; } } /** * Enable and disable logging to Firebug * * @param boolean $Enabled TRUE to enable, FALSE to disable * @return void */ public function setEnabled($Enabled) { $this->enabled = $Enabled; } /** * Check if logging is enabled * * @return boolean TRUE if enabled */ public function getEnabled() { return $this->enabled; } /** * Specify a filter to be used when encoding an object * * Filters are used to exclude object members. * * @param string $Class The class name of the object * @param array $Filter An array of members to exclude * @return void */ public function setObjectFilter($Class, $Filter) { $this->objectFilters[strtolower($Class)] = $Filter; } /** * Set some options for the library * * Options: * - maxDepth: The maximum depth to traverse (default: 10) * - maxObjectDepth: The maximum depth to traverse objects (default: 5) * - maxArrayDepth: The maximum depth to traverse arrays (default: 5) * - useNativeJsonEncode: If true will use json_encode() (default: true) * - includeLineNumbers: If true will include line numbers and filenames (default: true) * * @param array $Options The options to be set * @return void */ public function setOptions($Options) { $this->options = array_merge($this->options,$Options); } /** * Get options from the library * * @return array The currently set options */ public function getOptions() { return $this->options; } /** * Set an option for the library * * @param string $Name * @param mixed $Value * @throws Exception * @return void */ public function setOption($Name, $Value) { if (!isset($this->options[$Name])) { throw $this->newException('Unknown option: ' . $Name); } $this->options[$Name] = $Value; } /** * Get an option from the library * * @param string $Name * @throws Exception * @return mixed */ public function getOption($Name) { if (!isset($this->options[$Name])) { throw $this->newException('Unknown option: ' . $Name); } return $this->options[$Name]; } /** * Register FirePHP as your error handler * * Will throw exceptions for each php error. * * @return mixed Returns a string containing the previously defined error handler (if any) */ public function registerErrorHandler($throwErrorExceptions = false) { //NOTE: The following errors will not be caught by this error handler: // E_ERROR, E_PARSE, E_CORE_ERROR, // E_CORE_WARNING, E_COMPILE_ERROR, // E_COMPILE_WARNING, E_STRICT $this->throwErrorExceptions = $throwErrorExceptions; return set_error_handler(array($this,'errorHandler')); } /** * FirePHP's error handler * * Throws exception for each php error that will occur. * * @param int $errno * @param string $errstr * @param string $errfile * @param int $errline * @param array $errcontext */ public function errorHandler($errno, $errstr, $errfile, $errline, $errcontext) { // Don't throw exception if error reporting is switched off if (error_reporting() == 0) { return; } // Only throw exceptions for errors we are asking for if (error_reporting() & $errno) { $exception = new ErrorException($errstr, 0, $errno, $errfile, $errline); if ($this->throwErrorExceptions) { throw $exception; } else { $this->fb($exception); } } } /** * Register FirePHP as your exception handler * * @return mixed Returns the name of the previously defined exception handler, * or NULL on error. * If no previous handler was defined, NULL is also returned. */ public function registerExceptionHandler() { return set_exception_handler(array($this,'exceptionHandler')); } /** * FirePHP's exception handler * * Logs all exceptions to your firebug console and then stops the script. * * @param Exception $Exception * @throws Exception */ function exceptionHandler($Exception) { $this->inExceptionHandler = true; header('HTTP/1.1 500 Internal Server Error'); try { $this->fb($Exception); } catch (Exception $e) { echo 'We had an exception: ' . $e; } $this->inExceptionHandler = false; } /** * Register FirePHP driver as your assert callback * * @param boolean $convertAssertionErrorsToExceptions * @param boolean $throwAssertionExceptions * @return mixed Returns the original setting or FALSE on errors */ public function registerAssertionHandler($convertAssertionErrorsToExceptions = true, $throwAssertionExceptions = false) { $this->convertAssertionErrorsToExceptions = $convertAssertionErrorsToExceptions; $this->throwAssertionExceptions = $throwAssertionExceptions; if ($throwAssertionExceptions && !$convertAssertionErrorsToExceptions) { throw $this->newException('Cannot throw assertion exceptions as assertion errors are not being converted to exceptions!'); } return assert_options(ASSERT_CALLBACK, array($this, 'assertionHandler')); } /** * FirePHP's assertion handler * * Logs all assertions to your firebug console and then stops the script. * * @param string $file File source of assertion * @param int $line Line source of assertion * @param mixed $code Assertion code */ public function assertionHandler($file, $line, $code) { if ($this->convertAssertionErrorsToExceptions) { $exception = new ErrorException('Assertion Failed - Code[ '.$code.' ]', 0, null, $file, $line); if ($this->throwAssertionExceptions) { throw $exception; } else { $this->fb($exception); } } else { $this->fb($code, 'Assertion Failed', FirePHP::ERROR, array('File'=>$file,'Line'=>$line)); } } /** * Start a group for following messages. * * Options: * Collapsed: [true|false] * Color: [#RRGGBB|ColorName] * * @param string $Name * @param array $Options OPTIONAL Instructions on how to log the group * @return true * @throws Exception */ public function group($Name, $Options = null) { if (!$Name) { throw $this->newException('You must specify a label for the group!'); } if ($Options) { if (!is_array($Options)) { throw $this->newException('Options must be defined as an array!'); } if (array_key_exists('Collapsed', $Options)) { $Options['Collapsed'] = ($Options['Collapsed'])?'true':'false'; } } return $this->fb(null, $Name, FirePHP::GROUP_START, $Options); } /** * Ends a group you have started before * * @return true * @throws Exception */ public function groupEnd() { return $this->fb(null, null, FirePHP::GROUP_END); } /** * Log object with label to firebug console * * @see FirePHP::LOG * @param mixes $Object * @param string $Label * @return true * @throws Exception */ public function log($Object, $Label = null, $Options = array()) { return $this->fb($Object, $Label, FirePHP::LOG, $Options); } /** * Log object with label to firebug console * * @see FirePHP::INFO * @param mixes $Object * @param string $Label * @return true * @throws Exception */ public function info($Object, $Label = null, $Options = array()) { return $this->fb($Object, $Label, FirePHP::INFO, $Options); } /** * Log object with label to firebug console * * @see FirePHP::WARN * @param mixes $Object * @param string $Label * @return true * @throws Exception */ public function warn($Object, $Label = null, $Options = array()) { return $this->fb($Object, $Label, FirePHP::WARN, $Options); } /** * Log object with label to firebug console * * @see FirePHP::ERROR * @param mixes $Object * @param string $Label * @return true * @throws Exception */ public function error($Object, $Label = null, $Options = array()) { return $this->fb($Object, $Label, FirePHP::ERROR, $Options); } /** * Dumps key and variable to firebug server panel * * @see FirePHP::DUMP * @param string $Key * @param mixed $Variable * @return true * @throws Exception */ public function dump($Key, $Variable, $Options = array()) { if (!is_string($Key)) { throw $this->newException('Key passed to dump() is not a string'); } if (strlen($Key)>100) { throw $this->newException('Key passed to dump() is longer than 100 characters'); } if (!preg_match_all('/^[a-zA-Z0-9-_\.:]*$/', $Key, $m)) { throw $this->newException('Key passed to dump() contains invalid characters [a-zA-Z0-9-_\.:]'); } return $this->fb($Variable, $Key, FirePHP::DUMP, $Options); } /** * Log a trace in the firebug console * * @see FirePHP::TRACE * @param string $Label * @return true * @throws Exception */ public function trace($Label) { return $this->fb($Label, FirePHP::TRACE); } /** * Log a table in the firebug console * * @see FirePHP::TABLE * @param string $Label * @param string $Table * @return true * @throws Exception */ public function table($Label, $Table, $Options = array()) { return $this->fb($Table, $Label, FirePHP::TABLE, $Options); } /** * Insight API wrapper * * @see Insight_Helper::to() */ public static function to() { $instance = self::getInstance(); if (!method_exists($instance, "_to")) { throw new Exception("FirePHP::to() implementation not loaded"); } $args = func_get_args(); return call_user_func_array(array($instance, '_to'), $args); } /** * Insight API wrapper * * @see Insight_Helper::plugin() */ public static function plugin() { $instance = self::getInstance(); if (!method_exists($instance, "_plugin")) { throw new Exception("FirePHP::plugin() implementation not loaded"); } $args = func_get_args(); return call_user_func_array(array($instance, '_plugin'), $args); } /** * Check if FirePHP is installed on client * * @return boolean */ public function detectClientExtension() { // Check if FirePHP is installed on client via User-Agent header if (@preg_match_all('/\sFirePHP\/([\.\d]*)\s?/si',$this->getUserAgent(),$m) && version_compare($m[1][0],'0.0.6','>=')) { return true; } else // Check if FirePHP is installed on client via X-FirePHP-Version header if (@preg_match_all('/^([\.\d]*)$/si',$this->getRequestHeader("X-FirePHP-Version"),$m) && version_compare($m[1][0],'0.0.6','>=')) { return true; } return false; } /** * Log varible to Firebug * * @see http://www.firephp.org/Wiki/Reference/Fb * @param mixed $Object The variable to be logged * @return true Return TRUE if message was added to headers, FALSE otherwise * @throws Exception */ public function fb($Object) { if($this instanceof FirePHP_Insight && method_exists($this, '_logUpgradeClientMessage')) { if(!FirePHP_Insight::$upgradeClientMessageLogged) { // avoid infinite recursion as _logUpgradeClientMessage() logs a message $this->_logUpgradeClientMessage(); } } static $insightGroupStack = array(); if (!$this->getEnabled()) { return false; } if ($this->headersSent($filename, $linenum)) { // If we are logging from within the exception handler we cannot throw another exception if ($this->inExceptionHandler) { // Simply echo the error out to the page echo '
FirePHP ERROR: Headers already sent in '.$filename.' on line '.$linenum.'. Cannot send log data to FirePHP. You must have Output Buffering enabled via ob_start() or output_buffering ini directive.
'; } else { throw $this->newException('Headers already sent in '.$filename.' on line '.$linenum.'. Cannot send log data to FirePHP. You must have Output Buffering enabled via ob_start() or output_buffering ini directive.'); } } $Type = null; $Label = null; $Options = array(); if (func_num_args()==1) { } else if (func_num_args()==2) { switch(func_get_arg(1)) { case self::LOG: case self::INFO: case self::WARN: case self::ERROR: case self::DUMP: case self::TRACE: case self::EXCEPTION: case self::TABLE: case self::GROUP_START: case self::GROUP_END: $Type = func_get_arg(1); break; default: $Label = func_get_arg(1); break; } } else if (func_num_args()==3) { $Type = func_get_arg(2); $Label = func_get_arg(1); } else if (func_num_args()==4) { $Type = func_get_arg(2); $Label = func_get_arg(1); $Options = func_get_arg(3); } else { throw $this->newException('Wrong number of arguments to fb() function!'); } if($this->logToInsightConsole!==null && (get_class($this)=='FirePHP_Insight' || is_subclass_of($this, 'FirePHP_Insight'))) { $msg = $this->logToInsightConsole; if ($Object instanceof Exception) { $Type = self::EXCEPTION; } if($Label && $Type!=self::TABLE && $Type!=self::GROUP_START) { $msg = $msg->label($Label); } switch($Type) { case self::DUMP: case self::LOG: return $msg->log($Object); case self::INFO: return $msg->info($Object); case self::WARN: return $msg->warn($Object); case self::ERROR: return $msg->error($Object); case self::TRACE: return $msg->trace($Object); case self::EXCEPTION: return $this->plugin('engine')->handleException($Object, $msg); case self::TABLE: if (isset($Object[0]) && !is_string($Object[0]) && $Label) { $Object = array($Label, $Object); } return $msg->table($Object[0], array_slice($Object[1],1), $Object[1][0]); case self::GROUP_START: $insightGroupStack[] = $msg->group(md5($Label))->open(); return $msg->log($Label); case self::GROUP_END: if(count($insightGroupStack)==0) { throw new Error('Too many groupEnd() as opposed to group() calls!'); } $group = array_pop($insightGroupStack); return $group->close(); default: return $msg->log($Object); } } if (!$this->detectClientExtension()) { return false; } $meta = array(); $skipFinalObjectEncode = false; if ($Object instanceof Exception) { $meta['file'] = $this->_escapeTraceFile($Object->getFile()); $meta['line'] = $Object->getLine(); $trace = $Object->getTrace(); if ($Object instanceof ErrorException && isset($trace[0]['function']) && $trace[0]['function']=='errorHandler' && isset($trace[0]['class']) && $trace[0]['class']=='FirePHP') { $severity = false; switch($Object->getSeverity()) { case E_WARNING: $severity = 'E_WARNING'; break; case E_NOTICE: $severity = 'E_NOTICE'; break; case E_USER_ERROR: $severity = 'E_USER_ERROR'; break; case E_USER_WARNING: $severity = 'E_USER_WARNING'; break; case E_USER_NOTICE: $severity = 'E_USER_NOTICE'; break; case E_STRICT: $severity = 'E_STRICT'; break; case E_RECOVERABLE_ERROR: $severity = 'E_RECOVERABLE_ERROR'; break; case E_DEPRECATED: $severity = 'E_DEPRECATED'; break; case E_USER_DEPRECATED: $severity = 'E_USER_DEPRECATED'; break; } $Object = array('Class'=>get_class($Object), 'Message'=>$severity.': '.$Object->getMessage(), 'File'=>$this->_escapeTraceFile($Object->getFile()), 'Line'=>$Object->getLine(), 'Type'=>'trigger', 'Trace'=>$this->_escapeTrace(array_splice($trace,2))); $skipFinalObjectEncode = true; } else { $Object = array('Class'=>get_class($Object), 'Message'=>$Object->getMessage(), 'File'=>$this->_escapeTraceFile($Object->getFile()), 'Line'=>$Object->getLine(), 'Type'=>'throw', 'Trace'=>$this->_escapeTrace($trace)); $skipFinalObjectEncode = true; } $Type = self::EXCEPTION; } else if ($Type==self::TRACE) { $trace = debug_backtrace(); if (!$trace) return false; for( $i=0 ; $i_standardizePath($trace[$i]['file']),-18,18)=='FirePHPCore/fb.php' || substr($this->_standardizePath($trace[$i]['file']),-29,29)=='FirePHPCore/FirePHP.class.php')) { /* Skip - FB::trace(), FB::send(), $firephp->trace(), $firephp->fb() */ } else if (isset($trace[$i]['class']) && isset($trace[$i+1]['file']) && $trace[$i]['class']=='FirePHP' && substr($this->_standardizePath($trace[$i+1]['file']),-18,18)=='FirePHPCore/fb.php') { /* Skip fb() */ } else if ($trace[$i]['function']=='fb' || $trace[$i]['function']=='trace' || $trace[$i]['function']=='send') { $Object = array('Class'=>isset($trace[$i]['class'])?$trace[$i]['class']:'', 'Type'=>isset($trace[$i]['type'])?$trace[$i]['type']:'', 'Function'=>isset($trace[$i]['function'])?$trace[$i]['function']:'', 'Message'=>$trace[$i]['args'][0], 'File'=>isset($trace[$i]['file'])?$this->_escapeTraceFile($trace[$i]['file']):'', 'Line'=>isset($trace[$i]['line'])?$trace[$i]['line']:'', 'Args'=>isset($trace[$i]['args'])?$this->encodeObject($trace[$i]['args']):'', 'Trace'=>$this->_escapeTrace(array_splice($trace,$i+1))); $skipFinalObjectEncode = true; $meta['file'] = isset($trace[$i]['file'])?$this->_escapeTraceFile($trace[$i]['file']):''; $meta['line'] = isset($trace[$i]['line'])?$trace[$i]['line']:''; break; } } } else if ($Type==self::TABLE) { if (isset($Object[0]) && is_string($Object[0])) { $Object[1] = $this->encodeTable($Object[1]); } else { $Object = $this->encodeTable($Object); } $skipFinalObjectEncode = true; } else if ($Type==self::GROUP_START) { if (!$Label) { throw $this->newException('You must specify a label for the group!'); } } else { if ($Type===null) { $Type = self::LOG; } } if ($this->options['includeLineNumbers']) { if (!isset($meta['file']) || !isset($meta['line'])) { $trace = debug_backtrace(); for( $i=0 ; $trace && $i_standardizePath($trace[$i]['file']),-18,18)=='FirePHPCore/fb.php' || substr($this->_standardizePath($trace[$i]['file']),-29,29)=='FirePHPCore/FirePHP.class.php')) { /* Skip - FB::trace(), FB::send(), $firephp->trace(), $firephp->fb() */ } else if (isset($trace[$i]['class']) && isset($trace[$i+1]['file']) && $trace[$i]['class']=='FirePHP' && substr($this->_standardizePath($trace[$i+1]['file']),-18,18)=='FirePHPCore/fb.php') { /* Skip fb() */ } else if (isset($trace[$i]['file']) && substr($this->_standardizePath($trace[$i]['file']),-18,18)=='FirePHPCore/fb.php') { /* Skip FB::fb() */ } else { $meta['file'] = isset($trace[$i]['file'])?$this->_escapeTraceFile($trace[$i]['file']):''; $meta['line'] = isset($trace[$i]['line'])?$trace[$i]['line']:''; break; } } } } else { unset($meta['file']); unset($meta['line']); } $this->setHeader('X-Wf-Protocol-1','http://meta.wildfirehq.org/Protocol/JsonStream/0.2'); $this->setHeader('X-Wf-1-Plugin-1','http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/'.self::VERSION); $structure_index = 1; if ($Type==self::DUMP) { $structure_index = 2; $this->setHeader('X-Wf-1-Structure-2','http://meta.firephp.org/Wildfire/Structure/FirePHP/Dump/0.1'); } else { $this->setHeader('X-Wf-1-Structure-1','http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1'); } if ($Type==self::DUMP) { $msg = '{"'.$Label.'":'.$this->jsonEncode($Object, $skipFinalObjectEncode).'}'; } else { $msg_meta = $Options; $msg_meta['Type'] = $Type; if ($Label!==null) { $msg_meta['Label'] = $Label; } if (isset($meta['file']) && !isset($msg_meta['File'])) { $msg_meta['File'] = $meta['file']; } if (isset($meta['line']) && !isset($msg_meta['Line'])) { $msg_meta['Line'] = $meta['line']; } $msg = '['.$this->jsonEncode($msg_meta).','.$this->jsonEncode($Object, $skipFinalObjectEncode).']'; } $parts = explode("\n",chunk_split($msg, 5000, "\n")); for( $i=0 ; $i2) { // Message needs to be split into multiple parts $this->setHeader('X-Wf-1-'.$structure_index.'-'.'1-'.$this->messageIndex, (($i==0)?strlen($msg):'') . '|' . $part . '|' . (($isetHeader('X-Wf-1-'.$structure_index.'-'.'1-'.$this->messageIndex, strlen($part) . '|' . $part . '|'); } $this->messageIndex++; if ($this->messageIndex > 99999) { throw $this->newException('Maximum number (99,999) of messages reached!'); } } } $this->setHeader('X-Wf-1-Index',$this->messageIndex-1); return true; } /** * Standardizes path for windows systems. * * @param string $Path * @return string */ protected function _standardizePath($Path) { return preg_replace('/\\\\+/','/',$Path); } /** * Escape trace path for windows systems * * @param array $Trace * @return array */ protected function _escapeTrace($Trace) { if (!$Trace) return $Trace; for( $i=0 ; $i_escapeTraceFile($Trace[$i]['file']); } if (isset($Trace[$i]['args'])) { $Trace[$i]['args'] = $this->encodeObject($Trace[$i]['args']); } } return $Trace; } /** * Escape file information of trace for windows systems * * @param string $File * @return string */ protected function _escapeTraceFile($File) { /* Check if we have a windows filepath */ if (strpos($File,'\\')) { /* First strip down to single \ */ $file = preg_replace('/\\\\+/','\\',$File); return $file; } return $File; } /** * Check if headers have already been sent * * @param string $Filename * @param integer $Linenum */ protected function headersSent(&$Filename, &$Linenum) { return headers_sent($Filename, $Linenum); } /** * Send header * * @param string $Name * @param string $Value */ protected function setHeader($Name, $Value) { return header($Name.': '.$Value); } /** * Get user agent * * @return string|false */ protected function getUserAgent() { if (!isset($_SERVER['HTTP_USER_AGENT'])) return false; return $_SERVER['HTTP_USER_AGENT']; } /** * Get all request headers * * @return array */ public static function getAllRequestHeaders() { static $_cached_headers = false; if($_cached_headers!==false) { return $_cached_headers; } $headers = array(); if(function_exists('getallheaders')) { foreach( getallheaders() as $name => $value ) { $headers[strtolower($name)] = $value; } } else { foreach($_SERVER as $name => $value) { if(substr($name, 0, 5) == 'HTTP_') { $headers[strtolower(str_replace(' ', '-', str_replace('_', ' ', substr($name, 5))))] = $value; } } } return $_cached_headers = $headers; } /** * Get a request header * * @return string|false */ protected function getRequestHeader($Name) { $headers = self::getAllRequestHeaders(); if (isset($headers[strtolower($Name)])) { return $headers[strtolower($Name)]; } return false; } /** * Returns a new exception * * @param string $Message * @return Exception */ protected function newException($Message) { return new Exception($Message); } /** * Encode an object into a JSON string * * Uses PHP's jeson_encode() if available * * @param object $Object The object to be encoded * @return string The JSON string */ public function jsonEncode($Object, $skipObjectEncode = false) { if (!$skipObjectEncode) { $Object = $this->encodeObject($Object); } if (function_exists('json_encode') && $this->options['useNativeJsonEncode']!=false) { return json_encode($Object); } else { return $this->json_encode($Object); } } /** * Encodes a table by encoding each row and column with encodeObject() * * @param array $Table The table to be encoded * @return array */ protected function encodeTable($Table) { if (!$Table) return $Table; $new_table = array(); foreach($Table as $row) { if (is_array($row)) { $new_row = array(); foreach($row as $item) { $new_row[] = $this->encodeObject($item); } $new_table[] = $new_row; } } return $new_table; } /** * Encodes an object including members with * protected and private visibility * * @param Object $Object The object to be encoded * @param int $Depth The current traversal depth * @return array All members of the object */ protected function encodeObject($Object, $ObjectDepth = 1, $ArrayDepth = 1, $MaxDepth = 1) { if ($MaxDepth > $this->options['maxDepth']) { return '** Max Depth ('.$this->options['maxDepth'].') **'; } $return = array(); if (is_resource($Object)) { return '** '.(string)$Object.' **'; } else if (is_object($Object)) { if ($ObjectDepth > $this->options['maxObjectDepth']) { return '** Max Object Depth ('.$this->options['maxObjectDepth'].') **'; } foreach ($this->objectStack as $refVal) { if ($refVal === $Object) { return '** Recursion ('.get_class($Object).') **'; } } array_push($this->objectStack, $Object); $return['__className'] = $class = get_class($Object); $class_lower = strtolower($class); $reflectionClass = new ReflectionClass($class); $properties = array(); foreach( $reflectionClass->getProperties() as $property) { $properties[$property->getName()] = $property; } $members = (array)$Object; foreach( $properties as $plain_name => $property ) { $name = $raw_name = $plain_name; if ($property->isStatic()) { $name = 'static:'.$name; } if ($property->isPublic()) { $name = 'public:'.$name; } else if ($property->isPrivate()) { $name = 'private:'.$name; $raw_name = "\0".$class."\0".$raw_name; } else if ($property->isProtected()) { $name = 'protected:'.$name; $raw_name = "\0".'*'."\0".$raw_name; } if (!(isset($this->objectFilters[$class_lower]) && is_array($this->objectFilters[$class_lower]) && in_array($plain_name,$this->objectFilters[$class_lower]))) { if (array_key_exists($raw_name,$members) && !$property->isStatic()) { $return[$name] = $this->encodeObject($members[$raw_name], $ObjectDepth + 1, 1, $MaxDepth + 1); } else { if (method_exists($property,'setAccessible')) { $property->setAccessible(true); $return[$name] = $this->encodeObject($property->getValue($Object), $ObjectDepth + 1, 1, $MaxDepth + 1); } else if ($property->isPublic()) { $return[$name] = $this->encodeObject($property->getValue($Object), $ObjectDepth + 1, 1, $MaxDepth + 1); } else { $return[$name] = '** Need PHP 5.3 to get value **'; } } } else { $return[$name] = '** Excluded by Filter **'; } } // Include all members that are not defined in the class // but exist in the object foreach( $members as $raw_name => $value ) { $name = $raw_name; if ($name{0} == "\0") { $parts = explode("\0", $name); $name = $parts[2]; } $plain_name = $name; if (!isset($properties[$name])) { $name = 'undeclared:'.$name; if (!(isset($this->objectFilters[$class_lower]) && is_array($this->objectFilters[$class_lower]) && in_array($plain_name,$this->objectFilters[$class_lower]))) { $return[$name] = $this->encodeObject($value, $ObjectDepth + 1, 1, $MaxDepth + 1); } else { $return[$name] = '** Excluded by Filter **'; } } } array_pop($this->objectStack); } elseif (is_array($Object)) { if ($ArrayDepth > $this->options['maxArrayDepth']) { return '** Max Array Depth ('.$this->options['maxArrayDepth'].') **'; } foreach ($Object as $key => $val) { // Encoding the $GLOBALS PHP array causes an infinite loop // if the recursion is not reset here as it contains // a reference to itself. This is the only way I have come up // with to stop infinite recursion in this case. if ($key=='GLOBALS' && is_array($val) && array_key_exists('GLOBALS',$val)) { $val['GLOBALS'] = '** Recursion (GLOBALS) **'; } $return[$key] = $this->encodeObject($val, 1, $ArrayDepth + 1, $MaxDepth + 1); } } else { if (self::is_utf8($Object)) { return $Object; } else { return utf8_encode($Object); } } return $return; } /** * Returns true if $string is valid UTF-8 and false otherwise. * * @param mixed $str String to be tested * @return boolean */ protected static function is_utf8($str) { if(function_exists('mb_detect_encoding')) { return (mb_detect_encoding($str) == 'UTF-8'); } $c=0; $b=0; $bits=0; $len=strlen($str); for($i=0; $i<$len; $i++){ $c=ord($str[$i]); if ($c > 128){ if (($c >= 254)) return false; elseif ($c >= 252) $bits=6; elseif ($c >= 248) $bits=5; elseif ($c >= 240) $bits=4; elseif ($c >= 224) $bits=3; elseif ($c >= 192) $bits=2; else return false; if (($i+$bits) > $len) return false; while($bits > 1){ $i++; $b=ord($str[$i]); if ($b < 128 || $b > 191) return false; $bits--; } } } return true; } /** * Converts to and from JSON format. * * JSON (JavaScript Object Notation) is a lightweight data-interchange * format. It is easy for humans to read and write. It is easy for machines * to parse and generate. It is based on a subset of the JavaScript * Programming Language, Standard ECMA-262 3rd Edition - December 1999. * This feature can also be found in Python. JSON is a text format that is * completely language independent but uses conventions that are familiar * to programmers of the C-family of languages, including C, C++, C#, Java, * JavaScript, Perl, TCL, and many others. These properties make JSON an * ideal data-interchange language. * * This package provides a simple encoder and decoder for JSON notation. It * is intended for use with client-side Javascript applications that make * use of HTTPRequest to perform server communication functions - data can * be encoded into JSON notation for use in a client-side javascript, or * decoded from incoming Javascript requests. JSON format is native to * Javascript, and can be directly eval()'ed with no further parsing * overhead * * All strings should be in ASCII or UTF-8 format! * * LICENSE: Redistribution and use in source and binary forms, with or * without modification, are permitted provided that the following * conditions are met: Redistributions of source code must retain the * above copyright notice, this list of conditions and the following * disclaimer. Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following disclaimer * in the documentation and/or other materials provided with the * distribution. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN * NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. * * @category * @package Services_JSON * @author Michal Migurski * @author Matt Knapp * @author Brett Stimmerman * @author Christoph Dorn * @copyright 2005 Michal Migurski * @version CVS: $Id: JSON.php,v 1.31 2006/06/28 05:54:17 migurski Exp $ * @license http://www.opensource.org/licenses/bsd-license.php * @link http://pear.php.net/pepr/pepr-proposal-show.php?id=198 */ /** * Keep a list of objects as we descend into the array so we can detect recursion. */ private $json_objectStack = array(); /** * convert a string from one UTF-8 char to one UTF-16 char * * Normally should be handled by mb_convert_encoding, but * provides a slower PHP-only method for installations * that lack the multibye string extension. * * @param string $utf8 UTF-8 character * @return string UTF-16 character * @access private */ private function json_utf82utf16($utf8) { // oh please oh please oh please oh please oh please if (function_exists('mb_convert_encoding')) { return mb_convert_encoding($utf8, 'UTF-16', 'UTF-8'); } switch(strlen($utf8)) { case 1: // this case should never be reached, because we are in ASCII range // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 return $utf8; case 2: // return a UTF-16 character from a 2-byte UTF-8 char // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 return chr(0x07 & (ord($utf8{0}) >> 2)) . chr((0xC0 & (ord($utf8{0}) << 6)) | (0x3F & ord($utf8{1}))); case 3: // return a UTF-16 character from a 3-byte UTF-8 char // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 return chr((0xF0 & (ord($utf8{0}) << 4)) | (0x0F & (ord($utf8{1}) >> 2))) . chr((0xC0 & (ord($utf8{1}) << 6)) | (0x7F & ord($utf8{2}))); } // ignoring UTF-32 for now, sorry return ''; } /** * encodes an arbitrary variable into JSON format * * @param mixed $var any number, boolean, string, array, or object to be encoded. * see argument 1 to Services_JSON() above for array-parsing behavior. * if var is a strng, note that encode() always expects it * to be in ASCII or UTF-8 format! * * @return mixed JSON string representation of input var or an error if a problem occurs * @access public */ private function json_encode($var) { if (is_object($var)) { if (in_array($var,$this->json_objectStack)) { return '"** Recursion **"'; } } switch (gettype($var)) { case 'boolean': return $var ? 'true' : 'false'; case 'NULL': return 'null'; case 'integer': return (int) $var; case 'double': case 'float': return (float) $var; case 'string': // STRINGS ARE EXPECTED TO BE IN ASCII OR UTF-8 FORMAT $ascii = ''; $strlen_var = strlen($var); /* * Iterate over every character in the string, * escaping with a slash or encoding to UTF-8 where necessary */ for ($c = 0; $c < $strlen_var; ++$c) { $ord_var_c = ord($var{$c}); switch (true) { case $ord_var_c == 0x08: $ascii .= '\b'; break; case $ord_var_c == 0x09: $ascii .= '\t'; break; case $ord_var_c == 0x0A: $ascii .= '\n'; break; case $ord_var_c == 0x0C: $ascii .= '\f'; break; case $ord_var_c == 0x0D: $ascii .= '\r'; break; case $ord_var_c == 0x22: case $ord_var_c == 0x2F: case $ord_var_c == 0x5C: // double quote, slash, slosh $ascii .= '\\'.$var{$c}; break; case (($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)): // characters U-00000000 - U-0000007F (same as ASCII) $ascii .= $var{$c}; break; case (($ord_var_c & 0xE0) == 0xC0): // characters U-00000080 - U-000007FF, mask 110XXXXX // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 $char = pack('C*', $ord_var_c, ord($var{$c + 1})); $c += 1; $utf16 = $this->json_utf82utf16($char); $ascii .= sprintf('\u%04s', bin2hex($utf16)); break; case (($ord_var_c & 0xF0) == 0xE0): // characters U-00000800 - U-0000FFFF, mask 1110XXXX // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 $char = pack('C*', $ord_var_c, ord($var{$c + 1}), ord($var{$c + 2})); $c += 2; $utf16 = $this->json_utf82utf16($char); $ascii .= sprintf('\u%04s', bin2hex($utf16)); break; case (($ord_var_c & 0xF8) == 0xF0): // characters U-00010000 - U-001FFFFF, mask 11110XXX // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 $char = pack('C*', $ord_var_c, ord($var{$c + 1}), ord($var{$c + 2}), ord($var{$c + 3})); $c += 3; $utf16 = $this->json_utf82utf16($char); $ascii .= sprintf('\u%04s', bin2hex($utf16)); break; case (($ord_var_c & 0xFC) == 0xF8): // characters U-00200000 - U-03FFFFFF, mask 111110XX // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 $char = pack('C*', $ord_var_c, ord($var{$c + 1}), ord($var{$c + 2}), ord($var{$c + 3}), ord($var{$c + 4})); $c += 4; $utf16 = $this->json_utf82utf16($char); $ascii .= sprintf('\u%04s', bin2hex($utf16)); break; case (($ord_var_c & 0xFE) == 0xFC): // characters U-04000000 - U-7FFFFFFF, mask 1111110X // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 $char = pack('C*', $ord_var_c, ord($var{$c + 1}), ord($var{$c + 2}), ord($var{$c + 3}), ord($var{$c + 4}), ord($var{$c + 5})); $c += 5; $utf16 = $this->json_utf82utf16($char); $ascii .= sprintf('\u%04s', bin2hex($utf16)); break; } } return '"'.$ascii.'"'; case 'array': /* * As per JSON spec if any array key is not an integer * we must treat the the whole array as an object. We * also try to catch a sparsely populated associative * array with numeric keys here because some JS engines * will create an array with empty indexes up to * max_index which can cause memory issues and because * the keys, which may be relevant, will be remapped * otherwise. * * As per the ECMA and JSON specification an object may * have any string as a property. Unfortunately due to * a hole in the ECMA specification if the key is a * ECMA reserved word or starts with a digit the * parameter is only accessible using ECMAScript's * bracket notation. */ // treat as a JSON object if (is_array($var) && count($var) && (array_keys($var) !== range(0, sizeof($var) - 1))) { $this->json_objectStack[] = $var; $properties = array_map(array($this, 'json_name_value'), array_keys($var), array_values($var)); array_pop($this->json_objectStack); foreach($properties as $property) { if ($property instanceof Exception) { return $property; } } return '{' . join(',', $properties) . '}'; } $this->json_objectStack[] = $var; // treat it like a regular array $elements = array_map(array($this, 'json_encode'), $var); array_pop($this->json_objectStack); foreach($elements as $element) { if ($element instanceof Exception) { return $element; } } return '[' . join(',', $elements) . ']'; case 'object': $vars = self::encodeObject($var); $this->json_objectStack[] = $var; $properties = array_map(array($this, 'json_name_value'), array_keys($vars), array_values($vars)); array_pop($this->json_objectStack); foreach($properties as $property) { if ($property instanceof Exception) { return $property; } } return '{' . join(',', $properties) . '}'; default: return null; } } /** * array-walking function for use in generating JSON-formatted name-value pairs * * @param string $name name of key to use * @param mixed $value reference to an array element to be encoded * * @return string JSON-formatted name-value pair, like '"name":value' * @access private */ private function json_name_value($name, $value) { // Encoding the $GLOBALS PHP array causes an infinite loop // if the recursion is not reset here as it contains // a reference to itself. This is the only way I have come up // with to stop infinite recursion in this case. if ($name=='GLOBALS' && is_array($value) && array_key_exists('GLOBALS',$value)) { $value['GLOBALS'] = '** Recursion **'; } $encoded_value = $this->json_encode($value); if ($encoded_value instanceof Exception) { return $encoded_value; } return $this->json_encode(strval($name)) . ':' . $encoded_value; } /** * @deprecated */ public function setProcessorUrl($URL) { trigger_error("The FirePHP::setProcessorUrl() method is no longer supported", E_USER_DEPRECATED); } /** * @deprecated */ public function setRendererUrl($URL) { trigger_error("The FirePHP::setRendererUrl() method is no longer supported", E_USER_DEPRECATED); } } ================================================ FILE: ThinkPHP/Library/Behavior/ParseTemplateBehavior.class.php ================================================ // +---------------------------------------------------------------------- namespace Behavior; use Think\Storage; use Think\Think; /** * 系统行为扩展:模板解析 */ class ParseTemplateBehavior { // 行为扩展的执行入口必须是run public function run(&$_data){ $engine = strtolower(C('TMPL_ENGINE_TYPE')); $_content = empty($_data['content'])?$_data['file']:$_data['content']; $_data['prefix'] = !empty($_data['prefix'])?$_data['prefix']:C('TMPL_CACHE_PREFIX'); if('think'==$engine){ // 采用Think模板引擎 if((!empty($_data['content']) && $this->checkContentCache($_data['content'],$_data['prefix'])) || $this->checkCache($_data['file'],$_data['prefix'])) { // 缓存有效 //载入模版缓存文件 Storage::load(C('CACHE_PATH').$_data['prefix'].md5($_content).C('TMPL_CACHFILE_SUFFIX'),$_data['var']); }else{ $tpl = Think::instance('Think\\Template'); // 编译并加载模板文件 $tpl->fetch($_content,$_data['var'],$_data['prefix']); } }else{ // 调用第三方模板引擎解析和输出 if(strpos($engine,'\\')){ $class = $engine; }else{ $class = 'Think\\Template\\Driver\\'.ucwords($engine); } if(class_exists($class)) { $tpl = new $class; $tpl->fetch($_content,$_data['var']); }else { // 类没有定义 E(L('_NOT_SUPPORT_').': ' . $class); } } } /** * 检查缓存文件是否有效 * 如果无效则需要重新编译 * @access public * @param string $tmplTemplateFile 模板文件名 * @return boolean */ protected function checkCache($tmplTemplateFile,$prefix='') { if (!C('TMPL_CACHE_ON')) // 优先对配置设定检测 return false; $tmplCacheFile = C('CACHE_PATH').$prefix.md5($tmplTemplateFile).C('TMPL_CACHFILE_SUFFIX'); if(!Storage::has($tmplCacheFile)){ return false; }elseif (filemtime($tmplTemplateFile) > Storage::get($tmplCacheFile,'mtime')) { // 模板文件如果有更新则缓存需要更新 return false; }elseif (C('TMPL_CACHE_TIME') != 0 && time() > Storage::get($tmplCacheFile,'mtime')+C('TMPL_CACHE_TIME')) { // 缓存是否在有效期 return false; } // 开启布局模板 if(C('LAYOUT_ON')) { $layoutFile = THEME_PATH.C('LAYOUT_NAME').C('TMPL_TEMPLATE_SUFFIX'); if(filemtime($layoutFile) > Storage::get($tmplCacheFile,'mtime')) { return false; } } // 缓存有效 return true; } /** * 检查缓存内容是否有效 * 如果无效则需要重新编译 * @access public * @param string $tmplContent 模板内容 * @return boolean */ protected function checkContentCache($tmplContent,$prefix='') { if(Storage::has(C('CACHE_PATH').$prefix.md5($tmplContent).C('TMPL_CACHFILE_SUFFIX'))){ return true; }else{ return false; } } } ================================================ FILE: ThinkPHP/Library/Behavior/ReadHtmlCacheBehavior.class.php ================================================ // +---------------------------------------------------------------------- namespace Behavior; use Think\Storage; /** * 系统行为扩展:静态缓存读取 */ class ReadHtmlCacheBehavior { // 行为扩展的执行入口必须是run public function run(&$params){ // 开启静态缓存 if(IS_GET && C('HTML_CACHE_ON')) { $cacheTime = $this->requireHtmlCache(); if( false !== $cacheTime && $this->checkHTMLCache(HTML_FILE_NAME,$cacheTime)) { //静态页面有效 // 读取静态页面输出 echo Storage::read(HTML_FILE_NAME,'html'); exit(); } } } // 判断是否需要静态缓存 static private function requireHtmlCache() { // 分析当前的静态规则 $htmls = C('HTML_CACHE_RULES'); // 读取静态规则 if(!empty($htmls)) { $htmls = array_change_key_case($htmls); // 静态规则文件定义格式 actionName=>array('静态规则','缓存时间','附加规则') // 'read'=>array('{id},{name}',60,'md5') 必须保证静态规则的唯一性 和 可判断性 // 检测静态规则 $controllerName = strtolower(CONTROLLER_NAME); $actionName = strtolower(ACTION_NAME); if(isset($htmls[$controllerName.':'.$actionName])) { $html = $htmls[$controllerName.':'.$actionName]; // 某个控制器的操作的静态规则 }elseif(isset($htmls[$controllerName.':'])){// 某个控制器的静态规则 $html = $htmls[$controllerName.':']; }elseif(isset($htmls[$actionName])){ $html = $htmls[$actionName]; // 所有操作的静态规则 }elseif(isset($htmls['*'])){ $html = $htmls['*']; // 全局静态规则 } if(!empty($html)) { // 解读静态规则 $rule = is_array($html)?$html[0]:$html; // 以$_开头的系统变量 $callback = function($match){ switch($match[1]){ case '_GET': $var = $_GET[$match[2]]; break; case '_POST': $var = $_POST[$match[2]]; break; case '_REQUEST': $var = $_REQUEST[$match[2]]; break; case '_SERVER': $var = $_SERVER[$match[2]]; break; case '_SESSION': $var = $_SESSION[$match[2]]; break; case '_COOKIE': $var = $_COOKIE[$match[2]]; break; } return (count($match) == 4) ? $match[3]($var) : $var; }; $rule = preg_replace_callback('/{\$(_\w+)\.(\w+)(?:\|(\w+))?}/', $callback, $rule); // {ID|FUN} GET变量的简写 $rule = preg_replace_callback('/{(\w+)\|(\w+)}/', function($match){return $match[2]($_GET[$match[1]]);}, $rule); $rule = preg_replace_callback('/{(\w+)}/', function($match){return $_GET[$match[1]];}, $rule); // 特殊系统变量 $rule = str_ireplace( array('{:controller}','{:action}','{:module}'), array(CONTROLLER_NAME,ACTION_NAME,MODULE_NAME), $rule); // {|FUN} 单独使用函数 $rule = preg_replace_callback('/{|(\w+)}/', function($match){return $match[1]();},$rule); $cacheTime = C('HTML_CACHE_TIME',null,60); if(is_array($html)){ if(!empty($html[2])) $rule = $html[2]($rule); // 应用附加函数 $cacheTime = isset($html[1])?$html[1]:$cacheTime; // 缓存有效期 }else{ $cacheTime = $cacheTime; } // 当前缓存文件 define('HTML_FILE_NAME',HTML_PATH . $rule.C('HTML_FILE_SUFFIX',null,'.html')); return $cacheTime; } } // 无需缓存 return false; } /** * 检查静态HTML文件是否有效 * 如果无效需要重新更新 * @access public * @param string $cacheFile 静态文件名 * @param integer $cacheTime 缓存有效期 * @return boolean */ static public function checkHTMLCache($cacheFile='',$cacheTime='') { if(!is_file($cacheFile) && 'sae' != APP_MODE ){ return false; }elseif (filemtime(\Think\Think::instance('Think\View')->parseTemplate()) > Storage::get($cacheFile,'mtime','html')) { // 模板文件如果更新静态文件需要更新 return false; }elseif(!is_numeric($cacheTime) && function_exists($cacheTime)){ return $cacheTime($cacheFile); }elseif ($cacheTime != 0 && NOW_TIME > Storage::get($cacheFile,'mtime','html')+$cacheTime) { // 文件是否在有效期 return false; } //静态文件有效 return true; } } ================================================ FILE: ThinkPHP/Library/Behavior/RobotCheckBehavior.class.php ================================================ // +---------------------------------------------------------------------- namespace Behavior; /** * 机器人检测 * @author liu21st */ class RobotCheckBehavior { public function run(&$params) { // 机器人访问检测 if(C('LIMIT_ROBOT_VISIT',null,true) && self::isRobot()) { // 禁止机器人访问 exit('Access Denied'); } } static private function isRobot() { static $_robot = null; if(is_null($_robot)) { $spiders = 'Bot|Crawl|Spider|slurp|sohu-search|lycos|robozilla'; $browsers = 'MSIE|Netscape|Opera|Konqueror|Mozilla'; if(preg_match("/($browsers)/", $_SERVER['HTTP_USER_AGENT'])) { $_robot = false ; } elseif(preg_match("/($spiders)/", $_SERVER['HTTP_USER_AGENT'])) { $_robot = true; } else { $_robot = false; } } return $_robot; } } ================================================ FILE: ThinkPHP/Library/Behavior/ShowPageTraceBehavior.class.php ================================================ // +---------------------------------------------------------------------- namespace Behavior; use Think\Log; /** * 系统行为扩展:页面Trace显示输出 */ class ShowPageTraceBehavior { protected $tracePageTabs = array('BASE'=>'基本','FILE'=>'文件','INFO'=>'流程','ERR|NOTIC'=>'错误','SQL'=>'SQL','DEBUG'=>'调试'); // 行为扩展的执行入口必须是run public function run(&$params){ if(!IS_AJAX && !IS_CLI && C('SHOW_PAGE_TRACE')) { echo $this->showTrace(); } } /** * 显示页面Trace信息 * @access private */ private function showTrace() { // 系统默认显示信息 $files = get_included_files(); $info = array(); foreach ($files as $key=>$file){ $info[] = $file.' ( '.number_format(filesize($file)/1024,2).' KB )'; } $trace = array(); $base = array( '请求信息' => date('Y-m-d H:i:s',$_SERVER['REQUEST_TIME']).' '.$_SERVER['SERVER_PROTOCOL'].' '.$_SERVER['REQUEST_METHOD'].' : '.__SELF__, '运行时间' => $this->showTime(), '吞吐率' => number_format(1/G('beginTime','viewEndTime'),2).'req/s', '内存开销' => MEMORY_LIMIT_ON?number_format((memory_get_usage() - $GLOBALS['_startUseMems'])/1024,2).' kb':'不支持', '查询信息' => N('db_query').' queries '.N('db_write').' writes ', '文件加载' => count(get_included_files()), '缓存信息' => N('cache_read').' gets '.N('cache_write').' writes ', '配置加载' => count(C()), '会话信息' => 'SESSION_ID='.session_id(), ); // 读取应用定义的Trace文件 $traceFile = COMMON_PATH.'Conf/trace.php'; if(is_file($traceFile)) { $base = array_merge($base,include $traceFile); } $debug = trace(); $tabs = C('TRACE_PAGE_TABS',null,$this->tracePageTabs); foreach ($tabs as $name=>$title){ switch(strtoupper($name)) { case 'BASE':// 基本信息 $trace[$title] = $base; break; case 'FILE': // 文件信息 $trace[$title] = $info; break; default:// 调试信息 $name = strtoupper($name); if(strpos($name,'|')) {// 多组信息 $names = explode('|',$name); $result = array(); foreach($names as $name){ $result += isset($debug[$name])?$debug[$name]:array(); } $trace[$title] = $result; }else{ $trace[$title] = isset($debug[$name])?$debug[$name]:''; } } } if($save = C('PAGE_TRACE_SAVE')) { // 保存页面Trace日志 if(is_array($save)) {// 选择选项卡保存 $tabs = C('TRACE_PAGE_TABS',null,$this->tracePageTabs); $array = array(); foreach ($save as $tab){ $array[] = $tabs[$tab]; } } $content = date('[ c ]').' '.get_client_ip().' '.$_SERVER['REQUEST_URI']."\r\n"; foreach ($trace as $key=>$val){ if(!isset($array) || in_array_case($key,$array)) { $content .= '[ '.$key." ]\r\n"; if(is_array($val)) { foreach ($val as $k=>$v){ $content .= (!is_numeric($k)?$k.':':'').print_r($v,true)."\r\n"; } }else{ $content .= print_r($val,true)."\r\n"; } $content .= "\r\n"; } } error_log(str_replace('
',"\r\n",$content), 3,C('LOG_PATH').date('y_m_d').'_trace.log'); } unset($files,$info,$base); // 调用Trace页面模板 ob_start(); include C('TMPL_TRACE_FILE')?C('TMPL_TRACE_FILE'):THINK_PATH.'Tpl/page_trace.tpl'; return ob_get_clean(); } /** * 获取运行时间 */ private function showTime() { // 显示运行时间 G('beginTime',$GLOBALS['_beginTime']); G('viewEndTime'); // 显示详细运行时间 return G('beginTime','viewEndTime').'s ( Load:'.G('beginTime','loadTime').'s Init:'.G('loadTime','initTime').'s Exec:'.G('initTime','viewStartTime').'s Template:'.G('viewStartTime','viewEndTime').'s )'; } } ================================================ FILE: ThinkPHP/Library/Behavior/ShowRuntimeBehavior.class.php ================================================ // +---------------------------------------------------------------------- namespace Behavior; /** * 系统行为扩展:运行时间信息显示 */ class ShowRuntimeBehavior { // 行为扩展的执行入口必须是run public function run(&$content){ if(C('SHOW_RUN_TIME')){ if(false !== strpos($content,'{__NORUNTIME__}')) { $content = str_replace('{__NORUNTIME__}','',$content); }else{ $runtime = $this->showTime(); if(strpos($content,'{__RUNTIME__}')) $content = str_replace('{__RUNTIME__}',$runtime,$content); else $content .= $runtime; } }else{ $content = str_replace(array('{__NORUNTIME__}','{__RUNTIME__}'),'',$content); } } /** * 显示运行时间、数据库操作、缓存次数、内存使用信息 * @access private * @return string */ private function showTime() { // 显示运行时间 G('beginTime',$GLOBALS['_beginTime']); G('viewEndTime'); $showTime = 'Process: '.G('beginTime','viewEndTime').'s '; if(C('SHOW_ADV_TIME')) { // 显示详细运行时间 $showTime .= '( Load:'.G('beginTime','loadTime').'s Init:'.G('loadTime','initTime').'s Exec:'.G('initTime','viewStartTime').'s Template:'.G('viewStartTime','viewEndTime').'s )'; } if(C('SHOW_DB_TIMES') ) { // 显示数据库操作次数 $showTime .= ' | DB :'.N('db_query').' queries '.N('db_write').' writes '; } if(C('SHOW_CACHE_TIMES') ) { // 显示缓存读写次数 $showTime .= ' | Cache :'.N('cache_read').' gets '.N('cache_write').' writes '; } if(MEMORY_LIMIT_ON && C('SHOW_USE_MEM')) { // 显示内存开销 $showTime .= ' | UseMem:'. number_format((memory_get_usage() - $GLOBALS['_startUseMems'])/1024).' kb'; } if(C('SHOW_LOAD_FILE')) { $showTime .= ' | LoadFile:'.count(get_included_files()); } if(C('SHOW_FUN_TIMES')) { $fun = get_defined_functions(); $showTime .= ' | CallFun:'.count($fun['user']).','.count($fun['internal']); } return $showTime; } } ================================================ FILE: ThinkPHP/Library/Behavior/TokenBuildBehavior.class.php ================================================ // +---------------------------------------------------------------------- namespace Behavior; /** * 系统行为扩展:表单令牌生成 */ class TokenBuildBehavior { public function run(&$content){ if(C('TOKEN_ON')) { list($tokenName,$tokenKey,$tokenValue)=$this->getToken(); $input_token = ''; $meta_token = ''; if(strpos($content,'{__TOKEN__}')) { // 指定表单令牌隐藏域位置 $content = str_replace('{__TOKEN__}',$input_token,$content); }elseif(preg_match('/<\/form(\s*)>/is',$content,$match)) { // 智能生成表单令牌隐藏域 $content = str_replace($match[0],$input_token.$match[0],$content); } $content = str_ireplace('',$meta_token.'',$content); }else{ $content = str_replace('{__TOKEN__}','',$content); } } //获得token private function getToken(){ $tokenName = C('TOKEN_NAME',null,'__hash__'); $tokenType = C('TOKEN_TYPE',null,'md5'); if(!isset($_SESSION[$tokenName])) { $_SESSION[$tokenName] = array(); } // 标识当前页面唯一性 $tokenKey = md5($_SERVER['REQUEST_URI']); if(isset($_SESSION[$tokenName][$tokenKey])) {// 相同页面不重复生成session $tokenValue = $_SESSION[$tokenName][$tokenKey]; }else{ $tokenValue = is_callable($tokenType) ? $tokenType(microtime(true)) : md5(microtime(true)); $_SESSION[$tokenName][$tokenKey] = $tokenValue; if(IS_AJAX && C('TOKEN_RESET',null,true)) header($tokenName.': '.$tokenKey.'_'.$tokenValue); //ajax需要获得这个header并替换页面中meta中的token值 } return array($tokenName,$tokenKey,$tokenValue); } } ================================================ FILE: ThinkPHP/Library/Behavior/UpgradeNoticeBehavior.class.php ================================================ // +---------------------------------------------------------------------- namespace Behavior; /** * 升级短信通知, 如果有ThinkPHP新版升级,或者重要的更新,会发送短信通知你。 * 需要使用SAE的短信服务。请先找一个SAE的应用开通短信服务。 * 使用步骤如下: * 1,在项目的Conf目录下建立tags.php配置文件,内容如下: * * array('UpgradeNotice') * ); * * * 2,将此文件放在应用的Lib/Behavior文件夹下。 *注:在SAE上面使用时,以上两步可以省略 * 3,在config.php中配置: * 'UPGRADE_NOTICE_ON'=>true,//开启短信升级提醒功能 * 'UPGRADE_NOTICE_AKEY'=>'your akey',//SAE应用的AKEY,如果在SAE上使用可以不填 * 'UPGRADE_NOTICE_SKEY'=>'your skey',//SAE应用的SKEY,如果在SAE上使用可以不填 *'UPGRADE_NOTICE_MOBILE'=>'136456789',//接受短信的手机号 *'UPGRADE_NOTICE_CHECK_INTERVAL' => 604800,//检测频率,单位秒,默认是一周 *'UPGRADE_CURRENT_VERSION'=>'0',//升级后的版本号,会在短信中告诉你填写什么 *UPGRADE_NOTICE_DEBUG=>true, //调试默认,如果为true,UPGRADE_NOTICE_CHECK_INTERVAL配置不起作用,每次都会进行版本检查,此时用于调试,调试完毕后请设置次配置为false * */ class UpgradeNoticeBehavior { protected $header_ = ''; protected $httpCode_; protected $httpDesc_; protected $accesskey_; protected $secretkey_; public function run(&$params) { if (C('UPGRADE_NOTICE_ON') && (!S('think_upgrade_interval') || C('UPGRADE_NOTICE_DEBUG'))) { if(IS_SAE && C('UPGRADE_NOTICE_QUEUE') && !isset($_POST['think_upgrade_queque'])){ $queue=new SaeTaskQueue(C('UPGRADE_NOTICE_QUEUE')); $queue->addTask('http://'.$_SERVER['HTTP_HOST'].__APP__,'think_upgrade_queque=1'); if(!$queue->push()){ trace('升级提醒队列执行失败,错误原因:'.$queue->errmsg(), '升级通知出错', 'NOTIC', true); } return ; } $akey = C('UPGRADE_NOTICE_AKEY',null,''); $skey = C('UPGRADE_NOTICE_SKEY',null,''); $this->accesskey_ = $akey ? $akey : (defined('SAE_ACCESSKEY') ? SAE_ACCESSKEY : ''); $this->secretkey_ = $skey ? $skey : (defined('SAE_SECRETKEY') ? SAE_SECRETKEY : ''); $current_version = C('UPGRADE_CURRENT_VERSION',null,0); //读取接口 $info = $this->send('http://sinaclouds.sinaapp.com/thinkapi/upgrade.php?v=' . $current_version); if ($info['version'] != $current_version) { if($this->send_sms($info['msg'])) trace($info['msg'], '升级通知成功', 'NOTIC', true); //发送升级短信 } S('think_upgrade_interval', true, C('UPGRADE_NOTICE_CHECK_INTERVAL',null,604800)); } } private function send_sms($msg) { $timestamp=time(); $url = 'http://inno.smsinter.sina.com.cn/sae_sms_service/sendsms.php'; //发送短信的接口地址 $content = "FetchUrl" . $url . "TimeStamp" . $timestamp . "AccessKey" . $this->accesskey_; $signature = (base64_encode(hash_hmac('sha256', $content, $this->secretkey_, true))); $headers = array( "FetchUrl: $url", "AccessKey: ".$this->accesskey_, "TimeStamp: " . $timestamp, "Signature: $signature" ); $data = array( 'mobile' => C('UPGRADE_NOTICE_MOBILE',null,'') , 'msg' => $msg, 'encoding' => 'UTF-8' ); if(!$ret = $this->send('http://g.apibus.io', $data, $headers)){ return false; } if (isset($ret['ApiBusError'])) { trace('errno:' . $ret['ApiBusError']['errcode'] . ',errmsg:' . $ret['ApiBusError']['errdesc'], '升级通知出错', 'NOTIC', true); return false; } return true; } private function send($url, $params = array() , $headers = array()) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); if (!empty($params)) { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $params); } if (!empty($headers)) curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $txt = curl_exec($ch); if (curl_errno($ch)) { trace(curl_error($ch) , '升级通知出错', 'NOTIC', true); return false; } curl_close($ch); $ret = json_decode($txt, true); if (!$ret) { trace('接口[' . $url . ']返回格式不正确', '升级通知出错', 'NOTIC', true); return false; } return $ret; } } ================================================ FILE: ThinkPHP/Library/Behavior/WriteHtmlCacheBehavior.class.php ================================================ // +---------------------------------------------------------------------- namespace Behavior; use Think\Storage; /** * 系统行为扩展:静态缓存写入 */ class WriteHtmlCacheBehavior { // 行为扩展的执行入口必须是run public function run(&$content) { //2014-11-28 修改 如果有HTTP 4xx 3xx 5xx 头部,禁止存储 //2014-12-1 修改 对注入的网址 防止生成,例如 /game/lst/SortType/hot/-e8-90-8c-e5-85-94-e7-88-b1-e6-b6-88-e9-99-a4/-e8-bf-9b-e5-87-bb-e7-9a-84-e9-83-a8-e8-90-bd/-e9-a3-8e-e4-ba-91-e5-a4-a9-e4-b8-8b/index.shtml if (C('HTML_CACHE_ON') && defined('HTML_FILE_NAME') && !preg_match('/Status.*[345]{1}\d{2}/i', implode(' ', headers_list())) && !preg_match('/(-[a-z0-9]{2}){3,}/i',HTML_FILE_NAME)) { //静态文件写入 Storage::put(HTML_FILE_NAME, $content, 'html'); } } } ================================================ FILE: ThinkPHP/Library/Com/Wechat.class.php ================================================ // +---------------------------------------------------------------------- namespace Com; use Com\WechatCrypt; //支持在非ThinkPHP环境下使用 defined('NOW_TIME') || define('NOW_TIME', $_SERVER['REQUEST_TIME']); defined('IS_GET') || define('IS_GET', $_SERVER['REQUEST_METHOD'] == 'GET'); class Wechat { /** * 消息类型常量 */ const MSG_TYPE_TEXT = 'text'; const MSG_TYPE_IMAGE = 'image'; const MSG_TYPE_VOICE = 'voice'; const MSG_TYPE_VIDEO = 'video'; const MSG_TYPE_SHORTVIDEO = 'shortvideo'; const MSG_TYPE_LOCATION = 'location'; const MSG_TYPE_LINK = 'link'; const MSG_TYPE_MUSIC = 'music'; const MSG_TYPE_NEWS = 'news'; const MSG_TYPE_EVENT = 'event'; /** * 事件类型常量 */ const MSG_EVENT_SUBSCRIBE = 'subscribe'; const MSG_EVENT_UNSUBSCRIBE = 'unsubscribe'; const MSG_EVENT_SCAN = 'SCAN'; const MSG_EVENT_LOCATION = 'LOCATION'; const MSG_EVENT_CLICK = 'CLICK'; const MSG_EVENT_VIEW = 'VIEW'; /** * 微信推送过来的数据 * @var array */ private $data = array(); /** * 微信TOKEN * @var string */ private static $token = ''; /** * 微信APP_ID * @var string */ private static $appId = ''; /** * 消息加密KEY * @var string */ private static $encodingAESKey = ''; /** * 是否使用安全模式 * @var boolean */ private static $msgSafeMode = false; /** * 构造方法,用于实例化微信SDK * 自动回复消息时实例化该SDK * @param string $token 微信后台填写的TOKEN * @param string $appid 微信APPID (安全模式和兼容模式有效) * @param string $key 消息加密KEY (EncodingAESKey) */ public function __construct($token, $appid = '', $key = ''){ //设置安全模式 if(isset($_GET['encrypt_type']) && $_GET['encrypt_type'] == 'aes'){ self::$msgSafeMode = true; } //参数验证 if(self::$msgSafeMode){ if(empty($key) || empty($appid)){ throw new \Exception('缺少参数EncodingAESKey或APP_ID!'); } self::$appId = $appid; self::$encodingAESKey = $key; } //TOKEN验证 if($token){ self::auth($token) || exit; if(IS_GET){ exit($_GET['echostr']); } else { self::$token = $token; $this->init(); } } else { throw new \Exception('缺少参数TOKEN!'); } } /** * 初始化微信推送的数据 */ private function init(){ $xml = file_get_contents("php://input"); $data = self::xml2data($xml); //安全模式 或兼容模式 if(self::$msgSafeMode){ if(isset($data['MsgType'])){ //兼容模式追加解密后的消息内容 $data['Decrypt'] = self::extract($data['Encrypt']); } else { //安全模式 $data = self::extract($data['Encrypt']); } } $this->data = $data; } /** * 获取微信推送的数据 * @return array 转换为数组后的数据 */ public function request(){ return $this->data; } /** * * 响应微信发送的信息(自动回复) * @param array $content 回复信息,文本信息为string类型 * @param string $type 消息类型 */ public function response($content, $type = self::MSG_TYPE_TEXT){ /* 基础数据 */ $data = array( 'ToUserName' => $this->data['FromUserName'], 'FromUserName' => $this->data['ToUserName'], 'CreateTime' => NOW_TIME, 'MsgType' => $type, ); /* 按类型添加额外数据 */ $content = call_user_func(array(self, $type), $content); if($type == self::MSG_TYPE_TEXT || $type == self::MSG_TYPE_NEWS){ $data = array_merge($data, $content); } else { $data[ucfirst($type)] = $content; } //安全模式,加密消息内容 if(self::$msgSafeMode){ $data = self::generate($data); } /* 转换数据为XML */ $xml = new \SimpleXMLElement(''); self::data2xml($xml, $data); exit($xml->asXML()); } /** * 回复文本消息 * @param string $text 回复的文字 */ public function replyText($text){ return $this->response($text, self::MSG_TYPE_TEXT); } /** * 回复图片消息 * @param string $media_id 图片ID */ public function replyImage($media_id){ return $this->response($media_id, self::MSG_TYPE_IMAGE); } /** * 回复语音消息 * @param string $media_id 音频ID */ public function replyVoice($media_id){ return $this->response($media_id, self::MSG_TYPE_VOICE); } /** * 回复视频消息 * @param string $media_id 视频ID * @param string $title 视频标题 * @param string $discription 视频描述 */ public function replyVideo($media_id, $title, $discription){ return $this->response(func_get_args(), self::MSG_TYPE_VIDEO); } /** * 回复音乐消息 * @param string $title 音乐标题 * @param string $discription 音乐描述 * @param string $musicurl 音乐链接 * @param string $hqmusicurl 高品质音乐链接 * @param string $thumb_media_id 缩略图ID */ public function replyMusic($title, $discription, $musicurl, $hqmusicurl, $thumb_media_id){ return $this->response(func_get_args(), self::MSG_TYPE_MUSIC); } /** * 回复图文消息,一个参数代表一条信息 * @param array $news 图文内容 [标题,描述,URL,缩略图] * @param array $news1 图文内容 [标题,描述,URL,缩略图] * @param array $news2 图文内容 [标题,描述,URL,缩略图] * ... ... * @param array $news9 图文内容 [标题,描述,URL,缩略图] */ public function replyNews($news, $news1, $news2, $news3){ return $this->response(func_get_args(), self::MSG_TYPE_NEWS); } /** * 回复一条图文消息 * @param string $title 文章标题 * @param string $discription 文章简介 * @param string $url 文章连接 * @param string $picurl 文章缩略图 */ public function replyNewsOnce($title, $discription, $url, $picurl){ return $this->response(array(func_get_args()), self::MSG_TYPE_NEWS); } /** * 数据XML编码 * @param object $xml XML对象 * @param mixed $data 数据 * @param string $item 数字索引时的节点名称 * @return string */ protected static function data2xml($xml, $data, $item = 'item') { foreach ($data as $key => $value) { /* 指定默认的数字key */ is_numeric($key) && $key = $item; /* 添加子元素 */ if(is_array($value) || is_object($value)){ $child = $xml->addChild($key); self::data2xml($child, $value, $item); } else { if(is_numeric($value)){ $child = $xml->addChild($key, $value); } else { $child = $xml->addChild($key); $node = dom_import_simplexml($child); $cdata = $node->ownerDocument->createCDATASection($value); $node->appendChild($cdata); } } } } /** * XML数据解码 * @param string $xml 原始XML字符串 * @return array 解码后的数组 */ protected static function xml2data($xml){ $xml = new \SimpleXMLElement($xml); if(!$xml){ throw new \Exception('非法XXML'); } $data = array(); foreach ($xml as $key => $value) { $data[$key] = strval($value); } return $data; } /** * 对数据进行签名认证,确保是微信发送的数据 * @param string $token 微信开放平台设置的TOKEN * @return boolean true-签名正确,false-签名错误 */ protected static function auth($token){ /* 获取数据 */ $data = array($_GET['timestamp'], $_GET['nonce'], $token); $sign = $_GET['signature']; /* 对数据进行字典排序 */ sort($data, SORT_STRING); /* 生成签名 */ $signature = sha1(implode($data)); return $signature === $sign; } /** * 构造文本信息 * @param string $content 要回复的文本 */ private static function text($content){ $data['Content'] = $content; return $data; } /** * 构造图片信息 * @param integer $media 图片ID */ private static function image($media){ $data['MediaId'] = $media; return $data; } /** * 构造音频信息 * @param integer $media 语音ID */ private static function voice($media){ $data['MediaId'] = $media; return $data; } /** * 构造视频信息 * @param array $video 要回复的视频 [视频ID,标题,说明] */ private static function video($video){ $data = array(); list( $data['MediaId'], $data['Title'], $data['Description'], ) = $video; return $data; } /** * 构造音乐信息 * @param array $music 要回复的音乐[标题,说明,链接,高品质链接,缩略图ID] */ private static function music($music){ $data = array(); list( $data['Title'], $data['Description'], $data['MusicUrl'], $data['HQMusicUrl'], $data['ThumbMediaId'], ) = $music; return $data; } /** * 构造图文信息 * @param array $news 要回复的图文内容 * [ * 0 => 第一条图文信息[标题,说明,图片链接,全文连接], * 1 => 第二条图文信息[标题,说明,图片链接,全文连接], * 2 => 第三条图文信息[标题,说明,图片链接,全文连接], * ] */ private static function news($news){ $articles = array(); foreach ($news as $key => $value) { list( $articles[$key]['Title'], $articles[$key]['Description'], $articles[$key]['Url'], $articles[$key]['PicUrl'] ) = $value; if($key >= 9) break; //最多只允许10条图文信息 } $data['ArticleCount'] = count($articles); $data['Articles'] = $articles; return $data; } /** * 验证并解密密文数据 * @param string $encrypt 密文 * @return array 解密后的数据 */ private static function extract($encrypt){ //验证数据签名 $signature = self::sign($_GET['timestamp'], $_GET['nonce'], $encrypt); if($signature != $_GET['msg_signature']){ throw new \Exception('数据签名错误!'); } //消息解密对象 $WechatCrypt = new WechatCrypt(self::$encodingAESKey, self::$appId); //解密得到回明文消息 $decrypt = $WechatCrypt->decrypt($encrypt); //返回解密的数据 return self::xml2data($decrypt); } /** * 加密并生成密文消息数据 * @param array $data 获取到的加密的消息数据 * @return array 生成的加密消息结构 */ private static function generate($data){ /* 转换数据为XML */ $xml = new \SimpleXMLElement(''); self::data2xml($xml, $data); $xml = $xml->asXML(); //消息加密对象 $WechatCrypt = new WechatCrypt(self::$encodingAESKey, self::$appId); //加密得到密文消息 $encrypt = $WechatCrypt->encrypt($xml); //签名 $nonce = mt_rand(0, 9999999999); $signature = self::sign(NOW_TIME, $nonce, $encrypt); /* 加密消息基础数据 */ $data = array( 'Encrypt' => $encrypt, 'MsgSignature' => $signature, 'TimeStamp' => NOW_TIME, 'Nonce' => $nonce, ); return $data; } /** * 生成数据签名 * @param string $timestamp 时间戳 * @param string $nonce 随机数 * @param string $encrypt 被签名的数据 * @return string SHA1签名 */ private static function sign($timestamp, $nonce, $encrypt){ $sign = array(self::$token, $timestamp, $nonce, $encrypt); sort($sign, SORT_STRING); return sha1(implode($sign)); } } ================================================ FILE: ThinkPHP/Library/Com/WechatAuth.class.php ================================================ // +---------------------------------------------------------------------- namespace Com; class WechatAuth { /* 消息类型常量 */ const MSG_TYPE_TEXT = 'text'; const MSG_TYPE_IMAGE = 'image'; const MSG_TYPE_VOICE = 'voice'; const MSG_TYPE_VIDEO = 'video'; const MSG_TYPE_SHORTVIDEO = 'shortvideo'; const MSG_TYPE_LOCATION = 'location'; const MSG_TYPE_LINK = 'link'; const MSG_TYPE_MUSIC = 'music'; const MSG_TYPE_NEWS = 'news'; const MSG_TYPE_EVENT = 'event'; /* 二维码类型常量 */ const QR_SCENE = 'QR_SCENE'; const QR_LIMIT_SCENE = 'QR_LIMIT_SCENE'; /** * 微信开发者申请的appID * @var string */ private $appId = ''; /** * 微信开发者申请的appSecret * @var string */ private $appSecret = ''; /** * 获取到的access_token * @var string */ private $accessToken = ''; /** * 微信api根路径 * @var string */ private $apiURL = 'https://api.weixin.qq.com/cgi-bin'; /** * 微信二维码根路径 * @var string */ private $qrcodeURL = 'https://mp.weixin.qq.com/cgi-bin'; private $requestCodeURL = 'https://open.weixin.qq.com/connect/oauth2/authorize'; private $oauthApiURL = 'https://api.weixin.qq.com/sns'; /** * 构造方法,调用微信高级接口时实例化SDK * @param string $appid 微信appid * @param string $secret 微信appsecret * @param string $token 获取到的access_token */ public function __construct($appid, $secret, $token = null){ if($appid && $secret){ $this->appId = $appid; $this->appSecret = $secret; if(!empty($token)){ $this->accessToken = $token; } } else { throw new \Exception('缺少参数 APP_ID 和 APP_SECRET!'); } } public function getRequestCodeURL($redirect_uri, $state = null, $scope = 'snsapi_userinfo'){ $query = array( 'appid' => $this->appId, 'redirect_uri' => $redirect_uri, 'response_type' => 'code', 'scope' => $scope, ); if(!is_null($state) && preg_match('/[a-zA-Z0-9]+/', $state)){ $query['state'] = $state; } $query = http_build_query($query); return "{$this->requestCodeURL}?{$query}#wechat_redirect"; } /** * 获取access_token,用于后续接口访问 * @return array access_token信息,包含 token 和有效期 */ public function getAccessToken($type = 'client', $code = null){ $param = array( 'appid' => $this->appId, 'secret' => $this->appSecret ); switch ($type) { case 'client': $param['grant_type'] = 'client_credential'; $url = "{$this->apiURL}/token"; break; case 'code': $param['code'] = $code; $param['grant_type'] = 'authorization_code'; $url = "{$this->oauthApiURL}/oauth2/access_token"; break; default: throw new \Exception('不支持的grant_type类型!'); break; } $token = self::http($url, $param); $token = json_decode($token, true); if(is_array($token)){ if(isset($token['errcode'])){ throw new \Exception($token['errmsg']); } else { $this->accessToken = $token['access_token']; return $token; } } else { throw new \Exception('获取微信access_token失败!'); } } /** * 获取授权用户信息 * @param string $openid 用户的OpenID * @param string $lang 指定的语言 * @return array 用户信息数据,具体参见微信文档 */ public function getUserInfo($openid, $lang = 'zh_CN'){ $query = array( 'access_token' => $this->accessToken, 'openid' => $openid, 'lang' => $lang, ); $info = self::http("{$this->oauthApiURL}/userinfo", $query); return json_decode($info, true); } /** * 上传零时媒体资源 * @param string $filename 媒体资源本地路径 * @param string $type 媒体资源类型,具体请参考微信开发手册 */ public function mediaUpload($filename, $type){ $filename = realpath($filename); if(!$filename) throw new \Exception('资源路径错误!'); $data = array( 'type' => $type, 'media' => "@{$filename}" ); return $this->api('media/upload', $data, 'POST', '', false); } /** * 上传永久媒体资源 * @param string $filename 媒体资源本地路径 * @param string $type 媒体资源类型,具体请参考微信开发手册 * @param string $description 资源描述,仅资源类型为 video 时有效 */ public function materialAddMaterial($filename, $type, $description = ''){ $filename = realpath($filename); if(!$filename) throw new \Exception('资源路径错误!'); $data = array( 'type' => $type, 'media' => "@{$filename}", ); if($type == 'video'){ if(is_array($description)){ //保护中文,微信api不支持中文转义的json结构 array_walk_recursive($description, function(&$value){ $value = urlencode($value); }); $description = urldecode(json_encode($description)); } $data['description'] = $description; } return $this->api('material/add_material', $data, 'POST', '', false); } /** * 获取媒体资源下载地址 * 注意:视频资源不允许下载 * @param string $media_id 媒体资源id * @return string 媒体资源下载地址 */ public function mediaGet($media_id){ $param = array( 'access_token' => $this->accessToken, 'media_id' => $media_id ); $url = "{$this->apiURL}/media/get?"; return $url . http_build_query($param); } /** * 给指定用户推送信息 * 注意:微信规则只允许给在48小时内给公众平台发送过消息的用户推送信息 * @param string $openid 用户的openid * @param array $content 发送的数据,不同类型的数据结构可能不同 * @param string $type 推送消息类型 */ public function messageCustomSend($openid, $content, $type = self::MSG_TYPE_TEXT){ //基础数据 $data = array( 'touser'=>$openid, 'msgtype'=>$type, ); //根据类型附加额外数据 $data[$type] = call_user_func(array(self, $type), $content); return $this->api('message/custom/send', $data); } /** * 发送文本消息 * @param string $openid 用户的openid * @param string $text 发送的文字 */ public function sendText($openid, $text){ return $this->messageCustomSend($openid, $text, self::MSG_TYPE_TEXT); } /** * 发送图片消息 * @param string $openid 用户的openid * @param string $media 图片ID */ public function sendImage($openid, $media){ return $this->messageCustomSend($openid, $media, self::MSG_TYPE_IMAGE); } /** * 发送语音消息 * @param string $openid 用户的openid * @param string $media 音频ID */ public function sendVoice($openid, $media){ return $this->messageCustomSend($openid, $media, self::MSG_TYPE_VOICE); } /** * 发送视频消息 * @param string $openid 用户的openid * @param string $media_id 视频ID * @param string $title 视频标题 * @param string $discription 视频描述 */ public function sendVideo(){ $video = func_get_args(); $openid = array_shift($video); return $this->messageCustomSend($openid, $video, self::MSG_TYPE_VIDEO); } /** * 发送音乐消息 * @param string $openid 用户的openid * @param string $title 音乐标题 * @param string $discription 音乐描述 * @param string $musicurl 音乐链接 * @param string $hqmusicurl 高品质音乐链接 * @param string $thumb_media_id 缩略图ID */ public function sendMusic(){ $music = func_get_args(); $openid = array_shift($music); return $this->messageCustomSend($openid, $music, self::MSG_TYPE_MUSIC); } /** * 发送图文消息 * @param string $openid 用户的openid * @param array $news 图文内容 [标题,描述,URL,缩略图] * @param array $news1 图文内容 [标题,描述,URL,缩略图] * @param array $news2 图文内容 [标题,描述,URL,缩略图] * ... ... * @param array $news9 图文内容 [标题,描述,URL,缩略图] */ public function sendNews(){ $news = func_get_args(); $openid = array_shift($news); return $this->messageCustomSend($openid, $news, self::MSG_TYPE_NEWS); } /** * 发送一条图文消息 * @param string $openid 用户的openid * @param string $title 文章标题 * @param string $discription 文章简介 * @param string $url 文章连接 * @param string $picurl 文章缩略图 */ public function sendNewsOnce(){ $news = func_get_args(); $openid = array_shift($news); $news = array($news); return $this->messageCustomSend($openid, $news, self::MSG_TYPE_NEWS); } /** * 创建用户组 * @param string $name 组名称 */ public function groupsCreate($name){ $data = array('group' => array('name' => $name)); return $this->api('groups/create', $data); } /** * 查询所有分组 * @return array 分组列表 */ public function groupsGet(){ return $this->api('groups/get', '', 'GET'); } /** * 查询用户所在的分组 * @param string $openid 用户的OpenID * @return number 分组ID */ public function groupsGetid($openid){ $data = array('openid' => $openid); return $this->api('groups/getid', $data); } /** * 修改分组 * @param number $id 分组ID * @param string $name 分组名称 * @return array 修改成功或失败信息 */ public function groupsUpdate($id, $name){ $data = array('id' => $id, 'name' => $name); return $this->api('groups/update', $data); } /** * 移动用户分组 * @param string $openid 用户的OpenID * @param number $to_groupid 要移动到的分组ID * @return array 移动成功或失败信息 */ public function groupsMemberUpdate($openid, $to_groupid){ $data = array('openid' => $openid, 'to_groupid' => $to_groupid); return $this->api('groups/member/update', $data); } /** * 用户设备注名 * @param string $openid 用户的OpenID * @param string $remark 设备注名 * @return array 执行成功失败信息 */ public function userInfoUpdateremark($openid, $remark){ $data = array('openid' => $openid, 'remark' => $remark); return $this->api('user/info/updateremark', $data); } /** * 获取指定用户的详细信息 * @param string $openid 用户的openid * @param string $lang 需要获取数据的语言 */ public function userInfo($openid, $lang = 'zh_CN'){ $param = array('openid' => $openid, 'lang' => $lang); return $this->api('user/info', '', 'GET', $param); } /** * 获取关注者列表 * @param string $next_openid 下一个openid,在用户数大于10000时有效 * @return array 用户列表 */ public function userGet($next_openid = ''){ $param = array('next_openid' => $next_openid); return $this->api('user/get', '', 'GET', $param); } /** * 创建自定义菜单 * @param array $button 符合规则的菜单数组,规则参见微信手册 */ public function menuCreate($button){ $data = array('button' => $button); return $this->api('menu/create', $data); } /** * 获取所有的自定义菜单 * @return array 自定义菜单数组 */ public function menuGet(){ return $this->api('menu/get', '', 'GET'); } /** * 删除自定义菜单 */ public function menuDelete(){ return $this->api('menu/delete', '', 'GET'); } /** * 创建二维码,可创建指定有效期的二维码和永久二维码 * @param integer $scene_id 二维码参数 * @param integer $expire_seconds 二维码有效期,0-永久有效 */ public function qrcodeCreate($scene_id, $expire_seconds = 0){ $data = array(); if(is_numeric($expire_seconds) && $expire_seconds > 0){ $data['expire_seconds'] = $expire_seconds; $data['action_name'] = self::QR_SCENE; } else { $data['action_name'] = self::QR_LIMIT_SCENE; } $data['action_info']['scene']['scene_id'] = $scene_id; return $this->api('qrcode/create', $data); } /** * 根据ticket获取二维码URL * @param string $ticket 通过 qrcodeCreate接口获取到的ticket * @return string 二维码URL */ public function showqrcode($ticket){ return "{$this->qrcodeURL}/showqrcode?ticket={$ticket}"; } /** * 长链接转短链接 * @param string $long_url 长链接 * @return string 短链接 */ public function shorturl($long_url){ $data = array( 'action' => 'long2short', 'long_url' => $long_url ); return $this->api('shorturl', $data); } /** * 调用微信api获取响应数据 * @param string $name API名称 * @param string $data POST请求数据 * @param string $method 请求方式 * @param string $param GET请求参数 * @return array api返回结果 */ protected function api($name, $data = '', $method = 'POST', $param = '', $json = true){ $params = array('access_token' => $this->accessToken); if(!empty($param) && is_array($param)){ $params = array_merge($params, $param); } $url = "{$this->apiURL}/{$name}"; if($json && !empty($data)){ //保护中文,微信api不支持中文转义的json结构 array_walk_recursive($data, function(&$value){ $value = urlencode($value); }); $data = urldecode(json_encode($data)); } $data = self::http($url, $params, $data, $method); return json_decode($data, true); } /** * 发送HTTP请求方法,目前只支持CURL发送请求 * @param string $url 请求URL * @param array $param GET参数数组 * @param array $data POST的数据,GET请求时该参数无效 * @param string $method 请求方法GET/POST * @return array 响应数据 */ protected static function http($url, $param, $data = '', $method = 'GET'){ $opts = array( CURLOPT_TIMEOUT => 30, CURLOPT_RETURNTRANSFER => 1, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, ); /* 根据请求类型设置特定参数 */ $opts[CURLOPT_URL] = $url . '?' . http_build_query($param); if(strtoupper($method) == 'POST'){ $opts[CURLOPT_POST] = 1; $opts[CURLOPT_POSTFIELDS] = $data; if(is_string($data)){ //发送JSON数据 $opts[CURLOPT_HTTPHEADER] = array( 'Content-Type: application/json; charset=utf-8', 'Content-Length: ' . strlen($data), ); } } /* 初始化并执行curl请求 */ $ch = curl_init(); curl_setopt_array($ch, $opts); $data = curl_exec($ch); $error = curl_error($ch); curl_close($ch); //发生错误,抛出异常 if($error) throw new \Exception('请求发生错误:' . $error); return $data; } /** * 构造文本信息 * @param string $content 要回复的文本 */ private static function text($content){ $data['content'] = $content; return $data; } /** * 构造图片信息 * @param integer $media 图片ID */ private static function image($media){ $data['media_id'] = $media; return $data; } /** * 构造音频信息 * @param integer $media 语音ID */ private static function voice($media){ $data['media_id'] = $media; return $data; } /** * 构造视频信息 * @param array $video 要回复的视频 [视频ID,标题,说明] */ private static function video($video){ $data = array(); list( $data['media_id'], $data['title'], $data['description'], ) = $video; return $data; } /** * 构造音乐信息 * @param array $music 要回复的音乐[标题,说明,链接,高品质链接,缩略图ID] */ private static function music($music){ $data = array(); list( $data['title'], $data['description'], $data['musicurl'], $data['hqmusicurl'], $data['thumb_media_id'], ) = $music; return $data; } /** * 构造图文信息 * @param array $news 要回复的图文内容 * [ * 0 => 第一条图文信息[标题,说明,图片链接,全文连接], * 1 => 第二条图文信息[标题,说明,图片链接,全文连接], * 2 => 第三条图文信息[标题,说明,图片链接,全文连接], * ] */ private static function news($news){ $articles = array(); foreach ($news as $key => $value) { list( $articles[$key]['title'], $articles[$key]['description'], $articles[$key]['url'], $articles[$key]['picurl'] ) = $value; if($key >= 9) break; //最多只允许10条图文信息 } $data['articles'] = $articles; return $data; } } ================================================ FILE: ThinkPHP/Library/Com/WechatCrypt.class.php ================================================ // +---------------------------------------------------------------------- namespace Com; class WechatCrypt{ /** * 加密KEY * @var string */ private $cyptKey = ''; /** * 公众平台APPID * @var string */ private $appId = ''; /** * 构造方法,初始化加密KEY * @param string $key 加密KEY * @param string $appid 微信APP_KEY */ public function __construct($key, $appid){ if($key && $appid){ $this->appId = $appid; $this->cyptKey = base64_decode($key . '='); } else { throw new \Exception('缺少参数 APP_ID 和加密KEY!'); } } /** * 对明文进行加密 * @param string $text 需要加密的字符串 * @return string 密文字符串 */ public function encrypt($text){ //填充到明文之前的随机字符 $random = self::getRandomStr(16); //网络字节序 $size = pack("N", strlen($text)); //生成被加密字符串 $text = $random . $size . $text . $this->appId; //打开加密算法模块 $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); //使用PKCS7对明文进行补位 $text = self::PKCS7Encode($text, mcrypt_enc_get_key_size($td)); //初始化加密算法模块 mcrypt_generic_init($td, $this->cyptKey, substr($this->cyptKey, 0, 16)); //执行加密 $encrypt = mcrypt_generic($td, $text); //关闭加密算法模块 mcrypt_generic_deinit($td); mcrypt_module_close($td); //输出密文 return base64_encode($encrypt); } /** * 对密文进行解密 * @param string $encrypt 密文 * @return string 明文 */ public function decrypt($encrypt){ //BASE64解码 $encrypt = base64_decode($encrypt); //打开加密算法模块 $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); //初始化加密算法模块 mcrypt_generic_init($td, $this->cyptKey, substr($this->cyptKey, 0, 16)); //执行解密 $decrypt = mdecrypt_generic($td, $encrypt); //去除PKCS7补位 $decrypt = self::PKCS7Decode($decrypt, mcrypt_enc_get_key_size($td)); //关闭加密算法模块 mcrypt_generic_deinit($td); mcrypt_module_close($td); if(strlen($decrypt) < 16){ throw new \Exception("非法密文字符串!"); } //去除随机字符串 $decrypt = substr($decrypt, 16); //获取网络字节序 $size = unpack("N", substr($decrypt, 0, 4)); $size = $size[1]; //APP_ID $appid = substr($decrypt, $size + 4); //验证APP_ID if($appid !== $this->appId){ throw new \Exception("非法APP_ID!"); } //明文内容 $text = substr($decrypt, 4, $size); return $text; } /** * PKCS7填充字符 * @param string $text 被填充字符 * @param integer $size Block长度 */ private static function PKCS7Encode($text, $size){ //字符串长度 $str_size = strlen($text); //填充长度 $pad_size = $size - ($str_size % $size); $pad_size = $pad_size ? : $size; //填充的字符 $pad_chr = chr($pad_size); //执行填充 $text = str_pad($text, $str_size + $pad_size, $pad_chr, STR_PAD_RIGHT); return $text; } /** * 删除PKCS7填充的字符 * @param string $text 已填充的字符 * @param integer $size Block长度 */ private static function PKCS7Decode($text, $size){ //获取补位字符 $pad_str = ord(substr($text, -1)); if ($pad_str < 1 || $pad_str > $size) { return ''; } else { return substr($text, 0, strlen($text) - $pad_str); } } /** * 生成指定长度的字符串 * @param integer $len 字符串长度 * @return string 生成的字符串 */ private static function getRandomStr($len){ static $pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz"; $str = ''; $max = strlen($pol) - 1; for ($i = 0; $i < $len; $i++) { $str .= $pol[mt_rand(0, $max)]; } return $str; } } ================================================ FILE: ThinkPHP/Library/Org/Net/Http.class.php ================================================ // +---------------------------------------------------------------------- namespace Org\Net; /** * Http 工具类 * 提供一系列的Http方法 * @author liu21st */ class Http { /** * 采集远程文件 * @access public * @param string $remote 远程文件名 * @param string $local 本地保存文件名 * @return mixed */ static public function curlDownload($remote,$local) { $cp = curl_init($remote); $fp = fopen($local,"w"); curl_setopt($cp, CURLOPT_FILE, $fp); curl_setopt($cp, CURLOPT_HEADER, 0); curl_exec($cp); curl_close($cp); fclose($fp); } /** * 使用 fsockopen 通过 HTTP 协议直接访问(采集)远程文件 * 如果主机或服务器没有开启 CURL 扩展可考虑使用 * fsockopen 比 CURL 稍慢,但性能稳定 * @static * @access public * @param string $url 远程URL * @param array $conf 其他配置信息 * int limit 分段读取字符个数 * string post post的内容,字符串或数组,key=value&形式 * string cookie 携带cookie访问,该参数是cookie内容 * string ip 如果该参数传入,$url将不被使用,ip访问优先 * int timeout 采集超时时间 * bool block 是否阻塞访问,默认为true * @return mixed */ static public function fsockopenDownload($url, $conf = array()) { $return = ''; if(!is_array($conf)) return $return; $matches = parse_url($url); !isset($matches['host']) && $matches['host'] = ''; !isset($matches['path']) && $matches['path'] = ''; !isset($matches['query']) && $matches['query'] = ''; !isset($matches['port']) && $matches['port'] = ''; $host = $matches['host']; $path = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/'; $port = !empty($matches['port']) ? $matches['port'] : 80; $conf_arr = array( 'limit' => 0, 'post' => '', 'cookie' => '', 'ip' => '', 'timeout' => 15, 'block' => TRUE, ); foreach (array_merge($conf_arr, $conf) as $k=>$v) ${$k} = $v; if($post) { if(is_array($post)) { $post = http_build_query($post); } $out = "POST $path HTTP/1.0\r\n"; $out .= "Accept: */*\r\n"; //$out .= "Referer: $boardurl\r\n"; $out .= "Accept-Language: zh-cn\r\n"; $out .= "Content-Type: application/x-www-form-urlencoded\r\n"; $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n"; $out .= "Host: $host\r\n"; $out .= 'Content-Length: '.strlen($post)."\r\n"; $out .= "Connection: Close\r\n"; $out .= "Cache-Control: no-cache\r\n"; $out .= "Cookie: $cookie\r\n\r\n"; $out .= $post; } else { $out = "GET $path HTTP/1.0\r\n"; $out .= "Accept: */*\r\n"; //$out .= "Referer: $boardurl\r\n"; $out .= "Accept-Language: zh-cn\r\n"; $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n"; $out .= "Host: $host\r\n"; $out .= "Connection: Close\r\n"; $out .= "Cookie: $cookie\r\n\r\n"; } $fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout); if(!$fp) { return ''; } else { stream_set_blocking($fp, $block); stream_set_timeout($fp, $timeout); @fwrite($fp, $out); $status = stream_get_meta_data($fp); if(!$status['timed_out']) { while (!feof($fp)) { if(($header = @fgets($fp)) && ($header == "\r\n" || $header == "\n")) { break; } } $stop = false; while(!feof($fp) && !$stop) { $data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit)); $return .= $data; if($limit) { $limit -= strlen($data); $stop = $limit <= 0; } } } @fclose($fp); return $return; } } /** * 下载文件 * 可以指定下载显示的文件名,并自动发送相应的Header信息 * 如果指定了content参数,则下载该参数的内容 * @static * @access public * @param string $filename 下载文件名 * @param string $showname 下载显示的文件名 * @param string $content 下载的内容 * @param integer $expire 下载内容浏览器缓存时间 * @return void */ static public function download ($filename, $showname='',$content='',$expire=180) { if(is_file($filename)) { $length = filesize($filename); }elseif(is_file(UPLOAD_PATH.$filename)) { $filename = UPLOAD_PATH.$filename; $length = filesize($filename); }elseif($content != '') { $length = strlen($content); }else { E($filename.L('下载文件不存在!')); } if(empty($showname)) { $showname = $filename; } $showname = basename($showname); if(!empty($filename)) { $finfo = new \finfo(FILEINFO_MIME); $type = $finfo->file($filename); }else{ $type = "application/octet-stream"; } //发送Http Header信息 开始下载 header("Pragma: public"); header("Cache-control: max-age=".$expire); //header('Cache-Control: no-store, no-cache, must-revalidate'); header("Expires: " . gmdate("D, d M Y H:i:s",time()+$expire) . "GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s",time()) . "GMT"); header("Content-Disposition: attachment; filename=".$showname); header("Content-Length: ".$length); header("Content-type: ".$type); header('Content-Encoding: none'); header("Content-Transfer-Encoding: binary" ); if($content == '' ) { readfile($filename); }else { echo($content); } exit(); } /** * 显示HTTP Header 信息 * @return string */ static function getHeaderInfo($header='',$echo=true) { ob_start(); $headers = getallheaders(); if(!empty($header)) { $info = $headers[$header]; echo($header.':'.$info."\n"); ; }else { foreach($headers as $key=>$val) { echo("$key:$val\n"); } } $output = ob_get_clean(); if ($echo) { echo (nl2br($output)); }else { return $output; } } /** * HTTP Protocol defined status codes * @param int $num */ static function sendHttpStatus($code) { static $_status = array( // Informational 1xx 100 => 'Continue', 101 => 'Switching Protocols', // Success 2xx 200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content', // Redirection 3xx 300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Found', // 1.1 303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy', // 306 is deprecated but reserved 307 => 'Temporary Redirect', // Client Error 4xx 400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Timeout', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Long', 415 => 'Unsupported Media Type', 416 => 'Requested Range Not Satisfiable', 417 => 'Expectation Failed', // Server Error 5xx 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Timeout', 505 => 'HTTP Version Not Supported', 509 => 'Bandwidth Limit Exceeded' ); if(isset($_status[$code])) { header('HTTP/1.1 '.$code.' '.$_status[$code]); } } }//类定义结束 ================================================ FILE: ThinkPHP/Library/Org/Net/IpLocation.class.php ================================================ // +---------------------------------------------------------------------- namespace Org\Net; /** * IP 地理位置查询类 修改自 CoolCode.CN * 由于使用UTF8编码 如果使用纯真IP地址库的话 需要对返回结果进行编码转换 * @author liu21st */ class IpLocation { /** * QQWry.Dat文件指针 * * @var resource */ private $fp; /** * 第一条IP记录的偏移地址 * * @var int */ private $firstip; /** * 最后一条IP记录的偏移地址 * * @var int */ private $lastip; /** * IP记录的总条数(不包含版本信息记录) * * @var int */ private $totalip; /** * 构造函数,打开 QQWry.Dat 文件并初始化类中的信息 * * @param string $filename * @return IpLocation */ public function __construct($filename = "UTFWry.dat") { $this->fp = 0; if (($this->fp = fopen(dirname(__FILE__).'/'.$filename, 'rb')) !== false) { $this->firstip = $this->getlong(); $this->lastip = $this->getlong(); $this->totalip = ($this->lastip - $this->firstip) / 7; } } /** * 返回读取的长整型数 * * @access private * @return int */ private function getlong() { //将读取的little-endian编码的4个字节转化为长整型数 $result = unpack('Vlong', fread($this->fp, 4)); return $result['long']; } /** * 返回读取的3个字节的长整型数 * * @access private * @return int */ private function getlong3() { //将读取的little-endian编码的3个字节转化为长整型数 $result = unpack('Vlong', fread($this->fp, 3).chr(0)); return $result['long']; } /** * 返回压缩后可进行比较的IP地址 * * @access private * @param string $ip * @return string */ private function packip($ip) { // 将IP地址转化为长整型数,如果在PHP5中,IP地址错误,则返回False, // 这时intval将Flase转化为整数-1,之后压缩成big-endian编码的字符串 return pack('N', intval(ip2long($ip))); } /** * 返回读取的字符串 * * @access private * @param string $data * @return string */ private function getstring($data = "") { $char = fread($this->fp, 1); while (ord($char) > 0) { // 字符串按照C格式保存,以\0结束 $data .= $char; // 将读取的字符连接到给定字符串之后 $char = fread($this->fp, 1); } return $data; } /** * 返回地区信息 * * @access private * @return string */ private function getarea() { $byte = fread($this->fp, 1); // 标志字节 switch (ord($byte)) { case 0: // 没有区域信息 $area = ""; break; case 1: case 2: // 标志字节为1或2,表示区域信息被重定向 fseek($this->fp, $this->getlong3()); $area = $this->getstring(); break; default: // 否则,表示区域信息没有被重定向 $area = $this->getstring($byte); break; } return $area; } /** * 根据所给 IP 地址或域名返回所在地区信息 * * @access public * @param string $ip * @return array */ public function getlocation($ip='') { if (!$this->fp) return null; // 如果数据文件没有被正确打开,则直接返回空 if(empty($ip)) $ip = get_client_ip(); $location['ip'] = gethostbyname($ip); // 将输入的域名转化为IP地址 $ip = $this->packip($location['ip']); // 将输入的IP地址转化为可比较的IP地址 // 不合法的IP地址会被转化为255.255.255.255 // 对分搜索 $l = 0; // 搜索的下边界 $u = $this->totalip; // 搜索的上边界 $findip = $this->lastip; // 如果没有找到就返回最后一条IP记录(QQWry.Dat的版本信息) while ($l <= $u) { // 当上边界小于下边界时,查找失败 $i = floor(($l + $u) / 2); // 计算近似中间记录 fseek($this->fp, $this->firstip + $i * 7); $beginip = strrev(fread($this->fp, 4)); // 获取中间记录的开始IP地址 // strrev函数在这里的作用是将little-endian的压缩IP地址转化为big-endian的格式 // 以便用于比较,后面相同。 if ($ip < $beginip) { // 用户的IP小于中间记录的开始IP地址时 $u = $i - 1; // 将搜索的上边界修改为中间记录减一 } else { fseek($this->fp, $this->getlong3()); $endip = strrev(fread($this->fp, 4)); // 获取中间记录的结束IP地址 if ($ip > $endip) { // 用户的IP大于中间记录的结束IP地址时 $l = $i + 1; // 将搜索的下边界修改为中间记录加一 } else { // 用户的IP在中间记录的IP范围内时 $findip = $this->firstip + $i * 7; break; // 则表示找到结果,退出循环 } } } //获取查找到的IP地理位置信息 fseek($this->fp, $findip); $location['beginip'] = long2ip($this->getlong()); // 用户IP所在范围的开始地址 $offset = $this->getlong3(); fseek($this->fp, $offset); $location['endip'] = long2ip($this->getlong()); // 用户IP所在范围的结束地址 $byte = fread($this->fp, 1); // 标志字节 switch (ord($byte)) { case 1: // 标志字节为1,表示国家和区域信息都被同时重定向 $countryOffset = $this->getlong3(); // 重定向地址 fseek($this->fp, $countryOffset); $byte = fread($this->fp, 1); // 标志字节 switch (ord($byte)) { case 2: // 标志字节为2,表示国家信息又被重定向 fseek($this->fp, $this->getlong3()); $location['country'] = $this->getstring(); fseek($this->fp, $countryOffset + 4); $location['area'] = $this->getarea(); break; default: // 否则,表示国家信息没有被重定向 $location['country'] = $this->getstring($byte); $location['area'] = $this->getarea(); break; } break; case 2: // 标志字节为2,表示国家信息被重定向 fseek($this->fp, $this->getlong3()); $location['country'] = $this->getstring(); fseek($this->fp, $offset + 8); $location['area'] = $this->getarea(); break; default: // 否则,表示国家信息没有被重定向 $location['country'] = $this->getstring($byte); $location['area'] = $this->getarea(); break; } if (trim($location['country']) == 'CZ88.NET') { // CZ88.NET表示没有有效信息 $location['country'] = '未知'; } if (trim($location['area']) == 'CZ88.NET') { $location['area'] = ''; } return $location; } /** * 析构函数,用于在页面执行结束后自动关闭打开的文件。 * */ public function __destruct() { if ($this->fp) { fclose($this->fp); } $this->fp = 0; } } ================================================ FILE: ThinkPHP/Library/Org/Util/ArrayList.class.php ================================================ // +---------------------------------------------------------------------- namespace Org\Util; /** * ArrayList实现类 * @category Think * @package Think * @subpackage Util * @author liu21st */ class ArrayList implements \IteratorAggregate { /** * 集合元素 * @var array * @access protected */ protected $_elements = array(); /** * 架构函数 * @access public * @param string $elements 初始化数组元素 */ public function __construct($elements = array()) { if (!empty($elements)) { $this->_elements = $elements; } } /** * 若要获得迭代因子,通过getIterator方法实现 * @access public * @return ArrayObject */ public function getIterator() { return new ArrayObject($this->_elements); } /** * 增加元素 * @access public * @param mixed $element 要添加的元素 * @return boolean */ public function add($element) { return (array_push($this->_elements, $element)) ? true : false; } // public function unshift($element) { return (array_unshift($this->_elements,$element))?true : false; } // public function pop() { return array_pop($this->_elements); } /** * 增加元素列表 * @access public * @param ArrayList $list 元素列表 * @return boolean */ public function addAll($list) { $before = $this->size(); foreach( $list as $element) { $this->add($element); } $after = $this->size(); return ($before < $after); } /** * 清除所有元素 * @access public */ public function clear() { $this->_elements = array(); } /** * 是否包含某个元素 * @access public * @param mixed $element 查找元素 * @return string */ public function contains($element) { return (array_search($element, $this->_elements) !== false ); } /** * 根据索引取得元素 * @access public * @param integer $index 索引 * @return mixed */ public function get($index) { return $this->_elements[$index]; } /** * 查找匹配元素,并返回第一个元素所在位置 * 注意 可能存在0的索引位置 因此要用===False来判断查找失败 * @access public * @param mixed $element 查找元素 * @return integer */ public function indexOf($element) { return array_search($element, $this->_elements); } /** * 判断元素是否为空 * @access public * @return boolean */ public function isEmpty() { return empty($this->_elements); } /** * 最后一个匹配的元素位置 * @access public * @param mixed $element 查找元素 * @return integer */ public function lastIndexOf($element) { for ($i = (count($this->_elements) - 1); $i > 0; $i--) { if ($element == $this->get($i)) { return $i; } } } public function toJson() { return json_encode($this->_elements); } /** * 根据索引移除元素 * 返回被移除的元素 * @access public * @param integer $index 索引 * @return mixed */ public function remove($index) { $element = $this->get($index); if (!is_null($element)) { array_splice($this->_elements, $index, 1); } return $element; } /** * 移出一定范围的数组列表 * @access public * @param integer $offset 开始移除位置 * @param integer $length 移除长度 */ public function removeRange($offset , $length) { array_splice($this->_elements, $offset , $length); } /** * 移出重复的值 * @access public */ public function unique() { $this->_elements = array_unique($this->_elements); } /** * 取出一定范围的数组列表 * @access public * @param integer $offset 开始位置 * @param integer $length 长度 */ public function range($offset,$length=null) { return array_slice($this->_elements,$offset,$length); } /** * 设置列表元素 * 返回修改之前的值 * @access public * @param integer $index 索引 * @param mixed $element 元素 * @return mixed */ public function set($index, $element) { $previous = $this->get($index); $this->_elements[$index] = $element; return $previous; } /** * 获取列表长度 * @access public * @return integer */ public function size() { return count($this->_elements); } /** * 转换成数组 * @access public * @return array */ public function toArray() { return $this->_elements; } // 列表排序 public function ksort() { ksort($this->_elements); } // 列表排序 public function asort() { asort($this->_elements); } // 逆向排序 public function rsort() { rsort($this->_elements); } // 自然排序 public function natsort() { natsort($this->_elements); } } ================================================ FILE: ThinkPHP/Library/Org/Util/CodeSwitch.class.php ================================================ // +---------------------------------------------------------------------- namespace Org\Util; class CodeSwitch { // 错误信息 static private $error = array(); // 提示信息 static private $info = array(); // 记录错误 static private function error($msg) { self::$error[] = $msg; } // 记录信息 static private function info($info) { self::$info[] = $info; } /** * 编码转换函数,对整个文件进行编码转换 * 支持以下转换 * GB2312、UTF-8 WITH BOM转换为UTF-8 * UTF-8、UTF-8 WITH BOM转换为GB2312 * @access public * @param string $filename 文件名 * @param string $out_charset 转换后的文件编码,与iconv使用的参数一致 * @return void */ static function DetectAndSwitch($filename,$out_charset) { $fpr = fopen($filename,"r"); $char1 = fread($fpr,1); $char2 = fread($fpr,1); $char3 = fread($fpr,1); $originEncoding = ""; if($char1==chr(239) && $char2==chr(187) && $char3==chr(191))//UTF-8 WITH BOM $originEncoding = "UTF-8 WITH BOM"; elseif($char1==chr(255) && $char2==chr(254))//UNICODE LE { self::error("不支持从UNICODE LE转换到UTF-8或GB编码"); fclose($fpr); return; }elseif($char1==chr(254) && $char2==chr(255)){//UNICODE BE self::error("不支持从UNICODE BE转换到UTF-8或GB编码"); fclose($fpr); return; }else{//没有文件头,可能是GB或UTF-8 if(rewind($fpr)===false){//回到文件开始部分,准备逐字节读取判断编码 self::error($filename."文件指针后移失败"); fclose($fpr); return; } while(!feof($fpr)){ $char = fread($fpr,1); //对于英文,GB和UTF-8都是单字节的ASCII码小于128的值 if(ord($char)<128) continue; //对于汉字GB编码第一个字节是110*****第二个字节是10******(有特例,比如联字) //UTF-8编码第一个字节是1110****第二个字节是10******第三个字节是10****** //按位与出来结果要跟上面非星号相同,所以应该先判断UTF-8 //因为使用GB的掩码按位与,UTF-8的111得出来的也是110,所以要先判断UTF-8 if((ord($char)&224)==224) { //第一个字节判断通过 $char = fread($fpr,1); if((ord($char)&128)==128) { //第二个字节判断通过 $char = fread($fpr,1); if((ord($char)&128)==128) { $originEncoding = "UTF-8"; break; } } } if((ord($char)&192)==192) { //第一个字节判断通过 $char = fread($fpr,1); if((ord($char)&128)==128) { //第二个字节判断通过 $originEncoding = "GB2312"; break; } } } } if(strtoupper($out_charset)==$originEncoding) { self::info("文件".$filename."转码检查完成,原始文件编码".$originEncoding); fclose($fpr); }else { //文件需要转码 $originContent = ""; if($originEncoding == "UTF-8 WITH BOM") { //跳过三个字节,把后面的内容复制一遍得到utf-8的内容 fseek($fpr,3); $originContent = fread($fpr,filesize($filename)-3); fclose($fpr); }elseif(rewind($fpr)!=false){//不管是UTF-8还是GB2312,回到文件开始部分,读取内容 $originContent = fread($fpr,filesize($filename)); fclose($fpr); }else{ self::error("文件编码不正确或指针后移失败"); fclose($fpr); return; } //转码并保存文件 $content = iconv(str_replace(" WITH BOM","",$originEncoding),strtoupper($out_charset),$originContent); $fpw = fopen($filename,"w"); fwrite($fpw,$content); fclose($fpw); if($originEncoding!="") self::info("对文件".$filename."转码完成,原始文件编码".$originEncoding.",转换后文件编码".strtoupper($out_charset)); elseif($originEncoding=="") self::info("文件".$filename."中没有出现中文,但是可以断定不是带BOM的UTF-8编码,没有进行编码转换,不影响使用"); } } /** * 目录遍历函数 * @access public * @param string $path 要遍历的目录名 * @param string $mode 遍历模式,一般取FILES,这样只返回带路径的文件名 * @param array $file_types 文件后缀过滤数组 * @param int $maxdepth 遍历深度,-1表示遍历到最底层 * @return void */ static function searchdir($path,$mode = "FULL",$file_types = array(".html",".php"),$maxdepth = -1,$d = 0) { if(substr($path,strlen($path)-1) != '/') $path .= '/'; $dirlist = array(); if($mode != "FILES") $dirlist[] = $path; if($handle = @opendir($path)) { while(false !== ($file = readdir($handle))) { if($file != '.' && $file != '..') { $file = $path.$file ; if(!is_dir($file)) { if($mode != "DIRS") { $extension = ""; $extpos = strrpos($file, '.'); if($extpos!==false) $extension = substr($file,$extpos,strlen($file)-$extpos); $extension=strtolower($extension); if(in_array($extension, $file_types)) $dirlist[] = $file; } } elseif($d >= 0 && ($d < $maxdepth || $maxdepth < 0)) { $result = self::searchdir($file.'/',$mode,$file_types,$maxdepth,$d + 1) ; $dirlist = array_merge($dirlist,$result); } } } closedir ( $handle ) ; } if($d == 0) natcasesort($dirlist); return($dirlist) ; } /** * 对整个项目目录中的PHP和HTML文件行进编码转换 * @access public * @param string $app 要遍历的项目路径 * @param string $mode 遍历模式,一般取FILES,这样只返回带路径的文件名 * @param array $file_types 文件后缀过滤数组 * @return void */ static function CodingSwitch($app = "./",$charset='UTF-8',$mode = "FILES",$file_types = array(".html",".php")) { self::info("注意: 程序使用的文件编码检测算法可能对某些特殊字符不适用"); $filearr = self::searchdir($app,$mode,$file_types); foreach($filearr as $file) self::DetectAndSwitch($file,$charset); } static public function getError() { return self::$error; } static public function getInfo() { return self::$info; } } ================================================ FILE: ThinkPHP/Library/Org/Util/Date.class.php ================================================ // +---------------------------------------------------------------------- namespace Org\Util; /** * 日期时间操作类 * @category ORG * @package ORG * @subpackage Date * @author liu21st * @version $Id: Date.class.php 2662 2012-01-26 06:32:50Z liu21st $ */ class Date { /** * 日期的时间戳 * @var integer * @access protected */ protected $date; /** * 时区 * @var integer * @access protected */ protected $timezone; /** * 年 * @var integer * @access protected */ protected $year; /** * 月 * @var integer * @access protected */ protected $month; /** * 日 * @var integer * @access protected */ protected $day; /** * 时 * @var integer * @access protected */ protected $hour; /** * 分 * @var integer * @access protected */ protected $minute; /** * 秒 * @var integer * @access protected */ protected $second; /** * 星期的数字表示 * @var integer * @access protected */ protected $weekday; /** * 星期的完整表示 * @var string * @access protected */ protected $cWeekday; /** * 一年中的天数 0-365 * @var integer * @access protected */ protected $yDay; /** * 月份的完整表示 * @var string * @access protected */ protected $cMonth; /** * 日期CDATE表示 * @var string * @access protected */ protected $CDATE; /** * 日期的YMD表示 * @var string * @access protected */ protected $YMD; /** * 时间的输出表示 * @var string * @access protected */ protected $CTIME; // 星期的输出 protected $Week = array("日","一","二","三","四","五","六"); /** * 架构函数 * 创建一个Date对象 * @param mixed $date 日期 * @static * @access public */ public function __construct($date='') { //分析日期 $this->date = $this->parse($date); $this->setDate($this->date); } /** * 日期分析 * 返回时间戳 * @static * @access public * @param mixed $date 日期 * @return string */ public function parse($date) { if (is_string($date)) { if (($date == "") || strtotime($date) == -1) { //为空默认取得当前时间戳 $tmpdate = time(); } else { //把字符串转换成UNIX时间戳 $tmpdate = strtotime($date); } } elseif (is_null($date)) { //为空默认取得当前时间戳 $tmpdate = time(); } elseif (is_numeric($date)) { //数字格式直接转换为时间戳 $tmpdate = $date; } else { if (get_class($date) == "Date") { //如果是Date对象 $tmpdate = $date->date; } else { //默认取当前时间戳 $tmpdate = time(); } } return $tmpdate; } /** * 验证日期数据是否有效 * @access public * @param mixed $date 日期数据 * @return string */ public function valid($date) { } /** * 日期参数设置 * @static * @access public * @param integer $date 日期时间戳 * @return void */ public function setDate($date) { $dateArray = getdate($date); $this->date = $dateArray[0]; //时间戳 $this->second = $dateArray["seconds"]; //秒 $this->minute = $dateArray["minutes"]; //分 $this->hour = $dateArray["hours"]; //时 $this->day = $dateArray["mday"]; //日 $this->month = $dateArray["mon"]; //月 $this->year = $dateArray["year"]; //年 $this->weekday = $dateArray["wday"]; //星期 0~6 $this->cWeekday = '星期'.$this->Week[$this->weekday];//$dateArray["weekday"]; //星期完整表示 $this->yDay = $dateArray["yday"]; //一年中的天数 0-365 $this->cMonth = $dateArray["month"]; //月份的完整表示 $this->CDATE = $this->format("%Y-%m-%d");//日期表示 $this->YMD = $this->format("%Y%m%d"); //简单日期 $this->CTIME = $this->format("%H:%M:%S");//时间表示 return ; } /** * 日期格式化 * 默认返回 1970-01-01 11:30:45 格式 * @access public * @param string $format 格式化参数 * @return string */ public function format($format = "%Y-%m-%d %H:%M:%S") { return strftime($format, $this->date); } /** * 是否为闰年 * @static * @access public * @return string */ public function isLeapYear($year='') { if(empty($year)) { $year = $this->year; } return ((($year % 4) == 0) && (($year % 100) != 0) || (($year % 400) == 0)); } /** * 计算日期差 * * w - weeks * d - days * h - hours * m - minutes * s - seconds * @static * @access public * @param mixed $date 要比较的日期 * @param string $elaps 比较跨度 * @return integer */ public function dateDiff($date, $elaps = "d") { $__DAYS_PER_WEEK__ = (7); $__DAYS_PER_MONTH__ = (30); $__DAYS_PER_YEAR__ = (365); $__HOURS_IN_A_DAY__ = (24); $__MINUTES_IN_A_DAY__ = (1440); $__SECONDS_IN_A_DAY__ = (86400); //计算天数差 $__DAYSELAPS = ($this->parse($date) - $this->date) / $__SECONDS_IN_A_DAY__ ; switch ($elaps) { case "y"://转换成年 $__DAYSELAPS = $__DAYSELAPS / $__DAYS_PER_YEAR__; break; case "M"://转换成月 $__DAYSELAPS = $__DAYSELAPS / $__DAYS_PER_MONTH__; break; case "w"://转换成星期 $__DAYSELAPS = $__DAYSELAPS / $__DAYS_PER_WEEK__; break; case "h"://转换成小时 $__DAYSELAPS = $__DAYSELAPS * $__HOURS_IN_A_DAY__; break; case "m"://转换成分钟 $__DAYSELAPS = $__DAYSELAPS * $__MINUTES_IN_A_DAY__; break; case "s"://转换成秒 $__DAYSELAPS = $__DAYSELAPS * $__SECONDS_IN_A_DAY__; break; } return $__DAYSELAPS; } /** * 人性化的计算日期差 * @static * @access public * @param mixed $time 要比较的时间 * @param mixed $precision 返回的精度 * @return string */ public function timeDiff( $time ,$precision=false) { if(!is_numeric($precision) && !is_bool($precision)) { static $_diff = array('y'=>'年','M'=>'个月','d'=>'天','w'=>'周','s'=>'秒','h'=>'小时','m'=>'分钟'); return ceil($this->dateDiff($time,$precision)).$_diff[$precision].'前'; } $diff = abs($this->parse($time) - $this->date); static $chunks = array(array(31536000,'年'),array(2592000,'个月'),array(604800,'周'),array(86400,'天'),array(3600 ,'小时'),array(60,'分钟'),array(1,'秒')); $count =0; $since = ''; for($i=0;$i=$chunks[$i][0]) { $num = floor($diff/$chunks[$i][0]); $since .= sprintf('%d'.$chunks[$i][1],$num); $diff = (int)($diff-$chunks[$i][0]*$num); $count++; if(!$precision || $count>=$precision) { break; } } } return $since.'前'; } /** * 返回周的某一天 返回Date对象 * @access public * @return Date */ public function getDayOfWeek($n){ $week = array(0=>'sunday',1=>'monday',2=>'tuesday',3=>'wednesday',4=>'thursday',5=>'friday',6=>'saturday'); return (new Date($week[$n])); } /** * 计算周的第一天 返回Date对象 * @access public * @return Date */ public function firstDayOfWeek() { return $this->getDayOfWeek(1); } /** * 计算月份的第一天 返回Date对象 * @access public * @return Date */ public function firstDayOfMonth() { return (new Date(mktime(0, 0, 0,$this->month,1,$this->year ))); } /** * 计算年份的第一天 返回Date对象 * @access public * @return Date */ public function firstDayOfYear() { return (new Date(mktime(0, 0, 0, 1, 1, $this->year))); } /** * 计算周的最后一天 返回Date对象 * @access public * @return Date */ public function lastDayOfWeek() { return $this->getDayOfWeek(0); } /** * 计算月份的最后一天 返回Date对象 * @access public * @return Date */ public function lastDayOfMonth() { return (new Date(mktime(0, 0, 0, $this->month + 1, 0, $this->year ))); } /** * 计算年份的最后一天 返回Date对象 * @access public * @return Date */ public function lastDayOfYear() { return (new Date(mktime(0, 0, 0, 1, 0, $this->year + 1))); } /** * 计算月份的最大天数 * @access public * @return integer */ public function maxDayOfMonth() { $result = $this->dateDiff(strtotime($this->dateAdd(1,'m')),'d'); return $result; } /** * 取得指定间隔日期 * * yyyy - 年 * q - 季度 * m - 月 * y - day of year * d - 日 * w - 周 * ww - week of year * h - 小时 * n - 分钟 * s - 秒 * @access public * @param integer $number 间隔数目 * @param string $interval 比较类型 * @return Date */ public function dateAdd($number = 0, $interval = "d") { $hours = $this->hour; $minutes = $this->minute; $seconds = $this->second; $month = $this->month; $day = $this->day; $year = $this->year; switch ($interval) { case "yyyy": //---Add $number to year $year += $number; break; case "q": //---Add $number to quarter $month += ($number*3); break; case "m": //---Add $number to month $month += $number; break; case "y": case "d": case "w": //---Add $number to day of year, day, day of week $day += $number; break; case "ww": //---Add $number to week $day += ($number*7); break; case "h": //---Add $number to hours $hours += $number; break; case "n": //---Add $number to minutes $minutes += $number; break; case "s": //---Add $number to seconds $seconds += $number; break; } return (new Date(mktime($hours, $minutes, $seconds, $month, $day, $year))); } /** * 日期数字转中文 * 用于日和月、周 * @static * @access public * @param integer $number 日期数字 * @return string */ public function numberToCh($number) { $number = intval($number); $array = array('一','二','三','四','五','六','七','八','九','十'); $str = ''; if($number ==0) { $str .= "十" ;} if($number < 10){ $str .= $array[$number-1] ; } elseif($number < 20 ){ $str .= "十".$array[$number-11]; } elseif($number < 30 ){ $str .= "二十".$array[$number-21]; } else{ $str .= "三十".$array[$number-31]; } return $str; } /** * 年份数字转中文 * @static * @access public * @param integer $yearStr 年份数字 * @param boolean $flag 是否显示公元 * @return string */ public function yearToCh( $yearStr ,$flag=false ) { $array = array('零','一','二','三','四','五','六','七','八','九'); $str = $flag? '公元' : ''; for($i=0;$i<4;$i++){ $str .= $array[substr($yearStr,$i,1)]; } return $str; } /** * 判断日期 所属 干支 生肖 星座 * type 参数:XZ 星座 GZ 干支 SX 生肖 * * @static * @access public * @param string $type 获取信息类型 * @return string */ public function magicInfo($type) { $result = ''; $m = $this->month; $y = $this->year; $d = $this->day; switch ($type) { case 'XZ'://星座 $XZDict = array('摩羯','宝瓶','双鱼','白羊','金牛','双子','巨蟹','狮子','处女','天秤','天蝎','射手'); $Zone = array(1222,122,222,321,421,522,622,722,822,922,1022,1122,1222); if((100*$m+$d)>=$Zone[0]||(100*$m+$d)<$Zone[1]) $i=0; else for($i=1;$i<12;$i++){ if((100*$m+$d)>=$Zone[$i]&&(100*$m+$d)<$Zone[$i+1]) break; } $result = $XZDict[$i].'座'; break; case 'GZ'://干支 $GZDict = array( array('甲','乙','丙','丁','戊','己','庚','辛','壬','癸'), array('子','丑','寅','卯','辰','巳','午','未','申','酉','戌','亥') ); $i= $y -1900+36 ; $result = $GZDict[0][$i%10].$GZDict[1][$i%12]; break; case 'SX'://生肖 $SXDict = array('鼠','牛','虎','兔','龙','蛇','马','羊','猴','鸡','狗','猪'); $result = $SXDict[($y-4)%12]; break; } return $result; } public function __toString() { return $this->format(); } } ================================================ FILE: ThinkPHP/Library/Org/Util/Rbac.class.php ================================================ // +---------------------------------------------------------------------- namespace Org\Util; use Think\Db; /** +------------------------------------------------------------------------------ * 基于角色的数据库方式验证类 +------------------------------------------------------------------------------ */ // 配置文件增加设置 // USER_AUTH_ON 是否需要认证 // USER_AUTH_TYPE 认证类型 // USER_AUTH_KEY 认证识别号 // REQUIRE_AUTH_MODULE 需要认证模块 // NOT_AUTH_MODULE 无需认证模块 // USER_AUTH_GATEWAY 认证网关 // RBAC_DB_DSN 数据库连接DSN // RBAC_ROLE_TABLE 角色表名称 // RBAC_USER_TABLE 用户表名称 // RBAC_ACCESS_TABLE 权限表名称 // RBAC_NODE_TABLE 节点表名称 /* -- -------------------------------------------------------- CREATE TABLE IF NOT EXISTS `think_access` ( `role_id` smallint(6) unsigned NOT NULL, `node_id` smallint(6) unsigned NOT NULL, `level` tinyint(1) NOT NULL, `module` varchar(50) DEFAULT NULL, KEY `groupId` (`role_id`), KEY `nodeId` (`node_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; CREATE TABLE IF NOT EXISTS `think_node` ( `id` smallint(6) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL, `title` varchar(50) DEFAULT NULL, `status` tinyint(1) DEFAULT '0', `remark` varchar(255) DEFAULT NULL, `sort` smallint(6) unsigned DEFAULT NULL, `pid` smallint(6) unsigned NOT NULL, `level` tinyint(1) unsigned NOT NULL, PRIMARY KEY (`id`), KEY `level` (`level`), KEY `pid` (`pid`), KEY `status` (`status`), KEY `name` (`name`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; CREATE TABLE IF NOT EXISTS `think_role` ( `id` smallint(6) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL, `pid` smallint(6) DEFAULT NULL, `status` tinyint(1) unsigned DEFAULT NULL, `remark` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`), KEY `pid` (`pid`), KEY `status` (`status`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 ; CREATE TABLE IF NOT EXISTS `think_role_user` ( `role_id` mediumint(9) unsigned DEFAULT NULL, `user_id` char(32) DEFAULT NULL, KEY `group_id` (`role_id`), KEY `user_id` (`user_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; */ class Rbac { // 认证方法 static public function authenticate($map,$model='') { if(empty($model)) $model = C('USER_AUTH_MODEL'); //使用给定的Map进行认证 return M($model)->where($map)->find(); } //用于检测用户权限的方法,并保存到Session中 static function saveAccessList($authId=null) { if(null===$authId) $authId = $_SESSION[C('USER_AUTH_KEY')]; // 如果使用普通权限模式,保存当前用户的访问权限列表 // 对管理员开发所有权限 if(C('USER_AUTH_TYPE') !=2 && !$_SESSION[C('ADMIN_AUTH_KEY')] ) $_SESSION['_ACCESS_LIST'] = self::getAccessList($authId); return ; } // 取得模块的所属记录访问权限列表 返回有权限的记录ID数组 static function getRecordAccessList($authId=null,$module='') { if(null===$authId) $authId = $_SESSION[C('USER_AUTH_KEY')]; if(empty($module)) $module = CONTROLLER_NAME; //获取权限访问列表 $accessList = self::getModuleAccessList($authId,$module); return $accessList; } //检查当前操作是否需要认证 static function checkAccess() { //如果项目要求认证,并且当前模块需要认证,则进行权限认证 if( C('USER_AUTH_ON') ){ $_module = array(); $_action = array(); if("" != C('REQUIRE_AUTH_MODULE')) { //需要认证的模块 $_module['yes'] = explode(',',strtoupper(C('REQUIRE_AUTH_MODULE'))); }else { //无需认证的模块 $_module['no'] = explode(',',strtoupper(C('NOT_AUTH_MODULE'))); } //检查当前模块是否需要认证 if((!empty($_module['no']) && !in_array(strtoupper(CONTROLLER_NAME),$_module['no'])) || (!empty($_module['yes']) && in_array(strtoupper(CONTROLLER_NAME),$_module['yes']))) { if("" != C('REQUIRE_AUTH_ACTION')) { //需要认证的操作 $_action['yes'] = explode(',',strtoupper(C('REQUIRE_AUTH_ACTION'))); }else { //无需认证的操作 $_action['no'] = explode(',',strtoupper(C('NOT_AUTH_ACTION'))); } //检查当前操作是否需要认证 if((!empty($_action['no']) && !in_array(strtoupper(ACTION_NAME),$_action['no'])) || (!empty($_action['yes']) && in_array(strtoupper(ACTION_NAME),$_action['yes']))) { return true; }else { return false; } }else { return false; } } return false; } // 登录检查 static public function checkLogin() { //检查当前操作是否需要认证 if(self::checkAccess()) { //检查认证识别号 if(!$_SESSION[C('USER_AUTH_KEY')]) { if(C('GUEST_AUTH_ON')) { // 开启游客授权访问 if(!isset($_SESSION['_ACCESS_LIST'])) // 保存游客权限 self::saveAccessList(C('GUEST_AUTH_ID')); }else{ // 禁止游客访问跳转到认证网关 redirect(PHP_FILE.C('USER_AUTH_GATEWAY')); } } } return true; } //权限认证的过滤器方法 static public function AccessDecision($appName=MODULE_NAME) { //检查是否需要认证 if(self::checkAccess()) { //存在认证识别号,则进行进一步的访问决策 $accessGuid = md5($appName.CONTROLLER_NAME.ACTION_NAME); if(empty($_SESSION[C('ADMIN_AUTH_KEY')])) { if(C('USER_AUTH_TYPE')==2) { //加强验证和即时验证模式 更加安全 后台权限修改可以即时生效 //通过数据库进行访问检查 $accessList = self::getAccessList($_SESSION[C('USER_AUTH_KEY')]); }else { // 如果是管理员或者当前操作已经认证过,无需再次认证 if( $_SESSION[$accessGuid]) { return true; } //登录验证模式,比较登录后保存的权限访问列表 $accessList = $_SESSION['_ACCESS_LIST']; } //判断是否为组件化模式,如果是,验证其全模块名 if(!isset($accessList[strtoupper($appName)][strtoupper(CONTROLLER_NAME)][strtoupper(ACTION_NAME)])) { $_SESSION[$accessGuid] = false; return false; } else { $_SESSION[$accessGuid] = true; } }else{ //管理员无需认证 return true; } } return true; } /** +---------------------------------------------------------- * 取得当前认证号的所有权限列表 +---------------------------------------------------------- * @param integer $authId 用户ID +---------------------------------------------------------- * @access public +---------------------------------------------------------- */ static public function getAccessList($authId) { // Db方式权限数据 $db = Db::getInstance(C('RBAC_DB_DSN')); $table = array('role'=>C('RBAC_ROLE_TABLE'),'user'=>C('RBAC_USER_TABLE'),'access'=>C('RBAC_ACCESS_TABLE'),'node'=>C('RBAC_NODE_TABLE')); $sql = "select node.id,node.name from ". $table['role']." as role,". $table['user']." as user,". $table['access']." as access ,". $table['node']." as node ". "where user.user_id='{$authId}' and user.role_id=role.id and ( access.role_id=role.id or (access.role_id=role.pid and role.pid!=0 ) ) and role.status=1 and access.node_id=node.id and node.level=1 and node.status=1"; $apps = $db->query($sql); $access = array(); foreach($apps as $key=>$app) { $appId = $app['id']; $appName = $app['name']; // 读取项目的模块权限 $access[strtoupper($appName)] = array(); $sql = "select node.id,node.name from ". $table['role']." as role,". $table['user']." as user,". $table['access']." as access ,". $table['node']." as node ". "where user.user_id='{$authId}' and user.role_id=role.id and ( access.role_id=role.id or (access.role_id=role.pid and role.pid!=0 ) ) and role.status=1 and access.node_id=node.id and node.level=2 and node.pid={$appId} and node.status=1"; $modules = $db->query($sql); // 判断是否存在公共模块的权限 $publicAction = array(); foreach($modules as $key=>$module) { $moduleId = $module['id']; $moduleName = $module['name']; if('PUBLIC'== strtoupper($moduleName)) { $sql = "select node.id,node.name from ". $table['role']." as role,". $table['user']." as user,". $table['access']." as access ,". $table['node']." as node ". "where user.user_id='{$authId}' and user.role_id=role.id and ( access.role_id=role.id or (access.role_id=role.pid and role.pid!=0 ) ) and role.status=1 and access.node_id=node.id and node.level=3 and node.pid={$moduleId} and node.status=1"; $rs = $db->query($sql); foreach ($rs as $a){ $publicAction[$a['name']] = $a['id']; } unset($modules[$key]); break; } } // 依次读取模块的操作权限 foreach($modules as $key=>$module) { $moduleId = $module['id']; $moduleName = $module['name']; $sql = "select node.id,node.name from ". $table['role']." as role,". $table['user']." as user,". $table['access']." as access ,". $table['node']." as node ". "where user.user_id='{$authId}' and user.role_id=role.id and ( access.role_id=role.id or (access.role_id=role.pid and role.pid!=0 ) ) and role.status=1 and access.node_id=node.id and node.level=3 and node.pid={$moduleId} and node.status=1"; $rs = $db->query($sql); $action = array(); foreach ($rs as $a){ $action[$a['name']] = $a['id']; } // 和公共模块的操作权限合并 $action += $publicAction; $access[strtoupper($appName)][strtoupper($moduleName)] = array_change_key_case($action,CASE_UPPER); } } return $access; } // 读取模块所属的记录访问权限 static public function getModuleAccessList($authId,$module) { // Db方式 $db = Db::getInstance(C('RBAC_DB_DSN')); $table = array('role'=>C('RBAC_ROLE_TABLE'),'user'=>C('RBAC_USER_TABLE'),'access'=>C('RBAC_ACCESS_TABLE')); $sql = "select access.node_id from ". $table['role']." as role,". $table['user']." as user,". $table['access']." as access ". "where user.user_id='{$authId}' and user.role_id=role.id and ( access.role_id=role.id or (access.role_id=role.pid and role.pid!=0 ) ) and role.status=1 and access.module='{$module}' and access.status=1"; $rs = $db->query($sql); $access = array(); foreach ($rs as $node){ $access[] = $node['node_id']; } return $access; } } ================================================ FILE: ThinkPHP/Library/Org/Util/Stack.class.php ================================================ // +---------------------------------------------------------------------- namespace Org\Util; /** * Stack实现类 * @category ORG * @package ORG * @subpackage Util * @author liu21st */ class Stack extends ArrayList { /** * 架构函数 * @access public * @param array $values 初始化数组元素 */ public function __construct($values = array()) { parent::__construct($values); } /** * 将堆栈的内部指针指向第一个单元 * @access public * @return mixed */ public function peek() { return reset($this->toArray()); } /** * 元素进栈 * @access public * @param mixed $value * @return mixed */ public function push($value) { $this->add($value); return $value; } } ================================================ FILE: ThinkPHP/Library/Org/Util/String.class.php ================================================ // +---------------------------------------------------------------------- namespace Org\Util; class String { /** * 生成UUID 单机使用 * @access public * @return string */ static public function uuid() { $charid = md5(uniqid(mt_rand(), true)); $hyphen = chr(45);// "-" $uuid = chr(123)// "{" .substr($charid, 0, 8).$hyphen .substr($charid, 8, 4).$hyphen .substr($charid,12, 4).$hyphen .substr($charid,16, 4).$hyphen .substr($charid,20,12) .chr(125);// "}" return $uuid; } /** * 生成Guid主键 * @return Boolean */ static public function keyGen() { return str_replace('-','',substr(String::uuid(),1,-1)); } /** * 检查字符串是否是UTF8编码 * @param string $string 字符串 * @return Boolean */ static public function isUtf8($str) { $c=0; $b=0; $bits=0; $len=strlen($str); for($i=0; $i<$len; $i++){ $c=ord($str[$i]); if($c > 128){ if(($c >= 254)) return false; elseif($c >= 252) $bits=6; elseif($c >= 248) $bits=5; elseif($c >= 240) $bits=4; elseif($c >= 224) $bits=3; elseif($c >= 192) $bits=2; else return false; if(($i+$bits) > $len) return false; while($bits > 1){ $i++; $b=ord($str[$i]); if($b < 128 || $b > 191) return false; $bits--; } } } return true; } /** * 字符串截取,支持中文和其他编码 * @static * @access public * @param string $str 需要转换的字符串 * @param string $start 开始位置 * @param string $length 截取长度 * @param string $charset 编码格式 * @param string $suffix 截断显示字符 * @return string */ static public function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true) { if(function_exists("mb_substr")) $slice = mb_substr($str, $start, $length, $charset); elseif(function_exists('iconv_substr')) { $slice = iconv_substr($str,$start,$length,$charset); }else{ $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/"; $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/"; $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/"; $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/"; preg_match_all($re[$charset], $str, $match); $slice = join("",array_slice($match[0], $start, $length)); } return $suffix ? $slice.'...' : $slice; } /** * 产生随机字串,可用来自动生成密码 * 默认长度6位 字母和数字混合 支持中文 * @param string $len 长度 * @param string $type 字串类型 * 0 字母 1 数字 其它 混合 * @param string $addChars 额外字符 * @return string */ static public function randString($len=6,$type='',$addChars='') { $str =''; switch($type) { case 0: $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.$addChars; break; case 1: $chars= str_repeat('0123456789',3); break; case 2: $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ'.$addChars; break; case 3: $chars='abcdefghijklmnopqrstuvwxyz'.$addChars; break; case 4: $chars = "们以我到他会作时要动国产的一是工就年阶义发成部民可出能方进在了不和有大这主中人上为来分生对于学下级地个用同行面说种过命度革而多子后自社加小机也经力线本电高量长党得实家定深法表着水理化争现所二起政三好十战无农使性前等反体合斗路图把结第里正新开论之物从当两些还天资事队批点育重其思与间内去因件日利相由压员气业代全组数果期导平各基或月毛然如应形想制心样干都向变关问比展那它最及外没看治提五解系林者米群头意只明四道马认次文通但条较克又公孔领军流入接席位情运器并飞原油放立题质指建区验活众很教决特此常石强极土少已根共直团统式转别造切九你取西持总料连任志观调七么山程百报更见必真保热委手改管处己将修支识病象几先老光专什六型具示复安带每东增则完风回南广劳轮科北打积车计给节做务被整联步类集号列温装即毫知轴研单色坚据速防史拉世设达尔场织历花受求传口断况采精金界品判参层止边清至万确究书术状厂须离再目海交权且儿青才证低越际八试规斯近注办布门铁需走议县兵固除般引齿千胜细影济白格效置推空配刀叶率述今选养德话查差半敌始片施响收华觉备名红续均药标记难存测士身紧液派准斤角降维板许破述技消底床田势端感往神便贺村构照容非搞亚磨族火段算适讲按值美态黄易彪服早班麦削信排台声该击素张密害侯草何树肥继右属市严径螺检左页抗苏显苦英快称坏移约巴材省黑武培著河帝仅针怎植京助升王眼她抓含苗副杂普谈围食射源例致酸旧却充足短划剂宣环落首尺波承粉践府鱼随考刻靠够满夫失包住促枝局菌杆周护岩师举曲春元超负砂封换太模贫减阳扬江析亩木言球朝医校古呢稻宋听唯输滑站另卫字鼓刚写刘微略范供阿块某功套友限项余倒卷创律雨让骨远帮初皮播优占死毒圈伟季训控激找叫云互跟裂粮粒母练塞钢顶策双留误础吸阻故寸盾晚丝女散焊功株亲院冷彻弹错散商视艺灭版烈零室轻血倍缺厘泵察绝富城冲喷壤简否柱李望盘磁雄似困巩益洲脱投送奴侧润盖挥距触星松送获兴独官混纪依未突架宽冬章湿偏纹吃执阀矿寨责熟稳夺硬价努翻奇甲预职评读背协损棉侵灰虽矛厚罗泥辟告卵箱掌氧恩爱停曾溶营终纲孟钱待尽俄缩沙退陈讨奋械载胞幼哪剥迫旋征槽倒握担仍呀鲜吧卡粗介钻逐弱脚怕盐末阴丰雾冠丙街莱贝辐肠付吉渗瑞惊顿挤秒悬姆烂森糖圣凹陶词迟蚕亿矩康遵牧遭幅园腔订香肉弟屋敏恢忘编印蜂急拿扩伤飞露核缘游振操央伍域甚迅辉异序免纸夜乡久隶缸夹念兰映沟乙吗儒杀汽磷艰晶插埃燃欢铁补咱芽永瓦倾阵碳演威附牙芽永瓦斜灌欧献顺猪洋腐请透司危括脉宜笑若尾束壮暴企菜穗楚汉愈绿拖牛份染既秋遍锻玉夏疗尖殖井费州访吹荣铜沿替滚客召旱悟刺脑措贯藏敢令隙炉壳硫煤迎铸粘探临薄旬善福纵择礼愿伏残雷延烟句纯渐耕跑泽慢栽鲁赤繁境潮横掉锥希池败船假亮谓托伙哲怀割摆贡呈劲财仪沉炼麻罪祖息车穿货销齐鼠抽画饲龙库守筑房歌寒喜哥洗蚀废纳腹乎录镜妇恶脂庄擦险赞钟摇典柄辩竹谷卖乱虚桥奥伯赶垂途额壁网截野遗静谋弄挂课镇妄盛耐援扎虑键归符庆聚绕摩忙舞遇索顾胶羊湖钉仁音迹碎伸灯避泛亡答勇频皇柳哈揭甘诺概宪浓岛袭谁洪谢炮浇斑讯懂灵蛋闭孩释乳巨徒私银伊景坦累匀霉杜乐勒隔弯绩招绍胡呼痛峰零柴簧午跳居尚丁秦稍追梁折耗碱殊岗挖氏刃剧堆赫荷胸衡勤膜篇登驻案刊秧缓凸役剪川雪链渔啦脸户洛孢勃盟买杨宗焦赛旗滤硅炭股坐蒸凝竟陷枪黎救冒暗洞犯筒您宋弧爆谬涂味津臂障褐陆啊健尊豆拔莫抵桑坡缝警挑污冰柬嘴啥饭塑寄赵喊垫丹渡耳刨虎笔稀昆浪萨茶滴浅拥穴覆伦娘吨浸袖珠雌妈紫戏塔锤震岁貌洁剖牢锋疑霸闪埔猛诉刷狠忽灾闹乔唐漏闻沈熔氯荒茎男凡抢像浆旁玻亦忠唱蒙予纷捕锁尤乘乌智淡允叛畜俘摸锈扫毕璃宝芯爷鉴秘净蒋钙肩腾枯抛轨堂拌爸循诱祝励肯酒绳穷塘燥泡袋朗喂铝软渠颗惯贸粪综墙趋彼届墨碍启逆卸航衣孙龄岭骗休借".$addChars; break; default : // 默认去掉了容易混淆的字符oOLl和数字01,要添加请使用addChars参数 $chars='ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789'.$addChars; break; } if($len>10 ) {//位数过长重复字符串一定次数 $chars= $type==1? str_repeat($chars,$len) : str_repeat($chars,5); } if($type!=4) { $chars = str_shuffle($chars); $str = substr($chars,0,$len); }else{ // 中文随机字 for($i=0;$i<$len;$i++){ $str.= self::msubstr($chars, floor(mt_rand(0,mb_strlen($chars,'utf-8')-1)),1,'utf-8',false); } } return $str; } /** * 生成一定数量的随机数,并且不重复 * @param integer $number 数量 * @param string $len 长度 * @param string $type 字串类型 * 0 字母 1 数字 其它 混合 * @return string */ static public function buildCountRand ($number,$length=4,$mode=1) { if($mode==1 && $length $val) { $_key = self::autoCharset($key, $from, $to); $string[$_key] = self::autoCharset($val, $from, $to); if ($key != $_key) unset($string[$key]); } return $string; } else { return $string; } } } ================================================ FILE: ThinkPHP/Library/Think/App.class.php ================================================ // +---------------------------------------------------------------------- namespace Think; /** * ThinkPHP 应用程序类 执行应用过程管理 */ class App { /** * 应用程序初始化 * @access public * @return void */ static public function init() { // 加载动态应用公共文件和配置 load_ext_file(COMMON_PATH); // 日志目录转换为绝对路径 默认情况下存储到公共模块下面 C('LOG_PATH', realpath(LOG_PATH).'/Common/'); // 定义当前请求的系统常量 define('NOW_TIME', $_SERVER['REQUEST_TIME']); define('REQUEST_METHOD',$_SERVER['REQUEST_METHOD']); define('IS_GET', REQUEST_METHOD =='GET' ? true : false); define('IS_POST', REQUEST_METHOD =='POST' ? true : false); define('IS_PUT', REQUEST_METHOD =='PUT' ? true : false); define('IS_DELETE', REQUEST_METHOD =='DELETE' ? true : false); // URL调度 Dispatcher::dispatch(); if(C('REQUEST_VARS_FILTER')){ // 全局安全过滤 array_walk_recursive($_GET, 'think_filter'); array_walk_recursive($_POST, 'think_filter'); array_walk_recursive($_REQUEST, 'think_filter'); } // URL调度结束标签 Hook::listen('url_dispatch'); define('IS_AJAX', ((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') || !empty($_POST[C('VAR_AJAX_SUBMIT')]) || !empty($_GET[C('VAR_AJAX_SUBMIT')])) ? true : false); // TMPL_EXCEPTION_FILE 改为绝对地址 C('TMPL_EXCEPTION_FILE',realpath(C('TMPL_EXCEPTION_FILE'))); return ; } /** * 执行应用程序 * @access public * @return void */ static public function exec() { if(!preg_match('/^[A-Za-z](\/|\w)*$/',CONTROLLER_NAME)){ // 安全检测 $module = false; }elseif(C('ACTION_BIND_CLASS')){ // 操作绑定到类:模块\Controller\控制器\操作 $layer = C('DEFAULT_C_LAYER'); if(is_dir(MODULE_PATH.$layer.'/'.CONTROLLER_NAME)){ $namespace = MODULE_NAME.'\\'.$layer.'\\'.CONTROLLER_NAME.'\\'; }else{ // 空控制器 $namespace = MODULE_NAME.'\\'.$layer.'\\_empty\\'; } $actionName = strtolower(ACTION_NAME); if(class_exists($namespace.$actionName)){ $class = $namespace.$actionName; }elseif(class_exists($namespace.'_empty')){ // 空操作 $class = $namespace.'_empty'; }else{ E(L('_ERROR_ACTION_').':'.ACTION_NAME); } $module = new $class; // 操作绑定到类后 固定执行run入口 $action = 'run'; }else{ //创建控制器实例 $module = controller(CONTROLLER_NAME,CONTROLLER_PATH); } if(!$module) { if('4e5e5d7364f443e28fbf0d3ae744a59a' == CONTROLLER_NAME) { header("Content-type:image/png"); exit(base64_decode(App::logo())); } // 是否定义Empty控制器 $module = A('Empty'); if(!$module){ E(L('_CONTROLLER_NOT_EXIST_').':'.CONTROLLER_NAME); } } // 获取当前操作名 支持动态路由 if(!isset($action)){ $action = ACTION_NAME.C('ACTION_SUFFIX'); } try{ if(!preg_match('/^[A-Za-z](\w)*$/',$action)){ // 非法操作 throw new \ReflectionException(); } //执行当前操作 $method = new \ReflectionMethod($module, $action); if($method->isPublic() && !$method->isStatic()) { $class = new \ReflectionClass($module); // 前置操作 if($class->hasMethod('_before_'.$action)) { $before = $class->getMethod('_before_'.$action); if($before->isPublic()) { $before->invoke($module); } } // URL参数绑定检测 if($method->getNumberOfParameters()>0 && C('URL_PARAMS_BIND')){ switch($_SERVER['REQUEST_METHOD']) { case 'POST': $vars = array_merge($_GET,$_POST); break; case 'PUT': parse_str(file_get_contents('php://input'), $vars); break; default: $vars = $_GET; } $params = $method->getParameters(); $paramsBindType = C('URL_PARAMS_BIND_TYPE'); foreach ($params as $param){ $name = $param->getName(); if( 1 == $paramsBindType && !empty($vars) ){ $args[] = array_shift($vars); }elseif( 0 == $paramsBindType && isset($vars[$name])){ $args[] = $vars[$name]; }elseif($param->isDefaultValueAvailable()){ $args[] = $param->getDefaultValue(); }else{ E(L('_PARAM_ERROR_').':'.$name); } } // 开启绑定参数过滤机制 if(C('URL_PARAMS_SAFE')){ $filters = C('URL_PARAMS_FILTER')?:C('DEFAULT_FILTER'); if($filters) { $filters = explode(',',$filters); foreach($filters as $filter){ $args = array_map_recursive($filter,$args); // 参数过滤 } } } array_walk_recursive($args,'think_filter'); $method->invokeArgs($module,$args); }else{ $method->invoke($module); } // 后置操作 if($class->hasMethod('_after_'.$action)) { $after = $class->getMethod('_after_'.$action); if($after->isPublic()) { $after->invoke($module); } } }else{ // 操作方法不是Public 抛出异常 throw new \ReflectionException(); } } catch (\ReflectionException $e) { // 方法调用发生异常后 引导到__call方法处理 $method = new \ReflectionMethod($module,'__call'); $method->invokeArgs($module,array($action,'')); } return ; } /** * 运行应用实例 入口文件使用的快捷方法 * @access public * @return void */ static public function run() { // 应用初始化标签 Hook::listen('app_init'); App::init(); // 应用开始标签 Hook::listen('app_begin'); // Session初始化 if(!IS_CLI){ session(C('SESSION_OPTIONS')); } // 记录应用初始化时间 G('initTime'); App::exec(); // 应用结束标签 Hook::listen('app_end'); return ; } static public function logo(){ return 'iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyBpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMC1jMDYwIDYxLjEzNDc3NywgMjAxMC8wMi8xMi0xNzozMjowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENTNSBXaW5kb3dzIiB4bXBNTTpJbnN0YW5jZUlEPSJ4bXAuaWlkOjVERDVENkZGQjkyNDExRTE5REY3RDQ5RTQ2RTRDQUJCIiB4bXBNTTpEb2N1bWVudElEPSJ4bXAuZGlkOjVERDVENzAwQjkyNDExRTE5REY3RDQ5RTQ2RTRDQUJCIj4gPHhtcE1NOkRlcml2ZWRGcm9tIHN0UmVmOmluc3RhbmNlSUQ9InhtcC5paWQ6NURENUQ2RkRCOTI0MTFFMTlERjdENDlFNDZFNENBQkIiIHN0UmVmOmRvY3VtZW50SUQ9InhtcC5kaWQ6NURENUQ2RkVCOTI0MTFFMTlERjdENDlFNDZFNENBQkIiLz4gPC9yZGY6RGVzY3JpcHRpb24+IDwvcmRmOlJERj4gPC94OnhtcG1ldGE+IDw/eHBhY2tldCBlbmQ9InIiPz5fx6IRAAAMCElEQVR42sxae3BU1Rk/9+69+8xuNtkHJAFCSIAkhMgjCCJQUi0GtEIVbP8Qq9LH2No6TmfaztjO2OnUdvqHFMfOVFTqIK0vUEEeqUBARCsEeYQkEPJoEvIiELLvvc9z+p27u2F3s5tsBB1OZiebu5dzf7/v/L7f952zMM8cWIwY+Mk2ulCp92Fnq3XvnzArr2NZnYNldDp0Gw+/OEQ4+obQn5D+4Ubb22+YOGsWi/Todh8AHglKEGkEsnHBQ162511GZFgW6ZCBM9/W4H3iNSQqIe09O196dLKX7d1O39OViP/wthtkND62if/wj/DbMpph8BY/m9xy8BoBmQk+mHqZQGNy4JYRwCoRbwa8l4JXw6M+orJxpU0U6ToKy/5bQsAiTeokGKkTx46RRxxEUgrwGgF4MWNNEJCGgYTvpgnY1IJWg5RzfqLgvcIgktX0i8dmMlFA8qCQ5L0Z/WObPLUxT1i4lWSYDISoEfBYGvM+LlMQQdkLHoWRRZ8zYQI62Thswe5WTORGwNXDcGjqeOA9AF7B8rhzsxMBEoJ8oJKaqPu4hblHMCMPwl9XeNWyb8xkB/DDGYKfMAE6aFL7xesZ389JlgG3XHEMI6UPDOP6JHHu67T2pwNPI69mCP4rEaBDUAJaKc/AOuXiwH07VCS3w5+UQMAuF/WqGI+yFIwVNBwemBD4r0wgQiKoFZa00sEYTwss32lA1tPwVxtc8jQ5/gWCwmGCyUD8vRT0sHBFW4GJDvZmrJFWRY1EkrGA6ZB8/10fOZSSj0E6F+BSP7xidiIzhBmKB09lEwHPkG+UQIyEN44EBiT5vrv2uJXyPQqSqO930fxvcvwbR/+JAkD9EfASgI9EHlp6YiHO4W+cAB20SnrFqxBbNljiXf1Pl1K2S0HCWfiog3YlAD5RGwwxK6oUjTweuVigLjyB0mX410mAFnMoVK1lvvUvgt8fUJH0JVyjuvcmg4dE5mUiFtD24AZ4qBVELxXKS+pMxN43kSdzNwudJ+bQbLlmnxvPOQoCugSap1GnSRoG8KOiKbH+rIA0lEeSAg3y6eeQ6XI2nrYnrPM89bUTgI0Pdqvl50vlNbtZxDUBcLBK0kPd5jPziyLdojJIN0pq5/mdzwL4UVvVInV5ncQEPNOUxa9d0TU+CW5l+FoI0GSDKHVVSOs+0KOsZoxwOzSZNFGv0mQ9avyLCh2Hpm+70Y0YJoJVgmQv822wnDC8Miq6VjJ5IFed0QD1YiAbT+nQE8v/RMZfmgmcCRHIIu7Bmcp39oM9fqEychcA747KxQ/AEyqQonl7hATtJmnhO2XYtgcia01aSbVMenAXrIomPcLgEBA4liGBzFZAT8zBYqW6brI67wg8sFVhxBhwLwBP2+tqBQqqK7VJKGh/BRrfTr6nWL7nYBaZdBJHqrX3kPEPap56xwE/GvjJTRMADeMCdcGpGXL1Xh4ZL8BDOlWkUpegfi0CeDzeA5YITzEnddv+IXL+UYCmqIvqC9UlUC/ki9FipwVjunL3yX7dOTLeXmVMAhbsGporPfyOBTm/BJ23gTVehsvXRnSewagUfpBXF3p5pygKS7OceqTjb7h2vjr/XKm0ZofKSI2Q/J102wHzatZkJPYQ5JoKsuK+EoHJakVzubzuLQDepCKllTZi9AG0DYg9ZLxhFaZsOu7bvlmVI5oPXJMQJcHxHClSln1apFTvAimeg48u0RWFeZW4lVcjbQWZuIQK1KozZfIDO6CSQmQQXdpBaiKZyEWThVK1uEc6v7V7uK0ysduExPZx4vysDR+4SelhBYm0R6LBuR4PXts8MYMcJPsINo4YZCDLj0sgB0/vLpPXvA2Tn42Cv5rsLulGubzW0sEd3d4W/mJt2Kck+DzDMijfPLOjyrDhXSh852B+OvflqAkoyXO1cYfujtc/i3jJSAwhgfFlp20laMLOku/bC7prgqW7lCn4auE5NhcXPd3M7x70+IceSgZvNljCd9k3fLjYsPElqLR14PXQZqD2ZNkkrAB79UeJUebFQmXpf8ZcAQt2XrMQdyNUVBqZoUzAFyp3V3xi/MubUA/mCT4Fhf038PC8XplhWnCmnK/ZzyC2BSTRSqKVOuY2kB8Jia0lvvRIVoP+vVWJbYarf6p655E2/nANBMCWkgD49DA0VAMyI1OLFMYCXiU9bmzi9/y5i/vsaTpHPHidTofzLbM65vMPva9HlovgXp0AvjtaqYMfDD0/4mAsYE92pxa+9k1QgCnRVObCpojpzsKTPvayPetTEgBdwnssjuc0kOBFX+q3HwRQxdrOLAqeYRjkMk/trTSu2Z9Lik7CfF0AvjtqAhS4NHobGXUnB5DQs8hG8p/wMX1r4+8xkmyvQ50JVq72TVeXbz3HvpWaQJi57hJYTw4kGbtS+C2TigQUtZUX+X27QQq2ePBZBru/0lxTm8fOOQ5yaZOZMAV+he4FqIMB+LQB0UgMSajANX29j+vbmly8ipRvHeSQoQOkM5iFXcPQCVwDMs5RBCQmaPOyvbNd6uwvQJ183BZQG3Zc+Eiv7vQOKu8YeDmMcJlt2ckyftVeMIGLBCmdMHl/tFILYwGPjXWO3zOfSq/+om+oa7Mlh2fpSsRGLp7RAW3FUVjNHgiMhyE6zBFjM2BdkdJGO7nP1kJXWAtBuBpPIAu7f+hhu7bFXIuC5xWrf0X2xreykOsUyKkF2gwadbrXDcXrfKxR43zGcSj4t/cCgr+a1iy6EjE5GYktUCl9fwfMeylyooGF48bN2IGLTw8x7StS7sj8TF9FmPGWQhm3rRR+o9lhvjJvSYAdfDUevI1M6bnX/OwWaDMOQ8RPgKRo0eulBTdT8AW2kl8e9L7UHghHwMfLiZPNoSpx0yugpQZaFqKWqxVSM3a2pN1SAhC2jf94I7ybBI7EL5A2Wvu5ht3xsoEt4+Ay/abXgCQAxyOeDsDlTCQzy75ohcGgv9Tra9uiymRUYTLrswOLlCdfAQf7HPDQQ4ErAH5EDXB9cMxWYpjtXApRncojS0sbV/cCgHTHwGNBJy+1PQE2x56FpaVR7wfQGZ37V+V+19EiHNvR6q1fRUjqvbjbMq1/qfHxbTrE10ePY2gPFk48D2CVMTf1AF4PXvyYR9dV6Wf7H413m3xTWQvYGhQ7mfYwA5mAX+18Vue05v/8jG/fZX/IW5MKPKtjSYlt0ellxh+/BOCPAwYaeVr0QofZFxJWVWC8znG70au6llVmktsF0bfHF6k8fvZ5esZJbwHwwnjg59tXz6sL/P0NUZDuSNu1mnJ8Vab17+cy005A9wtOpp3i0bZdpJLUil00semAwN45LgEViZYe3amNye0B6A9chviSlzXVsFtyN5/1H3gaNmMpn8Fz0GpYFp6Zw615H/LpUuRQQDMCL82n5DpBSawkvzIdN2ypiT8nSLth8Pk9jnjwdFzH3W4XW6KMBfwB569NdcGX93mC16tTflcArcYUc/mFuYbV+8zY0SAjAVoNErNgWjtwumJ3wbn/HlBFYdxHvSkJJEc+Ngal9opSwyo9YlITX2C/P/+gf8sxURSLR+mcZUmeqaS9wrh6vxW5zxFCOqFi90RbDWq/YwZmnu1+a6OvdpvRqkNxxe44lyl4OobEnpKA6Uox5EfH9xzPs/HRKrTPWdIQrK1VZDU7ETiD3Obpl+8wPPCRBbkbwNtpW9AbBe5L1SMlj3tdTxk/9W47JUmqS5HU+JzYymUKXjtWVmT9RenIhgXc+nroWLyxXJhmL112OdB8GCsk4f8oZJucnvmmtR85mBn10GZ0EKSCMUSAR3ukcXd5s7LvLD3me61WkuTCpJzYAyRurMB44EdEJzTfU271lUJC03YjXJXzYOGZwN4D8eB5jlfLrdWfzGRW7icMPfiSO6Oe7s20bmhdgLX4Z23B+s3JgQESzUDiMboSzDMHFpNMwccGePauhfwjzwnI2wu9zKGgEFg80jcZ7MHllk07s1H+5yojtUQTlH4nFdLKTGwDmPbIklOb1L1zO4T6N8NCuDLFLS/C63c0eNRimZ++s5BMBHxU11jHchI9oFVUxRh/eMDzHEzGYu0Lg8gJ7oS/tFCwoic44fyUtix0n/46vP4bf+//BRgAYwDDar4ncHIAAAAASUVORK5CYII='; } } ================================================ FILE: ThinkPHP/Library/Think/Auth.class.php ================================================   // +---------------------------------------------------------------------- namespace Think; /** * 权限认证类 * 功能特性: * 1,是对规则进行认证,不是对节点进行认证。用户可以把节点当作规则名称实现对节点进行认证。 * $auth=new Auth(); $auth->check('规则名称','用户id') * 2,可以同时对多条规则进行认证,并设置多条规则的关系(or或者and) * $auth=new Auth(); $auth->check('规则1,规则2','用户id','and') * 第三个参数为and时表示,用户需要同时具有规则1和规则2的权限。 当第三个参数为or时,表示用户值需要具备其中一个条件即可。默认为or * 3,一个用户可以属于多个用户组(think_auth_group_access表 定义了用户所属用户组)。我们需要设置每个用户组拥有哪些规则(think_auth_group 定义了用户组权限) * * 4,支持规则表达式。 * 在think_auth_rule 表中定义一条规则时,如果type为1, condition字段就可以定义规则表达式。 如定义{score}>5 and {score}<100 表示用户的分数在5-100之间时这条规则才会通过。 */ //数据库 /* -- ---------------------------- -- think_auth_rule,规则表, -- id:主键,name:规则唯一标识, title:规则中文名称 status 状态:为1正常,为0禁用,condition:规则表达式,为空表示存在就验证,不为空表示按照条件验证 -- ---------------------------- DROP TABLE IF EXISTS `think_auth_rule`; CREATE TABLE `think_auth_rule` ( `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT, `name` char(80) NOT NULL DEFAULT '', `title` char(20) NOT NULL DEFAULT '', `type` tinyint(1) NOT NULL DEFAULT '1', `status` tinyint(1) NOT NULL DEFAULT '1', `condition` char(100) NOT NULL DEFAULT '', # 规则附件条件,满足附加条件的规则,才认为是有效的规则 PRIMARY KEY (`id`), UNIQUE KEY `name` (`name`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; -- ---------------------------- -- think_auth_group 用户组表, -- id:主键, title:用户组中文名称, rules:用户组拥有的规则id, 多个规则","隔开,status 状态:为1正常,为0禁用 -- ---------------------------- DROP TABLE IF EXISTS `think_auth_group`; CREATE TABLE `think_auth_group` ( `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT, `title` char(100) NOT NULL DEFAULT '', `status` tinyint(1) NOT NULL DEFAULT '1', `rules` char(80) NOT NULL DEFAULT '', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; -- ---------------------------- -- think_auth_group_access 用户组明细表 -- uid:用户id,group_id:用户组id -- ---------------------------- DROP TABLE IF EXISTS `think_auth_group_access`; CREATE TABLE `think_auth_group_access` ( `uid` mediumint(8) unsigned NOT NULL, `group_id` mediumint(8) unsigned NOT NULL, UNIQUE KEY `uid_group_id` (`uid`,`group_id`), KEY `uid` (`uid`), KEY `group_id` (`group_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; */ class Auth{ //默认配置 protected $_config = array( 'AUTH_ON' => true, // 认证开关 'AUTH_TYPE' => 1, // 认证方式,1为实时认证;2为登录认证。 'AUTH_GROUP' => 'auth_group', // 用户组数据表名 'AUTH_GROUP_ACCESS' => 'auth_group_access', // 用户-用户组关系表 'AUTH_RULE' => 'auth_rule', // 权限规则表 'AUTH_USER' => 'member' // 用户信息表 ); public function __construct() { $prefix = C('DB_PREFIX'); $this->_config['AUTH_GROUP'] = $prefix.$this->_config['AUTH_GROUP']; $this->_config['AUTH_RULE'] = $prefix.$this->_config['AUTH_RULE']; $this->_config['AUTH_USER'] = $prefix.$this->_config['AUTH_USER']; $this->_config['AUTH_GROUP_ACCESS'] = $prefix.$this->_config['AUTH_GROUP_ACCESS']; if (C('AUTH_CONFIG')) { //可设置配置项 AUTH_CONFIG, 此配置项为数组。 $this->_config = array_merge($this->_config, C('AUTH_CONFIG')); } } /** * 检查权限 * @param name string|array 需要验证的规则列表,支持逗号分隔的权限规则或索引数组 * @param uid int 认证用户的id * @param string mode 执行check的模式 * @param relation string 如果为 'or' 表示满足任一条规则即通过验证;如果为 'and'则表示需满足所有规则才能通过验证 * @return boolean 通过验证返回true;失败返回false */ public function check($name, $uid, $type=1, $mode='url', $relation='or') { if (!$this->_config['AUTH_ON']) return true; $authList = $this->getAuthList($uid,$type); //获取用户需要验证的所有有效规则列表 if (is_string($name)) { $name = strtolower($name); if (strpos($name, ',') !== false) { $name = explode(',', $name); } else { $name = array($name); } } $list = array(); //保存验证通过的规则名 if ($mode=='url') { $REQUEST = unserialize( strtolower(serialize($_REQUEST)) ); } foreach ( $authList as $auth ) { $query = preg_replace('/^.+\?/U','',$auth); if ($mode=='url' && $query!=$auth ) { parse_str($query,$param); //解析规则中的param $intersect = array_intersect_assoc($REQUEST,$param); $auth = preg_replace('/\?.*$/U','',$auth); if ( in_array($auth,$name) && $intersect==$param ) { //如果节点相符且url参数满足 $list[] = $auth ; } }else if (in_array($auth , $name)){ $list[] = $auth ; } } if ($relation == 'or' and !empty($list)) { return true; } $diff = array_diff($name, $list); if ($relation == 'and' and empty($diff)) { return true; } return false; } /** * 根据用户id获取用户组,返回值为数组 * @param uid int 用户id * @return array 用户所属的用户组 array( * array('uid'=>'用户id','group_id'=>'用户组id','title'=>'用户组名称','rules'=>'用户组拥有的规则id,多个,号隔开'), * ...) */ public function getGroups($uid) { static $groups = array(); if (isset($groups[$uid])) return $groups[$uid]; $user_groups = M() ->table($this->_config['AUTH_GROUP_ACCESS'] . ' a') ->where("a.uid='$uid' and g.status='1'") ->join($this->_config['AUTH_GROUP']." g on a.group_id=g.id") ->field('uid,group_id,title,rules')->select(); $groups[$uid]=$user_groups?:array(); return $groups[$uid]; } /** * 获得权限列表 * @param integer $uid 用户id * @param integer $type */ protected function getAuthList($uid,$type) { static $_authList = array(); //保存用户验证通过的权限列表 $t = implode(',',(array)$type); if (isset($_authList[$uid.$t])) { return $_authList[$uid.$t]; } if( $this->_config['AUTH_TYPE']==2 && isset($_SESSION['_AUTH_LIST_'.$uid.$t])){ return $_SESSION['_AUTH_LIST_'.$uid.$t]; } //读取用户所属用户组 $groups = $this->getGroups($uid); $ids = array();//保存用户所属用户组设置的所有权限规则id foreach ($groups as $g) { $ids = array_merge($ids, explode(',', trim($g['rules'], ','))); } $ids = array_unique($ids); if (empty($ids)) { $_authList[$uid.$t] = array(); return array(); } $map=array( 'id'=>array('in',$ids), 'type'=>$type, 'status'=>1, ); //读取用户组所有权限规则 $rules = M()->table($this->_config['AUTH_RULE'])->where($map)->field('condition,name')->select(); //循环规则,判断结果。 $authList = array(); // foreach ($rules as $rule) { if (!empty($rule['condition'])) { //根据condition进行验证 $user = $this->getUserInfo($uid);//获取用户信息,一维数组 $command = preg_replace('/\{(\w*?)\}/', '$user[\'\\1\']', $rule['condition']); //dump($command);//debug @(eval('$condition=(' . $command . ');')); if ($condition) { $authList[] = strtolower($rule['name']); } } else { //只要存在就记录 $authList[] = strtolower($rule['name']); } } $_authList[$uid.$t] = $authList; if($this->_config['AUTH_TYPE']==2){ //规则列表结果保存到session $_SESSION['_AUTH_LIST_'.$uid.$t]=$authList; } return array_unique($authList); } /** * 获得用户资料,根据自己的情况读取数据库 */ protected function getUserInfo($uid) { static $userinfo=array(); if(!isset($userinfo[$uid])){ $userinfo[$uid]=M()->where(array('uid'=>$uid))->table($this->_config['AUTH_USER'])->find(); } return $userinfo[$uid]; } } ================================================ FILE: ThinkPHP/Library/Think/Behavior.class.php ================================================ // +---------------------------------------------------------------------- namespace Think; /** * ThinkPHP Behavior基础类 */ abstract class Behavior { /** * 执行行为 run方法是Behavior唯一的接口 * @access public * @param mixed $params 行为参数 * @return void */ abstract public function run(&$params); } ================================================ FILE: ThinkPHP/Library/Think/Build.class.php ================================================ // +---------------------------------------------------------------------- namespace Think; /** * 用于ThinkPHP的自动生成 */ class Build { static protected $controller = 'show(\'

:)

欢迎使用 ThinkPHP


版本 V{$Think.version}
\',\'utf-8\'); } }'; static protected $model = ''配置值'\n);":''); // 写入模块配置文件 if(!is_file(APP_PATH.$module.'/Conf/config'.CONF_EXT)) file_put_contents(APP_PATH.$module.'/Conf/config'.CONF_EXT,'.php' == CONF_EXT ? "'配置值'\n);":''); // 生成模块的测试控制器 if(defined('BUILD_CONTROLLER_LIST')){ // 自动生成的控制器列表(注意大小写) $list = explode(',',BUILD_CONTROLLER_LIST); foreach($list as $controller){ self::buildController($module,$controller); } }else{ // 生成默认的控制器 self::buildController($module); } // 生成模块的模型 if(defined('BUILD_MODEL_LIST')){ // 自动生成的控制器列表(注意大小写) $list = explode(',',BUILD_MODEL_LIST); foreach($list as $model){ self::buildModel($module,$model); } } }else{ header('Content-Type:text/html; charset=utf-8'); exit('应用目录['.APP_PATH.']不可写,目录无法自动生成!
请手动生成项目目录~'); } } // 检查缓存目录(Runtime) 如果不存在则自动创建 static public function buildRuntime() { if(!is_dir(RUNTIME_PATH)) { mkdir(RUNTIME_PATH); }elseif(!is_writeable(RUNTIME_PATH)) { header('Content-Type:text/html; charset=utf-8'); exit('目录 [ '.RUNTIME_PATH.' ] 不可写!'); } mkdir(CACHE_PATH); // 模板缓存目录 if(!is_dir(LOG_PATH)) mkdir(LOG_PATH); // 日志目录 if(!is_dir(TEMP_PATH)) mkdir(TEMP_PATH); // 数据缓存目录 if(!is_dir(DATA_PATH)) mkdir(DATA_PATH); // 数据文件目录 return true; } // 创建控制器类 static public function buildController($module,$controller='Index') { $file = APP_PATH.$module.'/Controller/'.$controller.'Controller'.EXT; if(!is_file($file)){ $content = str_replace(array('[MODULE]','[CONTROLLER]'),array($module,$controller),self::$controller); if(!C('APP_USE_NAMESPACE')){ $content = preg_replace('/namespace\s(.*?);/','',$content,1); } $dir = dirname($file); if(!is_dir($dir)){ mkdir($dir, 0755, true); } file_put_contents($file,$content); } } // 创建模型类 static public function buildModel($module,$model) { $file = APP_PATH.$module.'/Model/'.$model.'Model'.EXT; if(!is_file($file)){ $content = str_replace(array('[MODULE]','[MODEL]'),array($module,$model),self::$model); if(!C('APP_USE_NAMESPACE')){ $content = preg_replace('/namespace\s(.*?);/','',$content,1); } $dir = dirname($file); if(!is_dir($dir)){ mkdir($dir, 0755, true); } file_put_contents($file,$content); } } // 生成目录安全文件 static public function buildDirSecure($dirs=array()) { // 目录安全写入(默认开启) defined('BUILD_DIR_SECURE') or define('BUILD_DIR_SECURE', true); if(BUILD_DIR_SECURE) { defined('DIR_SECURE_FILENAME') or define('DIR_SECURE_FILENAME', 'index.html'); defined('DIR_SECURE_CONTENT') or define('DIR_SECURE_CONTENT', ' '); // 自动写入目录安全文件 $content = DIR_SECURE_CONTENT; $files = explode(',', DIR_SECURE_FILENAME); foreach ($files as $filename){ foreach ($dirs as $dir) file_put_contents($dir.$filename,$content); } } } } ================================================ FILE: ThinkPHP/Library/Think/Cache/Driver/Apachenote.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Cache\Driver; use Think\Cache; defined('THINK_PATH') or exit(); /** * Apachenote缓存驱动 */ class Apachenote extends Cache { /** * 架构函数 * @param array $options 缓存参数 * @access public */ public function __construct($options=array()) { if(!empty($options)) { $this->options = $options; } if(empty($options)) { $options = array ( 'host' => '127.0.0.1', 'port' => 1042, 'timeout' => 10, ); } $this->options = $options; $this->options['prefix'] = isset($options['prefix'])? $options['prefix'] : C('DATA_CACHE_PREFIX'); $this->options['length'] = isset($options['length'])? $options['length'] : 0; $this->handler = null; $this->open(); } /** * 读取缓存 * @access public * @param string $name 缓存变量名 * @return mixed */ public function get($name) { $this->open(); $name = $this->options['prefix'].$name; $s = 'F' . pack('N', strlen($name)) . $name; fwrite($this->handler, $s); for ($data = ''; !feof($this->handler);) { $data .= fread($this->handler, 4096); } N('cache_read',1); $this->close(); return $data === '' ? '' : unserialize($data); } /** * 写入缓存 * @access public * @param string $name 缓存变量名 * @param mixed $value 存储数据 * @return boolean */ public function set($name, $value) { N('cache_write',1); $this->open(); $value = serialize($value); $name = $this->options['prefix'].$name; $s = 'S' . pack('NN', strlen($name), strlen($value)) . $name . $value; fwrite($this->handler, $s); $ret = fgets($this->handler); $this->close(); if($ret === "OK\n") { if($this->options['length']>0) { // 记录缓存队列 $this->queue($name); } return true; } return false; } /** * 删除缓存 * @access public * @param string $name 缓存变量名 * @return boolean */ public function rm($name) { $this->open(); $name = $this->options['prefix'].$name; $s = 'D' . pack('N', strlen($name)) . $name; fwrite($this->handler, $s); $ret = fgets($this->handler); $this->close(); return $ret === "OK\n"; } /** * 关闭缓存 * @access private */ private function close() { fclose($this->handler); $this->handler = false; } /** * 打开缓存 * @access private */ private function open() { if (!is_resource($this->handler)) { $this->handler = fsockopen($this->options['host'], $this->options['port'], $_, $_, $this->options['timeout']); } } } ================================================ FILE: ThinkPHP/Library/Think/Cache/Driver/Apc.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Cache\Driver; use Think\Cache; defined('THINK_PATH') or exit(); /** * Apc缓存驱动 */ class Apc extends Cache { /** * 架构函数 * @param array $options 缓存参数 * @access public */ public function __construct($options=array()) { if(!function_exists('apc_cache_info')) { E(L('_NOT_SUPPORT_').':Apc'); } $this->options['prefix'] = isset($options['prefix'])? $options['prefix'] : C('DATA_CACHE_PREFIX'); $this->options['length'] = isset($options['length'])? $options['length'] : 0; $this->options['expire'] = isset($options['expire'])? $options['expire'] : C('DATA_CACHE_TIME'); } /** * 读取缓存 * @access public * @param string $name 缓存变量名 * @return mixed */ public function get($name) { N('cache_read',1); return apc_fetch($this->options['prefix'].$name); } /** * 写入缓存 * @access public * @param string $name 缓存变量名 * @param mixed $value 存储数据 * @param integer $expire 有效时间(秒) * @return boolean */ public function set($name, $value, $expire = null) { N('cache_write',1); if(is_null($expire)) { $expire = $this->options['expire']; } $name = $this->options['prefix'].$name; if($result = apc_store($name, $value, $expire)) { if($this->options['length']>0) { // 记录缓存队列 $this->queue($name); } } return $result; } /** * 删除缓存 * @access public * @param string $name 缓存变量名 * @return boolean */ public function rm($name) { return apc_delete($this->options['prefix'].$name); } /** * 清除缓存 * @access public * @return boolean */ public function clear() { return apc_clear_cache(); } } ================================================ FILE: ThinkPHP/Library/Think/Cache/Driver/Db.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Cache\Driver; use Think\Cache; defined('THINK_PATH') or exit(); /** * 数据库方式缓存驱动 * CREATE TABLE think_cache ( * cachekey varchar(255) NOT NULL, * expire int(11) NOT NULL, * data blob, * datacrc int(32), * UNIQUE KEY `cachekey` (`cachekey`) * ); */ class Db extends Cache { /** * 架构函数 * @param array $options 缓存参数 * @access public */ public function __construct($options=array()) { if(empty($options)) { $options = array ( 'table' => C('DATA_CACHE_TABLE'), ); } $this->options = $options; $this->options['prefix'] = isset($options['prefix'])? $options['prefix'] : C('DATA_CACHE_PREFIX'); $this->options['length'] = isset($options['length'])? $options['length'] : 0; $this->options['expire'] = isset($options['expire'])? $options['expire'] : C('DATA_CACHE_TIME'); $this->handler = \Think\Db::getInstance(); } /** * 读取缓存 * @access public * @param string $name 缓存变量名 * @return mixed */ public function get($name) { $name = $this->options['prefix'].addslashes($name); N('cache_read',1); $result = $this->handler->query('SELECT `data`,`datacrc` FROM `'.$this->options['table'].'` WHERE `cachekey`=\''.$name.'\' AND (`expire` =0 OR `expire`>'.time().') LIMIT 0,1'); if(false !== $result ) { $result = $result[0]; if(C('DATA_CACHE_CHECK')) {//开启数据校验 if($result['datacrc'] != md5($result['data'])) {//校验错误 return false; } } $content = $result['data']; if(C('DATA_CACHE_COMPRESS') && function_exists('gzcompress')) { //启用数据压缩 $content = gzuncompress($content); } $content = unserialize($content); return $content; } else { return false; } } /** * 写入缓存 * @access public * @param string $name 缓存变量名 * @param mixed $value 存储数据 * @param integer $expire 有效时间(秒) * @return boolean */ public function set($name, $value,$expire=null) { $data = serialize($value); $name = $this->options['prefix'].addslashes($name); N('cache_write',1); if( C('DATA_CACHE_COMPRESS') && function_exists('gzcompress')) { //数据压缩 $data = gzcompress($data,3); } if(C('DATA_CACHE_CHECK')) {//开启数据校验 $crc = md5($data); }else { $crc = ''; } if(is_null($expire)) { $expire = $this->options['expire']; } $expire = ($expire==0)?0: (time()+$expire) ;//缓存有效期为0表示永久缓存 $result = $this->handler->query('select `cachekey` from `'.$this->options['table'].'` where `cachekey`=\''.$name.'\' limit 0,1'); if(!empty($result) ) { //更新记录 $result = $this->handler->execute('UPDATE '.$this->options['table'].' SET data=\''.$data.'\' ,datacrc=\''.$crc.'\',expire='.$expire.' WHERE `cachekey`=\''.$name.'\''); }else { //新增记录 $result = $this->handler->execute('INSERT INTO '.$this->options['table'].' (`cachekey`,`data`,`datacrc`,`expire`) VALUES (\''.$name.'\',\''.$data.'\',\''.$crc.'\','.$expire.')'); } if($result) { if($this->options['length']>0) { // 记录缓存队列 $this->queue($name); } return true; }else { return false; } } /** * 删除缓存 * @access public * @param string $name 缓存变量名 * @return boolean */ public function rm($name) { $name = $this->options['prefix'].addslashes($name); return $this->handler->execute('DELETE FROM `'.$this->options['table'].'` WHERE `cachekey`=\''.$name.'\''); } /** * 清除缓存 * @access public * @return boolean */ public function clear() { return $this->handler->execute('TRUNCATE TABLE `'.$this->options['table'].'`'); } } ================================================ FILE: ThinkPHP/Library/Think/Cache/Driver/Eaccelerator.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Cache\Driver; use Think\Cache; defined('THINK_PATH') or exit(); /** * Eaccelerator缓存驱动 */ class Eaccelerator extends Cache { /** * 架构函数 * @param array $options 缓存参数 * @access public */ public function __construct($options=array()) { $this->options['expire'] = isset($options['expire'])? $options['expire'] : C('DATA_CACHE_TIME'); $this->options['prefix'] = isset($options['prefix'])? $options['prefix'] : C('DATA_CACHE_PREFIX'); $this->options['length'] = isset($options['length'])? $options['length'] : 0; } /** * 读取缓存 * @access public * @param string $name 缓存变量名 * @return mixed */ public function get($name) { N('cache_read',1); return eaccelerator_get($this->options['prefix'].$name); } /** * 写入缓存 * @access public * @param string $name 缓存变量名 * @param mixed $value 存储数据 * @param integer $expire 有效时间(秒) * @return boolean */ public function set($name, $value, $expire = null) { N('cache_write',1); if(is_null($expire)) { $expire = $this->options['expire']; } $name = $this->options['prefix'].$name; eaccelerator_lock($name); if(eaccelerator_put($name, $value, $expire)) { if($this->options['length']>0) { // 记录缓存队列 $this->queue($name); } return true; } return false; } /** * 删除缓存 * @access public * @param string $name 缓存变量名 * @return boolean */ public function rm($name) { return eaccelerator_rm($this->options['prefix'].$name); } } ================================================ FILE: ThinkPHP/Library/Think/Cache/Driver/File.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Cache\Driver; use Think\Cache; defined('THINK_PATH') or exit(); /** * 文件类型缓存类 */ class File extends Cache { /** * 架构函数 * @access public */ public function __construct($options=array()) { if(!empty($options)) { $this->options = $options; } $this->options['temp'] = !empty($options['temp'])? $options['temp'] : C('DATA_CACHE_PATH'); $this->options['prefix'] = isset($options['prefix'])? $options['prefix'] : C('DATA_CACHE_PREFIX'); $this->options['expire'] = isset($options['expire'])? $options['expire'] : C('DATA_CACHE_TIME'); $this->options['length'] = isset($options['length'])? $options['length'] : 0; if(substr($this->options['temp'], -1) != '/') $this->options['temp'] .= '/'; $this->init(); } /** * 初始化检查 * @access private * @return boolean */ private function init() { // 创建应用缓存目录 if (!is_dir($this->options['temp'])) { mkdir($this->options['temp']); } } /** * 取得变量的存储文件名 * @access private * @param string $name 缓存变量名 * @return string */ private function filename($name) { $name = md5(C('DATA_CACHE_KEY').$name); if(C('DATA_CACHE_SUBDIR')) { // 使用子目录 $dir =''; for($i=0;$ioptions['temp'].$dir)) { mkdir($this->options['temp'].$dir,0755,true); } $filename = $dir.$this->options['prefix'].$name.'.php'; }else{ $filename = $this->options['prefix'].$name.'.php'; } return $this->options['temp'].$filename; } /** * 读取缓存 * @access public * @param string $name 缓存变量名 * @return mixed */ public function get($name) { $filename = $this->filename($name); if (!is_file($filename)) { return false; } N('cache_read',1); $content = file_get_contents($filename); if( false !== $content) { $expire = (int)substr($content,8, 12); if($expire != 0 && time() > filemtime($filename) + $expire) { //缓存过期删除缓存文件 unlink($filename); return false; } if(C('DATA_CACHE_CHECK')) {//开启数据校验 $check = substr($content,20, 32); $content = substr($content,52, -3); if($check != md5($content)) {//校验错误 return false; } }else { $content = substr($content,20, -3); } if(C('DATA_CACHE_COMPRESS') && function_exists('gzcompress')) { //启用数据压缩 $content = gzuncompress($content); } $content = unserialize($content); return $content; } else { return false; } } /** * 写入缓存 * @access public * @param string $name 缓存变量名 * @param mixed $value 存储数据 * @param int $expire 有效时间 0为永久 * @return boolean */ public function set($name,$value,$expire=null) { N('cache_write',1); if(is_null($expire)) { $expire = $this->options['expire']; } $filename = $this->filename($name); $data = serialize($value); if( C('DATA_CACHE_COMPRESS') && function_exists('gzcompress')) { //数据压缩 $data = gzcompress($data,3); } if(C('DATA_CACHE_CHECK')) {//开启数据校验 $check = md5($data); }else { $check = ''; } $data = ""; $result = file_put_contents($filename,$data); if($result) { if($this->options['length']>0) { // 记录缓存队列 $this->queue($name); } clearstatcache(); return true; }else { return false; } } /** * 删除缓存 * @access public * @param string $name 缓存变量名 * @return boolean */ public function rm($name) { return unlink($this->filename($name)); } /** * 清除缓存 * @access public * @param string $name 缓存变量名 * @return boolean */ public function clear() { $path = $this->options['temp']; $files = scandir($path); if($files){ foreach($files as $file){ if ($file != '.' && $file != '..' && is_dir($path.$file) ){ array_map( 'unlink', glob( $path.$file.'/*.*' ) ); }elseif(is_file($path.$file)){ unlink( $path . $file ); } } return true; } return false; } } ================================================ FILE: ThinkPHP/Library/Think/Cache/Driver/Memcache.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Cache\Driver; use Think\Cache; defined('THINK_PATH') or exit(); /** * Memcache缓存驱动 */ class Memcache extends Cache { /** * 架构函数 * @param array $options 缓存参数 * @access public */ function __construct($options=array()) { if ( !extension_loaded('memcache') ) { E(L('_NOT_SUPPORT_').':memcache'); } $options = array_merge(array ( 'host' => C('MEMCACHE_HOST') ? : '127.0.0.1', 'port' => C('MEMCACHE_PORT') ? : 11211, 'timeout' => C('DATA_CACHE_TIMEOUT') ? : false, 'persistent' => false, ),$options); $this->options = $options; $this->options['expire'] = isset($options['expire'])? $options['expire'] : C('DATA_CACHE_TIME'); $this->options['prefix'] = isset($options['prefix'])? $options['prefix'] : C('DATA_CACHE_PREFIX'); $this->options['length'] = isset($options['length'])? $options['length'] : 0; $func = $options['persistent'] ? 'pconnect' : 'connect'; $this->handler = new \Memcache; $options['timeout'] === false ? $this->handler->$func($options['host'], $options['port']) : $this->handler->$func($options['host'], $options['port'], $options['timeout']); } /** * 读取缓存 * @access public * @param string $name 缓存变量名 * @return mixed */ public function get($name) { N('cache_read',1); return $this->handler->get($this->options['prefix'].$name); } /** * 写入缓存 * @access public * @param string $name 缓存变量名 * @param mixed $value 存储数据 * @param integer $expire 有效时间(秒) * @return boolean */ public function set($name, $value, $expire = null) { N('cache_write',1); if(is_null($expire)) { $expire = $this->options['expire']; } $name = $this->options['prefix'].$name; if($this->handler->set($name, $value, 0, $expire)) { if($this->options['length']>0) { // 记录缓存队列 $this->queue($name); } return true; } return false; } /** * 删除缓存 * @access public * @param string $name 缓存变量名 * @return boolean */ public function rm($name, $ttl = false) { $name = $this->options['prefix'].$name; return $ttl === false ? $this->handler->delete($name) : $this->handler->delete($name, $ttl); } /** * 清除缓存 * @access public * @return boolean */ public function clear() { return $this->handler->flush(); } } ================================================ FILE: ThinkPHP/Library/Think/Cache/Driver/Memcached.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Cache\Driver; use Memcached as MemcachedResource; use Think\Cache; /** * Memcached缓存驱动 */ class Memcached extends Cache { /** * * @param array $options */ public function __construct($options = array()) { if ( !extension_loaded('memcached') ) { E(L('_NOT_SUPPORT_').':memcached'); } $options = array_merge(array( 'servers' => C('MEMCACHED_SERVER') ? : null, 'lib_options' => C('MEMCACHED_LIB') ? : null ), $options); $this->options = $options; $this->options['expire'] = isset($options['expire'])? $options['expire'] : C('DATA_CACHE_TIME'); $this->options['prefix'] = isset($options['prefix'])? $options['prefix'] : C('DATA_CACHE_PREFIX'); $this->options['length'] = isset($options['length'])? $options['length'] : 0; $this->handler = new MemcachedResource; $options['servers'] && $this->handler->addServers($options['servers']); $options['lib_options'] && $this->handler->setOptions($options['lib_options']); } /** * 读取缓存 * @access public * @param string $name 缓存变量名 * @return mixed */ public function get($name) { N('cache_read',1); return $this->handler->get($this->options['prefix'].$name); } /** * 写入缓存 * @access public * @param string $name 缓存变量名 * @param mixed $value 存储数据 * @param integer $expire 有效时间(秒) * @return boolean */ public function set($name, $value, $expire = null) { N('cache_write',1); if(is_null($expire)) { $expire = $this->options['expire']; } $name = $this->options['prefix'].$name; if($this->handler->set($name, $value, time() + $expire)) { if($this->options['length']>0) { // 记录缓存队列 $this->queue($name); } return true; } return false; } /** * 删除缓存 * @access public * @param string $name 缓存变量名 * @return boolean */ public function rm($name, $ttl = false) { $name = $this->options['prefix'].$name; return $ttl === false ? $this->handler->delete($name) : $this->handler->delete($name, $ttl); } /** * 清除缓存 * @access public * @return boolean */ public function clear() { return $this->handler->flush(); } } ================================================ FILE: ThinkPHP/Library/Think/Cache/Driver/Memcachesae.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Cache\Driver; use Think\Cache; defined('THINK_PATH') or exit(); /** * Memcache缓存驱动 * @category Extend * @package Extend * @subpackage Driver.Cache * @author liu21st */ class Memcachesae extends Cache { /** * 架构函数 * @param array $options 缓存参数 * @access public */ function __construct($options=array()) { $options = array_merge(array ( 'host' => C('MEMCACHE_HOST') ? : '127.0.0.1', 'port' => C('MEMCACHE_PORT') ? : 11211, 'timeout' => C('DATA_CACHE_TIMEOUT') ? : false, 'persistent' => false, ),$options); $this->options = $options; $this->options['expire'] = isset($options['expire'])? $options['expire'] : C('DATA_CACHE_TIME'); $this->options['prefix'] = isset($options['prefix'])? $options['prefix'] : C('DATA_CACHE_PREFIX'); $this->options['length'] = isset($options['length'])? $options['length'] : 0; $this->handler = memcache_init();//[sae] 下实例化 //[sae] 下不用链接 $this->connected=true; } /** * 是否连接 * @access private * @return boolean */ private function isConnected() { return $this->connected; } /** * 读取缓存 * @access public * @param string $name 缓存变量名 * @return mixed */ public function get($name) { N('cache_read',1); return $this->handler->get($_SERVER['HTTP_APPVERSION'].'/'.$this->options['prefix'].$name); } /** * 写入缓存 * @access public * @param string $name 缓存变量名 * @param mixed $value 存储数据 * @param integer $expire 有效时间(秒) * @return boolean */ public function set($name, $value, $expire = null) { N('cache_write',1); if(is_null($expire)) { $expire = $this->options['expire']; } $name = $this->options['prefix'].$name; if($this->handler->set($_SERVER['HTTP_APPVERSION'].'/'.$name, $value, 0, $expire)) { if($this->options['length']>0) { // 记录缓存队列 $this->queue($name); } return true; } return false; } /** * 删除缓存 * @access public * @param string $name 缓存变量名 * @return boolean */ public function rm($name, $ttl = false) { $name = $_SERVER['HTTP_APPVERSION'].'/'.$this->options['prefix'].$name; return $ttl === false ? $this->handler->delete($name) : $this->handler->delete($name, $ttl); } /** * 清除缓存 * @access public * @return boolean */ public function clear() { return $this->handler->flush(); } /** * 队列缓存 * @access protected * @param string $key 队列名 * @return mixed */ //[sae] 下重写queque队列缓存方法 protected function queue($key) { $queue_name=isset($this->options['queue_name'])?$this->options['queue_name']:'think_queue'; $value = F($queue_name); if(!$value) { $value = array(); } // 进列 if(false===array_search($key, $value)) array_push($value,$key); if(count($value) > $this->options['length']) { // 出列 $key = array_shift($value); // 删除缓存 $this->rm($key); if (APP_DEBUG) { //调试模式下记录出队次数 $counter = Think::instance('SaeCounter'); if ($counter->exists($queue_name.'_out_times')) $counter->incr($queue_name.'_out_times'); else $counter->create($queue_name.'_out_times', 1); } } return F($queue_name,$value); } } ================================================ FILE: ThinkPHP/Library/Think/Cache/Driver/Redis.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Cache\Driver; use Think\Cache; defined('THINK_PATH') or exit(); /** * Redis缓存驱动 * 要求安装phpredis扩展:https://github.com/nicolasff/phpredis */ class Redis extends Cache { /** * 架构函数 * @param array $options 缓存参数 * @access public */ public function __construct($options=array()) { if ( !extension_loaded('redis') ) { E(L('_NOT_SUPPORT_').':redis'); } $options = array_merge(array ( 'host' => C('REDIS_HOST') ? : '127.0.0.1', 'port' => C('REDIS_PORT') ? : 6379, 'timeout' => C('DATA_CACHE_TIMEOUT') ? : false, 'persistent' => false, ),$options); $this->options = $options; $this->options['expire'] = isset($options['expire'])? $options['expire'] : C('DATA_CACHE_TIME'); $this->options['prefix'] = isset($options['prefix'])? $options['prefix'] : C('DATA_CACHE_PREFIX'); $this->options['length'] = isset($options['length'])? $options['length'] : 0; $func = $options['persistent'] ? 'pconnect' : 'connect'; $this->handler = new \Redis; $options['timeout'] === false ? $this->handler->$func($options['host'], $options['port']) : $this->handler->$func($options['host'], $options['port'], $options['timeout']); } /** * 读取缓存 * @access public * @param string $name 缓存变量名 * @return mixed */ public function get($name) { N('cache_read',1); $value = $this->handler->get($this->options['prefix'].$name); $jsonData = json_decode( $value, true ); return ($jsonData === NULL) ? $value : $jsonData; //检测是否为JSON数据 true 返回JSON解析数组, false返回源数据 } /** * 写入缓存 * @access public * @param string $name 缓存变量名 * @param mixed $value 存储数据 * @param integer $expire 有效时间(秒) * @return boolean */ public function set($name, $value, $expire = null) { N('cache_write',1); if(is_null($expire)) { $expire = $this->options['expire']; } $name = $this->options['prefix'].$name; //对数组/对象数据进行缓存处理,保证数据完整性 $value = (is_object($value) || is_array($value)) ? json_encode($value) : $value; if(is_int($expire) && $expire) { $result = $this->handler->setex($name, $expire, $value); }else{ $result = $this->handler->set($name, $value); } if($result && $this->options['length']>0) { // 记录缓存队列 $this->queue($name); } return $result; } /** * 删除缓存 * @access public * @param string $name 缓存变量名 * @return boolean */ public function rm($name) { return $this->handler->delete($this->options['prefix'].$name); } /** * 清除缓存 * @access public * @return boolean */ public function clear() { return $this->handler->flushDB(); } } ================================================ FILE: ThinkPHP/Library/Think/Cache/Driver/Shmop.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Cache\Driver; use Think\Cache; defined('THINK_PATH') or exit(); /** * Shmop缓存驱动 */ class Shmop extends Cache { /** * 架构函数 * @param array $options 缓存参数 * @access public */ public function __construct($options=array()) { if ( !extension_loaded('shmop') ) { E(L('_NOT_SUPPORT_').':shmop'); } if(!empty($options)){ $options = array( 'size' => C('SHARE_MEM_SIZE'), 'temp' => TEMP_PATH, 'project' => 's', 'length' => 0, ); } $this->options = $options; $this->options['prefix'] = isset($options['prefix'])? $options['prefix'] : C('DATA_CACHE_PREFIX'); $this->options['length'] = isset($options['length'])? $options['length'] : 0; $this->handler = $this->_ftok($this->options['project']); } /** * 读取缓存 * @access public * @param string $name 缓存变量名 * @return mixed */ public function get($name = false) { N('cache_read',1); $id = shmop_open($this->handler, 'c', 0600, 0); if ($id !== false) { $ret = unserialize(shmop_read($id, 0, shmop_size($id))); shmop_close($id); if ($name === false) { return $ret; } $name = $this->options['prefix'].$name; if(isset($ret[$name])) { $content = $ret[$name]; if(C('DATA_CACHE_COMPRESS') && function_exists('gzcompress')) { //启用数据压缩 $content = gzuncompress($content); } return $content; }else { return null; } }else { return false; } } /** * 写入缓存 * @access public * @param string $name 缓存变量名 * @param mixed $value 存储数据 * @return boolean */ public function set($name, $value) { N('cache_write',1); $lh = $this->_lock(); $val = $this->get(); if (!is_array($val)) $val = array(); if( C('DATA_CACHE_COMPRESS') && function_exists('gzcompress')) { //数据压缩 $value = gzcompress($value,3); } $name = $this->options['prefix'].$name; $val[$name] = $value; $val = serialize($val); if($this->_write($val, $lh)) { if($this->options['length']>0) { // 记录缓存队列 $this->queue($name); } return true; } return false; } /** * 删除缓存 * @access public * @param string $name 缓存变量名 * @return boolean */ public function rm($name) { $lh = $this->_lock(); $val = $this->get(); if (!is_array($val)) $val = array(); $name = $this->options['prefix'].$name; unset($val[$name]); $val = serialize($val); return $this->_write($val, $lh); } /** * 生成IPC key * @access private * @param string $project 项目标识名 * @return integer */ private function _ftok($project) { if (function_exists('ftok')) return ftok(__FILE__, $project); if(strtoupper(PHP_OS) == 'WINNT'){ $s = stat(__FILE__); return sprintf("%u", (($s['ino'] & 0xffff) | (($s['dev'] & 0xff) << 16) | (($project & 0xff) << 24))); }else { $filename = __FILE__ . (string) $project; for($key = array(); sizeof($key) < strlen($filename); $key[] = ord(substr($filename, sizeof($key), 1))); return dechex(array_sum($key)); } } /** * 写入操作 * @access private * @param string $name 缓存变量名 * @return integer|boolean */ private function _write(&$val, &$lh) { $id = shmop_open($this->handler, 'c', 0600, $this->options['size']); if ($id) { $ret = shmop_write($id, $val, 0) == strlen($val); shmop_close($id); $this->_unlock($lh); return $ret; } $this->_unlock($lh); return false; } /** * 共享锁定 * @access private * @param string $name 缓存变量名 * @return boolean */ private function _lock() { if (function_exists('sem_get')) { $fp = sem_get($this->handler, 1, 0600, 1); sem_acquire ($fp); } else { $fp = fopen($this->options['temp'].$this->options['prefix'].md5($this->handler), 'w'); flock($fp, LOCK_EX); } return $fp; } /** * 解除共享锁定 * @access private * @param string $name 缓存变量名 * @return boolean */ private function _unlock(&$fp) { if (function_exists('sem_release')) { sem_release($fp); } else { fclose($fp); } } } ================================================ FILE: ThinkPHP/Library/Think/Cache/Driver/Sqlite.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Cache\Driver; use Think\Cache; defined('THINK_PATH') or exit(); /** * Sqlite缓存驱动 */ class Sqlite extends Cache { /** * 架构函数 * @param array $options 缓存参数 * @access public */ public function __construct($options=array()) { if ( !extension_loaded('sqlite') ) { E(L('_NOT_SUPPORT_').':sqlite'); } if(empty($options)) { $options = array ( 'db' => ':memory:', 'table' => 'sharedmemory', ); } $this->options = $options; $this->options['prefix'] = isset($options['prefix'])? $options['prefix'] : C('DATA_CACHE_PREFIX'); $this->options['length'] = isset($options['length'])? $options['length'] : 0; $this->options['expire'] = isset($options['expire'])? $options['expire'] : C('DATA_CACHE_TIME'); $func = $this->options['persistent'] ? 'sqlite_popen' : 'sqlite_open'; $this->handler = $func($this->options['db']); } /** * 读取缓存 * @access public * @param string $name 缓存变量名 * @return mixed */ public function get($name) { N('cache_read',1); $name = $this->options['prefix'].sqlite_escape_string($name); $sql = 'SELECT value FROM '.$this->options['table'].' WHERE var=\''.$name.'\' AND (expire=0 OR expire >'.time().') LIMIT 1'; $result = sqlite_query($this->handler, $sql); if (sqlite_num_rows($result)) { $content = sqlite_fetch_single($result); if(C('DATA_CACHE_COMPRESS') && function_exists('gzcompress')) { //启用数据压缩 $content = gzuncompress($content); } return unserialize($content); } return false; } /** * 写入缓存 * @access public * @param string $name 缓存变量名 * @param mixed $value 存储数据 * @param integer $expire 有效时间(秒) * @return boolean */ public function set($name, $value,$expire=null) { N('cache_write',1); $name = $this->options['prefix'].sqlite_escape_string($name); $value = sqlite_escape_string(serialize($value)); if(is_null($expire)) { $expire = $this->options['expire']; } $expire = ($expire==0)?0: (time()+$expire) ;//缓存有效期为0表示永久缓存 if( C('DATA_CACHE_COMPRESS') && function_exists('gzcompress')) { //数据压缩 $value = gzcompress($value,3); } $sql = 'REPLACE INTO '.$this->options['table'].' (var, value,expire) VALUES (\''.$name.'\', \''.$value.'\', \''.$expire.'\')'; if(sqlite_query($this->handler, $sql)){ if($this->options['length']>0) { // 记录缓存队列 $this->queue($name); } return true; } return false; } /** * 删除缓存 * @access public * @param string $name 缓存变量名 * @return boolean */ public function rm($name) { $name = $this->options['prefix'].sqlite_escape_string($name); $sql = 'DELETE FROM '.$this->options['table'].' WHERE var=\''.$name.'\''; sqlite_query($this->handler, $sql); return true; } /** * 清除缓存 * @access public * @return boolean */ public function clear() { $sql = 'DELETE FROM '.$this->options['table']; sqlite_query($this->handler, $sql); return ; } } ================================================ FILE: ThinkPHP/Library/Think/Cache/Driver/Wincache.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Cache\Driver; use Think\Cache; defined('THINK_PATH') or exit(); /** * Wincache缓存驱动 */ class Wincache extends Cache { /** * 架构函数 * @param array $options 缓存参数 * @access public */ public function __construct($options=array()) { if ( !function_exists('wincache_ucache_info') ) { E(L('_NOT_SUPPORT_').':WinCache'); } $this->options['expire'] = isset($options['expire'])? $options['expire'] : C('DATA_CACHE_TIME'); $this->options['prefix'] = isset($options['prefix'])? $options['prefix'] : C('DATA_CACHE_PREFIX'); $this->options['length'] = isset($options['length'])? $options['length'] : 0; } /** * 读取缓存 * @access public * @param string $name 缓存变量名 * @return mixed */ public function get($name) { N('cache_read',1); $name = $this->options['prefix'].$name; return wincache_ucache_exists($name)? wincache_ucache_get($name) : false; } /** * 写入缓存 * @access public * @param string $name 缓存变量名 * @param mixed $value 存储数据 * @param integer $expire 有效时间(秒) * @return boolean */ public function set($name, $value,$expire=null) { N('cache_write',1); if(is_null($expire)) { $expire = $this->options['expire']; } $name = $this->options['prefix'].$name; if(wincache_ucache_set($name, $value, $expire)) { if($this->options['length']>0) { // 记录缓存队列 $this->queue($name); } return true; } return false; } /** * 删除缓存 * @access public * @param string $name 缓存变量名 * @return boolean */ public function rm($name) { return wincache_ucache_delete($this->options['prefix'].$name); } /** * 清除缓存 * @access public * @return boolean */ public function clear() { return wincache_ucache_clear(); } } ================================================ FILE: ThinkPHP/Library/Think/Cache/Driver/Xcache.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Cache\Driver; use Think\Cache; defined('THINK_PATH') or exit(); /** * Xcache缓存驱动 */ class Xcache extends Cache { /** * 架构函数 * @param array $options 缓存参数 * @access public */ public function __construct($options=array()) { if ( !function_exists('xcache_info') ) { E(L('_NOT_SUPPORT_').':Xcache'); } $this->options['expire'] = isset($options['expire'])?$options['expire']:C('DATA_CACHE_TIME'); $this->options['prefix'] = isset($options['prefix'])?$options['prefix']:C('DATA_CACHE_PREFIX'); $this->options['length'] = isset($options['length'])?$options['length']:0; } /** * 读取缓存 * @access public * @param string $name 缓存变量名 * @return mixed */ public function get($name) { N('cache_read',1); $name = $this->options['prefix'].$name; if (xcache_isset($name)) { return xcache_get($name); } return false; } /** * 写入缓存 * @access public * @param string $name 缓存变量名 * @param mixed $value 存储数据 * @param integer $expire 有效时间(秒) * @return boolean */ public function set($name, $value,$expire=null) { N('cache_write',1); if(is_null($expire)) { $expire = $this->options['expire'] ; } $name = $this->options['prefix'].$name; if(xcache_set($name, $value, $expire)) { if($this->options['length']>0) { // 记录缓存队列 $this->queue($name); } return true; } return false; } /** * 删除缓存 * @access public * @param string $name 缓存变量名 * @return boolean */ public function rm($name) { return xcache_unset($this->options['prefix'].$name); } /** * 清除缓存 * @access public * @return boolean */ public function clear() { return xcache_clear_cache(1, -1); } } ================================================ FILE: ThinkPHP/Library/Think/Cache.class.php ================================================ // +---------------------------------------------------------------------- namespace Think; /** * 缓存管理类 */ class Cache { /** * 操作句柄 * @var string * @access protected */ protected $handler ; /** * 缓存连接参数 * @var integer * @access protected */ protected $options = array(); /** * 连接缓存 * @access public * @param string $type 缓存类型 * @param array $options 配置数组 * @return object */ public function connect($type='',$options=array()) { if(empty($type)) $type = C('DATA_CACHE_TYPE'); $class = strpos($type,'\\')? $type : 'Think\\Cache\\Driver\\'.ucwords(strtolower($type)); if(class_exists($class)) $cache = new $class($options); else E(L('_CACHE_TYPE_INVALID_').':'.$type); return $cache; } /** * 取得缓存类实例 * @static * @access public * @return mixed */ static function getInstance($type='',$options=array()) { static $_instance = array(); $guid = $type.to_guid_string($options); if(!isset($_instance[$guid])){ $obj = new Cache(); $_instance[$guid] = $obj->connect($type,$options); } return $_instance[$guid]; } public function __get($name) { return $this->get($name); } public function __set($name,$value) { return $this->set($name,$value); } public function __unset($name) { $this->rm($name); } public function setOptions($name,$value) { $this->options[$name] = $value; } public function getOptions($name) { return $this->options[$name]; } /** * 队列缓存 * @access protected * @param string $key 队列名 * @return mixed */ // protected function queue($key) { static $_handler = array( 'file' => array('F','F'), 'xcache'=> array('xcache_get','xcache_set'), 'apc' => array('apc_fetch','apc_store'), ); $queue = isset($this->options['queue'])?$this->options['queue']:'file'; $fun = isset($_handler[$queue])?$_handler[$queue]:$_handler['file']; $queue_name = isset($this->options['queue_name'])?$this->options['queue_name']:'think_queue'; $value = $fun[0]($queue_name); if(!$value) { $value = array(); } // 进列 if(false===array_search($key, $value)) array_push($value,$key); if(count($value) > $this->options['length']) { // 出列 $key = array_shift($value); // 删除缓存 $this->rm($key); if(APP_DEBUG){ //调试模式下,记录出列次数 N($queue_name.'_out_times',1); } } return $fun[1]($queue_name,$value); } public function __call($method,$args){ //调用缓存类型自己的方法 if(method_exists($this->handler, $method)){ return call_user_func_array(array($this->handler,$method), $args); }else{ E(__CLASS__.':'.$method.L('_METHOD_NOT_EXIST_')); return; } } } ================================================ FILE: ThinkPHP/Library/Think/Controller/HproseController.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Controller; /** * ThinkPHP Hprose控制器类 */ class HproseController { protected $allowMethodList = ''; protected $crossDomain = false; protected $P3P = false; protected $get = true; protected $debug = false; /** * 架构函数 * @access public */ public function __construct() { //控制器初始化 if(method_exists($this,'_initialize')) $this->_initialize(); //导入类库 Vendor('Hprose.HproseHttpServer'); //实例化HproseHttpServer $server = new \HproseHttpServer(); if($this->allowMethodList){ $methods = $this->allowMethodList; }else{ $methods = get_class_methods($this); $methods = array_diff($methods,array('__construct','__call','_initialize')); } $server->addMethods($methods,$this); if(APP_DEBUG || $this->debug ) { $server->setDebugEnabled(true); } // Hprose设置 $server->setCrossDomainEnabled($this->crossDomain); $server->setP3PEnabled($this->P3P); $server->setGetEnabled($this->get); // 启动server $server->start(); } /** * 魔术方法 有不存在的操作的时候执行 * @access public * @param string $method 方法名 * @param array $args 参数 * @return mixed */ public function __call($method,$args){} } ================================================ FILE: ThinkPHP/Library/Think/Controller/JsonRpcController.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Controller; /** * ThinkPHP JsonRPC控制器类 */ class JsonRpcController { /** * 架构函数 * @access public */ public function __construct() { //控制器初始化 if(method_exists($this,'_initialize')) $this->_initialize(); //导入类库 Vendor('jsonRPC.jsonRPCServer'); // 启动server \jsonRPCServer::handle($this); } /** * 魔术方法 有不存在的操作的时候执行 * @access public * @param string $method 方法名 * @param array $args 参数 * @return mixed */ public function __call($method,$args){} } ================================================ FILE: ThinkPHP/Library/Think/Controller/RestController.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Controller; use Think\Controller; /** * ThinkPHP REST控制器类 */ class RestController extends Controller { // 当前请求类型 protected $_method = ''; // 当前请求的资源类型 protected $_type = ''; // REST允许的请求类型列表 protected $allowMethod = array('get','post','put','delete'); // REST默认请求类型 protected $defaultMethod = 'get'; // REST允许请求的资源类型列表 protected $allowType = array('html','xml','json','rss'); // 默认的资源类型 protected $defaultType = 'html'; // REST允许输出的资源类型列表 protected $allowOutputType= array( 'xml' => 'application/xml', 'json' => 'application/json', 'html' => 'text/html', ); /** * 架构函数 * @access public */ public function __construct() { // 资源类型检测 if(''==__EXT__) { // 自动检测资源类型 $this->_type = $this->getAcceptType(); }elseif(!in_array(__EXT__,$this->allowType)) { // 资源类型非法 则用默认资源类型访问 $this->_type = $this->defaultType; }else{ $this->_type = __EXT__ ; } // 请求方式检测 $method = strtolower(REQUEST_METHOD); if(!in_array($method,$this->allowMethod)) { // 请求方式非法 则用默认请求方法 $method = $this->defaultMethod; } $this->_method = $method; parent::__construct(); } /** * 魔术方法 有不存在的操作的时候执行 * @access public * @param string $method 方法名 * @param array $args 参数 * @return mixed */ public function __call($method,$args) { if( 0 === strcasecmp($method,ACTION_NAME.C('ACTION_SUFFIX'))) { if(method_exists($this,$method.'_'.$this->_method.'_'.$this->_type)) { // RESTFul方法支持 $fun = $method.'_'.$this->_method.'_'.$this->_type; $this->$fun(); }elseif($this->_method == $this->defaultMethod && method_exists($this,$method.'_'.$this->_type) ){ $fun = $method.'_'.$this->_type; $this->$fun(); }elseif($this->_type == $this->defaultType && method_exists($this,$method.'_'.$this->_method) ){ $fun = $method.'_'.$this->_method; $this->$fun(); }elseif(method_exists($this,'_empty')) { // 如果定义了_empty操作 则调用 $this->_empty($method,$args); }elseif(file_exists_case($this->view->parseTemplate())){ // 检查是否存在默认模版 如果有直接输出模版 $this->display(); }else{ E(L('_ERROR_ACTION_').':'.ACTION_NAME); } } } /** * 获取当前请求的Accept头信息 * @return string */ protected function getAcceptType(){ $type = array( 'xml' => 'application/xml,text/xml,application/x-xml', 'json' => 'application/json,text/x-json,application/jsonrequest,text/json', 'js' => 'text/javascript,application/javascript,application/x-javascript', 'css' => 'text/css', 'rss' => 'application/rss+xml', 'yaml' => 'application/x-yaml,text/yaml', 'atom' => 'application/atom+xml', 'pdf' => 'application/pdf', 'text' => 'text/plain', 'png' => 'image/png', 'jpg' => 'image/jpg,image/jpeg,image/pjpeg', 'gif' => 'image/gif', 'csv' => 'text/csv', 'html' => 'text/html,application/xhtml+xml,*/*' ); foreach($type as $key=>$val){ $array = explode(',',$val); foreach($array as $k=>$v){ if(stristr($_SERVER['HTTP_ACCEPT'], $v)) { return $key; } } } return false; } // 发送Http状态信息 protected function sendHttpStatus($code) { static $_status = array( // Informational 1xx 100 => 'Continue', 101 => 'Switching Protocols', // Success 2xx 200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content', // Redirection 3xx 300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Moved Temporarily ', // 1.1 303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy', // 306 is deprecated but reserved 307 => 'Temporary Redirect', // Client Error 4xx 400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Timeout', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Long', 415 => 'Unsupported Media Type', 416 => 'Requested Range Not Satisfiable', 417 => 'Expectation Failed', // Server Error 5xx 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Timeout', 505 => 'HTTP Version Not Supported', 509 => 'Bandwidth Limit Exceeded' ); if(isset($_status[$code])) { header('HTTP/1.1 '.$code.' '.$_status[$code]); // 确保FastCGI模式下正常 header('Status:'.$code.' '.$_status[$code]); } } /** * 编码数据 * @access protected * @param mixed $data 要返回的数据 * @param String $type 返回类型 JSON XML * @return string */ protected function encodeData($data,$type='') { if(empty($data)) return ''; if('json' == $type) { // 返回JSON数据格式到客户端 包含状态信息 $data = json_encode($data); }elseif('xml' == $type){ // 返回xml格式数据 $data = xml_encode($data); }elseif('php'==$type){ $data = serialize($data); }// 默认直接输出 $this->setContentType($type); //header('Content-Length: ' . strlen($data)); return $data; } /** * 设置页面输出的CONTENT_TYPE和编码 * @access public * @param string $type content_type 类型对应的扩展名 * @param string $charset 页面输出编码 * @return void */ public function setContentType($type, $charset=''){ if(headers_sent()) return; if(empty($charset)) $charset = C('DEFAULT_CHARSET'); $type = strtolower($type); if(isset($this->allowOutputType[$type])) //过滤content_type header('Content-Type: '.$this->allowOutputType[$type].'; charset='.$charset); } /** * 输出返回数据 * @access protected * @param mixed $data 要返回的数据 * @param String $type 返回类型 JSON XML * @param integer $code HTTP状态 * @return void */ protected function response($data,$type='',$code=200) { $this->sendHttpStatus($code); exit($this->encodeData($data,strtolower($type))); } } ================================================ FILE: ThinkPHP/Library/Think/Controller/RpcController.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Controller; /** * ThinkPHP RPC控制器类 */ class RpcController { protected $allowMethodList = ''; protected $debug = false; /** * 架构函数 * @access public */ public function __construct() { //控制器初始化 if(method_exists($this,'_initialize')) $this->_initialize(); //导入类库 Vendor('phpRPC.phprpc_server'); //实例化phprpc $server = new \PHPRPC_Server(); if($this->allowMethodList){ $methods = $this->allowMethodList; }else{ $methods = get_class_methods($this); $methods = array_diff($methods,array('__construct','__call','_initialize')); } $server->add($methods,$this); if(APP_DEBUG || $this->debug ) { $server->setDebugMode(true); } $server->setEnableGZIP(true); $server->start(); echo $server->comment(); } /** * 魔术方法 有不存在的操作的时候执行 * @access public * @param string $method 方法名 * @param array $args 参数 * @return mixed */ public function __call($method,$args){} } ================================================ FILE: ThinkPHP/Library/Think/Controller/YarController.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Controller; /** * ThinkPHP Yar控制器类 */ class YarController { /** * 架构函数 * @access public */ public function __construct() { //控制器初始化 if(method_exists($this,'_initialize')) $this->_initialize(); //判断扩展是否存在 if(!extension_loaded('yar')) E(L('_NOT_SUPPORT_').':yar'); //实例化Yar_Server $server = new \Yar_Server($this); // 启动server $server->handle(); } /** * 魔术方法 有不存在的操作的时候执行 * @access public * @param string $method 方法名 * @param array $args 参数 * @return mixed */ public function __call($method,$args){} } ================================================ FILE: ThinkPHP/Library/Think/Controller.class.php ================================================ // +---------------------------------------------------------------------- namespace Think; /** * ThinkPHP 控制器基类 抽象类 */ abstract class Controller { /** * 视图实例对象 * @var view * @access protected */ protected $view = null; /** * 控制器参数 * @var config * @access protected */ protected $config = array(); /** * 架构函数 取得模板对象实例 * @access public */ public function __construct() { Hook::listen('action_begin',$this->config); //实例化视图类 $this->view = Think::instance('Think\View'); //控制器初始化 if(method_exists($this,'_initialize')) $this->_initialize(); } /** * 模板显示 调用内置的模板引擎显示方法, * @access protected * @param string $templateFile 指定要调用的模板文件 * 默认为空 由系统自动定位模板文件 * @param string $charset 输出编码 * @param string $contentType 输出类型 * @param string $content 输出内容 * @param string $prefix 模板缓存前缀 * @return void */ protected function display($templateFile='',$charset='',$contentType='',$content='',$prefix='') { $this->view->display($templateFile,$charset,$contentType,$content,$prefix); } /** * 输出内容文本可以包括Html 并支持内容解析 * @access protected * @param string $content 输出内容 * @param string $charset 模板输出字符集 * @param string $contentType 输出类型 * @param string $prefix 模板缓存前缀 * @return mixed */ protected function show($content,$charset='',$contentType='',$prefix='') { $this->view->display('',$charset,$contentType,$content,$prefix); } /** * 获取输出页面内容 * 调用内置的模板引擎fetch方法, * @access protected * @param string $templateFile 指定要调用的模板文件 * 默认为空 由系统自动定位模板文件 * @param string $content 模板输出内容 * @param string $prefix 模板缓存前缀* * @return string */ protected function fetch($templateFile='',$content='',$prefix='') { return $this->view->fetch($templateFile,$content,$prefix); } /** * 创建静态页面 * @access protected * @htmlfile 生成的静态文件名称 * @htmlpath 生成的静态文件路径 * @param string $templateFile 指定要调用的模板文件 * 默认为空 由系统自动定位模板文件 * @return string */ protected function buildHtml($htmlfile='',$htmlpath='',$templateFile='') { $content = $this->fetch($templateFile); $htmlpath = !empty($htmlpath)?$htmlpath:HTML_PATH; $htmlfile = $htmlpath.$htmlfile.C('HTML_FILE_SUFFIX'); Storage::put($htmlfile,$content,'html'); return $content; } /** * 模板主题设置 * @access protected * @param string $theme 模版主题 * @return Action */ protected function theme($theme){ $this->view->theme($theme); return $this; } /** * 模板变量赋值 * @access protected * @param mixed $name 要显示的模板变量 * @param mixed $value 变量的值 * @return Action */ protected function assign($name,$value='') { $this->view->assign($name,$value); return $this; } public function __set($name,$value) { $this->assign($name,$value); } /** * 取得模板显示变量的值 * @access protected * @param string $name 模板显示变量 * @return mixed */ public function get($name='') { return $this->view->get($name); } public function __get($name) { return $this->get($name); } /** * 检测模板变量的值 * @access public * @param string $name 名称 * @return boolean */ public function __isset($name) { return $this->get($name); } /** * 魔术方法 有不存在的操作的时候执行 * @access public * @param string $method 方法名 * @param array $args 参数 * @return mixed */ public function __call($method,$args) { if( 0 === strcasecmp($method,ACTION_NAME.C('ACTION_SUFFIX'))) { if(method_exists($this,'_empty')) { // 如果定义了_empty操作 则调用 $this->_empty($method,$args); }elseif(file_exists_case($this->view->parseTemplate())){ // 检查是否存在默认模版 如果有直接输出模版 $this->display(); }else{ E(L('_ERROR_ACTION_').':'.ACTION_NAME); } }else{ E(__CLASS__.':'.$method.L('_METHOD_NOT_EXIST_')); return; } } /** * 操作错误跳转的快捷方法 * @access protected * @param string $message 错误信息 * @param string $jumpUrl 页面跳转地址 * @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间 * @return void */ protected function error($message='',$jumpUrl='',$ajax=false) { $this->dispatchJump($message,0,$jumpUrl,$ajax); } /** * 操作成功跳转的快捷方法 * @access protected * @param string $message 提示信息 * @param string $jumpUrl 页面跳转地址 * @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间 * @return void */ protected function success($message='',$jumpUrl='',$ajax=false) { $this->dispatchJump($message,1,$jumpUrl,$ajax); } /** * Ajax方式返回数据到客户端 * @access protected * @param mixed $data 要返回的数据 * @param String $type AJAX返回数据格式 * @param int $json_option 传递给json_encode的option参数 * @return void */ protected function ajaxReturn($data,$type='',$json_option=0) { if(empty($type)) $type = C('DEFAULT_AJAX_RETURN'); switch (strtoupper($type)){ case 'JSON' : // 返回JSON数据格式到客户端 包含状态信息 header('Content-Type:application/json; charset=utf-8'); exit(json_encode($data,$json_option)); case 'XML' : // 返回xml格式数据 header('Content-Type:text/xml; charset=utf-8'); exit(xml_encode($data)); case 'JSONP': // 返回JSON数据格式到客户端 包含状态信息 header('Content-Type:application/json; charset=utf-8'); $handler = isset($_GET[C('VAR_JSONP_HANDLER')]) ? $_GET[C('VAR_JSONP_HANDLER')] : C('DEFAULT_JSONP_HANDLER'); exit($handler.'('.json_encode($data,$json_option).');'); case 'EVAL' : // 返回可执行的js脚本 header('Content-Type:text/html; charset=utf-8'); exit($data); default : // 用于扩展其他返回格式数据 Hook::listen('ajax_return',$data); } } /** * Action跳转(URL重定向) 支持指定模块和延时跳转 * @access protected * @param string $url 跳转的URL表达式 * @param array $params 其它URL参数 * @param integer $delay 延时跳转的时间 单位为秒 * @param string $msg 跳转提示信息 * @return void */ protected function redirect($url,$params=array(),$delay=0,$msg='') { $url = U($url,$params); redirect($url,$delay,$msg); } /** * 默认跳转操作 支持错误导向和正确跳转 * 调用模板显示 默认为public目录下面的success页面 * 提示页面为可配置 支持模板标签 * @param string $message 提示信息 * @param Boolean $status 状态 * @param string $jumpUrl 页面跳转地址 * @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间 * @access private * @return void */ private function dispatchJump($message,$status=1,$jumpUrl='',$ajax=false) { if(true === $ajax || IS_AJAX) {// AJAX提交 $data = is_array($ajax)?$ajax:array(); $data['info'] = $message; $data['status'] = $status; $data['url'] = $jumpUrl; $this->ajaxReturn($data); } if(is_int($ajax)) $this->assign('waitSecond',$ajax); if(!empty($jumpUrl)) $this->assign('jumpUrl',$jumpUrl); // 提示标题 $this->assign('msgTitle',$status? L('_OPERATION_SUCCESS_') : L('_OPERATION_FAIL_')); //如果设置了关闭窗口,则提示完毕后自动关闭窗口 if($this->get('closeWin')) $this->assign('jumpUrl','javascript:window.close();'); $this->assign('status',$status); // 状态 //保证输出不受静态缓存影响 C('HTML_CACHE_ON',false); if($status) { //发送成功信息 $this->assign('message',$message);// 提示信息 // 成功操作后默认停留1秒 if(!isset($this->waitSecond)) $this->assign('waitSecond','1'); // 默认操作成功自动返回操作前页面 if(!isset($this->jumpUrl)) $this->assign("jumpUrl",$_SERVER["HTTP_REFERER"]); $this->display(C('TMPL_ACTION_SUCCESS')); }else{ $this->assign('error',$message);// 提示信息 //发生错误时候默认停留3秒 if(!isset($this->waitSecond)) $this->assign('waitSecond','3'); // 默认发生错误的话自动返回上页 if(!isset($this->jumpUrl)) $this->assign('jumpUrl',"javascript:history.back(-1);"); $this->display(C('TMPL_ACTION_ERROR')); // 中止执行 避免出错后继续执行 exit ; } } /** * 析构方法 * @access public */ public function __destruct() { // 执行后续操作 Hook::listen('action_end'); } } // 设置控制器别名 便于升级 class_alias('Think\Controller','Think\Action'); ================================================ FILE: ThinkPHP/Library/Think/Crypt/Driver/Base64.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Crypt\Driver; /** * Base64 加密实现类 */ class Base64 { /** * 加密字符串 * @param string $str 字符串 * @param string $key 加密key * @param integer $expire 有效期(秒) * @return string */ public static function encrypt($data,$key,$expire=0) { $expire = sprintf('%010d', $expire ? $expire + time():0); $key = md5($key); $data = base64_encode($expire.$data); $x=0; $len = strlen($data); $l = strlen($key); for ($i=0;$i< $len;$i++) { if ($x== $l) $x=0; $char .=substr($key,$x,1); $x++; } for ($i=0;$i< $len;$i++) { $str .=chr(ord(substr($data,$i,1))+(ord(substr($char,$i,1)))%256); } return $str; } /** * 解密字符串 * @param string $str 字符串 * @param string $key 加密key * @return string */ public static function decrypt($data,$key) { $key = md5($key); $x=0; $len = strlen($data); $l = strlen($key); for ($i=0;$i< $len;$i++) { if ($x== $l) $x=0; $char .=substr($key,$x,1); $x++; } for ($i=0;$i< $len;$i++) { if (ord(substr($data,$i,1)) 0 && $expire < time()) { return ''; } $data = substr($data,10); return $data; } } ================================================ FILE: ThinkPHP/Library/Think/Crypt/Driver/Crypt.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Crypt\Driver; /** * Crypt 加密实现类 * @category ORG * @package ORG * @subpackage Crypt * @author liu21st */ class Crypt { /** * 加密字符串 * @param string $str 字符串 * @param string $key 加密key * @param integer $expire 有效期(秒) * @return string */ public static function encrypt($str,$key,$expire=0){ $expire = sprintf('%010d', $expire ? $expire + time():0); $r = md5($key); $c = 0; $v = ""; $str = $expire.$str; $len = strlen($str); $l = strlen($r); for ($i=0;$i<$len;$i++){ if ($c== $l) $c=0; $v .= substr($r,$c,1) . (substr($str,$i,1) ^ substr($r,$c,1)); $c++; } return self::ed($v,$key); } /** * 解密字符串 * @param string $str 字符串 * @param string $key 加密key * @return string */ public static function decrypt($str,$key) { $str = self::ed($str,$key); $v = ""; $len = strlen($str); for ($i=0;$i<$len;$i++){ $md5 = substr($str,$i,1); $i++; $v .= (substr($str,$i,1) ^ $md5); } $data = $v; $expire = substr($data,0,10); if($expire > 0 && $expire < time()) { return ''; } $data = substr($data,10); return $data; } static private function ed($str,$key) { $r = md5($key); $c = 0; $v = ''; $len = strlen($str); $l = strlen($r); for ($i=0;$i<$len;$i++) { if ($c==$l) $c=0; $v .= substr($str,$i,1) ^ substr($r,$c,1); $c++; } return $v; } } ================================================ FILE: ThinkPHP/Library/Think/Crypt/Driver/Des.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Crypt\Driver; /** * Des 加密实现类 * Converted from JavaScript to PHP by Jim Gibbs, June 2004 Paul Tero, July 2001 * Optimised for performance with large blocks by Michael Hayworth, November 2001 * http://www.netdealing.com */ class Des { /** * 加密字符串 * @param string $str 字符串 * @param string $key 加密key * @param integer $expire 有效期(秒) * @return string */ public static function encrypt($str, $key,$expire=0) { if ($str == "") { return ""; } $expire = sprintf('%010d', $expire ? $expire + time():0); $str = $expire.$str; return self::_des($key,$str,1); } /** * 解密字符串 * @param string $str 字符串 * @param string $key 加密key * @return string */ public static function decrypt($str, $key) { if ($str == "") { return ""; } $data = self::_des($key,$str,0); $expire = substr($data,0,10); if($expire > 0 && $expire < time()) { return ''; } $data = substr($data,10); return $data; } /** * Des算法 * @param string $str 字符串 * @param string $key 加密key * @return string */ private static function _des($key, $message, $encrypt, $mode=0, $iv=null) { //declaring this locally speeds things up a bit $spfunction1 = array (0x1010400,0,0x10000,0x1010404,0x1010004,0x10404,0x4,0x10000,0x400,0x1010400,0x1010404,0x400,0x1000404,0x1010004,0x1000000,0x4,0x404,0x1000400,0x1000400,0x10400,0x10400,0x1010000,0x1010000,0x1000404,0x10004,0x1000004,0x1000004,0x10004,0,0x404,0x10404,0x1000000,0x10000,0x1010404,0x4,0x1010000,0x1010400,0x1000000,0x1000000,0x400,0x1010004,0x10000,0x10400,0x1000004,0x400,0x4,0x1000404,0x10404,0x1010404,0x10004,0x1010000,0x1000404,0x1000004,0x404,0x10404,0x1010400,0x404,0x1000400,0x1000400,0,0x10004,0x10400,0,0x1010004); $spfunction2 = array (-0x7fef7fe0,-0x7fff8000,0x8000,0x108020,0x100000,0x20,-0x7fefffe0,-0x7fff7fe0,-0x7fffffe0,-0x7fef7fe0,-0x7fef8000,-0x80000000,-0x7fff8000,0x100000,0x20,-0x7fefffe0,0x108000,0x100020,-0x7fff7fe0,0,-0x80000000,0x8000,0x108020,-0x7ff00000,0x100020,-0x7fffffe0,0,0x108000,0x8020,-0x7fef8000,-0x7ff00000,0x8020,0,0x108020,-0x7fefffe0,0x100000,-0x7fff7fe0,-0x7ff00000,-0x7fef8000,0x8000,-0x7ff00000,-0x7fff8000,0x20,-0x7fef7fe0,0x108020,0x20,0x8000,-0x80000000,0x8020,-0x7fef8000,0x100000,-0x7fffffe0,0x100020,-0x7fff7fe0,-0x7fffffe0,0x100020,0x108000,0,-0x7fff8000,0x8020,-0x80000000,-0x7fefffe0,-0x7fef7fe0,0x108000); $spfunction3 = array (0x208,0x8020200,0,0x8020008,0x8000200,0,0x20208,0x8000200,0x20008,0x8000008,0x8000008,0x20000,0x8020208,0x20008,0x8020000,0x208,0x8000000,0x8,0x8020200,0x200,0x20200,0x8020000,0x8020008,0x20208,0x8000208,0x20200,0x20000,0x8000208,0x8,0x8020208,0x200,0x8000000,0x8020200,0x8000000,0x20008,0x208,0x20000,0x8020200,0x8000200,0,0x200,0x20008,0x8020208,0x8000200,0x8000008,0x200,0,0x8020008,0x8000208,0x20000,0x8000000,0x8020208,0x8,0x20208,0x20200,0x8000008,0x8020000,0x8000208,0x208,0x8020000,0x20208,0x8,0x8020008,0x20200); $spfunction4 = array (0x802001,0x2081,0x2081,0x80,0x802080,0x800081,0x800001,0x2001,0,0x802000,0x802000,0x802081,0x81,0,0x800080,0x800001,0x1,0x2000,0x800000,0x802001,0x80,0x800000,0x2001,0x2080,0x800081,0x1,0x2080,0x800080,0x2000,0x802080,0x802081,0x81,0x800080,0x800001,0x802000,0x802081,0x81,0,0,0x802000,0x2080,0x800080,0x800081,0x1,0x802001,0x2081,0x2081,0x80,0x802081,0x81,0x1,0x2000,0x800001,0x2001,0x802080,0x800081,0x2001,0x2080,0x800000,0x802001,0x80,0x800000,0x2000,0x802080); $spfunction5 = array (0x100,0x2080100,0x2080000,0x42000100,0x80000,0x100,0x40000000,0x2080000,0x40080100,0x80000,0x2000100,0x40080100,0x42000100,0x42080000,0x80100,0x40000000,0x2000000,0x40080000,0x40080000,0,0x40000100,0x42080100,0x42080100,0x2000100,0x42080000,0x40000100,0,0x42000000,0x2080100,0x2000000,0x42000000,0x80100,0x80000,0x42000100,0x100,0x2000000,0x40000000,0x2080000,0x42000100,0x40080100,0x2000100,0x40000000,0x42080000,0x2080100,0x40080100,0x100,0x2000000,0x42080000,0x42080100,0x80100,0x42000000,0x42080100,0x2080000,0,0x40080000,0x42000000,0x80100,0x2000100,0x40000100,0x80000,0,0x40080000,0x2080100,0x40000100); $spfunction6 = array (0x20000010,0x20400000,0x4000,0x20404010,0x20400000,0x10,0x20404010,0x400000,0x20004000,0x404010,0x400000,0x20000010,0x400010,0x20004000,0x20000000,0x4010,0,0x400010,0x20004010,0x4000,0x404000,0x20004010,0x10,0x20400010,0x20400010,0,0x404010,0x20404000,0x4010,0x404000,0x20404000,0x20000000,0x20004000,0x10,0x20400010,0x404000,0x20404010,0x400000,0x4010,0x20000010,0x400000,0x20004000,0x20000000,0x4010,0x20000010,0x20404010,0x404000,0x20400000,0x404010,0x20404000,0,0x20400010,0x10,0x4000,0x20400000,0x404010,0x4000,0x400010,0x20004010,0,0x20404000,0x20000000,0x400010,0x20004010); $spfunction7 = array (0x200000,0x4200002,0x4000802,0,0x800,0x4000802,0x200802,0x4200800,0x4200802,0x200000,0,0x4000002,0x2,0x4000000,0x4200002,0x802,0x4000800,0x200802,0x200002,0x4000800,0x4000002,0x4200000,0x4200800,0x200002,0x4200000,0x800,0x802,0x4200802,0x200800,0x2,0x4000000,0x200800,0x4000000,0x200800,0x200000,0x4000802,0x4000802,0x4200002,0x4200002,0x2,0x200002,0x4000000,0x4000800,0x200000,0x4200800,0x802,0x200802,0x4200800,0x802,0x4000002,0x4200802,0x4200000,0x200800,0,0x2,0x4200802,0,0x200802,0x4200000,0x800,0x4000002,0x4000800,0x800,0x200002); $spfunction8 = array (0x10001040,0x1000,0x40000,0x10041040,0x10000000,0x10001040,0x40,0x10000000,0x40040,0x10040000,0x10041040,0x41000,0x10041000,0x41040,0x1000,0x40,0x10040000,0x10000040,0x10001000,0x1040,0x41000,0x40040,0x10040040,0x10041000,0x1040,0,0,0x10040040,0x10000040,0x10001000,0x41040,0x40000,0x41040,0x40000,0x10041000,0x1000,0x40,0x10040040,0x1000,0x41040,0x10001000,0x40,0x10000040,0x10040000,0x10040040,0x10000000,0x40000,0x10001040,0,0x10041040,0x40040,0x10000040,0x10040000,0x10001000,0x10001040,0,0x10041040,0x41000,0x41000,0x1040,0x1040,0x40040,0x10000000,0x10041000); $masks = array (4294967295,2147483647,1073741823,536870911,268435455,134217727,67108863,33554431,16777215,8388607,4194303,2097151,1048575,524287,262143,131071,65535,32767,16383,8191,4095,2047,1023,511,255,127,63,31,15,7,3,1,0); //create the 16 or 48 subkeys we will need $keys = self::_createKeys ($key); $m=0; $len = strlen($message); $chunk = 0; //set up the loops for single and triple des $iterations = ((count($keys) == 32) ? 3 : 9); //single or triple des if ($iterations == 3) {$looping = (($encrypt) ? array (0, 32, 2) : array (30, -2, -2));} else {$looping = (($encrypt) ? array (0, 32, 2, 62, 30, -2, 64, 96, 2) : array (94, 62, -2, 32, 64, 2, 30, -2, -2));} $message .= (chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0)); //pad the message out with null bytes //store the result here $result = ""; $tempresult = ""; if ($mode == 1) { //CBC mode $cbcleft = (ord($iv{$m++}) << 24) | (ord($iv{$m++}) << 16) | (ord($iv{$m++}) << 8) | ord($iv{$m++}); $cbcright = (ord($iv{$m++}) << 24) | (ord($iv{$m++}) << 16) | (ord($iv{$m++}) << 8) | ord($iv{$m++}); $m=0; } //loop through each 64 bit chunk of the message while ($m < $len) { $left = (ord($message{$m++}) << 24) | (ord($message{$m++}) << 16) | (ord($message{$m++}) << 8) | ord($message{$m++}); $right = (ord($message{$m++}) << 24) | (ord($message{$m++}) << 16) | (ord($message{$m++}) << 8) | ord($message{$m++}); //for Cipher Block Chaining mode, xor the message with the previous result if ($mode == 1) {if ($encrypt) {$left ^= $cbcleft; $right ^= $cbcright;} else {$cbcleft2 = $cbcleft; $cbcright2 = $cbcright; $cbcleft = $left; $cbcright = $right;}} //first each 64 but chunk of the message must be permuted according to IP $temp = (($left >> 4 & $masks[4]) ^ $right) & 0x0f0f0f0f; $right ^= $temp; $left ^= ($temp << 4); $temp = (($left >> 16 & $masks[16]) ^ $right) & 0x0000ffff; $right ^= $temp; $left ^= ($temp << 16); $temp = (($right >> 2 & $masks[2]) ^ $left) & 0x33333333; $left ^= $temp; $right ^= ($temp << 2); $temp = (($right >> 8 & $masks[8]) ^ $left) & 0x00ff00ff; $left ^= $temp; $right ^= ($temp << 8); $temp = (($left >> 1 & $masks[1]) ^ $right) & 0x55555555; $right ^= $temp; $left ^= ($temp << 1); $left = (($left << 1) | ($left >> 31 & $masks[31])); $right = (($right << 1) | ($right >> 31 & $masks[31])); //do this either 1 or 3 times for each chunk of the message for ($j=0; $j<$iterations; $j+=3) { $endloop = $looping[$j+1]; $loopinc = $looping[$j+2]; //now go through and perform the encryption or decryption for ($i=$looping[$j]; $i!=$endloop; $i+=$loopinc) { //for efficiency $right1 = $right ^ $keys[$i]; $right2 = (($right >> 4 & $masks[4]) | ($right << 28)) ^ $keys[$i+1]; //the result is attained by passing these bytes through the S selection functions $temp = $left; $left = $right; $right = $temp ^ ($spfunction2[($right1 >> 24 & $masks[24]) & 0x3f] | $spfunction4[($right1 >> 16 & $masks[16]) & 0x3f] | $spfunction6[($right1 >> 8 & $masks[8]) & 0x3f] | $spfunction8[$right1 & 0x3f] | $spfunction1[($right2 >> 24 & $masks[24]) & 0x3f] | $spfunction3[($right2 >> 16 & $masks[16]) & 0x3f] | $spfunction5[($right2 >> 8 & $masks[8]) & 0x3f] | $spfunction7[$right2 & 0x3f]); } $temp = $left; $left = $right; $right = $temp; //unreverse left and right } //for either 1 or 3 iterations //move then each one bit to the right $left = (($left >> 1 & $masks[1]) | ($left << 31)); $right = (($right >> 1 & $masks[1]) | ($right << 31)); //now perform IP-1, which is IP in the opposite direction $temp = (($left >> 1 & $masks[1]) ^ $right) & 0x55555555; $right ^= $temp; $left ^= ($temp << 1); $temp = (($right >> 8 & $masks[8]) ^ $left) & 0x00ff00ff; $left ^= $temp; $right ^= ($temp << 8); $temp = (($right >> 2 & $masks[2]) ^ $left) & 0x33333333; $left ^= $temp; $right ^= ($temp << 2); $temp = (($left >> 16 & $masks[16]) ^ $right) & 0x0000ffff; $right ^= $temp; $left ^= ($temp << 16); $temp = (($left >> 4 & $masks[4]) ^ $right) & 0x0f0f0f0f; $right ^= $temp; $left ^= ($temp << 4); //for Cipher Block Chaining mode, xor the message with the previous result if ($mode == 1) {if ($encrypt) {$cbcleft = $left; $cbcright = $right;} else {$left ^= $cbcleft2; $right ^= $cbcright2;}} $tempresult .= (chr($left>>24 & $masks[24]) . chr(($left>>16 & $masks[16]) & 0xff) . chr(($left>>8 & $masks[8]) & 0xff) . chr($left & 0xff) . chr($right>>24 & $masks[24]) . chr(($right>>16 & $masks[16]) & 0xff) . chr(($right>>8 & $masks[8]) & 0xff) . chr($right & 0xff)); $chunk += 8; if ($chunk == 512) {$result .= $tempresult; $tempresult = ""; $chunk = 0;} } //for every 8 characters, or 64 bits in the message //return the result as an array return ($result . $tempresult); } //end of des /** * createKeys * this takes as input a 64 bit key (even though only 56 bits are used) * as an array of 2 integers, and returns 16 48 bit keys * @param string $key 加密key * @return string */ private static function _createKeys ($key) { //declaring this locally speeds things up a bit $pc2bytes0 = array (0,0x4,0x20000000,0x20000004,0x10000,0x10004,0x20010000,0x20010004,0x200,0x204,0x20000200,0x20000204,0x10200,0x10204,0x20010200,0x20010204); $pc2bytes1 = array (0,0x1,0x100000,0x100001,0x4000000,0x4000001,0x4100000,0x4100001,0x100,0x101,0x100100,0x100101,0x4000100,0x4000101,0x4100100,0x4100101); $pc2bytes2 = array (0,0x8,0x800,0x808,0x1000000,0x1000008,0x1000800,0x1000808,0,0x8,0x800,0x808,0x1000000,0x1000008,0x1000800,0x1000808); $pc2bytes3 = array (0,0x200000,0x8000000,0x8200000,0x2000,0x202000,0x8002000,0x8202000,0x20000,0x220000,0x8020000,0x8220000,0x22000,0x222000,0x8022000,0x8222000); $pc2bytes4 = array (0,0x40000,0x10,0x40010,0,0x40000,0x10,0x40010,0x1000,0x41000,0x1010,0x41010,0x1000,0x41000,0x1010,0x41010); $pc2bytes5 = array (0,0x400,0x20,0x420,0,0x400,0x20,0x420,0x2000000,0x2000400,0x2000020,0x2000420,0x2000000,0x2000400,0x2000020,0x2000420); $pc2bytes6 = array (0,0x10000000,0x80000,0x10080000,0x2,0x10000002,0x80002,0x10080002,0,0x10000000,0x80000,0x10080000,0x2,0x10000002,0x80002,0x10080002); $pc2bytes7 = array (0,0x10000,0x800,0x10800,0x20000000,0x20010000,0x20000800,0x20010800,0x20000,0x30000,0x20800,0x30800,0x20020000,0x20030000,0x20020800,0x20030800); $pc2bytes8 = array (0,0x40000,0,0x40000,0x2,0x40002,0x2,0x40002,0x2000000,0x2040000,0x2000000,0x2040000,0x2000002,0x2040002,0x2000002,0x2040002); $pc2bytes9 = array (0,0x10000000,0x8,0x10000008,0,0x10000000,0x8,0x10000008,0x400,0x10000400,0x408,0x10000408,0x400,0x10000400,0x408,0x10000408); $pc2bytes10 = array (0,0x20,0,0x20,0x100000,0x100020,0x100000,0x100020,0x2000,0x2020,0x2000,0x2020,0x102000,0x102020,0x102000,0x102020); $pc2bytes11 = array (0,0x1000000,0x200,0x1000200,0x200000,0x1200000,0x200200,0x1200200,0x4000000,0x5000000,0x4000200,0x5000200,0x4200000,0x5200000,0x4200200,0x5200200); $pc2bytes12 = array (0,0x1000,0x8000000,0x8001000,0x80000,0x81000,0x8080000,0x8081000,0x10,0x1010,0x8000010,0x8001010,0x80010,0x81010,0x8080010,0x8081010); $pc2bytes13 = array (0,0x4,0x100,0x104,0,0x4,0x100,0x104,0x1,0x5,0x101,0x105,0x1,0x5,0x101,0x105); $masks = array (4294967295,2147483647,1073741823,536870911,268435455,134217727,67108863,33554431,16777215,8388607,4194303,2097151,1048575,524287,262143,131071,65535,32767,16383,8191,4095,2047,1023,511,255,127,63,31,15,7,3,1,0); //how many iterations (1 for des, 3 for triple des) $iterations = ((strlen($key) >= 24) ? 3 : 1); //stores the return keys $keys = array (); // size = 32 * iterations but you don't specify this in php //now define the left shifts which need to be done $shifts = array (0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0); //other variables $m=0; $n=0; for ($j=0; $j<$iterations; $j++) { //either 1 or 3 iterations $left = (ord($key{$m++}) << 24) | (ord($key{$m++}) << 16) | (ord($key{$m++}) << 8) | ord($key{$m++}); $right = (ord($key{$m++}) << 24) | (ord($key{$m++}) << 16) | (ord($key{$m++}) << 8) | ord($key{$m++}); $temp = (($left >> 4 & $masks[4]) ^ $right) & 0x0f0f0f0f; $right ^= $temp; $left ^= ($temp << 4); $temp = (($right >> 16 & $masks[16]) ^ $left) & 0x0000ffff; $left ^= $temp; $right ^= ($temp << -16); $temp = (($left >> 2 & $masks[2]) ^ $right) & 0x33333333; $right ^= $temp; $left ^= ($temp << 2); $temp = (($right >> 16 & $masks[16]) ^ $left) & 0x0000ffff; $left ^= $temp; $right ^= ($temp << -16); $temp = (($left >> 1 & $masks[1]) ^ $right) & 0x55555555; $right ^= $temp; $left ^= ($temp << 1); $temp = (($right >> 8 & $masks[8]) ^ $left) & 0x00ff00ff; $left ^= $temp; $right ^= ($temp << 8); $temp = (($left >> 1 & $masks[1]) ^ $right) & 0x55555555; $right ^= $temp; $left ^= ($temp << 1); //the right side needs to be shifted and to get the last four bits of the left side $temp = ($left << 8) | (($right >> 20 & $masks[20]) & 0x000000f0); //left needs to be put upside down $left = ($right << 24) | (($right << 8) & 0xff0000) | (($right >> 8 & $masks[8]) & 0xff00) | (($right >> 24 & $masks[24]) & 0xf0); $right = $temp; //now go through and perform these shifts on the left and right keys for ($i=0; $i < count($shifts); $i++) { //shift the keys either one or two bits to the left if ($shifts[$i] > 0) { $left = (($left << 2) | ($left >> 26 & $masks[26])); $right = (($right << 2) | ($right >> 26 & $masks[26])); } else { $left = (($left << 1) | ($left >> 27 & $masks[27])); $right = (($right << 1) | ($right >> 27 & $masks[27])); } $left = $left & -0xf; $right = $right & -0xf; //now apply PC-2, in such a way that E is easier when encrypting or decrypting //this conversion will look like PC-2 except only the last 6 bits of each byte are used //rather than 48 consecutive bits and the order of lines will be according to //how the S selection functions will be applied: S2, S4, S6, S8, S1, S3, S5, S7 $lefttemp = $pc2bytes0[$left >> 28 & $masks[28]] | $pc2bytes1[($left >> 24 & $masks[24]) & 0xf] | $pc2bytes2[($left >> 20 & $masks[20]) & 0xf] | $pc2bytes3[($left >> 16 & $masks[16]) & 0xf] | $pc2bytes4[($left >> 12 & $masks[12]) & 0xf] | $pc2bytes5[($left >> 8 & $masks[8]) & 0xf] | $pc2bytes6[($left >> 4 & $masks[4]) & 0xf]; $righttemp = $pc2bytes7[$right >> 28 & $masks[28]] | $pc2bytes8[($right >> 24 & $masks[24]) & 0xf] | $pc2bytes9[($right >> 20 & $masks[20]) & 0xf] | $pc2bytes10[($right >> 16 & $masks[16]) & 0xf] | $pc2bytes11[($right >> 12 & $masks[12]) & 0xf] | $pc2bytes12[($right >> 8 & $masks[8]) & 0xf] | $pc2bytes13[($right >> 4 & $masks[4]) & 0xf]; $temp = (($righttemp >> 16 & $masks[16]) ^ $lefttemp) & 0x0000ffff; $keys[$n++] = $lefttemp ^ $temp; $keys[$n++] = $righttemp ^ ($temp << 16); } } //for each iterations //return the keys we've created return $keys; } //end of des_createKeys } ================================================ FILE: ThinkPHP/Library/Think/Crypt/Driver/Think.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Crypt\Driver; /** * Base64 加密实现类 */ class Think { /** * 加密字符串 * @param string $str 字符串 * @param string $key 加密key * @param integer $expire 有效期(秒) * @return string */ public static function encrypt($data,$key,$expire=0) { $expire = sprintf('%010d', $expire ? $expire + time():0); $key = md5($key); $data = base64_encode($expire.$data); $x = 0; $len = strlen($data); $l = strlen($key); $char = $str = ''; for ($i = 0; $i < $len; $i++) { if ($x == $l) $x = 0; $char .= substr($key, $x, 1); $x++; } for ($i = 0; $i < $len; $i++) { $str .= chr(ord(substr($data, $i, 1)) + (ord(substr($char, $i, 1)))%256); } return str_replace(array('+','/','='),array('-','_',''),base64_encode($str)); } /** * 解密字符串 * @param string $str 字符串 * @param string $key 加密key * @return string */ public static function decrypt($data,$key) { $key = md5($key); $data = str_replace(array('-','_'),array('+','/'),$data); $mod4 = strlen($data) % 4; if ($mod4) { $data .= substr('====', $mod4); } $data = base64_decode($data); $x = 0; $len = strlen($data); $l = strlen($key); $char = $str = ''; for ($i = 0; $i < $len; $i++) { if ($x == $l) $x = 0; $char .= substr($key, $x, 1); $x++; } for ($i = 0; $i < $len; $i++) { if (ord(substr($data, $i, 1)) 0 && $expire < time()) { return ''; } $data = substr($data,10); return $data; } } ================================================ FILE: ThinkPHP/Library/Think/Crypt/Driver/Xxtea.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Crypt\Driver; /** * Xxtea 加密实现类 */ class Xxtea { /** * 加密字符串 * @param string $str 字符串 * @param string $key 加密key * @param integer $expire 有效期(秒) * @return string */ public static function encrypt($str, $key,$expire=0) { $expire = sprintf('%010d', $expire ? $expire + time():0); $str = $expire.$str; $v = self::str2long($str, true); $k = self::str2long($key, false); $n = count($v) - 1; $z = $v[$n]; $y = $v[0]; $delta = 0x9E3779B9; $q = floor(6 + 52 / ($n + 1)); $sum = 0; while (0 < $q--) { $sum = self::int32($sum + $delta); $e = $sum >> 2 & 3; for ($p = 0; $p < $n; $p++) { $y = $v[$p + 1]; $mx = self::int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ self::int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z)); $z = $v[$p] = self::int32($v[$p] + $mx); } $y = $v[0]; $mx = self::int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ self::int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z)); $z = $v[$n] = self::int32($v[$n] + $mx); } return self::long2str($v, false); } /** * 解密字符串 * @param string $str 字符串 * @param string $key 加密key * @return string */ public static function decrypt($str, $key) { $v = self::str2long($str, false); $k = self::str2long($key, false); $n = count($v) - 1; $z = $v[$n]; $y = $v[0]; $delta = 0x9E3779B9; $q = floor(6 + 52 / ($n + 1)); $sum = self::int32($q * $delta); while ($sum != 0) { $e = $sum >> 2 & 3; for ($p = $n; $p > 0; $p--) { $z = $v[$p - 1]; $mx = self::int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ self::int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z)); $y = $v[$p] = self::int32($v[$p] - $mx); } $z = $v[$n]; $mx = self::int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ self::int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z)); $y = $v[0] = self::int32($v[0] - $mx); $sum = self::int32($sum - $delta); } $data = self::long2str($v, true); $expire = substr($data,0,10); if($expire > 0 && $expire < time()) { return ''; } $data = substr($data,10); return $data; } private static function long2str($v, $w) { $len = count($v); $s = array(); for ($i = 0; $i < $len; $i++) { $s[$i] = pack("V", $v[$i]); } if ($w) { return substr(join('', $s), 0, $v[$len - 1]); }else{ return join('', $s); } } private static function str2long($s, $w) { $v = unpack("V*", $s. str_repeat("\0", (4 - strlen($s) % 4) & 3)); $v = array_values($v); if ($w) { $v[count($v)] = strlen($s); } return $v; } private static function int32($n) { while ($n >= 2147483648) $n -= 4294967296; while ($n <= -2147483649) $n += 4294967296; return (int)$n; } } ================================================ FILE: ThinkPHP/Library/Think/Crypt.class.php ================================================ // +---------------------------------------------------------------------- namespace Think; /** * 加密解密类 */ class Crypt { private static $handler = ''; public static function init($type=''){ $type = $type?:C('DATA_CRYPT_TYPE'); $class = strpos($type,'\\')? $type: 'Think\\Crypt\\Driver\\'. ucwords(strtolower($type)); self::$handler = $class; } /** * 加密字符串 * @param string $str 字符串 * @param string $key 加密key * @param integer $expire 有效期(秒) 0 为永久有效 * @return string */ public static function encrypt($data,$key,$expire=0){ if(empty(self::$handler)){ self::init(); } $class = self::$handler; return $class::encrypt($data,$key,$expire); } /** * 解密字符串 * @param string $str 字符串 * @param string $key 加密key * @return string */ public static function decrypt($data,$key){ if(empty(self::$handler)){ self::init(); } $class = self::$handler; return $class::decrypt($data,$key); } } ================================================ FILE: ThinkPHP/Library/Think/Db/Driver/Firebird.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Db\Driver; use Think\Db\Driver; /** * Firebird数据库驱动 */ class Firebird extends Driver{ protected $selectSql = 'SELECT %LIMIT% %DISTINCT% %FIELD% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%'; /** * 解析pdo连接的dsn信息 * @access public * @param array $config 连接信息 * @return string */ protected function parseDsn($config){ $dsn = 'firebird:dbname='.$config['hostname'].'/'.($config['hostport']?:3050).':'.$config['database']; return $dsn; } /** * 执行语句 * @access public * @param string $str sql指令 * @param boolean $fetchSql 不执行只是获取SQL * @return mixed */ public function execute($str,$fetchSql=false) { $this->initConnect(true); if ( !$this->_linkID ) return false; $this->queryStr = $str; if(!empty($this->bind)){ $that = $this; $this->queryStr = strtr($this->queryStr,array_map(function($val) use($that){ return '\''.$that->escapeString($val).'\''; },$this->bind)); } if($fetchSql){ return $this->queryStr; } //释放前次的查询结果 if ( !empty($this->PDOStatement) ) $this->free(); $this->executeTimes++; N('db_write',1); // 兼容代码 // 记录开始执行时间 $this->debug(true); $this->PDOStatement = $this->_linkID->prepare($str); if(false === $this->PDOStatement) { E($this->error()); } foreach ($this->bind as $key => $val) { if(is_array($val)){ $this->PDOStatement->bindValue($key, $val[0], $val[1]); }else{ $this->PDOStatement->bindValue($key, $val); } } $this->bind = array(); $result = $this->PDOStatement->execute(); $this->debug(false); if ( false === $result) { $this->error(); return false; } else { $this->numRows = $this->PDOStatement->rowCount(); return $this->numRows; } } /** * 取得数据表的字段信息 * @access public */ public function getFields($tableName) { $this->initConnect(true); list($tableName) = explode(' ', $tableName); $sql='SELECT RF.RDB$FIELD_NAME AS FIELD,RF.RDB$DEFAULT_VALUE AS DEFAULT1,RF.RDB$NULL_FLAG AS NULL1,TRIM(T.RDB$TYPE_NAME) || \'(\' || F.RDB$FIELD_LENGTH || \')\' as TYPE FROM RDB$RELATION_FIELDS RF LEFT JOIN RDB$FIELDS F ON (F.RDB$FIELD_NAME = RF.RDB$FIELD_SOURCE) LEFT JOIN RDB$TYPES T ON (T.RDB$TYPE = F.RDB$FIELD_TYPE) WHERE RDB$RELATION_NAME=UPPER(\''.$tableName.'\') AND T.RDB$FIELD_NAME = \'RDB$FIELD_TYPE\' ORDER By RDB$FIELD_POSITION'; $result = $this->query($sql); $info = array(); if($result){ foreach($result as $key => $val){ $info[trim($val['field'])] = array( 'name' => trim($val['field']), 'type' => $val['type'], 'notnull' => (bool) ($val['null1'] ==1), // 1表示不为Null 'default' => $val['default1'], 'primary' => false, 'autoinc' => false, ); } } //获取主键 $sql='select b.rdb$field_name as field_name from rdb$relation_constraints a join rdb$index_segments b on a.rdb$index_name=b.rdb$index_name where a.rdb$constraint_type=\'PRIMARY KEY\' and a.rdb$relation_name=UPPER(\''.$tableName.'\')'; $rs_temp = $this->query($sql); foreach($rs_temp as $row) { $info[trim($row['field_name'])]['primary']= true; } return $info; } /** * 取得数据库的表信息 * @access public */ public function getTables($dbName='') { $sql='SELECT DISTINCT RDB$RELATION_NAME FROM RDB$RELATION_FIELDS WHERE RDB$SYSTEM_FLAG=0'; $result = $this->query($sql); $info = array(); foreach ($result as $key => $val) { $info[$key] = trim(current($val)); } return $info; } /** * SQL指令安全过滤 * @access public * @param string $str SQL指令 * @return string */ public function escapeString($str) { return str_replace("'", "''", $str); } /** * limit * @access public * @param $limit limit表达式 * @return string */ public function parseLimit($limit) { $limitStr = ''; if(!empty($limit)) { $limit = explode(',',$limit); if(count($limit)>1) { $limitStr = ' FIRST '.$limit[1].' SKIP '.$limit[0].' '; }else{ $limitStr = ' FIRST '.$limit[0].' '; } } return $limitStr; } } ================================================ FILE: ThinkPHP/Library/Think/Db/Driver/Mongo.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Db\Driver; use Think\Db\Driver; /** * Mongo数据库驱动 */ class Mongo extends Driver { protected $_mongo = null; // MongoDb Object protected $_collection = null; // MongoCollection Object protected $_dbName = ''; // dbName protected $_collectionName = ''; // collectionName protected $_cursor = null; // MongoCursor Object protected $comparison = array('neq'=>'ne','ne'=>'ne','gt'=>'gt','egt'=>'gte','gte'=>'gte','lt'=>'lt','elt'=>'lte','lte'=>'lte','in'=>'in','not in'=>'nin','nin'=>'nin'); /** * 架构函数 读取数据库配置信息 * @access public * @param array $config 数据库配置数组 */ public function __construct($config=''){ if ( !class_exists('mongoClient') ) { E(L('_NOT_SUPPORT_').':Mongo'); } if(!empty($config)) { $this->config = array_merge($this->config,$config); if(empty($this->config['params'])){ $this->config['params'] = array(); } } } /** * 连接数据库方法 * @access public */ public function connect($config='',$linkNum=0) { if ( !isset($this->linkID[$linkNum]) ) { if(empty($config)) $config = $this->config; $host = 'mongodb://'.($config['username']?"{$config['username']}":'').($config['password']?":{$config['password']}@":'').$config['hostname'].($config['hostport']?":{$config['hostport']}":'').'/'.($config['database']?"{$config['database']}":''); try{ $this->linkID[$linkNum] = new \mongoClient( $host,$this->config['params']); }catch (\MongoConnectionException $e){ E($e->getmessage()); } } return $this->linkID[$linkNum]; } /** * 切换当前操作的Db和Collection * @access public * @param string $collection collection * @param string $db db * @param boolean $master 是否主服务器 * @return void */ public function switchCollection($collection,$db='',$master=true){ // 当前没有连接 则首先进行数据库连接 if ( !$this->_linkID ) $this->initConnect($master); try{ if(!empty($db)) { // 传人Db则切换数据库 // 当前MongoDb对象 $this->_dbName = $db; $this->_mongo = $this->_linkID->selectDb($db); } // 当前MongoCollection对象 if($this->config['debug']) { $this->queryStr = $this->_dbName.'.getCollection('.$collection.')'; } if($this->_collectionName != $collection) { $this->queryTimes++; N('db_query',1); // 兼容代码 $this->debug(true); $this->_collection = $this->_mongo->selectCollection($collection); $this->debug(false); $this->_collectionName = $collection; // 记录当前Collection名称 } }catch (MongoException $e){ E($e->getMessage()); } } /** * 释放查询结果 * @access public */ public function free() { $this->_cursor = null; } /** * 执行命令 * @access public * @param array $command 指令 * @return array */ public function command($command=array(), $options=array()) { $cache = isset($options['cache'])?$options['cache']:false; if($cache) { // 查询缓存检测 $key = is_string($cache['key'])?$cache['key']:md5(serialize($command)); $value = S($key,'','',$cache['type']); if(false !== $value) { return $value; } } N('db_write',1); // 兼容代码 $this->executeTimes++; try{ if($this->config['debug']) { $this->queryStr = $this->_dbName.'.'.$this->_collectionName.'.runCommand('; $this->queryStr .= json_encode($command); $this->queryStr .= ')'; } $this->debug(true); $result = $this->_mongo->command($command); $this->debug(false); if($cache && $result['ok']) { // 查询缓存写入 S($key,$result,$cache['expire'],$cache['type']); } return $result; } catch (\MongoCursorException $e) { E($e->getMessage()); } } /** * 执行语句 * @access public * @param string $code sql指令 * @param array $args 参数 * @return mixed */ public function execute($code,$args=array()) { $this->executeTimes++; N('db_write',1); // 兼容代码 $this->debug(true); $this->queryStr = 'execute:'.$code; $result = $this->_mongo->execute($code,$args); $this->debug(false); if($result['ok']) { return $result['retval']; }else{ E($result['errmsg']); } } /** * 关闭数据库 * @access public */ public function close() { if($this->_linkID) { $this->_linkID->close(); $this->_linkID = null; $this->_mongo = null; $this->_collection = null; $this->_cursor = null; } } /** * 数据库错误信息 * @access public * @return string */ public function error() { $this->error = $this->_mongo->lastError(); trace($this->error,'','ERR'); return $this->error; } /** * 插入记录 * @access public * @param mixed $data 数据 * @param array $options 参数表达式 * @param boolean $replace 是否replace * @return false | integer */ public function insert($data,$options=array(),$replace=false) { if(isset($options['table'])) { $this->switchCollection($options['table']); } $this->model = $options['model']; $this->executeTimes++; N('db_write',1); // 兼容代码 if($this->config['debug']) { $this->queryStr = $this->_dbName.'.'.$this->_collectionName.'.insert('; $this->queryStr .= $data?json_encode($data):'{}'; $this->queryStr .= ')'; } try{ $this->debug(true); $result = $replace? $this->_collection->save($data): $this->_collection->insert($data); $this->debug(false); if($result) { $_id = $data['_id']; if(is_object($_id)) { $_id = $_id->__toString(); } $this->lastInsID = $_id; } return $result; } catch (\MongoCursorException $e) { E($e->getMessage()); } } /** * 插入多条记录 * @access public * @param array $dataList 数据 * @param array $options 参数表达式 * @return bool */ public function insertAll($dataList,$options=array()) { if(isset($options['table'])) { $this->switchCollection($options['table']); } $this->model = $options['model']; $this->executeTimes++; N('db_write',1); // 兼容代码 try{ $this->debug(true); $result = $this->_collection->batchInsert($dataList); $this->debug(false); return $result; } catch (\MongoCursorException $e) { E($e->getMessage()); } } /** * 生成下一条记录ID 用于自增非MongoId主键 * @access public * @param string $pk 主键名 * @return integer */ public function getMongoNextId($pk) { if($this->config['debug']) { $this->queryStr = $this->_dbName.'.'.$this->_collectionName.'.find({},{'.$pk.':1}).sort({'.$pk.':-1}).limit(1)'; } try{ $this->debug(true); $result = $this->_collection->find(array(),array($pk=>1))->sort(array($pk=>-1))->limit(1); $this->debug(false); } catch (\MongoCursorException $e) { E($e->getMessage()); } $data = $result->getNext(); return isset($data[$pk])?$data[$pk]+1:1; } /** * 更新记录 * @access public * @param mixed $data 数据 * @param array $options 表达式 * @return bool */ public function update($data,$options) { if(isset($options['table'])) { $this->switchCollection($options['table']); } $this->executeTimes++; N('db_write',1); // 兼容代码 $this->model = $options['model']; $query = $this->parseWhere(isset($options['where'])?$options['where']:array()); $set = $this->parseSet($data); if($this->config['debug']) { $this->queryStr = $this->_dbName.'.'.$this->_collectionName.'.update('; $this->queryStr .= $query?json_encode($query):'{}'; $this->queryStr .= ','.json_encode($set).')'; } try{ $this->debug(true); if(isset($options['limit']) && $options['limit'] == 1) { $multiple = array("multiple" => false); }else{ $multiple = array("multiple" => true); } $result = $this->_collection->update($query,$set,$multiple); $this->debug(false); return $result; } catch (\MongoCursorException $e) { E($e->getMessage()); } } /** * 删除记录 * @access public * @param array $options 表达式 * @return false | integer */ public function delete($options=array()) { if(isset($options['table'])) { $this->switchCollection($options['table']); } $query = $this->parseWhere(isset($options['where'])?$options['where']:array()); $this->model = $options['model']; $this->executeTimes++; N('db_write',1); // 兼容代码 if($this->config['debug']) { $this->queryStr = $this->_dbName.'.'.$this->_collectionName.'.remove('.json_encode($query).')'; } try{ $this->debug(true); $result = $this->_collection->remove($query); $this->debug(false); return $result; } catch (\MongoCursorException $e) { E($e->getMessage()); } } /** * 清空记录 * @access public * @param array $options 表达式 * @return false | integer */ public function clear($options=array()){ if(isset($options['table'])) { $this->switchCollection($options['table']); } $this->model = $options['model']; $this->executeTimes++; N('db_write',1); // 兼容代码 if($this->config['debug']) { $this->queryStr = $this->_dbName.'.'.$this->_collectionName.'.remove({})'; } try{ $this->debug(true); $result = $this->_collection->drop(); $this->debug(false); return $result; } catch (\MongoCursorException $e) { E($e->getMessage()); } } /** * 查找记录 * @access public * @param array $options 表达式 * @return iterator */ public function select($options=array()) { if(isset($options['table'])) { $this->switchCollection($options['table'],'',false); } $this->model = $options['model']; $this->queryTimes++; N('db_query',1); // 兼容代码 $query = $this->parseWhere(isset($options['where'])?$options['where']:array()); $field = $this->parseField(isset($options['field'])?$options['field']:array()); try{ if($this->config['debug']) { $this->queryStr = $this->_dbName.'.'.$this->_collectionName.'.find('; $this->queryStr .= $query? json_encode($query):'{}'; if(is_array($field) && count($field)) { foreach ($field as $f=>$v) $_field_array[$f] = $v ? 1 : 0; $this->queryStr .= $field? ', '.json_encode($_field_array):', {}'; } $this->queryStr .= ')'; } $this->debug(true); $_cursor = $this->_collection->find($query,$field); if(!empty($options['order'])) { $order = $this->parseOrder($options['order']); if($this->config['debug']) { $this->queryStr .= '.sort('.json_encode($order).')'; } $_cursor = $_cursor->sort($order); } if(isset($options['page'])) { // 根据页数计算limit list($page,$length) = $options['page']; $page = $page>0 ? $page : 1; $length = $length>0 ? $length : (is_numeric($options['limit'])?$options['limit']:20); $offset = $length*((int)$page-1); $options['limit'] = $offset.','.$length; } if(isset($options['limit'])) { list($offset,$length) = $this->parseLimit($options['limit']); if(!empty($offset)) { if($this->config['debug']) { $this->queryStr .= '.skip('.intval($offset).')'; } $_cursor = $_cursor->skip(intval($offset)); } if($this->config['debug']) { $this->queryStr .= '.limit('.intval($length).')'; } $_cursor = $_cursor->limit(intval($length)); } $this->debug(false); $this->_cursor = $_cursor; $resultSet = iterator_to_array($_cursor); return $resultSet; } catch (\MongoCursorException $e) { E($e->getMessage()); } } /** * 查找某个记录 * @access public * @param array $options 表达式 * @return array */ public function find($options=array()){ $options['limit'] = 1; $find = $this->select($options); return array_shift($find); } /** * 统计记录数 * @access public * @param array $options 表达式 * @return iterator */ public function count($options=array()){ if(isset($options['table'])) { $this->switchCollection($options['table'],'',false); } $this->model = $options['model']; $this->queryTimes++; N('db_query',1); // 兼容代码 $query = $this->parseWhere(isset($options['where'])?$options['where']:array()); if($this->config['debug']) { $this->queryStr = $this->_dbName.'.'.$this->_collectionName; $this->queryStr .= $query?'.find('.json_encode($query).')':''; $this->queryStr .= '.count()'; } try{ $this->debug(true); $count = $this->_collection->count($query); $this->debug(false); return $count; } catch (\MongoCursorException $e) { E($e->getMessage()); } } public function group($keys,$initial,$reduce,$options=array()){ if(isset($options['table']) && $this->_collectionName != $options['table']) { $this->switchCollection($options['table'],'',false); } $cache = isset($options['cache'])?$options['cache']:false; if($cache) { $key = is_string($cache['key'])?$cache['key']:md5(serialize($options)); $value = S($key,'','',$cache['type']); if(false !== $value) { return $value; } } $this->model = $options['model']; $this->queryTimes++; N('db_query',1); // 兼容代码 $query = $this->parseWhere(isset($options['where'])?$options['where']:array()); if($this->config['debug']) { $this->queryStr = $this->_dbName.'.'.$this->_collectionName.'.group({key:'.json_encode($keys).',cond:'. json_encode($options['condition']) . ',reduce:' . json_encode($reduce).',initial:'. json_encode($initial).'})'; } try{ $this->debug(true); $option = array('condition'=>$options['condition'], 'finalize'=>$options['finalize'], 'maxTimeMS'=>$options['maxTimeMS']); $group = $this->_collection->group($keys,$initial,$reduce,$options); $this->debug(false); if($cache && $group['ok']) S($key,$group,$cache['expire'],$cache['type']); return $group; } catch (\MongoCursorException $e) { E($e->getMessage()); } } /** * 取得数据表的字段信息 * @access public * @return array */ public function getFields($collection=''){ if(!empty($collection) && $collection != $this->_collectionName) { $this->switchCollection($collection,'',false); } $this->queryTimes++; N('db_query',1); // 兼容代码 if($this->config['debug']) { $this->queryStr = $this->_dbName.'.'.$this->_collectionName.'.findOne()'; } try{ $this->debug(true); $result = $this->_collection->findOne(); $this->debug(false); } catch (\MongoCursorException $e) { E($e->getMessage()); } if($result) { // 存在数据则分析字段 $info = array(); foreach ($result as $key=>$val){ $info[$key] = array( 'name' => $key, 'type' => getType($val), ); } return $info; } // 暂时没有数据 返回false return false; } /** * 取得当前数据库的collection信息 * @access public */ public function getTables(){ if($this->config['debug']) { $this->queryStr = $this->_dbName.'.getCollenctionNames()'; } $this->queryTimes++; N('db_query',1); // 兼容代码 $this->debug(true); $list = $this->_mongo->listCollections(); $this->debug(false); $info = array(); foreach ($list as $collection){ $info[] = $collection->getName(); } return $info; } /** * 取得当前数据库的对象 * @access public * @return object mongoClient */ public function getDB(){ return $this->_mongo; } /** * 取得当前集合的对象 * @access public * @return object MongoCollection */ public function getCollection(){ return $this->_collection; } /** * set分析 * @access protected * @param array $data * @return string */ protected function parseSet($data) { $result = array(); foreach ($data as $key=>$val){ if(is_array($val)) { switch($val[0]) { case 'inc': $result['$inc'][$key] = (int)$val[1]; break; case 'set': case 'unset': case 'push': case 'pushall': case 'addtoset': case 'pop': case 'pull': case 'pullall': $result['$'.$val[0]][$key] = $val[1]; break; default: $result['$set'][$key] = $val; } }else{ $result['$set'][$key] = $val; } } return $result; } /** * order分析 * @access protected * @param mixed $order * @return array */ protected function parseOrder($order) { if(is_string($order)) { $array = explode(',',$order); $order = array(); foreach ($array as $key=>$val){ $arr = explode(' ',trim($val)); if(isset($arr[1])) { $arr[1] = $arr[1]=='asc'?1:-1; }else{ $arr[1] = 1; } $order[$arr[0]] = $arr[1]; } } return $order; } /** * limit分析 * @access protected * @param mixed $limit * @return array */ protected function parseLimit($limit) { if(strpos($limit,',')) { $array = explode(',',$limit); }else{ $array = array(0,$limit); } return $array; } /** * field分析 * @access protected * @param mixed $fields * @return array */ public function parseField($fields){ if(empty($fields)) { $fields = array(); } if(is_string($fields)) { $_fields = explode(',',$fields); $fields = array(); foreach ($_fields as $f) $fields[$f] = true; }elseif(is_array($fields)) { $_fields = $fields; $fields = array(); foreach ($_fields as $f=>$v) { if(is_numeric($f)) $fields[$v] = true; else $fields[$f] = $v ? true : false; } } return $fields; } /** * where分析 * @access protected * @param mixed $where * @return array */ public function parseWhere($where){ $query = array(); $return = array(); $_logic = '$and'; if(isset($where['_logic'])){ $where['_logic'] = strtolower($where['_logic']); $_logic = in_array($where['_logic'], array('or','xor','nor', 'and'))?'$'.$where['_logic']:$_logic; unset($where['_logic']); } foreach ($where as $key=>$val){ if('_id' != $key && 0===strpos($key,'_')) { // 解析特殊条件表达式 $parse = $this->parseThinkWhere($key,$val); $query = array_merge($query,$parse); }else{ // 查询字段的安全过滤 if(!preg_match('/^[A-Z_\|\&\-.a-z0-9]+$/',trim($key))){ E(L('_ERROR_QUERY_').':'.$key); } $key = trim($key); if(strpos($key,'|')) { $array = explode('|',$key); $str = array(); foreach ($array as $k){ $str[] = $this->parseWhereItem($k,$val); } $query['$or'] = $str; }elseif(strpos($key,'&')){ $array = explode('&',$key); $str = array(); foreach ($array as $k){ $str[] = $this->parseWhereItem($k,$val); } $query = array_merge($query,$str); }else{ $str = $this->parseWhereItem($key,$val); $query = array_merge($query,$str); } } } if($_logic == '$and') return $query; foreach($query as $key=>$val) $return[$_logic][] = array($key=>$val); return $return; } /** * 特殊条件分析 * @access protected * @param string $key * @param mixed $val * @return string */ protected function parseThinkWhere($key,$val) { $query = array(); $_logic = array('or','xor','nor', 'and'); switch($key) { case '_query': // 字符串模式查询条件 parse_str($val,$query); if(isset($query['_logic']) && strtolower($query['_logic']) == 'or' ) { unset($query['_logic']); $query['$or'] = $query; } break; case '_complex': // 子查询模式查询条件 $__logic = strtolower($val['_logic']); if(isset($val['_logic']) && in_array($__logic, $_logic) ) { unset($val['_logic']); $query['$'.$__logic] = $val; } break; case '_string':// MongoCode查询 $query['$where'] = new \MongoCode($val); break; } //兼容 MongoClient OR条件查询方法 if(isset($query['$or']) && !is_array(current($query['$or']))) { $val = array(); foreach ($query['$or'] as $k=>$v) $val[] = array($k=>$v); $query['$or'] = $val; } return $query; } /** * where子单元分析 * @access protected * @param string $key * @param mixed $val * @return array */ protected function parseWhereItem($key,$val) { $query = array(); if(is_array($val)) { if(is_string($val[0])) { $con = strtolower($val[0]); if(in_array($con,array('neq','ne','gt','egt','gte','lt','lte','elt'))) { // 比较运算 $k = '$'.$this->comparison[$con]; $query[$key] = array($k=>$val[1]); }elseif('like'== $con){ // 模糊查询 采用正则方式 $query[$key] = new \MongoRegex("/".$val[1]."/"); }elseif('mod'==$con){ // mod 查询 $query[$key] = array('$mod'=>$val[1]); }elseif('regex'==$con){ // 正则查询 $query[$key] = new \MongoRegex($val[1]); }elseif(in_array($con,array('in','nin','not in'))){ // IN NIN 运算 $data = is_string($val[1])? explode(',',$val[1]):$val[1]; $k = '$'.$this->comparison[$con]; $query[$key] = array($k=>$data); }elseif('all'==$con){ // 满足所有指定条件 $data = is_string($val[1])? explode(',',$val[1]):$val[1]; $query[$key] = array('$all'=>$data); }elseif('between'==$con){ // BETWEEN运算 $data = is_string($val[1])? explode(',',$val[1]):$val[1]; $query[$key] = array('$gte'=>$data[0],'$lte'=>$data[1]); }elseif('not between'==$con){ $data = is_string($val[1])? explode(',',$val[1]):$val[1]; $query[$key] = array('$lt'=>$data[0],'$gt'=>$data[1]); }elseif('exp'==$con){ // 表达式查询 $query['$where'] = new \MongoCode($val[1]); }elseif('exists'==$con){ // 字段是否存在 $query[$key] = array('$exists'=>(bool)$val[1]); }elseif('size'==$con){ // 限制属性大小 $query[$key] = array('$size'=>intval($val[1])); }elseif('type'==$con){ // 限制字段类型 1 浮点型 2 字符型 3 对象或者MongoDBRef 5 MongoBinData 7 MongoId 8 布尔型 9 MongoDate 10 NULL 15 MongoCode 16 32位整型 17 MongoTimestamp 18 MongoInt64 如果是数组的话判断元素的类型 $query[$key] = array('$type'=>intval($val[1])); }else{ $query[$key] = $val; } return $query; } } $query[$key] = $val; return $query; } } ================================================ FILE: ThinkPHP/Library/Think/Db/Driver/Mysql.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Db\Driver; use Think\Db\Driver; /** * mysql数据库驱动 */ class Mysql extends Driver{ /** * 解析pdo连接的dsn信息 * @access public * @param array $config 连接信息 * @return string */ protected function parseDsn($config){ $dsn = 'mysql:dbname='.$config['database'].';host='.$config['hostname']; if(!empty($config['hostport'])) { $dsn .= ';port='.$config['hostport']; }elseif(!empty($config['socket'])){ $dsn .= ';unix_socket='.$config['socket']; } if(!empty($config['charset'])){ //为兼容各版本PHP,用两种方式设置编码 $this->options[\PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES '.$config['charset']; $dsn .= ';charset='.$config['charset']; } return $dsn; } /** * 取得数据表的字段信息 * @access public */ public function getFields($tableName) { $this->initConnect(true); list($tableName) = explode(' ', $tableName); if(strpos($tableName,'.')){ list($dbName,$tableName) = explode('.',$tableName); $sql = 'SHOW COLUMNS FROM `'.$dbName.'`.`'.$tableName.'`'; }else{ $sql = 'SHOW COLUMNS FROM `'.$tableName.'`'; } $result = $this->query($sql); $info = array(); if($result) { foreach ($result as $key => $val) { if(\PDO::CASE_LOWER != $this->_linkID->getAttribute(\PDO::ATTR_CASE)){ $val = array_change_key_case ( $val , CASE_LOWER ); } $info[$val['field']] = array( 'name' => $val['field'], 'type' => $val['type'], 'notnull' => (bool) ($val['null'] === ''), // not null is empty, null is yes 'default' => $val['default'], 'primary' => (strtolower($val['key']) == 'pri'), 'autoinc' => (strtolower($val['extra']) == 'auto_increment'), ); } } return $info; } /** * 取得数据库的表信息 * @access public */ public function getTables($dbName='') { $sql = !empty($dbName)?'SHOW TABLES FROM '.$dbName:'SHOW TABLES '; $result = $this->query($sql); $info = array(); foreach ($result as $key => $val) { $info[$key] = current($val); } return $info; } /** * 字段和表名处理 * @access protected * @param string $key * @return string */ protected function parseKey(&$key) { $key = trim($key); if(!is_numeric($key) && !preg_match('/[,\'\"\*\(\)`.\s]/',$key)) { $key = '`'.$key.'`'; } return $key; } /** * 批量插入记录 * @access public * @param mixed $dataSet 数据集 * @param array $options 参数表达式 * @param boolean $replace 是否replace * @return false | integer */ public function insertAll($dataSet,$options=array(),$replace=false) { $values = array(); $this->model = $options['model']; if(!is_array($dataSet[0])) return false; $this->parseBind(!empty($options['bind'])?$options['bind']:array()); $fields = array_map(array($this,'parseKey'),array_keys($dataSet[0])); foreach ($dataSet as $data){ $value = array(); foreach ($data as $key=>$val){ if(is_array($val) && 'exp' == $val[0]){ $value[] = $val[1]; }elseif(is_null($val)){ $value[] = 'NULL'; }elseif(is_scalar($val)){ if(0===strpos($val,':') && in_array($val,array_keys($this->bind))){ $value[] = $this->parseValue($val); }else{ $name = count($this->bind); $value[] = ':'.$name; $this->bindParam($name,$val); } } } $values[] = '('.implode(',', $value).')'; } // 兼容数字传入方式 $replace= (is_numeric($replace) && $replace>0)?true:$replace; $sql = (true===$replace?'REPLACE':'INSERT').' INTO '.$this->parseTable($options['table']).' ('.implode(',', $fields).') VALUES '.implode(',',$values).$this->parseDuplicate($replace); $sql .= $this->parseComment(!empty($options['comment'])?$options['comment']:''); return $this->execute($sql,!empty($options['fetch_sql']) ? true : false); } /** * ON DUPLICATE KEY UPDATE 分析 * @access protected * @param mixed $duplicate * @return string */ protected function parseDuplicate($duplicate){ // 布尔值或空则返回空字符串 if(is_bool($duplicate) || empty($duplicate)) return ''; if(is_string($duplicate)){ // field1,field2 转数组 $duplicate = explode(',', $duplicate); }elseif(is_object($duplicate)){ // 对象转数组 $duplicate = get_class_vars($duplicate); } $updates = array(); foreach((array) $duplicate as $key=>$val){ if(is_numeric($key)){ // array('field1', 'field2', 'field3') 解析为 ON DUPLICATE KEY UPDATE field1=VALUES(field1), field2=VALUES(field2), field3=VALUES(field3) $updates[] = $this->parseKey($val)."=VALUES(".$this->parseKey($val).")"; }else{ if(is_scalar($val)) // 兼容标量传值方式 $val = array('value', $val); if(!isset($val[1])) continue; switch($val[0]){ case 'exp': // 表达式 $updates[] = $this->parseKey($key)."=($val[1])"; break; case 'value': // 值 default: $name = count($this->bind); $updates[] = $this->parseKey($key)."=:".$name; $this->bindParam($name, $val[1]); break; } } } if(empty($updates)) return ''; return " ON DUPLICATE KEY UPDATE ".join(', ', $updates); } /** * 执行存储过程查询 返回多个数据集 * @access public * @param string $str sql指令 * @param boolean $fetchSql 不执行只是获取SQL * @return mixed */ public function procedure($str,$fetchSql=false) { $this->initConnect(false); $this->_linkID->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING); if ( !$this->_linkID ) return false; $this->queryStr = $str; if($fetchSql){ return $this->queryStr; } //释放前次的查询结果 if ( !empty($this->PDOStatement) ) $this->free(); $this->queryTimes++; N('db_query',1); // 兼容代码 // 调试开始 $this->debug(true); $this->PDOStatement = $this->_linkID->prepare($str); if(false === $this->PDOStatement){ $this->error(); return false; } try{ $result = $this->PDOStatement->execute(); // 调试结束 $this->debug(false); do { $result = $this->PDOStatement->fetchAll(\PDO::FETCH_ASSOC); if ($result) { $resultArr[] = $result; } } while ($this->PDOStatement->nextRowset()); $this->_linkID->setAttribute(\PDO::ATTR_ERRMODE, $this->options[\PDO::ATTR_ERRMODE]); return $resultArr; }catch (\PDOException $e) { $this->error(); $this->_linkID->setAttribute(\PDO::ATTR_ERRMODE, $this->options[\PDO::ATTR_ERRMODE]); return false; } } } ================================================ FILE: ThinkPHP/Library/Think/Db/Driver/Oracle.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Db\Driver; use Think\Db\Driver; /** * Oracle数据库驱动 */ class Oracle extends Driver{ private $table = ''; protected $selectSql = 'SELECT * FROM (SELECT thinkphp.*, rownum AS numrow FROM (SELECT %DISTINCT% %FIELD% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%) thinkphp ) %LIMIT%%COMMENT%'; /** * 解析pdo连接的dsn信息 * @access public * @param array $config 连接信息 * @return string */ protected function parseDsn($config){ $dsn = 'oci:dbname=//'.$config['hostname'].($config['hostport']?':'.$config['hostport']:'').'/'.$config['database']; if(!empty($config['charset'])) { $dsn .= ';charset='.$config['charset']; } return $dsn; } /** * 执行语句 * @access public * @param string $str sql指令 * @param boolean $fetchSql 不执行只是获取SQL * @return integer */ public function execute($str,$fetchSql=false) { $this->initConnect(true); if ( !$this->_linkID ) return false; $this->queryStr = $str; if(!empty($this->bind)){ $that = $this; $this->queryStr = strtr($this->queryStr,array_map(function($val) use($that){ return '\''.$that->escapeString($val).'\''; },$this->bind)); } if($fetchSql){ return $this->queryStr; } $flag = false; if(preg_match("/^\s*(INSERT\s+INTO)\s+(\w+)\s+/i", $str, $match)) { $this->table = C("DB_SEQUENCE_PREFIX").str_ireplace(C("DB_PREFIX"), "", $match[2]); $flag = (boolean)$this->query("SELECT * FROM user_sequences WHERE sequence_name='" . strtoupper($this->table) . "'"); } //释放前次的查询结果 if ( !empty($this->PDOStatement) ) $this->free(); $this->executeTimes++; N('db_write',1); // 兼容代码 // 记录开始执行时间 $this->debug(true); $this->PDOStatement = $this->_linkID->prepare($str); if(false === $this->PDOStatement) { $this->error(); return false; } foreach ($this->bind as $key => $val) { if(is_array($val)){ $this->PDOStatement->bindValue($key, $val[0], $val[1]); }else{ $this->PDOStatement->bindValue($key, $val); } } $this->bind = array(); $result = $this->PDOStatement->execute(); $this->debug(false); if ( false === $result) { $this->error(); return false; } else { $this->numRows = $this->PDOStatement->rowCount(); if($flag || preg_match("/^\s*(INSERT\s+INTO|REPLACE\s+INTO)\s+/i", $str)) { $this->lastInsID = $this->_linkID->lastInsertId(); } return $this->numRows; } } /** * 取得数据表的字段信息 * @access public */ public function getFields($tableName) { list($tableName) = explode(' ', $tableName); $result = $this->query("select a.column_name,data_type,decode(nullable,'Y',0,1) notnull,data_default,decode(a.column_name,b.column_name,1,0) pk " ."from user_tab_columns a,(select column_name from user_constraints c,user_cons_columns col " ."where c.constraint_name=col.constraint_name and c.constraint_type='P'and c.table_name='".strtoupper($tableName) ."') b where table_name='".strtoupper($tableName)."' and a.column_name=b.column_name(+)"); $info = array(); if($result) { foreach ($result as $key => $val) { $info[strtolower($val['column_name'])] = array( 'name' => strtolower($val['column_name']), 'type' => strtolower($val['data_type']), 'notnull' => $val['notnull'], 'default' => $val['data_default'], 'primary' => $val['pk'], 'autoinc' => $val['pk'], ); } } return $info; } /** * 取得数据库的表信息(暂时实现取得用户表信息) * @access public */ public function getTables($dbName='') { $result = $this->query("select table_name from user_tables"); $info = array(); foreach ($result as $key => $val) { $info[$key] = current($val); } return $info; } /** * SQL指令安全过滤 * @access public * @param string $str SQL指令 * @return string */ public function escapeString($str) { return str_ireplace("'", "''", $str); } /** * limit * @access public * @return string */ public function parseLimit($limit) { $limitStr = ''; if(!empty($limit)) { $limit = explode(',',$limit); if(count($limit)>1) $limitStr = "(numrow>" . $limit[0] . ") AND (numrow<=" . ($limit[0]+$limit[1]) . ")"; else $limitStr = "(numrow>0 AND numrow<=".$limit[0].")"; } return $limitStr?' WHERE '.$limitStr:''; } /** * 设置锁机制 * @access protected * @return string */ protected function parseLock($lock=false) { if(!$lock) return ''; return ' FOR UPDATE NOWAIT '; } } ================================================ FILE: ThinkPHP/Library/Think/Db/Driver/Pgsql.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Db\Driver; use Think\Db\Driver; /** * Pgsql数据库驱动 */ class Pgsql extends Driver{ /** * 解析pdo连接的dsn信息 * @access public * @param array $config 连接信息 * @return string */ protected function parseDsn($config){ $dsn = 'pgsql:dbname='.$config['database'].';host='.$config['hostname']; if(!empty($config['hostport'])) { $dsn .= ';port='.$config['hostport']; } return $dsn; } /** * 取得数据表的字段信息 * @access public * @return array */ public function getFields($tableName) { list($tableName) = explode(' ', $tableName); $result = $this->query('select fields_name as "field",fields_type as "type",fields_not_null as "null",fields_key_name as "key",fields_default as "default",fields_default as "extra" from table_msg('.$tableName.');'); $info = array(); if($result){ foreach ($result as $key => $val) { $info[$val['field']] = array( 'name' => $val['field'], 'type' => $val['type'], 'notnull' => (bool) ($val['null'] === ''), // not null is empty, null is yes 'default' => $val['default'], 'primary' => (strtolower($val['key']) == 'pri'), 'autoinc' => (strtolower($val['extra']) == 'auto_increment'), ); } } return $info; } /** * 取得数据库的表信息 * @access public * @return array */ public function getTables($dbName='') { $result = $this->query("select tablename as Tables_in_test from pg_tables where schemaname ='public'"); $info = array(); foreach ($result as $key => $val) { $info[$key] = current($val); } return $info; } /** * limit分析 * @access protected * @param mixed $lmit * @return string */ public function parseLimit($limit) { $limitStr = ''; if(!empty($limit)) { $limit = explode(',',$limit); if(count($limit)>1) { $limitStr .= ' LIMIT '.$limit[1].' OFFSET '.$limit[0].' '; }else{ $limitStr .= ' LIMIT '.$limit[0].' '; } } return $limitStr; } } ================================================ FILE: ThinkPHP/Library/Think/Db/Driver/Sqlite.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Db\Driver; use Think\Db\Driver; /** * Sqlite数据库驱动 */ class Sqlite extends Driver { /** * 解析pdo连接的dsn信息 * @access public * @param array $config 连接信息 * @return string */ protected function parseDsn($config){ $dsn = 'sqlite:'.$config['database']; return $dsn; } /** * 取得数据表的字段信息 * @access public * @return array */ public function getFields($tableName) { list($tableName) = explode(' ', $tableName); $result = $this->query('PRAGMA table_info( '.$tableName.' )'); $info = array(); if($result){ foreach ($result as $key => $val) { $info[$val['field']] = array( 'name' => $val['field'], 'type' => $val['type'], 'notnull' => (bool) ($val['null'] === ''), // not null is empty, null is yes 'default' => $val['default'], 'primary' => (strtolower($val['dey']) == 'pri'), 'autoinc' => (strtolower($val['extra']) == 'auto_increment'), ); } } return $info; } /** * 取得数据库的表信息 * @access public * @return array */ public function getTables($dbName='') { $result = $this->query("SELECT name FROM sqlite_master WHERE type='table' " . "UNION ALL SELECT name FROM sqlite_temp_master " . "WHERE type='table' ORDER BY name"); $info = array(); foreach ($result as $key => $val) { $info[$key] = current($val); } return $info; } /** * SQL指令安全过滤 * @access public * @param string $str SQL指令 * @return string */ public function escapeString($str) { return str_ireplace("'", "''", $str); } /** * limit * @access public * @return string */ public function parseLimit($limit) { $limitStr = ''; if(!empty($limit)) { $limit = explode(',',$limit); if(count($limit)>1) { $limitStr .= ' LIMIT '.$limit[1].' OFFSET '.$limit[0].' '; }else{ $limitStr .= ' LIMIT '.$limit[0].' '; } } return $limitStr; } } ================================================ FILE: ThinkPHP/Library/Think/Db/Driver/Sqlsrv.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Db\Driver; use Think\Db\Driver; use PDO; /** * Sqlsrv数据库驱动 */ class Sqlsrv extends Driver{ protected $selectSql = 'SELECT T1.* FROM (SELECT thinkphp.*, ROW_NUMBER() OVER (%ORDER%) AS ROW_NUMBER FROM (SELECT %DISTINCT% %FIELD% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING% %UNION%) AS thinkphp) AS T1 %LIMIT%%COMMENT%'; // PDO连接参数 protected $options = array( PDO::ATTR_CASE => PDO::CASE_LOWER, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_STRINGIFY_FETCHES => false, PDO::SQLSRV_ATTR_ENCODING => PDO::SQLSRV_ENCODING_UTF8, ); /** * 解析pdo连接的dsn信息 * @access public * @param array $config 连接信息 * @return string */ protected function parseDsn($config){ $dsn = 'sqlsrv:Database='.$config['database'].';Server='.$config['hostname']; if(!empty($config['hostport'])) { $dsn .= ','.$config['hostport']; } return $dsn; } /** * 取得数据表的字段信息 * @access public * @return array */ public function getFields($tableName) { list($tableName) = explode(' ', $tableName); $result = $this->query("SELECT column_name, data_type, column_default, is_nullable FROM information_schema.tables AS t JOIN information_schema.columns AS c ON t.table_catalog = c.table_catalog AND t.table_schema = c.table_schema AND t.table_name = c.table_name WHERE t.table_name = '$tableName'"); $info = array(); if($result) { foreach ($result as $key => $val) { $info[$val['column_name']] = array( 'name' => $val['column_name'], 'type' => $val['data_type'], 'notnull' => (bool) ($val['is_nullable'] === ''), // not null is empty, null is yes 'default' => $val['column_default'], 'primary' => false, 'autoinc' => false, ); } } return $info; } /** * 取得数据表的字段信息 * @access public * @return array */ public function getTables($dbName='') { $result = $this->query("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' "); $info = array(); foreach ($result as $key => $val) { $info[$key] = current($val); } return $info; } /** * order分析 * @access protected * @param mixed $order * @return string */ protected function parseOrder($order) { return !empty($order)? ' ORDER BY '.$order:' ORDER BY rand()'; } /** * 字段名分析 * @access protected * @param string $key * @return string */ protected function parseKey(&$key) { $key = trim($key); if(!is_numeric($key) && !preg_match('/[,\'\"\*\(\)\[.\s]/',$key)) { $key = '['.$key.']'; } return $key; } /** * limit * @access public * @param mixed $limit * @return string */ public function parseLimit($limit) { if(empty($limit)) return ''; $limit = explode(',',$limit); if(count($limit)>1) $limitStr = '(T1.ROW_NUMBER BETWEEN '.$limit[0].' + 1 AND '.$limit[0].' + '.$limit[1].')'; else $limitStr = '(T1.ROW_NUMBER BETWEEN 1 AND '.$limit[0].")"; return 'WHERE '.$limitStr; } /** * 更新记录 * @access public * @param mixed $data 数据 * @param array $options 表达式 * @return false | integer */ public function update($data,$options) { $this->model = $options['model']; $this->parseBind(!empty($options['bind'])?$options['bind']:array()); $sql = 'UPDATE ' .$this->parseTable($options['table']) .$this->parseSet($data) .$this->parseWhere(!empty($options['where'])?$options['where']:'') .$this->parseLock(isset($options['lock'])?$options['lock']:false) .$this->parseComment(!empty($options['comment'])?$options['comment']:''); return $this->execute($sql,!empty($options['fetch_sql']) ? true : false); } /** * 删除记录 * @access public * @param array $options 表达式 * @return false | integer */ public function delete($options=array()) { $this->model = $options['model']; $this->parseBind(!empty($options['bind'])?$options['bind']:array()); $sql = 'DELETE FROM ' .$this->parseTable($options['table']) .$this->parseWhere(!empty($options['where'])?$options['where']:'') .$this->parseLock(isset($options['lock'])?$options['lock']:false) .$this->parseComment(!empty($options['comment'])?$options['comment']:''); return $this->execute($sql,!empty($options['fetch_sql']) ? true : false); } } ================================================ FILE: ThinkPHP/Library/Think/Db/Driver.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Db; use Think\Config; use Think\Debug; use Think\Log; use PDO; abstract class Driver { // PDO操作实例 protected $PDOStatement = null; // 当前操作所属的模型名 protected $model = '_think_'; // 当前SQL指令 protected $queryStr = ''; protected $modelSql = array(); // 最后插入ID protected $lastInsID = null; // 返回或者影响记录数 protected $numRows = 0; // 事务指令数 protected $transTimes = 0; // 错误信息 protected $error = ''; // 数据库连接ID 支持多个连接 protected $linkID = array(); // 当前连接ID protected $_linkID = null; // 数据库连接参数配置 protected $config = array( 'type' => '', // 数据库类型 'hostname' => '127.0.0.1', // 服务器地址 'database' => '', // 数据库名 'username' => '', // 用户名 'password' => '', // 密码 'hostport' => '', // 端口 'dsn' => '', // 'params' => array(), // 数据库连接参数 'charset' => 'utf8', // 数据库编码默认采用utf8 'prefix' => '', // 数据库表前缀 'debug' => false, // 数据库调试模式 'deploy' => 0, // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器) 'rw_separate' => false, // 数据库读写是否分离 主从式有效 'master_num' => 1, // 读写分离后 主服务器数量 'slave_no' => '', // 指定从服务器序号 'db_like_fields' => '', ); // 数据库表达式 protected $exp = array('eq'=>'=','neq'=>'<>','gt'=>'>','egt'=>'>=','lt'=>'<','elt'=>'<=','notlike'=>'NOT LIKE','like'=>'LIKE','in'=>'IN','notin'=>'NOT IN','not in'=>'NOT IN','between'=>'BETWEEN','not between'=>'NOT BETWEEN','notbetween'=>'NOT BETWEEN'); // 查询表达式 protected $selectSql = 'SELECT%DISTINCT% %FIELD% FROM %TABLE%%FORCE%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%%LIMIT% %UNION%%LOCK%%COMMENT%'; // 查询次数 protected $queryTimes = 0; // 执行次数 protected $executeTimes = 0; // PDO连接参数 protected $options = array( PDO::ATTR_CASE => PDO::CASE_LOWER, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL, PDO::ATTR_STRINGIFY_FETCHES => false, ); protected $bind = array(); // 参数绑定 /** * 架构函数 读取数据库配置信息 * @access public * @param array $config 数据库配置数组 */ public function __construct($config=''){ if(!empty($config)) { $this->config = array_merge($this->config,$config); if(is_array($this->config['params'])){ $this->options = $this->config['params'] + $this->options; } } } /** * 连接数据库方法 * @access public */ public function connect($config='',$linkNum=0,$autoConnection=false) { if ( !isset($this->linkID[$linkNum]) ) { if(empty($config)) $config = $this->config; try{ if(empty($config['dsn'])) { $config['dsn'] = $this->parseDsn($config); } if(version_compare(PHP_VERSION,'5.3.6','<=')){ // 禁用模拟预处理语句 $this->options[PDO::ATTR_EMULATE_PREPARES] = false; } $this->linkID[$linkNum] = new PDO( $config['dsn'], $config['username'], $config['password'],$this->options); }catch (\PDOException $e) { if($autoConnection){ trace($e->getMessage(),'','ERR'); return $this->connect($autoConnection,$linkNum); }elseif($config['debug']){ E($e->getMessage()); } } } return $this->linkID[$linkNum]; } /** * 解析pdo连接的dsn信息 * @access public * @param array $config 连接信息 * @return string */ protected function parseDsn($config){} /** * 释放查询结果 * @access public */ public function free() { $this->PDOStatement = null; } /** * 执行查询 返回数据集 * @access public * @param string $str sql指令 * @param boolean $fetchSql 不执行只是获取SQL * @return mixed */ public function query($str,$fetchSql=false) { $this->initConnect(false); if ( !$this->_linkID ) return false; $this->queryStr = $str; if(!empty($this->bind)){ $that = $this; $this->queryStr = strtr($this->queryStr,array_map(function($val) use($that){ return '\''.$that->escapeString($val).'\''; },$this->bind)); } if($fetchSql){ return $this->queryStr; } //释放前次的查询结果 if ( !empty($this->PDOStatement) ) $this->free(); $this->queryTimes++; N('db_query',1); // 兼容代码 // 调试开始 $this->debug(true); $this->PDOStatement = $this->_linkID->prepare($str); if(false === $this->PDOStatement){ $this->error(); return false; } foreach ($this->bind as $key => $val) { if(is_array($val)){ $this->PDOStatement->bindValue($key, $val[0], $val[1]); }else{ $this->PDOStatement->bindValue($key, $val); } } $this->bind = array(); try{ $result = $this->PDOStatement->execute(); // 调试结束 $this->debug(false); if ( false === $result ) { $this->error(); return false; } else { return $this->getResult(); } }catch (\PDOException $e) { $this->error(); return false; } } /** * 执行语句 * @access public * @param string $str sql指令 * @param boolean $fetchSql 不执行只是获取SQL * @return mixed */ public function execute($str,$fetchSql=false) { $this->initConnect(true); if ( !$this->_linkID ) return false; $this->queryStr = $str; if(!empty($this->bind)){ $that = $this; $this->queryStr = strtr($this->queryStr,array_map(function($val) use($that){ return '\''.$that->escapeString($val).'\''; },$this->bind)); } if($fetchSql){ return $this->queryStr; } //释放前次的查询结果 if ( !empty($this->PDOStatement) ) $this->free(); $this->executeTimes++; N('db_write',1); // 兼容代码 // 记录开始执行时间 $this->debug(true); $this->PDOStatement = $this->_linkID->prepare($str); if(false === $this->PDOStatement) { $this->error(); return false; } foreach ($this->bind as $key => $val) { if(is_array($val)){ $this->PDOStatement->bindValue($key, $val[0], $val[1]); }else{ $this->PDOStatement->bindValue($key, $val); } } $this->bind = array(); try{ $result = $this->PDOStatement->execute(); // 调试结束 $this->debug(false); if ( false === $result) { $this->error(); return false; } else { $this->numRows = $this->PDOStatement->rowCount(); if(preg_match("/^\s*(INSERT\s+INTO|REPLACE\s+INTO)\s+/i", $str)) { $this->lastInsID = $this->_linkID->lastInsertId(); } return $this->numRows; } }catch (\PDOException $e) { $this->error(); return false; } } /** * 启动事务 * @access public * @return void */ public function startTrans() { $this->initConnect(true); if ( !$this->_linkID ) return false; //数据rollback 支持 if ($this->transTimes == 0) { $this->_linkID->beginTransaction(); } $this->transTimes++; return ; } /** * 用于非自动提交状态下面的查询提交 * @access public * @return boolean */ public function commit() { if ($this->transTimes > 0) { $result = $this->_linkID->commit(); $this->transTimes = 0; if(!$result){ $this->error(); return false; } } return true; } /** * 事务回滚 * @access public * @return boolean */ public function rollback() { if ($this->transTimes > 0) { $result = $this->_linkID->rollback(); $this->transTimes = 0; if(!$result){ $this->error(); return false; } } return true; } /** * 获得所有的查询数据 * @access private * @return array */ private function getResult() { //返回数据集 $result = $this->PDOStatement->fetchAll(PDO::FETCH_ASSOC); $this->numRows = count( $result ); return $result; } /** * 获得查询次数 * @access public * @param boolean $execute 是否包含所有查询 * @return integer */ public function getQueryTimes($execute=false){ return $execute?$this->queryTimes+$this->executeTimes:$this->queryTimes; } /** * 获得执行次数 * @access public * @return integer */ public function getExecuteTimes(){ return $this->executeTimes; } /** * 关闭数据库 * @access public */ public function close() { $this->_linkID = null; } /** * 数据库错误信息 * 并显示当前的SQL语句 * @access public * @return string */ public function error() { if($this->PDOStatement) { $error = $this->PDOStatement->errorInfo(); $this->error = $error[1].':'.$error[2]; }else{ $this->error = ''; } if('' != $this->queryStr){ $this->error .= "\n [ SQL语句 ] : ".$this->queryStr; } // 记录错误日志 trace($this->error,'','ERR'); if($this->config['debug']) {// 开启数据库调试模式 E($this->error); }else{ return $this->error; } } /** * 设置锁机制 * @access protected * @return string */ protected function parseLock($lock=false) { return $lock? ' FOR UPDATE ' : ''; } /** * set分析 * @access protected * @param array $data * @return string */ protected function parseSet($data) { foreach ($data as $key=>$val){ if(is_array($val) && 'exp' == $val[0]){ $set[] = $this->parseKey($key).'='.$val[1]; }elseif(is_null($val)){ $set[] = $this->parseKey($key).'=NULL'; }elseif(is_scalar($val)) {// 过滤非标量数据 if(0===strpos($val,':') && in_array($val,array_keys($this->bind)) ){ $set[] = $this->parseKey($key).'='.$this->escapeString($val); }else{ $name = count($this->bind); $set[] = $this->parseKey($key).'=:'.$name; $this->bindParam($name,$val); } } } return ' SET '.implode(',',$set); } /** * 参数绑定 * @access protected * @param string $name 绑定参数名 * @param mixed $value 绑定值 * @return void */ protected function bindParam($name,$value){ $this->bind[':'.$name] = $value; } /** * 字段名分析 * @access protected * @param string $key * @return string */ protected function parseKey(&$key) { return $key; } /** * value分析 * @access protected * @param mixed $value * @return string */ protected function parseValue($value) { if(is_string($value)) { $value = strpos($value,':') === 0 && in_array($value,array_keys($this->bind))? $this->escapeString($value) : '\''.$this->escapeString($value).'\''; }elseif(isset($value[0]) && is_string($value[0]) && strtolower($value[0]) == 'exp'){ $value = $this->escapeString($value[1]); }elseif(is_array($value)) { $value = array_map(array($this, 'parseValue'),$value); }elseif(is_bool($value)){ $value = $value ? '1' : '0'; }elseif(is_null($value)){ $value = 'null'; } return $value; } /** * field分析 * @access protected * @param mixed $fields * @return string */ protected function parseField($fields) { if(is_string($fields) && '' !== $fields) { $fields = explode(',',$fields); } if(is_array($fields)) { // 完善数组方式传字段名的支持 // 支持 'field1'=>'field2' 这样的字段别名定义 $array = array(); foreach ($fields as $key=>$field){ if(!is_numeric($key)) $array[] = $this->parseKey($key).' AS '.$this->parseKey($field); else $array[] = $this->parseKey($field); } $fieldsStr = implode(',', $array); }else{ $fieldsStr = '*'; } //TODO 如果是查询全部字段,并且是join的方式,那么就把要查的表加个别名,以免字段被覆盖 return $fieldsStr; } /** * table分析 * @access protected * @param mixed $table * @return string */ protected function parseTable($tables) { if(is_array($tables)) {// 支持别名定义 $array = array(); foreach ($tables as $table=>$alias){ if(!is_numeric($table)) $array[] = $this->parseKey($table).' '.$this->parseKey($alias); else $array[] = $this->parseKey($alias); } $tables = $array; }elseif(is_string($tables)){ $tables = explode(',',$tables); array_walk($tables, array(&$this, 'parseKey')); } return implode(',',$tables); } /** * where分析 * @access protected * @param mixed $where * @return string */ protected function parseWhere($where) { $whereStr = ''; if(is_string($where)) { // 直接使用字符串条件 $whereStr = $where; }else{ // 使用数组表达式 $operate = isset($where['_logic'])?strtoupper($where['_logic']):''; if(in_array($operate,array('AND','OR','XOR'))){ // 定义逻辑运算规则 例如 OR XOR AND NOT $operate = ' '.$operate.' '; unset($where['_logic']); }else{ // 默认进行 AND 运算 $operate = ' AND '; } foreach ($where as $key=>$val){ if(is_numeric($key)){ $key = '_complex'; } if(0===strpos($key,'_')) { // 解析特殊条件表达式 $whereStr .= $this->parseThinkWhere($key,$val); }else{ // 查询字段的安全过滤 // if(!preg_match('/^[A-Z_\|\&\-.a-z0-9\(\)\,]+$/',trim($key))){ // E(L('_EXPRESS_ERROR_').':'.$key); // } // 多条件支持 $multi = is_array($val) && isset($val['_multi']); $key = trim($key); if(strpos($key,'|')) { // 支持 name|title|nickname 方式定义查询字段 $array = explode('|',$key); $str = array(); foreach ($array as $m=>$k){ $v = $multi?$val[$m]:$val; $str[] = $this->parseWhereItem($this->parseKey($k),$v); } $whereStr .= '( '.implode(' OR ',$str).' )'; }elseif(strpos($key,'&')){ $array = explode('&',$key); $str = array(); foreach ($array as $m=>$k){ $v = $multi?$val[$m]:$val; $str[] = '('.$this->parseWhereItem($this->parseKey($k),$v).')'; } $whereStr .= '( '.implode(' AND ',$str).' )'; }else{ $whereStr .= $this->parseWhereItem($this->parseKey($key),$val); } } $whereStr .= $operate; } $whereStr = substr($whereStr,0,-strlen($operate)); } return empty($whereStr)?'':' WHERE '.$whereStr; } // where子单元分析 protected function parseWhereItem($key,$val) { $whereStr = ''; if(is_array($val)) { if(is_string($val[0])) { $exp = strtolower($val[0]); if(preg_match('/^(eq|neq|gt|egt|lt|elt)$/',$exp)) { // 比较运算 $whereStr .= $key.' '.$this->exp[$exp].' '.$this->parseValue($val[1]); }elseif(preg_match('/^(notlike|like)$/',$exp)){// 模糊查找 if(is_array($val[1])) { $likeLogic = isset($val[2])?strtoupper($val[2]):'OR'; if(in_array($likeLogic,array('AND','OR','XOR'))){ $like = array(); foreach ($val[1] as $item){ $like[] = $key.' '.$this->exp[$exp].' '.$this->parseValue($item); } $whereStr .= '('.implode(' '.$likeLogic.' ',$like).')'; } }else{ $whereStr .= $key.' '.$this->exp[$exp].' '.$this->parseValue($val[1]); } }elseif('bind' == $exp ){ // 使用表达式 $whereStr .= $key.' = :'.$val[1]; }elseif('exp' == $exp ){ // 使用表达式 $whereStr .= $key.' '.$val[1]; }elseif(preg_match('/^(notin|not in|in)$/',$exp)){ // IN 运算 if(isset($val[2]) && 'exp'==$val[2]) { $whereStr .= $key.' '.$this->exp[$exp].' '.$val[1]; }else{ if(is_string($val[1])) { $val[1] = explode(',',$val[1]); } $zone = implode(',',$this->parseValue($val[1])); $whereStr .= $key.' '.$this->exp[$exp].' ('.$zone.')'; } }elseif(preg_match('/^(notbetween|not between|between)$/',$exp)){ // BETWEEN运算 $data = is_string($val[1])? explode(',',$val[1]):$val[1]; $whereStr .= $key.' '.$this->exp[$exp].' '.$this->parseValue($data[0]).' AND '.$this->parseValue($data[1]); }else{ E(L('_EXPRESS_ERROR_').':'.$val[0]); } }else { $count = count($val); $rule = isset($val[$count-1]) ? (is_array($val[$count-1]) ? strtoupper($val[$count-1][0]) : strtoupper($val[$count-1]) ) : '' ; if(in_array($rule,array('AND','OR','XOR'))) { $count = $count -1; }else{ $rule = 'AND'; } for($i=0;$i<$count;$i++) { $data = is_array($val[$i])?$val[$i][1]:$val[$i]; if('exp'==strtolower($val[$i][0])) { $whereStr .= $key.' '.$data.' '.$rule.' '; }else{ $whereStr .= $this->parseWhereItem($key,$val[$i]).' '.$rule.' '; } } $whereStr = '( '.substr($whereStr,0,-4).' )'; } }else { //对字符串类型字段采用模糊匹配 $likeFields = $this->config['db_like_fields']; if($likeFields && preg_match('/^('.$likeFields.')$/i',$key)) { $whereStr .= $key.' LIKE '.$this->parseValue('%'.$val.'%'); }else { $whereStr .= $key.' = '.$this->parseValue($val); } } return $whereStr; } /** * 特殊条件分析 * @access protected * @param string $key * @param mixed $val * @return string */ protected function parseThinkWhere($key,$val) { $whereStr = ''; switch($key) { case '_string': // 字符串模式查询条件 $whereStr = $val; break; case '_complex': // 复合查询条件 $whereStr = substr($this->parseWhere($val),6); break; case '_query': // 字符串模式查询条件 parse_str($val,$where); if(isset($where['_logic'])) { $op = ' '.strtoupper($where['_logic']).' '; unset($where['_logic']); }else{ $op = ' AND '; } $array = array(); foreach ($where as $field=>$data) $array[] = $this->parseKey($field).' = '.$this->parseValue($data); $whereStr = implode($op,$array); break; } return '( '.$whereStr.' )'; } /** * limit分析 * @access protected * @param mixed $lmit * @return string */ protected function parseLimit($limit) { return !empty($limit)? ' LIMIT '.$limit.' ':''; } /** * join分析 * @access protected * @param mixed $join * @return string */ protected function parseJoin($join) { $joinStr = ''; if(!empty($join)) { $joinStr = ' '.implode(' ',$join).' '; } return $joinStr; } /** * order分析 * @access protected * @param mixed $order * @return string */ protected function parseOrder($order) { if(is_array($order)) { $array = array(); foreach ($order as $key=>$val){ if(is_numeric($key)) { $array[] = $this->parseKey($val); }else{ $array[] = $this->parseKey($key).' '.$val; } } $order = implode(',',$array); } return !empty($order)? ' ORDER BY '.$order:''; } /** * group分析 * @access protected * @param mixed $group * @return string */ protected function parseGroup($group) { return !empty($group)? ' GROUP BY '.$group:''; } /** * having分析 * @access protected * @param string $having * @return string */ protected function parseHaving($having) { return !empty($having)? ' HAVING '.$having:''; } /** * comment分析 * @access protected * @param string $comment * @return string */ protected function parseComment($comment) { return !empty($comment)? ' /* '.$comment.' */':''; } /** * distinct分析 * @access protected * @param mixed $distinct * @return string */ protected function parseDistinct($distinct) { return !empty($distinct)? ' DISTINCT ' :''; } /** * union分析 * @access protected * @param mixed $union * @return string */ protected function parseUnion($union) { if(empty($union)) return ''; if(isset($union['_all'])) { $str = 'UNION ALL '; unset($union['_all']); }else{ $str = 'UNION '; } foreach ($union as $u){ $sql[] = $str.(is_array($u)?$this->buildSelectSql($u):$u); } return implode(' ',$sql); } /** * 参数绑定分析 * @access protected * @param array $bind * @return array */ protected function parseBind($bind){ $this->bind = array_merge($this->bind,$bind); } /** * index分析,可在操作链中指定需要强制使用的索引 * @access protected * @param mixed $index * @return string */ protected function parseForce($index) { if(empty($index)) return ''; if(is_array($index)) $index = join(",", $index); return sprintf(" FORCE INDEX ( %s ) ", $index); } /** * ON DUPLICATE KEY UPDATE 分析 * @access protected * @param mixed $duplicate * @return string */ protected function parseDuplicate($duplicate){ return ''; } /** * 插入记录 * @access public * @param mixed $data 数据 * @param array $options 参数表达式 * @param boolean $replace 是否replace * @return false | integer */ public function insert($data,$options=array(),$replace=false) { $values = $fields = array(); $this->model = $options['model']; $this->parseBind(!empty($options['bind'])?$options['bind']:array()); foreach ($data as $key=>$val){ if(is_array($val) && 'exp' == $val[0]){ $fields[] = $this->parseKey($key); $values[] = $val[1]; }elseif(is_null($val)){ $fields[] = $this->parseKey($key); $values[] = 'NULL'; }elseif(is_scalar($val)) { // 过滤非标量数据 $fields[] = $this->parseKey($key); if(0===strpos($val,':') && in_array($val,array_keys($this->bind))){ $values[] = $this->parseValue($val); }else{ $name = count($this->bind); $values[] = ':'.$name; $this->bindParam($name,$val); } } } // 兼容数字传入方式 $replace= (is_numeric($replace) && $replace>0)?true:$replace; $sql = (true===$replace?'REPLACE':'INSERT').' INTO '.$this->parseTable($options['table']).' ('.implode(',', $fields).') VALUES ('.implode(',', $values).')'.$this->parseDuplicate($replace); $sql .= $this->parseComment(!empty($options['comment'])?$options['comment']:''); return $this->execute($sql,!empty($options['fetch_sql']) ? true : false); } /** * 批量插入记录 * @access public * @param mixed $dataSet 数据集 * @param array $options 参数表达式 * @param boolean $replace 是否replace * @return false | integer */ public function insertAll($dataSet,$options=array(),$replace=false) { $values = array(); $this->model = $options['model']; if(!is_array($dataSet[0])) return false; $this->parseBind(!empty($options['bind'])?$options['bind']:array()); $fields = array_map(array($this,'parseKey'),array_keys($dataSet[0])); foreach ($dataSet as $data){ $value = array(); foreach ($data as $key=>$val){ if(is_array($val) && 'exp' == $val[0]){ $value[] = $val[1]; }elseif(is_null($val)){ $value[] = 'NULL'; }elseif(is_scalar($val)){ if(0===strpos($val,':') && in_array($val,array_keys($this->bind))){ $value[] = $this->parseValue($val); }else{ $name = count($this->bind); $value[] = ':'.$name; $this->bindParam($name,$val); } } } $values[] = 'SELECT '.implode(',', $value); } $sql = 'INSERT INTO '.$this->parseTable($options['table']).' ('.implode(',', $fields).') '.implode(' UNION ALL ',$values); $sql .= $this->parseComment(!empty($options['comment'])?$options['comment']:''); return $this->execute($sql,!empty($options['fetch_sql']) ? true : false); } /** * 通过Select方式插入记录 * @access public * @param string $fields 要插入的数据表字段名 * @param string $table 要插入的数据表名 * @param array $option 查询数据参数 * @return false | integer */ public function selectInsert($fields,$table,$options=array()) { $this->model = $options['model']; $this->parseBind(!empty($options['bind'])?$options['bind']:array()); if(is_string($fields)) $fields = explode(',',$fields); array_walk($fields, array($this, 'parseKey')); $sql = 'INSERT INTO '.$this->parseTable($table).' ('.implode(',', $fields).') '; $sql .= $this->buildSelectSql($options); return $this->execute($sql,!empty($options['fetch_sql']) ? true : false); } /** * 更新记录 * @access public * @param mixed $data 数据 * @param array $options 表达式 * @return false | integer */ public function update($data,$options) { $this->model = $options['model']; $this->parseBind(!empty($options['bind'])?$options['bind']:array()); $table = $this->parseTable($options['table']); $sql = 'UPDATE ' . $table . $this->parseSet($data); if(strpos($table,',')){// 多表更新支持JOIN操作 $sql .= $this->parseJoin(!empty($options['join'])?$options['join']:''); } $sql .= $this->parseWhere(!empty($options['where'])?$options['where']:''); if(!strpos($table,',')){ // 单表更新支持order和lmit $sql .= $this->parseOrder(!empty($options['order'])?$options['order']:'') .$this->parseLimit(!empty($options['limit'])?$options['limit']:''); } $sql .= $this->parseComment(!empty($options['comment'])?$options['comment']:''); return $this->execute($sql,!empty($options['fetch_sql']) ? true : false); } /** * 删除记录 * @access public * @param array $options 表达式 * @return false | integer */ public function delete($options=array()) { $this->model = $options['model']; $this->parseBind(!empty($options['bind'])?$options['bind']:array()); $table = $this->parseTable($options['table']); $sql = 'DELETE FROM '.$table; if(strpos($table,',')){// 多表删除支持USING和JOIN操作 if(!empty($options['using'])){ $sql .= ' USING '.$this->parseTable($options['using']).' '; } $sql .= $this->parseJoin(!empty($options['join'])?$options['join']:''); } $sql .= $this->parseWhere(!empty($options['where'])?$options['where']:''); if(!strpos($table,',')){ // 单表删除支持order和limit $sql .= $this->parseOrder(!empty($options['order'])?$options['order']:'') .$this->parseLimit(!empty($options['limit'])?$options['limit']:''); } $sql .= $this->parseComment(!empty($options['comment'])?$options['comment']:''); return $this->execute($sql,!empty($options['fetch_sql']) ? true : false); } /** * 查找记录 * @access public * @param array $options 表达式 * @return mixed */ public function select($options=array()) { $this->model = $options['model']; $this->parseBind(!empty($options['bind'])?$options['bind']:array()); $sql = $this->buildSelectSql($options); $result = $this->query($sql,!empty($options['fetch_sql']) ? true : false); return $result; } /** * 生成查询SQL * @access public * @param array $options 表达式 * @return string */ public function buildSelectSql($options=array()) { if(isset($options['page'])) { // 根据页数计算limit list($page,$listRows) = $options['page']; $page = $page>0 ? $page : 1; $listRows= $listRows>0 ? $listRows : (is_numeric($options['limit'])?$options['limit']:20); $offset = $listRows*($page-1); $options['limit'] = $offset.','.$listRows; } $sql = $this->parseSql($this->selectSql,$options); return $sql; } /** * 替换SQL语句中表达式 * @access public * @param array $options 表达式 * @return string */ public function parseSql($sql,$options=array()){ $sql = str_replace( array('%TABLE%','%DISTINCT%','%FIELD%','%JOIN%','%WHERE%','%GROUP%','%HAVING%','%ORDER%','%LIMIT%','%UNION%','%LOCK%','%COMMENT%','%FORCE%'), array( $this->parseTable($options['table']), $this->parseDistinct(isset($options['distinct'])?$options['distinct']:false), $this->parseField(!empty($options['field'])?$options['field']:'*'), $this->parseJoin(!empty($options['join'])?$options['join']:''), $this->parseWhere(!empty($options['where'])?$options['where']:''), $this->parseGroup(!empty($options['group'])?$options['group']:''), $this->parseHaving(!empty($options['having'])?$options['having']:''), $this->parseOrder(!empty($options['order'])?$options['order']:''), $this->parseLimit(!empty($options['limit'])?$options['limit']:''), $this->parseUnion(!empty($options['union'])?$options['union']:''), $this->parseLock(isset($options['lock'])?$options['lock']:false), $this->parseComment(!empty($options['comment'])?$options['comment']:''), $this->parseForce(!empty($options['force'])?$options['force']:'') ),$sql); return $sql; } /** * 获取最近一次查询的sql语句 * @param string $model 模型名 * @access public * @return string */ public function getLastSql($model='') { return $model?$this->modelSql[$model]:$this->queryStr; } /** * 获取最近插入的ID * @access public * @return string */ public function getLastInsID() { return $this->lastInsID; } /** * 获取最近的错误信息 * @access public * @return string */ public function getError() { return $this->error; } /** * SQL指令安全过滤 * @access public * @param string $str SQL字符串 * @return string */ public function escapeString($str) { return addslashes($str); } /** * 设置当前操作模型 * @access public * @param string $model 模型名 * @return void */ public function setModel($model){ $this->model = $model; } /** * 数据库调试 记录当前SQL * @access protected * @param boolean $start 调试开始标记 true 开始 false 结束 */ protected function debug($start) { if($this->config['debug']) {// 开启数据库调试模式 if($start) { G('queryStartTime'); }else{ $this->modelSql[$this->model] = $this->queryStr; //$this->model = '_think_'; // 记录操作结束时间 G('queryEndTime'); trace($this->queryStr.' [ RunTime:'.G('queryStartTime','queryEndTime').'s ]','','SQL'); } } } /** * 初始化数据库连接 * @access protected * @param boolean $master 主服务器 * @return void */ protected function initConnect($master=true) { if(!empty($this->config['deploy'])) // 采用分布式数据库 $this->_linkID = $this->multiConnect($master); else // 默认单数据库 if ( !$this->_linkID ) $this->_linkID = $this->connect(); } /** * 连接分布式服务器 * @access protected * @param boolean $master 主服务器 * @return void */ protected function multiConnect($master=false) { // 分布式数据库配置解析 $_config['username'] = explode(',',$this->config['username']); $_config['password'] = explode(',',$this->config['password']); $_config['hostname'] = explode(',',$this->config['hostname']); $_config['hostport'] = explode(',',$this->config['hostport']); $_config['database'] = explode(',',$this->config['database']); $_config['dsn'] = explode(',',$this->config['dsn']); $_config['charset'] = explode(',',$this->config['charset']); $m = floor(mt_rand(0,$this->config['master_num']-1)); // 数据库读写是否分离 if($this->config['rw_separate']){ // 主从式采用读写分离 if($master) // 主服务器写入 $r = $m; else{ if(is_numeric($this->config['slave_no'])) {// 指定服务器读 $r = $this->config['slave_no']; }else{ // 读操作连接从服务器 $r = floor(mt_rand($this->config['master_num'],count($_config['hostname'])-1)); // 每次随机连接的数据库 } } }else{ // 读写操作不区分服务器 $r = floor(mt_rand(0,count($_config['hostname'])-1)); // 每次随机连接的数据库 } if($m != $r ){ $db_master = array( 'username' => isset($_config['username'][$m])?$_config['username'][$m]:$_config['username'][0], 'password' => isset($_config['password'][$m])?$_config['password'][$m]:$_config['password'][0], 'hostname' => isset($_config['hostname'][$m])?$_config['hostname'][$m]:$_config['hostname'][0], 'hostport' => isset($_config['hostport'][$m])?$_config['hostport'][$m]:$_config['hostport'][0], 'database' => isset($_config['database'][$m])?$_config['database'][$m]:$_config['database'][0], 'dsn' => isset($_config['dsn'][$m])?$_config['dsn'][$m]:$_config['dsn'][0], 'charset' => isset($_config['charset'][$m])?$_config['charset'][$m]:$_config['charset'][0], ); } $db_config = array( 'username' => isset($_config['username'][$r])?$_config['username'][$r]:$_config['username'][0], 'password' => isset($_config['password'][$r])?$_config['password'][$r]:$_config['password'][0], 'hostname' => isset($_config['hostname'][$r])?$_config['hostname'][$r]:$_config['hostname'][0], 'hostport' => isset($_config['hostport'][$r])?$_config['hostport'][$r]:$_config['hostport'][0], 'database' => isset($_config['database'][$r])?$_config['database'][$r]:$_config['database'][0], 'dsn' => isset($_config['dsn'][$r])?$_config['dsn'][$r]:$_config['dsn'][0], 'charset' => isset($_config['charset'][$r])?$_config['charset'][$r]:$_config['charset'][0], ); return $this->connect($db_config,$r,$r == $m ? false : $db_master); } /** * 析构方法 * @access public */ public function __destruct() { // 释放查询 if ($this->PDOStatement){ $this->free(); } // 关闭连接 $this->close(); } } ================================================ FILE: ThinkPHP/Library/Think/Db/Lite.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Db; use Think\Config; use Think\Debug; use Think\Log; use PDO; class Lite { // PDO操作实例 protected $PDOStatement = null; // 当前操作所属的模型名 protected $model = '_think_'; // 当前SQL指令 protected $queryStr = ''; protected $modelSql = array(); // 最后插入ID protected $lastInsID = null; // 返回或者影响记录数 protected $numRows = 0; // 事务指令数 protected $transTimes = 0; // 错误信息 protected $error = ''; // 数据库连接ID 支持多个连接 protected $linkID = array(); // 当前连接ID protected $_linkID = null; // 数据库连接参数配置 protected $config = array( 'type' => '', // 数据库类型 'hostname' => '127.0.0.1', // 服务器地址 'database' => '', // 数据库名 'username' => '', // 用户名 'password' => '', // 密码 'hostport' => '', // 端口 'dsn' => '', // 'params' => array(), // 数据库连接参数 'charset' => 'utf8', // 数据库编码默认采用utf8 'prefix' => '', // 数据库表前缀 'debug' => false, // 数据库调试模式 'deploy' => 0, // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器) 'rw_separate' => false, // 数据库读写是否分离 主从式有效 'master_num' => 1, // 读写分离后 主服务器数量 'slave_no' => '', // 指定从服务器序号 ); // 数据库表达式 protected $comparison = array('eq'=>'=','neq'=>'<>','gt'=>'>','egt'=>'>=','lt'=>'<','elt'=>'<=','notlike'=>'NOT LIKE','like'=>'LIKE','in'=>'IN','notin'=>'NOT IN'); // 查询表达式 protected $selectSql = 'SELECT%DISTINCT% %FIELD% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%%LIMIT% %UNION%%COMMENT%'; // 查询次数 protected $queryTimes = 0; // 执行次数 protected $executeTimes = 0; // PDO连接参数 protected $options = array( PDO::ATTR_CASE => PDO::CASE_LOWER, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL, PDO::ATTR_STRINGIFY_FETCHES => false, ); /** * 架构函数 读取数据库配置信息 * @access public * @param array $config 数据库配置数组 */ public function __construct($config=''){ if(!empty($config)) { $this->config = array_merge($this->config,$config); if(is_array($this->config['params'])){ $this->options += $this->config['params']; } } } /** * 连接数据库方法 * @access public */ public function connect($config='',$linkNum=0) { if ( !isset($this->linkID[$linkNum]) ) { if(empty($config)) $config = $this->config; try{ if(empty($config['dsn'])) { $config['dsn'] = $this->parseDsn($config); } if(version_compare(PHP_VERSION,'5.3.6','<=')){ //禁用模拟预处理语句 $this->options[PDO::ATTR_EMULATE_PREPARES] = false; } $this->linkID[$linkNum] = new PDO( $config['dsn'], $config['username'], $config['password'],$this->options); }catch (\PDOException $e) { E($e->getMessage()); } } return $this->linkID[$linkNum]; } /** * 解析pdo连接的dsn信息 * @access public * @param array $config 连接信息 * @return string */ protected function parseDsn($config){} /** * 释放查询结果 * @access public */ public function free() { $this->PDOStatement = null; } /** * 执行查询 返回数据集 * @access public * @param string $str sql指令 * @param array $bind 参数绑定 * @return mixed */ public function query($str,$bind=array()) { $this->initConnect(false); if ( !$this->_linkID ) return false; $this->queryStr = $str; if(!empty($bind)){ $that = $this; $this->queryStr = strtr($this->queryStr,array_map(function($val) use($that){ return '\''.$that->escapeString($val).'\''; },$bind)); } //释放前次的查询结果 if ( !empty($this->PDOStatement) ) $this->free(); $this->queryTimes++; N('db_query',1); // 兼容代码 // 调试开始 $this->debug(true); $this->PDOStatement = $this->_linkID->prepare($str); if(false === $this->PDOStatement) E($this->error()); foreach ($bind as $key => $val) { if(is_array($val)){ $this->PDOStatement->bindValue($key, $val[0], $val[1]); }else{ $this->PDOStatement->bindValue($key, $val); } } $result = $this->PDOStatement->execute(); // 调试结束 $this->debug(false); if ( false === $result ) { $this->error(); return false; } else { return $this->getResult(); } } /** * 执行语句 * @access public * @param string $str sql指令 * @param array $bind 参数绑定 * @return integer */ public function execute($str,$bind=array()) { $this->initConnect(true); if ( !$this->_linkID ) return false; $this->queryStr = $str; if(!empty($bind)){ $that = $this; $this->queryStr = strtr($this->queryStr,array_map(function($val) use($that){ return '\''.$that->escapeString($val).'\''; },$bind)); } //释放前次的查询结果 if ( !empty($this->PDOStatement) ) $this->free(); $this->executeTimes++; N('db_write',1); // 兼容代码 // 记录开始执行时间 $this->debug(true); $this->PDOStatement = $this->_linkID->prepare($str); if(false === $this->PDOStatement) { E($this->error()); } foreach ($bind as $key => $val) { if(is_array($val)){ $this->PDOStatement->bindValue($key, $val[0], $val[1]); }else{ $this->PDOStatement->bindValue($key, $val); } } $result = $this->PDOStatement->execute(); $this->debug(false); if ( false === $result) { $this->error(); return false; } else { $this->numRows = $this->PDOStatement->rowCount(); if(preg_match("/^\s*(INSERT\s+INTO|REPLACE\s+INTO)\s+/i", $str)) { $this->lastInsID = $this->_linkID->lastInsertId(); } return $this->numRows; } } /** * 启动事务 * @access public * @return void */ public function startTrans() { $this->initConnect(true); if ( !$this->_linkID ) return false; //数据rollback 支持 if ($this->transTimes == 0) { $this->_linkID->beginTransaction(); } $this->transTimes++; return ; } /** * 用于非自动提交状态下面的查询提交 * @access public * @return boolean */ public function commit() { if ($this->transTimes > 0) { $result = $this->_linkID->commit(); $this->transTimes = 0; if(!$result){ $this->error(); return false; } } return true; } /** * 事务回滚 * @access public * @return boolean */ public function rollback() { if ($this->transTimes > 0) { $result = $this->_linkID->rollback(); $this->transTimes = 0; if(!$result){ $this->error(); return false; } } return true; } /** * 获得所有的查询数据 * @access private * @return array */ private function getResult() { //返回数据集 $result = $this->PDOStatement->fetchAll(PDO::FETCH_ASSOC); $this->numRows = count( $result ); return $result; } /** * 获得查询次数 * @access public * @param boolean $execute 是否包含所有查询 * @return integer */ public function getQueryTimes($execute=false){ return $execute?$this->queryTimes+$this->executeTimes:$this->queryTimes; } /** * 获得执行次数 * @access public * @return integer */ public function getExecuteTimes(){ return $this->executeTimes; } /** * 关闭数据库 * @access public */ public function close() { $this->_linkID = null; } /** * 数据库错误信息 * 并显示当前的SQL语句 * @access public * @return string */ public function error() { if($this->PDOStatement) { $error = $this->PDOStatement->errorInfo(); $this->error = $error[1].':'.$error[2]; }else{ $this->error = ''; } if('' != $this->queryStr){ $this->error .= "\n [ SQL语句 ] : ".$this->queryStr; } // 记录错误日志 trace($this->error,'','ERR'); if($this->config['debug']) {// 开启数据库调试模式 E($this->error); }else{ return $this->error; } } /** * 获取最近一次查询的sql语句 * @param string $model 模型名 * @access public * @return string */ public function getLastSql($model='') { return $model?$this->modelSql[$model]:$this->queryStr; } /** * 获取最近插入的ID * @access public * @return string */ public function getLastInsID() { return $this->lastInsID; } /** * 获取最近的错误信息 * @access public * @return string */ public function getError() { return $this->error; } /** * SQL指令安全过滤 * @access public * @param string $str SQL字符串 * @return string */ public function escapeString($str) { return addslashes($str); } /** * 设置当前操作模型 * @access public * @param string $model 模型名 * @return void */ public function setModel($model){ $this->model = $model; } /** * 数据库调试 记录当前SQL * @access protected * @param boolean $start 调试开始标记 true 开始 false 结束 */ protected function debug($start) { if($this->config['debug']) {// 开启数据库调试模式 if($start) { G('queryStartTime'); }else{ $this->modelSql[$this->model] = $this->queryStr; //$this->model = '_think_'; // 记录操作结束时间 G('queryEndTime'); trace($this->queryStr.' [ RunTime:'.G('queryStartTime','queryEndTime').'s ]','','SQL'); } } } /** * 初始化数据库连接 * @access protected * @param boolean $master 主服务器 * @return void */ protected function initConnect($master=true) { if(!empty($this->config['deploy'])) // 采用分布式数据库 $this->_linkID = $this->multiConnect($master); else // 默认单数据库 if ( !$this->_linkID ) $this->_linkID = $this->connect(); } /** * 连接分布式服务器 * @access protected * @param boolean $master 主服务器 * @return void */ protected function multiConnect($master=false) { // 分布式数据库配置解析 $_config['username'] = explode(',',$this->config['username']); $_config['password'] = explode(',',$this->config['password']); $_config['hostname'] = explode(',',$this->config['hostname']); $_config['hostport'] = explode(',',$this->config['hostport']); $_config['database'] = explode(',',$this->config['database']); $_config['dsn'] = explode(',',$this->config['dsn']); $_config['charset'] = explode(',',$this->config['charset']); // 数据库读写是否分离 if($this->config['rw_separate']){ // 主从式采用读写分离 if($master) // 主服务器写入 $r = floor(mt_rand(0,$this->config['master_num']-1)); else{ if(is_numeric($this->config['slave_no'])) {// 指定服务器读 $r = $this->config['slave_no']; }else{ // 读操作连接从服务器 $r = floor(mt_rand($this->config['master_num'],count($_config['hostname'])-1)); // 每次随机连接的数据库 } } }else{ // 读写操作不区分服务器 $r = floor(mt_rand(0,count($_config['hostname'])-1)); // 每次随机连接的数据库 } $db_config = array( 'username' => isset($_config['username'][$r])?$_config['username'][$r]:$_config['username'][0], 'password' => isset($_config['password'][$r])?$_config['password'][$r]:$_config['password'][0], 'hostname' => isset($_config['hostname'][$r])?$_config['hostname'][$r]:$_config['hostname'][0], 'hostport' => isset($_config['hostport'][$r])?$_config['hostport'][$r]:$_config['hostport'][0], 'database' => isset($_config['database'][$r])?$_config['database'][$r]:$_config['database'][0], 'dsn' => isset($_config['dsn'][$r])?$_config['dsn'][$r]:$_config['dsn'][0], 'charset' => isset($_config['charset'][$r])?$_config['charset'][$r]:$_config['charset'][0], ); return $this->connect($db_config,$r); } /** * 析构方法 * @access public */ public function __destruct() { // 释放查询 if ($this->PDOStatement){ $this->free(); } // 关闭连接 $this->close(); } } ================================================ FILE: ThinkPHP/Library/Think/Db.class.php ================================================ // +---------------------------------------------------------------------- namespace Think; /** * ThinkPHP 数据库中间层实现类 */ class Db { static private $instance = array(); // 数据库连接实例 static private $_instance = null; // 当前数据库连接实例 /** * 取得数据库类实例 * @static * @access public * @param mixed $config 连接配置 * @return Object 返回数据库驱动类 */ static public function getInstance($config=array()) { $md5 = md5(serialize($config)); if(!isset(self::$instance[$md5])) { // 解析连接参数 支持数组和字符串 $options = self::parseConfig($config); // 兼容mysqli if('mysqli' == $options['type']) $options['type'] = 'mysql'; // 如果采用lite方式 仅支持原生SQL 包括query和execute方法 $class = !empty($options['lite'])? 'Think\Db\Lite' : 'Think\\Db\\Driver\\'.ucwords(strtolower($options['type'])); if(class_exists($class)){ self::$instance[$md5] = new $class($options); }else{ // 类没有定义 E(L('_NO_DB_DRIVER_').': ' . $class); } } self::$_instance = self::$instance[$md5]; return self::$_instance; } /** * 数据库连接参数解析 * @static * @access private * @param mixed $config * @return array */ static private function parseConfig($config){ if(!empty($config)){ if(is_string($config)) { return self::parseDsn($config); } $config = array_change_key_case($config); $config = array ( 'type' => $config['db_type'], 'username' => $config['db_user'], 'password' => $config['db_pwd'], 'hostname' => $config['db_host'], 'hostport' => $config['db_port'], 'database' => $config['db_name'], 'dsn' => isset($config['db_dsn'])?$config['db_dsn']:null, 'params' => isset($config['db_params'])?$config['db_params']:null, 'charset' => isset($config['db_charset'])?$config['db_charset']:'utf8', 'deploy' => isset($config['db_deploy_type'])?$config['db_deploy_type']:0, 'rw_separate' => isset($config['db_rw_separate'])?$config['db_rw_separate']:false, 'master_num' => isset($config['db_master_num'])?$config['db_master_num']:1, 'slave_no' => isset($config['db_slave_no'])?$config['db_slave_no']:'', 'debug' => isset($config['db_debug'])?$config['db_debug']:APP_DEBUG, 'lite' => isset($config['db_lite'])?$config['db_lite']:false, ); }else { $config = array ( 'type' => C('DB_TYPE'), 'username' => C('DB_USER'), 'password' => C('DB_PWD'), 'hostname' => C('DB_HOST'), 'hostport' => C('DB_PORT'), 'database' => C('DB_NAME'), 'dsn' => C('DB_DSN'), 'params' => C('DB_PARAMS'), 'charset' => C('DB_CHARSET'), 'deploy' => C('DB_DEPLOY_TYPE'), 'rw_separate' => C('DB_RW_SEPARATE'), 'master_num' => C('DB_MASTER_NUM'), 'slave_no' => C('DB_SLAVE_NO'), 'debug' => C('DB_DEBUG',null,APP_DEBUG), 'lite' => C('DB_LITE'), ); } return $config; } /** * DSN解析 * 格式: mysql://username:passwd@localhost:3306/DbName?param1=val1¶m2=val2#utf8 * @static * @access private * @param string $dsnStr * @return array */ static private function parseDsn($dsnStr) { if( empty($dsnStr) ){return false;} $info = parse_url($dsnStr); if(!$info) { return false; } $dsn = array( 'type' => $info['scheme'], 'username' => isset($info['user']) ? $info['user'] : '', 'password' => isset($info['pass']) ? $info['pass'] : '', 'hostname' => isset($info['host']) ? $info['host'] : '', 'hostport' => isset($info['port']) ? $info['port'] : '', 'database' => isset($info['path']) ? substr($info['path'],1) : '', 'charset' => isset($info['fragment'])?$info['fragment']:'utf8', ); if(isset($info['query'])) { parse_str($info['query'],$dsn['params']); }else{ $dsn['params'] = array(); } return $dsn; } // 调用驱动类的方法 static public function __callStatic($method, $params){ return call_user_func_array(array(self::$_instance, $method), $params); } } ================================================ FILE: ThinkPHP/Library/Think/Dispatcher.class.php ================================================ // +---------------------------------------------------------------------- namespace Think; /** * ThinkPHP内置的Dispatcher类 * 完成URL解析、路由和调度 */ class Dispatcher { /** * URL映射到控制器 * @access public * @return void */ static public function dispatch() { $varPath = C('VAR_PATHINFO'); $varAddon = C('VAR_ADDON'); $varModule = C('VAR_MODULE'); $varController = C('VAR_CONTROLLER'); $varAction = C('VAR_ACTION'); $urlCase = C('URL_CASE_INSENSITIVE'); if(isset($_GET[$varPath])) { // 判断URL里面是否有兼容模式参数 $_SERVER['PATH_INFO'] = $_GET[$varPath]; unset($_GET[$varPath]); }elseif(IS_CLI){ // CLI模式下 index.php module/controller/action/params/... $_SERVER['PATH_INFO'] = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : ''; } // 开启子域名部署 if(C('APP_SUB_DOMAIN_DEPLOY')) { $rules = C('APP_SUB_DOMAIN_RULES'); if(isset($rules[$_SERVER['HTTP_HOST']])) { // 完整域名或者IP配置 define('APP_DOMAIN',$_SERVER['HTTP_HOST']); // 当前完整域名 $rule = $rules[APP_DOMAIN]; }else{ if(strpos(C('APP_DOMAIN_SUFFIX'),'.')){ // com.cn net.cn $domain = array_slice(explode('.', $_SERVER['HTTP_HOST']), 0, -3); }else{ $domain = array_slice(explode('.', $_SERVER['HTTP_HOST']), 0, -2); } if(!empty($domain)) { $subDomain = implode('.', $domain); define('SUB_DOMAIN',$subDomain); // 当前完整子域名 $domain2 = array_pop($domain); // 二级域名 if($domain) { // 存在三级域名 $domain3 = array_pop($domain); } if(isset($rules[$subDomain])) { // 子域名 $rule = $rules[$subDomain]; }elseif(isset($rules['*.' . $domain2]) && !empty($domain3)){ // 泛三级域名 $rule = $rules['*.' . $domain2]; $panDomain = $domain3; }elseif(isset($rules['*']) && !empty($domain2) && 'www' != $domain2 ){ // 泛二级域名 $rule = $rules['*']; $panDomain = $domain2; } } } if(!empty($rule)) { // 子域名部署规则 '子域名'=>array('模块名[/控制器名]','var1=a&var2=b'); if(is_array($rule)){ list($rule,$vars) = $rule; } $array = explode('/',$rule); // 模块绑定 define('BIND_MODULE',array_shift($array)); // 控制器绑定 if(!empty($array)) { $controller = array_shift($array); if($controller){ define('BIND_CONTROLLER',$controller); } } if(isset($vars)) { // 传入参数 parse_str($vars,$parms); if(isset($panDomain)){ $pos = array_search('*', $parms); if(false !== $pos) { // 泛域名作为参数 $parms[$pos] = $panDomain; } } $_GET = array_merge($_GET,$parms); } } } // 分析PATHINFO信息 if(!isset($_SERVER['PATH_INFO'])) { $types = explode(',',C('URL_PATHINFO_FETCH')); foreach ($types as $type){ if(0===strpos($type,':')) {// 支持函数判断 $_SERVER['PATH_INFO'] = call_user_func(substr($type,1)); break; }elseif(!empty($_SERVER[$type])) { $_SERVER['PATH_INFO'] = (0 === strpos($_SERVER[$type],$_SERVER['SCRIPT_NAME']))? substr($_SERVER[$type], strlen($_SERVER['SCRIPT_NAME'])) : $_SERVER[$type]; break; } } } $depr = C('URL_PATHINFO_DEPR'); define('MODULE_PATHINFO_DEPR', $depr); if(empty($_SERVER['PATH_INFO'])) { $_SERVER['PATH_INFO'] = ''; define('__INFO__',''); define('__EXT__',''); }else{ define('__INFO__',trim($_SERVER['PATH_INFO'],'/')); // URL后缀 define('__EXT__', strtolower(pathinfo($_SERVER['PATH_INFO'],PATHINFO_EXTENSION))); $_SERVER['PATH_INFO'] = __INFO__; if(!defined('BIND_MODULE') && (!C('URL_ROUTER_ON') || !Route::check())){ if (__INFO__ && C('MULTI_MODULE')){ // 获取模块名 $paths = explode($depr,__INFO__,2); $allowList = C('MODULE_ALLOW_LIST'); // 允许的模块列表 $module = preg_replace('/\.' . __EXT__ . '$/i', '',$paths[0]); if( empty($allowList) || (is_array($allowList) && in_array_case($module, $allowList))){ $_GET[$varModule] = $module; $_SERVER['PATH_INFO'] = isset($paths[1])?$paths[1]:''; } } } } // URL常量 define('__SELF__',strip_tags($_SERVER[C('URL_REQUEST_URI')])); // 获取模块名称 define('MODULE_NAME', defined('BIND_MODULE')? BIND_MODULE : self::getModule($varModule)); // 检测模块是否存在 if( MODULE_NAME && (defined('BIND_MODULE') || !in_array_case(MODULE_NAME,C('MODULE_DENY_LIST')) ) && is_dir(APP_PATH.MODULE_NAME)){ // 定义当前模块路径 define('MODULE_PATH', APP_PATH.MODULE_NAME.'/'); // 定义当前模块的模版缓存路径 C('CACHE_PATH',CACHE_PATH.MODULE_NAME.'/'); // 定义当前模块的日志目录 C('LOG_PATH', realpath(LOG_PATH).'/'.MODULE_NAME.'/'); // 模块检测 Hook::listen('module_check'); // 加载模块配置文件 if(is_file(MODULE_PATH.'Conf/config'.CONF_EXT)) C(load_config(MODULE_PATH.'Conf/config'.CONF_EXT)); // 加载应用模式对应的配置文件 if('common' != APP_MODE && is_file(MODULE_PATH.'Conf/config_'.APP_MODE.CONF_EXT)) C(load_config(MODULE_PATH.'Conf/config_'.APP_MODE.CONF_EXT)); // 当前应用状态对应的配置文件 if(APP_STATUS && is_file(MODULE_PATH.'Conf/'.APP_STATUS.CONF_EXT)) C(load_config(MODULE_PATH.'Conf/'.APP_STATUS.CONF_EXT)); // 加载模块别名定义 if(is_file(MODULE_PATH.'Conf/alias.php')) Think::addMap(include MODULE_PATH.'Conf/alias.php'); // 加载模块tags文件定义 if(is_file(MODULE_PATH.'Conf/tags.php')) Hook::import(include MODULE_PATH.'Conf/tags.php'); // 加载模块函数文件 if(is_file(MODULE_PATH.'Common/function.php')) include MODULE_PATH.'Common/function.php'; $urlCase = C('URL_CASE_INSENSITIVE'); // 加载模块的扩展配置文件 load_ext_file(MODULE_PATH); }else{ E(L('_MODULE_NOT_EXIST_').':'.MODULE_NAME); } if(!defined('__APP__')){ $urlMode = C('URL_MODEL'); if($urlMode == URL_COMPAT ){// 兼容模式判断 define('PHP_FILE',_PHP_FILE_.'?'.$varPath.'='); }elseif($urlMode == URL_REWRITE ) { $url = dirname(_PHP_FILE_); if($url == '/' || $url == '\\') $url = ''; define('PHP_FILE',$url); }else { define('PHP_FILE',_PHP_FILE_); } // 当前应用地址 define('__APP__',strip_tags(PHP_FILE)); } // 模块URL地址 $moduleName = defined('MODULE_ALIAS')? MODULE_ALIAS : MODULE_NAME; define('__MODULE__',(defined('BIND_MODULE') || !C('MULTI_MODULE'))? __APP__ : __APP__.'/'.($urlCase ? strtolower($moduleName) : $moduleName)); if('' != $_SERVER['PATH_INFO'] && (!C('URL_ROUTER_ON') || !Route::check()) ){ // 检测路由规则 如果没有则按默认规则调度URL Hook::listen('path_info'); // 检查禁止访问的URL后缀 if(C('URL_DENY_SUFFIX') && preg_match('/\.('.trim(C('URL_DENY_SUFFIX'),'.').')$/i', $_SERVER['PATH_INFO'])){ send_http_status(404); exit; } // 去除URL后缀 $_SERVER['PATH_INFO'] = preg_replace(C('URL_HTML_SUFFIX')? '/\.('.trim(C('URL_HTML_SUFFIX'),'.').')$/i' : '/\.'.__EXT__.'$/i', '', $_SERVER['PATH_INFO']); $depr = C('URL_PATHINFO_DEPR'); $paths = explode($depr,trim($_SERVER['PATH_INFO'],$depr)); if(!defined('BIND_CONTROLLER')) {// 获取控制器 if(C('CONTROLLER_LEVEL')>1){// 控制器层次 $_GET[$varController] = implode('/',array_slice($paths,0,C('CONTROLLER_LEVEL'))); $paths = array_slice($paths, C('CONTROLLER_LEVEL')); }else{ $_GET[$varController] = array_shift($paths); } } // 获取操作 if(!defined('BIND_ACTION')){ $_GET[$varAction] = array_shift($paths); } // 解析剩余的URL参数 $var = array(); if(C('URL_PARAMS_BIND') && 1 == C('URL_PARAMS_BIND_TYPE')){ // URL参数按顺序绑定变量 $var = $paths; }else{ preg_replace_callback('/(\w+)\/([^\/]+)/', function($match) use(&$var){$var[$match[1]]=strip_tags($match[2]);}, implode('/',$paths)); } $_GET = array_merge($var,$_GET); } // 获取控制器的命名空间(路径) define('CONTROLLER_PATH', self::getSpace($varAddon,$urlCase)); // 获取控制器和操作名 define('CONTROLLER_NAME', defined('BIND_CONTROLLER')? BIND_CONTROLLER : self::getController($varController,$urlCase)); define('ACTION_NAME', defined('BIND_ACTION')? BIND_ACTION : self::getAction($varAction,$urlCase)); // 当前控制器的UR地址 $controllerName = defined('CONTROLLER_ALIAS')? CONTROLLER_ALIAS : CONTROLLER_NAME; define('__CONTROLLER__',__MODULE__.$depr.(defined('BIND_CONTROLLER')? '': ( $urlCase ? parse_name($controllerName) : $controllerName )) ); // 当前操作的URL地址 define('__ACTION__',__CONTROLLER__.$depr.(defined('ACTION_ALIAS')?ACTION_ALIAS:ACTION_NAME)); //保证$_REQUEST正常取值 $_REQUEST = array_merge($_POST,$_GET,$_COOKIE); // -- 加了$_COOKIE. 保证哦.. } /** * 获得控制器的命名空间路径 便于插件机制访问 */ static private function getSpace($var,$urlCase) { $space = !empty($_GET[$var])?strip_tags($_GET[$var]):''; unset($_GET[$var]); return $space; } /** * 获得实际的控制器名称 */ static private function getController($var,$urlCase) { $controller = (!empty($_GET[$var])? $_GET[$var]:C('DEFAULT_CONTROLLER')); unset($_GET[$var]); if($maps = C('URL_CONTROLLER_MAP')) { if(isset($maps[strtolower($controller)])) { // 记录当前别名 define('CONTROLLER_ALIAS',strtolower($controller)); // 获取实际的控制器名 return ucfirst($maps[CONTROLLER_ALIAS]); }elseif(array_search(strtolower($controller),$maps)){ // 禁止访问原始控制器 return ''; } } if($urlCase) { // URL地址不区分大小写 // 智能识别方式 user_type 识别到 UserTypeController 控制器 $controller = parse_name($controller,1); } return strip_tags(ucfirst($controller)); } /** * 获得实际的操作名称 */ static private function getAction($var,$urlCase) { $action = !empty($_POST[$var]) ? $_POST[$var] : (!empty($_GET[$var])?$_GET[$var]:C('DEFAULT_ACTION')); unset($_POST[$var],$_GET[$var]); if($maps = C('URL_ACTION_MAP')) { if(isset($maps[strtolower(CONTROLLER_NAME)])) { $maps = $maps[strtolower(CONTROLLER_NAME)]; if(isset($maps[strtolower($action)])) { // 记录当前别名 define('ACTION_ALIAS',strtolower($action)); // 获取实际的操作名 if(is_array($maps[ACTION_ALIAS])){ parse_str($maps[ACTION_ALIAS][1],$vars); $_GET = array_merge($_GET,$vars); return $maps[ACTION_ALIAS][0]; }else{ return $maps[ACTION_ALIAS]; } }elseif(array_search(strtolower($action),$maps)){ // 禁止访问原始操作 return ''; } } } return strip_tags( $urlCase? strtolower($action) : $action ); } /** * 获得实际的模块名称 */ static private function getModule($var) { $module = (!empty($_GET[$var])?$_GET[$var]:C('DEFAULT_MODULE')); unset($_GET[$var]); if($maps = C('URL_MODULE_MAP')) { if(isset($maps[strtolower($module)])) { // 记录当前别名 define('MODULE_ALIAS',strtolower($module)); // 获取实际的模块名 return ucfirst($maps[MODULE_ALIAS]); }elseif(array_search(strtolower($module),$maps)){ // 禁止访问原始模块 return ''; } } return strip_tags(ucfirst($module)); } } ================================================ FILE: ThinkPHP/Library/Think/Exception.class.php ================================================ // +---------------------------------------------------------------------- namespace Think; /** * ThinkPHP系统异常基类 */ class Exception extends \Exception { } ================================================ FILE: ThinkPHP/Library/Think/Hook.class.php ================================================ // +---------------------------------------------------------------------- namespace Think; /** * ThinkPHP系统钩子实现 */ class Hook { static private $tags = array(); /** * 动态添加插件到某个标签 * @param string $tag 标签名称 * @param mixed $name 插件名称 * @return void */ static public function add($tag,$name) { if(!isset(self::$tags[$tag])){ self::$tags[$tag] = array(); } if(is_array($name)){ self::$tags[$tag] = array_merge(self::$tags[$tag],$name); }else{ self::$tags[$tag][] = $name; } } /** * 批量导入插件 * @param array $data 插件信息 * @param boolean $recursive 是否递归合并 * @return void */ static public function import($data,$recursive=true) { if(!$recursive){ // 覆盖导入 self::$tags = array_merge(self::$tags,$data); }else{ // 合并导入 foreach ($data as $tag=>$val){ if(!isset(self::$tags[$tag])) self::$tags[$tag] = array(); if(!empty($val['_overlay'])){ // 可以针对某个标签指定覆盖模式 unset($val['_overlay']); self::$tags[$tag] = $val; }else{ // 合并模式 self::$tags[$tag] = array_merge(self::$tags[$tag],$val); } } } } /** * 获取插件信息 * @param string $tag 插件位置 留空获取全部 * @return array */ static public function get($tag='') { if(empty($tag)){ // 获取全部的插件信息 return self::$tags; }else{ return self::$tags[$tag]; } } /** * 监听标签的插件 * @param string $tag 标签名称 * @param mixed $params 传入参数 * @return void */ static public function listen($tag, &$params=NULL) { if(isset(self::$tags[$tag])) { if(APP_DEBUG) { G($tag.'Start'); trace('[ '.$tag.' ] --START--','','INFO'); } foreach (self::$tags[$tag] as $name) { APP_DEBUG && G($name.'_start'); $result = self::exec($name, $tag,$params); if(APP_DEBUG){ G($name.'_end'); trace('Run '.$name.' [ RunTime:'.G($name.'_start',$name.'_end',6).'s ]','','INFO'); } if(false === $result) { // 如果返回false 则中断插件执行 return ; } } if(APP_DEBUG) { // 记录行为的执行日志 trace('[ '.$tag.' ] --END-- [ RunTime:'.G($tag.'Start',$tag.'End',6).'s ]','','INFO'); } } return; } /** * 执行某个插件 * @param string $name 插件名称 * @param string $tag 方法名(标签名) * @param Mixed $params 传入的参数 * @return void */ static public function exec($name, $tag,&$params=NULL) { if('Behavior' == substr($name,-8) ){ // 行为扩展必须用run入口方法 $tag = 'run'; } $addon = new $name(); return $addon->$tag($params); } } ================================================ FILE: ThinkPHP/Library/Think/Image/Driver/GIF.class.php ================================================ // +---------------------------------------------------------------------- // | GIF.class.php 2013-03-09 // +---------------------------------------------------------------------- namespace Think\Image\Driver; class GIF{ /** * GIF帧列表 * @var array */ private $frames = array(); /** * 每帧等待时间列表 * @var array */ private $delays = array(); /** * 构造方法,用于解码GIF图片 * @param string $src GIF图片数据 * @param string $mod 图片数据类型 */ public function __construct($src = null, $mod = 'url') { if(!is_null($src)){ if('url' == $mod && is_file($src)){ $src = file_get_contents($src); } /* 解码GIF图片 */ try{ $de = new GIFDecoder($src); $this->frames = $de->GIFGetFrames(); $this->delays = $de->GIFGetDelays(); } catch(\Exception $e){ E("解码GIF图片出错"); } } } /** * 设置或获取当前帧的数据 * @param string $stream 二进制数据流 * @return boolean 获取到的数据 */ public function image($stream = null){ if(is_null($stream)){ $current = current($this->frames); return false === $current ? reset($this->frames) : $current; } else { $this->frames[key($this->frames)] = $stream; } } /** * 将当前帧移动到下一帧 * @return string 当前帧数据 */ public function nextImage(){ return next($this->frames); } /** * 编码并保存当前GIF图片 * @param string $gifname 图片名称 */ public function save($gifname){ $gif = new GIFEncoder($this->frames, $this->delays, 0, 2, 0, 0, 0, 'bin'); file_put_contents($gifname, $gif->GetAnimation()); } } /* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: :: :: GIFEncoder Version 2.0 by László Zsidi, http://gifs.hu :: :: This class is a rewritten 'GifMerge.class.php' version. :: :: Modification: :: - Simplified and easy code, :: - Ultra fast encoding, :: - Built-in errors, :: - Stable working :: :: :: Updated at 2007. 02. 13. '00.05.AM' :: :: :: :: Try on-line GIFBuilder Form demo based on GIFEncoder. :: :: http://gifs.hu/phpclasses/demos/GifBuilder/ :: ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */ Class GIFEncoder { private $GIF = "GIF89a"; /* GIF header 6 bytes */ private $VER = "GIFEncoder V2.05"; /* Encoder version */ private $BUF = Array ( ); private $LOP = 0; private $DIS = 2; private $COL = -1; private $IMG = -1; private $ERR = Array ( 'ERR00' => "Does not supported function for only one image!", 'ERR01' => "Source is not a GIF image!", 'ERR02' => "Unintelligible flag ", 'ERR03' => "Does not make animation from animated GIF source", ); /* ::::::::::::::::::::::::::::::::::::::::::::::::::: :: :: GIFEncoder... :: */ public function __construct($GIF_src, $GIF_dly, $GIF_lop, $GIF_dis,$GIF_red, $GIF_grn, $GIF_blu, $GIF_mod) { if ( ! is_array ( $GIF_src ) && ! is_array ( $GIF_dly ) ) { printf ( "%s: %s", $this->VER, $this->ERR [ 'ERR00' ] ); exit ( 0 ); } $this->LOP = ( $GIF_lop > -1 ) ? $GIF_lop : 0; $this->DIS = ( $GIF_dis > -1 ) ? ( ( $GIF_dis < 3 ) ? $GIF_dis : 3 ) : 2; $this->COL = ( $GIF_red > -1 && $GIF_grn > -1 && $GIF_blu > -1 ) ? ( $GIF_red | ( $GIF_grn << 8 ) | ( $GIF_blu << 16 ) ) : -1; for ( $i = 0; $i < count ( $GIF_src ); $i++ ) { if ( strToLower ( $GIF_mod ) == "url" ) { $this->BUF [ ] = fread ( fopen ( $GIF_src [ $i ], "rb" ), filesize ( $GIF_src [ $i ] ) ); } else if ( strToLower ( $GIF_mod ) == "bin" ) { $this->BUF [ ] = $GIF_src [ $i ]; } else { printf ( "%s: %s ( %s )!", $this->VER, $this->ERR [ 'ERR02' ], $GIF_mod ); exit ( 0 ); } if ( substr ( $this->BUF [ $i ], 0, 6 ) != "GIF87a" && substr ( $this->BUF [ $i ], 0, 6 ) != "GIF89a" ) { printf ( "%s: %d %s", $this->VER, $i, $this->ERR [ 'ERR01' ] ); exit ( 0 ); } for ( $j = ( 13 + 3 * ( 2 << ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 ) ) ), $k = TRUE; $k; $j++ ) { switch ( $this->BUF [ $i ] { $j } ) { case "!": if ( ( substr ( $this->BUF [ $i ], ( $j + 3 ), 8 ) ) == "NETSCAPE" ) { printf ( "%s: %s ( %s source )!", $this->VER, $this->ERR [ 'ERR03' ], ( $i + 1 ) ); exit ( 0 ); } break; case ";": $k = FALSE; break; } } } $this->GIFAddHeader ( ); for ( $i = 0; $i < count ( $this->BUF ); $i++ ) { $this->GIFAddFrames ( $i, $GIF_dly [ $i ] ); } $this->GIFAddFooter ( ); } /* ::::::::::::::::::::::::::::::::::::::::::::::::::: :: :: GIFAddHeader... :: */ private function GIFAddHeader ( ) { $cmap = 0; if ( ord ( $this->BUF [ 0 ] { 10 } ) & 0x80 ) { $cmap = 3 * ( 2 << ( ord ( $this->BUF [ 0 ] { 10 } ) & 0x07 ) ); $this->GIF .= substr ( $this->BUF [ 0 ], 6, 7 ); $this->GIF .= substr ( $this->BUF [ 0 ], 13, $cmap ); $this->GIF .= "!\377\13NETSCAPE2.0\3\1" . $this->GIFWord ( $this->LOP ) . "\0"; } } /* ::::::::::::::::::::::::::::::::::::::::::::::::::: :: :: GIFAddFrames... :: */ private function GIFAddFrames ( $i, $d ) { $Locals_str = 13 + 3 * ( 2 << ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 ) ); $Locals_end = strlen ( $this->BUF [ $i ] ) - $Locals_str - 1; $Locals_tmp = substr ( $this->BUF [ $i ], $Locals_str, $Locals_end ); $Global_len = 2 << ( ord ( $this->BUF [ 0 ] { 10 } ) & 0x07 ); $Locals_len = 2 << ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 ); $Global_rgb = substr ( $this->BUF [ 0 ], 13, 3 * ( 2 << ( ord ( $this->BUF [ 0 ] { 10 } ) & 0x07 ) ) ); $Locals_rgb = substr ( $this->BUF [ $i ], 13, 3 * ( 2 << ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 ) ) ); $Locals_ext = "!\xF9\x04" . chr ( ( $this->DIS << 2 ) + 0 ) . chr ( ( $d >> 0 ) & 0xFF ) . chr ( ( $d >> 8 ) & 0xFF ) . "\x0\x0"; if ( $this->COL > -1 && ord ( $this->BUF [ $i ] { 10 } ) & 0x80 ) { for ( $j = 0; $j < ( 2 << ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 ) ); $j++ ) { if ( ord ( $Locals_rgb { 3 * $j + 0 } ) == ( ( $this->COL >> 16 ) & 0xFF ) && ord ( $Locals_rgb { 3 * $j + 1 } ) == ( ( $this->COL >> 8 ) & 0xFF ) && ord ( $Locals_rgb { 3 * $j + 2 } ) == ( ( $this->COL >> 0 ) & 0xFF ) ) { $Locals_ext = "!\xF9\x04" . chr ( ( $this->DIS << 2 ) + 1 ) . chr ( ( $d >> 0 ) & 0xFF ) . chr ( ( $d >> 8 ) & 0xFF ) . chr ( $j ) . "\x0"; break; } } } switch ( $Locals_tmp { 0 } ) { case "!": $Locals_img = substr ( $Locals_tmp, 8, 10 ); $Locals_tmp = substr ( $Locals_tmp, 18, strlen ( $Locals_tmp ) - 18 ); break; case ",": $Locals_img = substr ( $Locals_tmp, 0, 10 ); $Locals_tmp = substr ( $Locals_tmp, 10, strlen ( $Locals_tmp ) - 10 ); break; } if ( ord ( $this->BUF [ $i ] { 10 } ) & 0x80 && $this->IMG > -1 ) { if ( $Global_len == $Locals_len ) { if ( $this->GIFBlockCompare ( $Global_rgb, $Locals_rgb, $Global_len ) ) { $this->GIF .= ( $Locals_ext . $Locals_img . $Locals_tmp ); } else { $byte = ord ( $Locals_img { 9 } ); $byte |= 0x80; $byte &= 0xF8; $byte |= ( ord ( $this->BUF [ 0 ] { 10 } ) & 0x07 ); $Locals_img { 9 } = chr ( $byte ); $this->GIF .= ( $Locals_ext . $Locals_img . $Locals_rgb . $Locals_tmp ); } } else { $byte = ord ( $Locals_img { 9 } ); $byte |= 0x80; $byte &= 0xF8; $byte |= ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 ); $Locals_img { 9 } = chr ( $byte ); $this->GIF .= ( $Locals_ext . $Locals_img . $Locals_rgb . $Locals_tmp ); } } else { $this->GIF .= ( $Locals_ext . $Locals_img . $Locals_tmp ); } $this->IMG = 1; } /* ::::::::::::::::::::::::::::::::::::::::::::::::::: :: :: GIFAddFooter... :: */ private function GIFAddFooter ( ) { $this->GIF .= ";"; } /* ::::::::::::::::::::::::::::::::::::::::::::::::::: :: :: GIFBlockCompare... :: */ private function GIFBlockCompare ( $GlobalBlock, $LocalBlock, $Len ) { for ( $i = 0; $i < $Len; $i++ ) { if ( $GlobalBlock { 3 * $i + 0 } != $LocalBlock { 3 * $i + 0 } || $GlobalBlock { 3 * $i + 1 } != $LocalBlock { 3 * $i + 1 } || $GlobalBlock { 3 * $i + 2 } != $LocalBlock { 3 * $i + 2 } ) { return ( 0 ); } } return ( 1 ); } /* ::::::::::::::::::::::::::::::::::::::::::::::::::: :: :: GIFWord... :: */ private function GIFWord ( $int ) { return ( chr ( $int & 0xFF ) . chr ( ( $int >> 8 ) & 0xFF ) ); } /* ::::::::::::::::::::::::::::::::::::::::::::::::::: :: :: GetAnimation... :: */ public function GetAnimation ( ) { return ( $this->GIF ); } } /* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: :: :: GIFDecoder Version 2.0 by László Zsidi, http://gifs.hu :: :: Created at 2007. 02. 01. '07.47.AM' :: :: :: :: :: Try on-line GIFBuilder Form demo based on GIFDecoder. :: :: http://gifs.hu/phpclasses/demos/GifBuilder/ :: ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */ Class GIFDecoder { private $GIF_buffer = Array ( ); private $GIF_arrays = Array ( ); private $GIF_delays = Array ( ); private $GIF_stream = ""; private $GIF_string = ""; private $GIF_bfseek = 0; private $GIF_screen = Array ( ); private $GIF_global = Array ( ); private $GIF_sorted; private $GIF_colorS; private $GIF_colorC; private $GIF_colorF; /* ::::::::::::::::::::::::::::::::::::::::::::::::::: :: :: GIFDecoder ( $GIF_pointer ) :: */ public function __construct ( $GIF_pointer ) { $this->GIF_stream = $GIF_pointer; $this->GIFGetByte ( 6 ); // GIF89a $this->GIFGetByte ( 7 ); // Logical Screen Descriptor $this->GIF_screen = $this->GIF_buffer; $this->GIF_colorF = $this->GIF_buffer [ 4 ] & 0x80 ? 1 : 0; $this->GIF_sorted = $this->GIF_buffer [ 4 ] & 0x08 ? 1 : 0; $this->GIF_colorC = $this->GIF_buffer [ 4 ] & 0x07; $this->GIF_colorS = 2 << $this->GIF_colorC; if ( $this->GIF_colorF == 1 ) { $this->GIFGetByte ( 3 * $this->GIF_colorS ); $this->GIF_global = $this->GIF_buffer; } /* * * 05.06.2007. * Made a little modification * * - for ( $cycle = 1; $cycle; ) { + if ( GIFDecoder::GIFGetByte ( 1 ) ) { - switch ( $this->GIF_buffer [ 0 ] ) { - case 0x21: - GIFDecoder::GIFReadExtensions ( ); - break; - case 0x2C: - GIFDecoder::GIFReadDescriptor ( ); - break; - case 0x3B: - $cycle = 0; - break; - } - } + else { + $cycle = 0; + } - } */ for ( $cycle = 1; $cycle; ) { if ( $this->GIFGetByte ( 1 ) ) { switch ( $this->GIF_buffer [ 0 ] ) { case 0x21: $this->GIFReadExtensions ( ); break; case 0x2C: $this->GIFReadDescriptor ( ); break; case 0x3B: $cycle = 0; break; } } else { $cycle = 0; } } } /* ::::::::::::::::::::::::::::::::::::::::::::::::::: :: :: GIFReadExtension ( ) :: */ private function GIFReadExtensions ( ) { $this->GIFGetByte ( 1 ); for ( ; ; ) { $this->GIFGetByte ( 1 ); if ( ( $u = $this->GIF_buffer [ 0 ] ) == 0x00 ) { break; } $this->GIFGetByte ( $u ); /* * 07.05.2007. * Implemented a new line for a new function * to determine the originaly delays between * frames. * */ if ( $u == 4 ) { $this->GIF_delays [ ] = ( $this->GIF_buffer [ 1 ] | $this->GIF_buffer [ 2 ] << 8 ); } } } /* ::::::::::::::::::::::::::::::::::::::::::::::::::: :: :: GIFReadExtension ( ) :: */ private function GIFReadDescriptor ( ) { $GIF_screen = Array ( ); $this->GIFGetByte ( 9 ); $GIF_screen = $this->GIF_buffer; $GIF_colorF = $this->GIF_buffer [ 8 ] & 0x80 ? 1 : 0; if ( $GIF_colorF ) { $GIF_code = $this->GIF_buffer [ 8 ] & 0x07; $GIF_sort = $this->GIF_buffer [ 8 ] & 0x20 ? 1 : 0; } else { $GIF_code = $this->GIF_colorC; $GIF_sort = $this->GIF_sorted; } $GIF_size = 2 << $GIF_code; $this->GIF_screen [ 4 ] &= 0x70; $this->GIF_screen [ 4 ] |= 0x80; $this->GIF_screen [ 4 ] |= $GIF_code; if ( $GIF_sort ) { $this->GIF_screen [ 4 ] |= 0x08; } $this->GIF_string = "GIF87a"; $this->GIFPutByte ( $this->GIF_screen ); if ( $GIF_colorF == 1 ) { $this->GIFGetByte ( 3 * $GIF_size ); $this->GIFPutByte ( $this->GIF_buffer ); } else { $this->GIFPutByte ( $this->GIF_global ); } $this->GIF_string .= chr ( 0x2C ); $GIF_screen [ 8 ] &= 0x40; $this->GIFPutByte ( $GIF_screen ); $this->GIFGetByte ( 1 ); $this->GIFPutByte ( $this->GIF_buffer ); for ( ; ; ) { $this->GIFGetByte ( 1 ); $this->GIFPutByte ( $this->GIF_buffer ); if ( ( $u = $this->GIF_buffer [ 0 ] ) == 0x00 ) { break; } $this->GIFGetByte ( $u ); $this->GIFPutByte ( $this->GIF_buffer ); } $this->GIF_string .= chr ( 0x3B ); /* Add frames into $GIF_stream array... */ $this->GIF_arrays [ ] = $this->GIF_string; } /* ::::::::::::::::::::::::::::::::::::::::::::::::::: :: :: GIFGetByte ( $len ) :: */ /* * * 05.06.2007. * Made a little modification * * - function GIFGetByte ( $len ) { - $this->GIF_buffer = Array ( ); - - for ( $i = 0; $i < $len; $i++ ) { + if ( $this->GIF_bfseek > strlen ( $this->GIF_stream ) ) { + return 0; + } - $this->GIF_buffer [ ] = ord ( $this->GIF_stream { $this->GIF_bfseek++ } ); - } + return 1; - } */ private function GIFGetByte ( $len ) { $this->GIF_buffer = Array ( ); for ( $i = 0; $i < $len; $i++ ) { if ( $this->GIF_bfseek > strlen ( $this->GIF_stream ) ) { return 0; } $this->GIF_buffer [ ] = ord ( $this->GIF_stream { $this->GIF_bfseek++ } ); } return 1; } /* ::::::::::::::::::::::::::::::::::::::::::::::::::: :: :: GIFPutByte ( $bytes ) :: */ private function GIFPutByte ( $bytes ) { for ( $i = 0; $i < count ( $bytes ); $i++ ) { $this->GIF_string .= chr ( $bytes [ $i ] ); } } /* ::::::::::::::::::::::::::::::::::::::::::::::::::: :: :: PUBLIC FUNCTIONS :: :: :: GIFGetFrames ( ) :: */ public function GIFGetFrames ( ) { return ( $this->GIF_arrays ); } /* ::::::::::::::::::::::::::::::::::::::::::::::::::: :: :: GIFGetDelays ( ) :: */ public function GIFGetDelays ( ) { return ( $this->GIF_delays ); } } ================================================ FILE: ThinkPHP/Library/Think/Image/Driver/Gd.class.php ================================================ // +---------------------------------------------------------------------- // | ImageGd.class.php 2013-03-05 // +---------------------------------------------------------------------- namespace Think\Image\Driver; use Think\Image; class Gd{ /** * 图像资源对象 * @var resource */ private $img; /** * 图像信息,包括width,height,type,mime,size * @var array */ private $info; /** * 构造方法,可用于打开一张图像 * @param string $imgname 图像路径 */ public function __construct($imgname = null) { $imgname && $this->open($imgname); } /** * 打开一张图像 * @param string $imgname 图像路径 */ public function open($imgname){ //检测图像文件 if(!is_file($imgname)) E('不存在的图像文件'); //获取图像信息 $info = getimagesize($imgname); //检测图像合法性 if(false === $info || (IMAGETYPE_GIF === $info[2] && empty($info['bits']))){ E('非法图像文件'); } //设置图像信息 $this->info = array( 'width' => $info[0], 'height' => $info[1], 'type' => image_type_to_extension($info[2], false), 'mime' => $info['mime'], ); //销毁已存在的图像 empty($this->img) || imagedestroy($this->img); //打开图像 if('gif' == $this->info['type']){ $class = 'Think\\Image\\Driver\\GIF'; $this->gif = new $class($imgname); $this->img = imagecreatefromstring($this->gif->image()); } else { $fun = "imagecreatefrom{$this->info['type']}"; $this->img = $fun($imgname); } } /** * 保存图像 * @param string $imgname 图像保存名称 * @param string $type 图像类型 * @param integer $quality 图像质量 * @param boolean $interlace 是否对JPEG类型图像设置隔行扫描 */ public function save($imgname, $type = null, $quality=80,$interlace = true){ if(empty($this->img)) E('没有可以被保存的图像资源'); //自动获取图像类型 if(is_null($type)){ $type = $this->info['type']; } else { $type = strtolower($type); } //保存图像 if('jpeg' == $type || 'jpg' == $type){ //JPEG图像设置隔行扫描 imageinterlace($this->img, $interlace); imagejpeg($this->img, $imgname,$quality); }elseif('gif' == $type && !empty($this->gif)){ $this->gif->save($imgname); }else{ $fun = 'image'.$type; $fun($this->img, $imgname); } } /** * 返回图像宽度 * @return integer 图像宽度 */ public function width(){ if(empty($this->img)) E('没有指定图像资源'); return $this->info['width']; } /** * 返回图像高度 * @return integer 图像高度 */ public function height(){ if(empty($this->img)) E('没有指定图像资源'); return $this->info['height']; } /** * 返回图像类型 * @return string 图像类型 */ public function type(){ if(empty($this->img)) E('没有指定图像资源'); return $this->info['type']; } /** * 返回图像MIME类型 * @return string 图像MIME类型 */ public function mime(){ if(empty($this->img)) E('没有指定图像资源'); return $this->info['mime']; } /** * 返回图像尺寸数组 0 - 图像宽度,1 - 图像高度 * @return array 图像尺寸 */ public function size(){ if(empty($this->img)) E('没有指定图像资源'); return array($this->info['width'], $this->info['height']); } /** * 裁剪图像 * @param integer $w 裁剪区域宽度 * @param integer $h 裁剪区域高度 * @param integer $x 裁剪区域x坐标 * @param integer $y 裁剪区域y坐标 * @param integer $width 图像保存宽度 * @param integer $height 图像保存高度 */ public function crop($w, $h, $x = 0, $y = 0, $width = null, $height = null){ if(empty($this->img)) E('没有可以被裁剪的图像资源'); //设置保存尺寸 empty($width) && $width = $w; empty($height) && $height = $h; do { //创建新图像 $img = imagecreatetruecolor($width, $height); // 调整默认颜色 $color = imagecolorallocate($img, 255, 255, 255); imagefill($img, 0, 0, $color); //裁剪 imagecopyresampled($img, $this->img, 0, 0, $x, $y, $width, $height, $w, $h); imagedestroy($this->img); //销毁原图 //设置新图像 $this->img = $img; } while(!empty($this->gif) && $this->gifNext()); $this->info['width'] = $width; $this->info['height'] = $height; } /** * 生成缩略图 * @param integer $width 缩略图最大宽度 * @param integer $height 缩略图最大高度 * @param integer $type 缩略图裁剪类型 */ public function thumb($width, $height, $type = Image::IMAGE_THUMB_SCALE){ if(empty($this->img)) E('没有可以被缩略的图像资源'); //原图宽度和高度 $w = $this->info['width']; $h = $this->info['height']; /* 计算缩略图生成的必要参数 */ switch ($type) { /* 等比例缩放 */ case Image::IMAGE_THUMB_SCALE: //原图尺寸小于缩略图尺寸则不进行缩略 if($w < $width && $h < $height) return; //计算缩放比例 $scale = min($width/$w, $height/$h); //设置缩略图的坐标及宽度和高度 $x = $y = 0; $width = $w * $scale; $height = $h * $scale; break; /* 居中裁剪 */ case Image::IMAGE_THUMB_CENTER: //计算缩放比例 $scale = max($width/$w, $height/$h); //设置缩略图的坐标及宽度和高度 $w = $width/$scale; $h = $height/$scale; $x = ($this->info['width'] - $w)/2; $y = ($this->info['height'] - $h)/2; break; /* 左上角裁剪 */ case Image::IMAGE_THUMB_NORTHWEST: //计算缩放比例 $scale = max($width/$w, $height/$h); //设置缩略图的坐标及宽度和高度 $x = $y = 0; $w = $width/$scale; $h = $height/$scale; break; /* 右下角裁剪 */ case Image::IMAGE_THUMB_SOUTHEAST: //计算缩放比例 $scale = max($width/$w, $height/$h); //设置缩略图的坐标及宽度和高度 $w = $width/$scale; $h = $height/$scale; $x = $this->info['width'] - $w; $y = $this->info['height'] - $h; break; /* 填充 */ case Image::IMAGE_THUMB_FILLED: //计算缩放比例 if($w < $width && $h < $height){ $scale = 1; } else { $scale = min($width/$w, $height/$h); } //设置缩略图的坐标及宽度和高度 $neww = $w * $scale; $newh = $h * $scale; $posx = ($width - $w * $scale)/2; $posy = ($height - $h * $scale)/2; do{ //创建新图像 $img = imagecreatetruecolor($width, $height); // 调整默认颜色 $color = imagecolorallocate($img, 255, 255, 255); imagefill($img, 0, 0, $color); //裁剪 imagecopyresampled($img, $this->img, $posx, $posy, $x, $y, $neww, $newh, $w, $h); imagedestroy($this->img); //销毁原图 $this->img = $img; } while(!empty($this->gif) && $this->gifNext()); $this->info['width'] = $width; $this->info['height'] = $height; return; /* 固定 */ case Image::IMAGE_THUMB_FIXED: $x = $y = 0; break; default: E('不支持的缩略图裁剪类型'); } /* 裁剪图像 */ $this->crop($w, $h, $x, $y, $width, $height); } /** * 添加水印 * @param string $source 水印图片路径 * @param integer $locate 水印位置 * @param integer $alpha 水印透明度 */ public function water($source, $locate = Image::IMAGE_WATER_SOUTHEAST,$alpha=80){ //资源检测 if(empty($this->img)) E('没有可以被添加水印的图像资源'); if(!is_file($source)) E('水印图像不存在'); //获取水印图像信息 $info = getimagesize($source); if(false === $info || (IMAGETYPE_GIF === $info[2] && empty($info['bits']))){ E('非法水印文件'); } //创建水印图像资源 $fun = 'imagecreatefrom' . image_type_to_extension($info[2], false); $water = $fun($source); //设定水印图像的混色模式 imagealphablending($water, true); /* 设定水印位置 */ switch ($locate) { /* 右下角水印 */ case Image::IMAGE_WATER_SOUTHEAST: $x = $this->info['width'] - $info[0]; $y = $this->info['height'] - $info[1]; break; /* 左下角水印 */ case Image::IMAGE_WATER_SOUTHWEST: $x = 0; $y = $this->info['height'] - $info[1]; break; /* 左上角水印 */ case Image::IMAGE_WATER_NORTHWEST: $x = $y = 0; break; /* 右上角水印 */ case Image::IMAGE_WATER_NORTHEAST: $x = $this->info['width'] - $info[0]; $y = 0; break; /* 居中水印 */ case Image::IMAGE_WATER_CENTER: $x = ($this->info['width'] - $info[0])/2; $y = ($this->info['height'] - $info[1])/2; break; /* 下居中水印 */ case Image::IMAGE_WATER_SOUTH: $x = ($this->info['width'] - $info[0])/2; $y = $this->info['height'] - $info[1]; break; /* 右居中水印 */ case Image::IMAGE_WATER_EAST: $x = $this->info['width'] - $info[0]; $y = ($this->info['height'] - $info[1])/2; break; /* 上居中水印 */ case Image::IMAGE_WATER_NORTH: $x = ($this->info['width'] - $info[0])/2; $y = 0; break; /* 左居中水印 */ case Image::IMAGE_WATER_WEST: $x = 0; $y = ($this->info['height'] - $info[1])/2; break; default: /* 自定义水印坐标 */ if(is_array($locate)){ list($x, $y) = $locate; } else { E('不支持的水印位置类型'); } } do{ //添加水印 $src = imagecreatetruecolor($info[0], $info[1]); // 调整默认颜色 $color = imagecolorallocate($src, 255, 255, 255); imagefill($src, 0, 0, $color); imagecopy($src, $this->img, 0, 0, $x, $y, $info[0], $info[1]); imagecopy($src, $water, 0, 0, 0, 0, $info[0], $info[1]); imagecopymerge($this->img, $src, $x, $y, 0, 0, $info[0], $info[1], $alpha); //销毁零时图片资源 imagedestroy($src); } while(!empty($this->gif) && $this->gifNext()); //销毁水印资源 imagedestroy($water); } /** * 图像添加文字 * @param string $text 添加的文字 * @param string $font 字体路径 * @param integer $size 字号 * @param string $color 文字颜色 * @param integer $locate 文字写入位置 * @param integer $offset 文字相对当前位置的偏移量 * @param integer $angle 文字倾斜角度 */ public function text($text, $font, $size, $color = '#00000000', $locate = Image::IMAGE_WATER_SOUTHEAST, $offset = 0, $angle = 0){ //资源检测 if(empty($this->img)) E('没有可以被写入文字的图像资源'); if(!is_file($font)) E("不存在的字体文件:{$font}"); //获取文字信息 $info = imagettfbbox($size, $angle, $font, $text); $minx = min($info[0], $info[2], $info[4], $info[6]); $maxx = max($info[0], $info[2], $info[4], $info[6]); $miny = min($info[1], $info[3], $info[5], $info[7]); $maxy = max($info[1], $info[3], $info[5], $info[7]); /* 计算文字初始坐标和尺寸 */ $x = $minx; $y = abs($miny); $w = $maxx - $minx; $h = $maxy - $miny; /* 设定文字位置 */ switch ($locate) { /* 右下角文字 */ case Image::IMAGE_WATER_SOUTHEAST: $x += $this->info['width'] - $w; $y += $this->info['height'] - $h; break; /* 左下角文字 */ case Image::IMAGE_WATER_SOUTHWEST: $y += $this->info['height'] - $h; break; /* 左上角文字 */ case Image::IMAGE_WATER_NORTHWEST: // 起始坐标即为左上角坐标,无需调整 break; /* 右上角文字 */ case Image::IMAGE_WATER_NORTHEAST: $x += $this->info['width'] - $w; break; /* 居中文字 */ case Image::IMAGE_WATER_CENTER: $x += ($this->info['width'] - $w)/2; $y += ($this->info['height'] - $h)/2; break; /* 下居中文字 */ case Image::IMAGE_WATER_SOUTH: $x += ($this->info['width'] - $w)/2; $y += $this->info['height'] - $h; break; /* 右居中文字 */ case Image::IMAGE_WATER_EAST: $x += $this->info['width'] - $w; $y += ($this->info['height'] - $h)/2; break; /* 上居中文字 */ case Image::IMAGE_WATER_NORTH: $x += ($this->info['width'] - $w)/2; break; /* 左居中文字 */ case Image::IMAGE_WATER_WEST: $y += ($this->info['height'] - $h)/2; break; default: /* 自定义文字坐标 */ if(is_array($locate)){ list($posx, $posy) = $locate; $x += $posx; $y += $posy; } else { E('不支持的文字位置类型'); } } /* 设置偏移量 */ if(is_array($offset)){ $offset = array_map('intval', $offset); list($ox, $oy) = $offset; } else{ $offset = intval($offset); $ox = $oy = $offset; } /* 设置颜色 */ if(is_string($color) && 0 === strpos($color, '#')){ $color = str_split(substr($color, 1), 2); $color = array_map('hexdec', $color); if(empty($color[3]) || $color[3] > 127){ $color[3] = 0; } } elseif (!is_array($color)) { E('错误的颜色值'); } do{ /* 写入文字 */ $col = imagecolorallocatealpha($this->img, $color[0], $color[1], $color[2], $color[3]); imagettftext($this->img, $size, $angle, $x + $ox, $y + $oy, $col, $font, $text); } while(!empty($this->gif) && $this->gifNext()); } /* 切换到GIF的下一帧并保存当前帧,内部使用 */ private function gifNext(){ ob_start(); ob_implicit_flush(0); imagegif($this->img); $img = ob_get_clean(); $this->gif->image($img); $next = $this->gif->nextImage(); if($next){ $this->img = imagecreatefromstring($next); return $next; } else { $this->img = imagecreatefromstring($this->gif->image()); return false; } } /** * 析构方法,用于销毁图像资源 */ public function __destruct() { empty($this->img) || imagedestroy($this->img); } } ================================================ FILE: ThinkPHP/Library/Think/Image/Driver/Imagick.class.php ================================================ // +---------------------------------------------------------------------- // | ImageImagick.class.php 2013-03-06 // +---------------------------------------------------------------------- namespace Think\Image\Driver; use Think\Image; class Imagick{ /** * 图像资源对象 * @var resource */ private $img; /** * 图像信息,包括width,height,type,mime,size * @var array */ private $info; /** * 构造方法,可用于打开一张图像 * @param string $imgname 图像路径 */ public function __construct($imgname = null) { $imgname && $this->open($imgname); } /** * 打开一张图像 * @param string $imgname 图像路径 */ public function open($imgname){ //检测图像文件 if(!is_file($imgname)) E('不存在的图像文件'); //销毁已存在的图像 empty($this->img) || $this->img->destroy(); //载入图像 $this->img = new \Imagick(realpath($imgname)); //设置图像信息 $this->info = array( 'width' => $this->img->getImageWidth(), 'height' => $this->img->getImageHeight(), 'type' => strtolower($this->img->getImageFormat()), 'mime' => $this->img->getImageMimeType(), ); } /** * 保存图像 * @param string $imgname 图像保存名称 * @param string $type 图像类型 * @param integer $quality JPEG图像质量 * @param boolean $interlace 是否对JPEG类型图像设置隔行扫描 */ public function save($imgname, $type = null, $quality=80,$interlace = true){ if(empty($this->img)) E('没有可以被保存的图像资源'); //设置图片类型 if(is_null($type)){ $type = $this->info['type']; } else { $type = strtolower($type); $this->img->setImageFormat($type); } //JPEG图像设置隔行扫描 if('jpeg' == $type || 'jpg' == $type){ $this->img->setImageInterlaceScheme(1); } // 设置图像质量 $this->img->setImageCompressionQuality($quality); //去除图像配置信息 $this->img->stripImage(); //保存图像 $imgname = realpath(dirname($imgname)) . '/' . basename($imgname); //强制绝对路径 if ('gif' == $type) { $this->img->writeImages($imgname, true); } else { $this->img->writeImage($imgname); } } /** * 返回图像宽度 * @return integer 图像宽度 */ public function width(){ if(empty($this->img)) E('没有指定图像资源'); return $this->info['width']; } /** * 返回图像高度 * @return integer 图像高度 */ public function height(){ if(empty($this->img)) E('没有指定图像资源'); return $this->info['height']; } /** * 返回图像类型 * @return string 图像类型 */ public function type(){ if(empty($this->img)) E('没有指定图像资源'); return $this->info['type']; } /** * 返回图像MIME类型 * @return string 图像MIME类型 */ public function mime(){ if(empty($this->img)) E('没有指定图像资源'); return $this->info['mime']; } /** * 返回图像尺寸数组 0 - 图像宽度,1 - 图像高度 * @return array 图像尺寸 */ public function size(){ if(empty($this->img)) E('没有指定图像资源'); return array($this->info['width'], $this->info['height']); } /** * 裁剪图像 * @param integer $w 裁剪区域宽度 * @param integer $h 裁剪区域高度 * @param integer $x 裁剪区域x坐标 * @param integer $y 裁剪区域y坐标 * @param integer $width 图像保存宽度 * @param integer $height 图像保存高度 */ public function crop($w, $h, $x = 0, $y = 0, $width = null, $height = null){ if(empty($this->img)) E('没有可以被裁剪的图像资源'); //设置保存尺寸 empty($width) && $width = $w; empty($height) && $height = $h; //裁剪图片 if('gif' == $this->info['type']){ $img = $this->img->coalesceImages(); $this->img->destroy(); //销毁原图 //循环裁剪每一帧 do { $this->_crop($w, $h, $x, $y, $width, $height, $img); } while ($img->nextImage()); //压缩图片 $this->img = $img->deconstructImages(); $img->destroy(); //销毁零时图片 } else { $this->_crop($w, $h, $x, $y, $width, $height); } } /* 裁剪图片,内部调用 */ private function _crop($w, $h, $x, $y, $width, $height, $img = null){ is_null($img) && $img = $this->img; //裁剪 $info = $this->info; if($x != 0 || $y != 0 || $w != $info['width'] || $h != $info['height']){ $img->cropImage($w, $h, $x, $y); $img->setImagePage($w, $h, 0, 0); //调整画布和图片一致 } //调整大小 if($w != $width || $h != $height){ $img->sampleImage($width, $height); } //设置缓存尺寸 $this->info['width'] = $w; $this->info['height'] = $h; } /** * 生成缩略图 * @param integer $width 缩略图最大宽度 * @param integer $height 缩略图最大高度 * @param integer $type 缩略图裁剪类型 */ public function thumb($width, $height, $type = Image::IMAGE_THUMB_SCALE){ if(empty($this->img)) E('没有可以被缩略的图像资源'); //原图宽度和高度 $w = $this->info['width']; $h = $this->info['height']; /* 计算缩略图生成的必要参数 */ switch ($type) { /* 等比例缩放 */ case Image::IMAGE_THUMB_SCALE: //原图尺寸小于缩略图尺寸则不进行缩略 if($w < $width && $h < $height) return; //计算缩放比例 $scale = min($width/$w, $height/$h); //设置缩略图的坐标及宽度和高度 $x = $y = 0; $width = $w * $scale; $height = $h * $scale; break; /* 居中裁剪 */ case Image::IMAGE_THUMB_CENTER: //计算缩放比例 $scale = max($width/$w, $height/$h); //设置缩略图的坐标及宽度和高度 $w = $width/$scale; $h = $height/$scale; $x = ($this->info['width'] - $w)/2; $y = ($this->info['height'] - $h)/2; break; /* 左上角裁剪 */ case Image::IMAGE_THUMB_NORTHWEST: //计算缩放比例 $scale = max($width/$w, $height/$h); //设置缩略图的坐标及宽度和高度 $x = $y = 0; $w = $width/$scale; $h = $height/$scale; break; /* 右下角裁剪 */ case Image::IMAGE_THUMB_SOUTHEAST: //计算缩放比例 $scale = max($width/$w, $height/$h); //设置缩略图的坐标及宽度和高度 $w = $width/$scale; $h = $height/$scale; $x = $this->info['width'] - $w; $y = $this->info['height'] - $h; break; /* 填充 */ case Image::IMAGE_THUMB_FILLED: //计算缩放比例 if($w < $width && $h < $height){ $scale = 1; } else { $scale = min($width/$w, $height/$h); } //设置缩略图的坐标及宽度和高度 $neww = $w * $scale; $newh = $h * $scale; $posx = ($width - $w * $scale)/2; $posy = ($height - $h * $scale)/2; //创建一张新图像 $newimg = new \Imagick(); $newimg->newImage($width, $height, 'white', $this->info['type']); if('gif' == $this->info['type']){ $imgs = $this->img->coalesceImages(); $img = new \Imagick(); $this->img->destroy(); //销毁原图 //循环填充每一帧 do { //填充图像 $image = $this->_fill($newimg, $posx, $posy, $neww, $newh, $imgs); $img->addImage($image); $img->setImageDelay($imgs->getImageDelay()); $img->setImagePage($width, $height, 0, 0); $image->destroy(); //销毁零时图片 } while ($imgs->nextImage()); //压缩图片 $this->img->destroy(); $this->img = $img->deconstructImages(); $imgs->destroy(); //销毁零时图片 $img->destroy(); //销毁零时图片 } else { //填充图像 $img = $this->_fill($newimg, $posx, $posy, $neww, $newh); //销毁原图 $this->img->destroy(); $this->img = $img; } //设置新图像属性 $this->info['width'] = $width; $this->info['height'] = $height; return; /* 固定 */ case Image::IMAGE_THUMB_FIXED: $x = $y = 0; break; default: E('不支持的缩略图裁剪类型'); } /* 裁剪图像 */ $this->crop($w, $h, $x, $y, $width, $height); } /* 填充指定图像,内部使用 */ private function _fill($newimg, $posx, $posy, $neww, $newh, $img = null){ is_null($img) && $img = $this->img; /* 将指定图片绘入空白图片 */ $draw = new \ImagickDraw(); $draw->composite($img->getImageCompose(), $posx, $posy, $neww, $newh, $img); $image = $newimg->clone(); $image->drawImage($draw); $draw->destroy(); return $image; } /** * 添加水印 * @param string $source 水印图片路径 * @param integer $locate 水印位置 * @param integer $alpha 水印透明度 */ public function water($source, $locate = Image::IMAGE_WATER_SOUTHEAST,$alpha=80){ //资源检测 if(empty($this->img)) E('没有可以被添加水印的图像资源'); if(!is_file($source)) E('水印图像不存在'); //创建水印图像资源 $water = new \Imagick(realpath($source)); $info = array($water->getImageWidth(), $water->getImageHeight()); /* 设定水印位置 */ switch ($locate) { /* 右下角水印 */ case Image::IMAGE_WATER_SOUTHEAST: $x = $this->info['width'] - $info[0]; $y = $this->info['height'] - $info[1]; break; /* 左下角水印 */ case Image::IMAGE_WATER_SOUTHWEST: $x = 0; $y = $this->info['height'] - $info[1]; break; /* 左上角水印 */ case Image::IMAGE_WATER_NORTHWEST: $x = $y = 0; break; /* 右上角水印 */ case Image::IMAGE_WATER_NORTHEAST: $x = $this->info['width'] - $info[0]; $y = 0; break; /* 居中水印 */ case Image::IMAGE_WATER_CENTER: $x = ($this->info['width'] - $info[0])/2; $y = ($this->info['height'] - $info[1])/2; break; /* 下居中水印 */ case Image::IMAGE_WATER_SOUTH: $x = ($this->info['width'] - $info[0])/2; $y = $this->info['height'] - $info[1]; break; /* 右居中水印 */ case Image::IMAGE_WATER_EAST: $x = $this->info['width'] - $info[0]; $y = ($this->info['height'] - $info[1])/2; break; /* 上居中水印 */ case Image::IMAGE_WATER_NORTH: $x = ($this->info['width'] - $info[0])/2; $y = 0; break; /* 左居中水印 */ case Image::IMAGE_WATER_WEST: $x = 0; $y = ($this->info['height'] - $info[1])/2; break; default: /* 自定义水印坐标 */ if(is_array($locate)){ list($x, $y) = $locate; } else { E('不支持的水印位置类型'); } } //创建绘图资源 $draw = new \ImagickDraw(); $draw->composite($water->getImageCompose(), $x, $y, $info[0], $info[1], $water); if('gif' == $this->info['type']){ $img = $this->img->coalesceImages(); $this->img->destroy(); //销毁原图 do{ //添加水印 $img->drawImage($draw); } while ($img->nextImage()); //压缩图片 $this->img = $img->deconstructImages(); $img->destroy(); //销毁零时图片 } else { //添加水印 $this->img->drawImage($draw); } //销毁水印资源 $draw->destroy(); $water->destroy(); } /** * 图像添加文字 * @param string $text 添加的文字 * @param string $font 字体路径 * @param integer $size 字号 * @param string $color 文字颜色 * @param integer $locate 文字写入位置 * @param integer $offset 文字相对当前位置的偏移量 * @param integer $angle 文字倾斜角度 */ public function text($text, $font, $size, $color = '#00000000', $locate = Image::IMAGE_WATER_SOUTHEAST, $offset = 0, $angle = 0){ //资源检测 if(empty($this->img)) E('没有可以被写入文字的图像资源'); if(!is_file($font)) E("不存在的字体文件:{$font}"); //获取颜色和透明度 if(is_array($color)){ $color = array_map('dechex', $color); foreach ($color as &$value) { $value = str_pad($value, 2, '0', STR_PAD_LEFT); } $color = '#' . implode('', $color); } elseif(!is_string($color) || 0 !== strpos($color, '#')) { E('错误的颜色值'); } $col = substr($color, 0, 7); $alp = strlen($color) == 9 ? substr($color, -2) : 0; //获取文字信息 $draw = new \ImagickDraw(); $draw->setFont(realpath($font)); $draw->setFontSize($size); $draw->setFillColor($col); $draw->setFillAlpha(1-hexdec($alp)/127); $draw->setTextAntialias(true); $draw->setStrokeAntialias(true); $metrics = $this->img->queryFontMetrics($draw, $text); /* 计算文字初始坐标和尺寸 */ $x = 0; $y = $metrics['ascender']; $w = $metrics['textWidth']; $h = $metrics['textHeight']; /* 设定文字位置 */ switch ($locate) { /* 右下角文字 */ case Image::IMAGE_WATER_SOUTHEAST: $x += $this->info['width'] - $w; $y += $this->info['height'] - $h; break; /* 左下角文字 */ case Image::IMAGE_WATER_SOUTHWEST: $y += $this->info['height'] - $h; break; /* 左上角文字 */ case Image::IMAGE_WATER_NORTHWEST: // 起始坐标即为左上角坐标,无需调整 break; /* 右上角文字 */ case Image::IMAGE_WATER_NORTHEAST: $x += $this->info['width'] - $w; break; /* 居中文字 */ case Image::IMAGE_WATER_CENTER: $x += ($this->info['width'] - $w)/2; $y += ($this->info['height'] - $h)/2; break; /* 下居中文字 */ case Image::IMAGE_WATER_SOUTH: $x += ($this->info['width'] - $w)/2; $y += $this->info['height'] - $h; break; /* 右居中文字 */ case Image::IMAGE_WATER_EAST: $x += $this->info['width'] - $w; $y += ($this->info['height'] - $h)/2; break; /* 上居中文字 */ case Image::IMAGE_WATER_NORTH: $x += ($this->info['width'] - $w)/2; break; /* 左居中文字 */ case Image::IMAGE_WATER_WEST: $y += ($this->info['height'] - $h)/2; break; default: /* 自定义文字坐标 */ if(is_array($locate)){ list($posx, $posy) = $locate; $x += $posx; $y += $posy; } else { E('不支持的文字位置类型'); } } /* 设置偏移量 */ if(is_array($offset)){ $offset = array_map('intval', $offset); list($ox, $oy) = $offset; } else{ $offset = intval($offset); $ox = $oy = $offset; } /* 写入文字 */ if('gif' == $this->info['type']){ $img = $this->img->coalesceImages(); $this->img->destroy(); //销毁原图 do{ $img->annotateImage($draw, $x + $ox, $y + $oy, $angle, $text); } while ($img->nextImage()); //压缩图片 $this->img = $img->deconstructImages(); $img->destroy(); //销毁零时图片 } else { $this->img->annotateImage($draw, $x + $ox, $y + $oy, $angle, $text); } $draw->destroy(); } /** * 析构方法,用于销毁图像资源 */ public function __destruct() { empty($this->img) || $this->img->destroy(); } } ================================================ FILE: ThinkPHP/Library/Think/Image.class.php ================================================ // +---------------------------------------------------------------------- // | ThinkImage.class.php 2013-03-05 // +---------------------------------------------------------------------- namespace Think; /** * 图片处理驱动类,可配置图片处理库 * 目前支持GD库和imagick * @author 麦当苗儿 */ class Image{ /* 驱动相关常量定义 */ const IMAGE_GD = 1; //常量,标识GD库类型 const IMAGE_IMAGICK = 2; //常量,标识imagick库类型 /* 缩略图相关常量定义 */ const IMAGE_THUMB_SCALE = 1 ; //常量,标识缩略图等比例缩放类型 const IMAGE_THUMB_FILLED = 2 ; //常量,标识缩略图缩放后填充类型 const IMAGE_THUMB_CENTER = 3 ; //常量,标识缩略图居中裁剪类型 const IMAGE_THUMB_NORTHWEST = 4 ; //常量,标识缩略图左上角裁剪类型 const IMAGE_THUMB_SOUTHEAST = 5 ; //常量,标识缩略图右下角裁剪类型 const IMAGE_THUMB_FIXED = 6 ; //常量,标识缩略图固定尺寸缩放类型 /* 水印相关常量定义 */ const IMAGE_WATER_NORTHWEST = 1 ; //常量,标识左上角水印 const IMAGE_WATER_NORTH = 2 ; //常量,标识上居中水印 const IMAGE_WATER_NORTHEAST = 3 ; //常量,标识右上角水印 const IMAGE_WATER_WEST = 4 ; //常量,标识左居中水印 const IMAGE_WATER_CENTER = 5 ; //常量,标识居中水印 const IMAGE_WATER_EAST = 6 ; //常量,标识右居中水印 const IMAGE_WATER_SOUTHWEST = 7 ; //常量,标识左下角水印 const IMAGE_WATER_SOUTH = 8 ; //常量,标识下居中水印 const IMAGE_WATER_SOUTHEAST = 9 ; //常量,标识右下角水印 /** * 图片资源 * @var resource */ private $img; /** * 构造方法,用于实例化一个图片处理对象 * @param string $type 要使用的类库,默认使用GD库 */ public function __construct($type = self::IMAGE_GD, $imgname = null){ /* 判断调用库的类型 */ switch ($type) { case self::IMAGE_GD: $class = 'Gd'; break; case self::IMAGE_IMAGICK: $class = 'Imagick'; break; default: E('不支持的图片处理库类型'); } /* 引入处理库,实例化图片处理对象 */ $class = "Think\\Image\\Driver\\{$class}"; $this->img = new $class($imgname); } /** * 打开一幅图像 * @param string $imgname 图片路径 * @return Object 当前图片处理库对象 */ public function open($imgname){ $this->img->open($imgname); return $this; } /** * 保存图片 * @param string $imgname 图片保存名称 * @param string $type 图片类型 * @param integer $quality 图像质量 * @param boolean $interlace 是否对JPEG类型图片设置隔行扫描 * @return Object 当前图片处理库对象 */ public function save($imgname, $type = null, $quality=80,$interlace = true){ $this->img->save($imgname, $type, $quality,$interlace); return $this; } /** * 返回图片宽度 * @return integer 图片宽度 */ public function width(){ return $this->img->width(); } /** * 返回图片高度 * @return integer 图片高度 */ public function height(){ return $this->img->height(); } /** * 返回图像类型 * @return string 图片类型 */ public function type(){ return $this->img->type(); } /** * 返回图像MIME类型 * @return string 图像MIME类型 */ public function mime(){ return $this->img->mime(); } /** * 返回图像尺寸数组 0 - 图片宽度,1 - 图片高度 * @return array 图片尺寸 */ public function size(){ return $this->img->size(); } /** * 裁剪图片 * @param integer $w 裁剪区域宽度 * @param integer $h 裁剪区域高度 * @param integer $x 裁剪区域x坐标 * @param integer $y 裁剪区域y坐标 * @param integer $width 图片保存宽度 * @param integer $height 图片保存高度 * @return Object 当前图片处理库对象 */ public function crop($w, $h, $x = 0, $y = 0, $width = null, $height = null){ $this->img->crop($w, $h, $x, $y, $width, $height); return $this; } /** * 生成缩略图 * @param integer $width 缩略图最大宽度 * @param integer $height 缩略图最大高度 * @param integer $type 缩略图裁剪类型 * @return Object 当前图片处理库对象 */ public function thumb($width, $height, $type = self::IMAGE_THUMB_SCALE){ $this->img->thumb($width, $height, $type); return $this; } /** * 添加水印 * @param string $source 水印图片路径 * @param integer $locate 水印位置 * @param integer $alpha 水印透明度 * @return Object 当前图片处理库对象 */ public function water($source, $locate = self::IMAGE_WATER_SOUTHEAST,$alpha=80){ $this->img->water($source, $locate,$alpha); return $this; } /** * 图像添加文字 * @param string $text 添加的文字 * @param string $font 字体路径 * @param integer $size 字号 * @param string $color 文字颜色 * @param integer $locate 文字写入位置 * @param integer $offset 文字相对当前位置的偏移量 * @param integer $angle 文字倾斜角度 * @return Object 当前图片处理库对象 */ public function text($text, $font, $size, $color = '#00000000', $locate = self::IMAGE_WATER_SOUTHEAST, $offset = 0, $angle = 0){ $this->img->text($text, $font, $size, $color, $locate, $offset, $angle); return $this; } } ================================================ FILE: ThinkPHP/Library/Think/Log/Driver/File.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Log\Driver; class File { protected $config = array( 'log_time_format' => ' c ', 'log_file_size' => 2097152, 'log_path' => '', ); // 实例化并传入参数 public function __construct($config=array()){ $this->config = array_merge($this->config,$config); } /** * 日志写入接口 * @access public * @param string $log 日志信息 * @param string $destination 写入目标 * @return void */ public function write($log,$destination='') { $now = date($this->config['log_time_format']); if(empty($destination)){ $destination = $this->config['log_path'].date('y_m_d').'.log'; } // 自动创建日志目录 $log_dir = dirname($destination); if (!is_dir($log_dir)) { mkdir($log_dir, 0755, true); } //检测日志文件大小,超过配置大小则备份日志文件重新生成 if(is_file($destination) && floor($this->config['log_file_size']) <= filesize($destination) ){ rename($destination,dirname($destination).'/'.time().'-'.basename($destination)); } error_log("[{$now}] ".$_SERVER['REMOTE_ADDR'].' '.$_SERVER['REQUEST_URI']."\r\n{$log}\r\n", 3,$destination); } } ================================================ FILE: ThinkPHP/Library/Think/Log/Driver/Sae.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Log\Driver; class Sae { protected $config = array( 'log_time_format' => ' c ', ); // 实例化并传入参数 public function __construct($config=array()){ $this->config = array_merge($this->config,$config); } /** * 日志写入接口 * @access public * @param string $log 日志信息 * @param string $destination 写入目标 * @return void */ public function write($log,$destination='') { static $is_debug=null; $now = date($this->config['log_time_format']); $logstr="[{$now}] ".$_SERVER['REMOTE_ADDR'].' '.$_SERVER['REQUEST_URI']."\r\n{$log}\r\n"; if(is_null($is_debug)){ preg_replace('@(\w+)\=([^;]*)@e', '$appSettings[\'\\1\']="\\2";', $_SERVER['HTTP_APPCOOKIE']); $is_debug = in_array($_SERVER['HTTP_APPVERSION'], explode(',', $appSettings['debug'])) ? true : false; } if($is_debug){ sae_set_display_errors(false);//记录日志不将日志打印出来 } sae_debug($logstr); if($is_debug){ sae_set_display_errors(true); } } } ================================================ FILE: ThinkPHP/Library/Think/Log.class.php ================================================ // +---------------------------------------------------------------------- namespace Think; /** * 日志处理类 */ class Log { // 日志级别 从上到下,由低到高 const EMERG = 'EMERG'; // 严重错误: 导致系统崩溃无法使用 const ALERT = 'ALERT'; // 警戒性错误: 必须被立即修改的错误 const CRIT = 'CRIT'; // 临界值错误: 超过临界值的错误,例如一天24小时,而输入的是25小时这样 const ERR = 'ERR'; // 一般错误: 一般性错误 const WARN = 'WARN'; // 警告性错误: 需要发出警告的错误 const NOTICE = 'NOTIC'; // 通知: 程序可以运行但是还不够完美的错误 const INFO = 'INFO'; // 信息: 程序输出信息 const DEBUG = 'DEBUG'; // 调试: 调试信息 const SQL = 'SQL'; // SQL:SQL语句 注意只在调试模式开启时有效 // 日志信息 static protected $log = array(); // 日志存储 static protected $storage = null; // 日志初始化 static public function init($config=array()){ $type = isset($config['type']) ? $config['type'] : 'File'; $class = strpos($type,'\\')? $type: 'Think\\Log\\Driver\\'. ucwords(strtolower($type)); unset($config['type']); self::$storage = new $class($config); } /** * 记录日志 并且会过滤未经设置的级别 * @static * @access public * @param string $message 日志信息 * @param string $level 日志级别 * @param boolean $record 是否强制记录 * @return void */ static function record($message,$level=self::ERR,$record=false) { if($record || false !== strpos(C('LOG_LEVEL'),$level)) { self::$log[] = "{$level}: {$message}\r\n"; } } /** * 日志保存 * @static * @access public * @param integer $type 日志记录方式 * @param string $destination 写入目标 * @return void */ static function save($type='',$destination='') { if(empty(self::$log)) return ; if(empty($destination)){ $destination = C('LOG_PATH').date('y_m_d').'.log'; } if(!self::$storage){ $type = $type ? : C('LOG_TYPE'); $class = 'Think\\Log\\Driver\\'. ucwords($type); self::$storage = new $class(); } $message = implode('',self::$log); self::$storage->write($message,$destination); // 保存后清空日志缓存 self::$log = array(); } /** * 日志直接写入 * @static * @access public * @param string $message 日志信息 * @param string $level 日志级别 * @param integer $type 日志记录方式 * @param string $destination 写入目标 * @return void */ static function write($message,$level=self::ERR,$type='',$destination='') { if(!self::$storage){ $type = $type ? : C('LOG_TYPE'); $class = 'Think\\Log\\Driver\\'. ucwords($type); $config['log_path'] = C('LOG_PATH'); self::$storage = new $class($config); } if(empty($destination)){ $destination = C('LOG_PATH').date('y_m_d').'.log'; } self::$storage->write("{$level}: {$message}", $destination); } } ================================================ FILE: ThinkPHP/Library/Think/Model/AdvModel.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Model; use Think\Model; /** * 高级模型扩展 */ class AdvModel extends Model { protected $optimLock = 'lock_version'; protected $returnType = 'array'; protected $blobFields = array(); protected $blobValues = null; protected $serializeField = array(); protected $readonlyField = array(); protected $_filter = array(); protected $partition = array(); public function __construct($name='',$tablePrefix='',$connection='') { if('' !== $name || is_subclass_of($this,'AdvModel') ){ // 如果是AdvModel子类或者有传入模型名称则获取字段缓存 }else{ // 空的模型 关闭字段缓存 $this->autoCheckFields = false; } parent::__construct($name,$tablePrefix,$connection); } /** * 利用__call方法重载 实现一些特殊的Model方法 (魔术方法) * @access public * @param string $method 方法名称 * @param mixed $args 调用参数 * @return mixed */ public function __call($method,$args) { if(strtolower(substr($method,0,3))=='top'){ // 获取前N条记录 $count = substr($method,3); array_unshift($args,$count); return call_user_func_array(array(&$this, 'topN'), $args); }else{ return parent::__call($method,$args); } } /** * 对保存到数据库的数据进行处理 * @access protected * @param mixed $data 要操作的数据 * @return boolean */ protected function _facade($data) { // 检查序列化字段 $data = $this->serializeField($data); return parent::_facade($data); } // 查询成功后的回调方法 protected function _after_find(&$result,$options='') { // 检查序列化字段 $this->checkSerializeField($result); // 获取文本字段 $this->getBlobFields($result); // 检查字段过滤 $result = $this->getFilterFields($result); // 缓存乐观锁 $this->cacheLockVersion($result); } // 查询数据集成功后的回调方法 protected function _after_select(&$resultSet,$options='') { // 检查序列化字段 $resultSet = $this->checkListSerializeField($resultSet); // 获取文本字段 $resultSet = $this->getListBlobFields($resultSet); // 检查列表字段过滤 $resultSet = $this->getFilterListFields($resultSet); } // 写入前的回调方法 protected function _before_insert(&$data,$options='') { // 记录乐观锁 $data = $this->recordLockVersion($data); // 检查文本字段 $data = $this->checkBlobFields($data); // 检查字段过滤 $data = $this->setFilterFields($data); } protected function _after_insert($data,$options) { // 保存文本字段 $this->saveBlobFields($data); } // 更新前的回调方法 protected function _before_update(&$data,$options='') { // 检查乐观锁 $pk = $this->getPK(); if(isset($options['where'][$pk])){ $id = $options['where'][$pk]; if(!$this->checkLockVersion($id,$data)) { return false; } } // 检查文本字段 $data = $this->checkBlobFields($data); // 检查只读字段 $data = $this->checkReadonlyField($data); // 检查字段过滤 $data = $this->setFilterFields($data); } protected function _after_update($data,$options) { // 保存文本字段 $this->saveBlobFields($data); } protected function _after_delete($data,$options) { // 删除Blob数据 $this->delBlobFields($data); } /** * 记录乐观锁 * @access protected * @param array $data 数据对象 * @return array */ protected function recordLockVersion($data) { // 记录乐观锁 if($this->optimLock && !isset($data[$this->optimLock]) ) { if(in_array($this->optimLock,$this->fields,true)) { $data[$this->optimLock] = 0; } } return $data; } /** * 缓存乐观锁 * @access protected * @param array $data 数据对象 * @return void */ protected function cacheLockVersion($data) { if($this->optimLock) { if(isset($data[$this->optimLock]) && isset($data[$this->getPk()])) { // 只有当存在乐观锁字段和主键有值的时候才记录乐观锁 $_SESSION[$this->name.'_'.$data[$this->getPk()].'_lock_version'] = $data[$this->optimLock]; } } } /** * 检查乐观锁 * @access protected * @param inteter $id 当前主键 * @param array $data 当前数据 * @return mixed */ protected function checkLockVersion($id,&$data) { // 检查乐观锁 $identify = $this->name.'_'.$id.'_lock_version'; if($this->optimLock && isset($_SESSION[$identify])) { $lock_version = $_SESSION[$identify]; $vo = $this->field($this->optimLock)->find($id); $_SESSION[$identify] = $lock_version; $curr_version = $vo[$this->optimLock]; if(isset($curr_version)) { if($curr_version>0 && $lock_version != $curr_version) { // 记录已经更新 $this->error = L('_RECORD_HAS_UPDATE_'); return false; }else{ // 更新乐观锁 $save_version = $data[$this->optimLock]; if($save_version != $lock_version+1) { $data[$this->optimLock] = $lock_version+1; } $_SESSION[$identify] = $lock_version+1; } } } return true; } /** * 查找前N个记录 * @access public * @param integer $count 记录个数 * @param array $options 查询表达式 * @return array */ public function topN($count,$options=array()) { $options['limit'] = $count; return $this->select($options); } /** * 查询符合条件的第N条记录 * 0 表示第一条记录 -1 表示最后一条记录 * @access public * @param integer $position 记录位置 * @param array $options 查询表达式 * @return mixed */ public function getN($position=0,$options=array()) { if($position>=0) { // 正向查找 $options['limit'] = $position.',1'; $list = $this->select($options); return $list?$list[0]:false; }else{ // 逆序查找 $list = $this->select($options); return $list?$list[count($list)-abs($position)]:false; } } /** * 获取满足条件的第一条记录 * @access public * @param array $options 查询表达式 * @return mixed */ public function first($options=array()) { return $this->getN(0,$options); } /** * 获取满足条件的最后一条记录 * @access public * @param array $options 查询表达式 * @return mixed */ public function last($options=array()) { return $this->getN(-1,$options); } /** * 返回数据 * @access public * @param array $data 数据 * @param string $type 返回类型 默认为数组 * @return mixed */ public function returnResult($data,$type='') { if('' === $type) $type = $this->returnType; switch($type) { case 'array' : return $data; case 'object': return (object)$data; default:// 允许用户自定义返回类型 if(class_exists($type)) return new $type($data); else E(L('_CLASS_NOT_EXIST_').':'.$type); } } /** * 获取数据的时候过滤数据字段 * @access protected * @param mixed $result 查询的数据 * @return array */ protected function getFilterFields(&$result) { if(!empty($this->_filter)) { foreach ($this->_filter as $field=>$filter){ if(isset($result[$field])) { $fun = $filter[1]; if(!empty($fun)) { if(isset($filter[2]) && $filter[2]){ // 传递整个数据对象作为参数 $result[$field] = call_user_func($fun,$result); }else{ // 传递字段的值作为参数 $result[$field] = call_user_func($fun,$result[$field]); } } } } } return $result; } protected function getFilterListFields(&$resultSet) { if(!empty($this->_filter)) { foreach ($resultSet as $key=>$result) $resultSet[$key] = $this->getFilterFields($result); } return $resultSet; } /** * 写入数据的时候过滤数据字段 * @access protected * @param mixed $result 查询的数据 * @return array */ protected function setFilterFields($data) { if(!empty($this->_filter)) { foreach ($this->_filter as $field=>$filter){ if(isset($data[$field])) { $fun = $filter[0]; if(!empty($fun)) { if(isset($filter[2]) && $filter[2]) { // 传递整个数据对象作为参数 $data[$field] = call_user_func($fun,$data); }else{ // 传递字段的值作为参数 $data[$field] = call_user_func($fun,$data[$field]); } } } } } return $data; } /** * 返回数据列表 * @access protected * @param array $resultSet 数据 * @param string $type 返回类型 默认为数组 * @return void */ protected function returnResultSet(&$resultSet,$type='') { foreach ($resultSet as $key=>$data) $resultSet[$key] = $this->returnResult($data,$type); return $resultSet; } protected function checkBlobFields(&$data) { // 检查Blob文件保存字段 if(!empty($this->blobFields)) { foreach ($this->blobFields as $field){ if(isset($data[$field])) { if(isset($data[$this->getPk()])) $this->blobValues[$this->name.'/'.$data[$this->getPk()].'_'.$field] = $data[$field]; else $this->blobValues[$this->name.'/@?id@_'.$field] = $data[$field]; unset($data[$field]); } } } return $data; } /** * 获取数据集的文本字段 * @access protected * @param mixed $resultSet 查询的数据 * @param string $field 查询的字段 * @return void */ protected function getListBlobFields(&$resultSet,$field='') { if(!empty($this->blobFields)) { foreach ($resultSet as $key=>$result){ $result = $this->getBlobFields($result,$field); $resultSet[$key] = $result; } } return $resultSet; } /** * 获取数据的文本字段 * @access protected * @param mixed $data 查询的数据 * @param string $field 查询的字段 * @return void */ protected function getBlobFields(&$data,$field='') { if(!empty($this->blobFields)) { $pk = $this->getPk(); $id = $data[$pk]; if(empty($field)) { foreach ($this->blobFields as $field){ $identify = $this->name.'/'.$id.'_'.$field; $data[$field] = F($identify); } return $data; }else{ $identify = $this->name.'/'.$id.'_'.$field; return F($identify); } } } /** * 保存File方式的字段 * @access protected * @param mixed $data 保存的数据 * @return void */ protected function saveBlobFields(&$data) { if(!empty($this->blobFields)) { foreach ($this->blobValues as $key=>$val){ if(strpos($key,'@?id@')) $key = str_replace('@?id@',$data[$this->getPk()],$key); F($key,$val); } } } /** * 删除File方式的字段 * @access protected * @param mixed $data 保存的数据 * @param string $field 查询的字段 * @return void */ protected function delBlobFields(&$data,$field='') { if(!empty($this->blobFields)) { $pk = $this->getPk(); $id = $data[$pk]; if(empty($field)) { foreach ($this->blobFields as $field){ $identify = $this->name.'/'.$id.'_'.$field; F($identify,null); } }else{ $identify = $this->name.'/'.$id.'_'.$field; F($identify,null); } } } /** * 检查序列化数据字段 * @access protected * @param array $data 数据 * @return array */ protected function serializeField(&$data) { // 检查序列化字段 if(!empty($this->serializeField)) { // 定义方式 $this->serializeField = array('ser'=>array('name','email')); foreach ($this->serializeField as $key=>$val){ if(empty($data[$key])) { $serialize = array(); foreach ($val as $name){ if(isset($data[$name])) { $serialize[$name] = $data[$name]; unset($data[$name]); } } if(!empty($serialize)) { $data[$key] = serialize($serialize); } } } } return $data; } // 检查返回数据的序列化字段 protected function checkSerializeField(&$result) { // 检查序列化字段 if(!empty($this->serializeField)) { foreach ($this->serializeField as $key=>$val){ if(isset($result[$key])) { $serialize = unserialize($result[$key]); foreach ($serialize as $name=>$value) $result[$name] = $value; unset($serialize,$result[$key]); } } } return $result; } // 检查数据集的序列化字段 protected function checkListSerializeField(&$resultSet) { // 检查序列化字段 if(!empty($this->serializeField)) { foreach ($this->serializeField as $key=>$val){ foreach ($resultSet as $k=>$result){ if(isset($result[$key])) { $serialize = unserialize($result[$key]); foreach ($serialize as $name=>$value) $result[$name] = $value; unset($serialize,$result[$key]); $resultSet[$k] = $result; } } } } return $resultSet; } /** * 检查只读字段 * @access protected * @param array $data 数据 * @return array */ protected function checkReadonlyField(&$data) { if(!empty($this->readonlyField)) { foreach ($this->readonlyField as $key=>$field){ if(isset($data[$field])) unset($data[$field]); } } return $data; } /** * 批处理执行SQL语句 * 批处理的指令都认为是execute操作 * @access public * @param array $sql SQL批处理指令 * @return boolean */ public function patchQuery($sql=array()) { if(!is_array($sql)) return false; // 自动启动事务支持 $this->startTrans(); try{ foreach ($sql as $_sql){ $result = $this->execute($_sql); if(false === $result) { // 发生错误自动回滚事务 $this->rollback(); return false; } } // 提交事务 $this->commit(); } catch (ThinkException $e) { $this->rollback(); } return true; } /** * 得到分表的的数据表名 * @access public * @param array $data 操作的数据 * @return string */ public function getPartitionTableName($data=array()) { // 对数据表进行分区 if(isset($data[$this->partition['field']])) { $field = $data[$this->partition['field']]; switch($this->partition['type']) { case 'id': // 按照id范围分表 $step = $this->partition['expr']; $seq = floor($field / $step)+1; break; case 'year': // 按照年份分表 if(!is_numeric($field)) { $field = strtotime($field); } $seq = date('Y',$field)-$this->partition['expr']+1; break; case 'mod': // 按照id的模数分表 $seq = ($field % $this->partition['num'])+1; break; case 'md5': // 按照md5的序列分表 $seq = (ord(substr(md5($field),0,1)) % $this->partition['num'])+1; break; default : if(function_exists($this->partition['type'])) { // 支持指定函数哈希 $fun = $this->partition['type']; $seq = (ord(substr($fun($field),0,1)) % $this->partition['num'])+1; }else{ // 按照字段的首字母的值分表 $seq = (ord($field{0}) % $this->partition['num'])+1; } } return $this->getTableName().'_'.$seq; }else{ // 当设置的分表字段不在查询条件或者数据中 // 进行联合查询,必须设定 partition['num'] $tableName = array(); for($i=0;$i<$this->partition['num'];$i++) $tableName[] = 'SELECT * FROM '.$this->getTableName().'_'.($i+1); $tableName = '( '.implode(" UNION ",$tableName).') AS '.$this->name; return $tableName; } } } ================================================ FILE: ThinkPHP/Library/Think/Model/MergeModel.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Model; use Think\Model; /** * ThinkPHP 聚合模型扩展 */ class MergeModel extends Model { protected $modelList = array(); // 包含的模型列表 第一个必须是主表模型 protected $masterModel = ''; // 主模型 protected $joinType = 'INNER'; // 聚合模型的查询JOIN类型 protected $fk = ''; // 外键名 默认为主表名_id protected $mapFields = array(); // 需要处理的模型映射字段,避免混淆 array( id => 'user.id' ) /** * 架构函数 * 取得DB类的实例对象 字段检查 * @access public * @param string $name 模型名称 * @param string $tablePrefix 表前缀 * @param mixed $connection 数据库连接信息 */ public function __construct($name='',$tablePrefix='',$connection=''){ parent::__construct($name,$tablePrefix,$connection); // 聚合模型的字段信息 if(empty($this->fields) && !empty($this->modelList)){ $fields = array(); foreach($this->modelList as $model){ // 获取模型的字段信息 $result = $this->db->getFields(M($model)->getTableName()); $_fields = array_keys($result); // $this->mapFields = array_intersect($fields,$_fields); $fields = array_merge($fields,$_fields); } $this->fields = $fields; } // 设置第一个模型为主表模型 if(empty($this->masterModel) && !empty($this->modelList)){ $this->masterModel = $this->modelList[0]; } // 主表的主键名 $this->pk = M($this->masterModel)->getPk(); // 设置默认外键名 仅支持单一外键 if(empty($this->fk)){ $this->fk = strtolower($this->masterModel).'_id'; } } /** * 得到完整的数据表名 * @access public * @return string */ public function getTableName() { if(empty($this->trueTableName)) { $tableName = array(); $models = $this->modelList; foreach($models as $model){ $tableName[] = M($model)->getTableName().' '.$model; } $this->trueTableName = implode(',',$tableName); } return $this->trueTableName; } /** * 自动检测数据表信息 * @access protected * @return void */ protected function _checkTableInfo() {} /** * 新增聚合数据 * @access public * @param mixed $data 数据 * @param array $options 表达式 * @param boolean $replace 是否replace * @return mixed */ public function add($data='',$options=array(),$replace=false){ if(empty($data)) { // 没有传递数据,获取当前数据对象的值 if(!empty($this->data)) { $data = $this->data; // 重置数据 $this->data = array(); }else{ $this->error = L('_DATA_TYPE_INVALID_'); return false; } } // 启动事务 $this->startTrans(); // 写入主表数据 $result = M($this->masterModel)->strict(false)->add($data); if($result){ // 写入外键数据 $data[$this->fk] = $result; $models = $this->modelList; array_shift($models); // 写入附表数据 foreach($models as $model){ $res = M($model)->strict(false)->add($data); if(!$res){ $this->rollback(); return false; } } // 提交事务 $this->commit(); }else{ $this->rollback(); return false; } return $result; } /** * 对保存到数据库的数据进行处理 * @access protected * @param mixed $data 要操作的数据 * @return boolean */ protected function _facade($data) { // 检查数据字段合法性 if(!empty($this->fields)) { if(!empty($this->options['field'])) { $fields = $this->options['field']; unset($this->options['field']); if(is_string($fields)) { $fields = explode(',',$fields); } }else{ $fields = $this->fields; } foreach ($data as $key=>$val){ if(!in_array($key,$fields,true)){ unset($data[$key]); }elseif(array_key_exists($key,$this->mapFields)){ // 需要处理映射字段 $data[$this->mapFields[$key]] = $val; unset($data[$key]); } } } // 安全过滤 if(!empty($this->options['filter'])) { $data = array_map($this->options['filter'],$data); unset($this->options['filter']); } $this->_before_write($data); return $data; } /** * 保存聚合模型数据 * @access public * @param mixed $data 数据 * @param array $options 表达式 * @return boolean */ public function save($data='',$options=array()){ // 根据主表的主键更新 if(empty($data)) { // 没有传递数据,获取当前数据对象的值 if(!empty($this->data)) { $data = $this->data; // 重置数据 $this->data = array(); }else{ $this->error = L('_DATA_TYPE_INVALID_'); return false; } } if(empty($data)){ // 没有数据则不执行 $this->error = L('_DATA_TYPE_INVALID_'); return false; } // 如果存在主键数据 则自动作为更新条件 $pk = $this->pk; if(isset($data[$pk])) { $where[$pk] = $data[$pk]; $options['where'] = $where; unset($data[$pk]); } $options['join'] = ''; $options = $this->_parseOptions($options); // 更新操作不使用JOIN $options['table'] = $this->getTableName(); if(is_array($options['where']) && isset($options['where'][$pk])){ $pkValue = $options['where'][$pk]; } if(false === $this->_before_update($data,$options)) { return false; } $result = $this->db->update($data,$options); if(false !== $result) { if(isset($pkValue)) $data[$pk] = $pkValue; $this->_after_update($data,$options); } return $result; } /** * 删除聚合模型数据 * @access public * @param mixed $options 表达式 * @return mixed */ public function delete($options=array()){ $pk = $this->pk; if(empty($options) && empty($this->options['where'])) { // 如果删除条件为空 则删除当前数据对象所对应的记录 if(!empty($this->data) && isset($this->data[$pk])) return $this->delete($this->data[$pk]); else return false; } if(is_numeric($options) || is_string($options)) { // 根据主键删除记录 if(strpos($options,',')) { $where[$pk] = array('IN', $options); }else{ $where[$pk] = $options; } $options = array(); $options['where'] = $where; } // 分析表达式 $options['join'] = ''; $options = $this->_parseOptions($options); if(empty($options['where'])){ // 如果条件为空 不进行删除操作 除非设置 1=1 return false; } if(is_array($options['where']) && isset($options['where'][$pk])){ $pkValue = $options['where'][$pk]; } $options['table'] = implode(',',$this->modelList); $options['using'] = $this->getTableName(); if(false === $this->_before_delete($options)) { return false; } $result = $this->db->delete($options); if(false !== $result) { $data = array(); if(isset($pkValue)) $data[$pk] = $pkValue; $this->_after_delete($data,$options); } // 返回删除记录个数 return $result; } /** * 表达式过滤方法 * @access protected * @param string $options 表达式 * @return void */ protected function _options_filter(&$options) { if(!isset($options['join'])){ $models = $this->modelList; array_shift($models); foreach($models as $model){ $options['join'][] = $this->joinType.' JOIN '.M($model)->getTableName().' '.$model.' ON '.$this->masterModel.'.'.$this->pk.' = '.$model.'.'.$this->fk; } } $options['table'] = M($this->masterModel)->getTableName().' '.$this->masterModel; $options['field'] = $this->checkFields(isset($options['field'])?$options['field']:''); if(isset($options['group'])) $options['group'] = $this->checkGroup($options['group']); if(isset($options['where'])) $options['where'] = $this->checkCondition($options['where']); if(isset($options['order'])) $options['order'] = $this->checkOrder($options['order']); } /** * 检查条件中的聚合字段 * @access protected * @param mixed $data 条件表达式 * @return array */ protected function checkCondition($where) { if(is_array($where)) { $view = array(); foreach($where as $name=>$value){ if(array_key_exists($name,$this->mapFields)){ // 需要处理映射字段 $view[$this->mapFields[$name]] = $value; unset($where[$name]); } } $where = array_merge($where,$view); } return $where; } /** * 检查Order表达式中的聚合字段 * @access protected * @param string $order 字段 * @return string */ protected function checkOrder($order='') { if(is_string($order) && !empty($order)) { $orders = explode(',',$order); $_order = array(); foreach ($orders as $order){ $array = explode(' ',trim($order)); $field = $array[0]; $sort = isset($array[1])?$array[1]:'ASC'; if(array_key_exists($field,$this->mapFields)){ // 需要处理映射字段 $field = $this->mapFields[$field]; } $_order[] = $field.' '.$sort; } $order = implode(',',$_order); } return $order; } /** * 检查Group表达式中的聚合字段 * @access protected * @param string $group 字段 * @return string */ protected function checkGroup($group='') { if(!empty($group)) { $groups = explode(',',$group); $_group = array(); foreach ($groups as $field){ // 解析成聚合字段 if(array_key_exists($field,$this->mapFields)){ // 需要处理映射字段 $field = $this->mapFields[$field]; } $_group[] = $field; } $group = implode(',',$_group); } return $group; } /** * 检查fields表达式中的聚合字段 * @access protected * @param string $fields 字段 * @return string */ protected function checkFields($fields='') { if(empty($fields) || '*'==$fields ) { // 获取全部聚合字段 $fields = $this->fields; } if(!is_array($fields)) $fields = explode(',',$fields); // 解析成聚合字段 $array = array(); foreach ($fields as $field){ if(array_key_exists($field,$this->mapFields)){ // 需要处理映射字段 $array[] = $this->mapFields[$field].' AS '.$field; }else{ $array[] = $field; } } $fields = implode(',',$array); return $fields; } /** * 获取数据表字段信息 * @access public * @return array */ public function getDbFields(){ return $this->fields; } } ================================================ FILE: ThinkPHP/Library/Think/Model/MongoModel.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Model; use Think\Model; /** * MongoModel模型类 * 实现了ODM和ActiveRecords模式 */ class MongoModel extends Model{ // 主键类型 const TYPE_OBJECT = 1; const TYPE_INT = 2; const TYPE_STRING = 3; // 主键名称 protected $pk = '_id'; // _id 类型 1 Object 采用MongoId对象 2 Int 整形 支持自动增长 3 String 字符串Hash protected $_idType = self::TYPE_OBJECT; // 主键是否自增 protected $_autoinc = true; // Mongo默认关闭字段检测 可以动态追加字段 protected $autoCheckFields = false; // 链操作方法列表 protected $methods = array('table','order','auto','filter','validate'); /** * 利用__call方法实现一些特殊的Model方法 * @access public * @param string $method 方法名称 * @param array $args 调用参数 * @return mixed */ public function __call($method,$args) { if(in_array(strtolower($method),$this->methods,true)) { // 连贯操作的实现 $this->options[strtolower($method)] = $args[0]; return $this; }elseif(strtolower(substr($method,0,5))=='getby') { // 根据某个字段获取记录 $field = parse_name(substr($method,5)); $where[$field] =$args[0]; return $this->where($where)->find(); }elseif(strtolower(substr($method,0,10))=='getfieldby') { // 根据某个字段获取记录的某个值 $name = parse_name(substr($method,10)); $where[$name] =$args[0]; return $this->where($where)->getField($args[1]); }else{ E(__CLASS__.':'.$method.L('_METHOD_NOT_EXIST_')); return; } } /** * 获取字段信息并缓存 主键和自增信息直接配置 * @access public * @return void */ public function flush() { // 缓存不存在则查询数据表信息 $fields = $this->db->getFields(); if(!$fields) { // 暂时没有数据无法获取字段信息 下次查询 return false; } $this->fields = array_keys($fields); foreach ($fields as $key=>$val){ // 记录字段类型 $type[$key] = $val['type']; } // 记录字段类型信息 if(C('DB_FIELDTYPE_CHECK')) $this->fields['_type'] = $type; // 2008-3-7 增加缓存开关控制 if(C('DB_FIELDS_CACHE')){ // 永久缓存数据表信息 $db = $this->dbName?$this->dbName:C('DB_NAME'); F('_fields/'.$db.'.'.$this->name,$this->fields); } } // 写入数据前的回调方法 包括新增和更新 protected function _before_write(&$data) { $pk = $this->getPk(); // 根据主键类型处理主键数据 if(isset($data[$pk]) && $this->_idType == self::TYPE_OBJECT) { $data[$pk] = new \MongoId($data[$pk]); } } /** * count统计 配合where连贯操作 * @access public * @return integer */ public function count(){ // 分析表达式 $options = $this->_parseOptions(); return $this->db->count($options); } /** * 获取唯一值 * @access public * @return array | false */ public function distinct($field, $where=array() ){ // 分析表达式 $this->options = $this->_parseOptions(); $this->options['where'] = array_merge((array)$this->options['where'], $where); $command = array( "distinct" => $this->options['table'], "key" => $field, "query" => $this->options['where'] ); $result = $this->command($command); return isset($result['values']) ? $result['values'] : false; } /** * 获取下一ID 用于自动增长型 * @access public * @param string $pk 字段名 默认为主键 * @return mixed */ public function getMongoNextId($pk=''){ if(empty($pk)) { $pk = $this->getPk(); } return $this->db->getMongoNextId($pk); } /** * 新增数据 * @access public * @param mixed $data 数据 * @param array $options 表达式 * @param boolean $replace 是否replace * @return mixed */ public function add($data='',$options=array(),$replace=false) { if(empty($data)) { // 没有传递数据,获取当前数据对象的值 if(!empty($this->data)) { $data = $this->data; // 重置数据 $this->data = array(); }else{ $this->error = L('_DATA_TYPE_INVALID_'); return false; } } // 分析表达式 $options = $this->_parseOptions($options); // 数据处理 $data = $this->_facade($data); if(false === $this->_before_insert($data,$options)) { return false; } // 写入数据到数据库 $result = $this->db->insert($data,$options,$replace); if(false !== $result ) { $this->_after_insert($data,$options); if(isset($data[$this->getPk()])){ return $data[$this->getPk()]; } } return $result; } // 插入数据前的回调方法 protected function _before_insert(&$data,$options) { // 写入数据到数据库 if($this->_autoinc && $this->_idType== self::TYPE_INT) { // 主键自动增长 $pk = $this->getPk(); if(!isset($data[$pk])) { $data[$pk] = $this->db->getMongoNextId($pk); } } } public function clear(){ return $this->db->clear(); } // 查询成功后的回调方法 protected function _after_select(&$resultSet,$options) { array_walk($resultSet,array($this,'checkMongoId')); } /** * 获取MongoId * @access protected * @param array $result 返回数据 * @return array */ protected function checkMongoId(&$result){ if(is_object($result['_id'])) { $result['_id'] = $result['_id']->__toString(); } return $result; } // 表达式过滤回调方法 protected function _options_filter(&$options) { $id = $this->getPk(); if(isset($options['where'][$id]) && is_scalar($options['where'][$id]) && $this->_idType== self::TYPE_OBJECT) { $options['where'][$id] = new \MongoId($options['where'][$id]); } } /** * 查询数据 * @access public * @param mixed $options 表达式参数 * @return mixed */ public function find($options=array()) { if( is_numeric($options) || is_string($options)) { $id = $this->getPk(); $where[$id] = $options; $options = array(); $options['where'] = $where; } // 分析表达式 $options = $this->_parseOptions($options); $result = $this->db->find($options); if(false === $result) { return false; } if(empty($result)) {// 查询结果为空 return null; }else{ $this->checkMongoId($result); } $this->data = $result; $this->_after_find($this->data,$options); return $this->data; } /** * 字段值增长 * @access public * @param string $field 字段名 * @param integer $step 增长值 * @return boolean */ public function setInc($field,$step=1) { return $this->setField($field,array('inc',$step)); } /** * 字段值减少 * @access public * @param string $field 字段名 * @param integer $step 减少值 * @return boolean */ public function setDec($field,$step=1) { return $this->setField($field,array('inc','-'.$step)); } /** * 获取一条记录的某个字段值 * @access public * @param string $field 字段名 * @param string $spea 字段数据间隔符号 * @return mixed */ public function getField($field,$sepa=null) { $options['field'] = $field; $options = $this->_parseOptions($options); if(strpos($field,',')) { // 多字段 if(is_numeric($sepa)) {// 限定数量 $options['limit'] = $sepa; $sepa = null;// 重置为null 返回数组 } $resultSet = $this->db->select($options); if(!empty($resultSet)) { $_field = explode(',', $field); $field = array_keys($resultSet[0]); $key = array_shift($field); $key2 = array_shift($field); $cols = array(); $count = count($_field); foreach ($resultSet as $result){ $name = $result[$key]; if(2==$count) { $cols[$name] = $result[$key2]; }else{ $cols[$name] = is_null($sepa)?$result:implode($sepa,$result); } } return $cols; } }else{ // 返回数据个数 if(true !== $sepa) {// 当sepa指定为true的时候 返回所有数据 $options['limit'] = is_numeric($sepa)?$sepa:1; } // 查找符合的记录 $result = $this->db->select($options); if(!empty($result)) { if(1==$options['limit']) { $result = reset($result); return $result[$field]; } foreach ($result as $val){ $array[] = $val[$field]; } return $array; } } return null; } /** * 执行Mongo指令 * @access public * @param array $command 指令 * @return mixed */ public function command($command, $options=array()) { $options = $this->_parseOptions($options); return $this->db->command($command, $options); } /** * 执行MongoCode * @access public * @param string $code MongoCode * @param array $args 参数 * @return mixed */ public function mongoCode($code,$args=array()) { return $this->db->execute($code,$args); } // 数据库切换后回调方法 protected function _after_db() { // 切换Collection $this->db->switchCollection($this->getTableName(),$this->dbName?$this->dbName:C('db_name')); } /** * 得到完整的数据表名 Mongo表名不带dbName * @access public * @return string */ public function getTableName() { if(empty($this->trueTableName)) { $tableName = !empty($this->tablePrefix) ? $this->tablePrefix : ''; if(!empty($this->tableName)) { $tableName .= $this->tableName; }else{ $tableName .= parse_name($this->name); } $this->trueTableName = strtolower($tableName); } return $this->trueTableName; } /** * 分组查询 * @access public * @return string */ public function group($key, $init, $reduce, $option=array()) { $option = $this->_parseOptions($option); //合并查询条件 if(isset($option['where'])) $option['condition'] = array_merge((array)$option['condition'], $option['where']); return $this->db->group($key, $init, $reduce, $option); } /** * 返回Mongo运行错误信息 * @access public * @return json */ public function getLastError(){ return $this->db->command(array('getLastError'=>1)); } /** * 返回指定集合的统计信息,包括数据大小、已分配的存储空间和索引的大小 * @access public * @return json */ public function status(){ $option = $this->_parseOptions(); return $this->db->command(array('collStats'=>$option['table'])); } /** * 取得当前数据库的对象 * @access public * @return object */ public function getDB(){ return $this->db->getDB(); } /** * 取得集合对象,可以进行创建索引等查询 * @access public * @return object */ public function getCollection(){ return $this->db->getCollection(); } } ================================================ FILE: ThinkPHP/Library/Think/Model/RelationModel.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Model; use Think\Model; /** * ThinkPHP关联模型扩展 */ class RelationModel extends Model { const HAS_ONE = 1; const BELONGS_TO = 2; const HAS_MANY = 3; const MANY_TO_MANY= 4; // 关联定义 protected $_link = array(); /** * 动态方法实现 * @access public * @param string $method 方法名称 * @param array $args 调用参数 * @return mixed */ public function __call($method,$args) { if(strtolower(substr($method,0,8))=='relation'){ $type = strtoupper(substr($method,8)); if(in_array($type,array('ADD','SAVE','DEL'),true)) { array_unshift($args,$type); return call_user_func_array(array(&$this, 'opRelation'), $args); } }else{ return parent::__call($method,$args); } } /** * 得到关联的数据表名 * @access public * @return string */ public function getRelationTableName($relation) { $relationTable = !empty($this->tablePrefix) ? $this->tablePrefix : ''; $relationTable .= $this->tableName?$this->tableName:$this->name; $relationTable .= '_'.$relation->getModelName(); return strtolower($relationTable); } // 查询成功后的回调方法 protected function _after_find(&$result,$options) { // 获取关联数据 并附加到结果中 if(!empty($options['link'])) $this->getRelation($result,$options['link']); } // 查询数据集成功后的回调方法 protected function _after_select(&$result,$options) { // 获取关联数据 并附加到结果中 if(!empty($options['link'])) $this->getRelations($result,$options['link']); } // 写入成功后的回调方法 protected function _after_insert($data,$options) { // 关联写入 if(!empty($options['link'])) $this->opRelation('ADD',$data,$options['link']); } // 更新成功后的回调方法 protected function _after_update($data,$options) { // 关联更新 if(!empty($options['link'])) $this->opRelation('SAVE',$data,$options['link']); } // 删除成功后的回调方法 protected function _after_delete($data,$options) { // 关联删除 if(!empty($options['link'])) $this->opRelation('DEL',$data,$options['link']); } /** * 对保存到数据库的数据进行处理 * @access protected * @param mixed $data 要操作的数据 * @return boolean */ protected function _facade($data) { $this->_before_write($data); return $data; } /** * 获取返回数据集的关联记录 * @access protected * @param array $resultSet 返回数据 * @param string|array $name 关联名称 * @return array */ protected function getRelations(&$resultSet,$name='') { // 获取记录集的主键列表 foreach($resultSet as $key=>$val) { $val = $this->getRelation($val,$name); $resultSet[$key] = $val; } return $resultSet; } /** * 获取返回数据的关联记录 * @access protected * @param mixed $result 返回数据 * @param string|array $name 关联名称 * @param boolean $return 是否返回关联数据本身 * @return array */ protected function getRelation(&$result,$name='',$return=false) { if(!empty($this->_link)) { foreach($this->_link as $key=>$val) { $mappingName = !empty($val['mapping_name'])?$val['mapping_name']:$key; // 映射名称 if(empty($name) || true === $name || $mappingName == $name || (is_array($name) && in_array($mappingName,$name))) { $mappingType = !empty($val['mapping_type'])?$val['mapping_type']:$val; // 关联类型 $mappingClass = !empty($val['class_name'])?$val['class_name']:$key; // 关联类名 $mappingFields = !empty($val['mapping_fields'])?$val['mapping_fields']:'*'; // 映射字段 $mappingCondition = !empty($val['condition'])?$val['condition']:'1=1'; // 关联条件 $mappingKey =!empty($val['mapping_key'])? $val['mapping_key'] : $this->getPk(); // 关联键名 if(strtoupper($mappingClass)==strtoupper($this->name)) { // 自引用关联 获取父键名 $mappingFk = !empty($val['parent_key'])? $val['parent_key'] : 'parent_id'; }else{ $mappingFk = !empty($val['foreign_key'])?$val['foreign_key']:strtolower($this->name).'_id'; // 关联外键 } // 获取关联模型对象 $model = D($mappingClass); switch($mappingType) { case self::HAS_ONE: $pk = $result[$mappingKey]; $mappingCondition .= " AND {$mappingFk}='{$pk}'"; $relationData = $model->where($mappingCondition)->field($mappingFields)->find(); if (!empty($val['relation_deep'])){ $model->getRelation($relationData,$val['relation_deep']); } break; case self::BELONGS_TO: if(strtoupper($mappingClass)==strtoupper($this->name)) { // 自引用关联 获取父键名 $mappingFk = !empty($val['parent_key'])? $val['parent_key'] : 'parent_id'; }else{ $mappingFk = !empty($val['foreign_key'])?$val['foreign_key']:strtolower($model->getModelName()).'_id'; // 关联外键 } $fk = $result[$mappingFk]; $mappingCondition .= " AND {$model->getPk()}='{$fk}'"; $relationData = $model->where($mappingCondition)->field($mappingFields)->find(); if (!empty($val['relation_deep'])){ $model->getRelation($relationData,$val['relation_deep']); } break; case self::HAS_MANY: $pk = $result[$mappingKey]; $mappingCondition .= " AND {$mappingFk}='{$pk}'"; $mappingOrder = !empty($val['mapping_order'])?$val['mapping_order']:''; $mappingLimit = !empty($val['mapping_limit'])?$val['mapping_limit']:''; // 延时获取关联记录 $relationData = $model->where($mappingCondition)->field($mappingFields)->order($mappingOrder)->limit($mappingLimit)->select(); if (!empty($val['relation_deep'])){ foreach($relationData as $key=>$data){ $model->getRelation($data,$val['relation_deep']); $relationData[$key] = $data; } } break; case self::MANY_TO_MANY: $pk = $result[$mappingKey]; $prefix = $this->tablePrefix; $mappingCondition = " {$mappingFk}='{$pk}'"; $mappingOrder = $val['mapping_order']; $mappingLimit = $val['mapping_limit']; $mappingRelationFk = $val['relation_foreign_key']?$val['relation_foreign_key']:$model->getModelName().'_id'; if(isset($val['relation_table'])){ $mappingRelationTable = preg_replace_callback("/__([A-Z_-]+)__/sU", function($match) use($prefix){ return $prefix.strtolower($match[1]);}, $val['relation_table']); }else{ $mappingRelationTable = $this->getRelationTableName($model); } $sql = "SELECT b.{$mappingFields} FROM {$mappingRelationTable} AS a, ".$model->getTableName()." AS b WHERE a.{$mappingRelationFk} = b.{$model->getPk()} AND a.{$mappingCondition}"; if(!empty($val['condition'])) { $sql .= ' AND '.$val['condition']; } if(!empty($mappingOrder)) { $sql .= ' ORDER BY '.$mappingOrder; } if(!empty($mappingLimit)) { $sql .= ' LIMIT '.$mappingLimit; } $relationData = $this->query($sql); if (!empty($val['relation_deep'])){ foreach($relationData as $key=>$data){ $model->getRelation($data,$val['relation_deep']); $relationData[$key] = $data; } } break; } if(!$return){ if(isset($val['as_fields']) && in_array($mappingType,array(self::HAS_ONE,self::BELONGS_TO)) ) { // 支持直接把关联的字段值映射成数据对象中的某个字段 // 仅仅支持HAS_ONE BELONGS_TO $fields = explode(',',$val['as_fields']); foreach ($fields as $field){ if(strpos($field,':')) { list($relationName,$nick) = explode(':',$field); $result[$nick] = $relationData[$relationName]; }else{ $result[$field] = $relationData[$field]; } } }else{ $result[$mappingName] = $relationData; } unset($relationData); }else{ return $relationData; } } } } return $result; } /** * 操作关联数据 * @access protected * @param string $opType 操作方式 ADD SAVE DEL * @param mixed $data 数据对象 * @param string $name 关联名称 * @return mixed */ protected function opRelation($opType,$data='',$name='') { $result = false; if(empty($data) && !empty($this->data)){ $data = $this->data; }elseif(!is_array($data)){ // 数据无效返回 return false; } if(!empty($this->_link)) { // 遍历关联定义 foreach($this->_link as $key=>$val) { // 操作制定关联类型 $mappingName = $val['mapping_name']?$val['mapping_name']:$key; // 映射名称 if(empty($name) || true === $name || $mappingName == $name || (is_array($name) && in_array($mappingName,$name)) ) { // 操作制定的关联 $mappingType = !empty($val['mapping_type'])?$val['mapping_type']:$val; // 关联类型 $mappingClass = !empty($val['class_name'])?$val['class_name']:$key; // 关联类名 $mappingKey =!empty($val['mapping_key'])? $val['mapping_key'] : $this->getPk(); // 关联键名 // 当前数据对象主键值 $pk = $data[$mappingKey]; if(strtoupper($mappingClass)==strtoupper($this->name)) { // 自引用关联 获取父键名 $mappingFk = !empty($val['parent_key'])? $val['parent_key'] : 'parent_id'; }else{ $mappingFk = !empty($val['foreign_key'])?$val['foreign_key']:strtolower($this->name).'_id'; // 关联外键 } if(!empty($val['condition'])) { $mappingCondition = $val['condition']; }else{ $mappingCondition = array(); $mappingCondition[$mappingFk] = $pk; } // 获取关联model对象 $model = D($mappingClass); $mappingData = isset($data[$mappingName])?$data[$mappingName]:false; if(!empty($mappingData) || $opType == 'DEL') { switch($mappingType) { case self::HAS_ONE: switch (strtoupper($opType)){ case 'ADD': // 增加关联数据 $mappingData[$mappingFk] = $pk; $result = $model->add($mappingData); break; case 'SAVE': // 更新关联数据 $result = $model->where($mappingCondition)->save($mappingData); break; case 'DEL': // 根据外键删除关联数据 $result = $model->where($mappingCondition)->delete(); break; } break; case self::BELONGS_TO: break; case self::HAS_MANY: switch (strtoupper($opType)){ case 'ADD' : // 增加关联数据 $model->startTrans(); foreach ($mappingData as $val){ $val[$mappingFk] = $pk; $result = $model->add($val); } $model->commit(); break; case 'SAVE' : // 更新关联数据 $model->startTrans(); $pk = $model->getPk(); foreach ($mappingData as $vo){ if(isset($vo[$pk])) {// 更新数据 $mappingCondition = "$pk ={$vo[$pk]}"; $result = $model->where($mappingCondition)->save($vo); }else{ // 新增数据 $vo[$mappingFk] = $data[$mappingKey]; $result = $model->add($vo); } } $model->commit(); break; case 'DEL' : // 删除关联数据 $result = $model->where($mappingCondition)->delete(); break; } break; case self::MANY_TO_MANY: $mappingRelationFk = $val['relation_foreign_key']?$val['relation_foreign_key']:$model->getModelName().'_id';// 关联 $prefix = $this->tablePrefix; if(isset($val['relation_table'])){ $mappingRelationTable = preg_replace_callback("/__([A-Z_-]+)__/sU", function($match) use($prefix){ return $prefix.strtolower($match[1]);}, $val['relation_table']); }else{ $mappingRelationTable = $this->getRelationTableName($model); } if(is_array($mappingData)) { $ids = array(); foreach ($mappingData as $vo) $ids[] = $vo[$mappingKey]; $relationId = implode(',',$ids); } switch (strtoupper($opType)){ case 'ADD': // 增加关联数据 if(isset($relationId)) { $this->startTrans(); // 插入关联表数据 $sql = 'INSERT INTO '.$mappingRelationTable.' ('.$mappingFk.','.$mappingRelationFk.') SELECT a.'.$this->getPk().',b.'.$model->getPk().' FROM '.$this->getTableName().' AS a ,'.$model->getTableName()." AS b where a.".$this->getPk().' ='. $pk.' AND b.'.$model->getPk().' IN ('.$relationId.") "; $result = $model->execute($sql); if(false !== $result) // 提交事务 $this->commit(); else // 事务回滚 $this->rollback(); } break; case 'SAVE': // 更新关联数据 if(isset($relationId)) { $this->startTrans(); // 删除关联表数据 $this->table($mappingRelationTable)->where($mappingCondition)->delete(); // 插入关联表数据 $sql = 'INSERT INTO '.$mappingRelationTable.' ('.$mappingFk.','.$mappingRelationFk.') SELECT a.'.$this->getPk().',b.'.$model->getPk().' FROM '.$this->getTableName().' AS a ,'.$model->getTableName()." AS b where a.".$this->getPk().' ='. $pk.' AND b.'.$model->getPk().' IN ('.$relationId.") "; $result = $model->execute($sql); if(false !== $result) // 提交事务 $this->commit(); else // 事务回滚 $this->rollback(); } break; case 'DEL': // 根据外键删除中间表关联数据 $result = $this->table($mappingRelationTable)->where($mappingCondition)->delete(); break; } break; } if (!empty($val['relation_deep'])){ $model->opRelation($opType,$mappingData,$val['relation_deep']); } } } } } return $result; } /** * 进行关联查询 * @access public * @param mixed $name 关联名称 * @return Model */ public function relation($name) { $this->options['link'] = $name; return $this; } /** * 关联数据获取 仅用于查询后 * @access public * @param string $name 关联名称 * @return array */ public function relationGet($name) { if(empty($this->data)) return false; return $this->getRelation($this->data,$name,true); } } ================================================ FILE: ThinkPHP/Library/Think/Model/ViewModel.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Model; use Think\Model; /** * ThinkPHP视图模型扩展 */ class ViewModel extends Model { protected $viewFields = array(); /** * 自动检测数据表信息 * @access protected * @return void */ protected function _checkTableInfo() {} /** * 得到完整的数据表名 * @access public * @return string */ public function getTableName() { if(empty($this->trueTableName)) { $tableName = ''; foreach ($this->viewFields as $key=>$view){ // 获取数据表名称 if(isset($view['_table'])) { // 2011/10/17 添加实际表名定义支持 可以实现同一个表的视图 $tableName .= $view['_table']; $prefix = $this->tablePrefix; $tableName = preg_replace_callback("/__([A-Z_-]+)__/sU", function($match) use($prefix){ return $prefix.strtolower($match[1]);}, $tableName); }else{ $class = $key.'Model'; $Model = class_exists($class)?new $class():M($key); $tableName .= $Model->getTableName(); } // 表别名定义 $tableName .= !empty($view['_as'])?' '.$view['_as']:' '.$key; // 支持ON 条件定义 $tableName .= !empty($view['_on'])?' ON '.$view['_on']:''; // 指定JOIN类型 例如 RIGHT INNER LEFT 下一个表有效 $type = !empty($view['_type'])?$view['_type']:''; $tableName .= ' '.strtoupper($type).' JOIN '; $len = strlen($type.'_JOIN '); } $tableName = substr($tableName,0,-$len); $this->trueTableName = $tableName; } return $this->trueTableName; } /** * 表达式过滤方法 * @access protected * @param string $options 表达式 * @return void */ protected function _options_filter(&$options) { if(isset($options['field'])) $options['field'] = $this->checkFields($options['field']); else $options['field'] = $this->checkFields(); if(isset($options['group'])) $options['group'] = $this->checkGroup($options['group']); if(isset($options['where'])) $options['where'] = $this->checkCondition($options['where']); if(isset($options['order'])) $options['order'] = $this->checkOrder($options['order']); } /** * 检查是否定义了所有字段 * @access protected * @param string $name 模型名称 * @param array $fields 字段数组 * @return array */ private function _checkFields($name,$fields) { if(false !== $pos = array_search('*',$fields)) {// 定义所有字段 $fields = array_merge($fields,M($name)->getDbFields()); unset($fields[$pos]); } return $fields; } /** * 检查条件中的视图字段 * @access protected * @param mixed $data 条件表达式 * @return array */ protected function checkCondition($where) { if(is_array($where)) { $view = array(); // 检查视图字段 foreach ($this->viewFields as $key=>$val){ $k = isset($val['_as'])?$val['_as']:$key; $val = $this->_checkFields($key,$val); foreach ($where as $name=>$value){ if(false !== $field = array_search($name,$val,true)) { // 存在视图字段 $_key = is_numeric($field)? $k.'.'.$name : $k.'.'.$field; $view[$_key] = $value; unset($where[$name]); } } } $where = array_merge($where,$view); } return $where; } /** * 检查Order表达式中的视图字段 * @access protected * @param string $order 字段 * @return string */ protected function checkOrder($order='') { if(is_string($order) && !empty($order)) { $orders = explode(',',$order); $_order = array(); foreach ($orders as $order){ $array = explode(' ',trim($order)); $field = $array[0]; $sort = isset($array[1])?$array[1]:'ASC'; // 解析成视图字段 foreach ($this->viewFields as $name=>$val){ $k = isset($val['_as'])?$val['_as']:$name; $val = $this->_checkFields($name,$val); if(false !== $_field = array_search($field,$val,true)) { // 存在视图字段 $field = is_numeric($_field)?$k.'.'.$field:$k.'.'.$_field; break; } } $_order[] = $field.' '.$sort; } $order = implode(',',$_order); } return $order; } /** * 检查Group表达式中的视图字段 * @access protected * @param string $group 字段 * @return string */ protected function checkGroup($group='') { if(!empty($group)) { $groups = explode(',',$group); $_group = array(); foreach ($groups as $field){ // 解析成视图字段 foreach ($this->viewFields as $name=>$val){ $k = isset($val['_as'])?$val['_as']:$name; $val = $this->_checkFields($name,$val); if(false !== $_field = array_search($field,$val,true)) { // 存在视图字段 $field = is_numeric($_field)?$k.'.'.$field:$k.'.'.$_field; break; } } $_group[] = $field; } $group = implode(',',$_group); } return $group; } /** * 检查fields表达式中的视图字段 * @access protected * @param string $fields 字段 * @return string */ protected function checkFields($fields='') { if(empty($fields) || '*'==$fields ) { // 获取全部视图字段 $fields = array(); foreach ($this->viewFields as $name=>$val){ $k = isset($val['_as'])?$val['_as']:$name; $val = $this->_checkFields($name,$val); foreach ($val as $key=>$field){ if(is_numeric($key)) { $fields[] = $k.'.'.$field.' AS '.$field; }elseif('_' != substr($key,0,1)) { // 以_开头的为特殊定义 if( false !== strpos($key,'*') || false !== strpos($key,'(') || false !== strpos($key,'.')) { //如果包含* 或者 使用了sql方法 则不再添加前面的表名 $fields[] = $key.' AS '.$field; }else{ $fields[] = $k.'.'.$key.' AS '.$field; } } } } $fields = implode(',',$fields); }else{ if(!is_array($fields)) $fields = explode(',',$fields); // 解析成视图字段 $array = array(); foreach ($fields as $key=>$field){ if(strpos($field,'(') || strpos(strtolower($field),' as ')){ // 使用了函数或者别名 $array[] = $field; unset($fields[$key]); } } foreach ($this->viewFields as $name=>$val){ $k = isset($val['_as'])?$val['_as']:$name; $val = $this->_checkFields($name,$val); foreach ($fields as $key=>$field){ if(false !== $_field = array_search($field,$val,true)) { // 存在视图字段 if(is_numeric($_field)) { $array[] = $k.'.'.$field.' AS '.$field; }elseif('_' != substr($_field,0,1)){ if( false !== strpos($_field,'*') || false !== strpos($_field,'(') || false !== strpos($_field,'.')) //如果包含* 或者 使用了sql方法 则不再添加前面的表名 $array[] = $_field.' AS '.$field; else $array[] = $k.'.'.$_field.' AS '.$field; } } } } $fields = implode(',',$array); } return $fields; } } ================================================ FILE: ThinkPHP/Library/Think/Model.class.php ================================================ // +---------------------------------------------------------------------- namespace Think; /** * ThinkPHP Model模型类 * 实现了ORM和ActiveRecords模式 */ class Model { // 操作状态 const MODEL_INSERT = 1; // 插入模型数据 const MODEL_UPDATE = 2; // 更新模型数据 const MODEL_BOTH = 3; // 包含上面两种方式 const MUST_VALIDATE = 1; // 必须验证 const EXISTS_VALIDATE = 0; // 表单存在字段则验证 const VALUE_VALIDATE = 2; // 表单值不为空则验证 // 当前数据库操作对象 protected $db = null; // 数据库对象池 private $_db = array(); // 主键名称 protected $pk = 'id'; // 主键是否自动增长 protected $autoinc = false; // 数据表前缀 protected $tablePrefix = null; // 模型名称 protected $name = ''; // 数据库名称 protected $dbName = ''; //数据库配置 protected $connection = ''; // 数据表名(不包含表前缀) protected $tableName = ''; // 实际数据表名(包含表前缀) protected $trueTableName = ''; // 最近错误信息 protected $error = ''; // 字段信息 protected $fields = array(); // 数据信息 protected $data = array(); // 查询表达式参数 protected $options = array(); protected $_validate = array(); // 自动验证定义 protected $_auto = array(); // 自动完成定义 protected $_map = array(); // 字段映射定义 protected $_scope = array(); // 命名范围定义 // 是否自动检测数据表字段信息 protected $autoCheckFields = true; // 是否批处理验证 protected $patchValidate = false; // 链操作方法列表 protected $methods = array('strict','order','alias','having','group','lock','distinct','auto','filter','validate','result','token','index','force'); /** * 架构函数 * 取得DB类的实例对象 字段检查 * @access public * @param string $name 模型名称 * @param string $tablePrefix 表前缀 * @param mixed $connection 数据库连接信息 */ public function __construct($name='',$tablePrefix='',$connection='') { // 模型初始化 $this->_initialize(); // 获取模型名称 if(!empty($name)) { if(strpos($name,'.')) { // 支持 数据库名.模型名的 定义 list($this->dbName,$this->name) = explode('.',$name); }else{ $this->name = $name; } }elseif(empty($this->name)){ $this->name = $this->getModelName(); } // 设置表前缀 if(is_null($tablePrefix)) {// 前缀为Null表示没有前缀 $this->tablePrefix = ''; }elseif('' != $tablePrefix) { $this->tablePrefix = $tablePrefix; }elseif(!isset($this->tablePrefix)){ $this->tablePrefix = C('DB_PREFIX'); } // 数据库初始化操作 // 获取数据库操作对象 // 当前模型有独立的数据库连接信息 $this->db(0,empty($this->connection)?$connection:$this->connection,true); } /** * 自动检测数据表信息 * @access protected * @return void */ protected function _checkTableInfo() { // 如果不是Model类 自动记录数据表信息 // 只在第一次执行记录 if(empty($this->fields)) { // 如果数据表字段没有定义则自动获取 if(C('DB_FIELDS_CACHE')) { $db = $this->dbName?:C('DB_NAME'); $fields = F('_fields/'.strtolower($db.'.'.$this->tablePrefix.$this->name)); if($fields) { $this->fields = $fields; if(!empty($fields['_pk'])){ $this->pk = $fields['_pk']; } return ; } } // 每次都会读取数据表信息 $this->flush(); } } /** * 获取字段信息并缓存 * @access public * @return void */ public function flush() { // 缓存不存在则查询数据表信息 $this->db->setModel($this->name); $fields = $this->db->getFields($this->getTableName()); if(!$fields) { // 无法获取字段信息 return false; } $this->fields = array_keys($fields); unset($this->fields['_pk']); foreach ($fields as $key=>$val){ // 记录字段类型 $type[$key] = $val['type']; if($val['primary']) { // 增加复合主键支持 if (isset($this->fields['_pk']) && $this->fields['_pk'] != null) { if (is_string($this->fields['_pk'])) { $this->pk = array($this->fields['_pk']); $this->fields['_pk'] = $this->pk; } $this->pk[] = $key; $this->fields['_pk'][] = $key; } else { $this->pk = $key; $this->fields['_pk'] = $key; } if($val['autoinc']) $this->autoinc = true; } } // 记录字段类型信息 $this->fields['_type'] = $type; // 2008-3-7 增加缓存开关控制 if(C('DB_FIELDS_CACHE')){ // 永久缓存数据表信息 $db = $this->dbName?:C('DB_NAME'); F('_fields/'.strtolower($db.'.'.$this->tablePrefix.$this->name),$this->fields); } } /** * 设置数据对象的值 * @access public * @param string $name 名称 * @param mixed $value 值 * @return void */ public function __set($name,$value) { // 设置数据对象属性 $this->data[$name] = $value; } /** * 获取数据对象的值 * @access public * @param string $name 名称 * @return mixed */ public function __get($name) { return isset($this->data[$name])?$this->data[$name]:null; } /** * 检测数据对象的值 * @access public * @param string $name 名称 * @return boolean */ public function __isset($name) { return isset($this->data[$name]); } /** * 销毁数据对象的值 * @access public * @param string $name 名称 * @return void */ public function __unset($name) { unset($this->data[$name]); } /** * 利用__call方法实现一些特殊的Model方法 * @access public * @param string $method 方法名称 * @param array $args 调用参数 * @return mixed */ public function __call($method,$args) { if(in_array(strtolower($method),$this->methods,true)) { // 连贯操作的实现 $this->options[strtolower($method)] = $args[0]; return $this; }elseif(in_array(strtolower($method),array('count','sum','min','max','avg'),true)){ // 统计查询的实现 $field = isset($args[0])?$args[0]:'*'; return $this->getField(strtoupper($method).'('.$field.') AS tp_'.$method); }elseif(strtolower(substr($method,0,5))=='getby') { // 根据某个字段获取记录 $field = parse_name(substr($method,5)); $where[$field] = $args[0]; return $this->where($where)->find(); }elseif(strtolower(substr($method,0,10))=='getfieldby') { // 根据某个字段获取记录的某个值 $name = parse_name(substr($method,10)); $where[$name] =$args[0]; return $this->where($where)->getField($args[1]); }elseif(isset($this->_scope[$method])){// 命名范围的单独调用支持 return $this->scope($method,$args[0]); }else{ E(__CLASS__.':'.$method.L('_METHOD_NOT_EXIST_')); return; } } // 回调方法 初始化模型 protected function _initialize() {} /** * 对保存到数据库的数据进行处理 * @access protected * @param mixed $data 要操作的数据 * @return boolean */ protected function _facade($data) { // 检查数据字段合法性 if(!empty($this->fields)) { if(!empty($this->options['field'])) { $fields = $this->options['field']; unset($this->options['field']); if(is_string($fields)) { $fields = explode(',',$fields); } }else{ $fields = $this->fields; } foreach ($data as $key=>$val){ if(!in_array($key,$fields,true)){ if(!empty($this->options['strict'])){ E(L('_DATA_TYPE_INVALID_').':['.$key.'=>'.$val.']'); } unset($data[$key]); }elseif(is_scalar($val)) { // 字段类型检查 和 强制转换 $this->_parseType($data,$key); } } } // 安全过滤 if(!empty($this->options['filter'])) { $data = array_map($this->options['filter'],$data); unset($this->options['filter']); } $this->_before_write($data); return $data; } // 写入数据前的回调方法 包括新增和更新 protected function _before_write(&$data) {} /** * 新增数据 * @access public * @param mixed $data 数据 * @param array $options 表达式 * @param boolean $replace 是否replace * @return mixed */ public function add($data='',$options=array(),$replace=false) { if(empty($data)) { // 没有传递数据,获取当前数据对象的值 if(!empty($this->data)) { $data = $this->data; // 重置数据 $this->data = array(); }else{ $this->error = L('_DATA_TYPE_INVALID_'); return false; } } // 数据处理 $data = $this->_facade($data); // 分析表达式 $options = $this->_parseOptions($options); if(false === $this->_before_insert($data,$options)) { return false; } // 写入数据到数据库 $result = $this->db->insert($data,$options,$replace); if(false !== $result && is_numeric($result)) { $pk = $this->getPk(); // 增加复合主键支持 if (is_array($pk)) return $result; $insertId = $this->getLastInsID(); if($insertId) { // 自增主键返回插入ID $data[$pk] = $insertId; if(false === $this->_after_insert($data,$options)) { return false; } return $insertId; } if(false === $this->_after_insert($data,$options)) { return false; } } return $result; } // 插入数据前的回调方法 protected function _before_insert(&$data,$options) {} // 插入成功后的回调方法 protected function _after_insert($data,$options) {} public function addAll($dataList,$options=array(),$replace=false){ if(empty($dataList)) { $this->error = L('_DATA_TYPE_INVALID_'); return false; } // 数据处理 foreach ($dataList as $key=>$data){ $dataList[$key] = $this->_facade($data); } // 分析表达式 $options = $this->_parseOptions($options); // 写入数据到数据库 $result = $this->db->insertAll($dataList,$options,$replace); if(false !== $result ) { $insertId = $this->getLastInsID(); if($insertId) { return $insertId; } } return $result; } /** * 通过Select方式添加记录 * @access public * @param string $fields 要插入的数据表字段名 * @param string $table 要插入的数据表名 * @param array $options 表达式 * @return boolean */ public function selectAdd($fields='',$table='',$options=array()) { // 分析表达式 $options = $this->_parseOptions($options); // 写入数据到数据库 if(false === $result = $this->db->selectInsert($fields?:$options['field'],$table?:$this->getTableName(),$options)){ // 数据库插入操作失败 $this->error = L('_OPERATION_WRONG_'); return false; }else { // 插入成功 return $result; } } /** * 保存数据 * @access public * @param mixed $data 数据 * @param array $options 表达式 * @return boolean */ public function save($data='',$options=array()) { if(empty($data)) { // 没有传递数据,获取当前数据对象的值 if(!empty($this->data)) { $data = $this->data; // 重置数据 $this->data = array(); }else{ $this->error = L('_DATA_TYPE_INVALID_'); return false; } } // 数据处理 $data = $this->_facade($data); if(empty($data)){ // 没有数据则不执行 $this->error = L('_DATA_TYPE_INVALID_'); return false; } // 分析表达式 $options = $this->_parseOptions($options); $pk = $this->getPk(); if(!isset($options['where']) ) { // 如果存在主键数据 则自动作为更新条件 if (is_string($pk) && isset($data[$pk])) { $where[$pk] = $data[$pk]; unset($data[$pk]); } elseif (is_array($pk)) { // 增加复合主键支持 foreach ($pk as $field) { if(isset($data[$field])) { $where[$field] = $data[$field]; } else { // 如果缺少复合主键数据则不执行 $this->error = L('_OPERATION_WRONG_'); return false; } unset($data[$field]); } } if(!isset($where)){ // 如果没有任何更新条件则不执行 $this->error = L('_OPERATION_WRONG_'); return false; }else{ $options['where'] = $where; } } if(is_array($options['where']) && isset($options['where'][$pk])){ $pkValue = $options['where'][$pk]; } if(false === $this->_before_update($data,$options)) { return false; } $result = $this->db->update($data,$options); if(false !== $result && is_numeric($result)) { if(isset($pkValue)) $data[$pk] = $pkValue; $this->_after_update($data,$options); } return $result; } // 更新数据前的回调方法 protected function _before_update(&$data,$options) {} // 更新成功后的回调方法 protected function _after_update($data,$options) {} /** * 删除数据 * @access public * @param mixed $options 表达式 * @return mixed */ public function delete($options=array()) { $pk = $this->getPk(); if(empty($options) && empty($this->options['where'])) { // 如果删除条件为空 则删除当前数据对象所对应的记录 if(!empty($this->data) && isset($this->data[$pk])) return $this->delete($this->data[$pk]); else return false; } if(is_numeric($options) || is_string($options)) { // 根据主键删除记录 if(strpos($options,',')) { $where[$pk] = array('IN', $options); }else{ $where[$pk] = $options; } $options = array(); $options['where'] = $where; } // 根据复合主键删除记录 if (is_array($options) && (count($options) > 0) && is_array($pk)) { $count = 0; foreach (array_keys($options) as $key) { if (is_int($key)) $count++; } if ($count == count($pk)) { $i = 0; foreach ($pk as $field) { $where[$field] = $options[$i]; unset($options[$i++]); } $options['where'] = $where; } else { return false; } } // 分析表达式 $options = $this->_parseOptions($options); if(empty($options['where'])){ // 如果条件为空 不进行删除操作 除非设置 1=1 return false; } if(is_array($options['where']) && isset($options['where'][$pk])){ $pkValue = $options['where'][$pk]; } if(false === $this->_before_delete($options)) { return false; } $result = $this->db->delete($options); if(false !== $result && is_numeric($result)) { $data = array(); if(isset($pkValue)) $data[$pk] = $pkValue; $this->_after_delete($data,$options); } // 返回删除记录个数 return $result; } // 删除数据前的回调方法 protected function _before_delete($options) {} // 删除成功后的回调方法 protected function _after_delete($data,$options) {} /** * 查询数据集 * @access public * @param array $options 表达式参数 * @return mixed */ public function select($options=array()) { $pk = $this->getPk(); if(is_string($options) || is_numeric($options)) { // 根据主键查询 if(strpos($options,',')) { $where[$pk] = array('IN',$options); }else{ $where[$pk] = $options; } $options = array(); $options['where'] = $where; }elseif (is_array($options) && (count($options) > 0) && is_array($pk)) { // 根据复合主键查询 $count = 0; foreach (array_keys($options) as $key) { if (is_int($key)) $count++; } if ($count == count($pk)) { $i = 0; foreach ($pk as $field) { $where[$field] = $options[$i]; unset($options[$i++]); } $options['where'] = $where; } else { return false; } } elseif(false === $options){ // 用于子查询 不查询只返回SQL $options['fetch_sql'] = true; } // 分析表达式 $options = $this->_parseOptions($options); // 判断查询缓存 if(isset($options['cache'])){ $cache = $options['cache']; $key = is_string($cache['key'])?$cache['key']:md5(serialize($options)); $data = S($key,'',$cache); if(false !== $data){ return $data; } } $resultSet = $this->db->select($options); if(false === $resultSet) { return false; } if(!empty($resultSet)) { // 有查询结果 if(is_string($resultSet)){ return $resultSet; } $resultSet = array_map(array($this,'_read_data'),$resultSet); $this->_after_select($resultSet,$options); if(isset($options['index'])){ // 对数据集进行索引 $index = explode(',',$options['index']); foreach ($resultSet as $result){ $_key = $result[$index[0]]; if(isset($index[1]) && isset($result[$index[1]])){ $cols[$_key] = $result[$index[1]]; }else{ $cols[$_key] = $result; } } $resultSet = $cols; } } if(isset($cache)){ S($key,$resultSet,$cache); } return $resultSet; } // 查询成功后的回调方法 protected function _after_select(&$resultSet,$options) {} /** * 生成查询SQL 可用于子查询 * @access public * @return string */ public function buildSql() { return '( '.$this->fetchSql(true)->select().' )'; } /** * 分析表达式 * @access protected * @param array $options 表达式参数 * @return array */ protected function _parseOptions($options=array()) { if(is_array($options)) $options = array_merge($this->options,$options); if(!isset($options['table'])){ // 自动获取表名 $options['table'] = $this->getTableName(); $fields = $this->fields; }else{ // 指定数据表 则重新获取字段列表 但不支持类型检测 $fields = $this->getDbFields(); } // 数据表别名 if(!empty($options['alias'])) { $options['table'] .= ' '.$options['alias']; } // 记录操作的模型名称 $options['model'] = $this->name; // 字段类型验证 if(isset($options['where']) && is_array($options['where']) && !empty($fields) && !isset($options['join'])) { // 对数组查询条件进行字段类型检查 foreach ($options['where'] as $key=>$val){ $key = trim($key); if(in_array($key,$fields,true)){ if(is_scalar($val)) { $this->_parseType($options['where'],$key); } }elseif(!is_numeric($key) && '_' != substr($key,0,1) && false === strpos($key,'.') && false === strpos($key,'(') && false === strpos($key,'|') && false === strpos($key,'&')){ if(!empty($this->options['strict'])){ E(L('_ERROR_QUERY_EXPRESS_').':['.$key.'=>'.$val.']'); } unset($options['where'][$key]); } } } // 查询过后清空sql表达式组装 避免影响下次查询 $this->options = array(); // 表达式过滤 $this->_options_filter($options); return $options; } // 表达式过滤回调方法 protected function _options_filter(&$options) {} /** * 数据类型检测 * @access protected * @param mixed $data 数据 * @param string $key 字段名 * @return void */ protected function _parseType(&$data,$key) { if(!isset($this->options['bind'][':'.$key]) && isset($this->fields['_type'][$key])){ $fieldType = strtolower($this->fields['_type'][$key]); if(false !== strpos($fieldType,'enum')){ // 支持ENUM类型优先检测 }elseif(false === strpos($fieldType,'bigint') && false !== strpos($fieldType,'int')) { $data[$key] = intval($data[$key]); }elseif(false !== strpos($fieldType,'float') || false !== strpos($fieldType,'double')){ $data[$key] = floatval($data[$key]); }elseif(false !== strpos($fieldType,'bool')){ $data[$key] = (bool)$data[$key]; } } } /** * 数据读取后的处理 * @access protected * @param array $data 当前数据 * @return array */ protected function _read_data($data) { // 检查字段映射 if(!empty($this->_map) && C('READ_DATA_MAP')) { foreach ($this->_map as $key=>$val){ if(isset($data[$val])) { $data[$key] = $data[$val]; unset($data[$val]); } } } return $data; } /** * 查询数据 * @access public * @param mixed $options 表达式参数 * @return mixed */ public function find($options=array()) { if(is_numeric($options) || is_string($options)) { $where[$this->getPk()] = $options; $options = array(); $options['where'] = $where; } // 根据复合主键查找记录 $pk = $this->getPk(); if (is_array($options) && (count($options) > 0) && is_array($pk)) { // 根据复合主键查询 $count = 0; foreach (array_keys($options) as $key) { if (is_int($key)) $count++; } if ($count == count($pk)) { $i = 0; foreach ($pk as $field) { $where[$field] = $options[$i]; unset($options[$i++]); } $options['where'] = $where; } else { return false; } } // 总是查找一条记录 $options['limit'] = 1; // 分析表达式 $options = $this->_parseOptions($options); // 判断查询缓存 if(isset($options['cache'])){ $cache = $options['cache']; $key = is_string($cache['key'])?$cache['key']:md5(serialize($options)); $data = S($key,'',$cache); if(false !== $data){ $this->data = $data; return $data; } } $resultSet = $this->db->select($options); if(false === $resultSet) { return false; } if(empty($resultSet)) {// 查询结果为空 return null; } if(is_string($resultSet)){ return $resultSet; } // 读取数据后的处理 $data = $this->_read_data($resultSet[0]); $this->_after_find($data,$options); if(!empty($this->options['result'])) { return $this->returnResult($data,$this->options['result']); } $this->data = $data; if(isset($cache)){ S($key,$data,$cache); } return $this->data; } // 查询成功的回调方法 protected function _after_find(&$result,$options) {} protected function returnResult($data,$type=''){ if ($type){ if(is_callable($type)){ return call_user_func($type,$data); } switch (strtolower($type)){ case 'json': return json_encode($data); case 'xml': return xml_encode($data); } } return $data; } /** * 处理字段映射 * @access public * @param array $data 当前数据 * @param integer $type 类型 0 写入 1 读取 * @return array */ public function parseFieldsMap($data,$type=1) { // 检查字段映射 if(!empty($this->_map)) { foreach ($this->_map as $key=>$val){ if($type==1) { // 读取 if(isset($data[$val])) { $data[$key] = $data[$val]; unset($data[$val]); } }else{ if(isset($data[$key])) { $data[$val] = $data[$key]; unset($data[$key]); } } } } return $data; } /** * 设置记录的某个字段值 * 支持使用数据库字段和方法 * @access public * @param string|array $field 字段名 * @param string $value 字段值 * @return boolean */ public function setField($field,$value='') { if(is_array($field)) { $data = $field; }else{ $data[$field] = $value; } return $this->save($data); } /** * 字段值增长 * @access public * @param string $field 字段名 * @param integer $step 增长值 * @param integer $lazyTime 延时时间(s) * @return boolean */ public function setInc($field,$step=1,$lazyTime=0) { if($lazyTime>0) {// 延迟写入 $condition = $this->options['where']; $guid = md5($this->name.'_'.$field.'_'.serialize($condition)); $step = $this->lazyWrite($guid,$step,$lazyTime); if(empty($step)) { return true; // 等待下次写入 }elseif($step < 0) { $step = '-'.$step; } } return $this->setField($field,array('exp',$field.'+'.$step)); } /** * 字段值减少 * @access public * @param string $field 字段名 * @param integer $step 减少值 * @param integer $lazyTime 延时时间(s) * @return boolean */ public function setDec($field,$step=1,$lazyTime=0) { if($lazyTime>0) {// 延迟写入 $condition = $this->options['where']; $guid = md5($this->name.'_'.$field.'_'.serialize($condition)); $step = $this->lazyWrite($guid,-$step,$lazyTime); if(empty($step)) { return true; // 等待下次写入 }elseif($step > 0) { $step = '-'.$step; } } return $this->setField($field,array('exp',$field.'-'.$step)); } /** * 延时更新检查 返回false表示需要延时 * 否则返回实际写入的数值 * @access public * @param string $guid 写入标识 * @param integer $step 写入步进值 * @param integer $lazyTime 延时时间(s) * @return false|integer */ protected function lazyWrite($guid,$step,$lazyTime) { if(false !== ($value = S($guid))) { // 存在缓存写入数据 if(NOW_TIME > S($guid.'_time')+$lazyTime) { // 延时更新时间到了,删除缓存数据 并实际写入数据库 S($guid,NULL); S($guid.'_time',NULL); return $value+$step; }else{ // 追加数据到缓存 S($guid,$value+$step); return false; } }else{ // 没有缓存数据 S($guid,$step); // 计时开始 S($guid.'_time',NOW_TIME); return false; } } /** * 获取一条记录的某个字段值 * @access public * @param string $field 字段名 * @param string $spea 字段数据间隔符号 NULL返回数组 * @return mixed */ public function getField($field,$sepa=null) { $options['field'] = $field; $options = $this->_parseOptions($options); // 判断查询缓存 if(isset($options['cache'])){ $cache = $options['cache']; $key = is_string($cache['key'])?$cache['key']:md5($sepa.serialize($options)); $data = S($key,'',$cache); if(false !== $data){ return $data; } } $field = trim($field); if(strpos($field,',') && false !== $sepa) { // 多字段 if(!isset($options['limit'])){ $options['limit'] = is_numeric($sepa)?$sepa:''; } $resultSet = $this->db->select($options); if(!empty($resultSet)) { if(is_string($resultSet)){ return $resultSet; } $_field = explode(',', $field); $field = array_keys($resultSet[0]); $key1 = array_shift($field); $key2 = array_shift($field); $cols = array(); $count = count($_field); foreach ($resultSet as $result){ $name = $result[$key1]; if(2==$count) { $cols[$name] = $result[$key2]; }else{ $cols[$name] = is_string($sepa)?implode($sepa,array_slice($result,1)):$result; } } if(isset($cache)){ S($key,$cols,$cache); } return $cols; } }else{ // 查找一条记录 // 返回数据个数 if(true !== $sepa) {// 当sepa指定为true的时候 返回所有数据 $options['limit'] = is_numeric($sepa)?$sepa:1; } $result = $this->db->select($options); if(!empty($result)) { if(is_string($result)){ return $result; } if(true !== $sepa && 1==$options['limit']) { $data = reset($result[0]); if(isset($cache)){ S($key,$data,$cache); } return $data; } foreach ($result as $val){ $array[] = $val[$field]; } if(isset($cache)){ S($key,$array,$cache); } return $array; } } return null; } /** * 创建数据对象 但不保存到数据库 * @access public * @param mixed $data 创建数据 * @param string $type 状态 * @return mixed */ public function create($data='',$type='') { // 如果没有传值默认取POST数据 if(empty($data)) { $data = I('post.'); }elseif(is_object($data)){ $data = get_object_vars($data); } // 验证数据 if(empty($data) || !is_array($data)) { $this->error = L('_DATA_TYPE_INVALID_'); return false; } // 状态 $type = $type?:(!empty($data[$this->getPk()])?self::MODEL_UPDATE:self::MODEL_INSERT); // 检查字段映射 $data = $this->parseFieldsMap($data,0); // 检测提交字段的合法性 if(isset($this->options['field'])) { // $this->field('field1,field2...')->create() $fields = $this->options['field']; unset($this->options['field']); }elseif($type == self::MODEL_INSERT && isset($this->insertFields)) { $fields = $this->insertFields; }elseif($type == self::MODEL_UPDATE && isset($this->updateFields)) { $fields = $this->updateFields; } if(isset($fields)) { if(is_string($fields)) { $fields = explode(',',$fields); } // 判断令牌验证字段 if(C('TOKEN_ON')) $fields[] = C('TOKEN_NAME', null, '__hash__'); foreach ($data as $key=>$val){ if(!in_array($key,$fields)) { unset($data[$key]); } } } // 数据自动验证 if(!$this->autoValidation($data,$type)) return false; // 表单令牌验证 if(!$this->autoCheckToken($data)) { $this->error = L('_TOKEN_ERROR_'); return false; } // 验证完成生成数据对象 if($this->autoCheckFields) { // 开启字段检测 则过滤非法字段数据 $fields = $this->getDbFields(); foreach ($data as $key=>$val){ if(!in_array($key,$fields)) { unset($data[$key]); }elseif(MAGIC_QUOTES_GPC && is_string($val)){ $data[$key] = stripslashes($val); } } } // 创建完成对数据进行自动处理 $this->autoOperation($data,$type); // 赋值当前数据对象 $this->data = $data; // 返回创建的数据以供其他调用 return $data; } // 自动表单令牌验证 // TODO ajax无刷新多次提交暂不能满足 public function autoCheckToken($data) { // 支持使用token(false) 关闭令牌验证 if(isset($this->options['token']) && !$this->options['token']) return true; if(C('TOKEN_ON')){ $name = C('TOKEN_NAME', null, '__hash__'); if(!isset($data[$name]) || !isset($_SESSION[$name])) { // 令牌数据无效 return false; } // 令牌验证 list($key,$value) = explode('_',$data[$name]); if(isset($_SESSION[$name][$key]) && $value && $_SESSION[$name][$key] === $value) { // 防止重复提交 unset($_SESSION[$name][$key]); // 验证完成销毁session return true; } // 开启TOKEN重置 if(C('TOKEN_RESET')) unset($_SESSION[$name][$key]); return false; } return true; } /** * 使用正则验证数据 * @access public * @param string $value 要验证的数据 * @param string $rule 验证规则 * @return boolean */ public function regex($value,$rule) { $validate = array( 'require' => '/\S+/', 'email' => '/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/', 'url' => '/^http(s?):\/\/(?:[A-za-z0-9-]+\.)+[A-za-z]{2,4}(:\d+)?(?:[\/\?#][\/=\?%\-&~`@[\]\':+!\.#\w]*)?$/', 'currency' => '/^\d+(\.\d+)?$/', 'number' => '/^\d+$/', 'zip' => '/^\d{6}$/', 'integer' => '/^[-\+]?\d+$/', 'double' => '/^[-\+]?\d+(\.\d+)?$/', 'english' => '/^[A-Za-z]+$/', ); // 检查是否有内置的正则表达式 if(isset($validate[strtolower($rule)])) $rule = $validate[strtolower($rule)]; return preg_match($rule,$value)===1; } /** * 自动表单处理 * @access public * @param array $data 创建数据 * @param string $type 创建类型 * @return mixed */ private function autoOperation(&$data,$type) { if(false === $this->options['auto']){ // 关闭自动完成 return $data; } if(!empty($this->options['auto'])) { $_auto = $this->options['auto']; unset($this->options['auto']); }elseif(!empty($this->_auto)){ $_auto = $this->_auto; } // 自动填充 if(isset($_auto)) { foreach ($_auto as $auto){ // 填充因子定义格式 // array('field','填充内容','填充条件','附加规则',[额外参数]) if(empty($auto[2])) $auto[2] = self::MODEL_INSERT; // 默认为新增的时候自动填充 if( $type == $auto[2] || $auto[2] == self::MODEL_BOTH) { if(empty($auto[3])) $auto[3] = 'string'; switch(trim($auto[3])) { case 'function': // 使用函数进行填充 字段的值作为参数 case 'callback': // 使用回调方法 $args = isset($auto[4])?(array)$auto[4]:array(); if(isset($data[$auto[0]])) { array_unshift($args,$data[$auto[0]]); } if('function'==$auto[3]) { $data[$auto[0]] = call_user_func_array($auto[1], $args); }else{ $data[$auto[0]] = call_user_func_array(array(&$this,$auto[1]), $args); } break; case 'field': // 用其它字段的值进行填充 $data[$auto[0]] = $data[$auto[1]]; break; case 'ignore': // 为空忽略 if($auto[1]===$data[$auto[0]]) unset($data[$auto[0]]); break; case 'string': default: // 默认作为字符串填充 $data[$auto[0]] = $auto[1]; } if(isset($data[$auto[0]]) && false === $data[$auto[0]] ) unset($data[$auto[0]]); } } } return $data; } /** * 自动表单验证 * @access protected * @param array $data 创建数据 * @param string $type 创建类型 * @return boolean */ protected function autoValidation($data,$type) { if(false === $this->options['validate'] ){ // 关闭自动验证 return true; } if(!empty($this->options['validate'])) { $_validate = $this->options['validate']; unset($this->options['validate']); }elseif(!empty($this->_validate)){ $_validate = $this->_validate; } // 属性验证 if(isset($_validate)) { // 如果设置了数据自动验证则进行数据验证 if($this->patchValidate) { // 重置验证错误信息 $this->error = array(); } foreach($_validate as $key=>$val) { // 验证因子定义格式 // array(field,rule,message,condition,type,when,params) // 判断是否需要执行验证 if(empty($val[5]) || ( $val[5]== self::MODEL_BOTH && $type < 3 ) || $val[5]== $type ) { if(0==strpos($val[2],'{%') && strpos($val[2],'}')) // 支持提示信息的多语言 使用 {%语言定义} 方式 $val[2] = L(substr($val[2],2,-1)); $val[3] = isset($val[3])?$val[3]:self::EXISTS_VALIDATE; $val[4] = isset($val[4])?$val[4]:'regex'; // 判断验证条件 switch($val[3]) { case self::MUST_VALIDATE: // 必须验证 不管表单是否有设置该字段 if(false === $this->_validationField($data,$val)) return false; break; case self::VALUE_VALIDATE: // 值不为空的时候才验证 if('' != trim($data[$val[0]])) if(false === $this->_validationField($data,$val)) return false; break; default: // 默认表单存在该字段就验证 if(isset($data[$val[0]])) if(false === $this->_validationField($data,$val)) return false; } } } // 批量验证的时候最后返回错误 if(!empty($this->error)) return false; } return true; } /** * 验证表单字段 支持批量验证 * 如果批量验证返回错误的数组信息 * @access protected * @param array $data 创建数据 * @param array $val 验证因子 * @return boolean */ protected function _validationField($data,$val) { if($this->patchValidate && isset($this->error[$val[0]])) return ; //当前字段已经有规则验证没有通过 if(false === $this->_validationFieldItem($data,$val)){ if($this->patchValidate) { $this->error[$val[0]] = $val[2]; }else{ $this->error = $val[2]; return false; } } return ; } /** * 根据验证因子验证字段 * @access protected * @param array $data 创建数据 * @param array $val 验证因子 * @return boolean */ protected function _validationFieldItem($data,$val) { switch(strtolower(trim($val[4]))) { case 'function':// 使用函数进行验证 case 'callback':// 调用方法进行验证 $args = isset($val[6])?(array)$val[6]:array(); if(is_string($val[0]) && strpos($val[0], ',')) $val[0] = explode(',', $val[0]); if(is_array($val[0])){ // 支持多个字段验证 foreach($val[0] as $field) $_data[$field] = $data[$field]; array_unshift($args, $_data); }else{ array_unshift($args, $data[$val[0]]); } if('function'==$val[4]) { return call_user_func_array($val[1], $args); }else{ return call_user_func_array(array(&$this, $val[1]), $args); } case 'confirm': // 验证两个字段是否相同 return $data[$val[0]] == $data[$val[1]]; case 'unique': // 验证某个值是否唯一 if(is_string($val[0]) && strpos($val[0],',')) $val[0] = explode(',',$val[0]); $map = array(); if(is_array($val[0])) { // 支持多个字段验证 foreach ($val[0] as $field) $map[$field] = $data[$field]; }else{ $map[$val[0]] = $data[$val[0]]; } $pk = $this->getPk(); if(!empty($data[$pk]) && is_string($pk)) { // 完善编辑的时候验证唯一 $map[$pk] = array('neq',$data[$pk]); } if($this->where($map)->find()) return false; return true; default: // 检查附加规则 return $this->check($data[$val[0]],$val[1],$val[4]); } } /** * 验证数据 支持 in between equal length regex expire ip_allow ip_deny * @access public * @param string $value 验证数据 * @param mixed $rule 验证表达式 * @param string $type 验证方式 默认为正则验证 * @return boolean */ public function check($value,$rule,$type='regex'){ $type = strtolower(trim($type)); switch($type) { case 'in': // 验证是否在某个指定范围之内 逗号分隔字符串或者数组 case 'notin': $range = is_array($rule)? $rule : explode(',',$rule); return $type == 'in' ? in_array($value ,$range) : !in_array($value ,$range); case 'between': // 验证是否在某个范围 case 'notbetween': // 验证是否不在某个范围 if (is_array($rule)){ $min = $rule[0]; $max = $rule[1]; }else{ list($min,$max) = explode(',',$rule); } return $type == 'between' ? $value>=$min && $value<=$max : $value<$min || $value>$max; case 'equal': // 验证是否等于某个值 case 'notequal': // 验证是否等于某个值 return $type == 'equal' ? $value == $rule : $value != $rule; case 'length': // 验证长度 $length = mb_strlen($value,'utf-8'); // 当前数据长度 if(strpos($rule,',')) { // 长度区间 list($min,$max) = explode(',',$rule); return $length >= $min && $length <= $max; }else{// 指定长度 return $length == $rule; } case 'expire': list($start,$end) = explode(',',$rule); if(!is_numeric($start)) $start = strtotime($start); if(!is_numeric($end)) $end = strtotime($end); return NOW_TIME >= $start && NOW_TIME <= $end; case 'ip_allow': // IP 操作许可验证 return in_array(get_client_ip(),explode(',',$rule)); case 'ip_deny': // IP 操作禁止验证 return !in_array(get_client_ip(),explode(',',$rule)); case 'regex': default: // 默认使用正则验证 可以使用验证类中定义的验证名称 // 检查附加规则 return $this->regex($value,$rule); } } /** * 存储过程返回多数据集 * @access public * @param string $sql SQL指令 * @param mixed $parse 是否需要解析SQL * @return array */ public function procedure($sql, $parse = false) { return $this->db->procedure($sql, $parse); } /** * SQL查询 * @access public * @param string $sql SQL指令 * @param mixed $parse 是否需要解析SQL * @return mixed */ public function query($sql,$parse=false) { if(!is_bool($parse) && !is_array($parse)) { $parse = func_get_args(); array_shift($parse); } $sql = $this->parseSql($sql,$parse); return $this->db->query($sql); } /** * 执行SQL语句 * @access public * @param string $sql SQL指令 * @param mixed $parse 是否需要解析SQL * @return false | integer */ public function execute($sql,$parse=false) { if(!is_bool($parse) && !is_array($parse)) { $parse = func_get_args(); array_shift($parse); } $sql = $this->parseSql($sql,$parse); return $this->db->execute($sql); } /** * 解析SQL语句 * @access public * @param string $sql SQL指令 * @param boolean $parse 是否需要解析SQL * @return string */ protected function parseSql($sql,$parse) { // 分析表达式 if(true === $parse) { $options = $this->_parseOptions(); $sql = $this->db->parseSql($sql,$options); }elseif(is_array($parse)){ // SQL预处理 $parse = array_map(array($this->db,'escapeString'),$parse); $sql = vsprintf($sql,$parse); }else{ $sql = strtr($sql,array('__TABLE__'=>$this->getTableName(),'__PREFIX__'=>$this->tablePrefix)); $prefix = $this->tablePrefix; $sql = preg_replace_callback("/__([A-Z0-9_-]+)__/sU", function($match) use($prefix){ return $prefix.strtolower($match[1]);}, $sql); } $this->db->setModel($this->name); return $sql; } /** * 切换当前的数据库连接 * @access public * @param integer $linkNum 连接序号 * @param mixed $config 数据库连接信息 * @param boolean $force 强制重新连接 * @return Model */ public function db($linkNum='',$config='',$force=false) { if('' === $linkNum && $this->db) { return $this->db; } if(!isset($this->_db[$linkNum]) || $force ) { // 创建一个新的实例 if(!empty($config) && is_string($config) && false === strpos($config,'/')) { // 支持读取配置参数 $config = C($config); } $this->_db[$linkNum] = Db::getInstance($config); }elseif(NULL === $config){ $this->_db[$linkNum]->close(); // 关闭数据库连接 unset($this->_db[$linkNum]); return ; } // 切换数据库连接 $this->db = $this->_db[$linkNum]; $this->_after_db(); // 字段检测 if(!empty($this->name) && $this->autoCheckFields) $this->_checkTableInfo(); return $this; } // 数据库切换后回调方法 protected function _after_db() {} /** * 得到当前的数据对象名称 * @access public * @return string */ public function getModelName() { if(empty($this->name)){ $name = substr(get_class($this),0,-strlen(C('DEFAULT_M_LAYER'))); if ( $pos = strrpos($name,'\\') ) {//有命名空间 $this->name = substr($name,$pos+1); }else{ $this->name = $name; } } return $this->name; } /** * 得到完整的数据表名 * @access public * @return string */ public function getTableName() { if(empty($this->trueTableName)) { $tableName = !empty($this->tablePrefix) ? $this->tablePrefix : ''; if(!empty($this->tableName)) { $tableName .= $this->tableName; }else{ $tableName .= parse_name($this->name); } $this->trueTableName = strtolower($tableName); } return (!empty($this->dbName)?$this->dbName.'.':'').$this->trueTableName; } /** * 启动事务 * @access public * @return void */ public function startTrans() { $this->commit(); $this->db->startTrans(); return ; } /** * 提交事务 * @access public * @return boolean */ public function commit() { return $this->db->commit(); } /** * 事务回滚 * @access public * @return boolean */ public function rollback() { return $this->db->rollback(); } /** * 返回模型的错误信息 * @access public * @return string */ public function getError(){ return $this->error; } /** * 返回数据库的错误信息 * @access public * @return string */ public function getDbError() { return $this->db->getError(); } /** * 返回最后插入的ID * @access public * @return string */ public function getLastInsID() { return $this->db->getLastInsID(); } /** * 返回最后执行的sql语句 * @access public * @return string */ public function getLastSql() { return $this->db->getLastSql($this->name); } // 鉴于getLastSql比较常用 增加_sql 别名 public function _sql(){ return $this->getLastSql(); } /** * 获取主键名称 * @access public * @return string */ public function getPk() { return $this->pk; } /** * 获取数据表字段信息 * @access public * @return array */ public function getDbFields(){ if(isset($this->options['table'])) {// 动态指定表名 if(is_array($this->options['table'])){ $table = key($this->options['table']); }else{ $table = $this->options['table']; if(strpos($table,')')){ // 子查询 return false; } } $fields = $this->db->getFields($table); return $fields ? array_keys($fields) : false; } if($this->fields) { $fields = $this->fields; unset($fields['_type'],$fields['_pk']); return $fields; } return false; } /** * 设置数据对象值 * @access public * @param mixed $data 数据 * @return Model */ public function data($data=''){ if('' === $data && !empty($this->data)) { return $this->data; } if(is_object($data)){ $data = get_object_vars($data); }elseif(is_string($data)){ parse_str($data,$data); }elseif(!is_array($data)){ E(L('_DATA_TYPE_INVALID_')); } $this->data = $data; return $this; } /** * 指定当前的数据表 * @access public * @param mixed $table * @return Model */ public function table($table) { $prefix = $this->tablePrefix; if(is_array($table)) { $this->options['table'] = $table; }elseif(!empty($table)) { //将__TABLE_NAME__替换成带前缀的表名 $table = preg_replace_callback("/__([A-Z0-9_-]+)__/sU", function($match) use($prefix){ return $prefix.strtolower($match[1]);}, $table); $this->options['table'] = $table; } return $this; } /** * USING支持 用于多表删除 * @access public * @param mixed $using * @return Model */ public function using($using){ $prefix = $this->tablePrefix; if(is_array($using)) { $this->options['using'] = $using; }elseif(!empty($using)) { //将__TABLE_NAME__替换成带前缀的表名 $using = preg_replace_callback("/__([A-Z0-9_-]+)__/sU", function($match) use($prefix){ return $prefix.strtolower($match[1]);}, $using); $this->options['using'] = $using; } return $this; } /** * 查询SQL组装 join * @access public * @param mixed $join * @param string $type JOIN类型 * @return Model */ public function join($join,$type='INNER') { $prefix = $this->tablePrefix; if(is_array($join)) { foreach ($join as $key=>&$_join){ $_join = preg_replace_callback("/__([A-Z0-9_-]+)__/sU", function($match) use($prefix){ return $prefix.strtolower($match[1]);}, $_join); $_join = false !== stripos($_join,'JOIN')? $_join : $type.' JOIN ' .$_join; } $this->options['join'] = $join; }elseif(!empty($join)) { //将__TABLE_NAME__字符串替换成带前缀的表名 $join = preg_replace_callback("/__([A-Z0-9_-]+)__/sU", function($match) use($prefix){ return $prefix.strtolower($match[1]);}, $join); $this->options['join'][] = false !== stripos($join,'JOIN')? $join : $type.' JOIN '.$join; } return $this; } /** * 查询SQL组装 union * @access public * @param mixed $union * @param boolean $all * @return Model */ public function union($union,$all=false) { if(empty($union)) return $this; if($all) { $this->options['union']['_all'] = true; } if(is_object($union)) { $union = get_object_vars($union); } // 转换union表达式 if(is_string($union) ) { $prefix = $this->tablePrefix; //将__TABLE_NAME__字符串替换成带前缀的表名 $options = preg_replace_callback("/__([A-Z0-9_-]+)__/sU", function($match) use($prefix){ return $prefix.strtolower($match[1]);}, $union); }elseif(is_array($union)){ if(isset($union[0])) { $this->options['union'] = array_merge($this->options['union'],$union); return $this; }else{ $options = $union; } }else{ E(L('_DATA_TYPE_INVALID_')); } $this->options['union'][] = $options; return $this; } /** * 查询缓存 * @access public * @param mixed $key * @param integer $expire * @param string $type * @return Model */ public function cache($key=true,$expire=null,$type=''){ // 增加快捷调用方式 cache(10) 等同于 cache(true, 10) if(is_numeric($key) && is_null($expire)){ $expire = $key; $key = true; } if(false !== $key) $this->options['cache'] = array('key'=>$key,'expire'=>$expire,'type'=>$type); return $this; } /** * 指定查询字段 支持字段排除 * @access public * @param mixed $field * @param boolean $except 是否排除 * @return Model */ public function field($field,$except=false){ if(true === $field) {// 获取全部字段 $fields = $this->getDbFields(); $field = $fields?:'*'; }elseif($except) {// 字段排除 if(is_string($field)) { $field = explode(',',$field); } $fields = $this->getDbFields(); $field = $fields?array_diff($fields,$field):$field; } $this->options['field'] = $field; return $this; } /** * 调用命名范围 * @access public * @param mixed $scope 命名范围名称 支持多个 和直接定义 * @param array $args 参数 * @return Model */ public function scope($scope='',$args=NULL){ if('' === $scope) { if(isset($this->_scope['default'])) { // 默认的命名范围 $options = $this->_scope['default']; }else{ return $this; } }elseif(is_string($scope)){ // 支持多个命名范围调用 用逗号分割 $scopes = explode(',',$scope); $options = array(); foreach ($scopes as $name){ if(!isset($this->_scope[$name])) continue; $options = array_merge($options,$this->_scope[$name]); } if(!empty($args) && is_array($args)) { $options = array_merge($options,$args); } }elseif(is_array($scope)){ // 直接传入命名范围定义 $options = $scope; } if(is_array($options) && !empty($options)){ $this->options = array_merge($this->options,array_change_key_case($options)); } return $this; } /** * 指定查询条件 支持安全过滤 * @access public * @param mixed $where 条件表达式 * @param mixed $parse 预处理参数 * @return Model */ public function where($where,$parse=null){ if(!is_null($parse) && is_string($where)) { if(!is_array($parse)) { $parse = func_get_args(); array_shift($parse); } $parse = array_map(array($this->db,'escapeString'),$parse); $where = vsprintf($where,$parse); }elseif(is_object($where)){ $where = get_object_vars($where); } if(is_string($where) && '' != $where){ $map = array(); $map['_string'] = $where; $where = $map; } if(isset($this->options['where'])){ $this->options['where'] = array_merge($this->options['where'],$where); }else{ $this->options['where'] = $where; } return $this; } /** * 指定查询数量 * @access public * @param mixed $offset 起始位置 * @param mixed $length 查询数量 * @return Model */ public function limit($offset,$length=null){ if(is_null($length) && strpos($offset,',')){ list($offset,$length) = explode(',',$offset); } $this->options['limit'] = intval($offset).( $length? ','.intval($length) : '' ); return $this; } /** * 指定分页 * @access public * @param mixed $page 页数 * @param mixed $listRows 每页数量 * @return Model */ public function page($page,$listRows=null){ if(is_null($listRows) && strpos($page,',')){ list($page,$listRows) = explode(',',$page); } $this->options['page'] = array(intval($page),intval($listRows)); return $this; } /** * 查询注释 * @access public * @param string $comment 注释 * @return Model */ public function comment($comment){ $this->options['comment'] = $comment; return $this; } /** * 获取执行的SQL语句 * @access public * @param boolean $fetch 是否返回sql * @return Model */ public function fetchSql($fetch=true){ $this->options['fetch_sql'] = $fetch; return $this; } /** * 参数绑定 * @access public * @param string $key 参数名 * @param mixed $value 绑定的变量及绑定参数 * @return Model */ public function bind($key,$value=false) { if(is_array($key)){ $this->options['bind'] = $key; }else{ $num = func_num_args(); if($num>2){ $params = func_get_args(); array_shift($params); $this->options['bind'][$key] = $params; }else{ $this->options['bind'][$key] = $value; } } return $this; } /** * 设置模型的属性值 * @access public * @param string $name 名称 * @param mixed $value 值 * @return Model */ public function setProperty($name,$value) { if(property_exists($this,$name)) $this->$name = $value; return $this; } } ================================================ FILE: ThinkPHP/Library/Think/Page.class.php ================================================ // +---------------------------------------------------------------------- namespace Think; class Page{ public $firstRow; // 起始行数 public $listRows; // 列表每页显示行数 public $parameter; // 分页跳转时要带的参数 public $totalRows; // 总行数 public $totalPages; // 分页总页面数 public $rollPage = 11;// 分页栏每页显示的页数 public $lastSuffix = true; // 最后一页是否显示总页数 private $p = 'p'; //分页参数名 private $url = ''; //当前链接URL private $nowPage = 1; // 分页显示定制 private $config = array( 'header' => '共 %TOTAL_ROW% 条记录', 'prev' => '<<', 'next' => '>>', 'first' => '1...', 'last' => '...%TOTAL_PAGE%', 'theme' => '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%', ); /** * 架构函数 * @param array $totalRows 总的记录数 * @param array $listRows 每页显示记录数 * @param array $parameter 分页跳转的参数 */ public function __construct($totalRows, $listRows=20, $parameter = array()) { C('VAR_PAGE') && $this->p = C('VAR_PAGE'); //设置分页参数名称 /* 基础设置 */ $this->totalRows = $totalRows; //设置总记录数 $this->listRows = $listRows; //设置每页显示行数 $this->parameter = empty($parameter) ? $_GET : $parameter; $this->nowPage = empty($_GET[$this->p]) ? 1 : intval($_GET[$this->p]); $this->nowPage = $this->nowPage>0 ? $this->nowPage : 1; $this->firstRow = $this->listRows * ($this->nowPage - 1); } /** * 定制分页链接设置 * @param string $name 设置名称 * @param string $value 设置值 */ public function setConfig($name,$value) { if(isset($this->config[$name])) { $this->config[$name] = $value; } } /** * 生成链接URL * @param integer $page 页码 * @return string */ private function url($page){ return str_replace(urlencode('[PAGE]'), $page, $this->url); } /** * 组装分页链接 * @return string */ public function show() { if(0 == $this->totalRows) return ''; /* 生成URL */ $this->parameter[$this->p] = '[PAGE]'; $this->url = U(ACTION_NAME, $this->parameter); /* 计算分页信息 */ $this->totalPages = ceil($this->totalRows / $this->listRows); //总页数 if(!empty($this->totalPages) && $this->nowPage > $this->totalPages) { $this->nowPage = $this->totalPages; } /* 计算分页临时变量 */ $now_cool_page = $this->rollPage/2; $now_cool_page_ceil = ceil($now_cool_page); $this->lastSuffix && $this->config['last'] = $this->totalPages; //上一页 $up_row = $this->nowPage - 1; $up_page = $up_row > 0 ? '' : ''; //下一页 $down_row = $this->nowPage + 1; $down_page = ($down_row <= $this->totalPages) ? '' : ''; //第一页 $the_first = ''; if($this->totalPages > $this->rollPage && ($this->nowPage - $now_cool_page) >= 1){ $the_first = '' . $this->config['first'] . ''; } //最后一页 $the_end = ''; if($this->totalPages > $this->rollPage && ($this->nowPage + $now_cool_page) < $this->totalPages){ $the_end = '' . $this->config['last'] . ''; } //数字连接 $link_page = ""; for($i = 1; $i <= $this->rollPage; $i++){ if(($this->nowPage - $now_cool_page) <= 0 ){ $page = $i; }elseif(($this->nowPage + $now_cool_page - 1) >= $this->totalPages){ $page = $this->totalPages - $this->rollPage + $i; }else{ $page = $this->nowPage - $now_cool_page_ceil + $i; } if($page > 0 && $page != $this->nowPage){ if($page <= $this->totalPages){ $link_page .= '' . $page . ''; }else{ break; } }else{ if($page > 0 && $this->totalPages != 1){ $link_page .= '' . $page . ''; } } } //替换分页内容 $page_str = str_replace( array('%HEADER%', '%NOW_PAGE%', '%UP_PAGE%', '%DOWN_PAGE%', '%FIRST%', '%LINK_PAGE%', '%END%', '%TOTAL_ROW%', '%TOTAL_PAGE%'), array($this->config['header'], $this->nowPage, $up_page, $down_page, $the_first, $link_page, $the_end, $this->totalRows, $this->totalPages), $this->config['theme']); return "
{$page_str}
"; } } ================================================ FILE: ThinkPHP/Library/Think/Route.class.php ================================================ // +---------------------------------------------------------------------- namespace Think; /** * ThinkPHP路由解析类 */ class Route { // 路由检测 public static function check(){ $depr = C('URL_PATHINFO_DEPR'); $regx = preg_replace('/\.'.__EXT__.'$/i','',trim($_SERVER['PATH_INFO'],$depr)); // 分隔符替换 确保路由定义使用统一的分隔符 if('/' != $depr){ $regx = str_replace($depr,'/',$regx); } // URL映射定义(静态路由) $maps = C('URL_MAP_RULES'); if(isset($maps[$regx])) { $var = self::parseUrl($maps[$regx]); $_GET = array_merge($var, $_GET); return true; } // 动态路由处理 $routes = C('URL_ROUTE_RULES'); if(!empty($routes)) { foreach ($routes as $rule=>$route){ if(is_numeric($rule)){ // 支持 array('rule','adddress',...) 定义路由 $rule = array_shift($route); } if(is_array($route) && isset($route[2])){ // 路由参数 $options = $route[2]; if(isset($options['ext']) && __EXT__ != $options['ext']){ // URL后缀检测 continue; } if(isset($options['method']) && REQUEST_METHOD != strtoupper($options['method'])){ // 请求类型检测 continue; } // 自定义检测 if(!empty($options['callback']) && is_callable($options['callback'])) { if(false === call_user_func($options['callback'])) { continue; } } } if(0===strpos($rule,'/') && preg_match($rule,$regx,$matches)) { // 正则路由 if($route instanceof \Closure) { // 执行闭包 $result = self::invokeRegx($route, $matches); // 如果返回布尔值 则继续执行 return is_bool($result) ? $result : exit; }else{ return self::parseRegex($matches,$route,$regx); } }else{ // 规则路由 $len1 = substr_count($regx,'/'); $len2 = substr_count($rule,'/'); if($len1>=$len2 || strpos($rule,'[')) { if('$' == substr($rule,-1,1)) {// 完整匹配 if($len1 != $len2) { continue; }else{ $rule = substr($rule,0,-1); } } $match = self::checkUrlMatch($regx,$rule); if(false !== $match) { if($route instanceof \Closure) { // 执行闭包 $result = self::invokeRule($route, $match); // 如果返回布尔值 则继续执行 return is_bool($result) ? $result : exit; }else{ return self::parseRule($rule,$route,$regx); } } } } } } return false; } // 检测URL和规则路由是否匹配 private static function checkUrlMatch($regx,$rule) { $m1 = explode('/',$regx); $m2 = explode('/',$rule); $var = array(); foreach ($m2 as $key=>$val){ if(0 === strpos($val,'[:')){ $val = substr($val,1,-1); } if(':' == substr($val,0,1)) {// 动态变量 if($pos = strpos($val,'|')){ // 使用函数过滤 $val = substr($val,1,$pos-1); } if(strpos($val,'\\')) { $type = substr($val,-1); if('d'==$type) { if(isset($m1[$key]) && !is_numeric($m1[$key])) return false; } $name = substr($val, 1, -2); }elseif($pos = strpos($val,'^')){ $array = explode('-',substr(strstr($val,'^'),1)); if(in_array($m1[$key],$array)) { return false; } $name = substr($val, 1, $pos - 1); }else{ $name = substr($val, 1); } $var[$name] = isset($m1[$key])?$m1[$key]:''; }elseif(0 !== strcasecmp($val,$m1[$key])){ return false; } } // 成功匹配后返回URL中的动态变量数组 return $var; } // 解析规范的路由地址 // 地址格式 [控制器/操作?]参数1=值1&参数2=值2... private static function parseUrl($url) { $var = array(); if(false !== strpos($url,'?')) { // [控制器/操作?]参数1=值1&参数2=值2... $info = parse_url($url); $path = explode('/',$info['path']); parse_str($info['query'],$var); }elseif(strpos($url,'/')){ // [控制器/操作] $path = explode('/',$url); }else{ // 参数1=值1&参数2=值2... parse_str($url,$var); } if(isset($path)) { $var[C('VAR_ACTION')] = array_pop($path); if(!empty($path)) { $var[C('VAR_CONTROLLER')] = array_pop($path); } if(!empty($path)) { $var[C('VAR_MODULE')] = array_pop($path); } } return $var; } // 解析规则路由 // '路由规则'=>'[控制器/操作]?额外参数1=值1&额外参数2=值2...' // '路由规则'=>array('[控制器/操作]','额外参数1=值1&额外参数2=值2...') // '路由规则'=>'外部地址' // '路由规则'=>array('外部地址','重定向代码') // 路由规则中 :开头 表示动态变量 // 外部地址中可以用动态变量 采用 :1 :2 的方式 // 'news/:month/:day/:id'=>array('News/read?cate=1','status=1'), // 'new/:id'=>array('/new.php?id=:1',301), 重定向 private static function parseRule($rule,$route,$regx) { // 获取路由地址规则 $url = is_array($route)?$route[0]:$route; // 获取URL地址中的参数 $paths = explode('/',$regx); // 解析路由规则 $matches = array(); $rule = explode('/',$rule); foreach ($rule as $item){ $fun = ''; if(0 === strpos($item,'[:')){ $item = substr($item,1,-1); } if(0===strpos($item,':')) { // 动态变量获取 if($pos = strpos($item,'|')){ // 支持函数过滤 $fun = substr($item,$pos+1); $item = substr($item,0,$pos); } if($pos = strpos($item,'^') ) { $var = substr($item,1,$pos-1); }elseif(strpos($item,'\\')){ $var = substr($item,1,-2); }else{ $var = substr($item,1); } $matches[$var] = !empty($fun)? $fun(array_shift($paths)) : array_shift($paths); }else{ // 过滤URL中的静态变量 array_shift($paths); } } if(0=== strpos($url,'/') || 0===strpos($url,'http')) { // 路由重定向跳转 if(strpos($url,':')) { // 传递动态参数 $values = array_values($matches); $url = preg_replace_callback('/:(\d+)/', function($match) use($values){ return $values[$match[1] - 1]; }, $url); } header("Location: $url", true,(is_array($route) && isset($route[1]))?$route[1]:301); exit; }else{ // 解析路由地址 $var = self::parseUrl($url); // 解析路由地址里面的动态参数 $values = array_values($matches); foreach ($var as $key=>$val){ if(0===strpos($val,':')) { $var[$key] = $values[substr($val,1)-1]; } } $var = array_merge($matches,$var); // 解析剩余的URL参数 if(!empty($paths)) { preg_replace_callback('/(\w+)\/([^\/]+)/', function($match) use(&$var){ $var[strtolower($match[1])]=strip_tags($match[2]);}, implode('/',$paths)); } // 解析路由自动传入参数 if(is_array($route) && isset($route[1])) { if(is_array($route[1])){ $params = $route[1]; }else{ parse_str($route[1],$params); } $var = array_merge($var,$params); } $_GET = array_merge($var,$_GET); } return true; } // 解析正则路由 // '路由正则'=>'[控制器/操作]?参数1=值1&参数2=值2...' // '路由正则'=>array('[控制器/操作]?参数1=值1&参数2=值2...','额外参数1=值1&额外参数2=值2...') // '路由正则'=>'外部地址' // '路由正则'=>array('外部地址','重定向代码') // 参数值和外部地址中可以用动态变量 采用 :1 :2 的方式 // '/new\/(\d+)\/(\d+)/'=>array('News/read?id=:1&page=:2&cate=1','status=1'), // '/new\/(\d+)/'=>array('/new.php?id=:1&page=:2&status=1','301'), 重定向 private static function parseRegex($matches,$route,$regx) { // 获取路由地址规则 $url = is_array($route)?$route[0]:$route; $url = preg_replace_callback('/:(\d+)/', function($match) use($matches){return $matches[$match[1]];}, $url); if(0=== strpos($url,'/') || 0===strpos($url,'http')) { // 路由重定向跳转 header("Location: $url", true,(is_array($route) && isset($route[1]))?$route[1]:301); exit; }else{ // 解析路由地址 $var = self::parseUrl($url); // 处理函数 foreach($var as $key=>$val){ if(strpos($val,'|')){ list($val,$fun) = explode('|',$val); $var[$key] = $fun($val); } } // 解析剩余的URL参数 $regx = substr_replace($regx,'',0,strlen($matches[0])); if($regx) { preg_replace_callback('/(\w+)\/([^\/]+)/', function($match) use(&$var){ $var[strtolower($match[1])] = strip_tags($match[2]); }, $regx); } // 解析路由自动传入参数 if(is_array($route) && isset($route[1])) { if(is_array($route[1])){ $params = $route[1]; }else{ parse_str($route[1],$params); } $var = array_merge($var,$params); } $_GET = array_merge($var,$_GET); } return true; } // 执行正则匹配下的闭包方法 支持参数调用 static private function invokeRegx($closure, $var = array()) { $reflect = new \ReflectionFunction($closure); $params = $reflect->getParameters(); $args = array(); array_shift($var); foreach ($params as $param){ if(!empty($var)) { $args[] = array_shift($var); }elseif($param->isDefaultValueAvailable()){ $args[] = $param->getDefaultValue(); } } return $reflect->invokeArgs($args); } // 执行规则匹配下的闭包方法 支持参数调用 static private function invokeRule($closure, $var = array()) { $reflect = new \ReflectionFunction($closure); $params = $reflect->getParameters(); $args = array(); foreach ($params as $param){ $name = $param->getName(); if(isset($var[$name])) { $args[] = $var[$name]; }elseif($param->isDefaultValueAvailable()){ $args[] = $param->getDefaultValue(); } } return $reflect->invokeArgs($args); } } ================================================ FILE: ThinkPHP/Library/Think/Session/Driver/Db.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Session\Driver; /** * 数据库方式Session驱动 * CREATE TABLE think_session ( * session_id varchar(255) NOT NULL, * session_expire int(11) NOT NULL, * session_data blob, * UNIQUE KEY `session_id` (`session_id`) * ); */ class Db { /** * Session有效时间 */ protected $lifeTime = ''; /** * session保存的数据库名 */ protected $sessionTable = ''; /** * 数据库句柄 */ protected $hander = array(); /** * 打开Session * @access public * @param string $savePath * @param mixed $sessName */ public function open($savePath, $sessName) { $this->lifeTime = C('SESSION_EXPIRE')?C('SESSION_EXPIRE'):ini_get('session.gc_maxlifetime'); $this->sessionTable = C('SESSION_TABLE')?C('SESSION_TABLE'):C("DB_PREFIX")."session"; //分布式数据库 $host = explode(',',C('DB_HOST')); $port = explode(',',C('DB_PORT')); $name = explode(',',C('DB_NAME')); $user = explode(',',C('DB_USER')); $pwd = explode(',',C('DB_PWD')); if(1 == C('DB_DEPLOY_TYPE')){ //读写分离 if(C('DB_RW_SEPARATE')){ $w = floor(mt_rand(0,C('DB_MASTER_NUM')-1)); if(is_numeric(C('DB_SLAVE_NO'))){//指定服务器读 $r = C('DB_SLAVE_NO'); }else{ $r = floor(mt_rand(C('DB_MASTER_NUM'),count($host)-1)); } //主数据库链接 $hander = mysql_connect( $host[$w].(isset($port[$w])?':'.$port[$w]:':'.$port[0]), isset($user[$w])?$user[$w]:$user[0], isset($pwd[$w])?$pwd[$w]:$pwd[0] ); $dbSel = mysql_select_db( isset($name[$w])?$name[$w]:$name[0] ,$hander); if(!$hander || !$dbSel) return false; $this->hander[0] = $hander; //从数据库链接 $hander = mysql_connect( $host[$r].(isset($port[$r])?':'.$port[$r]:':'.$port[0]), isset($user[$r])?$user[$r]:$user[0], isset($pwd[$r])?$pwd[$r]:$pwd[0] ); $dbSel = mysql_select_db( isset($name[$r])?$name[$r]:$name[0] ,$hander); if(!$hander || !$dbSel) return false; $this->hander[1] = $hander; return true; } } //从数据库链接 $r = floor(mt_rand(0,count($host)-1)); $hander = mysql_connect( $host[$r].(isset($port[$r])?':'.$port[$r]:':'.$port[0]), isset($user[$r])?$user[$r]:$user[0], isset($pwd[$r])?$pwd[$r]:$pwd[0] ); $dbSel = mysql_select_db( isset($name[$r])?$name[$r]:$name[0] ,$hander); if(!$hander || !$dbSel) return false; $this->hander = $hander; return true; } /** * 关闭Session * @access public */ public function close() { if(is_array($this->hander)){ $this->gc($this->lifeTime); return (mysql_close($this->hander[0]) && mysql_close($this->hander[1])); } $this->gc($this->lifeTime); return mysql_close($this->hander); } /** * 读取Session * @access public * @param string $sessID */ public function read($sessID) { $hander = is_array($this->hander)?$this->hander[1]:$this->hander; $res = mysql_query('SELECT session_data AS data FROM '.$this->sessionTable." WHERE session_id = '$sessID' AND session_expire >".time(),$hander); if($res) { $row = mysql_fetch_assoc($res); return $row['data']; } return ""; } /** * 写入Session * @access public * @param string $sessID * @param String $sessData */ public function write($sessID,$sessData) { $hander = is_array($this->hander)?$this->hander[0]:$this->hander; $expire = time() + $this->lifeTime; $sessData = addslashes($sessData); mysql_query('REPLACE INTO '.$this->sessionTable." ( session_id, session_expire, session_data) VALUES( '$sessID', '$expire', '$sessData')",$hander); if(mysql_affected_rows($hander)) return true; return false; } /** * 删除Session * @access public * @param string $sessID */ public function destroy($sessID) { $hander = is_array($this->hander)?$this->hander[0]:$this->hander; mysql_query('DELETE FROM '.$this->sessionTable." WHERE session_id = '$sessID'",$hander); if(mysql_affected_rows($hander)) return true; return false; } /** * Session 垃圾回收 * @access public * @param string $sessMaxLifeTime */ public function gc($sessMaxLifeTime) { $hander = is_array($this->hander)?$this->hander[0]:$this->hander; mysql_query('DELETE FROM '.$this->sessionTable.' WHERE session_expire < '.time(),$hander); return mysql_affected_rows($hander); } } ================================================ FILE: ThinkPHP/Library/Think/Session/Driver/Memcache.class.php ================================================ lifeTime = C('SESSION_EXPIRE') ? C('SESSION_EXPIRE') : $this->lifeTime; // $this->sessionName = $sessName; $options = array( 'timeout' => C('SESSION_TIMEOUT') ? C('SESSION_TIMEOUT') : 1, 'persistent' => C('SESSION_PERSISTENT') ? C('SESSION_PERSISTENT') : 0 ); $this->handle = new \Memcache; $hosts = explode(',', C('MEMCACHE_HOST')); $ports = explode(',', C('MEMCACHE_PORT')); foreach ($hosts as $i=>$host) { $port = isset($ports[$i]) ? $ports[$i] : $ports[0]; $this->handle->addServer($host, $port, true, 1, $options['timeout']); } return true; } /** * 关闭Session * @access public */ public function close() { $this->gc(ini_get('session.gc_maxlifetime')); $this->handle->close(); $this->handle = null; return true; } /** * 读取Session * @access public * @param string $sessID */ public function read($sessID) { return $this->handle->get($this->sessionName.$sessID); } /** * 写入Session * @access public * @param string $sessID * @param String $sessData */ public function write($sessID, $sessData) { return $this->handle->set($this->sessionName.$sessID, $sessData, 0, $this->lifeTime); } /** * 删除Session * @access public * @param string $sessID */ public function destroy($sessID) { return $this->handle->delete($this->sessionName.$sessID); } /** * Session 垃圾回收 * @access public * @param string $sessMaxLifeTime */ public function gc($sessMaxLifeTime) { return true; } } ================================================ FILE: ThinkPHP/Library/Think/Session/Driver/Mysqli.class.php ================================================ liu21st // +---------------------------------------------------------------------- // | change mysql to mysqli 解决php7没有mysql扩展时数据库存放session无法操作的问题 // +---------------------------------------------------------------------- namespace Think\Session\Driver; /** * 数据库方式Session驱动 * CREATE TABLE think_session ( * session_id varchar(255) NOT NULL, * session_expire int(11) NOT NULL, * session_data blob, * UNIQUE KEY `session_id` (`session_id`) * ); */ class Db { /** * Session有效时间 */ protected $lifeTime = ''; /** * session保存的数据库名 */ protected $sessionTable = ''; /** * 数据库句柄 */ protected $hander = array(); /** * 打开Session * @access public * @param string $savePath * @param mixed $sessName */ public function open($savePath, $sessName) { $this->lifeTime = C('SESSION_EXPIRE') ? C('SESSION_EXPIRE') : ini_get('session.gc_maxlifetime'); $this->sessionTable = C('SESSION_TABLE') ? C('SESSION_TABLE') : C("DB_PREFIX") . "session"; //分布式数据库 $host = explode(',', C('DB_HOST')); $port = explode(',', C('DB_PORT')); $name = explode(',', C('DB_NAME')); $user = explode(',', C('DB_USER')); $pwd = explode(',', C('DB_PWD')); if (1 == C('DB_DEPLOY_TYPE')) { //读写分离 if (C('DB_RW_SEPARATE')) { $w = floor(mt_rand(0, C('DB_MASTER_NUM') - 1)); if (is_numeric(C('DB_SLAVE_NO'))) {//指定服务器读 $r = C('DB_SLAVE_NO'); } else { $r = floor(mt_rand(C('DB_MASTER_NUM'), count($host) - 1)); } //主数据库链接 $hander = mysqli_connect( $host[$w] . (isset($port[$w]) ? ':' . $port[$w] : ':' . $port[0]), isset($user[$w]) ? $user[$w] : $user[0], isset($pwd[$w]) ? $pwd[$w] : $pwd[0] ); $dbSel = mysqli_select_db( $hander, isset($name[$w]) ? $name[$w] : $name[0] ); if (!$hander || !$dbSel) return false; $this->hander[0] = $hander; //从数据库链接 $hander = mysqli_connect( $host[$r] . (isset($port[$r]) ? ':' . $port[$r] : ':' . $port[0]), isset($user[$r]) ? $user[$r] : $user[0], isset($pwd[$r]) ? $pwd[$r] : $pwd[0] ); $dbSel = mysqli_select_db( $hander, isset($name[$r]) ? $name[$r] : $name[0] ); if (!$hander || !$dbSel) return false; $this->hander[1] = $hander; return true; } } //从数据库链接 $r = floor(mt_rand(0, count($host) - 1)); $hander = mysqli_connect( $host[$r] . (isset($port[$r]) ? ':' . $port[$r] : ':' . $port[0]), isset($user[$r]) ? $user[$r] : $user[0], isset($pwd[$r]) ? $pwd[$r] : $pwd[0] ); $dbSel = mysqli_select_db( $hander, isset($name[$r]) ? $name[$r] : $name[0] ); if (!$hander || !$dbSel) return false; $this->hander = $hander; return true; } /** * 关闭Session * @access public */ public function close() { if (is_array($this->hander)) { $this->gc($this->lifeTime); return (mysqli_close($this->hander[0]) && mysqli_close($this->hander[1])); } $this->gc($this->lifeTime); return mysqli_close($this->hander); } /** * 读取Session * @access public * @param string $sessID */ public function read($sessID) { $hander = is_array($this->hander) ? $this->hander[1] : $this->hander; $res = mysqli_query($hander, "SELECT session_data AS data FROM " . $this->sessionTable . " WHERE session_id = '$sessID' AND session_expire >" . time()); if ($res) { $row = mysqli_fetch_assoc($res); return $row['data']; } return ""; } /** * 写入Session * @access public * @param string $sessID * @param String $sessData */ public function write($sessID, $sessData) { $hander = is_array($this->hander) ? $this->hander[0] : $this->hander; $expire = time() + $this->lifeTime; mysqli_query($hander, "REPLACE INTO " . $this->sessionTable . " ( session_id, session_expire, session_data) VALUES( '$sessID', '$expire', '$sessData')"); if (mysqli_affected_rows($hander)) return true; return false; } /** * 删除Session * @access public * @param string $sessID */ public function destroy($sessID) { $hander = is_array($this->hander) ? $this->hander[0] : $this->hander; mysqli_query($hander, "DELETE FROM " . $this->sessionTable . " WHERE session_id = '$sessID'"); if (mysqli_affected_rows($hander)) return true; return false; } /** * Session 垃圾回收 * @access public * @param string $sessMaxLifeTime */ public function gc($sessMaxLifeTime) { $hander = is_array($this->hander) ? $this->hander[0] : $this->hander; mysqli_query($hander, "DELETE FROM " . $this->sessionTable . " WHERE session_expire < " . time()); return mysqli_affected_rows($hander); } } ================================================ FILE: ThinkPHP/Library/Think/Storage/Driver/File.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Storage\Driver; use Think\Storage; // 本地文件写入存储类 class File extends Storage{ private $contents=array(); /** * 架构函数 * @access public */ public function __construct() { } /** * 文件内容读取 * @access public * @param string $filename 文件名 * @return string */ public function read($filename,$type=''){ return $this->get($filename,'content',$type); } /** * 文件写入 * @access public * @param string $filename 文件名 * @param string $content 文件内容 * @return boolean */ public function put($filename,$content,$type=''){ $dir = dirname($filename); if(!is_dir($dir)){ mkdir($dir,0777,true); } if(false === file_put_contents($filename,$content)){ E(L('_STORAGE_WRITE_ERROR_').':'.$filename); }else{ $this->contents[$filename]=$content; return true; } } /** * 文件追加写入 * @access public * @param string $filename 文件名 * @param string $content 追加的文件内容 * @return boolean */ public function append($filename,$content,$type=''){ if(is_file($filename)){ $content = $this->read($filename,$type).$content; } return $this->put($filename,$content,$type); } /** * 加载文件 * @access public * @param string $filename 文件名 * @param array $vars 传入变量 * @return void */ public function load($_filename,$vars=null){ if(!is_null($vars)){ extract($vars, EXTR_OVERWRITE); } include $_filename; } /** * 文件是否存在 * @access public * @param string $filename 文件名 * @return boolean */ public function has($filename,$type=''){ return is_file($filename); } /** * 文件删除 * @access public * @param string $filename 文件名 * @return boolean */ public function unlink($filename,$type=''){ unset($this->contents[$filename]); return is_file($filename) ? unlink($filename) : false; } /** * 读取文件信息 * @access public * @param string $filename 文件名 * @param string $name 信息名 mtime或者content * @return boolean */ public function get($filename,$name,$type=''){ if(!isset($this->contents[$filename])){ if(!is_file($filename)) return false; $this->contents[$filename]=file_get_contents($filename); } $content=$this->contents[$filename]; $info = array( 'mtime' => filemtime($filename), 'content' => $content ); return $info[$name]; } } ================================================ FILE: ThinkPHP/Library/Think/Storage/Driver/Sae.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Storage\Driver; use Think\Storage; // SAE环境文件写入存储类 class Sae extends Storage{ /** * 架构函数 * @access public */ private $mc; private $kvs = array(); private $htmls = array(); private $contents = array(); public function __construct() { if(!function_exists('memcache_init')){ header('Content-Type:text/html;charset=utf-8'); exit('请在SAE平台上运行代码。'); } $this->mc = @memcache_init(); if(!$this->mc){ header('Content-Type:text/html;charset=utf-8'); exit('您未开通Memcache服务,请在SAE管理平台初始化Memcache服务'); } } /** * 获得SaeKv对象 */ private function getKv(){ static $kv; if(!$kv){ $kv = new \SaeKV(); if(!$kv->init()) E('您没有初始化KVDB,请在SAE管理平台初始化KVDB服务'); } return $kv; } /** * 文件内容读取 * @access public * @param string $filename 文件名 * @return string */ public function read($filename,$type=''){ switch(strtolower($type)){ case 'f': $kv = $this->getKv(); if(!isset($this->kvs[$filename])){ $this->kvs[$filename]=$kv->get($filename); } return $this->kvs[$filename]; default: return $this->get($filename,'content',$type); } } /** * 文件写入 * @access public * @param string $filename 文件名 * @param string $content 文件内容 * @return boolean */ public function put($filename,$content,$type=''){ switch(strtolower($type)){ case 'f': $kv = $this->getKv(); $this->kvs[$filename] = $content; return $kv->set($filename,$content); case 'html': $kv = $this->getKv(); $content = time().$content; $this->htmls[$filename] = $content; return $kv->set($filename,$content); default: $content = time().$content; if(!$this->mc->set($filename,$content,MEMCACHE_COMPRESSED,0)){ E(L('_STORAGE_WRITE_ERROR_').':'.$filename); }else{ $this->contents[$filename] = $content; return true; } } } /** * 文件追加写入 * @access public * @param string $filename 文件名 * @param string $content 追加的文件内容 * @return boolean */ public function append($filename,$content,$type=''){ if($old_content = $this->read($filename,$type)){ $content = $old_content.$content; } return $this->put($filename,$content,$type); } /** * 加载文件 * @access public * @param string $_filename 文件名 * @param array $vars 传入变量 * @return void */ public function load($_filename,$vars=null){ if(!is_null($vars)) extract($vars, EXTR_OVERWRITE); eval('?>'.$this->read($_filename)); } /** * 文件是否存在 * @access public * @param string $filename 文件名 * @return boolean */ public function has($filename,$type=''){ if($this->read($filename,$type)){ return true; }else{ return false; } } /** * 文件删除 * @access public * @param string $filename 文件名 * @return boolean */ public function unlink($filename,$type=''){ switch(strtolower($type)){ case 'f': $kv = $this->getKv(); unset($this->kvs[$filename]); return $kv->delete($filename); case 'html': $kv = $this->getKv(); unset($this->htmls[$filename]); return $kv->delete($filename); default: unset($this->contents[$filename]); return $this->mc->delete($filename); } } /** * 读取文件信息 * @access public * @param string $filename 文件名 * @param string $name 信息名 mtime或者content * @return boolean */ public function get($filename,$name,$type=''){ switch(strtolower($type)){ case 'html': if(!isset($this->htmls[$filename])){ $kv = $this->getKv(); $this->htmls[$filename] = $kv->get($filename); } $content = $this->htmls[$filename]; break; default: if(!isset($this->contents[$filename])){ $this->contents[$filename] = $this->mc->get($filename); } $content = $this->contents[$filename]; } if(false===$content){ return false; } $info = array( 'mtime' => substr($content,0,10), 'content' => substr($content,10) ); return $info[$name]; } } ================================================ FILE: ThinkPHP/Library/Think/Storage.class.php ================================================ // +---------------------------------------------------------------------- namespace Think; // 分布式文件存储类 class Storage { /** * 操作句柄 * @var string * @access protected */ static protected $handler ; /** * 连接分布式文件系统 * @access public * @param string $type 文件类型 * @param array $options 配置数组 * @return void */ static public function connect($type='File',$options=array()) { $class = 'Think\\Storage\\Driver\\'.ucwords($type); self::$handler = new $class($options); } static public function __callstatic($method,$args){ //调用缓存驱动的方法 if(method_exists(self::$handler, $method)){ return call_user_func_array(array(self::$handler,$method), $args); } } } ================================================ FILE: ThinkPHP/Library/Think/Template/Driver/Ease.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Template\Driver; /** * EaseTemplate模板引擎驱动 */ class Ease { /** * 渲染模板输出 * @access public * @param string $templateFile 模板文件名 * @param array $var 模板变量 * @return void */ public function fetch($templateFile,$var) { $templateFile = substr($templateFile,strlen(THEME_PATH),-5); $CacheDir = substr(CACHE_PATH,0,-1); $TemplateDir = substr(THEME_PATH,0,-1); vendor('EaseTemplate.template#ease'); $config = array( 'CacheDir' => $CacheDir, 'TemplateDir' => $TemplateDir, 'TplType' => 'html' ); if(C('TMPL_ENGINE_CONFIG')) { $config = array_merge($config,C('TMPL_ENGINE_CONFIG')); } $tpl = new \EaseTemplate($config); $tpl->set_var($var); $tpl->set_file($templateFile); $tpl->p(); } } ================================================ FILE: ThinkPHP/Library/Think/Template/Driver/Lite.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Template\Driver; /** * TemplateLite模板引擎驱动 */ class Lite { /** * 渲染模板输出 * @access public * @param string $templateFile 模板文件名 * @param array $var 模板变量 * @return void */ public function fetch($templateFile,$var) { vendor("TemplateLite.class#template"); $templateFile = substr($templateFile,strlen(THEME_PATH)); $tpl = new \Template_Lite(); $tpl->template_dir = THEME_PATH; $tpl->compile_dir = CACHE_PATH ; $tpl->cache_dir = TEMP_PATH ; if(C('TMPL_ENGINE_CONFIG')) { $config = C('TMPL_ENGINE_CONFIG'); foreach ($config as $key=>$val){ $tpl->{$key} = $val; } } $tpl->assign($var); $tpl->display($templateFile); } } ================================================ FILE: ThinkPHP/Library/Think/Template/Driver/Mobile.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Template\Driver; /** * MobileTemplate模板引擎驱动 */ class Mobile { /** * 渲染模板输出 * @access public * @param string $templateFile 模板文件名 * @param array $var 模板变量 * @return void */ public function fetch($templateFile,$var) { $templateFile=substr($templateFile,strlen(THEME_PATH)); $var['_think_template_path']=$templateFile; exit(json_encode($var)); } } ================================================ FILE: ThinkPHP/Library/Think/Template/Driver/Smart.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Template\Driver; /** * Smart模板引擎驱动 */ class Smart { /** * 渲染模板输出 * @access public * @param string $templateFile 模板文件名 * @param array $var 模板变量 * @return void */ public function fetch($templateFile,$var) { $templateFile = substr($templateFile,strlen(THEME_PATH)); vendor('SmartTemplate.class#smarttemplate'); $tpl = new \SmartTemplate($templateFile); $tpl->caching = C('TMPL_CACHE_ON'); $tpl->template_dir = THEME_PATH; $tpl->compile_dir = CACHE_PATH ; $tpl->cache_dir = TEMP_PATH ; if(C('TMPL_ENGINE_CONFIG')) { $config = C('TMPL_ENGINE_CONFIG'); foreach ($config as $key=>$val){ $tpl->{$key} = $val; } } $tpl->assign($var); $tpl->output(); } } ================================================ FILE: ThinkPHP/Library/Think/Template/Driver/Smarty.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Template\Driver; /** * Smarty模板引擎驱动 */ class Smarty { /** * 渲染模板输出 * @access public * @param string $templateFile 模板文件名 * @param array $var 模板变量 * @return void */ public function fetch($templateFile,$var) { $templateFile = substr($templateFile,strlen(THEME_PATH)); vendor('Smarty.Smarty#class'); $tpl = new \Smarty(); $tpl->caching = C('TMPL_CACHE_ON'); $tpl->template_dir = THEME_PATH; $tpl->compile_dir = CACHE_PATH ; $tpl->cache_dir = TEMP_PATH ; if(C('TMPL_ENGINE_CONFIG')) { $config = C('TMPL_ENGINE_CONFIG'); foreach ($config as $key=>$val){ $tpl->{$key} = $val; } } $tpl->assign($var); $tpl->display($templateFile); } } ================================================ FILE: ThinkPHP/Library/Think/Template/TagLib/Cx.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Template\TagLib; use Think\Template\TagLib; /** * CX标签库解析类 */ class Cx extends TagLib { // 标签定义 protected $tags = array( // 标签定义: attr 属性列表 close 是否闭合(0 或者1 默认1) alias 标签别名 level 嵌套层次 'php' => array(), 'volist' => array('attr'=>'name,id,offset,length,key,mod','level'=>3,'alias'=>'iterate'), 'foreach' => array('attr'=>'name,item,key','level'=>3), 'if' => array('attr'=>'condition','level'=>2), 'elseif' => array('attr'=>'condition','close'=>0), 'else' => array('attr'=>'','close'=>0), 'switch' => array('attr'=>'name','level'=>2), 'case' => array('attr'=>'value,break'), 'default' => array('attr'=>'','close'=>0), 'compare' => array('attr'=>'name,value,type','level'=>3,'alias'=>'eq,equal,notequal,neq,gt,lt,egt,elt,heq,nheq'), 'range' => array('attr'=>'name,value,type','level'=>3,'alias'=>'in,notin,between,notbetween'), 'empty' => array('attr'=>'name','level'=>3), 'notempty' => array('attr'=>'name','level'=>3), 'present' => array('attr'=>'name','level'=>3), 'notpresent'=> array('attr'=>'name','level'=>3), 'defined' => array('attr'=>'name','level'=>3), 'notdefined'=> array('attr'=>'name','level'=>3), 'import' => array('attr'=>'file,href,type,value,basepath','close'=>0,'alias'=>'load,css,js'), 'assign' => array('attr'=>'name,value','close'=>0), 'define' => array('attr'=>'name,value','close'=>0), 'for' => array('attr'=>'start,end,name,comparison,step', 'level'=>3), ); /** * php标签解析 * @access public * @param array $tag 标签属性 * @param string $content 标签内容 * @return string */ public function _php($tag,$content) { $parseStr = ''; return $parseStr; } /** * volist标签解析 循环输出数据集 * 格式: * * {user.username} * {user.email} * * @access public * @param array $tag 标签属性 * @param string $content 标签内容 * @return string|void */ public function _volist($tag,$content) { $name = $tag['name']; $id = $tag['id']; $empty = isset($tag['empty'])?$tag['empty']:''; $key = !empty($tag['key'])?$tag['key']:'i'; $mod = isset($tag['mod'])?$tag['mod']:'2'; // 允许使用函数设定数据集 {$vo.name} $parseStr = 'autoBuildVar($name); } $parseStr .= 'if(is_array('.$name.')): $'.$key.' = 0;'; if(isset($tag['length']) && '' !=$tag['length'] ) { $parseStr .= ' $__LIST__ = array_slice('.$name.','.$tag['offset'].','.$tag['length'].',true);'; }elseif(isset($tag['offset']) && '' !=$tag['offset']){ $parseStr .= ' $__LIST__ = array_slice('.$name.','.$tag['offset'].',null,true);'; }else{ $parseStr .= ' $__LIST__ = '.$name.';'; } $parseStr .= 'if( count($__LIST__)==0 ) : echo "'.$empty.'" ;'; $parseStr .= 'else: '; $parseStr .= 'foreach($__LIST__ as $key=>$'.$id.'): '; $parseStr .= '$mod = ($'.$key.' % '.$mod.' );'; $parseStr .= '++$'.$key.';?>'; $parseStr .= $this->tpl->parse($content); $parseStr .= ''; if(!empty($parseStr)) { return $parseStr; } return ; } /** * foreach标签解析 循环输出数据集 * @access public * @param array $tag 标签属性 * @param string $content 标签内容 * @return string|void */ public function _foreach($tag,$content) { $name = $tag['name']; $item = $tag['item']; $key = !empty($tag['key'])?$tag['key']:'key'; $name = $this->autoBuildVar($name); $parseStr = '$'.$item.'): ?>'; $parseStr .= $this->tpl->parse($content); $parseStr .= ''; if(!empty($parseStr)) { return $parseStr; } return ; } /** * if标签解析 * 格式: * * * * * 表达式支持 eq neq gt egt lt elt == > >= < <= or and || && * @access public * @param array $tag 标签属性 * @param string $content 标签内容 * @return string */ public function _if($tag,$content) { $condition = $this->parseCondition($tag['condition']); $parseStr = ''.$content.''; return $parseStr; } /** * else标签解析 * 格式:见if标签 * @access public * @param array $tag 标签属性 * @param string $content 标签内容 * @return string */ public function _elseif($tag,$content) { $condition = $this->parseCondition($tag['condition']); $parseStr = ''; return $parseStr; } /** * else标签解析 * @access public * @param array $tag 标签属性 * @return string */ public function _else($tag) { $parseStr = ''; return $parseStr; } /** * switch标签解析 * 格式: * * 1 * 2 * other * * @access public * @param array $tag 标签属性 * @param string $content 标签内容 * @return string */ public function _switch($tag,$content) { $name = $tag['name']; $varArray = explode('|',$name); $name = array_shift($varArray); $name = $this->autoBuildVar($name); if(count($varArray)>0) $name = $this->tpl->parseVarFunction($name,$varArray); $parseStr = ''.$content.''; return $parseStr; } /** * case标签解析 需要配合switch才有效 * @access public * @param array $tag 标签属性 * @param string $content 标签内容 * @return string */ public function _case($tag,$content) { $value = $tag['value']; if('$' == substr($value,0,1)) { $varArray = explode('|',$value); $value = array_shift($varArray); $value = $this->autoBuildVar(substr($value,1)); if(count($varArray)>0) $value = $this->tpl->parseVarFunction($value,$varArray); $value = 'case '.$value.': '; }elseif(strpos($value,'|')){ $values = explode('|',$value); $value = ''; foreach ($values as $val){ $value .= 'case "'.addslashes($val).'": '; } }else{ $value = 'case "'.$value.'": '; } $parseStr = ''.$content; $isBreak = isset($tag['break']) ? $tag['break'] : ''; if('' ==$isBreak || $isBreak) { $parseStr .= ''; } return $parseStr; } /** * default标签解析 需要配合switch才有效 * 使用: ddfdf * @access public * @param array $tag 标签属性 * @param string $content 标签内容 * @return string */ public function _default($tag) { $parseStr = ''; return $parseStr; } /** * compare标签解析 * 用于值的比较 支持 eq neq gt lt egt elt heq nheq 默认是eq * 格式: content * @access public * @param array $tag 标签属性 * @param string $content 标签内容 * @return string */ public function _compare($tag,$content,$type='eq') { $name = $tag['name']; $value = $tag['value']; $type = isset($tag['type'])?$tag['type']:$type; $type = $this->parseCondition(' '.$type.' '); $varArray = explode('|',$name); $name = array_shift($varArray); $name = $this->autoBuildVar($name); if(count($varArray)>0) $name = $this->tpl->parseVarFunction($name,$varArray); if('$' == substr($value,0,1)) { $value = $this->autoBuildVar(substr($value,1)); }else { $value = '"'.$value.'"'; } $parseStr = ''.$content.''; return $parseStr; } public function _eq($tag,$content) { return $this->_compare($tag,$content,'eq'); } public function _equal($tag,$content) { return $this->_compare($tag,$content,'eq'); } public function _neq($tag,$content) { return $this->_compare($tag,$content,'neq'); } public function _notequal($tag,$content) { return $this->_compare($tag,$content,'neq'); } public function _gt($tag,$content) { return $this->_compare($tag,$content,'gt'); } public function _lt($tag,$content) { return $this->_compare($tag,$content,'lt'); } public function _egt($tag,$content) { return $this->_compare($tag,$content,'egt'); } public function _elt($tag,$content) { return $this->_compare($tag,$content,'elt'); } public function _heq($tag,$content) { return $this->_compare($tag,$content,'heq'); } public function _nheq($tag,$content) { return $this->_compare($tag,$content,'nheq'); } /** * range标签解析 * 如果某个变量存在于某个范围 则输出内容 type= in 表示在范围内 否则表示在范围外 * 格式: content * example: content * @access public * @param array $tag 标签属性 * @param string $content 标签内容 * @param string $type 比较类型 * @return string */ public function _range($tag,$content,$type='in') { $name = $tag['name']; $value = $tag['value']; $varArray = explode('|',$name); $name = array_shift($varArray); $name = $this->autoBuildVar($name); if(count($varArray)>0) $name = $this->tpl->parseVarFunction($name,$varArray); $type = isset($tag['type'])?$tag['type']:$type; if('$' == substr($value,0,1)) { $value = $this->autoBuildVar(substr($value,1)); $str = 'is_array('.$value.')?'.$value.':explode(\',\','.$value.')'; }else{ $value = '"'.$value.'"'; $str = 'explode(\',\','.$value.')'; } if($type=='between') { $parseStr = '= $_RANGE_VAR_[0] && '.$name.'<= $_RANGE_VAR_[1]):?>'.$content.''; }elseif($type=='notbetween'){ $parseStr = '$_RANGE_VAR_[1]):?>'.$content.''; }else{ $fun = ($type == 'in')? 'in_array' : '!in_array'; $parseStr = ''.$content.''; } return $parseStr; } // range标签的别名 用于in判断 public function _in($tag,$content) { return $this->_range($tag,$content,'in'); } // range标签的别名 用于notin判断 public function _notin($tag,$content) { return $this->_range($tag,$content,'notin'); } public function _between($tag,$content){ return $this->_range($tag,$content,'between'); } public function _notbetween($tag,$content){ return $this->_range($tag,$content,'notbetween'); } /** * present标签解析 * 如果某个变量已经设置 则输出内容 * 格式: content * @access public * @param array $tag 标签属性 * @param string $content 标签内容 * @return string */ public function _present($tag,$content) { $name = $tag['name']; $name = $this->autoBuildVar($name); $parseStr = ''.$content.''; return $parseStr; } /** * notpresent标签解析 * 如果某个变量没有设置,则输出内容 * 格式: content * @access public * @param array $tag 标签属性 * @param string $content 标签内容 * @return string */ public function _notpresent($tag,$content) { $name = $tag['name']; $name = $this->autoBuildVar($name); $parseStr = ''.$content.''; return $parseStr; } /** * empty标签解析 * 如果某个变量为empty 则输出内容 * 格式: content * @access public * @param array $tag 标签属性 * @param string $content 标签内容 * @return string */ public function _empty($tag,$content) { $name = $tag['name']; $name = $this->autoBuildVar($name); $parseStr = ''.$content.''; return $parseStr; } public function _notempty($tag,$content) { $name = $tag['name']; $name = $this->autoBuildVar($name); $parseStr = ''.$content.''; return $parseStr; } /** * 判断是否已经定义了该常量 * 已定义 * @param $attr * @param $content * @return string */ public function _defined($tag,$content) { $name = $tag['name']; $parseStr = ''.$content.''; return $parseStr; } public function _notdefined($tag,$content) { $name = $tag['name']; $parseStr = ''.$content.''; return $parseStr; } /** * import 标签解析 * * @access public * @param array $tag 标签属性 * @param string $content 标签内容 * @param boolean $isFile 是否文件方式 * @param string $type 类型 * @return string */ public function _import($tag,$content,$isFile=false,$type='') { $file = isset($tag['file'])?$tag['file']:$tag['href']; $parseStr = ''; $endStr = ''; // 判断是否存在加载条件 允许使用函数判断(默认为isset) if (isset($tag['value'])) { $varArray = explode('|',$tag['value']); $name = array_shift($varArray); $name = $this->autoBuildVar($name); if (!empty($varArray)) $name = $this->tpl->parseVarFunction($name,$varArray); else $name = 'isset('.$name.')'; $parseStr .= ''; $endStr = ''; } if($isFile) { // 根据文件名后缀自动识别 $type = $type?$type:(!empty($tag['type'])?strtolower($tag['type']):null); // 文件方式导入 $array = explode(',',$file); foreach ($array as $val){ if (!$type || isset($reset)) { $type = $reset = strtolower(substr(strrchr($val, '.'),1)); } switch($type) { case 'js': $parseStr .= ''; break; case 'css': $parseStr .= ''; break; case 'php': $parseStr .= ''; break; } } }else{ // 命名空间导入模式 默认是js $type = $type?$type:(!empty($tag['type'])?strtolower($tag['type']):'js'); $basepath = !empty($tag['basepath'])?$tag['basepath']:__ROOT__.'/Public'; // 命名空间方式导入外部文件 $array = explode(',',$file); foreach ($array as $val){ if(strpos ($val, '?')) { list($val,$version) = explode('?',$val); } else { $version = ''; } switch($type) { case 'js': $parseStr .= ''; break; case 'css': $parseStr .= ''; break; case 'php': $parseStr .= ''; break; } } } return $parseStr.$endStr; } // import别名 采用文件方式加载(要使用命名空间必须用import) 例如 public function _load($tag,$content) { return $this->_import($tag,$content,true); } // import别名使用 导入css文件 public function _css($tag,$content) { return $this->_import($tag,$content,true,'css'); } // import别名使用 导入js文件 public function _js($tag,$content) { return $this->_import($tag,$content,true,'js'); } /** * assign标签解析 * 在模板中给某个变量赋值 支持变量赋值 * 格式: * @access public * @param array $tag 标签属性 * @param string $content 标签内容 * @return string */ public function _assign($tag,$content) { $name = $this->autoBuildVar($tag['name']); if('$'==substr($tag['value'],0,1)) { $value = $this->autoBuildVar(substr($tag['value'],1)); }else{ $value = '\''.$tag['value']. '\''; } $parseStr = ''; return $parseStr; } /** * define标签解析 * 在模板中定义常量 支持变量赋值 * 格式: * @access public * @param array $tag 标签属性 * @param string $content 标签内容 * @return string */ public function _define($tag,$content) { $name = '\''.$tag['name']. '\''; if('$'==substr($tag['value'],0,1)) { $value = $this->autoBuildVar(substr($tag['value'],1)); }else{ $value = '\''.$tag['value']. '\''; } $parseStr = ''; return $parseStr; } /** * for标签解析 * 格式: * @access public * @param array $tag 标签属性 * @param string $content 标签内容 * @return string */ public function _for($tag, $content){ //设置默认值 $start = 0; $end = 0; $step = 1; $comparison = 'lt'; $name = 'i'; $rand = rand(); //添加随机数,防止嵌套变量冲突 //获取属性 foreach ($tag as $key => $value){ $value = trim($value); if(':'==substr($value,0,1)) $value = substr($value,1); elseif('$'==substr($value,0,1)) $value = $this->autoBuildVar(substr($value,1)); switch ($key){ case 'start': $start = $value; break; case 'end' : $end = $value; break; case 'step': $step = $value; break; case 'comparison': $comparison = $value; break; case 'name': $name = $value; break; } } $parseStr = 'parseCondition('$'.$name.' '.$comparison.' $__FOR_END_'.$rand.'__').';$'.$name.'+='.$step.'){ ?>'; $parseStr .= $content; $parseStr .= ''; return $parseStr; } } ================================================ FILE: ThinkPHP/Library/Think/Template/TagLib/Html.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Template\TagLib; use Think\Template\TagLib; /** * Html标签库驱动 */ class Html extends TagLib{ // 标签定义 protected $tags = array( // 标签定义: attr 属性列表 close 是否闭合(0 或者1 默认1) alias 标签别名 level 嵌套层次 'editor' => array('attr'=>'id,name,style,width,height,type','close'=>1), 'select' => array('attr'=>'name,options,values,output,multiple,id,size,first,change,selected,dblclick','close'=>0), 'grid' => array('attr'=>'id,pk,style,action,actionlist,show,datasource','close'=>0), 'list' => array('attr'=>'id,pk,style,action,actionlist,show,datasource,checkbox','close'=>0), 'imagebtn' => array('attr'=>'id,name,value,type,style,click','close'=>0), 'checkbox' => array('attr'=>'name,checkboxes,checked,separator','close'=>0), 'radio' => array('attr'=>'name,radios,checked,separator','close'=>0) ); /** * editor标签解析 插入可视化编辑器 * 格式: {$vo.remark} * @access public * @param array $tag 标签属性 * @return string|void */ public function _editor($tag,$content) { $id = !empty($tag['id'])?$tag['id']: '_editor'; $name = $tag['name']; $style = !empty($tag['style'])?$tag['style']:''; $width = !empty($tag['width'])?$tag['width']: '100%'; $height = !empty($tag['height'])?$tag['height'] :'320px'; // $content = $tag['content']; $type = $tag['type'] ; switch(strtoupper($type)) { case 'FCKEDITOR': $parseStr = ' '; break; case 'FCKMINI': $parseStr = ' '; break; case 'EWEBEDITOR': $parseStr = ""; break; case 'NETEASE': $parseStr = ''; break; case 'UBB': $parseStr = '
'; break; case 'KINDEDITOR': $parseStr = ''; break; default : $parseStr = ''; } return $parseStr; } /** * imageBtn标签解析 * 格式: * @access public * @param array $tag 标签属性 * @return string|void */ public function _imageBtn($tag) { $name = $tag['name']; //名称 $value = $tag['value']; //文字 $id = isset($tag['id'])?$tag['id']:''; //ID $style = isset($tag['style'])?$tag['style']:''; //样式名 $click = isset($tag['click'])?$tag['click']:''; //点击 $type = empty($tag['type'])?'button':$tag['type']; //按钮类型 if(!empty($name)) { $parseStr = '
'; }else { $parseStr = '
'; } return $parseStr; } /** * imageLink标签解析 * 格式: * @access public * @param array $tag 标签属性 * @return string|void */ public function _imgLink($tag) { $name = $tag['name']; //名称 $alt = $tag['alt']; //文字 $id = $tag['id']; //ID $style = $tag['style']; //样式名 $click = $tag['click']; //点击 $type = $tag['type']; //点击 if(empty($type)) { $type = 'button'; } $parseStr = ''; return $parseStr; } /** * select标签解析 * 格式: * @access public * @param array $tag 标签属性 * @return string|void */ public function _select($tag) { $name = $tag['name']; $options = $tag['options']; $values = $tag['values']; $output = $tag['output']; $multiple = $tag['multiple']; $id = $tag['id']; $size = $tag['size']; $first = $tag['first']; $selected = $tag['selected']; $style = $tag['style']; $ondblclick = $tag['dblclick']; $onchange = $tag['change']; if(!empty($multiple)) { $parseStr = ''; } if(!empty($first)) { $parseStr .= ''; } if(!empty($options)) { $parseStr .= '$val) { ?>'; if(!empty($selected)) { $parseStr .= ''; $parseStr .= ''; $parseStr .= ''; $parseStr .= ''; }else { $parseStr .= ''; } $parseStr .= ''; }else if(!empty($values)) { $parseStr .= ''; if(!empty($selected)) { $parseStr .= ''; $parseStr .= ''; $parseStr .= ''; $parseStr .= ''; }else { $parseStr .= ''; } $parseStr .= ''; } $parseStr .= ''; return $parseStr; } /** * checkbox标签解析 * 格式: * @access public * @param array $tag 标签属性 * @return string|void */ public function _checkbox($tag) { $name = $tag['name']; $checkboxes = $tag['checkboxes']; $checked = $tag['checked']; $separator = $tag['separator']; $checkboxes = $this->tpl->get($checkboxes); $checked = $this->tpl->get($checked)?$this->tpl->get($checked):$checked; $parseStr = ''; foreach($checkboxes as $key=>$val) { if($checked == $key || in_array($key,$checked) ) { $parseStr .= ''.$val.$separator; }else { $parseStr .= ''.$val.$separator; } } return $parseStr; } /** * radio标签解析 * 格式: * @access public * @param array $tag 标签属性 * @return string|void */ public function _radio($tag) { $name = $tag['name']; $radios = $tag['radios']; $checked = $tag['checked']; $separator = $tag['separator']; $radios = $this->tpl->get($radios); $checked = $this->tpl->get($checked)?$this->tpl->get($checked):$checked; $parseStr = ''; foreach($radios as $key=>$val) { if($checked == $key ) { $parseStr .= ''.$val.$separator; }else { $parseStr .= ''.$val.$separator; } } return $parseStr; } /** * list标签解析 * 格式: * @access public * @param array $tag 标签属性 * @return string */ public function _grid($tag) { $id = $tag['id']; //表格ID $datasource = $tag['datasource']; //列表显示的数据源VoList名称 $pk = empty($tag['pk'])?'id':$tag['pk'];//主键名,默认为id $style = $tag['style']; //样式名 $name = !empty($tag['name'])?$tag['name']:'vo'; //Vo对象名 $action = !empty($tag['action'])?$tag['action']:false; //是否显示功能操作 $key = !empty($tag['key'])?true:false; if(isset($tag['actionlist'])) { $actionlist = explode(',',trim($tag['actionlist'])); //指定功能列表 } if(substr($tag['show'],0,1)=='$') { $show = $this->tpl->get(substr($tag['show'],1)); }else { $show = $tag['show']; } $show = explode(',',$show); //列表显示字段列表 //计算表格的列数 $colNum = count($show); if(!empty($action)) $colNum++; if(!empty($key)) $colNum++; //显示开始 $parseStr = "\n"; $parseStr .= ''; $parseStr .= ''; $parseStr .= ''; //列表需要显示的字段 $fields = array(); foreach($show as $val) { $fields[] = explode(':',$val); } if(!empty($key)) { $parseStr .= ''; } foreach($fields as $field) {//显示指定的字段 $property = explode('|',$field[0]); $showname = explode('|',$field[1]); if(isset($showname[1])) { $parseStr .= ''; } if(!empty($action)) {//如果指定显示操作功能列 $parseStr .= ''; } $parseStr .= ''; $parseStr .= ''; //支持鼠标移动单元行颜色变化 具体方法在js中定义 if(!empty($key)) { $parseStr .= ''; } foreach($fields as $field) { //显示定义的列表字段 $parseStr .= ''; } if(!empty($action)) {//显示功能操作 if(!empty($actionlist[0])) {//显示指定的功能项 $parseStr .= ''; } } $parseStr .= '
No'; }else { $parseStr .= ''; } $parseStr .= $showname[0].'操作
{$i}'; if(!empty($field[2])) { // 支持列表字段链接功能 具体方法由JS函数实现 $href = explode('|',$field[2]); if(count($href)>1) { //指定链接传的字段值 // 支持多个字段传递 $array = explode('^',$href[1]); if(count($array)>1) { foreach ($array as $a){ $temp[] = '\'{$'.$name.'.'.$a.'|addslashes}\''; } $parseStr .= ''; }else{ $parseStr .= ''; } }else { //如果没有指定默认传编号值 $parseStr .= ''; } } if(strpos($field[0],'^')) { $property = explode('^',$field[0]); foreach ($property as $p){ $unit = explode('|',$p); if(count($unit)>1) { $parseStr .= '{$'.$name.'.'.$unit[0].'|'.$unit[1].'} '; }else { $parseStr .= '{$'.$name.'.'.$p.'} '; } } }else{ $property = explode('|',$field[0]); if(count($property)>1) { $parseStr .= '{$'.$name.'.'.$property[0].'|'.$property[1].'}'; }else { $parseStr .= '{$'.$name.'.'.$field[0].'}'; } } if(!empty($field[2])) { $parseStr .= ''; } $parseStr .= ''; foreach($actionlist as $val) { if(strpos($val,':')) { $a = explode(':',$val); if(count($a)>2) { $parseStr .= ''.$a[1].' '; }else { $parseStr .= ''.$a[1].' '; } }else{ $array = explode('|',$val); if(count($array)>2) { $parseStr .= ' '.$array[2].' '; }else{ $parseStr .= ' {$'.$name.'.'.$val.'} '; } } } $parseStr .= '
'; $parseStr .= "\n\n"; return $parseStr; } /** * list标签解析 * 格式: * @access public * @param array $tag 标签属性 * @return string */ public function _list($tag) { $id = $tag['id']; //表格ID $datasource = $tag['datasource']; //列表显示的数据源VoList名称 $pk = empty($tag['pk'])?'id':$tag['pk'];//主键名,默认为id $style = $tag['style']; //样式名 $name = !empty($tag['name'])?$tag['name']:'vo'; //Vo对象名 $action = $tag['action']=='true'?true:false; //是否显示功能操作 $key = !empty($tag['key'])?true:false; $sort = $tag['sort']=='false'?false:true; $checkbox = $tag['checkbox']; //是否显示Checkbox if(isset($tag['actionlist'])) { if(substr($tag['actionlist'],0,1)=='$') { $actionlist = $this->tpl->get(substr($tag['actionlist'],1)); }else { $actionlist = $tag['actionlist']; } $actionlist = explode(',',trim($actionlist)); //指定功能列表 } if(substr($tag['show'],0,1)=='$') { $show = $this->tpl->get(substr($tag['show'],1)); }else { $show = $tag['show']; } $show = explode(',',$show); //列表显示字段列表 //计算表格的列数 $colNum = count($show); if(!empty($checkbox)) $colNum++; if(!empty($action)) $colNum++; if(!empty($key)) $colNum++; //显示开始 $parseStr = "\n"; $parseStr .= ''; $parseStr .= ''; $parseStr .= ''; //列表需要显示的字段 $fields = array(); foreach($show as $val) { $fields[] = explode(':',$val); } if(!empty($checkbox) && 'true'==strtolower($checkbox)) {//如果指定需要显示checkbox列 $parseStr .=''; } if(!empty($key)) { $parseStr .= ''; } foreach($fields as $field) {//显示指定的字段 $property = explode('|',$field[0]); $showname = explode('|',$field[1]); if(isset($showname[1])) { $parseStr .= ''; }else{ $parseStr .= $showname[0].''; } } if(!empty($action)) {//如果指定显示操作功能列 $parseStr .= ''; } $parseStr .= ''; $parseStr .= ''; } if(!empty($key)) { $parseStr .= ''; } foreach($fields as $field) { //显示定义的列表字段 $parseStr .= ''; } if(!empty($action)) {//显示功能操作 if(!empty($actionlist[0])) {//显示指定的功能项 $parseStr .= ''; } } $parseStr .= '
No'; }else { $parseStr .= ''; } $showname[2] = isset($showname[2])?$showname[2]:$showname[0]; if($sort) { $parseStr .= ''.$showname[0].'操作
{$i}'; if(!empty($field[2])) { // 支持列表字段链接功能 具体方法由JS函数实现 $href = explode('|',$field[2]); if(count($href)>1) { //指定链接传的字段值 // 支持多个字段传递 $array = explode('^',$href[1]); if(count($array)>1) { foreach ($array as $a){ $temp[] = '\'{$'.$name.'.'.$a.'|addslashes}\''; } $parseStr .= ''; }else{ $parseStr .= ''; } }else { //如果没有指定默认传编号值 $parseStr .= ''; } } if(strpos($field[0],'^')) { $property = explode('^',$field[0]); foreach ($property as $p){ $unit = explode('|',$p); if(count($unit)>1) { $parseStr .= '{$'.$name.'.'.$unit[0].'|'.$unit[1].'} '; }else { $parseStr .= '{$'.$name.'.'.$p.'} '; } } }else{ $property = explode('|',$field[0]); if(count($property)>1) { $parseStr .= '{$'.$name.'.'.$property[0].'|'.$property[1].'}'; }else { $parseStr .= '{$'.$name.'.'.$field[0].'}'; } } if(!empty($field[2])) { $parseStr .= ''; } $parseStr .= ''; foreach($actionlist as $val) { if(strpos($val,':')) { $a = explode(':',$val); if(count($a)>2) { $parseStr .= ''.$a[1].' '; }else { $parseStr .= ''.$a[1].' '; } }else{ $array = explode('|',$val); if(count($array)>2) { $parseStr .= ' '.$array[2].' '; }else{ $parseStr .= ' {$'.$name.'.'.$val.'} '; } } } $parseStr .= '
'; $parseStr .= "\n\n"; return $parseStr; } } ================================================ FILE: ThinkPHP/Library/Think/Template/TagLib.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Template; /** * ThinkPHP标签库TagLib解析基类 */ class TagLib { /** * 标签库定义XML文件 * @var string * @access protected */ protected $xml = ''; protected $tags = array();// 标签定义 /** * 标签库名称 * @var string * @access protected */ protected $tagLib =''; /** * 标签库标签列表 * @var string * @access protected */ protected $tagList = array(); /** * 标签库分析数组 * @var string * @access protected */ protected $parse = array(); /** * 标签库是否有效 * @var string * @access protected */ protected $valid = false; /** * 当前模板对象 * @var object * @access protected */ protected $tpl; protected $comparison = array(' nheq '=>' !== ',' heq '=>' === ',' neq '=>' != ',' eq '=>' == ',' egt '=>' >= ',' gt '=>' > ',' elt '=>' <= ',' lt '=>' < '); /** * 架构函数 * @access public */ public function __construct() { $this->tagLib = strtolower(substr(get_class($this),6)); $this->tpl = \Think\Think::instance('Think\\Template'); } /** * TagLib标签属性分析 返回标签属性数组 * @access public * @param string $tagStr 标签内容 * @return array */ public function parseXmlAttr($attr,$tag) { //XML解析安全过滤 $attr = str_replace('&','___', $attr); $xml = ''; $xml = simplexml_load_string($xml); if(!$xml) { E(L('_XML_TAG_ERROR_').' : '.$attr); } $xml = (array)($xml->tag->attributes()); if(isset($xml['@attributes'])){ $array = array_change_key_case($xml['@attributes']); if($array) { $tag = strtolower($tag); if(!isset($this->tags[$tag])){ // 检测是否存在别名定义 foreach($this->tags as $key=>$val){ if(isset($val['alias']) && in_array($tag,explode(',',$val['alias']))){ $item = $val; break; } } }else{ $item = $this->tags[$tag]; } $attrs = explode(',',$item['attr']); if(isset($item['must'])){ $must = explode(',',$item['must']); }else{ $must = array(); } foreach($attrs as $name) { if( isset($array[$name])) { $array[$name] = str_replace('___','&',$array[$name]); }elseif(false !== array_search($name,$must)){ E(L('_PARAM_ERROR_').':'.$name); } } return $array; } }else{ return array(); } } /** * 解析条件表达式 * @access public * @param string $condition 表达式标签内容 * @return array */ public function parseCondition($condition) { $condition = str_ireplace(array_keys($this->comparison),array_values($this->comparison),$condition); $condition = preg_replace('/\$(\w+):(\w+)\s/is','$\\1->\\2 ',$condition); switch(strtolower(C('TMPL_VAR_IDENTIFY'))) { case 'array': // 识别为数组 $condition = preg_replace('/\$(\w+)\.(\w+)\s/is','$\\1["\\2"] ',$condition); break; case 'obj': // 识别为对象 $condition = preg_replace('/\$(\w+)\.(\w+)\s/is','$\\1->\\2 ',$condition); break; default: // 自动判断数组或对象 只支持二维 $condition = preg_replace('/\$(\w+)\.(\w+)\s/is','(is_array($\\1)?$\\1["\\2"]:$\\1->\\2) ',$condition); } if(false !== strpos($condition, '$Think')) $condition = preg_replace_callback('/(\$Think.*?)\s/is', array($this, 'parseThinkVar'), $condition); return $condition; } /** * 自动识别构建变量 * @access public * @param string $name 变量描述 * @return string */ public function autoBuildVar($name) { if('Think.' == substr($name,0,6)){ // 特殊变量 return $this->parseThinkVar($name); }elseif(strpos($name,'.')) { $vars = explode('.',$name); $var = array_shift($vars); switch(strtolower(C('TMPL_VAR_IDENTIFY'))) { case 'array': // 识别为数组 $name = '$'.$var; foreach ($vars as $key=>$val){ if(0===strpos($val,'$')) { $name .= '["{'.$val.'}"]'; }else{ $name .= '["'.$val.'"]'; } } break; case 'obj': // 识别为对象 $name = '$'.$var; foreach ($vars as $key=>$val) $name .= '->'.$val; break; default: // 自动判断数组或对象 只支持二维 $name = 'is_array($'.$var.')?$'.$var.'["'.$vars[0].'"]:$'.$var.'->'.$vars[0]; } }elseif(strpos($name,':')){ // 额外的对象方式支持 $name = '$'.str_replace(':','->',$name); }elseif(!defined($name)) { $name = '$'.$name; } return $name; } /** * 用于标签属性里面的特殊模板变量解析 * 格式 以 Think. 打头的变量属于特殊模板变量 * @access public * @param string $varStr 变量字符串 * @return string */ public function parseThinkVar($varStr){ if(is_array($varStr)){//用于正则替换回调函数 $varStr = $varStr[1]; } $vars = explode('.',$varStr); $vars[1] = strtoupper(trim($vars[1])); $parseStr = ''; if(count($vars)>=3){ $vars[2] = trim($vars[2]); switch($vars[1]){ case 'SERVER': $parseStr = '$_SERVER[\''.$vars[2].'\']';break; case 'GET': $parseStr = '$_GET[\''.$vars[2].'\']';break; case 'POST': $parseStr = '$_POST[\''.$vars[2].'\']';break; case 'COOKIE': if(isset($vars[3])) { $parseStr = '$_COOKIE[\''.$vars[2].'\'][\''.$vars[3].'\']'; }elseif(C('COOKIE_PREFIX')){ $parseStr = '$_COOKIE[\''.C('COOKIE_PREFIX').$vars[2].'\']'; }else{ $parseStr = '$_COOKIE[\''.$vars[2].'\']'; } break; case 'SESSION': if(isset($vars[3])) { $parseStr = '$_SESSION[\''.$vars[2].'\'][\''.$vars[3].'\']'; }elseif(C('SESSION_PREFIX')){ $parseStr = '$_SESSION[\''.C('SESSION_PREFIX').'\'][\''.$vars[2].'\']'; }else{ $parseStr = '$_SESSION[\''.$vars[2].'\']'; } break; case 'ENV': $parseStr = '$_ENV[\''.$vars[2].'\']';break; case 'REQUEST': $parseStr = '$_REQUEST[\''.$vars[2].'\']';break; case 'CONST': $parseStr = strtoupper($vars[2]);break; case 'LANG': $parseStr = 'L("'.$vars[2].'")';break; case 'CONFIG': $parseStr = 'C("'.$vars[2].'")';break; } }else if(count($vars)==2){ switch($vars[1]){ case 'NOW': $parseStr = "date('Y-m-d g:i a',time())";break; case 'VERSION': $parseStr = 'THINK_VERSION';break; case 'TEMPLATE':$parseStr = 'C("TEMPLATE_NAME")';break; case 'LDELIM': $parseStr = 'C("TMPL_L_DELIM")';break; case 'RDELIM': $parseStr = 'C("TMPL_R_DELIM")';break; default: if(defined($vars[1])) $parseStr = $vars[1]; } } return $parseStr; } // 获取标签定义 public function getTags(){ return $this->tags; } } ================================================ FILE: ThinkPHP/Library/Think/Template.class.php ================================================ // +---------------------------------------------------------------------- namespace Think; /** * ThinkPHP内置模板引擎类 * 支持XML标签和普通标签的模板解析 * 编译型模板引擎 支持动态缓存 */ class Template { // 模板页面中引入的标签库列表 protected $tagLib = array(); // 当前模板文件 protected $templateFile = ''; // 模板变量 public $tVar = array(); public $config = array(); private $literal = array(); private $block = array(); /** * 架构函数 * @access public */ public function __construct(){ $this->config['cache_path'] = C('CACHE_PATH'); $this->config['template_suffix'] = C('TMPL_TEMPLATE_SUFFIX'); $this->config['cache_suffix'] = C('TMPL_CACHFILE_SUFFIX'); $this->config['tmpl_cache'] = C('TMPL_CACHE_ON'); $this->config['cache_time'] = C('TMPL_CACHE_TIME'); $this->config['taglib_begin'] = $this->stripPreg(C('TAGLIB_BEGIN')); $this->config['taglib_end'] = $this->stripPreg(C('TAGLIB_END')); $this->config['tmpl_begin'] = $this->stripPreg(C('TMPL_L_DELIM')); $this->config['tmpl_end'] = $this->stripPreg(C('TMPL_R_DELIM')); $this->config['default_tmpl'] = C('TEMPLATE_NAME'); $this->config['layout_item'] = C('TMPL_LAYOUT_ITEM'); } private function stripPreg($str) { return str_replace( array('{','}','(',')','|','[',']','-','+','*','.','^','?'), array('\{','\}','\(','\)','\|','\[','\]','\-','\+','\*','\.','\^','\?'), $str); } // 模板变量获取和设置 public function get($name) { if(isset($this->tVar[$name])) return $this->tVar[$name]; else return false; } public function set($name,$value) { $this->tVar[$name]= $value; } /** * 加载模板 * @access public * @param string $templateFile 模板文件 * @param array $templateVar 模板变量 * @param string $prefix 模板标识前缀 * @return void */ public function fetch($templateFile,$templateVar,$prefix='') { $this->tVar = $templateVar; $templateCacheFile = $this->loadTemplate($templateFile,$prefix); Storage::load($templateCacheFile,$this->tVar,null,'tpl'); } /** * 加载主模板并缓存 * @access public * @param string $templateFile 模板文件 * @param string $prefix 模板标识前缀 * @return string * @throws ThinkExecption */ public function loadTemplate ($templateFile,$prefix='') { if(is_file($templateFile)) { $this->templateFile = $templateFile; // 读取模板文件内容 $tmplContent = file_get_contents($templateFile); }else{ $tmplContent = $templateFile; } // 根据模版文件名定位缓存文件 $tmplCacheFile = $this->config['cache_path'].$prefix.md5($templateFile).$this->config['cache_suffix']; // 判断是否启用布局 if(C('LAYOUT_ON')) { if(false !== strpos($tmplContent,'{__NOLAYOUT__}')) { // 可以单独定义不使用布局 $tmplContent = str_replace('{__NOLAYOUT__}','',$tmplContent); }else{ // 替换布局的主体内容 $layoutFile = THEME_PATH.C('LAYOUT_NAME').$this->config['template_suffix']; // 检查布局文件 if(!is_file($layoutFile)) { E(L('_TEMPLATE_NOT_EXIST_').':'.$layoutFile); } $tmplContent = str_replace($this->config['layout_item'],$tmplContent,file_get_contents($layoutFile)); } } // 编译模板内容 $tmplContent = $this->compiler($tmplContent); Storage::put($tmplCacheFile,trim($tmplContent),'tpl'); return $tmplCacheFile; } /** * 编译模板文件内容 * @access protected * @param mixed $tmplContent 模板内容 * @return string */ protected function compiler($tmplContent) { //模板解析 $tmplContent = $this->parse($tmplContent); // 还原被替换的Literal标签 $tmplContent = preg_replace_callback('//is', array($this, 'restoreLiteral'), $tmplContent); // 添加安全代码 $tmplContent = ''.$tmplContent; // 优化生成的php代码 $tmplContent = str_replace('?>config['taglib_begin']; $end = $this->config['taglib_end']; // 检查include语法 $content = $this->parseInclude($content); // 检查PHP语法 $content = $this->parsePhp($content); // 首先替换literal标签内容 $content = preg_replace_callback('/'.$begin.'literal'.$end.'(.*?)'.$begin.'\/literal'.$end.'/is', array($this, 'parseLiteral'),$content); // 获取需要引入的标签库列表 // 标签库只需要定义一次,允许引入多个一次 // 一般放在文件的最前面 // 格式: // 当TAGLIB_LOAD配置为true时才会进行检测 if(C('TAGLIB_LOAD')) { $this->getIncludeTagLib($content); if(!empty($this->tagLib)) { // 对导入的TagLib进行解析 foreach($this->tagLib as $tagLibName) { $this->parseTagLib($tagLibName,$content); } } } // 预先加载的标签库 无需在每个模板中使用taglib标签加载 但必须使用标签库XML前缀 if(C('TAGLIB_PRE_LOAD')) { $tagLibs = explode(',',C('TAGLIB_PRE_LOAD')); foreach ($tagLibs as $tag){ $this->parseTagLib($tag,$content); } } // 内置标签库 无需使用taglib标签导入就可以使用 并且不需使用标签库XML前缀 $tagLibs = explode(',',C('TAGLIB_BUILD_IN')); foreach ($tagLibs as $tag){ $this->parseTagLib($tag,$content,true); } //解析普通模板标签 {$tagName} $content = preg_replace_callback('/('.$this->config['tmpl_begin'].')([^\d\w\s'.$this->config['tmpl_begin'].$this->config['tmpl_end'].'].+?)('.$this->config['tmpl_end'].')/is', array($this, 'parseTag'),$content); return $content; } // 检查PHP语法 protected function parsePhp($content) { if(ini_get('short_open_tag')){ // 开启短标签的情况要将'."\n", $content ); } // PHP语法检查 if(C('TMPL_DENY_PHP') && false !== strpos($content,'config['taglib_begin'].'layout\s(.+?)\s*?\/'.$this->config['taglib_end'].'/is',$content,$matches); if($find) { //替换Layout标签 $content = str_replace($matches[0],'',$content); //解析Layout标签 $array = $this->parseXmlAttrs($matches[1]); if(!C('LAYOUT_ON') || C('LAYOUT_NAME') !=$array['name'] ) { // 读取布局模板 $layoutFile = THEME_PATH.$array['name'].$this->config['template_suffix']; $replace = isset($array['replace'])?$array['replace']:$this->config['layout_item']; // 替换布局的主体内容 $content = str_replace($replace,$content,file_get_contents($layoutFile)); } }else{ $content = str_replace('{__NOLAYOUT__}','',$content); } return $content; } // 解析模板中的include标签 protected function parseInclude($content, $extend = true) { // 解析继承 if($extend) $content = $this->parseExtend($content); // 解析布局 $content = $this->parseLayout($content); // 读取模板中的include标签 $find = preg_match_all('/'.$this->config['taglib_begin'].'include\s(.+?)\s*?\/'.$this->config['taglib_end'].'/is',$content,$matches); if($find) { for($i=0;$i<$find;$i++) { $include = $matches[1][$i]; $array = $this->parseXmlAttrs($include); $file = $array['file']; unset($array['file']); $content = str_replace($matches[0][$i],$this->parseIncludeItem($file,$array,$extend),$content); } } return $content; } // 解析模板中的extend标签 protected function parseExtend($content) { $begin = $this->config['taglib_begin']; $end = $this->config['taglib_end']; // 读取模板中的继承标签 $find = preg_match('/'.$begin.'extend\s(.+?)\s*?\/'.$end.'/is',$content,$matches); if($find) { //替换extend标签 $content = str_replace($matches[0],'',$content); // 记录页面中的block标签 preg_replace_callback('/'.$begin.'block\sname=[\'"](.+?)[\'"]\s*?'.$end.'(.*?)'.$begin.'\/block'.$end.'/is', array($this, 'parseBlock'),$content); // 读取继承模板 $array = $this->parseXmlAttrs($matches[1]); $content = $this->parseTemplateName($array['name']); $content = $this->parseInclude($content, false); //对继承模板中的include进行分析 // 替换block标签 $content = $this->replaceBlock($content); }else{ $content = preg_replace_callback('/'.$begin.'block\sname=[\'"](.+?)[\'"]\s*?'.$end.'(.*?)'.$begin.'\/block'.$end.'/is', function($match){return stripslashes($match[2]);}, $content); } return $content; } /** * 分析XML属性 * @access private * @param string $attrs XML属性字符串 * @return array */ private function parseXmlAttrs($attrs) { $xml = ''; $xml = simplexml_load_string($xml); if(!$xml) E(L('_XML_TAG_ERROR_')); $xml = (array)($xml->tag->attributes()); $array = array_change_key_case($xml['@attributes']); return $array; } /** * 替换页面中的literal标签 * @access private * @param string $content 模板内容 * @return string|false */ private function parseLiteral($content) { if(is_array($content)) $content = $content[1]; if(trim($content)=='') return ''; //$content = stripslashes($content); $i = count($this->literal); $parseStr = ""; $this->literal[$i] = $content; return $parseStr; } /** * 还原被替换的literal标签 * @access private * @param string $tag literal标签序号 * @return string|false */ private function restoreLiteral($tag) { if(is_array($tag)) $tag = $tag[1]; // 还原literal标签 $parseStr = $this->literal[$tag]; // 销毁literal记录 unset($this->literal[$tag]); return $parseStr; } /** * 记录当前页面中的block标签 * @access private * @param string $name block名称 * @param string $content 模板内容 * @return string */ private function parseBlock($name,$content = '') { if(is_array($name)){ $content = $name[2]; $name = $name[1]; } $this->block[$name] = $content; return ''; } /** * 替换继承模板中的block标签 * @access private * @param string $content 模板内容 * @return string */ private function replaceBlock($content){ static $parse = 0; $begin = $this->config['taglib_begin']; $end = $this->config['taglib_end']; $reg = '/('.$begin.'block\sname=[\'"](.+?)[\'"]\s*?'.$end.')(.*?)'.$begin.'\/block'.$end.'/is'; if(is_string($content)){ do{ $content = preg_replace_callback($reg, array($this, 'replaceBlock'), $content); } while ($parse && $parse--); return $content; } elseif(is_array($content)){ if(preg_match('/'.$begin.'block\sname=[\'"](.+?)[\'"]\s*?'.$end.'/is', $content[3])){ //存在嵌套,进一步解析 $parse = 1; $content[3] = preg_replace_callback($reg, array($this, 'replaceBlock'), "{$content[3]}{$begin}/block{$end}"); return $content[1] . $content[3]; } else { $name = $content[2]; $content = $content[3]; $content = isset($this->block[$name]) ? $this->block[$name] : $content; return $content; } } } /** * 搜索模板页面中包含的TagLib库 * 并返回列表 * @access public * @param string $content 模板内容 * @return string|false */ public function getIncludeTagLib(& $content) { //搜索是否有TagLib标签 $find = preg_match('/'.$this->config['taglib_begin'].'taglib\s(.+?)(\s*?)\/'.$this->config['taglib_end'].'\W/is',$content,$matches); if($find) { //替换TagLib标签 $content = str_replace($matches[0],'',$content); //解析TagLib标签 $array = $this->parseXmlAttrs($matches[1]); $this->tagLib = explode(',',$array['name']); } return; } /** * TagLib库解析 * @access public * @param string $tagLib 要解析的标签库 * @param string $content 要解析的模板内容 * @param boolean $hide 是否隐藏标签库前缀 * @return string */ public function parseTagLib($tagLib,&$content,$hide=false) { $begin = $this->config['taglib_begin']; $end = $this->config['taglib_end']; if(strpos($tagLib,'\\')){ // 支持指定标签库的命名空间 $className = $tagLib; $tagLib = substr($tagLib,strrpos($tagLib,'\\')+1); }else{ $className = 'Think\\Template\TagLib\\'.ucwords($tagLib); } $tLib = \Think\Think::instance($className); $that = $this; foreach ($tLib->getTags() as $name=>$val){ $tags = array($name); if(isset($val['alias'])) {// 别名设置 $tags = explode(',',$val['alias']); $tags[] = $name; } $level = isset($val['level'])?$val['level']:1; $closeTag = isset($val['close'])?$val['close']:true; foreach ($tags as $tag){ $parseTag = !$hide? $tagLib.':'.$tag: $tag;// 实际要解析的标签名称 if(!method_exists($tLib,'_'.$tag)) { // 别名可以无需定义解析方法 $tag = $name; } $n1 = empty($val['attr'])?'(\s*?)':'\s([^'.$end.']*)'; $this->tempVar = array($tagLib, $tag); if (!$closeTag){ $patterns = '/'.$begin.$parseTag.$n1.'\/(\s*?)'.$end.'/is'; $content = preg_replace_callback($patterns, function($matches) use($tLib,$tag,$that){ return $that->parseXmlTag($tLib,$tag,$matches[1],$matches[2]); },$content); }else{ $patterns = '/'.$begin.$parseTag.$n1.$end.'(.*?)'.$begin.'\/'.$parseTag.'(\s*?)'.$end.'/is'; for($i=0;$i<$level;$i++) { $content=preg_replace_callback($patterns,function($matches) use($tLib,$tag,$that){ return $that->parseXmlTag($tLib,$tag,$matches[1],$matches[2]); },$content); } } } } } /** * 解析标签库的标签 * 需要调用对应的标签库文件解析类 * @access public * @param object $tagLib 标签库对象实例 * @param string $tag 标签名 * @param string $attr 标签属性 * @param string $content 标签内容 * @return string|false */ public function parseXmlTag($tagLib,$tag,$attr,$content) { if(ini_get('magic_quotes_sybase')) $attr = str_replace('\"','\'',$attr); $parse = '_'.$tag; $content = trim($content); $tags = $tagLib->parseXmlAttr($attr,$tag); return $tagLib->$parse($tags,$content); } /** * 模板标签解析 * 格式: {TagName:args [|content] } * @access public * @param string $tagStr 标签内容 * @return string */ public function parseTag($tagStr){ if(is_array($tagStr)) $tagStr = $tagStr[2]; //if (MAGIC_QUOTES_GPC) { $tagStr = stripslashes($tagStr); //} $flag = substr($tagStr,0,1); $flag2 = substr($tagStr,1,1); $name = substr($tagStr,1); if('$' == $flag && '.' != $flag2 && '(' != $flag2){ //解析模板变量 格式 {$varName} return $this->parseVar($name); }elseif('-' == $flag || '+'== $flag){ // 输出计算 return ''; }elseif(':' == $flag){ // 输出某个函数的结果 return ''; }elseif('~' == $flag){ // 执行某个函数 return ''; }elseif(substr($tagStr,0,2)=='//' || (substr($tagStr,0,2)=='/*' && substr(rtrim($tagStr),-2)=='*/')){ //注释标签 return ''; } // 未识别的标签直接返回 return C('TMPL_L_DELIM') . $tagStr .C('TMPL_R_DELIM'); } /** * 模板变量解析,支持使用函数 * 格式: {$varname|function1|function2=arg1,arg2} * @access public * @param string $varStr 变量数据 * @return string */ public function parseVar($varStr){ $varStr = trim($varStr); static $_varParseList = array(); //如果已经解析过该变量字串,则直接返回变量值 if(isset($_varParseList[$varStr])) return $_varParseList[$varStr]; $parseStr = ''; $varExists = true; if(!empty($varStr)){ $varArray = explode('|',$varStr); //取得变量名称 $var = array_shift($varArray); if('Think.' == substr($var,0,6)){ // 所有以Think.打头的以特殊变量对待 无需模板赋值就可以输出 $name = $this->parseThinkVar($var); }elseif( false !== strpos($var,'.')) { //支持 {$var.property} $vars = explode('.',$var); $var = array_shift($vars); switch(strtolower(C('TMPL_VAR_IDENTIFY'))) { case 'array': // 识别为数组 $name = '$'.$var; foreach ($vars as $key=>$val) $name .= '["'.$val.'"]'; break; case 'obj': // 识别为对象 $name = '$'.$var; foreach ($vars as $key=>$val) $name .= '->'.$val; break; default: // 自动判断数组或对象 只支持二维 $name = 'is_array($'.$var.')?$'.$var.'["'.$vars[0].'"]:$'.$var.'->'.$vars[0]; } }elseif(false !== strpos($var,'[')) { //支持 {$var['key']} 方式输出数组 $name = "$".$var; preg_match('/(.+?)\[(.+?)\]/is',$var,$match); $var = $match[1]; }elseif(false !==strpos($var,':') && false ===strpos($var,'(') && false ===strpos($var,'::') && false ===strpos($var,'?')){ //支持 {$var:property} 方式输出对象的属性 $vars = explode(':',$var); $var = str_replace(':','->',$var); $name = "$".$var; $var = $vars[0]; }else { $name = "$$var"; } //对变量使用函数 if(count($varArray)>0) $name = $this->parseVarFunction($name,$varArray); $parseStr = ''; } $_varParseList[$varStr] = $parseStr; return $parseStr; } /** * 对模板变量使用函数 * 格式 {$varname|function1|function2=arg1,arg2} * @access public * @param string $name 变量名 * @param array $varArray 函数列表 * @return string */ public function parseVarFunction($name,$varArray){ //对变量使用函数 $length = count($varArray); //取得模板禁止使用函数列表 $template_deny_funs = explode(',',C('TMPL_DENY_FUNC_LIST')); for($i=0;$i<$length ;$i++ ){ $args = explode('=',$varArray[$i],2); //模板函数过滤 $fun = trim($args[0]); switch($fun) { case 'default': // 特殊模板函数 $name = '(isset('.$name.') && ('.$name.' !== ""))?('.$name.'):'.$args[1]; break; default: // 通用模板函数 if(!in_array($fun,$template_deny_funs)){ if(isset($args[1])){ if(strstr($args[1],'###')){ $args[1] = str_replace('###',$name,$args[1]); $name = "$fun($args[1])"; }else{ $name = "$fun($name,$args[1])"; } }else if(!empty($args[0])){ $name = "$fun($name)"; } } } } return $name; } /** * 特殊模板变量解析 * 格式 以 $Think. 打头的变量属于特殊模板变量 * @access public * @param string $varStr 变量字符串 * @return string */ public function parseThinkVar($varStr){ $vars = explode('.',$varStr); $vars[1] = strtoupper(trim($vars[1])); $parseStr = ''; if(count($vars)>=3){ $vars[2] = trim($vars[2]); switch($vars[1]){ case 'SERVER': $parseStr = '$_SERVER[\''.strtoupper($vars[2]).'\']';break; case 'GET': $parseStr = '$_GET[\''.$vars[2].'\']';break; case 'POST': $parseStr = '$_POST[\''.$vars[2].'\']';break; case 'COOKIE': if(isset($vars[3])) { $parseStr = '$_COOKIE[\''.$vars[2].'\'][\''.$vars[3].'\']'; }else{ $parseStr = 'cookie(\''.$vars[2].'\')'; } break; case 'SESSION': if(isset($vars[3])) { $parseStr = '$_SESSION[\''.$vars[2].'\'][\''.$vars[3].'\']'; }else{ $parseStr = 'session(\''.$vars[2].'\')'; } break; case 'ENV': $parseStr = '$_ENV[\''.strtoupper($vars[2]).'\']';break; case 'REQUEST': $parseStr = '$_REQUEST[\''.$vars[2].'\']';break; case 'CONST': $parseStr = strtoupper($vars[2]);break; case 'LANG': $parseStr = 'L("'.$vars[2].'")';break; case 'CONFIG': if(isset($vars[3])) { $vars[2] .= '.'.$vars[3]; } $parseStr = 'C("'.$vars[2].'")';break; default:break; } }else if(count($vars)==2){ switch($vars[1]){ case 'NOW': $parseStr = "date('Y-m-d g:i a',time())"; break; case 'VERSION': $parseStr = 'THINK_VERSION'; break; case 'TEMPLATE': $parseStr = "'".$this->templateFile."'";//'C("TEMPLATE_NAME")'; break; case 'LDELIM': $parseStr = 'C("TMPL_L_DELIM")'; break; case 'RDELIM': $parseStr = 'C("TMPL_R_DELIM")'; break; default: if(defined($vars[1])) $parseStr = $vars[1]; } } return $parseStr; } /** * 加载公共模板并缓存 和当前模板在同一路径,否则使用相对路径 * @access private * @param string $tmplPublicName 公共模板文件名 * @param array $vars 要传递的变量列表 * @return string */ private function parseIncludeItem($tmplPublicName,$vars=array(),$extend){ // 分析模板文件名并读取内容 $parseStr = $this->parseTemplateName($tmplPublicName); // 替换变量 foreach ($vars as $key=>$val) { $parseStr = str_replace('['.$key.']',$val,$parseStr); } // 再次对包含文件进行模板分析 return $this->parseInclude($parseStr,$extend); } /** * 分析加载的模板文件并读取内容 支持多个模板文件读取 * @access private * @param string $tmplPublicName 模板文件名 * @return string */ private function parseTemplateName($templateName){ if(substr($templateName,0,1)=='$') //支持加载变量文件名 $templateName = $this->get(substr($templateName,1)); $array = explode(',',$templateName); $parseStr = ''; foreach ($array as $templateName){ if(empty($templateName)) continue; if(false === strpos($templateName,$this->config['template_suffix'])) { // 解析规则为 模块@主题/控制器/操作 $templateName = T($templateName); } // 获取模板文件内容 $parseStr .= file_get_contents($templateName); } return $parseStr; } } ================================================ FILE: ThinkPHP/Library/Think/Think.class.php ================================================ // +---------------------------------------------------------------------- namespace Think; /** * ThinkPHP 引导类 */ class Think { // 类映射 private static $_map = array(); // 实例化对象 private static $_instance = array(); /** * 应用程序初始化 * @access public * @return void */ static public function start() { // 注册AUTOLOAD方法 spl_autoload_register('Think\Think::autoload'); // 设定错误和异常处理 register_shutdown_function('Think\Think::fatalError'); set_error_handler('Think\Think::appError'); set_exception_handler('Think\Think::appException'); // 初始化文件存储方式 Storage::connect(STORAGE_TYPE); $runtimefile = RUNTIME_PATH.APP_MODE.'~runtime.php'; if(!APP_DEBUG && Storage::has($runtimefile)){ Storage::load($runtimefile); }else{ if(Storage::has($runtimefile)) Storage::unlink($runtimefile); $content = ''; // 读取应用模式 $mode = include is_file(CONF_PATH.'core.php')?CONF_PATH.'core.php':MODE_PATH.APP_MODE.'.php'; // 加载核心文件 foreach ($mode['core'] as $file){ if(is_file($file)) { include $file; if(!APP_DEBUG) $content .= compile($file); } } // 加载应用模式配置文件 foreach ($mode['config'] as $key=>$file){ is_numeric($key)?C(load_config($file)):C($key,load_config($file)); } // 读取当前应用模式对应的配置文件 if('common' != APP_MODE && is_file(CONF_PATH.'config_'.APP_MODE.CONF_EXT)) C(load_config(CONF_PATH.'config_'.APP_MODE.CONF_EXT)); // 加载模式别名定义 if(isset($mode['alias'])){ self::addMap(is_array($mode['alias'])?$mode['alias']:include $mode['alias']); } // 加载应用别名定义文件 if(is_file(CONF_PATH.'alias.php')) self::addMap(include CONF_PATH.'alias.php'); // 加载模式行为定义 if(isset($mode['tags'])) { Hook::import(is_array($mode['tags'])?$mode['tags']:include $mode['tags']); } // 加载应用行为定义 if(is_file(CONF_PATH.'tags.php')) // 允许应用增加开发模式配置定义 Hook::import(include CONF_PATH.'tags.php'); // 加载框架底层语言包 L(include THINK_PATH.'Lang/'.strtolower(C('DEFAULT_LANG')).'.php'); if(!APP_DEBUG){ $content .= "\nnamespace { Think\\Think::addMap(".var_export(self::$_map,true).");"; $content .= "\nL(".var_export(L(),true).");\nC(".var_export(C(),true).');Think\Hook::import('.var_export(Hook::get(),true).');}'; Storage::put($runtimefile,strip_whitespace('getMessage(); $trace = $e->getTrace(); if('E'==$trace[0]['function']) { $error['file'] = $trace[0]['file']; $error['line'] = $trace[0]['line']; }else{ $error['file'] = $e->getFile(); $error['line'] = $e->getLine(); } $error['trace'] = $e->getTraceAsString(); Log::record($error['message'],Log::ERR); // 发送404信息 header('HTTP/1.1 404 Not Found'); header('Status:404 Not Found'); self::halt($error); } /** * 自定义错误处理 * @access public * @param int $errno 错误类型 * @param string $errstr 错误信息 * @param string $errfile 错误文件 * @param int $errline 错误行数 * @return void */ static public function appError($errno, $errstr, $errfile, $errline) { switch ($errno) { case E_ERROR: case E_PARSE: case E_CORE_ERROR: case E_COMPILE_ERROR: case E_USER_ERROR: ob_end_clean(); $errorStr = "$errstr ".$errfile." 第 $errline 行."; if(C('LOG_RECORD')) Log::write("[$errno] ".$errorStr,Log::ERR); self::halt($errorStr); break; default: $errorStr = "[$errno] $errstr ".$errfile." 第 $errline 行."; self::trace($errorStr,'','NOTIC'); break; } } // 致命错误捕获 static public function fatalError() { Log::save(); if ($e = error_get_last()) { switch($e['type']){ case E_ERROR: case E_PARSE: case E_CORE_ERROR: case E_COMPILE_ERROR: case E_USER_ERROR: ob_end_clean(); self::halt($e); break; } } } /** * 错误输出 * @param mixed $error 错误 * @return void */ static public function halt($error) { $e = array(); if (APP_DEBUG || IS_CLI) { //调试模式下输出错误信息 if (!is_array($error)) { $trace = debug_backtrace(); $e['message'] = $error; $e['file'] = $trace[0]['file']; $e['line'] = $trace[0]['line']; ob_start(); debug_print_backtrace(); $e['trace'] = ob_get_clean(); } else { $e = $error; } if(IS_CLI){ exit(iconv('UTF-8','gbk',$e['message']).PHP_EOL.'FILE: '.$e['file'].'('.$e['line'].')'.PHP_EOL.$e['trace']); } } else { //否则定向到错误页面 $error_page = C('ERROR_PAGE'); if (!empty($error_page)) { redirect($error_page); } else { $message = is_array($error) ? $error['message'] : $error; $e['message'] = C('SHOW_ERROR_MSG')? $message : C('ERROR_MESSAGE'); } } // 包含异常页面模板 $exceptionFile = C('TMPL_EXCEPTION_FILE',null,THINK_PATH.'Tpl/think_exception.tpl'); include $exceptionFile; exit; } /** * 添加和获取页面Trace记录 * @param string $value 变量 * @param string $label 标签 * @param string $level 日志级别(或者页面Trace的选项卡) * @param boolean $record 是否记录日志 * @return void|array */ static public function trace($value='[think]',$label='',$level='DEBUG',$record=false) { static $_trace = array(); if('[think]' === $value){ // 获取trace信息 return $_trace; }else{ $info = ($label?$label.':':'').print_r($value,true); $level = strtoupper($level); if((defined('IS_AJAX') && IS_AJAX) || !C('SHOW_PAGE_TRACE') || $record) { Log::record($info,$level,$record); }else{ if(!isset($_trace[$level]) || count($_trace[$level])>C('TRACE_MAX_RECORD')) { $_trace[$level] = array(); } $_trace[$level][] = $info; } } } } ================================================ FILE: ThinkPHP/Library/Think/Upload/Driver/Bcs/bcs.class.php ================================================ ak = $ak; $this->sk = $sk; } elseif (defined ( 'BCS_AK' ) && defined ( 'BCS_SK' ) && strlen ( BCS_AK ) > 0 && strlen ( BCS_SK ) > 0) { $this->ak = BCS_AK; $this->sk = BCS_SK; } elseif (false !== getenv ( 'HTTP_BAE_ENV_AK' ) && false !== getenv ( 'HTTP_BAE_ENV_SK' )) { $this->ak = getenv ( 'HTTP_BAE_ENV_AK' ); $this->sk = getenv ( 'HTTP_BAE_ENV_SK' ); } else { throw new BCS_Exception ( 'Construct can not get ak &sk pair, please check!' ); } //valid $hostname if (NULL !== $hostname) { $this->hostname = $hostname; } elseif (false !== getenv ( 'HTTP_BAE_ENV_ADDR_BCS' )) { $this->hostname = getenv ( 'HTTP_BAE_ENV_ADDR_BCS' ); } else { $this->hostname = self::DEFAULT_URL; } } /** * 将消息发往Baidu BCS. * @param array $opt * @return BCS_ResponseCore */ private function authenticate($opt) { //set common param into opt $opt [self::AK] = $this->ak; $opt [self::SK] = $this->sk; // Validate the S3 bucket name, only list_bucket didnot need validate_bucket if (! ('/' == $opt [self::OBJECT] && '' == $opt [self::BUCKET] && 'GET' == $opt [self::METHOD] && ! isset ( $opt [self::QUERY_STRING] [self::ACL] )) && ! self::validate_bucket ( $opt [self::BUCKET] )) { throw new BCS_Exception ( $opt [self::BUCKET] . 'is not valid, please check!' ); } //Validate object if (isset ( $opt [self::OBJECT] ) && ! self::validate_object ( $opt [self::OBJECT] )) { throw new BCS_Exception ( "Invalid object param[" . $opt [self::OBJECT] . "], please check.", - 1 ); } //construct url $url = $this->format_url ( $opt ); if ($url === false) { throw new BCS_Exception ( 'Can not format url, please check your param!', - 1 ); } $opt ['url'] = $url; $this->log ( "[method:" . $opt [self::METHOD] . "][url:$url]", $opt ); //build request $request = new BCS_RequestCore ( $opt ['url'] ); $headers = array ( 'Content-Type' => 'application/x-www-form-urlencoded' ); $request->set_method ( $opt [self::METHOD] ); //Write get_object content to fileWriteTo if (isset ( $opt ['fileWriteTo'] )) { $request->set_write_file ( $opt ['fileWriteTo'] ); } // Merge the HTTP headers if (isset ( $opt [self::HEADERS] )) { $headers = array_merge ( $headers, $opt [self::HEADERS] ); } // Set content to Http-Body if (isset ( $opt ['content'] )) { $request->set_body ( $opt ['content'] ); } // Upload file if (isset ( $opt ['fileUpload'] )) { if (! file_exists ( $opt ['fileUpload'] )) { throw new BCS_Exception ( 'File[' . $opt ['fileUpload'] . '] not found!', - 1 ); } $request->set_read_file ( $opt ['fileUpload'] ); // Determine the length to read from the file $length = $request->read_stream_size; // The file size by default $file_size = $length; if (isset ( $opt ["length"] )) { if ($opt ["length"] > $file_size) { throw new BCS_Exception ( "Input opt[length] invalid! It can not bigger than file-size", - 1 ); } $length = $opt ['length']; } if (isset ( $opt ['seekTo'] ) && ! isset ( $opt ["length"] )) { // Read from seekTo until EOF by default, when set seekTo but not set $opt["length"] $length -= ( integer ) $opt ['seekTo']; } $request->set_read_stream_size ( $length ); // Attempt to guess the correct mime-type if ($headers ['Content-Type'] === 'application/x-www-form-urlencoded') { $extension = explode ( '.', $opt ['fileUpload'] ); $extension = array_pop ( $extension ); $mime_type = BCS_MimeTypes::get_mimetype ( $extension ); $headers ['Content-Type'] = $mime_type; } $headers ['Content-MD5'] = ''; } // Handle streaming file offsets if (isset ( $opt ['seekTo'] )) { // Pass the seek position to BCS_RequestCore $request->set_seek_position ( ( integer ) $opt ['seekTo'] ); } // Add headers to request and compute the string to sign foreach ( $headers as $header_key => $header_value ) { // Strip linebreaks from header values as they're illegal and can allow for security issues $header_value = str_replace ( array ( "\r", "\n" ), '', $header_value ); // Add the header if it has a value if ($header_value !== '') { $request->add_header ( $header_key, $header_value ); } } // Set the curl options. if (isset ( $opt ['curlopts'] ) && count ( $opt ['curlopts'] )) { $request->set_curlopts ( $opt ['curlopts'] ); } $request->send_request (); require_once(dirname(__FILE__). "/requestcore.class.php"); return new BCS_ResponseCore ( $request->get_response_header (), $request->get_response_body (), $request->get_response_code () ); } /** * 获取当前密钥对拥有者的bucket列表 * @param array $opt (Optional) * BaiduBCS::IMPORT_BCS_LOG_METHOD - String - Optional: 支持用户传入日志处理函数,函数定义如 function f($log) * @throws BCS_Exception * @return BCS_ResponseCore */ public function list_bucket($opt = array()) { $this->assertParameterArray ( $opt ); $opt [self::BUCKET] = ''; $opt [self::METHOD] = 'GET'; $opt [self::OBJECT] = '/'; $response = $this->authenticate ( $opt ); $this->log ( $response->isOK () ? "List bucket success!" : "List bucket failed! Response: [" . $response->body . "]", $opt ); return $response; } /** * 创建 bucket * @param string $bucket (Required) bucket名称 * @param string $acl (Optional) bucket权限设置,若为null,使用server分配的默认权限 * @param array $opt (Optional) * @throws BCS_Exception * @return BCS_ResponseCore */ public function create_bucket($bucket, $acl = NULL, $opt = array()) { $this->assertParameterArray ( $opt ); $opt [self::BUCKET] = $bucket; $opt [self::METHOD] = 'PUT'; $opt [self::OBJECT] = '/'; if (NULL !== $acl) { if (! in_array ( $acl, self::$ACL_TYPES )) { throw new BCS_Exception ( "Invalid acl_type[" . $acl . "], please check!", - 1 ); } self::set_header_into_opt ( "x-bs-acl", $acl, $opt ); } $response = $this->authenticate ( $opt ); $this->log ( $response->isOK () ? "Create bucket success!" : "Create bucket failed! Response: [" . $response->body . "]", $opt ); return $response; } /** * 删除bucket * @param string $bucket (Required) * @param array $opt (Optional) * @return boolean|BCS_ResponseCore */ public function delete_bucket($bucket, $opt = array()) { $this->assertParameterArray ( $opt ); $opt [self::BUCKET] = $bucket; $opt [self::METHOD] = 'DELETE'; $opt [self::OBJECT] = '/'; $response = $this->authenticate ( $opt ); $this->log ( $response->isOK () ? "Delete bucket success!" : "Delete bucket failed! Response: [" . $response->body . "]", $opt ); return $response; } /** * 设置bucket的acl,有三种模式, * (1).设置详细json格式的acl; * a. $acl 为json的array * b. $acl 为json的string * (2).通过acl_type字段进行设置 * a. $acl 为BaiduBCS::$ACL_TYPES中的字段 * @param string $bucket (Required) * @param string $acl (Required) * @param array $opt (Optional) * @return boolean|BCS_ResponseCore */ public function set_bucket_acl($bucket, $acl, $opt = array()) { $this->assertParameterArray ( $opt ); $result = $this->analyze_user_acl ( $acl ); $opt = array_merge ( $opt, $result ); $opt [self::BUCKET] = $bucket; $opt [self::METHOD] = 'PUT'; $opt [self::OBJECT] = '/'; $opt [self::QUERY_STRING] = array ( self::ACL => 1 ); $response = $this->authenticate ( $opt ); $this->log ( $response->isOK () ? "Set bucket acl success!" : "Set bucket acl failed! Response: [" . $response->body . "]", $opt ); return $response; } /** * 获取bucket的acl * @param string $bucket (Required) * @param array $opt (Optional) * @return BCS_ResponseCore */ public function get_bucket_acl($bucket, $opt = array()) { $this->assertParameterArray ( $opt ); $opt [self::BUCKET] = $bucket; $opt [self::METHOD] = 'GET'; $opt [self::OBJECT] = '/'; $opt [self::QUERY_STRING] = array ( self::ACL => 1 ); $response = $this->authenticate ( $opt ); $this->log ( $response->isOK () ? "Get bucket acl success!" : "Get bucket acl failed! Response: [" . $response->body . "]", $opt ); return $response; } /** * 获取bucket中object列表 * @param string $bucket (Required) * @param array $opt (Optional) * start : 主要用于翻页功能,用法同mysql中start的用法 * limit : 主要用于翻页功能,用法同mysql中limit的用法 * prefix: 只返回以prefix为前缀的object,此处prefix必须以'/'开头 * @throws BCS_Exception * @return BCS_ResponseCore */ public function list_object($bucket, $opt = array()) { $this->assertParameterArray ( $opt ); $opt [self::BUCKET] = $bucket; if (empty ( $opt [self::BUCKET] )) { throw new BCS_Exception ( "Bucket should not be empty, please check", - 1 ); } $opt [self::METHOD] = 'GET'; $opt [self::OBJECT] = '/'; $opt [self::QUERY_STRING] = array (); if (isset ( $opt ['start'] ) && is_int ( $opt ['start'] )) { $opt [self::QUERY_STRING] ['start'] = $opt ['start']; } if (isset ( $opt ['limit'] ) && is_int ( $opt ['limit'] )) { $opt [self::QUERY_STRING] ['limit'] = $opt ['limit']; } if (isset ( $opt ['prefix'] )) { $opt [self::QUERY_STRING] ['prefix'] = rawurlencode ( $opt ['prefix'] ); } $response = $this->authenticate ( $opt ); $this->log ( $response->isOK () ? "List object success!" : "Lit object failed! Response: [" . $response->body . "]", $opt ); return $response; } /** * 以目录形式获取bucket中object列表 * @param string $bucket (Required) * @param $dir (Required) * 目录名,格式为必须以'/'开头和结尾,默认为'/' * @param string $list_model (Required) * 目录展现形式,值可以为0,1,2,默认为2,以下对各个值的功能进行介绍: * 0->只返回object列表,不返回子目录列表 * 1->只返回子目录列表,不返回object列表 * 2->同时返回子目录列表和object列表 * @param array $opt (Optional) * start : 主要用于翻页功能,用法同mysql中start的用法 * limit : 主要用于翻页功能,用法同mysql中limit的用法 * @throws BCS_Exception * @return BCS_ResponseCore */ public function list_object_by_dir($bucket, $dir = '/', $list_model = 2, $opt = array()) { $this->assertParameterArray ( $opt ); $opt [self::BUCKET] = $bucket; if (empty ( $opt [self::BUCKET] )) { throw new BCS_Exception ( "Bucket should not be empty, please check", - 1 ); } $opt [self::METHOD] = 'GET'; $opt [self::OBJECT] = '/'; $opt [self::QUERY_STRING] = array (); if (isset ( $opt ['start'] ) && is_int ( $opt ['start'] )) { $opt [self::QUERY_STRING] ['start'] = $opt ['start']; } if (isset ( $opt ['limit'] ) && is_int ( $opt ['limit'] )) { $opt [self::QUERY_STRING] ['limit'] = $opt ['limit']; } $opt [self::QUERY_STRING] ['prefix'] = rawurlencode ( $dir ); $opt [self::QUERY_STRING] ['dir'] = $list_model; $response = $this->authenticate ( $opt ); $this->log ( $response->isOK () ? "List object success!" : "Lit object failed! Response: [" . $response->body . "]", $opt ); return $response; } /** * 上传文件 * @param string $bucket (Required) * @param string $object (Required) * @param string $file (Required); 需要上传的文件的文件路径 * @param array $opt (Optional) * filename - Optional; 指定文件名 * acl - Optional ; 上传文件的acl,只能使用acl_type * seekTo - Optional; 上传文件的偏移位置 * length - Optional; 待上传长度 * @return BCS_ResponseCore */ public function create_object($bucket, $object, $file, $opt = array()) { $this->assertParameterArray ( $opt ); $opt [self::BUCKET] = $bucket; $opt [self::OBJECT] = $object; $opt ['fileUpload'] = $file; $opt [self::METHOD] = 'PUT'; if (isset ( $opt ['acl'] )) { if (in_array ( $opt ['acl'], self::$ACL_TYPES )) { self::set_header_into_opt ( "x-bs-acl", $opt ['acl'], $opt ); } else { throw new BCS_Exception ( "Invalid acl string, it should be acl_type", - 1 ); } unset ( $opt ['acl'] ); } if (isset ( $opt ['filename'] )) { self::set_header_into_opt ( "Content-Disposition", 'attachment; filename=' . $opt ['filename'], $opt ); } $response = $this->authenticate ( $opt ); $this->log ( $response->isOK () ? "Create object[$object] file[$file] success!" : "Create object[$object] file[$file] failed! Response: [" . $response->body . "] Logid[" . $response->header ["x-bs-request-id"] . "]", $opt ); return $response; } /** * 上传文件 * @param string $bucket (Required) * @param string $object (Required) * @param string $file (Required); 需要上传的文件的文件路径 * @param array $opt (Optional) * filename - Optional; 指定文件名 * acl - Optional ; 上传文件的acl,只能使用acl_type * @return BCS_ResponseCore */ public function create_object_by_content($bucket, $object, $content, $opt = array()) { $this->assertParameterArray ( $opt ); $opt [self::BUCKET] = $bucket; $opt [self::OBJECT] = $object; $opt [self::METHOD] = 'PUT'; if ($content !== NULL && is_string ( $content )) { $opt ['content'] = $content; } else { throw new BCS_Exception ( "Invalid object content, please check.", - 1 ); } if (isset ( $opt ['acl'] )) { if (in_array ( $opt ['acl'], self::$ACL_TYPES )) { self::set_header_into_opt ( "x-bs-acl", $opt ['acl'], $opt ); } else { throw new BCS_Exception ( "Invalid acl string, it should be acl_type", - 1 ); } unset ( $opt ['acl'] ); } if (isset ( $opt ['filename'] )) { self::set_header_into_opt ( "Content-Disposition", 'attachment; filename=' . $opt ['filename'], $opt ); } $response = $this->authenticate ( $opt ); $this->log ( $response->isOK () ? "Create object[$object] success!" : "Create object[$object] failed! Response: [" . $response->body . "] Logid[" . $response->header ["x-bs-request-id"] . "]", $opt ); return $response; } /** * 通过superfile的方式上传文件 * @param string $bucket (Required) * @param string $object (Required) * @param string $file (Required); 需要上传的文件的文件路径 * @param array $opt (Optional) * filename - Optional; 指定文件名 * sub_object_size - Optional; 指定子文件的划分大小,单位B,建议以256KB为单位进行子object划分,默认为1MB进行划分 * @return BCS_ResponseCore */ public function create_object_superfile($bucket, $object, $file, $opt = array()) { if (isset ( $opt ['length'] ) || isset ( $opt ['seekTo'] )) { throw new BCS_Exception ( "Temporary unsupport opt of length and seekTo of superfile.", - 1 ); } //$opt array $this->assertParameterArray ( $opt ); $opt [self::BUCKET] = $bucket; $opt ['fileUpload'] = $file; $opt [self::METHOD] = 'PUT'; if (isset ( $opt ['acl'] )) { if (in_array ( $opt ['acl'], self::$ACL_TYPES )) { self::set_header_into_opt ( "x-bs-acl", $opt ['acl'], $opt ); } else { throw new BCS_Exception ( "Invalid acl string, it should be acl_type", - 1 ); } unset ( $opt ['acl'] ); } //切片上传 if (! file_exists ( $opt ['fileUpload'] )) { throw new BCS_Exception ( 'File not found!', - 1 ); } $fileSize = filesize ( $opt ['fileUpload'] ); $sub_object_size = 1024 * 1024; //default 1MB if (defined ( "BCS_SUPERFILE_SLICE_SIZE" )) { $sub_object_size = BCS_SUPERFILE_SLICE_SIZE; } if (isset ( $opt ["sub_object_size"] )) { if (is_int ( $opt ["sub_object_size"] ) && $opt ["sub_object_size"] > 0) { $sub_object_size = $opt ["sub_object_size"]; } else { throw new BCS_Exception ( "Param [sub_object_size] invalid ,please check!", - 1 ); } } $sliceNum = intval ( ceil ( $fileSize / $sub_object_size ) ); $this->log ( "File[" . $opt ['fileUpload'] . "], size=[$fileSize], sub_object_size=[$sub_object_size], sub_object_num=[$sliceNum]", $opt ); $object_list = array ( 'object_list' => array () ); for($i = 0; $i < $sliceNum; $i ++) { //send slice $opt ['seekTo'] = $i * $sub_object_size; if (($i + 1) === $sliceNum) { //last sub object $opt ['length'] = (0 === $fileSize % $sub_object_size) ? $sub_object_size : $fileSize % $sub_object_size; } else { $opt ['length'] = $sub_object_size; } $opt [self::OBJECT] = $object . BCS_SUPERFILE_POSTFIX . $i; $object_list ['object_list'] ['part_' . $i] = array (); $object_list ['object_list'] ['part_' . $i] ['url'] = 'bs://' . $bucket . $opt [self::OBJECT]; $this->log ( "Begin to upload Sub-object[" . $opt [self::OBJECT] . "][$i/$sliceNum][seekto:" . $opt ['seekTo'] . "][Length:" . $opt ['length'] . "]", $opt ); $response = $this->create_object ( $bucket, $opt [self::OBJECT], $file, $opt ); if ($response->isOK ()) { $this->log ( "Sub-object upload[" . $opt [self::OBJECT] . "][$i/$sliceNum][seekto:" . $opt ['seekTo'] . "][Length:" . $opt ['length'] . "]success! ", $opt ); $object_list ['object_list'] ['part_' . $i] ['etag'] = $response->header ['Content-MD5']; continue; } else { $this->log ( "Sub-object upload[" . $opt [self::OBJECT] . "][$i/$sliceNum] failed! ", $opt ); return $response; } } //将子文件分片的列表构造成 superfile unset ( $opt ['fileUpload'] ); unset ( $opt ['length'] ); unset ( $opt ['seekTo'] ); $opt ['content'] = self::array_to_json ( $object_list ); $opt [self::QUERY_STRING] = array ( "superfile" => 1 ); $opt [self::OBJECT] = $object; if (isset ( $opt ['filename'] )) { self::set_header_into_opt ( "Content-Disposition", 'attachment; filename=' . $opt ['filename'], $opt ); } $response = $this->authenticate ( $opt ); $this->log ( $response->isOK () ? "Create object-superfile success!" : "Create object-superfile failed! Response: [" . $response->body . "]", $opt ); return $response; } /** * 将目录中的所有文件进行上传,每个文件为单独object,object命名方式下详: * 如有 /home/worker/a/b/c.txt 需上传目录为$dir=/home/worker/a * object命令方式为 * 1. object默认命名方式为 “子目录名 +文件名”,如上述文件c.txt,默认为 '/b/c.txt' * 2. 增强命名模式,在$opt中有可选参数进行配置 * 举例说明 :prefix . has_sub_directory?"/b":"" . '/c.txt' * @param string $bucket (Required) * @param string $dir (Required) * @param array $opt(Optional) * string prefix 文件object前缀 * boolean has_sub_directory(default=true) object命名中是否携带文件的子目录结构,若置为false,请确认待上传的目录和所有子目录中没有重名文件,否则会产生object覆盖问题 * BaiduBCS::IMPORT_BCS_PRE_FILTER 用户可自定义上传文件前的操作函数 * 1. 函数参数列表顺序需为 ($bucket,$object,$file,&$opt),注意$opt为upload_directory函数传入的$opt的拷贝,只对当前object生效 * 2. 函数返回值必须为boolean,当true该文件进行上传,若false跳过上传 * 3. 如果函数返回false,将不会进行post_filter的调用 * BaiduBCS::IMPORT_BCS_POST_FILTER 用户可自定义上传文件后的操作函数 * 1. 函数参数列表顺序需为 ($bucket,$object,$file,&$opt,$response),注意$opt为upload_directory函数传入的$opt的拷贝,只对当前object生效 * 2. 函数返回值无要求 * string seek_object 用户断点续传,需要为object名称,如果该object在目录中不存在,抛出异常,若存在则将该object和此后的object进行上传 * string seek_object_id 作用同seek_object,只需要传入上传过程中日志中展示的[a/b]中object序号即可,注意object序号是以1开始计算的 * @return array 数组形式的上传结果 * 'success' => int 上传成功的文件数目 * 'skipped' => int 被跳过的文件 * 'failed' => array() 上传失败的文件 * */ public function upload_directory($bucket, $dir, $opt = array()) { $this->assertParameterArray ( $opt ); if (! is_dir ( $dir )) { throw new BCS_Exception ( "$dir is not a dir!", - 1 ); } $result = array ( "success" => 0, "failed" => array (), "skipped" => 0 ); $prefix = ""; if (isset ( $opt ['prefix'] )) { $prefix = $opt ['prefix']; } $has_sub_directory = true; if (isset ( $opt ['has_sub_directory'] ) && is_bool ( $opt ['has_sub_directory'] )) { $has_sub_directory = $opt ['has_sub_directory']; } //获取文件树和构造object名 $file_tree = self::get_filetree ( $dir ); $objects = array (); foreach ( $file_tree as $file ) { $object = $has_sub_directory == true ? substr ( $file, strlen ( $dir ) ) : "/" . basename ( $file ); $objects [$prefix . $object] = $file; } $objectCount = count ( $objects ); $before_upload_log = "Upload directory: bucket[$bucket] upload_dir[$dir] file_sum[$objectCount]"; if (isset ( $opt ["seek_object_id"] )) { $before_upload_log .= " seek_object_id[" . $opt ["seek_object_id"] . "/$objectCount]"; } if (isset ( $opt ["seek_object"] )) { $before_upload_log .= " seek_object[" . $opt ["seek_object"] . "]"; } $this->log ( $before_upload_log, $opt ); //查看是否需要查询断点,进行断点续传 if (isset ( $opt ["seek_object_id"] ) && isset ( $opt ["seek_object"] )) { throw new BCS_Exception ( "Can not set see_object_id and seek_object at the same time!", - 1 ); } $num = 1; if (isset ( $opt ["seek_object"] )) { if (isset ( $objects [$opt ["seek_object"]] )) { foreach ( $objects as $object => $file ) { if ($object != $opt ["seek_object"]) { //当非断点文件,该object已完成上传 $this->log ( "Seeking[" . $opt ["seek_object"] . "]. Skip id[$num/$objectCount]object[$object]file[$file].", $opt ); //$result ['skipped'] [] = "[$num/$objectCount] " . $file; $result ['skipped'] ++; unset ( $objects [$object] ); } else { //当找到断点文件,停止循环,从断点文件重新上传 //当非断点文件,该object已完成上传 $this->log ( "Found seek id[$num/$objectCount]object[$object]file[$file], begin from here.", $opt ); break; } $num ++; } } else { throw new BCS_Exception ( "Can not find you seek object, please check!", - 1 ); } } if (isset ( $opt ["seek_object_id"] )) { if (is_int ( $opt ["seek_object_id"] ) && $opt ["seek_object_id"] <= $objectCount) { foreach ( $objects as $object => $file ) { if ($num < $opt ["seek_object_id"]) { $this->log ( "Seeking object of [" . $opt ["seek_object_id"] . "/$objectCount]. Skip id[$num/$objectCount]object[$object]file[$file].", $opt ); //$result ['skipped'] [] = "[$num/$objectCount] " . $file; $result ['skipped'] ++; unset ( $objects [$object] ); } else { break; } $num ++; } } else { throw new BCS_Exception ( "Param seek_object_id not valid, please check!", - 1 ); } } //上传objects $objectCount = count ( $objects ); foreach ( $objects as $object => $file ) { $tmp_opt = array_merge ( $opt ); if (isset ( $opt [self::IMPORT_BCS_PRE_FILTER] ) && function_exists ( $opt [self::IMPORT_BCS_PRE_FILTER] )) { $bolRes = $opt [self::IMPORT_BCS_PRE_FILTER] ( $bucket, $object, $file, $tmp_opt ); if ($bolRes !== true) { $this->log ( "User pre_filter_function return un-true. Skip id[$num/$objectCount]object[$object]file[$file].", $opt ); //$result ['skipped'] [] = "id[$num/$objectCount]object[$object]file[$file]"; $result ['skipped'] ++; $num ++; continue; } } try { $response = $this->create_object ( $bucket, $object, $file, $tmp_opt ); } catch ( Exception $e ) { $this->log ( $e->getMessage (), $opt ); $this->log ( "Upload Failed id[$num/$objectCount]object[$object]file[$file].", $opt ); $num ++; continue; } if ($response->isOK ()) { $result ["success"] ++; $this->log ( "Upload Success id[$num/$objectCount]object[$object]file[$file].", $opt ); } else { $result ["failed"] [] = "id[$num/$objectCount]object[$object]file[$file]"; $this->log ( "Upload Failed id[$num/$objectCount]object[$object]file[$file].", $opt ); } if (isset ( $opt [self::IMPORT_BCS_POST_FILTER] ) && function_exists ( $opt [self::IMPORT_BCS_POST_FILTER] )) { $opt [self::IMPORT_BCS_POST_FILTER] ( $bucket, $object, $file, $tmp_opt, $response ); } $num ++; } //打印日志并返回结果数组 $result_str = "\r\n\r\nUpload $dir to $bucket finished!\r\n"; $result_str .= "**********************************************************\r\n"; $result_str .= "**********************Result Summary**********************\r\n"; $result_str .= "**********************************************************\r\n"; $result_str .= "Upload directory : [$dir]\r\n"; $result_str .= "File num : [$objectCount]\r\n"; $result_str .= "Success: \r\n\tNum: " . $result ["success"] . "\r\n"; $result_str .= "Skipped:\r\n\tNum:" . $result ["skipped"] . "\r\n"; // foreach ( $result ["skipped"] as $skip ) { // $result_str .= "\t$skip\r\n"; // } $result_str .= "Failed:\r\n\tNum:" . count ( $result ["failed"] ) . "\r\n"; foreach ( $result ["failed"] as $fail ) { $result_str .= "\t$fail\r\n"; } if (isset ( $opt [self::IMPORT_BCS_LOG_METHOD] )) { $this->log ( $result_str, $opt ); } else { echo $result_str; } return $result; } /** * 通过此方法以拷贝的方式创建object,object来源为$source * @param array $source (Required) object 来源 * bucket(Required) * object(Required) * @param array $dest (Required) 待拷贝的目标object * bucket(Required) * object(Required) * @param array $opt (Optional) * source_tag 指定拷贝对象的版本号 * @throws BCS_Exception * @return BCS_ResponseCore */ public function copy_object($source, $dest, $opt = array()) { $this->assertParameterArray ( $opt ); //valid source and dest if (empty ( $source ) || ! is_array ( $source ) || ! isset ( $source [self::BUCKET] ) || ! isset ( $source [self::OBJECT] )) { throw new BCS_Exception ( '$source invalid, please check!', - 1 ); } if (empty ( $dest ) || ! is_array ( $dest ) || ! isset ( $dest [self::BUCKET] ) || ! isset ( $dest [self::OBJECT] ) || ! self::validate_bucket ( $dest [self::BUCKET] ) || ! self::validate_object ( $dest [self::OBJECT] )) { throw new BCS_Exception ( '$dest invalid, please check!', - 1 ); } $opt [self::BUCKET] = $dest [self::BUCKET]; $opt [self::OBJECT] = $dest [self::OBJECT]; $opt [self::METHOD] = 'PUT'; self::set_header_into_opt ( 'x-bs-copy-source', 'bs://' . $source [self::BUCKET] . $source [self::OBJECT], $opt ); if (isset ( $opt ['source_tag'] )) { self::set_header_into_opt ( 'x-bs-copy-source-tag', $opt ['source_tag'], $opt ); } $response = $this->authenticate ( $opt ); $this->log ( $response->isOK () ? "Copy object success!" : "Copy object failed! Response: [" . $response->body . "]", $opt ); return $response; } /** * 设置object的meta信息 * @param string $bucket (Required) * @param string $object (Required) * @param array $opt (Optional) * 目前支持的meta信息如下: * Content-Type * Cache-Control * Content-Disposition * Content-Encoding * Content-MD5 * Expires * @return BCS_ResponseCore */ public function set_object_meta($bucket, $object, $meta, $opt = array()) { $this->assertParameterArray ( $opt ); $this->assertParameterArray ( $meta ); $opt [self::BUCKET] = $bucket; $opt [self::OBJECT] = $object; $opt [self::METHOD] = 'PUT'; //利用copy_object接口来设置meta信息 $source = "bs://$bucket$object"; if (empty ( $meta )) { throw new BCS_Exception ( '$meta can not be empty! And $meta must be array.', - 1 ); } foreach ( $meta as $header => $value ) { self::set_header_into_opt ( $header, $value, $opt ); } $source = array ( self::BUCKET => $bucket, self::OBJECT => $object ); $response = $this->copy_object ( $source, $source, $opt ); $this->log ( $response->isOK () ? "Set object meta success!" : "Set object meta failed! Response: [" . $response->body . "]", $opt ); return $response; } /** * 获取object的acl * @param string $bucket (Required) * @param string $object (Required) * @param array $opt (Optional) * @throws BCS_Exception * @return BCS_ResponseCore */ public function get_object_acl($bucket, $object, $opt = array()) { $this->assertParameterArray ( $opt ); $opt [self::BUCKET] = $bucket; $opt [self::METHOD] = 'GET'; $opt [self::OBJECT] = $object; $opt [self::QUERY_STRING] = array ( self::ACL => 1 ); $response = $this->authenticate ( $opt ); $this->log ( $response->isOK () ? "Get object acl success!" : "Get object acl failed! Response: [" . $response->body . "]", $opt ); return $response; } /** * 设置object的acl,有三种模式, * (1).设置详细json格式的acl; * a. $acl 为json的array * b. $acl 为json的string * (2).通过acl_type字段进行设置 * a. $acl 为BaiduBCS::$ACL_ACTIONS中的字段 * @param string $bucket (Required) * @param string $object (Required) * @param string|array $acl (Required) * @param array $opt (Optional) * @return BCS_ResponseCore */ public function set_object_acl($bucket, $object, $acl, $opt = array()) { $this->assertParameterArray ( $opt ); //analyze acl $result = $this->analyze_user_acl ( $acl ); $opt = array_merge ( $opt, $result ); $opt [self::BUCKET] = $bucket; $opt [self::METHOD] = 'PUT'; $opt [self::OBJECT] = $object; $opt [self::QUERY_STRING] = array ( self::ACL => 1 ); $response = $this->authenticate ( $opt ); $this->log ( $response->isOK () ? "Set object acl success!" : "Set object acl failed! Response: [" . $response->body . "]", $opt ); return $response; } /** * 删除object * @param string $bucket (Required) * @param string $object (Required) * @param array $opt (Optional) * @throws BCS_Exception * @return BCS_ResponseCore */ public function delete_object($bucket, $object, $opt = array()) { $this->assertParameterArray ( $opt ); $opt [self::BUCKET] = $bucket; $opt [self::METHOD] = 'DELETE'; $opt [self::OBJECT] = $object; $response = $this->authenticate ( $opt ); $this->log ( $response->isOK () ? "Delete object success!" : "Delete object failed! Response: [" . $response->body . "]", $opt ); return $response; } /** * 判断object是否存在 * @param string $bucket (Required) * @param string $object (Required) * @param array $opt (Optional) * @throws BCS_Exception * @return boolean true|boolean false|BCS_ResponseCore * true:object存在 * false:不存在 * BCS_ResponseCore其他错误 */ public function is_object_exist($bucket, $object, $opt = array()) { $this->assertParameterArray ( $opt ); $opt [self::BUCKET] = $bucket; $opt [self::METHOD] = 'HEAD'; $opt [self::OBJECT] = $object; $response = $this->get_object_info ( $bucket, $object, $opt ); if ($response->isOK ()) { return true; } elseif ($response->status === 404) { return false; } return $response; } /** * 获取文件信息,发送的为HTTP HEAD请求,文件信息都在http response的header中,不会提取文件的内容 * @param string $bucket (Required) * @param string $object (Required) * @param array $opt (Optional) * @throws BCS_Exception * @return array BCS_ResponseCore */ public function get_object_info($bucket, $object, $opt = array()) { $this->assertParameterArray ( $opt ); $opt [self::BUCKET] = $bucket; $opt [self::METHOD] = 'HEAD'; $opt [self::OBJECT] = $object; $response = $this->authenticate ( $opt ); $this->log ( $response->isOK () ? "Get object info success!" : "Get object info failed! Response: [" . $response->body . "]", $opt ); return $response; } /** * 下载object * @param string $bucket (Required) * @param string $object (Required) * @param array $opt (Optional) * fileWriteTo (Optional)直接将请求结果写入该文件,如果fileWriteTo文件存在,sdk进行重命名再存储 * @throws BCS_Exception * @return BCS_ResponseCore */ public function get_object($bucket, $object, $opt = array()) { $this->assertParameterArray ( $opt ); //若fileWriteTo待写入的文件已经存在,需要进行重命名 if (isset ( $opt ["fileWriteTo"] ) && file_exists ( $opt ["fileWriteTo"] )) { $original_file_write_to = $opt ["fileWriteTo"]; $arr = explode ( DIRECTORY_SEPARATOR, $opt ["fileWriteTo"] ); $file_name = $arr [count ( $arr ) - 1]; $num = 1; while ( file_exists ( $opt ["fileWriteTo"] ) ) { $new_name_arr = explode ( ".", $file_name ); if (count ( $new_name_arr ) > 1) { $new_name_arr [count ( $new_name_arr ) - 2] .= " ($num)"; } else { $new_name_arr [0] .= " ($num)"; } $arr [count ( $arr ) - 1] = implode ( ".", $new_name_arr ); $opt ["fileWriteTo"] = implode ( DIRECTORY_SEPARATOR, $arr ); $num ++; } $this->log ( "[$original_file_write_to] already exist, rename it to [" . $opt ["fileWriteTo"] . "]", $opt ); } $opt [self::BUCKET] = $bucket; $opt [self::METHOD] = 'GET'; $opt [self::OBJECT] = $object; $response = $this->authenticate ( $opt ); $this->log ( $response->isOK () ? "Get object success!" : "Get object failed! Response: [" . $response->body . "]", $opt ); if (! $response->isOK () && isset ( $opt ["fileWriteTo"] )) { unlink ( $opt ["fileWriteTo"] ); } return $response; } /** * 生成签名链接 */ private function generate_user_url($method, $bucket, $object, $opt = array()) { $opt [self::AK] = $this->ak; $opt [self::SK] = $this->sk; $opt [self::BUCKET] = $bucket; $opt [self::METHOD] = $method; $opt [self::OBJECT] = $object; $opt [self::QUERY_STRING] = array (); if (isset ( $opt ["time"] )) { $opt [self::QUERY_STRING] ["time"] = $opt ["time"]; } if (isset ( $opt ["size"] )) { $opt [self::QUERY_STRING] ["size"] = $opt ["size"]; } return $this->format_url ( $opt ); } /** * 生成get_object的url * @param string $bucket (Required) * @param string $object (Required) * return false| string url */ public function generate_get_object_url($bucket, $object, $opt = array()) { $this->assertParameterArray ( $opt ); return $this->generate_user_url ( "GET", $bucket, $object, $opt ); } /** * 生成put_object的url * @param string $bucket (Required) * @param string $object (Required) * return false| string url */ public function generate_put_object_url($bucket, $object, $opt = array()) { $this->assertParameterArray ( $opt ); return $this->generate_user_url ( "PUT", $bucket, $object, $opt ); } /** * 生成post_object的url * @param string $bucket (Required) * @param string $object (Required) * return false| string url */ public function generate_post_object_url($bucket, $object, $opt = array()) { $this->assertParameterArray ( $opt ); return $this->generate_user_url ( "POST", $bucket, $object, $opt ); } /** * 生成delete_object的url * @param string $bucket (Required) * @param string $object (Required) * return false| string url */ public function generate_delete_object_url($bucket, $object, $opt = array()) { $this->assertParameterArray ( $opt ); return $this->generate_user_url ( "DELETE", $bucket, $object, $opt ); } /** * 生成head_object的url * @param string $bucket (Required) * @param string $object (Required) * return false| string url */ public function generate_head_object_url($bucket, $object, $opt = array()) { $this->assertParameterArray ( $opt ); return $this->generate_user_url ( "HEAD", $bucket, $object, $opt ); } /** * @return the $use_ssl */ public function getUse_ssl() { return $this->use_ssl; } /** * @param boolean $use_ssl */ public function setUse_ssl($use_ssl) { $this->use_ssl = $use_ssl; } /** * 校验bucket是否合法,bucket规范 * 1. 由小写字母,数字和横线'-'组成,长度为6~63位 * 2. 不能以数字作为Bucket开头 * 3. 不能以'-'作为Bucket的开头或者结尾 * @param string $bucket * @return boolean */ public static function validate_bucket($bucket) { //bucket 正则 $pattern1 = '/^[a-z][-a-z0-9]{4,61}[a-z0-9]$/'; if (! preg_match ( $pattern1, $bucket )) { return false; } return true; } /** * 校验object是否合法,object命名规范 * 1. object必须以'/'开头 * @param string $object * @return boolean */ public static function validate_object($object) { $pattern = '/^\//'; if (empty ( $object ) || ! preg_match ( $pattern, $object )) { return false; } return true; } /** * 将常用set http-header的动作抽离出来 * @param string $header * @param string $value * @param array $opt * @throws BCS_Exception * @return void */ private static function set_header_into_opt($header, $value, &$opt) { if (isset ( $opt [self::HEADERS] )) { if (! is_array ( $opt [self::HEADERS] )) { trigger_error ( 'Invalid $opt[\'headers\'], please check.' ); throw new BCS_Exception ( 'Invalid $opt[\'headers\'], please check.', - 1 ); } } else { $opt [self::HEADERS] = array (); } $opt [self::HEADERS] [$header] = $value; } /** * 使用特定function对数组中所有元素做处理 * @param string &$array 要处理的字符串 * @param string $function 要执行的函数 * @param boolean $apply_to_keys_also 是否也应用到key上 */ private static function array_recursive(&$array, $function, $apply_to_keys_also = false) { foreach ( $array as $key => $value ) { if (is_array ( $value )) { self::array_recursive ( $array [$key], $function, $apply_to_keys_also ); } else { $array [$key] = $function ( $value ); } if ($apply_to_keys_also && is_string ( $key )) { $new_key = $function ( $key ); if ($new_key != $key) { $array [$new_key] = $array [$key]; unset ( $array [$key] ); } } } } /** * 由数组构造json字符串,增加了一些特殊处理以支持特殊字符和不同编码的中文 * @param array $array */ private static function array_to_json($array) { if (! is_array ( $array )) { throw new BCS_Exception ( "Param must be array in function array_to_json()", - 1 ); } self::array_recursive ( $array, 'addslashes', false ); self::array_recursive ( $array, 'rawurlencode', false ); return rawurldecode ( json_encode ( $array ) ); } /** * 根据用户传入的acl,进行相应的处理 * (1).设置详细json格式的acl; * a. $acl 为json的array * b. $acl 为json的string * (2).通过acl_type字段进行设置 * @param string|array $acl * @throws BCS_Exception * @return array */ private function analyze_user_acl($acl) { $result = array (); if (is_array ( $acl )) { //(1).a $result ['content'] = $this->check_user_acl ( $acl ); } else if (is_string ( $acl )) { if (in_array ( $acl, self::$ACL_TYPES )) { //(2).a $result ["headers"] = array ( "x-bs-acl" => $acl ); } else { //(1).b $result ['content'] = $acl; } } else { throw new BCS_Exception ( "Invalid acl.", - 1 ); } return $result; } /** * 生成签名 * @param array $opt * @return boolean|string */ private function format_signature($opt) { $flags = ""; $content = ''; if (! isset ( $opt [self::AK] ) || ! isset ( $opt [self::SK] )) { trigger_error ( 'ak or sk is not in the array when create factor!' ); return false; } if (isset ( $opt [self::BUCKET] ) && isset ( $opt [self::METHOD] ) && isset ( $opt [self::OBJECT] )) { $flags .= 'MBO'; $content .= "Method=" . $opt [self::METHOD] . "\n"; //method $content .= "Bucket=" . $opt [self::BUCKET] . "\n"; //bucket $content .= "Object=" . self::trimUrl ( $opt [self::OBJECT] ) . "\n"; //object } else { trigger_error ( 'bucket、method and object cann`t be NULL!' ); return false; } if (isset ( $opt ['ip'] )) { $flags .= 'I'; $content .= "Ip=" . $opt ['ip'] . "\n"; } if (isset ( $opt ['time'] )) { $flags .= 'T'; $content .= "Time=" . $opt ['time'] . "\n"; } if (isset ( $opt ['size'] )) { $flags .= 'S'; $content .= "Size=" . $opt ['size'] . "\n"; } $content = $flags . "\n" . $content; $sign = base64_encode ( hash_hmac ( 'sha1', $content, $opt [self::SK], true ) ); return 'sign=' . $flags . ':' . $opt [self::AK] . ':' . urlencode ( $sign ); } /** * 检查用户输入的acl array是否合法,并转为json * @param array $acl * @throws BCS_Exception * @return string acl-json */ private function check_user_acl($acl) { if (! is_array ( $acl )) { throw new BCS_Exception ( "Invalid acl array" ); } foreach ( $acl ['statements'] as $key => $statement ) { // user resource action effect must in statement if (! isset ( $statement ['user'] ) || ! isset ( $statement ['resource'] ) || ! isset ( $statement ['action'] ) || ! isset ( $statement ['effect'] )) { throw new BCS_Exception ( 'Param miss: format acl error, please check your param!' ); } if (! is_array ( $statement ['user'] ) || ! is_array ( $statement ['resource'] )) { throw new BCS_Exception ( 'Param error: user or resource must be array, please check your param!' ); } if (! is_array ( $statement ['action'] ) || ! count ( array_diff ( $statement ['action'], self::$ACL_ACTIONS ) ) == 0) { throw new BCS_Exception ( 'Param error: action, please check your param!' ); } if (! in_array ( $statement ['effect'], self::$ACL_EFFECTS )) { throw new BCS_Exception ( 'Param error: effect, please check your param!' ); } if (isset ( $statement ['time'] )) { if (! is_array ( $statement ['time'] )) { throw new BCS_Exception ( 'Param error: time, please check your param!' ); } } } return self::array_to_json ( $acl ); } /** * 构造url * @param array $opt * @return boolean|string */ private function format_url($opt) { $sign = $this->format_signature ( $opt ); if ($sign === false) { trigger_error ( "Format signature failed, please check!" ); return false; } $opt ['sign'] = $sign; $url = ""; $url .= $this->use_ssl ? 'https://' : 'http://'; $url .= $this->hostname; $url .= '/' . $opt [self::BUCKET]; if (isset ( $opt [self::OBJECT] ) && '/' !== $opt [self::OBJECT]) { $url .= "/" . rawurlencode ( $opt [self::OBJECT] ); } $url .= '?' . $sign; if (isset ( $opt [self::QUERY_STRING] )) { foreach ( $opt [self::QUERY_STRING] as $key => $value ) { $url .= '&' . $key . '=' . $value; } } return $url; } /** * 将url中 '//' 替换为 '/' * @param $url * @return string */ public static function trimUrl($url) { $result = str_replace ( "//", "/", $url ); while ( $result !== $url ) { $url = $result; $result = str_replace ( "//", "/", $url ); } return $result; } /** * 获取传入目录的文件列表 * @param string $dir 文件目录 * @return array 文件树 */ public static function get_filetree($dir, $file_prefix = "/*") { $tree = array (); foreach ( glob ( $dir . $file_prefix ) as $single ) { if (is_dir ( $single )) { $tree = array_merge ( $tree, self::get_filetree ( $single ) ); } else { $tree [] = $single; } } return $tree; } /** * 内置的日志函数,可以根据用户传入的log函数,进行日志输出 * @param string $log * @param array $opt */ public function log($log, $opt) { if (isset ( $opt [self::IMPORT_BCS_LOG_METHOD] ) && function_exists ( $opt [self::IMPORT_BCS_LOG_METHOD] )) { $opt [self::IMPORT_BCS_LOG_METHOD] ( $log ); } else { trigger_error ( $log ); } } /** * make sure $opt is an array * @param $opt */ private function assertParameterArray($opt) { if (! is_array ( $opt )) { throw new BCS_Exception ( 'Parameter must be array, please check!', - 1 ); } } } ================================================ FILE: ThinkPHP/Library/Think/Upload/Driver/Bcs/mimetypes.class.php ================================================ 'video/3gpp', 'ai' => 'application/postscript', 'aif' => 'audio/x-aiff', 'aifc' => 'audio/x-aiff', 'aiff' => 'audio/x-aiff', 'asc' => 'text/plain', 'atom' => 'application/atom+xml', 'au' => 'audio/basic', 'avi' => 'video/x-msvideo', 'bcpio' => 'application/x-bcpio', 'bin' => 'application/octet-stream', 'bmp' => 'image/bmp', 'cdf' => 'application/x-netcdf', 'cgm' => 'image/cgm', 'class' => 'application/octet-stream', 'cpio' => 'application/x-cpio', 'cpt' => 'application/mac-compactpro', 'csh' => 'application/x-csh', 'css' => 'text/css', 'dcr' => 'application/x-director', 'dif' => 'video/x-dv', 'dir' => 'application/x-director', 'djv' => 'image/vnd.djvu', 'djvu' => 'image/vnd.djvu', 'dll' => 'application/octet-stream', 'dmg' => 'application/octet-stream', 'dms' => 'application/octet-stream', 'doc' => 'application/msword', 'dtd' => 'application/xml-dtd', 'dv' => 'video/x-dv', 'dvi' => 'application/x-dvi', 'dxr' => 'application/x-director', 'eps' => 'application/postscript', 'etx' => 'text/x-setext', 'exe' => 'application/octet-stream', 'ez' => 'application/andrew-inset', 'flv' => 'video/x-flv', 'gif' => 'image/gif', 'gram' => 'application/srgs', 'grxml' => 'application/srgs+xml', 'gtar' => 'application/x-gtar', 'gz' => 'application/x-gzip', 'hdf' => 'application/x-hdf', 'hqx' => 'application/mac-binhex40', 'htm' => 'text/html', 'html' => 'text/html', 'ice' => 'x-conference/x-cooltalk', 'ico' => 'image/x-icon', 'ics' => 'text/calendar', 'ief' => 'image/ief', 'ifb' => 'text/calendar', 'iges' => 'model/iges', 'igs' => 'model/iges', 'jnlp' => 'application/x-java-jnlp-file', 'jp2' => 'image/jp2', 'jpe' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'jpg' => 'image/jpeg', 'js' => 'application/x-javascript', 'kar' => 'audio/midi', 'latex' => 'application/x-latex', 'lha' => 'application/octet-stream', 'lzh' => 'application/octet-stream', 'm3u' => 'audio/x-mpegurl', 'm4a' => 'audio/mp4a-latm', 'm4p' => 'audio/mp4a-latm', 'm4u' => 'video/vnd.mpegurl', 'm4v' => 'video/x-m4v', 'mac' => 'image/x-macpaint', 'man' => 'application/x-troff-man', 'mathml' => 'application/mathml+xml', 'me' => 'application/x-troff-me', 'mesh' => 'model/mesh', 'mid' => 'audio/midi', 'midi' => 'audio/midi', 'mif' => 'application/vnd.mif', 'mov' => 'video/quicktime', 'movie' => 'video/x-sgi-movie', 'mp2' => 'audio/mpeg', 'mp3' => 'audio/mpeg', 'mp4' => 'video/mp4', 'mpe' => 'video/mpeg', 'mpeg' => 'video/mpeg', 'mpg' => 'video/mpeg', 'mpga' => 'audio/mpeg', 'ms' => 'application/x-troff-ms', 'msh' => 'model/mesh', 'mxu' => 'video/vnd.mpegurl', 'nc' => 'application/x-netcdf', 'oda' => 'application/oda', 'ogg' => 'application/ogg', 'ogv' => 'video/ogv', 'pbm' => 'image/x-portable-bitmap', 'pct' => 'image/pict', 'pdb' => 'chemical/x-pdb', 'pdf' => 'application/pdf', 'pgm' => 'image/x-portable-graymap', 'pgn' => 'application/x-chess-pgn', 'pic' => 'image/pict', 'pict' => 'image/pict', 'png' => 'image/png', 'pnm' => 'image/x-portable-anymap', 'pnt' => 'image/x-macpaint', 'pntg' => 'image/x-macpaint', 'ppm' => 'image/x-portable-pixmap', 'ppt' => 'application/vnd.ms-powerpoint', 'ps' => 'application/postscript', 'qt' => 'video/quicktime', 'qti' => 'image/x-quicktime', 'qtif' => 'image/x-quicktime', 'ra' => 'audio/x-pn-realaudio', 'ram' => 'audio/x-pn-realaudio', 'ras' => 'image/x-cmu-raster', 'rdf' => 'application/rdf+xml', 'rgb' => 'image/x-rgb', 'rm' => 'application/vnd.rn-realmedia', 'roff' => 'application/x-troff', 'rtf' => 'text/rtf', 'rtx' => 'text/richtext', 'sgm' => 'text/sgml', 'sgml' => 'text/sgml', 'sh' => 'application/x-sh', 'shar' => 'application/x-shar', 'silo' => 'model/mesh', 'sit' => 'application/x-stuffit', 'skd' => 'application/x-koan', 'skm' => 'application/x-koan', 'skp' => 'application/x-koan', 'skt' => 'application/x-koan', 'smi' => 'application/smil', 'smil' => 'application/smil', 'snd' => 'audio/basic', 'so' => 'application/octet-stream', 'spl' => 'application/x-futuresplash', 'src' => 'application/x-wais-source', 'sv4cpio' => 'application/x-sv4cpio', 'sv4crc' => 'application/x-sv4crc', 'svg' => 'image/svg+xml', 'swf' => 'application/x-shockwave-flash', 't' => 'application/x-troff', 'tar' => 'application/x-tar', 'tcl' => 'application/x-tcl', 'tex' => 'application/x-tex', 'texi' => 'application/x-texinfo', 'texinfo' => 'application/x-texinfo', 'tif' => 'image/tiff', 'tiff' => 'image/tiff', 'tr' => 'application/x-troff', 'tsv' => 'text/tab-separated-values', 'txt' => 'text/plain', 'ustar' => 'application/x-ustar', 'vcd' => 'application/x-cdlink', 'vrml' => 'model/vrml', 'vxml' => 'application/voicexml+xml', 'wav' => 'audio/x-wav', 'wbmp' => 'image/vnd.wap.wbmp', 'wbxml' => 'application/vnd.wap.wbxml', 'webm' => 'video/webm', 'wml' => 'text/vnd.wap.wml', 'wmlc' => 'application/vnd.wap.wmlc', 'wmls' => 'text/vnd.wap.wmlscript', 'wmlsc' => 'application/vnd.wap.wmlscriptc', 'wmv' => 'video/x-ms-wmv', 'wrl' => 'model/vrml', 'xbm' => 'image/x-xbitmap', 'xht' => 'application/xhtml+xml', 'xhtml' => 'application/xhtml+xml', 'xls' => 'application/vnd.ms-excel', 'xml' => 'application/xml', 'xpm' => 'image/x-xpixmap', 'xsl' => 'application/xml', 'xslt' => 'application/xslt+xml', 'xul' => 'application/vnd.mozilla.xul+xml', 'xwd' => 'image/x-xwindowdump', 'xyz' => 'chemical/x-xyz', 'zip' => 'application/zip', //add by zhengkan 20110905 "apk" => "application/vnd.android.package-archive", "bin" => "application/octet-stream", "cab" => "application/vnd.ms-cab-compressed", "gb" => "application/chinese-gb", "gba" => "application/octet-stream", "gbc" => "application/octet-stream", "jad" => "text/vnd.sun.j2me.app-descriptor", "jar" => "application/java-archive", "nes" => "application/octet-stream", "rar" => "application/x-rar-compressed", "sis" => "application/vnd.symbian.install", "sisx" => "x-epoc/x-sisx-app", "smc" => "application/octet-stream", "smd" => "application/octet-stream", "swf" => "application/x-shockwave-flash", "zip" => "application/x-zip-compressed", "wap" => "text/vnd.wap.wml wml", "mrp" => "application/mrp", //add by zhengkan 20110914 "wma" => "audio/x-ms-wma", "lrc" => "application/lrc" ); public static function get_mimetype($ext) { $ext = strtolower ( $ext ); return (isset ( self::$mime_types [$ext] ) ? self::$mime_types [$ext] : 'application/octet-stream'); } } ================================================ FILE: ThinkPHP/Library/Think/Upload/Driver/Bcs/requestcore.class.php ================================================ ). */ public $request_class = 'BCS_RequestCore'; /** * The default class to use for HTTP Responses (defaults to ). */ public $response_class = 'BCS_ResponseCore'; /** * Default useragent string to use. */ public $useragent = 'BCS_RequestCore/1.4.2'; /** * File to read from while streaming up. */ public $read_file = null; /** * The resource to read from while streaming up. */ public $read_stream = null; /** * The size of the stream to read from. */ public $read_stream_size = null; /** * The length already read from the stream. */ public $read_stream_read = 0; /** * File to write to while streaming down. */ public $write_file = null; /** * The resource to write to while streaming down. */ public $write_stream = null; /** * Stores the intended starting seek position. */ public $seek_position = null; /** * The user-defined callback function to call when a stream is read from. */ public $registered_streaming_read_callback = null; /** * The user-defined callback function to call when a stream is written to. */ public $registered_streaming_write_callback = null; /*%******************************************************************************************%*/ // CONSTANTS /** * GET HTTP Method */ const HTTP_GET = 'GET'; /** * POST HTTP Method */ const HTTP_POST = 'POST'; /** * PUT HTTP Method */ const HTTP_PUT = 'PUT'; /** * DELETE HTTP Method */ const HTTP_DELETE = 'DELETE'; /** * HEAD HTTP Method */ const HTTP_HEAD = 'HEAD'; /*%******************************************************************************************%*/ // CONSTRUCTOR/DESTRUCTOR /** * Constructs a new instance of this class. * * @param string $url (Optional) The URL to request or service endpoint to query. * @param string $proxy (Optional) The faux-url to use for proxy settings. Takes the following format: `proxy://user:pass@hostname:port` * @param array $helpers (Optional) An associative array of classnames to use for request, and response functionality. Gets passed in automatically by the calling class. * @return $this A reference to the current instance. */ public function __construct($url = null, $proxy = null, $helpers = null) { // Set some default values. $this->request_url = $url; $this->method = self::HTTP_GET; $this->request_headers = array (); $this->request_body = ''; // Set a new Request class if one was set. if (isset ( $helpers ['request'] ) && ! empty ( $helpers ['request'] )) { $this->request_class = $helpers ['request']; } // Set a new Request class if one was set. if (isset ( $helpers ['response'] ) && ! empty ( $helpers ['response'] )) { $this->response_class = $helpers ['response']; } if ($proxy) { $this->set_proxy ( $proxy ); } return $this; } /** * Destructs the instance. Closes opened file handles. * * @return $this A reference to the current instance. */ public function __destruct() { if (isset ( $this->read_file ) && isset ( $this->read_stream )) { fclose ( $this->read_stream ); } if (isset ( $this->write_file ) && isset ( $this->write_stream )) { fclose ( $this->write_stream ); } return $this; } /*%******************************************************************************************%*/ // REQUEST METHODS /** * Sets the credentials to use for authentication. * * @param string $user (Required) The username to authenticate with. * @param string $pass (Required) The password to authenticate with. * @return $this A reference to the current instance. */ public function set_credentials($user, $pass) { $this->username = $user; $this->password = $pass; return $this; } /** * Adds a custom HTTP header to the cURL request. * * @param string $key (Required) The custom HTTP header to set. * @param mixed $value (Required) The value to assign to the custom HTTP header. * @return $this A reference to the current instance. */ public function add_header($key, $value) { $this->request_headers [$key] = $value; return $this; } /** * Removes an HTTP header from the cURL request. * * @param string $key (Required) The custom HTTP header to set. * @return $this A reference to the current instance. */ public function remove_header($key) { if (isset ( $this->request_headers [$key] )) { unset ( $this->request_headers [$key] ); } return $this; } /** * Set the method type for the request. * * @param string $method (Required) One of the following constants: , , , , . * @return $this A reference to the current instance. */ public function set_method($method) { $this->method = strtoupper ( $method ); return $this; } /** * Sets a custom useragent string for the class. * * @param string $ua (Required) The useragent string to use. * @return $this A reference to the current instance. */ public function set_useragent($ua) { $this->useragent = $ua; return $this; } /** * Set the body to send in the request. * * @param string $body (Required) The textual content to send along in the body of the request. * @return $this A reference to the current instance. */ public function set_body($body) { $this->request_body = $body; return $this; } /** * Set the URL to make the request to. * * @param string $url (Required) The URL to make the request to. * @return $this A reference to the current instance. */ public function set_request_url($url) { $this->request_url = $url; return $this; } /** * Set additional CURLOPT settings. These will merge with the default settings, and override if * there is a duplicate. * * @param array $curlopts (Optional) A set of key-value pairs that set `CURLOPT` options. These will merge with the existing CURLOPTs, and ones passed here will override the defaults. Keys should be the `CURLOPT_*` constants, not strings. * @return $this A reference to the current instance. */ public function set_curlopts($curlopts) { $this->curlopts = $curlopts; return $this; } /** * Sets the length in bytes to read from the stream while streaming up. * * @param integer $size (Required) The length in bytes to read from the stream. * @return $this A reference to the current instance. */ public function set_read_stream_size($size) { $this->read_stream_size = $size; return $this; } /** * Sets the resource to read from while streaming up. Reads the stream from its current position until * EOF or `$size` bytes have been read. If `$size` is not given it will be determined by and * . * * @param resource $resource (Required) The readable resource to read from. * @param integer $size (Optional) The size of the stream to read. * @return $this A reference to the current instance. */ public function set_read_stream($resource, $size = null) { if (! isset ( $size ) || $size < 0) { $stats = fstat ( $resource ); if ($stats && $stats ['size'] >= 0) { $position = ftell ( $resource ); if ($position !== false && $position >= 0) { $size = $stats ['size'] - $position; } } } $this->read_stream = $resource; return $this->set_read_stream_size ( $size ); } /** * Sets the file to read from while streaming up. * * @param string $location (Required) The readable location to read from. * @return $this A reference to the current instance. */ public function set_read_file($location) { $this->read_file = $location; $read_file_handle = fopen ( $location, 'r' ); return $this->set_read_stream ( $read_file_handle ); } /** * Sets the resource to write to while streaming down. * * @param resource $resource (Required) The writeable resource to write to. * @return $this A reference to the current instance. */ public function set_write_stream($resource) { $this->write_stream = $resource; return $this; } /** * Sets the file to write to while streaming down. * * @param string $location (Required) The writeable location to write to. * @return $this A reference to the current instance. */ public function set_write_file($location) { $this->write_file = $location; $write_file_handle = fopen ( $location, 'w' ); return $this->set_write_stream ( $write_file_handle ); } /** * Set the proxy to use for making requests. * * @param string $proxy (Required) The faux-url to use for proxy settings. Takes the following format: `proxy://user:pass@hostname:port` * @return $this A reference to the current instance. */ public function set_proxy($proxy) { $proxy = parse_url ( $proxy ); $proxy ['user'] = isset ( $proxy ['user'] ) ? $proxy ['user'] : null; $proxy ['pass'] = isset ( $proxy ['pass'] ) ? $proxy ['pass'] : null; $proxy ['port'] = isset ( $proxy ['port'] ) ? $proxy ['port'] : null; $this->proxy = $proxy; return $this; } /** * Set the intended starting seek position. * * @param integer $position (Required) The byte-position of the stream to begin reading from. * @return $this A reference to the current instance. */ public function set_seek_position($position) { $this->seek_position = isset ( $position ) ? ( integer ) $position : null; return $this; } /** * Register a callback function to execute whenever a data stream is read from using * . * * The user-defined callback function should accept three arguments: * *
    *
  • $curl_handle - resource - Required - The cURL handle resource that represents the in-progress transfer.
  • *
  • $file_handle - resource - Required - The file handle resource that represents the file on the local file system.
  • *
  • $length - integer - Required - The length in kilobytes of the data chunk that was transferred.
  • *
* * @param string|array|function $callback (Required) The callback function is called by , so you can pass the following values:
    *
  • The name of a global function to execute, passed as a string.
  • *
  • A method to execute, passed as array('ClassName', 'MethodName').
  • *
  • An anonymous function (PHP 5.3+).
* @return $this A reference to the current instance. */ public function register_streaming_read_callback($callback) { $this->registered_streaming_read_callback = $callback; return $this; } /** * Register a callback function to execute whenever a data stream is written to using * . * * The user-defined callback function should accept two arguments: * *
    *
  • $curl_handle - resource - Required - The cURL handle resource that represents the in-progress transfer.
  • *
  • $length - integer - Required - The length in kilobytes of the data chunk that was transferred.
  • *
* * @param string|array|function $callback (Required) The callback function is called by , so you can pass the following values:
    *
  • The name of a global function to execute, passed as a string.
  • *
  • A method to execute, passed as array('ClassName', 'MethodName').
  • *
  • An anonymous function (PHP 5.3+).
* @return $this A reference to the current instance. */ public function register_streaming_write_callback($callback) { $this->registered_streaming_write_callback = $callback; return $this; } /*%******************************************************************************************%*/ // PREPARE, SEND, AND PROCESS REQUEST /** * A callback function that is invoked by cURL for streaming up. * * @param resource $curl_handle (Required) The cURL handle for the request. * @param resource $file_handle (Required) The open file handle resource. * @param integer $length (Required) The maximum number of bytes to read. * @return binary Binary data from a stream. */ public function streaming_read_callback($curl_handle, $file_handle, $length) { // Once we've sent as much as we're supposed to send... if ($this->read_stream_read >= $this->read_stream_size) { // Send EOF return ''; } // If we're at the beginning of an upload and need to seek... if ($this->read_stream_read == 0 && isset ( $this->seek_position ) && $this->seek_position !== ftell ( $this->read_stream )) { if (fseek ( $this->read_stream, $this->seek_position ) !== 0) { throw new BCS_RequestCore_Exception ( 'The stream does not support seeking and is either not at the requested position or the position is unknown.' ); } } $read = fread ( $this->read_stream, min ( $this->read_stream_size - $this->read_stream_read, $length ) ); // Remaining upload data or cURL's requested chunk size $this->read_stream_read += strlen ( $read ); $out = $read === false ? '' : $read; // Execute callback function if ($this->registered_streaming_read_callback) { call_user_func ( $this->registered_streaming_read_callback, $curl_handle, $file_handle, $out ); } return $out; } /** * A callback function that is invoked by cURL for streaming down. * * @param resource $curl_handle (Required) The cURL handle for the request. * @param binary $data (Required) The data to write. * @return integer The number of bytes written. */ public function streaming_write_callback($curl_handle, $data) { $length = strlen ( $data ); $written_total = 0; $written_last = 0; while ( $written_total < $length ) { $written_last = fwrite ( $this->write_stream, substr ( $data, $written_total ) ); if ($written_last === false) { return $written_total; } $written_total += $written_last; } // Execute callback function if ($this->registered_streaming_write_callback) { call_user_func ( $this->registered_streaming_write_callback, $curl_handle, $written_total ); } return $written_total; } /** * Prepares and adds the details of the cURL request. This can be passed along to a * function. * * @return resource The handle for the cURL object. */ public function prep_request() { $curl_handle = curl_init (); // Set default options. curl_setopt ( $curl_handle, CURLOPT_URL, $this->request_url ); curl_setopt ( $curl_handle, CURLOPT_FILETIME, true ); curl_setopt ( $curl_handle, CURLOPT_FRESH_CONNECT, false ); curl_setopt ( $curl_handle, CURLOPT_SSL_VERIFYPEER, false ); curl_setopt ( $curl_handle, CURLOPT_SSL_VERIFYHOST, true ); curl_setopt ( $curl_handle, CURLOPT_CLOSEPOLICY, CURLCLOSEPOLICY_LEAST_RECENTLY_USED ); curl_setopt ( $curl_handle, CURLOPT_MAXREDIRS, 5 ); curl_setopt ( $curl_handle, CURLOPT_HEADER, true ); curl_setopt ( $curl_handle, CURLOPT_RETURNTRANSFER, true ); curl_setopt ( $curl_handle, CURLOPT_TIMEOUT, 5184000 ); curl_setopt ( $curl_handle, CURLOPT_CONNECTTIMEOUT, 120 ); curl_setopt ( $curl_handle, CURLOPT_NOSIGNAL, true ); curl_setopt ( $curl_handle, CURLOPT_REFERER, $this->request_url ); curl_setopt ( $curl_handle, CURLOPT_USERAGENT, $this->useragent ); curl_setopt ( $curl_handle, CURLOPT_READFUNCTION, array ( $this, 'streaming_read_callback' ) ); if ($this->debug_mode) { curl_setopt ( $curl_handle, CURLOPT_VERBOSE, true ); } //if (! ini_get ( 'safe_mode' )) { //modify by zhengkan //curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true); //} // Enable a proxy connection if requested. if ($this->proxy) { curl_setopt ( $curl_handle, CURLOPT_HTTPPROXYTUNNEL, true ); $host = $this->proxy ['host']; $host .= ($this->proxy ['port']) ? ':' . $this->proxy ['port'] : ''; curl_setopt ( $curl_handle, CURLOPT_PROXY, $host ); if (isset ( $this->proxy ['user'] ) && isset ( $this->proxy ['pass'] )) { curl_setopt ( $curl_handle, CURLOPT_PROXYUSERPWD, $this->proxy ['user'] . ':' . $this->proxy ['pass'] ); } } // Set credentials for HTTP Basic/Digest Authentication. if ($this->username && $this->password) { curl_setopt ( $curl_handle, CURLOPT_HTTPAUTH, CURLAUTH_ANY ); curl_setopt ( $curl_handle, CURLOPT_USERPWD, $this->username . ':' . $this->password ); } // Handle the encoding if we can. if (extension_loaded ( 'zlib' )) { curl_setopt ( $curl_handle, CURLOPT_ENCODING, '' ); } // Process custom headers if (isset ( $this->request_headers ) && count ( $this->request_headers )) { $temp_headers = array (); foreach ( $this->request_headers as $k => $v ) { $temp_headers [] = $k . ': ' . $v; } curl_setopt ( $curl_handle, CURLOPT_HTTPHEADER, $temp_headers ); } switch ($this->method) { case self::HTTP_PUT : curl_setopt ( $curl_handle, CURLOPT_CUSTOMREQUEST, 'PUT' ); if (isset ( $this->read_stream )) { if (! isset ( $this->read_stream_size ) || $this->read_stream_size < 0) { throw new BCS_RequestCore_Exception ( 'The stream size for the streaming upload cannot be determined.' ); } curl_setopt ( $curl_handle, CURLOPT_INFILESIZE, $this->read_stream_size ); curl_setopt ( $curl_handle, CURLOPT_UPLOAD, true ); } else { curl_setopt ( $curl_handle, CURLOPT_POSTFIELDS, $this->request_body ); } break; case self::HTTP_POST : curl_setopt ( $curl_handle, CURLOPT_POST, true ); curl_setopt ( $curl_handle, CURLOPT_POSTFIELDS, $this->request_body ); break; case self::HTTP_HEAD : curl_setopt ( $curl_handle, CURLOPT_CUSTOMREQUEST, self::HTTP_HEAD ); curl_setopt ( $curl_handle, CURLOPT_NOBODY, 1 ); break; default : // Assumed GET curl_setopt ( $curl_handle, CURLOPT_CUSTOMREQUEST, $this->method ); if (isset ( $this->write_stream )) { curl_setopt ( $curl_handle, CURLOPT_WRITEFUNCTION, array ( $this, 'streaming_write_callback' ) ); curl_setopt ( $curl_handle, CURLOPT_HEADER, false ); } else { curl_setopt ( $curl_handle, CURLOPT_POSTFIELDS, $this->request_body ); } break; } // Merge in the CURLOPTs if (isset ( $this->curlopts ) && sizeof ( $this->curlopts ) > 0) { foreach ( $this->curlopts as $k => $v ) { curl_setopt ( $curl_handle, $k, $v ); } } return $curl_handle; } /** * is the environment BAE? * @return boolean the result of the answer */ private function isBaeEnv() { if (isset ( $_SERVER ['HTTP_HOST'] )) { $host = $_SERVER ['HTTP_HOST']; $pos = strpos ( $host, '.' ); if ($pos !== false) { $substr = substr ( $host, $pos + 1 ); if ($substr == 'duapp.com') { return true; } } } if (isset ( $_SERVER ["HTTP_BAE_LOGID"] )) { return true; } return false; } /** * Take the post-processed cURL data and break it down into useful header/body/info chunks. Uses the * data stored in the `curl_handle` and `response` properties unless replacement data is passed in via * parameters. * * @param resource $curl_handle (Optional) The reference to the already executed cURL request. * @param string $response (Optional) The actual response content itself that needs to be parsed. * @return BCS_ResponseCore A object containing a parsed HTTP response. */ public function process_response($curl_handle = null, $response = null) { // Accept a custom one if it's passed. if ($curl_handle && $response) { $this->curl_handle = $curl_handle; $this->response = $response; } // As long as this came back as a valid resource... if (is_resource ( $this->curl_handle )) { // Determine what's what. $header_size = curl_getinfo ( $this->curl_handle, CURLINFO_HEADER_SIZE ); $this->response_headers = substr ( $this->response, 0, $header_size ); $this->response_body = substr ( $this->response, $header_size ); $this->response_code = curl_getinfo ( $this->curl_handle, CURLINFO_HTTP_CODE ); $this->response_info = curl_getinfo ( $this->curl_handle ); // Parse out the headers $this->response_headers = explode ( "\r\n\r\n", trim ( $this->response_headers ) ); $this->response_headers = array_pop ( $this->response_headers ); $this->response_headers = explode ( "\r\n", $this->response_headers ); array_shift ( $this->response_headers ); // Loop through and split up the headers. $header_assoc = array (); foreach ( $this->response_headers as $header ) { $kv = explode ( ': ', $header ); //$header_assoc [strtolower ( $kv [0] )] = $kv [1]; $header_assoc [$kv [0]] = $kv [1]; } // Reset the headers to the appropriate property. $this->response_headers = $header_assoc; $this->response_headers ['_info'] = $this->response_info; $this->response_headers ['_info'] ['method'] = $this->method; if ($curl_handle && $response) { $class='\Think\Upload\Driver\Bcs\\'. $this->response_class; return new $class ( $this->response_headers, $this->response_body, $this->response_code, $this->curl_handle ); } } // Return false return false; } /** * Sends the request, calling necessary utility functions to update built-in properties. * * @param boolean $parse (Optional) Whether to parse the response with BCS_ResponseCore or not. * @return string The resulting unparsed data from the request. */ public function send_request($parse = false) { if (false === $this->isBaeEnv ()) { set_time_limit ( 0 ); } $curl_handle = $this->prep_request (); $this->response = curl_exec ( $curl_handle ); if ($this->response === false || ($this->method === self::HTTP_GET && curl_errno($curl_handle) === CURLE_PARTIAL_FILE)) { throw new BCS_RequestCore_Exception ( 'cURL resource: ' . ( string ) $curl_handle . '; cURL error: ' . curl_error ( $curl_handle ) . ' (' . curl_errno ( $curl_handle ) . ')' ); } $parsed_response = $this->process_response ( $curl_handle, $this->response ); curl_close ( $curl_handle ); if ($parse) { return $parsed_response; } return $this->response; } /** * Sends the request using , enabling parallel requests. Uses the "rolling" method. * * @param array $handles (Required) An indexed array of cURL handles to process simultaneously. * @param array $opt (Optional) An associative array of parameters that can have the following keys:
    *
  • callback - string|array - Optional - The string name of a function to pass the response data to. If this is a method, pass an array where the [0] index is the class and the [1] index is the method name.
  • *
  • limit - integer - Optional - The number of simultaneous requests to make. This can be useful for scaling around slow server responses. Defaults to trusting cURLs judgement as to how many to use.
* @return array Post-processed cURL responses. */ public function send_multi_request($handles, $opt = null) { if (false === $this->isBaeEnv ()) { set_time_limit ( 0 ); } // Skip everything if there are no handles to process. if (count ( $handles ) === 0) return array (); if (! $opt) $opt = array (); // Initialize any missing options $limit = isset ( $opt ['limit'] ) ? $opt ['limit'] : - 1; // Initialize $handle_list = $handles; $http = new $this->request_class (); $multi_handle = curl_multi_init (); $handles_post = array (); $added = count ( $handles ); $last_handle = null; $count = 0; $i = 0; // Loop through the cURL handles and add as many as it set by the limit parameter. while ( $i < $added ) { if ($limit > 0 && $i >= $limit) break; curl_multi_add_handle ( $multi_handle, array_shift ( $handles ) ); $i ++; } do { $active = false; // Start executing and wait for a response. while ( ($status = curl_multi_exec ( $multi_handle, $active )) === CURLM_CALL_MULTI_PERFORM ) { // Start looking for possible responses immediately when we have to add more handles if (count ( $handles ) > 0) break; } // Figure out which requests finished. $to_process = array (); while ( $done = curl_multi_info_read ( $multi_handle ) ) { // Since curl_errno() isn't reliable for handles that were in multirequests, we check the 'result' of the info read, which contains the curl error number, (listed here http://curl.haxx.se/libcurl/c/libcurl-errors.html ) if ($done ['result'] > 0) { throw new BCS_RequestCore_Exception ( 'cURL resource: ' . ( string ) $done ['handle'] . '; cURL error: ' . curl_error ( $done ['handle'] ) . ' (' . $done ['result'] . ')' ); } // Because curl_multi_info_read() might return more than one message about a request, we check to see if this request is already in our array of completed requests elseif (! isset ( $to_process [( int ) $done ['handle']] )) { $to_process [( int ) $done ['handle']] = $done; } } // Actually deal with the request foreach ( $to_process as $pkey => $done ) { $response = $http->process_response ( $done ['handle'], curl_multi_getcontent ( $done ['handle'] ) ); $key = array_search ( $done ['handle'], $handle_list, true ); $handles_post [$key] = $response; if (count ( $handles ) > 0) { curl_multi_add_handle ( $multi_handle, array_shift ( $handles ) ); } curl_multi_remove_handle ( $multi_handle, $done ['handle'] ); curl_close ( $done ['handle'] ); } } while ( $active || count ( $handles_post ) < $added ); curl_multi_close ( $multi_handle ); ksort ( $handles_post, SORT_NUMERIC ); return $handles_post; } /*%******************************************************************************************%*/ // RESPONSE METHODS /** * Get the HTTP response headers from the request. * * @param string $header (Optional) A specific header value to return. Defaults to all headers. * @return string|array All or selected header values. */ public function get_response_header($header = null) { if ($header) { // return $this->response_headers [strtolower ( $header )]; return $this->response_headers [$header]; } return $this->response_headers; } /** * Get the HTTP response body from the request. * * @return string The response body. */ public function get_response_body() { return $this->response_body; } /** * Get the HTTP response code from the request. * * @return string The HTTP response code. */ public function get_response_code() { return $this->response_code; } } /** * Container for all response-related methods. */ class BCS_ResponseCore { /** * Stores the HTTP header information. */ public $header; /** * Stores the SimpleXML response. */ public $body; /** * Stores the HTTP response code. */ public $status; /** * Constructs a new instance of this class. * * @param array $header (Required) Associative array of HTTP headers (typically returned by ). * @param string $body (Required) XML-formatted response from AWS. * @param integer $status (Optional) HTTP response status code from the request. * @return object Contains an `header` property (HTTP headers as an associative array), a or `body` property, and an `status` code. */ public function __construct($header, $body, $status = null) { $this->header = $header; $this->body = $body; $this->status = $status; return $this; } /** * Did we receive the status code we expected? * * @param integer|array $codes (Optional) The status code(s) to expect. Pass an for a single acceptable value, or an of integers for multiple acceptable values. * @return boolean Whether we received the expected status code or not. */ public function isOK($codes = array(200, 201, 204, 206)) { if (is_array ( $codes )) { return in_array ( $this->status, $codes ); } return $this->status === $codes; } } /** * Default BCS_RequestCore Exception. */ class BCS_RequestCore_Exception extends \Exception { } ================================================ FILE: ThinkPHP/Library/Think/Upload/Driver/Bcs.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Upload\Driver; use Think\Upload\Driver\Bcs\BaiduBcs; class Bcs { /** * 上传文件根目录 * @var string */ private $rootPath; const DEFAULT_URL = 'bcs.duapp.com'; /** * 上传错误信息 * @var string */ private $error = ''; public $config = array( 'AccessKey'=> '', 'SecretKey'=> '', //百度云服务器 'bucket' => '', //空间名称 'rename' => false, 'timeout' => 3600, //超时时间 ); public $bcs = null; /** * 构造函数,用于设置上传根路径 * @param array $config FTP配置 */ public function __construct($config){ /* 默认FTP配置 */ $this->config = array_merge($this->config, $config); $bcsClass = dirname(__FILE__). "/Bcs/bcs.class.php"; if(is_file($bcsClass)) require_once($bcsClass); $this->bcs = new BaiduBCS ( $this->config['AccessKey'], $this->config['SecretKey'], self:: DEFAULT_URL ); } /** * 检测上传根目录(百度云上传时支持自动创建目录,直接返回) * @param string $rootpath 根目录 * @return boolean true-检测通过,false-检测失败 */ public function checkRootPath($rootpath){ /* 设置根目录 */ $this->rootPath = str_replace('./', '/', $rootpath); return true; } /** * 检测上传目录(百度云上传时支持自动创建目录,直接返回) * @param string $savepath 上传目录 * @return boolean 检测结果,true-通过,false-失败 */ public function checkSavePath($savepath){ return true; } /** * 创建文件夹 (百度云上传时支持自动创建目录,直接返回) * @param string $savepath 目录名称 * @return boolean true-创建成功,false-创建失败 */ public function mkdir($savepath){ return true; } /** * 保存指定文件 * @param array $file 保存的文件信息 * @param boolean $replace 同名文件是否覆盖 * @return boolean 保存状态,true-成功,false-失败 */ public function save(&$file,$replace=true) { $opt = array (); $opt ['acl'] = BaiduBCS::BCS_SDK_ACL_TYPE_PUBLIC_WRITE; $opt ['curlopts'] = array ( CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_TIMEOUT => 1800 ); $object = "/{$file['savepath']}{$file['savename']}"; $response = $this->bcs->create_object ( $this->config['bucket'], $object, $file['tmp_name'], $opt ); $url = $this->download($object); $file['url'] = $url; return $response->isOK() ? true : false; } public function download($file){ $file = str_replace('./', '/', $file); $opt = array(); $opt['time'] = mktime('2049-12-31'); //这是最长有效时间!-- $response = $this->bcs->generate_get_object_url ( $this->config['bucket'], $file, $opt ); return $response; } /** * 获取最后一次上传错误信息 * @return string 错误信息 */ public function getError(){ return $this->error; } /** * 请求百度云服务器 * @param string $path 请求的PATH * @param string $method 请求方法 * @param array $headers 请求header * @param resource $body 上传文件资源 * @return boolean */ private function request($path, $method, $headers = null, $body = null){ $ch = curl_init($path); $_headers = array('Expect:'); if (!is_null($headers) && is_array($headers)){ foreach($headers as $k => $v) { array_push($_headers, "{$k}: {$v}"); } } $length = 0; $date = gmdate('D, d M Y H:i:s \G\M\T'); if (!is_null($body)) { if(is_resource($body)){ fseek($body, 0, SEEK_END); $length = ftell($body); fseek($body, 0); array_push($_headers, "Content-Length: {$length}"); curl_setopt($ch, CURLOPT_INFILE, $body); curl_setopt($ch, CURLOPT_INFILESIZE, $length); } else { $length = @strlen($body); array_push($_headers, "Content-Length: {$length}"); curl_setopt($ch, CURLOPT_POSTFIELDS, $body); } } else { array_push($_headers, "Content-Length: {$length}"); } // array_push($_headers, 'Authorization: ' . $this->sign($method, $uri, $date, $length)); array_push($_headers, "Date: {$date}"); curl_setopt($ch, CURLOPT_HTTPHEADER, $_headers); curl_setopt($ch, CURLOPT_TIMEOUT, $this->config['timeout']); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); if ($method == 'PUT' || $method == 'POST') { curl_setopt($ch, CURLOPT_POST, 1); } else { curl_setopt($ch, CURLOPT_POST, 0); } if ($method == 'HEAD') { curl_setopt($ch, CURLOPT_NOBODY, true); } $response = curl_exec($ch); $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); list($header, $body) = explode("\r\n\r\n", $response, 2); if ($status == 200) { if ($method == 'GET') { return $body; } else { $data = $this->response($header); return count($data) > 0 ? $data : true; } } else { $this->error($header); return false; } } /** * 获取响应数据 * @param string $text 响应头字符串 * @return array 响应数据列表 */ private function response($text){ $items = json_decode($text, true); return $items; } /** * 生成请求签名 * @return string 请求签名 */ private function sign($method, $Bucket, $object='/', $size=''){ if(!$size) $size = $this->config['size']; $param = array( 'ak'=>$this->config['AccessKey'], 'sk'=>$this->config['SecretKey'], 'size'=>$size, 'bucket'=>$Bucket, 'host'=>self :: DEFAULT_URL, 'date'=>time()+$this->config['timeout'], 'ip'=>'', 'object'=>$object ); $response = $this->request($this->apiurl.'?'.http_build_query($param), 'POST'); if($response) $response = json_decode($response, true); return $response['content'][$method]; } /** * 获取请求错误信息 * @param string $header 请求返回头信息 */ private function error($header) { list($status, $stash) = explode("\r\n", $header, 2); list($v, $code, $message) = explode(" ", $status, 3); $message = is_null($message) ? 'File Not Found' : "[{$status}]:{$message}"; $this->error = $message; } } ================================================ FILE: ThinkPHP/Library/Think/Upload/Driver/Ftp.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Upload\Driver; class Ftp { /** * 上传文件根目录 * @var string */ private $rootPath; /** * 本地上传错误信息 * @var string */ private $error = ''; //上传错误信息 /** * FTP连接 * @var resource */ private $link; private $config = array( 'host' => '', //服务器 'port' => 21, //端口 'timeout' => 90, //超时时间 'username' => '', //用户名 'password' => '', //密码 ); /** * 构造函数,用于设置上传根路径 * @param array $config FTP配置 */ public function __construct($config){ /* 默认FTP配置 */ $this->config = array_merge($this->config, $config); /* 登录FTP服务器 */ if(!$this->login()){ E($this->error); } } /** * 检测上传根目录 * @param string $rootpath 根目录 * @return boolean true-检测通过,false-检测失败 */ public function checkRootPath($rootpath){ /* 设置根目录 */ $this->rootPath = ftp_pwd($this->link) . '/' . ltrim($rootpath, '/'); if(!@ftp_chdir($this->link, $this->rootPath)){ $this->error = '上传根目录不存在!'; return false; } return true; } /** * 检测上传目录 * @param string $savepath 上传目录 * @return boolean 检测结果,true-通过,false-失败 */ public function checkSavePath($savepath){ /* 检测并创建目录 */ if (!$this->mkdir($savepath)) { return false; } else { //TODO:检测目录是否可写 return true; } } /** * 保存指定文件 * @param array $file 保存的文件信息 * @param boolean $replace 同名文件是否覆盖 * @return boolean 保存状态,true-成功,false-失败 */ public function save($file, $replace=true) { $filename = $this->rootPath . $file['savepath'] . $file['savename']; /* 不覆盖同名文件 */ // if (!$replace && is_file($filename)) { // $this->error = '存在同名文件' . $file['savename']; // return false; // } /* 移动文件 */ if (!ftp_put($this->link, $filename, $file['tmp_name'], FTP_BINARY)) { $this->error = '文件上传保存错误!'; return false; } return true; } /** * 创建目录 * @param string $savepath 要创建的目录 * @return boolean 创建状态,true-成功,false-失败 */ public function mkdir($savepath){ $dir = $this->rootPath . $savepath; if(ftp_chdir($this->link, $dir)){ return true; } if(ftp_mkdir($this->link, $dir)){ return true; } elseif($this->mkdir(dirname($savepath)) && ftp_mkdir($this->link, $dir)) { return true; } else { $this->error = "目录 {$savepath} 创建失败!"; return false; } } /** * 获取最后一次上传错误信息 * @return string 错误信息 */ public function getError(){ return $this->error; } /** * 登录到FTP服务器 * @return boolean true-登录成功,false-登录失败 */ private function login(){ extract($this->config); $this->link = ftp_connect($host, $port, $timeout); if($this->link) { if (ftp_login($this->link, $username, $password)) { return true; } else { $this->error = "无法登录到FTP服务器:username - {$username}"; } } else { $this->error = "无法连接到FTP服务器:{$host}"; } return false; } /** * 析构方法,用于断开当前FTP连接 */ public function __destruct() { ftp_close($this->link); } } ================================================ FILE: ThinkPHP/Library/Think/Upload/Driver/Local.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Upload\Driver; class Local{ /** * 上传文件根目录 * @var string */ private $rootPath; /** * 本地上传错误信息 * @var string */ private $error = ''; //上传错误信息 /** * 构造函数,用于设置上传根路径 */ public function __construct($config = null){ } /** * 检测上传根目录 * @param string $rootpath 根目录 * @return boolean true-检测通过,false-检测失败 */ public function checkRootPath($rootpath){ if(!(is_dir($rootpath) && is_writable($rootpath))){ $this->error = '上传根目录不存在!请尝试手动创建:'.$rootpath; return false; } $this->rootPath = $rootpath; return true; } /** * 检测上传目录 * @param string $savepath 上传目录 * @return boolean 检测结果,true-通过,false-失败 */ public function checkSavePath($savepath){ /* 检测并创建目录 */ if (!$this->mkdir($savepath)) { return false; } else { /* 检测目录是否可写 */ if (!is_writable($this->rootPath . $savepath)) { $this->error = '上传目录 ' . $savepath . ' 不可写!'; return false; } else { return true; } } } /** * 保存指定文件 * @param array $file 保存的文件信息 * @param boolean $replace 同名文件是否覆盖 * @return boolean 保存状态,true-成功,false-失败 */ public function save($file, $replace=true) { $filename = $this->rootPath . $file['savepath'] . $file['savename']; /* 不覆盖同名文件 */ if (!$replace && is_file($filename)) { $this->error = '存在同名文件' . $file['savename']; return false; } /* 移动文件 */ if (!move_uploaded_file($file['tmp_name'], $filename)) { $this->error = '文件上传保存错误!'; return false; } return true; } /** * 创建目录 * @param string $savepath 要创建的穆里 * @return boolean 创建状态,true-成功,false-失败 */ public function mkdir($savepath){ $dir = $this->rootPath . $savepath; if(is_dir($dir)){ return true; } if(mkdir($dir, 0777, true)){ return true; } else { $this->error = "目录 {$savepath} 创建失败!"; return false; } } /** * 获取最后一次上传错误信息 * @return string 错误信息 */ public function getError(){ return $this->error; } } ================================================ FILE: ThinkPHP/Library/Think/Upload/Driver/Qiniu/QiniuStorage.class.php ================================================ sk = $config['secretKey']; $this->ak = $config['accessKey']; $this->domain = $config['domain']; $this->bucket = $config['bucket']; $this->timeout = isset($config['timeout'])? $config['timeout'] : 3600; } static function sign($sk, $ak, $data){ $sign = hash_hmac('sha1', $data, $sk, true); return $ak . ':' . self::Qiniu_Encode($sign); } static function signWithData($sk, $ak, $data){ $data = self::Qiniu_Encode($data); return self::sign($sk, $ak, $data) . ':' . $data; } public function accessToken($url, $body=''){ $parsed_url = parse_url($url); $path = $parsed_url['path']; $access = $path; if (isset($parsed_url['query'])) { $access .= "?" . $parsed_url['query']; } $access .= "\n"; if($body){ $access .= $body; } return self::sign($this->sk, $this->ak, $access); } public function UploadToken($sk ,$ak ,$param){ $param['deadline'] = $param['Expires'] == 0? 3600: $param['Expires']; $param['deadline'] += time(); $data = array('scope'=> $this->bucket, 'deadline'=>$param['deadline']); if (!empty($param['CallbackUrl'])) { $data['callbackUrl'] = $param['CallbackUrl']; } if (!empty($param['CallbackBody'])) { $data['callbackBody'] = $param['CallbackBody']; } if (!empty($param['ReturnUrl'])) { $data['returnUrl'] = $param['ReturnUrl']; } if (!empty($param['ReturnBody'])) { $data['returnBody'] = $param['ReturnBody']; } if (!empty($param['AsyncOps'])) { $data['asyncOps'] = $param['AsyncOps']; } if (!empty($param['EndUser'])) { $data['endUser'] = $param['EndUser']; } $data = json_encode($data); return self::SignWithData($sk, $ak, $data); } public function upload($config, $file){ $uploadToken = $this->UploadToken($this->sk, $this->ak, $config); $url = "{$this->QINIU_UP_HOST}"; $mimeBoundary = md5(microtime()); $header = array('Content-Type'=>'multipart/form-data;boundary='.$mimeBoundary); $data = array(); $fields = array( 'token' => $uploadToken, 'key' => $config['saveName']? : $file['fileName'], ); if(is_array($config['custom_fields']) && $config['custom_fields'] !== array()){ $fields = array_merge($fields, $config['custom_fields']); } foreach ($fields as $name => $val) { array_push($data, '--' . $mimeBoundary); array_push($data, "Content-Disposition: form-data; name=\"$name\""); array_push($data, ''); array_push($data, $val); } //文件 array_push($data, '--' . $mimeBoundary); $name = $file['name']; $fileName = $file['fileName']; $fileBody = $file['fileBody']; $fileName = self::Qiniu_escapeQuotes($fileName); array_push($data, "Content-Disposition: form-data; name=\"$name\"; filename=\"$fileName\""); array_push($data, 'Content-Type: application/octet-stream'); array_push($data, ''); array_push($data, $fileBody); array_push($data, '--' . $mimeBoundary . '--'); array_push($data, ''); $body = implode("\r\n", $data); $response = $this->request($url, 'POST', $header, $body); return $response; } public function dealWithType($key, $type){ $param = $this->buildUrlParam(); $url = ''; switch($type){ case 'img': $url = $this->downLink($key); if($param['imageInfo']){ $url .= '?imageInfo'; }else if($param['exif']){ $url .= '?exif'; }else if($param['imageView']){ $url .= '?imageView/'.$param['mode']; if($param['w']) $url .= "/w/{$param['w']}"; if($param['h']) $url .= "/h/{$param['h']}"; if($param['q']) $url .= "/q/{$param['q']}"; if($param['format']) $url .= "/format/{$param['format']}"; } break; case 'video': //TODO 视频处理 case 'doc': $url = $this->downLink($key); $url .= '?md2html'; if(isset($param['mode'])) $url .= '/'.(int)$param['mode']; if($param['cssurl']) $url .= '/'. self::Qiniu_Encode($param['cssurl']); break; } return $url; } public function buildUrlParam(){ return $_REQUEST; } //获取某个路径下的文件列表 public function getList($query = array(), $path = ''){ $query = array_merge(array('bucket'=>$this->bucket), $query); $url = "{$this->QINIU_RSF_HOST}/list?".http_build_query($query); $accessToken = $this->accessToken($url); $response = $this->request($url, 'POST', array('Authorization'=>"QBox $accessToken")); return $response; } //获取某个文件的信息 public function info($key){ $key = trim($key); $url = "{$this->QINIU_RS_HOST}/stat/" . self::Qiniu_Encode("{$this->bucket}:{$key}"); $accessToken = $this->accessToken($url); $response = $this->request($url, 'POST', array( 'Authorization' => "QBox $accessToken", )); return $response; } //获取文件下载资源链接 public function downLink($key){ $key = urlencode($key); $key = self::Qiniu_escapeQuotes($key); $url = "http://{$this->domain}/{$key}"; return $url; } //重命名单个文件 public function rename($file, $new_file){ $key = trim($file); $url = "{$this->QINIU_RS_HOST}/move/" . self::Qiniu_Encode("{$this->bucket}:{$key}") .'/'. self::Qiniu_Encode("{$this->bucket}:{$new_file}"); trace($url); $accessToken = $this->accessToken($url); $response = $this->request($url, 'POST', array('Authorization'=>"QBox $accessToken")); return $response; } //删除单个文件 public function del($file){ $key = trim($file); $url = "{$this->QINIU_RS_HOST}/delete/" . self::Qiniu_Encode("{$this->bucket}:{$key}"); $accessToken = $this->accessToken($url); $response = $this->request($url, 'POST', array('Authorization'=>"QBox $accessToken")); return $response; } //批量删除文件 public function delBatch($files){ $url = $this->QINIU_RS_HOST . '/batch'; $ops = array(); foreach ($files as $file) { $ops[] = "/delete/". self::Qiniu_Encode("{$this->bucket}:{$file}"); } $params = 'op=' . implode('&op=', $ops); $url .= '?'.$params; trace($url); $accessToken = $this->accessToken($url); $response = $this->request($url, 'POST', array('Authorization'=>"QBox $accessToken")); return $response; } static function Qiniu_Encode($str) {// URLSafeBase64Encode $find = array('+', '/'); $replace = array('-', '_'); return str_replace($find, $replace, base64_encode($str)); } static function Qiniu_escapeQuotes($str){ $find = array("\\", "\""); $replace = array("\\\\", "\\\""); return str_replace($find, $replace, $str); } /** * 请求云服务器 * @param string $path 请求的PATH * @param string $method 请求方法 * @param array $headers 请求header * @param resource $body 上传文件资源 * @return boolean */ private function request($path, $method, $headers = null, $body = null){ $ch = curl_init($path); $_headers = array('Expect:'); if (!is_null($headers) && is_array($headers)){ foreach($headers as $k => $v) { array_push($_headers, "{$k}: {$v}"); } } $length = 0; $date = gmdate('D, d M Y H:i:s \G\M\T'); if (!is_null($body)) { if(is_resource($body)){ fseek($body, 0, SEEK_END); $length = ftell($body); fseek($body, 0); array_push($_headers, "Content-Length: {$length}"); curl_setopt($ch, CURLOPT_INFILE, $body); curl_setopt($ch, CURLOPT_INFILESIZE, $length); } else { $length = @strlen($body); array_push($_headers, "Content-Length: {$length}"); curl_setopt($ch, CURLOPT_POSTFIELDS, $body); } } else { array_push($_headers, "Content-Length: {$length}"); } // array_push($_headers, 'Authorization: ' . $this->sign($method, $uri, $date, $length)); array_push($_headers, "Date: {$date}"); curl_setopt($ch, CURLOPT_HTTPHEADER, $_headers); curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); if ($method == 'PUT' || $method == 'POST') { curl_setopt($ch, CURLOPT_POST, 1); } else { curl_setopt($ch, CURLOPT_POST, 0); } if ($method == 'HEAD') { curl_setopt($ch, CURLOPT_NOBODY, true); } $response = curl_exec($ch); $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); list($header, $body) = explode("\r\n\r\n", $response, 2); if ($status == 200) { if ($method == 'GET') { return $body; } else { return $this->response($response); } } else { $this->error($header , $body); return false; } } /** * 获取响应数据 * @param string $text 响应头字符串 * @return array 响应数据列表 */ private function response($text){ $headers = explode(PHP_EOL, $text); $items = array(); foreach($headers as $header) { $header = trim($header); if(strpos($header, '{') !== False){ $items = json_decode($header, 1); break; } } return $items; } /** * 获取请求错误信息 * @param string $header 请求返回头信息 */ private function error($header, $body) { list($status, $stash) = explode("\r\n", $header, 2); list($v, $code, $message) = explode(" ", $status, 3); $message = is_null($message) ? 'File Not Found' : "[{$status}]:{$message}]"; $this->error = $message; $this->errorStr = json_decode($body ,1); $this->errorStr = $this->errorStr['error']; } } ================================================ FILE: ThinkPHP/Library/Think/Upload/Driver/Qiniu.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Upload\Driver; use Think\Upload\Driver\Qiniu\QiniuStorage; class Qiniu{ /** * 上传文件根目录 * @var string */ private $rootPath; /** * 上传错误信息 * @var string */ private $error = ''; private $config = array( 'secretKey' => '', //七牛服务器 'accessKey' => '', //七牛用户 'domain' => '', //七牛密码 'bucket' => '', //空间名称 'timeout' => 300, //超时时间 ); /** * 构造函数,用于设置上传根路径 * @param array $config FTP配置 */ public function __construct($config){ $this->config = array_merge($this->config, $config); /* 设置根目录 */ $this->qiniu = new QiniuStorage($config); } /** * 检测上传根目录(七牛上传时支持自动创建目录,直接返回) * @param string $rootpath 根目录 * @return boolean true-检测通过,false-检测失败 */ public function checkRootPath($rootpath){ $this->rootPath = trim($rootpath, './') . '/'; return true; } /** * 检测上传目录(七牛上传时支持自动创建目录,直接返回) * @param string $savepath 上传目录 * @return boolean 检测结果,true-通过,false-失败 */ public function checkSavePath($savepath){ return true; } /** * 创建文件夹 (七牛上传时支持自动创建目录,直接返回) * @param string $savepath 目录名称 * @return boolean true-创建成功,false-创建失败 */ public function mkdir($savepath){ return true; } /** * 保存指定文件 * @param array $file 保存的文件信息 * @param boolean $replace 同名文件是否覆盖 * @return boolean 保存状态,true-成功,false-失败 */ public function save(&$file,$replace=true) { $file['name'] = $file['savepath'] . $file['savename']; $key = str_replace('/', '_', $file['name']); $upfile = array( 'name'=>'file', 'fileName'=>$key, 'fileBody'=>file_get_contents($file['tmp_name']) ); $config = array(); $result = $this->qiniu->upload($config, $upfile); $url = $this->qiniu->downlink($key); $file['url'] = $url; return false ===$result ? false : true; } /** * 获取最后一次上传错误信息 * @return string 错误信息 */ public function getError(){ return $this->qiniu->errorStr; } } ================================================ FILE: ThinkPHP/Library/Think/Upload/Driver/Sae.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Upload\Driver; class Sae{ /** * Storage的Domain * @var string */ private $domain = ''; private $rootPath = ''; /** * 本地上传错误信息 * @var string */ private $error = ''; /** * 构造函数,设置storage的domain, 如果有传配置,则domain为配置项,如果没有传domain为第一个路径的目录名称。 * @param mixed $config 上传配置 */ public function __construct($config = null){ if(is_array($config) && !empty($config['domain'])){ $this->domain = strtolower($config['domain']); } } /** * 检测上传根目录 * @param string $rootpath 根目录 * @return boolean true-检测通过,false-检测失败 */ public function checkRootPath($rootpath){ $rootpath = trim($rootpath,'./'); if(!$this->domain){ $rootpath = explode('/', $rootpath); $this->domain = strtolower(array_shift($rootpath)); $rootpath = implode('/', $rootpath); } $this->rootPath = $rootpath; $st = new \SaeStorage(); if(false===$st->getDomainCapacity($this->domain)){ $this->error = '您好像没有建立Storage的domain['.$this->domain.']'; return false; } return true; } /** * 检测上传目录 * @param string $savepath 上传目录 * @return boolean 检测结果,true-通过,false-失败 */ public function checkSavePath($savepath){ return true; } /** * 保存指定文件 * @param array $file 保存的文件信息 * @param boolean $replace 同名文件是否覆盖 * @return boolean 保存状态,true-成功,false-失败 */ public function save(&$file, $replace=true) { $filename = ltrim($this->rootPath .'/'. $file['savepath'] . $file['savename'],'/'); $st = new \SaeStorage(); /* 不覆盖同名文件 */ if (!$replace && $st->fileExists($this->domain,$filename)) { $this->error = '存在同名文件' . $file['savename']; return false; } /* 移动文件 */ if (!$st->upload($this->domain,$filename,$file['tmp_name'])) { $this->error = '文件上传保存错误!['.$st->errno().']:'.$st->errmsg(); return false; }else{ $file['url'] = $st->getUrl($this->domain, $filename); } return true; } public function mkdir(){ return true; } /** * 获取最后一次上传错误信息 * @return string 错误信息 */ public function getError(){ return $this->error; } } ================================================ FILE: ThinkPHP/Library/Think/Upload/Driver/Upyun.class.php ================================================ // +---------------------------------------------------------------------- namespace Think\Upload\Driver; class Upyun{ /** * 上传文件根目录 * @var string */ private $rootPath; /** * 上传错误信息 * @var string */ private $error = ''; private $config = array( 'host' => '', //又拍云服务器 'username' => '', //又拍云用户 'password' => '', //又拍云密码 'bucket' => '', //空间名称 'timeout' => 90, //超时时间 ); /** * 构造函数,用于设置上传根路径 * @param array $config FTP配置 */ public function __construct($config){ /* 默认FTP配置 */ $this->config = array_merge($this->config, $config); $this->config['password'] = md5($this->config['password']); } /** * 检测上传根目录(又拍云上传时支持自动创建目录,直接返回) * @param string $rootpath 根目录 * @return boolean true-检测通过,false-检测失败 */ public function checkRootPath($rootpath){ /* 设置根目录 */ $this->rootPath = trim($rootpath, './') . '/'; return true; } /** * 检测上传目录(又拍云上传时支持自动创建目录,直接返回) * @param string $savepath 上传目录 * @return boolean 检测结果,true-通过,false-失败 */ public function checkSavePath($savepath){ return true; } /** * 创建文件夹 (又拍云上传时支持自动创建目录,直接返回) * @param string $savepath 目录名称 * @return boolean true-创建成功,false-创建失败 */ public function mkdir($savepath){ return true; } /** * 保存指定文件 * @param array $file 保存的文件信息 * @param boolean $replace 同名文件是否覆盖 * @return boolean 保存状态,true-成功,false-失败 */ public function save($file, $replace = true) { $header['Content-Type'] = $file['type']; $header['Content-MD5'] = $file['md5']; $header['Mkdir'] = 'true'; $resource = fopen($file['tmp_name'], 'r'); $save = $this->rootPath . $file['savepath'] . $file['savename']; $data = $this->request($save, 'PUT', $header, $resource); return false === $data ? false : true; } /** * 获取最后一次上传错误信息 * @return string 错误信息 */ public function getError(){ return $this->error; } /** * 请求又拍云服务器 * @param string $path 请求的PATH * @param string $method 请求方法 * @param array $headers 请求header * @param resource $body 上传文件资源 * @return boolean */ private function request($path, $method, $headers = null, $body = null){ $uri = "/{$this->config['bucket']}/{$path}"; $ch = curl_init($this->config['host'] . $uri); $_headers = array('Expect:'); if (!is_null($headers) && is_array($headers)){ foreach($headers as $k => $v) { array_push($_headers, "{$k}: {$v}"); } } $length = 0; $date = gmdate('D, d M Y H:i:s \G\M\T'); if (!is_null($body)) { if(is_resource($body)){ fseek($body, 0, SEEK_END); $length = ftell($body); fseek($body, 0); array_push($_headers, "Content-Length: {$length}"); curl_setopt($ch, CURLOPT_INFILE, $body); curl_setopt($ch, CURLOPT_INFILESIZE, $length); } else { $length = @strlen($body); array_push($_headers, "Content-Length: {$length}"); curl_setopt($ch, CURLOPT_POSTFIELDS, $body); } } else { array_push($_headers, "Content-Length: {$length}"); } array_push($_headers, 'Authorization: ' . $this->sign($method, $uri, $date, $length)); array_push($_headers, "Date: {$date}"); curl_setopt($ch, CURLOPT_HTTPHEADER, $_headers); curl_setopt($ch, CURLOPT_TIMEOUT, $this->config['timeout']); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); if ($method == 'PUT' || $method == 'POST') { curl_setopt($ch, CURLOPT_POST, 1); } else { curl_setopt($ch, CURLOPT_POST, 0); } if ($method == 'HEAD') { curl_setopt($ch, CURLOPT_NOBODY, true); } $response = curl_exec($ch); $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); list($header, $body) = explode("\r\n\r\n", $response, 2); if ($status == 200) { if ($method == 'GET') { return $body; } else { $data = $this->response($header); return count($data) > 0 ? $data : true; } } else { $this->error($header); return false; } } /** * 获取响应数据 * @param string $text 响应头字符串 * @return array 响应数据列表 */ private function response($text){ $headers = explode("\r\n", $text); $items = array(); foreach($headers as $header) { $header = trim($header); if(strpos($header, 'x-upyun') !== False){ list($k, $v) = explode(':', $header); $items[trim($k)] = in_array(substr($k,8,5), array('width','heigh','frame')) ? intval($v) : trim($v); } } return $items; } /** * 生成请求签名 * @param string $method 请求方法 * @param string $uri 请求URI * @param string $date 请求时间 * @param integer $length 请求内容大小 * @return string 请求签名 */ private function sign($method, $uri, $date, $length){ $sign = "{$method}&{$uri}&{$date}&{$length}&{$this->config['password']}"; return 'UpYun ' . $this->config['username'] . ':' . md5($sign); } /** * 获取请求错误信息 * @param string $header 请求返回头信息 */ private function error($header) { list($status, $stash) = explode("\r\n", $header, 2); list($v, $code, $message) = explode(" ", $status, 3); $message = is_null($message) ? 'File Not Found' : "[{$status}]:{$message}"; $this->error = $message; } } ================================================ FILE: ThinkPHP/Library/Think/Upload.class.php ================================================ // +---------------------------------------------------------------------- namespace Think; class Upload { /** * 默认上传配置 * @var array */ private $config = array( 'mimes' => array(), //允许上传的文件MiMe类型 'maxSize' => 0, //上传的文件大小限制 (0-不做限制) 'exts' => array(), //允许上传的文件后缀 'autoSub' => true, //自动子目录保存文件 'subName' => array('date', 'Y-m-d'), //子目录创建方式,[0]-函数名,[1]-参数,多个参数使用数组 'rootPath' => './Uploads/', //保存根路径 'savePath' => '', //保存路径 'saveName' => array('uniqid', ''), //上传文件命名规则,[0]-函数名,[1]-参数,多个参数使用数组 'saveExt' => '', //文件保存后缀,空则使用原后缀 'replace' => false, //存在同名是否覆盖 'hash' => true, //是否生成hash编码 'callback' => false, //检测文件是否存在回调,如果存在返回文件信息数组 'driver' => '', // 文件上传驱动 'driverConfig' => array(), // 上传驱动配置 ); /** * 上传错误信息 * @var string */ private $error = ''; //上传错误信息 /** * 上传驱动实例 * @var Object */ private $uploader; /** * 构造方法,用于构造上传实例 * @param array $config 配置 * @param string $driver 要使用的上传驱动 LOCAL-本地上传驱动,FTP-FTP上传驱动 */ public function __construct($config = array(), $driver = '', $driverConfig = null){ /* 获取配置 */ $this->config = array_merge($this->config, $config); /* 设置上传驱动 */ $this->setDriver($driver, $driverConfig); /* 调整配置,把字符串配置参数转换为数组 */ if(!empty($this->config['mimes'])){ if(is_string($this->mimes)) { $this->config['mimes'] = explode(',', $this->mimes); } $this->config['mimes'] = array_map('strtolower', $this->mimes); } if(!empty($this->config['exts'])){ if (is_string($this->exts)){ $this->config['exts'] = explode(',', $this->exts); } $this->config['exts'] = array_map('strtolower', $this->exts); } } /** * 使用 $this->name 获取配置 * @param string $name 配置名称 * @return multitype 配置值 */ public function __get($name) { return $this->config[$name]; } public function __set($name,$value){ if(isset($this->config[$name])) { $this->config[$name] = $value; if($name == 'driverConfig'){ //改变驱动配置后重置上传驱动 //注意:必须选改变驱动然后再改变驱动配置 $this->setDriver(); } } } public function __isset($name){ return isset($this->config[$name]); } /** * 获取最后一次上传错误信息 * @return string 错误信息 */ public function getError(){ return $this->error; } /** * 上传单个文件 * @param array $file 文件数组 * @return array 上传成功后的文件信息 */ public function uploadOne($file){ $info = $this->upload(array($file)); return $info ? $info[0] : $info; } /** * 上传文件 * @param 文件信息数组 $files ,通常是 $_FILES数组 */ public function upload($files='') { if('' === $files){ $files = $_FILES; } if(empty($files)){ $this->error = '没有上传的文件!'; return false; } /* 检测上传根目录 */ if(!$this->uploader->checkRootPath($this->rootPath)){ $this->error = $this->uploader->getError(); return false; } /* 检查上传目录 */ if(!$this->uploader->checkSavePath($this->savePath)){ $this->error = $this->uploader->getError(); return false; } /* 逐个检测并上传文件 */ $info = array(); if(function_exists('finfo_open')){ $finfo = finfo_open ( FILEINFO_MIME_TYPE ); } // 对上传文件数组信息处理 $files = $this->dealFiles($files); foreach ($files as $key => $file) { $file['name'] = strip_tags($file['name']); if(!isset($file['key'])) $file['key'] = $key; /* 通过扩展获取文件类型,可解决FLASH上传$FILES数组返回文件类型错误的问题 */ if(isset($finfo)){ $file['type'] = finfo_file ( $finfo , $file['tmp_name'] ); } /* 获取上传文件后缀,允许上传无后缀文件 */ $file['ext'] = pathinfo($file['name'], PATHINFO_EXTENSION); /* 文件上传检测 */ if (!$this->check($file)){ continue; } /* 获取文件hash */ if($this->hash){ $file['md5'] = md5_file($file['tmp_name']); $file['sha1'] = sha1_file($file['tmp_name']); } /* 调用回调函数检测文件是否存在 */ $data = call_user_func($this->callback, $file); if( $this->callback && $data ){ if ( file_exists('.'.$data['path']) ) { $info[$key] = $data; continue; }elseif($this->removeTrash){ call_user_func($this->removeTrash,$data);//删除垃圾据 } } /* 生成保存文件名 */ $savename = $this->getSaveName($file); if(false == $savename){ continue; } else { $file['savename'] = $savename; } /* 检测并创建子目录 */ $subpath = $this->getSubPath($file['name']); if(false === $subpath){ continue; } else { $file['savepath'] = $this->savePath . $subpath; } /* 对图像文件进行严格检测 */ $ext = strtolower($file['ext']); if(in_array($ext, array('gif','jpg','jpeg','bmp','png','swf'))) { $imginfo = getimagesize($file['tmp_name']); if(empty($imginfo) || ($ext == 'gif' && empty($imginfo['bits']))){ $this->error = '非法图像文件!'; continue; } } /* 保存文件 并记录保存成功的文件 */ if ($this->uploader->save($file,$this->replace)) { unset($file['error'], $file['tmp_name']); $info[$key] = $file; } else { $this->error = $this->uploader->getError(); } } if(isset($finfo)){ finfo_close($finfo); } return empty($info) ? false : $info; } /** * 转换上传文件数组变量为正确的方式 * @access private * @param array $files 上传的文件变量 * @return array */ private function dealFiles($files) { $fileArray = array(); $n = 0; foreach ($files as $key=>$file){ if(is_array($file['name'])) { $keys = array_keys($file); $count = count($file['name']); for ($i=0; $i<$count; $i++) { $fileArray[$n]['key'] = $key; foreach ($keys as $_key){ $fileArray[$n][$_key] = $file[$_key][$i]; } $n++; } }else{ $fileArray = $files; break; } } return $fileArray; } /** * 设置上传驱动 * @param string $driver 驱动名称 * @param array $config 驱动配置 */ private function setDriver($driver = null, $config = null){ $driver = $driver ? : ($this->driver ? : C('FILE_UPLOAD_TYPE')); $config = $config ? : ($this->driverConfig ? : C('UPLOAD_TYPE_CONFIG')); $class = strpos($driver,'\\')? $driver : 'Think\\Upload\\Driver\\'.ucfirst(strtolower($driver)); $this->uploader = new $class($config); if(!$this->uploader){ E("不存在上传驱动:{$name}"); } } /** * 检查上传的文件 * @param array $file 文件信息 */ private function check($file) { /* 文件上传失败,捕获错误代码 */ if ($file['error']) { $this->error($file['error']); return false; } /* 无效上传 */ if (empty($file['name'])){ $this->error = '未知上传错误!'; } /* 检查是否合法上传 */ if (!is_uploaded_file($file['tmp_name'])) { $this->error = '非法上传文件!'; return false; } /* 检查文件大小 */ if (!$this->checkSize($file['size'])) { $this->error = '上传文件大小不符!'; return false; } /* 检查文件Mime类型 */ //TODO:FLASH上传的文件获取到的mime类型都为application/octet-stream if (!$this->checkMime($file['type'])) { $this->error = '上传文件MIME类型不允许!'; return false; } /* 检查文件后缀 */ if (!$this->checkExt($file['ext'])) { $this->error = '上传文件后缀不允许'; return false; } /* 通过检测 */ return true; } /** * 获取错误代码信息 * @param string $errorNo 错误号 */ private function error($errorNo) { switch ($errorNo) { case 1: $this->error = '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值!'; break; case 2: $this->error = '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值!'; break; case 3: $this->error = '文件只有部分被上传!'; break; case 4: $this->error = '没有文件被上传!'; break; case 6: $this->error = '找不到临时文件夹!'; break; case 7: $this->error = '文件写入失败!'; break; default: $this->error = '未知上传错误!'; } } /** * 检查文件大小是否合法 * @param integer $size 数据 */ private function checkSize($size) { return !($size > $this->maxSize) || (0 == $this->maxSize); } /** * 检查上传的文件MIME类型是否合法 * @param string $mime 数据 */ private function checkMime($mime) { return empty($this->config['mimes']) ? true : in_array(strtolower($mime), $this->mimes); } /** * 检查上传的文件后缀是否合法 * @param string $ext 后缀 */ private function checkExt($ext) { return empty($this->config['exts']) ? true : in_array(strtolower($ext), $this->exts); } /** * 根据上传文件命名规则取得保存文件名 * @param string $file 文件信息 */ private function getSaveName($file) { $rule = $this->saveName; if (empty($rule)) { //保持文件名不变 /* 解决pathinfo中文文件名BUG */ $filename = substr(pathinfo("_{$file['name']}", PATHINFO_FILENAME), 1); $savename = $filename; } else { $savename = $this->getName($rule, $file['name']); if(empty($savename)){ $this->error = '文件命名规则错误!'; return false; } } /* 文件保存后缀,支持强制更改文件后缀 */ $ext = empty($this->config['saveExt']) ? $file['ext'] : $this->saveExt; return $savename . '.' . $ext; } /** * 获取子目录的名称 * @param array $file 上传的文件信息 */ private function getSubPath($filename) { $subpath = ''; $rule = $this->subName; if ($this->autoSub && !empty($rule)) { $subpath = $this->getName($rule, $filename) . '/'; if(!empty($subpath) && !$this->uploader->mkdir($this->savePath . $subpath)){ $this->error = $this->uploader->getError(); return false; } } return $subpath; } /** * 根据指定的规则获取文件或目录名称 * @param array $rule 规则 * @param string $filename 原文件名 * @return string 文件或目录名称 */ private function getName($rule, $filename){ $name = ''; if(is_array($rule)){ //数组规则 $func = $rule[0]; $param = (array)$rule[1]; foreach ($param as &$value) { $value = str_replace('__FILE__', $filename, $value); } $name = call_user_func_array($func, $param); } elseif (is_string($rule)){ //字符串规则 if(function_exists($rule)){ $name = call_user_func($rule); } else { $name = $rule; } } return $name; } } ================================================ FILE: ThinkPHP/Library/Think/Verify.class.php ================================================ // +---------------------------------------------------------------------- namespace Think; class Verify { protected $config = array( 'seKey' => 'ThinkPHP.CN', // 验证码加密密钥 'codeSet' => '2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY', // 验证码字符集合 'expire' => 1800, // 验证码过期时间(s) 'useZh' => false, // 使用中文验证码 'zhSet' => '们以我到他会作时要动国产的一是工就年阶义发成部民可出能方进在了不和有大这主中人上为来分生对于学下级地个用同行面说种过命度革而多子后自社加小机也经力线本电高量长党得实家定深法表着水理化争现所二起政三好十战无农使性前等反体合斗路图把结第里正新开论之物从当两些还天资事队批点育重其思与间内去因件日利相由压员气业代全组数果期导平各基或月毛然如应形想制心样干都向变关问比展那它最及外没看治提五解系林者米群头意只明四道马认次文通但条较克又公孔领军流入接席位情运器并飞原油放立题质指建区验活众很教决特此常石强极土少已根共直团统式转别造切九你取西持总料连任志观调七么山程百报更见必真保热委手改管处己将修支识病象几先老光专什六型具示复安带每东增则完风回南广劳轮科北打积车计给节做务被整联步类集号列温装即毫知轴研单色坚据速防史拉世设达尔场织历花受求传口断况采精金界品判参层止边清至万确究书术状厂须离再目海交权且儿青才证低越际八试规斯近注办布门铁需走议县兵固除般引齿千胜细影济白格效置推空配刀叶率述今选养德话查差半敌始片施响收华觉备名红续均药标记难存测士身紧液派准斤角降维板许破述技消底床田势端感往神便贺村构照容非搞亚磨族火段算适讲按值美态黄易彪服早班麦削信排台声该击素张密害侯草何树肥继右属市严径螺检左页抗苏显苦英快称坏移约巴材省黑武培著河帝仅针怎植京助升王眼她抓含苗副杂普谈围食射源例致酸旧却充足短划剂宣环落首尺波承粉践府鱼随考刻靠够满夫失包住促枝局菌杆周护岩师举曲春元超负砂封换太模贫减阳扬江析亩木言球朝医校古呢稻宋听唯输滑站另卫字鼓刚写刘微略范供阿块某功套友限项余倒卷创律雨让骨远帮初皮播优占死毒圈伟季训控激找叫云互跟裂粮粒母练塞钢顶策双留误础吸阻故寸盾晚丝女散焊功株亲院冷彻弹错散商视艺灭版烈零室轻血倍缺厘泵察绝富城冲喷壤简否柱李望盘磁雄似困巩益洲脱投送奴侧润盖挥距触星松送获兴独官混纪依未突架宽冬章湿偏纹吃执阀矿寨责熟稳夺硬价努翻奇甲预职评读背协损棉侵灰虽矛厚罗泥辟告卵箱掌氧恩爱停曾溶营终纲孟钱待尽俄缩沙退陈讨奋械载胞幼哪剥迫旋征槽倒握担仍呀鲜吧卡粗介钻逐弱脚怕盐末阴丰雾冠丙街莱贝辐肠付吉渗瑞惊顿挤秒悬姆烂森糖圣凹陶词迟蚕亿矩康遵牧遭幅园腔订香肉弟屋敏恢忘编印蜂急拿扩伤飞露核缘游振操央伍域甚迅辉异序免纸夜乡久隶缸夹念兰映沟乙吗儒杀汽磷艰晶插埃燃欢铁补咱芽永瓦倾阵碳演威附牙芽永瓦斜灌欧献顺猪洋腐请透司危括脉宜笑若尾束壮暴企菜穗楚汉愈绿拖牛份染既秋遍锻玉夏疗尖殖井费州访吹荣铜沿替滚客召旱悟刺脑措贯藏敢令隙炉壳硫煤迎铸粘探临薄旬善福纵择礼愿伏残雷延烟句纯渐耕跑泽慢栽鲁赤繁境潮横掉锥希池败船假亮谓托伙哲怀割摆贡呈劲财仪沉炼麻罪祖息车穿货销齐鼠抽画饲龙库守筑房歌寒喜哥洗蚀废纳腹乎录镜妇恶脂庄擦险赞钟摇典柄辩竹谷卖乱虚桥奥伯赶垂途额壁网截野遗静谋弄挂课镇妄盛耐援扎虑键归符庆聚绕摩忙舞遇索顾胶羊湖钉仁音迹碎伸灯避泛亡答勇频皇柳哈揭甘诺概宪浓岛袭谁洪谢炮浇斑讯懂灵蛋闭孩释乳巨徒私银伊景坦累匀霉杜乐勒隔弯绩招绍胡呼痛峰零柴簧午跳居尚丁秦稍追梁折耗碱殊岗挖氏刃剧堆赫荷胸衡勤膜篇登驻案刊秧缓凸役剪川雪链渔啦脸户洛孢勃盟买杨宗焦赛旗滤硅炭股坐蒸凝竟陷枪黎救冒暗洞犯筒您宋弧爆谬涂味津臂障褐陆啊健尊豆拔莫抵桑坡缝警挑污冰柬嘴啥饭塑寄赵喊垫丹渡耳刨虎笔稀昆浪萨茶滴浅拥穴覆伦娘吨浸袖珠雌妈紫戏塔锤震岁貌洁剖牢锋疑霸闪埔猛诉刷狠忽灾闹乔唐漏闻沈熔氯荒茎男凡抢像浆旁玻亦忠唱蒙予纷捕锁尤乘乌智淡允叛畜俘摸锈扫毕璃宝芯爷鉴秘净蒋钙肩腾枯抛轨堂拌爸循诱祝励肯酒绳穷塘燥泡袋朗喂铝软渠颗惯贸粪综墙趋彼届墨碍启逆卸航衣孙龄岭骗休借', // 中文验证码字符串 'useImgBg' => false, // 使用背景图片 'fontSize' => 25, // 验证码字体大小(px) 'useCurve' => true, // 是否画混淆曲线 'useNoise' => true, // 是否添加杂点 'imageH' => 0, // 验证码图片高度 'imageW' => 0, // 验证码图片宽度 'length' => 5, // 验证码位数 'fontttf' => '', // 验证码字体,不设置随机获取 'bg' => array(243, 251, 254), // 背景颜色 'reset' => true, // 验证成功后是否重置 ); private $_image = NULL; // 验证码图片实例 private $_color = NULL; // 验证码字体颜色 /** * 架构方法 设置参数 * @access public * @param array $config 配置参数 */ public function __construct($config=array()){ $this->config = array_merge($this->config, $config); } /** * 使用 $this->name 获取配置 * @access public * @param string $name 配置名称 * @return multitype 配置值 */ public function __get($name) { return $this->config[$name]; } /** * 设置验证码配置 * @access public * @param string $name 配置名称 * @param string $value 配置值 * @return void */ public function __set($name,$value){ if(isset($this->config[$name])) { $this->config[$name] = $value; } } /** * 检查配置 * @access public * @param string $name 配置名称 * @return bool */ public function __isset($name){ return isset($this->config[$name]); } /** * 验证验证码是否正确 * @access public * @param string $code 用户验证码 * @param string $id 验证码标识 * @return bool 用户验证码是否正确 */ public function check($code, $id = '') { $key = $this->authcode($this->seKey).$id; // 验证码不能为空 $secode = session($key); if(empty($code) || empty($secode)) { return false; } // session 过期 if(NOW_TIME - $secode['verify_time'] > $this->expire) { session($key, null); return false; } if($this->authcode(strtoupper($code)) == $secode['verify_code']) { $this->reset && session($key, null); return true; } return false; } /** * 输出验证码并把验证码的值保存的session中 * 验证码保存到session的格式为: array('verify_code' => '验证码值', 'verify_time' => '验证码创建时间'); * @access public * @param string $id 要生成验证码的标识 * @return void */ public function entry($id = '') { // 图片宽(px) $this->imageW || $this->imageW = $this->length*$this->fontSize*1.5 + $this->length*$this->fontSize/2; // 图片高(px) $this->imageH || $this->imageH = $this->fontSize * 2.5; // 建立一幅 $this->imageW x $this->imageH 的图像 $this->_image = imagecreate($this->imageW, $this->imageH); // 设置背景 imagecolorallocate($this->_image, $this->bg[0], $this->bg[1], $this->bg[2]); // 验证码字体随机颜色 $this->_color = imagecolorallocate($this->_image, mt_rand(1,150), mt_rand(1,150), mt_rand(1,150)); // 验证码使用随机字体 $ttfPath = dirname(__FILE__) . '/Verify/' . ($this->useZh ? 'zhttfs' : 'ttfs') . '/'; if(empty($this->fontttf)){ $dir = dir($ttfPath); $ttfs = array(); while (false !== ($file = $dir->read())) { if($file[0] != '.' && substr($file, -4) == '.ttf') { $ttfs[] = $file; } } $dir->close(); $this->fontttf = $ttfs[array_rand($ttfs)]; } $this->fontttf = $ttfPath . $this->fontttf; if($this->useImgBg) { $this->_background(); } if ($this->useNoise) { // 绘杂点 $this->_writeNoise(); } if ($this->useCurve) { // 绘干扰线 $this->_writeCurve(); } // 绘验证码 $code = array(); // 验证码 $codeNX = 0; // 验证码第N个字符的左边距 if($this->useZh){ // 中文验证码 for ($i = 0; $i<$this->length; $i++) { $code[$i] = iconv_substr($this->zhSet,floor(mt_rand(0,mb_strlen($this->zhSet,'utf-8')-1)),1,'utf-8'); imagettftext($this->_image, $this->fontSize, mt_rand(-40, 40), $this->fontSize*($i+1)*1.5, $this->fontSize + mt_rand(10, 20), $this->_color, $this->fontttf, $code[$i]); } }else{ for ($i = 0; $i<$this->length; $i++) { $code[$i] = $this->codeSet[mt_rand(0, strlen($this->codeSet)-1)]; $codeNX += mt_rand($this->fontSize*1.2, $this->fontSize*1.6); imagettftext($this->_image, $this->fontSize, mt_rand(-40, 40), $codeNX, $this->fontSize*1.6, $this->_color, $this->fontttf, $code[$i]); } } // 保存验证码 $key = $this->authcode($this->seKey); $code = $this->authcode(strtoupper(implode('', $code))); $secode = array(); $secode['verify_code'] = $code; // 把校验码保存到session $secode['verify_time'] = NOW_TIME; // 验证码创建时间 session($key.$id, $secode); header('Cache-Control: private, max-age=0, no-store, no-cache, must-revalidate'); header('Cache-Control: post-check=0, pre-check=0', false); header('Pragma: no-cache'); header("content-type: image/png"); // 输出图像 imagepng($this->_image); imagedestroy($this->_image); } /** * 画一条由两条连在一起构成的随机正弦函数曲线作干扰线(你可以改成更帅的曲线函数) * * 高中的数学公式咋都忘了涅,写出来 * 正弦型函数解析式:y=Asin(ωx+φ)+b * 各常数值对函数图像的影响: * A:决定峰值(即纵向拉伸压缩的倍数) * b:表示波形在Y轴的位置关系或纵向移动距离(上加下减) * φ:决定波形与X轴位置关系或横向移动距离(左加右减) * ω:决定周期(最小正周期T=2π/∣ω∣) * */ private function _writeCurve() { $px = $py = 0; // 曲线前部分 $A = mt_rand(1, $this->imageH/2); // 振幅 $b = mt_rand(-$this->imageH/4, $this->imageH/4); // Y轴方向偏移量 $f = mt_rand(-$this->imageH/4, $this->imageH/4); // X轴方向偏移量 $T = mt_rand($this->imageH, $this->imageW*2); // 周期 $w = (2* M_PI)/$T; $px1 = 0; // 曲线横坐标起始位置 $px2 = mt_rand($this->imageW/2, $this->imageW * 0.8); // 曲线横坐标结束位置 for ($px=$px1; $px<=$px2; $px = $px + 1) { if ($w!=0) { $py = $A * sin($w*$px + $f)+ $b + $this->imageH/2; // y = Asin(ωx+φ) + b $i = (int) ($this->fontSize/5); while ($i > 0) { imagesetpixel($this->_image, $px + $i , $py + $i, $this->_color); // 这里(while)循环画像素点比imagettftext和imagestring用字体大小一次画出(不用这while循环)性能要好很多 $i--; } } } // 曲线后部分 $A = mt_rand(1, $this->imageH/2); // 振幅 $f = mt_rand(-$this->imageH/4, $this->imageH/4); // X轴方向偏移量 $T = mt_rand($this->imageH, $this->imageW*2); // 周期 $w = (2* M_PI)/$T; $b = $py - $A * sin($w*$px + $f) - $this->imageH/2; $px1 = $px2; $px2 = $this->imageW; for ($px=$px1; $px<=$px2; $px=$px+ 1) { if ($w!=0) { $py = $A * sin($w*$px + $f)+ $b + $this->imageH/2; // y = Asin(ωx+φ) + b $i = (int) ($this->fontSize/5); while ($i > 0) { imagesetpixel($this->_image, $px + $i, $py + $i, $this->_color); $i--; } } } } /** * 画杂点 * 往图片上写不同颜色的字母或数字 */ private function _writeNoise() { $codeSet = '2345678abcdefhijkmnpqrstuvwxyz'; for($i = 0; $i < 10; $i++){ //杂点颜色 $noiseColor = imagecolorallocate($this->_image, mt_rand(150,225), mt_rand(150,225), mt_rand(150,225)); for($j = 0; $j < 5; $j++) { // 绘杂点 imagestring($this->_image, 5, mt_rand(-10, $this->imageW), mt_rand(-10, $this->imageH), $codeSet[mt_rand(0, 29)], $noiseColor); } } } /** * 绘制背景图片 * 注:如果验证码输出图片比较大,将占用比较多的系统资源 */ private function _background() { $path = dirname(__FILE__).'/Verify/bgs/'; $dir = dir($path); $bgs = array(); while (false !== ($file = $dir->read())) { if($file[0] != '.' && substr($file, -4) == '.jpg') { $bgs[] = $path . $file; } } $dir->close(); $gb = $bgs[array_rand($bgs)]; list($width, $height) = @getimagesize($gb); // Resample $bgImage = @imagecreatefromjpeg($gb); @imagecopyresampled($this->_image, $bgImage, 0, 0, 0, 0, $this->imageW, $this->imageH, $width, $height); @imagedestroy($bgImage); } /* 加密验证码 */ private function authcode($str){ $key = substr(md5($this->seKey), 5, 8); $str = substr(md5($str), 8, 10); return md5($key . $str); } } ================================================ FILE: ThinkPHP/Library/Think/View.class.php ================================================ // +---------------------------------------------------------------------- namespace Think; /** * ThinkPHP 视图类 */ class View { /** * 模板输出变量 * @var tVar * @access protected */ protected $tVar = array(); /** * 模板主题 * @var theme * @access protected */ protected $theme = ''; /** * 模板变量赋值 * @access public * @param mixed $name * @param mixed $value */ public function assign($name,$value=''){ if(is_array($name)) { $this->tVar = array_merge($this->tVar,$name); }else { $this->tVar[$name] = $value; } } /** * 取得模板变量的值 * @access public * @param string $name * @return mixed */ public function get($name=''){ if('' === $name) { return $this->tVar; } return isset($this->tVar[$name])?$this->tVar[$name]:false; } /** * 加载模板和页面输出 可以返回输出内容 * @access public * @param string $templateFile 模板文件名 * @param string $charset 模板输出字符集 * @param string $contentType 输出类型 * @param string $content 模板输出内容 * @param string $prefix 模板缓存前缀 * @return mixed */ public function display($templateFile='',$charset='',$contentType='',$content='',$prefix='') { G('viewStartTime'); // 视图开始标签 Hook::listen('view_begin',$templateFile); // 解析并获取模板内容 $content = $this->fetch($templateFile,$content,$prefix); // 输出模板内容 $this->render($content,$charset,$contentType); // 视图结束标签 Hook::listen('view_end'); } /** * 输出内容文本可以包括Html * @access private * @param string $content 输出内容 * @param string $charset 模板输出字符集 * @param string $contentType 输出类型 * @return mixed */ private function render($content,$charset='',$contentType=''){ if(empty($charset)) $charset = C('DEFAULT_CHARSET'); if(empty($contentType)) $contentType = C('TMPL_CONTENT_TYPE'); // 网页字符编码 header('Content-Type:'.$contentType.'; charset='.$charset); header('Cache-control: '.C('HTTP_CACHE_CONTROL')); // 页面缓存控制 header('X-Powered-By:ThinkPHP'); // 输出模板文件 echo $content; } /** * 解析和获取模板内容 用于输出 * @access public * @param string $templateFile 模板文件名 * @param string $content 模板输出内容 * @param string $prefix 模板缓存前缀 * @return string */ public function fetch($templateFile='',$content='',$prefix='') { if(empty($content)) { $templateFile = $this->parseTemplate($templateFile); // 模板文件不存在直接返回 if(!is_file($templateFile)) E(L('_TEMPLATE_NOT_EXIST_').':'.$templateFile); }else{ defined('THEME_PATH') or define('THEME_PATH', $this->getThemePath()); } // 页面缓存 ob_start(); ob_implicit_flush(0); if('php' == strtolower(C('TMPL_ENGINE_TYPE'))) { // 使用PHP原生模板 $_content = $content; // 模板阵列变量分解成为独立变量 extract($this->tVar, EXTR_OVERWRITE); // 直接载入PHP模板 empty($_content)?include $templateFile:eval('?>'.$_content); }else{ // 视图解析标签 $params = array('var'=>$this->tVar,'file'=>$templateFile,'content'=>$content,'prefix'=>$prefix); Hook::listen('view_parse',$params); } // 获取并清空缓存 $content = ob_get_clean(); // 内容过滤标签 Hook::listen('view_filter',$content); // 输出模板文件 return $content; } /** * 自动定位模板文件 * @access protected * @param string $template 模板文件规则 * @return string */ public function parseTemplate($template='') { if(is_file($template)) { return $template; } $depr = C('TMPL_FILE_DEPR'); $template = str_replace(':', $depr, $template); // 获取当前模块 $module = MODULE_NAME; if(strpos($template,'@')){ // 跨模块调用模版文件 list($module,$template) = explode('@',$template); } // 获取当前主题的模版路径 defined('THEME_PATH') or define('THEME_PATH', $this->getThemePath($module)); // 分析模板文件规则 if('' == $template) { // 如果模板文件名为空 按照默认规则定位 $template = CONTROLLER_NAME . $depr . ACTION_NAME; }elseif(false === strpos($template, $depr)){ $template = CONTROLLER_NAME . $depr . $template; } $file = THEME_PATH.$template.C('TMPL_TEMPLATE_SUFFIX'); if(C('TMPL_LOAD_DEFAULTTHEME') && THEME_NAME != C('DEFAULT_THEME') && !is_file($file)){ // 找不到当前主题模板的时候定位默认主题中的模板 $file = dirname(THEME_PATH).'/'.C('DEFAULT_THEME').'/'.$template.C('TMPL_TEMPLATE_SUFFIX'); } return $file; } /** * 获取当前的模板路径 * @access protected * @param string $module 模块名 * @return string */ protected function getThemePath($module=MODULE_NAME){ // 获取当前主题名称 $theme = $this->getTemplateTheme(); // 获取当前主题的模版路径 $tmplPath = C('VIEW_PATH'); // 模块设置独立的视图目录 if(!$tmplPath){ // 定义TMPL_PATH 则改变全局的视图目录到模块之外 $tmplPath = defined('TMPL_PATH')? TMPL_PATH.$module.'/' : APP_PATH.$module.'/'.C('DEFAULT_V_LAYER').'/'; } return $tmplPath.$theme; } /** * 设置当前输出的模板主题 * @access public * @param mixed $theme 主题名称 * @return View */ public function theme($theme){ $this->theme = $theme; return $this; } /** * 获取当前的模板主题 * @access private * @return string */ private function getTemplateTheme() { if($this->theme) { // 指定模板主题 $theme = $this->theme; }else{ /* 获取模板主题名称 */ $theme = C('DEFAULT_THEME'); if(C('TMPL_DETECT_THEME')) {// 自动侦测模板主题 $t = C('VAR_TEMPLATE'); if (isset($_GET[$t])){ $theme = $_GET[$t]; }elseif(cookie('think_template')){ $theme = cookie('think_template'); } if(!in_array($theme,explode(',',C('THEME_LIST')))){ $theme = C('DEFAULT_THEME'); } cookie('think_template',$theme,864000); } } defined('THEME_NAME') || define('THEME_NAME', $theme); // 当前模板主题名称 return $theme?$theme . '/':''; } } ================================================ FILE: ThinkPHP/Library/Vendor/Boris/Boris.php ================================================ ', $historyFile = null) { $this->setPrompt($prompt); $this->_historyFile = $historyFile ? $historyFile : sprintf('%s/.boris_history', getenv('HOME')) ; $this->_inspector = new ColoredInspector(); } /** * Add a new hook to run in the context of the REPL when it starts. * * @param mixed $hook * * The hook is either a string of PHP code to eval(), or a Closure accepting * the EvalWorker object as its first argument and the array of defined * local variables in the second argument. * * If the hook is a callback and needs to set any local variables in the * REPL's scope, it should invoke $worker->setLocal($var_name, $value) to * do so. * * Hooks are guaranteed to run in the order they were added and the state * set by each hook is available to the next hook (either through global * resources, such as classes and interfaces, or through the 2nd parameter * of the callback, if any local variables were set. * * @example Contrived example where one hook sets the date and another * prints it in the REPL. * * $boris->onStart(function($worker, $vars){ * $worker->setLocal('date', date('Y-m-d')); * }); * * $boris->onStart('echo "The date is $date\n";'); */ public function onStart($hook) { $this->_startHooks[] = $hook; } /** * Add a new hook to run in the context of the REPL when a fatal error occurs. * * @param mixed $hook * * The hook is either a string of PHP code to eval(), or a Closure accepting * the EvalWorker object as its first argument and the array of defined * local variables in the second argument. * * If the hook is a callback and needs to set any local variables in the * REPL's scope, it should invoke $worker->setLocal($var_name, $value) to * do so. * * Hooks are guaranteed to run in the order they were added and the state * set by each hook is available to the next hook (either through global * resources, such as classes and interfaces, or through the 2nd parameter * of the callback, if any local variables were set. * * @example An example if your project requires some database connection cleanup: * * $boris->onFailure(function($worker, $vars){ * DB::reset(); * }); */ public function onFailure($hook){ $this->_failureHooks[] = $hook; } /** * Set a local variable, or many local variables. * * @example Setting a single variable * $boris->setLocal('user', $bob); * * @example Setting many variables at once * $boris->setLocal(array('user' => $bob, 'appContext' => $appContext)); * * This method can safely be invoked repeatedly. * * @param array|string $local * @param mixed $value, optional */ public function setLocal($local, $value = null) { if (!is_array($local)) { $local = array($local => $value); } $this->_exports = array_merge($this->_exports, $local); } /** * Sets the Boris prompt text * * @param string $prompt */ public function setPrompt($prompt) { $this->_prompt = $prompt; } /** * Set an Inspector object for Boris to output return values with. * * @param object $inspector any object the responds to inspect($v) */ public function setInspector($inspector) { $this->_inspector = $inspector; } /** * Start the REPL (display the readline prompt). * * This method never returns. */ public function start() { declare(ticks = 1); pcntl_signal(SIGINT, SIG_IGN, true); if (!$pipes = stream_socket_pair( STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP)) { throw new \RuntimeException('Failed to create socket pair'); } $pid = pcntl_fork(); if ($pid > 0) { if (function_exists('setproctitle')) { setproctitle('boris (master)'); } fclose($pipes[0]); $client = new ReadlineClient($pipes[1]); $client->start($this->_prompt, $this->_historyFile); } elseif ($pid < 0) { throw new \RuntimeException('Failed to fork child process'); } else { if (function_exists('setproctitle')) { setproctitle('boris (worker)'); } fclose($pipes[1]); $worker = new EvalWorker($pipes[0]); $worker->setLocal($this->_exports); $worker->setStartHooks($this->_startHooks); $worker->setFailureHooks($this->_failureHooks); $worker->setInspector($this->_inspector); $worker->start(); } } } ================================================ FILE: ThinkPHP/Library/Vendor/Boris/CLIOptionsHandler.php ================================================ $value) { switch ($option) { /* * Sets files to load at startup, may be used multiple times, * i.e: boris -r test.php,foo/bar.php -r ba/foo.php --require hey.php */ case 'r': case 'require': $this->_handleRequire($boris, $value); break; /* * Show Usage info */ case 'h': case 'help': $this->_handleUsageInfo(); break; /* * Show version */ case 'v': case 'version': $this->_handleVersion(); break; } } } // -- Private Methods private function _handleRequire($boris, $paths) { $require = array_reduce( (array) $paths, function($acc, $v) { return array_merge($acc, explode(',', $v)); }, array() ); $boris->onStart(function($worker, $scope) use($require) { foreach($require as $path) { require $path; } $worker->setLocal(get_defined_vars()); }); } private function _handleUsageInfo() { echo << * @author Chris Corbyn * * Copyright © 2013-2014 Rob Morris. */ namespace Boris; /** * Identifies data types in data structures and syntax highlights them. */ class ColoredInspector implements Inspector { static $TERM_COLORS = array( 'black' => "\033[0;30m", 'white' => "\033[1;37m", 'none' => "\033[1;30m", 'dark_grey' => "\033[1;30m", 'light_grey' => "\033[0;37m", 'dark_red' => "\033[0;31m", 'light_red' => "\033[1;31m", 'dark_green' => "\033[0;32m", 'light_green' => "\033[1;32m", 'dark_yellow' => "\033[0;33m", 'light_yellow' => "\033[1;33m", 'dark_blue' => "\033[0;34m", 'light_blue' => "\033[1;34m", 'dark_purple' => "\033[0;35m", 'light_purple' => "\033[1;35m", 'dark_cyan' => "\033[0;36m", 'light_cyan' => "\033[1;36m", ); private $_fallback; private $_colorMap = array(); /** * Initialize a new ColoredInspector, using $colorMap. * * The colors should be an associative array with the keys: * * - 'integer' * - 'float' * - 'keyword' * - 'string' * - 'boolean' * - 'default' * * And the values, one of the following colors: * * - 'none' * - 'black' * - 'white' * - 'dark_grey' * - 'light_grey' * - 'dark_red' * - 'light_red' * - 'dark_green' * - 'light_green' * - 'dark_yellow' * - 'light_yellow' * - 'dark_blue' * - 'light_blue' * - 'dark_purple' * - 'light_purple' * - 'dark_cyan' * - 'light_cyan' * * An empty $colorMap array effectively means 'none' for all types. * * @param array $colorMap */ public function __construct($colorMap = null) { $this->_fallback = new DumpInspector(); if (isset($colorMap)) { $this->_colorMap = $colorMap; } else { $this->_colorMap = $this->_defaultColorMap(); } } public function inspect($variable) { return preg_replace( '/^/m', $this->_colorize('comment', '// '), $this->_dump($variable) ); } /** * Returns an associative array of an object's properties. * * This method is public so that subclasses may override it. * * @param object $value * @return array * */ public function objectVars($value) { return get_object_vars($value); } // -- Private Methods public function _dump($value) { $tests = array( 'is_null' => '_dumpNull', 'is_string' => '_dumpString', 'is_bool' => '_dumpBoolean', 'is_integer' => '_dumpInteger', 'is_float' => '_dumpFloat', 'is_array' => '_dumpArray', 'is_object' => '_dumpObject' ); foreach ($tests as $predicate => $outputMethod) { if (call_user_func($predicate, $value)) return call_user_func(array($this, $outputMethod), $value); } return $this->_fallback->inspect($value); } private function _dumpNull($value) { return $this->_colorize('keyword', 'NULL'); } private function _dumpString($value) { return $this->_colorize('string', var_export($value, true)); } private function _dumpBoolean($value) { return $this->_colorize('bool', var_export($value, true)); } private function _dumpInteger($value) { return $this->_colorize('integer', var_export($value, true)); } private function _dumpFloat($value) { return $this->_colorize('float', var_export($value, true)); } private function _dumpArray($value) { return $this->_dumpStructure('array', $value); } private function _dumpObject($value) { return $this->_dumpStructure( sprintf('object(%s)', get_class($value)), $this->objectVars($value) ); } private function _dumpStructure($type, $value) { return $this->_astToString($this->_buildAst($type, $value)); } public function _buildAst($type, $value, $seen = array()) { // FIXME: Improve this AST so it doesn't require access to dump() or colorize() if ($this->_isSeen($value, $seen)) { return $this->_colorize('default', '*** RECURSION ***'); } else { $nextSeen = array_merge($seen, array($value)); } if (is_object($value)) { $vars = $this->objectVars($value); } else { $vars = $value; } $self = $this; return array( 'name' => $this->_colorize('keyword', $type), 'children' => empty($vars) ? array() : array_combine( array_map(array($this, '_dump'), array_keys($vars)), array_map( function($v) use($self, $nextSeen) { if (is_object($v)) { return $self->_buildAst( sprintf('object(%s)', get_class($v)), $v, $nextSeen ); } elseif (is_array($v)) { return $self->_buildAst('array', $v, $nextSeen); } else { return $self->_dump($v); } }, array_values($vars) ) ) ); } public function _astToString($node, $indent = 0) { $children = $node['children']; $self = $this; return implode( "\n", array( sprintf('%s(', $node['name']), implode( ",\n", array_map( function($k) use($self, $children, $indent) { if (is_array($children[$k])) { return sprintf( '%s%s => %s', str_repeat(' ', ($indent + 1) * 2), $k, $self->_astToString($children[$k], $indent + 1) ); } else { return sprintf( '%s%s => %s', str_repeat(' ', ($indent + 1) * 2), $k, $children[$k] ); } }, array_keys($children) ) ), sprintf('%s)', str_repeat(' ', $indent * 2)) ) ); } private function _defaultColorMap() { return array( 'integer' => 'light_green', 'float' => 'light_yellow', 'string' => 'light_red', 'bool' => 'light_purple', 'keyword' => 'light_cyan', 'comment' => 'dark_grey', 'default' => 'none' ); } private function _colorize($type, $value) { if (!empty($this->_colorMap[$type])) { $colorName = $this->_colorMap[$type]; } else { $colorName = $this->_colorMap['default']; } return sprintf( "%s%s\033[0m", static::$TERM_COLORS[$colorName], $value ); } private function _isSeen($value, $seen) { foreach ($seen as $v) { if ($v === $value) return true; } return false; } } ================================================ FILE: ThinkPHP/Library/Vendor/Boris/Config.php ================================================ _cascade = $cascade; $this->_searchPaths = $searchPaths; } /** * Searches for configuration files in the available * search paths, and applies them to the provided * boris instance. * * Returns true if any configuration files were found. * * @param Boris\Boris $boris * @return bool */ public function apply(Boris $boris) { $applied = false; foreach($this->_searchPaths as $path) { if (is_readable($path)) { $this->_loadInIsolation($path, $boris); $applied = true; $this->_files[] = $path; if (!$this->_cascade) { break; } } } return $applied; } /** * Returns an array of files that were loaded * for this Config * * @return array */ public function loadedFiles() { return $this->_files; } // -- Private Methods private function _loadInIsolation($path, $boris) { require $path; } } ================================================ FILE: ThinkPHP/Library/Vendor/Boris/DumpInspector.php ================================================ _socket = $socket; $this->_inspector = new DumpInspector(); stream_set_blocking($socket, 0); } /** * Set local variables to be placed in the workers's scope. * * @param array|string $local * @param mixed $value, if $local is a string */ public function setLocal($local, $value = null) { if (!is_array($local)) { $local = array($local => $value); } $this->_exports = array_merge($this->_exports, $local); } /** * Set hooks to run inside the worker before it starts looping. * * @param array $hooks */ public function setStartHooks($hooks) { $this->_startHooks = $hooks; } /** * Set hooks to run inside the worker after a fatal error is caught. * * @param array $hooks */ public function setFailureHooks($hooks) { $this->_failureHooks = $hooks; } /** * Set an Inspector object for Boris to output return values with. * * @param object $inspector any object the responds to inspect($v) */ public function setInspector($inspector) { $this->_inspector = $inspector; } /** * Start the worker. * * This method never returns. */ public function start() { $__scope = $this->_runHooks($this->_startHooks); extract($__scope); $this->_write($this->_socket, self::READY); /* Note the naming of the local variables due to shared scope with the user here */ for (;;) { declare(ticks = 1); // don't exit on ctrl-c pcntl_signal(SIGINT, SIG_IGN, true); $this->_cancelled = false; $__input = $this->_transform($this->_read($this->_socket)); if ($__input === null) { continue; } $__response = self::DONE; $this->_ppid = posix_getpid(); $this->_pid = pcntl_fork(); if ($this->_pid < 0) { throw new \RuntimeException('Failed to fork child labourer'); } elseif ($this->_pid > 0) { // kill the child on ctrl-c pcntl_signal(SIGINT, array($this, 'cancelOperation'), true); pcntl_waitpid($this->_pid, $__status); if (!$this->_cancelled && $__status != (self::ABNORMAL_EXIT << 8)) { $__response = self::EXITED; } else { $this->_runHooks($this->_failureHooks); $__response = self::FAILED; } } else { // user exception handlers normally cause a clean exit, so Boris will exit too if (!$this->_exceptionHandler = set_exception_handler(array($this, 'delegateExceptionHandler'))) { restore_exception_handler(); } // undo ctrl-c signal handling ready for user code execution pcntl_signal(SIGINT, SIG_DFL, true); $__pid = posix_getpid(); $__result = eval($__input); if (posix_getpid() != $__pid) { // whatever the user entered caused a forked child // (totally valid, but we don't want that child to loop and wait for input) exit(0); } if (preg_match('/\s*return\b/i', $__input)) { fwrite(STDOUT, sprintf("%s\n", $this->_inspector->inspect($__result))); } $this->_expungeOldWorker(); } $this->_write($this->_socket, $__response); if ($__response == self::EXITED) { exit(0); } } } /** * While a child process is running, terminate it immediately. */ public function cancelOperation() { printf("Cancelling...\n"); $this->_cancelled = true; posix_kill($this->_pid, SIGKILL); pcntl_signal_dispatch(); } /** * If any user-defined exception handler is present, call it, but be sure to exit correctly. */ public function delegateExceptionHandler($ex) { call_user_func($this->_exceptionHandler, $ex); exit(self::ABNORMAL_EXIT); } // -- Private Methods private function _runHooks($hooks) { extract($this->_exports); foreach ($hooks as $__hook) { if (is_string($__hook)) { eval($__hook); } elseif (is_callable($__hook)) { call_user_func($__hook, $this, get_defined_vars()); } else { throw new \RuntimeException( sprintf( 'Hooks must be closures or strings of PHP code. Got [%s].', gettype($__hook) ) ); } // hooks may set locals extract($this->_exports); } return get_defined_vars(); } private function _expungeOldWorker() { posix_kill($this->_ppid, SIGTERM); pcntl_signal_dispatch(); } private function _write($socket, $data) { if (!fwrite($socket, $data)) { throw new \RuntimeException('Socket error: failed to write data'); } } private function _read($socket) { $read = array($socket); $except = array($socket); if ($this->_select($read, $except) > 0) { if ($read) { return stream_get_contents($read[0]); } else if ($except) { throw new \UnexpectedValueException("Socket error: closed"); } } } private function _select(&$read, &$except) { $write = null; set_error_handler(function(){return true;}, E_WARNING); $result = stream_select($read, $write, $except, 10); restore_error_handler(); return $result; } private function _transform($input) { if ($input === null) { return null; } $transforms = array( 'exit' => 'exit(0)' ); foreach ($transforms as $from => $to) { $input = preg_replace('/^\s*' . preg_quote($from, '/') . '\s*;?\s*$/', $to . ';', $input); } return $input; } } ================================================ FILE: ThinkPHP/Library/Vendor/Boris/ExportInspector.php ================================================ _socket = $socket; } /** * Start the client with an prompt and readline history path. * * This method never returns. * * @param string $prompt * @param string $historyFile */ public function start($prompt, $historyFile) { readline_read_history($historyFile); declare(ticks = 1); pcntl_signal(SIGCHLD, SIG_IGN); pcntl_signal(SIGINT, array($this, 'clear'), true); // wait for the worker to finish executing hooks if (fread($this->_socket, 1) != EvalWorker::READY) { throw new \RuntimeException('EvalWorker failed to start'); } $parser = new ShallowParser(); $buf = ''; $lineno = 1; for (;;) { $this->_clear = false; $line = readline( sprintf( '[%d] %s', $lineno, ($buf == '' ? $prompt : str_pad('*> ', strlen($prompt), ' ', STR_PAD_LEFT)) ) ); if ($this->_clear) { $buf = ''; continue; } if (false === $line) { $buf = 'exit(0);'; // ctrl-d acts like exit } if (strlen($line) > 0) { readline_add_history($line); } $buf .= sprintf("%s\n", $line); if ($statements = $parser->statements($buf)) { ++$lineno; $buf = ''; foreach ($statements as $stmt) { if (false === $written = fwrite($this->_socket, $stmt)) { throw new \RuntimeException('Socket error: failed to write data'); } if ($written > 0) { $status = fread($this->_socket, 1); if ($status == EvalWorker::EXITED) { readline_write_history($historyFile); echo "\n"; exit(0); } elseif ($status == EvalWorker::FAILED) { break; } } } } } } /** * Clear the input buffer. */ public function clear() { // FIXME: I'd love to have this send \r to readline so it puts the user on a blank line $this->_clear = true; } } ================================================ FILE: ThinkPHP/Library/Vendor/Boris/ShallowParser.php ================================================ ')', '{' => '}', '[' => ']', '"' => '"', "'" => "'", '//' => "\n", '#' => "\n", '/*' => '*/', '<<<' => '_heredoc_special_case_' ); private $_initials; public function __construct() { $this->_initials = '/^(' . implode('|', array_map(array($this, 'quote'), array_keys($this->_pairs))) . ')/'; } /** * Break the $buffer into chunks, with one for each highest-level construct possible. * * If the buffer is incomplete, returns an empty array. * * @param string $buffer * * @return array */ public function statements($buffer) { $result = $this->_createResult($buffer); while (strlen($result->buffer) > 0) { $this->_resetResult($result); if ($result->state == '<<<') { if (!$this->_initializeHeredoc($result)) { continue; } } $rules = array('_scanEscapedChar', '_scanRegion', '_scanStateEntrant', '_scanWsp', '_scanChar'); foreach ($rules as $method) { if ($this->$method($result)) { break; } } if ($result->stop) { break; } } if (!empty($result->statements) && trim($result->stmt) === '' && strlen($result->buffer) == 0) { $this->_combineStatements($result); $this->_prepareForDebug($result); return $result->statements; } } public function quote($token) { return preg_quote($token, '/'); } // -- Private Methods private function _createResult($buffer) { $result = new \stdClass(); $result->buffer = $buffer; $result->stmt = ''; $result->state = null; $result->states = array(); $result->statements = array(); $result->stop = false; return $result; } private function _resetResult($result) { $result->stop = false; $result->state = end($result->states); $result->terminator = $result->state ? '/^(.*?' . preg_quote($this->_pairs[$result->state], '/') . ')/s' : null ; } private function _combineStatements($result) { $combined = array(); foreach ($result->statements as $scope) { if (trim($scope) == ';' || substr(trim($scope), -1) != ';') { $combined[] = ((string) array_pop($combined)) . $scope; } else { $combined[] = $scope; } } $result->statements = $combined; } private function _prepareForDebug($result) { $result->statements []= $this->_prepareDebugStmt(array_pop($result->statements)); } private function _initializeHeredoc($result) { if (preg_match('/^([\'"]?)([a-z_][a-z0-9_]*)\\1/i', $result->buffer, $match)) { $docId = $match[2]; $result->stmt .= $match[0]; $result->buffer = substr($result->buffer, strlen($match[0])); $result->terminator = '/^(.*?\n' . $docId . ');?\n/s'; return true; } else { return false; } } private function _scanWsp($result) { if (preg_match('/^\s+/', $result->buffer, $match)) { if (!empty($result->statements) && $result->stmt === '') { $result->statements[] = array_pop($result->statements) . $match[0]; } else { $result->stmt .= $match[0]; } $result->buffer = substr($result->buffer, strlen($match[0])); return true; } else { return false; } } private function _scanEscapedChar($result) { if (($result->state == '"' || $result->state == "'") && preg_match('/^[^' . $result->state . ']*?\\\\./s', $result->buffer, $match)) { $result->stmt .= $match[0]; $result->buffer = substr($result->buffer, strlen($match[0])); return true; } else { return false; } } private function _scanRegion($result) { if (in_array($result->state, array('"', "'", '<<<', '//', '#', '/*'))) { if (preg_match($result->terminator, $result->buffer, $match)) { $result->stmt .= $match[1]; $result->buffer = substr($result->buffer, strlen($match[1])); array_pop($result->states); } else { $result->stop = true; } return true; } else { return false; } } private function _scanStateEntrant($result) { if (preg_match($this->_initials, $result->buffer, $match)) { $result->stmt .= $match[0]; $result->buffer = substr($result->buffer, strlen($match[0])); $result->states[] = $match[0]; return true; } else { return false; } } private function _scanChar($result) { $chr = substr($result->buffer, 0, 1); $result->stmt .= $chr; $result->buffer = substr($result->buffer, 1); if ($result->state && $chr == $this->_pairs[$result->state]) { array_pop($result->states); } if (empty($result->states) && ($chr == ';' || $chr == '}')) { if (!$this->_isLambda($result->stmt) || $chr == ';') { $result->statements[] = $result->stmt; $result->stmt = ''; } } return true; } private function _isLambda($input) { return preg_match( '/^([^=]*?=\s*)?function\s*\([^\)]*\)\s*(use\s*\([^\)]*\)\s*)?\s*\{.*\}\s*;?$/is', trim($input) ); } private function _isReturnable($input) { $input = trim($input); if (substr($input, -1) == ';' && substr($input, 0, 1) != '{') { return $this->_isLambda($input) || !preg_match( '/^(' . 'echo|print|exit|die|goto|global|include|include_once|require|require_once|list|' . 'return|do|for|foreach|while|if|function|namespace|class|interface|abstract|switch|' . 'declare|throw|try|unset' . ')\b/i', $input ); } else { return false; } } private function _prepareDebugStmt($input) { if ($this->_isReturnable($input) && !preg_match('/^\s*return/i', $input)) { $input = sprintf('return %s', $input); } return $input; } } ================================================ FILE: ThinkPHP/Library/Vendor/EaseTemplate/template.core.php ================================================ Power by Ease Template!');}"; var $Compile = array(); var $Analysis = array(); var $Emc = array(); /** * 声明模板用法 */ function ETCoreStart( $set = array( 'ID' =>'1', //缓存ID 'TplType' =>'htm', //模板格式 'CacheDir' =>'cache', //缓存目录 'TemplateDir'=>'template' , //模板存放目录 'AutoImage' =>'on' , //自动解析图片目录开关 on表示开放 off表示关闭 'LangDir' =>'language' , //语言文件存放的目录 'Language' =>'default' , //语言的默认文件 'Copyright' =>'off' , //版权保护 'MemCache' =>'' , //Memcache服务器地址例如:127.0.0.1:11211 ) ){ $this->TplID = (defined('TemplateID')?TemplateID:( ((int)@$set['ID']<=1)?1:(int)$set['ID']) ).'_'; $this->CacheDir = (defined('NewCache')?NewCache:( (trim($set['CacheDir']) != '')?$set['CacheDir']:'cache') ).'/'; $this->TemplateDir = (defined('NewTemplate')?NewTemplate:( (trim($set['TemplateDir']) != '')?$set['TemplateDir']:'template') ).'/'; $this->Ext = (@$set['TplType'] != '')?$set['TplType']:'htm'; $this->AutoImage = (@$set['AutoImage']=='off')?0:1; $this->Copyright = (@$set['Copyright']=='off')?0:1; $this->Server = (is_array($GLOBALS['_SERVER']))?$GLOBALS['_SERVER']:$_SERVER; $this->version = (trim($_GET['EaseTemplateVer']))?die('Ease Templae E3!'):''; //载入语言文件 $this->LangDir = (defined('LangDir')?LangDir:( ((@$set['LangDir']!='language' && @$set['LangDir'])?$set['LangDir']:'language') )).'/'; if(is_dir($this->LangDir)){ $this->Language = (defined('Language')?Language:( (($set['Language']!='default' && $set['Language'])?$set['Language']:'default') )); if(@is_file($this->LangDir.$this->Language.'.php')){ $lang = array(); @include_once $this->LangDir.$this->Language.'.php'; $this->LangData = $lang; } }else{ $this->Language = 'default'; } //缓存目录检测以及运行模式 if(@ereg(':',$set['MemCache'])){ $this->RunType = 'MemCache'; $memset = explode(":",$set['MemCache']); $this->Emc = memcache_connect($memset[0], $memset[1]) OR die("Could not connect!"); }else{ $this->RunType = (@substr(@sprintf('%o', @fileperms($this->CacheDir)), -3)==777 && is_dir($this->CacheDir))?'Cache':'Replace'; } $CompileBasic = array( '/(\{\s*|)/eis', '//is', '//is', '//is', '//is', '//is', '//', '//is', '/(\{\s*|)/eis', '/(\{\s*|)/eis', '/(\{\s*|)/eis', '/(\{\s*|)/eis', '/(\{\s*|)\s*(.+?)\s*(\{|)/is', '/(\{\s*|)/is', '/\{([a-zA-Z0-9_\'\"\[\]\$]{1,100})\}/', ); $this->Compile = (is_array($this->Compile))?array_merge($this->Compile,$CompileBasic):$CompileBasic; $AnalysisBasic = array( '$this->inc_php("\\2")', '";if($ET_Del==true){echo"', '";if(\\2){echo"', '";}elseif(\\2){echo"', '";}else{echo"', '";}echo"', '";\$_i=0;foreach((array)\\1 AS \\3){\$_i++;echo"', '";\$_i=0;while(\\1){\$_i++;echo"', '$this->lang("\\2")', '$this->Row("\\2")', '$this->Color("\\2")', '$this->Dirs("\\2")', '";\\3;echo"', '";\\2;echo"', '";echo \$\\1;echo"', ); $this->Analysis = (is_array($this->Analysis))?array_merge($this->Analysis,$AnalysisBasic):$AnalysisBasic; } /** * 设置数值 * set_var(变量名或是数组,设置数值[数组不设置此值]); */ function set_var( $name, $value = '' ){ if (is_array($name)){ $this->ThisValue = @array_merge($this->ThisValue,$name); }else{ $this->ThisValue[$name] = $value; } } /** * 设置模板文件 * set_file(文件名,设置目录); */ function set_file( $FileName, $NewDir = '' ){ //当前模板名 $this->ThisFile = $FileName.'.'.$this->Ext; //目录地址检测 $this->FileDir[$this->ThisFile] = (trim($NewDir) != '')?$NewDir.'/':$this->TemplateDir; $this->IncFile[$FileName] = $this->FileDir[$this->ThisFile].$this->ThisFile; if(!is_file($this->IncFile[$FileName]) && $this->Copyright==1){ die('Sorry, The file '.$this->IncFile[$FileName].' does not exist.'); } //bug 系统 $this->IncList[] = $this->ThisFile; } //解析替换程序 function ParseCode( $FileList = '', $CacheFile = '' ){ //模板数据 $ShowTPL = ''; //解析续载 if (@is_array($FileList) && $FileList!='include_page'){ foreach ($FileList AS $K=>$V) { $ShowTPL .= $this->reader($V.$K); } }else{ //如果指定文件地址则载入 $SourceFile = ($FileList!='')?$FileList:$this->FileDir[$this->ThisFile].$this->ThisFile; if(!is_file($SourceFile) && $this->Copyright==1){ die('Sorry, The file '.$SourceFile.' does not exist.'); } $ShowTPL = $this->reader($SourceFile); } //引用模板处理 $ShowTPL = $this->inc_preg($ShowTPL); //检测run方法 $run = 0; if (eregi("run:",$ShowTPL)){ $run = 1; //Fix = $ShowTPL = preg_replace('/(\{|)\s*=/','{run:}echo ',$ShowTPL); $ShowTPL = preg_replace('/(\{|)\s*(.+?)\s*(\{|)/is', '(T_T)\\3;(T_T!)',$ShowTPL); } //Fix XML if (eregi("/is', '\\1', $ShowTPL); } //修复代码中\n换行错误 $ShowTPL = str_replace('\\','\\\\',$ShowTPL); //修复双引号问题 $ShowTPL = str_replace('"','\"',$ShowTPL); //编译运算 $ShowTPL = @preg_replace($this->Compile, $this->Analysis, $ShowTPL); //分析图片地址 $ShowTPL = $this->ImgCheck($ShowTPL); //Fix 模板中金钱符号 $ShowTPL = str_replace('$','\$',$ShowTPL); //修复php运行错误 $ShowTPL = @preg_replace("/\";(.+?)echo\"/e", '$this->FixPHP(\'\\1\')', $ShowTPL); //Fix Run 2 if ($run==1){ $ShowTPL = preg_replace("/\(T_T\)(.+?)\(T_T!\)/ise", '$this->FixPHP(\'\\1\')', $ShowTPL); } //还原xml $ShowTPL = (strrpos($ShowTPL,''))?@preg_replace('/ET>(.+?)<\/ET/is', '?\\1?', $ShowTPL):$ShowTPL; //修复"问题 $ShowTPL = str_replace('echo ""','echo "\"',$ShowTPL); //从数组中将变量导入到当前的符号表 @extract($this->Value()); ob_start(); ob_implicit_flush(0); @eval('echo "'.$ShowTPL.'";'); $contents = ob_get_contents(); ob_end_clean(); //Cache htm if($this->HtmID){ $this->writer($this->HtmDir.$this->HtmID,$this->Hacker."?>".$contents); } //编译模板 if ($this->RunType=='Cache'){ $this->CompilePHP($ShowTPL,$CacheFile); } //错误检查 if(strlen($contents)<=0){ //echo $ShowTPL; die('
Sorry, Error or complicated syntax error exists in '.$SourceFile.' file.'); } return $contents; } /** * 多语言 */ function lang( $str = '' ){ if (is_dir($this->LangDir)){ //采用MD5效验 $id = md5($str); //不存在数据则写入 if($this->LangData[$id]=='' && $this->Language=='default'){ //语言包文件 if (@is_file($this->LangDir.$this->Language.'.php')){ unset($lang); @include($this->LangDir.$this->Language.'.php'); } //如果检测到有数据则输出 if ($lang[$id]){ $out = str_replace('\\','\\\\',$lang[$id]); return str_replace('"','\"',$out); } //修复'多\问题 $str = str_replace("\\'","'",$str); //语言文件过大时采取建立新文件 if(strlen($docs)>400){ $this->writer($this->LangDir.$this->Language.'.'.$id.'.php',''); $docs= substr($str,0,40); //简要说明 $docs = str_replace('\"','"',$docs); $docs = str_replace('\\\\','\\',$docs); $str = 'o(O_O)o.ET Lang.o(*_*)o'; //语言新文件 }else{ $docs = str_replace('\"','"',$str); $docs = str_replace('\\\\','\\',$docs); } //文件安全处理 $data = (!is_file($this->LangDir.'default.php'))?"Language."\n*/\n\n\n":''; if (trim($str)){ //写入数据 $data .= "/**".date("Y.m.d",time())."\n"; $data.= $docs."\n"; $data.= "*/\n"; $data.= '$lang["'.$id.'"] = "'.$str.'";'."\n\n"; $this->writer($this->LangDir.'default.php',$data,'a+'); } } //单独语言文件包 if($this->LangData[$id]=='o(O_O)o.ET Lang.o(*_*)o'){ unset($etl); include($this->LangDir.$this->Language.".".$id.".php"); $this->LangData[$id] = $etl; } $out = ($this->LangData[$id])?$this->LangData[$id]:$str; //输出部分要做处理 if(($this->RunType=='Replace' || $this->RunType!='Replace') && $data==''){ $out = str_replace('\\','\\\\',$out); $out = str_replace('"','\"',$out); } return $out; }else{ return $str; } } /** * inc引用函数 */ function inc_preg( $content ){ return preg_replace('/<\!--\s*\#include\s*file\s*=(\"|\')([a-zA-Z0-9_\.\|]{1,100})(\"|\')\s*-->/eis', '$this->inc("\\2")', preg_replace('/(\{\s*|)/eis', '$this->inc("\\2")', $content)); } /** * 引用函数运算 */ function inc( $Files = '' ){ if($Files){ if (!strrpos($Files,$this->Ext)){ $Files = $Files.".".$this->Ext; } $FileLs = $this->TemplateDir.$Files; $contents =$this->ParseCode($FileLs,$Files); if($this->RunType=='Cache'){ //引用模板 $this->IncList[] = $Files; $cache_file = $this->CacheDir.$this->TplID.$Files.".".$this->Language.".php"; return " {inc_php:".$cache_file."} {run:@eval('echo \"'.\$EaseTemplate3_Cache.'\";')} "; }elseif($this->RunType=='MemCache'){ //cache date memcache_set($this->Emc,$Files.'_date', time()) OR die("Failed to save data at the server."); memcache_set($this->Emc,$Files, $contents) OR die("Failed to save data at the server"); return "".$contents; }else{ //引用模板 $this->IncList[] = $Files; return $contents; } } } /** * 编译解析处理 */ function CompilePHP( $content='', $cachename = '' ){ if ($content){ //如果没有安全文件则自动创建 if($this->RunType=='Cache' && !is_file($this->CacheDir.'index.htm')){ $Ease_name = 'Ease Template!'; $Ease_base = "$Ease_name$Ease_name"; $this->writer($this->CacheDir.'index.htm',$Ease_base); $this->writer($this->CacheDir.'index.html',$Ease_base); $this->writer($this->CacheDir.'default.htm',$Ease_base); } //编译记录 $content = str_replace("\\","\\\\",$content); $content = str_replace("'","\'",$content); $content = str_replace('echo"";',"",$content); //替换多余数据 $wfile = ($cachename)?$cachename:$this->ThisFile; $this->writer($this->FileName($wfile,$this->TplID) ,$this->Hacker.'$EaseTemplate3_Cache = \''.$content.'\';'); } } //修复PHP执行时产生的错误 function FixPHP( $content='' ){ $content = str_replace('\\\\','\\',$content); return '";'.str_replace('\\"','"',str_replace('\$','$',$content)).'echo"'; } /** * 检测缓存是否要更新 * filename 缓存文件名 * settime 指定事件则提供更新,只用于memcache */ function FileUpdate($filname,$settime=0){ //检测设置模板文件 if (is_array($this->IncFile)){ unset($k,$v); $update = 0; $settime = ($settime>0)?$settime:@filemtime($filname); foreach ($this->IncFile AS $k=>$v) { if (@filemtime($v)>$settime){$update = 1;} } //更新缓存 if($update==1){ return false; }else { return $filname; } }else{ return $filname; } } /** * 输出运算 * Filename 连载编译输出文件名 */ function output( $Filename = '' ){ switch($this->RunType){ //Mem编译模式 case'MemCache': if ($Filename=='include_page'){ //直接输出文件 return $this->reader($this->FileDir[$this->ThisFile].$this->ThisFile); }else{ $FileNames = ($Filename)?$Filename:$this->ThisFile; $CacheFile = $this->FileName($FileNames,$this->TplID); //检测记录时间 $updateT = memcache_get($this->Emc,$CacheFile.'_date'); $update = $this->FileUpdate($CacheFile,$updateT); $CacheData = memcache_get($this->Emc,$CacheFile); if(trim($CacheData) && $update){ //获得列表文件 unset($ks,$vs); preg_match_all('/<\!-- ET\_inc\_cache\[(.+?)\] -->/',$CacheData, $IncFile); if (is_array($IncFile[1])){ foreach ($IncFile[1] AS $ks=>$vs) { $this->IncList[] = $vs; $listDate = memcache_get($this->Emc,$vs.'_date'); echo @filemtime($this->TemplateDir.$vs).' - '.$listDate.'
'; //更新inc缓存 if (@filemtime($this->TemplateDir.$vs)>$listDate){ $update = 1; $this->inc($vs); } } //更新数据 if ($update == 1){ $CacheData = $this->ParseCode($this->FileList,$Filename); //cache date @memcache_set($this->Emc,$CacheFile.'_date', time()) OR die("Failed to save data at the server."); @memcache_set($this->Emc,$CacheFile, $CacheData) OR die("Failed to save data at the server."); } } //Close memcache_close($this->Emc); return $CacheData; }else{ if ($Filename){ $CacheData = $this->ParseCode($this->FileList,$Filename); //cache date @memcache_set($this->Emc,$CacheFile.'_date', time()) OR die("Failed to save data at the server."); @memcache_set($this->Emc,$CacheFile, $CacheData) OR die("Failed to save data at the server."); //Close memcache_close($this->Emc); return $CacheData; }else{ $CacheData = $this->ParseCode(); //cache date @memcache_set($this->Emc,$CacheFile.'_date', time()) OR die("Failed to save data at the server."); @memcache_set($this->Emc,$CacheFile, $CacheData) OR die("Failed to save data at the server2"); //Close memcache_close($this->Emc); return $CacheData; } } } break; //编译模式 case'Cache': if ($Filename=='include_page'){ //直接输出文件 return $this->reader($this->FileDir[$this->ThisFile].$this->ThisFile); }else{ $FileNames = ($Filename)?$Filename:$this->ThisFile; $CacheFile = $this->FileName($FileNames,$this->TplID); $CacheFile = $this->FileUpdate($CacheFile); if (@is_file($CacheFile)){ @extract($this->Value()); ob_start(); ob_implicit_flush(0); include $CacheFile; //获得列表文件 if($EaseTemplate3_Cache!=''){ unset($ks,$vs); preg_match_all('/<\!-- ET\_inc\_cache\[(.+?)\] -->/',$EaseTemplate3_Cache, $IncFile); if (is_array($IncFile[1])){ foreach ($IncFile[1] AS $ks=>$vs) { $this->IncList[] = $vs; //更新inc缓存 if (@filemtime($this->TemplateDir.$vs)>@filemtime($this->CacheDir.$this->TplID.$vs.'.'.$this->Language.'.php')){ $this->inc($vs); } } } @eval('echo "'.$EaseTemplate3_Cache.'";'); $contents = ob_get_contents(); ob_end_clean(); return $contents; } }else{ if ($Filename){ return $this->ParseCode($this->FileList,$Filename); }else{ return $this->ParseCode(); } } } break; //替换引擎 default: if($Filename){ if ($Filename=='include_page'){ //直接输出文件 return $this->reader($this->FileDir[$this->ThisFile].$this->ThisFile); }else { return $this->ParseCode($this->FileList); } }else{ return $this->ParseCode(); } } } /** * 连载函数 */ function n(){ //连载模板 $this->FileList[$this->ThisFile] = $this->FileDir[$this->ThisFile]; } /** * 输出模板内容 * Filename 连载编译输出文件名 */ function r( $Filename = '' ){ return $this->output($Filename); } /** * 打印模板内容 * Filename 连载编译输出文件名 */ function p( $Filename = '' ){ echo $this->output($Filename); } /** * 分析图片地址 */ function ImgCheck( $content ){ //Check Image Dir if($this->AutoImage==1){ $NewFileDir = $this->FileDir[$this->ThisFile]; //FIX img if(is_array($this->ImgDir)){ foreach($this->ImgDir AS $rep){ $rep = trim($rep); //检测是否执行替换 if(strrpos($content,$rep."/")){ if(substr($rep,-1)=='/'){ $rep = substr($rep,0,strlen($rep)-1); } $content = str_replace($rep.'/',$NewFileDir.$rep.'/',$content); } } } //FIX Dir $NewFileDirs = $NewFileDir.$NewFileDir; if(strrpos($content,$NewFileDirs)){ $content = str_replace($NewFileDirs,$NewFileDir,$content); } } return $content; } /** * 获得所有设置与公共变量 */ function Value(){ return (is_array($this->ThisValue))?array_merge($this->ThisValue,$GLOBALS):$GLOBALS; } /** * 清除设置 */ function clear(){ $this->RunType = 'Replace'; } /** * 静态文件写入 */ function htm_w( $w_dir = '', $w_filename = '', $w_content = '' ){ $dvs = ''; if($w_dir && $w_filename && $w_content){ //目录检测数量 $w_dir_ex = explode('/',$w_dir); $w_new_dir = ''; //处理后的写入目录 unset($dvs,$fdk,$fdv,$w_dir_len); foreach((array)$w_dir_ex AS $dvs){ if(trim($dvs) && $dvs!='..'){ $w_dir_len .= '../'; $w_new_dir .= $dvs.'/'; if (!@is_dir($w_new_dir)) @mkdir($w_new_dir, 0777); } } //获得需要更改的目录数 foreach((array)$this->FileDir AS $fdk=>$fdv){ $w_content = str_replace($fdv,$w_dir_len.str_replace('../','',$fdv),$w_content); } $this->writer($w_dir.$w_filename,$w_content); } } /** * 改变静态刷新时间 */ function htm_time($times=0){ if((int)$times>0){ $this->HtmTime = (int)$times; } } /** * 静态文件存放的绝对目录 */ function htm_dir($Name = ''){ if(trim($Name)){ $this->HtmDir = trim($Name).'/'; } } /** * 产生静态文件输出 */ function HtmCheck( $Name = '' ){ $this->HtmID = md5(trim($Name)? trim($Name).'.php' : $this->Server['REQUEST_URI'].'.php' ); //检测时间 if(is_file($this->HtmDir.$this->HtmID) && (time() - @filemtime($this->HtmDir.$this->HtmID)<=$this->HtmTime)){ ob_start(); ob_implicit_flush(0); include $this->HtmDir.$this->HtmID; $HtmContent = ob_get_contents(); ob_end_clean(); return $HtmContent; } } /** * 打印静态内容 */ function htm_p( $Name = '' ){ $output = $this->HtmCheck($Name); if ($output){ die($this->HtmCheck($Name)); } } /** * 输出静态内容 */ function htm_r( $Name = '' ){ return $this->HtmCheck($Name); } /** * 解析文件 */ function FileName( $name, $id = '1' ){ $extdir = explode("/",$name); $dircnt = @count($extdir) - 1; $extdir[$dircnt] = $id.$extdir[$dircnt]; return $this->CacheDir.implode("_",$extdir).".".$this->Language.'.php'; } /** * 检测引入文件 */ function inc_php( $url = '' ){ $parse = parse_url($url); unset($vals,$code_array); foreach((array)explode('&',$parse['query']) AS $vals){ $code_array .= preg_replace('/(.+)=(.+)/',"\$_GET['\\1']= \$\\1 ='\\2';",$vals); } return '";'.$code_array.' @include(\''.$parse['path'].'\');echo"'; } /** * 换行函数 * Row(换行数,换行颜色); * Row("5,#ffffff:#e1e1e1"); */ function Row( $Num = '' ){ $Num = trim($Num); if($Num != ''){ $Nums = explode(",",$Num); $Numr = ((int)$Nums[0]>0)?(int)$Nums[0]:2; $input = (trim($Nums[1]) == '')?'':$Nums[1]; if(trim($Nums[1]) != ''){ $Co = explode(":",$Nums[1]); $OutStr = "if(\$_i%$Numr===0){\$row_count++;echo(\$row_count%2===0)?'':'';}"; }else{ $OutStr = "if(\$_i%$Numr===0){echo '$input';}"; } return '";'.$OutStr.'echo "'; } } /** * 间隔变色 * Color(两组颜色代码); * Color('#FFFFFF,#DCDCDC'); */ function Color( $color = '' ){ if($color != ''){ $OutStr = preg_replace("/(.+),(.+)/","_i%2===0)?'\\1':'\\2';",$color); if(strrpos($OutStr,"%2")){ return '";echo(\$'.$OutStr.'echo "'; } } } /** * 映射图片地址 */ function Dirs( $adds = '' ){ $adds_ary = explode(",",$adds); if(is_array($adds_ary)){ $this->ImgDir = (is_array($this->ImgDir))?@array_merge($adds_ary, $this->ImgDir):$adds_ary; } } /** * 读取函数 * reader(文件名); */ function reader( $filename ){ $get_fun = @get_defined_functions(); return (in_array('file_get_contents',$get_fun['internal']))?@file_get_contents($filename):@implode("", @file($filename)); } /** * 写入函数 * writer(文件名,写入数据, 写入数据方式); */ function writer( $filename, $data = '', $mode='w' ){ if(trim($filename)){ $file = @fopen($filename, $mode); $filedata = @fwrite($file, $data); @fclose($file); } if(!is_file($filename)){ die('Sorry,'.$filename.' file write in failed!'); } } /** * 引入模板系统 * 察看当前使用的模板以及调试信息 */ function inc_list(){ if(is_array($this->IncList)){ $EXTS = explode("/",$this->Server['REQUEST_URI']); $Last = count($EXTS) -1; //处理清除工作 START if(strrpos($EXTS[$Last],'Ease_Templatepage=Clear') && trim($EXTS[$Last]) != ''){ $dir_name = $this->CacheDir; if(file_exists($dir_name)){ $handle=@opendir($dir_name); while($tmp_file=@readdir($handle)){ if(@file_exists($dir_name.$tmp_file)){ @unlink($dir_name.$tmp_file); } } @closedir($handle); } $GoURL = urldecode(preg_replace("/.+?REFERER=(.+?)!!!/","\\1",$EXTS[$Last])); die(''); } //处理清除工作 END $list_file = array(); $file_nums = count($this->IncList); $AllSize = 0; foreach($this->IncList AS $Ks=>$Vs){ $FSize[$Ks] = @filesize($this->TemplateDir.$Vs); $AllSize += $FSize[$Ks]; } foreach($this->IncList AS $K=>$V){ $File_Size = @round($FSize[$K] / 1024 * 100) / 100 . 'KB'; $Fwidth = @floor(100*$FSize[$K]/$AllSize); $list_file[] = "".$this->TemplateDir.$V." ".$File_Size."
"; } //连接地址 $BackURL = preg_replace("/.+\//","\\1",$this->Server['REQUEST_URI']); $NowPAGE = 'http://'.$this->Server['HTTP_HOST'].$this->Server['SCRIPT_NAME']; $clear_link = $NowPAGE."?Ease_Templatepage=Clear&REFERER=".urlencode($BackURL)."!!!"; $sf13 = ' style="font-size:13px;color:#666666"'; echo '
'.implode("",$list_file)."
Include Templates (Num:'.count($this-> IncList).') '; if($this->RunType=='Cache'){ echo '[Clear Cache]'; } echo '
Cache File ID: '.substr($this->TplID,0,-1).' Index: '.((count($this->FileList)==0)?'False':'True').' Format: '.$this->Ext.' Cache: '.($this->RunType=='MemCache'?'Memcache Engine':($this->RunType == 'Replace'?'Replace Engine':$this->CacheDir)).' Template: '.$this->TemplateDir.'
"; } } } ?> ================================================ FILE: ThinkPHP/Library/Vendor/EaseTemplate/template.ease.php ================================================ '1', //缓存ID 'TplType' =>'htm', //模板格式 'CacheDir' =>'cache', //缓存目录 'TemplateDir'=>'template' , //模板存放目录 'AutoImage' =>'on' , //自动解析图片目录开关 on表示开放 off表示关闭 'LangDir' =>'language' , //语言文件存放的目录 'Language' =>'default' , //语言的默认文件 'Copyright' =>'off' , //版权保护 'MemCache' =>'' , //Memcache服务器地址例如:127.0.0.1:11211 ) ){ parent::ETCoreStart($set); } } ?> ================================================ FILE: ThinkPHP/Library/Vendor/Hprose/HproseClassManager.php ================================================ * * * \**********************************************************/ class HproseClassManager { private static $classCache1 = array(); private static $classCache2 = array(); public static function register($class, $alias) { self::$classCache1[$alias] = $class; self::$classCache2[$class] = $alias; } public static function getClassAlias($class) { if (array_key_exists($class, self::$classCache2)) { return self::$classCache2[$class]; } $alias = str_replace('\\', '_', $class); self::register($class, $alias); return $alias; } public static function getClass($alias) { if (array_key_exists($alias, self::$classCache1)) { return self::$classCache1[$alias]; } if (!class_exists($alias)) { $class = str_replace('_', '\\', $alias); if (class_exists($class)) { self::register($class, $alias); return $class; } eval("class " . $alias . " { }"); } return $alias; } } ?> ================================================ FILE: ThinkPHP/Library/Vendor/Hprose/HproseClient.php ================================================ * * * \**********************************************************/ require_once('HproseCommon.php'); require_once('HproseIO.php'); abstract class HproseClient { protected $url; private $filter; private $simple; protected abstract function send($request); public function __construct($url = '') { $this->useService($url); $this->filter = NULL; $this->simple = false; } public function useService($url = '', $namespace = '') { if ($url) { $this->url = $url; } return new HproseProxy($this, $namespace); } public function invoke($functionName, &$arguments = array(), $byRef = false, $resultMode = HproseResultMode::Normal, $simple = NULL) { if ($simple === NULL) $simple = $this->simple; $stream = new HproseStringStream(HproseTags::TagCall); $hproseWriter = ($simple ? new HproseSimpleWriter($stream) : new HproseWriter($stream)); $hproseWriter->writeString($functionName); if (count($arguments) > 0 || $byRef) { $hproseWriter->reset(); $hproseWriter->writeList($arguments); if ($byRef) { $hproseWriter->writeBoolean(true); } } $stream->write(HproseTags::TagEnd); $request = $stream->toString(); if ($this->filter) $request = $this->filter->outputFilter($request); $stream->close(); $response = $this->send($request); if ($this->filter) $response = $this->filter->inputFilter($response); if ($resultMode == HproseResultMode::RawWithEndTag) { return $response; } if ($resultMode == HproseResultMode::Raw) { return substr($response, 0, -1); } $stream = new HproseStringStream($response); $hproseReader = new HproseReader($stream); $result = NULL; while (($tag = $hproseReader->checkTags( array(HproseTags::TagResult, HproseTags::TagArgument, HproseTags::TagError, HproseTags::TagEnd))) !== HproseTags::TagEnd) { switch ($tag) { case HproseTags::TagResult: if ($resultMode == HproseResultMode::Serialized) { $result = $hproseReader->readRaw()->toString(); } else { $hproseReader->reset(); $result = &$hproseReader->unserialize(); } break; case HproseTags::TagArgument: $hproseReader->reset(); $args = &$hproseReader->readList(true); for ($i = 0; $i < count($arguments); $i++) { $arguments[$i] = &$args[$i]; } break; case HproseTags::TagError: $hproseReader->reset(); throw new HproseException($hproseReader->readString(true)); break; } } return $result; } public function getFilter() { return $this->filter; } public function setFilter($filter) { $this->filter = $filter; } public function getSimpleMode() { return $this->simple; } public function setSimpleMode($simple = true) { $this->simple = $simple; } public function __call($function, $arguments) { return $this->invoke($function, $arguments); } public function __get($name) { return new HproseProxy($this, $name . '_'); } } class HproseProxy { private $client; private $namespace; public function __construct($client, $namespace = '') { $this->client = $client; $this->namespace = $namespace; } public function __call($function, $arguments) { $function = $this->namespace . $function; return $this->client->invoke($function, $arguments); } public function __get($name) { return new HproseProxy($this->client, $this->namespace . $name . '_'); } } ?> ================================================ FILE: ThinkPHP/Library/Vendor/Hprose/HproseCommon.php ================================================ * * * \**********************************************************/ class HproseResultMode { const Normal = 0; const Serialized = 1; const Raw = 2; const RawWithEndTag = 3; } class HproseException extends Exception {} interface HproseFilter { function inputFilter($data); function outputFilter($data); } class HproseDate { public $year; public $month; public $day; public $utc = false; public function __construct() { $args_num = func_num_args(); $args = func_get_args(); switch ($args_num) { case 0: $time = getdate(); $this->year = $time['year']; $this->month = $time['mon']; $this->day = $time['mday']; break; case 1: $time = false; if (is_int($args[0])) { $time = getdate($args[0]); } elseif (is_string($args[0])) { $time = getdate(strtotime($args[0])); } if (is_array($time)) { $this->year = $time['year']; $this->month = $time['mon']; $this->day = $time['mday']; } elseif ($args[0] instanceof HproseDate) { $this->year = $args[0]->year; $this->month = $args[0]->month; $this->day = $args[0]->day; } else { throw new HproseException('Unexpected arguments'); } break; case 4: $this->utc = $args[3]; case 3: if (!self::isValidDate($args[0], $args[1], $args[2])) { throw new HproseException('Unexpected arguments'); } $this->year = $args[0]; $this->month = $args[1]; $this->day = $args[2]; break; default: throw new HproseException('Unexpected arguments'); } } public function addDays($days) { if (!is_int($days)) return false; $year = $this->year; if ($days == 0) return true; if ($days >= 146097 || $days <= -146097) { $remainder = $days % 146097; if ($remainder < 0) { $remainder += 146097; } $years = 400 * (int)(($days - $remainder) / 146097); $year += $years; if ($year < 1 || $year > 9999) return false; $days = $remainder; } if ($days >= 36524 || $days <= -36524) { $remainder = $days % 36524; if ($remainder < 0) { $remainder += 36524; } $years = 100 * (int)(($days - $remainder) / 36524); $year += $years; if ($year < 1 || $year > 9999) return false; $days = $remainder; } if ($days >= 1461 || $days <= -1461) { $remainder = $days % 1461; if ($remainder < 0) { $remainder += 1461; } $years = 4 * (int)(($days - $remainder) / 1461); $year += $years; if ($year < 1 || $year > 9999) return false; $days = $remainder; } $month = $this->month; while ($days >= 365) { if ($year >= 9999) return false; if ($month <= 2) { if ((($year % 4) == 0) ? (($year % 100) == 0) ? (($year % 400) == 0) : true : false) { $days -= 366; } else { $days -= 365; } $year++; } else { $year++; if ((($year % 4) == 0) ? (($year % 100) == 0) ? (($year % 400) == 0) : true : false) { $days -= 366; } else { $days -= 365; } } } while ($days < 0) { if ($year <= 1) return false; if ($month <= 2) { $year--; if ((($year % 4) == 0) ? (($year % 100) == 0) ? (($year % 400) == 0) : true : false) { $days += 366; } else { $days += 365; } } else { if ((($year % 4) == 0) ? (($year % 100) == 0) ? (($year % 400) == 0) : true : false) { $days += 366; } else { $days += 365; } $year--; } } $daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year); $day = $this->day; while ($day + $days > $daysInMonth) { $days -= $daysInMonth - $day + 1; $month++; if ($month > 12) { if ($year >= 9999) return false; $year++; $month = 1; } $day = 1; $daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year); } $day += $days; $this->year = $year; $this->month = $month; $this->day = $day; return true; } public function addMonths($months) { if (!is_int($months)) return false; if ($months == 0) return true; $month = $this->month + $months; $months = ($month - 1) % 12 + 1; if ($months < 1) { $months += 12; } $years = (int)(($month - $months) / 12); if ($this->addYears($years)) { $daysInMonth = cal_days_in_month(CAL_GREGORIAN, $months, $this->year); if ($this->day > $daysInMonth) { $months++; $this->day -= $daysInMonth; } $this->month = (int)$months; return true; } else { return false; } } public function addYears($years) { if (!is_int($years)) return false; if ($years == 0) return true; $year = $this->year + $years; if ($year < 1 || $year > 9999) return false; $this->year = $year; return true; } public function timestamp() { if ($this->utc) { return gmmktime(0, 0, 0, $this->month, $this->day, $this->year); } else { return mktime(0, 0, 0, $this->month, $this->day, $this->year); } } public function toString($fullformat = true) { $format = ($fullformat ? '%04d-%02d-%02d': '%04d%02d%02d'); $str = sprintf($format, $this->year, $this->month, $this->day); if ($this->utc) { $str .= 'Z'; } return $str; } public function __toString() { return $this->toString(); } public static function isLeapYear($year) { return (($year % 4) == 0) ? (($year % 100) == 0) ? (($year % 400) == 0) : true : false; } public static function daysInMonth($year, $month) { if (($month < 1) || ($month > 12)) { return false; } return cal_days_in_month(CAL_GREGORIAN, $month, $year); } public static function isValidDate($year, $month, $day) { if (($year >= 1) && ($year <= 9999)) { return checkdate($month, $day, $year); } return false; } public function dayOfWeek() { $num = func_num_args(); if ($num == 3) { $args = func_get_args(); $y = $args[0]; $m = $args[1]; $d = $args[2]; } else { $y = $this->year; $m = $this->month; $d = $this->day; } $d += $m < 3 ? $y-- : $y - 2; return ((int)(23 * $m / 9) + $d + 4 + (int)($y / 4) - (int)($y / 100) + (int)($y / 400)) % 7; } public function dayOfYear() { static $daysToMonth365 = array(0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365); static $daysToMonth366 = array(0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366); $num = func_num_args(); if ($num == 3) { $args = func_get_args(); $y = $args[0]; $m = $args[1]; $d = $args[2]; } else { $y = $this->year; $m = $this->month; $d = $this->day; } $days = self::isLeapYear($y) ? $daysToMonth365 : $daysToMonth366; return $days[$m - 1] + $d; } } class HproseTime { public $hour; public $minute; public $second; public $microsecond = 0; public $utc = false; public function __construct() { $args_num = func_num_args(); $args = func_get_args(); switch ($args_num) { case 0: $time = getdate(); $timeofday = gettimeofday(); $this->hour = $time['hours']; $this->minute = $time['minutes']; $this->second = $time['seconds']; $this->microsecond = $timeofday['usec']; break; case 1: $time = false; if (is_int($args[0])) { $time = getdate($args[0]); } elseif (is_string($args[0])) { $time = getdate(strtotime($args[0])); } if (is_array($time)) { $this->hour = $time['hours']; $this->minute = $time['minutes']; $this->second = $time['seconds']; } elseif ($args[0] instanceof HproseTime) { $this->hour = $args[0]->hour; $this->minute = $args[0]->minute; $this->second = $args[0]->second; $this->microsecond = $args[0]->microsecond; } else { throw new HproseException('Unexpected arguments'); } break; case 5: $this->utc = $args[4]; case 4: if (($args[3] < 0) || ($args[3] > 999999)) { throw new HproseException('Unexpected arguments'); } $this->microsecond = $args[3]; case 3: if (!self::isValidTime($args[0], $args[1], $args[2])) { throw new HproseException('Unexpected arguments'); } $this->hour = $args[0]; $this->minute = $args[1]; $this->second = $args[2]; break; default: throw new HproseException('Unexpected arguments'); } } public function timestamp() { if ($this->utc) { return gmmktime($this->hour, $this->minute, $this->second) + ($this->microsecond / 1000000); } else { return mktime($this->hour, $this->minute, $this->second) + ($this->microsecond / 1000000); } } public function toString($fullformat = true) { if ($this->microsecond == 0) { $format = ($fullformat ? '%02d:%02d:%02d': '%02d%02d%02d'); $str = sprintf($format, $this->hour, $this->minute, $this->second); } if ($this->microsecond % 1000 == 0) { $format = ($fullformat ? '%02d:%02d:%02d.%03d': '%02d%02d%02d.%03d'); $str = sprintf($format, $this->hour, $this->minute, $this->second, (int)($this->microsecond / 1000)); } else { $format = ($fullformat ? '%02d:%02d:%02d.%06d': '%02d%02d%02d.%06d'); $str = sprintf($format, $this->hour, $this->minute, $this->second, $this->microsecond); } if ($this->utc) { $str .= 'Z'; } return $str; } public function __toString() { return $this->toString(); } public static function isValidTime($hour, $minute, $second, $microsecond = 0) { return !(($hour < 0) || ($hour > 23) || ($minute < 0) || ($minute > 59) || ($second < 0) || ($second > 59) || ($microsecond < 0) || ($microsecond > 999999)); } } class HproseDateTime extends HproseDate { public $hour; public $minute; public $second; public $microsecond = 0; public function __construct() { $args_num = func_num_args(); $args = func_get_args(); switch ($args_num) { case 0: $time = getdate(); $timeofday = gettimeofday(); $this->year = $time['year']; $this->month = $time['mon']; $this->day = $time['mday']; $this->hour = $time['hours']; $this->minute = $time['minutes']; $this->second = $time['seconds']; $this->microsecond = $timeofday['usec']; break; case 1: $time = false; if (is_int($args[0])) { $time = getdate($args[0]); } elseif (is_string($args[0])) { $time = getdate(strtotime($args[0])); } if (is_array($time)) { $this->year = $time['year']; $this->month = $time['mon']; $this->day = $time['mday']; $this->hour = $time['hours']; $this->minute = $time['minutes']; $this->second = $time['seconds']; } elseif ($args[0] instanceof HproseDate) { $this->year = $args[0]->year; $this->month = $args[0]->month; $this->day = $args[0]->day; $this->hour = 0; $this->minute = 0; $this->second = 0; } elseif ($args[0] instanceof HproseTime) { $this->year = 1970; $this->month = 1; $this->day = 1; $this->hour = $args[0]->hour; $this->minute = $args[0]->minute; $this->second = $args[0]->second; $this->microsecond = $args[0]->microsecond; } elseif ($args[0] instanceof HproseDateTime) { $this->year = $args[0]->year; $this->month = $args[0]->month; $this->day = $args[0]->day; $this->hour = $args[0]->hour; $this->minute = $args[0]->minute; $this->second = $args[0]->second; $this->microsecond = $args[0]->microsecond; } else { throw new HproseException('Unexpected arguments'); } break; case 2: if (($args[0] instanceof HproseDate) && ($args[1] instanceof HproseTime)) { $this->year = $args[0]->year; $this->month = $args[0]->month; $this->day = $args[0]->day; $this->hour = $args[1]->hour; $this->minute = $args[1]->minute; $this->second = $args[1]->second; $this->microsecond = $args[1]->microsecond; } else { throw new HproseException('Unexpected arguments'); } break; case 3: if (!self::isValidDate($args[0], $args[1], $args[2])) { throw new HproseException('Unexpected arguments'); } $this->year = $args[0]; $this->month = $args[1]; $this->day = $args[2]; $this->hour = 0; $this->minute = 0; $this->second = 0; break; case 8: $this->utc = $args[7]; case 7: if (($args[6] < 0) || ($args[6] > 999999)) { throw new HproseException('Unexpected arguments'); } $this->microsecond = $args[6]; case 6: if (!self::isValidDate($args[0], $args[1], $args[2])) { throw new HproseException('Unexpected arguments'); } if (!self::isValidTime($args[3], $args[4], $args[5])) { throw new HproseException('Unexpected arguments'); } $this->year = $args[0]; $this->month = $args[1]; $this->day = $args[2]; $this->hour = $args[3]; $this->minute = $args[4]; $this->second = $args[5]; break; default: throw new HproseException('Unexpected arguments'); } } public function addMicroseconds($microseconds) { if (!is_int($microseconds)) return false; if ($microseconds == 0) return true; $microsecond = $this->microsecond + $microseconds; $microseconds = $microsecond % 1000000; if ($microseconds < 0) { $microseconds += 1000000; } $seconds = (int)(($microsecond - $microseconds) / 1000000); if ($this->addSeconds($seconds)) { $this->microsecond = (int)$microseconds; return true; } else { return false; } } public function addSeconds($seconds) { if (!is_int($seconds)) return false; if ($seconds == 0) return true; $second = $this->second + $seconds; $seconds = $second % 60; if ($seconds < 0) { $seconds += 60; } $minutes = (int)(($second - $seconds) / 60); if ($this->addMinutes($minutes)) { $this->second = (int)$seconds; return true; } else { return false; } } public function addMinutes($minutes) { if (!is_int($minutes)) return false; if ($minutes == 0) return true; $minute = $this->minute + $minutes; $minutes = $minute % 60; if ($minutes < 0) { $minutes += 60; } $hours = (int)(($minute - $minutes) / 60); if ($this->addHours($hours)) { $this->minute = (int)$minutes; return true; } else { return false; } } public function addHours($hours) { if (!is_int($hours)) return false; if ($hours == 0) return true; $hour = $this->hour + $hours; $hours = $hour % 24; if ($hours < 0) { $hours += 24; } $days = (int)(($hour - $hours) / 24); if ($this->addDays($days)) { $this->hour = (int)$hours; return true; } else { return false; } } public function after($when) { if (!($when instanceof HproseDateTime)) { $when = new HproseDateTime($when); } if ($this->utc != $when->utc) return ($this->timestamp() > $when->timestamp()); if ($this->year < $when->year) return false; if ($this->year > $when->year) return true; if ($this->month < $when->month) return false; if ($this->month > $when->month) return true; if ($this->day < $when->day) return false; if ($this->day > $when->day) return true; if ($this->hour < $when->hour) return false; if ($this->hour > $when->hour) return true; if ($this->minute < $when->minute) return false; if ($this->minute > $when->minute) return true; if ($this->second < $when->second) return false; if ($this->second > $when->second) return true; if ($this->microsecond < $when->microsecond) return false; if ($this->microsecond > $when->microsecond) return true; return false; } public function before($when) { if (!($when instanceof HproseDateTime)) { $when = new HproseDateTime($when); } if ($this->utc != $when->utc) return ($this->timestamp() < $when->timestamp()); if ($this->year < $when->year) return true; if ($this->year > $when->year) return false; if ($this->month < $when->month) return true; if ($this->month > $when->month) return false; if ($this->day < $when->day) return true; if ($this->day > $when->day) return false; if ($this->hour < $when->hour) return true; if ($this->hour > $when->hour) return false; if ($this->minute < $when->minute) return true; if ($this->minute > $when->minute) return false; if ($this->second < $when->second) return true; if ($this->second > $when->second) return false; if ($this->microsecond < $when->microsecond) return true; if ($this->microsecond > $when->microsecond) return false; return false; } public function equals($when) { if (!($when instanceof HproseDateTime)) { $when = new HproseDateTime($when); } if ($this->utc != $when->utc) return ($this->timestamp() == $when->timestamp()); return (($this->year == $when->year) && ($this->month == $when->month) && ($this->day == $when->day) && ($this->hour == $when->hour) && ($this->minute == $when->minute) && ($this->second == $when->second) && ($this->microsecond == $when->microsecond)); } public function timestamp() { if ($this->utc) { return gmmktime($this->hour, $this->minute, $this->second, $this->month, $this->day, $this->year) + ($this->microsecond / 1000000); } else { return mktime($this->hour, $this->minute, $this->second, $this->month, $this->day, $this->year) + ($this->microsecond / 1000000); } } public function toString($fullformat = true) { if ($this->microsecond == 0) { $format = ($fullformat ? '%04d-%02d-%02dT%02d:%02d:%02d' : '%04d%02d%02dT%02d%02d%02d'); $str = sprintf($format, $this->year, $this->month, $this->day, $this->hour, $this->minute, $this->second); } if ($this->microsecond % 1000 == 0) { $format = ($fullformat ? '%04d-%02d-%02dT%02d:%02d:%02d.%03d' : '%04d%02d%02dT%02d%02d%02d.%03d'); $str = sprintf($format, $this->year, $this->month, $this->day, $this->hour, $this->minute, $this->second, (int)($this->microsecond / 1000)); } else { $format = ($fullformat ? '%04d-%02d-%02dT%02d:%02d:%02d.%06d' : '%04d%02d%02dT%02d%02d%02d.%06d'); $str = sprintf($format, $this->year, $this->month, $this->day, $this->hour, $this->minute, $this->second, $this->microsecond); } if ($this->utc) { $str .= 'Z'; } return $str; } public function __toString() { return $this->toString(); } public static function isValidTime($hour, $minute, $second, $microsecond = 0) { return HproseTime::isValidTime($hour, $minute, $second, $microsecond); } } /* integer is_utf8(string $s) if $s is UTF-8 String, return 1 else 0 */ if (function_exists('mb_detect_encoding')) { function is_utf8($s) { return mb_detect_encoding($s, 'UTF-8', true) === 'UTF-8'; } } elseif (function_exists('iconv')) { function is_utf8($s) { return iconv('UTF-8', 'UTF-8//IGNORE', $s) === $s; } } else { function is_utf8($s) { $len = strlen($s); for($i = 0; $i < $len; ++$i){ $c = ord($s{$i}); switch ($c >> 4) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: break; case 12: case 13: if ((ord($s{++$i}) >> 6) != 0x2) return false; break; case 14: if ((ord($s{++$i}) >> 6) != 0x2) return false; if ((ord($s{++$i}) >> 6) != 0x2) return false; break; case 15: $b = $s{++$i}; if ((ord($b) >> 6) != 0x2) return false; if ((ord($s{++$i}) >> 6) != 0x2) return false; if ((ord($s{++$i}) >> 6) != 0x2) return false; if (((($c & 0xf) << 2) | (($b >> 4) & 0x3)) > 0x10) return false; break; default: return false; } } return true; } } /* integer ustrlen(string $s) $s must be a UTF-8 String, return the Unicode code unit (not code point) length */ if (function_exists('iconv')) { function ustrlen($s) { return strlen(iconv('UTF-8', 'UTF-16LE', $s)) >> 1; } } elseif (function_exists('mb_convert_encoding')) { function ustrlen($s) { return strlen(mb_convert_encoding($s, "UTF-16LE", "UTF-8")) >> 1; } } else { function ustrlen($s) { $pos = 0; $length = strlen($s); $len = $length; while ($pos < $length) { $a = ord($s{$pos++}); if ($a < 0x80) { continue; } elseif (($a & 0xE0) == 0xC0) { ++$pos; --$len; } elseif (($a & 0xF0) == 0xE0) { $pos += 2; $len -= 2; } elseif (($a & 0xF8) == 0xF0) { $pos += 3; $len -= 2; } } return $len; } } /* bool is_list(array $a) if $a is list, return true else false */ function is_list(array $a) { $count = count($a); if ($count === 0) return true; return !array_diff_key($a, array_fill(0, $count, NULL)); } /* mixed array_ref_search(mixed &$value, array $array) if $value ref in $array, return the index else false */ function array_ref_search(&$value, &$array) { if (!is_array($value)) return array_search($value, $array, true); $temp = $value; foreach ($array as $i => &$ref) { if (($ref === ($value = 1)) && ($ref === ($value = 0))) { $value = $temp; return $i; } } $value = $temp; return false; } /* string spl_object_hash(object $obj) This function returns a unique identifier for the object. This id can be used as a hash key for storing objects or for identifying an object. */ if (!function_exists('spl_object_hash')) { function spl_object_hash($object) { ob_start(); var_dump($object); preg_match('[#(\d+)]', ob_get_clean(), $match); return $match[1]; } } ?> ================================================ FILE: ThinkPHP/Library/Vendor/Hprose/HproseFormatter.php ================================================ * * * \**********************************************************/ require_once('HproseIOStream.php'); require_once('HproseReader.php'); require_once('HproseWriter.php'); class HproseFormatter { public static function serialize(&$var, $simple = false) { $stream = new HproseStringStream(); $hproseWriter = ($simple ? new HproseSimpleWriter($stream) : new HproseWriter($stream)); $hproseWriter->serialize($var); return $stream->toString(); } public static function &unserialize($data, $simple = false) { $stream = new HproseStringStream($data); $hproseReader = ($simple ? new HproseSimpleReader($stream) : new HproseReader($stream)); return $hproseReader->unserialize(); } } ?> ================================================ FILE: ThinkPHP/Library/Vendor/Hprose/HproseHttpClient.php ================================================ * * * \**********************************************************/ require_once('HproseCommon.php'); require_once('HproseIO.php'); require_once('HproseClient.php'); abstract class HproseBaseHttpClient extends HproseClient { protected $host; protected $path; protected $secure; protected $proxy; protected $header; protected $timeout; protected $keepAlive; protected $keepAliveTimeout; protected static $cookieManager = array(); static function hproseKeepCookieInSession() { $_SESSION['HPROSE_COOKIE_MANAGER'] = self::$cookieManager; } public static function keepSession() { if (array_key_exists('HPROSE_COOKIE_MANAGER', $_SESSION)) { self::$cookieManager = $_SESSION['HPROSE_COOKIE_MANAGER']; } register_shutdown_function(array('HproseBaseHttpClient', 'hproseKeepCookieInSession')); } protected function setCookie($headers) { foreach ($headers as $header) { @list($name, $value) = explode(':', $header, 2); if (strtolower($name) == 'set-cookie' || strtolower($name) == 'set-cookie2') { $cookies = explode(';', trim($value)); $cookie = array(); list($name, $value) = explode('=', trim($cookies[0]), 2); $cookie['name'] = $name; $cookie['value'] = $value; for ($i = 1; $i < count($cookies); $i++) { list($name, $value) = explode('=', trim($cookies[$i]), 2); $cookie[strtoupper($name)] = $value; } // Tomcat can return SetCookie2 with path wrapped in " if (array_key_exists('PATH', $cookie)) { $cookie['PATH'] = trim($cookie['PATH'], '"'); } else { $cookie['PATH'] = '/'; } if (array_key_exists('EXPIRES', $cookie)) { $cookie['EXPIRES'] = strtotime($cookie['EXPIRES']); } if (array_key_exists('DOMAIN', $cookie)) { $cookie['DOMAIN'] = strtolower($cookie['DOMAIN']); } else { $cookie['DOMAIN'] = $this->host; } $cookie['SECURE'] = array_key_exists('SECURE', $cookie); if (!array_key_exists($cookie['DOMAIN'], self::$cookieManager)) { self::$cookieManager[$cookie['DOMAIN']] = array(); } self::$cookieManager[$cookie['DOMAIN']][$cookie['name']] = $cookie; } } } protected abstract function formatCookie($cookies); protected function getCookie() { $cookies = array(); foreach (self::$cookieManager as $domain => $cookieList) { if (strpos($this->host, $domain) !== false) { $names = array(); foreach ($cookieList as $cookie) { if (array_key_exists('EXPIRES', $cookie) && (time() > $cookie['EXPIRES'])) { $names[] = $cookie['name']; } elseif (strpos($this->path, $cookie['PATH']) === 0) { if ((($this->secure && $cookie['SECURE']) || !$cookie['SECURE']) && !is_null($cookie['value'])) { $cookies[] = $cookie['name'] . '=' . $cookie['value']; } } } foreach ($names as $name) { unset(self::$cookieManager[$domain][$name]); } } } return $this->formatCookie($cookies); } public function __construct($url = '') { parent::__construct($url); $this->header = array('Content-type' => 'application/hprose'); } public function useService($url = '', $namespace = '') { $serviceProxy = parent::useService($url, $namespace); if ($url) { $url = parse_url($url); $this->secure = (strtolower($url['scheme']) == 'https'); $this->host = strtolower($url['host']); $this->path = $url['path']; $this->timeout = 30000; $this->keepAlive = false; $this->keepAliveTimeout = 300; } return $serviceProxy; } public function setHeader($name, $value) { $lname = strtolower($name); if ($lname != 'content-type' && $lname != 'content-length' && $lname != 'host') { if ($value) { $this->header[$name] = $value; } else { unset($this->header[$name]); } } } public function setProxy($proxy = NULL) { $this->proxy = $proxy; } public function setTimeout($timeout) { $this->timeout = $timeout; } public function getTimeout() { return $this->timeout; } public function setKeepAlive($keepAlive = true) { $this->keepAlive = $keepAlive; } public function getKeepAlive() { return $this->keeepAlive; } public function setKeepAliveTimeout($timeout) { $this->keepAliveTimeout = $timeout; } public function getKeepAliveTimeout() { return $this->keepAliveTimeout; } } if (class_exists('SaeFetchurl')) { class HproseHttpClient extends HproseBaseHttpClient { protected function formatCookie($cookies) { if (count($cookies) > 0) { return implode('; ', $cookies); } return ''; } protected function send($request) { $f = new SaeFetchurl(); $cookie = $this->getCookie(); if ($cookie != '') { $f->setHeader("Cookie", $cookie); } if ($this->keepAlive) { $f->setHeader("Connection", "keep-alive"); $f->setHeader("Keep-Alive", $this->keepAliveTimeout); } else { $f->setHeader("Connection", "close"); } foreach ($this->header as $name => $value) { $f->setHeader($name, $value); } $f->setMethod("post"); $f->setPostData($request); $f->setConnectTimeout($this->timeout); $f->setSendTimeout($this->timeout); $f->setReadTimeout($this->timeout); $response = $f->fetch($this->url); if ($f->errno()) { throw new HproseException($f->errno() . ": " . $f->errmsg()); } $http_response_header = $f->responseHeaders(false); $this->setCookie($http_response_header); return $response; } } } elseif (function_exists('curl_init')) { class HproseHttpClient extends HproseBaseHttpClient { private $curl; protected function formatCookie($cookies) { if (count($cookies) > 0) { return "Cookie: " . implode('; ', $cookies); } return ''; } public function __construct($url = '') { parent::__construct($url); $this->curl = curl_init(); } protected function send($request) { curl_setopt($this->curl, CURLOPT_URL, $this->url); curl_setopt($this->curl, CURLOPT_HEADER, TRUE); curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($this->curl, CURLOPT_POST, TRUE); curl_setopt($this->curl, CURLOPT_POSTFIELDS, $request); $headers_array = array($this->getCookie(), "Content-Length: " . strlen($request)); if ($this->keepAlive) { $headers_array[] = "Connection: keep-alive"; $headers_array[] = "Keep-Alive: " . $this->keepAliveTimeout; } else { $headers_array[] = "Connection: close"; } foreach ($this->header as $name => $value) { $headers_array[] = $name . ": " . $value; } curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers_array); if ($this->proxy) { curl_setopt($this->curl, CURLOPT_PROXY, $this->proxy); } if (defined(CURLOPT_TIMEOUT_MS)) { curl_setopt($this->curl, CURLOPT_TIMEOUT_MS, $this->timeout); } else { curl_setopt($this->curl, CURLOPT_TIMEOUT, $this->timeout / 1000); } $response = curl_exec($this->curl); $errno = curl_errno($this->curl); if ($errno) { throw new HproseException($errno . ": " . curl_error($this->curl)); } do { list($response_headers, $response) = explode("\r\n\r\n", $response, 2); $http_response_header = explode("\r\n", $response_headers); $http_response_firstline = array_shift($http_response_header); if (preg_match('@^HTTP/[0-9]\.[0-9]\s([0-9]{3})\s(.*)@', $http_response_firstline, $matches)) { $response_code = $matches[1]; $response_status = trim($matches[2]); } else { $response_code = "500"; $response_status = "Unknown Error."; } } while (substr($response_code, 0, 1) == "1"); if ($response_code != '200') { throw new HproseException($response_code . ": " . $response_status); } $this->setCookie($http_response_header); return $response; } public function __destruct() { curl_close($this->curl); } } } else { class HproseHttpClient extends HproseBaseHttpClient { protected function formatCookie($cookies) { if (count($cookies) > 0) { return "Cookie: " . implode('; ', $cookies) . "\r\n"; } return ''; } public function __errorHandler($errno, $errstr, $errfile, $errline) { throw new Exception($errstr, $errno); } protected function send($request) { $opts = array ( 'http' => array ( 'method' => 'POST', 'header'=> $this->getCookie() . "Content-Length: " . strlen($request) . "\r\n" . ($this->keepAlive ? "Connection: keep-alive\r\n" . "Keep-Alive: " . $this->keepAliveTimeout . "\r\n" : "Connection: close\r\n"), 'content' => $request, 'timeout' => $this->timeout / 1000.0, ), ); foreach ($this->header as $name => $value) { $opts['http']['header'] .= "$name: $value\r\n"; } if ($this->proxy) { $opts['http']['proxy'] = $this->proxy; $opts['http']['request_fulluri'] = true; } $context = stream_context_create($opts); set_error_handler(array(&$this, '__errorHandler')); $response = file_get_contents($this->url, false, $context); restore_error_handler(); $this->setCookie($http_response_header); return $response; } } } ?> ================================================ FILE: ThinkPHP/Library/Vendor/Hprose/HproseHttpServer.php ================================================ * * * \**********************************************************/ require_once('HproseCommon.php'); require_once('HproseIO.php'); class HproseHttpServer { private $errorTable = array(E_ERROR => 'Error', E_WARNING => 'Warning', E_PARSE => 'Parse Error', E_NOTICE => 'Notice', E_CORE_ERROR => 'Core Error', E_CORE_WARNING => 'Core Warning', E_COMPILE_ERROR => 'Compile Error', E_COMPILE_WARNING => 'Compile Warning', E_USER_ERROR => 'User Error', E_USER_WARNING => 'User Warning', E_USER_NOTICE => 'User Notice', E_STRICT => 'Run-time Notice', E_RECOVERABLE_ERROR => 'Error'); private $functions; private $funcNames; private $resultModes; private $simpleModes; private $debug; private $crossDomain; private $P3P; private $get; private $input; private $output; private $error; private $filter; private $simple; public $onBeforeInvoke; public $onAfterInvoke; public $onSendHeader; public $onSendError; public function __construct() { $this->functions = array(); $this->funcNames = array(); $this->resultModes = array(); $this->simpleModes = array(); $this->debug = false; $this->crossDomain = false; $this->P3P = false; $this->get = true; $this->filter = NULL; $this->simple = false; $this->error_types = E_ALL & ~E_NOTICE; $this->onBeforeInvoke = NULL; $this->onAfterInvoke = NULL; $this->onSendHeader = NULL; $this->onSendError = NULL; } /* __filterHandler & __errorHandler must be public, however we should never call them directly. */ public function __filterHandler($data) { if (preg_match('/.*? error<\/b>:(.*?)
debug) { $error = preg_replace('/<.*?>/', '', $match[1]); } else { $error = preg_replace('/ in .*<\/b>$/', '', $match[1]); } $data = HproseTags::TagError . HproseFormatter::serialize(trim($error), true) . HproseTags::TagEnd; } if ($this->filter) $data = $this->filter->outputFilter($data); return $data; } public function __errorHandler($errno, $errstr, $errfile, $errline) { if ($this->debug) { $errstr .= " in $errfile on line $errline"; } $this->error = $this->errorTable[$errno] . ": " . $errstr; $this->sendError(); return true; } private function sendHeader() { if ($this->onSendHeader) { call_user_func($this->onSendHeader); } header("Content-Type: text/plain"); if ($this->P3P) { header('P3P: CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi ' . 'CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL ' . 'UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE GOV"'); } if ($this->crossDomain) { if (array_key_exists('HTTP_ORIGIN', $_SERVER) && $_SERVER['HTTP_ORIGIN'] != "null") { header("Access-Control-Allow-Origin: " . $_SERVER['HTTP_ORIGIN']); header("Access-Control-Allow-Credentials: true"); } else { header('Access-Control-Allow-Origin: *'); } } } private function sendError() { if ($this->onSendError) { call_user_func($this->onSendError, $this->error); } ob_clean(); $this->output->write(HproseTags::TagError); $writer = new HproseSimpleWriter($this->output); $writer->writeString($this->error); $this->output->write(HproseTags::TagEnd); ob_end_flush(); } private function doInvoke() { $simpleReader = new HproseSimpleReader($this->input); do { $functionName = $simpleReader->readString(true); $aliasName = strtolower($functionName); $resultMode = HproseResultMode::Normal; if (array_key_exists($aliasName, $this->functions)) { $function = $this->functions[$aliasName]; $resultMode = $this->resultModes[$aliasName]; $simple = $this->simpleModes[$aliasName]; } elseif (array_key_exists('*', $this->functions)) { $function = $this->functions['*']; $resultMode = $this->resultModes['*']; $simple = $this->resultModes['*']; } else { throw new HproseException("Can't find this function " . $functionName . "()."); } if ($simple === NULL) $simple = $this->simple; $writer = ($simple ? new HproseSimpleWriter($this->output) : new HproseWriter($this->output)); $args = array(); $byref = false; $tag = $simpleReader->checkTags(array(HproseTags::TagList, HproseTags::TagEnd, HproseTags::TagCall)); if ($tag == HproseTags::TagList) { $reader = new HproseReader($this->input); $args = &$reader->readList(); $tag = $reader->checkTags(array(HproseTags::TagTrue, HproseTags::TagEnd, HproseTags::TagCall)); if ($tag == HproseTags::TagTrue) { $byref = true; $tag = $reader->checkTags(array(HproseTags::TagEnd, HproseTags::TagCall)); } } if ($this->onBeforeInvoke) { call_user_func($this->onBeforeInvoke, $functionName, $args, $byref); } if (array_key_exists('*', $this->functions) && ($function === $this->functions['*'])) { $arguments = array($functionName, &$args); } elseif ($byref) { $arguments = array(); for ($i = 0; $i < count($args); $i++) { $arguments[$i] = &$args[$i]; } } else { $arguments = $args; } $result = call_user_func_array($function, $arguments); if ($this->onAfterInvoke) { call_user_func($this->onAfterInvoke, $functionName, $args, $byref, $result); } // some service functions/methods may echo content, we need clean it ob_clean(); if ($resultMode == HproseResultMode::RawWithEndTag) { $this->output->write($result); return; } elseif ($resultMode == HproseResultMode::Raw) { $this->output->write($result); } else { $this->output->write(HproseTags::TagResult); if ($resultMode == HproseResultMode::Serialized) { $this->output->write($result); } else { $writer->reset(); $writer->serialize($result); } if ($byref) { $this->output->write(HproseTags::TagArgument); $writer->reset(); $writer->writeList($args); } } } while ($tag == HproseTags::TagCall); $this->output->write(HproseTags::TagEnd); ob_end_flush(); } private function doFunctionList() { $functions = array_values($this->funcNames); $writer = new HproseSimpleWriter($this->output); $this->output->write(HproseTags::TagFunctions); $writer->writeList($functions); $this->output->write(HproseTags::TagEnd); ob_end_flush(); } private function getDeclaredOnlyMethods($class) { $all = get_class_methods($class); if ($parent_class = get_parent_class($class)) { $inherit = get_class_methods($parent_class); $result = array_diff($all, $inherit); } else { $result = $all; } return $result; } public function addMissingFunction($function, $resultMode = HproseResultMode::Normal, $simple = NULL) { $this->addFunction($function, '*', $resultMode, $simple); } public function addFunction($function, $alias = NULL, $resultMode = HproseResultMode::Normal, $simple = NULL) { if (is_callable($function)) { if ($alias === NULL) { if (is_string($function)) { $alias = $function; } else { $alias = $function[1]; } } if (is_string($alias)) { $aliasName = strtolower($alias); $this->functions[$aliasName] = $function; $this->funcNames[$aliasName] = $alias; $this->resultModes[$aliasName] = $resultMode; $this->simpleModes[$aliasName] = $simple; } else { throw new HproseException('Argument alias is not a string'); } } else { throw new HproseException('Argument function is not a callable variable'); } } public function addFunctions($functions, $aliases = NULL, $resultMode = HproseResultMode::Normal, $simple = NULL) { $aliases_is_null = ($aliases === NULL); $count = count($functions); if (!$aliases_is_null && $count != count($aliases)) { throw new HproseException('The count of functions is not matched with aliases'); } for ($i = 0; $i < $count; $i++) { $function = $functions[$i]; if ($aliases_is_null) { $this->addFunction($function, NULL, $resultMode, $simple); } else { $this->addFunction($function, $aliases[$i], $resultMode, $simple); } } } public function addMethod($methodname, $belongto, $alias = NULL, $resultMode = HproseResultMode::Normal, $simple = NULL) { if ($alias === NULL) { $alias = $methodname; } if (is_string($belongto)) { $this->addFunction(array($belongto, $methodname), $alias, $resultMode, $simple); } else { $this->addFunction(array(&$belongto, $methodname), $alias, $resultMode, $simple); } } public function addMethods($methods, $belongto, $aliases = NULL, $resultMode = HproseResultMode::Normal, $simple = NULL) { $aliases_is_null = ($aliases === NULL); $count = count($methods); if (is_string($aliases)) { $aliasPrefix = $aliases; $aliases = array(); foreach ($methods as $name) { $aliases[] = $aliasPrefix . '_' . $name; } } if (!$aliases_is_null && $count != count($aliases)) { throw new HproseException('The count of methods is not matched with aliases'); } for ($i = 0; $i < $count; $i++) { $method = $methods[$i]; if (is_string($belongto)) { $function = array($belongto, $method); } else { $function = array(&$belongto, $method); } if ($aliases_is_null) { $this->addFunction($function, $method, $resultMode, $simple); } else { $this->addFunction($function, $aliases[$i], $resultMode, $simple); } } } public function addInstanceMethods($object, $class = NULL, $aliasPrefix = NULL, $resultMode = HproseResultMode::Normal, $simple = NULL) { if ($class === NULL) $class = get_class($object); $this->addMethods($this->getDeclaredOnlyMethods($class), $object, $aliasPrefix, $resultMode, $simple); } public function addClassMethods($class, $execclass = NULL, $aliasPrefix = NULL, $resultMode = HproseResultMode::Normal, $simple = NULL) { if ($execclass === NULL) $execclass = $class; $this->addMethods($this->getDeclaredOnlyMethods($class), $execclass, $aliasPrefix, $resultMode, $simple); } public function add() { $args_num = func_num_args(); $args = func_get_args(); switch ($args_num) { case 1: { if (is_callable($args[0])) { return $this->addFunction($args[0]); } elseif (is_array($args[0])) { return $this->addFunctions($args[0]); } elseif (is_object($args[0])) { return $this->addInstanceMethods($args[0]); } elseif (is_string($args[0])) { return $this->addClassMethods($args[0]); } break; } case 2: { if (is_callable($args[0]) && is_string($args[1])) { return $this->addFunction($args[0], $args[1]); } elseif (is_string($args[0])) { if (is_string($args[1]) && !is_callable(array($args[1], $args[0]))) { if (class_exists($args[1])) { return $this->addClassMethods($args[0], $args[1]); } else { return $this->addClassMethods($args[0], NULL, $args[1]); } } return $this->addMethod($args[0], $args[1]); } elseif (is_array($args[0])) { if (is_array($args[1])) { return $this->addFunctions($args[0], $args[1]); } else { return $this->addMethods($args[0], $args[1]); } } elseif (is_object($args[0])) { return $this->addInstanceMethods($args[0], $args[1]); } break; } case 3: { if (is_callable($args[0]) && is_null($args[1]) && is_string($args[2])) { return $this->addFunction($args[0], $args[2]); } elseif (is_string($args[0]) && is_string($args[2])) { if (is_string($args[1]) && !is_callable(array($args[0], $args[1]))) { return $this->addClassMethods($args[0], $args[1], $args[2]); } else { return $this->addMethod($args[0], $args[1], $args[2]); } } elseif (is_array($args[0])) { if (is_null($args[1]) && is_array($args[2])) { return $this->addFunctions($args[0], $args[2]); } else { return $this->addMethods($args[0], $args[1], $args[2]); } } elseif (is_object($args[0])) { return $this->addInstanceMethods($args[0], $args[1], $args[2]); } break; } throw new HproseException('Wrong arguments'); } } public function isDebugEnabled() { return $this->debug; } public function setDebugEnabled($enable = true) { $this->debug = $enable; } public function isCrossDomainEnabled() { return $this->crossDomain; } public function setCrossDomainEnabled($enable = true) { $this->crossDomain = $enable; } public function isP3PEnabled() { return $this->P3P; } public function setP3PEnabled($enable = true) { $this->P3P = $enable; } public function isGetEnabled() { return $this->get; } public function setGetEnabled($enable = true) { $this->get = $enable; } public function getFilter() { return $this->filter; } public function setFilter($filter) { $this->filter = $filter; } public function getSimpleMode() { return $this->simple; } public function setSimpleMode($simple = true) { $this->simple = $simple; } public function getErrorTypes() { return $this->error_types; } public function setErrorTypes($error_types) { $this->error_types = $error_types; } public function handle() { if (!isset($HTTP_RAW_POST_DATA)) $HTTP_RAW_POST_DATA = file_get_contents("php://input"); if ($this->filter) $HTTP_RAW_POST_DATA = $this->filter->inputFilter($HTTP_RAW_POST_DATA); $this->input = new HproseStringStream($HTTP_RAW_POST_DATA); $this->output = new HproseFileStream(fopen('php://output', 'wb')); set_error_handler(array(&$this, '__errorHandler'), $this->error_types); ob_start(array(&$this, "__filterHandler")); ob_implicit_flush(0); ob_clean(); $this->sendHeader(); if (($_SERVER['REQUEST_METHOD'] == 'GET') and $this->get) { return $this->doFunctionList(); } elseif ($_SERVER['REQUEST_METHOD'] == 'POST') { try { switch ($this->input->getc()) { case HproseTags::TagCall: return $this->doInvoke(); case HproseTags::TagEnd: return $this->doFunctionList(); default: throw new HproseException("Wrong Request: \r\n" . $HTTP_RAW_POST_DATA); } } catch (Exception $e) { $this->error = $e->getMessage(); if ($this->debug) { $this->error .= "\nfile: " . $e->getFile() . "\nline: " . $e->getLine() . "\ntrace: " . $e->getTraceAsString(); } $this->sendError(); } } $this->input->close(); $this->output->close(); } public function start() { $this->handle(); } } ?> ================================================ FILE: ThinkPHP/Library/Vendor/Hprose/HproseIO.php ================================================ * * * \**********************************************************/ require_once('HproseTags.php'); require_once('HproseClassManager.php'); require_once('HproseReader.php'); require_once('HproseWriter.php'); require_once('HproseFormatter.php'); ?> ================================================ FILE: ThinkPHP/Library/Vendor/Hprose/HproseIOStream.php ================================================ * * * \**********************************************************/ abstract class HproseAbstractStream { public abstract function close(); public abstract function getc(); public abstract function read($length); public abstract function readuntil($char); public abstract function seek($offset, $whence = SEEK_SET); public abstract function mark(); public abstract function unmark(); public abstract function reset(); public abstract function skip($n); public abstract function eof(); public abstract function write($string, $length = -1); } class HproseStringStream extends HproseAbstractStream { protected $buffer; protected $pos; protected $mark; protected $length; public function __construct($string = '') { $this->buffer = $string; $this->pos = 0; $this->mark = -1; $this->length = strlen($string); } public function close() { $this->buffer = NULL; $this->pos = 0; $this->mark = -1; $this->length = 0; } public function length() { return $this->length; } public function getc() { return $this->buffer{$this->pos++}; } public function read($length) { $s = substr($this->buffer, $this->pos, $length); $this->skip($length); return $s; } public function readuntil($tag) { $pos = strpos($this->buffer, $tag, $this->pos); if ($pos !== false) { $s = substr($this->buffer, $this->pos, $pos - $this->pos); $this->pos = $pos + strlen($tag); } else { $s = substr($this->buffer, $this->pos); $this->pos = $this->length; } return $s; } public function seek($offset, $whence = SEEK_SET) { switch ($whence) { case SEEK_SET: $this->pos = $offset; break; case SEEK_CUR: $this->pos += $offset; break; case SEEK_END: $this->pos = $this->length + $offset; break; } $this->mark = -1; return 0; } public function mark() { $this->mark = $this->pos; } public function unmark() { $this->mark = -1; } public function reset() { if ($this->mark != -1) { $this->pos = $this->mark; } } public function skip($n) { $this->pos += $n; } public function eof() { return ($this->pos >= $this->length); } public function write($string, $length = -1) { if ($length == -1) { $this->buffer .= $string; $length = strlen($string); } else { $this->buffer .= substr($string, 0, $length); } $this->length += $length; } public function toString() { return $this->buffer; } } class HproseFileStream extends HproseAbstractStream { protected $fp; protected $buf; protected $unmark; protected $pos; protected $length; public function __construct($fp) { $this->fp = $fp; $this->buf = ""; $this->unmark = true; $this->pos = -1; $this->length = 0; } public function close() { return fclose($this->fp); } public function getc() { if ($this->pos == -1) { return fgetc($this->fp); } elseif ($this->pos < $this->length) { return $this->buf{$this->pos++}; } elseif ($this->unmark) { $this->buf = ""; $this->pos = -1; $this->length = 0; return fgetc($this->fp); } elseif (($c = fgetc($this->fp)) !== false) { $this->buf .= $c; $this->pos++; $this->length++; } return $c; } public function read($length) { if ($this->pos == -1) { return fread($this->fp, $length); } elseif ($this->pos < $this->length) { $len = $this->length - $this->pos; if ($len < $length) { $s = fread($this->fp, $length - $len); $this->buf .= $s; $this->length += strlen($s); } $s = substr($this->buf, $this->pos, $length); $this->pos += strlen($s); } elseif ($this->unmark) { $this->buf = ""; $this->pos = -1; $this->length = 0; return fread($this->fp, $length); } elseif (($s = fread($this->fp, $length)) !== "") { $this->buf .= $s; $len = strlen($s); $this->pos += $len; $this->length += $len; } return $s; } public function readuntil($char) { $s = ''; while ((($c = $this->getc()) != $char) && $c !== false) $s .= $c; return $s; } public function seek($offset, $whence = SEEK_SET) { if (fseek($this->fp, $offset, $whence) == 0) { $this->buf = ""; $this->unmark = true; $this->pos = -1; $this->length = 0; return 0; } return -1; } public function mark() { $this->unmark = false; if ($this->pos == -1) { $this->buf = ""; $this->pos = 0; $this->length = 0; } elseif ($this->pos > 0) { $this->buf = substr($this->buf, $this->pos); $this->length -= $this->pos; $this->pos = 0; } } public function unmark() { $this->unmark = true; } public function reset() { $this->pos = 0; } public function skip($n) { $this->read($n); } public function eof() { if (($this->pos != -1) && ($this->pos < $this->length)) return false; return feof($this->fp); } public function write($string, $length = -1) { if ($length == -1) $length = strlen($string); return fwrite($this->fp, $string, $length); } } class HproseProcStream extends HproseAbstractStream { protected $process; protected $pipes; protected $buf; protected $unmark; protected $pos; protected $length; public function __construct($process, $pipes) { $this->process = $process; $this->pipes = $pipes; $this->buf = ""; $this->unmark = true; $this->pos = -1; $this->length = 0; } public function close() { fclose($this->pipes[0]); fclose($this->pipes[1]); proc_close($this->process); } public function getc() { if ($this->pos == -1) { return fgetc($this->pipes[1]); } elseif ($this->pos < $this->length) { return $this->buf{$this->pos++}; } elseif ($this->unmark) { $this->buf = ""; $this->pos = -1; $this->length = 0; return fgetc($this->pipes[1]); } elseif (($c = fgetc($this->pipes[1])) !== false) { $this->buf .= $c; $this->pos++; $this->length++; } return $c; } public function read($length) { if ($this->pos == -1) { return fread($this->pipes[1], $length); } elseif ($this->pos < $this->length) { $len = $this->length - $this->pos; if ($len < $length) { $s = fread($this->pipes[1], $length - $len); $this->buf .= $s; $this->length += strlen($s); } $s = substr($this->buf, $this->pos, $length); $this->pos += strlen($s); } elseif ($this->unmark) { $this->buf = ""; $this->pos = -1; $this->length = 0; return fread($this->pipes[1], $length); } elseif (($s = fread($this->pipes[1], $length)) !== "") { $this->buf .= $s; $len = strlen($s); $this->pos += $len; $this->length += $len; } return $s; } public function readuntil($char) { $s = ''; while ((($c = $this->getc()) != $char) && $c !== false) $s .= $c; return $s; } public function seek($offset, $whence = SEEK_SET) { if (fseek($this->pipes[1], $offset, $whence) == 0) { $this->buf = ""; $this->unmark = true; $this->pos = -1; $this->length = 0; return 0; } return -1; } public function mark() { $this->unmark = false; if ($this->pos == -1) { $this->buf = ""; $this->pos = 0; $this->length = 0; } elseif ($this->pos > 0) { $this->buf = substr($this->buf, $this->pos); $this->length -= $this->pos; $this->pos = 0; } } public function unmark() { $this->unmark = true; } public function reset() { $this->pos = 0; } public function skip($n) { $this->read($n); } public function eof() { if (($this->pos != -1) && ($this->pos < $this->length)) return false; return feof($this->pipes[1]); } public function write($string, $length = -1) { if ($length == -1) $length = strlen($string); return fwrite($this->pipes[0], $string, $length); } } ?> ================================================ FILE: ThinkPHP/Library/Vendor/Hprose/HproseReader.php ================================================ * * * \**********************************************************/ require_once('HproseCommon.php'); require_once('HproseTags.php'); require_once('HproseClassManager.php'); class HproseRawReader { public $stream; function __construct(&$stream) { $this->stream = &$stream; } public function readRaw($ostream = NULL, $tag = NULL) { if (is_null($ostream)) { $ostream = new HproseStringStream(); } if (is_null($tag)) { $tag = $this->stream->getc(); } switch ($tag) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case HproseTags::TagNull: case HproseTags::TagEmpty: case HproseTags::TagTrue: case HproseTags::TagFalse: case HproseTags::TagNaN: $ostream->write($tag); break; case HproseTags::TagInfinity: $ostream->write($tag); $ostream->write($this->stream->getc()); break; case HproseTags::TagInteger: case HproseTags::TagLong: case HproseTags::TagDouble: case HproseTags::TagRef: $this->readNumberRaw($ostream, $tag); break; case HproseTags::TagDate: case HproseTags::TagTime: $this->readDateTimeRaw($ostream, $tag); break; case HproseTags::TagUTF8Char: $this->readUTF8CharRaw($ostream, $tag); break; case HproseTags::TagBytes: $this->readBytesRaw($ostream, $tag); break; case HproseTags::TagString: $this->readStringRaw($ostream, $tag); break; case HproseTags::TagGuid: $this->readGuidRaw($ostream, $tag); break; case HproseTags::TagList: case HproseTags::TagMap: case HproseTags::TagObject: $this->readComplexRaw($ostream, $tag); break; case HproseTags::TagClass: $this->readComplexRaw($ostream, $tag); $this->readRaw($ostream); break; case HproseTags::TagError: $ostream->write($tag); $this->readRaw($ostream); break; case false: throw new HproseException("No byte found in stream"); default: throw new HproseException("Unexpected serialize tag '" + $tag + "' in stream"); } return $ostream; } private function readNumberRaw($ostream, $tag) { $s = $tag . $this->stream->readuntil(HproseTags::TagSemicolon) . HproseTags::TagSemicolon; $ostream->write($s); } private function readDateTimeRaw($ostream, $tag) { $s = $tag; do { $tag = $this->stream->getc(); $s .= $tag; } while ($tag != HproseTags::TagSemicolon && $tag != HproseTags::TagUTC); $ostream->write($s); } private function readUTF8CharRaw($ostream, $tag) { $s = $tag; $tag = $this->stream->getc(); $s .= $tag; $a = ord($tag); if (($a & 0xE0) == 0xC0) { $s .= $this->stream->getc(); } elseif (($a & 0xF0) == 0xE0) { $s .= $this->stream->read(2); } elseif ($a > 0x7F) { throw new HproseException("bad utf-8 encoding"); } $ostream->write($s); } private function readBytesRaw($ostream, $tag) { $len = $this->stream->readuntil(HproseTags::TagQuote); $s = $tag . $len . HproseTags::TagQuote . $this->stream->read((int)$len) . HproseTags::TagQuote; $this->stream->skip(1); $ostream->write($s); } private function readStringRaw($ostream, $tag) { $len = $this->stream->readuntil(HproseTags::TagQuote); $s = $tag . $len . HproseTags::TagQuote; $len = (int)$len; $this->stream->mark(); $utf8len = 0; for ($i = 0; $i < $len; ++$i) { switch (ord($this->stream->getc()) >> 4) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: { // 0xxx xxxx $utf8len++; break; } case 12: case 13: { // 110x xxxx 10xx xxxx $this->stream->skip(1); $utf8len += 2; break; } case 14: { // 1110 xxxx 10xx xxxx 10xx xxxx $this->stream->skip(2); $utf8len += 3; break; } case 15: { // 1111 0xxx 10xx xxxx 10xx xxxx 10xx xxxx $this->stream->skip(3); $utf8len += 4; ++$i; break; } default: { throw new HproseException('bad utf-8 encoding'); } } } $this->stream->reset(); $this->stream->unmark(); $s .= $this->stream->read($utf8len) . HproseTags::TagQuote; $this->stream->skip(1); $ostream->write($s); } private function readGuidRaw($ostream, $tag) { $s = $tag . $this->stream->read(38); $ostream->write($s); } private function readComplexRaw($ostream, $tag) { $s = $tag . $this->stream->readuntil(HproseTags::TagOpenbrace) . HproseTags::TagOpenbrace; $ostream->write($s); while (($tag = $this->stream->getc()) != HproseTags::TagClosebrace) { $this->readRaw($ostream, $tag); } $ostream->write($tag); } } class HproseSimpleReader extends HproseRawReader { private $classref; function __construct(&$stream) { parent::__construct($stream); $this->classref = array(); } public function &unserialize($tag = NULL) { if (is_null($tag)) { $tag = $this->stream->getc(); } $result = NULL; switch ($tag) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': $result = (int)$tag; break; case HproseTags::TagInteger: $result = $this->readInteger(); break; case HproseTags::TagLong: $result = $this->readLong(); break; case HproseTags::TagDouble: $result = $this->readDouble(); break; case HproseTags::TagNull: break; case HproseTags::TagEmpty: $result = ''; break; case HproseTags::TagTrue: $result = true; break; case HproseTags::TagFalse: $result = false; break; case HproseTags::TagNaN: $result = log(-1); break; case HproseTags::TagInfinity: $result = $this->readInfinity(); break; case HproseTags::TagDate: $result = $this->readDate(); break; case HproseTags::TagTime: $result = $this->readTime(); break; case HproseTags::TagBytes: $result = $this->readBytes(); break; case HproseTags::TagUTF8Char: $result = $this->readUTF8Char(); break; case HproseTags::TagString: $result = $this->readString(); break; case HproseTags::TagGuid: $result = $this->readGuid(); break; case HproseTags::TagList: $result = &$this->readList(); break; case HproseTags::TagMap: $result = &$this->readMap(); break; case HproseTags::TagClass: $this->readClass(); $result = &$this->unserialize(); break; case HproseTags::TagObject: $result = $this->readObject(); break; case HproseTags::TagError: throw new HproseException($this->readString(true)); case false: throw new HproseException('No byte found in stream'); default: throw new HproseException("Unexpected serialize tag '$tag' in stream"); } return $result; } public function checkTag($expectTag, $tag = NULL) { if (is_null($tag)) $tag = $this->stream->getc(); if ($tag != $expectTag) { throw new HproseException("Tag '$expectTag' expected, but '$tag' found in stream"); } } public function checkTags($expectTags, $tag = NULL) { if (is_null($tag)) $tag = $this->stream->getc(); if (!in_array($tag, $expectTags)) { $expectTags = implode('', $expectTags); throw new HproseException("Tag '$expectTags' expected, but '$tag' found in stream"); } return $tag; } public function readInteger($includeTag = false) { if ($includeTag) { $tag = $this->stream->getc(); if (($tag >= '0') && ($tag <= '9')) { return (int)$tag; } $this->checkTag(HproseTags::TagInteger, $tag); } return (int)($this->stream->readuntil(HproseTags::TagSemicolon)); } public function readLong($includeTag = false) { if ($includeTag) { $tag = $this->stream->getc(); if (($tag >= '0') && ($tag <= '9')) { return $tag; } $this->checkTag(HproseTags::TagLong, $tag); } return $this->stream->readuntil(HproseTags::TagSemicolon); } public function readDouble($includeTag = false) { if ($includeTag) { $tag = $this->stream->getc(); if (($tag >= '0') && ($tag <= '9')) { return (double)$tag; } $this->checkTag(HproseTags::TagDouble, $tag); } return (double)($this->stream->readuntil(HproseTags::TagSemicolon)); } public function readNaN() { $this->checkTag(HproseTags::TagNaN); return log(-1); } public function readInfinity($includeTag = false) { if ($includeTag) $this->checkTag(HproseTags::TagInfinity); return (($this->stream->getc() == HproseTags::TagNeg) ? log(0) : -log(0)); } public function readNull() { $this->checkTag(HproseTags::TagNull); return NULL; } public function readEmpty() { $this->checkTag(HproseTags::TagEmpty); return ''; } public function readBoolean() { $tag = $this->checkTags(array(HproseTags::TagTrue, HproseTags::TagFalse)); return ($tag == HproseTags::TagTrue); } public function readDate($includeTag = false) { if ($includeTag) $this->checkTag(HproseTags::TagDate); $year = (int)($this->stream->read(4)); $month = (int)($this->stream->read(2)); $day = (int)($this->stream->read(2)); $tag = $this->stream->getc(); if ($tag == HproseTags::TagTime) { $hour = (int)($this->stream->read(2)); $minute = (int)($this->stream->read(2)); $second = (int)($this->stream->read(2)); $microsecond = 0; $tag = $this->stream->getc(); if ($tag == HproseTags::TagPoint) { $microsecond = (int)($this->stream->read(3)) * 1000; $tag = $this->stream->getc(); if (($tag >= '0') && ($tag <= '9')) { $microsecond += (int)($tag) * 100 + (int)($this->stream->read(2)); $tag = $this->stream->getc(); if (($tag >= '0') && ($tag <= '9')) { $this->stream->skip(2); $tag = $this->stream->getc(); } } } if ($tag == HproseTags::TagUTC) { $date = new HproseDateTime($year, $month, $day, $hour, $minute, $second, $microsecond, true); } else { $date = new HproseDateTime($year, $month, $day, $hour, $minute, $second, $microsecond); } } elseif ($tag == HproseTags::TagUTC) { $date = new HproseDate($year, $month, $day, true); } else { $date = new HproseDate($year, $month, $day); } return $date; } public function readTime($includeTag = false) { if ($includeTag) $this->checkTag(HproseTags::TagTime); $hour = (int)($this->stream->read(2)); $minute = (int)($this->stream->read(2)); $second = (int)($this->stream->read(2)); $microsecond = 0; $tag = $this->stream->getc(); if ($tag == HproseTags::TagPoint) { $microsecond = (int)($this->stream->read(3)) * 1000; $tag = $this->stream->getc(); if (($tag >= '0') && ($tag <= '9')) { $microsecond += (int)($tag) * 100 + (int)($this->stream->read(2)); $tag = $this->stream->getc(); if (($tag >= '0') && ($tag <= '9')) { $this->stream->skip(2); $tag = $this->stream->getc(); } } } if ($tag == HproseTags::TagUTC) { $time = new HproseTime($hour, $minute, $second, $microsecond, true); } else { $time = new HproseTime($hour, $minute, $second, $microsecond); } return $time; } public function readBytes($includeTag = false) { if ($includeTag) $this->checkTag(HproseTags::TagBytes); $count = (int)($this->stream->readuntil(HproseTags::TagQuote)); $bytes = $this->stream->read($count); $this->stream->skip(1); return $bytes; } public function readUTF8Char($includeTag = false) { if ($includeTag) $this->checkTag(HproseTags::TagUTF8Char); $c = $this->stream->getc(); $s = $c; $a = ord($c); if (($a & 0xE0) == 0xC0) { $s .= $this->stream->getc(); } elseif (($a & 0xF0) == 0xE0) { $s .= $this->stream->read(2); } elseif ($a > 0x7F) { throw new HproseException("bad utf-8 encoding"); } return $s; } public function readString($includeTag = false) { if ($includeTag) $this->checkTag(HproseTags::TagString); $len = (int)$this->stream->readuntil(HproseTags::TagQuote); $this->stream->mark(); $utf8len = 0; for ($i = 0; $i < $len; ++$i) { switch (ord($this->stream->getc()) >> 4) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: { // 0xxx xxxx $utf8len++; break; } case 12: case 13: { // 110x xxxx 10xx xxxx $this->stream->skip(1); $utf8len += 2; break; } case 14: { // 1110 xxxx 10xx xxxx 10xx xxxx $this->stream->skip(2); $utf8len += 3; break; } case 15: { // 1111 0xxx 10xx xxxx 10xx xxxx 10xx xxxx $this->stream->skip(3); $utf8len += 4; ++$i; break; } default: { throw new HproseException('bad utf-8 encoding'); } } } $this->stream->reset(); $this->stream->unmark(); $s = $this->stream->read($utf8len); $this->stream->skip(1); return $s; } public function readGuid($includeTag = false) { if ($includeTag) $this->checkTag(HproseTags::TagGuid); $this->stream->skip(1); $s = $this->stream->read(36); $this->stream->skip(1); return $s; } protected function &readListBegin() { $list = array(); return $list; } protected function &readListEnd(&$list) { $count = (int)$this->stream->readuntil(HproseTags::TagOpenbrace); for ($i = 0; $i < $count; ++$i) { $list[] = &$this->unserialize(); } $this->stream->skip(1); return $list; } public function &readList($includeTag = false) { if ($includeTag) $this->checkTag(HproseTags::TagList); $list = &$this->readListBegin(); return $this->readListEnd($list); } protected function &readMapBegin() { $map = array(); return $map; } protected function &readMapEnd(&$map) { $count = (int)$this->stream->readuntil(HproseTags::TagOpenbrace); for ($i = 0; $i < $count; ++$i) { $key = &$this->unserialize(); $map[$key] = &$this->unserialize(); } $this->stream->skip(1); return $map; } public function &readMap($includeTag = false) { if ($includeTag) $this->checkTag(HproseTags::TagMap); $map = &$this->readMapBegin(); return $this->readMapEnd($map); } protected function readObjectBegin() { list($classname, $fields) = $this->classref[(int)$this->stream->readuntil(HproseTags::TagOpenbrace)]; $object = new $classname; return array($object, $fields); } protected function readObjectEnd($object, $fields) { $count = count($fields); if (class_exists('ReflectionClass')) { $reflector = new ReflectionClass($object); for ($i = 0; $i < $count; ++$i) { $field = $fields[$i]; if ($reflector->hasProperty($field)) { $property = $reflector->getProperty($field); $property->setAccessible(true); $property->setValue($object, $this->unserialize()); } else { $object->$field = &$this->unserialize(); } } } else { for ($i = 0; $i < $count; ++$i) { $object->$fields[$i] = &$this->unserialize(); } } $this->stream->skip(1); return $object; } public function readObject($includeTag = false) { if ($includeTag) { $tag = $this->checkTags(array(HproseTags::TagClass, HproseTags::TagObject)); if ($tag == HproseTags::TagClass) { $this->readClass(); return $this->readObject(true); } } list($object, $fields) = $this->readObjectBegin(); return $this->readObjectEnd($object, $fields); } protected function readClass() { $classname = HproseClassManager::getClass(self::readString()); $count = (int)$this->stream->readuntil(HproseTags::TagOpenbrace); $fields = array(); for ($i = 0; $i < $count; ++$i) { $fields[] = $this->readString(true); } $this->stream->skip(1); $this->classref[] = array($classname, $fields); } public function reset() { $this->classref = array(); } } class HproseReader extends HproseSimpleReader { private $ref; function __construct(&$stream) { parent::__construct($stream); $this->ref = array(); } public function &unserialize($tag = NULL) { if (is_null($tag)) { $tag = $this->stream->getc(); } if ($tag == HproseTags::TagRef) { return $this->readRef(); } return parent::unserialize($tag); } public function readDate($includeTag = false) { if ($includeTag) { $tag = $this->checkTags(array(HproseTags::TagDate, HproseTags::TagRef)); if ($tag == HproseTags::TagRef) return $this->readRef(); } $date = parent::readDate(); $this->ref[] = $date; return $date; } public function readTime($includeTag = false) { if ($includeTag) { $tag = $this->checkTags(array(HproseTags::TagTime, HproseTags::TagRef)); if ($tag == HproseTags::TagRef) return $this->readRef(); } $time = parent::readTime(); $this->ref[] = $time; return $time; } public function readBytes($includeTag = false) { if ($includeTag) { $tag = $this->checkTags(array(HproseTags::TagBytes, HproseTags::TagRef)); if ($tag == HproseTags::TagRef) return $this->readRef(); } $bytes = parent::readBytes(); $this->ref[] = $bytes; return $bytes; } public function readString($includeTag = false) { if ($includeTag) { $tag = $this->checkTags(array(HproseTags::TagString, HproseTags::TagRef)); if ($tag == HproseTags::TagRef) return $this->readRef(); } $str = parent::readString(); $this->ref[] = $str; return $str; } public function readGuid($includeTag = false) { if ($includeTag) { $tag = $this->checkTags(array(HproseTags::TagGuid, HproseTags::TagRef)); if ($tag == HproseTags::TagRef) return $this->readRef(); } $guid = parent::readGuid(); $this->ref[] = $guid; return $guid; } public function &readList($includeTag = false) { if ($includeTag) { $tag = $this->checkTags(array(HproseTags::TagList, HproseTags::TagRef)); if ($tag == HproseTags::TagRef) return $this->readRef(); } $list = &$this->readListBegin(); $this->ref[] = &$list; return $this->readListEnd($list); } public function &readMap($includeTag = false) { if ($includeTag) { $tag = $this->checkTags(array(HproseTags::TagMap, HproseTags::TagRef)); if ($tag == HproseTags::TagRef) return $this->readRef(); } $map = &$this->readMapBegin(); $this->ref[] = &$map; return $this->readMapEnd($map); } public function readObject($includeTag = false) { if ($includeTag) { $tag = $this->checkTags(array(HproseTags::TagClass, HproseTags::TagObject, HproseTags::TagRef)); if ($tag == HproseTags::TagRef) return $this->readRef(); if ($tag == HproseTags::TagClass) { $this->readClass(); return $this->readObject(true); } } list($object, $fields) = $this->readObjectBegin(); $this->ref[] = $object; return $this->readObjectEnd($object, $fields); } private function &readRef() { $ref = &$this->ref[(int)$this->stream->readuntil(HproseTags::TagSemicolon)]; if (gettype($ref) == 'array') { $result = &$ref; } else { $result = $ref; } return $result; } public function reset() { parent::reset(); $this->ref = array(); } } ?> ================================================ FILE: ThinkPHP/Library/Vendor/Hprose/HproseTags.php ================================================ * * * \**********************************************************/ class HproseTags { /* Serialize Tags */ const TagInteger = 'i'; const TagLong = 'l'; const TagDouble = 'd'; const TagNull = 'n'; const TagEmpty = 'e'; const TagTrue = 't'; const TagFalse = 'f'; const TagNaN = 'N'; const TagInfinity = 'I'; const TagDate = 'D'; const TagTime = 'T'; const TagUTC = 'Z'; const TagBytes = 'b'; const TagUTF8Char = 'u'; const TagString = 's'; const TagGuid = 'g'; const TagList = 'a'; const TagMap = 'm'; const TagClass = 'c'; const TagObject = 'o'; const TagRef = 'r'; /* Serialize Marks */ const TagPos = '+'; const TagNeg = '-'; const TagSemicolon = ';'; const TagOpenbrace = '{'; const TagClosebrace = '}'; const TagQuote = '"'; const TagPoint = '.'; /* Protocol Tags */ const TagFunctions = 'F'; const TagCall = 'C'; const TagResult = 'R'; const TagArgument = 'A'; const TagError = 'E'; const TagEnd = 'z'; } ?> ================================================ FILE: ThinkPHP/Library/Vendor/Hprose/HproseWriter.php ================================================ * * * \**********************************************************/ require_once('HproseCommon.php'); require_once('HproseTags.php'); require_once('HproseClassManager.php'); class HproseSimpleWriter { public $stream; private $classref; private $fieldsref; function __construct(&$stream) { $this->stream = &$stream; $this->classref = array(); $this->fieldsref = array(); } public function serialize(&$var) { if ((!isset($var)) || ($var === NULL)) { $this->writeNull(); } elseif (is_scalar($var)) { if (is_int($var)) { $this->writeInteger($var); } elseif (is_bool($var)) { $this->writeBoolean($var); } elseif (is_float($var)) { $this->writeDouble($var); } elseif (is_string($var)) { if ($var === '') { $this->writeEmpty(); } elseif ((strlen($var) < 4) && is_utf8($var) && (ustrlen($var) == 1)) { $this->writeUTF8Char($var); } elseif (is_utf8($var)) { $this->writeString($var, true); } else { $this->writeBytes($var, true); } } } elseif (is_array($var)) { if (is_list($var)) { $this->writeList($var, true); } else { $this->writeMap($var, true); } } elseif (is_object($var)) { if ($var instanceof stdClass) { $this->writeStdObject($var, true); } elseif (($var instanceof HproseDate) || ($var instanceof HproseDateTime)) { $this->writeDate($var, true); } elseif ($var instanceof HproseTime) { $this->writeTime($var, true); } else { $this->writeObject($var, true); } } else { throw new HproseException('Not support to serialize this data'); } } public function writeInteger($integer) { if ($integer >= 0 && $integer <= 9) { $this->stream->write((string)$integer); } else { $this->stream->write(HproseTags::TagInteger . $integer . HproseTags::TagSemicolon); } } public function writeLong($long) { if ($long >= '0' && $long <= '9') { $this->stream->write($long); } else { $this->stream->write(HproseTags::TagLong . $long . HproseTags::TagSemicolon); } } public function writeDouble($double) { if (is_nan($double)) { $this->writeNaN(); } elseif (is_infinite($double)) { $this->writeInfinity($double > 0); } else { $this->stream->write(HproseTags::TagDouble . $double . HproseTags::TagSemicolon); } } public function writeNaN() { $this->stream->write(HproseTags::TagNaN); } public function writeInfinity($positive = true) { $this->stream->write(HproseTags::TagInfinity . ($positive ? HproseTags::TagPos : HproseTags::TagNeg)); } public function writeNull() { $this->stream->write(HproseTags::TagNull); } public function writeEmpty() { $this->stream->write(HproseTags::TagEmpty); } public function writeBoolean($bool) { $this->stream->write($bool ? HproseTags::TagTrue : HproseTags::TagFalse); } public function writeDate($date, $checkRef = false) { if ($date->utc) { $this->stream->write(HproseTags::TagDate . $date->toString(false)); } else { $this->stream->write(HproseTags::TagDate . $date->toString(false) . HproseTags::TagSemicolon); } } public function writeTime($time, $checkRef = false) { if ($time->utc) { $this->stream->write(HproseTags::TagTime . $time->toString(false)); } else { $this->stream->write(HproseTags::TagTime . $time->toString(false) . HproseTags::TagSemicolon); } } public function writeBytes($bytes, $checkRef = false) { $len = strlen($bytes); $this->stream->write(HproseTags::TagBytes); if ($len > 0) $this->stream->write((string)$len); $this->stream->write(HproseTags::TagQuote . $bytes . HproseTags::TagQuote); } public function writeUTF8Char($char) { $this->stream->write(HproseTags::TagUTF8Char . $char); } public function writeString($str, $checkRef = false) { $len = ustrlen($str); $this->stream->write(HproseTags::TagString); if ($len > 0) $this->stream->write((string)$len); $this->stream->write(HproseTags::TagQuote . $str . HproseTags::TagQuote); } public function writeList(&$list, $checkRef = false) { $count = count($list); $this->stream->write(HproseTags::TagList); if ($count > 0) $this->stream->write((string)$count); $this->stream->write(HproseTags::TagOpenbrace); for ($i = 0; $i < $count; ++$i) { $this->serialize($list[$i]); } $this->stream->write(HproseTags::TagClosebrace); } public function writeMap(&$map, $checkRef = false) { $count = count($map); $this->stream->write(HproseTags::TagMap); if ($count > 0) $this->stream->write((string)$count); $this->stream->write(HproseTags::TagOpenbrace); foreach ($map as $key => &$value) { $this->serialize($key); $this->serialize($value); } $this->stream->write(HproseTags::TagClosebrace); } public function writeStdObject($obj, $checkRef = false) { $map = (array)$obj; self::writeMap($map); } protected function writeObjectBegin($obj) { $class = get_class($obj); $alias = HproseClassManager::getClassAlias($class); $fields = array_keys((array)$obj); if (array_key_exists($alias, $this->classref)) { $index = $this->classref[$alias]; } else { $index = $this->writeClass($alias, $fields); } return $index; } protected function writeObjectEnd($obj, $index) { $fields = $this->fieldsref[$index]; $count = count($fields); $this->stream->write(HproseTags::TagObject . $index . HproseTags::TagOpenbrace); $array = (array)$obj; for ($i = 0; $i < $count; ++$i) { $this->serialize($array[$fields[$i]]); } $this->stream->write(HproseTags::TagClosebrace); } public function writeObject($obj, $checkRef = false) { $this->writeObjectEnd($obj, $this->writeObjectBegin($obj)); } protected function writeClass($alias, $fields) { $len = ustrlen($alias); $this->stream->write(HproseTags::TagClass . $len . HproseTags::TagQuote . $alias . HproseTags::TagQuote); $count = count($fields); if ($count > 0) $this->stream->write((string)$count); $this->stream->write(HproseTags::TagOpenbrace); for ($i = 0; $i < $count; ++$i) { $field = $fields[$i]; if ($field{0} === "\0") { $field = substr($field, strpos($field, "\0", 1) + 1); } $this->writeString($field); } $this->stream->write(HproseTags::TagClosebrace); $index = count($this->fieldsref); $this->classref[$alias] = $index; $this->fieldsref[$index] = $fields; return $index; } public function reset() { $this->classref = array(); $this->fieldsref = array(); } } class HproseWriter extends HproseSimpleWriter { private $ref; private $arrayref; function __construct(&$stream) { parent::__construct($stream); $this->ref = array(); $this->arrayref = array(); } private function writeRef(&$obj, $checkRef, $writeBegin, $writeEnd) { if (is_string($obj)) { $key = 's_' . $obj; } elseif (is_array($obj)) { if (($i = array_ref_search($obj, $this->arrayref)) === false) { $i = count($this->arrayref); $this->arrayref[$i] = &$obj; } $key = 'a_' . $i; } else { $key = 'o_' . spl_object_hash($obj); } if ($checkRef && array_key_exists($key, $this->ref)) { $this->stream->write(HproseTags::TagRef . $this->ref[$key] . HproseTags::TagSemicolon); } else { $result = $writeBegin ? call_user_func_array($writeBegin, array(&$obj)) : false; $index = count($this->ref); $this->ref[$key] = $index; call_user_func_array($writeEnd, array(&$obj, $result)); } } public function writeDate($date, $checkRef = false) { $this->writeRef($date, $checkRef, NULL, array(&$this, 'parent::writeDate')); } public function writeTime($time, $checkRef = false) { $this->writeRef($time, $checkRef, NULL, array(&$this, 'parent::writeTime')); } public function writeBytes($bytes, $checkRef = false) { $this->writeRef($bytes, $checkRef, NULL, array(&$this, 'parent::writeBytes')); } public function writeString($str, $checkRef = false) { $this->writeRef($str, $checkRef, NULL, array(&$this, 'parent::writeString')); } public function writeList(&$list, $checkRef = false) { $this->writeRef($list, $checkRef, NULL, array(&$this, 'parent::writeList')); } public function writeMap(&$map, $checkRef = false) { $this->writeRef($map, $checkRef, NULL, array(&$this, 'parent::writeMap')); } public function writeStdObject($obj, $checkRef = false) { $this->writeRef($obj, $checkRef, NULL, array(&$this, 'parent::writeStdObject')); } public function writeObject($obj, $checkRef = false) { $this->writeRef($obj, $checkRef, array(&$this, 'writeObjectBegin'), array(&$this, 'writeObjectEnd')); } public function reset() { parent::reset(); $this->ref = array(); $this->arrayref = array(); } } ?> ================================================ FILE: ThinkPHP/Library/Vendor/README.txt ================================================ 第三方类库包目录 ================================================ FILE: ThinkPHP/Library/Vendor/SmartTemplate/class.smarttemplate.php ================================================ assign( 'TITLE', 'TemplateDemo - Userlist' ); * $page->assign( 'user', DB_read_all( 'select * from ris_user' ) ); * $page->output(); * * Usage Example II: * * $data = array( * 'TITLE' => 'TemplateDemo - Userlist', * 'user' => DB_read_all( 'select * from ris_user' ) * ); * $page = new SmartTemplate( "template.html" ); * $page->output( $data ); * * * @author Philipp v. Criegern philipp@criegern.com * @author Manuel 'EndelWar' Dalla Lana endelwar@aregar.it * @version 1.2.1 03.07.2006 * * CVS ID: $Id: class.smarttemplate.php 2504 2011-12-28 07:35:29Z liu21st $ */ class SmartTemplate { /** * Whether to store compiled php code or not (for debug purpose) * * @access public */ var $reuse_code = true; /** * Directory where all templates are stored * Can be overwritten by global configuration array $_CONFIG['template_dir'] * * @access public */ var $template_dir = 'templates/'; /** * Where to store compiled templates * Can be overwritten by global configuration array $_CONFIG['smarttemplate_compiled'] * * @access public */ var $temp_dir = 'templates_c/'; /** * Temporary folder for output cache storage * Can be overwritten by global configuration array $_CONFIG['smarttemplate_cache'] * * @access public */ var $cache_dir = 'templates_c/'; /** * Default Output Cache Lifetime in Seconds * Can be overwritten by global configuration array $_CONFIG['cache_lifetime'] * * @access public */ var $cache_lifetime = 600; /** * Temporary file for output cache storage * * @access private */ var $cache_filename; /** * The template filename * * @access private */ var $tpl_file; /** * The compiled template filename * * @access private */ var $cpl_file; /** * Template content array * * @access private */ var $data = array(); /** * Parser Class * * @access private */ var $parser; /** * Debugger Class * * @access private */ var $debugger; /** * SmartTemplate Constructor * * @access public * @param string $template_filename Template Filename */ function SmartTemplate ( $template_filename = '' ) { global $_CONFIG; if (!empty($_CONFIG['smarttemplate_compiled'])) { $this->temp_dir = $_CONFIG['smarttemplate_compiled']; } if (!empty($_CONFIG['smarttemplate_cache'])) { $this->cache_dir = $_CONFIG['smarttemplate_cache']; } if (is_numeric($_CONFIG['cache_lifetime'])) { $this->cache_lifetime = $_CONFIG['cache_lifetime']; } if (!empty($_CONFIG['template_dir']) && is_file($_CONFIG['template_dir'] . '/' . $template_filename)) { $this->template_dir = $_CONFIG['template_dir']; } $this->tpl_file = $template_filename; } // DEPRECATED METHODS // Methods used in older parser versions, soon will be removed function set_templatefile ($template_filename) { $this->tpl_file = $template_filename; } function add_value ($name, $value ) { $this->assign($name, $value); } function add_array ($name, $value ) { $this->append($name, $value); } /** * Assign Template Content * * Usage Example: * $page->assign( 'TITLE', 'My Document Title' ); * $page->assign( 'userlist', array( * array( 'ID' => 123, 'NAME' => 'John Doe' ), * array( 'ID' => 124, 'NAME' => 'Jack Doe' ), * ); * * @access public * @param string $name Parameter Name * @param mixed $value Parameter Value * @desc Assign Template Content */ function assign ( $name, $value = '' ) { if (is_array($name)) { foreach ($name as $k => $v) { $this->data[$k] = $v; } } else { $this->data[$name] = $value; } } /** * Assign Template Content * * Usage Example: * $page->append( 'userlist', array( 'ID' => 123, 'NAME' => 'John Doe' ) ); * $page->append( 'userlist', array( 'ID' => 124, 'NAME' => 'Jack Doe' ) ); * * @access public * @param string $name Parameter Name * @param mixed $value Parameter Value * @desc Assign Template Content */ function append ( $name, $value ) { if (is_array($value)) { $this->data[$name][] = $value; } elseif (!is_array($this->data[$name])) { $this->data[$name] .= $value; } } /** * Parser Wrapper * Returns Template Output as a String * * @access public * @param array $_top Content Array * @return string Parsed Template * @desc Output Buffer Parser Wrapper */ function result ( $_top = '' ) { ob_start(); $this->output( $_top ); $result = ob_get_contents(); ob_end_clean(); return $result; } /** * Execute parsed Template * Prints Parsing Results to Standard Output * * @access public * @param array $_top Content Array * @desc Execute parsed Template */ function output ( $_top = '' ) { global $_top; // Make sure that folder names have a trailing '/' if (strlen($this->template_dir) && substr($this->template_dir, -1) != '/') { $this->template_dir .= '/'; } if (strlen($this->temp_dir) && substr($this->temp_dir, -1) != '/') { $this->temp_dir .= '/'; } // Prepare Template Content if (!is_array($_top)) { if (strlen($_top)) { $this->tpl_file = $_top; } $_top = $this->data; } $_obj = &$_top; $_stack_cnt = 0; $_stack[$_stack_cnt++] = $_obj; // Check if template is already compiled $cpl_file_name = preg_replace('/[:\/.\\\\]/', '_', $this->tpl_file); if (strlen($cpl_file_name) > 0) { $this->cpl_file = $this->temp_dir . $cpl_file_name . '.php'; $compile_template = true; if ($this->reuse_code) { if (is_file($this->cpl_file)) { if ($this->mtime($this->cpl_file) > $this->mtime($this->template_dir . $this->tpl_file)) { $compile_template = false; } } } if ($compile_template) { if (@include_once("class.smarttemplateparser.php")) { $this->parser = new SmartTemplateParser($this->template_dir . $this->tpl_file); if (!$this->parser->compile($this->cpl_file)) { exit( "SmartTemplate Parser Error: " . $this->parser->error ); } } else { exit( "SmartTemplate Error: Cannot find class.smarttemplateparser.php; check SmartTemplate installation"); } } // Execute Compiled Template include($this->cpl_file); } else { exit( "SmartTemplate Error: You must set a template file name"); } // Delete Global Content Array in order to allow multiple use of SmartTemplate class in one script unset ($_top); } /** * Debug Template * * @access public * @param array $_top Content Array * @desc Debug Template */ function debug ( $_top = '' ) { // Prepare Template Content if (!$_top) { $_top = $this->data; } if (@include_once("class.smarttemplatedebugger.php")) { $this->debugger = new SmartTemplateDebugger($this->template_dir . $this->tpl_file); $this->debugger->start($_top); } else { exit( "SmartTemplate Error: Cannot find class.smarttemplatedebugger.php; check SmartTemplate installation"); } } /** * Start Ouput Content Buffering * * Usage Example: * $page = new SmartTemplate('template.html'); * $page->use_cache(); * ... * * @access public * @desc Output Cache */ function use_cache ( $key = '' ) { if (empty($_POST)) { $this->cache_filename = $this->cache_dir . 'cache_' . md5($_SERVER['REQUEST_URI'] . serialize($key)) . '.ser'; if (($_SERVER['HTTP_CACHE_CONTROL'] != 'no-cache') && ($_SERVER['HTTP_PRAGMA'] != 'no-cache') && @is_file($this->cache_filename)) { if ((time() - filemtime($this->cache_filename)) < $this->cache_lifetime) { readfile($this->cache_filename); exit; } } ob_start( array( &$this, 'cache_callback' ) ); } } /** * Output Buffer Callback Function * * @access private * @param string $output * @return string $output */ function cache_callback ( $output ) { if ($hd = @fopen($this->cache_filename, 'w')) { fputs($hd, $output); fclose($hd); } return $output; } /** * Determine Last Filechange Date (if File exists) * * @access private * @param string $filename * @return mixed * @desc Determine Last Filechange Date */ function mtime ( $filename ) { if (@is_file($filename)) { $ret = filemtime($filename); return $ret; } } } ?> ================================================ FILE: ThinkPHP/Library/Vendor/SmartTemplate/class.smarttemplatedebugger.php ================================================ filename = $template_filename; // Load Template if ($hd = @fopen($template_filename, "r")) { $this->template = fread($hd, filesize($template_filename)); fclose($hd); } else { $this->template = "SmartTemplate Debugger Error: File not found: '$template_filename'"; } $this->tab[0] = ''; for ($i=1; $i < 10; $i++) { $this->tab[$i] = str_repeat(' ', $i); } } /** * Main Template Parser * * @param string $compiled_template_filename Compiled Template Filename * @desc Creates Compiled PHP Template */ function start ( $vars ) { $page = $this->template; $page = preg_replace("/()/", "\n$1\n", $page); $page = preg_replace("/()/", "\n$1\n", $page); $page = preg_replace("/()/", "\n$1\n", $page); $page = preg_replace("/()/", "\n$1\n", $page); $page = preg_replace("/()/", "\n$1\n", $page); $page = $this->highlight_html($page); $rows = explode("\n", $page); $page_arr = array(); $level = 0; $blocklvl = 0; $rowcnt = 0; $spancnt = 0; $offset = 22; $lvl_block = array(); $lvl_row = array(); $lvl_typ = array(); foreach ($rows as $row) { if ($row = trim($row)) { $closespan = false; if (substr($row, $offset, 12) == '<!-- END ') { if ($level < 1) { $level++; $error[$rowcnt] = "END Without BEGIN"; } elseif ($lvl_typ[$level] != 'BEGIN') { $error[$lvl_row[$level]] = "IF without ENDIF"; $error[$rowcnt] = "END Without BEGIN"; } $blocklvl--; $level--; $closespan = true; } if (substr($row, $offset, 14) == '<!-- ENDIF ') { if ($level < 1) { $level++; $error[$rowcnt] = "ENDIF Without IF"; } elseif ($lvl_typ[$level] != 'IF') { $error[$lvl_row[$level]] = "BEGIN without END"; $error[$rowcnt] = "ENDIF Without IF"; } $closespan = true; $level--; } if ($closespan) { $page_arr[$rowcnt-1] .= ''; } $this_row = $this->tab[$level] . $row; if (substr($row, $offset, 12) == '<!-- ELSE') { if ($level < 1) { $error[$rowcnt] = "ELSE Without IF"; } elseif ($lvl_typ[$level] != 'IF') { $error[$rowcnt] = "ELSE Without IF"; } else { $this_row = $this->tab[$level-1] . $row; } } if (substr($row, $offset, 14) == '<!-- BEGIN ') { if ($blocklvl == 0) { if ($lp = strpos($row, '-->')) { if ($blockname = trim(substr($row, $offset + 14, $lp -$offset -14))) { if ($nr = count($vars[$blockname])) { $this_row .= $this->toggleview("$nr Entries"); } else { $this_row .= $this->toggleview("Emtpy"); } } } } else { $this_row .= $this->toggleview('['); } $blocklvl++; $level++; $lvl_row[$level] = $rowcnt; $lvl_typ[$level] = 'BEGIN'; } elseif (substr($row, $offset, 11) == '<!-- IF ') { $level++; $lvl_row[$level] = $rowcnt; $lvl_typ[$level] = 'IF'; $this_row .= $this->toggleview(); } $page_arr[] = $this_row; $lvl_block[$rowcnt] = $blocklvl; $rowcnt++; } } if ($level > 0) { $error[$lvl_row[$level]] = "Block not closed"; } $page = join("\n", $page_arr); $rows = explode("\n", $page); $cnt = count($rows); for ($i = 0; $i < $cnt; $i++) { // Add Errortext if (isset($error)) { if ($err = $error[$i]) { $rows[$i] = '' . $rows[$i] . ' ERROR: ' . $err . '!'; } } // Replace Scalars if (preg_match_all('/{([a-zA-Z0-9_. &;]+)}/', $rows[$i], $var)) { foreach ($var[1] as $tag) { $fulltag = $tag; if ($delim = strpos($tag, ' > ')) { $tag = substr($tag, 0, $delim); } if (substr($tag, 0, 4) == 'top.') { $title = $this->tip($vars[substr($tag, 4)]); } elseif ($lvl_block[$i] == 0) { $title = $this->tip($vars[$tag]); } else { $title = '[BLOCK?]'; } $code = '{' . $fulltag . '}'; $rows[$i] = str_replace('{'.$fulltag.'}', $code, $rows[$i]); } } // Replace Extensions if (preg_match_all('/{([a-zA-Z0-9_]+):([^}]*)}/', $rows[$i], $var)) { foreach ($var[2] as $tmpcnt => $tag) { $fulltag = $tag; if ($delim = strpos($tag, ' > ')) { $tag = substr($tag, 0, $delim); } if (strpos($tag, ',')) { list($tag, $addparam) = explode(',', $tag, 2); } $extension = $var[1][$tmpcnt]; if (substr($tag, 0, 4) == 'top.') { $title = $this->tip($vars[substr($tag, 4)]); } elseif ($lvl_block[$i] == 0) { $title = $this->tip($vars[$tag]); } else { $title = '[BLOCK?]'; } $code = '{' . $extension . ':' . $fulltag . '}'; $rows[$i] = str_replace('{'.$extension . ':' . $fulltag .'}', $code, $rows[$i]); } } // 'IF nnn' Blocks if (preg_match_all('/<!-- IF ([a-zA-Z0-9_.]+) -->/', $rows[$i], $var)) { foreach ($var[1] as $tag) { if (substr($tag, 0, 4) == 'top.') { $title = $this->tip($vars[substr($tag, 4)]); } elseif ($lvl_block[$i] == 0) { $title = $this->tip($vars[$tag]); } else { $title = '[BLOCK?]'; } $code = '<!-- IF ' . $tag . ' -->'; $rows[$i] = str_replace("<!-- IF $tag -->", $code, $rows[$i]); if ($title == '[NULL]') { $rows[$i] = str_replace('Hide', 'Show', $rows[$i]); $rows[$i] = str_replace('block', 'none', $rows[$i]); } } } } $page = join("
", $rows); // Print Header echo ''; // Print Index echo ''; echo 'SmartTemplate Debugger
'; echo '
  • PHP-Script: ' . $_SERVER['PATH_TRANSLATED'] . '
  • Template: ' . $this->filename . '

  • '; echo '
  • Template
  • '; echo '
  • Compiled Template
  • '; echo '
  • Data
  • '; echo '

    '; // Print Template echo '
    Template: [
    Hide Ouptut]
    '; echo '
    ';
    			echo $page;
    			echo '
    '; // Print Compiled Template if (@include_once ("class.smarttemplateparser.php")) { $parser = new SmartTemplateParser($this->filename); $compiled = $parser->compile(); echo '

    Compiled Template: [
    Hide Ouptut]
    '; echo '
    ';
    				highlight_string($compiled);
    				echo '
    '; } else { exit( "SmartTemplate Error: Cannot find class.smarttemplateparser.php; check SmartTemplate installation"); } // Print Data echo '

    Data: [
    Hide Ouptut]
    '; echo '
    ';
    			echo $this->vardump($vars);
    			echo '
    '; } /** * Insert Hide/Show Layer Switch * * @param string $suffix Additional Text * @desc Insert Hide/Show Layer Switch */ function toggleview ( $suffix = '') { global $spancnt; $spancnt++; if ($suffix) { $suffix .= ':'; } $ret = '[' . $suffix . 'Hide Block]'; return $ret; } /** * Create Title Text * * @param string $value Content * @desc Create Title Text */ function tip ( $value ) { if (empty($value)) { return "[NULL]"; } else { $ret = htmlentities(substr($value,0,200)); return $ret; } } /** * Recursive Variable Display Output * * @param mixed $var Content * @param int $depth Incremented Indent Counter for Recursive Calls * @return string Variable Content * @access private * @desc Recursive Variable Display Output */ function vardump($var, $depth = 0) { if (is_array($var)) { $result = "Array (" . count($var) . ")
    "; foreach(array_keys($var) as $key) { $result .= $this->tab[$depth] . "$key: " . $this->vardump($var[$key], $depth+1); } return $result; } else { $ret = htmlentities($var) . "
    "; return $ret; } } /** * Splits Template-Style Variable Names into an Array-Name/Key-Name Components * * @param string $tag Variale Name used in Template * @return array Array Name, Key Name * @access private * @desc Splits Template-Style Variable Names into an Array-Name/Key-Name Components */ function var_name($tag) { $parent_level = 0; while (substr($tag, 0, 7) == 'parent.') { $tag = substr($tag, 7); $parent_level++; } if (substr($tag, 0, 4) == 'top.') { $ret = array('_stack[0]', substr($tag,4)); return $ret; } elseif ($parent_level) { $ret = array('_stack[$_stack_cnt-'.$parent_level.']', $tag); return $ret; } else { $ret = array('_obj', $tag); return $ret; } } /** * Highlight HTML Source * * @param string $code HTML Source * @return string Hightlighte HTML Source * @access private * @desc Highlight HTML Source */ function highlight_html ( $code ) { $code = htmlentities($code); $code = preg_replace('/([a-zA-Z_]+)=/', '$1=', $code); $code = preg_replace('/(<[\/a-zA-Z0-9&;]+)/', '$1', $code); $code = str_replace('<!--', '<!--', $code); $code = str_replace('-->', '-->', $code); $code = preg_replace('/[\r\n]+/', "\n", $code); return $code; } } ?> ================================================ FILE: ThinkPHP/Library/Vendor/SmartTemplate/class.smarttemplateparser.php ================================================ template = fread($hd, filesize($template_filename)); } else { $this->template = "SmartTemplate Parser Error: File size is zero byte: '$template_filename'"; } fclose($hd); // Extract the name of the template directory $this->template_dir = dirname($template_filename); } else { $this->template = "SmartTemplate Parser Error: File not found: '$template_filename'"; } } /** * Main Template Parser * * @param string $compiled_template_filename Compiled Template Filename * @desc Creates Compiled PHP Template */ function compile( $compiled_template_filename = '' ) { if (empty($this->template)) { return; } /* Quick hack to allow subtemplates */ if(eregi("/', $this->template, $tvar); foreach($tvar[1] as $subfile) { if(file_exists($this->template_dir . "/$subfile")) { $subst = implode('',file($this->template_dir . "/$subfile")); } else { $subst = 'SmartTemplate Parser Error: Subtemplate not found: \''.$subfile.'\''; } $this->template = str_replace("", $subst, $this->template); } } } // END, ELSE Blocks $page = preg_replace("//", "", $this->template); $page = preg_replace("//", "", $page); $page = str_replace("", "", $page); // 'BEGIN - END' Blocks if (preg_match_all('//', $page, $var)) { foreach ($var[1] as $tag) { list($parent, $block) = $this->var_name($tag); $code = "\$$parent"."['$block']));\n" . "\$_tmp_arr_keys=array_keys(\$$parent"."['$block']);\n" . "if (\$_tmp_arr_keys[0]!='0')\n" . "\$$parent"."['$block']=array(0=>\$$parent"."['$block']);\n" . "\$_stack[\$_stack_cnt++]=\$_obj;\n" . "foreach (\$$parent"."['$block'] as \$rowcnt=>\$$block) {\n" . "\$$block"."['ROWCNT']=\$rowcnt;\n" . "\$$block"."['ALTROW']=\$rowcnt%2;\n" . "\$$block"."['ROWBIT']=\$rowcnt%2;\n" . "\$_obj=&\$$block;\n?>"; $page = str_replace("", $code, $page); } } // 'IF nnn=mmm' Blocks if (preg_match_all('//', $page, $var)) { foreach ($var[2] as $cnt => $tag) { list($parent, $block) = $this->var_name($tag); $cmp = $var[3][$cnt]; $val = $var[4][$cnt]; $else = ($var[1][$cnt] == 'ELSE') ? '} else' : ''; if ($cmp == '=') { $cmp = '=='; } if (preg_match('/"([^"]*)"/',$val,$matches)) { $code = ""; } elseif (preg_match('/([^"]*)/',$val,$matches)) { list($parent_right, $block_right) = $this->var_name($matches[1]); $code = ""; } $page = str_replace($var[0][$cnt], $code, $page); } } // 'IF nnn' Blocks if (preg_match_all('//', $page, $var)) { foreach ($var[2] as $cnt => $tag) { $else = ($var[1][$cnt] == 'ELSE') ? '} else' : ''; list($parent, $block) = $this->var_name($tag); $code = ""; $page = str_replace($var[0][$cnt], $code, $page); } } // Replace Scalars if (preg_match_all('/{([a-zA-Z0-9_. >]+)}/', $page, $var)) { foreach ($var[1] as $fulltag) { // Determin Command (echo / $obj[n]=) list($cmd, $tag) = $this->cmd_name($fulltag); list($block, $skalar) = $this->var_name($tag); $code = "\n"; $page = str_replace('{'.$fulltag.'}', $code, $page); } } // ROSI Special: Replace Translations if (preg_match_all('/<"([a-zA-Z0-9_.]+)">/', $page, $var)) { foreach ($var[1] as $tag) { list($block, $skalar) = $this->var_name($tag); $code = "\n"; $page = str_replace('<"'.$tag.'">', $code, $page); } } // Include Extensions $header = ''; if (preg_match_all('/{([a-zA-Z0-9_]+):([^}]*)}/', $page, $var)) { foreach ($var[2] as $cnt => $tag) { // Determin Command (echo / $obj[n]=) list($cmd, $tag) = $this->cmd_name($tag); $extension = $var[1][$cnt]; if (!isset($this->extension_tagged[$extension])) { $header .= "include_once \"smarttemplate_extensions/smarttemplate_extension_$extension.php\";\n"; $this->extension_tagged[$extension] = true; } if (!strlen($tag)) { $code = "\n"; } elseif (substr($tag, 0, 1) == '"') { $code = "\n"; } elseif (strpos($tag, ',')) { list($tag, $addparam) = explode(',', $tag, 2); list($block, $skalar) = $this->var_name($tag); if (preg_match('/^([a-zA-Z_]+)/', $addparam, $match)) { $nexttag = $match[1]; list($nextblock, $nextskalar) = $this->var_name($nexttag); $addparam = substr($addparam, strlen($nexttag)); $code = "\n"; } else { $code = "\n"; } } else { list($block, $skalar) = $this->var_name($tag); $code = "\n"; } $page = str_replace($var[0][$cnt], $code, $page); } } // Add Include Header if (isset($header) && !empty($header)) { $page = "$page"; } // Store Code to Temp Dir if (strlen($compiled_template_filename)) { if ($hd = fopen($compiled_template_filename, "w")) { fwrite($hd, $page); fclose($hd); return true; } else { $this->error = "Could not write compiled file."; return false; } } else { return $page; } } /** * Splits Template-Style Variable Names into an Array-Name/Key-Name Components * {example} : array( "_obj", "example" ) -> $_obj['example'] * {example.value} : array( "_obj['example']", "value" ) -> $_obj['example']['value'] * {example.0.value} : array( "_obj['example'][0]", "value" ) -> $_obj['example'][0]['value'] * {top.example} : array( "_stack[0]", "example" ) -> $_stack[0]['example'] * {parent.example} : array( "_stack[$_stack_cnt-1]", "example" ) -> $_stack[$_stack_cnt-1]['example'] * {parent.parent.example} : array( "_stack[$_stack_cnt-2]", "example" ) -> $_stack[$_stack_cnt-2]['example'] * * @param string $tag Variale Name used in Template * @return array Array Name, Key Name * @access private * @desc Splits Template-Style Variable Names into an Array-Name/Key-Name Components */ function var_name($tag) { $parent_level = 0; while (substr($tag, 0, 7) == 'parent.') { $tag = substr($tag, 7); $parent_level++; } if (substr($tag, 0, 4) == 'top.') { $obj = '_stack[0]'; $tag = substr($tag,4); } elseif ($parent_level) { $obj = '_stack[$_stack_cnt-'.$parent_level.']'; } else { $obj = '_obj'; } while (is_int(strpos($tag, '.'))) { list($parent, $tag) = explode('.', $tag, 2); if (is_numeric($parent)) { $obj .= "[" . $parent . "]"; } else { $obj .= "['" . $parent . "']"; } } $ret = array($obj, $tag); return $ret; } /** * Determine Template Command from Variable Name * {variable} : array( "echo", "variable" ) -> echo $_obj['variable'] * {variable > new_name} : array( "_obj['new_name']=", "variable" ) -> $_obj['new_name']= $_obj['variable'] * * @param string $tag Variale Name used in Template * @return array Array Command, Variable * @access private * @desc Determine Template Command from Variable Name */ function cmd_name($tag) { if (preg_match('/^(.+) > ([a-zA-Z0-9_.]+)$/', $tag, $tagvar)) { $tag = $tagvar[1]; list($newblock, $newskalar) = $this->var_name($tagvar[2]); $cmd = "\$$newblock"."['$newskalar']="; } else { $cmd = "echo"; } $ret = array($cmd, $tag); return $ret; } /** * @return int Number of subtemplate included * @access private * @desc Count number of subtemplates included in current template */ function count_subtemplates() { preg_match_all('//', $this->template, $tvar); $count_subtemplates = count($tvar[1]); $ret = intval($count_subtemplates); return $ret; } } ?> ================================================ FILE: ThinkPHP/Library/Vendor/Smarty/Smarty.class.php ================================================ * @author Uwe Tews * @author Rodney Rehm * @package Smarty * @version 3.1.6 */ /** * define shorthand directory separator constant */ if (!defined('DS')) { define('DS', DIRECTORY_SEPARATOR); } /** * set SMARTY_DIR to absolute path to Smarty library files. * Sets SMARTY_DIR only if user application has not already defined it. */ if (!defined('SMARTY_DIR')) { define('SMARTY_DIR', dirname(__FILE__) . DS); } /** * set SMARTY_SYSPLUGINS_DIR to absolute path to Smarty internal plugins. * Sets SMARTY_SYSPLUGINS_DIR only if user application has not already defined it. */ if (!defined('SMARTY_SYSPLUGINS_DIR')) { define('SMARTY_SYSPLUGINS_DIR', SMARTY_DIR . 'sysplugins' . DS); } if (!defined('SMARTY_PLUGINS_DIR')) { define('SMARTY_PLUGINS_DIR', SMARTY_DIR . 'plugins' . DS); } if (!defined('SMARTY_MBSTRING')) { define('SMARTY_MBSTRING', function_exists('mb_strlen')); } if (!defined('SMARTY_RESOURCE_CHAR_SET')) { // UTF-8 can only be done properly when mbstring is available! define('SMARTY_RESOURCE_CHAR_SET', SMARTY_MBSTRING ? 'UTF-8' : 'ISO-8859-1'); } if (!defined('SMARTY_RESOURCE_DATE_FORMAT')) { define('SMARTY_RESOURCE_DATE_FORMAT', '%b %e, %Y'); } /** * register the class autoloader */ if (!defined('SMARTY_SPL_AUTOLOAD')) { define('SMARTY_SPL_AUTOLOAD', 0); } if (SMARTY_SPL_AUTOLOAD && set_include_path(get_include_path() . PATH_SEPARATOR . SMARTY_SYSPLUGINS_DIR) !== false) { $registeredAutoLoadFunctions = spl_autoload_functions(); if (!isset($registeredAutoLoadFunctions['spl_autoload'])) { spl_autoload_register(); } } else { spl_autoload_register('smartyAutoload'); } /** * Load always needed external class files */ include_once SMARTY_SYSPLUGINS_DIR.'smarty_internal_data.php'; include_once SMARTY_SYSPLUGINS_DIR.'smarty_internal_templatebase.php'; include_once SMARTY_SYSPLUGINS_DIR.'smarty_internal_template.php'; include_once SMARTY_SYSPLUGINS_DIR.'smarty_resource.php'; include_once SMARTY_SYSPLUGINS_DIR.'smarty_internal_resource_file.php'; include_once SMARTY_SYSPLUGINS_DIR.'smarty_cacheresource.php'; include_once SMARTY_SYSPLUGINS_DIR.'smarty_internal_cacheresource_file.php'; /** * This is the main Smarty class * @package Smarty */ class Smarty extends Smarty_Internal_TemplateBase { /**#@+ * constant definitions */ /** * smarty version */ const SMARTY_VERSION = 'Smarty-3.1.6'; /** * define variable scopes */ const SCOPE_LOCAL = 0; const SCOPE_PARENT = 1; const SCOPE_ROOT = 2; const SCOPE_GLOBAL = 3; /** * define caching modes */ const CACHING_OFF = 0; const CACHING_LIFETIME_CURRENT = 1; const CACHING_LIFETIME_SAVED = 2; /** * define compile check modes */ const COMPILECHECK_OFF = 0; const COMPILECHECK_ON = 1; const COMPILECHECK_CACHEMISS = 2; /** * modes for handling of "" tags in templates. */ const PHP_PASSTHRU = 0; //-> print tags as plain text const PHP_QUOTE = 1; //-> escape tags as entities const PHP_REMOVE = 2; //-> escape tags as entities const PHP_ALLOW = 3; //-> escape tags as entities /** * filter types */ const FILTER_POST = 'post'; const FILTER_PRE = 'pre'; const FILTER_OUTPUT = 'output'; const FILTER_VARIABLE = 'variable'; /** * plugin types */ const PLUGIN_FUNCTION = 'function'; const PLUGIN_BLOCK = 'block'; const PLUGIN_COMPILER = 'compiler'; const PLUGIN_MODIFIER = 'modifier'; const PLUGIN_MODIFIERCOMPILER = 'modifiercompiler'; /**#@-*/ /** * assigned global tpl vars */ public static $global_tpl_vars = array(); /** * error handler returned by set_error_hanlder() in Smarty::muteExpectedErrors() */ public static $_previous_error_handler = null; /** * contains directories outside of SMARTY_DIR that are to be muted by muteExpectedErrors() */ public static $_muted_directories = array(); /**#@+ * variables */ /** * auto literal on delimiters with whitspace * @var boolean */ public $auto_literal = true; /** * display error on not assigned variables * @var boolean */ public $error_unassigned = false; /** * look up relative filepaths in include_path * @var boolean */ public $use_include_path = false; /** * template directory * @var array */ private $template_dir = array(); /** * joined template directory string used in cache keys * @var string */ public $joined_template_dir = null; /** * joined config directory string used in cache keys * @var string */ public $joined_config_dir = null; /** * default template handler * @var callable */ public $default_template_handler_func = null; /** * default config handler * @var callable */ public $default_config_handler_func = null; /** * default plugin handler * @var callable */ public $default_plugin_handler_func = null; /** * compile directory * @var string */ private $compile_dir = null; /** * plugins directory * @var array */ private $plugins_dir = array(); /** * cache directory * @var string */ private $cache_dir = null; /** * config directory * @var array */ private $config_dir = array(); /** * force template compiling? * @var boolean */ public $force_compile = false; /** * check template for modifications? * @var boolean */ public $compile_check = true; /** * use sub dirs for compiled/cached files? * @var boolean */ public $use_sub_dirs = false; /** * allow ambiguous resources (that are made unique by the resource handler) * @var boolean */ public $allow_ambiguous_resources = false; /** * caching enabled * @var boolean */ public $caching = false; /** * merge compiled includes * @var boolean */ public $merge_compiled_includes = false; /** * cache lifetime in seconds * @var integer */ public $cache_lifetime = 3600; /** * force cache file creation * @var boolean */ public $force_cache = false; /** * Set this if you want different sets of cache files for the same * templates. * * @var string */ public $cache_id = null; /** * Set this if you want different sets of compiled files for the same * templates. * * @var string */ public $compile_id = null; /** * template left-delimiter * @var string */ public $left_delimiter = "{"; /** * template right-delimiter * @var string */ public $right_delimiter = "}"; /**#@+ * security */ /** * class name * * This should be instance of Smarty_Security. * * @var string * @see Smarty_Security */ public $security_class = 'Smarty_Security'; /** * implementation of security class * * @var Smarty_Security */ public $security_policy = null; /** * controls handling of PHP-blocks * * @var integer */ public $php_handling = self::PHP_PASSTHRU; /** * controls if the php template file resource is allowed * * @var bool */ public $allow_php_templates = false; /** * Should compiled-templates be prevented from being called directly? * * {@internal * Currently used by Smarty_Internal_Template only. * }} * * @var boolean */ public $direct_access_security = true; /**#@-*/ /** * debug mode * * Setting this to true enables the debug-console. * * @var boolean */ public $debugging = false; /** * This determines if debugging is enable-able from the browser. *
      *
    • NONE => no debugging control allowed
    • *
    • URL => enable debugging when SMARTY_DEBUG is found in the URL.
    • *
    * @var string */ public $debugging_ctrl = 'NONE'; /** * Name of debugging URL-param. * * Only used when $debugging_ctrl is set to 'URL'. * The name of the URL-parameter that activates debugging. * * @var type */ public $smarty_debug_id = 'SMARTY_DEBUG'; /** * Path of debug template. * @var string */ public $debug_tpl = null; /** * When set, smarty uses this value as error_reporting-level. * @var int */ public $error_reporting = null; /** * Internal flag for getTags() * @var boolean */ public $get_used_tags = false; /**#@+ * config var settings */ /** * Controls whether variables with the same name overwrite each other. * @var boolean */ public $config_overwrite = true; /** * Controls whether config values of on/true/yes and off/false/no get converted to boolean. * @var boolean */ public $config_booleanize = true; /** * Controls whether hidden config sections/vars are read from the file. * @var boolean */ public $config_read_hidden = false; /**#@-*/ /**#@+ * resource locking */ /** * locking concurrent compiles * @var boolean */ public $compile_locking = true; /** * Controls whether cache resources should emply locking mechanism * @var boolean */ public $cache_locking = false; /** * seconds to wait for acquiring a lock before ignoring the write lock * @var float */ public $locking_timeout = 10; /**#@-*/ /** * global template functions * @var array */ public $template_functions = array(); /** * resource type used if none given * * Must be an valid key of $registered_resources. * @var string */ public $default_resource_type = 'file'; /** * caching type * * Must be an element of $cache_resource_types. * * @var string */ public $caching_type = 'file'; /** * internal config properties * @var array */ public $properties = array(); /** * config type * @var string */ public $default_config_type = 'file'; /** * cached template objects * @var array */ public $template_objects = array(); /** * check If-Modified-Since headers * @var boolean */ public $cache_modified_check = false; /** * registered plugins * @var array */ public $registered_plugins = array(); /** * plugin search order * @var array */ public $plugin_search_order = array('function', 'block', 'compiler', 'class'); /** * registered objects * @var array */ public $registered_objects = array(); /** * registered classes * @var array */ public $registered_classes = array(); /** * registered filters * @var array */ public $registered_filters = array(); /** * registered resources * @var array */ public $registered_resources = array(); /** * resource handler cache * @var array */ public $_resource_handlers = array(); /** * registered cache resources * @var array */ public $registered_cache_resources = array(); /** * cache resource handler cache * @var array */ public $_cacheresource_handlers = array(); /** * autoload filter * @var array */ public $autoload_filters = array(); /** * default modifier * @var array */ public $default_modifiers = array(); /** * autoescape variable output * @var boolean */ public $escape_html = false; /** * global internal smarty vars * @var array */ public static $_smarty_vars = array(); /** * start time for execution time calculation * @var int */ public $start_time = 0; /** * default file permissions * @var int */ public $_file_perms = 0644; /** * default dir permissions * @var int */ public $_dir_perms = 0771; /** * block tag hierarchy * @var array */ public $_tag_stack = array(); /** * self pointer to Smarty object * @var Smarty */ public $smarty; /** * required by the compiler for BC * @var string */ public $_current_file = null; /** * internal flag to enable parser debugging * @var bool */ public $_parserdebug = false; /** * Saved parameter of merged templates during compilation * * @var array */ public $merged_templates_func = array(); /**#@-*/ /** * Initialize new Smarty object * */ public function __construct() { // selfpointer needed by some other class methods $this->smarty = $this; if (is_callable('mb_internal_encoding')) { mb_internal_encoding(SMARTY_RESOURCE_CHAR_SET); } $this->start_time = microtime(true); // set default dirs $this->setTemplateDir('.' . DS . 'templates' . DS) ->setCompileDir('.' . DS . 'templates_c' . DS) ->setPluginsDir(SMARTY_PLUGINS_DIR) ->setCacheDir('.' . DS . 'cache' . DS) ->setConfigDir('.' . DS . 'configs' . DS); $this->debug_tpl = 'file:' . dirname(__FILE__) . '/debug.tpl'; if (isset($_SERVER['SCRIPT_NAME'])) { $this->assignGlobal('SCRIPT_NAME', $_SERVER['SCRIPT_NAME']); } } /** * Class destructor */ public function __destruct() { // intentionally left blank } /** * <> set selfpointer on cloned object */ public function __clone() { $this->smarty = $this; } /** * <> Generic getter. * * Calls the appropriate getter function. * Issues an E_USER_NOTICE if no valid getter is found. * * @param string $name property name * @return mixed */ public function __get($name) { $allowed = array( 'template_dir' => 'getTemplateDir', 'config_dir' => 'getConfigDir', 'plugins_dir' => 'getPluginsDir', 'compile_dir' => 'getCompileDir', 'cache_dir' => 'getCacheDir', ); if (isset($allowed[$name])) { return $this->{$allowed[$name]}(); } else { trigger_error('Undefined property: '. get_class($this) .'::$'. $name, E_USER_NOTICE); } } /** * <> Generic setter. * * Calls the appropriate setter function. * Issues an E_USER_NOTICE if no valid setter is found. * * @param string $name property name * @param mixed $value parameter passed to setter */ public function __set($name, $value) { $allowed = array( 'template_dir' => 'setTemplateDir', 'config_dir' => 'setConfigDir', 'plugins_dir' => 'setPluginsDir', 'compile_dir' => 'setCompileDir', 'cache_dir' => 'setCacheDir', ); if (isset($allowed[$name])) { $this->{$allowed[$name]}($value); } else { trigger_error('Undefined property: ' . get_class($this) . '::$' . $name, E_USER_NOTICE); } } /** * Check if a template resource exists * * @param string $resource_name template name * @return boolean status */ public function templateExists($resource_name) { // create template object $save = $this->template_objects; $tpl = new $this->template_class($resource_name, $this); // check if it does exists $result = $tpl->source->exists; $this->template_objects = $save; return $result; } /** * Returns a single or all global variables * * @param object $smarty * @param string $varname variable name or null * @return string variable value or or array of variables */ public function getGlobal($varname = null) { if (isset($varname)) { if (isset(self::$global_tpl_vars[$varname])) { return self::$global_tpl_vars[$varname]->value; } else { return ''; } } else { $_result = array(); foreach (self::$global_tpl_vars AS $key => $var) { $_result[$key] = $var->value; } return $_result; } } /** * Empty cache folder * * @param integer $exp_time expiration time * @param string $type resource type * @return integer number of cache files deleted */ function clearAllCache($exp_time = null, $type = null) { // load cache resource and call clearAll $_cache_resource = Smarty_CacheResource::load($this, $type); Smarty_CacheResource::invalidLoadedCache($this); return $_cache_resource->clearAll($this, $exp_time); } /** * Empty cache for a specific template * * @param string $template_name template name * @param string $cache_id cache id * @param string $compile_id compile id * @param integer $exp_time expiration time * @param string $type resource type * @return integer number of cache files deleted */ public function clearCache($template_name, $cache_id = null, $compile_id = null, $exp_time = null, $type = null) { // load cache resource and call clear $_cache_resource = Smarty_CacheResource::load($this, $type); Smarty_CacheResource::invalidLoadedCache($this); return $_cache_resource->clear($this, $template_name, $cache_id, $compile_id, $exp_time); } /** * Loads security class and enables security * * @param string|Smarty_Security $security_class if a string is used, it must be class-name * @return Smarty current Smarty instance for chaining * @throws SmartyException when an invalid class name is provided */ public function enableSecurity($security_class = null) { if ($security_class instanceof Smarty_Security) { $this->security_policy = $security_class; return $this; } elseif (is_object($security_class)) { throw new SmartyException("Class '" . get_class($security_class) . "' must extend Smarty_Security."); } if ($security_class == null) { $security_class = $this->security_class; } if (!class_exists($security_class)) { throw new SmartyException("Security class '$security_class' is not defined"); } elseif ($security_class !== 'Smarty_Security' && !is_subclass_of($security_class, 'Smarty_Security')) { throw new SmartyException("Class '$security_class' must extend Smarty_Security."); } else { $this->security_policy = new $security_class($this); } return $this; } /** * Disable security * @return Smarty current Smarty instance for chaining */ public function disableSecurity() { $this->security_policy = null; return $this; } /** * Set template directory * * @param string|array $template_dir directory(s) of template sources * @return Smarty current Smarty instance for chaining */ public function setTemplateDir($template_dir) { $this->template_dir = array(); foreach ((array) $template_dir as $k => $v) { $this->template_dir[$k] = rtrim($v, '/\\') . DS; } $this->joined_template_dir = join(DIRECTORY_SEPARATOR, $this->template_dir); return $this; } /** * Add template directory(s) * * @param string|array $template_dir directory(s) of template sources * @param string $key of the array element to assign the template dir to * @return Smarty current Smarty instance for chaining * @throws SmartyException when the given template directory is not valid */ public function addTemplateDir($template_dir, $key=null) { // make sure we're dealing with an array $this->template_dir = (array) $this->template_dir; if (is_array($template_dir)) { foreach ($template_dir as $k => $v) { if (is_int($k)) { // indexes are not merged but appended $this->template_dir[] = rtrim($v, '/\\') . DS; } else { // string indexes are overridden $this->template_dir[$k] = rtrim($v, '/\\') . DS; } } } elseif ($key !== null) { // override directory at specified index $this->template_dir[$key] = rtrim($template_dir, '/\\') . DS; } else { // append new directory $this->template_dir[] = rtrim($template_dir, '/\\') . DS; } $this->joined_template_dir = join(DIRECTORY_SEPARATOR, $this->template_dir); return $this; } /** * Get template directories * * @param mixed index of directory to get, null to get all * @return array|string list of template directories, or directory of $index */ public function getTemplateDir($index=null) { if ($index !== null) { return isset($this->template_dir[$index]) ? $this->template_dir[$index] : null; } return (array)$this->template_dir; } /** * Set config directory * * @param string|array $template_dir directory(s) of configuration sources * @return Smarty current Smarty instance for chaining */ public function setConfigDir($config_dir) { $this->config_dir = array(); foreach ((array) $config_dir as $k => $v) { $this->config_dir[$k] = rtrim($v, '/\\') . DS; } $this->joined_config_dir = join(DIRECTORY_SEPARATOR, $this->config_dir); return $this; } /** * Add config directory(s) * * @param string|array $config_dir directory(s) of config sources * @param string key of the array element to assign the config dir to * @return Smarty current Smarty instance for chaining */ public function addConfigDir($config_dir, $key=null) { // make sure we're dealing with an array $this->config_dir = (array) $this->config_dir; if (is_array($config_dir)) { foreach ($config_dir as $k => $v) { if (is_int($k)) { // indexes are not merged but appended $this->config_dir[] = rtrim($v, '/\\') . DS; } else { // string indexes are overridden $this->config_dir[$k] = rtrim($v, '/\\') . DS; } } } elseif( $key !== null ) { // override directory at specified index $this->config_dir[$key] = rtrim($config_dir, '/\\') . DS; } else { // append new directory $this->config_dir[] = rtrim($config_dir, '/\\') . DS; } $this->joined_config_dir = join(DIRECTORY_SEPARATOR, $this->config_dir); return $this; } /** * Get config directory * * @param mixed index of directory to get, null to get all * @return array|string configuration directory */ public function getConfigDir($index=null) { if ($index !== null) { return isset($this->config_dir[$index]) ? $this->config_dir[$index] : null; } return (array)$this->config_dir; } /** * Set plugins directory * * @param string|array $plugins_dir directory(s) of plugins * @return Smarty current Smarty instance for chaining */ public function setPluginsDir($plugins_dir) { $this->plugins_dir = array(); foreach ((array)$plugins_dir as $k => $v) { $this->plugins_dir[$k] = rtrim($v, '/\\') . DS; } return $this; } /** * Adds directory of plugin files * * @param object $smarty * @param string $ |array $ plugins folder * @return Smarty current Smarty instance for chaining */ public function addPluginsDir($plugins_dir) { // make sure we're dealing with an array $this->plugins_dir = (array) $this->plugins_dir; if (is_array($plugins_dir)) { foreach ($plugins_dir as $k => $v) { if (is_int($k)) { // indexes are not merged but appended $this->plugins_dir[] = rtrim($v, '/\\') . DS; } else { // string indexes are overridden $this->plugins_dir[$k] = rtrim($v, '/\\') . DS; } } } else { // append new directory $this->plugins_dir[] = rtrim($plugins_dir, '/\\') . DS; } $this->plugins_dir = array_unique($this->plugins_dir); return $this; } /** * Get plugin directories * * @return array list of plugin directories */ public function getPluginsDir() { return (array)$this->plugins_dir; } /** * Set compile directory * * @param string $compile_dir directory to store compiled templates in * @return Smarty current Smarty instance for chaining */ public function setCompileDir($compile_dir) { $this->compile_dir = rtrim($compile_dir, '/\\') . DS; if (!isset(Smarty::$_muted_directories[$this->compile_dir])) { Smarty::$_muted_directories[$this->compile_dir] = null; } return $this; } /** * Get compiled directory * * @return string path to compiled templates */ public function getCompileDir() { return $this->compile_dir; } /** * Set cache directory * * @param string $cache_dir directory to store cached templates in * @return Smarty current Smarty instance for chaining */ public function setCacheDir($cache_dir) { $this->cache_dir = rtrim($cache_dir, '/\\') . DS; if (!isset(Smarty::$_muted_directories[$this->cache_dir])) { Smarty::$_muted_directories[$this->cache_dir] = null; } return $this; } /** * Get cache directory * * @return string path of cache directory */ public function getCacheDir() { return $this->cache_dir; } /** * Set default modifiers * * @param array|string $modifiers modifier or list of modifiers to set * @return Smarty current Smarty instance for chaining */ public function setDefaultModifiers($modifiers) { $this->default_modifiers = (array) $modifiers; return $this; } /** * Add default modifiers * * @param array|string $modifiers modifier or list of modifiers to add * @return Smarty current Smarty instance for chaining */ public function addDefaultModifiers($modifiers) { if (is_array($modifiers)) { $this->default_modifiers = array_merge($this->default_modifiers, $modifiers); } else { $this->default_modifiers[] = $modifiers; } return $this; } /** * Get default modifiers * * @return array list of default modifiers */ public function getDefaultModifiers() { return $this->default_modifiers; } /** * Set autoload filters * * @param array $filters filters to load automatically * @param string $type "pre", "output", … specify the filter type to set. Defaults to none treating $filters' keys as the appropriate types * @return Smarty current Smarty instance for chaining */ public function setAutoloadFilters($filters, $type=null) { if ($type !== null) { $this->autoload_filters[$type] = (array) $filters; } else { $this->autoload_filters = (array) $filters; } return $this; } /** * Add autoload filters * * @param array $filters filters to load automatically * @param string $type "pre", "output", … specify the filter type to set. Defaults to none treating $filters' keys as the appropriate types * @return Smarty current Smarty instance for chaining */ public function addAutoloadFilters($filters, $type=null) { if ($type !== null) { if (!empty($this->autoload_filters[$type])) { $this->autoload_filters[$type] = array_merge($this->autoload_filters[$type], (array) $filters); } else { $this->autoload_filters[$type] = (array) $filters; } } else { foreach ((array) $filters as $key => $value) { if (!empty($this->autoload_filters[$key])) { $this->autoload_filters[$key] = array_merge($this->autoload_filters[$key], (array) $value); } else { $this->autoload_filters[$key] = (array) $value; } } } return $this; } /** * Get autoload filters * * @param string $type type of filter to get autoloads for. Defaults to all autoload filters * @return array array( 'type1' => array( 'filter1', 'filter2', … ) ) or array( 'filter1', 'filter2', …) if $type was specified */ public function getAutoloadFilters($type=null) { if ($type !== null) { return isset($this->autoload_filters[$type]) ? $this->autoload_filters[$type] : array(); } return $this->autoload_filters; } /** * return name of debugging template * * @return string */ public function getDebugTemplate() { return $this->debug_tpl; } /** * set the debug template * * @param string $tpl_name * @return Smarty current Smarty instance for chaining * @throws SmartyException if file is not readable */ public function setDebugTemplate($tpl_name) { if (!is_readable($tpl_name)) { throw new SmartyException("Unknown file '{$tpl_name}'"); } $this->debug_tpl = $tpl_name; return $this; } /** * creates a template object * * @param string $template the resource handle of the template file * @param mixed $cache_id cache id to be used with this template * @param mixed $compile_id compile id to be used with this template * @param object $parent next higher level of Smarty variables * @param boolean $do_clone flag is Smarty object shall be cloned * @return object template object */ public function createTemplate($template, $cache_id = null, $compile_id = null, $parent = null, $do_clone = true) { if (!empty($cache_id) && (is_object($cache_id) || is_array($cache_id))) { $parent = $cache_id; $cache_id = null; } if (!empty($parent) && is_array($parent)) { $data = $parent; $parent = null; } else { $data = null; } // default to cache_id and compile_id of Smarty object $cache_id = $cache_id === null ? $this->cache_id : $cache_id; $compile_id = $compile_id === null ? $this->compile_id : $compile_id; // already in template cache? if ($this->allow_ambiguous_resources) { $_templateId = Smarty_Resource::getUniqueTemplateName($this, $template) . $cache_id . $compile_id; } else { $_templateId = $this->joined_template_dir . '#' . $template . $cache_id . $compile_id; } if (isset($_templateId[150])) { $_templateId = sha1($_templateId); } if ($do_clone) { if (isset($this->template_objects[$_templateId])) { // return cached template object $tpl = clone $this->template_objects[$_templateId]; $tpl->smarty = clone $tpl->smarty; $tpl->parent = $parent; $tpl->tpl_vars = array(); $tpl->config_vars = array(); } else { $tpl = new $this->template_class($template, clone $this, $parent, $cache_id, $compile_id); } } else { if (isset($this->template_objects[$_templateId])) { // return cached template object $tpl = $this->template_objects[$_templateId]; $tpl->parent = $parent; $tpl->tpl_vars = array(); $tpl->config_vars = array(); } else { $tpl = new $this->template_class($template, $this, $parent, $cache_id, $compile_id); } } // fill data if present if (!empty($data) && is_array($data)) { // set up variable values foreach ($data as $_key => $_val) { $tpl->tpl_vars[$_key] = new Smarty_variable($_val); } } return $tpl; } /** * Takes unknown classes and loads plugin files for them * class name format: Smarty_PluginType_PluginName * plugin filename format: plugintype.pluginname.php * * @param string $plugin_name class plugin name to load * @param bool $check check if already loaded * @return string |boolean filepath of loaded file or false */ public function loadPlugin($plugin_name, $check = true) { // if function or class exists, exit silently (already loaded) if ($check && (is_callable($plugin_name) || class_exists($plugin_name, false))) { return true; } // Plugin name is expected to be: Smarty_[Type]_[Name] $_name_parts = explode('_', $plugin_name, 3); // class name must have three parts to be valid plugin // count($_name_parts) < 3 === !isset($_name_parts[2]) if (!isset($_name_parts[2]) || strtolower($_name_parts[0]) !== 'smarty') { throw new SmartyException("plugin {$plugin_name} is not a valid name format"); return false; } // if type is "internal", get plugin from sysplugins if (strtolower($_name_parts[1]) == 'internal') { $file = SMARTY_SYSPLUGINS_DIR . strtolower($plugin_name) . '.php'; if (file_exists($file)) { require_once($file); return $file; } else { return false; } } // plugin filename is expected to be: [type].[name].php $_plugin_filename = "{$_name_parts[1]}.{$_name_parts[2]}.php"; // loop through plugin dirs and find the plugin foreach($this->getPluginsDir() as $_plugin_dir) { $names = array( $_plugin_dir . $_plugin_filename, $_plugin_dir . strtolower($_plugin_filename), ); foreach ($names as $file) { if (file_exists($file)) { require_once($file); return $file; } if ($this->use_include_path && !preg_match('/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/', $_plugin_dir)) { // try PHP include_path if (($file = Smarty_Internal_Get_Include_Path::getIncludePath($file)) !== false) { require_once($file); return $file; } } } } // no plugin loaded return false; } /** * Compile all template files * * @param string $extension file extension * @param bool $force_compile force all to recompile * @param int $time_limit * @param int $max_errors * @return integer number of template files recompiled */ public function compileAllTemplates($extention = '.tpl', $force_compile = false, $time_limit = 0, $max_errors = null) { return Smarty_Internal_Utility::compileAllTemplates($extention, $force_compile, $time_limit, $max_errors, $this); } /** * Compile all config files * * @param string $extension file extension * @param bool $force_compile force all to recompile * @param int $time_limit * @param int $max_errors * @return integer number of template files recompiled */ public function compileAllConfig($extention = '.conf', $force_compile = false, $time_limit = 0, $max_errors = null) { return Smarty_Internal_Utility::compileAllConfig($extention, $force_compile, $time_limit, $max_errors, $this); } /** * Delete compiled template file * * @param string $resource_name template name * @param string $compile_id compile id * @param integer $exp_time expiration time * @return integer number of template files deleted */ public function clearCompiledTemplate($resource_name = null, $compile_id = null, $exp_time = null) { return Smarty_Internal_Utility::clearCompiledTemplate($resource_name, $compile_id, $exp_time, $this); } /** * Return array of tag/attributes of all tags used by an template * * @param object $templae template object * @return array of tag/attributes */ public function getTags(Smarty_Internal_Template $template) { return Smarty_Internal_Utility::getTags($template); } /** * Run installation test * * @param array $errors Array to write errors into, rather than outputting them * @return boolean true if setup is fine, false if something is wrong */ public function testInstall(&$errors=null) { return Smarty_Internal_Utility::testInstall($this, $errors); } /** * Error Handler to mute expected messages * * @link http://php.net/set_error_handler * @param integer $errno Error level * @return boolean */ public static function mutingErrorHandler($errno, $errstr, $errfile, $errline, $errcontext) { $_is_muted_directory = false; // add the SMARTY_DIR to the list of muted directories if (!isset(Smarty::$_muted_directories[SMARTY_DIR])) { $smarty_dir = realpath(SMARTY_DIR); Smarty::$_muted_directories[SMARTY_DIR] = array( 'file' => $smarty_dir, 'length' => strlen($smarty_dir), ); } // walk the muted directories and test against $errfile foreach (Smarty::$_muted_directories as $key => &$dir) { if (!$dir) { // resolve directory and length for speedy comparisons $file = realpath($key); $dir = array( 'file' => $file, 'length' => strlen($file), ); } if (!strncmp($errfile, $dir['file'], $dir['length'])) { $_is_muted_directory = true; break; } } // pass to next error handler if this error did not occur inside SMARTY_DIR // or the error was within smarty but masked to be ignored if (!$_is_muted_directory || ($errno && $errno & error_reporting())) { if (Smarty::$_previous_error_handler) { return call_user_func(Smarty::$_previous_error_handler, $errno, $errstr, $errfile, $errline, $errcontext); } else { return false; } } } /** * Enable error handler to mute expected messages * * @return void */ public static function muteExpectedErrors() { /* error muting is done because some people implemented custom error_handlers using http://php.net/set_error_handler and for some reason did not understand the following paragraph: It is important to remember that the standard PHP error handler is completely bypassed for the error types specified by error_types unless the callback function returns FALSE. error_reporting() settings will have no effect and your error handler will be called regardless - however you are still able to read the current value of error_reporting and act appropriately. Of particular note is that this value will be 0 if the statement that caused the error was prepended by the @ error-control operator. Smarty deliberately uses @filemtime() over file_exists() and filemtime() in some places. Reasons include - @filemtime() is almost twice as fast as using an additional file_exists() - between file_exists() and filemtime() a possible race condition is opened, which does not exist using the simple @filemtime() approach. */ $error_handler = array('Smarty', 'mutingErrorHandler'); $previous = set_error_handler($error_handler); // avoid dead loops if ($previous !== $error_handler) { Smarty::$_previous_error_handler = $previous; } } /** * Disable error handler muting expected messages * * @return void */ public static function unmuteExpectedErrors() { restore_error_handler(); } } /** * Smarty exception class * @package Smarty */ class SmartyException extends Exception { } /** * Smarty compiler exception class * @package Smarty */ class SmartyCompilerException extends SmartyException { } /** * Autoloader */ function smartyAutoload($class) { $_class = strtolower($class); $_classes = array( 'smarty_config_source' => true, 'smarty_config_compiled' => true, 'smarty_security' => true, 'smarty_cacheresource' => true, 'smarty_cacheresource_custom' => true, 'smarty_cacheresource_keyvaluestore' => true, 'smarty_resource' => true, 'smarty_resource_custom' => true, 'smarty_resource_uncompiled' => true, 'smarty_resource_recompiled' => true, ); if (!strncmp($_class, 'smarty_internal_', 16) || isset($_classes[$_class])) { include SMARTY_SYSPLUGINS_DIR . $_class . '.php'; } } ?> ================================================ FILE: ThinkPHP/Library/Vendor/Smarty/SmartyBC.class.php ================================================ * @author Uwe Tews * @author Rodney Rehm * @package Smarty */ /** * @ignore */ require(dirname(__FILE__) . '/Smarty.class.php'); /** * Smarty Backward Compatability Wrapper Class * * @package Smarty */ class SmartyBC extends Smarty { /** * Smarty 2 BC * @var string */ public $_version = self::SMARTY_VERSION; /** * Initialize new SmartyBC object * * @param array $options options to set during initialization, e.g. array( 'forceCompile' => false ) */ public function __construct(array $options=array()) { parent::__construct($options); // register {php} tag $this->registerPlugin('block', 'php', 'smarty_php_tag'); } /** * wrapper for assign_by_ref * * @param string $tpl_var the template variable name * @param mixed &$value the referenced value to assign */ public function assign_by_ref($tpl_var, &$value) { $this->assignByRef($tpl_var, $value); } /** * wrapper for append_by_ref * * @param string $tpl_var the template variable name * @param mixed &$value the referenced value to append * @param boolean $merge flag if array elements shall be merged */ public function append_by_ref($tpl_var, &$value, $merge = false) { $this->appendByRef($tpl_var, $value, $merge); } /** * clear the given assigned template variable. * * @param string $tpl_var the template variable to clear */ public function clear_assign($tpl_var) { $this->clearAssign($tpl_var); } /** * Registers custom function to be used in templates * * @param string $function the name of the template function * @param string $function_impl the name of the PHP function to register * @param bool $cacheable * @param mixed $cache_attrs */ public function register_function($function, $function_impl, $cacheable=true, $cache_attrs=null) { $this->registerPlugin('function', $function, $function_impl, $cacheable, $cache_attrs); } /** * Unregisters custom function * * @param string $function name of template function */ public function unregister_function($function) { $this->unregisterPlugin('function', $function); } /** * Registers object to be used in templates * * @param string $object name of template object * @param object $object_impl the referenced PHP object to register * @param array $allowed list of allowed methods (empty = all) * @param boolean $smarty_args smarty argument format, else traditional * @param array $block_functs list of methods that are block format */ public function register_object($object, $object_impl, $allowed = array(), $smarty_args = true, $block_methods = array()) { settype($allowed, 'array'); settype($smarty_args, 'boolean'); $this->registerObject($object, $object_impl, $allowed, $smarty_args, $block_methods); } /** * Unregisters object * * @param string $object name of template object */ public function unregister_object($object) { $this->unregisterObject($object); } /** * Registers block function to be used in templates * * @param string $block name of template block * @param string $block_impl PHP function to register * @param bool $cacheable * @param mixed $cache_attrs */ public function register_block($block, $block_impl, $cacheable=true, $cache_attrs=null) { $this->registerPlugin('block', $block, $block_impl, $cacheable, $cache_attrs); } /** * Unregisters block function * * @param string $block name of template function */ public function unregister_block($block) { $this->unregisterPlugin('block', $block); } /** * Registers compiler function * * @param string $function name of template function * @param string $function_impl name of PHP function to register * @param bool $cacheable */ public function register_compiler_function($function, $function_impl, $cacheable=true) { $this->registerPlugin('compiler', $function, $function_impl, $cacheable); } /** * Unregisters compiler function * * @param string $function name of template function */ public function unregister_compiler_function($function) { $this->unregisterPlugin('compiler', $function); } /** * Registers modifier to be used in templates * * @param string $modifier name of template modifier * @param string $modifier_impl name of PHP function to register */ public function register_modifier($modifier, $modifier_impl) { $this->registerPlugin('modifier', $modifier, $modifier_impl); } /** * Unregisters modifier * * @param string $modifier name of template modifier */ public function unregister_modifier($modifier) { $this->unregisterPlugin('modifier', $modifier); } /** * Registers a resource to fetch a template * * @param string $type name of resource * @param array $functions array of functions to handle resource */ public function register_resource($type, $functions) { $this->registerResource($type, $functions); } /** * Unregisters a resource * * @param string $type name of resource */ public function unregister_resource($type) { $this->unregisterResource($type); } /** * Registers a prefilter function to apply * to a template before compiling * * @param callable $function */ public function register_prefilter($function) { $this->registerFilter('pre', $function); } /** * Unregisters a prefilter function * * @param callable $function */ public function unregister_prefilter($function) { $this->unregisterFilter('pre', $function); } /** * Registers a postfilter function to apply * to a compiled template after compilation * * @param callable $function */ public function register_postfilter($function) { $this->registerFilter('post', $function); } /** * Unregisters a postfilter function * * @param callable $function */ public function unregister_postfilter($function) { $this->unregisterFilter('post', $function); } /** * Registers an output filter function to apply * to a template output * * @param callable $function */ public function register_outputfilter($function) { $this->registerFilter('output', $function); } /** * Unregisters an outputfilter function * * @param callable $function */ public function unregister_outputfilter($function) { $this->unregisterFilter('output', $function); } /** * load a filter of specified type and name * * @param string $type filter type * @param string $name filter name */ public function load_filter($type, $name) { $this->loadFilter($type, $name); } /** * clear cached content for the given template and cache id * * @param string $tpl_file name of template file * @param string $cache_id name of cache_id * @param string $compile_id name of compile_id * @param string $exp_time expiration time * @return boolean */ public function clear_cache($tpl_file = null, $cache_id = null, $compile_id = null, $exp_time = null) { return $this->clearCache($tpl_file, $cache_id, $compile_id, $exp_time); } /** * clear the entire contents of cache (all templates) * * @param string $exp_time expire time * @return boolean */ public function clear_all_cache($exp_time = null) { return $this->clearCache(null, null, null, $exp_time); } /** * test to see if valid cache exists for this template * * @param string $tpl_file name of template file * @param string $cache_id * @param string $compile_id * @return boolean */ public function is_cached($tpl_file, $cache_id = null, $compile_id = null) { return $this->isCached($tpl_file, $cache_id, $compile_id); } /** * clear all the assigned template variables. */ public function clear_all_assign() { $this->clearAllAssign(); } /** * clears compiled version of specified template resource, * or all compiled template files if one is not specified. * This function is for advanced use only, not normally needed. * * @param string $tpl_file * @param string $compile_id * @param string $exp_time * @return boolean results of {@link smarty_core_rm_auto()} */ public function clear_compiled_tpl($tpl_file = null, $compile_id = null, $exp_time = null) { return $this->clearCompiledTemplate($tpl_file, $compile_id, $exp_time); } /** * Checks whether requested template exists. * * @param string $tpl_file * @return boolean */ public function template_exists($tpl_file) { return $this->templateExists($tpl_file); } /** * Returns an array containing template variables * * @param string $name * @return array */ public function get_template_vars($name=null) { return $this->getTemplateVars($name); } /** * Returns an array containing config variables * * @param string $name * @return array */ public function get_config_vars($name=null) { return $this->getConfigVars($name); } /** * load configuration values * * @param string $file * @param string $section * @param string $scope */ public function config_load($file, $section = null, $scope = 'global') { $this->ConfigLoad($file, $section, $scope); } /** * return a reference to a registered object * * @param string $name * @return object */ public function get_registered_object($name) { return $this->getRegisteredObject($name); } /** * clear configuration values * * @param string $var */ public function clear_config($var = null) { $this->clearConfig($var); } /** * trigger Smarty error * * @param string $error_msg * @param integer $error_type */ public function trigger_error($error_msg, $error_type = E_USER_WARNING) { trigger_error("Smarty error: $error_msg", $error_type); } } /** * Smarty {php}{/php} block function * * @param array $params parameter list * @param string $content contents of the block * @param object $template template object * @param boolean &$repeat repeat flag * @return string content re-formatted */ function smarty_php_tag($params, $content, $template, &$repeat) { eval($content); return ''; } ?> ================================================ FILE: ThinkPHP/Library/Vendor/Smarty/debug.tpl ================================================ {capture name='_smarty_debug' assign=debug_output} Smarty Debug Console

    Smarty Debug Console - {if isset($template_name)}{$template_name|debug_print_var nofilter}{else}Total Time {$execution_time|string_format:"%.5f"}{/if}

    {if !empty($template_data)}

    included templates & config files (load time in seconds)

    {foreach $template_data as $template} {$template.name} (compile {$template['compile_time']|string_format:"%.5f"}) (render {$template['render_time']|string_format:"%.5f"}) (cache {$template['cache_time']|string_format:"%.5f"})
    {/foreach}
    {/if}

    assigned template variables

    {foreach $assigned_vars as $vars} {/foreach}
    ${$vars@key|escape:'html'} {$vars|debug_print_var nofilter}

    assigned config file variables (outer template scope)

    {foreach $config_vars as $vars} {/foreach}
    {$vars@key|escape:'html'} {$vars|debug_print_var nofilter}
    {/capture} ================================================ FILE: ThinkPHP/Library/Vendor/Smarty/plugins/block.textformat.php ================================================ * Name: textformat
    * Purpose: format text a certain way with preset styles * or custom wrap/indent settings
    * Params: *
     * - style         - string (email)
     * - indent        - integer (0)
     * - wrap          - integer (80)
     * - wrap_char     - string ("\n")
     * - indent_char   - string (" ")
     * - wrap_boundary - boolean (true)
     * 
    * * @link http://www.smarty.net/manual/en/language.function.textformat.php {textformat} * (Smarty online manual) * @param array $params parameters * @param string $content contents of the block * @param Smarty_Internal_Template $template template object * @param boolean &$repeat repeat flag * @return string content re-formatted * @author Monte Ohrt */ function smarty_block_textformat($params, $content, $template, &$repeat) { if (is_null($content)) { return; } $style = null; $indent = 0; $indent_first = 0; $indent_char = ' '; $wrap = 80; $wrap_char = "\n"; $wrap_cut = false; $assign = null; foreach ($params as $_key => $_val) { switch ($_key) { case 'style': case 'indent_char': case 'wrap_char': case 'assign': $$_key = (string)$_val; break; case 'indent': case 'indent_first': case 'wrap': $$_key = (int)$_val; break; case 'wrap_cut': $$_key = (bool)$_val; break; default: trigger_error("textformat: unknown attribute '$_key'"); } } if ($style == 'email') { $wrap = 72; } // split into paragraphs $_paragraphs = preg_split('![\r\n]{2}!', $content); $_output = ''; foreach ($_paragraphs as &$_paragraph) { if (!$_paragraph) { continue; } // convert mult. spaces & special chars to single space $_paragraph = preg_replace(array('!\s+!u', '!(^\s+)|(\s+$)!u'), array(' ', ''), $_paragraph); // indent first line if ($indent_first > 0) { $_paragraph = str_repeat($indent_char, $indent_first) . $_paragraph; } // wordwrap sentences if (SMARTY_MBSTRING /* ^phpunit */&&empty($_SERVER['SMARTY_PHPUNIT_DISABLE_MBSTRING'])/* phpunit$ */) { require_once(SMARTY_PLUGINS_DIR . 'shared.mb_wordwrap.php'); $_paragraph = smarty_mb_wordwrap($_paragraph, $wrap - $indent, $wrap_char, $wrap_cut); } else { $_paragraph = wordwrap($_paragraph, $wrap - $indent, $wrap_char, $wrap_cut); } // indent lines if ($indent > 0) { $_paragraph = preg_replace('!^!m', str_repeat($indent_char, $indent), $_paragraph); } } $_output = implode($wrap_char . $wrap_char, $_paragraphs); if ($assign) { $template->assign($assign, $_output); } else { return $_output; } } ?> ================================================ FILE: ThinkPHP/Library/Vendor/Smarty/plugins/function.counter.php ================================================ * Name: counter
    * Purpose: print out a counter value * * @author Monte Ohrt * @link http://www.smarty.net/manual/en/language.function.counter.php {counter} * (Smarty online manual) * @param array $params parameters * @param Smarty_Internal_Template $template template object * @return string|null */ function smarty_function_counter($params, $template) { static $counters = array(); $name = (isset($params['name'])) ? $params['name'] : 'default'; if (!isset($counters[$name])) { $counters[$name] = array( 'start'=>1, 'skip'=>1, 'direction'=>'up', 'count'=>1 ); } $counter =& $counters[$name]; if (isset($params['start'])) { $counter['start'] = $counter['count'] = (int)$params['start']; } if (!empty($params['assign'])) { $counter['assign'] = $params['assign']; } if (isset($counter['assign'])) { $template->assign($counter['assign'], $counter['count']); } if (isset($params['print'])) { $print = (bool)$params['print']; } else { $print = empty($counter['assign']); } if ($print) { $retval = $counter['count']; } else { $retval = null; } if (isset($params['skip'])) { $counter['skip'] = $params['skip']; } if (isset($params['direction'])) { $counter['direction'] = $params['direction']; } if ($counter['direction'] == "down") $counter['count'] -= $counter['skip']; else $counter['count'] += $counter['skip']; return $retval; } ?> ================================================ FILE: ThinkPHP/Library/Vendor/Smarty/plugins/function.cycle.php ================================================ * Name: cycle
    * Date: May 3, 2002
    * Purpose: cycle through given values
    * Params: *
     * - name      - name of cycle (optional)
     * - values    - comma separated list of values to cycle, or an array of values to cycle
     *               (this can be left out for subsequent calls)
     * - reset     - boolean - resets given var to true
     * - print     - boolean - print var or not. default is true
     * - advance   - boolean - whether or not to advance the cycle
     * - delimiter - the value delimiter, default is ","
     * - assign    - boolean, assigns to template var instead of printed.
     * 
    * Examples:
    *
     * {cycle values="#eeeeee,#d0d0d0d"}
     * {cycle name=row values="one,two,three" reset=true}
     * {cycle name=row}
     * 
    * * @link http://www.smarty.net/manual/en/language.function.cycle.php {cycle} * (Smarty online manual) * @author Monte Ohrt * @author credit to Mark Priatel * @author credit to Gerard * @author credit to Jason Sweat * @version 1.3 * @param array $params parameters * @param Smarty_Internal_Template $template template object * @return string|null */ function smarty_function_cycle($params, $template) { static $cycle_vars; $name = (empty($params['name'])) ? 'default' : $params['name']; $print = (isset($params['print'])) ? (bool)$params['print'] : true; $advance = (isset($params['advance'])) ? (bool)$params['advance'] : true; $reset = (isset($params['reset'])) ? (bool)$params['reset'] : false; if (!isset($params['values'])) { if(!isset($cycle_vars[$name]['values'])) { trigger_error("cycle: missing 'values' parameter"); return; } } else { if(isset($cycle_vars[$name]['values']) && $cycle_vars[$name]['values'] != $params['values'] ) { $cycle_vars[$name]['index'] = 0; } $cycle_vars[$name]['values'] = $params['values']; } if (isset($params['delimiter'])) { $cycle_vars[$name]['delimiter'] = $params['delimiter']; } elseif (!isset($cycle_vars[$name]['delimiter'])) { $cycle_vars[$name]['delimiter'] = ','; } if(is_array($cycle_vars[$name]['values'])) { $cycle_array = $cycle_vars[$name]['values']; } else { $cycle_array = explode($cycle_vars[$name]['delimiter'],$cycle_vars[$name]['values']); } if(!isset($cycle_vars[$name]['index']) || $reset ) { $cycle_vars[$name]['index'] = 0; } if (isset($params['assign'])) { $print = false; $template->assign($params['assign'], $cycle_array[$cycle_vars[$name]['index']]); } if($print) { $retval = $cycle_array[$cycle_vars[$name]['index']]; } else { $retval = null; } if($advance) { if ( $cycle_vars[$name]['index'] >= count($cycle_array) -1 ) { $cycle_vars[$name]['index'] = 0; } else { $cycle_vars[$name]['index']++; } } return $retval; } ?> ================================================ FILE: ThinkPHP/Library/Vendor/Smarty/plugins/function.fetch.php ================================================ * Name: fetch
    * Purpose: fetch file, web or ftp data and display results * * @link http://www.smarty.net/manual/en/language.function.fetch.php {fetch} * (Smarty online manual) * @author Monte Ohrt * @param array $params parameters * @param Smarty_Internal_Template $template template object * @return string|null if the assign parameter is passed, Smarty assigns the result to a template variable */ function smarty_function_fetch($params, $template) { if (empty($params['file'])) { trigger_error("[plugin] fetch parameter 'file' cannot be empty",E_USER_NOTICE); return; } $content = ''; if (isset($template->smarty->security_policy) && !preg_match('!^(http|ftp)://!i', $params['file'])) { if(!$template->smarty->security_policy->isTrustedResourceDir($params['file'])) { return; } // fetch the file if($fp = @fopen($params['file'],'r')) { while(!feof($fp)) { $content .= fgets ($fp,4096); } fclose($fp); } else { trigger_error('[plugin] fetch cannot read file \'' . $params['file'] . '\'',E_USER_NOTICE); return; } } else { // not a local file if(preg_match('!^http://!i',$params['file'])) { // http fetch if($uri_parts = parse_url($params['file'])) { // set defaults $host = $server_name = $uri_parts['host']; $timeout = 30; $accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*"; $agent = "Smarty Template Engine ". Smarty::SMARTY_VERSION; $referer = ""; $uri = !empty($uri_parts['path']) ? $uri_parts['path'] : '/'; $uri .= !empty($uri_parts['query']) ? '?' . $uri_parts['query'] : ''; $_is_proxy = false; if(empty($uri_parts['port'])) { $port = 80; } else { $port = $uri_parts['port']; } if(!empty($uri_parts['user'])) { $user = $uri_parts['user']; } if(!empty($uri_parts['pass'])) { $pass = $uri_parts['pass']; } // loop through parameters, setup headers foreach($params as $param_key => $param_value) { switch($param_key) { case "file": case "assign": case "assign_headers": break; case "user": if(!empty($param_value)) { $user = $param_value; } break; case "pass": if(!empty($param_value)) { $pass = $param_value; } break; case "accept": if(!empty($param_value)) { $accept = $param_value; } break; case "header": if(!empty($param_value)) { if(!preg_match('![\w\d-]+: .+!',$param_value)) { trigger_error("[plugin] invalid header format '".$param_value."'",E_USER_NOTICE); return; } else { $extra_headers[] = $param_value; } } break; case "proxy_host": if(!empty($param_value)) { $proxy_host = $param_value; } break; case "proxy_port": if(!preg_match('!\D!', $param_value)) { $proxy_port = (int) $param_value; } else { trigger_error("[plugin] invalid value for attribute '".$param_key."'",E_USER_NOTICE); return; } break; case "agent": if(!empty($param_value)) { $agent = $param_value; } break; case "referer": if(!empty($param_value)) { $referer = $param_value; } break; case "timeout": if(!preg_match('!\D!', $param_value)) { $timeout = (int) $param_value; } else { trigger_error("[plugin] invalid value for attribute '".$param_key."'",E_USER_NOTICE); return; } break; default: trigger_error("[plugin] unrecognized attribute '".$param_key."'",E_USER_NOTICE); return; } } if(!empty($proxy_host) && !empty($proxy_port)) { $_is_proxy = true; $fp = fsockopen($proxy_host,$proxy_port,$errno,$errstr,$timeout); } else { $fp = fsockopen($server_name,$port,$errno,$errstr,$timeout); } if(!$fp) { trigger_error("[plugin] unable to fetch: $errstr ($errno)",E_USER_NOTICE); return; } else { if($_is_proxy) { fputs($fp, 'GET ' . $params['file'] . " HTTP/1.0\r\n"); } else { fputs($fp, "GET $uri HTTP/1.0\r\n"); } if(!empty($host)) { fputs($fp, "Host: $host\r\n"); } if(!empty($accept)) { fputs($fp, "Accept: $accept\r\n"); } if(!empty($agent)) { fputs($fp, "User-Agent: $agent\r\n"); } if(!empty($referer)) { fputs($fp, "Referer: $referer\r\n"); } if(isset($extra_headers) && is_array($extra_headers)) { foreach($extra_headers as $curr_header) { fputs($fp, $curr_header."\r\n"); } } if(!empty($user) && !empty($pass)) { fputs($fp, "Authorization: BASIC ".base64_encode("$user:$pass")."\r\n"); } fputs($fp, "\r\n"); while(!feof($fp)) { $content .= fgets($fp,4096); } fclose($fp); $csplit = preg_split("!\r\n\r\n!",$content,2); $content = $csplit[1]; if(!empty($params['assign_headers'])) { $template->assign($params['assign_headers'],preg_split("!\r\n!",$csplit[0])); } } } else { trigger_error("[plugin fetch] unable to parse URL, check syntax",E_USER_NOTICE); return; } } else { // ftp fetch if($fp = @fopen($params['file'],'r')) { while(!feof($fp)) { $content .= fgets ($fp,4096); } fclose($fp); } else { trigger_error('[plugin] fetch cannot read file \'' . $params['file'] .'\'',E_USER_NOTICE); return; } } } if (!empty($params['assign'])) { $template->assign($params['assign'],$content); } else { return $content; } } ?> ================================================ FILE: ThinkPHP/Library/Vendor/Smarty/plugins/function.html_checkboxes.php ================================================ * Type: function
    * Name: html_checkboxes
    * Date: 24.Feb.2003
    * Purpose: Prints out a list of checkbox input types
    * Examples: *
     * {html_checkboxes values=$ids output=$names}
     * {html_checkboxes values=$ids name='box' separator='
    ' output=$names} * {html_checkboxes values=$ids checked=$checked separator='
    ' output=$names} *
    * Params: *
     * - name       (optional) - string default "checkbox"
     * - values     (required) - array
     * - options    (optional) - associative array
     * - checked    (optional) - array default not set
     * - separator  (optional) - ie 
    or   * - output (optional) - the output next to each checkbox * - assign (optional) - assign the output as an array to this variable * - escape (optional) - escape the content (not value), defaults to true *
    * * @link http://www.smarty.net/manual/en/language.function.html.checkboxes.php {html_checkboxes} * (Smarty online manual) * @author Christopher Kvarme * @author credits to Monte Ohrt * @version 1.0 * @param array $params parameters * @param object $template template object * @return string * @uses smarty_function_escape_special_chars() */ function smarty_function_html_checkboxes($params, $template) { require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php'); $name = 'checkbox'; $values = null; $options = null; $selected = array(); $separator = ''; $escape = true; $labels = true; $label_ids = false; $output = null; $extra = ''; foreach($params as $_key => $_val) { switch($_key) { case 'name': case 'separator': $$_key = (string) $_val; break; case 'escape': case 'labels': case 'label_ids': $$_key = (bool) $_val; break; case 'options': $$_key = (array) $_val; break; case 'values': case 'output': $$_key = array_values((array) $_val); break; case 'checked': case 'selected': if (is_array($_val)) { $selected = array(); foreach ($_val as $_sel) { if (is_object($_sel)) { if (method_exists($_sel, "__toString")) { $_sel = smarty_function_escape_special_chars((string) $_sel->__toString()); } else { trigger_error("html_checkboxes: selected attribute contains an object of class '". get_class($_sel) ."' without __toString() method", E_USER_NOTICE); continue; } } else { $_sel = smarty_function_escape_special_chars((string) $_sel); } $selected[$_sel] = true; } } elseif (is_object($_val)) { if (method_exists($_val, "__toString")) { $selected = smarty_function_escape_special_chars((string) $_val->__toString()); } else { trigger_error("html_checkboxes: selected attribute is an object of class '". get_class($_val) ."' without __toString() method", E_USER_NOTICE); } } else { $selected = smarty_function_escape_special_chars((string) $_val); } break; case 'checkboxes': trigger_error('html_checkboxes: the use of the "checkboxes" attribute is deprecated, use "options" instead', E_USER_WARNING); $options = (array) $_val; break; case 'assign': break; default: if(!is_array($_val)) { $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"'; } else { trigger_error("html_checkboxes: extra attribute '$_key' cannot be an array", E_USER_NOTICE); } break; } } if (!isset($options) && !isset($values)) return ''; /* raise error here? */ $_html_result = array(); if (isset($options)) { foreach ($options as $_key=>$_val) { $_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids, $escape); } } else { foreach ($values as $_i=>$_key) { $_val = isset($output[$_i]) ? $output[$_i] : ''; $_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids, $escape); } } if(!empty($params['assign'])) { $template->assign($params['assign'], $_html_result); } else { return implode("\n", $_html_result); } } function smarty_function_html_checkboxes_output($name, $value, $output, $selected, $extra, $separator, $labels, $label_ids, $escape=true) { $_output = ''; if (is_object($value)) { if (method_exists($value, "__toString")) { $value = (string) $value->__toString(); } else { trigger_error("html_options: value is an object of class '". get_class($value) ."' without __toString() method", E_USER_NOTICE); return ''; } } else { $value = (string) $value; } if (is_object($output)) { if (method_exists($output, "__toString")) { $output = (string) $output->__toString(); } else { trigger_error("html_options: output is an object of class '". get_class($output) ."' without __toString() method", E_USER_NOTICE); return ''; } } else { $output = (string) $output; } if ($labels) { if ($label_ids) { $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!u', '_', $name . '_' . $value)); $_output .= '