| Conditions | 45 |
| Paths | 1346 |
| Total Lines | 195 |
| Code Lines | 107 |
| Lines | 8 |
| Ratio | 4.1 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 87 | function makeFilesWritable(&$files) |
||
| 88 | { |
||
| 89 | global $upcontext, $boarddir, $sourcedir; |
||
| 90 | |||
| 91 | if (empty($files)) |
||
| 92 | return true; |
||
| 93 | |||
| 94 | $failure = false; |
||
| 95 | // On linux, it's easy - just use is_writable! |
||
| 96 | if (substr(__FILE__, 1, 2) != ':\\') |
||
| 97 | { |
||
| 98 | $upcontext['systemos'] = 'linux'; |
||
| 99 | |||
| 100 | foreach ($files as $k => $file) |
||
| 101 | { |
||
| 102 | if (!is_writable($file)) |
||
| 103 | { |
||
| 104 | @chmod($file, 0755); |
||
|
|
|||
| 105 | |||
| 106 | // Well, 755 hopefully worked... if not, try 777. |
||
| 107 | if (!is_writable($file) && !@chmod($file, 0777)) |
||
| 108 | $failure = true; |
||
| 109 | // Otherwise remove it as it's good! |
||
| 110 | else |
||
| 111 | unset($files[$k]); |
||
| 112 | } |
||
| 113 | else |
||
| 114 | unset($files[$k]); |
||
| 115 | } |
||
| 116 | } |
||
| 117 | // Windows is trickier. Let's try opening for r+... |
||
| 118 | else |
||
| 119 | { |
||
| 120 | $upcontext['systemos'] = 'windows'; |
||
| 121 | |||
| 122 | foreach ($files as $k => $file) |
||
| 123 | { |
||
| 124 | // Folders can't be opened for write... but the index.php in them can ;). |
||
| 125 | if (is_dir($file)) |
||
| 126 | $file .= '/index.php'; |
||
| 127 | |||
| 128 | // Funny enough, chmod actually does do something on windows - it removes the read only attribute. |
||
| 129 | @chmod($file, 0777); |
||
| 130 | $fp = @fopen($file, 'r+'); |
||
| 131 | |||
| 132 | // Hmm, okay, try just for write in that case... |
||
| 133 | if (!$fp) |
||
| 134 | $fp = @fopen($file, 'w'); |
||
| 135 | |||
| 136 | if (!$fp) |
||
| 137 | $failure = true; |
||
| 138 | else |
||
| 139 | unset($files[$k]); |
||
| 140 | @fclose($fp); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | if (empty($files)) |
||
| 145 | return true; |
||
| 146 | |||
| 147 | if (!isset($_SERVER)) |
||
| 148 | return !$failure; |
||
| 149 | |||
| 150 | // What still needs to be done? |
||
| 151 | $upcontext['chmod']['files'] = $files; |
||
| 152 | |||
| 153 | // If it's windows it's a mess... |
||
| 154 | if ($failure && substr(__FILE__, 1, 2) == ':\\') |
||
| 155 | { |
||
| 156 | $upcontext['chmod']['ftp_error'] = 'total_mess'; |
||
| 157 | |||
| 158 | return false; |
||
| 159 | } |
||
| 160 | // We're going to have to use... FTP! |
||
| 161 | elseif ($failure) |
||
| 162 | { |
||
| 163 | // Load any session data we might have... |
||
| 164 | if (!isset($_POST['ftp_username']) && isset($_SESSION['installer_temp_ftp'])) |
||
| 165 | { |
||
| 166 | $upcontext['chmod']['server'] = $_SESSION['installer_temp_ftp']['server']; |
||
| 167 | $upcontext['chmod']['port'] = $_SESSION['installer_temp_ftp']['port']; |
||
| 168 | $upcontext['chmod']['username'] = $_SESSION['installer_temp_ftp']['username']; |
||
| 169 | $upcontext['chmod']['password'] = $_SESSION['installer_temp_ftp']['password']; |
||
| 170 | $upcontext['chmod']['path'] = $_SESSION['installer_temp_ftp']['path']; |
||
| 171 | } |
||
| 172 | // Or have we submitted? |
||
| 173 | View Code Duplication | elseif (isset($_POST['ftp_username'])) |
|
| 174 | { |
||
| 175 | $upcontext['chmod']['server'] = $_POST['ftp_server']; |
||
| 176 | $upcontext['chmod']['port'] = $_POST['ftp_port']; |
||
| 177 | $upcontext['chmod']['username'] = $_POST['ftp_username']; |
||
| 178 | $upcontext['chmod']['password'] = $_POST['ftp_password']; |
||
| 179 | $upcontext['chmod']['path'] = $_POST['ftp_path']; |
||
| 180 | } |
||
| 181 | |||
| 182 | require_once($sourcedir . '/Class-Package.php'); |
||
| 183 | if (isset($upcontext['chmod']['username'])) |
||
| 184 | { |
||
| 185 | $ftp = new ftp_connection($upcontext['chmod']['server'], $upcontext['chmod']['port'], $upcontext['chmod']['username'], $upcontext['chmod']['password']); |
||
| 186 | |||
| 187 | if ($ftp->error === false) |
||
| 188 | { |
||
| 189 | // Try it without /home/abc just in case they messed up. |
||
| 190 | if (!$ftp->chdir($upcontext['chmod']['path'])) |
||
| 191 | { |
||
| 192 | $upcontext['chmod']['ftp_error'] = $ftp->last_message; |
||
| 193 | $ftp->chdir(preg_replace('~^/home[2]?/[^/]+?~', '', $upcontext['chmod']['path'])); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | if (!isset($ftp) || $ftp->error !== false) |
||
| 199 | { |
||
| 200 | if (!isset($ftp)) |
||
| 201 | $ftp = new ftp_connection(null); |
||
| 202 | // Save the error so we can mess with listing... |
||
| 203 | elseif ($ftp->error !== false && !isset($upcontext['chmod']['ftp_error'])) |
||
| 204 | $upcontext['chmod']['ftp_error'] = $ftp->last_message === null ? '' : $ftp->last_message; |
||
| 205 | |||
| 206 | list ($username, $detect_path, $found_path) = $ftp->detect_path(dirname(__FILE__)); |
||
| 207 | |||
| 208 | if ($found_path || !isset($upcontext['chmod']['path'])) |
||
| 209 | $upcontext['chmod']['path'] = $detect_path; |
||
| 210 | |||
| 211 | if (!isset($upcontext['chmod']['username'])) |
||
| 212 | $upcontext['chmod']['username'] = $username; |
||
| 213 | |||
| 214 | // Don't forget the login token. |
||
| 215 | $upcontext += createToken('login'); |
||
| 216 | |||
| 217 | return false; |
||
| 218 | } |
||
| 219 | else |
||
| 220 | { |
||
| 221 | // We want to do a relative path for FTP. |
||
| 222 | if (!in_array($upcontext['chmod']['path'], array('', '/'))) |
||
| 223 | { |
||
| 224 | $ftp_root = strtr($boarddir, array($upcontext['chmod']['path'] => '')); |
||
| 225 | if (substr($ftp_root, -1) == '/' && ($upcontext['chmod']['path'] == '' || $upcontext['chmod']['path'][0] === '/')) |
||
| 226 | $ftp_root = substr($ftp_root, 0, -1); |
||
| 227 | } |
||
| 228 | else |
||
| 229 | $ftp_root = $boarddir; |
||
| 230 | |||
| 231 | // Save the info for next time! |
||
| 232 | $_SESSION['installer_temp_ftp'] = array( |
||
| 233 | 'server' => $upcontext['chmod']['server'], |
||
| 234 | 'port' => $upcontext['chmod']['port'], |
||
| 235 | 'username' => $upcontext['chmod']['username'], |
||
| 236 | 'password' => $upcontext['chmod']['password'], |
||
| 237 | 'path' => $upcontext['chmod']['path'], |
||
| 238 | 'root' => $ftp_root, |
||
| 239 | ); |
||
| 240 | |||
| 241 | foreach ($files as $k => $file) |
||
| 242 | { |
||
| 243 | if (!is_writable($file)) |
||
| 244 | $ftp->chmod($file, 0755); |
||
| 245 | if (!is_writable($file)) |
||
| 246 | $ftp->chmod($file, 0777); |
||
| 247 | |||
| 248 | // Assuming that didn't work calculate the path without the boarddir. |
||
| 249 | if (!is_writable($file)) |
||
| 250 | { |
||
| 251 | if (strpos($file, $boarddir) === 0) |
||
| 252 | { |
||
| 253 | $ftp_file = strtr($file, array($_SESSION['installer_temp_ftp']['root'] => '')); |
||
| 254 | $ftp->chmod($ftp_file, 0755); |
||
| 255 | if (!is_writable($file)) |
||
| 256 | $ftp->chmod($ftp_file, 0777); |
||
| 257 | // Sometimes an extra slash can help... |
||
| 258 | $ftp_file = '/' . $ftp_file; |
||
| 259 | if (!is_writable($file)) |
||
| 260 | $ftp->chmod($ftp_file, 0755); |
||
| 261 | if (!is_writable($file)) |
||
| 262 | $ftp->chmod($ftp_file, 0777); |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | if (is_writable($file)) |
||
| 267 | unset($files[$k]); |
||
| 268 | } |
||
| 269 | |||
| 270 | $ftp->close(); |
||
| 271 | } |
||
| 272 | } |
||
| 273 | |||
| 274 | // What remains? |
||
| 275 | $upcontext['chmod']['files'] = $files; |
||
| 276 | |||
| 277 | if (empty($files)) |
||
| 278 | return true; |
||
| 279 | |||
| 280 | return false; |
||
| 281 | } |
||
| 282 | |||
| 411 | } |
If you suppress an error, we recommend checking for the error condition explicitly: