Issues (3627)

CoreBundle/Controller/Api/ThemeApiController.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2016 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\CoreBundle\Controller\Api;
13
14
use Mautic\ApiBundle\Controller\CommonApiController;
15
use Mautic\CoreBundle\Helper\InputHelper;
16
use Symfony\Component\HttpFoundation\BinaryFileResponse;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
20
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
21
22
/**
23
 * Class ThemeApiController.
24
 */
25
class ThemeApiController extends CommonApiController
26
{
27
    /**
28
     * @var Mautic\CoreBundle\Helper\ThemeHelper
29
     */
30
    protected $themeHelper;
31
32
    public function initialize(FilterControllerEvent $event)
33
    {
34
        $this->themeHelper = $this->container->get('mautic.helper.theme');
35
36
        parent::initialize($event);
37
    }
38
39
    /**
40
     * Accepts the zip file and installs the theme from it.
41
     *
42
     * @return \Symfony\Component\HttpFoundation\JsonResponse
43
     */
44
    public function newAction(Request $request)
45
    {
46
        if (!$this->security->isGranted('core:themes:create')) {
47
            return $this->accessDenied();
48
        }
49
50
        $response  = ['success' => false];
51
        $themeZip  = $request->files->get('file');
52
        $extension = $themeZip->getClientOriginalExtension();
53
54
        if (!$themeZip) {
55
            return $this->returnError(
56
                $this->translator->trans('mautic.core.theme.upload.empty', [], 'validators'),
57
                Response::HTTP_BAD_REQUEST
58
            );
59
        } elseif ('zip' !== $extension) {
60
            return $this->returnError(
61
                $this->translator->trans('mautic.core.not.allowed.file.extension', ['%extension%' => $extension], 'validators'),
62
                Response::HTTP_BAD_REQUEST
63
            );
64
        } else {
65
            $fileName  = InputHelper::filename($themeZip->getClientOriginalName());
66
            $themeName = basename($fileName, '.zip');
0 ignored issues
show
The assignment to $themeName is dead and can be removed.
Loading history...
67
            $dir       = $this->get('mautic.helper.paths')->getSystemPath('themes', true);
68
69
            if (!empty($themeZip)) {
70
                try {
71
                    $themeZip->move($dir, $fileName);
72
                    $response['success'] = $this->themeHelper->install($dir.'/'.$fileName);
73
                } catch (\Exception $e) {
74
                    return $this->returnError(
75
                        $this->translator->trans($e->getMessage(), [], 'validators')
76
                    );
77
                }
78
            } else {
79
                return $this->returnError(
80
                    $this->translator->trans('mautic.dashboard.upload.filenotfound', [], 'validators')
81
                );
82
            }
83
        }
84
85
        $view = $this->view($response);
86
87
        return $this->handleView($view);
88
    }
89
90
    /**
91
     * Get zip file of a theme.
92
     *
93
     * @param string $theme dir name
94
     *
95
     * @return BinaryFileResponse
96
     */
97
    public function getAction($theme)
98
    {
99
        if (!$this->security->isGranted('core:themes:view')) {
100
            return $this->accessDenied();
101
        }
102
103
        try {
104
            $themeZip = $this->themeHelper->zip($theme);
105
        } catch (\Exception $e) {
106
            return $this->returnError($e->getMessage());
107
        }
108
109
        if (!$themeZip) {
110
            return $this->returnError(
111
                $this->translator->trans(
112
                    'mautic.core.dir.not.accesssible',
113
                    ['%dir%' => $theme]
114
                )
115
            );
116
        }
117
118
        $response = new BinaryFileResponse($themeZip);
119
        $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT);
120
121
        return $response;
122
    }
123
124
    /**
125
     * List the folders (themes) in the /themes directory.
126
     *
127
     * @return \Symfony\Component\HttpFoundation\JsonResponse
128
     */
129
    public function listAction()
130
    {
131
        if (!$this->security->isGranted('core:themes:view')) {
132
            return $this->accessDenied();
133
        }
134
135
        try {
136
            $themes = $this->themeHelper->getInstalledThemes('all', true, false, false);
137
        } catch (\Exception $e) {
138
            return $this->returnError($e->getMessage());
139
        }
140
141
        $view = $this->view(['themes' => $themes]);
142
143
        return $this->handleView($view);
144
    }
145
146
    /**
147
     * Delete a theme.
148
     *
149
     * @param string $theme
150
     *
151
     * @return \Symfony\Component\HttpFoundation\JsonResponse
152
     */
153
    public function deleteAction($theme)
154
    {
155
        if (!$this->security->isGranted('core:themes:delete')) {
156
            return $this->accessDenied();
157
        }
158
159
        try {
160
            $this->themeHelper->delete($theme);
161
            $response = ['success' => true];
162
        } catch (\Exception $e) {
163
            return $this->returnError($e->getMessage());
164
        }
165
166
        $view = $this->view($response);
167
168
        return $this->handleView($view);
169
    }
170
}
171