搜尋
首頁後端開發php教程用來自動壓縮js檔的php程式碼

$t1 = microtime(true); $php'; source_dir = realpath(dirname(__FILE__)."/..")."/common/script_unpacked";
  1. /* 2008 年 4 月 9 日。版本1.1
  2. *
  3. * 這是Dean Edwards JavaScript Packer 的php 版本,
  4. * 基於:
  5. *
  6. * ParseMaster,版本1.0.2 (2005-08-19) 版權所有2005Master,版本1.0.2 (2005-08-19) 版權所有2005Master,版本 1.0.2 (2005-08-19) 版權所有2005Master ,Dean Edwards
  7. * 多模式解析器。
  8. * 已知錯誤:使用escapeChar 替換時會出現錯誤
  9. * 值是一個函數
  10. *
  11. * packer,版本2.0.2 (2005-08-19) 版權所有2004-2005 , Dean Edwards
  12. *
  13. * 授權:http://creativecommons.org/licenses/LGPL/2.1/
  14. *
  15. * 由Nicolas Martin 移植到PHP。
  16. *
  17. * ------------------------------------------ ----------------------------
  18. * 變更日誌:
  19. * 1.1 : 修正一個錯誤,然後打包'/0 '解壓後變成“/”。
  20. * ---------------------------------------------- ------------ ------------------------
  21. *
  22. * 使用範例:
  23. * $myPacker = new JavaScriptPacker($script, 62 ,真,假);
  24. * $packed = $myPacker->pack();
  25. *
  26. * 或
  27. *
  28. * $ myPacker = new JavaScriptPacker($script, 'Normal', true, false);
  29. * $packed = $myPacker->pack();
  30. *
  31. * 或(預設值)
  32. *
  33. * $myPacker = new JavaScriptPacker($script);
  34. * $packed = $myPacker->pack();
  35. *
  36. *
  37. * 建構子的參數:
  38. * $ $ script:要打包的JavaScript,字串。
  39. * $encoding:編碼級別,整數或字串:
  40. * 0,10,62,95 或 'None', 'Numeric', 'Normal', 'High ASCII'。
  41. * 預設值:62。
  42. * $fastDecode:在打包結果中包含快速解碼器,布林值。
  43. * 預設值:true。
  44. * $specialChars:如果您在腳本中標記了私有和局部變數
  45. *,布林值。
  46. * 預設值: false。
  47. *
  48. * pack() 方法以字串形式傳回壓縮的 JavasScript。
  49. *
  50. * 請參閱 http://dean.edwards.name/packer/usage/ 以了解更多資訊。
  51. *
  52. * 注意:
  53. * # 需要 PHP 5 。使用 PHP 5.1.2、5.1.3、5.1.4、5.2.3 測試
  54. *
  55. * # 包裝結果可能與 Dean Edwards
  56. * 版本不同,但長度相同。原因是 PHP
  57. * 函數 usort 對陣列進行排序不一定保留
  58. * 兩個相等成員的原始順序。 Javascript 排序函數
  59. * 實際上保留了這個順序(但這不是
  60. * ECMAScript 標準所要求的)。因此,兩個結果中編碼的關鍵字順序可能
  61. *不同。
  62. *
  63. * # 如果您在檔案中使用
  64. * UTF-8,請小心使用「高ASCII」等級編碼...
  65. */
  66. class JavaScriptPacker {
  67. // 常數
  68. const IGNORE = '$1';
  69. // 驗證參數
  70. private $_script = '';
  71. 私有$_encoding = 62;
  72. 私有私有。
  73. 私有$_specialChars = false;
  74. private $LITERAL_ENCODING = array(
  75. '無' => 0,
  76. '數字' => 10,
  77. '正常' => 6🎜> '數字' => 10,
  78. '正常' => 6 222 ,
  79. '高ASCII' => 95
  80. );
  81. 公用函數__construct($_script, $_encoding = 62, $_fastDecode = true, $_specialChars = false)
  82. {
  83. {
  84. $this->_script = $_script 。 「/n」;
  85. if (array_key_exists($_encoding, $this->LITERAL_ENCODING))
  86. $_encoding = $this->LITERAL_ENCODING[$_encoding];
  87. $this->LITERAL_ENCODING[$_encoding];
  88. $this->_enco = min(this-min( int)$_encoding, 95);
  89. $this->_fastDecode = $_fastDecode;
  90. $this->_specialChars = $_specialChars;
  91. }
  92. public function pack() {
  93. $this->_addParser('_basicCompression');
  94. if ($this->_specialChars)
  95. $this->_addParser('_enencodeChars');
  96. if ($this->_encoding) if ($this->_encoding)
  97. $this->_addParser('_encodeKeywords');
  98. // 走吧!
  99. return $this->_pack($this->_script);
  100. }
  101. // 應用所有解析程式
  102. private function _pack($script) {
  103. for ( $i = 0; isset($this->_parsers[$i]) ; $i++) { $script = call_user_func(array(&$this,$this->_parsers[$i]), $script) ; } 回傳$script; }
  104. // 保留一個解析函數列表,它們會立即執行
  105. private $_parsers = array();
  106. 私有函數_addParser($parser) {
  107. $this->_parsers[ ] = $parser;
  108. }
  109. // 零編碼- 只刪除空格與註解
  110. private function _basicCompression($script) {
  111. $parser = new ParseMaster();
  112. // 確保安全性
  113. $parser->escapeChar = '//';
  114. // 保護字串
  115. $parser->add('//'[^/'//n//r] */'/', self::IGNORE);
  116. $parser->add('/"[^"//n//r]*"/', self::IGNORE);
  117. //刪除註解
  118. $parser->add ('///////[^//n//r]*[//n//r]/', ' '); $parser->add( '/////); /*[^*]*//*+([^///][^*]*//*+)*////', ' '); // 保護正規表示式
  119. $parser->add('///s+(///[^/////n//r//*][^/////n//r]*/ //g?i ?)/', '$2'); // 忽略
  120. $parser->add('/[^//w//x24////'"*)//?:] ///[ ^/////n//r//*][^/////n//r]*///g?i?/', self::IGNORE);
  121. // 刪除:;;;做某件事();
  122. if ($this->_specialChars) $parser->add('/;;;[^//n//r]+[//n/ /r]/');
  123. // 刪除多餘的分號
  124. $parser->add('///(;;//)/', self::IGNORE); // 保護for ( ;;) 迴圈
  125. $parser->add('/;+//s*([};])/', '$2');
  126. // 應用上面的
  127. $script = $ parser->exec($script);
  128. // 刪除空格
  129. $parser->add('/(//b|//x24)//s+(//b|//x24)/' , '$2 $3') ;
  130. $parser->add('/([+//-])//s+([+//-])/', '$2 $3');
  131. $ parser->add('///s+/', '');
  132. // 完成
  133. return $parser->exec($script);
  134. }
  135. 私有函數_ encodeSpecialChars($script) {
  136. $parser = new ParseMaster();
  137. // 替換:$name -> n,$$名稱-> na
  138. $parser->add('/((/ /x24+)([a-zA-Z$_]+))(//d*)/',
  139. array('fn' = > '_替換名稱')
  140. );
  141. / / 替換:_name -> _0, 雙底線(__name) 被忽略
  142. $regexp = '///b_[A-Za-z//d]//w*/';
  143. // 建構單字列表
  144. $keywords = $this->_analyze($script, $regexp, '_encodePrivate');
  145. // 快速參考
  146. $encoded = $keywords['encoded'];
  147. $parser->add($regexp,
  148. array(
  149. 'fn' => '_replace_encoded',
  150. 'data' => $encoded
  151. )
  152. );
  153. return $parser->exec($script);
  154. }
  155. private function _encodeKeywords($script) {
  156. // 轉義腳本中已有的高位元ASCII 值(即字串中)
  157. if ($this->_encoding >; 62)
  158. $script = $this->_escape95($script);
  159. // 建立解析器
  160. $parser = new ParseMaster();
  161. $encode = $this->_getEncoder($this->_encoding);
  162. // 對於高ASCII,不要編碼單一字元低ASCII
  163. $regexp = ($this->_encoding > 62) ? '/ //w//w+/' : '///w+/';
  164. // 建立單字清單
  165. $keywords = $this->_analyze($script, $regexp, $encode);
  166. $encoded = $keywords['encoded'];
  167. // 編碼
  168. $parser->add($regexp,
  169. array(
  170. 'fn' => '_replace_encoded',
  171. 'data' => $編碼
  172. )
  173. );
  174. if (emptyempty($script)) return $script;
  175. else {
  176. //$res = $parser-> exec($script);
  177. //$res = $this->_bootStrap($res, $keywords);
  178. //回傳$res;
  179. return $this->_bootStrap($parser-> exec($script), $keywords);
  180. }
  181. }
  182. private function _analyze($script, $regexp, $encode) {
  183. // 分析
  184. // 擷取腳本中的所有單字
  185. $all = array();
  186. preg_match_all($regexp, $script, $all);
  187. $_sorted = array(); // 依頻率排序的單字清單
  188. $_encoded = array(); // 單字字典->編碼
  189. $_protected = array(); // 「受保護」單字的實例
  190. $all = $all[0]; // 模擬全域匹配的javascript 行為
  191. if (!emptyempty($all)) {
  192. $unsorted = array(); // 相同的列表,未排序
  193. $protected = array(); // 「受保護」的單字(單字字典->「單字」)
  194. $value = array(); // charCode->encoding 字典(例如256->ff)
  195. $this->_count = array(); // 字數->計數
  196. $i = count($all); $ j = 0; //$word = null;
  197. // 計算出現次數- 用於稍後排序
  198. do {
  199. --$i;
  200. $word = '$' 。 $全部[$i];
  201. if (!isset($this->_count[$word])) {
  202. $this->_count[$word] = 0;
  203. $unsorted[$j ] = $word;
  204. // 為該腳本中所有受保護的單字建立一個字典
  205. // 這些單字可能會被誤認為編碼
  206. //if (is_string($encode) && method_exists( $this, $編碼))
  207. $values[$j] = call_user_func(array(&$this, $encode), $j);
  208. $protected['$' . $values[$j]] = $j++;
  209. }
  210. // 增加單字計數器
  211. $this->_count[$word]++;
  212. } while ($i > 0);
  213. // 準備對單字列表進行排序,首先我們必須保護
  214. // 也用作代碼的單字。我們為它們分配一個代碼
  215. // 相當於單字本身。
  216. // 例如如果「do」落在我們的編碼範圍內
  217. // 那麼我們儲存keywords["do"] = "do";
  218. // 這可以避免解碼時出現問題
  219. $i = count($unsorted);
  220. do {
  221. $word = $unsorted[--$i];
  222. if (isset($protected[$word]) /*!= null* /) {
  223. $_sorted[$protected[$word]] = substr($word, 1);
  224. $_protected[$protected[$word]] = true;
  225. $this->_count[ $word] = 0;
  226. }
  227. } while ($i);
  228. // 依頻率對單字排序
  229. // 注意:javascript 和php 版本的排序可能不同:
  230. // 在php 手冊中,usort :
  231. // " 如果兩個成員比較相等,
  232. // 它們在排序數組中的順序未定義。」
  233. // 因此最終打包的腳本與Dean 的javascript 版本不同
  234. // 但等效。
  235. // ECMAscript 標準不保證這種行為,
  236. // 因此並非所有瀏覽器(例如​​可以追溯到
  237. // 至少 2003 年的 Mozilla 版本)都尊重這一點。
  238. usort($unsorted, array(&$this, '_sortWords'));
  239. $j = 0;
  240. // 因為清單中有「受保護」的單字
  241. // 我們必須在它們周圍加入排序後的單字
  242. do {
  243. if (!isset($_sorted[$i]))
  244. $_sorted[$i] = substr($unsorted[$j++], 1) ;
  245. $_encoded[$_sorted[$i]] = $values[$i];
  246. } while (++$i }
  247. 傳回陣列(
  248. 'sorted' => $_sorted,
  249. 'encoded' => $_encoded,
  250. 'protected' => $_protected);
  251. }
  252. private $_count = array( );
  253. private function _sortWords($match1, $match2) {
  254. return $this->_count[$match2] - $this->_count[$match1];
  255. }
  256. // 建立用於載入和解碼的引導函數
  257. private function _bootStrap($packed, $keywords) {
  258. $ENCODE = $this->_safeRegExp('$encode/( $ count//)');
  259. // $packed:打包腳本
  260. $packed = "'" . $this->_escape($packed) 。 「'」;
  261. // $ascii:編碼基礎
  262. $ascii = min(count($keywords['sorted']), $this->_encoding);
  263. if ($ascii == 0 ) $ascii = 1;
  264. // $count:腳本中包含的單字數
  265. $count = count($keywords['sorted']);
  266. // $keywords:腳本中包含的單字列表
  267. foreach ($keywords['protected'] as $i=>$value) {
  268. $keywords['sorted'][$i ] = '';
  269. }
  270. // 從字串轉換為陣列
  271. ksort($keywords['sorted']);
  272. $keywords = "'" 。 implode('|',$keywords['已排序']) 。 "'.split('|')";
  273. $encode = ($this->_encoding > 62) ? '_encode95' : $this->_getEncoder($ascii);
  274. $encode = $this- >_getJSFunction($encode);
  275. $encode = preg_replace('/_encoding/','$ascii', $encode);
  276. $encode = preg_replace('/arguments//.callee/','$ encode', $encode);
  277. $inline = '//$count' . ($ascii > 10 ? '.toString(//$ascii)' : '');
  278. // $decode:加速解碼的代碼片段
  279. if ($this->_fastDecode) {
  280. // 創建解碼器
  281. $decode = $this->_getJSFunction('_decodeBody) ');
  282. if ($this- >_encoding > 62)
  283. $decode = preg_replace('/////w/', '[//xa1-//xff]', $decode);
  284. // 對較低的ascii 值執行內嵌編碼
  285. elseif ($ascii $decode = preg_replace($ENCODE, $inline, $decode);
  286. // 特殊情況:當$count==0 時沒有關鍵字。我想保留
  287. // 解包函數的基本形狀,所以我會修改程式碼...
  288. if ($count == 0)
  289. $decode = preg_replace($this->; _safeRegExp(' ($count)//s*=//s*1'), '$1=0', $decode, 1);
  290. }
  291. // 啟動函數
  292. $unpack = $this-> _getJSFunction('_unpack');
  293. if ($this->_fastDecode) {
  294. // 插入解碼器
  295. $this->buffer = $decode;
  296. $unpack = preg_replace_callback('/ /{/', array(&$this, '_insertFastDecode'), $unpack, 1);
  297. }
  298. $unpack = preg_replace('/"/', "'", $unpack);
  299. if ($this->_encoding > 62) { // 高位ASCII
  300. // 去掉正規表示式符合的字邊界
  301. $unpack = preg_replace('//'////////b /'/s*//+|//+/s*/' ////////b/'/', '', $unpack> }
  302. if ($ascii > 36 || $ this->_encoding > 62 || $this ->_fastDecode) {
  303. // 插入編碼函數
  304. $this->buffer = $encode;
  305. $unpack = preg_replace_callback('///{/' , array(&$this, '_insertFastEncode'), $unpack, 1);
  306. } else {
  307. // 執行內嵌編碼
  308. $unpack = preg_replace($ENCODE, $inline, $unpack) ; > // 也打包啟動函數
  309. $unpackPacker = new JavaScriptPacker($unpack, 0, false, true);
  310. $unpack = $unpackPacker->pack(); //參數
  311. $params = array($packed, $ascii, $count, $keywords);
  312. if ($this->_fastDecode) {
  313. $params[] = 0;
  314. $params[] = '{}';
  315. }
  316. $params = implode(',', $params);
  317. // 整個事情
  318. return 'eval(' . $unpack . '(' . $params . " ))/n";
  319. }
  320. 私有$buffer;
  321. private function _insertFastDecode($match) {
  322. return '{' . $this->緩衝區。 ';';
  323. }
  324. 私有函數 _insertFastEncode($match) {
  325. return '{$encode=' . $this->緩衝區。 ';';
  326. }
  327. // 嗯.. ..我需要哪一個?
  328. private function _getEncoder($ascii) {
  329. return $ascii >; 10? $ascii > 36? $ascii > 62?
  330. '_encode95' : '_encode62' : '_encode36' : '_encode10';
  331. }
  332. // 零編碼
  333. // 字元:0123456789
  334. private function _encode10($charCode) {
  335. return $charCode;
  336. }
  337. }
  338. >🎜 base36 支援
  339. // 字元:0123456789abcdefghijklmnopqrstuvwxyz
  340. private function _encode36($charCode) {
  341. return base_convert($charCode, 10, 36) // 字元:0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
  342. private function _encode62($charCode) {
  343. private function _encode62($charCode) {
  344. $private function _encode62($charCode) {
  345. $resm = charl_resm = char>==00en) { <.> $res = $this->_encode62((int)($charCode / $this->_encoding));
  346. }
  347. $charCode = $charCode % $this->_encoding;
  348. if ($charCode > 35)
  349. 回傳$res 。 chr($charCode + 29);
  350. 否則
  351. 回傳 $res 。 base_convert($charCode, 10, 36);
  352. }
  353. // 使用高位ASCII 值
  354. // 字元:?¢£¤¥|§´?a??-?ˉ°± 23′μ?·?1o?? ???àá??????èéê?ìí??D?òó???×?ùú?üYT?àáa??????èéê?ìí??e? òó???÷?ùú?üyt
  355. 私有函數_encode95($charCode) {
  356. $res = '';
  357. if ($charCode >= $this->_encoding)
  358. $res = $this->_encode95($charCode / $this->_encoding);
  359. 回傳$res 。 chr(($charCode % $this->_encoding) + 161);
  360. }
  361. private function _safeRegExp($string) {
  362. return '/'.preg_replace('//$/' , '///$', $string).'/';
  363. }
  364. 私有函數_encodePrivate($charCode) {
  365. return "_" . $字元程式碼;
  366. }
  367. // 保護解析器使用的字元
  368. private function _escape($script) {
  369. return preg_replace('/([/////'])/', '// /$1', $script);
  370. }
  371. // 保護腳本中已有的高位ASCII 字元
  372. private function _escape95($script) {
  373. return preg_replace_callback(
  374. ' /[//xa1-// xff]/',
  375. array(&$this, '_escape95Bis'),
  376. $script
  377. );
  378. }
  379. 私有函數_escape95Bis($ match) {
  380. return '/x'.((string)dechex(ord($match)));
  381. }
  382. private function _getJSFunction($aName) {
  383. if (define('self::JSFUNCTION'.$aName))
  384. return Constant('self::JSFUNCTION '.$aName);
  385. 否則
  386. 回傳'';
  387. }
  388. }
  389. // 使用的JavaScript 函數。
  390. // 注意:在 Dean 的版本中,這些函數將
  391. // 轉換為 'String(aFunctionName);'。
  392. // 這個內部轉換完成了原始程式碼,例如:
  393. // 'while (aBool) anAction();'轉換成
  394. // 'while (aBool) { anAction(); }'。
  395. // 下面的 JavaScript 函數已修正。
  396. // 解包函數- 這是引導函數
  397. // 從該打包例程中提取的資料將傳遞給
  398. // 在目標中解碼時此函數
  399. //注意! : 沒有「;」最終的。
  400. const JSFUNCTION_unpack =
  401. 'function($packed, $ascii, $count, $keywords, $encode, $decode) {
  402. while ($count--) {
  403. if ($keywords[ $count]) {
  404. $packed = $packed.replace(new RegExp(/'////b/' + $encode($count) + /'////b/', /'g/ '), $keywords[$count]);
  405. }
  406. }
  407. 回傳$packed;
  408. }';
  409. /*
  410. 'function($packed, $ascii, $ count, $keywords, $encode, $decode) {
  411. while ($count--)
  412. if ($keywords[$count ])
  413. $packed = $packed.replace(new RegExp(/' ////b/' + $encode($count) + /'////b/', /'g/'), $關鍵字[$計數]);
  414. 回傳$packed;
  415. }';
  416. */
  417. // 插入解包器的程式碼片段加速解碼
  418. const JSFUNCTION_decodeBody =
  419. //_decode = function() {
  420. // 瀏覽器是否支援String.replace
  421. // 替換值是一個函數?
  422. ' if (!/'/'.replace(/^/, String)) {
  423. // 解碼我們需要的所有值
  424. while ($count--) {
  425. $decode[ $encode($count)] = $keywords[$count] || $編碼($計數);
  426. }
  427. // 全域替換函數
  428. $keywords = [function ($encoded) {return $decode[$encoded]}];
  429. // 通用匹配
  430. $encode = function () {return / '////w+/'};
  431. // 重置循環計數器- 我們現在正在進行全域替換
  432. $count = 1;
  433. }
  434. ';
  435. //};
  436. /*
  437. ' if (!/'/'.replace(/^/, String)) {
  438. // 解碼我們需要的所有值
  439. while ($count--) $decode [$encode($count)] = $keywords[$count] || $編碼($計數);
  440. // 全域替換函數
  441. $keywords = [function ($encoded) {return $decode[$ encoded]}];
  442. // 通用匹配
  443. $encode = function () {return/'////w+/'};
  444. // 重置循環計數器- 我們現在正在進行全域替換
  445. $count = 1;
  446. }';
  447. */
  448. // 零編碼
  449. // 字元:0123456789
  450. const JSFUNCTION_encode10 =
  451. 'function($unc. charCode) {
  452. return $charCode;
  453. }';//;';
  454. // 固有的Base36 支援
  455. // 字元:0123456789abcdefghijklmnopqrstuvwklmnopqrstuvw; 'function($charCode) {
  456. return $charCode.toString(36);
  457. }';//;';
  458. //搭乘base36騎行並加上上情況alpha字符
  459. //字元:0123456789ABCDEFGHIJKLMNOPQRMNOPQRSTUVWXYZABCDEFGHIJKLMNNOPQRMNOPQRSTUVWXYZERSTUVWXYZ charCode 35 ? String. fromCharCode($charCode + 29) : $charCode.toString(36));
  460. }';
  461. // 使用高位元ASCII 值
  462. // 字元: ?¢£¤¥|§? a??-?ˉ°±23′μ?·?1o??????àá ??????èéê?ìí??D?òó???×?ùú?üYT?àáa???? ??èéê?ìí??e?òó???÷?ùú?üyt
  463. const JSFUNCTION_encode95 =
  464. 'function($charCode) {
  465. return ($charCode String.fromCharCode($charCode % _encoding + 161);
  466. }';
  467. }
  468. class ParseMaster {
  469. public $ignoreCase = false;
  470. public $escapeChar = '';
  471. // 常數
  472. const EXPRESSION = 0;
  473. const 替換= 1;
  474. const 長度= 2
  475. // 用來決定巢狀等級
  476. private $GROUPS = '///(/';//g
  477. private $SUB_REPLACE = '///$//d/';
  478. 私人$INDEXED = '/^//$//d+$/';
  479. 私有$TRIM = '/([/'"])//1//.(.*)//.// 1//1$/';
  480. 私有$ESCAPE = '////./';//g
  481. 私有$QUOTE = '//'/';
  482. 私有$DELETED = '/ //x01[^//x01]*//x01/';//g
  483. public function add($expression, $replacement = '') {
  484. // 統計子數表達式
  485. // - 增加一個,因為每個模式本身就是一個子表達式
  486. $length = 1 + preg_match_all($this->GROUPS, $this->_internalEscape((string)$expression), $ out) ;
  487. // 僅處理字串$replacement
  488. if (is_string($replacement)) {
  489. // 該模式是否處理子表達式? this->SUB_REPLACE, $replacement)) {
  490. // 一個簡單的查找? (例如“$2”)
  491. if (preg_match($this->INDEXED, $replacement)) {
  492. // 儲存索引(用於快速擷取匹配的字串)
  493. $replacement = (int ) (substr($替換, 1)) - 1;
  494. } else { // 一個複雜的查找(例如“Hello $2 $1”)
  495. // 建立一個函數來執行查找
  496. $quote = preg_match ($this->QUOTE, $this->gt ;_internalEscape($replacement))
  497. ? '"' : "'";
  498. $replacement = array(
  499. 'fn' => '_backReferences' ,
  500. 'data' => array(
  501. 'replacement' => $替換,
  502. 'length' => $length,
  503. 'quote' => $quote
  504. )
  505. );
  506. // 提交修改後的參數
  507. if (!emptyempty($expression)) $this->_add($expression, $replacement, $length);
  508. else $this->_add('/^$/', $replacement, $length);
  509. }
  510. public function exec($string) {
  511. // 執行全域替換
  512. $this->_escaped = array();
  513. // 模擬 Dean 的 _patterns.toSTring
  514. $regexp = '/';
  515. foreach ($this->_patterns as $reg) {
  516. $regexp .= '(' . substr($reg[self::EXPRESSION], 1, -1) . ')|';
  517. }
  518. $regexp = substr($regexp, 0, -1) 。
  519. $regexp .= ($this->ignoreCase) ? ‘我’:’’;
  520. $string = $this->_escape($string, $this->escapeChar);
  521. $string = preg_replace_callback(
  522. $regexp,
  523. array(
  524. &$this,
  525. '_replacement'
  526. ),
  527. $string
  528. );
  529. $string = $this->_unescape($string, $this->escapeChar);
  530. return preg_replace($this->DELETED, '', $string);
  531. }
  532. public function reset() {
  533. // 清除模式集合,方便可以重複使用該物件
  534. $this->_patterns = array();
  535. }
  536. // private
  537. private $_escaped = array(); // 轉義字元
  538. private $_patterns = array(); // 依索引儲存的模式
  539. // 建立新模式並加入模式集合
  540. private function _add() {
  541. $arguments = func_get_args();
  542. $this->_patterns[] = $arguments;
  543. }
  544. // 這是全域替換函數(相當複雜)
  545. private function _replacement($arguments) {
  546. if (emptyempty($arguments)) return '';
  547. $i = 1; $j = 0;
  548. // 循環遍歷模式
  549. while (isset($this->_patterns[$j])) {
  550. $pattern = $this->_patterns[$j++];
  551. // 我們有結果了嗎? ::替代品];
  552. if (is_array($replacement) && isset($replacement['fn'])) {
  553. if (isset($replacement['data'])) $this->; = 平面圖$替換['數據'];
  554. return call_user_func(array(&$this, $replacement['fn']), $arguments, $i);
  555. } elseif (is_int($replacement)) {
  556. return $arguments[$replacement + $i];
  557. }
  558. $delete = ($this->escapeChar == '' ||
  559. strpos($arguments[$i], $this->escapeChar) === false)
  560. ? > } else {
  561. $i += $pattern[self::LENGTH];
  562. }
  563. }
  564. }
  565. private function _backReferences($match, $offset) {
  566. $replacement = $this->buffer['replacement'];
  567. $quote = $this->buffer['quote'];
  568. $i = $this->buffer['length'];
  569. while ($i) {
  570. $replacement = str_replace('$'.$i--, $match[$offset + $i], $replacement);
  571. }
  572. 回傳 $replacement;
  573. }
  574. 導管函數 _replace_name($match, $offset){
  575. $length = strlen($match[$offset + 2]);
  576. $start = $length - max($length - strlen($match[$offset + 3]), 0);
  577. 回傳 substr($match[$offset + 1], $start, $length) 。
  578. }
  579. private function _replace_encoded($match, $offset) {
  580. return $this->buffer[$match[$offset]];
  581. }
  582. // php : 我們無法將附加資料傳遞給preg_replace_callback,
  583. // 且我們無法在create_function 中使用&$this,所以讓我們進入較低階
  584. private $ buffer;
  585. // 編碼轉義字元
  586. private function _escape($string, $escapeChar) {
  587. if ($escapeChar) {
  588. $this->buffer = $this-Char return preg_replace_callback(
  589. '///' . $escapeChar . '(.)' .'/',
  590. array(&$this, '_escapeBis'),
  591. $string
  592. ) ;
  593. } else {
  594. return $string;
  595. }
  596. }
  597. 私有函數_escapeBis($match) {
  598. $this->_escaped[] = $match [1];
  599. return $this->buffer;
  600. }
  601. // 解碼轉義字元
  602. private function _unescape($string, $escapeChar) {
  603. if ($ escapeChar) {
  604. $regexp = '/'.'/ /'.$escapeChar.'/';
  605. $this->buffer = array('escapeChar'=> $escapeChar, 'i' => 0 );
  606. return preg_replace_callback
  607. (
  608. $regexp,
  609. array(&$this, '_unescapeBis'),
  610. $string
  611. );
  612. } elsel.
  613. return $string;
  614. }
  615. }
  616. private function _unescapeBis() {
  617. if (isset($this->_escaped[$this->buffer['i']])
  618. && $this ->_escaped[$this->buffer['i']] != '')
  619. {
  620. $temp = $this->_escaped[$this->buffer['i' ] ];
  621. } else {
  622. $temp = '';
  623. }
  624. $this->buffer['i']++;
  625. 回傳$this->buffer['escapeChar '] 。 $暫時;
  626. }
  627. private function _internalEscape($string) {
  628. return preg_replace($this->ESCAPE, '', $string);
  629. }
  630. }
  631. }
  632. }
}
} } }
}

}

}

}

}
}
}
    }
  1. }
  2. }
  3. }
  4. }
  5. }
  6. }
  7. }
  8. }
  9. }
  10. }
  11. } ?>
  12. 複製程式碼
  13. 2、執行壓縮的PHP檔 pack_js_file.php
  14. 需要'tom-class.JavaScriptPacker.php';
