/
helpers.inc.php
executable file
·156 lines (135 loc) · 8.72 KB
/
helpers.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
<?php
Class Helpers {
static $file_cache;
static function rglob($pattern, $flags = 0, $path = '') {
if (!$path && ($dir = dirname($pattern)) != '.') {
if ($dir == '\\' || $dir == '/') $dir = '';
return self::rglob(basename($pattern), $flags, $dir . '/');
}
$paths = glob($path . '*', GLOB_ONLYDIR | GLOB_NOSORT);
$files = glob($path . $pattern, $flags);
if(is_array($paths) && is_array($files)) {
foreach ($paths as $p) $files = array_merge($files, self::rglob($pattern, $flags, $p . '/'));
}
return is_array($files) ? $files : array();
}
static function sort_by_length($a,$b){
if($a == $b) return 0;
return (strlen($a) > strlen($b) ? -1 : 1);
}
static function file_path_to_url($file_path) {
$url = preg_replace(array('/\d+?\./', '/\.\/content(\/)?/'), '', $file_path);
return $url ? $url : 'index';
}
static function url_to_file_path($url) {
# if the url is empty, we're looking for the index page
$url = empty($url) ? 'index': $url;
$file_path = './content';
# Split the url and recursively unclean the parts into folder names
$url_parts = explode('/', $url);
foreach($url_parts as $u) {
# Look for a folder at the current path that doesn't start with an underscore
if(!preg_match('/^_/', $u)) $matches = array_keys(Helpers::list_files($file_path, '/^(\d+?\.)?'.$u.'$/', true));
# No matches means a bad url
if(empty($matches)) return false;
else $file_path .= '/'.$matches[0];
}
return $file_path;
}
static function has_children($dir) {
# check if this folder contains inner folders - if it does, then it is a category
$inner_folders = Helpers::list_files($dir, '/.*/', true);
return !empty($inner_folders);
}
static function file_cache($dir = false) {
if(!self::$file_cache) {
# build file cache
self::build_file_cache('./content');
self::build_file_cache('./templates');
}
if($dir && !self::$file_cache[$dir]) return array();
return $dir ? self::$file_cache[$dir] : self::$file_cache;
}
static function build_file_cache($dir = '.') {
# build file cache
$files = glob($dir.'/*');
$files = is_array($files) ? $files : array();
foreach($files as $path) {
$file = basename($path);
if(substr($file, 0, 1) == "." || $file == "_cache") continue;
if(is_dir($path)) self::build_file_cache($path);
if(is_readable($path)) {
self::$file_cache[$dir][] = array(
'path' => $path,
'file_name' => $file,
'is_folder' => (is_dir($path) ? 1 : 0),
'mtime' => filemtime($path)
);
}
}
}
static function list_files($dir, $regex, $folders_only = false) {
$files = array();
foreach(self::file_cache($dir) as $file) {
# if file matches regex, continue
if(preg_match($regex, $file['file_name'])) {
# if $folders_only is true and the file is not a folder, skip it
if($folders_only && !$file['is_folder']) continue;
# otherwise, add file to results list
$files[$file['file_name']] = $file['path'];
}
}
# sort list in reverse-numeric order
krsort($files, SORT_NUMERIC);
return $files;
}
static function modrewrite_parse($url) {
# if the .htaccess file is missing or mod_rewrite is disabled, overwrite the clean urls
if(!file_exists('.htaccess') && preg_match('/\/$/', $url)) {
$url = '?/'.$url;
}
return $url;
}
static function relative_root_path($url = '') {
global $current_page_file_path;
$link_path = '';
if(!preg_match('/index/', $current_page_file_path) && !preg_match('/\/\?\//', $_SERVER['REQUEST_URI'])) {
# split file path by slashes
$split_path = explode('/', $current_page_file_path);
# if the request uri is pointing at a document, drop another folder from the file path
if(preg_match('/\./', $_SERVER['REQUEST_URI'])) array_pop($split_path);
# add a ../ for each parent folder
for($i = 2; $i < count($split_path); $i++) $link_path .= '../';
}
$link_path = empty($link_path) ? './' : $link_path;
return $link_path .= self::modrewrite_parse($url);
}
static function last_modified($dir) {
$last_modified = 0;
if(is_dir($dir)) {
foreach(Helpers::list_files($dir, '/.*/', false) as $file) {
if(!is_dir($file)) $last_modified = (filemtime($file) > $last_modified) ? filemtime($file) : $last_modified;
}
}
return $last_modified;
}
static function site_last_modified($dir = './content') {
$last_updated = 0;
foreach(Helpers::list_files($dir, '/.*/', false) as $file) {
if(filemtime($file) > $last_updated) $last_updated = filemtime($file);
if(is_dir($file)) {
$child_updated = self::site_last_modified($file);
if($child_updated > $last_updated) $last_updated = $child_updated;
}
}
return $last_updated;
}
static function translate_named_entities($string) {
$mapping = array('&'=>'&','''=>''', '−'=>'-', 'ˆ'=>'^', '˜'=>'~', 'Š'=>'Š', '‹'=>'‹', 'Œ'=>'Œ', '‘'=>'‘', '’'=>'’', '“'=>'“', '”'=>'”', '•'=>'•', '–'=>'–', '—'=>'—', '˜'=>'˜', '™'=>'™', 'š'=>'š', '›'=>'›', 'œ'=>'œ', 'Ÿ'=>'Ÿ', 'ÿ'=>'ÿ', 'Œ'=>'Œ', 'œ'=>'œ', 'Š'=>'Š', 'š'=>'š', 'Ÿ'=>'Ÿ', 'ƒ'=>'ƒ', 'ˆ'=>'ˆ', '˜'=>'˜', 'Α'=>'Α', 'Β'=>'Β', 'Γ'=>'Γ', 'Δ'=>'Δ', 'Ε'=>'Ε', 'Ζ'=>'Ζ', 'Η'=>'Η', 'Θ'=>'Θ', 'Ι'=>'Ι', 'Κ'=>'Κ', 'Λ'=>'Λ', 'Μ'=>'Μ', 'Ν'=>'Ν', 'Ξ'=>'Ξ', 'Ο'=>'Ο', 'Π'=>'Π', 'Ρ'=>'Ρ', 'Σ'=>'Σ', 'Τ'=>'Τ', 'Υ'=>'Υ', 'Φ'=>'Φ', 'Χ'=>'Χ', 'Ψ'=>'Ψ', 'Ω'=>'Ω', 'α'=>'α', 'β'=>'β', 'γ'=>'γ', 'δ'=>'δ', 'ε'=>'ε', 'ζ'=>'ζ', 'η'=>'η', 'θ'=>'θ', 'ι'=>'ι', 'κ'=>'κ', 'λ'=>'λ', 'μ'=>'μ', 'ν'=>'ν', 'ξ'=>'ξ', 'ο'=>'ο', 'π'=>'π', 'ρ'=>'ρ', 'ς'=>'ς', 'σ'=>'σ', 'τ'=>'τ', 'υ'=>'υ', 'φ'=>'φ', 'χ'=>'χ', 'ψ'=>'ψ', 'ω'=>'ω', 'ϑ'=>'ϑ', 'ϒ'=>'ϒ', 'ϖ'=>'ϖ', ' '=>' ', ' '=>' ', ' '=>' ', '‌'=>'‌', '‍'=>'‍', '‎'=>'‎', '‏'=>'‏', '–'=>'–', '—'=>'—', '‘'=>'‘', '’'=>'’', '‚'=>'‚', '“'=>'“', '”'=>'”', '„'=>'„', '†'=>'†', '‡'=>'‡', '•'=>'•', '…'=>'…', '‰'=>'‰', '′'=>'′', '″'=>'″', '‹'=>'‹', '›'=>'›', '‾'=>'‾', '⁄'=>'⁄', '€'=>'€', 'ℑ'=>'ℑ', '℘'=>'℘', 'ℜ'=>'ℜ', '™'=>'™', 'ℵ'=>'ℵ', '←'=>'←', '↑'=>'↑', '→'=>'→', '↓'=>'↓', '↔'=>'↔', '↵'=>'↵', '⇐'=>'⇐', '⇑'=>'⇑', '⇒'=>'⇒', '⇓'=>'⇓', '⇔'=>'⇔', '∀'=>'∀', '∂'=>'∂', '∃'=>'∃', '∅'=>'∅', '∇'=>'∇', '∈'=>'∈', '∉'=>'∉', '∋'=>'∋', '∏'=>'∏', '∑'=>'∑', '−'=>'−', '∗'=>'∗', '√'=>'√', '∝'=>'∝', '∞'=>'∞', '∠'=>'∠', '∧'=>'∧', '∨'=>'∨', '∩'=>'∩', '∪'=>'∪', '∫'=>'∫', '∴'=>'∴', '∼'=>'∼', '≅'=>'≅', '≈'=>'≈', '≠'=>'≠', '≡'=>'≡', '≤'=>'≤', '≥'=>'≥', '⊂'=>'⊂', '⊃'=>'⊃', '⊄'=>'⊄', '⊆'=>'⊆', '⊇'=>'⊇', '⊕'=>'⊕', '⊗'=>'⊗', '⊥'=>'⊥', '⋅'=>'⋅', '⌈'=>'⌈', '⌉'=>'⌉', '⌊'=>'⌊', '⌋'=>'⌋', '⟨'=>'〈', '⟩'=>'〉', '◊'=>'◊', '♠'=>'♠', '♣'=>'♣', '♥'=>'♥', '♦'=>'♦');
foreach (get_html_translation_table(HTML_ENTITIES, ENT_QUOTES) as $char => $entity){
$mapping[$entity] = '&#' . ord($char) . ';';
}
return str_replace(array_keys($mapping), $mapping, $string);
}
}
?>