Completed
Push — master ( db7223...a1696c )
by Michael
01:56
created

GbookUtilities::gbookIP()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
c 0
b 0
f 0
nc 4
nop 0
dl 0
loc 17
rs 9.2
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
 * GbookUtilities Class
13
 *
14
 * @copyright   XOOPS Project (http://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 GbookUtilities
26
 */
27
class GbookUtilities
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
        }
45
        catch (Exception $e) {
46
            echo 'Caught exception: ', $e->getMessage(), "\n", '<br/>';
47
        }
48
    }
49
50
    /**
51
     * @param $file
52
     * @param $folder
53
     * @return bool
54
     */
55
    public static function copyFile($file, $folder)
56
    {
57
        return copy($file, $folder);
58
        //        try {
59
        //            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...
60
        //                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...
61
        //            } 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...
62
        //                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...
63
        //            }
64
        //        } 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...
65
        //            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...
66
        //        }
67
        //        return false;
68
    }
69
70
    /**
71
     * @param $src
72
     * @param $dst
73
     */
74
    public static function recurseCopy($src, $dst)
75
    {
76
        $dir = opendir($src);
77
        //    @mkdir($dst);
78
        while (false !== ($file = readdir($dir))) {
79
            if (($file !== '.') && ($file !== '..')) {
80
                if (is_dir($src . '/' . $file)) {
81
                    self::recurseCopy($src . '/' . $file, $dst . '/' . $file);
82
                } else {
83
                    copy($src . '/' . $file, $dst . '/' . $file);
84
                }
85
            }
86
        }
87
        closedir($dir);
88
    }
89
90
    /**
91
     * @param $name
92
     * @param $value
93
     * @return XoopsFormDhtmlTextArea|XoopsFormEditor
94
     */
95
    public static function getEditor($name, $value)
96
    {
97
        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...
98
        $options = array();
99
        $isAdmin = $xoopsUser->isAdmin($xoopsModule->getVar('mid'));
100
101
        if (class_exists('XoopsFormEditor')) {
102
            $options['name']   = $name;
103
            $options['value']  = $value;
104
            $options['rows']   = 5;
105
            $options['cols']   = '100%';
106
            $options['width']  = '100%';
107
            $options['height'] = '200px';
108
            if ($isAdmin) {
109
                $descEditor = new XoopsFormEditor(ucfirst($name), $xoopsModuleConfig['editorAdmin'], $options, $nohtml = false, $onfailure = 'textarea');
110
            } else {
111
                $descEditor = new XoopsFormEditor(ucfirst($name), $xoopsModuleConfig['editorUser'], $options, $nohtml = false, $onfailure = 'textarea');
112
            }
113
        } else {
114
            $descEditor = new XoopsFormDhtmlTextArea(ucfirst($name), $name, $value, '100%', '100%');
115
        }
116
117
        //        $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...
118
119
        return $descEditor;
120
    }
121
122
    /**
123
     * @param $nameTmp
124
     * @param $emailTmp
125
     * @param $urlTmp
126
     * @param $messageTmp
127
     * @return XoopsThemeForm
128
     */
129
    public static function getSignForm($nameTmp, $emailTmp, $urlTmp, $messageTmp)
130
    {
131
        $name      = new XoopsFormText(_MD_GBOOK_NAME, 'Name', 43, 100, $nameTmp);
132
        $email     = new XoopsFormText(_MD_GBOOK_EMAIL, 'Email', 43, 100, $emailTmp);
133
        $url       = new XoopsFormText(_MD_GBOOK_URL, 'URL', 43, 100, $urlTmp);
134
        $message   = new XoopsFormTextArea(_MD_GBOOK_MESSAGE, 'Message', $messageTmp);
135
        $submit    = new XoopsFormButton('', 'submit', _MD_GBOOK_SUBMIT, 'submit');
136
        $gbookform = new XoopsThemeForm(_MD_GBOOK_SIGN, 'gbookform', 'sign.php');
137
138
        $gbookform->addElement($name, true);
139
        $gbookform->addElement($email);
140
        $gbookform->addElement($url);
141
        $gbookform->addElement($message, true);
142
        $gbookform->addElement(new XoopsFormCaptcha(), true);
143
        $gbookform->addElement($submit);
144
145
        return $gbookform;
146
    }
147
148
    /**
149
     * @return string
150
     */
151
    public static function gbookIP()
152
    {
153
        $ip = 'UNKNOWN';
154
        if (getenv('HTTP_CLIENT_IP')) {
155
            $ip = getenv('HTTP_CLIENT_IP');
156
        } else {
157
            if (getenv('HTTP_X_FORWARDED_FOR')) {
158
                $ip = getenv('HTTP_X_FORWARDED_FOR');
159
            } else {
160
                if (getenv('REMOTE_ADDR')) {
161
                    $ip = getenv('REMOTE_ADDR');
162
                }
163
            }
164
        }
165
166
        return $ip;
167
    }
168
    /**
169
     *
170
     * Verifies XOOPS version meets minimum requirements for this module
171
     * @static
172
     * @param XoopsModule $module
173
     *
174
     * @return bool true if meets requirements, false if not
175
     */
176
    public static function checkXoopsVer(XoopsModule $module)
177
    {
178
        xoops_loadLanguage('admin', $module->dirname());
179
        //check for minimum XOOPS version
180
        $currentVer  = substr(XOOPS_VERSION, 6); // get the numeric part of string
181
        $currArray   = explode('.', $currentVer);
182
        $requiredVer = '' . $module->getInfo('min_xoops'); //making sure it's a string
183
        $reqArray    = explode('.', $requiredVer);
184
        $success     = true;
185
        foreach ($reqArray as $k => $v) {
186
            if (isset($currArray[$k])) {
187
                if ($currArray[$k] > $v) {
188
                    break;
189
                } elseif ($currArray[$k] == $v) {
190
                    continue;
191
                } else {
192
                    $success = false;
193
                    break;
194
                }
195
            } else {
196
                if ((int)$v > 0) { // handles things like x.x.x.0_RC2
197
                    $success = false;
198
                    break;
199
                }
200
            }
201
        }
202
203
        if (!$success) {
204
            $module->setErrors(sprintf(_AM_GBOOK_ERROR_BAD_XOOPS, $requiredVer, $currentVer));
205
        }
206
207
        return $success;
208
    }
209
210
    /**
211
     *
212
     * Verifies PHP version meets minimum requirements for this module
213
     * @static
214
     * @param XoopsModule $module
215
     *
216
     * @return bool true if meets requirements, false if not
217
     */
218
    public static function checkPhpVer(XoopsModule $module)
219
    {
220
        xoops_loadLanguage('admin', $module->dirname());
221
        // check for minimum PHP version
222
        $success = true;
223
        $verNum  = phpversion();
224
        $reqVer  =& $module->getInfo('min_php');
225
        if (false !== $reqVer && '' !== $reqVer) {
226
            if (version_compare($verNum, $reqVer, '<')) {
227
                $module->setErrors(sprintf(_AM_GBOOK_ERROR_BAD_PHP, $reqVer, $verNum));
228
                $success = false;
229
            }
230
        }
231
232
        return $success;
233
    }
234
}
235