$t1 = microtime(true); $php'; $t1 = microtime(true);
$php';

$t1 = microtime(true);

$php';

$t1 = microtime(true);

$php';
$out_dir = realpath(dirname(__FILE__)."/..")."/common/script"; $d = dir( $source_dir );
//echo "句柄: " . $d->句柄。 “/n”;
//回顯「路徑:」。 $d->路徑。 「/n」;
while (false !== ($entry = $d->read())) {
if($entry=='.') continue;
if($entry =='..') 繼續;
if($entry=='.svn') 繼續; echo $entry."/n"; $script = file_get_contents($source_dir.'/ '.$entry); $packer = new JavaScriptPacker($script, '正常', true, false); $packed = $packer->pack(); file_put_contents($out_dir.' /'.$entry, $packed); } $d->close(); $t2 = microtime(true); $time = sprintf('%.4f', ($t2 - $t1) ); echo '腳本在', $time, ' s.', "/n"; 死; ?>複製程式碼3、建立一個自動執行的bat檔。 在文件中輸入以下的內容: /usr/local/php/bin/php pack_js_file.php 總結: 只要填好相對路徑或絕對路徑,一鍵加密JS完成,那是相當的方便! !
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
您如何防止與會議有關的跨站點腳本(XSS)攻擊?您如何防止與會議有關的跨站點腳本(XSS)攻擊?Apr 23, 2025 am 12:16 AM

