How to encrypt PHP with HTML file using phpBolt? - php

I'm trying to encrypt .php file using phpBolt. when only with PHP code then it's working, But if it's a mix of HTML then it is not working.
Encryption code:
<?php
/**
* src : source folder
* encrypted : Output folder
*/
$src = 'src';
$php_blot_key = "kyc7fh";
/**
* No need to edit following code
*/
$excludes = array('vendor');
foreach($excludes as $key => $file){
$excludes[ $key ] = $src.'/'.$file;
}
// $rec = new RecursiveIteratorIterator(new RecursiveDirectoryIterator( $src ));
$rec = new DirectoryIterator($src);
$require_funcs = array('include_once', 'include', 'require', 'require_once');
foreach ($rec as $file) {
if ($file->isDir()) {
$newDir = str_replace( 'src', 'encrypted', $file->getPath() );
if( !is_dir( $newDir ) ) mkdir( $newDir );
continue;
};
$filePath = $file->getPathname();
if( pathinfo($filePath, PATHINFO_EXTENSION) != 'php' ||
in_array( $filePath, $excludes ) ) {
$newFile = str_replace('src', 'encrypted', $filePath );
copy( $filePath, $newFile );
continue;
}
$contents = file_get_contents( $filePath );
$preppand = '<?php define("PHP_BOLT_KEY", "kyc7fh"); bolt_decrypt( __FILE__ , PHP_BOLT_KEY); return 0;
##!!!##';
$re = '/\<\?php/m';
preg_match($re, $contents, $matches );
if(!empty($matches[0]) ){
$contents = preg_replace( $re, '', $contents );
##!!!##';
}
/*$cipher = bolt_encrypt( "?> ".$contents, $php_blot_key );*/
$cipher = bolt_encrypt( $contents, $php_blot_key );
$newFile = str_replace('src', 'encrypted', $filePath );
$fp = fopen( $newFile, 'w');
fwrite($fp, $preppand.$cipher);
fclose($fp);
unset( $cipher );
unset( $contents );
}
$out_str = substr_replace($src, '', 0, 4);
$file_location = __DIR__."/encrypted/".$out_str;
echo "Successfully Encrypted... Please check in <b>" .$file_location."</a></b> folder.";
encryption not working in this .php file :
<html>
<body>
<h1>
<?php
echo "Hello Sarbaz Ali !!!";
?>
</h1>
</body>
</html>
But when the file is look like this, then it will work:
<?php
echo "<h1> Hello Sarbaz Ali !!! </h1>";
?>
Can I encrypt the above file (with HTML tags) using phpBolt?

Depending on the Laravel-Source-Encrypter package, which is based on phpBolt encoder:
The blade files are not real PHP files. They mostly contain HTML and CSS codes.
Blade files are HTML code with PHP code, so there is no meaning in encrypting HTML code because there is no logic inside it.
phpBolt can only decrypt-and-execute php files (as a single indivisible step), it is not going to be possible to decrypt them again.
You can see also: Can you tell me the how we can encrypt blade file?

I would suggest that write your php code in seperate file , encrypt it with php_bolt and then include that file using PHP include function.

Related

How do I edit multiple .txt files with different names in a folder?

