index.php 7.99 KB
Newer Older
Henning Leutz's avatar
Henning Leutz committed
1 2
<?php

3 4 5 6 7 8 9 10 11 12 13 14 15
/**
 *  _______          _________ _______  _______  _______  _______
 * (  ___  )|\     /|\__   __/(  ___  )(  ___  )(  ____ \(  ____ )
 * | (   ) || )   ( |   ) (   | (   ) || (   ) || (    \/| (    )|
 * | |   | || |   | |   | |   | |   | || |   | || (__    | (____)|
 * | |   | || |   | |   | |   | |   | || |   | ||  __)   |     __)
 * | | /\| || |   | |   | |   | | /\| || | /\| || (      | (\ (
 * | (_\ \ || (___) |___) (___| (_\ \ || (_\ \ || (____/\| ) \ \__
 * (____\/_)(_______)\_______/(____\/_)(____\/_)(_______/|/   \__/
 *
 * @author www.pcsg.com (Henning Leutz)
 */

16

17
\error_reporting(E_ALL);
Henning Leutz's avatar
Henning Leutz committed
18

19 20
if (!\defined('QUIQQER_SYSTEM')) {
    \define('QUIQQER_SYSTEM', true);
21
}
22

Henning Leutz's avatar
Henning Leutz committed
23
// Mailto
24
if (isset($_REQUEST['_url'])
25
    && \strpos($_REQUEST['_url'], '[mailto]') !== false
26
) {
27 28
    $addr = \str_replace('[mailto]', '', $_REQUEST['_url']);
    list($user, $host) = \explode("[at]", $addr);
Henning Leutz's avatar
Henning Leutz committed
29

30
    if (isset($user) && isset($host)) {
31
        \header("Location: mailto:".$user."@".$host);
Henning Leutz's avatar
Henning Leutz committed
32 33
        exit;
    }
Henning Leutz's avatar
Henning Leutz committed
34 35
}

36
use \Symfony\Component\HttpFoundation\Response;
37 38 39 40 41

use QUI\Utils\System\Debug;
use QUI\Utils\Security\Orthos;
use QUI\System\Log;

