Completed
Branch master (88d491)
by Michael
03:04 queued 01:04
created

GBookUtilities::getEditor()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 3
eloc 17
nc 3
nop 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
//include_once dirname(__DIR__) . '/include/common.php';
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% 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...
23
24
//namespace GBook;
25
26
/**
27
 * Class GBookUtilities
28
 */
29
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...
30
{
31
    /**
32
     * Function responsible for checking if a directory exists, we can also write in and create an index.html file
33
     *
34
     * @param string $folder The full path of the directory to check
35
     *
36
     * @return void
37
     */
38
    public static function createFolder($folder)
39
    {
40
        try {
41
            if (!@mkdir($folder) && !is_dir($folder)) {
42
                throw new \RuntimeException(sprintf('Unable to create the %s directory', $folder));
43
            } else {
44
                file_put_contents($folder . '/index.html', '<script>history.go(-1);</script>');
45
            }
46
        } catch (Exception $e) {
47
            echo 'Caught exception: ', $e->getMessage(), "\n", '<br/>';
48
        }
49
    }
50
51
    /**
52
     * @param $file
53
     * @param $folder
54
     * @return bool
55
     */
56
    public static function copyFile($file, $folder)
57
    {
58
        return copy($file, $folder);
59
        //        try {
60
        //            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...
61
        //                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...
62
        //            } 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...
63
        //                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...
64
        //            }
65
        //        } 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...
66
        //            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...
67
        //        }
68
        //        return false;
69
    }
70
71
    /**
72
     * @param $src
73
     * @param $dst
74
     */
75
    public static function recurseCopy($src, $dst)
76
    {
77
        $dir = opendir($src);
78
        //    @mkdir($dst);
79
        while (false !== ($file = readdir($dir))) {
80
            if (($file !== '.') && ($file !== '..')) {
81
                if (is_dir($src . '/' . $file)) {
82
                    self::recurseCopy($src . '/' . $file, $dst . '/' . $file);
83
                } else {
84
                    copy($src . '/' . $file, $dst . '/' . $file);
85
                }
86
            }
87
        }
88
        closedir($dir);
89
    }
90
91
    /**
92
     * @param $name
93
     * @param $value
94
     * @return XoopsFormDhtmlTextArea|XoopsFormEditor
95
     */
96
    public static function getEditor($name, $value)
97
    {
98
        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...
99
        $isAdmin = $xoopsUser->isAdmin($xoopsModule->getVar('mid'));
100
101
        if (class_exists('XoopsFormEditor')) {
102
            $options['name']   = $name;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$options was never initialized. Although not strictly required by PHP, it is generally a good practice to add $options = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
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
        //        $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 View Code Duplication
    public static function gbookSignForm($nameTmp, $emailTmp, $urlTmp, $messageTmp)
0 ignored issues
show
Duplication introduced by
This method 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...
129
    {
130
        $name      = new XoopsFormText(_GBOOK_NAME, 'Name', 43, 100, $nameTmp);
131
        $email     = new XoopsFormText(_GBOOK_EMAIL, 'Email', 43, 100, $emailTmp);
132
        $url       = new XoopsFormText(_GBOOK_URL, 'URL', 43, 100, $urlTmp);
133
        $message   = new XoopsFormTextArea(_GBOOK_MESSAGE, 'Message', $messageTmp);
134
        $submit    = new XoopsFormButton('', 'submit', _GBOOK_SUBMIT, 'submit');
135
        $gbookform = new XoopsThemeForm(_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 View Code Duplication
    public static function gbookIP()
0 ignored issues
show
Duplication introduced by
This method 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...
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