decodeCMemDump_v2.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!php.exe -q
  2. <?php
  3. /**
  4. * 解码CMEM的dump文件, 输出为key作为文件名的txt文件,value作为其文本内容.
  5. * @author gwang email:wanggangzero@qq.com
  6. * @copyright ? 2015-6-8, SJZ LoyalSoft Corporation & gwang. All rights reserved.
  7. * @version 2017.04.27 更新了解析规则, 对方发来的数据带有了4个字节的类型信息如果是0则没有压缩,2采用压缩.
  8. * 这比原来的数据友好多了, 全部数据完美解析.
  9. * 2015-6-8 created
  10. * @description
  11. * CMEM有两中模式二进制和字符串.一般都用字符串模式,概因效率相差不多.
  12. * 本脚本内解码算法亦以字符串模式为基础.
  13. * 原理: php写入Memcache的字符串对象(c格式的即字节数组)将会被转换为hex字符串
  14. * 再写入memcache, memcache的压缩标志 MEMCACHE_COMPRESSED 指定使用zlib压缩.
  15. * 如果自己的cmem库中使用了压缩,那么需要在将hex还原为字符串后再解压缩一遍还原为
  16. * 原来的数组.
  17. * addition: 即便使用了 MEMCACHE_COMPRESSED 标志位, memcache规定,
  18. * 压缩比大于0.8的不采用压缩value
  19. * 将不被压缩. 因此,下文代码中针对这一特殊规定做了处理.
  20. */
  21. # 导入主代码库
  22. include_once __DIR__ . '/../../main.php';
  23. # 设置时区为中国区(东8区)
  24. date_default_timezone_set("PRC");
  25. set_time_limit(0); # cli 不限定执行时间
  26. //
  27. // <editor-fold defaultstate="collapsed" desc=" hack for hex2bin">
  28. if (!function_exists('hex2bin')) { // first include in php 5.4
  29. function hex2bin($str)
  30. {
  31. $sbin = "";
  32. $len = strlen($str);
  33. for ($i = 0; $i < $len; $i += 2) {
  34. $sbin .= pack("H*", substr($str, $i, 2));
  35. }
  36. return $sbin;
  37. }
  38. }
  39. // </editor-fold>
  40. //
  41. /**
  42. * 全局
  43. * @staticvar CRedisUtil $gRedis
  44. * @return \CRedisUtil
  45. */
  46. function gRedis()
  47. {
  48. static $gRedis;
  49. if ($gRedis == null) {
  50. $gRedis = new CRedisUtil();
  51. $host = '192.168.10.51';
  52. $port = '6666';
  53. $pwd = 'wanggang1985';
  54. $gRedis->conn($host, $port, $pwd);
  55. }
  56. return $gRedis;
  57. }
  58. function output($key, $value)
  59. {
  60. if (strpos($key, '-info') !== FALSE) { // userinfo
  61. $userinfo = JsonUtil::decode($value);
  62. if (property_exists($userinfo, 'user') //
  63. && property_exists($userinfo->user, 'ts') //
  64. && (day() - day($userinfo->user->ts) > 60)) { // 距上次登录60天以上了
  65. // uid, zoneid, tsday, info(bin2hex(gzcompress())
  66. $uid = $userinfo->user->oId;
  67. $zoneid = $userinfo->zoneid;
  68. $tsday = day($userinfo->user->ts);
  69. $info = bin2hex(gzdeflate($value));
  70. $SQL = sprintf("call mgodpay.insertUserInfo('%s',%d,%d,'%s');", $uid, $zoneid, $tsday, $info);
  71. $paydb = CPayInit();
  72. $paydb->query($SQL);
  73. // $paydb->close();
  74. echo $key . "\r\n";
  75. return;
  76. }
  77. }
  78. $redis = gRedis();
  79. $redis->set($key, $value);
  80. // $handle = fopen(__DIR__ . "/datas4/" . "$key.txt", "w");
  81. // fwrite($handle, $value);
  82. // fclose($handle);
  83. }
  84. if ($argc <= 1 # 未传参数的情况下
  85. || $argv[1] == "-h" || $argv[1] == "\\h") { # 或者是输入\h -h 查询帮助
  86. echo " usage: php.exe $argv[0] dumpfilename \n";
  87. echo " output: a txt file that use key as filename,value as contents.\n";
  88. echo "\t(attension: to avoid too many output in console, filter is suggested.)\n";
  89. } else {
  90. $n = 1;
  91. $filename = $argv[1];
  92. $zoneid = intval(str_replace('cmem_zone', '', $filename));
  93. if (!file_exists($filename)) {
  94. echo " file $filename not exist!";
  95. } else {
  96. $handle = fopen($filename, "rb"); # 打开文件
  97. if ($handle) {
  98. while (( $line = fgets($handle)) !== false) {
  99. $strarr = explode(" ", $line);
  100. $key = hex2bin($strarr[0]);
  101. $sec = $strarr[1];
  102. $type = (int) substr($sec, 0, 8);
  103. $value = hex2bin(substr($sec, 8));
  104. // echo "type: $type\n\ndecoded value: \n\n$value\n\n";
  105. if ($type == 2) {
  106. $value = gzuncompress($value);
  107. // echo "\n\ndecoded value: \n\n$value\n\n";
  108. }
  109. // echo "\"". $strarr[0]."\":".$value ."\n";
  110. output("$key-zone$zoneid", $value);
  111. if ($n++ % 50 == 0) {
  112. echo "dealed $n records!\n";
  113. // break;
  114. }
  115. }
  116. if (!feof($handle)) {
  117. // echo "Error: unexpected fgets() fail\n" ;
  118. }
  119. fclose($handle);
  120. }
  121. }
  122. }