-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.php
106 lines (93 loc) · 3.43 KB
/
common.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
<?php
include 'config.php';
header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
header('Expires: Sat, 01 Jan 2000 00:00:00 GMT');
function escape($str)
{
return htmlspecialchars($str, ENT_QUOTES, 'UTF-8');
}
function list_dir($dir)
{
$dir = rtrim($dir, '/');
if (!file_exists($dir)) return array(array(), array());
if (!is_dir($dir)) return array(array(), array());
$dirs = array();
$files = array();
$scan = scandir($dir);
foreach ($scan as $sc)
{
if ($sc === '.' || $sc === '..') continue;
$filename = $dir . '/' . $sc;
if (is_dir($filename))
{
$dirs[] = $sc;
}
else
{
$files[] = $sc;
}
}
return [$dirs, $files];
}
function get_file_info($filename)
{
static $extension_map = array(
'phps' => 'php',
'phtml' => 'php',
'html' => 'php',
'htm' => 'php',
'js' => 'javascript',
'pl' => 'perl',
'py' => 'python',
'rb' => 'ruby',
'c' => 'c_cpp',
'cc' => 'c_cpp',
'cpp' => 'c_cpp',
'h' => 'c_cpp',
'cs' => 'csharp',
'go' => 'golang',
'md' => 'markdown',
);
static $binary_extensions = array(
'doc', 'dot', 'docx', 'dotx', 'xls', 'xlsx', 'ppt', 'pptx', 'hwp', 'pdf',
'odt', 'ods', 'odp', 'odb', 'odg', 'mdb', 'accdb', 'rtf', 'ttf', 'otf', 'eot', 'woff',
'zip', 'rar', 'lzma', 'xul', 'bz2', 'gz', 'xz', 'tgz', 'tbz', 'tbz2', 'tar', 'deb', 'rpm', 'so', 'dll', 'exe',
'bmp', 'gif', 'ico', 'jpeg', 'jpg', 'png', 'svg', 'tif', 'tiff', 'psd', 'ai', 'eps',
'mp2', 'mp3', 'mp4', 'mid', 'midi', 'aif', 'aiff', 'ra', 'wav', 'ogg', 'flac',
'avi', 'mkv', 'mpg', 'mpeg', 'qt', 'mov', 'flv', 'swf', 'wma', 'wmv',
);
$extension = ($pos = strrpos($filename, '.')) !== false ? strtolower(substr($filename, $pos + 1)) : 'text';
if (array_key_exists($extension, $extension_map)) $extension = $extension_map[$extension];
if (in_array($extension, $binary_extensions)) return array('error' => 'Not a text file.');
if (filesize($filename) > 1048576) return array('error' => 'File is too big.');
$content = file_get_contents($filename);
$encoding = mb_detect_encoding($content, $GLOBALS['config']['detect_encodings']);
if ($encoding !== false && $encoding !== 'UTF-8') $content = mb_convert_encoding($content, 'UTF-8', $encoding);
$relative_path = substr($filename, strlen($GLOBALS['config']['basedir']) + 1);
$tab_type = $GLOBALS['config']['tab_type'];
foreach ($GLOBALS['config']['tab_override_paths'] as $path_prefix)
{
if (strncmp($filename, $path_prefix, strlen($path_prefix)) == 0)
{
$tab_type = strpos($tab_type, 'tab') !== false ? 'space' : 'tab';
break;
}
}
foreach ($GLOBALS['config']['tab_override_override_paths'] as $path_prefix)
{
if (strncmp($filename, $path_prefix, strlen($path_prefix)) == 0)
{
$tab_type = $GLOBALS['config']['tab_type'];
break;
}
}
return array(
'editorid' => sha1($relative_path),
'filename' => $relative_path,
'basename' => basename($filename),
'extension' => $extension,
'encoding' => ($encoding === false) ? 'UTF-8' : $encoding,
'tab_type' => $tab_type,
'content' => $content,
);
}