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

newbb_getImageLibs()   C

Complexity

Conditions 14
Paths 50

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 14
eloc 18
nc 50
nop 0
dl 0
loc 28
rs 5.0864
c 1
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 50 and the first side effect is on line 34.

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: Kazumi Ono (AKA onokazu)                                          //
28
// URL: http://www.myweb.ne.jp/, https://xoops.org/, http://jp.xoops.org/ //
29
// Project: XOOPS Project                                                    //
30
// ------------------------------------------------------------------------- //
31
32
use XoopsModules\Newbb;
33
34
include_once __DIR__ . '/admin_header.php';
35
//include_once __DIR__ . '/../class/Utility.php';
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
36
include_once __DIR__ . '/../include/functions.stats.php';
37
38
$attach_path = $GLOBALS['xoops']->path($GLOBALS['xoopsModuleConfig']['dir_attachments'] . '/');
39
$thumb_path  = $attach_path . 'thumbs/';
40
$folder      = [$attach_path, $thumb_path];
41
42
/** @var Xmf\Module\Admin $adminObject */
43
$adminObject = Xmf\Module\Admin::getInstance();
0 ignored issues
show
Bug introduced by
The type Xmf\Module\Admin 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...
44
45
/**
46
 * @param       $target
47
 * @param  int  $mode
48
 * @return bool
49
 */
50
function newbb_admin_mkdir($target, $mode = 0777)
51
{
52
    $target = str_replace('..', '', $target);
53
54
    // http://www.php.net/manual/en/function.mkdir.php
55
    return is_dir($target) || (newbb_admin_mkdir(dirname($target), $mode) && mkdir($target, $mode));
56
}
57
58
/**
59
 * @param       $target
60
 * @param  int  $mode
61
 * @return bool
62
 */
63
function newbb_admin_chmod($target, $mode = 0777)
64
{
65
    $target = str_replace('..', '', $target);
66
67
    return @chmod($target, $mode);
68
}
69
70
/**
71
 * @return array
72
 */
73
function newbb_getImageLibs()
74
{
75
    $imageLibs = [];
76
    unset($output, $status);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $output seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $status seems to be never defined.
Loading history...
77
    if (1 == $GLOBALS['xoopsModuleConfig']['image_lib'] || 0 == $GLOBALS['xoopsModuleConfig']['image_lib']) {
78
        $path = empty($GLOBALS['xoopsModuleConfig']['path_magick']) ? '' : $GLOBALS['xoopsModuleConfig']['path_magick'] . '/';
79
        @exec($path . 'convert -version', $output, $status);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for exec(). 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

79
        /** @scrutinizer ignore-unhandled */ @exec($path . 'convert -version', $output, $status);

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...
80
        if (empty($status) && !empty($output) && preg_match("/imagemagick[ \t]+([0-9\.]+)/i", $output[0], $matches)) {
81
            $imageLibs['imagemagick'] = $matches[0];
82
        }
83
84
        unset($output, $status);
85
    }
86
    if (2 == $GLOBALS['xoopsModuleConfig']['image_lib'] || 0 == $GLOBALS['xoopsModuleConfig']['image_lib']) {
87
        $path = empty($GLOBALS['xoopsModuleConfig']['path_netpbm']) ? '' : $GLOBALS['xoopsModuleConfig']['path_netpbm'] . '/';
88
        @exec($path . 'jpegtopnm -version 2>&1', $output, $status);
89
        if (empty($status) && !empty($output) && preg_match("/netpbm[ \t]+([0-9\.]+)/i", $output[0], $matches)) {
90
            $imageLibs['netpbm'] = $matches[0];
91
        }
92
        unset($output, $status);
93
    }
94
95
    if (function_exists('gd_info')) {
96
        $tmpInfo         = gd_info();
97
        $imageLibs['gd'] = $tmpInfo['GD Version'];
98
    }
99
100
    return $imageLibs;
101
}
102
103
xoops_cp_header();
0 ignored issues
show
Bug introduced by
The function xoops_cp_header 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

103
/** @scrutinizer ignore-call */ 
104
xoops_cp_header();
Loading history...
104
105
$imageLibs = newbb_getImageLibs();
106
/** @var \XoopsModuleHandler $moduleHandler */
107
$moduleHandler = xoops_getHandler('module');
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

