I want to search for a specific string inside a zip file, "Plugin Name" / "Theme Name" inside a file contents. But the problem is some zip files contain child zip files. How do I perform a search inside all zip files included child zip files and get the string and parent zip path?
Zip 1:
-folder
-folder
-phpfile.php
Zip 2:
-Child zip
-Child zip
-folder
-folder
-phpfile
-style.css
-Child zip
I want to find the Theme Name inside the style.css for example.
Style.css contain:
/*
Theme Name: Twenty Twenty
Theme URI: https://wordpress.org/themes/twentytwenty/
Author: the WordPress team
Author URI: https://wordpress.org/
Description: Our default theme for 2020 is designed to take full advantage of the flexibility of the block editor. Organizations and businesses have the ability to create dynamic landing pages with endless layouts using the group and column blocks. The centered content column and fine-tuned typography also makes it perfect for traditional blogs. Complete editor styles give you a good idea of what your content will look like, even before you publish. You can give your site a personal touch by changing the background colors and the accent color in the Customizer. The colors of all elements on your site are automatically calculated based on the colors you pick, ensuring a high, accessible color contrast for your visitors.
Tags: blog, one-column, custom-background, custom-colors, custom-logo, custom-menu, editor-style, featured-images, footer-widgets, full-width-template, rtl-language-support, sticky-post, theme-options, threaded-comments, translation-ready, block-styles, wide-blocks, accessibility-ready
Version: 1.3
Requires at least: 5.0
Tested up to: 5.4
Requires PHP: 7.0
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Text Domain: twentytwenty
This theme, like WordPress, is licensed under the GPL.
Use it to make something cool, have fun, and share what you've learned with others.
*/
Code example:
$search='Plugin Name';
$replace=' --BRIGHT YELLOW HIPPOPOTAMUS-- ';
function modifyzip($archive,$search,$replace)
{
$zip = new ZipArchive();
$status = $zip->open( $archive );
if( $status == true ){
/* Get the count of files BEFORE the loop */
$filecount = $zip->numFiles;
for( $i=0; $i < $filecount; $i++ ){
$name = $zip->getNameIndex( $i );
$content = $zip->getFromName( $name );
$edited = str_replace( $search, $replace, $content );
$zip->deleteName( $name );
$zip->addFromString( $name, $edited );
}
}
$zip->close();
}
$dir=DIR . '/zips/';
$col=glob( $dir . '*.zip' );
if( $col && count( $col ) > 0 ){
foreach( $col as $archive ){
call_user_func( 'modifyzip',$archive,$search,$replace );
}
}
But it will replace a string
Also this way can read a php file but it won't help so much because need to find a specific string inside all contents:
$zip = new ZipArchive;
if ($zip->open('test1.zip') === TRUE) {
echo $zip->getFromName('testfromfile.php');
$zip->close();
} else {
echo 'failed';
}
I am new to PHP so I ask for help.
EDIT:
I have tried the code on WAMP (Localhost, Windows 10)
$path = getcwd()."/files";
$object = new PhpDirectoryIterator();
$object->addModifier("css", function ($path) {
if(strpos(file_get_contents($path), "Theme Name: ") !== false) {
$explodedPath = explode('/', $path);
$fileName = end($explodedPath);
echo "Found it in : " . $fileName;
}
}
);
Failed with this error:
Warning: scandir(C:\wamp64\www\ziptest/files/actionable-google-analytics-woocommerce.zip301a8ba5a9b4cb56eb47debf7ceda1da,C:\wamp64\www\ziptest/files/actionable-google-analytics-woocommerce.zip301a8ba5a9b4cb56eb47debf7ceda1da): The system cannot find the file specified. (code: 2) in C:\wamp64\www\ziptest\index2.php on line 24
I have a folder with ZIP Files which i want to make the search