Completed
Branch master (b7ffcb)
by Tomas Norre
17:57
created

TsfeHook   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 113
rs 10
c 0
b 0
f 0
wmc 17
lcom 0
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fe_init() 0 20 4
A fe_feuserInit() 0 13 4
A fe_isOutputting() 0 6 2
C fe_eofe() 0 33 7
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)
0 ignored issues
show
Unused Code introduced by
The parameter $ref is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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!');
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)
0 ignored issues
show
Unused Code introduced by
The parameter $ref is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $ref is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $ref is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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;
142
        }
143
    }
144
}
145