{"id":935,"date":"2016-10-27T16:38:49","date_gmt":"2016-10-27T08:38:49","guid":{"rendered":"http:\/\/www.zyuns.com\/?p=935"},"modified":"2016-10-27T16:38:49","modified_gmt":"2016-10-27T08:38:49","slug":"%e5%9f%ba%e4%ba%8ethinkphp%e7%94%a8%e6%88%b7%e8%a1%8c%e4%b8%ba%e8%ae%b0%e5%bd%95","status":"publish","type":"post","link":"https:\/\/www.siediyer.cn\/?p=935","title":{"rendered":"\u57fa\u4e8eThinkPHP\u7528\u6237\u884c\u4e3a\u8bb0\u5f55"},"content":{"rendered":"<pre class=\"lang:default decode:true  \">&lt;?php\r\n\/**\r\n +------------------------------------------------------------------------------\r\n * \u57fa\u4e8e\u7528\u6237\u7684\u64cd\u4f5c\u8bb0\u5f55\u9a8c\u8bc1\u7c7b\r\n +------------------------------------------------------------------------------\r\n * @category   ORG\r\n * @package  ORG\r\n * @version   1.0\r\n +------------------------------------------------------------------------------\r\n *\/\r\n\/\/ \u914d\u7f6e\u6587\u4ef6\u589e\u52a0\u8bbe\u7f6e\r\n\/\/  'OPERATION_ON'=&gt;true,\/\/ \u5f00\u542f\u7528\u6237\u8bb0\u5f55\u65e5\u5fd7\r\n\/\/     'OPERATION_MEMBER'=&gt;'learn_member',\r\n\/\/     'OPERATION_TYPE'=&gt;'web',\/\/\u5206\u522b\u4e3aweb,interface\u4e5f\u5c31\u662f\u7f51\u7ad9,\u548c\u63a5\u53e3\r\n\/\/     'OPERATION_MEMBER_ID'=&gt;'member_id', \/\/\u5982\u679c\u540e\u53f0\u5c31\u53d6session,\u5982\u679c\u63a5\u53e3\u5c31\u76f4\u63a5\u53d6get,post\u8bf7\u6c42\u7684\u503c\r\n\/*\r\n-- --------------------------------------------------------\r\nCREATE TABLE IF NOT EXISTS `msj_operation_log` (\r\n  `operation_log` mediumint(8) unsigned NOT NULL AUTO_INCREMENT COMMENT '\u64cd\u4f5c\u8bb0\u5f55\u4e3b\u952e',\r\n  `operation_uid` mediumint(4) NOT NULL DEFAULT '0' COMMENT '\u64cd\u4f5c\u4eba\/\u5982\u679c\u662f\u63a5\u53e3\u8fd4\u56de-1\u6682\u4e0d\u8bb0\u5f55\u63a5\u53e3\u8bf7\u6c42\u4eba',\r\n  `operation_node` char(50) COLLATE utf8_bin NOT NULL DEFAULT '' COMMENT '\u64cd\u4f5c\u8282\u70b9',\r\n  `operation_ip` mediumtext COLLATE utf8_bin NOT NULL COMMENT '\u8bb0\u5f55\u64cd\u4f5cIP,\u7701\u5e02,\u7b49\u4fe1\u606f',\r\n  `operation_time` int(10) NOT NULL DEFAULT '0' COMMENT '\u64cd\u4f5c\u65f6\u95f4',\r\n  PRIMARY KEY (`operation_log`),\r\n  KEY `index_uid_node` (`operation_uid`,`operation_node`,`operation_log`)\r\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='@author PHP@\u5996\u5b7d\\r\\n@since 2014-5-4'\r\n\r\n*\/\r\nclass Operation {\r\n    \r\n    private $operation_on;\/\/\u64cd\u4f5c\u8bb0\u5f55\u5f00\u5173\r\n    public    $error;\/\/\u9519\u8bef\u4fe1\u606f\r\n    \r\n    \/**\r\n     * @todo  \u9a8c\u8bc1\u662f\u5426\u5f00\u542f\u8bb0\u5f55\r\n     *\/\r\n    public function __construct(){\r\n        $this-&gt;operation_on = C('OPERATION_ON');\r\n        if($this-&gt;operation_on === false){\r\n            return false;\r\n        }\r\n    }\r\n    \r\n    \/**\r\n     *  @todo\u83b7\u53d6\u5ba2\u6237\u7aefIP\u5730\u5740\r\n     * @param integer $type \u8fd4\u56de\u7c7b\u578b 0 \u8fd4\u56deIP\u5730\u5740 1 \u8fd4\u56deIPV4\u5730\u5740\u6570\u5b57\r\n     * @return mixed\r\n     *\/\r\n    private function getClientIp($type=0){\r\n        $type       =  $type ? 1 : 0;\r\n        static $ip  =   NULL;\r\n        if ($ip !== NULL) return $ip[$type];\r\n        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {\r\n            $arr    =   explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);\r\n            $pos    =   array_search('unknown',$arr);\r\n            if(false !== $pos) unset($arr[$pos]);\r\n            $ip     =   trim($arr[0]);\r\n        }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {\r\n            $ip     =   $_SERVER['HTTP_CLIENT_IP'];\r\n        }elseif (isset($_SERVER['REMOTE_ADDR'])) {\r\n            $ip     =   $_SERVER['REMOTE_ADDR'];\r\n        }\r\n        \/\/ IP\u5730\u5740\u5408\u6cd5\u9a8c\u8bc1\r\n        $long = sprintf(\"%u\",ip2long($ip));\r\n        $ip   = $long ? array($ip, $long) : array('0.0.0.0', 0);\r\n        return $ip[$type];\r\n    }\r\n    \r\n    \/**\r\n     * @todo \u68c0\u6d4b\u8868\u662f\u5426\u5b58\u5728,\u5982\u679c\u4e0d\u5b58\u5728\u5219\u521b\u5efa\u65b0\u8868\r\n     *\/\r\n    static public function checkTableIsExist(){\r\n        $db     =   Db::getInstance(C('RBAC_DB_DSN'));\r\n        $table_prefix = C('DB_PREFIX');\r\n        $sql    =   \"CREATE TABLE IF NOT EXISTS `{$table_prefix}msj_operation_log` (\r\n          `operation_log` mediumint(8) unsigned NOT NULL AUTO_INCREMENT COMMENT '\u64cd\u4f5c\u8bb0\u5f55\u4e3b\u952e',\r\n          `operation_uid` mediumint(4) NOT NULL DEFAULT '0' COMMENT '\u64cd\u4f5c\u4eba\/\u5982\u679c\u662f\u63a5\u53e3\u8fd4\u56de-1\u6682\u4e0d\u8bb0\u5f55\u63a5\u53e3\u8bf7\u6c42\u4eba',\r\n          `operation_node` char(50) COLLATE utf8_bin NOT NULL DEFAULT '' COMMENT '\u64cd\u4f5c\u8282\u70b9',\r\n          `operation_ip` mediumtext COLLATE utf8_bin NOT NULL COMMENT '\u8bb0\u5f55\u64cd\u4f5cIP,\u7701\u5e02,\u7b49\u4fe1\u606f',\r\n          `operation_time` int(10) NOT NULL DEFAULT '0' COMMENT '\u64cd\u4f5c\u65f6\u95f4',\r\n          PRIMARY KEY (`operation_log`),\r\n          KEY `index_uid_node` (`operation_uid`,`operation_node`,`operation_log`)\r\n        ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='@author PHP@\u5996\u5b7d\\r\\n@since 2014-5-4'\";\r\n        $db-&gt;execute($sql);\r\n    }\r\n    \r\n    \/**\r\n     * @todo \u5199\u5165\u64cd\u4f5c\u65e5\u5fd7\r\n     *\/\r\n    public function writeLog(){\r\n        (defined('NOW_TIME'))?$time = NOW_TIME: $time=time();\r\n                \r\n        switch (C('OPERATION_TYPE')){\r\n            case 'web':\r\n                $uid = session(C('OPERATION_MEMBER_ID'));\r\n                $uid = ($uid)?$uid:0;\r\n                break;\r\n            case 'interface':\/\/\u9884\u7559\r\n                $uid = -1;\/\/\u63a5\u53e3\u7684\u64cd\u4f5c\u65e5\u5fd7\u6682\u65f6\u4e0d\u8bb0\u5f55\u64cd\u4f5c\u4eba\r\n                break;\r\n            default:\r\n                $uid = -2;\r\n                break;\r\n        }\r\n        \r\n        $db_name =C('DB_NAME') ;\r\n        $table_prefix = C('DB_PREFIX');\r\n        import('@.ORG.Msj.IpLocation');\/\/ \u5bfc\u5165IpLocation\u7c7b\r\n        $Ip = new IpLocation(); \/\/ \u5b9e\u4f8b\u5316\u7c7b\r\n        $ip_info = $Ip-&gt;getlocation($this-&gt;getClientIp()); \/\/ \u83b7\u53d6\u67d0\u4e2aIP\u5730\u5740\u6240\u5728\u7684\u4f4d\u7f6e\r\n        $ip_info['country'] = iconv('gbk', 'utf-8', $ip_info['country']);\r\n        $db     =   Db::getInstance(C('RBAC_DB_DSN'));\r\n        $sql    =    \"INSERT INTO `{$db_name}`.`{$table_prefix}msj_operation_log` (`operation_uid`, `operation_node`, `operation_ip`, `operation_time`) VALUES ('\".$uid.\"','\".$_SERVER['REQUEST_URI'].\"','\".serialize($ip_info).\"','\".$time.\"');\"; \r\n        if($db-&gt;execute($sql) === false ){\r\n            \/\/\u63d2\u5165\u5931\u8d25\u5199\u65e5\u5fd7\r\n            Log::write(\"uid:{$uid},\".'node:'.$_SERVER['REQUEST_URI'].',operation_ip:'.serialize($ip_info).',time:'.date('Y-m-d H:i:s',$time));\r\n        }\r\n        \r\n    }\r\n    \r\n    \/**\r\n     * @todo \u67e5\u8be2\u64cd\u4f5c\u65e5\u5fd7\r\n     * @param array $map \u76ee\u524d\u53ea\u652f\u6301\u7528\u6237id\u7684\u67e5\u8be2.\r\n     *\/\r\n    public function logList($map=array()){\r\n        $db     =   Db::getInstance(C('RBAC_DB_DSN'));\r\n        $member_table_name = C('OPERATION_MEMBER');\r\n        $operation_table_name =C('DB_PREFIX').'msj_operation_log';\r\n        $member_id = implode(',',$map);\r\n        $sql = \"(SELECT \r\n                  msj_operation_log.operation_log AS operation_log,\r\n                  msj_operation_log.operation_uid AS operation_uid,\r\n                  msj_operation_log.operation_node AS operation_node,\r\n                  msj_operation_log.operation_ip AS operation_ip,\r\n                  msj_operation_log.operation_time AS operation_time,\r\n                  Member.member_name AS member_name \r\n                FROM\r\n                  {$operation_table_name} msj_operation_log \r\n                  JOIN {$member_table_name} Member \r\n                    ON msj_operation_log.operation_uid = Member.member_id \r\n                WHERE (`member_id` IN('{$member_id}')))\";\r\n        $log_list = $db-&gt;query($sql);\r\n        $Ip = new IpLocation(); \/\/ \u5b9e\u4f8b\u5316\u7c7b\r\n        $ip_info = $Ip-&gt;getlocation($this-&gt;getClientIp()); \/\/ \u83b7\u53d6\u67d0\u4e2aIP\u5730\u5740\u6240\u5728\u7684\u4f4d\u7f6e\r\n        if(!empty($log_list)){\r\n            foreach($log_list as $key=&gt;$val){\r\n                $log_list[$key]['operation_time'] = date('Y-m-d H:i:s',$val['operation_time']);\r\n                $info = unserialize($val['operation_ip']);\r\n                $log_list[$key]['operation_ip'] = \"\u5730\u533a:\".$info['area'].',\u57ce\u5e02:'.$info['country'].',IP:'.$info['ip'];\r\n            }\r\n            return $log_list;\r\n        }else{\r\n            return false;\r\n        }\r\n    }\r\n    \r\n    public function __destruct(){\r\n        $this-&gt;operation_on=false;\r\n        $this-&gt;error ='';\r\n    }\r\n    \r\n\r\n}\r\n\r\n\/\/\u67e5list;\r\nimport('@.ORG.Msj.Operation');\r\n$operation_obj = new Operation();\r\n$log_list  = $operation_obj-&gt;logList(array('member_id'=&gt;2086));\r\n\r\n\/\/\u8bb0\u5f55\u65e5\u5fd7\r\n\r\n$operation_obj-&gt;writeLog();<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&lt;?php \/** +&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[55,56],"class_list":["post-935","post","type-post","status-publish","format-standard","hentry","category-php","tag-operation","tag-56"],"_links":{"self":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/935","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=935"}],"version-history":[{"count":1,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/935\/revisions"}],"predecessor-version":[{"id":936,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/935\/revisions\/936"}],"wp:attachment":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=935"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=935"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=935"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}