@@ -27,7 +27,7 @@ |
||
27 | 27 | |
28 | 28 | class Admin_Dashboard extends PageType { |
29 | 29 | |
30 | - // |
|
30 | + // |
|
31 | 31 | |
32 | 32 | } |
33 | 33 |
@@ -18,99 +18,99 @@ |
||
18 | 18 | |
19 | 19 | class Robots { |
20 | 20 | |
21 | - //https://developers.google.com/search/reference/robots_txt?hl=de |
|
21 | + //https://developers.google.com/search/reference/robots_txt?hl=de |
|
22 | 22 | |
23 | - protected $robots = array(); |
|
23 | + protected $robots = array(); |
|
24 | 24 | |
25 | - public function __construct () { |
|
26 | - // |
|
27 | - } |
|
25 | + public function __construct () { |
|
26 | + // |
|
27 | + } |
|
28 | 28 | |
29 | - public function loadFromDB () { |
|
30 | - $rows = DataBase::getInstance()->listRows("SELECT * FROM `{prefix}robots` WHERE `activated` = '1'; "); |
|
31 | - $this->robots = $rows; |
|
32 | - } |
|
29 | + public function loadFromDB () { |
|
30 | + $rows = DataBase::getInstance()->listRows("SELECT * FROM `{prefix}robots` WHERE `activated` = '1'; "); |
|
31 | + $this->robots = $rows; |
|
32 | + } |
|
33 | 33 | |
34 | - public function sortByUserAgent () { |
|
35 | - $array = array(); |
|
34 | + public function sortByUserAgent () { |
|
35 | + $array = array(); |
|
36 | 36 | |
37 | - foreach ($this->robots as $row) { |
|
38 | - if (!isset($row['useragent'])) { |
|
39 | - $array[$row['useragent']] = array(); |
|
40 | - } |
|
37 | + foreach ($this->robots as $row) { |
|
38 | + if (!isset($row['useragent'])) { |
|
39 | + $array[$row['useragent']] = array(); |
|
40 | + } |
|
41 | 41 | |
42 | - $array[$row['useragent']][] = $row; |
|
43 | - } |
|
42 | + $array[$row['useragent']][] = $row; |
|
43 | + } |
|
44 | 44 | |
45 | - return $array; |
|
46 | - } |
|
45 | + return $array; |
|
46 | + } |
|
47 | 47 | |
48 | - public function writeFile () { |
|
49 | - $array = $this->sortByUserAgent(); |
|
48 | + public function writeFile () { |
|
49 | + $array = $this->sortByUserAgent(); |
|
50 | 50 | |
51 | - if (!is_writable(ROOT_PATH . "robots.txt")) { |
|
52 | - throw new IllegalStateException(ROOT_PATH . "robots.txt is not writable, please correct file permissions!"); |
|
53 | - } |
|
51 | + if (!is_writable(ROOT_PATH . "robots.txt")) { |
|
52 | + throw new IllegalStateException(ROOT_PATH . "robots.txt is not writable, please correct file permissions!"); |
|
53 | + } |
|
54 | 54 | |
55 | - $handle = fopen(ROOT_PATH . "robots.txt", "w"); |
|
55 | + $handle = fopen(ROOT_PATH . "robots.txt", "w"); |
|
56 | 56 | |
57 | - foreach ($array as $useragent=>$value) { |
|
57 | + foreach ($array as $useragent=>$value) { |
|
58 | 58 | |
59 | - fwrite($handle, "User-agent: " . $useragent . "\r\n"); |
|
59 | + fwrite($handle, "User-agent: " . $useragent . "\r\n"); |
|
60 | 60 | |
61 | - foreach ($value as $line) { |
|
62 | - fwrite($handle, "" . $line['option'] . ": " . $line['value'] . "\r\n"); |
|
63 | - } |
|
61 | + foreach ($value as $line) { |
|
62 | + fwrite($handle, "" . $line['option'] . ": " . $line['value'] . "\r\n"); |
|
63 | + } |
|
64 | 64 | |
65 | - fwrite($handle, "\r\n"); |
|
65 | + fwrite($handle, "\r\n"); |
|
66 | 66 | |
67 | - } |
|
67 | + } |
|
68 | 68 | |
69 | - fclose($handle); |
|
70 | - } |
|
69 | + fclose($handle); |
|
70 | + } |
|
71 | 71 | |
72 | - public function getContent () { |
|
73 | - $array = $this->sortByUserAgent(); |
|
72 | + public function getContent () { |
|
73 | + $array = $this->sortByUserAgent(); |
|
74 | 74 | |
75 | - $buffer = ""; |
|
75 | + $buffer = ""; |
|
76 | 76 | |
77 | - foreach ($array as $useragent=>$value) { |
|
78 | - $buffer .= "User-agent: " . $useragent . "\r\n"; |
|
77 | + foreach ($array as $useragent=>$value) { |
|
78 | + $buffer .= "User-agent: " . $useragent . "\r\n"; |
|
79 | 79 | |
80 | - foreach ($value as $line) { |
|
81 | - $buffer .= "" . $line['option'] . ": " . $line['value'] . "\r\n"; |
|
82 | - } |
|
80 | + foreach ($value as $line) { |
|
81 | + $buffer .= "" . $line['option'] . ": " . $line['value'] . "\r\n"; |
|
82 | + } |
|
83 | 83 | |
84 | - $buffer .= "\r\n"; |
|
85 | - } |
|
84 | + $buffer .= "\r\n"; |
|
85 | + } |
|
86 | 86 | |
87 | - return $buffer; |
|
88 | - } |
|
87 | + return $buffer; |
|
88 | + } |
|
89 | 89 | |
90 | - public static function addRule (string $option, string $value, string $useragent = "*") { |
|
91 | - Database::getInstance()->execute("INSERT INTO `{praefix}robots` ( |
|
90 | + public static function addRule (string $option, string $value, string $useragent = "*") { |
|
91 | + Database::getInstance()->execute("INSERT INTO `{praefix}robots` ( |
|
92 | 92 | `useragent`, `option`, `value`, `activated` |
93 | 93 | ) VALUES ( |
94 | 94 | :useragent, :option, :value, '1' |
95 | 95 | ) ON DUPLICATE KEY UPDATE `option` = :option, `activated` = '1'; ", array( |
96 | - 'useragent' => $useragent, |
|
97 | - 'option' => $option, |
|
98 | - 'value' => $value |
|
99 | - )); |
|
100 | - } |
|
101 | - |
|
102 | - public static function removeRule (string $option, string $value, string $useragent = "*") { |
|
103 | - Database::getInstance()->execute("DELETE FROM `{praefix}robots` WHERE `useragent` = :useragent AND `option` = :option AND `value` = :value; ", array( |
|
104 | - 'useragent' => $useragent, |
|
105 | - 'option' => $option, |
|
106 | - 'value' => $value |
|
107 | - )); |
|
108 | - } |
|
109 | - |
|
110 | - public static function listRules () : array { |
|
111 | - $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}robots`; "); |
|
112 | - return $rows; |
|
113 | - } |
|
96 | + 'useragent' => $useragent, |
|
97 | + 'option' => $option, |
|
98 | + 'value' => $value |
|
99 | + )); |
|
100 | + } |
|
101 | + |
|
102 | + public static function removeRule (string $option, string $value, string $useragent = "*") { |
|
103 | + Database::getInstance()->execute("DELETE FROM `{praefix}robots` WHERE `useragent` = :useragent AND `option` = :option AND `value` = :value; ", array( |
|
104 | + 'useragent' => $useragent, |
|
105 | + 'option' => $option, |
|
106 | + 'value' => $value |
|
107 | + )); |
|
108 | + } |
|
109 | + |
|
110 | + public static function listRules () : array { |
|
111 | + $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}robots`; "); |
|
112 | + return $rows; |
|
113 | + } |
|
114 | 114 | |
115 | 115 | } |
116 | 116 |
@@ -22,16 +22,16 @@ discard block |
||
22 | 22 | |
23 | 23 | protected $robots = array(); |
24 | 24 | |
25 | - public function __construct () { |
|
25 | + public function __construct() { |
|
26 | 26 | // |
27 | 27 | } |
28 | 28 | |
29 | - public function loadFromDB () { |
|
29 | + public function loadFromDB() { |
|
30 | 30 | $rows = DataBase::getInstance()->listRows("SELECT * FROM `{prefix}robots` WHERE `activated` = '1'; "); |
31 | 31 | $this->robots = $rows; |
32 | 32 | } |
33 | 33 | |
34 | - public function sortByUserAgent () { |
|
34 | + public function sortByUserAgent() { |
|
35 | 35 | $array = array(); |
36 | 36 | |
37 | 37 | foreach ($this->robots as $row) { |
@@ -45,7 +45,7 @@ discard block |
||
45 | 45 | return $array; |
46 | 46 | } |
47 | 47 | |
48 | - public function writeFile () { |
|
48 | + public function writeFile() { |
|
49 | 49 | $array = $this->sortByUserAgent(); |
50 | 50 | |
51 | 51 | if (!is_writable(ROOT_PATH . "robots.txt")) { |
@@ -69,7 +69,7 @@ discard block |
||
69 | 69 | fclose($handle); |
70 | 70 | } |
71 | 71 | |
72 | - public function getContent () { |
|
72 | + public function getContent() { |
|
73 | 73 | $array = $this->sortByUserAgent(); |
74 | 74 | |
75 | 75 | $buffer = ""; |
@@ -87,7 +87,7 @@ discard block |
||
87 | 87 | return $buffer; |
88 | 88 | } |
89 | 89 | |
90 | - public static function addRule (string $option, string $value, string $useragent = "*") { |
|
90 | + public static function addRule(string $option, string $value, string $useragent = "*") { |
|
91 | 91 | Database::getInstance()->execute("INSERT INTO `{praefix}robots` ( |
92 | 92 | `useragent`, `option`, `value`, `activated` |
93 | 93 | ) VALUES ( |
@@ -99,7 +99,7 @@ discard block |
||
99 | 99 | )); |
100 | 100 | } |
101 | 101 | |
102 | - public static function removeRule (string $option, string $value, string $useragent = "*") { |
|
102 | + public static function removeRule(string $option, string $value, string $useragent = "*") { |
|
103 | 103 | Database::getInstance()->execute("DELETE FROM `{praefix}robots` WHERE `useragent` = :useragent AND `option` = :option AND `value` = :value; ", array( |
104 | 104 | 'useragent' => $useragent, |
105 | 105 | 'option' => $option, |
@@ -107,7 +107,7 @@ discard block |
||
107 | 107 | )); |
108 | 108 | } |
109 | 109 | |
110 | - public static function listRules () : array { |
|
110 | + public static function listRules() : array { |
|
111 | 111 | $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}robots`; "); |
112 | 112 | return $rows; |
113 | 113 | } |
@@ -27,27 +27,27 @@ |
||
27 | 27 | |
28 | 28 | class RobotsPage extends PageType { |
29 | 29 | |
30 | - public function getContentType(): string { |
|
31 | - return "text/plain"; |
|
32 | - } |
|
33 | - |
|
34 | - public function showDesign() { |
|
35 | - return false; |
|
36 | - } |
|
37 | - |
|
38 | - public function exitAfterOutput() { |
|
39 | - //dont add additional text (like HTML comments for benchmarks) at end of this content |
|
40 | - return true; |
|
41 | - } |
|
42 | - |
|
43 | - public function getContent(): string { |
|
44 | - //load robots.txt entries from database |
|
45 | - $robots = new Robots(); |
|
46 | - $robots->loadFromDB(); |
|
47 | - |
|
48 | - //get robots.txt content |
|
49 | - return $robots->getContent(); |
|
50 | - } |
|
30 | + public function getContentType(): string { |
|
31 | + return "text/plain"; |
|
32 | + } |
|
33 | + |
|
34 | + public function showDesign() { |
|
35 | + return false; |
|
36 | + } |
|
37 | + |
|
38 | + public function exitAfterOutput() { |
|
39 | + //dont add additional text (like HTML comments for benchmarks) at end of this content |
|
40 | + return true; |
|
41 | + } |
|
42 | + |
|
43 | + public function getContent(): string { |
|
44 | + //load robots.txt entries from database |
|
45 | + $robots = new Robots(); |
|
46 | + $robots->loadFromDB(); |
|
47 | + |
|
48 | + //get robots.txt content |
|
49 | + return $robots->getContent(); |
|
50 | + } |
|
51 | 51 | |
52 | 52 | } |
53 | 53 |
@@ -27,102 +27,102 @@ |
||
27 | 27 | |
28 | 28 | class UserRights { |
29 | 29 | |
30 | - protected $userID = -1; |
|
31 | - |
|
32 | - //array with permissions - value: 0 (no), 1 (yes), 2 (never) |
|
33 | - protected $permissions = array(); |
|
34 | - |
|
35 | - public function __construct(int $userID) { |
|
36 | - $this->userID = $userID; |
|
37 | - |
|
38 | - //load permissions |
|
39 | - $this->load($userID); |
|
40 | - } |
|
41 | - |
|
42 | - protected function load (int $userID) { |
|
43 | - if (Cache::contains("user_permissions", "user_" . $userID)) { |
|
44 | - $this->permissions = Cache::get("user_permissions", "user_" . $userID); |
|
45 | - } else { |
|
46 | - $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}user_rights` WHERE `userID` = :userID; ", array( |
|
47 | - 'userID' => array( |
|
48 | - 'type' => PDO::PARAM_INT, |
|
49 | - 'value' => $userID |
|
50 | - ) |
|
51 | - )); |
|
52 | - |
|
53 | - $this->permissions = array(); |
|
54 | - |
|
55 | - foreach ($rows as $row) { |
|
56 | - $this->permissions[$row['token']] = $row['value']; |
|
57 | - } |
|
58 | - |
|
59 | - //cache result |
|
60 | - Cache::put("user_permissions", "user_user_" . $userID, $this->permissions); |
|
61 | - } |
|
62 | - } |
|
63 | - |
|
64 | - public function setRight (string $token, int $value = 1) { |
|
65 | - //validate token |
|
66 | - $token = Validator_Token::get($token); |
|
67 | - |
|
68 | - if ($value < 0 || $value > 2) { |
|
69 | - throw new IllegalArgumentException("token value '" . $value . "' is not allowed, value has to be >= 0 and <= 2."); |
|
70 | - } |
|
71 | - |
|
72 | - //update database |
|
73 | - Database::getInstance()->execute("INSERT INTO `{praefix}user_rights` ( |
|
30 | + protected $userID = -1; |
|
31 | + |
|
32 | + //array with permissions - value: 0 (no), 1 (yes), 2 (never) |
|
33 | + protected $permissions = array(); |
|
34 | + |
|
35 | + public function __construct(int $userID) { |
|
36 | + $this->userID = $userID; |
|
37 | + |
|
38 | + //load permissions |
|
39 | + $this->load($userID); |
|
40 | + } |
|
41 | + |
|
42 | + protected function load (int $userID) { |
|
43 | + if (Cache::contains("user_permissions", "user_" . $userID)) { |
|
44 | + $this->permissions = Cache::get("user_permissions", "user_" . $userID); |
|
45 | + } else { |
|
46 | + $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}user_rights` WHERE `userID` = :userID; ", array( |
|
47 | + 'userID' => array( |
|
48 | + 'type' => PDO::PARAM_INT, |
|
49 | + 'value' => $userID |
|
50 | + ) |
|
51 | + )); |
|
52 | + |
|
53 | + $this->permissions = array(); |
|
54 | + |
|
55 | + foreach ($rows as $row) { |
|
56 | + $this->permissions[$row['token']] = $row['value']; |
|
57 | + } |
|
58 | + |
|
59 | + //cache result |
|
60 | + Cache::put("user_permissions", "user_user_" . $userID, $this->permissions); |
|
61 | + } |
|
62 | + } |
|
63 | + |
|
64 | + public function setRight (string $token, int $value = 1) { |
|
65 | + //validate token |
|
66 | + $token = Validator_Token::get($token); |
|
67 | + |
|
68 | + if ($value < 0 || $value > 2) { |
|
69 | + throw new IllegalArgumentException("token value '" . $value . "' is not allowed, value has to be >= 0 and <= 2."); |
|
70 | + } |
|
71 | + |
|
72 | + //update database |
|
73 | + Database::getInstance()->execute("INSERT INTO `{praefix}user_rights` ( |
|
74 | 74 | `userID`, `token`, `value` |
75 | 75 | ) VALUES ( |
76 | 76 | :userID, :token, :value |
77 | 77 | ) ON DUPLICATE KEY UPDATE `value` = :value; ", array( |
78 | - 'userID' => array( |
|
79 | - 'type' => PDO::PARAM_INT, |
|
80 | - 'value' => $this->userID |
|
81 | - ), |
|
82 | - 'token' => $token, |
|
83 | - 'value' => array( |
|
84 | - 'type' => PDO::PARAM_INT, |
|
85 | - 'value' => $value |
|
86 | - ) |
|
87 | - )); |
|
88 | - |
|
89 | - $this->permissions[$token] = $value; |
|
90 | - |
|
91 | - //clear cache |
|
92 | - Cache::clear("user_permissions", "user_" . $this->userID); |
|
93 | - Cache::clear("permissions", "permissions_user_" . $this->userID); |
|
94 | - } |
|
95 | - |
|
96 | - public function removeRight (string $token) { |
|
97 | - //validate token |
|
98 | - $token = Validator_Token::get($token); |
|
99 | - |
|
100 | - //delete from database |
|
101 | - Database::getInstance()->execute("DELETE FROM `{praefix}user_rights` WHERE `userID` = :userID AND `token` = :token; ", array( |
|
102 | - 'userID' => array( |
|
103 | - 'type' => PDO::PARAM_INT, |
|
104 | - 'value' => $this->userID |
|
105 | - ), |
|
106 | - 'token' => $token |
|
107 | - )); |
|
108 | - |
|
109 | - //delete from in-memory cache |
|
110 | - if (isset($this->permissions[$token])) { |
|
111 | - unset($this->permissions[$token]); |
|
112 | - } |
|
113 | - |
|
114 | - //clear cache |
|
115 | - Cache::clear("user_permissions", "user_" . $this->userID); |
|
116 | - Cache::clear("permissions", "permissions_user_" . $this->userID); |
|
117 | - } |
|
118 | - |
|
119 | - public function listRights () : array { |
|
120 | - return $this->permissions; |
|
121 | - } |
|
122 | - |
|
123 | - public function hasRight (string $token) { |
|
124 | - return isset($this->permissions[$token]) && $this->permissions[$token] == 1; |
|
125 | - } |
|
78 | + 'userID' => array( |
|
79 | + 'type' => PDO::PARAM_INT, |
|
80 | + 'value' => $this->userID |
|
81 | + ), |
|
82 | + 'token' => $token, |
|
83 | + 'value' => array( |
|
84 | + 'type' => PDO::PARAM_INT, |
|
85 | + 'value' => $value |
|
86 | + ) |
|
87 | + )); |
|
88 | + |
|
89 | + $this->permissions[$token] = $value; |
|
90 | + |
|
91 | + //clear cache |
|
92 | + Cache::clear("user_permissions", "user_" . $this->userID); |
|
93 | + Cache::clear("permissions", "permissions_user_" . $this->userID); |
|
94 | + } |
|
95 | + |
|
96 | + public function removeRight (string $token) { |
|
97 | + //validate token |
|
98 | + $token = Validator_Token::get($token); |
|
99 | + |
|
100 | + //delete from database |
|
101 | + Database::getInstance()->execute("DELETE FROM `{praefix}user_rights` WHERE `userID` = :userID AND `token` = :token; ", array( |
|
102 | + 'userID' => array( |
|
103 | + 'type' => PDO::PARAM_INT, |
|
104 | + 'value' => $this->userID |
|
105 | + ), |
|
106 | + 'token' => $token |
|
107 | + )); |
|
108 | + |
|
109 | + //delete from in-memory cache |
|
110 | + if (isset($this->permissions[$token])) { |
|
111 | + unset($this->permissions[$token]); |
|
112 | + } |
|
113 | + |
|
114 | + //clear cache |
|
115 | + Cache::clear("user_permissions", "user_" . $this->userID); |
|
116 | + Cache::clear("permissions", "permissions_user_" . $this->userID); |
|
117 | + } |
|
118 | + |
|
119 | + public function listRights () : array { |
|
120 | + return $this->permissions; |
|
121 | + } |
|
122 | + |
|
123 | + public function hasRight (string $token) { |
|
124 | + return isset($this->permissions[$token]) && $this->permissions[$token] == 1; |
|
125 | + } |
|
126 | 126 | |
127 | 127 | } |
128 | 128 |
@@ -39,7 +39,7 @@ discard block |
||
39 | 39 | $this->load($userID); |
40 | 40 | } |
41 | 41 | |
42 | - protected function load (int $userID) { |
|
42 | + protected function load(int $userID) { |
|
43 | 43 | if (Cache::contains("user_permissions", "user_" . $userID)) { |
44 | 44 | $this->permissions = Cache::get("user_permissions", "user_" . $userID); |
45 | 45 | } else { |
@@ -61,7 +61,7 @@ discard block |
||
61 | 61 | } |
62 | 62 | } |
63 | 63 | |
64 | - public function setRight (string $token, int $value = 1) { |
|
64 | + public function setRight(string $token, int $value = 1) { |
|
65 | 65 | //validate token |
66 | 66 | $token = Validator_Token::get($token); |
67 | 67 | |
@@ -93,7 +93,7 @@ discard block |
||
93 | 93 | Cache::clear("permissions", "permissions_user_" . $this->userID); |
94 | 94 | } |
95 | 95 | |
96 | - public function removeRight (string $token) { |
|
96 | + public function removeRight(string $token) { |
|
97 | 97 | //validate token |
98 | 98 | $token = Validator_Token::get($token); |
99 | 99 | |
@@ -116,11 +116,11 @@ discard block |
||
116 | 116 | Cache::clear("permissions", "permissions_user_" . $this->userID); |
117 | 117 | } |
118 | 118 | |
119 | - public function listRights () : array { |
|
119 | + public function listRights() : array { |
|
120 | 120 | return $this->permissions; |
121 | 121 | } |
122 | 122 | |
123 | - public function hasRight (string $token) { |
|
123 | + public function hasRight(string $token) { |
|
124 | 124 | return isset($this->permissions[$token]) && $this->permissions[$token] == 1; |
125 | 125 | } |
126 | 126 |
@@ -27,102 +27,102 @@ |
||
27 | 27 | |
28 | 28 | class GroupRights { |
29 | 29 | |
30 | - protected $groupID = -1; |
|
31 | - |
|
32 | - //array with permissions - value: 0 (no), 1 (yes), 2 (never) |
|
33 | - protected $permissions = array(); |
|
34 | - |
|
35 | - public function __construct(int $groupID) { |
|
36 | - $this->groupID = $groupID; |
|
37 | - |
|
38 | - //load permissions |
|
39 | - $this->load($groupID); |
|
40 | - } |
|
41 | - |
|
42 | - protected function load (int $groupID) { |
|
43 | - if (Cache::contains("group_permissions", "group_" . $groupID)) { |
|
44 | - $this->permissions = Cache::get("group_permissions", "group_" . $groupID); |
|
45 | - } else { |
|
46 | - $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}group_rights` WHERE `groupID` = :groupID; ", array( |
|
47 | - 'groupID' => array( |
|
48 | - 'type' => PDO::PARAM_INT, |
|
49 | - 'value' => $groupID |
|
50 | - ) |
|
51 | - )); |
|
52 | - |
|
53 | - $this->permissions = array(); |
|
54 | - |
|
55 | - foreach ($rows as $row) { |
|
56 | - $this->permissions[$row['token']] = $row['value']; |
|
57 | - } |
|
58 | - |
|
59 | - //cache result |
|
60 | - Cache::put("group_permissions", "group_" . $groupID, $this->permissions); |
|
61 | - } |
|
62 | - } |
|
63 | - |
|
64 | - public function setRight (string $token, int $value = 1) { |
|
65 | - //validate token |
|
66 | - $token = Validator_Token::get($token); |
|
67 | - |
|
68 | - if ($value < 0 || $value > 2) { |
|
69 | - throw new IllegalArgumentException("token value '" . $value . "' is not allowed, value has to be >= 0 and <= 2."); |
|
70 | - } |
|
71 | - |
|
72 | - //update database |
|
73 | - Database::getInstance()->execute("INSERT INTO `{praefix}group_rights` ( |
|
30 | + protected $groupID = -1; |
|
31 | + |
|
32 | + //array with permissions - value: 0 (no), 1 (yes), 2 (never) |
|
33 | + protected $permissions = array(); |
|
34 | + |
|
35 | + public function __construct(int $groupID) { |
|
36 | + $this->groupID = $groupID; |
|
37 | + |
|
38 | + //load permissions |
|
39 | + $this->load($groupID); |
|
40 | + } |
|
41 | + |
|
42 | + protected function load (int $groupID) { |
|
43 | + if (Cache::contains("group_permissions", "group_" . $groupID)) { |
|
44 | + $this->permissions = Cache::get("group_permissions", "group_" . $groupID); |
|
45 | + } else { |
|
46 | + $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}group_rights` WHERE `groupID` = :groupID; ", array( |
|
47 | + 'groupID' => array( |
|
48 | + 'type' => PDO::PARAM_INT, |
|
49 | + 'value' => $groupID |
|
50 | + ) |
|
51 | + )); |
|
52 | + |
|
53 | + $this->permissions = array(); |
|
54 | + |
|
55 | + foreach ($rows as $row) { |
|
56 | + $this->permissions[$row['token']] = $row['value']; |
|
57 | + } |
|
58 | + |
|
59 | + //cache result |
|
60 | + Cache::put("group_permissions", "group_" . $groupID, $this->permissions); |
|
61 | + } |
|
62 | + } |
|
63 | + |
|
64 | + public function setRight (string $token, int $value = 1) { |
|
65 | + //validate token |
|
66 | + $token = Validator_Token::get($token); |
|
67 | + |
|
68 | + if ($value < 0 || $value > 2) { |
|
69 | + throw new IllegalArgumentException("token value '" . $value . "' is not allowed, value has to be >= 0 and <= 2."); |
|
70 | + } |
|
71 | + |
|
72 | + //update database |
|
73 | + Database::getInstance()->execute("INSERT INTO `{praefix}group_rights` ( |
|
74 | 74 | `groupID`, `token`, `value` |
75 | 75 | ) VALUES ( |
76 | 76 | :groupID, :token, :value |
77 | 77 | ) ON DUPLICATE KEY UPDATE `value` = :value; ", array( |
78 | - 'groupID' => array( |
|
79 | - 'type' => PDO::PARAM_INT, |
|
80 | - 'value' => $this->groupID |
|
81 | - ), |
|
82 | - 'token' => $token, |
|
83 | - 'value' => array( |
|
84 | - 'type' => PDO::PARAM_INT, |
|
85 | - 'value' => $value |
|
86 | - ) |
|
87 | - )); |
|
88 | - |
|
89 | - $this->permissions[$token] = $value; |
|
90 | - |
|
91 | - //clear cache |
|
92 | - Cache::clear("group_permissions", "group_" . $this->groupID); |
|
93 | - Cache::clear("permissions"); |
|
94 | - } |
|
95 | - |
|
96 | - public function removeRight (string $token) { |
|
97 | - //validate token |
|
98 | - $token = Validator_Token::get($token); |
|
99 | - |
|
100 | - //delete from database |
|
101 | - Database::getInstance()->execute("DELETE FROM `{praefix}group_rights` WHERE `groupID` = :groupID AND `token` = :token; ", array( |
|
102 | - 'groupID' => array( |
|
103 | - 'type' => PDO::PARAM_INT, |
|
104 | - 'value' => $this->groupID |
|
105 | - ), |
|
106 | - 'token' => $token |
|
107 | - )); |
|
108 | - |
|
109 | - //delete from in-memory cache |
|
110 | - if (isset($this->permissions[$token])) { |
|
111 | - unset($this->permissions[$token]); |
|
112 | - } |
|
113 | - |
|
114 | - //clear cache |
|
115 | - Cache::clear("group_permissions", "group_" . $this->groupID); |
|
116 | - Cache::clear("permissions"); |
|
117 | - } |
|
118 | - |
|
119 | - public function listRights () : array { |
|
120 | - return $this->permissions; |
|
121 | - } |
|
122 | - |
|
123 | - public function hasRight (string $token) { |
|
124 | - return isset($this->permissions[$token]) && $this->permissions[$token] == 1; |
|
125 | - } |
|
78 | + 'groupID' => array( |
|
79 | + 'type' => PDO::PARAM_INT, |
|
80 | + 'value' => $this->groupID |
|
81 | + ), |
|
82 | + 'token' => $token, |
|
83 | + 'value' => array( |
|
84 | + 'type' => PDO::PARAM_INT, |
|
85 | + 'value' => $value |
|
86 | + ) |
|
87 | + )); |
|
88 | + |
|
89 | + $this->permissions[$token] = $value; |
|
90 | + |
|
91 | + //clear cache |
|
92 | + Cache::clear("group_permissions", "group_" . $this->groupID); |
|
93 | + Cache::clear("permissions"); |
|
94 | + } |
|
95 | + |
|
96 | + public function removeRight (string $token) { |
|
97 | + //validate token |
|
98 | + $token = Validator_Token::get($token); |
|
99 | + |
|
100 | + //delete from database |
|
101 | + Database::getInstance()->execute("DELETE FROM `{praefix}group_rights` WHERE `groupID` = :groupID AND `token` = :token; ", array( |
|
102 | + 'groupID' => array( |
|
103 | + 'type' => PDO::PARAM_INT, |
|
104 | + 'value' => $this->groupID |
|
105 | + ), |
|
106 | + 'token' => $token |
|
107 | + )); |
|
108 | + |
|
109 | + //delete from in-memory cache |
|
110 | + if (isset($this->permissions[$token])) { |
|
111 | + unset($this->permissions[$token]); |
|
112 | + } |
|
113 | + |
|
114 | + //clear cache |
|
115 | + Cache::clear("group_permissions", "group_" . $this->groupID); |
|
116 | + Cache::clear("permissions"); |
|
117 | + } |
|
118 | + |
|
119 | + public function listRights () : array { |
|
120 | + return $this->permissions; |
|
121 | + } |
|
122 | + |
|
123 | + public function hasRight (string $token) { |
|
124 | + return isset($this->permissions[$token]) && $this->permissions[$token] == 1; |
|
125 | + } |
|
126 | 126 | |
127 | 127 | } |
128 | 128 |
@@ -39,7 +39,7 @@ discard block |
||
39 | 39 | $this->load($groupID); |
40 | 40 | } |
41 | 41 | |
42 | - protected function load (int $groupID) { |
|
42 | + protected function load(int $groupID) { |
|
43 | 43 | if (Cache::contains("group_permissions", "group_" . $groupID)) { |
44 | 44 | $this->permissions = Cache::get("group_permissions", "group_" . $groupID); |
45 | 45 | } else { |
@@ -61,7 +61,7 @@ discard block |
||
61 | 61 | } |
62 | 62 | } |
63 | 63 | |
64 | - public function setRight (string $token, int $value = 1) { |
|
64 | + public function setRight(string $token, int $value = 1) { |
|
65 | 65 | //validate token |
66 | 66 | $token = Validator_Token::get($token); |
67 | 67 | |
@@ -93,7 +93,7 @@ discard block |
||
93 | 93 | Cache::clear("permissions"); |
94 | 94 | } |
95 | 95 | |
96 | - public function removeRight (string $token) { |
|
96 | + public function removeRight(string $token) { |
|
97 | 97 | //validate token |
98 | 98 | $token = Validator_Token::get($token); |
99 | 99 | |
@@ -116,11 +116,11 @@ discard block |
||
116 | 116 | Cache::clear("permissions"); |
117 | 117 | } |
118 | 118 | |
119 | - public function listRights () : array { |
|
119 | + public function listRights() : array { |
|
120 | 120 | return $this->permissions; |
121 | 121 | } |
122 | 122 | |
123 | - public function hasRight (string $token) { |
|
123 | + public function hasRight(string $token) { |
|
124 | 124 | return isset($this->permissions[$token]) && $this->permissions[$token] == 1; |
125 | 125 | } |
126 | 126 |
@@ -57,1006 +57,1006 @@ |
||
57 | 57 | |
58 | 58 | class JSqueeze |
59 | 59 | { |
60 | - const |
|
61 | - |
|
62 | - SPECIAL_VAR_PACKER = '(\$+[a-zA-Z_]|_[a-zA-Z0-9$])[a-zA-Z0-9_$]*'; |
|
63 | - |
|
64 | - public |
|
65 | - |
|
66 | - $charFreq; |
|
67 | - |
|
68 | - protected |
|
69 | - |
|
70 | - $strings, |
|
71 | - $closures, |
|
72 | - $str0, |
|
73 | - $str1, |
|
74 | - $argFreq, |
|
75 | - $specialVarRx, |
|
76 | - $keepImportantComments, |
|
77 | - |
|
78 | - $varRx = '(?:[a-zA-Z_$])[a-zA-Z0-9_$]*', |
|
79 | - $reserved = array( |
|
80 | - // Literals |
|
81 | - 'true','false','null', |
|
82 | - // ES6 |
|
83 | - 'break','case','class','catch','const','continue','debugger','default','delete','do','else','export','extends','finally','for','function','if','import','in','instanceof','new','return','super','switch','this','throw','try','typeof','var','void','while','with','yield', |
|
84 | - // Future |
|
85 | - 'enum', |
|
86 | - // Strict mode |
|
87 | - 'implements','package','protected','static','let','interface','private','public', |
|
88 | - // Module |
|
89 | - 'await', |
|
90 | - // Older standards |
|
91 | - 'abstract','boolean','byte','char','double','final','float','goto','int','long','native','short','synchronized','throws','transient','volatile', |
|
92 | - ); |
|
93 | - |
|
94 | - public function __construct() |
|
95 | - { |
|
96 | - $this->reserved = array_flip($this->reserved); |
|
97 | - $this->charFreq = array_fill(0, 256, 0); |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * Squeezes a JavaScript source code. |
|
102 | - * |
|
103 | - * Set $singleLine to false if you want optional |
|
104 | - * semi-colons to be replaced by line feeds. |
|
105 | - * |
|
106 | - * Set $keepImportantComments to false if you want /*! comments to be removed. |
|
107 | - * |
|
108 | - * $specialVarRx defines the regular expression of special variables names |
|
109 | - * for global vars, methods, properties and in string substitution. |
|
110 | - * Set it to false if you don't want any. |
|
111 | - * |
|
112 | - * If the analysed javascript source contains a single line comment like |
|
113 | - * this one, then the directive will overwrite $specialVarRx: |
|
114 | - * |
|
115 | - * // jsqueeze.specialVarRx = your_special_var_regexp_here |
|
116 | - * |
|
117 | - * Only the first directive is parsed, others are ignored. It is not possible |
|
118 | - * to redefine $specialVarRx in the middle of the javascript source. |
|
119 | - * |
|
120 | - * Example: |
|
121 | - * $parser = new JSqueeze; |
|
122 | - * $squeezed_js = $parser->squeeze($fat_js); |
|
123 | - */ |
|
124 | - public function squeeze($code, $singleLine = true, $keepImportantComments = true, $specialVarRx = false) |
|
125 | - { |
|
126 | - $code = trim($code); |
|
127 | - if ('' === $code) { |
|
128 | - return ''; |
|
129 | - } |
|
130 | - |
|
131 | - $this->argFreq = array(-1 => 0); |
|
132 | - $this->specialVarRx = $specialVarRx; |
|
133 | - $this->keepImportantComments = !!$keepImportantComments; |
|
134 | - |
|
135 | - if (preg_match("#//[ \t]*jsqueeze\.specialVarRx[ \t]*=[ \t]*([\"']?)(.*)\\1#i", $code, $key)) { |
|
136 | - if (!$key[1]) { |
|
137 | - $key[2] = trim($key[2]); |
|
138 | - $key[1] = strtolower($key[2]); |
|
139 | - $key[1] = $key[1] && $key[1] != 'false' && $key[1] != 'none' && $key[1] != 'off'; |
|
140 | - } |
|
141 | - |
|
142 | - $this->specialVarRx = $key[1] ? $key[2] : false; |
|
143 | - } |
|
144 | - |
|
145 | - // Remove capturing parentheses |
|
146 | - $this->specialVarRx && $this->specialVarRx = preg_replace('/(?<!\\\\)((?:\\\\\\\\)*)\((?!\?)/', '(?:', $this->specialVarRx); |
|
147 | - |
|
148 | - false !== strpos($code, "\r") && $code = strtr(str_replace("\r\n", "\n", $code), "\r", "\n"); |
|
149 | - false !== strpos($code, "\xC2\x85") && $code = str_replace("\xC2\x85", "\n", $code); // Next Line |
|
150 | - false !== strpos($code, "\xE2\x80\xA8") && $code = str_replace("\xE2\x80\xA8", "\n", $code); // Line Separator |
|
151 | - false !== strpos($code, "\xE2\x80\xA9") && $code = str_replace("\xE2\x80\xA9", "\n", $code); // Paragraph Separator |
|
152 | - |
|
153 | - list($code, $this->strings) = $this->extractStrings($code); |
|
154 | - list($code, $this->closures) = $this->extractClosures($code); |
|
155 | - |
|
156 | - $key = "//''\"\"#0'"; // This crap has a wonderful property: it can not happen in any valid javascript, even in strings |
|
157 | - $this->closures[$key] = &$code; |
|
158 | - |
|
159 | - $tree = array($key => array('parent' => false)); |
|
160 | - $this->makeVars($code, $tree[$key], $key); |
|
161 | - $this->renameVars($tree[$key], true); |
|
162 | - |
|
163 | - $code = substr($tree[$key]['code'], 1); |
|
164 | - $code = preg_replace("'\breturn !'", 'return!', $code); |
|
165 | - $code = preg_replace("'\}(?=(else|while)[^\$.a-zA-Z0-9_])'", "}\r", $code); |
|
166 | - $code = str_replace(array_keys($this->strings), array_values($this->strings), $code); |
|
167 | - |
|
168 | - if ($singleLine) { |
|
169 | - $code = strtr($code, "\n", ';'); |
|
170 | - } else { |
|
171 | - $code = str_replace("\n", ";\n", $code); |
|
172 | - } |
|
173 | - false !== strpos($code, "\r") && $code = strtr(trim($code), "\r", "\n"); |
|
174 | - |
|
175 | - // Cleanup memory |
|
176 | - $this->charFreq = array_fill(0, 256, 0); |
|
177 | - $this->strings = $this->closures = $this->argFreq = array(); |
|
178 | - $this->str0 = $this->str1 = ''; |
|
179 | - |
|
180 | - return $code; |
|
181 | - } |
|
182 | - |
|
183 | - protected function extractStrings($f) |
|
184 | - { |
|
185 | - if ($cc_on = false !== strpos($f, '@cc_on')) { |
|
186 | - // Protect conditional comments from being removed |
|
187 | - $f = str_replace('#', '##', $f); |
|
188 | - $f = str_replace('/*@', '1#@', $f); |
|
189 | - $f = preg_replace("'//@([^\n]+)'", '2#@$1@#3', $f); |
|
190 | - $f = str_replace('@*/', '@#1', $f); |
|
191 | - } |
|
192 | - |
|
193 | - $len = strlen($f); |
|
194 | - $code = str_repeat(' ', $len); |
|
195 | - $j = 0; |
|
196 | - |
|
197 | - $strings = array(); |
|
198 | - $K = 0; |
|
199 | - |
|
200 | - $instr = false; |
|
201 | - |
|
202 | - $q = array( |
|
203 | - "'", '"', |
|
204 | - "'" => 0, |
|
205 | - '"' => 0, |
|
206 | - ); |
|
207 | - |
|
208 | - // Extract strings, removes comments |
|
209 | - for ($i = 0; $i < $len; ++$i) { |
|
210 | - if ($instr) { |
|
211 | - if ('//' == $instr) { |
|
212 | - if ("\n" == $f[$i]) { |
|
213 | - $f[$i--] = ' '; |
|
214 | - $instr = false; |
|
215 | - } |
|
216 | - } elseif ($f[$i] == $instr || ('/' == $f[$i] && "/'" == $instr)) { |
|
217 | - if ('!' == $instr) { |
|
218 | - } elseif ('*' == $instr) { |
|
219 | - if ('/' == $f[$i + 1]) { |
|
220 | - ++$i; |
|
221 | - $instr = false; |
|
222 | - } |
|
223 | - } else { |
|
224 | - if ("/'" == $instr) { |
|
225 | - while (isset($f[$i + 1]) && false !== strpos('gmi', $f[$i + 1])) { |
|
226 | - $s[] = $f[$i++]; |
|
227 | - } |
|
228 | - $s[] = $f[$i]; |
|
229 | - } |
|
230 | - |
|
231 | - $instr = false; |
|
232 | - } |
|
233 | - } elseif ('*' == $instr) { |
|
234 | - } elseif ('!' == $instr) { |
|
235 | - if ('*' == $f[$i] && '/' == $f[$i + 1]) { |
|
236 | - $s[] = "*/\r"; |
|
237 | - ++$i; |
|
238 | - $instr = false; |
|
239 | - } elseif ("\n" == $f[$i]) { |
|
240 | - $s[] = "\r"; |
|
241 | - } else { |
|
242 | - $s[] = $f[$i]; |
|
243 | - } |
|
244 | - } elseif ('\\' == $f[$i]) { |
|
245 | - ++$i; |
|
246 | - |
|
247 | - if ("\n" != $f[$i]) { |
|
248 | - isset($q[$f[$i]]) && ++$q[$f[$i]]; |
|
249 | - $s[] = '\\'.$f[$i]; |
|
250 | - } |
|
251 | - } elseif ('[' == $f[$i] && "/'" == $instr) { |
|
252 | - $instr = '/['; |
|
253 | - $s[] = '['; |
|
254 | - } elseif (']' == $f[$i] && '/[' == $instr) { |
|
255 | - $instr = "/'"; |
|
256 | - $s[] = ']'; |
|
257 | - } elseif ("'" == $f[$i] || '"' == $f[$i]) { |
|
258 | - ++$q[$f[$i]]; |
|
259 | - $s[] = '\\'.$f[$i]; |
|
260 | - } else { |
|
261 | - $s[] = $f[$i]; |
|
262 | - } |
|
263 | - } else { |
|
264 | - switch ($f[$i]) { |
|
265 | - case ';': |
|
266 | - // Remove triple semi-colon |
|
267 | - if ($i > 0 && ';' == $f[$i - 1] && $i + 1 < $len && ';' == $f[$i + 1]) { |
|
268 | - $f[$i] = $f[$i + 1] = '/'; |
|
269 | - } else { |
|
270 | - $code[++$j] = ';'; |
|
271 | - break; |
|
272 | - } |
|
273 | - |
|
274 | - case '/': |
|
275 | - if ('*' == $f[$i + 1]) { |
|
276 | - ++$i; |
|
277 | - $instr = '*'; |
|
278 | - |
|
279 | - if ($this->keepImportantComments && '!' == $f[$i + 1]) { |
|
280 | - ++$i; |
|
281 | - // no break here |
|
282 | - } else { |
|
283 | - break; |
|
284 | - } |
|
285 | - } elseif ('/' == $f[$i + 1]) { |
|
286 | - ++$i; |
|
287 | - $instr = '//'; |
|
288 | - break; |
|
289 | - } else { |
|
290 | - $a = $j && (' ' == $code[$j] || "\x7F" == $code[$j]) ? $code[$j - 1] : $code[$j]; |
|
291 | - if (false !== strpos('-!%&;<=>~:^+|,()*?[{} ', $a) |
|
292 | - || (false !== strpos('oenfd', $a) |
|
293 | - && preg_match( |
|
294 | - "'(?<![\$.a-zA-Z0-9_])(do|else|return|typeof|yield[ \x7F]?\*?)[ \x7F]?$'", |
|
295 | - substr($code, $j - 7, 8) |
|
296 | - )) |
|
297 | - ) { |
|
298 | - if (')' === $a && $j > 1) { |
|
299 | - $a = 1; |
|
300 | - $k = $j - (' ' == $code[$j] || "\x7F" == $code[$j]) - 1; |
|
301 | - while ($k >= 0 && $a) { |
|
302 | - if ('(' === $code[$k]) { |
|
303 | - --$a; |
|
304 | - } elseif (')' === $code[$k]) { |
|
305 | - ++$a; |
|
306 | - } |
|
307 | - --$k; |
|
308 | - } |
|
309 | - if (!preg_match("'(?<![\$.a-zA-Z0-9_])(if|for|while)[ \x7F]?$'", substr($code, 0, $k + 1))) { |
|
310 | - $code[++$j] = '/'; |
|
311 | - break; |
|
312 | - } |
|
313 | - } |
|
314 | - |
|
315 | - $key = "//''\"\"".$K++.$instr = "/'"; |
|
316 | - $a = $j; |
|
317 | - $code .= $key; |
|
318 | - while (isset($key[++$j - $a - 1])) { |
|
319 | - $code[$j] = $key[$j - $a - 1]; |
|
320 | - } |
|
321 | - --$j; |
|
322 | - isset($s) && ($s = implode('', $s)) && $cc_on && $this->restoreCc($s); |
|
323 | - $strings[$key] = array('/'); |
|
324 | - $s = &$strings[$key]; |
|
325 | - } else { |
|
326 | - $code[++$j] = '/'; |
|
327 | - } |
|
328 | - |
|
329 | - break; |
|
330 | - } |
|
331 | - |
|
332 | - case "'": |
|
333 | - case '"': |
|
334 | - $instr = $f[$i]; |
|
335 | - $key = "//''\"\"".$K++.('!' == $instr ? ']' : "'"); |
|
336 | - $a = $j; |
|
337 | - $code .= $key; |
|
338 | - while (isset($key[++$j - $a - 1])) { |
|
339 | - $code[$j] = $key[$j - $a - 1]; |
|
340 | - } |
|
341 | - --$j; |
|
342 | - isset($s) && ($s = implode('', $s)) && $cc_on && $this->restoreCc($s); |
|
343 | - $strings[$key] = array(); |
|
344 | - $s = &$strings[$key]; |
|
345 | - '!' == $instr && $s[] = "\r/*!"; |
|
346 | - |
|
347 | - break; |
|
348 | - |
|
349 | - case "\n": |
|
350 | - if ($j > 3) { |
|
351 | - if (' ' == $code[$j] || "\x7F" == $code[$j]) { |
|
352 | - --$j; |
|
353 | - } |
|
354 | - |
|
355 | - $code[++$j] = |
|
356 | - false !== strpos('kend+-', $code[$j - 1]) |
|
357 | - && preg_match( |
|
358 | - "'(?:\+\+|--|(?<![\$.a-zA-Z0-9_])(break|continue|return|yield[ \x7F]?\*?))[ \x7F]?$'", |
|
359 | - substr($code, $j - 8, 9) |
|
360 | - ) |
|
361 | - ? ';' : "\x7F"; |
|
362 | - |
|
363 | - break; |
|
364 | - } |
|
365 | - |
|
366 | - case "\t": $f[$i] = ' '; |
|
367 | - case ' ': |
|
368 | - if (!$j || ' ' == $code[$j] || "\x7F" == $code[$j]) { |
|
369 | - break; |
|
370 | - } |
|
371 | - |
|
372 | - default: |
|
373 | - $code[++$j] = $f[$i]; |
|
374 | - } |
|
375 | - } |
|
376 | - } |
|
377 | - |
|
378 | - isset($s) && ($s = implode('', $s)) && $cc_on && $this->restoreCc($s); |
|
379 | - unset($s); |
|
380 | - |
|
381 | - $code = substr($code, 0, $j + 1); |
|
382 | - $cc_on && $this->restoreCc($code, false); |
|
383 | - |
|
384 | - // Protect wanted spaces and remove unwanted ones |
|
385 | - $code = strtr($code, "\x7F", ' '); |
|
386 | - $code = str_replace('- -', "-\x7F-", $code); |
|
387 | - $code = str_replace('+ +', "+\x7F+", $code); |
|
388 | - $code = preg_replace("'(\d)\s+\.\s*([a-zA-Z\$_[(])'", "$1\x7F.$2", $code); |
|
389 | - $code = preg_replace("# ([-!%&;<=>~:.^+|,()*?[\]{}/']+)#", '$1', $code); |
|
390 | - $code = preg_replace("#([-!%&;<=>~:.^+|,()*?[\]{}/]+) #", '$1', $code); |
|
391 | - $cc_on && $code = preg_replace_callback("'//[^\'].*?@#3'", function ($m) { return strtr($m[0], ' ', "\x7F"); }, $code); |
|
392 | - |
|
393 | - // Replace new Array/Object by []/{} |
|
394 | - false !== strpos($code, 'new Array') && $code = preg_replace("'new Array(?:\(\)|([;\])},:]))'", '[]$1', $code); |
|
395 | - false !== strpos($code, 'new Object') && $code = preg_replace("'new Object(?:\(\)|([;\])},:]))'", '{}$1', $code); |
|
396 | - |
|
397 | - // Add missing semi-colons after curly braces |
|
398 | - // This adds more semi-colons than strictly needed, |
|
399 | - // but it seems that later gzipping is favorable to the repetition of "};" |
|
400 | - $code = preg_replace("'\}(?![:,;.()\[\]}\|&]|(else|catch|finally|while)[^\$.a-zA-Z0-9_])'", '};', $code); |
|
401 | - |
|
402 | - // Tag possible empty instruction for easy detection |
|
403 | - $code = preg_replace("'(?<![\$.a-zA-Z0-9_])if\('", '1#(', $code); |
|
404 | - $code = preg_replace("'(?<![\$.a-zA-Z0-9_])for\('", '2#(', $code); |
|
405 | - $code = preg_replace("'(?<![\$.a-zA-Z0-9_])while\('", '3#(', $code); |
|
406 | - |
|
407 | - $forPool = array(); |
|
408 | - $instrPool = array(); |
|
409 | - $s = 0; |
|
410 | - |
|
411 | - $f = array(); |
|
412 | - $j = -1; |
|
413 | - |
|
414 | - // Remove as much semi-colon as possible |
|
415 | - $len = strlen($code); |
|
416 | - for ($i = 0; $i < $len; ++$i) { |
|
417 | - switch ($code[$i]) { |
|
418 | - case '(': |
|
419 | - if ($j >= 0 && "\n" == $f[$j]) { |
|
420 | - $f[$j] = ';'; |
|
421 | - } |
|
422 | - |
|
423 | - ++$s; |
|
424 | - |
|
425 | - if ($i && '#' == $code[$i - 1]) { |
|
426 | - $instrPool[$s - 1] = 1; |
|
427 | - if ('2' == $code[$i - 2]) { |
|
428 | - $forPool[$s] = 1; |
|
429 | - } |
|
430 | - } |
|
431 | - |
|
432 | - $f[++$j] = '('; |
|
433 | - break; |
|
434 | - |
|
435 | - case ']': |
|
436 | - case ')': |
|
437 | - if ($i + 1 < $len && !isset($forPool[$s]) && !isset($instrPool[$s - 1]) && preg_match("'[a-zA-Z0-9_\$]'", $code[$i + 1])) { |
|
438 | - $f[$j] .= $code[$i]; |
|
439 | - $f[++$j] = "\n"; |
|
440 | - } else { |
|
441 | - $f[++$j] = $code[$i]; |
|
442 | - } |
|
443 | - |
|
444 | - if (')' == $code[$i]) { |
|
445 | - unset($forPool[$s]); |
|
446 | - --$s; |
|
447 | - } |
|
448 | - |
|
449 | - continue 2; |
|
450 | - |
|
451 | - case '}': |
|
452 | - if ("\n" == $f[$j]) { |
|
453 | - $f[$j] = '}'; |
|
454 | - } else { |
|
455 | - $f[++$j] = '}'; |
|
456 | - } |
|
457 | - break; |
|
458 | - |
|
459 | - case ';': |
|
460 | - if (isset($forPool[$s]) || isset($instrPool[$s])) { |
|
461 | - $f[++$j] = ';'; |
|
462 | - } elseif ($j >= 0 && "\n" != $f[$j] && ';' != $f[$j]) { |
|
463 | - $f[++$j] = "\n"; |
|
464 | - } |
|
465 | - |
|
466 | - break; |
|
467 | - |
|
468 | - case '#': |
|
469 | - switch ($f[$j]) { |
|
470 | - case '1': $f[$j] = 'if'; break 2; |
|
471 | - case '2': $f[$j] = 'for'; break 2; |
|
472 | - case '3': $f[$j] = 'while'; break 2; |
|
473 | - } |
|
474 | - |
|
475 | - case '['; |
|
476 | - if ($j >= 0 && "\n" == $f[$j]) { |
|
477 | - $f[$j] = ';'; |
|
478 | - } |
|
479 | - |
|
480 | - default: $f[++$j] = $code[$i]; |
|
481 | - } |
|
482 | - |
|
483 | - unset($instrPool[$s]); |
|
484 | - } |
|
485 | - |
|
486 | - $f = implode('', $f); |
|
487 | - $cc_on && $f = str_replace('@#3', "\r", $f); |
|
488 | - |
|
489 | - // Fix "else ;" empty instructions |
|
490 | - $f = preg_replace("'(?<![\$.a-zA-Z0-9_])else\n'", "\n", $f); |
|
491 | - |
|
492 | - $r1 = array( // keywords with a direct object |
|
493 | - 'case','delete','do','else','function','in','instanceof','of','break', |
|
494 | - 'new','return','throw','typeof','var','void','yield','let','if', |
|
495 | - 'const','get','set', |
|
496 | - ); |
|
497 | - |
|
498 | - $r2 = array( // keywords with a subject |
|
499 | - 'in','instanceof','of', |
|
500 | - ); |
|
501 | - |
|
502 | - // Fix missing semi-colons |
|
503 | - $f = preg_replace("'(?<!(?<![a-zA-Z0-9_\$])".implode(')(?<!(?<![a-zA-Z0-9_\$])', $r1).') (?!('.implode('|', $r2).")(?![a-zA-Z0-9_\$]))'", "\n", $f); |
|
504 | - $f = preg_replace("'(?<!(?<![a-zA-Z0-9_\$])do)(?<!(?<![a-zA-Z0-9_\$])else) if\('", "\nif(", $f); |
|
505 | - $f = preg_replace("'(?<=--|\+\+)(?<![a-zA-Z0-9_\$])(".implode('|', $r1).")(?![a-zA-Z0-9_\$])'", "\n$1", $f); |
|
506 | - $f = preg_replace("'(?<![a-zA-Z0-9_\$])for\neach\('", 'for each(', $f); |
|
507 | - $f = preg_replace("'(?<![a-zA-Z0-9_\$])\n(".implode('|', $r2).")(?![a-zA-Z0-9_\$])'", '$1', $f); |
|
508 | - |
|
509 | - // Merge strings |
|
510 | - if ($q["'"] > $q['"']) { |
|
511 | - $q = array($q[1], $q[0]); |
|
512 | - } |
|
513 | - $f = preg_replace("#//''\"\"[0-9]+'#", $q[0].'$0'.$q[0], $f); |
|
514 | - strpos($f, $q[0].'+'.$q[0]) && $f = str_replace($q[0].'+'.$q[0], '', $f); |
|
515 | - $len = count($strings); |
|
516 | - foreach ($strings as $r1 => &$r2) { |
|
517 | - $r2 = "/'" == substr($r1, -2) |
|
518 | - ? str_replace(array("\\'", '\\"'), array("'", '"'), $r2) |
|
519 | - : str_replace('\\'.$q[1], $q[1], $r2); |
|
520 | - } |
|
521 | - |
|
522 | - // Restore wanted spaces |
|
523 | - $f = strtr($f, "\x7F", ' '); |
|
524 | - |
|
525 | - return array($f, $strings); |
|
526 | - } |
|
527 | - |
|
528 | - protected function extractClosures($code) |
|
529 | - { |
|
530 | - $code = ';'.$code; |
|
531 | - |
|
532 | - $this->argFreq[-1] += substr_count($code, '}catch('); |
|
533 | - |
|
534 | - if ($this->argFreq[-1]) { |
|
535 | - // Special catch scope handling |
|
536 | - |
|
537 | - // FIXME: this implementation doesn't work with nested catch scopes who need |
|
538 | - // access to their parent's caught variable (but who needs that?). |
|
539 | - |
|
540 | - $f = preg_split("@}catch\(({$this->varRx})@", $code, -1, PREG_SPLIT_DELIM_CAPTURE); |
|
541 | - |
|
542 | - $code = 'catch$scope$var'.mt_rand(); |
|
543 | - $this->specialVarRx = $this->specialVarRx ? '(?:'.$this->specialVarRx.'|'.preg_quote($code).')' : preg_quote($code); |
|
544 | - $i = count($f) - 1; |
|
545 | - |
|
546 | - while ($i) { |
|
547 | - $c = 1; |
|
548 | - $j = 0; |
|
549 | - $l = strlen($f[$i]); |
|
550 | - |
|
551 | - while ($c && $j < $l) { |
|
552 | - $s = $f[$i][$j++]; |
|
553 | - $c += '(' == $s ? 1 : (')' == $s ? -1 : 0); |
|
554 | - } |
|
555 | - |
|
556 | - if (!$c) { |
|
557 | - do { |
|
558 | - $s = $f[$i][$j++]; |
|
559 | - $c += '{' == $s ? 1 : ('}' == $s ? -1 : 0); |
|
560 | - } while ($c && $j < $l); |
|
561 | - } |
|
562 | - |
|
563 | - $c = preg_quote($f[$i - 1], '#'); |
|
564 | - $f[$i - 2] .= '}catch('.preg_replace("#([.,{]?)(?<![a-zA-Z0-9_\$@]){$c}\\b#", '$1'.$code, $f[$i - 1].substr($f[$i], 0, $j)).substr($f[$i], $j); |
|
565 | - |
|
566 | - unset($f[$i--], $f[$i--]); |
|
567 | - } |
|
568 | - |
|
569 | - $code = $f[0]; |
|
570 | - } |
|
571 | - |
|
572 | - $f = preg_split("'(?<![a-zA-Z0-9_\$])((?:function[ (]|get |set ).*?\{)'", $code, -1, PREG_SPLIT_DELIM_CAPTURE); |
|
573 | - $i = count($f) - 1; |
|
574 | - $closures = array(); |
|
575 | - |
|
576 | - while ($i) { |
|
577 | - $c = 1; |
|
578 | - $j = 0; |
|
579 | - $l = strlen($f[$i]); |
|
580 | - |
|
581 | - while ($c && $j < $l) { |
|
582 | - $s = $f[$i][$j++]; |
|
583 | - $c += '{' == $s ? 1 : ('}' == $s ? -1 : 0); |
|
584 | - } |
|
585 | - |
|
586 | - switch (substr($f[$i - 2], -1)) { |
|
587 | - default: |
|
588 | - if (false !== $c = strpos($f[$i - 1], ' ', 8)) { |
|
589 | - break; |
|
590 | - } |
|
591 | - case false: case "\n": case ';': case '{': case '}': case ')': case ']': |
|
592 | - $c = strpos($f[$i - 1], '(', 4); |
|
593 | - } |
|
594 | - |
|
595 | - $l = "//''\"\"#$i'"; |
|
596 | - $code = substr($f[$i - 1], $c); |
|
597 | - $closures[$l] = $code.substr($f[$i], 0, $j); |
|
598 | - $f[$i - 2] .= substr($f[$i - 1], 0, $c).$l.substr($f[$i], $j); |
|
599 | - |
|
600 | - if ('(){' !== $code) { |
|
601 | - $j = substr_count($code, ','); |
|
602 | - do { |
|
603 | - isset($this->argFreq[$j]) ? ++$this->argFreq[$j] : $this->argFreq[$j] = 1; |
|
604 | - } while ($j--); |
|
605 | - } |
|
606 | - |
|
607 | - $i -= 2; |
|
608 | - } |
|
609 | - |
|
610 | - return array($f[0], $closures); |
|
611 | - } |
|
612 | - |
|
613 | - protected function makeVars($closure, &$tree, $key) |
|
614 | - { |
|
615 | - $tree['code'] = &$closure; |
|
616 | - $tree['nfe'] = false; |
|
617 | - $tree['used'] = array(); |
|
618 | - $tree['local'] = array(); |
|
619 | - |
|
620 | - // Replace multiple "var" declarations by a single one |
|
621 | - $closure = preg_replace_callback("'(?<=[\n\{\}])var [^\n\{\};]+(?:\nvar [^\n\{\};]+)+'", array($this, 'mergeVarDeclarations'), $closure); |
|
622 | - |
|
623 | - // Get all local vars (functions, arguments and "var" prefixed) |
|
624 | - |
|
625 | - $vars = &$tree['local']; |
|
626 | - |
|
627 | - if (preg_match("'^( [^(]*)?\((.*?)\)\{'", $closure, $v)) { |
|
628 | - if ($v[1]) { |
|
629 | - $vars[$tree['nfe'] = substr($v[1], 1)] = -1; |
|
630 | - $tree['parent']['local'][';'.$key] = &$vars[$tree['nfe']]; |
|
631 | - } |
|
632 | - |
|
633 | - if ($v[2]) { |
|
634 | - $i = 0; |
|
635 | - $v = explode(',', $v[2]); |
|
636 | - foreach ($v as $w) { |
|
637 | - $vars[$w] = $this->argFreq[$i++] - 1; // Give a bonus to argument variables |
|
638 | - } |
|
639 | - } |
|
640 | - } |
|
641 | - |
|
642 | - $v = preg_split("'(?<![\$.a-zA-Z0-9_])var '", $closure); |
|
643 | - if ($i = count($v) - 1) { |
|
644 | - $w = array(); |
|
645 | - |
|
646 | - while ($i) { |
|
647 | - $j = $c = 0; |
|
648 | - $l = strlen($v[$i]); |
|
649 | - |
|
650 | - while ($j < $l) { |
|
651 | - switch ($v[$i][$j]) { |
|
652 | - case '(': case '[': case '{': |
|
653 | - ++$c; |
|
654 | - break; |
|
655 | - |
|
656 | - case ')': case ']': case '}': |
|
657 | - if ($c-- <= 0) { |
|
658 | - break 2; |
|
659 | - } |
|
660 | - break; |
|
661 | - |
|
662 | - case ';': case "\n": |
|
663 | - if (!$c) { |
|
664 | - break 2; |
|
665 | - } |
|
666 | - |
|
667 | - default: |
|
668 | - $c || $w[] = $v[$i][$j]; |
|
669 | - } |
|
670 | - |
|
671 | - ++$j; |
|
672 | - } |
|
673 | - |
|
674 | - $w[] = ','; |
|
675 | - --$i; |
|
676 | - } |
|
677 | - |
|
678 | - $v = explode(',', implode('', $w)); |
|
679 | - foreach ($v as $w) { |
|
680 | - if (preg_match("'^{$this->varRx}'", $w, $v)) { |
|
681 | - isset($vars[$v[0]]) || $vars[$v[0]] = 0; |
|
682 | - } |
|
683 | - } |
|
684 | - } |
|
685 | - |
|
686 | - if (preg_match_all("@function ({$this->varRx})//''\"\"#@", $closure, $v)) { |
|
687 | - foreach ($v[1] as $w) { |
|
688 | - isset($vars[$w]) || $vars[$w] = 0; |
|
689 | - } |
|
690 | - } |
|
691 | - |
|
692 | - if ($this->argFreq[-1] && preg_match_all("@}catch\(({$this->varRx})@", $closure, $v)) { |
|
693 | - $v[0] = array(); |
|
694 | - foreach ($v[1] as $w) { |
|
695 | - isset($v[0][$w]) ? ++$v[0][$w] : $v[0][$w] = 1; |
|
696 | - } |
|
697 | - foreach ($v[0] as $w => $v) { |
|
698 | - $vars[$w] = $this->argFreq[-1] - $v; |
|
699 | - } |
|
700 | - } |
|
701 | - |
|
702 | - // Get all used vars, local and non-local |
|
703 | - |
|
704 | - $vars = &$tree['used']; |
|
705 | - |
|
706 | - if (preg_match_all("#([.,{]?(?:[gs]et )?)(?<![a-zA-Z0-9_\$])({$this->varRx})(:?)#", $closure, $w, PREG_SET_ORDER)) { |
|
707 | - foreach ($w as $k) { |
|
708 | - if (isset($k[1][0]) && (',' === $k[1][0] || '{' === $k[1][0])) { |
|
709 | - if (':' === $k[3]) { |
|
710 | - $k = '.'.$k[2]; |
|
711 | - } elseif ('get ' === substr($k[1], 1, 4) || 'set ' === substr($k[1], 1, 4)) { |
|
712 | - ++$this->charFreq[ord($k[1][1])]; // "g" or "s" |
|
713 | - ++$this->charFreq[101]; // "e" |
|
714 | - ++$this->charFreq[116]; // "t" |
|
715 | - $k = '.'.$k[2]; |
|
716 | - } else { |
|
717 | - $k = $k[2]; |
|
718 | - } |
|
719 | - } else { |
|
720 | - $k = $k[1].$k[2]; |
|
721 | - } |
|
722 | - |
|
723 | - isset($vars[$k]) ? ++$vars[$k] : $vars[$k] = 1; |
|
724 | - } |
|
725 | - } |
|
726 | - |
|
727 | - if (preg_match_all("#//''\"\"[0-9]+(?:['!]|/')#", $closure, $w)) { |
|
728 | - foreach ($w[0] as $a) { |
|
729 | - $v = "'" === substr($a, -1) && "/'" !== substr($a, -2) && $this->specialVarRx |
|
730 | - ? preg_split("#([.,{]?(?:[gs]et )?(?<![a-zA-Z0-9_\$@]){$this->specialVarRx}:?)#", $this->strings[$a], -1, PREG_SPLIT_DELIM_CAPTURE) |
|
731 | - : array($this->strings[$a]); |
|
732 | - $a = count($v); |
|
733 | - |
|
734 | - for ($i = 0; $i < $a; ++$i) { |
|
735 | - $k = $v[$i]; |
|
736 | - |
|
737 | - if (1 === $i % 2) { |
|
738 | - if (',' === $k[0] || '{' === $k[0]) { |
|
739 | - if (':' === substr($k, -1)) { |
|
740 | - $k = '.'.substr($k, 1, -1); |
|
741 | - } elseif ('get ' === substr($k, 1, 4) || 'set ' === substr($k, 1, 4)) { |
|
742 | - ++$this->charFreq[ord($k[1])]; // "g" or "s" |
|
743 | - ++$this->charFreq[101]; // "e" |
|
744 | - ++$this->charFreq[116]; // "t" |
|
745 | - $k = '.'.substr($k, 5); |
|
746 | - } else { |
|
747 | - $k = substr($k, 1); |
|
748 | - } |
|
749 | - } elseif (':' === substr($k, -1)) { |
|
750 | - $k = substr($k, 0, -1); |
|
751 | - } |
|
752 | - |
|
753 | - $w = &$tree; |
|
754 | - |
|
755 | - while (isset($w['parent']) && !(isset($w['used'][$k]) || isset($w['local'][$k]))) { |
|
756 | - $w = &$w['parent']; |
|
757 | - } |
|
758 | - |
|
759 | - (isset($w['used'][$k]) || isset($w['local'][$k])) && (isset($vars[$k]) ? ++$vars[$k] : $vars[$k] = 1); |
|
760 | - |
|
761 | - unset($w); |
|
762 | - } |
|
763 | - |
|
764 | - if (0 === $i % 2 || !isset($vars[$k])) { |
|
765 | - foreach (count_chars($v[$i], 1) as $k => $w) { |
|
766 | - $this->charFreq[$k] += $w; |
|
767 | - } |
|
768 | - } |
|
769 | - } |
|
770 | - } |
|
771 | - } |
|
772 | - |
|
773 | - // Propagate the usage number to parents |
|
774 | - |
|
775 | - foreach ($vars as $w => $a) { |
|
776 | - $k = &$tree; |
|
777 | - $chain = array(); |
|
778 | - do { |
|
779 | - $vars = &$k['local']; |
|
780 | - $chain[] = &$k; |
|
781 | - if (isset($vars[$w])) { |
|
782 | - unset($k['used'][$w]); |
|
783 | - if (isset($vars[$w])) { |
|
784 | - $vars[$w] += $a; |
|
785 | - } else { |
|
786 | - $vars[$w] = $a; |
|
787 | - } |
|
788 | - $a = false; |
|
789 | - break; |
|
790 | - } |
|
791 | - } while ($k['parent'] && $k = &$k['parent']); |
|
792 | - |
|
793 | - if ($a && !$k['parent']) { |
|
794 | - if (isset($vars[$w])) { |
|
795 | - $vars[$w] += $a; |
|
796 | - } else { |
|
797 | - $vars[$w] = $a; |
|
798 | - } |
|
799 | - } |
|
800 | - |
|
801 | - if (isset($tree['used'][$w]) && isset($vars[$w])) { |
|
802 | - foreach ($chain as &$b) { |
|
803 | - isset($b['local'][$w]) || $b['used'][$w] = &$vars[$w]; |
|
804 | - } |
|
805 | - } |
|
806 | - } |
|
807 | - |
|
808 | - // Analyse childs |
|
809 | - |
|
810 | - $tree['childs'] = array(); |
|
811 | - $vars = &$tree['childs']; |
|
812 | - |
|
813 | - if (preg_match_all("@//''\"\"#[0-9]+'@", $closure, $w)) { |
|
814 | - foreach ($w[0] as $a) { |
|
815 | - $vars[$a] = array('parent' => &$tree); |
|
816 | - $this->makeVars($this->closures[$a], $vars[$a], $a); |
|
817 | - } |
|
818 | - } |
|
819 | - } |
|
820 | - |
|
821 | - protected function mergeVarDeclarations($m) |
|
822 | - { |
|
823 | - return str_replace("\nvar ", ',', $m[0]); |
|
824 | - } |
|
825 | - |
|
826 | - protected function renameVars(&$tree, $root) |
|
827 | - { |
|
828 | - if ($root) { |
|
829 | - $tree['local'] += $tree['used']; |
|
830 | - $tree['used'] = array(); |
|
831 | - |
|
832 | - foreach ($tree['local'] as $k => $v) { |
|
833 | - if ('.' == $k[0]) { |
|
834 | - $k = substr($k, 1); |
|
835 | - } |
|
836 | - |
|
837 | - if ('true' === $k) { |
|
838 | - $this->charFreq[48] += $v; |
|
839 | - } elseif ('false' === $k) { |
|
840 | - $this->charFreq[49] += $v; |
|
841 | - } elseif (!$this->specialVarRx || !preg_match("#^{$this->specialVarRx}$#", $k)) { |
|
842 | - foreach (count_chars($k, 1) as $k => $w) { |
|
843 | - $this->charFreq[$k] += $w * $v; |
|
844 | - } |
|
845 | - } elseif (2 == strlen($k)) { |
|
846 | - $tree['used'][] = $k[1]; |
|
847 | - } |
|
848 | - } |
|
849 | - |
|
850 | - $this->charFreq = $this->rsort($this->charFreq); |
|
851 | - |
|
852 | - $this->str0 = ''; |
|
853 | - $this->str1 = ''; |
|
854 | - |
|
855 | - foreach ($this->charFreq as $k => $v) { |
|
856 | - if (!$v) { |
|
857 | - break; |
|
858 | - } |
|
859 | - |
|
860 | - $v = chr($k); |
|
861 | - |
|
862 | - if ((64 < $k && $k < 91) || (96 < $k && $k < 123)) { // A-Z a-z |
|
863 | - $this->str0 .= $v; |
|
864 | - $this->str1 .= $v; |
|
865 | - } elseif (47 < $k && $k < 58) { // 0-9 |
|
866 | - $this->str1 .= $v; |
|
867 | - } |
|
868 | - } |
|
869 | - |
|
870 | - if ('' === $this->str0) { |
|
871 | - $this->str0 = 'claspemitdbfrugnjvhowkxqyzCLASPEMITDBFRUGNJVHOWKXQYZ'; |
|
872 | - $this->str1 = $this->str0.'0123456789'; |
|
873 | - } |
|
874 | - |
|
875 | - foreach ($tree['local'] as $var => $root) { |
|
876 | - if ('.' != substr($var, 0, 1) && isset($tree['local'][".{$var}"])) { |
|
877 | - $tree['local'][$var] += $tree['local'][".{$var}"]; |
|
878 | - } |
|
879 | - } |
|
880 | - |
|
881 | - foreach ($tree['local'] as $var => $root) { |
|
882 | - if ('.' == substr($var, 0, 1) && isset($tree['local'][substr($var, 1)])) { |
|
883 | - $tree['local'][$var] = $tree['local'][substr($var, 1)]; |
|
884 | - } |
|
885 | - } |
|
886 | - |
|
887 | - $tree['local'] = $this->rsort($tree['local']); |
|
888 | - |
|
889 | - foreach ($tree['local'] as $var => $root) { |
|
890 | - switch (substr($var, 0, 1)) { |
|
891 | - case '.': |
|
892 | - if (!isset($tree['local'][substr($var, 1)])) { |
|
893 | - $tree['local'][$var] = '#'.($this->specialVarRx && 3 < strlen($var) && preg_match("'^\.{$this->specialVarRx}$'", $var) ? $this->getNextName($tree).'$' : substr($var, 1)); |
|
894 | - } |
|
895 | - break; |
|
896 | - |
|
897 | - case ';': $tree['local'][$var] = 0 === $root ? '' : $this->getNextName($tree); |
|
898 | - case '#': break; |
|
899 | - |
|
900 | - default: |
|
901 | - $root = $this->specialVarRx && 2 < strlen($var) && preg_match("'^{$this->specialVarRx}$'", $var) ? $this->getNextName($tree).'$' : $var; |
|
902 | - $tree['local'][$var] = $root; |
|
903 | - if (isset($tree['local'][".{$var}"])) { |
|
904 | - $tree['local'][".{$var}"] = '#'.$root; |
|
905 | - } |
|
906 | - } |
|
907 | - } |
|
908 | - |
|
909 | - foreach ($tree['local'] as $var => $root) { |
|
910 | - $tree['local'][$var] = preg_replace("'^#'", '.', $tree['local'][$var]); |
|
911 | - } |
|
912 | - } else { |
|
913 | - $tree['local'] = $this->rsort($tree['local']); |
|
914 | - if (false !== $tree['nfe']) { |
|
915 | - $tree['used'][] = $tree['local'][$tree['nfe']]; |
|
916 | - } |
|
917 | - |
|
918 | - foreach ($tree['local'] as $var => $root) { |
|
919 | - if ($tree['nfe'] !== $var) { |
|
920 | - $tree['local'][$var] = 0 === $root ? '' : $this->getNextName($tree); |
|
921 | - } |
|
922 | - } |
|
923 | - } |
|
924 | - |
|
925 | - $this->local_tree = &$tree['local']; |
|
926 | - $this->used_tree = &$tree['used']; |
|
927 | - |
|
928 | - $tree['code'] = preg_replace_callback("#[.,{ ]?(?:[gs]et )?(?<![a-zA-Z0-9_\$@]){$this->varRx}:?#", array($this, 'getNewName'), $tree['code']); |
|
929 | - |
|
930 | - if ($this->specialVarRx && preg_match_all("#//''\"\"[0-9]+'#", $tree['code'], $b)) { |
|
931 | - foreach ($b[0] as $a) { |
|
932 | - $this->strings[$a] = preg_replace_callback( |
|
933 | - "#[.,{]?(?:[gs]et )?(?<![a-zA-Z0-9_\$@]){$this->specialVarRx}:?#", |
|
934 | - array($this, 'getNewName'), |
|
935 | - $this->strings[$a] |
|
936 | - ); |
|
937 | - } |
|
938 | - } |
|
939 | - |
|
940 | - foreach ($tree['childs'] as $a => &$b) { |
|
941 | - $this->renameVars($b, false); |
|
942 | - $tree['code'] = str_replace($a, $b['code'], $tree['code']); |
|
943 | - unset($tree['childs'][$a]); |
|
944 | - } |
|
945 | - } |
|
946 | - |
|
947 | - protected function getNewName($m) |
|
948 | - { |
|
949 | - $m = $m[0]; |
|
950 | - |
|
951 | - $pre = '.' === $m[0] ? '.' : ''; |
|
952 | - $post = ''; |
|
953 | - |
|
954 | - if (',' === $m[0] || '{' === $m[0] || ' ' === $m[0]) { |
|
955 | - $pre = $m[0]; |
|
956 | - |
|
957 | - if (':' === substr($m, -1)) { |
|
958 | - $post = ':'; |
|
959 | - $m = (' ' !== $m[0] ? '.' : '').substr($m, 1, -1); |
|
960 | - } elseif ('get ' === substr($m, 1, 4) || 'set ' === substr($m, 1, 4)) { |
|
961 | - $pre .= substr($m, 1, 4); |
|
962 | - $m = '.'.substr($m, 5); |
|
963 | - } else { |
|
964 | - $m = substr($m, 1); |
|
965 | - } |
|
966 | - } elseif (':' === substr($m, -1)) { |
|
967 | - $post = ':'; |
|
968 | - $m = substr($m, 0, -1); |
|
969 | - } |
|
970 | - |
|
971 | - $post = (isset($this->reserved[$m]) |
|
972 | - ? ('true' === $m ? '!0' : ('false' === $m ? '!1' : $m)) |
|
973 | - : ( |
|
974 | - isset($this->local_tree[$m]) |
|
975 | - ? $this->local_tree[$m] |
|
976 | - : ( |
|
977 | - isset($this->used_tree[$m]) |
|
978 | - ? $this->used_tree[$m] |
|
979 | - : $m |
|
980 | - ) |
|
981 | - ) |
|
982 | - ).$post; |
|
983 | - |
|
984 | - return '' === $post ? '' : ($pre.('.' === $post[0] ? substr($post, 1) : $post)); |
|
985 | - } |
|
986 | - |
|
987 | - protected function getNextName(&$tree = array(), &$counter = false) |
|
988 | - { |
|
989 | - if (false === $counter) { |
|
990 | - $counter = &$tree['counter']; |
|
991 | - isset($counter) || $counter = -1; |
|
992 | - $exclude = array_flip($tree['used']); |
|
993 | - } else { |
|
994 | - $exclude = $tree; |
|
995 | - } |
|
996 | - |
|
997 | - ++$counter; |
|
998 | - |
|
999 | - $len0 = strlen($this->str0); |
|
1000 | - $len1 = strlen($this->str0); |
|
1001 | - |
|
1002 | - $name = $this->str0[$counter % $len0]; |
|
1003 | - |
|
1004 | - $i = intval($counter / $len0) - 1; |
|
1005 | - while ($i >= 0) { |
|
1006 | - $name .= $this->str1[ $i % $len1 ]; |
|
1007 | - $i = intval($i / $len1) - 1; |
|
1008 | - } |
|
1009 | - |
|
1010 | - return !(isset($this->reserved[$name]) || isset($exclude[$name])) ? $name : $this->getNextName($exclude, $counter); |
|
1011 | - } |
|
1012 | - |
|
1013 | - protected function restoreCc(&$s, $lf = true) |
|
1014 | - { |
|
1015 | - $lf && $s = str_replace('@#3', '', $s); |
|
1016 | - |
|
1017 | - $s = str_replace('@#1', '@*/', $s); |
|
1018 | - $s = str_replace('2#@', '//@', $s); |
|
1019 | - $s = str_replace('1#@', '/*@', $s); |
|
1020 | - $s = str_replace('##', '#', $s); |
|
1021 | - } |
|
1022 | - |
|
1023 | - private function rsort($array) |
|
1024 | - { |
|
1025 | - if (!$array) { |
|
1026 | - return $array; |
|
1027 | - } |
|
1028 | - |
|
1029 | - $i = 0; |
|
1030 | - $tuples = array(); |
|
1031 | - foreach ($array as $k => &$v) { |
|
1032 | - $tuples[] = array(++$i, $k, &$v); |
|
1033 | - } |
|
1034 | - |
|
1035 | - usort($tuples, function ($a, $b) { |
|
1036 | - if ($b[2] > $a[2]) { |
|
1037 | - return 1; |
|
1038 | - } |
|
1039 | - if ($b[2] < $a[2]) { |
|
1040 | - return -1; |
|
1041 | - } |
|
1042 | - if ($b[0] > $a[0]) { |
|
1043 | - return -1; |
|
1044 | - } |
|
1045 | - if ($b[0] < $a[0]) { |
|
1046 | - return 1; |
|
1047 | - } |
|
1048 | - |
|
1049 | - return 0; |
|
1050 | - }); |
|
1051 | - |
|
1052 | - $array = array(); |
|
1053 | - |
|
1054 | - foreach ($tuples as $t) { |
|
1055 | - $array[$t[1]] = &$t[2]; |
|
1056 | - } |
|
1057 | - |
|
1058 | - return $array; |
|
1059 | - } |
|
60 | + const |
|
61 | + |
|
62 | + SPECIAL_VAR_PACKER = '(\$+[a-zA-Z_]|_[a-zA-Z0-9$])[a-zA-Z0-9_$]*'; |
|
63 | + |
|
64 | + public |
|
65 | + |
|
66 | + $charFreq; |
|
67 | + |
|
68 | + protected |
|
69 | + |
|
70 | + $strings, |
|
71 | + $closures, |
|
72 | + $str0, |
|
73 | + $str1, |
|
74 | + $argFreq, |
|
75 | + $specialVarRx, |
|
76 | + $keepImportantComments, |
|
77 | + |
|
78 | + $varRx = '(?:[a-zA-Z_$])[a-zA-Z0-9_$]*', |
|
79 | + $reserved = array( |
|
80 | + // Literals |
|
81 | + 'true','false','null', |
|
82 | + // ES6 |
|
83 | + 'break','case','class','catch','const','continue','debugger','default','delete','do','else','export','extends','finally','for','function','if','import','in','instanceof','new','return','super','switch','this','throw','try','typeof','var','void','while','with','yield', |
|
84 | + // Future |
|
85 | + 'enum', |
|
86 | + // Strict mode |
|
87 | + 'implements','package','protected','static','let','interface','private','public', |
|
88 | + // Module |
|
89 | + 'await', |
|
90 | + // Older standards |
|
91 | + 'abstract','boolean','byte','char','double','final','float','goto','int','long','native','short','synchronized','throws','transient','volatile', |
|
92 | + ); |
|
93 | + |
|
94 | + public function __construct() |
|
95 | + { |
|
96 | + $this->reserved = array_flip($this->reserved); |
|
97 | + $this->charFreq = array_fill(0, 256, 0); |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * Squeezes a JavaScript source code. |
|
102 | + * |
|
103 | + * Set $singleLine to false if you want optional |
|
104 | + * semi-colons to be replaced by line feeds. |
|
105 | + * |
|
106 | + * Set $keepImportantComments to false if you want /*! comments to be removed. |
|
107 | + * |
|
108 | + * $specialVarRx defines the regular expression of special variables names |
|
109 | + * for global vars, methods, properties and in string substitution. |
|
110 | + * Set it to false if you don't want any. |
|
111 | + * |
|
112 | + * If the analysed javascript source contains a single line comment like |
|
113 | + * this one, then the directive will overwrite $specialVarRx: |
|
114 | + * |
|
115 | + * // jsqueeze.specialVarRx = your_special_var_regexp_here |
|
116 | + * |
|
117 | + * Only the first directive is parsed, others are ignored. It is not possible |
|
118 | + * to redefine $specialVarRx in the middle of the javascript source. |
|
119 | + * |
|
120 | + * Example: |
|
121 | + * $parser = new JSqueeze; |
|
122 | + * $squeezed_js = $parser->squeeze($fat_js); |
|
123 | + */ |
|
124 | + public function squeeze($code, $singleLine = true, $keepImportantComments = true, $specialVarRx = false) |
|
125 | + { |
|
126 | + $code = trim($code); |
|
127 | + if ('' === $code) { |
|
128 | + return ''; |
|
129 | + } |
|
130 | + |
|
131 | + $this->argFreq = array(-1 => 0); |
|
132 | + $this->specialVarRx = $specialVarRx; |
|
133 | + $this->keepImportantComments = !!$keepImportantComments; |
|
134 | + |
|
135 | + if (preg_match("#//[ \t]*jsqueeze\.specialVarRx[ \t]*=[ \t]*([\"']?)(.*)\\1#i", $code, $key)) { |
|
136 | + if (!$key[1]) { |
|
137 | + $key[2] = trim($key[2]); |
|
138 | + $key[1] = strtolower($key[2]); |
|
139 | + $key[1] = $key[1] && $key[1] != 'false' && $key[1] != 'none' && $key[1] != 'off'; |
|
140 | + } |
|
141 | + |
|
142 | + $this->specialVarRx = $key[1] ? $key[2] : false; |
|
143 | + } |
|
144 | + |
|
145 | + // Remove capturing parentheses |
|
146 | + $this->specialVarRx && $this->specialVarRx = preg_replace('/(?<!\\\\)((?:\\\\\\\\)*)\((?!\?)/', '(?:', $this->specialVarRx); |
|
147 | + |
|
148 | + false !== strpos($code, "\r") && $code = strtr(str_replace("\r\n", "\n", $code), "\r", "\n"); |
|
149 | + false !== strpos($code, "\xC2\x85") && $code = str_replace("\xC2\x85", "\n", $code); // Next Line |
|
150 | + false !== strpos($code, "\xE2\x80\xA8") && $code = str_replace("\xE2\x80\xA8", "\n", $code); // Line Separator |
|
151 | + false !== strpos($code, "\xE2\x80\xA9") && $code = str_replace("\xE2\x80\xA9", "\n", $code); // Paragraph Separator |
|
152 | + |
|
153 | + list($code, $this->strings) = $this->extractStrings($code); |
|
154 | + list($code, $this->closures) = $this->extractClosures($code); |
|
155 | + |
|
156 | + $key = "//''\"\"#0'"; // This crap has a wonderful property: it can not happen in any valid javascript, even in strings |
|
157 | + $this->closures[$key] = &$code; |
|
158 | + |
|
159 | + $tree = array($key => array('parent' => false)); |
|
160 | + $this->makeVars($code, $tree[$key], $key); |
|
161 | + $this->renameVars($tree[$key], true); |
|
162 | + |
|
163 | + $code = substr($tree[$key]['code'], 1); |
|
164 | + $code = preg_replace("'\breturn !'", 'return!', $code); |
|
165 | + $code = preg_replace("'\}(?=(else|while)[^\$.a-zA-Z0-9_])'", "}\r", $code); |
|
166 | + $code = str_replace(array_keys($this->strings), array_values($this->strings), $code); |
|
167 | + |
|
168 | + if ($singleLine) { |
|
169 | + $code = strtr($code, "\n", ';'); |
|
170 | + } else { |
|
171 | + $code = str_replace("\n", ";\n", $code); |
|
172 | + } |
|
173 | + false !== strpos($code, "\r") && $code = strtr(trim($code), "\r", "\n"); |
|
174 | + |
|
175 | + // Cleanup memory |
|
176 | + $this->charFreq = array_fill(0, 256, 0); |
|
177 | + $this->strings = $this->closures = $this->argFreq = array(); |
|
178 | + $this->str0 = $this->str1 = ''; |
|
179 | + |
|
180 | + return $code; |
|
181 | + } |
|
182 | + |
|
183 | + protected function extractStrings($f) |
|
184 | + { |
|
185 | + if ($cc_on = false !== strpos($f, '@cc_on')) { |
|
186 | + // Protect conditional comments from being removed |
|
187 | + $f = str_replace('#', '##', $f); |
|
188 | + $f = str_replace('/*@', '1#@', $f); |
|
189 | + $f = preg_replace("'//@([^\n]+)'", '2#@$1@#3', $f); |
|
190 | + $f = str_replace('@*/', '@#1', $f); |
|
191 | + } |
|
192 | + |
|
193 | + $len = strlen($f); |
|
194 | + $code = str_repeat(' ', $len); |
|
195 | + $j = 0; |
|
196 | + |
|
197 | + $strings = array(); |
|
198 | + $K = 0; |
|
199 | + |
|
200 | + $instr = false; |
|
201 | + |
|
202 | + $q = array( |
|
203 | + "'", '"', |
|
204 | + "'" => 0, |
|
205 | + '"' => 0, |
|
206 | + ); |
|
207 | + |
|
208 | + // Extract strings, removes comments |
|
209 | + for ($i = 0; $i < $len; ++$i) { |
|
210 | + if ($instr) { |
|
211 | + if ('//' == $instr) { |
|
212 | + if ("\n" == $f[$i]) { |
|
213 | + $f[$i--] = ' '; |
|
214 | + $instr = false; |
|
215 | + } |
|
216 | + } elseif ($f[$i] == $instr || ('/' == $f[$i] && "/'" == $instr)) { |
|
217 | + if ('!' == $instr) { |
|
218 | + } elseif ('*' == $instr) { |
|
219 | + if ('/' == $f[$i + 1]) { |
|
220 | + ++$i; |
|
221 | + $instr = false; |
|
222 | + } |
|
223 | + } else { |
|
224 | + if ("/'" == $instr) { |
|
225 | + while (isset($f[$i + 1]) && false !== strpos('gmi', $f[$i + 1])) { |
|
226 | + $s[] = $f[$i++]; |
|
227 | + } |
|
228 | + $s[] = $f[$i]; |
|
229 | + } |
|
230 | + |
|
231 | + $instr = false; |
|
232 | + } |
|
233 | + } elseif ('*' == $instr) { |
|
234 | + } elseif ('!' == $instr) { |
|
235 | + if ('*' == $f[$i] && '/' == $f[$i + 1]) { |
|
236 | + $s[] = "*/\r"; |
|
237 | + ++$i; |
|
238 | + $instr = false; |
|
239 | + } elseif ("\n" == $f[$i]) { |
|
240 | + $s[] = "\r"; |
|
241 | + } else { |
|
242 | + $s[] = $f[$i]; |
|
243 | + } |
|
244 | + } elseif ('\\' == $f[$i]) { |
|
245 | + ++$i; |
|
246 | + |
|
247 | + if ("\n" != $f[$i]) { |
|
248 | + isset($q[$f[$i]]) && ++$q[$f[$i]]; |
|
249 | + $s[] = '\\'.$f[$i]; |
|
250 | + } |
|
251 | + } elseif ('[' == $f[$i] && "/'" == $instr) { |
|
252 | + $instr = '/['; |
|
253 | + $s[] = '['; |
|
254 | + } elseif (']' == $f[$i] && '/[' == $instr) { |
|
255 | + $instr = "/'"; |
|
256 | + $s[] = ']'; |
|
257 | + } elseif ("'" == $f[$i] || '"' == $f[$i]) { |
|
258 | + ++$q[$f[$i]]; |
|
259 | + $s[] = '\\'.$f[$i]; |
|
260 | + } else { |
|
261 | + $s[] = $f[$i]; |
|
262 | + } |
|
263 | + } else { |
|
264 | + switch ($f[$i]) { |
|
265 | + case ';': |
|
266 | + // Remove triple semi-colon |
|
267 | + if ($i > 0 && ';' == $f[$i - 1] && $i + 1 < $len && ';' == $f[$i + 1]) { |
|
268 | + $f[$i] = $f[$i + 1] = '/'; |
|
269 | + } else { |
|
270 | + $code[++$j] = ';'; |
|
271 | + break; |
|
272 | + } |
|
273 | + |
|
274 | + case '/': |
|
275 | + if ('*' == $f[$i + 1]) { |
|
276 | + ++$i; |
|
277 | + $instr = '*'; |
|
278 | + |
|
279 | + if ($this->keepImportantComments && '!' == $f[$i + 1]) { |
|
280 | + ++$i; |
|
281 | + // no break here |
|
282 | + } else { |
|
283 | + break; |
|
284 | + } |
|
285 | + } elseif ('/' == $f[$i + 1]) { |
|
286 | + ++$i; |
|
287 | + $instr = '//'; |
|
288 | + break; |
|
289 | + } else { |
|
290 | + $a = $j && (' ' == $code[$j] || "\x7F" == $code[$j]) ? $code[$j - 1] : $code[$j]; |
|
291 | + if (false !== strpos('-!%&;<=>~:^+|,()*?[{} ', $a) |
|
292 | + || (false !== strpos('oenfd', $a) |
|
293 | + && preg_match( |
|
294 | + "'(?<![\$.a-zA-Z0-9_])(do|else|return|typeof|yield[ \x7F]?\*?)[ \x7F]?$'", |
|
295 | + substr($code, $j - 7, 8) |
|
296 | + )) |
|
297 | + ) { |
|
298 | + if (')' === $a && $j > 1) { |
|
299 | + $a = 1; |
|
300 | + $k = $j - (' ' == $code[$j] || "\x7F" == $code[$j]) - 1; |
|
301 | + while ($k >= 0 && $a) { |
|
302 | + if ('(' === $code[$k]) { |
|
303 | + --$a; |
|
304 | + } elseif (')' === $code[$k]) { |
|
305 | + ++$a; |
|
306 | + } |
|
307 | + --$k; |
|
308 | + } |
|
309 | + if (!preg_match("'(?<![\$.a-zA-Z0-9_])(if|for|while)[ \x7F]?$'", substr($code, 0, $k + 1))) { |
|
310 | + $code[++$j] = '/'; |
|
311 | + break; |
|
312 | + } |
|
313 | + } |
|
314 | + |
|
315 | + $key = "//''\"\"".$K++.$instr = "/'"; |
|
316 | + $a = $j; |
|
317 | + $code .= $key; |
|
318 | + while (isset($key[++$j - $a - 1])) { |
|
319 | + $code[$j] = $key[$j - $a - 1]; |
|
320 | + } |
|
321 | + --$j; |
|
322 | + isset($s) && ($s = implode('', $s)) && $cc_on && $this->restoreCc($s); |
|
323 | + $strings[$key] = array('/'); |
|
324 | + $s = &$strings[$key]; |
|
325 | + } else { |
|
326 | + $code[++$j] = '/'; |
|
327 | + } |
|
328 | + |
|
329 | + break; |
|
330 | + } |
|
331 | + |
|
332 | + case "'": |
|
333 | + case '"': |
|
334 | + $instr = $f[$i]; |
|
335 | + $key = "//''\"\"".$K++.('!' == $instr ? ']' : "'"); |
|
336 | + $a = $j; |
|
337 | + $code .= $key; |
|
338 | + while (isset($key[++$j - $a - 1])) { |
|
339 | + $code[$j] = $key[$j - $a - 1]; |
|
340 | + } |
|
341 | + --$j; |
|
342 | + isset($s) && ($s = implode('', $s)) && $cc_on && $this->restoreCc($s); |
|
343 | + $strings[$key] = array(); |
|
344 | + $s = &$strings[$key]; |
|
345 | + '!' == $instr && $s[] = "\r/*!"; |
|
346 | + |
|
347 | + break; |
|
348 | + |
|
349 | + case "\n": |
|
350 | + if ($j > 3) { |
|
351 | + if (' ' == $code[$j] || "\x7F" == $code[$j]) { |
|
352 | + --$j; |
|
353 | + } |
|
354 | + |
|
355 | + $code[++$j] = |
|
356 | + false !== strpos('kend+-', $code[$j - 1]) |
|
357 | + && preg_match( |
|
358 | + "'(?:\+\+|--|(?<![\$.a-zA-Z0-9_])(break|continue|return|yield[ \x7F]?\*?))[ \x7F]?$'", |
|
359 | + substr($code, $j - 8, 9) |
|
360 | + ) |
|
361 | + ? ';' : "\x7F"; |
|
362 | + |
|
363 | + break; |
|
364 | + } |
|
365 | + |
|
366 | + case "\t": $f[$i] = ' '; |
|
367 | + case ' ': |
|
368 | + if (!$j || ' ' == $code[$j] || "\x7F" == $code[$j]) { |
|
369 | + break; |
|
370 | + } |
|
371 | + |
|
372 | + default: |
|
373 | + $code[++$j] = $f[$i]; |
|
374 | + } |
|
375 | + } |
|
376 | + } |
|
377 | + |
|
378 | + isset($s) && ($s = implode('', $s)) && $cc_on && $this->restoreCc($s); |
|
379 | + unset($s); |
|
380 | + |
|
381 | + $code = substr($code, 0, $j + 1); |
|
382 | + $cc_on && $this->restoreCc($code, false); |
|
383 | + |
|
384 | + // Protect wanted spaces and remove unwanted ones |
|
385 | + $code = strtr($code, "\x7F", ' '); |
|
386 | + $code = str_replace('- -', "-\x7F-", $code); |
|
387 | + $code = str_replace('+ +', "+\x7F+", $code); |
|
388 | + $code = preg_replace("'(\d)\s+\.\s*([a-zA-Z\$_[(])'", "$1\x7F.$2", $code); |
|
389 | + $code = preg_replace("# ([-!%&;<=>~:.^+|,()*?[\]{}/']+)#", '$1', $code); |
|
390 | + $code = preg_replace("#([-!%&;<=>~:.^+|,()*?[\]{}/]+) #", '$1', $code); |
|
391 | + $cc_on && $code = preg_replace_callback("'//[^\'].*?@#3'", function ($m) { return strtr($m[0], ' ', "\x7F"); }, $code); |
|
392 | + |
|
393 | + // Replace new Array/Object by []/{} |
|
394 | + false !== strpos($code, 'new Array') && $code = preg_replace("'new Array(?:\(\)|([;\])},:]))'", '[]$1', $code); |
|
395 | + false !== strpos($code, 'new Object') && $code = preg_replace("'new Object(?:\(\)|([;\])},:]))'", '{}$1', $code); |
|
396 | + |
|
397 | + // Add missing semi-colons after curly braces |
|
398 | + // This adds more semi-colons than strictly needed, |
|
399 | + // but it seems that later gzipping is favorable to the repetition of "};" |
|
400 | + $code = preg_replace("'\}(?![:,;.()\[\]}\|&]|(else|catch|finally|while)[^\$.a-zA-Z0-9_])'", '};', $code); |
|
401 | + |
|
402 | + // Tag possible empty instruction for easy detection |
|
403 | + $code = preg_replace("'(?<![\$.a-zA-Z0-9_])if\('", '1#(', $code); |
|
404 | + $code = preg_replace("'(?<![\$.a-zA-Z0-9_])for\('", '2#(', $code); |
|
405 | + $code = preg_replace("'(?<![\$.a-zA-Z0-9_])while\('", '3#(', $code); |
|
406 | + |
|
407 | + $forPool = array(); |
|
408 | + $instrPool = array(); |
|
409 | + $s = 0; |
|
410 | + |
|
411 | + $f = array(); |
|
412 | + $j = -1; |
|
413 | + |
|
414 | + // Remove as much semi-colon as possible |
|
415 | + $len = strlen($code); |
|
416 | + for ($i = 0; $i < $len; ++$i) { |
|
417 | + switch ($code[$i]) { |
|
418 | + case '(': |
|
419 | + if ($j >= 0 && "\n" == $f[$j]) { |
|
420 | + $f[$j] = ';'; |
|
421 | + } |
|
422 | + |
|
423 | + ++$s; |
|
424 | + |
|
425 | + if ($i && '#' == $code[$i - 1]) { |
|
426 | + $instrPool[$s - 1] = 1; |
|
427 | + if ('2' == $code[$i - 2]) { |
|
428 | + $forPool[$s] = 1; |
|
429 | + } |
|
430 | + } |
|
431 | + |
|
432 | + $f[++$j] = '('; |
|
433 | + break; |
|
434 | + |
|
435 | + case ']': |
|
436 | + case ')': |
|
437 | + if ($i + 1 < $len && !isset($forPool[$s]) && !isset($instrPool[$s - 1]) && preg_match("'[a-zA-Z0-9_\$]'", $code[$i + 1])) { |
|
438 | + $f[$j] .= $code[$i]; |
|
439 | + $f[++$j] = "\n"; |
|
440 | + } else { |
|
441 | + $f[++$j] = $code[$i]; |
|
442 | + } |
|
443 | + |
|
444 | + if (')' == $code[$i]) { |
|
445 | + unset($forPool[$s]); |
|
446 | + --$s; |
|
447 | + } |
|
448 | + |
|
449 | + continue 2; |
|
450 | + |
|
451 | + case '}': |
|
452 | + if ("\n" == $f[$j]) { |
|
453 | + $f[$j] = '}'; |
|
454 | + } else { |
|
455 | + $f[++$j] = '}'; |
|
456 | + } |
|
457 | + break; |
|
458 | + |
|
459 | + case ';': |
|
460 | + if (isset($forPool[$s]) || isset($instrPool[$s])) { |
|
461 | + $f[++$j] = ';'; |
|
462 | + } elseif ($j >= 0 && "\n" != $f[$j] && ';' != $f[$j]) { |
|
463 | + $f[++$j] = "\n"; |
|
464 | + } |
|
465 | + |
|
466 | + break; |
|
467 | + |
|
468 | + case '#': |
|
469 | + switch ($f[$j]) { |
|
470 | + case '1': $f[$j] = 'if'; break 2; |
|
471 | + case '2': $f[$j] = 'for'; break 2; |
|
472 | + case '3': $f[$j] = 'while'; break 2; |
|
473 | + } |
|
474 | + |
|
475 | + case '['; |
|
476 | + if ($j >= 0 && "\n" == $f[$j]) { |
|
477 | + $f[$j] = ';'; |
|
478 | + } |
|
479 | + |
|
480 | + default: $f[++$j] = $code[$i]; |
|
481 | + } |
|
482 | + |
|
483 | + unset($instrPool[$s]); |
|
484 | + } |
|
485 | + |
|
486 | + $f = implode('', $f); |
|
487 | + $cc_on && $f = str_replace('@#3', "\r", $f); |
|
488 | + |
|
489 | + // Fix "else ;" empty instructions |
|
490 | + $f = preg_replace("'(?<![\$.a-zA-Z0-9_])else\n'", "\n", $f); |
|
491 | + |
|
492 | + $r1 = array( // keywords with a direct object |
|
493 | + 'case','delete','do','else','function','in','instanceof','of','break', |
|
494 | + 'new','return','throw','typeof','var','void','yield','let','if', |
|
495 | + 'const','get','set', |
|
496 | + ); |
|
497 | + |
|
498 | + $r2 = array( // keywords with a subject |
|
499 | + 'in','instanceof','of', |
|
500 | + ); |
|
501 | + |
|
502 | + // Fix missing semi-colons |
|
503 | + $f = preg_replace("'(?<!(?<![a-zA-Z0-9_\$])".implode(')(?<!(?<![a-zA-Z0-9_\$])', $r1).') (?!('.implode('|', $r2).")(?![a-zA-Z0-9_\$]))'", "\n", $f); |
|
504 | + $f = preg_replace("'(?<!(?<![a-zA-Z0-9_\$])do)(?<!(?<![a-zA-Z0-9_\$])else) if\('", "\nif(", $f); |
|
505 | + $f = preg_replace("'(?<=--|\+\+)(?<![a-zA-Z0-9_\$])(".implode('|', $r1).")(?![a-zA-Z0-9_\$])'", "\n$1", $f); |
|
506 | + $f = preg_replace("'(?<![a-zA-Z0-9_\$])for\neach\('", 'for each(', $f); |
|
507 | + $f = preg_replace("'(?<![a-zA-Z0-9_\$])\n(".implode('|', $r2).")(?![a-zA-Z0-9_\$])'", '$1', $f); |
|
508 | + |
|
509 | + // Merge strings |
|
510 | + if ($q["'"] > $q['"']) { |
|
511 | + $q = array($q[1], $q[0]); |
|
512 | + } |
|
513 | + $f = preg_replace("#//''\"\"[0-9]+'#", $q[0].'$0'.$q[0], $f); |
|
514 | + strpos($f, $q[0].'+'.$q[0]) && $f = str_replace($q[0].'+'.$q[0], '', $f); |
|
515 | + $len = count($strings); |
|
516 | + foreach ($strings as $r1 => &$r2) { |
|
517 | + $r2 = "/'" == substr($r1, -2) |
|
518 | + ? str_replace(array("\\'", '\\"'), array("'", '"'), $r2) |
|
519 | + : str_replace('\\'.$q[1], $q[1], $r2); |
|
520 | + } |
|
521 | + |
|
522 | + // Restore wanted spaces |
|
523 | + $f = strtr($f, "\x7F", ' '); |
|
524 | + |
|
525 | + return array($f, $strings); |
|
526 | + } |
|
527 | + |
|
528 | + protected function extractClosures($code) |
|
529 | + { |
|
530 | + $code = ';'.$code; |
|
531 | + |
|
532 | + $this->argFreq[-1] += substr_count($code, '}catch('); |
|
533 | + |
|
534 | + if ($this->argFreq[-1]) { |
|
535 | + // Special catch scope handling |
|
536 | + |
|
537 | + // FIXME: this implementation doesn't work with nested catch scopes who need |
|
538 | + // access to their parent's caught variable (but who needs that?). |
|
539 | + |
|
540 | + $f = preg_split("@}catch\(({$this->varRx})@", $code, -1, PREG_SPLIT_DELIM_CAPTURE); |
|
541 | + |
|
542 | + $code = 'catch$scope$var'.mt_rand(); |
|
543 | + $this->specialVarRx = $this->specialVarRx ? '(?:'.$this->specialVarRx.'|'.preg_quote($code).')' : preg_quote($code); |
|
544 | + $i = count($f) - 1; |
|
545 | + |
|
546 | + while ($i) { |
|
547 | + $c = 1; |
|
548 | + $j = 0; |
|
549 | + $l = strlen($f[$i]); |
|
550 | + |
|
551 | + while ($c && $j < $l) { |
|
552 | + $s = $f[$i][$j++]; |
|
553 | + $c += '(' == $s ? 1 : (')' == $s ? -1 : 0); |
|
554 | + } |
|
555 | + |
|
556 | + if (!$c) { |
|
557 | + do { |
|
558 | + $s = $f[$i][$j++]; |
|
559 | + $c += '{' == $s ? 1 : ('}' == $s ? -1 : 0); |
|
560 | + } while ($c && $j < $l); |
|
561 | + } |
|
562 | + |
|
563 | + $c = preg_quote($f[$i - 1], '#'); |
|
564 | + $f[$i - 2] .= '}catch('.preg_replace("#([.,{]?)(?<![a-zA-Z0-9_\$@]){$c}\\b#", '$1'.$code, $f[$i - 1].substr($f[$i], 0, $j)).substr($f[$i], $j); |
|
565 | + |
|
566 | + unset($f[$i--], $f[$i--]); |
|
567 | + } |
|
568 | + |
|
569 | + $code = $f[0]; |
|
570 | + } |
|
571 | + |
|
572 | + $f = preg_split("'(?<![a-zA-Z0-9_\$])((?:function[ (]|get |set ).*?\{)'", $code, -1, PREG_SPLIT_DELIM_CAPTURE); |
|
573 | + $i = count($f) - 1; |
|
574 | + $closures = array(); |
|
575 | + |
|
576 | + while ($i) { |
|
577 | + $c = 1; |
|
578 | + $j = 0; |
|
579 | + $l = strlen($f[$i]); |
|
580 | + |
|
581 | + while ($c && $j < $l) { |
|
582 | + $s = $f[$i][$j++]; |
|
583 | + $c += '{' == $s ? 1 : ('}' == $s ? -1 : 0); |
|
584 | + } |
|
585 | + |
|
586 | + switch (substr($f[$i - 2], -1)) { |
|
587 | + default: |
|
588 | + if (false !== $c = strpos($f[$i - 1], ' ', 8)) { |
|
589 | + break; |
|
590 | + } |
|
591 | + case false: case "\n": case ';': case '{': case '}': case ')': case ']': |
|
592 | + $c = strpos($f[$i - 1], '(', 4); |
|
593 | + } |
|
594 | + |
|
595 | + $l = "//''\"\"#$i'"; |
|
596 | + $code = substr($f[$i - 1], $c); |
|
597 | + $closures[$l] = $code.substr($f[$i], 0, $j); |
|
598 | + $f[$i - 2] .= substr($f[$i - 1], 0, $c).$l.substr($f[$i], $j); |
|
599 | + |
|
600 | + if ('(){' !== $code) { |
|
601 | + $j = substr_count($code, ','); |
|
602 | + do { |
|
603 | + isset($this->argFreq[$j]) ? ++$this->argFreq[$j] : $this->argFreq[$j] = 1; |
|
604 | + } while ($j--); |
|
605 | + } |
|
606 | + |
|
607 | + $i -= 2; |
|
608 | + } |
|
609 | + |
|
610 | + return array($f[0], $closures); |
|
611 | + } |
|
612 | + |
|
613 | + protected function makeVars($closure, &$tree, $key) |
|
614 | + { |
|
615 | + $tree['code'] = &$closure; |
|
616 | + $tree['nfe'] = false; |
|
617 | + $tree['used'] = array(); |
|
618 | + $tree['local'] = array(); |
|
619 | + |
|
620 | + // Replace multiple "var" declarations by a single one |
|
621 | + $closure = preg_replace_callback("'(?<=[\n\{\}])var [^\n\{\};]+(?:\nvar [^\n\{\};]+)+'", array($this, 'mergeVarDeclarations'), $closure); |
|
622 | + |
|
623 | + // Get all local vars (functions, arguments and "var" prefixed) |
|
624 | + |
|
625 | + $vars = &$tree['local']; |
|
626 | + |
|
627 | + if (preg_match("'^( [^(]*)?\((.*?)\)\{'", $closure, $v)) { |
|
628 | + if ($v[1]) { |
|
629 | + $vars[$tree['nfe'] = substr($v[1], 1)] = -1; |
|
630 | + $tree['parent']['local'][';'.$key] = &$vars[$tree['nfe']]; |
|
631 | + } |
|
632 | + |
|
633 | + if ($v[2]) { |
|
634 | + $i = 0; |
|
635 | + $v = explode(',', $v[2]); |
|
636 | + foreach ($v as $w) { |
|
637 | + $vars[$w] = $this->argFreq[$i++] - 1; // Give a bonus to argument variables |
|
638 | + } |
|
639 | + } |
|
640 | + } |
|
641 | + |
|
642 | + $v = preg_split("'(?<![\$.a-zA-Z0-9_])var '", $closure); |
|
643 | + if ($i = count($v) - 1) { |
|
644 | + $w = array(); |
|
645 | + |
|
646 | + while ($i) { |
|
647 | + $j = $c = 0; |
|
648 | + $l = strlen($v[$i]); |
|
649 | + |
|
650 | + while ($j < $l) { |
|
651 | + switch ($v[$i][$j]) { |
|
652 | + case '(': case '[': case '{': |
|
653 | + ++$c; |
|
654 | + break; |
|
655 | + |
|
656 | + case ')': case ']': case '}': |
|
657 | + if ($c-- <= 0) { |
|
658 | + break 2; |
|
659 | + } |
|
660 | + break; |
|
661 | + |
|
662 | + case ';': case "\n": |
|
663 | + if (!$c) { |
|
664 | + break 2; |
|
665 | + } |
|
666 | + |
|
667 | + default: |
|
668 | + $c || $w[] = $v[$i][$j]; |
|
669 | + } |
|
670 | + |
|
671 | + ++$j; |
|
672 | + } |
|
673 | + |
|
674 | + $w[] = ','; |
|
675 | + --$i; |
|
676 | + } |
|
677 | + |
|
678 | + $v = explode(',', implode('', $w)); |
|
679 | + foreach ($v as $w) { |
|
680 | + if (preg_match("'^{$this->varRx}'", $w, $v)) { |
|
681 | + isset($vars[$v[0]]) || $vars[$v[0]] = 0; |
|
682 | + } |
|
683 | + } |
|
684 | + } |
|
685 | + |
|
686 | + if (preg_match_all("@function ({$this->varRx})//''\"\"#@", $closure, $v)) { |
|
687 | + foreach ($v[1] as $w) { |
|
688 | + isset($vars[$w]) || $vars[$w] = 0; |
|
689 | + } |
|
690 | + } |
|
691 | + |
|
692 | + if ($this->argFreq[-1] && preg_match_all("@}catch\(({$this->varRx})@", $closure, $v)) { |
|
693 | + $v[0] = array(); |
|
694 | + foreach ($v[1] as $w) { |
|
695 | + isset($v[0][$w]) ? ++$v[0][$w] : $v[0][$w] = 1; |
|
696 | + } |
|
697 | + foreach ($v[0] as $w => $v) { |
|
698 | + $vars[$w] = $this->argFreq[-1] - $v; |
|
699 | + } |
|
700 | + } |
|
701 | + |
|
702 | + // Get all used vars, local and non-local |
|
703 | + |
|
704 | + $vars = &$tree['used']; |
|
705 | + |
|
706 | + if (preg_match_all("#([.,{]?(?:[gs]et )?)(?<![a-zA-Z0-9_\$])({$this->varRx})(:?)#", $closure, $w, PREG_SET_ORDER)) { |
|
707 | + foreach ($w as $k) { |
|
708 | + if (isset($k[1][0]) && (',' === $k[1][0] || '{' === $k[1][0])) { |
|
709 | + if (':' === $k[3]) { |
|
710 | + $k = '.'.$k[2]; |
|
711 | + } elseif ('get ' === substr($k[1], 1, 4) || 'set ' === substr($k[1], 1, 4)) { |
|
712 | + ++$this->charFreq[ord($k[1][1])]; // "g" or "s" |
|
713 | + ++$this->charFreq[101]; // "e" |
|
714 | + ++$this->charFreq[116]; // "t" |
|
715 | + $k = '.'.$k[2]; |
|
716 | + } else { |
|
717 | + $k = $k[2]; |
|
718 | + } |
|
719 | + } else { |
|
720 | + $k = $k[1].$k[2]; |
|
721 | + } |
|
722 | + |
|
723 | + isset($vars[$k]) ? ++$vars[$k] : $vars[$k] = 1; |
|
724 | + } |
|
725 | + } |
|
726 | + |
|
727 | + if (preg_match_all("#//''\"\"[0-9]+(?:['!]|/')#", $closure, $w)) { |
|
728 | + foreach ($w[0] as $a) { |
|
729 | + $v = "'" === substr($a, -1) && "/'" !== substr($a, -2) && $this->specialVarRx |
|
730 | + ? preg_split("#([.,{]?(?:[gs]et )?(?<![a-zA-Z0-9_\$@]){$this->specialVarRx}:?)#", $this->strings[$a], -1, PREG_SPLIT_DELIM_CAPTURE) |
|
731 | + : array($this->strings[$a]); |
|
732 | + $a = count($v); |
|
733 | + |
|
734 | + for ($i = 0; $i < $a; ++$i) { |
|
735 | + $k = $v[$i]; |
|
736 | + |
|
737 | + if (1 === $i % 2) { |
|
738 | + if (',' === $k[0] || '{' === $k[0]) { |
|
739 | + if (':' === substr($k, -1)) { |
|
740 | + $k = '.'.substr($k, 1, -1); |
|
741 | + } elseif ('get ' === substr($k, 1, 4) || 'set ' === substr($k, 1, 4)) { |
|
742 | + ++$this->charFreq[ord($k[1])]; // "g" or "s" |
|
743 | + ++$this->charFreq[101]; // "e" |
|
744 | + ++$this->charFreq[116]; // "t" |
|
745 | + $k = '.'.substr($k, 5); |
|
746 | + } else { |
|
747 | + $k = substr($k, 1); |
|
748 | + } |
|
749 | + } elseif (':' === substr($k, -1)) { |
|
750 | + $k = substr($k, 0, -1); |
|
751 | + } |
|
752 | + |
|
753 | + $w = &$tree; |
|
754 | + |
|
755 | + while (isset($w['parent']) && !(isset($w['used'][$k]) || isset($w['local'][$k]))) { |
|
756 | + $w = &$w['parent']; |
|
757 | + } |
|
758 | + |
|
759 | + (isset($w['used'][$k]) || isset($w['local'][$k])) && (isset($vars[$k]) ? ++$vars[$k] : $vars[$k] = 1); |
|
760 | + |
|
761 | + unset($w); |
|
762 | + } |
|
763 | + |
|
764 | + if (0 === $i % 2 || !isset($vars[$k])) { |
|
765 | + foreach (count_chars($v[$i], 1) as $k => $w) { |
|
766 | + $this->charFreq[$k] += $w; |
|
767 | + } |
|
768 | + } |
|
769 | + } |
|
770 | + } |
|
771 | + } |
|
772 | + |
|
773 | + // Propagate the usage number to parents |
|
774 | + |
|
775 | + foreach ($vars as $w => $a) { |
|
776 | + $k = &$tree; |
|
777 | + $chain = array(); |
|
778 | + do { |
|
779 | + $vars = &$k['local']; |
|
780 | + $chain[] = &$k; |
|
781 | + if (isset($vars[$w])) { |
|
782 | + unset($k['used'][$w]); |
|
783 | + if (isset($vars[$w])) { |
|
784 | + $vars[$w] += $a; |
|
785 | + } else { |
|
786 | + $vars[$w] = $a; |
|
787 | + } |
|
788 | + $a = false; |
|
789 | + break; |
|
790 | + } |
|
791 | + } while ($k['parent'] && $k = &$k['parent']); |
|
792 | + |
|
793 | + if ($a && !$k['parent']) { |
|
794 | + if (isset($vars[$w])) { |
|
795 | + $vars[$w] += $a; |
|
796 | + } else { |
|
797 | + $vars[$w] = $a; |
|
798 | + } |
|
799 | + } |
|
800 | + |
|
801 | + if (isset($tree['used'][$w]) && isset($vars[$w])) { |
|
802 | + foreach ($chain as &$b) { |
|
803 | + isset($b['local'][$w]) || $b['used'][$w] = &$vars[$w]; |
|
804 | + } |
|
805 | + } |
|
806 | + } |
|
807 | + |
|
808 | + // Analyse childs |
|
809 | + |
|
810 | + $tree['childs'] = array(); |
|
811 | + $vars = &$tree['childs']; |
|
812 | + |
|
813 | + if (preg_match_all("@//''\"\"#[0-9]+'@", $closure, $w)) { |
|
814 | + foreach ($w[0] as $a) { |
|
815 | + $vars[$a] = array('parent' => &$tree); |
|
816 | + $this->makeVars($this->closures[$a], $vars[$a], $a); |
|
817 | + } |
|
818 | + } |
|
819 | + } |
|
820 | + |
|
821 | + protected function mergeVarDeclarations($m) |
|
822 | + { |
|
823 | + return str_replace("\nvar ", ',', $m[0]); |
|
824 | + } |
|
825 | + |
|
826 | + protected function renameVars(&$tree, $root) |
|
827 | + { |
|
828 | + if ($root) { |
|
829 | + $tree['local'] += $tree['used']; |
|
830 | + $tree['used'] = array(); |
|
831 | + |
|
832 | + foreach ($tree['local'] as $k => $v) { |
|
833 | + if ('.' == $k[0]) { |
|
834 | + $k = substr($k, 1); |
|
835 | + } |
|
836 | + |
|
837 | + if ('true' === $k) { |
|
838 | + $this->charFreq[48] += $v; |
|
839 | + } elseif ('false' === $k) { |
|
840 | + $this->charFreq[49] += $v; |
|
841 | + } elseif (!$this->specialVarRx || !preg_match("#^{$this->specialVarRx}$#", $k)) { |
|
842 | + foreach (count_chars($k, 1) as $k => $w) { |
|
843 | + $this->charFreq[$k] += $w * $v; |
|
844 | + } |
|
845 | + } elseif (2 == strlen($k)) { |
|
846 | + $tree['used'][] = $k[1]; |
|
847 | + } |
|
848 | + } |
|
849 | + |
|
850 | + $this->charFreq = $this->rsort($this->charFreq); |
|
851 | + |
|
852 | + $this->str0 = ''; |
|
853 | + $this->str1 = ''; |
|
854 | + |
|
855 | + foreach ($this->charFreq as $k => $v) { |
|
856 | + if (!$v) { |
|
857 | + break; |
|
858 | + } |
|
859 | + |
|
860 | + $v = chr($k); |
|
861 | + |
|
862 | + if ((64 < $k && $k < 91) || (96 < $k && $k < 123)) { // A-Z a-z |
|
863 | + $this->str0 .= $v; |
|
864 | + $this->str1 .= $v; |
|
865 | + } elseif (47 < $k && $k < 58) { // 0-9 |
|
866 | + $this->str1 .= $v; |
|
867 | + } |
|
868 | + } |
|
869 | + |
|
870 | + if ('' === $this->str0) { |
|
871 | + $this->str0 = 'claspemitdbfrugnjvhowkxqyzCLASPEMITDBFRUGNJVHOWKXQYZ'; |
|
872 | + $this->str1 = $this->str0.'0123456789'; |
|
873 | + } |
|
874 | + |
|
875 | + foreach ($tree['local'] as $var => $root) { |
|
876 | + if ('.' != substr($var, 0, 1) && isset($tree['local'][".{$var}"])) { |
|
877 | + $tree['local'][$var] += $tree['local'][".{$var}"]; |
|
878 | + } |
|
879 | + } |
|
880 | + |
|
881 | + foreach ($tree['local'] as $var => $root) { |
|
882 | + if ('.' == substr($var, 0, 1) && isset($tree['local'][substr($var, 1)])) { |
|
883 | + $tree['local'][$var] = $tree['local'][substr($var, 1)]; |
|
884 | + } |
|
885 | + } |
|
886 | + |
|
887 | + $tree['local'] = $this->rsort($tree['local']); |
|
888 | + |
|
889 | + foreach ($tree['local'] as $var => $root) { |
|
890 | + switch (substr($var, 0, 1)) { |
|
891 | + case '.': |
|
892 | + if (!isset($tree['local'][substr($var, 1)])) { |
|
893 | + $tree['local'][$var] = '#'.($this->specialVarRx && 3 < strlen($var) && preg_match("'^\.{$this->specialVarRx}$'", $var) ? $this->getNextName($tree).'$' : substr($var, 1)); |
|
894 | + } |
|
895 | + break; |
|
896 | + |
|
897 | + case ';': $tree['local'][$var] = 0 === $root ? '' : $this->getNextName($tree); |
|
898 | + case '#': break; |
|
899 | + |
|
900 | + default: |
|
901 | + $root = $this->specialVarRx && 2 < strlen($var) && preg_match("'^{$this->specialVarRx}$'", $var) ? $this->getNextName($tree).'$' : $var; |
|
902 | + $tree['local'][$var] = $root; |
|
903 | + if (isset($tree['local'][".{$var}"])) { |
|
904 | + $tree['local'][".{$var}"] = '#'.$root; |
|
905 | + } |
|
906 | + } |
|
907 | + } |
|
908 | + |
|
909 | + foreach ($tree['local'] as $var => $root) { |
|
910 | + $tree['local'][$var] = preg_replace("'^#'", '.', $tree['local'][$var]); |
|
911 | + } |
|
912 | + } else { |
|
913 | + $tree['local'] = $this->rsort($tree['local']); |
|
914 | + if (false !== $tree['nfe']) { |
|
915 | + $tree['used'][] = $tree['local'][$tree['nfe']]; |
|
916 | + } |
|
917 | + |
|
918 | + foreach ($tree['local'] as $var => $root) { |
|
919 | + if ($tree['nfe'] !== $var) { |
|
920 | + $tree['local'][$var] = 0 === $root ? '' : $this->getNextName($tree); |
|
921 | + } |
|
922 | + } |
|
923 | + } |
|
924 | + |
|
925 | + $this->local_tree = &$tree['local']; |
|
926 | + $this->used_tree = &$tree['used']; |
|
927 | + |
|
928 | + $tree['code'] = preg_replace_callback("#[.,{ ]?(?:[gs]et )?(?<![a-zA-Z0-9_\$@]){$this->varRx}:?#", array($this, 'getNewName'), $tree['code']); |
|
929 | + |
|
930 | + if ($this->specialVarRx && preg_match_all("#//''\"\"[0-9]+'#", $tree['code'], $b)) { |
|
931 | + foreach ($b[0] as $a) { |
|
932 | + $this->strings[$a] = preg_replace_callback( |
|
933 | + "#[.,{]?(?:[gs]et )?(?<![a-zA-Z0-9_\$@]){$this->specialVarRx}:?#", |
|
934 | + array($this, 'getNewName'), |
|
935 | + $this->strings[$a] |
|
936 | + ); |
|
937 | + } |
|
938 | + } |
|
939 | + |
|
940 | + foreach ($tree['childs'] as $a => &$b) { |
|
941 | + $this->renameVars($b, false); |
|
942 | + $tree['code'] = str_replace($a, $b['code'], $tree['code']); |
|
943 | + unset($tree['childs'][$a]); |
|
944 | + } |
|
945 | + } |
|
946 | + |
|
947 | + protected function getNewName($m) |
|
948 | + { |
|
949 | + $m = $m[0]; |
|
950 | + |
|
951 | + $pre = '.' === $m[0] ? '.' : ''; |
|
952 | + $post = ''; |
|
953 | + |
|
954 | + if (',' === $m[0] || '{' === $m[0] || ' ' === $m[0]) { |
|
955 | + $pre = $m[0]; |
|
956 | + |
|
957 | + if (':' === substr($m, -1)) { |
|
958 | + $post = ':'; |
|
959 | + $m = (' ' !== $m[0] ? '.' : '').substr($m, 1, -1); |
|
960 | + } elseif ('get ' === substr($m, 1, 4) || 'set ' === substr($m, 1, 4)) { |
|
961 | + $pre .= substr($m, 1, 4); |
|
962 | + $m = '.'.substr($m, 5); |
|
963 | + } else { |
|
964 | + $m = substr($m, 1); |
|
965 | + } |
|
966 | + } elseif (':' === substr($m, -1)) { |
|
967 | + $post = ':'; |
|
968 | + $m = substr($m, 0, -1); |
|
969 | + } |
|
970 | + |
|
971 | + $post = (isset($this->reserved[$m]) |
|
972 | + ? ('true' === $m ? '!0' : ('false' === $m ? '!1' : $m)) |
|
973 | + : ( |
|
974 | + isset($this->local_tree[$m]) |
|
975 | + ? $this->local_tree[$m] |
|
976 | + : ( |
|
977 | + isset($this->used_tree[$m]) |
|
978 | + ? $this->used_tree[$m] |
|
979 | + : $m |
|
980 | + ) |
|
981 | + ) |
|
982 | + ).$post; |
|
983 | + |
|
984 | + return '' === $post ? '' : ($pre.('.' === $post[0] ? substr($post, 1) : $post)); |
|
985 | + } |
|
986 | + |
|
987 | + protected function getNextName(&$tree = array(), &$counter = false) |
|
988 | + { |
|
989 | + if (false === $counter) { |
|
990 | + $counter = &$tree['counter']; |
|
991 | + isset($counter) || $counter = -1; |
|
992 | + $exclude = array_flip($tree['used']); |
|
993 | + } else { |
|
994 | + $exclude = $tree; |
|
995 | + } |
|
996 | + |
|
997 | + ++$counter; |
|
998 | + |
|
999 | + $len0 = strlen($this->str0); |
|
1000 | + $len1 = strlen($this->str0); |
|
1001 | + |
|
1002 | + $name = $this->str0[$counter % $len0]; |
|
1003 | + |
|
1004 | + $i = intval($counter / $len0) - 1; |
|
1005 | + while ($i >= 0) { |
|
1006 | + $name .= $this->str1[ $i % $len1 ]; |
|
1007 | + $i = intval($i / $len1) - 1; |
|
1008 | + } |
|
1009 | + |
|
1010 | + return !(isset($this->reserved[$name]) || isset($exclude[$name])) ? $name : $this->getNextName($exclude, $counter); |
|
1011 | + } |
|
1012 | + |
|
1013 | + protected function restoreCc(&$s, $lf = true) |
|
1014 | + { |
|
1015 | + $lf && $s = str_replace('@#3', '', $s); |
|
1016 | + |
|
1017 | + $s = str_replace('@#1', '@*/', $s); |
|
1018 | + $s = str_replace('2#@', '//@', $s); |
|
1019 | + $s = str_replace('1#@', '/*@', $s); |
|
1020 | + $s = str_replace('##', '#', $s); |
|
1021 | + } |
|
1022 | + |
|
1023 | + private function rsort($array) |
|
1024 | + { |
|
1025 | + if (!$array) { |
|
1026 | + return $array; |
|
1027 | + } |
|
1028 | + |
|
1029 | + $i = 0; |
|
1030 | + $tuples = array(); |
|
1031 | + foreach ($array as $k => &$v) { |
|
1032 | + $tuples[] = array(++$i, $k, &$v); |
|
1033 | + } |
|
1034 | + |
|
1035 | + usort($tuples, function ($a, $b) { |
|
1036 | + if ($b[2] > $a[2]) { |
|
1037 | + return 1; |
|
1038 | + } |
|
1039 | + if ($b[2] < $a[2]) { |
|
1040 | + return -1; |
|
1041 | + } |
|
1042 | + if ($b[0] > $a[0]) { |
|
1043 | + return -1; |
|
1044 | + } |
|
1045 | + if ($b[0] < $a[0]) { |
|
1046 | + return 1; |
|
1047 | + } |
|
1048 | + |
|
1049 | + return 0; |
|
1050 | + }); |
|
1051 | + |
|
1052 | + $array = array(); |
|
1053 | + |
|
1054 | + foreach ($tuples as $t) { |
|
1055 | + $array[$t[1]] = &$t[2]; |
|
1056 | + } |
|
1057 | + |
|
1058 | + return $array; |
|
1059 | + } |
|
1060 | 1060 | } |
1061 | 1061 | |
1062 | 1062 | ?> |
@@ -78,17 +78,17 @@ discard block |
||
78 | 78 | $varRx = '(?:[a-zA-Z_$])[a-zA-Z0-9_$]*', |
79 | 79 | $reserved = array( |
80 | 80 | // Literals |
81 | - 'true','false','null', |
|
81 | + 'true', 'false', 'null', |
|
82 | 82 | // ES6 |
83 | - 'break','case','class','catch','const','continue','debugger','default','delete','do','else','export','extends','finally','for','function','if','import','in','instanceof','new','return','super','switch','this','throw','try','typeof','var','void','while','with','yield', |
|
83 | + 'break', 'case', 'class', 'catch', 'const', 'continue', 'debugger', 'default', 'delete', 'do', 'else', 'export', 'extends', 'finally', 'for', 'function', 'if', 'import', 'in', 'instanceof', 'new', 'return', 'super', 'switch', 'this', 'throw', 'try', 'typeof', 'var', 'void', 'while', 'with', 'yield', |
|
84 | 84 | // Future |
85 | 85 | 'enum', |
86 | 86 | // Strict mode |
87 | - 'implements','package','protected','static','let','interface','private','public', |
|
87 | + 'implements', 'package', 'protected', 'static', 'let', 'interface', 'private', 'public', |
|
88 | 88 | // Module |
89 | 89 | 'await', |
90 | 90 | // Older standards |
91 | - 'abstract','boolean','byte','char','double','final','float','goto','int','long','native','short','synchronized','throws','transient','volatile', |
|
91 | + 'abstract', 'boolean', 'byte', 'char', 'double', 'final', 'float', 'goto', 'int', 'long', 'native', 'short', 'synchronized', 'throws', 'transient', 'volatile', |
|
92 | 92 | ); |
93 | 93 | |
94 | 94 | public function __construct() |
@@ -246,7 +246,7 @@ discard block |
||
246 | 246 | |
247 | 247 | if ("\n" != $f[$i]) { |
248 | 248 | isset($q[$f[$i]]) && ++$q[$f[$i]]; |
249 | - $s[] = '\\'.$f[$i]; |
|
249 | + $s[] = '\\' . $f[$i]; |
|
250 | 250 | } |
251 | 251 | } elseif ('[' == $f[$i] && "/'" == $instr) { |
252 | 252 | $instr = '/['; |
@@ -256,7 +256,7 @@ discard block |
||
256 | 256 | $s[] = ']'; |
257 | 257 | } elseif ("'" == $f[$i] || '"' == $f[$i]) { |
258 | 258 | ++$q[$f[$i]]; |
259 | - $s[] = '\\'.$f[$i]; |
|
259 | + $s[] = '\\' . $f[$i]; |
|
260 | 260 | } else { |
261 | 261 | $s[] = $f[$i]; |
262 | 262 | } |
@@ -312,7 +312,7 @@ discard block |
||
312 | 312 | } |
313 | 313 | } |
314 | 314 | |
315 | - $key = "//''\"\"".$K++.$instr = "/'"; |
|
315 | + $key = "//''\"\"" . $K++ . $instr = "/'"; |
|
316 | 316 | $a = $j; |
317 | 317 | $code .= $key; |
318 | 318 | while (isset($key[++$j - $a - 1])) { |
@@ -332,7 +332,7 @@ discard block |
||
332 | 332 | case "'": |
333 | 333 | case '"': |
334 | 334 | $instr = $f[$i]; |
335 | - $key = "//''\"\"".$K++.('!' == $instr ? ']' : "'"); |
|
335 | + $key = "//''\"\"" . $K++ . ('!' == $instr ? ']' : "'"); |
|
336 | 336 | $a = $j; |
337 | 337 | $code .= $key; |
338 | 338 | while (isset($key[++$j - $a - 1])) { |
@@ -388,7 +388,7 @@ discard block |
||
388 | 388 | $code = preg_replace("'(\d)\s+\.\s*([a-zA-Z\$_[(])'", "$1\x7F.$2", $code); |
389 | 389 | $code = preg_replace("# ([-!%&;<=>~:.^+|,()*?[\]{}/']+)#", '$1', $code); |
390 | 390 | $code = preg_replace("#([-!%&;<=>~:.^+|,()*?[\]{}/]+) #", '$1', $code); |
391 | - $cc_on && $code = preg_replace_callback("'//[^\'].*?@#3'", function ($m) { return strtr($m[0], ' ', "\x7F"); }, $code); |
|
391 | + $cc_on && $code = preg_replace_callback("'//[^\'].*?@#3'", function($m) { return strtr($m[0], ' ', "\x7F"); }, $code); |
|
392 | 392 | |
393 | 393 | // Replace new Array/Object by []/{} |
394 | 394 | false !== strpos($code, 'new Array') && $code = preg_replace("'new Array(?:\(\)|([;\])},:]))'", '[]$1', $code); |
@@ -467,8 +467,8 @@ discard block |
||
467 | 467 | |
468 | 468 | case '#': |
469 | 469 | switch ($f[$j]) { |
470 | - case '1': $f[$j] = 'if'; break 2; |
|
471 | - case '2': $f[$j] = 'for'; break 2; |
|
470 | + case '1': $f[$j] = 'if'; break 2; |
|
471 | + case '2': $f[$j] = 'for'; break 2; |
|
472 | 472 | case '3': $f[$j] = 'while'; break 2; |
473 | 473 | } |
474 | 474 | |
@@ -490,33 +490,33 @@ discard block |
||
490 | 490 | $f = preg_replace("'(?<![\$.a-zA-Z0-9_])else\n'", "\n", $f); |
491 | 491 | |
492 | 492 | $r1 = array( // keywords with a direct object |
493 | - 'case','delete','do','else','function','in','instanceof','of','break', |
|
494 | - 'new','return','throw','typeof','var','void','yield','let','if', |
|
495 | - 'const','get','set', |
|
493 | + 'case', 'delete', 'do', 'else', 'function', 'in', 'instanceof', 'of', 'break', |
|
494 | + 'new', 'return', 'throw', 'typeof', 'var', 'void', 'yield', 'let', 'if', |
|
495 | + 'const', 'get', 'set', |
|
496 | 496 | ); |
497 | 497 | |
498 | 498 | $r2 = array( // keywords with a subject |
499 | - 'in','instanceof','of', |
|
499 | + 'in', 'instanceof', 'of', |
|
500 | 500 | ); |
501 | 501 | |
502 | 502 | // Fix missing semi-colons |
503 | - $f = preg_replace("'(?<!(?<![a-zA-Z0-9_\$])".implode(')(?<!(?<![a-zA-Z0-9_\$])', $r1).') (?!('.implode('|', $r2).")(?![a-zA-Z0-9_\$]))'", "\n", $f); |
|
503 | + $f = preg_replace("'(?<!(?<![a-zA-Z0-9_\$])" . implode(')(?<!(?<![a-zA-Z0-9_\$])', $r1) . ') (?!(' . implode('|', $r2) . ")(?![a-zA-Z0-9_\$]))'", "\n", $f); |
|
504 | 504 | $f = preg_replace("'(?<!(?<![a-zA-Z0-9_\$])do)(?<!(?<![a-zA-Z0-9_\$])else) if\('", "\nif(", $f); |
505 | - $f = preg_replace("'(?<=--|\+\+)(?<![a-zA-Z0-9_\$])(".implode('|', $r1).")(?![a-zA-Z0-9_\$])'", "\n$1", $f); |
|
505 | + $f = preg_replace("'(?<=--|\+\+)(?<![a-zA-Z0-9_\$])(" . implode('|', $r1) . ")(?![a-zA-Z0-9_\$])'", "\n$1", $f); |
|
506 | 506 | $f = preg_replace("'(?<![a-zA-Z0-9_\$])for\neach\('", 'for each(', $f); |
507 | - $f = preg_replace("'(?<![a-zA-Z0-9_\$])\n(".implode('|', $r2).")(?![a-zA-Z0-9_\$])'", '$1', $f); |
|
507 | + $f = preg_replace("'(?<![a-zA-Z0-9_\$])\n(" . implode('|', $r2) . ")(?![a-zA-Z0-9_\$])'", '$1', $f); |
|
508 | 508 | |
509 | 509 | // Merge strings |
510 | 510 | if ($q["'"] > $q['"']) { |
511 | 511 | $q = array($q[1], $q[0]); |
512 | 512 | } |
513 | - $f = preg_replace("#//''\"\"[0-9]+'#", $q[0].'$0'.$q[0], $f); |
|
514 | - strpos($f, $q[0].'+'.$q[0]) && $f = str_replace($q[0].'+'.$q[0], '', $f); |
|
513 | + $f = preg_replace("#//''\"\"[0-9]+'#", $q[0] . '$0' . $q[0], $f); |
|
514 | + strpos($f, $q[0] . '+' . $q[0]) && $f = str_replace($q[0] . '+' . $q[0], '', $f); |
|
515 | 515 | $len = count($strings); |
516 | 516 | foreach ($strings as $r1 => &$r2) { |
517 | 517 | $r2 = "/'" == substr($r1, -2) |
518 | 518 | ? str_replace(array("\\'", '\\"'), array("'", '"'), $r2) |
519 | - : str_replace('\\'.$q[1], $q[1], $r2); |
|
519 | + : str_replace('\\' . $q[1], $q[1], $r2); |
|
520 | 520 | } |
521 | 521 | |
522 | 522 | // Restore wanted spaces |
@@ -527,7 +527,7 @@ discard block |
||
527 | 527 | |
528 | 528 | protected function extractClosures($code) |
529 | 529 | { |
530 | - $code = ';'.$code; |
|
530 | + $code = ';' . $code; |
|
531 | 531 | |
532 | 532 | $this->argFreq[-1] += substr_count($code, '}catch('); |
533 | 533 | |
@@ -539,8 +539,8 @@ discard block |
||
539 | 539 | |
540 | 540 | $f = preg_split("@}catch\(({$this->varRx})@", $code, -1, PREG_SPLIT_DELIM_CAPTURE); |
541 | 541 | |
542 | - $code = 'catch$scope$var'.mt_rand(); |
|
543 | - $this->specialVarRx = $this->specialVarRx ? '(?:'.$this->specialVarRx.'|'.preg_quote($code).')' : preg_quote($code); |
|
542 | + $code = 'catch$scope$var' . mt_rand(); |
|
543 | + $this->specialVarRx = $this->specialVarRx ? '(?:' . $this->specialVarRx . '|' . preg_quote($code) . ')' : preg_quote($code); |
|
544 | 544 | $i = count($f) - 1; |
545 | 545 | |
546 | 546 | while ($i) { |
@@ -561,7 +561,7 @@ discard block |
||
561 | 561 | } |
562 | 562 | |
563 | 563 | $c = preg_quote($f[$i - 1], '#'); |
564 | - $f[$i - 2] .= '}catch('.preg_replace("#([.,{]?)(?<![a-zA-Z0-9_\$@]){$c}\\b#", '$1'.$code, $f[$i - 1].substr($f[$i], 0, $j)).substr($f[$i], $j); |
|
564 | + $f[$i - 2] .= '}catch(' . preg_replace("#([.,{]?)(?<![a-zA-Z0-9_\$@]){$c}\\b#", '$1' . $code, $f[$i - 1] . substr($f[$i], 0, $j)) . substr($f[$i], $j); |
|
565 | 565 | |
566 | 566 | unset($f[$i--], $f[$i--]); |
567 | 567 | } |
@@ -594,8 +594,8 @@ discard block |
||
594 | 594 | |
595 | 595 | $l = "//''\"\"#$i'"; |
596 | 596 | $code = substr($f[$i - 1], $c); |
597 | - $closures[$l] = $code.substr($f[$i], 0, $j); |
|
598 | - $f[$i - 2] .= substr($f[$i - 1], 0, $c).$l.substr($f[$i], $j); |
|
597 | + $closures[$l] = $code . substr($f[$i], 0, $j); |
|
598 | + $f[$i - 2] .= substr($f[$i - 1], 0, $c) . $l . substr($f[$i], $j); |
|
599 | 599 | |
600 | 600 | if ('(){' !== $code) { |
601 | 601 | $j = substr_count($code, ','); |
@@ -627,7 +627,7 @@ discard block |
||
627 | 627 | if (preg_match("'^( [^(]*)?\((.*?)\)\{'", $closure, $v)) { |
628 | 628 | if ($v[1]) { |
629 | 629 | $vars[$tree['nfe'] = substr($v[1], 1)] = -1; |
630 | - $tree['parent']['local'][';'.$key] = &$vars[$tree['nfe']]; |
|
630 | + $tree['parent']['local'][';' . $key] = &$vars[$tree['nfe']]; |
|
631 | 631 | } |
632 | 632 | |
633 | 633 | if ($v[2]) { |
@@ -707,17 +707,17 @@ discard block |
||
707 | 707 | foreach ($w as $k) { |
708 | 708 | if (isset($k[1][0]) && (',' === $k[1][0] || '{' === $k[1][0])) { |
709 | 709 | if (':' === $k[3]) { |
710 | - $k = '.'.$k[2]; |
|
710 | + $k = '.' . $k[2]; |
|
711 | 711 | } elseif ('get ' === substr($k[1], 1, 4) || 'set ' === substr($k[1], 1, 4)) { |
712 | 712 | ++$this->charFreq[ord($k[1][1])]; // "g" or "s" |
713 | 713 | ++$this->charFreq[101]; // "e" |
714 | 714 | ++$this->charFreq[116]; // "t" |
715 | - $k = '.'.$k[2]; |
|
715 | + $k = '.' . $k[2]; |
|
716 | 716 | } else { |
717 | 717 | $k = $k[2]; |
718 | 718 | } |
719 | 719 | } else { |
720 | - $k = $k[1].$k[2]; |
|
720 | + $k = $k[1] . $k[2]; |
|
721 | 721 | } |
722 | 722 | |
723 | 723 | isset($vars[$k]) ? ++$vars[$k] : $vars[$k] = 1; |
@@ -737,12 +737,12 @@ discard block |
||
737 | 737 | if (1 === $i % 2) { |
738 | 738 | if (',' === $k[0] || '{' === $k[0]) { |
739 | 739 | if (':' === substr($k, -1)) { |
740 | - $k = '.'.substr($k, 1, -1); |
|
740 | + $k = '.' . substr($k, 1, -1); |
|
741 | 741 | } elseif ('get ' === substr($k, 1, 4) || 'set ' === substr($k, 1, 4)) { |
742 | 742 | ++$this->charFreq[ord($k[1])]; // "g" or "s" |
743 | 743 | ++$this->charFreq[101]; // "e" |
744 | 744 | ++$this->charFreq[116]; // "t" |
745 | - $k = '.'.substr($k, 5); |
|
745 | + $k = '.' . substr($k, 5); |
|
746 | 746 | } else { |
747 | 747 | $k = substr($k, 1); |
748 | 748 | } |
@@ -869,7 +869,7 @@ discard block |
||
869 | 869 | |
870 | 870 | if ('' === $this->str0) { |
871 | 871 | $this->str0 = 'claspemitdbfrugnjvhowkxqyzCLASPEMITDBFRUGNJVHOWKXQYZ'; |
872 | - $this->str1 = $this->str0.'0123456789'; |
|
872 | + $this->str1 = $this->str0 . '0123456789'; |
|
873 | 873 | } |
874 | 874 | |
875 | 875 | foreach ($tree['local'] as $var => $root) { |
@@ -890,7 +890,7 @@ discard block |
||
890 | 890 | switch (substr($var, 0, 1)) { |
891 | 891 | case '.': |
892 | 892 | if (!isset($tree['local'][substr($var, 1)])) { |
893 | - $tree['local'][$var] = '#'.($this->specialVarRx && 3 < strlen($var) && preg_match("'^\.{$this->specialVarRx}$'", $var) ? $this->getNextName($tree).'$' : substr($var, 1)); |
|
893 | + $tree['local'][$var] = '#' . ($this->specialVarRx && 3 < strlen($var) && preg_match("'^\.{$this->specialVarRx}$'", $var) ? $this->getNextName($tree) . '$' : substr($var, 1)); |
|
894 | 894 | } |
895 | 895 | break; |
896 | 896 | |
@@ -898,10 +898,10 @@ discard block |
||
898 | 898 | case '#': break; |
899 | 899 | |
900 | 900 | default: |
901 | - $root = $this->specialVarRx && 2 < strlen($var) && preg_match("'^{$this->specialVarRx}$'", $var) ? $this->getNextName($tree).'$' : $var; |
|
901 | + $root = $this->specialVarRx && 2 < strlen($var) && preg_match("'^{$this->specialVarRx}$'", $var) ? $this->getNextName($tree) . '$' : $var; |
|
902 | 902 | $tree['local'][$var] = $root; |
903 | 903 | if (isset($tree['local'][".{$var}"])) { |
904 | - $tree['local'][".{$var}"] = '#'.$root; |
|
904 | + $tree['local'][".{$var}"] = '#' . $root; |
|
905 | 905 | } |
906 | 906 | } |
907 | 907 | } |
@@ -956,10 +956,10 @@ discard block |
||
956 | 956 | |
957 | 957 | if (':' === substr($m, -1)) { |
958 | 958 | $post = ':'; |
959 | - $m = (' ' !== $m[0] ? '.' : '').substr($m, 1, -1); |
|
959 | + $m = (' ' !== $m[0] ? '.' : '') . substr($m, 1, -1); |
|
960 | 960 | } elseif ('get ' === substr($m, 1, 4) || 'set ' === substr($m, 1, 4)) { |
961 | 961 | $pre .= substr($m, 1, 4); |
962 | - $m = '.'.substr($m, 5); |
|
962 | + $m = '.' . substr($m, 5); |
|
963 | 963 | } else { |
964 | 964 | $m = substr($m, 1); |
965 | 965 | } |
@@ -979,9 +979,9 @@ discard block |
||
979 | 979 | : $m |
980 | 980 | ) |
981 | 981 | ) |
982 | - ).$post; |
|
982 | + ) . $post; |
|
983 | 983 | |
984 | - return '' === $post ? '' : ($pre.('.' === $post[0] ? substr($post, 1) : $post)); |
|
984 | + return '' === $post ? '' : ($pre . ('.' === $post[0] ? substr($post, 1) : $post)); |
|
985 | 985 | } |
986 | 986 | |
987 | 987 | protected function getNextName(&$tree = array(), &$counter = false) |
@@ -1003,7 +1003,7 @@ discard block |
||
1003 | 1003 | |
1004 | 1004 | $i = intval($counter / $len0) - 1; |
1005 | 1005 | while ($i >= 0) { |
1006 | - $name .= $this->str1[ $i % $len1 ]; |
|
1006 | + $name .= $this->str1[$i % $len1]; |
|
1007 | 1007 | $i = intval($i / $len1) - 1; |
1008 | 1008 | } |
1009 | 1009 | |
@@ -1032,7 +1032,7 @@ discard block |
||
1032 | 1032 | $tuples[] = array(++$i, $k, &$v); |
1033 | 1033 | } |
1034 | 1034 | |
1035 | - usort($tuples, function ($a, $b) { |
|
1035 | + usort($tuples, function($a, $b) { |
|
1036 | 1036 | if ($b[2] > $a[2]) { |
1037 | 1037 | return 1; |
1038 | 1038 | } |
@@ -23,179 +23,179 @@ |
||
23 | 23 | |
24 | 24 | class Lang { |
25 | 25 | |
26 | - protected static $supported_languages = array(); |
|
27 | - protected static $initialized = false; |
|
26 | + protected static $supported_languages = array(); |
|
27 | + protected static $initialized = false; |
|
28 | 28 | |
29 | - //https://paulund.co.uk/auto-detect-browser-language-in-php |
|
29 | + //https://paulund.co.uk/auto-detect-browser-language-in-php |
|
30 | 30 | |
31 | - public static function getPrefLangToken () : string { |
|
32 | - if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { |
|
33 | - $_SERVER['HTTP_ACCEPT_LANGUAGE'] = Settings::get("default_lang"); |
|
34 | - } |
|
31 | + public static function getPrefLangToken () : string { |
|
32 | + if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { |
|
33 | + $_SERVER['HTTP_ACCEPT_LANGUAGE'] = Settings::get("default_lang"); |
|
34 | + } |
|
35 | 35 | |
36 | - return substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); |
|
37 | - } |
|
36 | + return substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); |
|
37 | + } |
|
38 | 38 | |
39 | - public static function getLangToken (array $supported_lang_tokens) : string { |
|
40 | - //http://php.net/manual/fa/function.http-negotiate-language.php |
|
39 | + public static function getLangToken (array $supported_lang_tokens) : string { |
|
40 | + //http://php.net/manual/fa/function.http-negotiate-language.php |
|
41 | 41 | |
42 | - //https://stackoverflow.com/questions/6038236/using-the-php-http-accept-language-server-variable |
|
42 | + //https://stackoverflow.com/questions/6038236/using-the-php-http-accept-language-server-variable |
|
43 | 43 | |
44 | - //https://stackoverflow.com/questions/3770513/detect-browser-language-in-php |
|
44 | + //https://stackoverflow.com/questions/3770513/detect-browser-language-in-php |
|
45 | 45 | |
46 | - return self::prefered_language($supported_lang_tokens);//http_negotiate_language($supported_lang_tokens); |
|
47 | - } |
|
46 | + return self::prefered_language($supported_lang_tokens);//http_negotiate_language($supported_lang_tokens); |
|
47 | + } |
|
48 | 48 | |
49 | - public static function loadSupportedLangs () { |
|
50 | - if (Cache::contains("supported-languages", "list")) { |
|
51 | - self::$supported_languages = Cache::get("supported-languages", "list"); |
|
52 | - } else { |
|
53 | - $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}supported_languages`; "); |
|
49 | + public static function loadSupportedLangs () { |
|
50 | + if (Cache::contains("supported-languages", "list")) { |
|
51 | + self::$supported_languages = Cache::get("supported-languages", "list"); |
|
52 | + } else { |
|
53 | + $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}supported_languages`; "); |
|
54 | 54 | |
55 | - $array = array(); |
|
55 | + $array = array(); |
|
56 | 56 | |
57 | - foreach ($rows as $row) { |
|
58 | - $array[$row['lang_token']] = $row; |
|
59 | - } |
|
57 | + foreach ($rows as $row) { |
|
58 | + $array[$row['lang_token']] = $row; |
|
59 | + } |
|
60 | 60 | |
61 | - //cache values |
|
62 | - Cache::put("supported-languages", "list", $array); |
|
61 | + //cache values |
|
62 | + Cache::put("supported-languages", "list", $array); |
|
63 | 63 | |
64 | - self::$supported_languages = $array; |
|
65 | - } |
|
64 | + self::$supported_languages = $array; |
|
65 | + } |
|
66 | 66 | |
67 | - self::$initialized = true; |
|
68 | - } |
|
67 | + self::$initialized = true; |
|
68 | + } |
|
69 | 69 | |
70 | - protected static function loadIfAbsent () { |
|
71 | - if (!self::$initialized) { |
|
72 | - self::loadSupportedLangs(); |
|
73 | - } |
|
74 | - } |
|
70 | + protected static function loadIfAbsent () { |
|
71 | + if (!self::$initialized) { |
|
72 | + self::loadSupportedLangs(); |
|
73 | + } |
|
74 | + } |
|
75 | 75 | |
76 | - public static function listSupportedLangTokens () { |
|
77 | - //load tokens, if not initialized |
|
78 | - self::loadIfAbsent(); |
|
76 | + public static function listSupportedLangTokens () { |
|
77 | + //load tokens, if not initialized |
|
78 | + self::loadIfAbsent(); |
|
79 | 79 | |
80 | - $keys = array_keys(self::$supported_languages); |
|
80 | + $keys = array_keys(self::$supported_languages); |
|
81 | 81 | |
82 | - //get default language |
|
83 | - $default_lang = Settings::get("default_lang"); |
|
82 | + //get default language |
|
83 | + $default_lang = Settings::get("default_lang"); |
|
84 | 84 | |
85 | - if (!in_array($default_lang, $keys)) { |
|
86 | - throw new IllegalStateException("default language (in global settings) isnt a supported language"); |
|
87 | - } |
|
85 | + if (!in_array($default_lang, $keys)) { |
|
86 | + throw new IllegalStateException("default language (in global settings) isnt a supported language"); |
|
87 | + } |
|
88 | 88 | |
89 | - //remove element from array |
|
90 | - if (($key = array_search($default_lang, $keys)) !== false) { |
|
91 | - unset($keys[$key]); |
|
92 | - } |
|
89 | + //remove element from array |
|
90 | + if (($key = array_search($default_lang, $keys)) !== false) { |
|
91 | + unset($keys[$key]); |
|
92 | + } |
|
93 | 93 | |
94 | - //add as first element |
|
95 | - array_unshift($keys, $default_lang); |
|
94 | + //add as first element |
|
95 | + array_unshift($keys, $default_lang); |
|
96 | 96 | |
97 | - return $keys; |
|
98 | - } |
|
97 | + return $keys; |
|
98 | + } |
|
99 | 99 | |
100 | - public static function addLang (string $token, string $title) { |
|
101 | - Database::getInstance()->execute("INSERT INTO `{praefix}supported_languages` ( |
|
100 | + public static function addLang (string $token, string $title) { |
|
101 | + Database::getInstance()->execute("INSERT INTO `{praefix}supported_languages` ( |
|
102 | 102 | `lang_token`, `title` |
103 | 103 | ) VALUES ( |
104 | 104 | :token, :title |
105 | 105 | ); ", array( |
106 | - 'token' => $token, |
|
107 | - 'title' => $title |
|
108 | - )); |
|
106 | + 'token' => $token, |
|
107 | + 'title' => $title |
|
108 | + )); |
|
109 | 109 | |
110 | - //clear local in-memory cache |
|
111 | - self::$initialized = false; |
|
110 | + //clear local in-memory cache |
|
111 | + self::$initialized = false; |
|
112 | 112 | |
113 | - //clear cache |
|
114 | - Cache::clear("supported-languages"); |
|
115 | - } |
|
113 | + //clear cache |
|
114 | + Cache::clear("supported-languages"); |
|
115 | + } |
|
116 | 116 | |
117 | - public static function addLangOrUpdate (string $token, string $title) { |
|
118 | - Database::getInstance()->execute("INSERT INTO `{praefix}supported_languages` ( |
|
117 | + public static function addLangOrUpdate (string $token, string $title) { |
|
118 | + Database::getInstance()->execute("INSERT INTO `{praefix}supported_languages` ( |
|
119 | 119 | `lang_token`, `title` |
120 | 120 | ) VALUES ( |
121 | 121 | :token, :title |
122 | 122 | ) ON DUPLICATE KEY UPDATE `title` = :title; ", array( |
123 | - 'token' => $token, |
|
124 | - 'title' => $title |
|
125 | - )); |
|
126 | - |
|
127 | - //clear local in-memory cache |
|
128 | - self::$initialized = false; |
|
129 | - |
|
130 | - //clear cache |
|
131 | - Cache::clear("supported-languages"); |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * determine which language out of an available set the user prefers most |
|
136 | - * |
|
137 | - * @param $available_languages array with language-tag-strings (must be lowercase) that are available |
|
138 | - * @param $http_accept_language a HTTP_ACCEPT_LANGUAGE string (read from $_SERVER['HTTP_ACCEPT_LANGUAGE'] if left out) |
|
139 | - */ |
|
140 | - |
|
141 | - /** |
|
142 | - * determine which language out of an available set the user prefers most |
|
143 | - * |
|
144 | - * @param $available_languages array with language-tag-strings (must be lowercase) that are available |
|
145 | - * @param $http_accept_language string HTTP_ACCEPT_LANGUAGE string (read from $_SERVER['HTTP_ACCEPT_LANGUAGE'] if left out) |
|
146 | - * |
|
147 | - * @link http://www.theserverpages.com/php/manual/en/function.http-negotiate-language.php |
|
148 | - * |
|
149 | - * @return prefered language |
|
150 | - */ |
|
151 | - protected static function prefered_language (array $available_languages, string $http_accept_language = "auto") : string { |
|
152 | - if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { |
|
153 | - $_SERVER['HTTP_ACCEPT_LANGUAGE'] = Settings::get("default_lang"); |
|
154 | - } |
|
155 | - |
|
156 | - // if $http_accept_language was left out, read it from the HTTP-Header |
|
157 | - if ($http_accept_language == "auto") $http_accept_language = $_SERVER['HTTP_ACCEPT_LANGUAGE']; |
|
158 | - |
|
159 | - // standard for HTTP_ACCEPT_LANGUAGE is defined under |
|
160 | - // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4 |
|
161 | - // pattern to find is therefore something like this: |
|
162 | - // 1#( language-range [ ";" "q" "=" qvalue ] ) |
|
163 | - // where: |
|
164 | - // language-range = ( ( 1*8ALPHA *( "-" 1*8ALPHA ) ) | "*" ) |
|
165 | - // qvalue = ( "0" [ "." 0*3DIGIT ] ) |
|
166 | - // | ( "1" [ "." 0*3("0") ] ) |
|
167 | - preg_match_all("/([[:alpha:]]{1,8})(-([[:alpha:]|-]{1,8}))?" . |
|
168 | - "(\s*;\s*q\s*=\s*(1\.0{0,3}|0\.\d{0,3}))?\s*(,|$)/i", |
|
169 | - $http_accept_language, $hits, PREG_SET_ORDER); |
|
170 | - |
|
171 | - // default language (in case of no hits) is the first in the array |
|
172 | - $bestlang = $available_languages[0]; |
|
173 | - $bestqval = 0; |
|
174 | - |
|
175 | - foreach ($hits as $arr) { |
|
176 | - // read data from the array of this hit |
|
177 | - $langprefix = strtolower ($arr[1]); |
|
178 | - if (!empty($arr[3])) { |
|
179 | - $langrange = strtolower ($arr[3]); |
|
180 | - $language = $langprefix . "-" . $langrange; |
|
181 | - } |
|
182 | - else $language = $langprefix; |
|
183 | - $qvalue = 1.0; |
|
184 | - if (!empty($arr[5])) $qvalue = floatval($arr[5]); |
|
185 | - |
|
186 | - // find q-maximal language |
|
187 | - if (in_array($language,$available_languages) && ($qvalue > $bestqval)) { |
|
188 | - $bestlang = $language; |
|
189 | - $bestqval = $qvalue; |
|
190 | - } |
|
191 | - // if no direct hit, try the prefix only but decrease q-value by 10% (as http_negotiate_language does) |
|
192 | - else if (in_array($langprefix,$available_languages) && (($qvalue*0.9) > $bestqval)) { |
|
193 | - $bestlang = $langprefix; |
|
194 | - $bestqval = $qvalue*0.9; |
|
195 | - } |
|
196 | - } |
|
197 | - return $bestlang; |
|
198 | - } |
|
123 | + 'token' => $token, |
|
124 | + 'title' => $title |
|
125 | + )); |
|
126 | + |
|
127 | + //clear local in-memory cache |
|
128 | + self::$initialized = false; |
|
129 | + |
|
130 | + //clear cache |
|
131 | + Cache::clear("supported-languages"); |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * determine which language out of an available set the user prefers most |
|
136 | + * |
|
137 | + * @param $available_languages array with language-tag-strings (must be lowercase) that are available |
|
138 | + * @param $http_accept_language a HTTP_ACCEPT_LANGUAGE string (read from $_SERVER['HTTP_ACCEPT_LANGUAGE'] if left out) |
|
139 | + */ |
|
140 | + |
|
141 | + /** |
|
142 | + * determine which language out of an available set the user prefers most |
|
143 | + * |
|
144 | + * @param $available_languages array with language-tag-strings (must be lowercase) that are available |
|
145 | + * @param $http_accept_language string HTTP_ACCEPT_LANGUAGE string (read from $_SERVER['HTTP_ACCEPT_LANGUAGE'] if left out) |
|
146 | + * |
|
147 | + * @link http://www.theserverpages.com/php/manual/en/function.http-negotiate-language.php |
|
148 | + * |
|
149 | + * @return prefered language |
|
150 | + */ |
|
151 | + protected static function prefered_language (array $available_languages, string $http_accept_language = "auto") : string { |
|
152 | + if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { |
|
153 | + $_SERVER['HTTP_ACCEPT_LANGUAGE'] = Settings::get("default_lang"); |
|
154 | + } |
|
155 | + |
|
156 | + // if $http_accept_language was left out, read it from the HTTP-Header |
|
157 | + if ($http_accept_language == "auto") $http_accept_language = $_SERVER['HTTP_ACCEPT_LANGUAGE']; |
|
158 | + |
|
159 | + // standard for HTTP_ACCEPT_LANGUAGE is defined under |
|
160 | + // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4 |
|
161 | + // pattern to find is therefore something like this: |
|
162 | + // 1#( language-range [ ";" "q" "=" qvalue ] ) |
|
163 | + // where: |
|
164 | + // language-range = ( ( 1*8ALPHA *( "-" 1*8ALPHA ) ) | "*" ) |
|
165 | + // qvalue = ( "0" [ "." 0*3DIGIT ] ) |
|
166 | + // | ( "1" [ "." 0*3("0") ] ) |
|
167 | + preg_match_all("/([[:alpha:]]{1,8})(-([[:alpha:]|-]{1,8}))?" . |
|
168 | + "(\s*;\s*q\s*=\s*(1\.0{0,3}|0\.\d{0,3}))?\s*(,|$)/i", |
|
169 | + $http_accept_language, $hits, PREG_SET_ORDER); |
|
170 | + |
|
171 | + // default language (in case of no hits) is the first in the array |
|
172 | + $bestlang = $available_languages[0]; |
|
173 | + $bestqval = 0; |
|
174 | + |
|
175 | + foreach ($hits as $arr) { |
|
176 | + // read data from the array of this hit |
|
177 | + $langprefix = strtolower ($arr[1]); |
|
178 | + if (!empty($arr[3])) { |
|
179 | + $langrange = strtolower ($arr[3]); |
|
180 | + $language = $langprefix . "-" . $langrange; |
|
181 | + } |
|
182 | + else $language = $langprefix; |
|
183 | + $qvalue = 1.0; |
|
184 | + if (!empty($arr[5])) $qvalue = floatval($arr[5]); |
|
185 | + |
|
186 | + // find q-maximal language |
|
187 | + if (in_array($language,$available_languages) && ($qvalue > $bestqval)) { |
|
188 | + $bestlang = $language; |
|
189 | + $bestqval = $qvalue; |
|
190 | + } |
|
191 | + // if no direct hit, try the prefix only but decrease q-value by 10% (as http_negotiate_language does) |
|
192 | + else if (in_array($langprefix,$available_languages) && (($qvalue*0.9) > $bestqval)) { |
|
193 | + $bestlang = $langprefix; |
|
194 | + $bestqval = $qvalue*0.9; |
|
195 | + } |
|
196 | + } |
|
197 | + return $bestlang; |
|
198 | + } |
|
199 | 199 | |
200 | 200 | } |
201 | 201 |
@@ -28,7 +28,7 @@ discard block |
||
28 | 28 | |
29 | 29 | //https://paulund.co.uk/auto-detect-browser-language-in-php |
30 | 30 | |
31 | - public static function getPrefLangToken () : string { |
|
31 | + public static function getPrefLangToken() : string { |
|
32 | 32 | if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { |
33 | 33 | $_SERVER['HTTP_ACCEPT_LANGUAGE'] = Settings::get("default_lang"); |
34 | 34 | } |
@@ -36,17 +36,17 @@ discard block |
||
36 | 36 | return substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); |
37 | 37 | } |
38 | 38 | |
39 | - public static function getLangToken (array $supported_lang_tokens) : string { |
|
39 | + public static function getLangToken(array $supported_lang_tokens) : string { |
|
40 | 40 | //http://php.net/manual/fa/function.http-negotiate-language.php |
41 | 41 | |
42 | 42 | //https://stackoverflow.com/questions/6038236/using-the-php-http-accept-language-server-variable |
43 | 43 | |
44 | 44 | //https://stackoverflow.com/questions/3770513/detect-browser-language-in-php |
45 | 45 | |
46 | - return self::prefered_language($supported_lang_tokens);//http_negotiate_language($supported_lang_tokens); |
|
46 | + return self::prefered_language($supported_lang_tokens); //http_negotiate_language($supported_lang_tokens); |
|
47 | 47 | } |
48 | 48 | |
49 | - public static function loadSupportedLangs () { |
|
49 | + public static function loadSupportedLangs() { |
|
50 | 50 | if (Cache::contains("supported-languages", "list")) { |
51 | 51 | self::$supported_languages = Cache::get("supported-languages", "list"); |
52 | 52 | } else { |
@@ -67,13 +67,13 @@ discard block |
||
67 | 67 | self::$initialized = true; |
68 | 68 | } |
69 | 69 | |
70 | - protected static function loadIfAbsent () { |
|
70 | + protected static function loadIfAbsent() { |
|
71 | 71 | if (!self::$initialized) { |
72 | 72 | self::loadSupportedLangs(); |
73 | 73 | } |
74 | 74 | } |
75 | 75 | |
76 | - public static function listSupportedLangTokens () { |
|
76 | + public static function listSupportedLangTokens() { |
|
77 | 77 | //load tokens, if not initialized |
78 | 78 | self::loadIfAbsent(); |
79 | 79 | |
@@ -97,7 +97,7 @@ discard block |
||
97 | 97 | return $keys; |
98 | 98 | } |
99 | 99 | |
100 | - public static function addLang (string $token, string $title) { |
|
100 | + public static function addLang(string $token, string $title) { |
|
101 | 101 | Database::getInstance()->execute("INSERT INTO `{praefix}supported_languages` ( |
102 | 102 | `lang_token`, `title` |
103 | 103 | ) VALUES ( |
@@ -114,7 +114,7 @@ discard block |
||
114 | 114 | Cache::clear("supported-languages"); |
115 | 115 | } |
116 | 116 | |
117 | - public static function addLangOrUpdate (string $token, string $title) { |
|
117 | + public static function addLangOrUpdate(string $token, string $title) { |
|
118 | 118 | Database::getInstance()->execute("INSERT INTO `{praefix}supported_languages` ( |
119 | 119 | `lang_token`, `title` |
120 | 120 | ) VALUES ( |
@@ -148,7 +148,7 @@ discard block |
||
148 | 148 | * |
149 | 149 | * @return prefered language |
150 | 150 | */ |
151 | - protected static function prefered_language (array $available_languages, string $http_accept_language = "auto") : string { |
|
151 | + protected static function prefered_language(array $available_languages, string $http_accept_language = "auto") : string { |
|
152 | 152 | if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { |
153 | 153 | $_SERVER['HTTP_ACCEPT_LANGUAGE'] = Settings::get("default_lang"); |
154 | 154 | } |
@@ -174,9 +174,9 @@ discard block |
||
174 | 174 | |
175 | 175 | foreach ($hits as $arr) { |
176 | 176 | // read data from the array of this hit |
177 | - $langprefix = strtolower ($arr[1]); |
|
177 | + $langprefix = strtolower($arr[1]); |
|
178 | 178 | if (!empty($arr[3])) { |
179 | - $langrange = strtolower ($arr[3]); |
|
179 | + $langrange = strtolower($arr[3]); |
|
180 | 180 | $language = $langprefix . "-" . $langrange; |
181 | 181 | } |
182 | 182 | else $language = $langprefix; |
@@ -184,14 +184,14 @@ discard block |
||
184 | 184 | if (!empty($arr[5])) $qvalue = floatval($arr[5]); |
185 | 185 | |
186 | 186 | // find q-maximal language |
187 | - if (in_array($language,$available_languages) && ($qvalue > $bestqval)) { |
|
187 | + if (in_array($language, $available_languages) && ($qvalue > $bestqval)) { |
|
188 | 188 | $bestlang = $language; |
189 | 189 | $bestqval = $qvalue; |
190 | 190 | } |
191 | 191 | // if no direct hit, try the prefix only but decrease q-value by 10% (as http_negotiate_language does) |
192 | - else if (in_array($langprefix,$available_languages) && (($qvalue*0.9) > $bestqval)) { |
|
192 | + else if (in_array($langprefix, $available_languages) && (($qvalue * 0.9) > $bestqval)) { |
|
193 | 193 | $bestlang = $langprefix; |
194 | - $bestqval = $qvalue*0.9; |
|
194 | + $bestqval = $qvalue * 0.9; |
|
195 | 195 | } |
196 | 196 | } |
197 | 197 | return $bestlang; |
@@ -154,7 +154,9 @@ discard block |
||
154 | 154 | } |
155 | 155 | |
156 | 156 | // if $http_accept_language was left out, read it from the HTTP-Header |
157 | - if ($http_accept_language == "auto") $http_accept_language = $_SERVER['HTTP_ACCEPT_LANGUAGE']; |
|
157 | + if ($http_accept_language == "auto") { |
|
158 | + $http_accept_language = $_SERVER['HTTP_ACCEPT_LANGUAGE']; |
|
159 | + } |
|
158 | 160 | |
159 | 161 | // standard for HTTP_ACCEPT_LANGUAGE is defined under |
160 | 162 | // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4 |
@@ -178,10 +180,13 @@ discard block |
||
178 | 180 | if (!empty($arr[3])) { |
179 | 181 | $langrange = strtolower ($arr[3]); |
180 | 182 | $language = $langprefix . "-" . $langrange; |
183 | + } else { |
|
184 | + $language = $langprefix; |
|
181 | 185 | } |
182 | - else $language = $langprefix; |
|
183 | 186 | $qvalue = 1.0; |
184 | - if (!empty($arr[5])) $qvalue = floatval($arr[5]); |
|
187 | + if (!empty($arr[5])) { |
|
188 | + $qvalue = floatval($arr[5]); |
|
189 | + } |
|
185 | 190 | |
186 | 191 | // find q-maximal language |
187 | 192 | if (in_array($language,$available_languages) && ($qvalue > $bestqval)) { |
@@ -27,198 +27,198 @@ |
||
27 | 27 | |
28 | 28 | class Domain { |
29 | 29 | |
30 | - protected static $instance = null; |
|
31 | - |
|
32 | - protected $id = 0; |
|
33 | - protected $row = null; |
|
34 | - |
|
35 | - /** |
|
36 | - * constructor |
|
37 | - */ |
|
38 | - public function __construct() { |
|
39 | - // |
|
40 | - } |
|
41 | - |
|
42 | - public function load ($id = null) { |
|
43 | - if ($id == null) { |
|
44 | - //get current domain |
|
45 | - $id = self::getIDByDomain(DomainUtils::getCurrentDomain()); |
|
46 | - } |
|
47 | - |
|
48 | - if (!is_int($id)) { |
|
49 | - throw new IllegalArgumentException("domain id has to be an integer."); |
|
50 | - } |
|
51 | - |
|
52 | - $this->id = $id; |
|
53 | - |
|
54 | - if (Cache::getCache()->contains("domain", "domain_" . $id)) { |
|
55 | - $this->row = Cache::getCache()->get("domain", "domain_" . $id); |
|
56 | - } else { |
|
57 | - //load domain from database |
|
58 | - $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}domain` WHERE `id` = :id; ", array('id' => $id)); |
|
59 | - |
|
60 | - if (!$row) { |
|
61 | - throw new DomainNotFoundException("Couldnt find domainID " . $id . " in database."); |
|
62 | - } |
|
63 | - |
|
64 | - $this->row = $row; |
|
65 | - |
|
66 | - //put row into cache |
|
67 | - Cache::getCache()->put("domain", "domain_" . $id, $this->row); |
|
68 | - } |
|
69 | - |
|
70 | - //throw event, so plugin can execute code here |
|
71 | - Events::throwEvent("load_domain", array( |
|
72 | - 'id' => &$this->id, |
|
73 | - 'row' => &$this->row |
|
74 | - )); |
|
75 | - } |
|
30 | + protected static $instance = null; |
|
31 | + |
|
32 | + protected $id = 0; |
|
33 | + protected $row = null; |
|
34 | + |
|
35 | + /** |
|
36 | + * constructor |
|
37 | + */ |
|
38 | + public function __construct() { |
|
39 | + // |
|
40 | + } |
|
41 | + |
|
42 | + public function load ($id = null) { |
|
43 | + if ($id == null) { |
|
44 | + //get current domain |
|
45 | + $id = self::getIDByDomain(DomainUtils::getCurrentDomain()); |
|
46 | + } |
|
47 | + |
|
48 | + if (!is_int($id)) { |
|
49 | + throw new IllegalArgumentException("domain id has to be an integer."); |
|
50 | + } |
|
51 | + |
|
52 | + $this->id = $id; |
|
53 | + |
|
54 | + if (Cache::getCache()->contains("domain", "domain_" . $id)) { |
|
55 | + $this->row = Cache::getCache()->get("domain", "domain_" . $id); |
|
56 | + } else { |
|
57 | + //load domain from database |
|
58 | + $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}domain` WHERE `id` = :id; ", array('id' => $id)); |
|
59 | + |
|
60 | + if (!$row) { |
|
61 | + throw new DomainNotFoundException("Couldnt find domainID " . $id . " in database."); |
|
62 | + } |
|
63 | + |
|
64 | + $this->row = $row; |
|
65 | + |
|
66 | + //put row into cache |
|
67 | + Cache::getCache()->put("domain", "domain_" . $id, $this->row); |
|
68 | + } |
|
69 | + |
|
70 | + //throw event, so plugin can execute code here |
|
71 | + Events::throwEvent("load_domain", array( |
|
72 | + 'id' => &$this->id, |
|
73 | + 'row' => &$this->row |
|
74 | + )); |
|
75 | + } |
|
76 | 76 | |
77 | - public function isAlias () : bool { |
|
78 | - return $this->row['alias'] != -1 && $this->row['alias'] != 0; |
|
79 | - } |
|
80 | - |
|
81 | - public function getDomain () : string { |
|
82 | - return $this->row['domain']; |
|
83 | - } |
|
84 | - |
|
85 | - public function getStyleID () : int { |
|
86 | - return $this->row['styleID']; |
|
87 | - } |
|
77 | + public function isAlias () : bool { |
|
78 | + return $this->row['alias'] != -1 && $this->row['alias'] != 0; |
|
79 | + } |
|
80 | + |
|
81 | + public function getDomain () : string { |
|
82 | + return $this->row['domain']; |
|
83 | + } |
|
84 | + |
|
85 | + public function getStyleID () : int { |
|
86 | + return $this->row['styleID']; |
|
87 | + } |
|
88 | 88 | |
89 | - public function getHomePage () : string { |
|
90 | - return $this->row['home_page']; |
|
91 | - } |
|
89 | + public function getHomePage () : string { |
|
90 | + return $this->row['home_page']; |
|
91 | + } |
|
92 | 92 | |
93 | - public function isRedirectUrl () : bool { |
|
94 | - return $this->row['redirect_url'] !== "none"; |
|
95 | - } |
|
93 | + public function isRedirectUrl () : bool { |
|
94 | + return $this->row['redirect_url'] !== "none"; |
|
95 | + } |
|
96 | 96 | |
97 | - public function getRedirectUrl () : string { |
|
98 | - return $this->row['redirect_url']; |
|
99 | - } |
|
97 | + public function getRedirectUrl () : string { |
|
98 | + return $this->row['redirect_url']; |
|
99 | + } |
|
100 | 100 | |
101 | - public function getRedirectCode () : int { |
|
102 | - return $this->row['redirect_code']; |
|
103 | - } |
|
101 | + public function getRedirectCode () : int { |
|
102 | + return $this->row['redirect_code']; |
|
103 | + } |
|
104 | 104 | |
105 | - public static function getIDByDomain (string $domain) : int { |
|
106 | - if (is_int($domain)) { |
|
107 | - throw new IllegalArgumentException("domain cannot be an integer, because a domain string is requested."); |
|
108 | - } |
|
105 | + public static function getIDByDomain (string $domain) : int { |
|
106 | + if (is_int($domain)) { |
|
107 | + throw new IllegalArgumentException("domain cannot be an integer, because a domain string is requested."); |
|
108 | + } |
|
109 | 109 | |
110 | - if (Cache::getCache()->contains("domain", "id_" . $domain)) { |
|
111 | - return (int) Cache::getCache()->contains("domain", "id_" . $domain); |
|
112 | - } else { |
|
113 | - //get id from database |
|
114 | - $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}domain` WHERE `domain` = :domain AND `activated` = '1'; ", array('domain' => $domain)); |
|
110 | + if (Cache::getCache()->contains("domain", "id_" . $domain)) { |
|
111 | + return (int) Cache::getCache()->contains("domain", "id_" . $domain); |
|
112 | + } else { |
|
113 | + //get id from database |
|
114 | + $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}domain` WHERE `domain` = :domain AND `activated` = '1'; ", array('domain' => $domain)); |
|
115 | 115 | |
116 | - $id = -1; |
|
116 | + $id = -1; |
|
117 | 117 | |
118 | - if ($row) { |
|
119 | - //get id from database row |
|
120 | - $id = $row['id']; |
|
121 | - } |
|
118 | + if ($row) { |
|
119 | + //get id from database row |
|
120 | + $id = $row['id']; |
|
121 | + } |
|
122 | 122 | |
123 | - if ($id == -1) { |
|
124 | - //domain doesnt exists, so get wildcard domain |
|
125 | - $wildcard_domain_row = self::getWildcardDomainRow(); |
|
123 | + if ($id == -1) { |
|
124 | + //domain doesnt exists, so get wildcard domain |
|
125 | + $wildcard_domain_row = self::getWildcardDomainRow(); |
|
126 | 126 | |
127 | - //check, if id belongs to wildcard domain |
|
128 | - if ($wildcard_domain_row['domain'] != $domain) { |
|
129 | - //get id of wildcard domain |
|
130 | - $id = self::getIDByDomain(self::getWildcardDomain()); |
|
131 | - } else { |
|
132 | - //throw exception |
|
133 | - throw new DomainNotFoundException("Couldnt find domain " . htmlspecialchars($domain) . " in database."); |
|
134 | - } |
|
135 | - } |
|
136 | - |
|
137 | - //add id to cache |
|
138 | - Cache::getCache()->put("domain", "id_" . $domain, (int) $id); |
|
127 | + //check, if id belongs to wildcard domain |
|
128 | + if ($wildcard_domain_row['domain'] != $domain) { |
|
129 | + //get id of wildcard domain |
|
130 | + $id = self::getIDByDomain(self::getWildcardDomain()); |
|
131 | + } else { |
|
132 | + //throw exception |
|
133 | + throw new DomainNotFoundException("Couldnt find domain " . htmlspecialchars($domain) . " in database."); |
|
134 | + } |
|
135 | + } |
|
136 | + |
|
137 | + //add id to cache |
|
138 | + Cache::getCache()->put("domain", "id_" . $domain, (int) $id); |
|
139 | 139 | |
140 | - return $id; |
|
141 | - } |
|
142 | - } |
|
140 | + return $id; |
|
141 | + } |
|
142 | + } |
|
143 | 143 | |
144 | - public static function getWildcardDomainID () : int { |
|
145 | - $row = self::getWildcardDomainRow(); |
|
144 | + public static function getWildcardDomainID () : int { |
|
145 | + $row = self::getWildcardDomainRow(); |
|
146 | 146 | |
147 | - if (!$row) { |
|
148 | - //repair the missing wildcard domain |
|
149 | - self::createWildcardDomain(); |
|
147 | + if (!$row) { |
|
148 | + //repair the missing wildcard domain |
|
149 | + self::createWildcardDomain(); |
|
150 | 150 | |
151 | - //select domain again |
|
152 | - return self::getWildcardDomainID(); |
|
151 | + //select domain again |
|
152 | + return self::getWildcardDomainID(); |
|
153 | 153 | |
154 | - //throw new WildcardDomainNotFoundException("Couldnt found wildcard domain in database."); |
|
155 | - } |
|
154 | + //throw new WildcardDomainNotFoundException("Couldnt found wildcard domain in database."); |
|
155 | + } |
|
156 | 156 | |
157 | - return $row['id']; |
|
158 | - } |
|
157 | + return $row['id']; |
|
158 | + } |
|
159 | 159 | |
160 | - public static function getWildcardDomain () : string { |
|
161 | - $row = self::getWildcardDomainRow(); |
|
160 | + public static function getWildcardDomain () : string { |
|
161 | + $row = self::getWildcardDomainRow(); |
|
162 | 162 | |
163 | - if (!$row) { |
|
164 | - //repair the missing wildcard domain |
|
165 | - self::createWildcardDomain(); |
|
163 | + if (!$row) { |
|
164 | + //repair the missing wildcard domain |
|
165 | + self::createWildcardDomain(); |
|
166 | 166 | |
167 | - //select domain again |
|
168 | - return self::getWildcardDomainID(); |
|
167 | + //select domain again |
|
168 | + return self::getWildcardDomainID(); |
|
169 | 169 | |
170 | - //throw new WildcardDomainNotFoundException("Couldnt found wildcard domain in database."); |
|
171 | - } |
|
170 | + //throw new WildcardDomainNotFoundException("Couldnt found wildcard domain in database."); |
|
171 | + } |
|
172 | 172 | |
173 | - return $row['domain']; |
|
174 | - } |
|
173 | + return $row['domain']; |
|
174 | + } |
|
175 | 175 | |
176 | - public static function getWildcardDomainRow () { |
|
177 | - if (Cache::getCache()->contains("domain", "wildcard_domain_row")) { |
|
178 | - return Cache::getCache()->get("domain", "wildcard_domain_row"); |
|
179 | - } else { |
|
180 | - //get domain id from database |
|
181 | - $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}domain` WHERE `wildcard` = 'YES' AND `activated` = '1'; "); |
|
176 | + public static function getWildcardDomainRow () { |
|
177 | + if (Cache::getCache()->contains("domain", "wildcard_domain_row")) { |
|
178 | + return Cache::getCache()->get("domain", "wildcard_domain_row"); |
|
179 | + } else { |
|
180 | + //get domain id from database |
|
181 | + $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}domain` WHERE `wildcard` = 'YES' AND `activated` = '1'; "); |
|
182 | 182 | |
183 | - if (!$row) { |
|
184 | - self::createWildcardDomain(); |
|
183 | + if (!$row) { |
|
184 | + self::createWildcardDomain(); |
|
185 | 185 | |
186 | - return self::getWildcardDomainRow(); |
|
186 | + return self::getWildcardDomainRow(); |
|
187 | 187 | |
188 | - //throw new WildcardDomainNotFoundException("Couldnt found wildcard domain in database."); |
|
189 | - } |
|
188 | + //throw new WildcardDomainNotFoundException("Couldnt found wildcard domain in database."); |
|
189 | + } |
|
190 | 190 | |
191 | - //put id into cache |
|
192 | - Cache::getCache()->put("domain", "wildcard_domain_row", $row); |
|
191 | + //put id into cache |
|
192 | + Cache::getCache()->put("domain", "wildcard_domain_row", $row); |
|
193 | 193 | |
194 | - //return id |
|
195 | - return $row; |
|
196 | - } |
|
197 | - } |
|
194 | + //return id |
|
195 | + return $row; |
|
196 | + } |
|
197 | + } |
|
198 | 198 | |
199 | - public static function createWildcardDomain () { |
|
200 | - Database::getInstance()->execute("INSERT INTO `{praefix}domain` ( |
|
199 | + public static function createWildcardDomain () { |
|
200 | + Database::getInstance()->execute("INSERT INTO `{praefix}domain` ( |
|
201 | 201 | `id`, `domain`, `alias`, `home_page`, `wildcard`, `styleID`, `redirect_url`, `redirect_code`, `lastUpdate`, `activated` |
202 | 202 | ) VALUES ( |
203 | 203 | NULL, '*', '-1', 'home', 'YES', '-1', 'none', '302', CURRENT_TIMESTAMP, '1' |
204 | 204 | ) ON DUPLICATE KEY UPDATE `activated` = '1'; "); |
205 | - } |
|
205 | + } |
|
206 | 206 | |
207 | - public static function getCurrent () : Domain { |
|
208 | - //check, if instance exists |
|
209 | - if (self::$instance == null) { |
|
210 | - //create new instance of domain |
|
211 | - self::$instance = new Domain(); |
|
207 | + public static function getCurrent () : Domain { |
|
208 | + //check, if instance exists |
|
209 | + if (self::$instance == null) { |
|
210 | + //create new instance of domain |
|
211 | + self::$instance = new Domain(); |
|
212 | 212 | |
213 | - //get id of domain |
|
214 | - $domainID = self::getIDByDomain(DomainUtils::getDomain()); |
|
213 | + //get id of domain |
|
214 | + $domainID = self::getIDByDomain(DomainUtils::getDomain()); |
|
215 | 215 | |
216 | - //load data of domain with id |
|
217 | - self::$instance->load($domainID); |
|
218 | - } |
|
216 | + //load data of domain with id |
|
217 | + self::$instance->load($domainID); |
|
218 | + } |
|
219 | 219 | |
220 | - return self::$instance; |
|
221 | - } |
|
220 | + return self::$instance; |
|
221 | + } |
|
222 | 222 | |
223 | 223 | } |
224 | 224 |
@@ -39,7 +39,7 @@ discard block |
||
39 | 39 | // |
40 | 40 | } |
41 | 41 | |
42 | - public function load ($id = null) { |
|
42 | + public function load($id = null) { |
|
43 | 43 | if ($id == null) { |
44 | 44 | //get current domain |
45 | 45 | $id = self::getIDByDomain(DomainUtils::getCurrentDomain()); |
@@ -74,35 +74,35 @@ discard block |
||
74 | 74 | )); |
75 | 75 | } |
76 | 76 | |
77 | - public function isAlias () : bool { |
|
77 | + public function isAlias() : bool { |
|
78 | 78 | return $this->row['alias'] != -1 && $this->row['alias'] != 0; |
79 | 79 | } |
80 | 80 | |
81 | - public function getDomain () : string { |
|
81 | + public function getDomain() : string { |
|
82 | 82 | return $this->row['domain']; |
83 | 83 | } |
84 | 84 | |
85 | - public function getStyleID () : int { |
|
85 | + public function getStyleID() : int { |
|
86 | 86 | return $this->row['styleID']; |
87 | 87 | } |
88 | 88 | |
89 | - public function getHomePage () : string { |
|
89 | + public function getHomePage() : string { |
|
90 | 90 | return $this->row['home_page']; |
91 | 91 | } |
92 | 92 | |
93 | - public function isRedirectUrl () : bool { |
|
93 | + public function isRedirectUrl() : bool { |
|
94 | 94 | return $this->row['redirect_url'] !== "none"; |
95 | 95 | } |
96 | 96 | |
97 | - public function getRedirectUrl () : string { |
|
97 | + public function getRedirectUrl() : string { |
|
98 | 98 | return $this->row['redirect_url']; |
99 | 99 | } |
100 | 100 | |
101 | - public function getRedirectCode () : int { |
|
101 | + public function getRedirectCode() : int { |
|
102 | 102 | return $this->row['redirect_code']; |
103 | 103 | } |
104 | 104 | |
105 | - public static function getIDByDomain (string $domain) : int { |
|
105 | + public static function getIDByDomain(string $domain) : int { |
|
106 | 106 | if (is_int($domain)) { |
107 | 107 | throw new IllegalArgumentException("domain cannot be an integer, because a domain string is requested."); |
108 | 108 | } |
@@ -141,7 +141,7 @@ discard block |
||
141 | 141 | } |
142 | 142 | } |
143 | 143 | |
144 | - public static function getWildcardDomainID () : int { |
|
144 | + public static function getWildcardDomainID() : int { |
|
145 | 145 | $row = self::getWildcardDomainRow(); |
146 | 146 | |
147 | 147 | if (!$row) { |
@@ -157,7 +157,7 @@ discard block |
||
157 | 157 | return $row['id']; |
158 | 158 | } |
159 | 159 | |
160 | - public static function getWildcardDomain () : string { |
|
160 | + public static function getWildcardDomain() : string { |
|
161 | 161 | $row = self::getWildcardDomainRow(); |
162 | 162 | |
163 | 163 | if (!$row) { |
@@ -173,7 +173,7 @@ discard block |
||
173 | 173 | return $row['domain']; |
174 | 174 | } |
175 | 175 | |
176 | - public static function getWildcardDomainRow () { |
|
176 | + public static function getWildcardDomainRow() { |
|
177 | 177 | if (Cache::getCache()->contains("domain", "wildcard_domain_row")) { |
178 | 178 | return Cache::getCache()->get("domain", "wildcard_domain_row"); |
179 | 179 | } else { |
@@ -196,7 +196,7 @@ discard block |
||
196 | 196 | } |
197 | 197 | } |
198 | 198 | |
199 | - public static function createWildcardDomain () { |
|
199 | + public static function createWildcardDomain() { |
|
200 | 200 | Database::getInstance()->execute("INSERT INTO `{praefix}domain` ( |
201 | 201 | `id`, `domain`, `alias`, `home_page`, `wildcard`, `styleID`, `redirect_url`, `redirect_code`, `lastUpdate`, `activated` |
202 | 202 | ) VALUES ( |
@@ -204,7 +204,7 @@ discard block |
||
204 | 204 | ) ON DUPLICATE KEY UPDATE `activated` = '1'; "); |
205 | 205 | } |
206 | 206 | |
207 | - public static function getCurrent () : Domain { |
|
207 | + public static function getCurrent() : Domain { |
|
208 | 208 | //check, if instance exists |
209 | 209 | if (self::$instance == null) { |
210 | 210 | //create new instance of domain |
@@ -47,7 +47,7 @@ |
||
47 | 47 | return false; |
48 | 48 | } |
49 | 49 | |
50 | - protected function getHTML () : string { |
|
50 | + protected function getHTML() : string { |
|
51 | 51 | $content = $this->getPage()->getContent(); |
52 | 52 | |
53 | 53 | Events::throwEvent("get_content", array( |
@@ -27,37 +27,37 @@ |
||
27 | 27 | |
28 | 28 | class IndexPage extends HTMLPage { |
29 | 29 | |
30 | - public function getContent(): string { |
|
31 | - //first check, if specific template exists |
|
32 | - $current_style = Registry::singleton()->getSetting("current_style_name"); |
|
33 | - if (file_exists(STYLE_PATH . $current_style . "/pages/home.tpl")) { |
|
34 | - $template = new Template($this->getPage()->hasCustomTemplate() ? $this->getPage()->getCustomTemplate() : "pages/home"); |
|
30 | + public function getContent(): string { |
|
31 | + //first check, if specific template exists |
|
32 | + $current_style = Registry::singleton()->getSetting("current_style_name"); |
|
33 | + if (file_exists(STYLE_PATH . $current_style . "/pages/home.tpl")) { |
|
34 | + $template = new Template($this->getPage()->hasCustomTemplate() ? $this->getPage()->getCustomTemplate() : "pages/home"); |
|
35 | 35 | |
36 | - $template->assign("TITLE", $this->getPage()->getTitle()); |
|
37 | - $template->assign("CONTENT", $this->getHTML()); |
|
36 | + $template->assign("TITLE", $this->getPage()->getTitle()); |
|
37 | + $template->assign("CONTENT", $this->getHTML()); |
|
38 | 38 | |
39 | - $template->parse("main"); |
|
40 | - return $template->getCode(); |
|
41 | - } else { |
|
42 | - return $this->getHTML(); |
|
43 | - } |
|
44 | - } |
|
39 | + $template->parse("main"); |
|
40 | + return $template->getCode(); |
|
41 | + } else { |
|
42 | + return $this->getHTML(); |
|
43 | + } |
|
44 | + } |
|
45 | 45 | |
46 | - public function showFooter(): bool { |
|
47 | - return false; |
|
48 | - } |
|
46 | + public function showFooter(): bool { |
|
47 | + return false; |
|
48 | + } |
|
49 | 49 | |
50 | - protected function getHTML () : string { |
|
51 | - $content = $this->getPage()->getContent(); |
|
50 | + protected function getHTML () : string { |
|
51 | + $content = $this->getPage()->getContent(); |
|
52 | 52 | |
53 | - Events::throwEvent("get_content", array( |
|
54 | - 'content' => &$content, |
|
55 | - 'page' => &$this->page, |
|
56 | - 'page_type' => &$this |
|
57 | - )); |
|
53 | + Events::throwEvent("get_content", array( |
|
54 | + 'content' => &$content, |
|
55 | + 'page' => &$this->page, |
|
56 | + 'page_type' => &$this |
|
57 | + )); |
|
58 | 58 | |
59 | - return $content; |
|
60 | - } |
|
59 | + return $content; |
|
60 | + } |
|
61 | 61 | |
62 | 62 | } |
63 | 63 |