Repository navigation
Expand file tree
/
Copy pathfunctions.php
More file actions
executable file
·247 lines (213 loc) · 8.15 KB
/
Copy pathfunctions.php
File metadata and controls
executable file
·247 lines (213 loc) · 8.15 KB
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
<?php
function localhost_redirect($url){
if (($_SERVER['SERVER_NAME'] == 'localhost') && $url !="")
{
header('Location: '.$url);
}
}
/**
* Sitelister
*
* This is a self contained class designed to do site listing with default or saved
* parameters.
*/
class Sitelister
{
public $defaults;
public $config;
public function __construct()
{
# Setup Defaults
$this->defaults['domain_name'] = 'localhost';
$this->defaults['path'] = '/var/www/';
$this->defaults['base_url'] = 'http://localhost';
$this->defaults['submode'] = "FALSE";
$this->defaults['class'] = 'file_list';
$this->defaults['exclude_list'] = ['.','..','functions.php', 'data', 'style.css', 'README.md', 'config.php'];
$this->defaults['datapath'] = 'data';
$this->defaults['pinned_sites'] = [];
$this->defaults['sortbyframework'] = "TRUE";
$this->config = $this->config();
}
public function config($data = FALSE)
{
# Read the current data file
$current_data = unserialize(file_get_contents($this->defaults['datapath']));
# If no data needs to be saved, just return what's there in array format
if ($data)
{
$data_file = fopen($this->defaults['datapath'], "w") or die("Unable to open data file!");
foreach ($data as $key => $value)
{
$current_data[$key] = $value;
}
fwrite($data_file, serialize($current_data));
return TRUE;
}
# Add all default values if they don't exist in the config
foreach ($this->defaults as $key => $value)
{
if (!isset($current_data[$key]))
{
$current_data[$key] = $value;
}
}
return $current_data;
}
public function pin_site($project_name)
{
$pinned_sites = $this->config()['pinned_sites'];
if (($key = array_search($project_name, $pinned_sites)) !== false) {
unset($pinned_sites[$key]);
}
else
{
array_push($pinned_sites, $project_name);
}
$pinned_sites = array_values($pinned_sites);
$this->config(['pinned_sites' => $pinned_sites]);
}
public function get_folder_size($path)
{
$path = "'".$path."'";
# Not sure if this works since I don't use Windows, but worth added a check anyway
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
{
$obj = new COM ( 'scripting.filesystemobject' );
$ref = $obj->getfolder ( $path );
$obj = null;
return 'Directory: ' . $path . ' => Size: ' . $ref->size;
}
$io = popen ( '/usr/bin/du -h -s '.$path, 'r' );
$size = fgets ( $io, 4096);
$size = substr ( $size, 0, strpos ( $size, "\t" ) );
pclose ( $io );
return $size;
}
public function detect_framework($project)
{
$dir_handle = @opendir($this->config['path']."/".$project) or die("Invalid Framework Directory");
$files = [];
$i = 0;
while ( false !== ($folder = readdir($dir_handle)) )
{
$files[$i] = $folder;
$i++;
} # End While
closedir($dir_handle);
if (in_array("wp-config.php", $files))
{
return "Wordpress";
}
if (in_array("application", $files))
{
return "CodeIgnitor";
}
if (in_array("app", $files) && in_array("src", $files))
{
return "Symfony";
}
if (in_array("backend", $files) && in_array("console", $files))
{
return "Yii";
}
return 'Custom Project';
}
public function list_sites()
{
$projects = [];
$used_frameworks = [];
# Create an array of folders in the project folder
$dir_handle = @opendir($this->config['path']) or die("Invalid www Directory");
while ( false !== ($folder = readdir($dir_handle)) )
{
# Only continue if the folder isn't excluded
if(in_array($folder, $this->config['exclude_list']))
continue;
if(in_array($folder, $this->config['pinned_sites']))
continue;
$projects[$folder] = $this->detect_framework($folder);
# Create a list of frameworks for use later
if (!in_array($projects[$folder], $used_frameworks))
{
array_push($used_frameworks, $projects[$folder]);
}
} # End While
closedir($dir_handle);
$grouped_projects = [];
if ($this->config['sortbyframework'] == "TRUE")
{
foreach ($used_frameworks as $framework)
{
$grouped_projects[$framework] = [];
$i = 0;
foreach ($projects as $project => $project_framework)
{
if ($project_framework == $framework)
{
array_push($grouped_projects[$framework], $project);
}
$i++;
}
# Sort each porject in the framework groups by name
asort($grouped_projects[$framework]);
}
}
# Sort the main frameworks by name
arsort($grouped_projects);
$grouped_projects; # Contains Framework grouped projects
$projects; # Contains all projects
foreach ($grouped_projects as $framework => $projects)
{
foreach ($projects as $project)
{
# Get Folder Size if we set it in config
$size = "";
if ($this->config()['filesize'] == "TRUE")
{
$size = "<div class='tag-size'>  = ".$this->get_folder_size($this->config['path']."/".$project."/")."</div>";
}
# Make some pretty File names
$file_title = str_replace("_", " ", $project);
$file_title = ucwords(strtolower($file_title));
if ($this->config['submode'] == true)
{
echo "<li class='".$this->config['class']." ".$framework."'><a href='https://".$project.".".$this->config['domain_name']."'>".$file_title."</a><small class='tags ".$framework."'>".$framework.$size."</small></li>";
}
else
{
echo "<li class='".$this->config['class']." ".$framework."'><a href='https://".$this->config['domain_name']."/".$project."'>".$file_title."</a><small class='tags ".$framework."'>".$framework." <div class='tag-size'>= ".$size."</div></small></li>";
}
}
echo "<br/><br/>";
}
}
public function list_pinned_sites()
{
echo "<h4>Pinned Sites</h4>";
$projects = $this->config()['pinned_sites'];
foreach ($projects as $project)
{
$framework = $this->detect_framework($project);
# Get Folder Size if we set it in config
$size = "";
if ($this->config()['filesize'] == "TRUE")
{
$size = "<div class='tag-size'>  = ".$this->get_folder_size($this->config['path']."/".$project."/")."</div>";
}
# Make some pretty File names
$file_title = str_replace("_", " ", $project);
$file_title = ucwords(strtolower($file_title));
if ($this->config['submode'] == true)
{
echo "<li class='".$this->config['class']." ".$framework."'><a href='https://".$project.".".$this->config['domain_name']."'>".$file_title."</a><small class='tags ".$framework."'>".$framework.$size."</small></li>";
}
else
{
echo "<li class='".$this->config['class']." ".$framework."'><a href='https://".$this->config['domain_name']."/".$project."'>".$file_title."</a><small class='tags ".$framework."'>".$framework." <div class='tag-size'>= ".$size."</div></small></li>";
}
}
echo "<br/><br/>";
}
}
?>