This repository has been archived by the owner on Sep 8, 2023. It is now read-only.
/
class.fileextension_textmode.php
346 lines (313 loc) · 8.15 KB
/
class.fileextension_textmode.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php
/*
* (c) Codiad & ccvca (https://github.com/ccvca)
* @author ccvca (https://github.com/ccvca)
* This Code is released under the same licence as Codiad (https://github.com/Codiad/Codiad)
* See [root]/license.txt for more. This information must remain intact.
*/
require_once '../../common.php';
class fileextension_textmode{
//////////////////////////////////////////////////////////////////
//default associations
//////////////////////////////////////////////////////////////////
private $defaultExtensions = array(
'html' => 'html',
'htm' => 'html',
'tpl' => 'html',
'js' => 'javascript',
'css' => 'css',
'scss' => 'scss',
'sass' => 'scss',
'less' => 'less',
'php' => 'php',
'php4' => 'php',
'php5' => 'php',
'phtml' => 'php',
'json' => 'json',
'java' => 'java',
'xml' => 'xml',
'sql' => 'sql',
'md' => 'markdown',
'c' => 'c_cpp',
'cpp' => 'c_cpp',
'd' => 'd',
'h' => 'c_cpp',
'hpp' => 'c_cpp',
'py' => 'python',
'rb' => 'ruby',
'erb' => 'html_ruby',
'jade' => 'jade',
'coffee' => 'coffee',
'vm' => 'velocity');
//////////////////////////////////////////////////////////////////
//availiable text modes
//////////////////////////////////////////////////////////////////
private $availiableTextModes = array(
'abap',
'abc',
'actionscript',
'ada',
'apache_conf',
'applescript',
'asciidoc',
'assembly_x86',
'autohotkey',
'batchfile',
'c9search',
'c_cpp',
'cirru',
'clojure',
'cobol',
'coffee',
'coldfusion',
'csharp',
'css',
'curly',
'd',
'dart',
'diff',
'django',
'dockerfile',
'dot',
'eiffel',
'ejs',
'elixir',
'elm',
'erlang',
'forth',
'ftl',
'gcode',
'gherkin',
'gitignore',
'glsl',
'gobstones',
'golang',
'groovy',
'haml',
'handlebars',
'haskell',
'haxe',
'html',
'html_elixir',
'html_ruby',
'ini',
'io',
'jack',
'jade',
'java',
'javascript',
'json',
'jsoniq',
'jsp',
'jsx',
'julia',
'latex',
'lean',
'less',
'liquid',
'lisp',
'livescript',
'logiql',
'lsl',
'lua',
'luapage',
'lucene',
'makefile',
'markdown',
'mask',
'matlab',
'maze',
'mel',
'mips_assembler',
'mushcode',
'mysql',
'nix',
'nsis',
'objectivec',
'ocaml',
'pascal',
'perl',
'pgsql',
'php',
'plain_text',
'powershell',
'praat',
'prolog',
'protobuf',
'python',
'r',
'razor',
'rdoc',
'rhtml',
'rst',
'ruby',
'rust',
'sass',
'scad',
'scala',
'scheme',
'scss',
'sh',
'sjs',
'smarty',
'snippets',
'soy_template',
'space',
'sql',
'sqlserver',
'stylus',
'svg',
'swift',
'swig',
'tcl',
'tex',
'text',
'textile',
'toml',
'twig',
'typescript',
'vala',
'vbscript',
'velocity',
'verilog',
'vhdl',
'wollok',
'xml',
'xquery',
'yaml'
);
const storeFilename = 'extensions.php';
//////////////////////////////////////////////////////////////////
//check the session if the user is allowed to do anything here
//////////////////////////////////////////////////////////////////
public function __construct(){
Common::checkSession();
}
public function getAvailiableTextModes(){
return $this->availiableTextModes;
}
public function getDefaultExtensions(){
return $this->defaultExtensions;
}
//////////////////////////////////////////////////////////////////
//checks if the sended extensions are valid to prevent any injections
//////////////////////////////////////////////////////////////////
public function validateExtension($extension){
return preg_match('#^[a-z0-9\_]+$#i', $extension);
}
//////////////////////////////////////////////////////////////////
//checks if the sended extensions are valid to prevent any injections and usage of removed text modes
//////////////////////////////////////////////////////////////////
public function validTextMode($mode){
return in_array($mode, $this->availiableTextModes);
}
//////////////////////////////////////////////////////////////////
//process the form with the associations
//////////////////////////////////////////////////////////////////
private function processFileExtTextModeForm(){
if(!Common::checkAccess()){
return array('status' =>'error', 'msg' =>'You are not allowed to edit the file extensions.');
}
//Store Fileextensions and Textmodes in File:
if(!isset($_POST['extension']) || !is_array($_POST['extension'])
|| !isset($_POST['textMode']) || !is_array($_POST['textMode'])){
return json_encode(array('status' => 'error', 'msg' => 'incorrect data send'));
}
$exMap = array();
$warning = '';
//Iterate over the sended extensions
foreach ($_POST['extension'] as $key => $extension){
//ignore empty extensions, so that they are going to removed
if(trim($extension) == '' ){
continue;
}
//get the sended data and check it
if(!isset($_POST["textMode"][$key])){
return json_encode(array('status' => 'error', 'msg' => 'incorrect data send.'));
}
$extension = strtolower(trim($extension));
$textMode = strtolower(trim($_POST["textMode"][$key]));
if(!$this->validateExtension($extension)){
return json_encode(array('status' => 'error', 'msg' => 'incorrect extension:'.htmlentities($extension)));
}
if(!$this->validTextMode($textMode)){
return json_encode(array('status' => 'error', 'msg' => 'incorrect text mode:'.htmlentities($textMode)));
}
//data was correct and could be insert
if(isset($exMap[$extension])){
$warning = htmlentities($extension).' is already set.<br/>';
}else{
$exMap[$extension] = $textMode;
}
}
//store the associations
Common::saveJSON(fileextension_textmode::storeFilename, $exMap);
if($warning != ''){
return json_encode(array('status' => 'warning', 'msg' => $warning, 'extensions' => $exMap ));
}else{
return json_encode(array('status' => 'success', 'msg' => 'File extensions are saved successfully.', 'extensions' => $exMap));
}
}
//////////////////////////////////////////////////////////////////
//process all the possible forms
//////////////////////////////////////////////////////////////////
public function processForms(){
if(!isset($_GET['action'])){
return json_encode(array('status' => 'error', 'msg' => 'incorrect data send.'));
}
switch($_GET['action']){
case 'FileExtTextModeForm':
return $this->processFileExtTextModeForm();
break;
case 'GetFileExtTextModes':
return $this->prcessGetFileExtTextModes();
break;
default:
return json_encode(array('status' => 'error', 'msg' => 'Incorrect data send'));
break;
}
}
//////////////////////////////////////////////////////////////////
//Send the default extensions
//////////////////////////////////////////////////////////////////
private function prcessGetFileExtTextModes(){
$ext = false;
//ignore warnings
$ext = @Common::getJSON(fileextension_textmode::storeFilename);
if(!is_array($ext)){
//default extensions
$ext = $this->defaultExtensions;
}
//the availiable extensions, which aren't removed
$availEx = array();
foreach($ext as $ex => $mode){
if(in_array($mode, $this->availiableTextModes)){
$availEx[$ex] = $mode;
}
}
return json_encode(array('status' => 'success', 'extensions' => $availEx, 'textModes' => $this->availiableTextModes));
}
//////////////////////////////////////////////////////////////////
//return a select-field with all availiable text modes, the one in the parameter is selected
//////////////////////////////////////////////////////////////////
public function getTextModeSelect($extension){
$extension = trim(strtolower($extension));
$find = false;
$ret = '<select name="textMode[]" class="textMode">'."\n";
foreach($this->getAvailiableTextModes() as $textmode){
$ret .= ' <option';
if($textmode == $extension){
$ret .= ' selected="selected"';
$find = true;
}
$ret .='>'.$textmode.'</option>'."\n";
}
//unknown extension, print it in the end
if(!$find && $extension != ''){
$ret .= ' <option selected="selected">'.$textmode.'</option>'."\n";
}
$ret .= '</select>'."\n";
return $ret;
}
}
?>