File size: 25,418 Bytes
07c3cdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
<?php

namespace Maveriks;

use Bootstrap;
use Maveriks\Util;
use ProcessMaker\Core\System;
use ProcessMaker\Plugins\PluginRegistry;
use ProcessMaker\Services;
use ProcessMaker\Services\Api;
use Luracast\Restler\RestException;
use Illuminate\Foundation\Http\Kernel;
use G;

/**
 * Web application bootstrap
 */
class WebApplication
{
    const RUNNING_DEFAULT = "default.running";
    const RUNNING_INDEX = "index.running";
    const RUNNING_WORKFLOW = "workflow.running";
    const RUNNING_API = "api.running";
    const RUNNING_OAUTH2 = "api.oauth2";
    const SERVICE_API = "service.api";
    const SERVICE_OAUTH2 = "service.oauth2";
    const REDIRECT_DEFAULT = "redirect.default";

    /**
     * @var string application root directory
     */
    protected $rootDir = "";

    /**
     * @var string workflow directory
     */
    protected $workflowDir = "";

    /**
     * @var string workspace directory located into shared directory
     */
    protected $workspaceDir = "";

    /**
     * @var string workspace cache directory
     */
    protected $workspaceCacheDir = "";

    /**
     * @var string request location uri
     */
    protected $requestUri = "";

    /**
     * @var array holds multiple request response
     */
    protected $responseMultipart = array();

    /**
     * @var \Maveriks\Extension\Restler main REST dispatcher object
     */
    protected $rest;

    /**
     * class constructor
     */
    public function __construct()
    {
        defined("DS") || define("DS", DIRECTORY_SEPARATOR);
    }

    /**
     * @param string $rootDir
     */
    public function setRootDir($rootDir)
    {
        $this->rootDir = rtrim($rootDir, DS);
        $this->workflowDir = $rootDir . DS . "workflow" . DS;
    }

    /**
     * @return string
     */
    public function getRootDir()
    {
        return $this->rootDir;
    }

    /**
     * @param string $requestUri
     */
    public function setRequestUri($requestUri)
    {
        $this->requestUri = $requestUri;
    }

    /**
     * @return string
     */
    public function getRequestUri()
    {
        return $this->requestUri;
    }

    /**
     * Routes the request to dispatch
     * @return string
     */
    public function route()
    {
        if ($this->requestUri === "/") {
            if (file_exists("index.html")) {
                return self::RUNNING_INDEX;
            } else {
                return self::RUNNING_DEFAULT;
            }
        } elseif (substr($this->requestUri, 1, 3) === "api" && count(explode("/", $this->requestUri)) >= 4 // url api pattern: /api/1.0/<workspace>/<resource>
        ) {
            return self::RUNNING_API;
        } else {
            list($this->requestUri, ) = explode('?', $this->requestUri);
            $uriParts = explode('/', $this->requestUri);

            if (isset($uriParts[2]) && $uriParts[2] == "oauth2") {
                return self::RUNNING_OAUTH2;
            } else {
                return self::RUNNING_WORKFLOW;
            }
        }
    }

    /**
     * Run application
     * @param string $type the request type to run and dispatch, by now only self::SERVICE_API is accepted
     */
    public function run($type = "")
    {
        switch ($type) {
            case self::SERVICE_API:
                $request = $this->parseApiRequestUri();

                if ($request["version"] != $this->getApiVersion()) {
                    $rest = new \Maveriks\Extension\Restler();
                    $rest->setMessage(new RestException(Api::STAT_APP_EXCEPTION, "Invalid API version."));

                    exit(0);
                }

                $this->loadEnvironment($request["workspace"]);

                if (isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtoupper($_SERVER["HTTP_X_REQUESTED_WITH"]) == 'MULTIPART') {
                    $this->dispatchMultipleApiRequest($request["uri"], $request["version"]);
                } else {
                    $this->dispatchApiRequest($request["uri"], $request["version"]);
                }

                break;

            case self::SERVICE_OAUTH2:
                $uriTemp = explode('/', $_SERVER['REQUEST_URI']);
                array_shift($uriTemp);
                $workspace = array_shift($uriTemp);
                $uri = '/' . implode('/', $uriTemp);

                $this->loadEnvironment($workspace);
                $this->dispatchApiRequest($uri, $version = "1.0");
                break;
        }
    }

