public function handle($path) { $files = []; $folders = []; $list = $this->filesystem->listContents($path); $ignored = ['.', '..', '.DS_Store', '.gitignore', '.htaccess']; foreach ($list as $entry) { if (in_array($entry['basename'], $ignored)) { continue; } if (!$this->filesystem->authorized($entry['path'])) { continue; } if ($entry['type'] === 'file') { try { $url = $this->filesystem->url($entry['path']); } catch (\Exception $e) { $url = $entry['path']; } // Ugh, for some reason the foldername for the theme is included twice. Why? // For now we 'fix' this with an ugly hack, replacing it. :-/ // TODO: dig into Filesystem and figure out why this happens. $pathsegments = explode('/', $entry['path']); if (!empty($pathsegments[0])) { $url = str_replace('/' . $pathsegments[0] . '/' . $pathsegments[0] . '/', '/' . $pathsegments[0] . '/', $url); } $files[$entry['path']] = ['path' => $entry['dirname'], 'filename' => $entry['basename'], 'newpath' => $entry['path'], 'relativepath' => $entry['path'], 'writable' => true, 'readable' => false, 'type' => isset($entry['extension']) ? $entry['extension'] : '', 'filesize' => Lib::formatFilesize($entry['size']), 'modified' => date("Y/m/d H:i:s", $entry['timestamp']), 'permissions' => 'public', 'url' => $url]; /* **** Extra checks for files that can be resolved via PHP urlopen functions **** */ try { $files[$entry['path']]['permissions'] = $this->filesystem->getVisibility($entry['path']); } catch (\Exception $e) { // Computer says "No!" } $fullfilename = $this->filesystem->getAdapter()->applyPathPrefix($entry['path']); if (is_readable($fullfilename)) { $files[$entry['path']]['readable'] = true; if (!empty($entry['extension']) && in_array($entry['extension'], ['gif', 'jpg', 'png', 'jpeg'])) { $size = getimagesize($fullfilename); $files[$entry['path']]['imagesize'] = sprintf("%s × %s", $size[0], $size[1]); } $files[$entry['path']]['permissions'] = util::full_permissions($fullfilename); } } if ($entry['type'] == 'dir') { $folders[$entry['path']] = ['path' => $entry['dirname'], 'foldername' => $entry['basename'], 'newpath' => $entry['path'], 'modified' => date("Y/m/d H:i:s", $entry['timestamp']), 'writable' => true]; $fullfilename = $this->filesystem->getAdapter()->applyPathPrefix($entry['path']); /* **** Extra checks for files that can be resolved via PHP urlopen functions **** */ if (is_readable($fullfilename)) { if (!is_writable($fullfilename)) { $folders[$entry['path']]['writable'] = false; } } } } ksort($files); ksort($folders); return [$files, $folders]; }
/** * Return a list with the current stacked items. Add some relevant info to each item, * and also check if the item is present and readable. * * @param integer $count * @param string $typefilter * * @return array */ public function listitems($count = 100, $typefilter = '') { $this->initialize(); // Make sure typefilter is an array, if passed something like "image, document" if (!empty($typefilter)) { $typefilter = array_map('trim', explode(',', $typefilter)); } // Our basepaths for all files that can be on the stack: 'files' and 'theme'. $filespath = $this->app['resources']->getPath('filespath'); $themepath = $this->app['resources']->getPath('themebasepath'); $items = $this->items; $list = []; foreach ($items as $item) { $extension = strtolower(Lib::getExtension($item)); if (in_array($extension, $this->imageTypes)) { $type = 'image'; } elseif (in_array($extension, $this->documentTypes)) { $type = 'document'; } else { $type = 'other'; } // Skip this one, if it doesn't match the type. if (!empty($typefilter) && !in_array($type, $typefilter)) { continue; } // Figure out the full path, based on the two possible locations. $fullpath = ''; if (is_readable(str_replace('files/files/', 'files/', $filespath . '/' . $item))) { $fullpath = str_replace('files/files/', 'files/', $filespath . '/' . $item); } elseif (is_readable($themepath . '/' . $item)) { $fullpath = $themepath . '/' . $item; } // No dice! skip this one. if (empty($fullpath)) { continue; } $thisitem = ['basename' => basename($item), 'extension' => $extension, 'filepath' => str_replace('files/', '', $item), 'type' => $type, 'writable' => is_writable($fullpath), 'readable' => is_readable($fullpath), 'filesize' => Lib::formatFilesize(filesize($fullpath)), 'modified' => date('Y/m/d H:i:s', filemtime($fullpath)), 'permissions' => util::full_permissions($fullpath)]; $thisitem['info'] = sprintf('%s: <code>%s</code><br>%s: %s<br>%s: %s<br>%s: <code>%s</code>', Trans::__('Path'), $thisitem['filepath'], Trans::__('Filesize'), $thisitem['filesize'], Trans::__('Modified'), $thisitem['modified'], Trans::__('Permissions'), $thisitem['permissions']); if ($type == 'image') { $size = getimagesize($fullpath); $thisitem['imagesize'] = sprintf('%s × %s', $size[0], $size[1]); $thisitem['info'] .= sprintf('<br>%s: %s × %s px', Trans::__('Size'), $size[0], $size[1]); } //add it to our list. $list[] = $thisitem; } $list = array_slice($list, 0, $count); return $list; }
/** * Return a list with the current stacked items. Add some relevant info to each item, * and also check if the item is present and readable. * * @param int $count * @param string $typefilter * @return array */ public function listitems($count = 100, $typefilter = "") { // Make sure typefilter is an array, if passed something like "image, document" if (!empty($typefilter)) { $typefilter = array_map("trim", explode(",", $typefilter)); } // Our basepaths for all files that can be on the stack: 'files' and 'theme'. $filespath = $this->app['paths']['filespath']; $themepath = $this->app['paths']['themebasepath']; $items = $this->items; $list = array(); foreach ($items as $item) { $extension = strtolower(getExtension($item)); if (in_array($extension, $this->imagetypes)) { $type = "image"; } elseif (in_array($extension, $this->documenttypes)) { $type = "document"; } else { $type = "other"; } // Skip this one, if it doesn't match the type. if (!empty($typefilter) && !in_array($type, $typefilter)) { continue; } // Figure out the full path, based on the two possible locations. $fullpath = ''; if (is_readable(str_replace("files/files/", "files/", $filespath . "/" . $item))) { $fullpath = str_replace("files/files/", "files/", $filespath . "/" . $item); } elseif (is_readable($themepath . "/" . $item)) { $fullpath = $themepath . "/" . $item; } // No dice! skip this one. if (empty($fullpath)) { continue; } $thisitem = array('basename' => basename($item), 'extension' => $extension, 'filepath' => str_replace("files/", "", $item), 'type' => $type, 'writable' => is_writable($fullpath), 'readable' => is_readable($fullpath), 'filesize' => formatFilesize(filesize($fullpath)), 'modified' => date("Y/m/d H:i:s", filemtime($fullpath)), 'permissions' => \utilphp\util::full_permissions($fullpath)); $thisitem['info'] = sprintf("%s: <code>%s</code><br>%s: %s<br>%s: %s<br>%s: <code>%s</code>", __('Path'), $thisitem['filepath'], __('Filesize'), $thisitem['filesize'], __('Modified'), $thisitem['modified'], __('Permissions'), $thisitem['permissions']); if ($type == "image") { $size = getimagesize($fullpath); $thisitem['imagesize'] = sprintf("%s × %s", $size[0], $size[1]); $thisitem['info'] .= sprintf("<br>%s: %s × %s px", __("Size"), $size[0], $size[1]); } //add it to our list.. $list[] = $thisitem; } $list = array_slice($list, 0, $count); return $list; }
public function test_set_writable() { if (strncasecmp(PHP_OS, 'WIN', 3) === 0) { $this->markTestSkipped('This functionality is not working on Windows.'); } if (posix_geteuid() === 0) { $this->markTestSkipped('These tests don\'t work when run as root'); } $this->assertFalse(util::set_writable('/no/such/file')); // Create a file to test with $dirname = dirname(__FILE__); $file = $dirname . '/test7'; touch($file); chmod($file, 0644); // The file is owned by us so it should be writable $this->assertTrue(is_writable($file)); $this->assertEquals('-rw-r--r--', util::full_permissions($file)); // Toggle writable bit off for us util::set_writable($file, false); clearstatcache(); $this->assertFalse(is_writable($file)); $this->assertEquals('-r--r--r--', util::full_permissions($file)); // Toggle writable bit back on for us util::set_writable($file, true); clearstatcache(); $this->assertTrue(is_writable($file)); $this->assertEquals('-rw-r--r--', util::full_permissions($file)); unlink($file); }