-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbsgallery.php
220 lines (171 loc) · 4.73 KB
/
bsgallery.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
<?php
// Implements the business logic for administering the gallery
require 'bsclasses.php';
require 'crypt.php';
// If not authenticated, return error right away
session_start();
if ($_SESSION["auth"] != true) {
$ro = new BaseRO();
$ro->success = false;
$ro->retcode = 1;
echo json_encode($ro);
exit;
}
///////// Business methods /////
/*
Input: None
Output: GetAlbumsRO object
*/
function getAlbums() {
$ro = new GetAlbumsRO();
$files = glob('Pics/*');
$numfiles = count($files);
for ($i=0; $i < $numfiles; $i++)
{
$onefile = $files[$i];
$imgs_in_album = glob("$onefile/*.[jJ][pP][gG]");
if (count($imgs_in_album) > 0)
$album_image = $imgs_in_album[0];
else
$album_image = "res/GenericAlbum.jpg";
$album_name = basename($onefile);
$oneAlbum = new AlbumInfo($album_name, $album_image);
array_push($ro->albums, $oneAlbum);
}
return $ro;
}
/*
Input: None
Output: AlbumSummaryRO object
*/
function albumSummary() {
$ro = new AlbumSummaryRO();
$files = glob('Pics/*');
$ro->numAlbums = count($files);
$ro->usedSpace = round(filesize_r('Pics') / 1000000); // space in MiB
$ro->totalSpace = $ro->usedSpace + round(disk_free_space("/") / 1000000); // space in GiB
return $ro;
}
/*
Input: Array with property "name"
Output: BaseRO
*/
function createAlbum($inputArr) {
$ro = new BaseRO();
$aname = $inputArr["name"];
$oldumask = umask(0);
$ro->success = mkdir("Pics/$aname", 0777);
if (!($ro->success)) {
$ro->retcode = 2;
}
umask($oldumask);
return $ro;
}
/*
Input: Array with property "name"
Output: GetPhotosRO object
*/
function getPhotos($inputArr) {
$ro = new GetPhotosRO();
$album = $inputArr["name"];
$ro->albumName = $album;
$picPath = "Pics/$album/*.[jJ][pP][gG]";
$files = glob($picPath);
$numfiles = count($files);
for ($i=0; $i < $numfiles; $i++)
{
$photoUrl = $files[$i];
$info = basename($photoUrl);
$onePhoto = new PhotoInfo($photoUrl, $info);
array_push($ro->photos, $onePhoto);
}
error_log(print_r($ro, TRUE));
return $ro;
}
/*
Input: Array with property "name"
Output: AlbumSummaryRO object
*/
function photoSummary($inputArr) {
$ro = new PhotoSummaryRO();
$album = $inputArr["name"];
$picPath = "Pics/$album/*.[jJ][pP][gG]";
$files = glob($picPath);
$ro->numPics = count($files);
$ro->usedSpace = round(filesize_r("Pics/$album") / 1000000); // space in MiB
$plinkRO = publicLink($inputArr);
$ro->albumPublicUrl = $plinkRO->url;
return $ro;
}
/*
Input: Array with property "name"
Output: PublicLinkRO
*/
function publicLink($inputArr) {
$ro = new PublicLinkRO();
$aname = $inputArr["name"];
$config = parse_ini_file("config.ini");
$cryptpw = $config["cryptpw"];
error_log("Album name = $aname");
$album_enc = urlencode(encrypt($aname, $cryptpw));
$ro->url = "slides.php?album=$album_enc";
return $ro;
}
/*
Input: Array with properties "albumName" and "picurls"
Output: BaseRO object
*/
function deletePhotos($inputArr) {
$ro = new BaseRO();
$album = $inputArr["albumName"];
$filesToDel = $inputArr["picurls"];
$numfiles = count($filesToDel);
for ($i=0; $i < $numfiles; $i++)
{
$ro->success = unlink($filesToDel[$i]);
$ro->retmsg = "Deleted " . $filesToDel[$i];
if (!($ro->success)) {
$ro->retcode = 4;
$ro->retmsg = "Couldn't delete files";
return $ro;
}
}
return $ro;
}
///////// Dispatcher ///////
function dispatch($method, $data) {
// Use a dispatch table of valid funcs to prevent code-injection attacks
$validFuncs = array("getAlbums",
"createAlbum",
"publicLink",
"albumSummary",
"getPhotos",
"photoSummary",
"deletePhotos");
if (in_array($method, $validFuncs)) {
return call_user_func($method, $data);
} else {
$ro = new BaseRO();
$ro->success = false;
$ro->retcode = 3;
return $ro;
}
}
///////// Utils /////////////
function filesize_r($path){
if(!file_exists($path)) return 0;
if(is_file($path)) return filesize($path);
$ret = 0;
foreach(glob($path."/*") as $fn)
$ret += filesize_r($fn);
return $ret;
}
///////// Main /////////////
// Now, find method and args object
$method = $_POST["method"];
$data = $_POST["args"];
// error_log(print_r($data, TRUE));
// Dispatch the method
$ro = dispatch($method, $data);
echo json_encode($ro);
?>