    /**
     * Dispatch multiple api request
     *
     * @param string $uri the request uri
     * @param string $version version of api
     * @author Brayan Pereyra (Cochalo) <brayan@colosa.com>
     */
    public function dispatchMultipleApiRequest($uri, $version = "1.0")
    {
        $stringInput = file_get_contents('php://input');

        if (empty($stringInput)) {
            $rest = new \Maveriks\Extension\Restler();
            $rest->setMessage(new RestException(Api::STAT_APP_EXCEPTION, "Invalid Request, multipart without body."));
            exit();
        } else {
            $input = json_decode($stringInput);
            if (empty($input->calls)) {
                $rest = new \Maveriks\Extension\Restler();
                $rest->setMessage(new RestException(Api::STAT_APP_EXCEPTION, "Invalid Request, multipart body without calls."));
                exit();
            }
        }

        $baseUrl = (empty($input->base_url)) ? $uri : $input->base_url;

        foreach ($input->calls as $key => $value) {
            $_SERVER["REQUEST_METHOD"] = empty($value->method) ? 'GET' : $value->method;
            $uriTemp = trim($baseUrl) . trim($value->url);

            if (strpos($uriTemp, '?') !== false) {
                $dataGet = explode('?', $uriTemp);
                parse_str($dataGet[1], $_GET);
            }

            $inputExecute = empty($value->data) ? '' : json_encode($value->data);
            $this->responseMultipart[$key] = $this->dispatchApiRequest($uriTemp, $version, true, $inputExecute);
        }

        echo json_encode($this->responseMultipart);
    }

    /**
     * This method dispatch rest/api service
     * @author Erik Amaru Ortiz <erik@colosa.com>
     */
    public function dispatchApiRequest($uri, $version = "1.0", $multipart = false, $inputExecute = '')
    {
        $uri = $this->initRest($uri, "1.0", $multipart);

        // to handle a request with "OPTIONS" method
        if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
            header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS, HEADERS');
            header('Access-Control-Allow-Headers: authorization, content-type');
            header("Access-Control-Allow-Credentials", "false");
            header('Access-Control-Allow-Origin: *');
            header('Access-Control-Max-Age: 60');
            die();
        }

        /*
         * Enable this header to allow "Cross Domain AJAX" requests;
         * This works because processmaker is handling correctly requests with method 'OPTIONS'
         * that automatically is sent by a client using XmlHttpRequest or similar.
         */
        header('Access-Control-Allow-Origin: *');

        $_SERVER['REQUEST_URI'] = $uri;

        $this->rest->inputExecute = $inputExecute;
        $this->rest->handle();

