File size: 12,647 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
<?php

use ProcessMaker\Core\System;
use ProcessMaker\Plugins\PluginRegistry;

if (!defined("PATH_PM_ENTERPRISE")) {
    define("PATH_PM_ENTERPRISE", PATH_CORE . "enterprise/");
}

if (!defined("PATH_DATA_SITE")) {
    define("PATH_DATA_SITE", PATH_DATA . "sites/" . config("system.workspace") . "/");
}

set_include_path(PATH_PM_ENTERPRISE . PATH_SEPARATOR . get_include_path());

class enterprisePlugin extends PMPlugin
{
    public function enterprisePlugin($sNamespace, $sFilename = null)
    {
        $pathPluginTrunk = PATH_CORE . "enterprise";

        $VERSION = System::getVersion();

        $res = parent::PMPlugin($sNamespace, $sFilename);
        $this->sFriendlyName = "ProcessMaker Enterprise Core Edition";
        $this->sDescription  = "ProcessMaker Enterprise Core Edition $VERSION";
        $this->sPluginFolder = "enterprise";
        $this->sSetupPage    = "../enterprise/addonsStore.php";
        $this->iVersion      = $VERSION;
        $this->iPMVersion    = "2.0.31";
        $this->aDependences  = null;
        $this->aWorkspaces   = null;

        $this->database = "workflow";
        $this->table    = array("ADDONS_STORE", "ADDONS_MANAGER", "LICENSE_MANAGER");

        if (!isset($_SESSION["__EE_INSTALLATION__"])) {
            $_SESSION["__EE_INSTALLATION__"] = 0;
        }

        if (!isset($_SESSION["__EE_SW_PMLICENSEMANAGER__"])) {
            $_SESSION["__EE_SW_PMLICENSEMANAGER__"] = 1;
        }

        $sw = 1;
        $msgf = null;
        $msgd = null;

        if (file_exists(PATH_CORE . "plugins" . PATH_SEP . "pmLicenseManager.php")) {
            $_SESSION["__EE_INSTALLATION__"] = 1;
            $_SESSION["__EE_SW_PMLICENSEMANAGER__"] = 0;

            $plugin = "pmLicenseManager";
            $this->pluginUninstall($plugin);

            if (file_exists(PATH_CORE . "plugins" . PATH_SEP . $plugin . ".php") || file_exists(PATH_CORE . "plugins" . PATH_SEP . $plugin)) {
                $msgf = $msgf . (($msgf != null)? ", " : null) . $plugin . ".php";
                $msgd = $msgd . (($msgd != null)? ", " : null) . $plugin;
                $sw = 0;
            }

            $plugin = "enterprise";
            $this->pluginUninstall($plugin);

            if (file_exists(PATH_CORE . "plugins" . PATH_SEP . $plugin . ".php") || file_exists(PATH_CORE . "plugins" . PATH_SEP . $plugin)) {
                $msgf = $msgf . (($msgf != null)? ", " : null) . $plugin . ".php";
                $msgd = $msgd . (($msgd != null)? ", " : null) . $plugin;
                $sw = 0;
            }

            $this->uninstall();
        } else {
            $_SESSION["__EE_INSTALLATION__"] = $_SESSION["__EE_INSTALLATION__"] + 1;
        }

        if ($sw == 0) {
            unset($_SESSION["__EE_INSTALLATION__"]);
            unset($_SESSION["__EE_SW_PMLICENSEMANAGER__"]);

            ///////
            $js = "window.open(\"/sys" . config("system.workspace") . "/" . SYS_LANG . "/" . SYS_SKIN . "/setup/main?s=PLUGINS\", \"_top\", \"\");";

            if (substr(SYS_SKIN, 0, 2) == "ux" && SYS_SKIN != "uxs") {
                $js = "window.open(\"/sys" . config("system.workspace") . "/" . SYS_LANG . "/" . SYS_SKIN . "/main\", \"_top\", \"\");";
            }

            ///////
            G::SendMessageText("ProcessMaker Enterprise plug-in can't delete the files \"$msgf\" and directories \"$msgd\" of \"" . (PATH_CORE . "plugins") . "\". Before proceeding with the installation of the plug-in must remove them.", "INFO");

            echo "<script type=\"text/javascript\">" . $js . "</script>";
            exit(0);
        }

        if ($_SESSION["__EE_SW_PMLICENSEMANAGER__"] == 0 && $_SESSION["__EE_INSTALLATION__"] == 2) {
            unset($_SESSION["__EE_INSTALLATION__"]);
            unset($_SESSION["__EE_SW_PMLICENSEMANAGER__"]);

            $this->install();
        }

        ///////
        return $res;
    }