要保護應用免受與會話相關的XSS攻擊,需採取以下措施:1.設置HttpOnly和Secure標誌保護會話cookie。 2.對所有用戶輸入進行輸出編碼。 3.實施內容安全策略(CSP)限制腳本來源。通過這些策略,可以有效防護會話相關的XSS攻擊,確保用戶數據安全。

您如何優化PHP會話性能?您如何優化PHP會話性能?Apr 23, 2025 am 12:13 AM

优化PHP会话性能的方法包括:1.延迟会话启动,2.使用数据库存储会话,3.压缩会话数据,4.管理会话生命周期,5.实现会话共享。这些策略能显著提升应用在高并发环境下的效率。

什麼是session.gc_maxlifetime配置設置?什麼是session.gc_maxlifetime配置設置?Apr 23, 2025 am 12:10 AM

theSession.gc_maxlifetimesettinginphpdeterminesthelifespanofsessiondata,setInSeconds.1)它'sconfiguredinphp.iniorviaini_set().2)abalanceisesneededeededeedeedeededto toavoidperformance andunununununexpectedLogOgouts.3)

您如何在PHP中配置會話名?您如何在PHP中配置會話名?Apr 23, 2025 am 12:08 AM

在PHP中,可以使用session_name()函數配置會話名稱。具體步驟如下:1.使用session_name()函數設置會話名稱,例如session_name("my_session")。 2.在設置會話名稱後,調用session_start()啟動會話。配置會話名稱可以避免多應用間的會話數據衝突,並增強安全性,但需注意會話名稱的唯一性、安全性、長度和設置時機。

