Passed
Push — master ( 7c413a...6f0b36 )
by Andreas
12:34
created

midcom_core_urlmethods::process_unlock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 10
1
<?php
2
/**
3
 * @package midcom
4
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
use Symfony\Component\HttpFoundation\StreamedResponse;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\BinaryFileResponse;
12
use Symfony\Component\HttpFoundation\Response;
13
use Symfony\Component\HttpFoundation\JsonResponse;
14
15
/**
16
 * MidCOM URL methods.
17
 *
18
 * See individual methods for documentation.
19
 *
20
 * @package midcom
21
 */
22
class midcom_core_urlmethods
23
{
24
    public function process_config() : Response
25
    {
26
        return new StreamedResponse(function() {
27
            midcom::get()->style->show_midcom('config-test');
28
        });
29
    }
30
31
    /**
32
     * This method will serve the attachment denoted by the given ID/GUID.
33
     * It should enable caching of browsers for Navigation images and so on.
34
     */
35
    public function process_serveattachmentguid(Request $request, string $guid) : Response
36
    {
37
        $attachment = new midcom_db_attachment($guid);
38
        if (!$attachment->can_do('midgard:autoserve_attachment')) {
39
            throw new midcom_error_notfound('Failed to access attachment: Autoserving denied.');
40
        }
41
42
        // Doublecheck that this is registered
43
        midcom::get()->cache->content->register($attachment->guid);
44
45
        $response = new BinaryFileResponse($attachment->get_path());
46
        $last_modified = (int) $response->getLastModified()->format('U');
47
        $etag = md5("{$last_modified}{$attachment->name}{$attachment->mimetype}{$attachment->guid}");
48
        $response->setEtag($etag);
49
50
        if (!$response->isNotModified($request)) {
51
            $response->prepare($request);
52
53
            if (midcom::get()->config->get('attachment_xsendfile_enable')) {
54
                BinaryFileResponse::trustXSendfileTypeHeader();
55
                $response->headers->set('X-Sendfile-Type', 'X-Sendfile');
56
            }
57
        }
58
        midcom::get()->cache->content->cache_control_headers($response);
59
        // Store metadata in cache so _check_hit() can help us
60
        midcom::get()->cache->content->write_meta_cache('A-' . $etag, $request, $response);
61
        return $response;
62
    }
63
64
    /**
65
     * This will resolve the given GUID into the MidCOM NAP tree, relocating to the
66
     * URL corresponding to the node/leaf. The Permalink can be created by using the
67
     * key MIDCOM_NAV_PERMALINK of any NAP data array. Upon resolving it, MidCOM will
68
     * relocate to the automatically computed MIDCOM_NAV_FULLURL.
69
     *
70
     * @throws midcom_error_notfound
71
     */
72
    public function process_permalink(string $guid) : Response
73
    {
74
        $destination = midcom::get()->permalinks->resolve_permalink($guid);
75
        if ($destination === null) {
76
            throw new midcom_error_notfound("This Permalink is unknown.");
77
        }
78
79
        // We use "302 Found" here so that search engines and others will keep using the PermaLink instead of the temporary
80
        return new midcom_response_relocate($destination, 302);
81
    }
82
83
    /**
84
     * will clear the cache of the current site
85
     */
86
    public function invalidate_cache(Request $request)
87
    {
88
        if (!in_array($request->getClientIp(), midcom::get()->config->get_array('indexer_reindex_allowed_ips'))) {
89
            midcom::get()->auth->require_valid_user('basic');
90
            midcom::get()->auth->require_admin_user();
91
        }
92
93
        // The annoying thing about APCU is that it's per SAPI, so we can't clear the server
94
        // cache from the CLI. So instead we do it here
95
        if (function_exists('apcu_clear_cache')) {
96
            apcu_clear_cache();
97
        }
98
99
        midcom::get()->cache->content->enable_live_mode();
100
        midcom::get()->cache->invalidate_all();
101
        midcom::get()->uimessages->add(
102
            midcom::get()->i18n->get_string('MidCOM', 'midcom'),
103
            midcom::get()->i18n->get_string('cache invalidation successful', 'midcom'),
104
        );
105
106
        $url = $request->server->get('HTTP_REFERER') ?: midcom_connection::get_url('self');
107
        return new midcom_response_relocate($url);
0 ignored issues
show
Bug introduced by
It seems like $url can also be of type null; however, parameter $url of midcom_response_relocate::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
        return new midcom_response_relocate(/** @scrutinizer ignore-type */ $url);
Loading history...
108
    }
109
110
    public function process_logout(Request $request, string $url) : Response
111
    {
112
        midcom::get()->cache->content->no_cache();
113
        midcom::get()->auth->logout();
114
        return $this->redirect($request, $url);
115
    }
116
117
    public function process_login(Request $request, string $url) : Response
118
    {
119
        if (midcom::get()->auth->is_valid_user()) {
120
            return $this->redirect($request, $url);
121
        }
122
        return new midcom_response_login;
123
    }
124
125
    public function process_unlock(string $guid) : Response
126
    {
127
        $object = midcom::get()->dbfactory->get_object_by_guid($guid);
128
        return new JsonResponse([
129
            'success' => $object->unlock()
130
        ]);
131
    }
132
133
    private function redirect(Request $request, string $redirect_to) : Response
134
    {
135
        if (!empty($request->server->get('QUERY_STRING'))) {
136
            $redirect_to .= '?' . $request->getQueryString();
137
        }
138
        return new midcom_response_relocate($redirect_to);
139
    }
140
141
    /**
142
     * Allows you to execute certain php files directly, in full MidCOM context.
143
     * The argument is the name of the component, which holds the script to be
144
     * executed. Script files are searched in the subdirectory "exec" of the component.
145
     * If you use "midcom" as component name, MidCOM core scripts, located in
146
     * lib/midcom/exec will be accessible. The next argument on the command line must
147
     * be the name of the script file. Accessing subdirectories is not possible, only
148
     * a single argument will be taken.
149
     *
150
     * The scripts executed need to do their own permission checks, they will work with
151
     * the credentials of the current MidCOM instance unconditionally.
152
     *
153
     * Example: http://$host/midcom-exec-midcom/reindex.php
154
     *
155
     * The script file is executed in the cache's live mode to allow for long running
156
     * scripts (just produce any output regularly, or Apache will kill you after ~ 2 mins.).
157
     *
158
     * The remaining arguments will be placed into the global $argv.
159
     *
160
     * @see midcom_services_cache_module_content::enable_live_mode()
161
     */
162
    public function process_exec(Request $request, string $component, string $filename, string $argv) : Response
163
    {
164
        $componentloader = midcom::get()->componentloader;
165
        $path = $componentloader->path_to_snippetpath($component) . '/exec/' . $filename;
166
        if (!is_file($path)) {
167
            throw new midcom_error_notfound("File not found.");
168
        }
169
170
        // We seem to be in a valid place
171
        $context = $request->attributes->get('context');
172
        if ($component !== 'midcom') {
173
            $context->set_key(MIDCOM_CONTEXT_COMPONENT, $component);
174
        }
175
        // Collect remaining arguments and put them to global vars.
176
        $GLOBALS['argv'] = explode('/', $argv);
177
178
        return new StreamedResponse(function() use ($path) {
179
            midcom::get()->cache->content->enable_live_mode();
180
181
            // Exec the file with the current permissions.
182
            require $path;
183
        });
184
    }
185
}
186