forked from gohai/sausagemachine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.inc.php
207 lines (184 loc) · 4.45 KB
/
util.inc.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
<?php
/*
Portable utility functions for Sausage Machine
Partially taken from Hotglue
Copyright (C) 2010 Gottfried Haider, Danja Vasiliev
Copyright (C) 2015 Gottfried Haider for PublishingLab
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Get the base URL
* @return String, including protocol and trailing slash
*/
function base_url() {
$base_url = '';
if (!empty($_SERVER['HTTPS'])) {
$base_url = 'https';
} else {
$base_url = 'http';
}
$base_url .= '://' . $_SERVER['HTTP_HOST'];
// XXX (later): handle custom port?
$pos = strrpos($_SERVER['PHP_SELF'], '/');
if ($pos !== false) {
$base_url .= substr($_SERVER['PHP_SELF'], 0, $pos);
}
$base_url .= '/';
return $base_url;
}
/**
* Copy the content of a directory
* @param $src path to a file or directory
* @param $dst path to a file or directory
* @return true if successful, false if not
*/
function cp_recursive($src, $dst) {
if (is_file($src)) {
return @copy($src, $dst);
} else if (is_dir($src)) {
if (($childs = @scandir($src)) === false) {
return false;
}
if (!is_dir(rtrim($dst, '/'))) {
if (false === @mkdir(rtrim($dst, '/'))) {
return false;
}
}
$success = true;
foreach ($childs as $child) {
if ($child == '.' || $child == '..') {
continue;
}
if (false === cp_recursive(rtrim($src, '/') . '/' . $child, rtrim($dst, '/') . '/' . $child)) {
$success = false;
}
}
return $success;
}
}
function create_containing_dir($fn) {
$pos = strrpos($fn, '/');
if ($pos === false) {
return true;
} else {
$dir = substr($fn, 0, $pos);
}
if (@is_dir($dir)) {
return true;
} else {
return @mkdir($dir, 0777, true);
}
}
/**
* Return a file's extension
* @param string $fn file name
* @return string, excluding the dot
*/
function filext($fn) {
$pos = strrpos($fn, '.');
if ($pos !== false) {
return substr($fn, $pos+1);
} else {
return '';
}
}
/**
* Return a file's mime type
* @param string $fn file name
* @return string if successful, false if not
*/
function get_mime($fn) {
if (function_exists('finfo_open')) {
$finfo = @finfo_open(FILEINFO_MIME_TYPE);
$out = @finfo_file($finfo, $fn);
@finfo_close($finfo);
return $out;
} else {
return mime_content_type($fn);
}
}
function get_server_home_dir() {
if (!empty($_SERVER['HOME'] )) {
return $_SERVER['HOME'];
} else {
// doesn't seem to be set with apache2 & mod_php5
return posix_getpwuid(posix_getuid())['dir'];
}
}
function list_files_recursive($base_dir, $cur_dir = '') {
$dir = rtrim($base_dir, '/');
if (!empty($cur_dir)) {
$dir .= '/' . $cur_dir;
}
$fns = @scandir($dir);
if ($fns === false) {
return false;
}
$ret = array();
foreach ($fns as $fn) {
if (in_array($fn, array('.', '..'))) {
continue;
}
if (@is_dir($dir . '/' . $fn)) {
$childs = list_files_recursive($base_dir, $cur_dir . '/' . $fn);
if (is_array($childs)) {
$ret = array_merge($ret, $childs);
}
} else {
$ret[] = ltrim($cur_dir . '/' . $fn, '/');
}
}
return $ret;
}
/**
* Render a PHP view and return the output as a string
* @param string $fn filename
* @param $data variable to pass to the view
* @return string
*/
function render_php($fn, $data = array()) {
@ob_start();
@include($fn);
$ret = @ob_get_contents();
@ob_end_clean();
return $ret;
}
/**
* Delete a file or directory
*
* Function taken from Hotglue's util.inc.php.
* @param string $f file name
* @return true if successful, false if not
*/
function rm_recursive($f)
{
if (is_file($f) || is_link($f)) {
// note: symlinks get deleted right away, and not recursed into
return @unlink($f);
} else {
if (($childs = @scandir($f)) === false) {
return false;
}
// strip a tailing slash
if (substr($f, -1) == '/') {
$f = substr($f, 0, -1);
}
foreach ($childs as $child) {
if ($child == '.' || $child == '..') {
continue;
} else {
rm_recursive($f . '/' . $child);
}
}
return @rmdir($f);
}
}