I'm trying this
<?php
/* read the PHP source code */
$source_code = file_get_contents("hello.php");
$source_code = preg_replace('#^<\?php\s+#', '', $source_code);
$source_code = preg_replace('#\s+\?>\s*$#', '', $source_code);
/* create the encrypted version */
$redistributable_key = blenc_encrypt($source_code, "encrypt.php", "my_fixed_password");
$key_file = __DIR__ ."\keys";
file_put_contents($key_file, $redistributable_key . "\n", FILE_APPEND);
include 'encrypt.php';
echo $hello;
?>
hello.php
<?php
$hello = "Ciao";
I got this error
PHP Fatal error: blenc_compile: Validation of script
'encrypt.php' failed, cannot execute.
Please note that:
The key file is created, I'm already using the '\n' fix
I replaced <?php and ?> because another Stack Overflow question told me that it's a problem
<?php
$file_name = basename($file);
$unencrypted_key = = md5(time());
$source_code = file_get_contents($file);
//This covers old-asp tags, php short-tags, php echo tags, and normal php tags.
$contents = preg_replace(array('/^<(\?|\%)\=?(php)?/', '/(\%|\?)>$/'), array('',''), $source_code);
$html .= "<br> BLENC blowfish unencrypted key: $unencrypted_key" . PHP_EOL;
$html .= "<br> BLENC file to encode: " . $file_name . PHP_EOL;
//file_put_contents('blencode-log', "---\nFILE: $file_name\nSIZE: ".strlen($contents)."\nMD5: ".md5($contents)."\n", FILE_APPEND);
$redistributable_key = blenc_encrypt($contents, TARGET_DIR . '/blenc/' . $file_name, $unencrypted_key);
$html .= "<br> BLENC size of content: " . strlen($contents) . PHP_EOL;
/**
* Server key
* key_file.blenc
*/
file_put_contents(TARGET_DIR . '/blenc/' . 'key_file.blenc', $redistributable_key . PHP_EOL);
$html .= "<br> BLENC redistributable key file key_file.blenc updated." . PHP_EOL;
exec("cat key_file.blenc >> /usr/local/etc/blenckeys");
?>
https://github.com/codex-corp/ncryptd/blob/master/app/controllers/MagicalController.php#L479
you should put the key to
blenckeys
file on your server
Note: Sometimes you need to reload the apache, if you have "Validation of script" issues
How to use BLENC in PHP?
Related
I have a project where I have to make a new log file if the current log file is larger than 50mb. I know that we have to test for the size of the file.
I am starting out with this $log variable:
$log =
"Branch: ".$branchDir . PHP_EOL.
"Phase: ". $phaseDir . PHP_EOL.
"Total number of results files deleted: ". count($folderCounter). PHP_EOL.
"File names: " . $filePathToDelete . PHP_EOL.
"Starting CustomerID directory name: " . $CustIDvalue . PHP_EOL;
// $log = 'this is a test' . $branch . PHP_EOL;
$dt = time();
$mysql_datetime = strftime("%Y-%m-%d %H:%M:%S", $dt);
$mysql_datetimes = preg_replace('/\s+/', '', $mysql_datetime);
$logFileName = "ResultsFileDeletion" . $mysql_datetimes . ".txt";
if (unlink($filePathToDelete)) {
//this echo string will be moved to a log file
echo "first unlink: {$filePathToDelete} was deleted <br>";
$myFile =
file_put_contents("c:\\sites\\EtonBio/sequencing/logs/{$logFileName}",
$log, FILE_APPEND);
echo 'my file: ' . $myFile . '<br>';
}
I found this code snippet:
$size = filesize($_FILES['foto']['tmp_name']);
I believe my if statement would look something like this:
if($size < 52428800) {
*creates another log file
}
I want to format the $size variable correctly and I am also not sure how to make a new log file.
$size = filesize($_FILES['foto']['tmp_name']); only works if you are uploading the file.
If you want to check a file already in you server ou can simply do
$size = filesize("path/to/file");
Also, there is a ' in your $log declaration that shouldn't be there
I have a testcode.php file need to encode:
<?php
$hello = "Hello World!";
?>
And I created file encode.php to encrypt and test that file:
<?php
/* read the PHP source code */
$source_code = file_get_contents("testcode.php");
/* create the encrypted version */
$redistributable_key = blenc_encrypt($source_code, "encrypt.php");
/* read which is the key_file */
$key_file = ini_get('blenc.key_file');
/* save the redistributable key */
file_put_contents($key_file, $redistributable_key, FILE_APPEND);
include 'encrypt.php';
echo $hello;
?>
but I recevied these errors when I ran encode.php:
Warning: blenc_compile: Validation of script 'encrypt.php' failed.
MD5_FILE: 910e6a45f806ba3dc42830839971cb53
MD5_CALC: c38a6b2f389267a272ea656073a463ed in
C:\xampp\htdocs\PHPEncode\encode.php on line 14
and
Fatal error: blenc_compile: Validation of script 'encrypt.php' failed,
cannot execute. in C:\xampp\htdocs\PHPEncode\encode.php on line 14
Help me fix it, thank you! :)
BLENC has issues when there is more than one redistributable key in blenc.key_file. See PHP bug #68490 that I've reported.
Also when you run your script multiple times, redistributable keys will get corrupted in blenc.key_file. This is because you are appending to the file, but all keys are saved on the same line (the same broken example is on php manual page). You should change it to:
file_put_contents($key_file, $redistributable_key."\n", FILE_APPEND);
The second Fatal error you got was probably because of corrupted blenc.key_file.
;) just delete "<?php ?>" in your page *.php
compiled this not with "<?php and ?>"
just
$hello = "Hello World!";
and is ok :) !
<?php
$file_name = basename($file);
$source_code = file_get_contents($file);
//This covers old-asp tags, php short-tags, php echo tags, and normal php tags.
$contents = preg_replace(array('/^<(\?|\%)\=?(php)?/', '/(\%|\?)>$/'), array('',''), $source_code);
$html .= "<br> BLENC blowfish unencrypted key: $unencrypted_key" . PHP_EOL;
$html .= "<br> BLENC file to encode: " . $file_name . PHP_EOL;
//file_put_contents('blencode-log', "---\nFILE: $file_name\nSIZE: ".strlen($contents)."\nMD5: ".md5($contents)."\n", FILE_APPEND);
$redistributable_key = blenc_encrypt($contents, TARGET_DIR . '/blenc/' . $file_name, $unencrypted_key);
$html .= "<br> BLENC size of content: " . strlen($contents) . PHP_EOL;
/**
* Server key
* key_file.blenc
*/
file_put_contents(TARGET_DIR . '/blenc/' . 'key_file.blenc', $redistributable_key . PHP_EOL);
$html .= "<br> BLENC redistributable key file key_file.blenc updated." . PHP_EOL;
exec("cat key_file.blenc >> /usr/local/etc/blenckeys");
?>
https://github.com/codex-corp/ncryptd/blob/master/app/controllers/MagicalController.php#L479
You need to specify the full location of the blenc.key_file in php.ini variable called blenc.key_file or via .htaccess , setting at runtime with ini_set() is not possible (at this moment the key file has already been read).
.htaccess example:
php_value blenc.key_file /path/path/path/key_file.blenc
Every time you encrypt a file a new $redistributable_key will be generated!
You have to include all keys in the key#
Or use a fixed (private) encryption key for all your encryption:
$private_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxVCHANGEME";
$redistributable_key = blenc_encrypt($source_code, "encrypt.php", $private_key);
I am creating a plugin for a WP site and I have to make an initial import of a huge amount of files from another directory. (The plugin is working well, etc etc.) I want to run the import using command line. Everything is OK if executing on local (Windows), but when I execute from Linux bash the defined constants are not evaluated but printed as a string.
define("ASCOR_RECORDINGS_TBL", $wpdb->prefix . "recordings");
Is there any setting I have to make to enable evaluation of PHP defined constants? My website is running on Dreamhost server.
When I execute the folowing line:
[myserver]$ /usr/local/php5/bin/php /home/path/to/import.php
I get:
WordPress database error Table 'db_ro.ASCOR_RECORDINGS_TBL' doesn't exist for query INSERT INTO `ASCOR_RECORDINGS_TBL` ...........
The content of the file I execute:
<?php
require_once dirname(dirname(dirname(dirname(__FILE__)))) . "/wp-load.php";
require_once "class/AscorDbHelper.php";
$origin = realpath("/path/to/files");
$upload_dir = wp_upload_dir();
$subdir = $upload_dir['subdir'];
mkdir(ASCOR_UPLOAD_DIR . "/" . $subdir, 777, true);
require_once $origin . "/classes/PConfFiles.inc.php";
$db = new AscorDbHelper();
$cf = new PConfFiles('/^PC_([0-9\-]+)_(.*)\.([a-z0-9]{2,4})$/i', $origin);
$list = $cf->getFilesList();
$catPC = $db->addCategory("Special");
$catOther = $db->addCategory("Other");
if($list){
$pc = $db->addAuthor("Ciprian V.");
foreach($list as $rec){
$fileUrl = $subdir . "/" . $rec[0];
$desc = str_replace("-", " ", $rec[2]);
copy(realpath($origin . "/" . $rec[0]), ASCOR_UPLOAD_DIR . "/" . $fileUrl );
$db->addRecording($fileUrl, $catPC->id, $pc->id, $desc, null, $rec[1]);
echo "Added: " . $rec[0] . "\n";
}
}
$cf = new PConfFiles('/^([0-9\-]+)\_([^\_]+)_(.*)\.([a-z0-9]{2,4})$/i', $origin);
$list = $cf->getFilesList();
if($list){
foreach($list as $rec){
$authorName = str_replace("-", " ", $rec[2]);
$date = $rec[1];
$desc = str_replace("-", " ", $rec[3]);
$fileUrl = $subdir . "/" . $rec[0];
$authorId = $db->getAuthorIdOrSaveIt($authorName);
copy(realpath($origin . "/" . $rec[0]), ASCOR_UPLOAD_DIR . "/" . $fileUrl );
$db->addRecording($fileUrl, $catOther->id, $authorId, $desc, null, $date);
echo "Added: " . $rec[0] . "\n";
}
}
echo "done";
The constants are defined in the main file of the plugin:
define("ASCOR_RECORDINGS_TBL", $wpdb->prefix . "recordings");
define("ASCOR_RECORDINGS_AUTHORS_TBL", $wpdb->prefix . "recordings_authors");
define("ASCOR_RECORDINGS_CATEGORIES_TBL", $wpdb->prefix . "recordings_categories");
define("ASCOR_RECORDS_PER_PAGE", 50);
define("ASCOR_EXTEND_DIR", dirname(__FILE__));
define("ASCOR_EXTEND_URL", plugins_url("", __FILE__));
define("ASCOR_NOTIFY_UPDATED", "updated");
define("ASCOR_NOTIFY_ERROR", "error");
define("ASCOR_UPLOAD_DIR", ABSPATH . "/wp-content/uploads/recordings");
Is the file loaded? Introduce a parse error into it and see if it fails. And try it on Windows too.
In appending an attachment I am receiving a warning message preventing me attaching an existing pdf file to my e-mail. I know it isn't my headers since the script is successfully attaching a vcard generated by a string earlier within it.
Since I am not editing the file, TDPDF & FPDF shouldn't be required. (not 100% on that though) Here is the code I've been working with. I've included my output testing lines & comments.
//File Definition
$filepath = "docs/";
//$filepath = "./docs/"; //tried: same result
$fullpath = $filepath . $filename; //$filename defined earlier
//File Manipulation
$file = fopen($fullpath,'rb');
$pdfdata = fread($file, filesize($fullpath));
fclose($file);
//Testing
echo $fullpath . "<br />\n";
echo "Filename: " .$filename . "<br />\n";
echo "Filesize: " .filesize($fullpath). "<br />\n";
echo "String Length: " .strlen($pdfdata). "<br />\n";
//The Following line proved the variable is dumping properly,
//but its content cannot be used for file_get_contents...huh?
//var_dump($pdfdata); //Only used for proofing
echo "Probable Errors for file_get_contents<br />\n";
$data = file_get_contents($pdfdata);
// The following line: Sends File, but is 0 bytes
//$attachment = chunk_split(base64_encode($pdfdata));
//default
$attachment = chunk_split(base64_encode(file_get_contents($pdfdata)));
This outputs:
docs/pdf-to-send.pdf
Filename: pdf-to-send.pdf
Filesize: 37907
String Length: 37907
Probable Errors for file_get_contents
Warning: file_get_contents(%PDF-1.5 % ... (truncated by me)
... ) [function.file-get-contents]: failed to open stream: No such file or directory in /my/hosting/directory/mailer.php on line 337
Warning: file_get_contents(%PDF-1.5 % ... (truncated by me )
... ) [function.file-get-contents]: failed to open stream: No such file or directory in /my/hosting/directory/mailer.php on line 339
Its telling me the file size, that can be found in 2 different variables: $pdfdata & $filesize. They match up. I will mention that the response that I truncated (due to the charset) is already truncated by the server. Its why I started checking the length.
Finally, just in case it could possibly be my headers since I was able to successfully send a 0 byte file, here are those lines...
$body .= "--". $mime_boundary. "\r\n" ;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"". "\r\n";
$body .= "Content-Transfer-Encoding: base64" . "\r\n";
$body .= "Content-Disposition: attachment;" . "\r\n";
$body .= "filename=\"".$filename."\"" . "\r\n\n";
$body .= $attachment . "\n\n";
I am aware that I can change (and have tried) the "Content-Type" to "application/pdf".
My charset is UTF-8. I may be misunderstanding "binary-safe" descriptions for fopen() & fread(), but that shouldn't cause the script to fail. Should it?
Any help resolving this would be greatly appreciated.
Ok. I fixed it. Oddly enough it WAS in my headers.
The header command I posted was actually correct. Sadly I posted my vcard attachment body ammendment. So the question already had my answer in it. ::bonk::
This line is correct.
$body .= "filename=\"".$filename."\"" . "\r\n\n";
$body .= $attachment . "\n\n";
This is what I actually had.
$body .= "filename=\"".$filename."\"" . "\r\n";
This explains why I had a 0 byte attachment.
And, just to show the snippet that actually worked:
//File Manipulation
$file = fopen($fullpath,'rb');
$pdfdata = fread($file, filesize($fullpath));
fclose($file);
//Testing
// echo $fullpath . "<br />\n";
//echo "Filename: " .$filename . "<br />\n";
//echo "Filesize: " .filesize($fullpath). "<br />\n";
// echo "String Length: " .strlen($pdfdata). "<br />\n";
//var_dump($pdfdata); // Don't remove this (as a comment) again. LOL
// echo "Probable Errors for file_get_contents<br />\n";
//$data = file_get_contents($pdfdata, true);
$attachment = chunk_split(base64_encode($pdfdata));
//$attachment = chunk_split(base64_encode(file_get_contents($pdfdata)));
Sorry if I wasted anyone's time.
I have a question here, tho, I've been digging here at SO, it seems I can't find the real deal;
I am trying the ff:
<?php
$filename = $trantype . $delimiter . $dateToday . $fileExtension ;
//echo $filename . '<br/>';
$fileToOpen = $filepath . $filename;
echo "File To Open: " . $fileToOpen . '<br/>';
$string = file_get_contents($fileToOpen);
//$string = file_get_contents("../transactions/o/O_20120809.xx");
$json_array = json_decode($string, true);
echo "Echo: " . $json_array[0]['itemheader_sysid'] . '<br/>';
echo "The File Contents: " . $string;
?>
The $string = file_get_contents('../transaction/o/O_20120809.xx') works smoothly on the other hand the $string = file_get_contents($fileToOpen); doesn't seem to work and is giving me the ff: error;
Warning: file_get_contents(../transactions/o/0_20120809.xx) [function.file-get-contents]: failed to open stream: No such file or directory in C:\xampp\htdocs\xx\helper\upo.php on line 19
why so?
Anyone please?