I have a folder that contains several .txt files called :
A500_1.txt
A500_2.txt
A700_1.txt
A700_2.txt
A900_1.txt
...
In each of the .txt files there is :
PRXC1_|TB|CCAAO9-RC|9353970324463|24.99
PRXC1_|TB|CFEXK4-RC|9353970294766|84.99
PRXC1_|TB|CFEXK4-RC|9353970294773|84.99
...
I'd like you to :
if the filename starts with A500_ replace "TB" with "MD"
if the filename starts with A700_ replace "TB" by "JB"
if the filename senter code heretarts with A900_ replace "TB" with "LD"
I wrote this function but it just creates me an empty A500_2.TXT file at the root of the project and displays :
Warning: file_get_contents(A500_2.TXT): failed to open stream:
Where's my error?
<?php
function processFile( $path ) {
$dir = './test/';
$allFiles = scandir($dir);
foreach($allFiles as $file) {
$filename = basename( $file );
if ( ! in_array($file,array(".","..")))
{
//read the entire string
$str = file_get_contents( $file );
// var_dump($str);
// replace something in the file string
if ( strpos( $filename, 'A500_' ) === 0 ) {
$str = str_replace( 'TB', 'MD', $str );
} else if ( strpos( $filename, 'A700_' ) === 0 ) {
$str = str_replace( 'TB', 'JB', $str );
} else if ( strpos( $filename, 'A900_' ) === 0 ) {
$str = str_replace( 'TB', 'LD', $str );
} else {
// Return false if we don't know what to do with this file
return false;
}
//write the entire string
$writeResult = file_put_contents( $file, $str );
//return true after a file is written successfully, or false on failure
return $writeResult >= 0;
}
}
}
if(processFile( './test/' )) echo "good!";
?>
Both the file_get_contents warning and the blank file being created are down to the same problem - scandir returns just the filename, not a relative path to the currently running script.
I'd guess that you're expecting it to return relative paths, which is why you're calling basename at the top of your loop. As it is, your $file and $filename arguments are going to always be set to the same thing.
The quickest solution will be to prepend $file with the scanned directory name before processing anything else:
$file = $dir . $file;
This should fix both the read and write calls.

How to save the image fetched from DOCX by PHP using ZipArchive

Brief description: I have a Docx file. I have done a simple code in PHP which extracts the images in that file and display it on the page.
What I want to achieve: I want that these images should be saved beside my php file with the same name and format.
My folder has sample.docx (which has images), extract.php (which extracts the images from docx) and display.php
Below is the code of extract.php
<?php
/*Name of the document file*/
$document = 'sample.docx';
/*Function to extract images*/
function readZippedImages($filename) {
/*Create a new ZIP archive object*/
$zip = new ZipArchive;
/*Open the received archive file*/
if (true === $zip->open($filename)) {
for ($i=0; $i<$zip->numFiles;$i++) {
/*Loop via all the files to check for image files*/
$zip_element = $zip->statIndex($i);
/*Check for images*/
if(preg_match("([^\s]+(\.(?i)(jpg|jpeg|png|gif|bmp))$)",$zip_element['name'])) {
/*Display images if present by using display.php*/
echo "<image src='display.php?filename=".$filename."&index=".$i."' /><hr />";
}
}
}
}
readZippedImages($document);
?>
display.php
<?php
/*Tell the browser that we want to display an image*/
header('Content-Type: image/jpeg');
/*Create a new ZIP archive object*/
$zip = new ZipArchive;
/*Open the received archive file*/
if (true === $zip->open($_GET['filename'])) {
/*Get the content of the specified index of ZIP archive*/
echo $zip->getFromIndex($_GET['index']);
}
$zip->close();
?>
How can I do that?
I not sure that you need to open the zip archive multiple times like this - especially when another instance is already open but I'd be tempted to try something like the following - I should stress that it is totally untested though.
Updated after testing:
There is no need to use display.php if you do like this - seems to work ok on different .docx files. The data returned by $zip->getFromIndex yields the raw image data ( so I discovered )so passing that in a query string is not possible due to the length. I tried to avoid opening/closing the zip archive unnecessarily hence the approach below which adds the raw data to the output array and the image is then displayed using this base64 encoded data inline.
<?php
#extract.php
$document = 'sample.docx';
function readZippedImages($filename) {
$paths=[];
$zip = new ZipArchive;
if( true === $zip->open( $filename ) ) {
for( $i=0; $i < $zip->numFiles;$i++ ) {
$zip_element = $zip->statIndex( $i );
if( preg_match( "([^\s]+(\.(?i)(jpg|jpeg|png|gif|bmp))$)", $zip_element['name'] ) ) {
$paths[ $zip_element['name'] ]=base64_encode( $zip->getFromIndex( $i ) );
}
}
}
$zip->close();
return $paths;
}
$paths=readZippedImages( $document );
/* to display & save the images */
foreach( $paths as $name => $data ){
$filepath=__DIR__ . '/' . $name;
$dirpath=pathinfo( $filepath, PATHINFO_DIRNAME );
$ext=pathinfo( $name, PATHINFO_EXTENSION );
if( !file_exists( $dirpath ) )mkdir( $dirpath,0777, true );
if( !file_exists( $filepath ) )file_put_contents( $filepath, base64_decode( $data ) );
printf('<img src="data:image/%s;base64, %s" />', $ext, $data );
}
?>

