Completed
Push — master ( aeaa5d...9fb24b )
by Gino
23:28 queued 09:34
created

AdminPermissions::getPermissionsHeader()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 24
rs 8.9713
cc 3
eloc 19
nc 3
nop 2
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 29 and the first side effect is on line 24.

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
 You may not change or alter any portion of this comment or credits
5
 of supporting developers from this source code or any supporting source code
6
 which is considered copyrighted (c) material of the original comment or credit authors.
7
8
 This program is distributed in the hope that it will be useful,
9
 but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
/**
13
 * tdmcreate module.
14
 *
15
 * @copyright       The XOOPS Project http://sourceforge.net/projects/xoops/
16
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
17
 *
18
 * @since           2.5.0
19
 *
20
 * @author          Txmod Xoops http://www.txmodxoops.org
21
 *
22
 * @version         $Id: AdminPermissions.php 12258 2014-01-02 09:33:29Z timgno $
23
 */
24
defined('XOOPS_ROOT_PATH') || die('Restricted access');
25
26
/**
27
 * Class AdminPermissions.
28
 */
29
class AdminPermissions extends TDMCreateFile
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
30
{
31
    /*
32
    *  @public function constructor
33
    *  @param null
34
    */
35
    /**
36
     *
37
     */
38
    public function __construct()
39
    {
40
        parent::__construct();
41
        $this->phpcode = TDMCreatePhpCode::getInstance();
0 ignored issues
show
Bug introduced by
The property phpcode does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
42
        $this->xoopscode = TDMCreateXoopsCode::getInstance();
0 ignored issues
show
Bug introduced by
The property xoopscode does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
43
        $this->adminxoopscode = AdminXoopsCode::getInstance();
0 ignored issues
show
Bug introduced by
The property adminxoopscode does not seem to exist. Did you mean xoopscode?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
44
    }
45
46
    /*
47
    *  @static function &getInstance
48
    *  @param null
49
    */
50
    /**
51
     * @return AdminPermissions
52
     */
53
    public static function &getInstance()
54
    {
55
        static $instance = false;
56
        if (!$instance) {
57
            $instance = new self();
58
        }
59
60
        return $instance;
61
    }
62
63
    /*
64
    *  @public function write
65
    *  @param string $module
66
    *  @param mixed $tables
67
    *  @param string $filename
68
    */
69
    /**
70
     * @param $module
71
     * @param $tables
72
     * @param $filename
73
     */
74
    public function write($module, $tables, $filename)
75
    {
76
        $this->setModule($module);
77
        $this->setTables($tables);
78
        $this->setFileName($filename);
79
    }
80
81
    /*
82
     * @private function getPermissionsHeader    
83
     * @param $module
84
     * @param $language
85
     *
86
     * @return string
87
     */
88
    private function getPermissionsHeader($module, $language)
89
    {
90
        $moduleDirname = $module->getVar('mod_dirname');
91
        $tables = $this->getTableTables($module->getVar('mod_id'));
92
        foreach (array_keys($tables) as $t) {
93
            if (1 == $tables[$t]->getVar('table_permissions')) {
94
                $tableName = $tables[$t]->getVar('table_name');
95
            }
96
        }
97
        $ret = $this->getInclude('header');		
98
		$ret .= $this->phpcode->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'class/xoopsform/grouppermform', true);		
99
        $ret .= $this->xoopscode->getXoopsHandlerLine($moduleDirname, $tableName);
0 ignored issues
show
Bug introduced by
The variable $tableName does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
100
		$ret .= $this->getCommentLine('Check admin have access to this page');
101
		$ret .= $this->adminxoopscode->getAdminTemplateMain($moduleDirname, 'permissions');		
0 ignored issues
show
Bug introduced by
The property adminxoopscode does not seem to exist. Did you mean xoopscode?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
102
		$ret .= $this->xoopscode->getXoopsCodeTplAssign('navigation', "\$adminMenu->addNavigation('permissions.php')");
103
		$ret .= $this->xoopscode->getXoopsCodeXoopsRequest('op', 'op', 'global');
104
		$ret .= $this->xoopscode->getXoopsCodeLoad('XoopsFormLoader');
105
		$optionsSelect = array('global' => "{$language}PERMISSIONS_GLOBAL", 'approve' => "{$language}PERMISSIONS_GLOBAL", 
106
								'submit' => "{$language}PERMISSIONS_GLOBAL", 'view' => "{$language}PERMISSIONS_GLOBAL");
107
		$formSelect = $this->xoopscode->getXoopsFormSelect('formSelect', '\'\'', 'op', $optionsSelect, 'onchange="document.fselperm.submit()"');
108
		$ret .= $this->xoopscode->getXoopsSimpleForm('permTableForm', 'formSelect', $formSelect, '\'\'', 'fselperm', 'permissions');
109
110
        return $ret;
111
    }
112
113
    /*
114
     *  @private function getPermissionsSwitch
115
     *  @param $moduleDirname
116
     *  @param $language
117
     *
118
     * @return string
119
     */
120
    private function getPermissionsSwitch($moduleDirname, $language)
121
    {        		
122
		$cases = array('global' => array("\$formTitle = {$language}PERMISSIONS_GLOBAL;",
123
										"\$permName = '{$moduleDirname}_ac';",
124
										"\$permDesc = {$language}PERMISSIONS_GLOBAL_DESC;",
125
										"\$globalPerms = array( '4' => {$language}PERMISSIONS_GLOBAL_4, '8' => {$language}PERMISSIONS_GLOBAL_8, '16' => {$language}PERMISSIONS_GLOBAL_16 );"), 
126
						'approve' => array("\$formTitle = {$language}PERMISSIONS_APPROVE;",
127
										"\$permName = '{$moduleDirname}_approve';",
128
										"\$permDesc = {$language}PERMISSIONS_APPROVE_DESC;"),
129
						'submit' => array("\$formTitle = {$language}PERMISSIONS_SUBMIT;",
130
										"\$permName = '{$moduleDirname}_submit';",
131
										"\$permDesc = {$language}PERMISSIONS_SUBMIT_DESC;"),
132
						'view' => array("\$formTitle = {$language}PERMISSIONS_VIEW;", 
133
										"\$permName = '{$moduleDirname}_view';", 
134
										"\$permDesc = {$language}PERMISSIONS_VIEW_DESC;"));
135
		
136
		$contentSwitch = $this->phpcode->getPhpCodeCaseSwitch($cases, true);
137
        
138
		return $this->phpcode->getPhpCodeSwitch('op', $contentSwitch);
139
    }
140
141
    /*
142
    *  @private function getPermissionsBody
143
    *  @param string $module
144
    *  @param string $language
145
    */
146
    /**
147
     * @param $module
148
     * @param $language
149
     *
150
     * @return string
151
     */
152
    private function getPermissionsBody($module, $language)
153
    {
154
        $tables = $this->getTableTables($module->getVar('mod_id'));
155
        foreach (array_keys($tables) as $t) {
156
            if (1 == $tables[$t]->getVar('table_permissions')) {
157
                $tableId = $tables[$t]->getVar('table_id');
158
                $tableMid = $tables[$t]->getVar('table_mid');
159
                $tableName = $tables[$t]->getVar('table_name');
160
            }
161
        }
162
        $ucfTableName = ucfirst($tableName);
0 ignored issues
show
Bug introduced by
The variable $tableName does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Unused Code introduced by
$ucfTableName is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
163
        $fields = $this->getTableFields($tableMid, $tableId);
0 ignored issues
show
Bug introduced by
The variable $tableMid does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $tableId does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
164
        $fieldId = 'id';
165
        $fieldMain = 'title';
166
        foreach (array_keys($fields) as $f) {
167
            $fieldName = $fields[$f]->getVar('field_name');
168
            if (0 == $f) {
169
                $fieldId = $fieldName;
170
            }
171
            if (1 == $fields[$f]->getVar('field_main')) {
172
                $fieldMain = $fieldName;
173
            }
174
        }		
175
        
176
		$ret = $this->xoopscode->getXoopsCodeGetVar('moduleId', 'xoopsModule', 'mid');
177
		$ret .= $this->xoopscode->getXoopsCodeGroupPermForm('permform', '$formTitle', '$moduleId', '$permName', '$permDesc', "'admin/permissions.php'");
178
		$foreach1 = "\t".$this->xoopscode->getXoopsCodeAddItem('permform', '$gPermId', '$gPermName');
179
        $if1 = $this->phpcode->getPhpCodeForeach('globalPerms', false, 'gPermId', 'gPermName', $foreach1);
180
		$if1 .= "\t".$this->xoopscode->getXoopsCodeTplAssign('form', '$permform->render()');
181
		$else = $this->xoopscode->getXoopsCodeObjHandlerCount($tableName);
182
		$else .= $this->xoopscode->getXoopsCodeObjHandlerAll($tableName, $fieldMain);
183
		$getVar1 = $this->xoopscode->getXoopsCodeGetVar('', "{$tableName}All[\$i]", $fieldId, true);
184
		$getVar2 = $this->xoopscode->getXoopsCodeGetVar('', "{$tableName}All[\$i]", $fieldMain, true);
185
		$foreach2 = $this->xoopscode->getXoopsCodeAddItem('permform', $getVar1, $getVar2);
186
		$else .=  $this->phpcode->getPhpCodeForeach("{$tableName}All", true, false, 'i', $foreach2);
187
		$if2 = $this->xoopscode->getXoopsCodeTplAssign('form', '$permform->render()');
188
		$elseInter = $this->xoopscode->getXoopsCodeRedirectHeader($tableName, '?op=new', '3', "{$language}NO_PERMISSIONS_SET");
189
		$elseInter .= $this->getSimpleString("\texit();");
190
		$else .= "\t".$this->phpcode->getPhpCodeConditions("\${$tableName}Count", ' > ', '0', $if2, $elseInter);
191
		
192
		$ret .= "\t".$this->phpcode->getPhpCodeConditions('$op', ' == ', "'global'", $if1, $else);
193
		$ret .= $this->phpcode->getPhpCodeUnset('permform');
194
		
195
		return $ret;
196
    }
197
198
    /*
199
    *  @public function render
200
    *  @param null
201
    */
202
    /**
203
     * @return bool|string
204
     */
205
    public function render()
206
    {
207
        $module = $this->getModule();
208
        $filename = $this->getFileName();
209
        $moduleDirname = $module->getVar('mod_dirname');
210
        $language = $this->getLanguage($moduleDirname, 'AM');
211
        $content = $this->getHeaderFilesComments($module, $filename);
212
        $content .= $this->getPermissionsHeader($module, $language);
213
        $content .= $this->getPermissionsSwitch($moduleDirname, $language);
214
        $content .= $this->getPermissionsBody($module, $language);
215
        $content .= $this->getInclude('footer');
216
        //
217
        $this->create($moduleDirname, 'admin', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED);
0 ignored issues
show
Documentation introduced by
_AM_TDMCREATE_FILE_CREATED is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
_AM_TDMCREATE_FILE_NOTCREATED is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
218
219
        return $this->renderFile();
220
    }
221
}
222