        if ($this->rest->flagMultipart === true) {
            return $this->rest->responseMultipart;
        }
    }

    /**
     * create a new instance of local $rest Restler object
     */
    protected function initRest($uri, $version, $multipart = false)
    {
        require_once $this->rootDir . "/framework/src/Maveriks/Extension/Restler/UploadFormat.php";

        // $servicesDir contains directory where Services Classes are allocated
        $servicesDir = $this->workflowDir . 'engine' . DS . 'src' . DS . 'ProcessMaker' . DS . 'Services' . DS;
        // $apiDir - contains directory to scan classes and add them to Restler
        $apiDir = $servicesDir . 'Api' . DS;
        // $apiIniFile - contains file name of api ini configuration
        $apiIniFile = $servicesDir . DS . 'api.ini';
        // $authenticationClass - contains the class name that validate the authentication for Restler
        $authenticationClass = 'ProcessMaker\\Services\\OAuth2\\Server';
        // $accessControlClass - contains the class name that validate the Access Control for Restler
        $accessControlClass = 'ProcessMaker\\Policies\\AccessControl';
        // $controlUnderUpdating - ControlUnderUpdating sends an error signal 503 to report that the application is in update
        $controlUnderUpdating = 'ProcessMaker\\Policies\\ControlUnderUpdating';
        // $pmOauthClientId - contains PM Local OAuth Id (Web Designer)
        $pmOauthClientId = 'x-pm-local-client';

        /*
         * Load Api ini file for Rest Service
         */
        $config = array();

        if (file_exists($apiIniFile)) {
            $cachedConfig = $this->workspaceCacheDir . "api-config.php";

            // verify if config cache file exists, is array and the last modification date is the same when cache was created.
            if (!file_exists($cachedConfig) || !is_array($config = include($cachedConfig)) || $config["_chk"] != filemtime($apiIniFile)) {
                $config = Util\Common::parseIniFile($apiIniFile);
                $config["_chk"] = filemtime($apiIniFile);
                if (!is_dir(dirname($cachedConfig))) {
                    Util\Common::mk_dir(dirname($cachedConfig));
                }
                file_put_contents($cachedConfig, "<?php return " . var_export($config, true) . ";");
                Util\Logger::log("Configuration cache was loaded and cached to: $cachedConfig");
            } else {
                Util\Logger::log("Loading Api Configuration from: $cachedConfig");
            }
        }

        // Setting current workspace to Api class
        Services\Api::setWorkspace(config("system.workspace"));
        $cacheDir = defined("PATH_WORKSPACE") ? PATH_WORKSPACE : (defined("PATH_C") ? PATH_C : sys_get_temp_dir());

        $sysConfig = System::getSystemConfiguration();

        \Luracast\Restler\Defaults::$cacheDirectory = $cacheDir;
        $productionMode = (bool) !(isset($sysConfig["service_api_debug"]) && $sysConfig["service_api_debug"]);

        Util\Logger::log("Serving API mode: " . ($productionMode ? "production" : "development"));

        // create a new Restler instance
        //$rest = new \Luracast\Restler\Restler();
        $this->rest = new \Maveriks\Extension\Restler($productionMode);
        // setting flag for multipart to Restler
        $this->rest->setFlagMultipart($multipart);
        // setting api version to Restler
        $this->rest->setAPIVersion($version);
        // adding $authenticationClass to Restler
        $this->rest->addAuthenticationClass($authenticationClass, '');
        // adding $accessControlClass to Restler
        $this->rest->addAuthenticationClass($accessControlClass);
        // adding $controlUnderUpdating to Restler
        $this->rest->addAuthenticationClass($controlUnderUpdating);

        // Setting database connection source
        list($host, $port) = strpos(DB_HOST, ':') !== false ? explode(':', DB_HOST) : array(DB_HOST, '');
        $port = empty($port) ? '' : ";port=$port";
        Services\OAuth2\Server::setDatabaseSource(DB_USER, DB_PASS, DB_ADAPTER . ":host=$host;dbname=" . DB_NAME . $port);
        if (DB_NAME != DB_RBAC_NAME) { //it's PM < 3
            list($host, $port) = strpos(DB_RBAC_HOST, ':') !== false ? explode(':', DB_RBAC_HOST) : array(DB_RBAC_HOST, '');
            $port = empty($port) ? '' : ";port=$port";
            Services\OAuth2\Server::setDatabaseSourceRBAC(DB_RBAC_USER, DB_RBAC_PASS, DB_ADAPTER . ":host=$host;dbname=" . DB_RBAC_NAME . $port);
        }

        // Setting default OAuth Client id, for local PM Web Designer
        Services\OAuth2\Server::setPmClientId($pmOauthClientId);

        $this->rest->setOverridingFormats('JsonFormat', 'UploadFormat');

        // scan all api directory to find api classes
        $classesList = Util\Common::rglob($apiDir . "/*");

        foreach ($classesList as $classFile) {
            if (pathinfo($classFile, PATHINFO_EXTENSION) === 'php') {
                $relClassPath = str_replace('.php', '', str_replace($servicesDir, '', $classFile));
                $namespace = '\\ProcessMaker\\Services\\' . str_replace(DS, '\\', $relClassPath);
                $namespace = strpos($namespace, "//") === false ? $namespace : str_replace("//", '', $namespace);
                $this->rest->addAPIClass($namespace);
            }
        }
        // adding aliases for Restler
        if (array_key_exists('alias', $config)) {
            foreach ($config['alias'] as $alias => $aliasData) {
                if (is_array($aliasData)) {
                    foreach ($aliasData as $label => $namespace) {
                        $namespace = '\\' . ltrim($namespace, '\\');
                        $this->rest->addAPIClass($namespace, $alias);
                    }
                }
            }
        }

        //
        // Register API Plugins classes
        $isPluginRequest = strpos($uri, '/plugin-') !== false ? true : false;

        if ($isPluginRequest) {
            $tmp = explode('/', $uri);
            array_shift($tmp);
            $tmp = array_shift($tmp);
            $tmp = explode('-', $tmp);
            $pluginName = $tmp[1];
            $uri = str_replace('plugin-' . $pluginName, strtolower($pluginName), $uri);
        }

        // hook to get rest api classes from plugins
        if (class_exists('ProcessMaker\Plugins\PluginRegistry')) {
            $pluginRegistry = PluginRegistry::loadSingleton();
            $plugins = $pluginRegistry->getRegisteredRestServices();

            if (!empty($plugins)) {
                foreach ($plugins as $pluginName => $plugin) {
                    $pluginSourceDir = PATH_PLUGINS . $pluginName . DIRECTORY_SEPARATOR . 'src';

                    $loader = \Maveriks\Util\ClassLoader::getInstance();
                    $loader->add($pluginSourceDir);

                    foreach ($plugin as $class) {
                        $className = is_object($class) ? $class->namespace : $class['namespace'];
                        if (class_exists($className)) {
                            $this->rest->addAPIClass($className, strtolower($pluginName));
                        }
                    }
                }
            }
        }

        Services\OAuth2\Server::setWorkspace(config("system.workspace"));
        $this->rest->addAPIClass('\ProcessMaker\\Services\\OAuth2\\Server', 'oauth2');

        return $uri;
    }

    public function parseApiRequestUri()
    {
        $url = explode("/", $this->requestUri);
        array_shift($url);
        array_shift($url);
        $version = array_shift($url);
        $workspace = array_shift($url);
        $restUri = "";

        foreach ($url as $urlPart) {
            $restUri .= "/" . $urlPart;
        }

        return array(
            "uri" => $restUri,
            "version" => $version,
            "workspace" => $workspace
        );
    }

    public function loadEnvironment($workspace = "")
    {
        define("PATH_SEP", DIRECTORY_SEPARATOR);

        define("PATH_TRUNK", $this->rootDir . PATH_SEP);
        define("PATH_OUTTRUNK", realpath($this->rootDir . "/../") . PATH_SEP);
        define("PATH_HOME", $this->rootDir . PATH_SEP . "workflow" . PATH_SEP);

        define("PATH_HTML", PATH_HOME . "public_html" . PATH_SEP);
        define("PATH_RBAC_HOME", PATH_TRUNK . "rbac" . PATH_SEP);
        define("PATH_GULLIVER_HOME", PATH_TRUNK . "gulliver" . PATH_SEP);
        define("PATH_GULLIVER", PATH_GULLIVER_HOME . "system" . PATH_SEP); //gulliver system classes
        define("PATH_GULLIVER_BIN", PATH_GULLIVER_HOME . "bin" . PATH_SEP); //gulliver bin classes
        define("PATH_TEMPLATE", PATH_GULLIVER_HOME . "templates" . PATH_SEP);
        define("PATH_THIRDPARTY", PATH_TRUNK . "thirdparty" . PATH_SEP);
        define("PATH_RBAC", PATH_RBAC_HOME . "engine" . PATH_SEP . "classes" . PATH_SEP); //to enable rbac version 2
        define("PATH_RBAC_CORE", PATH_RBAC_HOME . "engine" . PATH_SEP);
        define("PATH_CORE", PATH_HOME . "engine" . PATH_SEP);
        define("PATH_CLASSES", PATH_HOME . "engine" . PATH_SEP . "classes" . PATH_SEP);
        define("PATH_SKINS", PATH_CORE . "skins" . PATH_SEP);
        define("PATH_SKIN_ENGINE", PATH_CORE . "skinEngine" . PATH_SEP);
        define("PATH_METHODS", PATH_CORE . "methods" . PATH_SEP);
        define("PATH_XMLFORM", PATH_CORE . "xmlform" . PATH_SEP);
        define("PATH_CONFIG", PATH_CORE . "config" . PATH_SEP);
        define("PATH_PLUGINS", PATH_CORE . "plugins" . PATH_SEP);
        define("PATH_HTMLMAIL", PATH_CORE . "html_templates" . PATH_SEP);
        define("PATH_TPL", PATH_CORE . "templates" . PATH_SEP);
        define("PATH_TEST", PATH_CORE . "test" . PATH_SEP);
        define("PATH_FIXTURES", PATH_TEST . "fixtures" . PATH_SEP);
        define("PATH_RTFDOCS", PATH_CORE . "rtf_templates" . PATH_SEP);
        define("PATH_DYNACONT", PATH_CORE . "content" . PATH_SEP . "dynaform" . PATH_SEP);
        define("SYS_UPLOAD_PATH", PATH_HOME . "public_html/files/");
        define("PATH_UPLOAD", PATH_HTML . "files" . PATH_SEP);
        define("PATH_WORKFLOW_MYSQL_DATA", PATH_CORE . "data" . PATH_SEP . "mysql" . PATH_SEP);
        define("PATH_RBAC_MYSQL_DATA", PATH_RBAC_CORE . "data" . PATH_SEP . "mysql" . PATH_SEP);
        define("FILE_PATHS_INSTALLED", PATH_CORE . "config" . PATH_SEP . "paths_installed.php");
        define("PATH_WORKFLOW_MSSQL_DATA", PATH_CORE . "data" . PATH_SEP . "mssql" . PATH_SEP);
        define("PATH_RBAC_MSSQL_DATA", PATH_RBAC_CORE . "data" . PATH_SEP . "mssql" . PATH_SEP);
        define("PATH_CONTROLLERS", PATH_CORE . "controllers" . PATH_SEP);
        define("PATH_SERVICES_REST", PATH_CORE . "services" . PATH_SEP . "rest" . PATH_SEP);

        G::defineConstants();
        $arraySystemConfiguration = System::getSystemConfiguration();

        ini_set('date.timezone', $arraySystemConfiguration['time_zone']); //Set Time Zone
        // set include path
        set_include_path(
                PATH_CORE . PATH_SEPARATOR .
                PATH_THIRDPARTY . PATH_SEPARATOR .
                PATH_THIRDPARTY . "pear" . PATH_SEPARATOR .
                PATH_RBAC_CORE . PATH_SEPARATOR .
                get_include_path()
        );

        /*
         * Setting Up Workspace
         */
        if (!file_exists(FILE_PATHS_INSTALLED)) {
            throw new \Exception("Can't locate system file: " . FILE_PATHS_INSTALLED);
        }

        // include the server installed configuration
        require_once PATH_CORE . "config" . PATH_SEP . "paths_installed.php";

        // defining system constant when a valid server environment exists
        define("PATH_LANGUAGECONT", PATH_DATA . "META-INF" . PATH_SEP);
        define("PATH_CUSTOM_SKINS", PATH_DATA . "skins" . PATH_SEP);
        define("PATH_TEMPORAL", PATH_C . "dynEditor/");
        define("PATH_DB", PATH_DATA . "sites" . PATH_SEP);

        // Change storage path
        app()->useStoragePath(realpath(PATH_DATA));
        app()->make(Kernel::class)->bootstrap();
        restore_error_handler();
        error_reporting(error_reporting() & ~E_STRICT & ~E_DEPRECATED);

        Bootstrap::setLanguage();

        Bootstrap::LoadTranslationObject((defined("SYS_LANG")) ? SYS_LANG : "en");

        if (empty($workspace)) {
            return true;
        }

        define("SYS_SYS", $workspace);
        config(["system.workspace" => $workspace]);

        if (!file_exists(PATH_DB . config("system.workspace") . PATH_SEP . "db.php")) {
            $rest = new \Maveriks\Extension\Restler();
            $rest->setMessage(new RestException(Api::STAT_APP_EXCEPTION, \G::LoadTranslation("ID_NOT_WORKSPACE")));

            exit(0);
        }

        $arraySystemConfiguration = System::getSystemConfiguration('', '', config("system.workspace"));

        //Do not change any of these settings directly, use env.ini instead
        ini_set('display_errors', $arraySystemConfiguration['display_errors']);
        ini_set('error_reporting', $arraySystemConfiguration['error_reporting']);
        ini_set('short_open_tag', 'On'); //??
        ini_set('default_charset', 'UTF-8'); //??
        ini_set('memory_limit', $arraySystemConfiguration['memory_limit']);
        ini_set('soap.wsdl_cache_enabled', $arraySystemConfiguration['wsdl_cache']);

        define('DEBUG_SQL_LOG', $arraySystemConfiguration['debug_sql']);
        define('DEBUG_TIME_LOG', $arraySystemConfiguration['debug_time']);
        define('DEBUG_CALENDAR_LOG', $arraySystemConfiguration['debug_calendar']);
        define('MEMCACHED_ENABLED', $arraySystemConfiguration['memcached']);
        define('MEMCACHED_SERVER', $arraySystemConfiguration['memcached_server']);
        define('SYS_SKIN', $arraySystemConfiguration['default_skin']);
        define('DISABLE_DOWNLOAD_DOCUMENTS_SESSION_VALIDATION', $arraySystemConfiguration['disable_download_documents_session_validation']);

        require_once(PATH_DB . config("system.workspace") . "/db.php");

        // defining constant for workspace shared directory
        $this->workspaceDir = PATH_DB . config("system.workspace") . PATH_SEP;
        $this->workspaceCacheDir = PATH_DB . config("system.workspace") . PATH_SEP . "cache" . PATH_SEP;

        define("PATH_WORKSPACE", $this->workspaceDir);
        // including workspace shared classes -> particularlly for pmTables

        set_include_path(get_include_path() . PATH_SEPARATOR . PATH_WORKSPACE);

        // smarty constants
        define("PATH_SMARTY_C", PATH_C . "smarty" . PATH_SEP . "c");
        define("PATH_SMARTY_CACHE", PATH_C . "smarty" . PATH_SEP . "cache");

        define("PATH_DATA_SITE", PATH_DATA . "sites/" . config("system.workspace") . "/");
        define("PATH_DOCUMENT", PATH_DATA_SITE . "files/");
        define("PATH_DATA_MAILTEMPLATES", PATH_DATA_SITE . "mailTemplates/");
        define("PATH_DATA_PUBLIC", PATH_DATA_SITE . "public/");
        define("PATH_DATA_REPORTS", PATH_DATA_SITE . "reports/");
        define("PATH_DYNAFORM", PATH_DATA_SITE . "xmlForms/");
        define("PATH_IMAGES_ENVIRONMENT_FILES", PATH_DATA_SITE . "usersFiles" . PATH_SEP);
        define("PATH_IMAGES_ENVIRONMENT_USERS", PATH_DATA_SITE . "usersPhotographies" . PATH_SEP);
        define('DISABLE_PHP_UPLOAD_EXECUTION', $arraySystemConfiguration['disable_php_upload_execution']);

        /**
         * Global definitions, before it was the defines.php file
         */
        // URL Key
        define("URL_KEY", 'c0l0s40pt1mu59r1m3');

        // Other definitions
        define('TIMEOUT_RESPONSE', 100); //web service timeout
        define('APPLICATION_CODE', 'ProcessMaker'); //to login like workflow system
        define('MAIN_POFILE', 'processmaker');
        define('PO_SYSTEM_VERSION', 'PM 4.0.1');

        // Environment definitions
        define('G_PRO_ENV', 'PRODUCTION');
        define('G_DEV_ENV', 'DEVELOPMENT');
        define('G_TEST_ENV', 'TEST');

        // Number of files per folder at PATH_UPLOAD (cases documents)
        define('APPLICATION_DOCUMENTS_PER_FOLDER', 1000);

        // Server of ProcessMaker Library
        define('PML_SERVER', 'http://library.processmaker.com');
        define('PML_WSDL_URL', PML_SERVER . '/syspmLibrary/en/green/services/wsdl');
        define('PML_UPLOAD_URL', PML_SERVER . '/syspmLibrary/en/green/services/uploadProcess');
        define('PML_DOWNLOAD_URL', PML_SERVER . '/syspmLibrary/en/green/services/download');

        \Propel::init(PATH_CONFIG . "databases.php");

        //Set Time Zone
        /*----------------------------------********---------------------------------*/

        ini_set('date.timezone', (isset($_SESSION['__SYSTEM_UTC_TIME_ZONE__']) && $_SESSION['__SYSTEM_UTC_TIME_ZONE__']) ? 'UTC' : $arraySystemConfiguration['time_zone']); //Set Time Zone

        define('TIME_ZONE', ini_get('date.timezone'));

        $oPluginRegistry = PluginRegistry::loadSingleton();
        $attributes = $oPluginRegistry->getAttributes();
        Bootstrap::LoadTranslationPlugins(defined('SYS_LANG') ? SYS_LANG : "en", $attributes);
        // Initialization functions plugins
        $oPluginRegistry->init();
        //get and setup enabled plugins
        $oPluginRegistry->setupPlugins();

        return true;
    }

    public function getApiVersion()
    {
        try {
            $arrayConfig = array();

            //$apiIniFile - Contains file name of api ini configuration
            $apiIniFile = $this->workflowDir . "engine" . DS . "src" . DS . "ProcessMaker" . DS . "Services" . DS . "api.ini";

            if (file_exists($apiIniFile)) {
                $arrayConfig = Util\Common::parseIniFile($apiIniFile);
            }

            return (isset($arrayConfig["api"]["version"]))? $arrayConfig["api"]["version"] : "1.0";
        } catch (\Exception $e) {
            throw $e;
        }
    }

    public static function purgeRestApiCache($workspace)
    {
        @unlink(PATH_DATA . 'compiled' . DS . 'routes.php');
        @unlink(PATH_DATA_SITE . 'routes.php');
        @unlink(PATH_DATA . 'sites' . DS . $workspace . DS . 'api-config.php');
    }
}