Completed
Push — master ( adc6c1...fb8e44 )
by Michael
01:12
created

GbookUtility::checkVerPhp()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 3
nop 1
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
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
 * GbookUtility Class
13
 *
14
 * @copyright   XOOPS Project (https://xoops.org)
15
 * @license     http://www.fsf.org/copyleft/gpl.html GNU public license
16
 * @author      XOOPS Development Team
17
 * @package     GBook
18
 * @since       1.03
19
 *
20
 */
21
22
//namespace GBook;
23
24
/**
25
 * Class GbookUtility
26
 */
27
class GbookUtility
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...
28
{
29
    /**
30
     * Function responsible for checking if a directory exists, we can also write in and create an index.html file
31
     *
32
     * @param string $folder The full path of the directory to check
33
     *
34
     * @return void
35
     */
36
    public static function createFolder($folder)
37
    {
38
        try {
39
            if (!@mkdir($folder) && !is_dir($folder)) {
40
                throw new \RuntimeException(sprintf('Unable to create the %s directory', $folder));
41
            } else {
42
                file_put_contents($folder . '/index.html', '<script>history.go(-1);</script>');
43
            }
44
        } catch (Exception $e) {
45
            echo 'Caught exception: ', $e->getMessage(), "\n", '<br>';
46
        }
47
    }
48
49
    /**
50
     * @param $file
51
     * @param $folder
52
     * @return bool
53
     */
54
    public static function copyFile($file, $folder)
55
    {
56
        return copy($file, $folder);
57
        //        try {
58
        //            if (!is_dir($folder)) {
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...
59
        //                throw new \RuntimeException(sprintf('Unable to copy file as: %s ', $folder));
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% 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...
60
        //            } else {
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...
61
        //                return copy($file, $folder);
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% 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...
62
        //            }
63
        //        } catch (Exception $e) {
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...
64
        //            echo 'Caught exception: ', $e->getMessage(), "\n", "<br>";
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...
65
        //        }
66
        //        return false;
67
    }
68
69
    /**
70
     * @param $src
71
     * @param $dst
72
     */
73
    public static function recurseCopy($src, $dst)
74
    {
75
        $dir = opendir($src);
76
        //    @mkdir($dst);
77
        while (false !== ($file = readdir($dir))) {
78
            if (($file !== '.') && ($file !== '..')) {
79
                if (is_dir($src . '/' . $file)) {
80
                    self::recurseCopy($src . '/' . $file, $dst . '/' . $file);
81
                } else {
82
                    copy($src . '/' . $file, $dst . '/' . $file);
83
                }
84
            }
85
        }
86
        closedir($dir);
87
    }
88
89
    /**
90
     * @param $name
91
     * @param $value
92
     * @return XoopsFormDhtmlTextArea|XoopsFormEditor
93
     */
94
    public static function getEditor($name, $value)
95
    {
96
        global $xoopsUser, $xoopsModule, $xoopsModuleConfig;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
97
        $options = [];
98
        $isAdmin = $xoopsUser->isAdmin($xoopsModule->getVar('mid'));
99
100
        if (class_exists('XoopsFormEditor')) {
101
            $options['name']   = $name;
102
            $options['value']  = $value;
103
            $options['rows']   = 5;
104
            $options['cols']   = '100%';
105
            $options['width']  = '100%';
106
            $options['height'] = '200px';
107
            if ($isAdmin) {
108
                $descEditor = new XoopsFormEditor(ucfirst($name), $xoopsModuleConfig['editorAdmin'], $options, $nohtml = false, $onfailure = 'textarea');
109
            } else {
110
                $descEditor = new XoopsFormEditor(ucfirst($name), $xoopsModuleConfig['editorUser'], $options, $nohtml = false, $onfailure = 'textarea');
111
            }
112
        } else {
113
            $descEditor = new XoopsFormDhtmlTextArea(ucfirst($name), $name, $value, '100%', '100%');
114
        }
115
116
        //        $form->addElement($descEditor);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% 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...
117
118
        return $descEditor;
119
    }
120
121
    /**
122
     * @param $nameTmp
123
     * @param $emailTmp
124
     * @param $urlTmp
125
     * @param $messageTmp
126
     * @return XoopsThemeForm
127
     */
128
    public static function getSignForm($nameTmp, $emailTmp, $urlTmp, $messageTmp)
129
    {
130
        $name      = new XoopsFormText(_MD_GBOOK_NAME, 'Name', 43, 100, $nameTmp);
131
        $email     = new XoopsFormText(_MD_GBOOK_EMAIL, 'Email', 43, 100, $emailTmp);
132
        $url       = new XoopsFormText(_MD_GBOOK_URL, 'URL', 43, 100, $urlTmp);
133
        $message   = new XoopsFormTextArea(_MD_GBOOK_MESSAGE, 'Message', $messageTmp);
134
        $submit    = new XoopsFormButton('', 'submit', _MD_GBOOK_SUBMIT, 'submit');
135
        $gbookform = new XoopsThemeForm(_MD_GBOOK_SIGN, 'gbookform', 'sign.php');
136
137
        $gbookform->addElement($name, true);
138
        $gbookform->addElement($email);
139
        $gbookform->addElement($url);
140
        $gbookform->addElement($message, true);
141
        $gbookform->addElement(new XoopsFormCaptcha(), true);
142
        $gbookform->addElement($submit);
143
144
        return $gbookform;
145
    }
146
147
    /**
148
     * @return string
149
     */
150
    public static function gbookIP()
151
    {
152
        $ip = 'UNKNOWN';
153
        if (getenv('HTTP_CLIENT_IP')) {
154
            $ip = getenv('HTTP_CLIENT_IP');
155
        } else {
156
            if (getenv('HTTP_X_FORWARDED_FOR')) {
157
                $ip = getenv('HTTP_X_FORWARDED_FOR');
158
            } else {
159
                if (getenv('REMOTE_ADDR')) {
160
                    $ip = getenv('REMOTE_ADDR');
161
                }
162
            }
163
        }
164
165
        return $ip;
166
    }
167
    /**
168
     *
169
     * Verifies XOOPS version meets minimum requirements for this module
170
     * @static
171
     * @param XoopsModule $module
0 ignored issues
show
Documentation introduced by
Should the type for parameter $module not be null|XoopsModule?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
172
     *
173
     * @param null|string        $requiredVer
174
     * @return bool true if meets requirements, false if not
175
     */
176
    public static function checkVerXoops(XoopsModule $module = null, $requiredVer = null)
177
    {
178
        $moduleDirName = basename(dirname(__DIR__));
179
        if (null === $module) {
180
            $module        = XoopsModule::getByDirname($moduleDirName);
181
        }
182
        xoops_loadLanguage('admin', $moduleDirName);
183
        //check for minimum XOOPS version
184
        $currentVer = substr(XOOPS_VERSION, 6); // get the numeric part of string
185
        $currArray  = explode('.', $currentVer);
186
        if (null === $requiredVer) {
187
            $requiredVer = '' . $module->getInfo('min_xoops'); //making sure it's a string
188
        }
189
        $reqArray = explode('.', $requiredVer);
190
        $success  = true;
191
        foreach ($reqArray as $k => $v) {
192
            if (isset($currArray[$k])) {
193
                if ($currArray[$k] > $v) {
194
                    break;
195
                } elseif ($currArray[$k] == $v) {
196
                    continue;
197
                } else {
198
                    $success = false;
199
                    break;
200
                }
201
            } else {
202
                if ((int)$v > 0) { // handles versions like x.x.x.0_RC2
203
                    $success = false;
204
                    break;
205
                }
206
            }
207
        }
208
209
        if (false === $success) {
210
            $module->setErrors(sprintf(_AM_GBOOK_ERROR_BAD_XOOPS, $requiredVer, $currentVer));
211
        }
212
213
        return $success;
214
    }
215
216
    /**
217
     *
218
     * Verifies PHP version meets minimum requirements for this module
219
     * @static
220
     * @param XoopsModule $module
221
     *
222
     * @return bool true if meets requirements, false if not
223
     */
224
    public static function checkVerPhp(XoopsModule $module)
225
    {
226
        xoops_loadLanguage('admin', $module->dirname());
227
        // check for minimum PHP version
228
        $success = true;
229
        $verNum  = PHP_VERSION;
230
        $reqVer  = $module->getInfo('min_php');
231
        if (false !== $reqVer && '' !== $reqVer) {
232
            if (version_compare($verNum, (string)$reqVer, '<')) {
233
                $module->setErrors(sprintf(_AM_GBOOK_ERROR_BAD_PHP, $reqVer, $verNum));
234
                $success = false;
235
            }
236
        }
237
238
        return $success;
239
    }
240
}
241