Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like PublisherUtils 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 PublisherUtils, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class PublisherUtils |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * Includes scripts in HTML header |
||
| 27 | * |
||
| 28 | * @return void |
||
| 29 | */ |
||
| 30 | public static function cpHeader() |
||
| 31 | { |
||
| 32 | $xoops = Xoops::getInstance(); |
||
| 33 | $publisher = Publisher::getInstance(); |
||
| 34 | $xoops->header(); |
||
| 35 | |||
| 36 | $css = array(); |
||
| 37 | $css[] = $publisher->path('css/publisher.css'); |
||
| 38 | $xoops->theme()->addBaseStylesheetAssets($css); |
||
| 39 | |||
| 40 | $js = array(); |
||
| 41 | $js[] = $publisher->path('js/funcs.js'); |
||
| 42 | $js[] = $publisher->path('js/cookies.js'); |
||
| 43 | $js[] = $publisher->path('js/ajaxupload.3.9.js'); |
||
| 44 | $js[] = $publisher->path('js/publisher.js'); |
||
| 45 | $xoops->theme()->addBaseScriptAssets($js); |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Default sorting for a given order |
||
| 50 | * |
||
| 51 | * @param string $sort |
||
| 52 | * |
||
| 53 | * @return string |
||
| 54 | */ |
||
| 55 | public static function getOrderBy($sort) |
||
| 56 | { |
||
| 57 | if (in_array($sort, array("datesub", "counter"))) { |
||
| 58 | return 'DESC'; |
||
| 59 | } |
||
| 60 | |||
| 61 | return 'ASC'; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @credits Thanks to Mithandir |
||
| 66 | * |
||
| 67 | * @param string $str |
||
| 68 | * @param int $start |
||
| 69 | * @param int $length |
||
| 70 | * @param string $trimmarker |
||
| 71 | * |
||
| 72 | * @return string |
||
| 73 | */ |
||
| 74 | public static function substr($str, $start, $length, $trimmarker = '...') |
||
| 75 | { |
||
| 76 | // if the string is empty, let's get out ;-) |
||
| 77 | if ($str == '') { |
||
| 78 | return $str; |
||
| 79 | } |
||
| 80 | |||
| 81 | // reverse a string that is shortened with '' as trimmarker |
||
| 82 | $reversed_string = strrev(XoopsLocale::substr($str, $start, $length, '')); |
||
| 83 | |||
| 84 | // find first space in reversed string |
||
| 85 | $position_of_space = strpos($reversed_string, " ", 0); |
||
| 86 | |||
| 87 | // truncate the original string to a length of $length |
||
| 88 | // minus the position of the last space |
||
| 89 | // plus the length of the $trimmarker |
||
| 90 | $truncated_string = XoopsLocale::substr($str, $start, $length - $position_of_space + strlen($trimmarker), $trimmarker); |
||
| 91 | |||
| 92 | return $truncated_string; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param string $document |
||
| 97 | * |
||
| 98 | * @return string |
||
| 99 | */ |
||
| 100 | View Code Duplication | public static function html2text($document) |
|
| 101 | { |
||
| 102 | // PHP Manual:: function preg_replace |
||
| 103 | // $document should contain an HTML document. |
||
| 104 | // This will remove HTML tags, javascript sections |
||
| 105 | // and white space. It will also convert some |
||
| 106 | // common HTML entities to their text equivalent. |
||
| 107 | // Credits : newbb2 |
||
| 108 | $search = array( |
||
| 109 | "'<script[^>]*?>.*?</script>'si", // Strip out javascript |
||
| 110 | "'<img.*?/>'si", // Strip out img tags |
||
| 111 | "'<[\/\!]*?[^<>]*?>'si", // Strip out HTML tags |
||
| 112 | "'([\r\n])[\s]+'", // Strip out white space |
||
| 113 | "'&(quot|#34);'i", // Replace HTML entities |
||
| 114 | "'&(amp|#38);'i", "'&(lt|#60);'i", "'&(gt|#62);'i", "'&(nbsp|#160);'i", "'&(iexcl|#161);'i", |
||
| 115 | "'&(cent|#162);'i", "'&(pound|#163);'i", "'&(copy|#169);'i", "'&#(\d+);'e" |
||
| 116 | ); // evaluate as php |
||
| 117 | |||
| 118 | $replace = array( |
||
| 119 | "", "", "", "\\1", "\"", "&", "<", ">", " ", chr(161), chr(162), chr(163), chr(169), "chr(\\1)" |
||
| 120 | ); |
||
| 121 | |||
| 122 | $text = preg_replace($search, $replace, $document); |
||
| 123 | |||
| 124 | return $text; |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @return string[] |
||
| 129 | */ |
||
| 130 | public static function getAllowedImagesTypes() |
||
| 131 | { |
||
| 132 | return array( |
||
| 133 | 'jpg/jpeg', 'image/bmp', 'image/gif', 'image/jpeg', 'image/jpg', 'image/x-png', 'image/png', 'image/pjpeg' |
||
| 134 | ); |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param bool $withLink |
||
| 139 | * |
||
| 140 | * @return string |
||
| 141 | */ |
||
| 142 | public static function moduleHome($withLink = true) |
||
| 143 | { |
||
| 144 | $xoops = Xoops::getInstance(); |
||
| 145 | $publisher = Publisher::getInstance(); |
||
| 146 | |||
| 147 | if (!$publisher->getConfig('format_breadcrumb_modname')) { |
||
| 148 | return ''; |
||
| 149 | } |
||
| 150 | |||
| 151 | if (!$withLink) { |
||
| 152 | return $publisher->getModule()->getVar('name'); |
||
| 153 | } else { |
||
| 154 | return '<a href="' . $xoops->url(PUBLISHER_URL) . '/">' . $publisher->getModule()->getVar('name') . '</a>'; |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Copy a file, or a folder and its contents |
||
| 160 | * |
||
| 161 | * @author Aidan Lister <[email protected]> |
||
| 162 | * @version 1.0.0 |
||
| 163 | * |
||
| 164 | * @param string $source The source |
||
| 165 | * @param string $dest The destination |
||
| 166 | * |
||
| 167 | * @return bool Returns true on success, false on failure |
||
| 168 | */ |
||
| 169 | public static function copyr($source, $dest) |
||
| 170 | { |
||
| 171 | // Simple copy for a file |
||
| 172 | if (is_file($source)) { |
||
| 173 | return copy($source, $dest); |
||
| 174 | } |
||
| 175 | |||
| 176 | // Make destination directory |
||
| 177 | if (!is_dir($dest)) { |
||
| 178 | mkdir($dest); |
||
| 179 | } |
||
| 180 | |||
| 181 | // Loop through the folder |
||
| 182 | $dir = dir($source); |
||
| 183 | while (false !== $entry = $dir->read()) { |
||
| 184 | // Skip pointers |
||
| 185 | if ($entry === '.' || $entry === '..') { |
||
| 186 | continue; |
||
| 187 | } |
||
| 188 | |||
| 189 | // Deep copy directories |
||
| 190 | if (is_dir("$source/$entry") && ($dest !== "$source/$entry")) { |
||
| 191 | self::copyr("$source/$entry", "$dest/$entry"); |
||
| 192 | } else { |
||
| 193 | copy("$source/$entry", "$dest/$entry"); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | // Clean up |
||
| 198 | $dir->close(); |
||
| 199 | |||
| 200 | return true; |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @credits Thanks to the NewBB2 Development Team |
||
| 205 | * @param string $item |
||
| 206 | * @param bool $getStatus |
||
| 207 | * |
||
| 208 | * @todo check undefined string |
||
| 209 | * @return bool|int|string |
||
| 210 | */ |
||
| 211 | public static function getPathStatus($item, $getStatus = false) |
||
| 212 | { |
||
| 213 | $publisher = Publisher::getInstance(); |
||
| 214 | if ($item === 'root') { |
||
| 215 | $path = ''; |
||
| 216 | } else { |
||
| 217 | $path = $item; |
||
| 218 | } |
||
| 219 | |||
| 220 | $thePath = self::getUploadDir(true, $path); |
||
| 221 | |||
| 222 | if (empty($thePath)) { |
||
| 223 | return false; |
||
| 224 | } |
||
| 225 | if (@is_writable($thePath)) { |
||
| 226 | $pathCheckResult = 1; |
||
| 227 | $path_status = _AM_PUBLISHER_AVAILABLE; |
||
| 228 | } elseif (!@is_dir($thePath)) { |
||
| 229 | $pathCheckResult = -1; |
||
| 230 | $path_status = _AM_PUBLISHER_NOTAVAILABLE . " <a href='" . $publisher->url("admin/index.php?op=createdir&path={$item}") . "'>" . _AM_PUBLISHER_CREATETHEDIR . "</a>"; |
||
| 231 | } else { |
||
| 232 | $pathCheckResult = -2; |
||
| 233 | $path_status = _AM_PUBLISHER_NOTWRITABLE . " <a href='" . $publisher->url("admin/index.php?op=setperm&path={$item}") . "'>" . _AM_SCS_SETMPERM . "</a>"; |
||
| 234 | } |
||
| 235 | if (!$getStatus) { |
||
| 236 | return $path_status; |
||
| 237 | } else { |
||
| 238 | return $pathCheckResult; |
||
| 239 | } |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @credits Thanks to the NewBB2 Development Team |
||
| 244 | * |
||
| 245 | * @param string $target |
||
| 246 | * |
||
| 247 | * @return bool |
||
| 248 | */ |
||
| 249 | public static function mkdir($target) |
||
| 250 | { |
||
| 251 | // http://www.php.net/manual/en/function.mkdir.php |
||
| 252 | // saint at corenova.com |
||
| 253 | // bart at cdasites dot com |
||
| 254 | if (is_dir($target) || empty($target)) { |
||
| 255 | return true; // best case check first |
||
| 256 | } |
||
| 257 | |||
| 258 | if (XoopsLoad::fileExists($target) && !is_dir($target)) { |
||
| 259 | return false; |
||
| 260 | } |
||
| 261 | |||
| 262 | if (self::mkdir(substr($target, 0, strrpos($target, '/')))) { |
||
| 263 | if (!XoopsLoad::fileExists($target)) { |
||
| 264 | $res = mkdir($target, 0777); // crawl back up & create dir tree |
||
| 265 | self::chmod($target); |
||
| 266 | |||
| 267 | return $res; |
||
| 268 | } |
||
| 269 | } |
||
| 270 | $res = is_dir($target); |
||
| 271 | |||
| 272 | return $res; |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @credits Thanks to the NewBB2 Development Team |
||
| 277 | * |
||
| 278 | * @param string $target |
||
| 279 | * @param int $mode |
||
| 280 | * |
||
| 281 | * @return bool |
||
| 282 | */ |
||
| 283 | public static function chmod($target, $mode = 0777) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @param bool $hasPath |
||
| 290 | * @param bool $item |
||
| 291 | * |
||
| 292 | * @return string |
||
| 293 | */ |
||
| 294 | public static function getUploadDir($hasPath = true, $item = false) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param string $item |
||
| 316 | * @param bool $hasPath |
||
| 317 | * |
||
| 318 | * @return string |
||
| 319 | */ |
||
| 320 | public static function getImageDir($item = '', $hasPath = true) |
||
| 321 | { |
||
| 322 | if ($item) { |
||
| 323 | $item = "images/{$item}"; |
||
| 324 | } else { |
||
| 325 | $item = "images"; |
||
| 326 | } |
||
| 327 | |||
| 328 | return self::getUploadDir($hasPath, $item); |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @param array $errors |
||
| 333 | * |
||
| 334 | * @return string |
||
| 335 | */ |
||
| 336 | public static function formatErrors($errors = array()) |
||
| 337 | { |
||
| 338 | $ret = ''; |
||
| 339 | foreach ($errors as $value) { |
||
| 340 | $ret .= '<br /> - ' . $value; |
||
| 341 | } |
||
| 342 | |||
| 343 | return $ret; |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Check is current user is author of a given article |
||
| 348 | * |
||
| 349 | * @param object $itemObj |
||
| 350 | * |
||
| 351 | * @return bool |
||
| 352 | */ |
||
| 353 | public static function IsUserAuthor($itemObj) |
||
| 354 | { |
||
| 355 | $xoops = Xoops::getInstance(); |
||
| 356 | |||
| 357 | return ($xoops->isUser() && is_object($itemObj) && ($xoops->user->getVar('uid') == $itemObj->getVar('uid'))); |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Check is current user is moderator of a given article |
||
| 362 | * |
||
| 363 | * @param PublisherItem $itemObj |
||
| 364 | * |
||
| 365 | * @return bool |
||
| 366 | */ |
||
| 367 | public static function IsUserModerator($itemObj) |
||
| 368 | { |
||
| 369 | $publisher = Publisher::getInstance(); |
||
| 370 | $categoriesGranted = $publisher->getPermissionHandler()->getGrantedItems('category_moderation'); |
||
| 371 | |||
| 372 | return (is_object($itemObj) && in_array($itemObj->getVar('categoryid'), $categoriesGranted)); |
||
| 373 | } |
||
| 374 | |||
| 375 | public static function IsUserAdmin() |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Saves permissions for the selected category |
||
| 382 | * |
||
| 383 | * @param array $groups : group with granted permission |
||
| 384 | * @param integer $categoryid : categoryid on which we are setting permissions |
||
| 385 | * @param string $perm_name : name of the permission |
||
| 386 | * |
||
| 387 | * @todo Move to category class |
||
| 388 | * @return boolean : TRUE if the no errors occured |
||
| 389 | */ |
||
| 390 | public static function saveCategoryPermissions($groups, $categoryid, $perm_name) |
||
| 391 | { |
||
| 392 | $xoops = Xoops::getInstance(); |
||
| 393 | $publisher = Publisher::getInstance(); |
||
| 394 | |||
| 395 | $result = true; |
||
| 396 | |||
| 397 | $module_id = $publisher->getModule()->getVar('mid'); |
||
| 398 | $gperm_handler = $xoops->getHandlerGroupPermission(); |
||
| 399 | // First, if the permissions are already there, delete them |
||
| 400 | $gperm_handler->deleteByModule($module_id, $perm_name, $categoryid); |
||
| 401 | |||
| 402 | // Save the new permissions |
||
| 403 | if (count($groups) > 0) { |
||
| 404 | foreach ($groups as $group_id) { |
||
| 405 | $gperm_handler->addRight($perm_name, $categoryid, $group_id, $module_id); |
||
| 406 | } |
||
| 407 | } |
||
| 408 | |||
| 409 | return $result; |
||
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @param string $tablename |
||
| 414 | * @param string $iconname |
||
| 415 | * @param string $tabletitle |
||
| 416 | * @param string $tabledsc |
||
| 417 | * @param bool $open |
||
| 418 | * |
||
| 419 | * @return void |
||
| 420 | */ |
||
| 421 | public static function openCollapsableBar($tablename = '', $iconname = '', $tabletitle = '', $tabledsc = '', $open = true) |
||
| 422 | { |
||
| 423 | $publisher = Publisher::getInstance(); |
||
| 424 | $image = 'open12.gif'; |
||
| 425 | $display = 'none'; |
||
| 426 | if ($open) { |
||
| 427 | $image = 'close12.gif'; |
||
| 428 | $display = 'block'; |
||
| 429 | } |
||
| 430 | |||
| 431 | echo "<h3 style=\"color: #2F5376; font-weight: bold; font-size: 14px; margin: 6px 0 0 0; \"><a href='javascript:;' onclick=\"toggle('" . $tablename . "'); toggleIcon('" . $iconname . "')\";>"; |
||
| 432 | echo "<img id='" . $iconname . "' src='" . $publisher->url("images/links/" . $image) . "' alt='' /></a> " . $tabletitle . "</h3>"; |
||
| 433 | echo "<div id='" . $tablename . "' style='display: " . $display . ";'>"; |
||
| 434 | if ($tabledsc != '') { |
||
| 435 | echo "<span style=\"color: #567; margin: 3px 0 12px 0; font-size: small; display: block; \">" . $tabledsc . "</span>"; |
||
| 436 | } |
||
| 437 | } |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @param string $name |
||
| 441 | * @param string $icon |
||
| 442 | * |
||
| 443 | * @return void |
||
| 444 | */ |
||
| 445 | public static function closeCollapsableBar($name, $icon) |
||
| 446 | { |
||
| 447 | echo "</div>"; |
||
| 448 | |||
| 449 | $urls = self::getCurrentUrls(); |
||
| 450 | $path = $urls['phpself']; |
||
| 451 | |||
| 452 | $cookie_name = $path . '_publisher_collaps_' . $name; |
||
| 453 | $cookie_name = str_replace('.', '_', $cookie_name); |
||
| 454 | $cookie = self::getCookieVar($cookie_name, ''); |
||
| 455 | |||
| 456 | if ($cookie === 'none') { |
||
| 457 | echo ' |
||
| 458 | <script type="text/javascript"><!-- |
||
| 459 | toggle("' . $name . '"); toggleIcon("' . $icon . '"); |
||
| 460 | //--> |
||
| 461 | </script> |
||
| 462 | '; |
||
| 463 | } |
||
| 464 | } |
||
| 465 | |||
| 466 | /** |
||
| 467 | * @param string $name |
||
| 468 | * @param string $value |
||
| 469 | * @param int $time |
||
| 470 | * |
||
| 471 | * @return void |
||
| 472 | */ |
||
| 473 | public static function setCookieVar($name, $value, $time = 0) |
||
| 474 | { |
||
| 475 | if ($time == 0) { |
||
| 476 | $time = time() + 3600 * 24 * 365; |
||
| 477 | } |
||
| 478 | setcookie($name, $value, $time, '/'); |
||
| 479 | } |
||
| 480 | |||
| 481 | /** |
||
| 482 | * @param string $name |
||
| 483 | * @param string $default |
||
| 484 | * |
||
| 485 | * @return string |
||
| 486 | */ |
||
| 487 | public static function getCookieVar($name, $default = '') |
||
|
|
|||
| 488 | { |
||
| 489 | if (isset($_COOKIE[$name]) && ($_COOKIE[$name] > '')) { |
||
| 490 | return $_COOKIE[$name]; |
||
| 491 | } else { |
||
| 492 | return $default; |
||
| 493 | } |
||
| 494 | } |
||
| 495 | |||
| 496 | /** |
||
| 497 | * @return array |
||
| 498 | */ |
||
| 499 | public static function getCurrentUrls() |
||
| 500 | { |
||
| 501 | $http = strpos(\XoopsBaseConfig::get('url'), "https://") === false ? "http://" : "https://"; |
||
| 502 | $phpself = $_SERVER['PHP_SELF']; |
||
| 503 | $httphost = $_SERVER['HTTP_HOST']; |
||
| 504 | $querystring = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : ''; |
||
| 505 | |||
| 506 | if ($querystring != '') { |
||
| 507 | $querystring = '?' . $querystring; |
||
| 508 | } |
||
| 509 | |||
| 510 | $currenturl = $http . $httphost . $phpself . $querystring; |
||
| 511 | |||
| 512 | $urls = array(); |
||
| 513 | $urls['http'] = $http; |
||
| 514 | $urls['httphost'] = $httphost; |
||
| 515 | $urls['phpself'] = $phpself; |
||
| 516 | $urls['querystring'] = $querystring; |
||
| 517 | $urls['full'] = $currenturl; |
||
| 518 | |||
| 519 | return $urls; |
||
| 520 | } |
||
| 521 | |||
| 522 | /** |
||
| 523 | * @return string |
||
| 524 | */ |
||
| 525 | public static function getCurrentPage() |
||
| 526 | { |
||
| 527 | $urls = self::getCurrentUrls(); |
||
| 528 | |||
| 529 | return $urls['full']; |
||
| 530 | } |
||
| 531 | |||
| 532 | /** |
||
| 533 | * @param object $categoryObj |
||
| 534 | * @param int $selectedid |
||
| 535 | * @param int $level |
||
| 536 | * @param string $ret |
||
| 537 | * |
||
| 538 | * @todo move to ccategory class |
||
| 539 | * @return string |
||
| 540 | */ |
||
| 541 | public static function addCategoryOption($categoryObj, $selectedid = 0, $level = 0, $ret = '') |
||
| 542 | { |
||
| 543 | $publisher = Publisher::getInstance(); |
||
| 544 | |||
| 545 | $spaces = ''; |
||
| 546 | for ($j = 0; $j < $level; ++$j) { |
||
| 547 | $spaces .= '--'; |
||
| 548 | } |
||
| 549 | |||
| 550 | $ret .= "<option value='" . $categoryObj->getVar('categoryid') . "'"; |
||
| 551 | if (is_array($selectedid) && in_array($categoryObj->getVar('categoryid'), $selectedid)) { |
||
| 552 | $ret .= " selected='selected'"; |
||
| 553 | } elseif ($categoryObj->getVar('categoryid') == $selectedid) { |
||
| 554 | $ret .= " selected='selected'"; |
||
| 555 | } |
||
| 556 | $ret .= ">" . $spaces . $categoryObj->getVar('name') . "</option>\n"; |
||
| 557 | |||
| 558 | $subCategoriesObj = $publisher->getCategoryHandler()->getCategories(0, 0, $categoryObj->getVar('categoryid')); |
||
| 559 | if (count($subCategoriesObj) > 0) { |
||
| 560 | ++$level; |
||
| 561 | foreach ($subCategoriesObj as $subCategoryObj) { |
||
| 562 | $ret .= self::addCategoryOption($subCategoryObj, $selectedid, $level); |
||
| 563 | } |
||
| 564 | } |
||
| 565 | |||
| 566 | return $ret; |
||
| 567 | } |
||
| 568 | |||
| 569 | /** |
||
| 570 | * @param int $selectedid |
||
| 571 | * @param int $parentcategory |
||
| 572 | * @param bool $allCatOption |
||
| 573 | * @param string $selectname |
||
| 574 | * |
||
| 575 | * @todo move to category class |
||
| 576 | * @return string |
||
| 577 | */ |
||
| 578 | public static function createCategorySelect($selectedid = 0, $parentcategory = 0, $allCatOption = true, $selectname = 'options[0]') |
||
| 579 | { |
||
| 580 | $publisher = Publisher::getInstance(); |
||
| 581 | |||
| 582 | $selectedid = explode(',', $selectedid); |
||
| 583 | |||
| 584 | $ret = "<select name='" . $selectname . "[]' multiple='multiple' size='10'>"; |
||
| 585 | if ($allCatOption) { |
||
| 586 | $ret .= "<option value='0'"; |
||
| 587 | if (in_array(0, $selectedid)) { |
||
| 588 | $ret .= " selected='selected'"; |
||
| 589 | } |
||
| 590 | $ret .= ">" . _MB_PUBLISHER_ALLCAT . "</option>"; |
||
| 591 | } |
||
| 592 | |||
| 593 | // Creating category objects |
||
| 594 | $categoriesObj = $publisher->getCategoryHandler()->getCategories(0, 0, $parentcategory); |
||
| 595 | |||
| 596 | if (count($categoriesObj) > 0) { |
||
| 597 | foreach ($categoriesObj as $catID => $categoryObj) { |
||
| 598 | $ret .= self::addCategoryOption($categoryObj, $selectedid); |
||
| 599 | } |
||
| 600 | } |
||
| 601 | $ret .= "</select>"; |
||
| 602 | |||
| 603 | return $ret; |
||
| 604 | } |
||
| 605 | |||
| 606 | /** |
||
| 607 | * @param int $selectedid |
||
| 608 | * @param int $parentcategory |
||
| 609 | * @param bool $allCatOption |
||
| 610 | * |
||
| 611 | * @todo move to category class |
||
| 612 | * @return string |
||
| 613 | */ |
||
| 614 | public static function createCategoryOptions($selectedid = 0, $parentcategory = 0, $allCatOption = true) |
||
| 634 | |||
| 635 | /** |
||
| 636 | * @param array $err_arr |
||
| 637 | * @param string $reseturl |
||
| 638 | * |
||
| 639 | * @todo check this undefined strings |
||
| 640 | * @return void |
||
| 641 | */ |
||
| 642 | public static function renderErrors(&$err_arr, $reseturl = '') |
||
| 643 | { |
||
| 644 | if (is_array($err_arr) && count($err_arr) > 0) { |
||
| 645 | echo '<div id="readOnly" class="errorMsg" style="border:1px solid #D24D00; background:#FEFECC url(' . PUBLISHER_URL . '/images/important-32.png) no-repeat 7px 50%;color:#333;padding-left:45px;">'; |
||
| 646 | |||
| 647 | echo '<h4 style="text-align:left;margin:0; padding-top:0">' . _AM_PUBLISHER_MSG_SUBMISSION_ERR; |
||
| 648 | |||
| 649 | if ($reseturl) { |
||
| 650 | echo ' <a href="' . $reseturl . '">[' . _AM_PUBLISHER_TEXT_SESSION_RESET . ']</a>'; |
||
| 651 | } |
||
| 652 | |||
| 653 | echo '</h4><ul>'; |
||
| 654 | |||
| 655 | foreach ($err_arr as $key => $error) { |
||
| 656 | if (is_array($error)) { |
||
| 657 | foreach ($error as $err) { |
||
| 658 | echo '<li><a href="#' . $key . '" onclick="var e = xoopsGetElementById(\'' . $key . '\'); e.focus();">' . htmlspecialchars($err) . '</a></li>'; |
||
| 659 | } |
||
| 660 | } else { |
||
| 661 | echo '<li><a href="#' . $key . '" onclick="var e = xoopsGetElementById(\'' . $key . '\'); e.focus();">' . htmlspecialchars($error) . '</a></li>'; |
||
| 662 | } |
||
| 663 | } |
||
| 664 | echo "</ul></div><br />"; |
||
| 665 | } |
||
| 666 | } |
||
| 667 | |||
| 668 | /** |
||
| 669 | * Generate publisher URL |
||
| 670 | * |
||
| 671 | * @param string $page |
||
| 672 | * @param array $vars |
||
| 673 | * @param bool $encodeAmp |
||
| 674 | * |
||
| 675 | * @return string |
||
| 676 | * @credit : xHelp module, developped by 3Dev |
||
| 677 | */ |
||
| 678 | public static function makeURI($page, $vars = array(), $encodeAmp = true) |
||
| 696 | |||
| 697 | /** |
||
| 698 | * @param string $subject |
||
| 699 | * |
||
| 700 | * @return string |
||
| 701 | */ |
||
| 702 | public static function tellafriend($subject = '') |
||
| 703 | { |
||
| 704 | $xoops = Xoops::getInstance(); |
||
| 705 | if (stristr($subject, '%')) { |
||
| 706 | $subject = rawurldecode($subject); |
||
| 707 | } |
||
| 708 | $target_uri = $xoops->url($_SERVER['REQUEST_URI']); |
||
| 709 | |||
| 710 | return $xoops->url('modules/tellafriend/index.php?target_uri=' . rawurlencode($target_uri) . '&subject=' . rawurlencode($subject)); |
||
| 711 | } |
||
| 712 | |||
| 713 | /** |
||
| 714 | * @param bool $another |
||
| 715 | * @param bool $withRedirect |
||
| 716 | * @param $itemObj |
||
| 717 | * |
||
| 718 | * @return bool|string |
||
| 719 | */ |
||
| 720 | public static function uploadFile($another = false, $withRedirect = true, &$itemObj) |
||
| 721 | { |
||
| 722 | $xoops = Xoops::getInstance(); |
||
| 723 | |||
| 724 | $publisher = Publisher::getInstance(); |
||
| 725 | |||
| 726 | $itemid = isset($_POST['itemid']) ? (int)($_POST['itemid']) : 0; |
||
| 727 | $uid = $xoops->isUser() ? $xoops->user->getVar('uid') : 0; |
||
| 728 | $session = new Session(); |
||
| 729 | $session->set('publisher_file_filename', isset($_POST['item_file_name']) ? $_POST['item_file_name'] : ''); |
||
| 730 | $session->set('publisher_file_description', isset($_POST['item_file_description']) ? $_POST['item_file_description'] : ''); |
||
| 731 | $session->set('publisher_file_status', isset($_POST['item_file_status']) ? (int)($_POST['item_file_status']) : 1); |
||
| 732 | $session->set('publisher_file_uid', $uid); |
||
| 733 | $session->set('publisher_file_itemid', $itemid); |
||
| 734 | |||
| 735 | if (!is_object($itemObj)) { |
||
| 736 | $itemObj = $publisher->getItemHandler()->get($itemid); |
||
| 737 | } |
||
| 738 | |||
| 739 | $fileObj = $publisher->getFileHandler()->create(); |
||
| 740 | $fileObj->setVar('name', isset($_POST['item_file_name']) ? $_POST['item_file_name'] : ''); |
||
| 741 | $fileObj->setVar('description', isset($_POST['item_file_description']) ? $_POST['item_file_description'] : ''); |
||
| 742 | $fileObj->setVar('status', isset($_POST['item_file_status']) ? (int)($_POST['item_file_status']) : 1); |
||
| 743 | $fileObj->setVar('uid', $uid); |
||
| 744 | $fileObj->setVar('itemid', $itemObj->getVar('itemid')); |
||
| 745 | $fileObj->setVar('datesub', time()); |
||
| 746 | |||
| 747 | // Get available mimetypes for file uploading |
||
| 748 | $allowed_mimetypes = $publisher->getMimetypeHandler()->getArrayByType(); |
||
| 749 | // TODO : display the available mimetypes to the user |
||
| 750 | $errors = array(); |
||
| 751 | /* @var $fileObj PublisherFile */ |
||
| 752 | if ($publisher->getConfig('perm_upload') && is_uploaded_file($_FILES['item_upload_file']['tmp_name'])) { |
||
| 753 | if (!$ret = $fileObj->checkUpload('item_upload_file', $allowed_mimetypes, $errors)) { |
||
| 754 | $errorstxt = implode('<br />', $errors); |
||
| 755 | |||
| 756 | $message = sprintf(_CO_PUBLISHER_MESSAGE_FILE_ERROR, $errorstxt); |
||
| 757 | if ($withRedirect) { |
||
| 758 | $xoops->redirect("file.php?op=mod&itemid=" . $itemid, 5, $message); |
||
| 759 | } else { |
||
| 760 | return $message; |
||
| 761 | } |
||
| 762 | } |
||
| 763 | } |
||
| 764 | |||
| 765 | // Storing the file |
||
| 766 | if (!$fileObj->store($allowed_mimetypes)) { |
||
| 767 | if ($withRedirect) { |
||
| 768 | $xoops->redirect("file.php?op=mod&itemid=" . $fileObj->getVar('itemid'), 3, _CO_PUBLISHER_FILEUPLOAD_ERROR . self::formatErrors($fileObj->getErrors())); |
||
| 769 | } else { |
||
| 770 | return _CO_PUBLISHER_FILEUPLOAD_ERROR . self::formatErrors($fileObj->getErrors()); |
||
| 771 | } |
||
| 772 | } |
||
| 773 | |||
| 774 | if ($withRedirect) { |
||
| 775 | $redirect_page = $another ? 'file.php' : 'item.php'; |
||
| 776 | $xoops->redirect($redirect_page . "?op=mod&itemid=" . $fileObj->getVar('itemid'), 2, _CO_PUBLISHER_FILEUPLOAD_SUCCESS); |
||
| 777 | } |
||
| 778 | |||
| 779 | return true; |
||
| 780 | } |
||
| 781 | |||
| 782 | /** |
||
| 783 | * @return string |
||
| 784 | */ |
||
| 785 | public static function newFeatureTag() |
||
| 786 | { |
||
| 787 | $ret = '<span style="padding-right: 4px; font-weight: bold; color: red;">' . _CO_PUBLISHER_NEW_FEATURE . '</span>'; |
||
| 788 | |||
| 789 | return $ret; |
||
| 790 | } |
||
| 791 | |||
| 792 | /** |
||
| 793 | * Smarty truncate_tagsafe modifier plugin |
||
| 794 | * Type: modifier<br> |
||
| 795 | * Name: truncate_tagsafe<br> |
||
| 796 | * Purpose: Truncate a string to a certain length if necessary, |
||
| 797 | * optionally splitting in the middle of a word, and |
||
| 798 | * appending the $etc string or inserting $etc into the middle. |
||
| 799 | * Makes sure no tags are left half-open or half-closed |
||
| 800 | * (e.g. "Banana in a <a...") |
||
| 801 | * |
||
| 802 | * @author Monte Ohrt <monte at ohrt dot com>, modified by Amos Robinson |
||
| 803 | * <amos dot robinson at gmail dot com> |
||
| 804 | * |
||
| 805 | * @param string |
||
| 806 | * @param integer |
||
| 807 | * @param string |
||
| 808 | * @param boolean |
||
| 809 | * @param boolean |
||
| 810 | * |
||
| 811 | * @return string |
||
| 812 | */ |
||
| 813 | public static function truncateTagSafe($string, $length = 80, $etc = '...', $break_words = false) |
||
| 832 | |||
| 833 | /** |
||
| 834 | * @author Monte Ohrt <monte at ohrt dot com>, modified by Amos Robinson |
||
| 835 | * <amos dot robinson at gmail dot com> |
||
| 836 | * |
||
| 837 | * @param string $string |
||
| 838 | * |
||
| 839 | * @return string |
||
| 840 | */ |
||
| 841 | public static function closeTags($string) |
||
| 842 | { |
||
| 843 | // match opened tags |
||
| 844 | if (preg_match_all('/<([a-z\:\-]+)[^\/]>/', $string, $start_tags)) { |
||
| 845 | $start_tags = $start_tags[1]; |
||
| 846 | // match closed tags |
||
| 847 | if (preg_match_all('/<\/([a-z]+)>/', $string, $end_tags)) { |
||
| 848 | $complete_tags = array(); |
||
| 849 | $end_tags = $end_tags[1]; |
||
| 850 | |||
| 851 | foreach ($start_tags as $val) { |
||
| 852 | $posb = array_search($val, $end_tags); |
||
| 853 | if (is_integer($posb)) { |
||
| 854 | unset($end_tags[$posb]); |
||
| 855 | } else { |
||
| 856 | $complete_tags[] = $val; |
||
| 857 | } |
||
| 858 | } |
||
| 871 | |||
| 872 | /** |
||
| 873 | * @param int $itemid |
||
| 874 | * |
||
| 875 | * @return string |
||
| 876 | */ |
||
| 877 | public static function ratingBar($itemid) |
||
| 952 | |||
| 953 | /** |
||
| 954 | * @param array $allowed_editors |
||
| 955 | * |
||
| 956 | * @return array |
||
| 957 | */ |
||
| 958 | public static function getEditors($allowed_editors = null) |
||
| 980 | |||
| 981 | /** |
||
| 982 | * @param string $string |
||
| 983 | * @param int $length |
||
| 984 | * |
||
| 985 | * @return int |
||
| 986 | */ |
||
| 987 | public static function stringToInt($string = '', $length = 5) |
||
| 994 | |||
| 995 | /** |
||
| 996 | * @param string $item |
||
| 997 | * |
||
| 998 | * @return string |
||
| 999 | */ |
||
| 1000 | public static function convertCharset($item) |
||
| 1021 | |||
| 1022 | public static function seoTitle($title = '', $withExt = true) |
||
| 1069 | |||
| 1070 | /** |
||
| 1071 | * seoGenUrl |
||
| 1072 | * |
||
| 1073 | * @param string $op |
||
| 1074 | * @param integer $id |
||
| 1075 | * @param string $short_url |
||
| 1076 | * |
||
| 1077 | * @return string |
||
| 1078 | */ |
||
| 1079 | public static function seoGenUrl($op, $id, $short_url = "") |
||
| 1111 | |||
| 1112 | /** |
||
| 1113 | * @param string $url |
||
| 1114 | * @param int $width |
||
| 1115 | * @param int $height |
||
| 1116 | * |
||
| 1117 | * @return string |
||
| 1118 | */ |
||
| 1119 | public static function displayFlash($url, $width = 0, $height = 0) |
||
| 1144 | } |
||
| 1145 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: