Passed
Push — master ( d3e687...4990f6 )
by Michael
02:43
created

xoops_module_update_newbb()   C

Complexity

Conditions 8
Paths 18

Size

Total Lines 42
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 8
eloc 24
nc 18
nop 2
dl 0
loc 42
rs 5.3846
c 3
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 38 and the first side effect is on line 36.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
//
3
//  ------------------------------------------------------------------------ //
4
//                XOOPS - PHP Content Management System                      //
5
//                  Copyright (c) 2000-2016 XOOPS.org                        //
6
//                       <https://xoops.org/>                             //
7
//  ------------------------------------------------------------------------ //
8
//  This program is free software; you can redistribute it and/or modify     //
9
//  it under the terms of the GNU General Public License as published by     //
10
//  the Free Software Foundation; either version 2 of the License, or        //
11
//  (at your option) any later version.                                      //
12
//                                                                           //
13
//  You may not change or alter any portion of this comment or credits       //
14
//  of supporting developers from this source code or any supporting         //
15
//  source code which is considered copyrighted (c) material of the          //
16
//  original comment or credit authors.                                      //
17
//                                                                           //
18
//  This program is distributed in the hope that it will be useful,          //
19
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
20
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
21
//  GNU General Public License for more details.                             //
22
//                                                                           //
23
//  You should have received a copy of the GNU General Public License        //
24
//  along with this program; if not, write to the Free Software              //
25
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
26
//  ------------------------------------------------------------------------ //
27
//  Author: phppp (D.J., [email protected])                                  //
28
//  URL: https://xoops.org                                                    //
29
//  Project: Article Project                                                 //
30
//  ------------------------------------------------------------------------ //
31
// defined('XOOPS_ROOT_PATH') || die('Restricted access');
32
33
use XoopsModules\Newbb;
34
35
if (defined('XOOPS_MODULE_NEWBB_FUCTIONS')) {
36
    exit();
37
}
38
define('XOOPS_MODULE_NEWBB_FUCTIONS', 1);
39
40
include_once $GLOBALS['xoops']->path('modules/newbb/include/functions.ini.php');
41
42
/**
43
 * @param  XoopsModule $module
44
 * @param  null        $oldversion
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $oldversion is correct as it would always require null to be passed?
Loading history...
45
 * @return bool
46
 */
47
function xoops_module_update_newbb(\XoopsModule $module, $oldversion = null)
0 ignored issues
show
Unused Code introduced by
The parameter $oldversion is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

