Completed
Push — master ( 4ee861...7492dc )
by Michael
02:09
created

utilities.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 View Code Duplication
class GBookUtilities
0 ignored issues
show
Comprehensibility Best Practice introduced by
The type GBookUtilities has been defined more than once; this definition is ignored, only the first definition in class/utilities.php (L27-166) is considered.

This check looks for classes that have been defined more than once.

If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.

This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.

Loading history...
This class seems to be duplicated in 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...
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)) {
59
        //                throw new \RuntimeException(sprintf('Unable to copy file as: %s ', $folder));
60
        //            } else {
61
        //                return copy($file, $folder);
62
        //            }
63
        //        } catch (Exception $e) {
64
        //            echo 'Caught exception: ', $e->getMessage(), "\n", "<br/>";
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;
97
        $options = array();
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
        //        $form->addElement($descEditor);
116
117
        return $descEditor;
118
    }
119
120
    /**
121
     * @param $nameTmp
122
     * @param $emailTmp
123
     * @param $urlTmp
124
     * @param $messageTmp
125
     * @return XoopsThemeForm
126
     */
127
    public static function gbookSignForm($nameTmp, $emailTmp, $urlTmp, $messageTmp)
128
    {
129
        $name      = new XoopsFormText(_GBOOK_NAME, 'Name', 43, 100, $nameTmp);
130
        $email     = new XoopsFormText(_GBOOK_EMAIL, 'Email', 43, 100, $emailTmp);
131
        $url       = new XoopsFormText(_GBOOK_URL, 'URL', 43, 100, $urlTmp);
132
        $message   = new XoopsFormTextArea(_GBOOK_MESSAGE, 'Message', $messageTmp);
133
        $submit    = new XoopsFormButton('', 'submit', _GBOOK_SUBMIT, 'submit');
134
        $gbookform = new XoopsThemeForm(_GBOOK_SIGN, 'gbookform', 'sign.php');
135
136
        $gbookform->addElement($name, true);
137
        $gbookform->addElement($email);
138
        $gbookform->addElement($url);
139
        $gbookform->addElement($message, true);
140
        $gbookform->addElement(new XoopsFormCaptcha(), true);
141
        $gbookform->addElement($submit);
142
143
        return $gbookform;
144
    }
145
146
    /**
147
     * @return string
148
     */
149
    public static function gbookIP()
150
    {
151
        $ip = 'UNKNOWN';
152
        if (getenv('HTTP_CLIENT_IP')) {
153
            $ip = getenv('HTTP_CLIENT_IP');
154
        } else {
155
            if (getenv('HTTP_X_FORWARDED_FOR')) {
156
                $ip = getenv('HTTP_X_FORWARDED_FOR');
157
            } else {
158
                if (getenv('REMOTE_ADDR')) {
159
                    $ip = getenv('REMOTE_ADDR');
160
                }
161
            }
162
        }
163
164
        return $ip;
165
    }
166
}
167