File size: 3,170 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
<?php

namespace ProcessMaker\Util;

use Propel;

/**
 * Class Cnn
 * @package ProcessMaker\Util
 */
class Cnn
{
    private $dbFile;
    private $workspace;

    /**
     * Establishes connection for the workspace
     * @param string $workspace Name workspace
     */
    public static function connect($workspace)
    {
        $cnn = new static();
        $cnn->workspace = $workspace;
        Propel::initConfiguration($cnn->buildParams());
    }

    /**
     * Loads the parameters required to connect to each workspace database
     * @return array
     */
    public function buildParams()
    {
        if ($this->readFileDBWorkspace()) {
            return $this->prepareDataSources();
        }
        return [];
    }

    /**
     * Reads the workspace db.php file
     * @return bool
     */
    private function readFileDBWorkspace()
    {
        if (file_exists(PATH_DB . $this->workspace . PATH_SEP . 'db.php')) {
            $this->dbFile = file_get_contents(PATH_DB . $this->workspace . PATH_SEP . 'db.php');
            return true;
        }
        return false;
    }

    /**
     * Prepares data resources
     * @return array
     */
    private function prepareDataSources()
    {
        $phpCode = preg_replace(
            '/define\s*\(\s*[\x22\x27](.*)[\x22\x27]\s*,\s*(\x22.*\x22|\x27.*\x27)\s*\)\s*;/i',
            '$$1 = $2;',
            $this->dbFile
        );
        $phpCode = str_replace(['<?php', '<?', '?>'], '', $phpCode);

        eval($phpCode);

        $dataSources = [];
        $dataSources['datasources'] = array(
            'workflow' => array(
                'connection' => $this->buildDsnString(
                    $DB_ADAPTER,
                    $DB_HOST,
                    $DB_NAME,
                    $DB_USER,
                    urlencode($DB_PASS)
                ),
                'adapter' => "mysql"
            ),
            'rbac' => array(
                'connection' => $this->buildDsnString(
                    $DB_ADAPTER,
                    $DB_RBAC_HOST,
                    $DB_RBAC_NAME,
                    $DB_RBAC_USER,
                    urlencode($DB_RBAC_PASS)
                ),
                'adapter' => "mysql"
            ),
            'report' => array(
                'connection' => $this->buildDsnString(
                    $DB_ADAPTER,
                    $DB_REPORT_HOST,
                    $DB_REPORT_NAME,
                    $DB_REPORT_USER,
                    urlencode($DB_REPORT_PASS)
                ),
                'adapter' => "mysql"
            )
        );
        return $dataSources;
    }

    /**
     * Builds the DSN string to be used by PROPEL
     * @param string $adapter
     * @param string $host
     * @param string $name
     * @param string $user
     * @param string $pass
     * @return string
     */
    private function buildDsnString($adapter, $host, $name, $user, $pass)
    {
        $dns = $adapter . "://" . $user . ":" . $pass . "@" . $host . "/" . $name;
        switch ($adapter) {
            case 'mysql':
                $dns .= '?encoding=utf8';
                break;
        }
        return $dns;
    }
}