    public function install()
    {
        $pluginRegistry = PluginRegistry::loadSingleton();

        $pluginDetail = $pluginRegistry->getPluginDetails("enterprise.php");
        $pluginRegistry->enablePlugin($pluginDetail->getNamespace());

        file_put_contents(PATH_DATA_SITE . "plugin.singleton", $pluginRegistry->serializeInstance());
    }

    public function uninstall()
    {
    }

    public function setup()
    {
        if (!PluginsRegistryPeer::retrieveByPK(md5('enterprise'))) {
            $pluginRegistry = PluginRegistry::loadSingleton();
            $pluginDetail = $pluginRegistry->getPluginDetails("enterprise.php");
            $pluginRegistry->enablePlugin($pluginDetail->getNamespace());
            $pluginRegistry->savePlugin($pluginDetail->getNamespace());
        }
    }

    public function enable()
    {
        $this->setConfiguration();

        require_once (PATH_CORE . 'classes/model/AddonsStore.php');
        AddonsStore::checkLicenseStore();
        $licenseManager = &PmLicenseManager::getSingleton();
        AddonsStore::updateAll(false);
    }

    public function disable()
    {
    }

    public function setConfiguration()
    {
        $confEeUid = "enterpriseConfiguration";

        $criteria = new Criteria("workflow");

        $criteria->addSelectColumn(ConfigurationPeer::CFG_VALUE);
        $criteria->add(ConfigurationPeer::CFG_UID, "EE");
        $criteria->add(ConfigurationPeer::OBJ_UID, $confEeUid);

        $rsCriteria = ConfigurationPeer::doSelectRS($criteria);

        if (!$rsCriteria->next()) {
            $conf = new Configuration();

            $data = array("internetConnection" => 1);

            $conf->create(
                array(
                    "CFG_UID"   => "EE",
                    "OBJ_UID"   => $confEeUid,
                    "CFG_VALUE" => serialize($data),
                    "PRO_UID"   => "",
                    "USR_UID"   => "",
                    "APP_UID"   => ""
                )
            );
        }
    }

    public function pluginUninstall($pluginName)
    {
        //define("PATH_PLUGINS", PATH_CORE . "plugins" . PATH_SEP);

        if (file_exists(PATH_CORE . "plugins" . PATH_SEP . $pluginName . ".php")) {
            require_once (PATH_CORE . "plugins" . PATH_SEP . $pluginName . ".php");

            $pluginRegistry = PluginRegistry::loadSingleton();

            $pluginDetail = $pluginRegistry->getPluginDetails($pluginName . ".php");

            if ($pluginDetail) {
                $pluginRegistry->enablePlugin($pluginDetail->getNamespace());
                $pluginRegistry->disablePlugin($pluginDetail->getNamespace());

                ///////
                $className = $pluginDetail->getClassName();
                $plugin = new $className($pluginDetail->getNamespace(), $pluginDetail->getFile());
                //$this->_aPlugins[$pluginDetail->sNamespace] = $plugin;

                if (method_exists($plugin, "uninstall")) {
                    $plugin->uninstall();
                }

                ///////
                $pluginRegistry->savePlugin($pluginDetail->getNamespace());
            }

            ///////
            unlink(PATH_CORE . "plugins" . PATH_SEP . $pluginName . ".php");

            if (file_exists(PATH_CORE . "plugins" . PATH_SEP . $pluginName)) {
                G::rm_dir(PATH_CORE . "plugins" . PATH_SEP . $pluginName);
            }
        }
    }

    public function registerEE($pluginFile, $pluginVersion)
    {
        if (file_exists(PATH_DATA_SITE . "ee")) {
            $this->systemAvailable = unserialize(trim(file_get_contents(PATH_DATA_SITE . "ee")));
        }

        $this->systemAvailable[$pluginFile]["sFilename"] = $pluginFile . "-" . $pluginVersion . ".tar";
        file_put_contents(PATH_DATA_SITE . "ee", serialize($this->systemAvailable));

        return true;
    }

    public function checkDependencies()
    {
    }

