Passed
Push — master ( d35171...b3de3c )
by Michael
02:47
created

include/onupdate.php (1 issue)

Labels
Severity
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
/**
13
 * Contact module
14
 *
15
 * @copyright   XOOPS Project (https://xoops.org)
16
 * @license     http://www.fsf.org/copyleft/gpl.html GNU public license
17
 * @author      Kazumi Ono (aka Onokazu)
18
 * @author      Trabis <[email protected]>
19
 * @author      Hossein Azizabadi (AKA Voltan)
20
 * @param \XoopsModule $module
21
 * @param              $version
22
 */
23
24
use XoopsModules\Contact;
25
use XoopsModules\Contact\Utility;
26
27
if ((!defined('XOOPS_ROOT_PATH')) || !($GLOBALS['xoopsUser'] instanceof \XoopsUser)
28
    || !$GLOBALS['xoopsUser']->isAdmin()) {
29
    exit('Restricted access' . PHP_EOL);
30
}
31
32
33
34
/**
35
 * Prepares system prior to attempting to install module
36
 * @param \XoopsModule $module {@link XoopsModule}
37
 *
38
 * @return bool true if ready to install, false if not
39
 */
40
function xoops_module_pre_update_contact(\XoopsModule $module)
41
{
42
    /** @var Contact\Helper $helper */
43
    /** @var Contact\Utility $utility */
44
    $moduleDirName = \basename(\dirname(__DIR__));
45
    $helper        = Contact\Helper::getInstance();
46
    $utility       = new Utility();
47
48
    $xoopsSuccess = $utility::checkVerXoops($module);
49
    $phpSuccess   = $utility::checkVerPhp($module);
50
51
    return $xoopsSuccess && $phpSuccess;
52
}
53
54
/**
55
 * Performs tasks required during update of the module
56
 * @param \XoopsModule $module {@link XoopsModule}
57
 * @param null         $previousVersion
58
 *
59
 * @return bool true if update successful, false if not
60
 */
61
function xoops_module_update_contact(\XoopsModule $module, $previousVersion = null)
62
{
63
    $moduleDirName      = \basename(\dirname(__DIR__));
64
    $moduleDirNameUpper = mb_strtoupper($moduleDirName);
65
66
    /** @var Contact\Helper $helper */ /** @var Contact\Utility $utility */
67
    /** @var Contact\Common\Configurator $configurator */
68
    $helper       = Contact\Helper::getInstance();
69
    $utility      = new Utility();
70
    $configurator = new Contact\Common\Configurator();
71
72
    $xoopsDB = \XoopsDatabaseFactory::getDatabaseConnection();
73
74
    if ($previousVersion < 180) {
75
        $sql = 'CREATE TABLE ' . $xoopsDB->prefix('contact') . ' (
76
        contact_id int(10) unsigned NOT NULL auto_increment,
77
        contact_uid int(10) NOT NULL,
78
        contact_cid int(10) NOT NULL,
79
        contact_create int(10) NOT NULL,
80
        contact_subject varchar(255) NOT NULL,
81
        contact_name varchar(255) NOT NULL,
82
        contact_mail varchar(255) NOT NULL,
83
        contact_url varchar(255) NOT NULL,
84
        contact_icq varchar(255) NOT NULL,
85
        contact_company varchar(255) NOT NULL,
86
        contact_location varchar(255) NOT NULL,
87
        contact_department varchar(60) NOT NULL,
88
        contact_ip varchar(20) NOT NULL,
89
        contact_phone varchar(20) NOT NULL,
90
        contact_message text NOT NULL,
91
        contact_address text NOT NULL,
92
        contact_reply tinyint(1) NOT NULL,
93
        PRIMARY KEY  (contact_id)
94
        ) ENGINE=MyISAM;';
95
        $xoopsDB->query($sql);
96
    }