42 43
try {
    require_once 'bootstrap.php';
Henning Leutz's avatar
Henning Leutz committed
44

45
    $Response = QUI::getGlobalResponse();
46
    $Request  = QUI::getRequest();
Henning Leutz's avatar
Henning Leutz committed
47

48 49
    // UTF 8 Prüfung für umlaute in url
    if (isset($_REQUEST['_url'])) {
50
        $_REQUEST['_url'] = QUI\Utils\StringHelper::toUTF8($_REQUEST['_url']);
51
    }
52

53 54
    //\QUI\Utils\System\Debug::$run = true;
    Debug::marker('index start');
55

56 57 58
    // check if one projects exists
    if (!QUI::getProjectManager()->count()) {
        $Response->setStatusCode(Response::HTTP_NOT_FOUND);
59

60 61
        $Response->setContent(
            '<div style="text-align: center; margin-top: 100px;">
62
                <img src="'.URL_BIN_DIR.'quiqqer_logo.png" style="max-width: 100%;" />
63 64
            </div>'
        );
Henning Leutz's avatar
Henning Leutz committed
65

66
        $Response->send();
67
        QUI::getEvents()->fireEvent('responseSent', [$Response]);
68 69
        exit;
    }
70

71 72 73 74 75 76 77
    // start
    $Rewrite = QUI::getRewrite();
    $Rewrite->exec();

    QUI::getLocale()->setCurrent(
        $Rewrite->getProject()->getLang()
    );
78

Henning Leutz's avatar
Henning Leutz committed
79

80
    // switch off language
81 82 83 84
    if (isset($_REQUEST['lang']) && $_REQUEST['lang'] == 'false') {
        $Response->headers->set('X-Robots-Tag', 'noindex, nofollow');
        QUI::getLocale()->no_translation = true;
    }
Henning Leutz's avatar
Henning Leutz committed
85

86 87
    $Project = $Rewrite->getProject();
    $Site    = $Rewrite->getSite();
88
    $Engine  = QUI::getTemplateManager()->getEngine();
Henning Leutz's avatar
Henning Leutz committed
89

90
    $Site->load();
Henning Leutz's avatar
Henning Leutz committed
91

92 93 94 95
    if (isset($Locale)) {
        unset($Locale);
        $Locale = QUI::getLocale();
    }
Henning Leutz's avatar
Henning Leutz committed
96

97
    if (\defined('LOGIN_FAILED')
98 99 100 101 102 103
        || isset($_POST['login'])
        || isset($_GET['logout'])
    ) {
        $Site->setAttribute('nocache', true);
    }

104
    /**
105
     * Referral System
106 107 108
     */
    if (isset($_REQUEST['ref'])) {
        QUI::getSession()->set('ref', Orthos::clear($_REQUEST['ref']));
Henning Leutz's avatar
Henning Leutz committed
109
    }
Henning Leutz's avatar
Henning Leutz committed
110

111
    /**
112
     * maintenance work
113 114
     */
    if (QUI::conf('globals', 'maintenance')
115
        && !(QUI::getUserBySession()->getId() && QUI::getUserBySession()->isSU())
116 117 118 119 120 121 122
    ) {
        $Response->setStatusCode(Response::HTTP_SERVICE_UNAVAILABLE);
        $Response->headers->set('X-Powered-By', '');
        $Response->headers->set('Retry-After', 3600);

        $Smarty = QUI::getTemplateManager()->getEngine();

123
        $Smarty->assign([
Henning Leutz's avatar
Henning Leutz committed
124 125
            'Project'     => $Project,
            'URL_DIR'     => URL_DIR,
126 127 128 129 130
            'URL_BIN_DIR' => URL_BIN_DIR,
            'URL_LIB_DIR' => URL_LIB_DIR,
            'URL_VAR_DIR' => URL_VAR_DIR,
            'URL_OPT_DIR' => URL_OPT_DIR,
            'URL_USR_DIR' => URL_USR_DIR,
131 132
            'URL_TPL_DIR' => URL_USR_DIR.$Project->getName().'/',
            'TPL_DIR'     => OPT_DIR.$Project->getName().'/',
133
        ]);
134

135 136
        $file  = LIB_DIR.'templates/maintenance.html';
        $pfile = USR_DIR.$Project->getName().'/lib/maintenance.html';
137

138
        if (\file_exists($pfile)) {
139 140 141 142 143
            $file = $pfile;
        }

        $Response->setContent($Smarty->fetch($file));
        $Response->send();
144 145

        QUI::getEvents()->fireEvent('responseSent', [$Response]);
146 147
        exit;
    }
Henning Leutz's avatar
Henning Leutz committed
148

149 150 151
    // Event onstart
    QUI::getEvents()->fireEvent('start');
    Debug::marker('objekte initialisiert');
Henning Leutz's avatar
Henning Leutz committed
152

153
    $siteCachePath = $Site->getCachePath().'/'.\md5(QUI::getRequest()->getRequestUri());
154

155 156 157 158 159 160
    // Check if user is allowed to view Site and set appropriate error code if not
    if ($Site instanceof QUI\Projects\Site\PermissionDenied) {
        $statusCode = (int)QUI::conf('globals', 'sitePermissionDeniedErrorCode');
        $Response->setStatusCode($statusCode);
    }

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
    // url query check
    // if url query exists, dont ask the cache and dont create the cache
    // @todo collect get query lists and consider the query params
    $query = $Request->getQueryString();

    if (\is_string($query)) {
        \parse_str($query, $query);
    }

    if (!\is_array($query)) {
        $query = [];
    }

    if (isset($query['_url'])) {
        unset($query['_url']);
    }
177 178 179

    // if cache exists, and cache should also be used
    if (CACHE
180 181
        && $Site->getAttribute('nocache') != true
        && !QUI::getUsers()->isAuth(QUI::getUserBySession())
182
        && empty($query)
183
        && $Rewrite->getHeaderCode() === 200
184
    ) {
185 186 187 188 189 190 191 192
        try {
            $cache_content = QUI\Cache\Manager::get($siteCachePath);
            $content       = $Rewrite->outputFilter($cache_content);
            $_content      = $content;

            QUI::getEvents()->fireEvent('requestOutput', [&$_content]);
            $Response->setContent($content);
            $Response->send();
193 194

            QUI::getEvents()->fireEvent('responseSent', [$Response]);
195 196 197 198
            exit;
        } catch (\Exception $Exception) {
            Log::writeDebugException($Exception);
        }
199
    }
Henning Leutz's avatar
Henning Leutz committed
200

201
    // Template Content generating
202 203
    try {
        $Template = new QUI\Template();
204
        $content  = $Template->fetchSite($Site);
Henning Leutz's avatar
Henning Leutz committed
205

206
        Debug::marker('fetch Template');
Henning Leutz's avatar
Henning Leutz committed
207

208 209 210
        $content = $Rewrite->outputFilter($content);
        $content = QUI\Control\Manager::setCSSToHead($content);
        Debug::marker('output done');
Henning Leutz's avatar
Henning Leutz committed
211

212
        QUI::getEvents()->fireEvent('requestOutput', [&$content]);
213

214 215
        $Response->setContent($content);
        Debug::marker('content done');
Henning Leutz's avatar
Henning Leutz committed
216

217 218 219
        // cachefile erstellen
        if ($Site->getAttribute('nocache') != true
            && !QUI::getUsers()->isAuth(QUI::getUserBySession())
220
            && empty($query)
221
            && $Rewrite->getHeaderCode() === 200
222
        ) {
223 224 225 226 227
            try {
                QUI\Cache\Manager::set($siteCachePath, $content);
            } catch (\Exception $Exception) {
                Log::writeDebugException($Exception);
            }
228
        }
229

230
        if (Debug::$run) {
231
            Log::writeRecursive(Debug::output());
232
        }
Henning Leutz's avatar
Henning Leutz committed
233

234 235 236 237
        QUI::getSession()->set(
            'CURRENT_LANG',
            QUI::getLocale()->getCurrent()
        );
238 239 240 241 242 243
    } catch (QUI\Exception $Exception) {
        if ($Exception->getCode() == 404) {
            $Response->setStatusCode(Response::HTTP_NOT_FOUND);
        } else {
            $Response->setStatusCode(Response::HTTP_SERVICE_UNAVAILABLE);
        }
244

Henning Leutz's avatar
Henning Leutz committed
245

246
        Log::writeException($Exception, Log::LEVEL_ERROR);
247

248
        $Template = new QUI\Template();
249

250
        try {
251
            $content = $Template->fetchSite($Rewrite->getErrorSite());
252
        } catch (QUI\Exception $Exception) {
253
            $content = $Template->fetchSite($Project->firstChild());
254 255 256 257 258 259
        }

        $content = $Rewrite->outputFilter($content);
        $content = QUI\Control\Manager::setCSSToHead($content);

        $Response->setContent($content);
260 261
    }

262 263
    $Response->prepare(QUI::getRequest());
    $Response->send();
264 265

    QUI::getEvents()->fireEvent('responseSent', [$Response]);
266 267
} catch (\Exception $Exception) {
    // error ??
268 269
    \header('HTTP/1.1 503 Service Temporarily Unavailable');
    \header('Status: 503 Service Temporarily Unavailable');
270

271 272
    \error_log($Exception->getTraceAsString());
    \error_log($Exception->getMessage());
273

274 275
    echo \file_get_contents(
        \dirname(__FILE__).'/lib/templates/error.html'
276 277
    );
}