Completed
Branch master (a2dd5b)
by Michael
03:54 queued 01:50
created

GBookUtilities   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 18
c 2
b 0
f 0
lcom 0
cbo 0
dl 0
loc 140
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createFolder() 0 12 4
A copyFile() 0 14 1
B recurseCopy() 0 15 5
B getEditor() 0 25 3
A gbookSignForm() 0 18 1
A gbookIP() 0 17 4
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
        } 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 = 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);
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...
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