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