newbtophp Posted November 1, 2009 Share Posted November 1, 2009 Im stuck, im trying to read the contents of an uploaded zip, and want to echo its contents each preg_match This is my code: <?php if (isset($_FILES['file'])) { $file = file_get_contents($_FILES['file']['tmp_name']); $submit = $_POST['submit']; if($file == "") echo "Error"; else { if($submit == "Submit" ) { $filename = $_FILES['file']['tmp_name']; if (($temp = zip_open($filename))) { while ($entry = zip_read($temp)) if (preg_match('/\.php$/',zip_entry_name($entry))) { if (zip_entry_open($temp, $entry)) { $file2 = zip_entry_read($entry, 1024); if (preg_match('/female/', $file2)) { $file = str_replace('female', 'girl', $file2); echo $file2; } } } zip_close($temp); } } } } ?> :-\ :-\ Quote Link to comment https://forums.phpfreaks.com/topic/179780-i-need-help-how-to-do-this/ Share on other sites More sharing options...
newbtophp Posted November 1, 2009 Author Share Posted November 1, 2009 This is giving me a headache Ok i've figured whats wrong, but dont know how to resolve. Im trying to make my code allow zips. (I've tried but no luck, as you can see from my first post). <?php if (isset($_FILES['file'])) { $file = file_get_contents($_FILES['file']['tmp_name']); $submit = $_POST['submit']; if($file == "") echo "Error"; else { if($submit == "Submit") { if (preg_match('/male/', $file)) { $file = str_replace('male', 'boy', $file); header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Disposition: attachment;"); header("Content-Transfer-Encoding: binary"); echo $file; exit(); } if (preg_match('/female/', $file)) { $file = str_replace('female', 'girl', $file); header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Disposition: attachment;"); header("Content-Transfer-Encoding: binary"); echo $file; exit(); } if (preg_match('/girl/', $file)) { $file = str_replace('girl', 'female', $file); header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Disposition: attachment;"); header("Content-Transfer-Encoding: binary"); echo $file; exit(); } if (preg_match('/boy/', $file)) { $file = str_replace('boy', 'male', $file); header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Disposition: attachment;"); header("Content-Transfer-Encoding: binary"); echo $file; exit(); } } } } ?> Currently the code works on single files, and not zips. What im trying to do is also add a way to allow zips, so it scans the zips contents for a match then runs whatever that match does on them files, then finally downloads the new zip (containing the contents of the match). Quote Link to comment https://forums.phpfreaks.com/topic/179780-i-need-help-how-to-do-this/#findComment-948516 Share on other sites More sharing options...
newbtophp Posted November 3, 2009 Author Share Posted November 3, 2009 Anyone can help, please Quote Link to comment https://forums.phpfreaks.com/topic/179780-i-need-help-how-to-do-this/#findComment-949797 Share on other sites More sharing options...
MadTechie Posted November 3, 2009 Share Posted November 3, 2009 What's the problem? Quote Link to comment https://forums.phpfreaks.com/topic/179780-i-need-help-how-to-do-this/#findComment-949810 Share on other sites More sharing options...
MadTechie Posted November 3, 2009 Share Posted November 3, 2009 re-read, try this <?php $replaceall = array("female"=>"girl","male"=>"boy"); if (isset($_FILES['file'])) { $file = file_get_contents($_FILES['file']['tmp_name']); $submit = $_POST['submit']; if($file == "") echo "Error"; else { if($submit == "Submit" ) { if (($temp = zip_open($file))) { while ($entry = zip_read($temp)){ $file = zip_entry_name($entry); if(strtolower(substr($file, strrpos($file, '.')+1)) == "php"){ if (zip_entry_open($temp, $entry)) { $file2 = zip_entry_read($entry,zip_entry_filesize($entry)); foreach($replaceall as $find => $replace){ if(strpos($file2, $find) !== false){ echo str_replace($find,$replace,$file2); exit(); } } } } } zip_close($temp); } } } } Quote Link to comment https://forums.phpfreaks.com/topic/179780-i-need-help-how-to-do-this/#findComment-949814 Share on other sites More sharing options...
newbtophp Posted November 4, 2009 Author Share Posted November 4, 2009 Thanks madtechie, you forgot to remove the file_get_contents <?php if (isset($_FILES['file'])) { $file = $_FILES['file']['tmp_name']; $submit = $_POST['submit']; if($file == "") echo "Error"; else { if($submit == "Submit" ) { if (($temp = zip_open($file))) { while ($entry = zip_read($temp)){ $file = zip_entry_name($entry); if(strtolower(substr($file, strrpos($file, '.')+1)) == "php"){ if (zip_entry_open($temp, $entry)) { $file2 = zip_entry_read($entry,zip_entry_filesize($entry)); if (preg_match("/eval\(gzinflate\(base64_decode\('([^\']*?)'\)\)\);/", $file2)) { //include a function to modify $file2... include "base64.php"; } if (preg_match("~.*?\('[ *a-zA-Z0-9+=/]+?'\).*?\?>~s", $file2)) { include "code.php"; } } } } zip_close($temp); } //now im trying to make the zip download?, containing the changed php files within? } } } ?> I slightly modified it, and added if preg_matches, but its dont seem to work :-\ I also want too, if the preg_matches are found within the zip, to execute them and then download a new zip containing the changed files. Example: If its a single file (.php): (Currently does this) 1. Check if the file contains the preg_matches, if it does proceed to step 2, if it dont proceed to step 3. 2. Execute the function within the if statement (included), then download the file 3. Do nothing. If its a zip (archive of files): (Trying to add this) 1. Check if the zip files contains the preg_matches, if they do proceed to step 2, if it dont proceed to step 3 2. Execute the function within the if statement (included), then download the new zip containing the changes. 3. Do nothing. All help is greatly apreciated :-\ Like this site has: http://www.fopo.com.ar/ (upload a zip containing php to test) Quote Link to comment https://forums.phpfreaks.com/topic/179780-i-need-help-how-to-do-this/#findComment-950634 Share on other sites More sharing options...
MadTechie Posted November 4, 2009 Share Posted November 4, 2009 And the problem is ? Quote Link to comment https://forums.phpfreaks.com/topic/179780-i-need-help-how-to-do-this/#findComment-950636 Share on other sites More sharing options...
newbtophp Posted November 4, 2009 Author Share Posted November 4, 2009 And the problem is ? The preg_matches dont seem to work on $file2? Also I don't know how to make the zip downloadable containing the changes, after the files have been preg_matched. Quote Link to comment https://forums.phpfreaks.com/topic/179780-i-need-help-how-to-do-this/#findComment-950638 Share on other sites More sharing options...
MadTechie Posted November 4, 2009 Share Posted November 4, 2009 Your need to rebuild the zip, ie <?php $zip = new ZipArchive; if ($zip->open('test.zip') === TRUE) { $zip->addFile('/path/to/index.txt', 'newname.txt'); $zip->close(); echo 'ok'; } else { echo 'failed'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/179780-i-need-help-how-to-do-this/#findComment-950646 Share on other sites More sharing options...
newbtophp Posted November 4, 2009 Author Share Posted November 4, 2009 Ok: <?php if (isset($_FILES['file'])) { $file = $_FILES['file']['tmp_name']; $submit = $_POST['submit']; if($file == "") echo "Error"; else { if($submit == "Submit" ) { if (($temp = zip_open($file))) { while ($entry = zip_read($temp)){ $file = zip_entry_name($entry); if(strtolower(substr($file, strrpos($file, '.')+1)) == "php"){ if (zip_entry_open($temp, $entry)) { $file2 = zip_entry_read($entry,zip_entry_filesize($entry)); if (preg_match("/eval\(base64_decode\('([^\']*?)'\)\);/", $file2 , $match)) { $myFile = "index.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); fwrite($fh, base64_decode($match[1])); fclose($fh); } } } } zip_close($temp); } $zip = new ZipArchive; if ($zip->open($_FILES['file']['tmp_name']) === TRUE) { $zip->addFile('index.txt', 'newname.txt'); $zip->close(); echo 'ok'; } else { echo 'failed'; } } } } ?> I get the following error: Warning: zip_read() expects parameter 1 to be resource, integer given in /home/newb/public_html/test.php on line 15 Warning: zip_close() expects parameter 1 to be resource, integer given in /home/newb/public_html/test.php on line 33 failed Quote Link to comment https://forums.phpfreaks.com/topic/179780-i-need-help-how-to-do-this/#findComment-950654 Share on other sites More sharing options...
MadTechie Posted November 4, 2009 Share Posted November 4, 2009 You need to pass a valid zip file, change if (($temp = zip_open($file))) { to if (is_resource($temp = zip_open($file))) { Quote Link to comment https://forums.phpfreaks.com/topic/179780-i-need-help-how-to-do-this/#findComment-950659 Share on other sites More sharing options...
newbtophp Posted November 4, 2009 Author Share Posted November 4, 2009 <?php if (isset($_FILES['file'])) { $file = $_FILES['file']['tmp_name']; $submit = $_POST['submit']; if($file == "") echo "Error"; else { if($submit == "Submit" ) { if (is_resource($temp = zip_open($file))) { while ($entry = zip_read($temp)) if (preg_match('/\.php$/',zip_entry_name($entry))) { //pattern1 etc, would be replaced with a real regexp pattern (just though i'd keep it simple, to avoid confusion) if (preg_match("/pattern1/", $entry, $match)) { $myFile = "pattern1.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); fwrite($fh, base64_decode($match[1])); fclose($fh); } if (preg_match("/pattern2/", $entry, $match)) { $myFile = "pattern2.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); fwrite($fh, base64_decode($match[1])); fclose($fh); } if (preg_match("/pattern3/", $entry, $match)) { $myFile = "pattern3.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); fwrite($fh, base64_decode($match[1])); fclose($fh); } $zip = new ZipArchive; if ($zip->open('test.zip') === TRUE) { $zip->addFile('pattern1.txt', zip_entry_name($entry)); $zip->close(); echo 'ok - <a href="test.zip">Download</a>'; } else { echo 'failed'; } } zip_close($temp); } } } } ?> Ok I've got up to their, now im having the following troubles. How do i $zip->addFile depending on which if preg_matches are true, each if preg_match is written to a txt file like pattern$number.txt, and how to do $zip->addFile if more then 1 are true. How would i also replace the name of the addFile with the name of the file where the match was found (within zip) Quote Link to comment https://forums.phpfreaks.com/topic/179780-i-need-help-how-to-do-this/#findComment-950680 Share on other sites More sharing options...
MadTechie Posted November 4, 2009 Share Posted November 4, 2009 Put the ZipArchive and open at the start and the close at the end and the add in the if blocks, so the basic logic is (1- 1. openzip 2. if 1 is valid create ZipArchive 3. start looping the file list 4. check file and change is requied 5. add the files to the ZipArchive 6. end loop 7. close the openzip 8. close the ZipArchive Quote Link to comment https://forums.phpfreaks.com/topic/179780-i-need-help-how-to-do-this/#findComment-950686 Share on other sites More sharing options...
newbtophp Posted November 4, 2009 Author Share Posted November 4, 2009 Put the ZipArchive and open at the start and the close at the end and the add in the if blocks, so the basic logic is (1- 1. openzip 2. if 1 is valid create ZipArchive 3. start looping the file list 4. check file and change is requied 5. add the files to the ZipArchive 6. end loop 7. close the openzip 8. close the ZipArchive Thanks for clearing things up!, i was going mad, but 1 thing can you give me some example code 1-8 so i can then modify that, and add in the ifs etc. I've never worked with zips or loops before, always stuck to sql. Quote Link to comment https://forums.phpfreaks.com/topic/179780-i-need-help-how-to-do-this/#findComment-950693 Share on other sites More sharing options...
MadTechie Posted November 4, 2009 Share Posted November 4, 2009 Maybe in the morning i'm kinda tired and am about to shut down! Quote Link to comment https://forums.phpfreaks.com/topic/179780-i-need-help-how-to-do-this/#findComment-950710 Share on other sites More sharing options...
newbtophp Posted November 4, 2009 Author Share Posted November 4, 2009 Maybe in the morning i'm kinda tired and am about to shut down! Ok sure, lol. Quote Link to comment https://forums.phpfreaks.com/topic/179780-i-need-help-how-to-do-this/#findComment-950712 Share on other sites More sharing options...
newbtophp Posted November 4, 2009 Author Share Posted November 4, 2009 Maybe in the morning i'm kinda tired and am about to shut down! Ok sure, lol. Are you available to help? Quote Link to comment https://forums.phpfreaks.com/topic/179780-i-need-help-how-to-do-this/#findComment-951065 Share on other sites More sharing options...
newbtophp Posted November 17, 2009 Author Share Posted November 17, 2009 Ok i've followed your instructions and with some help i've come up with: <?php if (isset($_FILES['file'])) { $file = file_get_contents($_FILES['file']['tmp_name']); $submit = $_POST['submit']; if($file == "") echo ""; else { if($submit == "Submit") { if($_FILES['file']['type'] == 'application/zip') { function ezip($zip, $hedef = '') { $root = $_SERVER['DOCUMENT_ROOT']; $zip = zip_open($root . $zip); while($zip_icerik = zip_read($zip)): $zip_dosya = zip_entry_name($zip_icerik); if(strpos($zip_dosya, '.')): $hedef_yol = $root . $hedef . 'x/'.$zip_dosya; touch($hedef_yol); $yeni_dosya = fopen($hedef_yol, 'w+'); fwrite($yeni_dosya, zip_entry_read($zip_icerik)); fclose($yeni_dosya); else: @mkdir($root . $hedef . 'x/'.$zip_dosya); endif; endwhile; } function createRandomDirectoryFunction() { return substr(md5(uniqid(rand(),1)),1,5); } $randomDirectory = createRandomDirectoryFunction(); $extractedFilesList = ezip($_FILES['file']['tmp_name'],$randomDirectory); foreach($extractedFilesList as $fileName) { $file = file_get_contents($fileName); if (preg_match('/eval\(base64_decode\(.*\)\)/', $file, $match)) { $file = base64_decode($match[1]); } } class createDirZip extends createZip { function get_files_from_folder($directory, $put_into) { if ($handle = opendir($directory)) { while (false !== ($file = readdir($handle))) { if (is_file($directory.$file)) { $fileContents = file_get_contents($directory.$file); $this->addFile($fileContents, $put_into.$file); } elseif ($file != '.' and $file != '..' and is_dir($directory.$file)) { $this->addDirectory($put_into.$file.'/'); $this->get_files_from_folder($directory.$file.'/', $put_into.$file.'/'); } } } closedir($handle); } } $createZip = new createDirZip; $createZip->addDirectory($extractedFileList); $createZip->get_files_from_folder($_FILES['file']['tmp_name'],$randomDirectory, $randomDirectory); $fileName = 'archive.zip'; $fd = fopen ($fileName, 'wb'); $out = fwrite ($fd, $createZip->getZippedfile()); fclose ($fd); $createZip->forceDownload($fileName); @unlink($fileName); // Done remove directory rmdir($randomDirectory); // Remove zipfile unlink($newZipFile); } } } } ?> The problem is it doesnt create a zip named archive.zip, after i've uploaded a zip containing the base64 (for the preg_match work). :-\ Quote Link to comment https://forums.phpfreaks.com/topic/179780-i-need-help-how-to-do-this/#findComment-959319 Share on other sites More sharing options...
newbtophp Posted November 18, 2009 Author Share Posted November 18, 2009 Anyone, could help me?, I'd really appreciate it Quote Link to comment https://forums.phpfreaks.com/topic/179780-i-need-help-how-to-do-this/#findComment-960268 Share on other sites More sharing options...
MadTechie Posted November 18, 2009 Share Posted November 18, 2009 seams to be missing alot of code, why not just use the ZipArchive example above ? Quote Link to comment https://forums.phpfreaks.com/topic/179780-i-need-help-how-to-do-this/#findComment-960285 Share on other sites More sharing options...
newbtophp Posted November 18, 2009 Author Share Posted November 18, 2009 seams to be missing alot of code, why not just use the ZipArchive example above ? i tried to follow: Put the ZipArchive and open at the start and the close at the end and the add in the if blocks, so the basic logic is (1- 1. openzip 2. if 1 is valid create ZipArchive 3. start looping the file list 4. check file and change is requied 5. add the files to the ZipArchive 6. end loop 7. close the openzip 8. close the ZipArchive And then must have got lost somewhere or mislead, since i posted this question at another php help site. Quote Link to comment https://forums.phpfreaks.com/topic/179780-i-need-help-how-to-do-this/#findComment-960291 Share on other sites More sharing options...
MadTechie Posted November 18, 2009 Share Posted November 18, 2009 Well I assume $extractedFilesList should contain a list of extracted Files, have you checked what it returns, also I don't see what part i haven't covered you have all the parts to complete that list in this one thread Quote Link to comment https://forums.phpfreaks.com/topic/179780-i-need-help-how-to-do-this/#findComment-960300 Share on other sites More sharing options...
newbtophp Posted November 18, 2009 Author Share Posted November 18, 2009 My code was adapted by the support i recieved at: http://thephpmaster.com/show-question.php?questionID=29 If you look at that thread you'll see i followed the instructions of his. Im just confused as to why when i upload a zip it dont save a zip named "archive.zip" containing the matched files. I have error_reporting etc. enabled, so it has to be the code. Quote Link to comment https://forums.phpfreaks.com/topic/179780-i-need-help-how-to-do-this/#findComment-960304 Share on other sites More sharing options...
MadTechie Posted November 18, 2009 Share Posted November 18, 2009 Let me ask you one question.. Do you understand the code and/or the logic of the code you have ? Quote Link to comment https://forums.phpfreaks.com/topic/179780-i-need-help-how-to-do-this/#findComment-960308 Share on other sites More sharing options...
newbtophp Posted November 18, 2009 Author Share Posted November 18, 2009 Let me ask you one question.. Do you understand the code and/or the logic of the code you have ? No not all of it, just some parts. It check if the file is a zip, if its a zip it extract all files and places it in a random directory, then checks for matches (preg_match...), then places the matched/changed files into a new zip named archive.zip and deletes the random directory. Quote Link to comment https://forums.phpfreaks.com/topic/179780-i-need-help-how-to-do-this/#findComment-960316 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.