-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfis.php
executable file
·232 lines (184 loc) · 5.66 KB
/
fis.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
#!/usr/bin/php
<?php
/**
* README:
* https://github.com/chrisputnam9/phpfolderindexsymlinks
*/
// Anything uncaught
ini_set('display_errors', 1);
ini_set('html_errors', 0);
error_reporting(E_ERROR);
define(DS, DIRECTORY_SEPARATOR);
try {
// Options
$config = array(
'dryrun' => false,
'help' => false,
'ignore' => array('.', '..'),
'verbose' => false,
);
lg('Evaluating Options');
foreach ($argv as $a => $arg)
{
if (preg_match('/^--([^\=]+)(\=(.*))?$/', $arg, $matches))
{
$key = $matches[1];
if (!isset($config[$key])) continue;
$value = isset($matches[2]) ? $matches[3] : true;
if (is_array($config[$key]))
$config[$key][] = $value;
else
$config[$key] = $value;
unset($argv[$a]);
}
}
$argv = array_values($argv);
lg('Processed Options:');
lg($config);
if ($config['help'])
{
usage();
exit;
}
if ($config['dryrun']) $config['verbose'] = true;
if (empty($argv[1]) or !is_dir($argv[1]))
lg("Must specify valid source directory to be indexed", 2);
$source = realpath($argv[1]);
$target = empty($argv[2]) ? $source."__index" : $argv[2];
if (empty($config['dryrun']))
{
mkdir($target, 0755, true);
if (!is_dir($target)) lg("Failed to create target directory ($target)", 3);
}
lg("Indexing '$source' to '$target'");
lg("Opening '$source' and looping over contents");
$dir = opendir($source);
$index_cache = array();
while ($filename = readdir($dir))
{
if (in_array($filename, $config['ignore'])) continue;
lg(" - $filename");
$index = '_';
if (preg_match('/^[^a-z0-9]*([a-z0-9])/i', $filename, $matches))
{
$index = strtolower($matches[1]);
// reduce all numbers to single folder
if (is_numeric($index))
$index='0-9';
}
lg(" --- Indexing under '$index'");
$to_dir = $target . DS . $index;
if (!in_array($index, $index_cache))
{
lg(" --- Creating folder for '$index'");
if (empty($config['dryrun']))
mkdir($to_dir, 0755, true);
$index_cache[]= $index;
}
$to_dir = realpath($to_dir);
$to = $to_dir . DS . $filename;
$from = $source . DS . $filename;
lg(" --- Symlinking '$from' to '$to'");
$from = getRelativePath($to_dir, $from);
lg(" --- Relative path: '$from'");
if (empty($config['dryrun']))
{
$original_dir = getcwd();
$success = chdir($to_dir);
if (empty($success))
lg("Unable to change directory to '$to_dir'", 4);
$success = symlink($from, $filename);
if (empty($success))
lg("Unable to symlink ($from, './')", 5);
$success = chdir($original_dir);
if (empty($success))
lg("Unable to change directory back to '$original_dir'", 6);
}
}
closedir($dir);
lg('Success!');
} catch (Exception $e) {
lg($e->getMessage(), 1);
}
/**
* Get relative path from given path to another
* Credit: https://goo.gl/g53sMe
*/
function getRelativePath($from, $to)
{
// remove trailing slashes
$from = rtrim($from, DS);
$to = rtrim($to, DS);
// Add trailing slash if directory
$from = is_dir($from) ? $from . DS : $from;
$to = is_dir($to) ? $to . DS : $to;
// split by directory separators
$from = explode(DS, $from);
$to = explode(DS, $to);
$relPath = $to;
// Loop through from levels
foreach($from as $depth => $dir) {
// find first non-matching dir
if($dir === $to[$depth]) {
// ignore this directory
array_shift($relPath);
} else {
// get number of remaining dirs to $from
$remaining = count($from) - $depth;
if($remaining > 1) {
// add traversals up to first matching dir
$padLength = (count($relPath) + $remaining - 1) * -1;
$relPath = array_pad($relPath, $padLength, '..');
break;
} else {
$relPath[0] = './' . $relPath[0];
}
}
}
return implode('/', $relPath);
}
/**
* Log function
*/
function lg($data, $error=false)
{
global $config;
if (empty($config['verbose']) and empty($error)) return;
if (is_bool($data))
$data = $data ? '(Boolean) TRUE' : '(Boolean) FALSE';
if (is_array($data) or is_object($data))
$data = print_r($data, true);
if ($error)
$data = "ERROR ($error): " . $data;
if ($config['verbose'])
$data = date('Y-m-d H:i:s ... ') . $data;
echo $data . "\n";
if ($error)
{
if (in_array($error, array(2,3)))
{
echo "--------------------\n";
usage();
}
exit($error);
}
}
/**
* Usage
*/
function usage()
{
echo <<<USAGE
USAGE:
fis {--option=value} <source> [target]
source (required) - The folder to index
target (optional) - The folder in which to create the index. If not specified, it will
default to "source__index"
Options:
--dryrun - show output without any actual side effects. Automatically enables verbose.
--help - show usage help
--ignore=path - ignore file by the given name within the source directory. Repeat this
flag for multiple ignores.
--verbose - display timestamps and verbose output
USAGE;
}