Php that should add +1 to a file name instead moves file to the back

So I have a bit of an issue. I'm trying to create something that once ran will check if the file it's trying to save as exists, if it exists then it renames the existing file its number +1, and what it's suppose to do is if that file exists then rename that file.
So basically
1(A) 2(B) 3(C)
Save file X as 1
1(X) 2(A) 3(B) 4(C)
But currently instead of that it's moving the first file being renamed to the last number and I'm unsure how to fix it.
What it's doing
1(A) 2(B) 3(C)
Save file X as 1
1(X) 2(B) 3(C) 4(A)
<?php
ob_start(); ?>
<html>
All HTML here
</html>
<?php
$path = "cache/home-".$imputnum.".html";
?>
<?php
if (file_exists($path)) {
$i=$imputnum;
$new_path=$path;
while (file_exists($new_path))
{
$extension = "html";
$filename = "home";
$directory = "cache";
$new_path = $directory . '/' . $filename . '-' . $i . '.' . $extension;
$i++;
}
if(!rename($path, $new_path)){
echo 'error renaming file';
}
}
?>
<?php
$fp = fopen("cache/home-".$imputnum.".html", 'w');
fwrite($fp, ob_get_contents());
ob_end_flush();
?>
If there are already 3 files, you'll need to rename all 3 of them.. For instance
x-3.ext -> x-4.ext
x-2.ext -> x-3.ext
x-1.ext -> x-2.ext
(from last to first). So, the rename must be inside a loop.
Here's an example:
function save_file( $name, $ext, $content ) {
$f = "$name.$ext";
$i = 0;
while ( file_exists( $f ) )
$f = "$name-".++$i.".$ext";
while ( $i > 0 )
rename( $name.(--$i==0?"":"-$i").".$ext", "$name-".($i+1).".$ext" );
file_put_contents( "$name.$ext", $content );
}
save_file( "home", "html", "A" );
save_file( "home", "html", "AB" );
save_file( "home", "html", "ABC" );
save_file( "home", "html", "ABCD" );
After this is run, we have:
home.html: "ABCD"
home-1.html: "ABC"
home-2.html: "AB"
home-3.html: "A"
here's a snippet of how i did it
// Get a unique filename
$filename = "$IMAGES_DIR/UploadedImg.$ext";
while(file_exists($filename)){
$chunks = explode(".", $filename);
$extention = array_pop($chunks);
$basename = implode(".", $chunks);
$num = isset($num) ? ($num+1) : 0;
$filename = "$basename$num.$extention";
if(file_exists($filename)) $filename = "$basename.$extention";
}
What you are trying to do sounds like a job for array_unshift which prepends to an array. See http://php.net/manual/en/function.array-unshift.php
After inserting the base file name without numbers into the array, I would loop through the array and append/prepend the index like so:
$filenames = $exising_filenames;
array_unshift($filenames, $new_filename);
foreach($filenames as $index => &$filename {
// Do some operation to remove the numbers from filename here.
// Now add back the number using the array's index.
$filename = ($index+1).$filename;
// You may rename the existing files using the filenames.
}
// Insert the new file with filename using $filenames[0]

Exploring a file structure using php using scandir()

I am new to php and trying to learn how to navigate a local file structure in for the format:
-Folder
-SubFolder
-SubSubFolder
-SubSubFolder
-SubFolder
-SubSubFolder
...
From another stackoverflow question I have been able to use this code using scandir():
<?php
$scan = scandir('Folder');
foreach($scan as $file)
{
if (!is_dir($file))
{
$str = "Folder/".$file;
echo $str;
}
}
?>
This allows me to generate a list of strings of all the 'SubFolder' in my folder directory.
What I am trying to do is list all the 'SubSubFolder' in each 'SubFolder', so that I can create a string of the 'SubSubFolder' name in combination with its 'SubFolder' parent and add it to an array.
<?php
$scan = scandir('Folder');
foreach($scan as $file)
{
if (!is_dir($file))
{
$str = "Folder/".$file;
//echo $str;
$scan2 = scandir($str);
foreach($scan2 as $file){
if (!is_dir($file))
{
echo "Folder/SubFolder/".$file;
}
}
}
}
?>
This however isn't working, and I wasn't sure if it was because I cannot do consecutive scandir() or if I cannot use $file again.
There is probably a better solution, but hopefully the following will be of some help.
<?php
function getDirectory( $path = '.', $level = 0 ){
$ignore = array( 'cgi-bin', '.', '..' );
// Directories to ignore when listing output. Many hosts
// will deny PHP access to the cgi-bin.
$dh = #opendir( $path );
// Open the directory to the handle $dh
while( false !== ( $file = readdir( $dh ) ) ){
// Loop through the directory
if( !in_array( $file, $ignore ) ){
// Check that this file is not to be ignored
$spaces = str_repeat( ' ', ( $level * 4 ) );
// Just to add spacing to the list, to better
// show the directory tree.
if( is_dir( "$path/$file" ) ){
// Its a directory, so we need to keep reading down...
echo "<strong>$spaces -$file</strong><br />";
getDirectory( "$path/$file", ($level+1) );
// Re-call this same function but on a new directory.
// this is what makes function recursive.
} else {
//To list folders names only and not the files within comment out the following line.
echo "$spaces $file<br />.";
// Just print out the filename
}
}
}
closedir( $dh );
// Close the directory handle
}
getDirectory( "folder" );
// Get the current directory
?>

problem with folder handling with php

Friends,
I have a problem............
Help me please........
Am getting the image url from my client, i want to store those images in my local folder.
if those images are in less, i will save them manually
But they are greater than 5000 images.........
Please give some code to down load all the images with PHP
you could try file_get_contents for this. just loop over the array of files and use file_get_contents('url'); to retrieve the files into a string and then file_put_contents('new file name'); to write the files again.
You may download file using file_get_contents() PHP function, and then write it on your local computer, for example, with fwrite() function.
The only opened question is, where to get list of files supposed to be downloaded - you did not specify it in your question.
Code draft:
$filesList = // obtain URLs list somehow
$targetDir = // specify target dir
foreach ($filesList: $fileUrl) {
$urlParts = explode("/", $fileUrl);
$name = $urlParts[count($urlParts - 1)];
$contents = file_get_contents($fileUrl);
$handle = fopen($targetDir.$filename, 'a');
fwrite($handle, $contents);
fclose($handle);
}
I'm not sure that this is what you want. Given a folder's (where PHP has the authority to get the folder's contents) URL and a URL you want to write to, this will copy all of the files:
function copyFilesLocally( $source, $target_folder, $index = 5000 )
{
copyFiles( glob( $source ), $target_folder, $index );
}
function copyFiles( array $files, $target_folder, $index )
{
if( count( $files ) > $index )
{
foreach( $files as $file )
{
copy( $file, $target_folder . filename( $file ) );
}
}
}
If you're looking to a remote server, try this:
function copyRemoteFiles( $directory, $target_folder, $exclutionFunction, $index = 5000)
{
$dom = new DOMDocument();
$dom->loadHTML( file_get_contents( $directory ) );
// This is a list of all links which is what is served up by Apache
// when listing a directory without an index.
$list = $dom->getElementsByTagName( "a" );
$images = array();
foreach( $list as $item )
{
$curr = $item->attributes->getNamedItem( "href" )->nodeValue;
if( $exclutionFunction( $curr ) )
$images[] = "$directory/$curr";
}
copyFiles( $images, $target_folder, $index );
}
function exclude_non_dots( $curr )
{
return strpos( $curr, "." ) != FALSE;
}
copyRemoteFiles( "http://example.com", "/var/www/images", "exclude_non_dots" );

Categories