Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <pre>
- <?php
- @ob_start();
- @session_start();
- @set_time_limit(0);
- @ignore_user_abort(0); //Change it to "1" if you want to keep running the script when you close the browser
- @error_reporting("E_NOTICE");
- $globals = array();
- $time = time();
- /**
- * READ THIS \/
- * @author Pedro Prata L. Papadópolis (@papadopolis on twitter)
- * @copyright 2011
- * @description Convert text files to DJSON files
- * @aboutme 17, M, Brazilian. -- My english isn't the best one, but i think u can understand the comentaries lines. --
- * @aboutscript Make sure that your domain host have permission to download FTP files and write into files.
- */
- //Configuration vars
- define("FTP_HOST","ftp.x470.com"); //Your server IP (without the port)
- define("FTP_PORT",21); //Your FTP port, default: 21 (this isn't your SERVER GAME port)
- define("FTP_USER","x470c719"); //Your FTP user (Have sure that this user have all permissions)
- define("FTP_PASSWD","p@p@d0p0l1s1599510019xxx"); //FTP password
- define("FTP_PATH","public_html/samp/scriptfiles"); //The path to configuration folder (file server.txt)
- $globals["EXTs"] = array("cfg","ini","txt"); //The extensions of the files that will be converted
- $globals["filesCount"] = 0; //Don't change it
- $globals["files"] = array(); //Don't change it
- $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
- "server_log.txt",
- "Log.txt"
- );
- /*
- Pay attention on this part, if you want to convert ALL FOLDERS to DJSON files, keep the value of the var below in "1",
- but if u want to choose what folders will be converted to djson, uncomment the script "#snippet1" and change the value
- of the var below to "0".
- */
- define("CONVERT_ALL",1);
- //SCRIPT: #snippet1
- /* DELETE THIS LINE TO UNCOMMENT
- $globals["folders"] = array(
- FTP_PATH."/scriptfiles",
- FTP_PATH."/scriptfiles/accounts",
- FTP_PATH."/scriptfiles/houses",
- FTP_PATH."/scriptfiles/vehicles"
- );
- DELETE THIS LINE TO UNCOMMENT*/
- _log("The converter was initialized");
- //Verifying if the folder "json" is created, if isn't, create it.
- if(!is_dir("json"))
- @mkdir("json");
- //Log-in into FTP
- $globals["FTPConn"] = @ftp_connect(FTP_HOST,FTP_PORT,10);
- $globals["FTPLogin"] = @ftp_login($globals["FTPConn"],FTP_USER,FTP_PASSWD);
- $globals["FTPPM"] = @ftp_pasv($globals["FTPConn"], true);
- //Check if the connection with the FTP HOST was done with success
- ($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));
- //Check if the login was done with success
- ($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));
- if(CONVERT_ALL) {
- $globals["folders"] = array();
- findFolders(FTP_PATH);
- } else {
- foreach($globals["folders"] as $folder)
- findFolders($folder);
- }
- function findFolders($path) {
- global $globals;
- _log(sprintf("Looking for folders or/and files into <b><i>%s</i></b>..",$path));
- $folders = array();
- $dir = ftp_nlist($globals["FTPConn"],$path);
- $filesCount = 0;
- if(!$dir)
- _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));
- foreach($dir as $file)
- if(!strstr($file,"\x2E"))
- $folders[] = $path."\x2F".$file;
- else {
- @preg_match("/\x2E([a-zA-Z]+)$/",$file,$extension);
- if(in_array($extension[1],$globals["EXTs"]) && !@in_array($file,$globals["exceptions"])) {
- $globals["files"][] = $path."\x2F".$file;
- ++$filesCount;
- ++$globals["filesCount"];
- }
- }
- if($filesCount > 0)
- _log(sprintf("was found <i>%d</i> file(s) to be converted in this folder",$filesCount));
- else
- _log("No one file was found in this path");
- $globals["folders"] = @array_merge($globals["folders"],$folders);
- if(CONVERT_ALL) {
- foreach($folders as $folder)
- findFolders($folder);
- }
- }
- _log(sprintf("<b>TOTAL:</b> <i>%d</i> folders was founded and <i>%d</i> files will be converted",count($globals["folders"]),$globals["filesCount"]));
- if(count($globals["files"]) == 0)
- die(_log("<b>Notice:</b> No one file was found to be converted, are you sure that you had configurate the configuration variables corretly?"));
- $zip = new ZipArchive();
- $zip->open("djson_".round(rand(1,10000)).".zip",ZIPARCHIVE::CREATE);
- $count = 1;
- $totalCount = $globals["filesCount"];
- foreach($globals["files"] as $file) {
- $folder = explode("\x2F",$file);
- $fileName = $folder[count($folder)-1];
- $folder = $folder[count($folder)-2];
- $fileName = explode(".",$fileName);
- $fileName = $fileName[0];
- $fileSize = @ftp_size($globals["FTPConn"],$file);
- _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))."%)");
- $fp = fopen("temp.txt","w+");
- $data = ftp_nb_fget($globals["FTPConn"], $fp, $file, FTP_BINARY);
- while ($data == FTP_MOREDATA) {
- @clearstatcache();
- $dld = filesize("temp.txt");
- if($dld > 0) {
- $percent = ($dld/$fileSize)*100;
- echo sprintf("<script>document.getElementById(\x22%s\x22).innerHTML = \x22%d\x22</script>",base64_encode($file),$percent);
- }
- ob_flush();flush();
- $data = ftp_nb_continue($globals["FTPConn"]);
- }
- if($data != FTP_FINISHED)
- _log("<b>Notice:</b> There was an error during the download of this file");
- $dini = file_get_contents("temp.txt");
- $djson = diniToDjson($dini,$fileName);
- $path = str_replace(FTP_PATH."\x2F","",$file);
- if(!file_exists("json/".$folder.".json"))
- @fopen("json/".$folder.".json","w");
- @fwrite(@fopen("json/".$folder.".json","a+"),$djson."\n");
- ++$count;
- }
- if($dh = opendir("json/")) {
- while (($file = readdir($dh)) !== false) {
- _log("sending json/".$file." to zip file");
- $zip->addFile("json/".$file);
- }
- closedir($dh);
- }
- $zip->close();
- @rmdir("json");
- $time = time()-$time;
- _log(sprintf("The script was executted in %d seconds",$time));
- function diniToDjson($dini,$name) {
- $json = array();
- $json["ID"] = $name;
- $lines = explode("\n",$dini);
- foreach($lines as $line) {
- $ln = explode("=",$line);
- $ind = $ln[0];
- $val = $ln[1];
- $json["{$ind}"] = $val;
- }
- return json_encode($json);
- }
- function _log($txt) {
- $timenow = date("H:i:s");
- $text = "<b>%s:</b> %s.\n";
- echo sprintf($text,$timenow,$txt);
- ob_flush(); flush();
- }
- ?>
- </pre>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement