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 | class GBookUtilities |
||
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 | $isAdmin = $xoopsUser->isAdmin($xoopsModule->getVar('mid')); |
||
98 | |||
99 | if (class_exists('XoopsFormEditor')) { |
||
100 | $options['name'] = $name; |
||
0 ignored issues
–
show
|
|||
101 | $options['value'] = $value; |
||
102 | $options['rows'] = 5; |
||
103 | $options['cols'] = '100%'; |
||
104 | $options['width'] = '100%'; |
||
105 | $options['height'] = '200px'; |
||
106 | if ($isAdmin) { |
||
107 | $descEditor = new XoopsFormEditor(ucfirst($name), $xoopsModuleConfig['editorAdmin'], $options, $nohtml = false, $onfailure = 'textarea'); |
||
108 | } else { |
||
109 | $descEditor = new XoopsFormEditor(ucfirst($name), $xoopsModuleConfig['editorUser'], $options, $nohtml = false, $onfailure = 'textarea'); |
||
110 | } |
||
111 | } else { |
||
112 | $descEditor = new XoopsFormDhtmlTextArea(ucfirst($name), $name, $value, '100%', '100%'); |
||
113 | } |
||
114 | // $form->addElement($descEditor); |
||
115 | |||
116 | return $descEditor; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @param $nameTmp |
||
121 | * @param $emailTmp |
||
122 | * @param $urlTmp |
||
123 | * @param $messageTmp |
||
124 | * @return XoopsThemeForm |
||
125 | */ |
||
126 | public static function gbookSignForm($nameTmp, $emailTmp, $urlTmp, $messageTmp) |
||
127 | { |
||
128 | $name = new XoopsFormText(_GBOOK_NAME, 'Name', 43, 100, $nameTmp); |
||
129 | $email = new XoopsFormText(_GBOOK_EMAIL, 'Email', 43, 100, $emailTmp); |
||
130 | $url = new XoopsFormText(_GBOOK_URL, 'URL', 43, 100, $urlTmp); |
||
131 | $message = new XoopsFormTextArea(_GBOOK_MESSAGE, 'Message', $messageTmp); |
||
132 | $submit = new XoopsFormButton('', 'submit', _GBOOK_SUBMIT, 'submit'); |
||
133 | $gbookform = new XoopsThemeForm(_GBOOK_SIGN, 'gbookform', 'sign.php'); |
||
134 | |||
135 | $gbookform->addElement($name, true); |
||
136 | $gbookform->addElement($email); |
||
137 | $gbookform->addElement($url); |
||
138 | $gbookform->addElement($message, true); |
||
139 | $gbookform->addElement(new XoopsFormCaptcha(), true); |
||
140 | $gbookform->addElement($submit); |
||
141 | |||
142 | return $gbookform; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * @return string |
||
147 | */ |
||
148 | public static function gbookIP() |
||
149 | { |
||
150 | $ip = 'UNKNOWN'; |
||
151 | if (getenv('HTTP_CLIENT_IP')) { |
||
152 | $ip = getenv('HTTP_CLIENT_IP'); |
||
153 | } else { |
||
154 | if (getenv('HTTP_X_FORWARDED_FOR')) { |
||
155 | $ip = getenv('HTTP_X_FORWARDED_FOR'); |
||
156 | } else { |
||
157 | if (getenv('REMOTE_ADDR')) { |
||
158 | $ip = getenv('REMOTE_ADDR'); |
||
159 | } |
||
160 | } |
||
161 | } |
||
162 | |||
163 | return $ip; |
||
164 | } |
||
165 | } |
||
166 |
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:
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 thebar
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.