|
5 | 5 | require_once '../lib/boot.php'; |
6 | 6 |
|
7 | 7 | use Photobooth\Image; |
8 | | -use Photobooth\Helper; |
9 | 8 | use Photobooth\Collage; |
10 | 9 | use Photobooth\Enum\FolderEnum; |
11 | 10 | use Photobooth\Enum\ImageFilterEnum; |
12 | 11 | use Photobooth\Service\DatabaseManagerService; |
13 | 12 | use Photobooth\Service\LoggerService; |
| 13 | +use Photobooth\Service\RemoteStorageService; |
14 | 14 | use Photobooth\Utility\ImageUtility; |
15 | 15 |
|
16 | 16 | header('Content-Type: application/json'); |
|
19 | 19 | $logger->debug(basename($_SERVER['PHP_SELF'])); |
20 | 20 |
|
21 | 21 | $database = DatabaseManagerService::getInstance(); |
| 22 | +$remoteStorage = RemoteStorageService::getInstance(); |
22 | 23 |
|
23 | 24 | try { |
24 | 25 | if (empty($_POST['file'])) { |
|
255 | 256 | } |
256 | 257 | } |
257 | 258 |
|
258 | | - // send to ftp server |
| 259 | + // Store images on remote storage |
259 | 260 | if ($config['ftp']['enabled']) { |
260 | | - // init connection to ftp server |
261 | | - $ftp = ftp_ssl_connect($config['ftp']['baseURL'], $config['ftp']['port']); |
262 | | - |
263 | | - if ($ftp === false) { |
264 | | - $message = 'Failed to connect to FTP Server!'; |
265 | | - $logger->error($message, $config['ftp']); |
266 | | - echo json_encode(['error' => $message]); |
267 | | - die(); |
| 261 | + $remoteStorage->write($remoteStorage->getStorageFolder() . '/images/' . $image, (string) file_get_contents($filename_photo)); |
| 262 | + $remoteStorage->write($remoteStorage->getStorageFolder() . '/thumbs/' . $image, (string) file_get_contents($filename_thumb)); |
| 263 | + if ($config['ftp']['create_webpage']) { |
| 264 | + $remoteStorage->createWebpage(); |
268 | 265 | } |
269 | | - ftp_set_option($ftp, FTP_TIMEOUT_SEC, 10); |
270 | | - |
271 | | - // login to ftp server |
272 | | - $login_result = ftp_login($ftp, $config['ftp']['username'], $config['ftp']['password']); |
273 | | - |
274 | | - if (!$login_result) { |
275 | | - $message = 'Can\'t connect to FTP Server!'; |
276 | | - $logger->error($message, $config['ftp']); |
277 | | - echo json_encode(['error' => $message]); |
278 | | - die(); |
279 | | - } |
280 | | - |
281 | | - // turn passive mode on to enable creation of folder and upload of files |
282 | | - ftp_pasv($ftp, true); |
283 | | - |
284 | | - $destination = empty($config['ftp']['baseFolder']) ? '' : DIRECTORY_SEPARATOR . $config['ftp']['baseFolder'] . DIRECTORY_SEPARATOR; |
285 | | - |
286 | | - $destination .= $config['ftp']['folder'] . DIRECTORY_SEPARATOR . Helper::slugify($config['ftp']['title']); |
287 | | - if ($config['ftp']['appendDate']) { |
288 | | - $destination .= DIRECTORY_SEPARATOR . date('Y/m/d'); |
289 | | - } |
290 | | - |
291 | | - // navigate trough folder on the server to the destination |
292 | | - @Helper::cdFTPTree($ftp, $destination); |
293 | | - |
294 | | - // upload processed picture into destination folder |
295 | | - $put_result = @ftp_put($ftp, $image, $filename_photo, FTP_BINARY); |
296 | | - |
297 | | - if (!$put_result) { |
298 | | - $message = 'Unable to save file on FTP Server!'; |
299 | | - $logger->error($message, $config['ftp']); |
300 | | - echo json_encode(['error' => $message]); |
301 | | - die(); |
302 | | - } |
303 | | - |
304 | | - // upload the thumbnail if enabled |
305 | | - if ($config['ftp']['upload_thumb']) { |
306 | | - $thumb_result = ftp_put($ftp, 'tmb_' . $image, $filename_thumb, FTP_BINARY); |
307 | | - if (!$thumb_result) { |
308 | | - $logger->error('Unable to load the thumbnail', $config['ftp']); |
309 | | - } |
310 | | - } |
311 | | - |
312 | | - // check if the webpage is enabled and is not already loaded on the ftp server |
313 | | - if ($config['ftp']['create_webpage'] && (!isset($_SESSION['ftpWebpageLoaded']) || $_SESSION['ftpWebpageLoaded'] != $config['ftp']['title'])) { |
314 | | - // if the date folder structure is appended, return to the main folder |
315 | | - if ($config['ftp']['appendDate']) { |
316 | | - @Helper::cdFTPTree($ftp, '../../../'); |
317 | | - } |
318 | | - |
319 | | - // another security check on the file in the server (e.g. 2-day event with the same ftp folder location) |
320 | | - $webpage_exist = ftp_size($ftp, 'index.php'); |
321 | | - if ($webpage_exist == -1) { |
322 | | - // get the index.php template file from the configured location |
323 | | - $webpage_template = file_get_contents($config['ftp']['template_location']); |
324 | | - |
325 | | - if ($webpage_template === false) { |
326 | | - throw new \Exception('File could not be read: ' . $config['ftp']['template_location']); |
327 | | - } |
328 | | - // set the {title} variable |
329 | | - $final_webpage = str_replace('{title}', $config['ftp']['title'], $webpage_template); |
330 | | - |
331 | | - // put the file into a stream |
332 | | - $stream = fopen('php://memory', 'r+'); |
333 | | - if ($stream === false) { |
334 | | - throw new \Exception('Could not put the file into a stream!'); |
335 | | - } |
336 | | - fwrite($stream, $final_webpage); |
337 | | - rewind($stream); |
338 | | - |
339 | | - // load the index.php result file in the ftp server |
340 | | - $upload_webpage = ftp_fput($ftp, 'index.php', $stream, FTP_BINARY); |
341 | | - |
342 | | - fclose($stream); |
343 | | - |
344 | | - if (!$upload_webpage) { |
345 | | - $message = 'Unable to save file on FTP Server!'; |
346 | | - $logger->error($message, $config['ftp']); |
347 | | - echo json_encode(['error' => $message]); |
348 | | - die(); |
349 | | - } |
350 | | - |
351 | | - // update the session variable to avoid unnecessary checks |
352 | | - $_SESSION['ftpWebpageLoaded'] = $config['ftp']['title']; |
353 | | - } |
354 | | - } |
355 | | - |
356 | | - // close the connection |
357 | | - @ftp_close($ftp); |
358 | 266 | } |
359 | 267 |
|
360 | 268 | // Change permissions |
|
0 commit comments