apcal_minical_ex.php ➔ apcal_minical_ex_show()   F
last analyzed

Complexity

Conditions 26
Paths 15169

Size

Total Lines 152

Duplication

Lines 17
Ratio 11.18 %

Importance

Changes 0
Metric Value
cc 26
nc 15169
nop 1
dl 17
loc 152
rs 0
c 0
b 0
f 0

How to fix   Long Method    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
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   {@link http://xoops.org/ XOOPS Project}
14
 * @license     {@link http://www.fsf.org/copyleft/gpl.html GNU public license}
15
 * @package
16
 * @since
17
 * @author       XOOPS Development Team,
18
 * @author       Antiques Promotion (http://www.antiquespromotion.ca)
19
 * @author       GIJ=CHECKMATE (PEAK Corp. http://www.peak.ne.jp/)
20
 */
21
22
if (!defined('APCAL_BLOCK_MINICAL_EX')) {
23
    define('APCAL_BLOCK_MINICAL_EX', 1);
24
25
    // XOOPS 2.1/2.2
26
    if (substr(XOOPS_VERSION, 6, 3) > 2.0) {
27
        $GLOBALS['apcal_blockinstance_id'] = $this->getVar('instanceid');
28
    }
29
30
    /**
31
     * @param $options
32
     * @return array|mixed
33
     */
34
    function apcal_minical_ex_show($options)
35
    {
36
        global $xoopsConfig, $xoopsDB, $xoopsUser;
37
38
        // speed check
39
        //global $GIJ_common_time ;
40
        //list($usec, $sec) = explode(" ",microtime());
41
        //echo ((float) $sec + (float) $usec) - $GIJ_common_time ;
42
43
        // get bid
44
        if (substr(XOOPS_VERSION, 6, 3) > 2.0) {
45
            // XOOPS 2.1/2.2
46
            // instanceid as bid from block_instance
47
            $bid = @$GLOBALS['apcal_blockinstance_id'];
48
        } else {
49
            // XOOPS 2.0.x
50
            if (is_object($GLOBALS['block_arr'][$GLOBALS['i']])) {
51
                // bid from newblocks
52
                $bid = $GLOBALS['block_arr'][$GLOBALS['i']]->getVar('bid');
53
            } else {
54
                return array();
55
            }
56
        }
57
58
        $moduleDirName = empty($options[0]) ? basename(dirname(__DIR__)) : $options[0];
59
        $gifaday       = empty($options[1]) ? 2 : (int)$options[1];
60
        $just1gif      = empty($options[2]) ? 0 : 1;
61
        //  $plugins_tmp = empty( $options[3] ) ? array() : explode( ',' , $options[3] ) ;
62
        // robots mode (arrows in minicalex will point not a current page but APCal)
63
        $robots_mode = preg_match('/(msnbot|Googlebot|Yahoo! Slurp)/i', $_SERVER['HTTP_USER_AGENT']);
64
65
        // GET URL extraction
66
        // Only integer values are valid (for preventing from XSS)
67
        $additional_get = '';
68
        if (!$robots_mode) {
69
            foreach ($_GET as $g_key => $g_val) {
70
                if ($g_key === 'caldate' || $g_key == session_name()) {
71
                    continue;
72
                }
73
                if ((int)$g_val != $g_val) {
74
                    $additional_get = '';
75
                    break;
76
                } else {
77
                    $additional_get .= '&amp;' . urlencode($g_key) . '=' . (int)$g_val;
78
                }
79
            }
80
        }
81
82
        // cache enable or not
83
        if (empty($_POST['apcal_jumpcaldate'])
84
            && (empty($_GET['caldate'])
85
                || in_array(substr($_GET['caldate'], 0, 4), array(date('Y'), date('Y') - 1)))
86
        ) {
87
            $enable_cache = true;
88
            //      $enable_cache = false ;
89
        } else {
90
            $enable_cache = false;
91
        }
92
93
        // cache read
94
        if ($enable_cache) {
95
            if (empty($_GET['caldate'])) {
96
                $Ym = date('Ym');
97
            } else {
98
                list($Y, $m) = explode('-', $_GET['caldate']);
99
                if (empty($m)) {
100
                    $Ym = date('Ym');
101
                } else {
102
                    $Ym = sprintf('%04d%02d', $Y, $m);
103
                }
104
            }
105
            $bid_hash   = substr(md5($bid . XOOPS_DB_PREFIX), -6);
106
            $uid        = is_object($xoopsUser) ? $xoopsUser->getVar('uid') : 0;
107
            $cache_file = XOOPS_CACHE_PATH . "/{$moduleDirName }_minical_ex_{$bid_hash}_{$xoopsConfig['language']}_";
108
            if (file_exists($cache_file . $Ym)) {
109
                $cache_bodies = file($cache_file . $Ym);
110
                if (count($cache_bodies) == 3) {
111
                    $expire   = (int)$cache_bodies[0];
112
                    $prev_uid = (int)$cache_bodies[1];
113
                    if ($expire > time() && $prev_uid == $uid) {
114
                        $block = unserialize($cache_bodies[2]);
115 View Code Duplication
                        if ($robots_mode) {
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...
116
                            $block['root_url'] = $block['mod_url'] . '/';
117
                            // $block['php_self'] = '/' ;
118
                            $block['additional_get'] = '';
119
                        } else {
120
                            $block['root_url'] = '';
121
                            // $block['php_self'] = $_SERVER['SCRIPT_NAME'] ;
122
                            $block['additional_get'] = $additional_get;
123
                        }
124
                        // speed check
125
                        //list($usec, $sec) = explode(" ",microtime());
126
                        //echo ((float) $sec + (float) $usec) - $GIJ_common_time ;
127
                        return $block;
128
                    }
129
                }
130
            }
131
        }
132
133
        // MyTextSanitizer
134
        $myts = MyTextSanitizer::getInstance();
135
136
        // setting physical & virtual paths
137
        $mod_path = XOOPS_ROOT_PATH . "/modules/$moduleDirName";
138
        $mod_url  = XOOPS_URL . "/modules/$moduleDirName";
139
140
        // defining class of APCal
141
        if (!class_exists('APCal_xoops')) {
142
            require_once "$mod_path/class/APCal.php";
143
            require_once "$mod_path/class/APCal_xoops.php";
144
        }
145
146
        // creating an instance of APCal
147
        $cal = new APCal_xoops('', $xoopsConfig['language'], true);
148
149
        // ignoring cid from GET
150
        $cal->now_cid = 0;
151
152
        // setting properties of APCal
153
        $cal->conn = $GLOBALS['xoopsDB']->conn;
154
        include "$mod_path/include/read_configs.php";
155
        $cal->base_url    = $mod_url;
156
        $cal->base_path   = $mod_path;
157
        $cal->images_url  = "$mod_url/assets/images/$skin_folder";
0 ignored issues
show
Bug introduced by
The variable $skin_folder does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
158
        $cal->images_path = "$mod_path/assets/images/$skin_folder";
159
160
        $block = $cal->get_minical_ex($gifaday, $just1gif, $cal->get_plugins("mcx{$bid}"));
161
162
        // speed check
163
        // global $GIJ_common_time ;
164
        // list($usec, $sec) = explode(" ",microtime());
165
        // echo ((float) $sec + (float) $usec) - $GIJ_common_time ;
166
167
        if ($enable_cache) {
168
            $fp = fopen($cache_file . sprintf('%04d%02d', $cal->year, $cal->month), 'w');
0 ignored issues
show
Bug introduced by
The variable $cache_file 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...
169
            fwrite($fp, (time() + 300) . "\n"); // 5 mininutes (hard coded)
170
            fwrite($fp, (int)$uid . "\n");
0 ignored issues
show
Bug introduced by
The variable $uid 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...
171
            fwrite($fp, serialize($block));
172
            fclose($fp);
173
        }
174
175 View Code Duplication
        if ($robots_mode) {
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...
176
            $block['root_url'] = $block['mod_url'] . '/';
177
            // $block['php_self'] = '/' ;
178
            $block['additional_get'] = '';
179
        } else {
180
            $block['root_url']       = '';
181
            $block['additional_get'] = $additional_get;
182
        }
183
184
        return $block;
185
    }
186
187
    /**
188
     * @param $options
189
     * @return string
190
     */
191
    function apcal_minical_ex_edit($options)
192
    {
193
        global $xoopsDB, $xoopsConfig;
194
195
        $moduleDirName      = empty($options[0]) ? basename(dirname(__DIR__)) : $options[0];
196
        $gifaday            = empty($options[1]) ? 2 : (int)$options[1];
197
        $just1gif_radio_yes = empty($options[2]) ? '' : 'checked';
198
        $just1gif_radio_no  = empty($options[2]) ? 'checked' : '';
199
        //$plugins4disp = empty( $options[3] ) ? '' : htmlspecialchars( str_replace( array( ' ' , "\t" , "\0" ) , '' ,  $options[3] ) , ENT_QUOTES ) ;
200
201
        // setting physical & virtual paths
202
        $mod_path = XOOPS_ROOT_PATH . "/modules/$moduleDirName";
203
        $mod_url  = XOOPS_URL . "/modules/$moduleDirName";
204
205
        // defining class of APCal
206
        require_once "$mod_path/class/APCal.php";
207
        require_once "$mod_path/class/APCal_xoops.php";
208
209
        // creating an instance of APCal
210
        $cal                = new APCal_xoops(date('Y-n-j'), $xoopsConfig['language'], true);
211
        $cal->use_server_TZ = true;
212
213
        // setting properties of APCal
214
        $cal->conn = $GLOBALS['xoopsDB']->conn;
215
        include "$mod_path/include/read_configs.php";
216
        $cal->base_url    = $mod_url;
217
        $cal->base_path   = $mod_path;
218
        $cal->images_url  = "$mod_url/assets/images/$skin_folder";
0 ignored issues
show
Bug introduced by
The variable $skin_folder does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
219
        $cal->images_path = "$mod_path/assets/images/$skin_folder";
220
221
        $ret = "<input type='hidden' name='options[0]' value='$moduleDirName' />\n";
222
223
        // max dotgifs a day
224
        $ret .= _MB_APCAL_MAXGIFSADAY . ':';
225
        $ret .= "<input type='text' size='4' name='options[1]' value='$gifaday' style='text-align:right;' /><br>\n";
226
227
        // disallow multi gifs per a plugin per a day
228
        $ret .= _MB_APCAL_JUSTONCEADAYAPLUGIN . ':';
229
        $ret .= "<input type='radio' name='options[2]' value='1' $just1gif_radio_yes />" . _YES . "&nbsp;\n";
230
        $ret .= "<input type='radio' name='options[2]' value='0' $just1gif_radio_no />" . _NO . "<br>\n";
231
232
        return $ret;
233
    }
234
}
235