Completed
Push — master ( 1b04e6...82e336 )
by Tomas Norre
08:07
created

Classes/Hooks/TsfeHook.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace AOE\Crawler\Hooks;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2016 AOE GmbH <[email protected]>
8
 *
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
/**
29
 * Class TsfeHook
30
 * @package AOE\Crawler\Hooks
31
 */
32
class TsfeHook
33
{
34
    /**
35
     * Initialization hook (called after database connection)
36
     * Takes the "HTTP_X_T3CRAWLER" header and looks up queue record and verifies if the session comes from the system (by comparing hashes)
37
     *
38
     * @param array Parameters from frontend
39
     * @param object TSFE object (reference under PHP5)
40
     * @return void
41
     *
42
     * TODO: Write Unit test
43
     */
44
    public function fe_init(&$params, $ref)
45
    {
46
47
        // Authenticate crawler request:
48
        if (isset($_SERVER['HTTP_X_T3CRAWLER'])) {
49
            //@todo: ask service to exclude current call for special reasons: for example no relevance because the language version is not affected
50
51
            list($queueId, $hash) = explode(':', $_SERVER['HTTP_X_T3CRAWLER']);
52
            list($queueRec) = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('*', 'tx_crawler_queue', 'qid=' . intval($queueId));
53
54
            // If a crawler record was found and hash was matching, set it up:
55
            if (is_array($queueRec) && $hash === md5($queueRec['qid'] . '|' . $queueRec['set_id'] . '|' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'])) {
56
                $params['pObj']->applicationData['tx_crawler']['running'] = true;
57
                $params['pObj']->applicationData['tx_crawler']['parameters'] = unserialize($queueRec['parameters']);
58
                $params['pObj']->applicationData['tx_crawler']['log'] = [];
59
            } else {
60
                die('No crawler entry found!');
0 ignored issues
show
Coding Style Compatibility introduced by
The method fe_init() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
61
            }
62
        }
63
    }
64
65
    /**
66
     * Initialization of FE-user, setting the user-group list if applicable.
67
     *
68
     * @param array  Parameters from frontend
69
     * @param object  TSFE object
70
     * @return void
71
     *
72
     * TODO: Write Unit test
73
     */
74
    public function fe_feuserInit(&$params, $ref)
75
    {
76
        if ($params['pObj']->applicationData['tx_crawler']['running']) {
77
            $grList = $params['pObj']->applicationData['tx_crawler']['parameters']['feUserGroupList'];
78
            if ($grList) {
79
                if (!is_array($params['pObj']->fe_user->user)) {
80
                    $params['pObj']->fe_user->user = [];
81
                }
82
                $params['pObj']->fe_user->user['usergroup'] = $grList;
83
                $params['pObj']->applicationData['tx_crawler']['log'][] = 'User Groups: ' . $grList;
84
            }
85
        }
86
    }
87
88
    /**
89
     * Whether to output rendered content or not. If the crawler is running, the rendered output is never outputted!
90
     *
91
     * @param array  Parameters from frontend
92
     * @param object  TSFE object
93
     * @return void
94
     *
95
     * TODO: Write Unit test
96
     */
97
    public function fe_isOutputting(&$params, $ref)
98
    {
99
        if ($params['pObj']->applicationData['tx_crawler']['running']) {
100
            $params['enableOutput'] = false;
101
        }
102
    }
103
104
    /**
105
     * Concluding: Outputting serialized information instead of letting rendered content out.
106
     *
107
     * @param array  Parameters from frontend
108
     * @param object  TSFE object
109
     * @return void
110
     */
111
    public function fe_eofe(&$params, $ref)
112
    {
113
        if ($params['pObj']->applicationData['tx_crawler']['running']) {
114
            $params['pObj']->applicationData['tx_crawler']['vars'] = [
115
                'id' => $params['pObj']->id,
116
                'gr_list' => $params['pObj']->gr_list,
117
                'no_cache' => $params['pObj']->no_cache,
118
            ];
119
120
            /**
121
             * Required because some extensions (staticpub) might never be requested to run due to some Core side effects
122
             * and since this is considered as error the crawler should handle it properly
123
             */
124
            if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['pollSuccess'])) {
125
                foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['pollSuccess'] as $pollable) {
126
                    if (is_array($params['pObj']->applicationData['tx_crawler']['content']['parameters']['procInstructions']) && in_array($pollable, $params['pObj']->applicationData['tx_crawler']['content']['parameters']['procInstructions'])) {
127
                        if (empty($params['pObj']->applicationData['tx_crawler']['success'][$pollable])) {
128
                            $params['pObj']->applicationData['tx_crawler']['errorlog'][] = 'Error: Pollable extension (' . $pollable . ') did not complete successfully.';
129
                        }
130
                    }
131
                }
132
            }
133
134
            // Output log data for crawler (serialized content):
135
            $str = serialize($params['pObj']->applicationData['tx_crawler']);
136
            //just make sure that no other output distracts this
137
            ob_clean();
138
            header('Content-Length: ' . strlen($str));
139
            echo $str;
140
            // Exit since we don't want anymore output!
141
            exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method fe_eofe() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
142
        }
143
    }
144
}
145