PagantisConfigModuleFrontController   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 79
c 1
b 0
f 0
dl 0
loc 138
rs 10
wmc 20

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getMethod() 0 3 1
A initContent() 0 17 3
B postMethod() 0 48 8
A authorize() 0 14 4
A getExtraConfigs() 0 27 4
1
<?php
2
/**
3
 * This file is part of the official Pagantis module for PrestaShop.
4
 *
5
 * @author    Pagantis <[email protected]>
6
 * @copyright 2019 Pagantis
7
 * @license   proprietary
8
 */
9
10
/**
11
 * Class PagantisLogModuleFrontController
12
 */
13
class PagantisConfigModuleFrontController extends ModuleFrontController
0 ignored issues
show
Bug introduced by
The type ModuleFrontController was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
{
15
    /**
16
     * Initial method
17
     */
18
    public function initContent()
19
    {
20
        $this->authorize();
21
        $method = Tools::strtolower($_SERVER['REQUEST_METHOD']) . "Method";
0 ignored issues
show
Bug introduced by
The type Tools was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
        $params = (_PS_VERSION_ < 1.6) ? $_POST + $_GET : Tools::getAllValues();
0 ignored issues
show
Bug introduced by
The constant _PS_VERSION_ was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
23
        if (method_exists($this, $method)) {
24
            header('HTTP/1.1 200 Ok', true, 200);
25
            header('Content-Type: application/json', true);
26
            $result = json_encode($this->{$method}($params['product']));
27
            header('Content-Length: ' . Tools::strlen($result));
28
            echo $result;
29
            exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
30
        }
31
        header('HTTP/1.1 405 Method not allowed', true, 405);
32
        header('Content-Type: application/json', true);
33
34
        exit();
35
    }
36
37
    /**
38
     * @param null $product
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $product is correct as it would always require null to be passed?
Loading history...
39
     * @return array
40
     */
41
    public function getExtraConfigs($product = null)
42
    {
43
        $availableProductsSQL = 'select * from ' . _DB_PREFIX_. 'pagantis_config where config = \'PRODUCTS\'';
0 ignored issues
show
Bug introduced by
The constant _DB_PREFIX_ was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
44
        $dbProducts = Db::getInstance()->executeS($availableProductsSQL);
0 ignored issues
show
Bug introduced by
The type Db was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
45
        $availableProductsArray = explode(',', $dbProducts[0]['value']);
46
        $unrequestedProducts = array_diff($availableProductsArray, array($product));
47
        $unrequestedProductSQL = '';
48
        foreach ($unrequestedProducts as $unrequestedProduct) {
49
            $unrequestedProductSQL .= "'". $unrequestedProduct . "',";
50
        }
51
        $unrequestedProductSQL = rtrim($unrequestedProductSQL, ",");
52
        $sql_content = 'select * from ' . _DB_PREFIX_.
53
            'pagantis_config where config not in (' . $unrequestedProductSQL . ') 
54
             and config not like (\'PAGANTIS_%\')  and config not like (\'PMT_%\') ';
55
56
        $dbConfigs = Db::getInstance()->executeS($sql_content);
57
58
        $simpleDbConfigs = array();
59
        foreach ($dbConfigs as $config) {
60
            $productConfigs = json_decode($config['value'], true);
61
            if ($productConfigs) {
62
                $simpleDbConfigs = array_merge($simpleDbConfigs, $productConfigs);
63
            }
64
            $simpleDbConfigs[$config['config']] = $config['value'];
65
        }
66
        unset($simpleDbConfigs[$product]);
67
        return $simpleDbConfigs;
68
    }
69
70
    /**
71
     * Update POST params in DB
72
     */
73
    public function postMethod()
74
    {
75
76
        $errors = array();
77
        $params = (_PS_VERSION_ < 1.6) ? $_POST + $_GET : Tools::getAllValues();
0 ignored issues
show
Bug introduced by
The constant _PS_VERSION_ was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
78
        unset($params['fc']);
79
        unset($params['module']);
80
        unset($params['controller']);
81
        unset($params['secret']);
82
        $product = $params['product'];
83
        unset($params['product']);
84
        $productConfigsSQL = 'select * from ' . _DB_PREFIX_.
0 ignored issues
show
Bug introduced by
The constant _DB_PREFIX_ was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
85
            'pagantis_config where config = \''. pSQL($product) . '\'';
0 ignored issues
show
Bug introduced by
The function pSQL was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

85
            'pagantis_config where config = \''. /** @scrutinizer ignore-call */ pSQL($product) . '\'';
Loading history...
86
        $productConfigs = Db::getInstance()->executeS($productConfigsSQL);
87
        $availableProductsArray = json_decode($productConfigs[0]['value'], true);
88
        if (count($params) > 0) {
89
            foreach ($params as $config => $value) {
90
                if (array_key_exists($config, $availableProductsArray)) {
91
                    $availableProductsArray[$config] = $value;
92
                } else {
93
                    $defaultConfigs = $this->getExtraConfigs($product);
94
                    if (isset($defaultConfigs[$config])) {
95
                        if ($config !== 'product') {
96
                            Db::getInstance()->update(
97
                                'pagantis_config',
98
                                array('value' => pSQL($value)),
99
                                'config = \''. pSQL($config) .'\''
100
                            );
101
                        }
102
                    } else {
103
                        $errors[$config] = $value;
104
                    }
105
                }
106
                Db::getInstance()->update(
107
                    'pagantis_config',
108
                    array('value' => json_encode($availableProductsArray)),
109
                    'config = \''. pSQL($product) .'\''
110
                );
111
            }
112
        } else {
113
            $errors['NO_POST_DATA'] = 'No post data provided';
114
        }
115
116
        $dbConfigs = $this->getMethod($product);
117
        if (count($errors) > 0) {
118
            $dbConfigs['__ERRORS__'] = $errors;
119
        }
120
        return $dbConfigs;
121
    }
122
123
    /**
124
     * PTM configs
125
     *
126
     * @param null $product
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $product is correct as it would always require null to be passed?
Loading history...
127
     * @return array
128
     */
129
    public function getMethod($product = null)
130
    {
131
        return $this->getExtraConfigs($product);
132
    }
133
134
    /**
135
     * @return bool|null
136
     */
137
    public function authorize()
138
    {
139
        $productCode = Tools::getValue('product', false);
140
        $products = explode(',', Pagantis::getExtraConfig('PRODUCTS', null));
141
        $privateKey = Configuration::get(Tools::strtolower($productCode) . '_private_key');
0 ignored issues
show
Bug introduced by
The type Configuration was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
142
        $privateKeyGet = Tools::getValue('secret', false);
143
        if (!empty($privateKeyGet) && $privateKeyGet === $privateKey && in_array(Tools::strtoupper($productCode), $products)) {
144
            return true;
145
        }
146
147
        header('HTTP/1.1 403 Forbidden', true, 403);
148
        header('Content-Type: application/json', true);
149
150
        exit();
151
    }
152
}
153