    public function tableBackup($tableBackup, $backupPrefix = "_", $backupSuffix = "_TEMP")
    {
        //Database Connections
        $cnn = Propel::getConnection($this->database);
        $stmt = $cnn->createStatement();

        foreach ($tableBackup as $key => $table) {
            $tablebak = $backupPrefix . $table . $backupSuffix;

            //First Search if the Table exists
            $sqlTable = "SHOW TABLES LIKE '$table'";
            $rsTable = $stmt->executeQuery($sqlTable, ResultSet::FETCHMODE_ASSOC);
            if ($rsTable->getRecordCount() > 0) {
                //Table $table exists, so we can Backup
                //If there are records in $table Backup
                $sqlSelectTable = "SELECT * FROM $table";
                $rsSelectTable = $stmt->executeQuery($sqlSelectTable, ResultSet::FETCHMODE_ASSOC);
                if ($rsSelectTable->getRecordCount() > 0) {
                    //There are records in $table!! Backup!
                    //Delete a previous Backup if exists
                    $sql = "DROP TABLE IF EXISTS $tablebak;";
                    $rs = $stmt->executeQuery($sql, ResultSet::FETCHMODE_ASSOC);

                    //Create a COPY of $table in $tablebak :: Backup
                    $sql = "CREATE TABLE $tablebak SELECT * FROM $table";
                    $rs = $stmt->executeQuery($sql, ResultSet::FETCHMODE_ASSOC);

                    //Delete a previous $table if exists
                    $sql = "DROP TABLE IF EXISTS $table;";
                    $rs = $stmt->executeQuery($sql, ResultSet::FETCHMODE_ASSOC);
                }
            }
        }
    }

    public function tableBackupRestore($tableBackup, $backupPrefix = "_", $backupSuffix = "_TEMP")
    {
        //Database Connections
        $cnn = Propel::getConnection($this->database);
        $stmt = $cnn->createStatement();

        foreach ($tableBackup as $key => $table) {
            $tablebak = $backupPrefix . $table . $backupSuffix;

            //First Search if the $tablebak exists
            $sqlTablebak = "SHOW TABLES LIKE '$tablebak'";
            $rsTablebak = $stmt->executeQuery($sqlTablebak, ResultSet::FETCHMODE_ASSOC);
            if ($rsTablebak->getRecordCount() > 0) {
                //Table $tablebak exists, so we can Restore
                $sqlSelectTablebak = "SELECT * FROM $tablebak";
                $rsSelectTablebak = $stmt->executeQuery($sqlSelectTablebak, ResultSet::FETCHMODE_ASSOC);
                if ($rsSelectTablebak->getRecordCount() > 0) {
                    $strTable = str_replace("_", " ", strtolower($table));
                    $strTable = str_replace(" ", null, ucwords($strTable));

                    require_once (PATH_PLUGINS . "enterprise" . PATH_SEP . "classes" . PATH_SEP . "model" . PATH_SEP . "$strTable.php");

                    while ($rsSelectTablebak->next()) {
                        $row = $rsSelectTablebak->getRow();

                        //INSERT INTO TABLEN(FIELD1, FIELD2) VALUES('VALUE1', 'VALUE2')
                        $oTable = new $strTable();
                        $oTable->fromArray($row, BasePeer::TYPE_FIELDNAME); //Fill an object from of the array //Fill attributes
                        $oTable->save();
                    }
                }

                //Delete Backup
                $sql = "DROP TABLE IF EXISTS $tablebak;";
                $rs = $stmt->executeQuery($sql, ResultSet::FETCHMODE_ASSOC);
            }
        }
    }

    public function sqlExecute($sqlFile)
    {
        $file = fopen($sqlFile, "r");

        if ($file) {
            $line = null;

            while (!feof($file)) {
                $buffer = trim(fgets($file, 4096)); //Read a line.

                if (strlen($buffer) > 0 && $buffer[0] != "#") {
                    //Check for valid lines
                    $line = $line . $buffer;

                    if ($buffer[strlen($buffer) - 1] == ";") {
                        $cnn = Propel::getConnection($this->database);
                        $stmt = $cnn->createStatement();
                        $rs = $stmt->executeQuery($line, ResultSet::FETCHMODE_NUM);
                        $line = null;
                    }
                }
            }

            fclose($file);
        }
    }
}

$oPluginRegistry = PluginRegistry::loadSingleton();
$oPluginRegistry->registerPlugin('enterprise', __FILE__); //<- enterprise string must be in single quote, otherwise generate error

//since we are placing pmLicenseManager and EE together.. after register EE, we need to require_once the pmLicenseManager
//if( !defined("PATH_PM_LICENSE_MANAGER") ) {
//  define("PATH_PM_LICENSE_MANAGER", PATH_CORE . "/plugins/pmLicenseManager/");
//}
//set_include_path(
//  PATH_PM_LICENSE_MANAGER.PATH_SEPARATOR.
//  get_include_path()
//);