decodeCMemDump_v2.2.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. namespace loyalsoft;
  22. # 导入主代码库
  23. include_once __DIR__ . '/../../main.php';
  24. # 设置时区为中国区(东8区)
  25. date_default_timezone_set("PRC");
  26. set_time_limit(0); # cli 不限定执行时间
  27. //
  28. // <editor-fold defaultstate="collapsed" desc=" hack for hex2bin">
  29. if (!function_exists('hex2bin')) { // first include in php 5.4
  30. function hex2bin($str)
  31. {
  32. return pack('H*', $str);
  33. // safer方法:2个字节一转换,确保最大限度完成转换.
  34. $sbin = "";
  35. $len = strlen($str);
  36. for ($i = 0; $i < $len; $i += 2) {
  37. $sbin .= pack("H*", substr($str, $i, 2));
  38. }
  39. return $sbin;
  40. }
  41. }
  42. // </editor-fold>
  43. //
  44. /**
  45. * 全局
  46. * @staticvar CRedisUtil $gRedis
  47. * @return \CRedisUtil
  48. */
  49. function gRedis()
  50. {
  51. static $gRedis;
  52. if ($gRedis == null) {
  53. $gRedis = new CRedisUtil();
  54. $host = '192.168.10.51';
  55. $port = '6666';
  56. $pwd = 'wanggang1985';
  57. $gRedis->conn($host, $port, $pwd);
  58. }
  59. return $gRedis;
  60. }
  61. function output($key, $value)
  62. {
  63. if (strpos($key, '-info') !== FALSE) { // userinfo
  64. $userinfo = JsonUtil::decode($value);
  65. if (property_exists($userinfo, 'user') //
  66. && property_exists($userinfo->user, 'ts') //
  67. && (day() - day($userinfo->user->ts) > 60)) { // 距上次登录60天以上了
  68. // uid, zoneid, tsday, info(bin2hex(gzcompress())
  69. $uid = $userinfo->user->oId;
  70. $zoneid = $userinfo->zoneid;
  71. $tsday = day($userinfo->user->ts);
  72. $info = base64_encode(gzcompress($value));
  73. $SQL = sprintf("call mgodpay.insertUserInfo('%s',%d,%d,'%s');", $uid, $zoneid, $tsday, $info);
  74. $paydb = CPayInit();
  75. $paydb->query($SQL);
  76. // echo $SQL. "\r\n";
  77. // $paydb->close();
  78. echo $key . "\r\n";
  79. return;
  80. }
  81. }
  82. $redis = gRedis();
  83. $redis->set($key, $value);
  84. // $handle = fopen(__DIR__ . "/datas4/" . "$key.txt", "w");
  85. // fwrite($handle, $value);
  86. // fclose($handle);
  87. }
  88. if ($argc <= 1 # 未传参数的情况下
  89. || $argv[1] == "-h" || $argv[1] == "\\h") { # 或者是输入\h -h 查询帮助
  90. echo " usage: php.exe $argv[0] dumpfilename \n";
  91. echo " output: a txt file that use key as filename,value as contents.\n";
  92. echo "\t(attension: to avoid too many output in console, filter is suggested.)\n";
  93. } else {
  94. $n = 1;
  95. $filename = $argv[1];
  96. $zoneid = intval(str_replace('cmem_zone', '', $filename));
  97. if (!file_exists($filename)) {
  98. echo " file $filename not exist!";
  99. } else {
  100. $handle = fopen($filename, "rb"); # 打开文件
  101. if ($handle) {
  102. while (( $line = fgets($handle)) !== false) {
  103. $strarr = explode(" ", $line);
  104. $key = hex2bin($strarr[0]);
  105. // echo "key: $key \n";
  106. $sec = $strarr[1];
  107. $type = (int) substr($sec, 0, 8);
  108. $value = hex2bin(substr($sec, 8));
  109. if ($type == 2) {
  110. $value = gzuncompress($value);
  111. // echo "\n\ndecoded value: \n\n$value\n\n";
  112. }
  113. // echo "type: $type\n\ndecoded value: \n\n$value\n\n";
  114. // echo $key." : ".$value ."\n";
  115. output("$key-zone$zoneid", $value);
  116. if ($n++ % 50 == 0) {
  117. echo "dealed $n records!\n";
  118. // break;
  119. }
  120. }
  121. if (!feof($handle)) {
  122. // echo "Error: unexpected fgets() fail\n" ;
  123. }
  124. fclose($handle);
  125. }
  126. }
  127. }