18
18
*/
19
19
20
20
define ('MAX_FILE_LIMIT ' , 1024 * 1024 * 2 );//2 Megabytes max html file size
21
+ define ('ALLOW_PHP ' , false );//check if saved html contains php tag and don't save if not allowed
22
+ define ('ALLOWED_OEMBED_DOMAINS ' , [
23
+ 'https://www.youtube.com/ ' ,
24
+ 'https://www.vimeo.com/ ' ,
25
+ 'https://www.twitter.com/ '
26
+ ]);//load urls only from allowed websites for oembed
21
27
22
28
function sanitizeFileName ($ file , $ allowedExtension = 'html ' ) {
29
+ $ basename = basename ($ file );
30
+ $ disallow = ['.htaccess ' , 'passwd ' ];
31
+ if (in_array ($ basename , $ disallow )) {
32
+ showError ('Filename not allowed! ' );
33
+ return '' ;
34
+ }
35
+
23
36
//sanitize, remove double dot .. and remove get parameters if any
24
- $ file = __DIR__ . '/ ' . preg_replace ('@\?.*$@ ' , '' , preg_replace ('@\.{2,}@ ' , '' , preg_replace ('@[^\/ \\a-zA-Z0-9\-\._]@ ' , '' , $ file )));
37
+ $ file = preg_replace ('@\?.*$@ ' , '' , preg_replace ('@\.{2,}@ ' , '' , preg_replace ('@[^\/ \\a-zA-Z0-9\-\._]@ ' , '' , $ file )));
38
+
39
+ if ($ file ) {
40
+ $ file = __DIR__ . DIRECTORY_SEPARATOR . $ file ;
41
+ } else {
42
+ return '' ;
43
+ }
25
44
26
45
//allow only .html extension
27
46
if ($ allowedExtension ) {
@@ -35,19 +54,38 @@ function showError($error) {
35
54
die ($ error );
36
55
}
37
56
57
+ function validOembedUrl ($ url ) {
58
+ foreach (ALLOWED_OEMBED_DOMAINS as $ domain ) {
59
+ if (strpos ($ url , $ domain ) === 0 ) {
60
+ return true ;
61
+ }
62
+ }
63
+
64
+ return false ;
65
+ }
66
+
38
67
$ html = '' ;
39
68
$ file = '' ;
40
69
$ action = '' ;
41
70
42
71
if (isset ($ _POST ['startTemplateUrl ' ]) && !empty ($ _POST ['startTemplateUrl ' ])) {
43
72
$ startTemplateUrl = sanitizeFileName ($ _POST ['startTemplateUrl ' ]);
44
- $ html = file_get_contents ($ startTemplateUrl );
73
+ $ html = '' ;
74
+ if ($ startTemplateUrl ) {
75
+ $ html = file_get_contents ($ startTemplateUrl );
76
+ }
45
77
} else if (isset ($ _POST ['html ' ])){
46
78
$ html = substr ($ _POST ['html ' ], 0 , MAX_FILE_LIMIT );
79
+ if (!ALLOW_PHP ) {
80
+ //if (strpos($html, '<?php') !== false) {
81
+ if (preg_match ('@<\?php|<\? |<\?=|<\s*script\s*language\s*=\s*"\s*php\s*"\s*>@ ' , $ html )) {
82
+ showError ('PHP not allowed! ' );
83
+ }
84
+ }
47
85
}
48
86
49
87
if (isset ($ _POST ['file ' ])) {
50
- $ file = sanitizeFileName ($ _POST ['file ' ], false );
88
+ $ file = sanitizeFileName ($ _POST ['file ' ]);
51
89
}
52
90
53
91
if (isset ($ _GET ['action ' ])) {
@@ -58,7 +96,7 @@ function showError($error) {
58
96
//file manager actions, delete and rename
59
97
switch ($ action ) {
60
98
case 'rename ' :
61
- $ newfile = sanitizeFileName ($ _POST ['newfile ' ], false );
99
+ $ newfile = sanitizeFileName ($ _POST ['newfile ' ]);
62
100
if ($ file && $ newfile ) {
63
101
if (rename ($ file , $ newfile )) {
64
102
echo "File ' $ file' renamed to ' $ newfile' " ;
@@ -85,28 +123,37 @@ function showError($error) {
85
123
if ($ type && $ name && $ html ) {
86
124
87
125
$ file = sanitizeFileName ("$ type/ $ name " );
88
- $ dir = dirname ($ file );
89
- if (!is_dir ($ dir )) {
90
- echo "$ dir folder does not exist \n" ;
91
- if (mkdir ($ dir , 0777 , true )) {
92
- echo "$ dir folder was created \n" ;
126
+ if ($ file ) {
127
+ $ dir = dirname ($ file );
128
+ if (!is_dir ($ dir )) {
129
+ echo "$ dir folder does not exist \n" ;
130
+ if (mkdir ($ dir , 0777 , true )) {
131
+ echo "$ dir folder was created \n" ;
132
+ } else {
133
+ showError ("Error creating folder ' $ dir' \n" );
134
+ }
135
+ }
136
+
137
+ if (file_put_contents ($ file , $ html )) {
138
+ echo "File saved ' $ file' " ;
93
139
} else {
94
- showError ("Error creating folder ' $ dir' \n" );
95
- }
96
- }
97
-
98
- if (file_put_contents ($ file , $ html )) {
99
- echo "File saved ' $ file' " ;
140
+ showError ("Error saving file ' $ file' \nPossible causes are missing write permission or incorrect file path! " );
141
+ }
100
142
} else {
101
- showError (" Error saving file ' $ file ' \n Possible causes are missing write permission or incorrect file path! " );
143
+ showError (' Invalid filename! ' );
102
144
}
103
145
} else {
104
146
showError ("Missing reusable element data! \n" );
105
147
}
106
148
break ;
107
149
case 'oembedProxy ' :
108
- header ('Content-Type: application/json ' );
109
- echo file_get_contents ($ _GET ['url ' ]);
150
+ $ url = $ _GET ['url ' ] ?? '' ;
151
+ if (validOembedUrl ($ url )) {
152
+ header ('Content-Type: application/json ' );
153
+ echo file_get_contents ($ url );
154
+ } else {
155
+ showError ('Invalid url! ' );
156
+ }
110
157
break ;
111
158
default :
112
159
showError ("Invalid action ' $ action'! " );
0 commit comments