現(xiàn)在 手里 有好幾個(gè) 項(xiàng)目在進(jìn)行,每個(gè)項(xiàng)目都有部分通用的代碼,只想維護(hù)一個(gè) 函數(shù)庫、類庫,并且每個(gè)項(xiàng)目都不想有冗余代碼,函數(shù)功能更新后,其他項(xiàng)目的函數(shù)也需要更新。晚上抽空寫了個(gè) 簡單的打包小腳本:one.php,以后 更新函數(shù)或類時(shí),只需要在唯一的 函數(shù)庫、類庫 中更新,其他項(xiàng)目使用 打包后的 php 腳本即可(理論上也能提高PHP的運(yùn)行速度,只需要加載、分析一個(gè)文件)。
因?yàn)槲业?nbsp;函數(shù)庫、類庫都在一個(gè)目錄下,所以沒有針對相對路徑 做處理(懶),cmd 進(jìn)入 core 目錄,執(zhí)行 php one.php 即可按規(guī)則打包成一個(gè)獨(dú)立的文件,運(yùn)行效果如下。
打包流程,以 public.php 為例。
現(xiàn)在功能有限,僅支持 同一個(gè)目錄(因?yàn)槲抑挥玫搅藛文夸洠绻心奈淮笊?在此基礎(chǔ)上修改了多目錄版本,請一定要分享一分給我。
至于用處,除了 方便維護(hù)多個(gè)項(xiàng)目(A項(xiàng)目、B項(xiàng)目)或同一個(gè)項(xiàng)目的多個(gè)版本(比如:VIP版、普通版),最大的用處,可以用于商業(yè)版程序混淆加密。比如商業(yè)軟件:index.php,product.php 每個(gè)文件都打包混淆加密,每個(gè)文件都包含了所有的代碼(幾萬行)。破解者解密后,看到幾萬行代碼,上百個(gè)函數(shù)(可能都還有用),同一個(gè)功能,各個(gè)文件內(nèi)的函數(shù)名都不一致,會(huì)哭死的。。。。
測試包下載地址:
one.php 源代碼:onephp.rar
核心代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
<?php /** * 類名:One * 作者:mqycn * 博客:http://www.miaoqiyuan.cn * 源碼:http://www.miaoqiyuan.cn/p/one-php * 說明:多項(xiàng)目 函數(shù)庫、類庫 統(tǒng)一為一個(gè)版本的方法 */ class OneFile { //已經(jīng)合并的文件 public static $includes ; //處理一個(gè)文件 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 '' ; } } //代碼修復(fù) 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 多項(xiàng)目、函數(shù)庫、類庫 統(tǒng)一為一個(gè)版本的方法的文章就介紹到這了,更多相關(guān)多項(xiàng)目、函數(shù)庫、類庫統(tǒng)一為一個(gè)內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:http://www.miaoqiyuan.cn/p/one-php