Completed
Push — master ( 17598f...0cca5d )
by Tim
14:26
created

Classes/Controller/PluginController.php (5 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
3
namespace FRUIT\Popup\Controller;
4
5
use TYPO3\CMS\Core\Utility\GeneralUtility;
6
7
/***************************************************************
8
 *  Copyright notice
9
 *
10
 *  (c) 2009 Tim Lochmueller <[email protected]>
11
 *  2003-2006 Martin Kutschker <[email protected]>
12
 *  2003: Traktor Wien (formerly Global Spanking Industries)
13
 *  2005: ACTIVE SOLUTION Software AG
14
 *  All rights reserved
15
 *
16
 *  This script is part of the Typo3 project. The Typo3 project is
17
 *  free software; you can redistribute it and/or modify
18
 *  it under the terms of the GNU General Public License as published by
19
 *  the Free Software Foundation; either version 2 of the License, or
20
 *  (at your option) any later version.
21
 *
22
 *  The GNU General Public License can be found at
23
 *  http://www.gnu.org/copyleft/gpl.html.
24
 *
25
 *  This script is distributed in the hope that it will be useful,
26
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
27
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28
 *  GNU General Public License for more details.
29
 *
30
 *  This copyright notice MUST APPEAR in all copies of the script!
31
 ***************************************************************/
32
class PluginController extends \TYPO3\CMS\Frontend\Plugin\AbstractPlugin
33
{
34
35
    /**
36
     * Same as class name
37
     */
38
    public $prefixId = 'tx_popup_pi1';
39
40
41
    /**
42
     * Path to this script relative to the extension dir.
43
     */
44
    public $scriptRelPath = 'pi1/class.tx_popup_pi1.php';
45
46
47
    /**
48
     * The extension key.
49
     */
50
    public $extKey = 'popup';
51
52
53
    /**
54
     * Init the popup frontend plugin
55
     *
56
     * @return void
57
     */
58
    protected function init()
59
    {
60
        $this->popup = GeneralUtility::makeInstance('FRUIT\\Popup\\Popup');
0 ignored issues
show
The property popup does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
61
        $this->allowedParams = $this->popup->allowedParams;
62
        $this->customParams = $this->popup->advancedParams;
0 ignored issues
show
The property customParams does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
63
    } # function - init
64
65
66
    /**
67
     * Generate JS code for opening pop-up
68
     * Main Plugin function for T3
69
     *
70
     * @param $content    String
71
     * @param $conf        Array    Plugin configuration
72
     * @return The Plugin Output
73
     */
74
    public function main($content, $conf)
0 ignored issues
show
The parameter $content 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
        // Init
77
        $this->init();
78
79
80
        $link = $this->cObj->data['tx_popup_auto'];
81
        if (!$link) {
82
            GeneralUtility::devLog('No link defined', $this->extKey);
83
            return '';
84
        }
85
86
        // get session data
87
        if ($conf['advancedParams.']['once_per_session']) {
88
            $popups = $GLOBALS['TSFE']->fe_user->getKey('ses', $this->prefixId);
89
            if ($conf['advancedParams.']['once_per_link']) {
90
                if ($popups['links'][$link]) {
91
                    GeneralUtility::devLog('Pop-up link "' . $link . '" already shown.', $this->extKey);
92
                    return '';
93
                } else {
94
                    $popups['links'][$link] = 1;
95
                }
96
            } else {
97
                if ($popups['pages'][$GLOBALS['TSFE']->id]) { // we have been on the current page
98
                    GeneralUtility::devLog('Pop-up already shown on this page.', $this->extKey);
99
                    return '';
100
                } else {
101
                    $popups['pages'][$GLOBALS['TSFE']->id] = 1;
102
                }
103
            }
104
            $GLOBALS['TSFE']->fe_user->setKey('ses', $this->prefixId, $popups);
105
        }
106
107
        // create the url
108
        $url = $this->cObj->getTypoLink_URL($link);
109
        if (!$url) {
110
            GeneralUtility::devLog('No valid pop-up (e.g. a hidden page):' . $link, $this->extKey);
111
            return '';
112
        }
113
114
        // get the JS window parameters directly (protect from errors in TS?)
115
        $params = $conf['allowedParams.'];
116
117
        // get the custom parameters
118
        $cParams = [];
119
        foreach ($this->customParams as $name => $type) {
120
            $v = $conf['advancedParams.'][$name];
121
            $cParams[$name] = ($v === '1' || $v == 'yes' || $v == 'on') ? true : false;
122
        }
123
124
125
        // thanks to Alex Widschwendter
126
        if ($cParams['maximize']) {
127
            $params['left'] = 0;
128
            $params['top'] = 0;
129
            $params['width'] = '\' + screen.width + \'';
130
            $params['height'] = '\' + screen.height + \'';
131
        } // thanks to Daniel Rampanelli
132
        elseif ($cParams['center'] && $params['width'] > 0 && $params['height'] > 0) {
133
            $params['left'] = '\' + ((screen.width - ' . $params['width'] . ') / 2) + \'';
134
            $params['top'] = '\' + ((screen.height - ' . $params['height'] . ') / 2) + \'';
135
            $params['screenX'] = '\' + ((screen.width - ' . $params['width'] . ') / 2) + \'';
136
            $params['screenY'] = '\' + ((screen.height - ' . $params['height'] . ') / 2) + \'';
137
        }
138
139
        while (list($key, $val) = each($params)) {
140
            if (isset($val) && $val != '') {
141
                $tmp_params[] = $key . '=' . $val;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$tmp_params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $tmp_params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
142
            }
143
        }
144
        $params = implode(",", $tmp_params);
0 ignored issues
show
The variable $tmp_params does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
145
146
147
        // Build Javascript
148
        $content = '<script type="text/javascript">
149
		/*<![CDATA[*/
150
		<!--
151
		';
152
        $window = uniqid('_popup_');
153
154
        $content .= "var $window = window.open('$url', 'Window', '$params');\n";
155
156
        // thanks to Tom Binder for the timeout
157
        if ($cParams['popunder']) {
158
            $content .= "if ($window) { window.setTimeout('$window.blur()',500); window.focus(); }";
159
        } else {
160
            $content .= "if ($window) { window.setTimeout('$window.focus()',500); }";
161
        }
162
163
            $content .= "\n// -->\n/*]]>*/</script>\n";
164
165
            return $content;
166
    }
167
}
168