Advertisement
Papadopolis

Untitled

Jul 8th, 2011
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.27 KB | None | 0 0
  1. <pre>
  2. <?php
  3. @ob_start();
  4. @session_start();
  5. @set_time_limit(0);
  6. @ignore_user_abort(0); //Change it to "1" if you want to keep running the script when you close the browser
  7. @error_reporting("E_NOTICE");
  8.  
  9. $globals    = array();
  10. $time       = time();
  11.  
  12. /**
  13.  *                  READ THIS \/
  14.  * @author Pedro Prata L. Papadópolis (@papadopolis on twitter)
  15.  * @copyright 2011
  16.  * @description Convert text files to DJSON files
  17.  * @aboutme 17, M, Brazilian. -- My english isn't the best one, but i think u can understand the comentaries lines. --
  18.  * @aboutscript Make sure that your domain host have permission to download FTP files and write into files.
  19.  */
  20.  
  21. //Configuration vars
  22. define("FTP_HOST","ftp.x470.com"); //Your server IP (without the port)
  23. define("FTP_PORT",21); //Your FTP port, default: 21 (this isn't your SERVER GAME port)
  24. define("FTP_USER","x470c719"); //Your FTP user (Have sure that this user have all permissions)
  25. define("FTP_PASSWD","p@p@d0p0l1s1599510019xxx"); //FTP password
  26. define("FTP_PATH","public_html/samp/scriptfiles"); //The path to configuration folder (file server.txt)
  27.  
  28. $globals["EXTs"]            = array("cfg","ini","txt"); //The extensions of the files that will be converted
  29. $globals["filesCount"]      = 0; //Don't change it
  30. $globals["files"]           = array(); //Don't change it
  31. $globals["exceptions"]      = array ( //Create an exception on the files into this array, some files don't need to be converted, like server_log and server_config
  32.                                 "server_log.txt",
  33.                                 "Log.txt"
  34.                             );
  35.  
  36. /*
  37. Pay attention on this part, if you want to convert ALL FOLDERS to DJSON files, keep the value of the var below in "1",
  38. but if u want to choose what folders will be converted to djson, uncomment the script "#snippet1" and change the value
  39. of the var below to "0".
  40. */
  41. define("CONVERT_ALL",1);
  42.  
  43.  
  44. //SCRIPT: #snippet1
  45.  
  46. /* DELETE THIS LINE TO UNCOMMENT
  47. $globals["folders"] = array(
  48.     FTP_PATH."/scriptfiles",
  49.     FTP_PATH."/scriptfiles/accounts",
  50.     FTP_PATH."/scriptfiles/houses",
  51.     FTP_PATH."/scriptfiles/vehicles"
  52. );
  53. DELETE THIS LINE TO UNCOMMENT*/
  54.  
  55. _log("The converter was initialized");
  56.  
  57. //Verifying if the folder "json" is created, if isn't, create it.
  58. if(!is_dir("json"))
  59.     @mkdir("json");
  60.  
  61. //Log-in into FTP
  62. $globals["FTPConn"]     = @ftp_connect(FTP_HOST,FTP_PORT,10);
  63. $globals["FTPLogin"]    = @ftp_login($globals["FTPConn"],FTP_USER,FTP_PASSWD);
  64. $globals["FTPPM"]       = @ftp_pasv($globals["FTPConn"], true);
  65.  
  66. //Check if the connection with the FTP HOST was done with success
  67. ($globals["FTPConn"])?_log(sprintf("Connection with the host '%s' successfull",FTP_HOST)):die(sprintf("<b>Script error:</b> Error on connection with <i>%s</i>",FTP_HOST));
  68.  
  69. //Check if the login was done with success
  70. ($globals["FTPLogin"])?_log(sprintf("Log-in user '%s' was done with success",FTP_USER)):die(sprintf("<b>Script error:</b> The log-in with the user '%s' failed to connect (verify the password)",FTP_USER));
  71.  
  72. if(CONVERT_ALL) {
  73.     $globals["folders"] = array();
  74.     findFolders(FTP_PATH);
  75. } else {
  76.     foreach($globals["folders"] as $folder)
  77.         findFolders($folder);
  78. }
  79.  
  80. function findFolders($path) {
  81.     global $globals;
  82.    
  83.     _log(sprintf("Looking for folders or/and files into <b><i>%s</i></b>..",$path));
  84.    
  85.     $folders    = array();
  86.     $dir        = ftp_nlist($globals["FTPConn"],$path);
  87.     $filesCount = 0;
  88.    
  89.     if(!$dir)
  90.         _log(sprintf("<b>Notice:</b> couldn't open the folder <i>%s</i>, the files into it cannot be accessed and will not be converted.",$path));
  91.        
  92.     foreach($dir as $file)
  93.         if(!strstr($file,"\x2E"))
  94.             $folders[] = $path."\x2F".$file;
  95.         else {
  96.             @preg_match("/\x2E([a-zA-Z]+)$/",$file,$extension);
  97.             if(in_array($extension[1],$globals["EXTs"]) && !@in_array($file,$globals["exceptions"])) {
  98.                 $globals["files"][] = $path."\x2F".$file;
  99.                 ++$filesCount;
  100.                 ++$globals["filesCount"];
  101.             }
  102.         }
  103.    
  104.     if($filesCount > 0)
  105.         _log(sprintf("was found <i>%d</i> file(s) to be converted in this folder",$filesCount));
  106.     else
  107.         _log("No one file was found in this path");
  108.    
  109.     $globals["folders"] = @array_merge($globals["folders"],$folders);
  110.    
  111.     if(CONVERT_ALL) {
  112.         foreach($folders as $folder)
  113.             findFolders($folder);
  114.     }      
  115. }
  116.  
  117. _log(sprintf("<b>TOTAL:</b> <i>%d</i> folders was founded and <i>%d</i> files will be converted",count($globals["folders"]),$globals["filesCount"]));
  118.  
  119. if(count($globals["files"]) == 0)
  120.     die(_log("<b>Notice:</b> No one file was found to be converted, are you sure that you had configurate the configuration variables corretly?"));
  121.    
  122. $zip = new ZipArchive();
  123. $zip->open("djson_".round(rand(1,10000)).".zip",ZIPARCHIVE::CREATE);
  124.  
  125. $count      = 1;
  126. $totalCount = $globals["filesCount"];
  127. foreach($globals["files"] as $file) {
  128.     $folder     = explode("\x2F",$file);
  129.     $fileName   = $folder[count($folder)-1];
  130.     $folder     = $folder[count($folder)-2];
  131.    
  132.     $fileName = explode(".",$fileName);
  133.     $fileName = $fileName[0];
  134.    
  135.     $fileSize = @ftp_size($globals["FTPConn"],$file);
  136.    
  137.     _log(sprintf("[%d/%d] downloading the file <b><i>%s</i></b> (%d bytes) from the FTP server. (<span id=\x22%s\x22>0</span>",$count,$totalCount,$file,$fileSize,base64_encode($file))."%)");
  138.    
  139.     $fp = fopen("temp.txt","w+");
  140.    
  141.     $data = ftp_nb_fget($globals["FTPConn"], $fp, $file, FTP_BINARY);
  142.     while ($data == FTP_MOREDATA) {
  143.         @clearstatcache();
  144.         $dld = filesize("temp.txt");
  145.        
  146.         if($dld > 0) {
  147.             $percent = ($dld/$fileSize)*100;
  148.             echo sprintf("<script>document.getElementById(\x22%s\x22).innerHTML = \x22%d\x22</script>",base64_encode($file),$percent);
  149.         }  
  150.        
  151.         ob_flush();flush();
  152.        
  153.         $data = ftp_nb_continue($globals["FTPConn"]);
  154.     }
  155.    
  156.     if($data != FTP_FINISHED)
  157.         _log("<b>Notice:</b> There was an error during the download of this file");
  158.        
  159.     $dini   = file_get_contents("temp.txt");
  160.     $djson  = diniToDjson($dini,$fileName);
  161.    
  162.     $path   = str_replace(FTP_PATH."\x2F","",$file);
  163.    
  164.     if(!file_exists("json/".$folder.".json"))
  165.         @fopen("json/".$folder.".json","w");
  166.        
  167.     @fwrite(@fopen("json/".$folder.".json","a+"),$djson."\n");
  168.  
  169.     ++$count;
  170. }
  171.    
  172. if($dh = opendir("json/")) {
  173.     while (($file = readdir($dh)) !== false) {
  174.         _log("sending json/".$file." to zip file");
  175.         $zip->addFile("json/".$file);
  176.     }
  177.     closedir($dh);
  178. }
  179.  
  180. $zip->close();
  181. @rmdir("json");
  182.  
  183. $time = time()-$time;
  184. _log(sprintf("The script was executted in %d seconds",$time));
  185.  
  186. function diniToDjson($dini,$name) {
  187.     $json = array();
  188.    
  189.     $json["ID"] = $name;
  190.        
  191.     $lines = explode("\n",$dini);
  192.     foreach($lines as $line) {
  193.         $ln = explode("=",$line);
  194.         $ind = $ln[0];
  195.         $val = $ln[1];
  196.        
  197.         $json["{$ind}"] = $val;
  198.     }
  199.    
  200.     return json_encode($json);
  201. }
  202.  
  203. function _log($txt) {
  204.     $timenow    = date("H:i:s");
  205.     $text       = "<b>%s:</b> %s.\n";
  206.     echo sprintf($text,$timenow,$txt);
  207.     ob_flush(); flush();
  208. }
  209. ?>
  210. </pre>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement