/
InboxController.php
87 lines (74 loc) · 3.01 KB
/
InboxController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
namespace Pumukit\NewAdminBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
/**
* @Security("is_granted('ROLE_ACCESS_INBOX')")
*/
class InboxController extends Controller implements NewAdminController
{
/**
* @Route("/inbox", defaults={"_format"="json"})
*/
public function dirAction(Request $request)
{
$dir = $request->query->get('dir', '');
$type = $request->query->get('type', 'file');
$baseDir = realpath($this->container->getParameter('pumukit2.inbox'));
/*
if(0 !== strpos($dir, $baseDir)) {
throw $this->createAccessDeniedException();
}
*/
$finder = new Finder();
$res = array();
if ('file' == $type) {
$finder->depth('< 1')->followLinks()->in($dir);
$finder->sortByName();
foreach ($finder as $f) {
$res[] = array('path' => $f->getRealpath(),
'relativepath' => $f->getRelativePathname(),
'is_file' => $f->isFile(),
'hash' => hash('md5', $f->getRealpath()),
'content' => false, );
}
} else {
$finder->depth('< 1')->directories()->followLinks()->in($dir);
$finder->sortByName();
foreach ($finder as $f) {
if (0 !== (count(glob("$f/*")))) {
$contentFinder = new Finder();
$contentFinder->files()->in($f->getRealpath());
$res[] = array('path' => $f->getRealpath(),
'relativepath' => $f->getRelativePathname(),
'is_file' => $f->isFile(),
'hash' => hash('md5', $f->getRealpath()),
'content' => $contentFinder->count(), );
}
}
}
return new JsonResponse($res);
}
/**
* @Template
*/
public function formAction($onlyDir = false)
{
if (!$this->container->hasParameter('pumukit2.inbox')) {
return $this->render('@PumukitNewAdmin/Inbox/form_noconf.html.twig');
}
$dir = realpath($this->container->getParameter('pumukit2.inbox'));
if (!file_exists($dir)) {
return $this->render('@PumukitNewAdmin/Inbox/form_nofile.html.twig', array('dir' => $dir));
}
if (!is_readable($dir)) {
return $this->render('@PumukitNewAdmin/Inbox/form_noperm.html.twig', array('dir' => $dir));
}
return $this->render('@PumukitNewAdmin/Inbox/form.html.twig', array('dir' => $dir, 'onlyDir' => $onlyDir));
}
}