107
$moduleHandler = /** @scrutinizer ignore-call */ xoops_getHandler('module');
Loading history...
108
///** @var Newbb\ReportHandler $reportHandler */
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
109
//$reportHandler = Newbb\Helper::getInstance()->getHandler('Report');
110
111
$isOK = false;
112
// START irmtfan add a poll_module config
113
//XOOPS_POLL
114
$xoopspoll = $moduleHandler->getByDirname($GLOBALS['xoopsModuleConfig']['poll_module']);
115
if (is_object($xoopspoll)) {
116
    $isOK = $xoopspoll->getVar('isactive');
117
}
118
// END irmtfan add a poll_module config
119
120
$memlimit_iniphp    = return_bytes(@ini_get('memory_limit'));
121
$postmaxsize_iniphp = return_bytes(@ini_get('post_max_size'));
122
$uploadlimit        = _AM_NEWBB_MEMLIMITTOLARGE;
123
if ($postmaxsize_iniphp < $memlimit_iniphp) {
124
    $uploadlimit = sprintf(_AM_NEWBB_MEMLIMITOK, return_bytes($postmaxsize_iniphp, true));
125
}
126
127
$adminObject->addInfoBox(_AM_NEWBB_PREFERENCES);
128
// START irmtfan better poll module display link and version - check if xoops poll module is available
129
if ($isOK) {
130
    $pollLink = _AM_NEWBB_AVAILABLE . ': ';
131
    $pollLink .= '<a href="' . XOOPS_URL . '/modules/' . $xoopspoll->getVar('dirname') . '/admin/index.php"';
0 ignored issues
show
Bug introduced by
The constant XOOPS_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
132
    $pollLink .= ' alt="' . $xoopspoll->getVar('name') . ' ' . _VERSION . ' (' . $xoopspoll->getInfo('version') . ') "';
0 ignored issues
show
Bug introduced by
The constant _VERSION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
133
    $pollLink .= ' title="' . $xoopspoll->getVar('name') . ' ' . _VERSION . ' (' . $xoopspoll->getInfo('version') . ') "';
134
    $pollLink .= '>' . '(' . $xoopspoll->getVar('name') . ')</a>';
135
} else {
136
    $pollLink = _AM_NEWBB_NOTAVAILABLE;
137
}
138
$adminObject->addInfoBoxLine(sprintf(_AM_NEWBB_POLLMODULE . ': %s', $pollLink));
139
// END irmtfan better poll module display link and version - check if xoops poll module is available
140
$adminObject->addInfoBoxLine(sprintf(_AM_NEWBB_IMAGEMAGICK . ' %s', array_key_exists('imagemagick', $imageLibs) ? _AM_NEWBB_AUTODETECTED . $imageLibs['imagemagick'] : _AM_NEWBB_NOTAVAILABLE));
141
$adminObject->addInfoBoxLine(sprintf('NetPBM' . ': %s', array_key_exists('netpbm', $imageLibs) ? _AM_NEWBB_AUTODETECTED . $imageLibs['netpbm'] : _AM_NEWBB_NOTAVAILABLE));
142
$adminObject->addInfoBoxLine(sprintf(_AM_NEWBB_GDLIB . ' %s', array_key_exists('gd', $imageLibs) ? _AM_NEWBB_AUTODETECTED . $imageLibs['gd'] : _AM_NEWBB_NOTAVAILABLE));
143
$adminObject->addInfoBoxLine(sprintf(_AM_NEWBB_UPLOAD . ' %s', $uploadlimit));
144
$adminObject->addInfoBoxLine(sprintf(_AM_NEWBB_INDEX_PDF_PAGE . '', ''));
145
146
$adminObject->addInfoBox(_AM_NEWBB_BOARDSUMMARY);
147
$adminObject->addInfoBoxLine(sprintf(_AM_NEWBB_TOTALTOPICS . ': %s', getTotalTopics()));
148
$adminObject->addInfoBoxLine(sprintf(_AM_NEWBB_TOTALPOSTS . ': %s', getTotalPosts()));
149
$adminObject->addInfoBoxLine(sprintf(_AM_NEWBB_TOTALVIEWS . ': %s', getTotalViews()));
0 ignored issues
show
Bug introduced by
Are you sure the usage of getTotalViews() is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
150
151
$adminObject->addInfoBox(_AM_NEWBB_REPORT);
152
$adminObject->addInfoBoxLine(sprintf(_AM_NEWBB_REPORT_PENDING . ': %s', $reportHandler->getCount(new \Criteria('report_result', 0))));
0 ignored issues
show
Bug introduced by
The type Criteria 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...
153
$adminObject->addInfoBoxLine(sprintf(_AM_NEWBB_REPORT_PROCESSED . ': %s', $reportHandler->getCount(new \Criteria('report_result', 1))));
154
155
foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
156
    Newbb\Utility::prepareFolder($uploadFolders[$i]);
157
    $adminObject->addConfigBoxLine($uploadFolders[$i], 'folder');
158
}
159
160
$adminObject->displayNavigation(basename(__FILE__));
161
$adminObject->displayIndex();
162
163
include_once __DIR__ . '/admin_footer.php';
164
165
$cacheHelper = Newbb\Utility::cleanCache();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $cacheHelper is correct as XoopsModules\Newbb\Utility::cleanCache() targeting XoopsModules\Newbb\Utility::cleanCache() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
166
//$cacheHelper = new \Xmf\Module\Helper\Cache('newbb');
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
167
//$cacheHelper->delete('config');
168
//$cacheHelper->delete('permission');
169
170
/**
171
 * @param             $sizeAsString
172
 * @param  bool       $b
173
 * @return int|string
174
 */
175
function return_bytes($sizeAsString, $b = false)
176
{
177
    if (false === $b) {
178
        switch (substr($sizeAsString, -1)) {
179
            case 'M':
180
            case 'm':
181
                return (int)$sizeAsString * 1048576;
182
            case 'K':
183
            case 'k':
184
                return (int)$sizeAsString * 1024;
185
            case 'G':
186
            case 'g':
187
                return (int)$sizeAsString * 1073741824;
188
            default:
189
                return $sizeAsString;
190
        }
191
    } else {
192
        $base   = log($sizeAsString) / log(1024);
193
        $suffix = ['', 'KB', 'MB', 'GB', 'TB'];
194
195
        return round(pow(1024, $base - floor($base))) . ' ' . $suffix[(int)floor($base)];
196
    }
197
}
198