-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchal3.php
67 lines (57 loc) · 1.74 KB
/
chal3.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
<?php
include 'auth.php';
use OpenCloud\ObjectStore\Resource\DataObject;
$cf = $cloud->objectStoreService('cloudFiles', 'LON');
$arguments = getopt("c:d:l");
if (empty($arguments)) {
echo "Usage: php chal3.php -c <container> -d <directory> [-l]:\n" .
"\t-c: container to upload to (creates if not present)\n" .
"\t-d: directory to upload from\n" .
"\t-l: list all available containers\n";
exit(1);
} else {
$cont_name = $arguments["c"];
$dir_name = $arguments["d"];
if (!isset($cont_name) && !isset($dir_name)) {
if (isset($arguments["l"])) {
$containerlist = $cf->listContainers();
while ($container = $containerlist->next()) {
echo "$container->name\n";
}
exit(1);
}
}
}
if (!is_dir($dir_name)) {
echo "Invalid directory specified!\n";
exit(1);
} elseif ($dh = opendir($dir_name)) {
// sanitise last slash
if (substr($dir_name, -1) == "/") {
$dir_name = substr($dir_name, 0, -1);
}
// store files in an array
$i = 0;
while (($file = readdir($dh)) !== false) {
if ($file == '.' || $file == '..') {
continue;
}
$objects[$i]["name"] = $file;
$objects[$i]["path"] = $dir_name . '/' . $file;
$i++;
}
closedir($dh);
}
// check to see if desired container exists
$containerlist = $cf->listContainers();
while ($container = $containerlist->next()) {
if ($container->name == $cont_name) {
break;
}
}
if(!$container) {
echo "Container $cont_name not found, creating it...\n";
$container = $cf->createContainer($cont_name);
}
echo "Uploading files from $dir_name into container $cont_name...\n";
$container->uploadObjects($objects);