Advertisement
Ritchie

binlib.php

Jul 29th, 2015
823
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.79 KB | None | 0 0
  1. <?php
  2.  
  3. function readNumber($con, $offset, $length) {
  4.     $val = 0;
  5.     for ($i=$length-1; $i>=0; $i--) {
  6.         $val *= 256;
  7.         $val += ord($con[$offset+$i]);
  8.     }
  9.     return $val;
  10. }
  11.  
  12. function readByte($con, $offset) {
  13.     return readNumber($con, $offset, 1);
  14. }
  15.  
  16. function readShort($con, $offset) {
  17.     return readNumber($con, $offset, 2);
  18. }
  19.  
  20. function readInt($con, $offset) {
  21.     return readNumber($con, $offset, 4);
  22. }
  23.  
  24. function readFloat($con, $offset) {
  25.     $result = unpack('f', substr($con, $offset, 4));
  26.     return $result[1];
  27. }
  28.  
  29. function readOffsets($con, $offsets) {
  30.     $ret = array();
  31.     foreach($offsets as $key => $offset) {
  32.         $length = 1;
  33.         if (is_array($offset)) {
  34.             list($offset, $length) = $offset;
  35.         }
  36.         $ret[$key] = readNumber($con, $offset, $length);
  37.     }
  38.     return $ret;
  39. }
  40.  
  41. function readTextsFromFile($file) {
  42.     return explode("\n", str_replace("\r","",encodeToUtf8(file_get_contents($file))));
  43. }
  44.  
  45. function readTextsIntoRows($file, $rows, $key, $indexKey = null) {
  46.     $texts = readTextsFromFile($file);
  47.    
  48.     foreach($rows as $index => &$row) {
  49.         if ($indexKey !== null) {
  50.             $index = $row[$indexKey];
  51.         }
  52.         $row[$key] = trim($texts[$index]);
  53.     }
  54.        
  55.     return $rows;
  56. }
  57.  
  58. function rowsToSql($table, $rows) {
  59.  
  60.     mysql_set_charset('utf-8');
  61.  
  62.     $fields = Array();
  63.     $fieldsize = Array();
  64.  
  65.     foreach($rows as $row) {
  66.        
  67.         foreach($row as $key => $val) {
  68.            
  69.             if (!isset($fields[$key])) {
  70.                 $fields[$key] = $val;
  71.             }
  72.             elseif (is_int($val) && is_int($fields[$key])) {
  73.                 $field = $fields[$key];
  74.                 $fields[$key] = max($val, $field);
  75.             }
  76.             else {
  77.                 if (strlen($val) > strlen($fields[$key])) $fields[$key] = $val."";
  78.                 if (is_array($val)) {
  79.                     var_dump($row);
  80.                     exit;
  81.                     }
  82.             }
  83.            
  84.         }
  85.     }
  86.     ksort($fields);
  87.     $sql = "";
  88.     foreach($fields as $key => $val) {
  89.         if ($val === NULL) {
  90.             unset($fields[$key]);
  91.         }
  92.         else {
  93.             $sql .= "-- ALTER TABLE $table ADD $key ";
  94.             if (is_int($val)) {
  95.                 $sql .=  "INT";
  96.             }
  97.             else if (strlen($val) < 10) {
  98.                 $sql .=  "CHAR(32)";
  99.             }
  100.             else if (strlen($val) < 100) {
  101.                 $sql .=  "VARCHAR(255)";
  102.             }
  103.             else {
  104.                 $sql .=  "TEXT";
  105.             }
  106.             $sql .=  ";\n";
  107.         }
  108.     }
  109.  
  110.     $sql .= "\n\n\nTRUNCATE TABLE $table;\n";
  111.  
  112.     foreach($rows as $index => $row) {
  113.         if (!count($row)) continue;
  114.         $sql .= "insert into $table (".implode(",",array_keys($row)).") values (";
  115.        
  116.         foreach($row as $key=>$val) {
  117.             $sql .= "'".mysql_real_escape_string($val)."', ";
  118.         }
  119.         $sql = substr($sql, 0, -2);
  120.        
  121.         $sql .= ");\n";
  122.        
  123.     }
  124.    
  125.     return $sql;
  126. }
  127.  
  128. function encodeToUtf8($string) {
  129.      return mb_convert_encoding($string, "UTF-8", mb_detect_encoding($string, "UTF-8, ISO-8859-1, ISO-8859-15", true));
  130. }
  131.  
  132. function dirToLines($dir) {
  133.     $ret = array();
  134.     foreach(glob("$dir/*") as $file) {
  135.         $ret[] = file_get_contents($file);
  136.     }
  137.     return $ret;
  138. }
  139.  
  140. function findFile($dirs, $target) {
  141.     foreach($dirs as $dir) {
  142.         if (file_exists("$dir/$target")) {
  143.             return "$dir/$target";
  144.         }
  145.     }
  146. }
  147.  
  148. function findAllFiles($dirs, $target) {
  149.     $result = array();
  150.     foreach($dirs as $dir) {
  151.         if (file_exists("$dir/$target")) {
  152.             $result[] = "$dir/$target";
  153.         }
  154.     }
  155.     return $result;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement