helper.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace OCA\Files_Trashbin;
  3. class Helper
  4. {
  5. /**
  6. * Retrieves the contents of a trash bin directory.
  7. * @param string $dir path to the directory inside the trashbin
  8. * or empty to retrieve the root of the trashbin
  9. * @return array of files
  10. */
  11. public static function getTrashFiles($dir){
  12. $result = array();
  13. $user = \OCP\User::getUser();
  14. if ($dir && $dir !== '/') {
  15. $view = new \OC_Filesystemview('/'.$user.'/files_trashbin/files');
  16. $dirContent = $view->opendir($dir);
  17. if ($dirContent === false){
  18. return null;
  19. }
  20. if(is_resource($dirContent)){
  21. while(($entryName = readdir($dirContent)) !== false) {
  22. if (!\OC\Files\Filesystem::isIgnoredDir($entryName)) {
  23. $pos = strpos($dir.'/', '/', 1);
  24. $tmp = substr($dir, 0, $pos);
  25. $pos = strrpos($tmp, '.d');
  26. $timestamp = substr($tmp, $pos+2);
  27. $result[] = array(
  28. 'id' => $entryName,
  29. 'timestamp' => $timestamp,
  30. 'mime' => $view->getMimeType($dir.'/'.$entryName),
  31. 'type' => $view->is_dir($dir.'/'.$entryName) ? 'dir' : 'file',
  32. 'location' => $dir,
  33. );
  34. }
  35. }
  36. closedir($dirContent);
  37. }
  38. } else {
  39. $query = \OC_DB::prepare('SELECT `id`,`location`,`timestamp`,`type`,`mime` FROM `*PREFIX*files_trash` WHERE `user` = ?');
  40. $result = $query->execute(array($user))->fetchAll();
  41. }
  42. $files = array();
  43. foreach ($result as $r) {
  44. $i = array();
  45. $i['name'] = $r['id'];
  46. $i['date'] = \OCP\Util::formatDate($r['timestamp']);
  47. $i['timestamp'] = $r['timestamp'];
  48. $i['mimetype'] = $r['mime'];
  49. $i['type'] = $r['type'];
  50. if ($i['type'] === 'file') {
  51. $fileinfo = pathinfo($r['id']);
  52. $i['basename'] = $fileinfo['filename'];
  53. $i['extension'] = isset($fileinfo['extension']) ? ('.'.$fileinfo['extension']) : '';
  54. }
  55. $i['directory'] = $r['location'];
  56. if ($i['directory'] === '/') {
  57. $i['directory'] = '';
  58. }
  59. $i['permissions'] = \OCP\PERMISSION_READ;
  60. $i['isPreviewAvailable'] = \OC::$server->getPreviewManager()->isMimeSupported($r['mime']);
  61. $i['icon'] = \OCA\Files\Helper::determineIcon($i);
  62. $files[] = $i;
  63. }
  64. usort($files, array('\OCA\Files\Helper', 'fileCmp'));
  65. return $files;
  66. }
  67. /**
  68. * Splits the given path into a breadcrumb structure.
  69. * @param string $dir path to process
  70. * @return array where each entry is a hash of the absolute
  71. * directory path and its name
  72. */
  73. public static function makeBreadcrumb($dir){
  74. // Make breadcrumb
  75. $pathtohere = '';
  76. $breadcrumb = array();
  77. foreach (explode('/', $dir) as $i) {
  78. if ($i !== '') {
  79. if ( preg_match('/^(.+)\.d[0-9]+$/', $i, $match) ) {
  80. $name = $match[1];
  81. } else {
  82. $name = $i;
  83. }
  84. $pathtohere .= '/' . $i;
  85. $breadcrumb[] = array('dir' => $pathtohere, 'name' => $name);
  86. }
  87. }
  88. return $breadcrumb;
  89. }
  90. }