Completed
Push — master ( f87886...b3b862 )
by Michael
02:18
created

oninstall.php ➔ xoops_module_install_extgallery()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 144
Code Lines 40

Duplication

Lines 7
Ratio 4.86 %

Importance

Changes 0
Metric Value
cc 6
eloc 40
nc 8
nop 1
dl 7
loc 144
rs 8.1463
c 0
b 0
f 0

How to fix   Long Method   

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
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 (http://www.gnu.org/licenses/gpl-2.0.html)
15
 * @author       XOOPS Development Team
16
 */
17
18
/**
19
 *
20
 * Prepares system prior to attempting to install module
21
 * @param XoopsModule $module {@link XoopsModule}
22
 *
23
 * @return bool true if ready to install, false if not
24
 */
25
function xoops_module_pre_install_extgallery(XoopsModule $module)
0 ignored issues
show
Coding Style introduced by
xoops_module_pre_install_extgallery uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
26
{
27
    $moduleDirName = basename(dirname(__DIR__));
28
    $classUtility     = ucfirst($moduleDirName) . 'Utility';
29
    if (!class_exists($classUtility)) {
30
        xoops_load('utility', $moduleDirName);
31
    }
32
    //check for minimum XOOPS version
33
    if (!$classUtility::checkVerXoops($module)) {
34
        return false;
35
    }
36
37
    // check for minimum PHP version
38
    if (!$classUtility::checkVerPhp($module)) {
39
        return false;
40
    }
41
42
    $mod_tables = $module->getInfo('tables');
43
    foreach ($mod_tables as $table) {
44
        $GLOBALS['xoopsDB']->queryF('DROP TABLE IF EXISTS ' . $GLOBALS['xoopsDB']->prefix($table) . ';');
45
    }
46
47
    return true;
48
}
49
50
/**
51
 *
52
 * Performs tasks required during installation of the module
53
 * @param XoopsModule $xoopsModule
54
 * @return bool true if installation successful, false if not
55
 * @internal param XoopsModule $module <a href='psi_element://XoopsModule'>XoopsModule</a>
56
 *
57
 */
58
function xoops_module_install_extgallery(XoopsModule $xoopsModule)
0 ignored issues
show
Coding Style introduced by
xoops_module_install_extgallery uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
59
{
60
    $module_id = $xoopsModule->getVar('mid');
61
    /** @var XoopsGroupPermHandler $gpermHandler */
62
    $gpermHandler = xoops_getHandler('groupperm');
63
    /** @var XoopsModuleHandler $moduleHandler */
64
    $configHandler = xoops_getHandler('config');
65
66
    /**
67
     * Default public category permission mask
68
     */
69
70
    // Access right
71
    $gpermHandler->addRight('extgallery_public_mask', 1, XOOPS_GROUP_ADMIN, $module_id);
72
    $gpermHandler->addRight('extgallery_public_mask', 1, XOOPS_GROUP_USERS, $module_id);
73
    $gpermHandler->addRight('extgallery_public_mask', 1, XOOPS_GROUP_ANONYMOUS, $module_id);
74
75
    // Public rate
76
    $gpermHandler->addRight('extgallery_public_mask', 2, XOOPS_GROUP_ADMIN, $module_id);
77
    $gpermHandler->addRight('extgallery_public_mask', 2, XOOPS_GROUP_USERS, $module_id);
78
79
    // Public eCard
80
    $gpermHandler->addRight('extgallery_public_mask', 4, XOOPS_GROUP_ADMIN, $module_id);
81
    $gpermHandler->addRight('extgallery_public_mask', 4, XOOPS_GROUP_USERS, $module_id);
82
83
    // Public download
84
    $gpermHandler->addRight('extgallery_public_mask', 8, XOOPS_GROUP_ADMIN, $module_id);
85
    $gpermHandler->addRight('extgallery_public_mask', 8, XOOPS_GROUP_USERS, $module_id);
86
87
    // Public upload
88
    $gpermHandler->addRight('extgallery_public_mask', 16, XOOPS_GROUP_ADMIN, $module_id);
89
90
    // Public autoapprove
91
    $gpermHandler->addRight('extgallery_public_mask', 32, XOOPS_GROUP_ADMIN, $module_id);
92
93
    // Public display
94
    $gpermHandler->addRight('extgallery_public_mask', 128, XOOPS_GROUP_ADMIN, $module_id);
95
    $gpermHandler->addRight('extgallery_public_mask', 128, XOOPS_GROUP_USERS, $module_id);
96
    $gpermHandler->addRight('extgallery_public_mask', 128, XOOPS_GROUP_ANONYMOUS, $module_id);
97
98
    /**
99
     * Default User's category permission
100
     */
101
102
    // Private gallery
103
104
    // Private rate
105
    $gpermHandler->addRight('extgallery_private', 2, XOOPS_GROUP_ADMIN, $module_id);
106
    $gpermHandler->addRight('extgallery_private', 2, XOOPS_GROUP_USERS, $module_id);
107
108
    // Private eCard
109
    $gpermHandler->addRight('extgallery_private', 4, XOOPS_GROUP_ADMIN, $module_id);
110
    $gpermHandler->addRight('extgallery_private', 4, XOOPS_GROUP_USERS, $module_id);
111
112
    // Private download
113
    $gpermHandler->addRight('extgallery_private', 8, XOOPS_GROUP_ADMIN, $module_id);
114
    $gpermHandler->addRight('extgallery_private', 8, XOOPS_GROUP_USERS, $module_id);
115
116
    // Private autoapprove
117
    $gpermHandler->addRight('extgallery_private', 16, XOOPS_GROUP_ADMIN, $module_id);
118
119
    /*
120
121
      // Create eXtGallery main upload directory
122
      $dir = XOOPS_ROOT_PATH . '/uploads/extgallery';
123
      if (!is_dir($dir)) {
124
          mkdir($dir, 0777);
125
      }
126
      chmod($dir, 0777);
127
      // Create directory for photo in public album
128
      $dir = XOOPS_ROOT_PATH . '/uploads/extgallery/public-photo';
129
      if (!is_dir($dir)) {
130
          mkdir($dir, 0777);
131
      }
132
      chmod($dir, 0777);
133
      $dir = XOOPS_ROOT_PATH . '/uploads/extgallery/public-photo/original';
134
      if (!is_dir($dir)) {
135
          mkdir($dir, 0777);
136
      }
137
      chmod($dir, 0777);
138
      $dir = XOOPS_ROOT_PATH . '/uploads/extgallery/public-photo/large';
139
      if (!is_dir($dir)) {
140
          mkdir($dir, 0777);
141
      }
142
      chmod($dir, 0777);
143
      $dir = XOOPS_ROOT_PATH . '/uploads/extgallery/public-photo/medium';
144
      if (!is_dir($dir)) {
145
          mkdir($dir, 0777);
146
      }
147
      chmod($dir, 0777);
148
      $dir = XOOPS_ROOT_PATH . '/uploads/extgallery/public-photo/thumb';
149
      if (!is_dir($dir)) {
150
          mkdir($dir, 0777);
151
      }
152
      chmod($dir, 0777);
153
154
155
156
      // Create directory for photo in user's album
157
      //mkdir(XOOPS_ROOT_PATH."/uploads/extgallery/user-photo");
158
159
      // Copy index.html files on uploads folders
160
      $indexFile = XOOPS_ROOT_PATH . '/modules/extgallery/include/index.html';
161
      copy($indexFile, XOOPS_ROOT_PATH . '/uploads/extgallery/index.html');
162
      copy($indexFile, XOOPS_ROOT_PATH . '/uploads/extgallery/public-photo/index.html');
163
      copy($indexFile, XOOPS_ROOT_PATH . '/uploads/extgallery/public-photo/original/index.html');
164
      copy($indexFile, XOOPS_ROOT_PATH . '/uploads/extgallery/public-photo/large/index.html');
165
      copy($indexFile, XOOPS_ROOT_PATH . '/uploads/extgallery/public-photo/medium/index.html');
166
      copy($indexFile, XOOPS_ROOT_PATH . '/uploads/extgallery/public-photo/thumb/index.html');
167
168
  */
169
170
    require_once __DIR__ . '/../../../include/cp_header.php';
171
172
173
        $moduleDirName = basename(dirname(__DIR__));
174
175
176
    //    $moduleDirName = $xoopsModule->getVar('dirname');
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% 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...
177
    $configurator = include $GLOBALS['xoops']->path('modules/' . $moduleDirName . '/include/config.php');
178
179
    $classUtility = ucfirst($moduleDirName) . 'Utility';
180
    if (!class_exists($classUtility)) {
181
        xoops_load('utility', $moduleDirName);
182
    }
183
184
    //    require_once __DIR__ . '/config.php';
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% 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...
185
186
    if (count($configurator['uploadFolders']) > 0) {
187
        //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
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...
188
        foreach (array_keys($configurator['uploadFolders']) as $i) {
189
            $classUtility::createFolder($configurator['uploadFolders'][$i]);
190
        }
191
    }
192 View Code Duplication
    if (count($configurator['copyFiles']) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
193
        $file = __DIR__ . '/../assets/images/blank.png';
194
        foreach (array_keys($configurator['copyFiles']) as $i) {
195
            $dest = $configurator['copyFiles'][$i] . '/blank.png';
196
            $classUtility::copyFile($file, $dest);
197
        }
198
    }
199
200
    return true;
201
}
202