您應該多久再生一次會話ID?您應該多久再生一次會話ID?Apr 23, 2025 am 12:03 AM

會話ID應在登錄時、敏感操作前和每30分鐘定期重新生成。 1.登錄時重新生成會話ID可防會話固定攻擊。 2.敏感操作前重新生成提高安全性。 3.定期重新生成降低長期利用風險,但需權衡用戶體驗。

如何在PHP中設置會話cookie參數?如何在PHP中設置會話cookie參數?Apr 22, 2025 pm 05:33 PM

在PHP中設置會話cookie參數可以通過session_set_cookie_params()函數實現。 1)使用該函數設置參數,如過期時間、路徑、域名、安全標誌等;2)調用session_start()使參數生效;3)根據需求動態調整參數,如用戶登錄狀態;4)注意設置secure和httponly標誌以提升安全性。

在PHP中使用會議的主要目的是什麼?在PHP中使用會議的主要目的是什麼?Apr 22, 2025 pm 05:25 PM

在PHP中使用會話的主要目的是維護用戶在不同頁面之間的狀態。 1)會話通過session_start()函數啟動,創建唯一會話ID並存儲在用戶cookie中。 2)會話數據保存在服務器上,允許在不同請求間傳遞數據,如登錄狀態和購物車內容。

您如何在子域中分享會議?您如何在子域中分享會議?Apr 22, 2025 pm 05:21 PM

如何在子域名間共享會話?通過設置通用域名的會話cookie實現。 1.在服務器端設置會話cookie的域為.example.com。 2.選擇合適的會話存儲方式,如內存、數據庫或分佈式緩存。 3.通過cookie傳遞會話ID,服務器根據ID檢索和更新會話數據。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),