Passed
Push — master ( c3fe63...2ac8fd )
by Michael
44s queued 11s
created

include/onupdate.php (1 issue)

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
 * @copyright    XOOPS Project https://xoops.org/
14
 * @license      GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
15
 * @package
16
 * @since
17
 * @author       XOOPS Development Team
18
 */
19
20
use XoopsModules\Xsitemap\{
21
    Common\Configurator,
22
    Helper,
23
    Utility
24
};
25
/** @var Utility $utility */
26
/** @var Helper $helper */
27
/** @var Configurator $configurator */
28
29
// require_once  dirname(__DIR__) . '/class/Utility.php';
30
if ((!defined('XOOPS_ROOT_PATH')) || !($GLOBALS['xoopsUser'] instanceof \XoopsUser)
31
    || !$GLOBALS['xoopsUser']->isAdmin()) {
32
    exit('Restricted access' . PHP_EOL);
33
}
34
/**
35
 * Prepares system prior to attempting to update module
36
 *
37
 * @param \XoopsModule $module
38
 *
39
 * @return bool true if successfully ready to update module, false if not
40
 */
41
function xoops_module_pre_update_xsitemap(\XoopsModule $module)
42
{
43
    $moduleDirName = basename(dirname(__DIR__));
44
    $utility       = new Utility();
45
    $xoopsSuccess  = $utility::checkVerXoops($module);
46
    $phpSuccess    = $utility::checkVerPhp($module);
47
    return $xoopsSuccess && $phpSuccess;
48
}
49
50
/**
51
 * Functions to upgrade from previous version of the module
52
 *
53
 * @param \XoopsModule $module
54
 * @param int|null     $previousVersion
55
 * @return bool true if successfully updated module, false if not
56
 * @internal param int $curr_version version number of module currently installed
57
 */
58
function xoops_module_update_xsitemap(\XoopsModule $module, $previousVersion = null)
59
{
60
    /*======================================================================
61
        //----------------------------------------------------------------
62
        // Remove xSitemap uploads folder (and all subfolders) if they exist
63
        //----------------------------------------------------------------*
64
        $utility = new Utility();
65
        if (!class_exists($utility)) {
66
            xoops_load('utility', $moduleDirName);
67
        }
68
69
        // Recursively delete directories
70
        $xsUploadDir = realpath(XOOPS_UPLOAD_PATH . "/" . $module->dirname());
71
        $success = $utility::rrmdir($xsUploadDir);
72
        if (true !== $success) {
73
            \Xmf\Language::load('admin', $module->dirname());
74
            $module->setErrors(sprintf(_AM_XSITEMAP_ERROR_BAD_DEL_PATH, $xsUploadDir));
75
        }
76
        return $success;
77
    ======================================================================*/
78
    $moduleDirName      = basename(dirname(__DIR__));
79
    $moduleDirNameUpper = mb_strtoupper($moduleDirName);
80
    $helper       = Helper::getInstance();
81
    $utility      = new Utility();
82
    //-----------------------------------------------------------------------
83
    // Upgrade for Xsitemap < 1.54
84
    //-----------------------------------------------------------------------
85
    $success = true;
86
    $helper->loadLanguage('modinfo');
87
    $helper->loadLanguage('admin');
88
    if ($previousVersion < 154) {
89
        //----------------------------------------------------------------
90
        // Remove previous css & images directories since they've been relocated to ./assets
91
        // Also remove uploads directories since they're no longer used
92
        //----------------------------------------------------------------
93
        $old_directories = [
94
            $helper->path('css/'),
95
            $helper->path('js/'),
96
            $helper->path('images/'),
97
            XOOPS_UPLOAD_PATH . '/' . $module->dirname(),
98
        ];
99
        foreach ($old_directories as $old_dir) {
100
            $dirInfo = new \SplFileInfo($old_dir);
101
            if ($dirInfo->isDir()) {
102
                // The directory exists so delete it
103
                if (false === $utility::rrmdir($old_dir)) {
104
                    $module->setErrors(sprintf(_AM_XSITEMAP_ERROR_BAD_DEL_PATH, $old_dir));
105
                    return false;
106
                }
107
            }
108
            unset($dirInfo);
109
        }
110
        //-----------------------------------------------------------------------
111
        // Remove ./template/*.html (except index.html) files since they've
112
        // been replaced by *.tpl files
113
        // Note: this will also remove /template/xsitemap_style.html since it's no longer used
114
        //-----------------------------------------------------------------------
115
        $path       = $helper->path('templates/');
116
        $unfiltered = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
117
        $iterator   = new RegexIterator($unfiltered, '/.*\.html/');
118
        foreach ($iterator as $name => $fObj) {
119
            if ($fObj->isFile() && ('index.html' !== $fObj->getFilename())) {
120
                if (false === ($success = unlink($fObj->getPathname()))) {
121
                    $module->setErrors(sprintf(_AM_XSITEMAP_ERROR_BAD_REMOVE, $fObj->getPathname()));
122
                    return false;
123
                }
124
            }
125
        }
126
        //-----------------------------------------------------------------------
127
        // Now remove a some misc files that were renamed or deprecated
128
        //-----------------------------------------------------------------------
129
        $oldFiles = [
130
            $helper->path('include/install.php'),
131
            $helper->path('class/module.php'),
132
            $helper->path('class/menu.php'),
133
        ];
134
        foreach ($oldFiles as $file) {
135
            if (is_file($file)) {
136
                if (false === ($delOk = unlink($file))) {
137
                    $module->setErrors(sprintf(_AM_XSITEMAP_ERROR_BAD_REMOVE, $file));
138
                }
139
                $success = $success && $delOk;
140
            }
141
        }
142
    }
143
    if ($previousVersion < 156) {
144
        // update table (add new field)
145
        $table = $GLOBALS['xoopsDB']->prefix('xsitemap_plugin');
146
        $field = 'plugin_where';
147
        $check = $GLOBALS['xoopsDB']->queryF('SHOW COLUMNS FROM `' . $table . "` LIKE '" . $field . "'");
148
        $numRows = $GLOBALS['xoopsDB']->getRowsNum($check);
149
        if (!$numRows) {
150
            $sql = "ALTER TABLE `$table` ADD `$field` VARCHAR(255) NOT NULL AFTER `plugin_weight`;";
151
            if (!$result = $GLOBALS['xoopsDB']->queryF($sql)) {
152
                xoops_error($GLOBALS['xoopsDB']->error() . '<br>' . $sql);
153
                $module->setErrors("Error when adding '$field' to table '$table'.");
154
                $ret = false;
0 ignored issues
show
The assignment to $ret is dead and can be removed.
Loading history...
155
            }
156
            $success = $success && $result;
157
        }
158
    }
159
    
160
    return $success;
161
}
162