Code for using bcompiler to encrypt PHP files _ PHP

Source: Internet
Author: User
Tags glob
I accidentally saw this function code on the Internet and haven't tried it yet. I will try it later. Add to favorites. Instructions for use:

// Load the function
Include_once ('phpcodezip. php ');
// Create an encrypted file (the Directory of the PHP file to be encrypted by sourceDir and the Directory of the files encrypted by targetDir)
$ Encryption = new PhoCodeZip ('sourcedir', 'targetdir ');
// Execute row encryption
$ Encryption-> zip ();

PhpCodeZip. php source code download
PhpCodeZip.rar
PhpCodeZip. php source code content
The code is as follows:
/*
* @ License: MIT & GPL
*/
Class PhpCodeZip {
// Source information to be encrypted
Var $ sourceDir = '.';
// Encrypt the stored data
Var $ targetDir = 'tmp ';
// Whether the data is encrypted
Var $ bcompiler = true;
// Whether to remove the blank trim to parse the rows
Var $ strip = true;
// Route records of the source data records
Var $ sourcefilePaths = array ();
// Destination information records
Var $ targetPaths = array ();
// Enter the data size before encryption
Var $ sizeBeforeZip = null;
// Encrypt the encrypted data size.
Var $ sizeAfterZip = null;
// Extract the output of the rows
Var $ newline = '';
/**
* Create a constructor
*
* @ Param string $ sourceDir Origin Resource
* @ Param string $ targetDir target resource
* @ Param boolean $ whether bcompiler is encrypted
* @ Param boolean $ whether strip removes the blank limit to parse the limit row
* @ Return boolean
*/
Public function PhpCodeZip ($ sourceDir = '.', $ targetDir = 'tmp ', $ bcompiler = true, $ strip = true ){
// Configure the initial changes
$ This-> sourceDir = $ sourceDir;
$ This-> targetDir = $ targetDir;
$ This-> bcompiler = $ bcompiler;
// Check whether the source data exists

If (! Is_dir ($ this-> sourceDir )){
Die ('specifies the source resource quota'. $ this-> sourceDir. 'does not exist. please reset it ');
} Else {
// If the specified destination resource exists, cut down the duplicate shard.
If (is_dir ($ this-> targetDir )){
Echo '[initialize destination information]'. $ this-> newline. $ this-> newline;
$ This-> cleanDir ($ this-> targetDir, true );
}
// Establish the same object information as the source data structure
Mkdir ($ this-> targetDir, 0777 );
$ Dir_paths = $ this-> getPaths ($ this-> sourceDir, '*', GLOB_ONLYDIR );
Foreach ($ dir_paths as $ key => $ path ){
$ Path = explode ('/', $ path );
$ Path [0] = $ this-> targetDir;
Echo '=>'. join ('/', $ path). $ this-> newline;
Mkdir (join ('/', $ path), 0777 );
}
// Retrieve the inventory statement of the source data
$ This-> sourcefilePaths = $ this-> getPaths ($ this-> sourceDir ,'*');
// Configure the checklist for the corresponding destination
Foreach ($ this-> sourcefilePaths as $ key => $ path ){
// Set the target data warehouse case
$ Path = explode ('/', $ path );
$ Path [0] = $ this-> targetDir;
$ This-> targetPaths [$ key] = join ('/', $ path );
}
// Record the data size before the row
$ This-> sizeBeforeZip = $ this-> getSizeUnit ($ this-> getDirSize ($ this-> sourceDir), 2 );
Echo $ this-> newline. $ this-> newline;
}
}
/**
* Perform encryption.
* @ Return boolean
*/
Public function zip (){
$ This-> newline = '';
Echo '[initial encryption program] (item size:'. $ this-> sizeBeforeZip. ')'. $ this-> newline. $ this-> newline;
// Prepare the source region in the future
Foreach ($ this-> sourcefilePaths as $ key => $ path ){
If (is_file ($ path )){
// Obtain the case information
$ PathInfo = pathInfo ($ path );
Echo 'fetch source response: '. $ path. $ this-> newline;
// Obtain the content after the specified volume
Echo '=> remove blank solution ..........';
If ($ this-> strip & $ pathInfo ['extension'] = 'php '){
$ FileAterZip = php_strip_whitespace ($ path );
} Else {
$ FileAterZip = file_get_contents ($ path );
}
Echo 'finished Line'. $ this-> newline;

// Retrieve the internal volume after the upload to the destination location
$ Fp = fopen ($ this-> targetPaths [$ key], 'W + ');
Echo '=> login into destination ..........';
Fwrite ($ fp, $ fileAterZip );
Fclose ($ fp );
Echo 'finished Line'. $ this-> newline;
// If you select encryption
If ($ this-> bcompiler & $ pathInfo ['extension'] = 'php '){
Echo '=> encrypt the original token ..........';
// Original Shard
$ Fh = fopen ($ this-> targetPaths [$ key]. 'encryption. php', "w ");
Bcompiler_write_header ($ fh );
Bcompiler_write_file ($ fh, $ this-> targetPaths [$ key]);
Bcompiler_write_footer ($ fh );
Fclose ($ fh );
// Remove unencrypted original tokens

Unlink ($ this-> targetPaths [$ key]);
// Rename the encrypted case

Rename ($ this-> targetPaths [$ key]. 'encryption. php', $ this-> targetPaths [$ key]);
Echo 'finished Line'. $ this-> newline;
}
Echo $ this-> newline. $ this-> newline;
}
}
// Calculate the size of the encrypted data volume.
$ This-> sizeAfterZip = $ this-> getSizeUnit ($ this-> getDirSize ($ this-> targetDir), 2 );
Echo '[end encryption program]'. $ this-> newline. $ this-> newline;

Echo 'website advertising information'. $ this-> newline;
Echo 'source information sources: '. $ this-> sourceDir.' ('. $ this-> sizeBeforeZip.') '. $ this-> newline;
Echo 'Destination information: '. $ this-> targetDir.' ('. $ this-> sizeAfterZip.') '. $ this-> newline;
Echo 'Growth in case size: + '. $ this-> getSizeUnit ($ this-> getDirSize ($ this-> targetDir)-$ this-> getDirSize ($ this-> sourceDir ))). $ this-> newline;
Echo 'Summary: '. count ($ this-> sourcefilePaths).'. $ this-> newline;

}
/**
* Excluding all cases
*
* @ Param string $ information to be deleted by dir
* @ Param boolean $ deleteSelf: same-time data addition
* @ Return void
*/
Private function cleanDir ($ dir = '.', $ deleteSelf = true ){
If (! $ Dh = @ opendir ($ dir) return;
While ($ obj = readdir ($ dh ))){
If ($ obj = '.' | $ obj = '..') continue;
If (! @ Unlink ($ dir. '/'. $ obj) $ this-> cleanDir ($ dir. '/'. $ obj, true );
}
If ($ deleteSelf ){
Closedir ($ dh );
@ Rmdir ($ dir );
}
}
/**
* The total size of data records obtained
*
* @ Param string $ information to be analyzed by dir
* @ Return int-bit MetaBase
*/
Private function getDirSize ($ dir = '.'){
// Obtain the statement.
$ FilePaths = $ this-> getPaths ($ dir ,'*');
// Initialize the calculator
$ SizeCounter = 0;
Foreach ($ filePaths as $ key => $ path ){
$ SizeCounter = $ sizeCounter + filesize ($ path );
}
Return ($ sizeCounter );
}
/**
* Obtain all matching route entries of the resource.
*
* @ Param string $ start_dir information to be analyzed
* @ Return array route condition column
*/
Private function getPaths ($ sDir, $ sPattern, $ nFlags = NULL ){
$ SDir = escapeshellcmd ($ sDir );
$ AFiles = glob ("$ sDir/$ sPattern", $ nFlags );
Foreach (glob ("$ sDir/*", GLOB_ONLYDIR) as $ sSubDir ){
$ ASubFiles = $ this-> getPaths ($ sSubDir, $ sPattern, $ nFlags );
$ AFiles = array_merge ($ aFiles, $ aSubFiles );
}
Return $ aFiles;
}
/**
* Statement of batch case size (single digit)
*
* @ Param int refers to the case size
* @ Param int small data point
* @ Param boolean: whether to cut the data into bytes columns
* @ Return mix string or character column
*/
Public function getSizeUnit ($ size, $ decimal = 2, $ split = false ){
// Set the single-bit sequence
$ Unit = array ('bytes ', 'KB', 'mb', 'GB', 'TB', 'petab', 'EB', 'zb ', 'yb ');
// Initialize the index
$ Flag = 0;
// Perform computation
While ($ size> = 1024 ){
$ Size = $ size/1024;
$ Flag ++;
}
// Whether to separate the data value from the single digit
If ($ split ){
$ SizeUnit = array (
'Size' => number_format ($ size, $ decimal ),
'Unit '=> $ unit [$ flag]
);
} Else {
$ SizeUnit = (number_format ($ size, $ decimal). $ unit [$ flag];
}
// Response size and single bit
Return ($ sizeUnit );
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.