undelete.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. * @author Vincent Petry <pvince81@owncloud.com>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. OCP\JSON::checkLoggedIn();
  29. OCP\JSON::callCheck();
  30. \OC::$server->getSession()->close();
  31. $dir = '/';
  32. if (isset($_POST['dir'])) {
  33. $dir = rtrim((string)$_POST['dir'], '/'). '/';
  34. }
  35. $allFiles = false;
  36. if (isset($_POST['allfiles']) && (string)$_POST['allfiles'] === 'true') {
  37. $allFiles = true;
  38. $list = array();
  39. $dirListing = true;
  40. if ($dir === '' || $dir === '/') {
  41. $dirListing = false;
  42. }
  43. foreach (OCA\Files_Trashbin\Helper::getTrashFiles($dir, \OCP\User::getUser()) as $file) {
  44. $fileName = $file['name'];
  45. if (!$dirListing) {
  46. $fileName .= '.d' . $file['mtime'];
  47. }
  48. $list[] = $fileName;
  49. }
  50. } else {
  51. $list = json_decode($_POST['files']);
  52. }
  53. $error = array();
  54. $success = array();
  55. $i = 0;
  56. foreach ($list as $file) {
  57. $path = $dir . '/' . $file;
  58. if ($dir === '/') {
  59. $file = ltrim($file, '/');
  60. $delimiter = strrpos($file, '.d');
  61. $filename = substr($file, 0, $delimiter);
  62. $timestamp = substr($file, $delimiter+2);
  63. } else {
  64. $path_parts = pathinfo($file);
  65. $filename = $path_parts['basename'];
  66. $timestamp = null;
  67. }
  68. if ( !OCA\Files_Trashbin\Trashbin::restore($path, $filename, $timestamp) ) {
  69. $error[] = $filename;
  70. \OCP\Util::writeLog('trashbin', 'can\'t restore ' . $filename, \OCP\Util::ERROR);
  71. } else {
  72. $success[$i]['filename'] = $file;
  73. $success[$i]['timestamp'] = $timestamp;
  74. $i++;
  75. }
  76. }
  77. if ( $error ) {
  78. $filelist = '';
  79. foreach ( $error as $e ) {
  80. $filelist .= $e.', ';
  81. }
  82. $l = OC::$server->getL10N('files_trashbin');
  83. $message = $l->t("Couldn't restore %s", array(rtrim($filelist, ', ')));
  84. OCP\JSON::error(array("data" => array("message" => $message,
  85. "success" => $success, "error" => $error)));
  86. } else {
  87. OCP\JSON::success(array("data" => array("success" => $success)));
  88. }