undelete.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * @author Bart Visscher <bartv@thisnet.nl>
  4. * @author Björn Schießle <bjoern@schiessle.org>
  5. * @author Lukas Reschke <lukas@statuscode.ch>
  6. * @author Robin Appelman <icewind@owncloud.com>
  7. * @author Roeland Jago Douma <rullzer@owncloud.com>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. * @author Vincent Petry <pvince81@owncloud.com>
  10. *
  11. * @copyright Copyright (c) 2016, ownCloud, Inc.
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. OCP\JSON::checkLoggedIn();
  28. OCP\JSON::callCheck();
  29. \OC::$server->getSession()->close();
  30. $files = $_POST['files'];
  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($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. }