現(xiàn)在 手里 有好幾個 項目在進行,每個項目都有部分通用的代碼,只想維護一個 函數(shù)庫、類庫,并且每個項目都不想有冗余代碼,函數(shù)功能更新后,其他項目的函數(shù)也需要更新。晚上抽空寫了個 簡單的打包小腳本:one.php,以后 更新函數(shù)或類時,只需要在唯一的 函數(shù)庫、類庫 中更新,其他項目使用 打包后的 php 腳本即可(理論上也能提高PHP的運行速度,只需要加載、分析一個文件)。
因為我的 函數(shù)庫、類庫都在一個目錄下,所以沒有針對相對路徑 做處理(懶),cmd 進入 core 目錄,執(zhí)行 php one.php 即可按規(guī)則打包成一個獨立的文件,運行效果如下。
至于用處,除了 方便維護多個項目(A項目、B項目)或同一個項目的多個版本(比如:VIP版、普通版),最大的用處,可以用于商業(yè)版程序混淆加密。比如商業(yè)軟件:index.php,product.php 每個文件都打包混淆加密,每個文件都包含了所有的代碼(幾萬行)。破解者解密后,看到幾萬行代碼,上百個函數(shù)(可能都還有用),同一個功能,各個文件內(nèi)的函數(shù)名都不一致,會哭死的。。。。
?php
/**
* 類名:One
* 作者:mqycn
* 博客:http://www.miaoqiyuan.cn
* 源碼:http://www.miaoqiyuan.cn/p/one-php
* 說明:多項目 函數(shù)庫、類庫 統(tǒng)一為一個版本的方法
*/
class OneFile {
//已經(jīng)合并的文件
public static $includes;
//處理一個文件
public static function run($index_file, $output_file) {
self::$includes = array();
self::log('Input', $index_file);
$output = self::parse($index_file);
file_put_contents($output_file, self::repair($output));
self::log('Output', $output_file);
}
//分析PHP文件
public static function parse($file) {
if (empty(self::$includes[$file])) {
self::log('Append', $file);
self::$includes[$file] = true;
$code = file_get_contents($file);
if (preg_match_all("/(require_once|require|include_once|include)\s+'([^']*)';/", $code, $match)) {
for ($i = 0; $i count($match[0]); $i++) {
$code = str_replace($match[0][$i], self::parse($match[2][$i]), $code);
}
}
return $code;
} else {
self::log('Ignore', $file);
return '';
}
}
//代碼修復
public static function repair($code) {
$php_prefix = "?php\r\n";
$php_suffix = "\r\n?>";
$code = str_replace("\n", "\r\n", $code);
$code = str_replace("\r\r\n", "\r\n", $code);
$code = str_replace($php_prefix, '', $code);
$code = str_replace($php_suffix, '', $code);
for ($i = 0; $i 5; $i++) {
$code = str_replace("\r\n\r\n", "\r\n", $code);
}
return $php_prefix . $code . $php_suffix;
}
//輸出日志
public static function log($type, $text, $status = '') {
if (in_array($type, array('Append', 'Ignore'))) {
$status = "- ${type}";
$type = " |-- ";
} else {
$type = "${type}:";
}
echo "${type} ${text} {$status}\r\n";
}
}
OneFile::run('vip.php', '../vip.php');
OneFile::run('public.php', '../public.php');
到此這篇關(guān)于one.php 多項目、函數(shù)庫、類庫 統(tǒng)一為一個版本的方法的文章就介紹到這了,更多相關(guān)多項目、函數(shù)庫、類庫統(tǒng)一為一個內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!