Completed
Push — master ( 470eb5...4fc6d2 )
by Marcus
02:35
created

HCSF   B

Complexity

Total Complexity 29

Size/Duplication

Total Lines 212
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 16

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 29
c 3
b 1
f 1
lcom 1
cbo 16
dl 0
loc 212
rs 8.4614

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
B init() 0 38 4
A setupRequest() 0 16 2
D setupSession() 0 33 9
A setupHardcodedTextcats() 0 14 3
B setupDB() 0 27 1
A setupTextcats() 0 16 1
C setupTwig() 0 37 7
A getServiceManager() 0 4 1
1
<?php
2
3
namespace HaaseIT\HCSF;
4
5
use Zend\ServiceManager\ServiceManager;
6
7
class HCSF
8
{
9
    protected $serviceManager;
10
11
    public function __construct()
12
    {
13
        define('HCSF_BASEDIR', dirname(__DIR__).DIRECTORY_SEPARATOR);
14
        define("DB_ADDRESSFIELDS", 'cust_id, cust_no, cust_email, cust_corp, cust_name, cust_street, cust_zip, cust_town, cust_phone, cust_cellphone, cust_fax, cust_country, cust_group, cust_active, cust_emailverified, cust_tosaccepted, cust_cancellationdisclaimeraccepted');
15
        define("DB_ITEMFIELDS", 'itm_no, itm_name, itm_price, itm_vatid, itm_rg, itm_img, itm_group, itm_data, itm_weight, itml_name_override, itml_text1, itml_text2, itm_index');
16
        define("DB_ITEMGROUPFIELDS", 'itmg_no, itmg_name, itmg_img, itmgt_shorttext, itmgt_details');
17
        define("FILE_PAYPALLOG", 'ipnlog.txt');
18
19
        // set scale for bcmath
20
        bcscale(6);
21
    }
22
23
    public function init()
24
    {
25
        $this->serviceManager = new ServiceManager();
26
27
        $this->setupRequest();
28
29
        HelperConfig::init();
30
        if (HelperConfig::$core['debug']) {
31
            \HaaseIT\Toolbox\Tools::$bEnableDebug = true;
32
        }
33
34
        $this->setupSession();
35
36
        date_default_timezone_set(HelperConfig::$core["defaulttimezone"]);
37
38
        $this->setupHardcodedTextcats();
39
40
        $this->serviceManager->setFactory('db', function () {
41
            return null;
42
        });
43
44
        if (!HelperConfig::$core['maintenancemode']) {
45
            $this->setupDB();
46
            $this->setupTextcats();
47
            HelperConfig::loadNavigation($this->serviceManager);
48
        }
49
50
        $this->setupTwig();
51
52
        if (HelperConfig::$core["enable_module_shop"]) {
53
            $this->serviceManager->setFactory('oItem', function (ServiceManager $serviceManager) {
54
                return new \HaaseIT\HCSF\Shop\Items($serviceManager);
55
            });
56
        }
57
58
        $router = new \HaaseIT\HCSF\Router($this->serviceManager);
59
        return $router->getPage();
60
    }
61
62
    protected function setupRequest()
0 ignored issues
show
Coding Style introduced by
setupRequest uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
63
    {
64
        // PSR-7 Stuff
65
        // Init request object
66
        $this->serviceManager->setFactory('request', function () {
67
            $request = \Zend\Diactoros\ServerRequestFactory::fromGlobals();
68
69
            // cleanup request
70
            $requesturi = urldecode($request->getRequestTarget());
71
            $parsedrequesturi = substr($requesturi, strlen(dirname($_SERVER['PHP_SELF'])));
72
            if (substr($parsedrequesturi, 1, 1) !== '/') {
73
                $parsedrequesturi = '/'.$parsedrequesturi;
74
            }
75
            return $request->withRequestTarget($parsedrequesturi);
76
        });
77
    }
78
79
    protected function setupSession()
0 ignored issues
show
Coding Style introduced by
setupSession uses the super-global variable $_COOKIE which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
setupSession uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
setupSession uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
80
    {
81
        if (HelperConfig::$core["enable_module_customer"] && isset($_COOKIE["acceptscookies"]) && $_COOKIE["acceptscookies"] == 'yes') {
82
// Session handling
83
// session.use_trans_sid wenn nötig aktivieren
84
            ini_set('session.use_only_cookies', 0);
85
            session_name('sid');
86
            if(ini_get('session.use_trans_sid') == 1) {
87
                ini_set('session.use_trans_sid', 0);
88
            }
89
// Session wenn nötig starten
90
            if (session_id() == '') {
91
                session_start();
92
            }
93
94
            // check if the stored ip and ua equals the clients, if not, reset. if not set at all, reset
95
            if (!empty($_SESSION['hijackprevention'])) {
96
                if (
97
                    $_SESSION['hijackprevention']['remote_addr'] != $_SERVER['REMOTE_ADDR']
98
                    ||
99
                    $_SESSION['hijackprevention']['user_agent'] != $_SERVER['HTTP_USER_AGENT']
100
                ) {
101
                    \session_regenerate_id();
102
                    \session_unset();
103
                }
104
            } else {
105
                \session_regenerate_id();
106
                \session_unset();
107
                $_SESSION['hijackprevention']['remote_addr'] = $_SERVER['REMOTE_ADDR'];
108
                $_SESSION['hijackprevention']['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
109
            }
110
        }
111
    }
112
113
    protected function setupHardcodedTextcats()
114
    {
115
        if (file_exists(HCSF_BASEDIR.'src/hardcodedtextcats/'.HelperConfig::$lang.'.php')) {
116
            $HT = require HCSF_BASEDIR.'src/hardcodedtextcats/'.HelperConfig::$lang.'.php';
117
        } else {
118
            if (file_exists(HCSF_BASEDIR.'src/hardcodedtextcats/'.key(HelperConfig::$core["lang_available"]).'.php')) {
119
                $HT = require HCSF_BASEDIR.'src/hardcodedtextcats/'.key(HelperConfig::$core["lang_available"]).'.php';
120
            } else {
121
                $HT = require HCSF_BASEDIR.'src/hardcodedtextcats/de.php';
122
            }
123
        }
124
125
        HardcodedText::init($HT);
126
    }
127
128
    protected function setupDB()
129
    {
130
        $this->serviceManager->setFactory('dbal', function () {
131
            $config = new \Doctrine\DBAL\Configuration();
132
133
            $connectionParams = [
134
                'url' =>
135
                    HelperConfig::$secrets['db_type'].'://'
136
                    .HelperConfig::$secrets['db_user'].':'
137
                    .HelperConfig::$secrets['db_password'].'@'
138
                    .HelperConfig::$secrets['db_server'].'/'
139
                    .HelperConfig::$secrets['db_name'],
140
                'charset' => 'UTF8',
141
                'driverOptions' => [
142
                    \PDO::ATTR_EMULATE_PREPARES => false,
143
                    \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
144
                    \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
145
                ],
146
            ];
147
148
            return \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
149
        });
150
151
        $this->serviceManager->setFactory('db', function (ServiceManager $serviceManager) {
152
            return $serviceManager->get('dbal')->getWrappedConnection();
153
        });
154
    }
155
156
    protected function setupTextcats()
157
    {
158
        $this->serviceManager->setFactory('textcats', function (ServiceManager $serviceManager) {
159
            $langavailable = HelperConfig::$core["lang_available"];
160
            $textcats = new \HaaseIT\Toolbox\Textcat(
161
                HelperConfig::$lang,
162
                $serviceManager->get('db'),
163
                key($langavailable),
164
                HelperConfig::$core['textcatsverbose'],
165
                PATH_LOGS
166
            );
167
            $textcats->loadTextcats();
168
169
            return $textcats;
170
        });
171
    }
172
173
    protected function setupTwig()
174
    {
175
        $this->serviceManager->setFactory('twig', function (ServiceManager $serviceManager) {
176
            $loader = new \Twig_Loader_Filesystem([PATH_BASEDIR.'customviews', HCSF_BASEDIR.'src/views/']);
177
178
            $twig_options = [
179
                'autoescape' => false,
180
                'debug' => (HelperConfig::$core["debug"] ? true : false),
181
            ];
182
            if (HelperConfig::$core["templatecache_enable"] &&
183
                is_dir(PATH_TEMPLATECACHE) && is_writable(PATH_TEMPLATECACHE)) {
184
                $twig_options["cache"] = PATH_TEMPLATECACHE;
185
            }
186
            $twig = new \Twig_Environment($loader, $twig_options);
187
188
            if (HelperConfig::$core['allow_parsing_of_page_content']) {
189
                $twig->addExtension(new \Twig_Extension_StringLoader());
190
            } else { // make sure, template_from_string is callable
191
                $twig->addFunction(new \Twig_SimpleFunction('template_from_string', '\HaaseIT\HCSF\Helper::reachThrough'));
192
            }
193
194
            if (!HelperConfig::$core['maintenancemode']) {
195
                $twig->addFunction(new \Twig_SimpleFunction('T', [$serviceManager->get('textcats'), 'T']));
196
            } else {
197
                $twig->addFunction(new \Twig_SimpleFunction('T', '\HaaseIT\HCSF\Helper::returnEmptyString'));
198
            }
199
200
            $twig->addFunction(new \Twig_SimpleFunction('HT', '\HaaseIT\HCSF\HardcodedText::get'));
201
            $twig->addFunction(new \Twig_SimpleFunction('gFF', '\HaaseIT\Toolbox\Tools::getFormField'));
202
            $twig->addFunction(new \Twig_SimpleFunction('ImgURL', '\HaaseIT\HCSF\Helper::getSignedGlideURL'));
203
            $twig->addFunction(new \Twig_SimpleFunction('callback', 'HaaseIT\HCSF\Helper::twigCallback'));
204
            $twig->addFunction(new \Twig_SimpleFunction('makeLinkHRefWithAddedGetVars', '\HaaseIT\Toolbox\Tools::makeLinkHRefWithAddedGetVars'));
205
            $twig->addFilter(new \Twig_SimpleFilter('decodehtmlentity', 'html_entity_decode'));
206
207
            return $twig;
208
        });
209
    }
210
211
    /**
212
     * @return mixed
213
     */
214
    public function getServiceManager()
215
    {
216
        return $this->serviceManager;
217
    }
218
}