47
function xoops_module_update_newbb(\XoopsModule $module, /** @scrutinizer ignore-unused */ $oldversion = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
48
{
49
    $cacheHelper = new \Xmf\Module\Helper\Cache('newbb');
0 ignored issues
show
Bug introduced by
The type Xmf\Module\Helper\Cache was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
50
    $cacheHelper->delete('config');
51
52
    $newbbConfig = newbbLoadConfig();
53
54
    // remove old html template files
55
    // create an array with all folders, and then run this once
56
57
    $templateDirectory = $GLOBALS['xoops']->path('modules/' . $module->getVar('dirname', 'n') . '/templates/');
58
    $template_list     = array_diff(scandir($templateDirectory, SCANDIR_SORT_NONE), ['..', '.']);
59
    foreach ($template_list as $k => $v) {
60
        $fileinfo = new \SplFileInfo($templateDirectory . $v);
61
        if ('html' === $fileinfo->getExtension() && 'index.html' !== $fileinfo->getFilename()) {
62
            @unlink($templateDirectory . $v);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

62
            /** @scrutinizer ignore-unhandled */ @unlink($templateDirectory . $v);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
63
        }
64
    }
65
    $templateDirectory = $GLOBALS['xoops']->path('modules/' . $module->getVar('dirname', 'n') . '/templates/blocks');
66
    $template_list     = array_diff(scandir($templateDirectory, SCANDIR_SORT_NONE), ['..', '.']);
67
    foreach ($template_list as $k => $v) {
68
        $fileinfo = new \SplFileInfo($templateDirectory . $v);
69
        if ('html' === $fileinfo->getExtension() && 'index.html' !== $fileinfo->getFilename()) {
70
            @unlink($templateDirectory . $v);
71
        }
72
    }
73
    // Load class XoopsFile
74
    xoops_load('xoopsfile');
0 ignored issues
show
Bug introduced by
The function xoops_load was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
    /** @scrutinizer ignore-call */ 
75
    xoops_load('xoopsfile');
Loading history...
75
    //remove /images directory
76
    $imagesDirectory = $GLOBALS['xoops']->path('modules/' . $module->getVar('dirname', 'n') . '/images/');
77
    $folderHandler   = \XoopsFile::getHandler('folder', $imagesDirectory);
0 ignored issues
show
Bug introduced by
The type XoopsFile was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
78
    $folderHandler->delete($imagesDirectory);
79
80
    //remove old changelogs
81
    array_map('unlink', glob(dirname(__DIR__) . '/docs/changelog-rev*.txt'));
82
83
    if (!empty($newbbConfig['syncOnUpdate'])) {
84
        include_once __DIR__ . '/../include/functions.recon.php';
85
        newbbSynchronization();
86
    }
87
88
    return true;
89
}
90
91
/**
92
 * @param  XoopsModule $module
93
 * @return bool
94
 */
95
function xoops_module_pre_update_newbb(\XoopsModule $module)
0 ignored issues
show
Unused Code introduced by
The parameter $module is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

95
function xoops_module_pre_update_newbb(/** @scrutinizer ignore-unused */ \XoopsModule $module)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
96
{
97
    XoopsLoad::load('migrate', 'newbb');
0 ignored issues
show
Bug introduced by
The type XoopsLoad was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
98
    $newbbMigrate = new Newbb\Migrate();
99
    $newbbMigrate->synchronizeSchema();
100
101
    return true;
102
}
103
104
/**
105
 * @param  XoopsModule $module
106
 * @return bool
107
 */
108
function xoops_module_pre_install_newbb(\XoopsModule $module)
109
{
110
    $mod_tables =& $module->getInfo('tables');
111
    foreach ($mod_tables as $table) {
112
        $GLOBALS['xoopsDB']->queryF('DROP TABLE IF EXISTS ' . $GLOBALS['xoopsDB']->prefix($table) . ';');
113
    }
114
115
    return true;
116
}
117
118
/**
119
 * @param  XoopsModule $module
120
 * @return bool
121
 */
122
function xoops_module_install_newbb(\XoopsModule $module)
123
{
124
    /* Create a test category */
125
    /** @var Newbb\CategoryHandler $categoryHandler */
126
    $categoryHandler = Newbb\Helper::getInstance()->getHandler('Category');
127
    $category        = $categoryHandler->create();
128
    $category->setVar('cat_title', _MI_NEWBB_INSTALL_CAT_TITLE, true);
129
    $category->setVar('cat_image', '', true);
130
    $category->setVar('cat_description', _MI_NEWBB_INSTALL_CAT_DESC, true);
131
    $category->setVar('cat_url', 'https://xoops.org XOOPS Project', true);
132
    if (!$cat_id = $categoryHandler->insert($category)) {
133
        return true;
134
    }
135
136
    /* Create a forum for test */
137
    /** @var Newbb\ForumHandler $forumHandler */
138
    $forumHandler = Newbb\Helper::getInstance()->getHandler('Forum');
139
    $forum        = $forumHandler->create();
140
    $forum->setVar('forum_name', _MI_NEWBB_INSTALL_FORUM_NAME, true);
141
    $forum->setVar('forum_desc', _MI_NEWBB_INSTALL_FORUM_DESC, true);
142
    $forum->setVar('forum_moderator', []);
143
    $forum->setVar('parent_forum', 0);
144
    $forum->setVar('cat_id', $cat_id);
145
    $forum->setVar('attach_maxkb', 100);
146
    $forum->setVar('attach_ext', 'zip|jpg|gif|png');
147
    $forum->setVar('hot_threshold', 20);
148
    $forum_id = $forumHandler->insert($forum);
149
150
    /* Set corresponding permissions for the category and the forum */
151
    $module_id = $module->getVar('mid');
152
    /** @var XoopsGroupPermHandler $gpermHandler */
153
    $gpermHandler = xoops_getHandler('groupperm');
0 ignored issues
show
Bug introduced by
The function xoops_getHandler was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

153
    $gpermHandler = /** @scrutinizer ignore-call */ xoops_getHandler('groupperm');
Loading history...
154
    $groups_view  = [XOOPS_GROUP_ADMIN, XOOPS_GROUP_USERS, XOOPS_GROUP_ANONYMOUS];
0 ignored issues
show
Bug introduced by
The constant XOOPS_GROUP_ADMIN was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant XOOPS_GROUP_USERS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant XOOPS_GROUP_ANONYMOUS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
155
    $groups_post  = [XOOPS_GROUP_ADMIN, XOOPS_GROUP_USERS];
156
    // irmtfan bug fix: html and signature permissions, add: pdf and print permissions
157
    $post_items = [
158
        'post',
159
        'reply',
160
        'edit',
161
        'delete',
162
        'addpoll',
163
        'vote',
164
        'attach',
165
        'noapprove',
166
        'type',
167
        'html',
168
        'signature',
169
        'pdf',
170
        'print'
171
    ];
172
    foreach ($groups_view as $group_id) {
173
        $gpermHandler->addRight('category_access', $cat_id, $group_id, $module_id);
174
        $gpermHandler->addRight('forum_access', $forum_id, $group_id, $module_id);
175
        $gpermHandler->addRight('forum_view', $forum_id, $group_id, $module_id);
176
    }
177
    foreach ($groups_post as $group_id) {
178
        foreach ($post_items as $item) {
179
            $gpermHandler->addRight('forum_' . $item, $forum_id, $group_id, $module_id);
180
        }
181
    }
182
183
    /* Create a test post */
184
    include_once __DIR__ . '/functions.user.php';
185
    /** @var Newbb\PostHandler $postHandler */
186
    $postHandler = Newbb\Helper::getInstance()->getHandler('Post');
187
    /** @var  $forumpost */
188
    $forumpost = $postHandler->create();
189
    $forumpost->setVar('poster_ip', \Xmf\IPAddress::fromRequest()->asReadable());
0 ignored issues
show
Bug introduced by
The type Xmf\IPAddress was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
190
    $forumpost->setVar('uid', $GLOBALS['xoopsUser']->getVar('uid'));
191
    $forumpost->setVar('approved', 1);
192
    $forumpost->setVar('forum_id', $forum_id);
193
    $forumpost->setVar('subject', _MI_NEWBB_INSTALL_POST_SUBJECT, true);
194
    $forumpost->setVar('dohtml', 1);
195
    $forumpost->setVar('dosmiley', 1);
196
    $forumpost->setVar('doxcode', 1);
197
    $forumpost->setVar('dobr', 1);
198
    $forumpost->setVar('icon', '', true);
199
    $forumpost->setVar('attachsig', 1);
200
    $forumpost->setVar('post_time', time());
201
    $forumpost->setVar('post_text', _MI_NEWBB_INSTALL_POST_TEXT, true);
202
    $postid = $postHandler->insert($forumpost);
0 ignored issues
show
Unused Code introduced by
The assignment to $postid is dead and can be removed.
Loading history...
203
204
    return true;
205
}
206