| Total Complexity | 62 |
| Total Lines | 379 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Files often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Files, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class Files |
||
| 29 | { |
||
| 30 | public $db; |
||
| 31 | public $table; |
||
| 32 | public $fileid; |
||
| 33 | public $filerealname; |
||
| 34 | public $storyid; |
||
| 35 | public $date; |
||
| 36 | public $mimetype; |
||
| 37 | public $downloadname; |
||
| 38 | public $counter; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param $fileid |
||
| 42 | */ |
||
| 43 | public function __construct($fileid = -1) |
||
| 44 | { |
||
| 45 | /** @var \XoopsMySQLDatabase $db */ |
||
| 46 | $this->db = \XoopsDatabaseFactory::getDatabaseConnection(); |
||
| 47 | $this->table = $this->db->prefix('news_stories_files'); |
||
| 48 | $this->storyid = 0; |
||
| 49 | $this->filerealname = ''; |
||
| 50 | $this->date = 0; |
||
| 51 | $this->mimetype = ''; |
||
| 52 | $this->downloadname = 'downloadfile'; |
||
| 53 | $this->counter = 0; |
||
| 54 | if (\is_array($fileid)) { |
||
| 55 | $this->makeFile($fileid); |
||
| 56 | } elseif (-1 != $fileid) { |
||
| 57 | $this->getFile((int)$fileid); |
||
| 58 | } |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param $folder |
||
| 63 | * @param $filename |
||
| 64 | * @param bool $trimname |
||
| 65 | * |
||
| 66 | * @return string |
||
| 67 | */ |
||
| 68 | public function createUploadName($folder, $filename, $trimname = false) |
||
| 69 | { |
||
| 70 | $workingfolder = $folder; |
||
| 71 | if ('/' !== \xoops_substr($workingfolder, mb_strlen($workingfolder) - 1, 1)) { |
||
| 72 | $workingfolder .= '/'; |
||
| 73 | } |
||
| 74 | $ext = \basename($filename); |
||
| 75 | $ext = \explode('.', $ext); |
||
| 76 | $ext = '.' . $ext[\count($ext) - 1]; |
||
| 77 | $true = true; |
||
| 78 | while ($true) { |
||
| 79 | $ipbits = \explode('.', $_SERVER['REMOTE_ADDR']); |
||
| 80 | [$usec, $sec] = \explode(' ', \microtime()); |
||
| 81 | |||
| 82 | $usec *= 65536; |
||
| 83 | $sec = ((int)$sec) & 0xFFFF; |
||
| 84 | |||
| 85 | if ($trimname) { |
||
| 86 | $uid = \sprintf('%06x%04x%04x', ($ipbits[0] << 24) | ($ipbits[1] << 16) | ($ipbits[2] << 8) | $ipbits[3], $sec, $usec); |
||
| 87 | } else { |
||
| 88 | $uid = \sprintf('%08x-%04x-%04x', ($ipbits[0] << 24) | ($ipbits[1] << 16) | ($ipbits[2] << 8) | $ipbits[3], $sec, $usec); |
||
| 89 | } |
||
| 90 | if (!\file_exists($workingfolder . $uid . $ext)) { |
||
| 91 | $true = false; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | return $uid . $ext; |
||
|
|
|||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param string $filename |
||
| 100 | * |
||
| 101 | * @return string |
||
| 102 | */ |
||
| 103 | public function giveMimetype($filename = '') |
||
| 104 | { |
||
| 105 | $cmimetype = new Mimetype(); |
||
| 106 | $workingfile = $this->downloadname; |
||
| 107 | if ('' !== \xoops_trim($filename)) { |
||
| 108 | $workingfile = $filename; |
||
| 109 | |||
| 110 | return $cmimetype->getType($workingfile); |
||
| 111 | } |
||
| 112 | |||
| 113 | return ''; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param $storyid |
||
| 118 | * |
||
| 119 | * @return array |
||
| 120 | */ |
||
| 121 | public function getAllbyStory($storyid) |
||
| 122 | { |
||
| 123 | $ret = []; |
||
| 124 | $sql = 'SELECT * FROM ' . $this->table . ' WHERE storyid=' . (int)$storyid; |
||
| 125 | $result = $this->db->query($sql); |
||
| 126 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 127 | $ret[] = new self($myrow); |
||
| 128 | } |
||
| 129 | |||
| 130 | return $ret; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param $id |
||
| 135 | */ |
||
| 136 | public function getFile($id) |
||
| 137 | { |
||
| 138 | $sql = 'SELECT * FROM ' . $this->table . ' WHERE fileid=' . (int)$id; |
||
| 139 | $array = $this->db->fetchArray($this->db->query($sql)); |
||
| 140 | $this->makeFile($array); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param $array |
||
| 145 | */ |
||
| 146 | public function makeFile($array) |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @return bool |
||
| 155 | */ |
||
| 156 | public function store() |
||
| 157 | { |
||
| 158 | $myts = \MyTextSanitizer::getInstance(); |
||
| 159 | $fileRealName = $myts->addSlashes($this->filerealname); |
||
| 160 | $downloadname = $myts->addSlashes($this->downloadname); |
||
| 161 | $date = \time(); |
||
| 162 | $mimetype = $myts->addSlashes($this->mimetype); |
||
| 163 | $counter = (int)$this->counter; |
||
| 164 | $storyid = (int)$this->storyid; |
||
| 165 | |||
| 166 | if (!isset($this->fileid)) { |
||
| 167 | $newid = (int)$this->db->genId($this->table . '_fileid_seq'); |
||
| 168 | $sql = 'INSERT INTO ' . $this->table . ' (fileid, storyid, filerealname, date, mimetype, downloadname, counter) ' . 'VALUES (' . $newid . ',' . $storyid . ",'" . $fileRealName . "','" . $date . "','" . $mimetype . "','" . $downloadname . "'," . $counter . ')'; |
||
| 169 | $this->fileid = $newid; |
||
| 170 | } else { |
||
| 171 | $sql = 'UPDATE ' . $this->table . ' SET storyid=' . $storyid . ",filerealname='" . $fileRealName . "',date=" . $date . ",mimetype='" . $mimetype . "',downloadname='" . $downloadname . "',counter=" . $counter . ' WHERE fileid=' . $this->getFileid(); |
||
| 172 | } |
||
| 173 | if (!$result = $this->db->query($sql)) { |
||
| 174 | return false; |
||
| 175 | } |
||
| 176 | |||
| 177 | return true; |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param string $workdir |
||
| 182 | * |
||
| 183 | * @return bool |
||
| 184 | */ |
||
| 185 | public function delete($workdir = XOOPS_UPLOAD_PATH) |
||
| 186 | { |
||
| 187 | $sql = 'DELETE FROM ' . $this->table . ' WHERE fileid=' . $this->getFileid(); |
||
| 188 | if (!$result = $this->db->query($sql)) { |
||
| 189 | return false; |
||
| 190 | } |
||
| 191 | if (\is_file($workdir . '/' . $this->downloadname)) { |
||
| 192 | \unlink($workdir . '/' . $this->downloadname); |
||
| 193 | } |
||
| 194 | |||
| 195 | return true; |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @return bool |
||
| 200 | */ |
||
| 201 | public function updateCounter() |
||
| 202 | { |
||
| 203 | $sql = 'UPDATE ' . $this->table . ' SET counter=counter+1 WHERE fileid=' . $this->getFileid(); |
||
| 204 | if (!$result = $this->db->queryF($sql)) { |
||
| 205 | return false; |
||
| 206 | } |
||
| 207 | |||
| 208 | return true; |
||
| 209 | } |
||
| 210 | |||
| 211 | // **************************************************************************************************************** |
||
| 212 | // All the Sets |
||
| 213 | // **************************************************************************************************************** |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @param $filename |
||
| 217 | */ |
||
| 218 | public function setFileRealName($filename) |
||
| 219 | { |
||
| 220 | $this->filerealname = $filename; |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param $id |
||
| 225 | */ |
||
| 226 | public function setStoryid($id) |
||
| 227 | { |
||
| 228 | $this->storyid = (int)$id; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @param $value |
||
| 233 | */ |
||
| 234 | public function setMimetype($value) |
||
| 235 | { |
||
| 236 | $this->mimetype = $value; |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @param $value |
||
| 241 | */ |
||
| 242 | public function setDownloadname($value) |
||
| 243 | { |
||
| 244 | $this->downloadname = $value; |
||
| 245 | } |
||
| 246 | |||
| 247 | // **************************************************************************************************************** |
||
| 248 | // All the Gets |
||
| 249 | // **************************************************************************************************************** |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @return int |
||
| 253 | */ |
||
| 254 | public function getFileid() |
||
| 255 | { |
||
| 256 | return (int)$this->fileid; |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @return int |
||
| 261 | */ |
||
| 262 | public function getStoryid() |
||
| 263 | { |
||
| 264 | return (int)$this->storyid; |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @return int |
||
| 269 | */ |
||
| 270 | public function getCounter() |
||
| 271 | { |
||
| 272 | return (int)$this->counter; |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @return int |
||
| 277 | */ |
||
| 278 | public function getDate() |
||
| 279 | { |
||
| 280 | return (int)$this->date; |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @param string $format |
||
| 285 | * |
||
| 286 | * @return mixed |
||
| 287 | */ |
||
| 288 | public function getFileRealName($format = 'S') |
||
| 289 | { |
||
| 290 | $myts = \MyTextSanitizer::getInstance(); |
||
| 291 | switch ($format) { |
||
| 292 | case 'S': |
||
| 293 | case 'Show': |
||
| 294 | $filerealname = htmlspecialchars($this->filerealname, ENT_QUOTES | ENT_HTML5); |
||
| 295 | break; |
||
| 296 | case 'E': |
||
| 297 | case 'Edit': |
||
| 298 | $filerealname = htmlspecialchars($this->filerealname, ENT_QUOTES | ENT_HTML5); |
||
| 299 | break; |
||
| 300 | case 'P': |
||
| 301 | case 'Preview': |
||
| 302 | $filerealname = htmlspecialchars($this->filerealname, ENT_QUOTES | ENT_HTML5); |
||
| 303 | break; |
||
| 304 | case 'F': |
||
| 305 | case 'InForm': |
||
| 306 | $filerealname = htmlspecialchars($this->filerealname, ENT_QUOTES | ENT_HTML5); |
||
| 307 | break; |
||
| 308 | } |
||
| 309 | |||
| 310 | return $filerealname; |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @param string $format |
||
| 315 | * |
||
| 316 | * @return mixed |
||
| 317 | */ |
||
| 318 | public function getMimetype($format = 'S') |
||
| 319 | { |
||
| 320 | $myts = \MyTextSanitizer::getInstance(); |
||
| 321 | switch ($format) { |
||
| 322 | case 'S': |
||
| 323 | case 'Show': |
||
| 324 | $filemimetype = htmlspecialchars($this->mimetype, ENT_QUOTES | ENT_HTML5); |
||
| 325 | break; |
||
| 326 | case 'E': |
||
| 327 | case 'Edit': |
||
| 328 | $filemimetype = htmlspecialchars($this->mimetype, ENT_QUOTES | ENT_HTML5); |
||
| 329 | break; |
||
| 330 | case 'P': |
||
| 331 | case 'Preview': |
||
| 332 | $filemimetype = htmlspecialchars($this->mimetype, ENT_QUOTES | ENT_HTML5); |
||
| 333 | break; |
||
| 334 | case 'F': |
||
| 335 | case 'InForm': |
||
| 336 | $filemimetype = htmlspecialchars($this->mimetype, ENT_QUOTES | ENT_HTML5); |
||
| 337 | break; |
||
| 338 | } |
||
| 339 | |||
| 340 | return $filemimetype; |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @param string $format |
||
| 345 | * |
||
| 346 | * @return mixed |
||
| 347 | */ |
||
| 348 | public function getDownloadname($format = 'S') |
||
| 349 | { |
||
| 350 | $myts = \MyTextSanitizer::getInstance(); |
||
| 351 | switch ($format) { |
||
| 352 | case 'S': |
||
| 353 | case 'Show': |
||
| 354 | $filedownname = htmlspecialchars($this->downloadname, ENT_QUOTES | ENT_HTML5); |
||
| 355 | break; |
||
| 356 | case 'E': |
||
| 357 | case 'Edit': |
||
| 358 | $filedownname = htmlspecialchars($this->downloadname, ENT_QUOTES | ENT_HTML5); |
||
| 359 | break; |
||
| 360 | case 'P': |
||
| 361 | case 'Preview': |
||
| 362 | $filedownname = htmlspecialchars($this->downloadname, ENT_QUOTES | ENT_HTML5); |
||
| 363 | break; |
||
| 364 | case 'F': |
||
| 365 | case 'InForm': |
||
| 366 | $filedownname = htmlspecialchars($this->downloadname, ENT_QUOTES | ENT_HTML5); |
||
| 367 | break; |
||
| 368 | } |
||
| 369 | |||
| 370 | return $filedownname; |
||
| 371 | } |
||
| 372 | |||
| 373 | // Deprecated |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @param $storyid |
||
| 377 | * |
||
| 378 | * @return mixed |
||
| 379 | */ |
||
| 380 | public function getCountbyStory($storyid) |
||
| 381 | { |
||
| 382 | $sql = 'SELECT count(fileid) AS cnt FROM ' . $this->table . ' WHERE storyid=' . (int)$storyid . ''; |
||
| 383 | $result = $this->db->query($sql); |
||
| 384 | $myrow = $this->db->fetchArray($result); |
||
| 385 | |||
| 386 | return $myrow['cnt']; |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @param $stories |
||
| 391 | * |
||
| 392 | * @return array |
||
| 393 | */ |
||
| 394 | public function getCountbyStories($stories) |
||
| 407 | } |
||
| 408 | } |
||
| 409 |