-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDerbyLogos.php
More file actions
472 lines (395 loc) · 14.1 KB
/
DerbyLogos.php
File metadata and controls
472 lines (395 loc) · 14.1 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
<?php
class Logos {
public $dir = null;
private $defaults = array ("facebook_page" => null, "league_email" => null, "copyright_waiver" => null);
public $status;
function __construct($dir = null) {
if ($dir == null)
$dir = ".";
$dir = $this->checkFilename($dir);
$this->updateDir($dir);
}
function cleanPath(&$dir = null) { // Reference
if ($dir == null) {
$dir = $this->dir;
return;
}
$newDir = array();
$parts = explode("/", $dir);
if ($parts[0] == "") { // Leading /
array_shift($parts);
}
foreach ($parts as $part) {
if ($part == "..") { // Back a directory, just strip it off.
array_pop($newDir);
} elseif ($part != "." ) {
array_push($newDir, $part);
}
}
$dir = join("/", $newDir);
return;
}
function cleanHTML($string) {
// Replace < and > with > and <
$source = array( '/</', '/>/', "/'/", '/"/' );
$dest = array( '<', '>', ''', '"' );
return preg_replace($source, $dest, $string);
}
function checkFilename($filename) {
$this->cleanPath($filename);
return $filename;
}
function updateDir($dir) {
$this->cleanPath($dir);
$this->dir = $dir;
}
function canWrite() {
// Try to create a file, return true if you can
$filename = tempnam($this->dir, "canwrite");
if (false !== $filename) {
unlink($filename);
return true;
} else {
return false;
}
}
function getAllUnderFolder($dir = null) {
$this->cleanPath($dir);
if ($dir == "")
$dir = ".";
// Return a list of all directories under the Directory specified.
$dirs = array_filter(glob("$dir/*"), 'is_dir');
$dirs = preg_replace('/^\.\//', '', $dirs);
$files = array_filter(glob("$dir/*"), 'is_file');
$files = preg_replace('/^.\//', '', $files);
if ($dir != ".")
return array("directories" => $dirs, "files" => $files);
else
return array("directories" => $dirs, "files" => array());
}
function getAllDirectories($dir = null) {
$this->cleanPath($dir);
$tmparr = $this->getAllUnderFolder($dir);
return $tmparr['directories'];
}
function getRawImage($file = null) {
if ($file == null)
return null;
$this->cleanPath($file);
$raw = file_get_contents($file);
return $raw;
}
function noSubdirs($dir = null) {
$this->cleanPath($dir);
$subdirs = $this->getAllUnderFolder($dir);
if ($subdirs['directories'] == array())
return true;
else
return false;
}
function showDirectories($dirs) {
foreach ($dirs as $dir) {
$dir = $this->cleanHTML($dir);
print "<a href='manage.php?dir=$dir'>$dir</a><br />\n";
}
}
function showLeague($dir = null) {
$this->cleanPath($dir);
$this->getStatus($dir);
$all_images = $this->getAllImages($dir);
$teams = $this->getTeams();
// This inherits to all forms on the page. Handy.
print "<input type='hidden' name='dir' value='$dir'>\n";
foreach ($teams as $teamname => $team) {
print "<form method='post'>\n";
print "<h2>".$team['description']." <a href='manage.php?dir=".$this->getPreviousDirectory($dir)."'>Back</a></h2>";
$this->displayLeagueDetails();
print "<input type='hidden' name='teamname' value='$teamname'>\n";
print "<input type='text' name='".$teamname."' value='".$team['description']."'><input type='submit' value='Update' name='action' /><input type='submit' name='action' value='Delete'><input type='submit' name='action' value='Create'>\n";
// Display summary information about the league
print "</form>";
print "<table>";
foreach ($team['logos'] as $index => $img_array) {
$filename = $img_array['filename'];
// Just ignore files with odd characters in them.
if($filename != $this->cleanHTML($filename))
continue;
if (isset($all_images[$filename])) {
unset($all_images[$filename]);
print "<tr><form method='post'><input type='hidden' name='filename' value='".$filename."'><td>".basename($filename)."</td>";
print "<td style='background-color: black'><img width=100px src='$filename'>";
print "</td>";
print "<td style='background-color: white'><img width=100px src='$filename'>";
print "</td>";
if ($img_array['black'] == true ) $black = 'checked'; else $black = "";
if ($img_array['white'] == true ) $white = 'checked'; else $white = "";
if ($img_array['other'] == true ) $other = 'checked'; else $other = "";
print "<td>Black? <input type='checkbox' name='black' value='true' $black></td>";
print "<td>White? <input type='checkbox' name='white' value='true' $white></td>";
print "<td>Other? <input type='checkbox' name='other' value='true' $other></td>";
} else {
// File missing.
print "<tr><form method='post'><input type='hidden' name='filename' value='".$filename."'><td>".basename($filename)."</td>";
print "<td colspan=5>Image missing</td>\n";
}
print "<td>Assign to Team: <select name='teamname'>";
foreach ($teams as $name => $teamsel) {
print "<option value='$name'>".$teamsel['description']."</option>\n"; }
print "</select></td>";
print "<td><input type='submit' name='action' value='Change' /></td>";
print "<td><input type='submit' name='action' value='Remove' /></td>";
print "</form></tr>\n";
}
print "</table>\n";
}
if ($all_images != null) { // There's files left over.
print "<h2>New Files</h2>\n";
print "<form method='post'><input type='text' name='newteamname' value='New Team Name'><input type='submit' value='New Team' /></form>\n";
print "<table>";
foreach ($all_images as $filename) {
print "<tr><form method='post'><input type='hidden' name='filename' value='".$filename."'><td>".basename($filename)."</td>";
print "<td style='background-color: black'><img width=100px src='$filename'>";
print "</td>";
print "<td style='background-color: white'><img width=100px src='$filename'>";
print "</td>";
print "<td>Black? <input type='checkbox' name='black' value='true'></td>";
print "<td>White? <input type='checkbox' name='white' value='true'></td>";
print "<td>Other? <input type='checkbox' name='other' value='true'></td>";
print "<td>Assign to Team: <select name='teamname'>";
foreach ($teams as $name => $teamsel) { print "<option value='$name'>".$teamsel['description']."</option>\n"; }
print "</select></td>";
print "<td><input type='submit' name='action' value='Add' /></td></form></tr>";
}
}
print "</form>\n";
}
function displayLeagueDetails() {
// Display update-able information about contacts, facebook page, and copyright.
$deets = array("Facebook Page" => "facebook_page", "League Contact" => "league_email", "Logo usage granted by" => "copyright_waiver");
print "<table>";
foreach ($deets as $text => $var) {
if (!isset($this->status[$var])) {
$this->status[$var] = 'Unknown';
$this->updateStatusFile();
}
print "<tr><td>$text</td><td><input type='text' name='$var' value='".$this->cleanHTML($this->status[$var])."'></td></tr>";
}
print "</table>";
}
private function explodeTeam($dir) {
// Figure out everything we already know about the league.
// I'm just going to assume the the directory seperator is '/'. This is windows unsafe.
$parts = explode("/", $dir);
// If first directory entry is ".", ignore it.
if ($parts[0] == ".")
array_shift($parts);
$ret['leaguename'] = basename($dir);
$ret['country'] = $parts[0];
if (isset($parts[2])) {
// This country has States/Counties.
$ret['state'] = $parts[1];
$ret['league'] = $parts[2];
} else {
// No States/Counties.
$ret['league'] = $parts[1];
$ret['state'] = "";
}
return $ret;
}
function getStatus($dir = null) {
$this->cleanPath($dir);
if (!file_exists("$dir/.status")) {
$this->createStatusFile($dir);
}
$this->status = json_decode(file_get_contents("$dir/.status"), true);
// Bit of Sanity checking. There should always be one team there, at least.
if (count($this->status['teams']) == 0) {
// Add default team.
$this->addTeam("default", "Default Team");
}
return $this->status;
}
function createStatusFile($dir) {
$this->dir = $dir;
// Only called if .status doesn't exist.
if (file_exists("$dir/.status")) {
return false;
}
$leaguearray = $this->explodeTeam($dir);
// Really basic .status file
$this->status = array_merge($this->defaults, array ("League" => $leaguearray['league'], "Country" => $leaguearray['country'], "Region" => $leaguearray['state']));
$this->updateStatusFile($dir);
// Create a default team
$this->addTeam("default", "Default Team");
// Associate all images with Default Team
$images = $this->getAllImages();
foreach ($images as $image) {
$this->addImageToTeam("Default Team", $image);
}
$this->updateStatusFile($dir);
}
function updateStatusFile($dir = null) {
if ($dir == null)
$dir = $this->dir;
file_put_contents("$dir/.status", json_encode($this->status));
}
function getAllImages($dir = null) {
if ($dir == null)
$dir = $this->dir;
$all_files = glob("$dir/*");
foreach ($all_files as $filename) {
if (preg_match('/(png|bmp|jpg|gif)$/', $filename)) {
$allimages[$filename] = $filename;
}
}
return $allimages;
}
function addImageToTeam($teamname, $image) {
if (!isset($this->status['teams'][$teamname]))
return false;
$logo_array = array ( 'filename' => $image, 'name' => $image, 'black' => false, 'white' => false, 'other' => false);
$this->status['teams'][$teamname]['logos'][] = $logo_array;
$this->updateStatusFile();
}
function addTeam($teamname, $description) {
if (isset($this->status['teams'][$teamname]))
return false;
$this->status['teams'][$teamname]['description'] = $this->cleanHTML($description);
$this->updateStatusFile();
}
function doUpdateTeam($teamname, $newname) {
if (!isset($this->status['teams'][$teamname]))
return false;
$this->status['teams'][$teamname]['description'] = $this->cleanHTML($newname);
}
function getTeams() {
return ($this->status['teams']);
}
function updateData($req) {
// Has anything been given to us?
if (!isset($req['action']))
return true;
// We have stuff to do.
switch($req['action']) {
case 'Add':
$this->addFoundLogo($req);
break;
case 'Remove':
$this->removeLogo($req);
break;
case 'Change':
$this->updateLogo($req);
break;
case 'Update':
$this->updateTeam($req);
break;
case 'Create':
$this->createTeam($req);
break;
case 'Delete':
$this->deleteTeam($req);
}
}
function addFoundLogo($req) {
// Someone's clicked on 'Add' to add a new file.
// Make sure that everything's sane.
foreach (array ('dir', 'filename', 'teamname', 'black', 'white', 'other') as $var) {
$$var = isset($req[$var])?$this->checkFilename($req[$var]):null;
}
if ($dir == null || $filename == null || $teamname == null)
return false;
$this->getStatus($dir);
// Add a logo to the required team.
$this->addImageToTeam($teamname, $filename);
$this->updateLogoUsage($filename, "black", $black);
$this->updateLogoUsage($filename, "white", $white);
$this->updateLogoUsage($filename, "other", $other);
}
function updateLogoUsage($filename, $colour, $state) {
if ($state == "" || $state == null)
$state = false;
else
$state = true;
// Find the file as part of the team.
foreach ($this->status['teams'] as $teamkey => $team) {
foreach ($team['logos'] as $logokey => $logo) {
if ($logo['filename'] == $filename)
$this->status['teams'][$teamkey]['logos'][$logokey][$colour] = $state;
}
}
$this->updateStatusFile();
}
function removeLogo($req) {
foreach (array ('dir', 'filename', 'teamname', 'black', 'white', 'other') as $var) {
$$var = isset($req[$var])?$this->checkFilename($req[$var]):null;
}
if ($dir == null || $filename == null || $teamname == null)
return false;
$this->doRemoveLogo($dir, $filename);
}
function doRemoveLogo($dir, $filename) {
$this->getStatus($dir);
// We've been asked to remove a logo.
foreach ($this->status['teams'] as $teamkey => $team) {
foreach ($team['logos'] as $logokey => $logo) {
if ($logo['filename'] == $filename) {
unset($this->status['teams'][$teamkey]['logos'][$logokey]);
// Only do it once.
$this->updateStatusFile();
return true;
}
}
}
}
function updateLogo($req) {
foreach (array ('dir', 'filename', 'teamname', 'black', 'white', 'other') as $var) {
$$var = isset($req[$var])?$this->checkFilename($req[$var]):null;
}
if ($dir == null || $filename == null || $teamname == null)
return false;
// Delete it.
$this->doRemoveLogo($dir, $filename);
// Add it to a team.
$this->addImageToTeam($teamname, $filename);
// Set the colours
$this->updateLogoUsage($filename, "black", $black);
$this->updateLogoUsage($filename, "white", $white);
$this->updateLogoUsage($filename, "other", $other);
$this->updateStatusFile();
}
function updateTeam($req) {
$dir = isset($req['dir'])?$this->checkFilename($req['dir']):null;
$this->getStatus($dir);
// Not QUITE as easy as the previous ones.
foreach (array ('facebook_page', 'league_email', 'copyright_waiver') as $var) {
$this->status[$var] = isset($req[$var])?$this->cleanHTML($req[$var]):null;
}
$teamname = isset($req['teamname'])?$this->checkFilename($req['teamname']):null;
$newname = isset($req[$teamname])?$req[$teamname]:null;
if ($dir == null || $teamname == null || $newname == null)
return false;
$this->doUpdateTeam($teamname, $newname);
$this->updateStatusFile();
}
function deleteTeam($req) {
$dir = isset($req['dir'])?$this->checkFilename($req['dir']):null;
$teamname = isset($req['teamname'])?$req['teamname']:null;
$this->getStatus($dir);
// Bye-Bye, team.
unset($this->status['teams'][$teamname]);
$this->updateStatusFile();
}
function createTeam($req) {
$dir = isset($req['dir'])?$this->checkFilename($req['dir']):null;
$teamname = isset($req['teamname'])?$this->cleanHTML($req['teamname']):null;
$newname = isset($req[$teamname])?$this->cleanHTML($req[$teamname]):null;
$this->getStatus($dir);
$this->addTeam(preg_replace('/ /', '_', $newname), $newname);
$this->updateStatusFile();
}
function getPreviousDirectory($dir) {
$arr = explode("/", $dir);
array_pop($arr);
return join("/", $arr);
}
}