Beispiel #1
0
/**
 * Delete a file
 *
 * @param Application $app Silex Application
 * @param Request $request Request parameters
 *
 * @return JsonResponse Array of objects, directory content after the delete process
 */
function delete(Application $app, Request $request)
{
    if ($app["rights.canDelete"] == false) {
        $app->abort(403, "This user doesn't have the rights to delete this file");
    }
    $filepath = Utils\check_path($app['cakebox.root'], $request->get('path'));
    if (!isset($filepath)) {
        $app->abort(400, "Missing parameters");
    }
    $file = "{$app['cakebox.root']}/{$filepath}";
    if (file_exists($file) === false) {
        $app->abort(404, "File not found");
    }
    if (is_file($file) === false) {
        $app->abort(403, "This is not a file");
    }
    if (is_writable($file) === false) {
        $app->abort(403, "This file is not writable");
    }
    unlink($file);
    $subRequest = Request::create('/api/directories', 'GET', ['path' => dirname($filepath)]);
    return $app->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
}
Beispiel #2
0
/**
 * Archive a directory
 *
 * @param Application $app Silex Application
 * @param Request $request Request parameters
 *
 * @return JsonResponse Array of objects, directory content after the archive process
 */
function archive(Application $app, Request $request)
{
    if ($app["rights.canArchiveDirectory"] == false) {
        $app->abort(403, "This user doesn't have the rights to archive a directory");
    }
    $dirpath = Utils\check_path($app['cakebox.root'], $request->get('path'));
    if (!isset($dirpath)) {
        $app->abort(400, "Missing parameters");
    }
    $dir = "{$app['cakebox.root']}/{$dirpath}";
    if (file_exists($dir) === false) {
        $app->abort(404, "Directory not found");
    }
    if (is_dir($dir) === false) {
        $app->abort(403, "This is not a directory");
    }
    $dirpath_info = pathinfo($dir);
    $dirname = $dirpath_info["dirname"];
    $basename = $dirpath_info["basename"];
    if (is_writable($dir) === false) {
        $app->abort(403, "Parent directory is not writable");
    }
    $archive_path = "{$app['cakebox.root']}/{$dirpath}/../{$basename}.tar";
    if (file_exists("{$archive_path}.inc") || file_exists($archive_path)) {
        $app->abort(406, "This directory already have a tar file or is already under a tar process");
    }
    file_put_contents("{$archive_path}.inc", "Creation of {$basename}.tar.inc is in progress... If not, remove this file manualy.");
    $p = new \PharData($archive_path);
    $p->compress(\Phar::NONE);
    $p->buildFromDirectory($dir);
    unlink("{$archive_path}.inc");
    $subRequest = Request::create('/api/directories', 'GET', ['path' => dirname($dirpath)]);
    return $app->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
}