This repository has been archived by the owner on Sep 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
php_end_removal.php
150 lines (125 loc) · 3.72 KB
/
php_end_removal.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/**
* Author: NewEraCracker
* License: Public Domain
* Version: 2016.0204.1
*
* Will remove PHP ending tag if code conforms certain specifications.
*
* https://secure.php.net/manual/en/language.basic-syntax.phptags.php
* https://secure.php.net/manual/en/language.basic-syntax.instruction-separation.php
*
*/
# Basic configuration
$path = './';
$php_types = array('php','inc');
# Functions
/** Array with the paths a dir contains */
function readdir_recursive($dir='.', $show_dirs=false, $ignored=array())
{
// Set types for stack and return value
$stack = $result = array();
// Initialize stack
$stack[] = $dir;
// Pop the first element of stack and evaluate it (do this until stack is fully empty)
while($dir = array_shift($stack))
{
$dh = opendir($dir);
while($dh && (false !== ($path = readdir($dh))))
{
if($path != '.' && $path != '..')
{
// Prepend dir to current path
$path = $dir.'/'.$path;
if(is_dir($path))
{
// Check ignored dirs
if(is_array($ignored) && count($ignored) && in_array($path.'/', $ignored))
continue;
// Add dir to stack for reading
$stack[] = $path;
// If $show_dirs is true, add dir path to result
if($show_dirs)
$result[] = $path;
}
elseif(is_file($path))
{
// Check ignored files
if(is_array($ignored) && count($ignored) && in_array($path, $ignored))
continue;
// Add file path to result
$result[] = $path;
}
}
}
closedir($dh);
}
// Sort the array using simple ordering
sort($result);
// Now we can return it
return $result;
}
/** Normalize a PHP file by trimming extra whitespace/tags and changing EOL to LF */
function normalize_php_file($file, $remove_close_tag = true)
{
// Check if we can work with the file
if(!is_file($file) || !is_readable($file) || !is_writable($file))
{
return false;
}
// Grab content and trim ending whitespace
$contents = rtrim(file_get_contents($file));
$stripped = rtrim(php_strip_whitespace($file));
// Check if content was grabbed
if(!$contents || !$stripped)
{
return false;
}
// Verify both strings have begin and ending tags. Ignore otherwise.
// Important! Be very strict when checking offset! Keep this line where it is!
if(substr($stripped, 0, 5) !== '<?php' || substr($stripped, -2) !== '?>' ||
substr($contents, 0, 5) !== '<?php' || substr($contents, -2) !== '?>')
{
return false;
}
// Convert line endings to LF
$contents = str_replace("\r", "\n", str_replace("\r\n", "\n", $contents));
// Count the number of opening tags for full & short types.
// We'll count from contents to ensure we get accurate values.
$php_tags_no = substr_count($contents, '<?php');
$short_tags_no = substr_count($contents, '<?') - $php_tags_no;
// Dynamic fix depending if file is pure PHP code or not.
// Important! File must not have short tags neither have more than one opening tag!
if($remove_close_tag && $short_tags_no == 0 && $php_tags_no == 1)
{
$stripped = rtrim(substr($stripped, 0, -2));
$stripped_len = strlen($stripped);
// Test the stripped file validity without the tag
if($stripped[$stripped_len-1] === ';' || $stripped[$stripped_len-1] === '}')
{
// Ending tag removal is safe.
// Act on the contents themselves and add a final EOL.
$contents = rtrim(substr($contents, 0, -2))."\n";
}
}
// Write the new file
return file_put_contents($file, $contents);
}
# Main
// Check
if( !is_dir($path) || !is_readable($path) || !is_writable($path) )
exit($path.' is invalid');
// Fix
$path = rtrim($path, '/');
// Walk
foreach(readdir_recursive($path) as $f)
{
// Grab extension
$ext = pathinfo($f,PATHINFO_EXTENSION);
// Normalize PHP files
if(in_array($ext, $php_types))
{
normalize_php_file($f);
}
}
?>