97
98
    if ($previousVersion < 181) {
99
        // Add contact_platform
100
        $sql = 'ALTER TABLE `' . $xoopsDB->prefix('contact') . "` ADD `contact_platform` ENUM('Android','Ios','Web') NOT NULL DEFAULT 'Web'";
101
        $xoopsDB->query($sql);
102
        // Add contact_type
103
        $sql = 'ALTER TABLE `' . $xoopsDB->prefix('contact') . "` ADD `contact_type` ENUM('Contact','Phone','Mail') NOT NULL DEFAULT 'Contact'";
104
        $xoopsDB->query($sql);
105
        // Add index contact_uid
106
        $sql = 'ALTER TABLE `' . $xoopsDB->prefix('contact') . '` ADD INDEX `contact_uid` ( `contact_uid` )';
107
        $xoopsDB->query($sql);
108
        // Add index contact_cid
109
        $sql = 'ALTER TABLE `' . $xoopsDB->prefix('contact') . '` ADD INDEX `contact_cid` ( `contact_cid` )';
110
        $xoopsDB->query($sql);
111
        // Add index contact_create
112
        $sql = 'ALTER TABLE `' . $xoopsDB->prefix('contact') . '` ADD INDEX `contact_create` ( `contact_create` )';
113
        $xoopsDB->query($sql);
114
        // Add index contact_mail
115
        $sql = 'ALTER TABLE `' . $xoopsDB->prefix('contact') . '` ADD INDEX `contact_mail` ( `contact_mail` )';
116
        $xoopsDB->query($sql);
117
        // Add index contact_phone
118
        $sql = 'ALTER TABLE `' . $xoopsDB->prefix('contact') . '` ADD INDEX `contact_phone` ( `contact_phone` )';
119
        $xoopsDB->query($sql);
120
        // Add index contact_platform
121
        $sql = 'ALTER TABLE `' . $xoopsDB->prefix('contact') . '` ADD INDEX `contact_platform` ( `contact_platform` )';
122
        $xoopsDB->query($sql);
123
        // Add index contact_type
124
        $sql = 'ALTER TABLE `' . $xoopsDB->prefix('contact') . '` ADD INDEX `contact_type` ( `contact_type` )';
125
        $xoopsDB->query($sql);
126
    }
127
128
    if ($previousVersion < 227) {
129
        require_once __DIR__ . '/config.php';
130
        $configurator = new ContactConfigurator();
131
        $utility = new Utility();
132
133
        //delete old HTML templates
134
        if (count($configurator->templateFolders) > 0) {
135
            foreach ($configurator->templateFolders as $folder) {
136
                $templateFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $folder);
137
                if (is_dir($templateFolder)) {
138
                    $templateList = array_diff(scandir($templateFolder, SCANDIR_SORT_NONE), ['..', '.']);
139
                    foreach ($templateList as $k => $v) {
140
                        $fileInfo = new \SplFileInfo($templateFolder . $v);
141
                        if ('html' === $fileInfo->getExtension() && 'index.html' !== $fileInfo->getFilename()) {
142
                            if (is_file($templateFolder . $v)) {
143
                                unlink($templateFolder . $v);
144
                            }
145
                        }
146
                    }
147
                }
148
            }
149
        }
150
151
        //  ---  DELETE OLD FILES ---------------
152
        if (count($configurator->oldFiles) > 0) {
153
            //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
154
            foreach (array_keys($configurator->oldFiles) as $i) {
155
                $tempFile = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFiles[$i]);
156
                if (is_file($tempFile)) {
157
                    unlink($tempFile);
158
                }
159
            }
160
        }
161
162
        //  ---  DELETE OLD FOLDERS ---------------
163
        xoops_load('XoopsFile');
164
        if (count($configurator->oldFolders) > 0) {
165
            //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
166
            foreach (array_keys($configurator->oldFolders) as $i) {
167
                $tempFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFolders[$i]);
168
                /* @var XoopsObjectHandler $folderHandler */
169
                $folderHandler = XoopsFile::getHandler('folder', $tempFolder);
170
                $folderHandler->delete($tempFolder);
171
            }
172
        }
173
174
        //  ---  CREATE FOLDERS ---------------
175
        if (count($configurator->uploadFolders) > 0) {
176
            //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
177
            foreach (array_keys($configurator->uploadFolders) as $i) {
178
                $utility::createFolder($configurator->uploadFolders[$i]);
179
            }
180
        }
181
182
        //  ---  COPY blank.png FILES ---------------
183
        if (count($configurator->copyBlankFiles) > 0) {
0 ignored issues
show
The property copyBlankFiles does not seem to exist on ContactConfigurator.
Loading history...
184
            $file = dirname(__DIR__) . '/assets/images/blank.png';
185
            foreach (array_keys($configurator->copyBlankFiles) as $i) {
186
                $dest = $configurator->copyBlankFiles[$i] . '/blank.png';
187
                $utility::copyFile($file, $dest);
188
            }
189
        }
190
191
        //delete .html entries from the tpl table
192
        $sql = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('tplfile') . " WHERE `tpl_module` = '" . $module->getVar('dirname', 'n') . '\' AND `tpl_file` LIKE \'%.html%\'';
193
        $GLOBALS['xoopsDB']->queryF($sql);
194
195
        /** @var \XoopsGroupPermHandler $grouppermHandler */
196
        $grouppermHandler = xoops_getHandler('groupperm');
197
198
        return $grouppermHandler->deleteByModule($module->getVar('mid'), 'item_read');
199
    }
200
201
    if ($previousVersion < 227) {
202
        // Add contact_skype
203
        $sql = 'ALTER TABLE `' . $xoopsDB->prefix('contact') . "` ADD `contact_skype` VARCHAR(255) NULL AFTER `contact_icq`";
204
        $xoopsDB->query($sql);
205
    }
206
}
207