| Total Complexity | 305 |
| Total Lines | 2117 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Utility 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 Utility, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class Utility extends Common\SysUtility |
||
| 34 | { |
||
| 35 | //--------------- Custom module methods ----------------------------- |
||
| 36 | /** |
||
| 37 | * @param $categoryObj |
||
| 38 | * @param int $level |
||
| 39 | */ |
||
| 40 | public static function displayCategory(Wfdownloads\Category $categoryObj, $level = 0) |
||
| 41 | { |
||
| 42 | $helper = Helper::getInstance(); |
||
| 43 | |||
| 44 | $description = $categoryObj->description(); |
||
|
|
|||
| 45 | if (!XOOPS_USE_MULTIBYTES) { |
||
| 46 | if (mb_strlen($description) >= 100) { |
||
| 47 | $description = mb_substr($description, 0, 100 - 1) . '...'; |
||
| 48 | } |
||
| 49 | } |
||
| 50 | $modify = "<a href='category.php?op=mod&categoryid=" . $categoryObj->categoryid() . '&parentid=' . $categoryObj->parentid() . "'><img src='" . PUBLISHER_URL . "/assets/images/links/edit.gif' title='" . \_AM_PUBLISHER_EDITCOL . "' alt='" . \_AM_PUBLISHER_EDITCOL . "'></a>"; |
||
| 51 | $delete = "<a href='category.php?op=del&categoryid=" . $categoryObj->categoryid() . "'><img src='" . PUBLISHER_URL . "/assets/images/links/delete.png' title='" . \_AM_PUBLISHER_DELETECOL . "' alt='" . \_AM_PUBLISHER_DELETECOL . "'></a>"; |
||
| 52 | |||
| 53 | $spaces = ''; |
||
| 54 | for ($j = 0; $j < $level; ++$j) { |
||
| 55 | $spaces .= ' '; |
||
| 56 | } |
||
| 57 | |||
| 58 | echo '<tr>'; |
||
| 59 | echo "<td class='even' align='center'>" . $categoryObj->categoryid() . '</td>'; |
||
| 60 | echo "<td class='even' align='left'>" . $spaces . "<a href='" . PUBLISHER_URL . '/category.php?categoryid=' . $categoryObj->categoryid() . "'><img src='" . PUBLISHER_URL . "/assets/images/links/subcat.gif' alt=''> " . $categoryObj->name() . '</a></td>'; |
||
| 61 | echo "<td class='even' align='center'>" . $categoryObj->weight() . '</td>'; |
||
| 62 | echo "<td class='even' align='center'> $modify $delete </td>"; |
||
| 63 | echo '</tr>'; |
||
| 64 | $subCategoriesObj = $helper->getHandler('Category')->getCategories(0, 0, $categoryObj->categoryid()); |
||
| 65 | if (\count($subCategoriesObj) > 0) { |
||
| 66 | ++$level; |
||
| 67 | foreach ($subCategoriesObj as $key => $thiscat) { |
||
| 68 | self::displayCategory($thiscat, $level); |
||
| 69 | } |
||
| 70 | unset($key, $thiscat); |
||
| 71 | } |
||
| 72 | // unset($categoryObj); |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param bool $showmenu |
||
| 77 | * @param int $categoryId |
||
| 78 | * @param int $nbSubCats |
||
| 79 | * @param null $categoryObj |
||
| 80 | */ |
||
| 81 | public static function editCategory($showmenu = false, $categoryId = 0, $nbSubCats = 4, $categoryObj = null) |
||
| 82 | { |
||
| 83 | $helper = Helper::getInstance(); |
||
| 84 | |||
| 85 | // if there is a parameter, and the id exists, retrieve data: we're editing a category |
||
| 86 | if (0 != $categoryId) { |
||
| 87 | // Creating the category object for the selected category |
||
| 88 | $categoryObj = $helper->getHandler('Category')->get($categoryId); |
||
| 89 | if ($categoryObj->notLoaded()) { |
||
| 90 | \redirect_header('category.php', 1, \_AM_PUBLISHER_NOCOLTOEDIT); |
||
| 91 | // exit(); |
||
| 92 | } |
||
| 93 | } else { |
||
| 94 | if (!$categoryObj) { |
||
| 95 | $categoryObj = $helper->getHandler('Category')->create(); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | if (0 != $categoryId) { |
||
| 100 | echo "<br>\n"; |
||
| 101 | publisherOpenCollapsableBar('edittable', 'edittableicon', \_AM_PUBLISHER_EDITCOL, \_AM_PUBLISHER_CATEGORY_EDIT_INFO); |
||
| 102 | } else { |
||
| 103 | publisherOpenCollapsableBar('createtable', 'createtableicon', \_AM_PUBLISHER_CATEGORY_CREATE, \_AM_PUBLISHER_CATEGORY_CREATE_INFO); |
||
| 104 | } |
||
| 105 | |||
| 106 | $sform = $categoryObj->getForm($nbSubCats); |
||
| 107 | $sform->display(); |
||
| 108 | |||
| 109 | if (!$categoryId) { |
||
| 110 | publisherCloseCollapsableBar('createtable', 'createtableicon'); |
||
| 111 | } else { |
||
| 112 | publisherCloseCollapsableBar('edittable', 'edittableicon'); |
||
| 113 | } |
||
| 114 | |||
| 115 | //Added by fx2024 |
||
| 116 | if ($categoryId) { |
||
| 117 | $selCat = $categoryId; |
||
| 118 | |||
| 119 | publisherOpenCollapsableBar('subcatstable', 'subcatsicon', \_AM_PUBLISHER_SUBCAT_CAT, \_AM_PUBLISHER_SUBCAT_CAT_DSC); |
||
| 120 | // Get the total number of sub-categories |
||
| 121 | $categoriesObj = $helper->getHandler('Category')->get($selCat); |
||
| 122 | $totalsubs = $helper->getHandler('Category')->getCategoriesCount($selCat); |
||
| 123 | // creating the categories objects that are published |
||
| 124 | $subcatsObj = $helper->getHandler('Category')->getCategories(0, 0, $categoriesObj->categoryid()); |
||
| 125 | $totalSCOnPage = \count($subcatsObj); |
||
| 126 | echo "<table width='100%' cellspacing=1 cellpadding=3 border=0 class = outer>"; |
||
| 127 | echo '<tr>'; |
||
| 128 | echo "<td width='60' class='bg3' align='left'><strong>" . \_AM_PUBLISHER_CATID . '</strong></td>'; |
||
| 129 | echo "<td width='20%' class='bg3' align='left'><strong>" . \_AM_PUBLISHER_CATCOLNAME . '</strong></td>'; |
||
| 130 | echo "<td class='bg3' align='left'><strong>" . \_AM_PUBLISHER_SUBDESCRIPT . '</strong></td>'; |
||
| 131 | echo "<td width='60' class='bg3' align='right'><strong>" . \_AM_PUBLISHER_ACTION . '</strong></td>'; |
||
| 132 | echo '</tr>'; |
||
| 133 | if ($totalsubs > 0) { |
||
| 134 | foreach ($subcatsObj as $subcat) { |
||
| 135 | $modify = "<a href='category.php?op=mod&categoryid=" . $subcat->categoryid() . "'><img src='" . XOOPS_URL . '/modules/' . $helper->getModule()->dirname() . "/assets/images/links/edit.gif' title='" . \_AM_PUBLISHER_MODIFY . "' alt='" . \_AM_PUBLISHER_MODIFY . "'></a>"; |
||
| 136 | $delete = "<a href='category.php?op=del&categoryid=" . $subcat->categoryid() . "'><img src='" . XOOPS_URL . '/modules/' . $helper->getModule()->dirname() . "/assets/images/links/delete.png' title='" . \_AM_PUBLISHER_DELETE . "' alt='" . \_AM_PUBLISHER_DELETE . "'></a>"; |
||
| 137 | echo '<tr>'; |
||
| 138 | echo "<td class='head' align='left'>" . $subcat->categoryid() . '</td>'; |
||
| 139 | echo "<td class='even' align='left'><a href='" . XOOPS_URL . '/modules/' . $helper->getModule()->dirname() . '/category.php?categoryid=' . $subcat->categoryid() . '&parentid=' . $subcat->parentid() . "'>" . $subcat->name() . '</a></td>'; |
||
| 140 | echo "<td class='even' align='left'>" . $subcat->description() . '</td>'; |
||
| 141 | echo "<td class='even' align='right'> {$modify} {$delete} </td>"; |
||
| 142 | echo '</tr>'; |
||
| 143 | } |
||
| 144 | // unset($subcat); |
||
| 145 | } else { |
||
| 146 | echo '<tr>'; |
||
| 147 | echo "<td class='head' align='center' colspan= '7'>" . \_AM_PUBLISHER_NOSUBCAT . '</td>'; |
||
| 148 | echo '</tr>'; |
||
| 149 | } |
||
| 150 | echo "</table>\n"; |
||
| 151 | echo "<br>\n"; |
||
| 152 | publisherCloseCollapsableBar('subcatstable', 'subcatsicon'); |
||
| 153 | |||
| 154 | publisherOpenCollapsableBar('bottomtable', 'bottomtableicon', \_AM_PUBLISHER_CAT_ITEMS, \_AM_PUBLISHER_CAT_ITEMS_DSC); |
||
| 155 | $startitem = Request::getInt('startitem'); |
||
| 156 | // Get the total number of published ITEMS |
||
| 157 | $totalitems = $helper->getHandler('Item')->getItemsCount($selCat, [Constants::PUBLISHER_STATUS_PUBLISHED]); |
||
| 158 | // creating the items objects that are published |
||
| 159 | $itemsObj = $helper->getHandler('Item')->getAllPublished($helper->getConfig('idxcat_perpage'), $startitem, $selCat); |
||
| 160 | $allcats = $helper->getHandler('Category')->getObjects(null, true); |
||
| 161 | echo "<table width='100%' cellspacing=1 cellpadding=3 border=0 class = outer>"; |
||
| 162 | echo '<tr>'; |
||
| 163 | echo "<td width='40' class='bg3' align='center'><strong>" . \_AM_PUBLISHER_ITEMID . '</strong></td>'; |
||
| 164 | echo "<td width='20%' class='bg3' align='left'><strong>" . \_AM_PUBLISHER_ITEMCOLNAME . '</strong></td>'; |
||
| 165 | echo "<td class='bg3' align='left'><strong>" . \_AM_PUBLISHER_ITEMDESC . '</strong></td>'; |
||
| 166 | echo "<td width='90' class='bg3' align='center'><strong>" . \_AM_PUBLISHER_CREATED . '</strong></td>'; |
||
| 167 | echo "<td width='60' class='bg3' align='center'><strong>" . \_AM_PUBLISHER_ACTION . '</strong></td>'; |
||
| 168 | echo '</tr>'; |
||
| 169 | if ($totalitems > 0) { |
||
| 170 | foreach ($itemsObj as $iValue) { |
||
| 171 | $categoryObj = $allcats[$iValue->categoryid()]; |
||
| 172 | $modify = "<a href='item.php?op=mod&itemid=" . $iValue->itemid() . "'><img src='" . XOOPS_URL . '/modules/' . $helper->getModule()->dirname() . "/assets/images/links/edit.gif' title='" . \_AM_PUBLISHER_EDITITEM . "' alt='" . \_AM_PUBLISHER_EDITITEM . "'></a>"; |
||
| 173 | $delete = "<a href='item.php?op=del&itemid=" |
||
| 174 | . $iValue->itemid() |
||
| 175 | . "'><img src='" |
||
| 176 | . XOOPS_URL |
||
| 177 | . '/modules/' |
||
| 178 | . $helper->getModule()->dirname() |
||
| 179 | . "/assets/images/links/delete.png' title='" |
||
| 180 | . \_AM_PUBLISHER_DELETEITEM |
||
| 181 | . "' alt='" |
||
| 182 | . \_AM_PUBLISHER_DELETEITEM |
||
| 183 | . "'></a>"; |
||
| 184 | echo '<tr>'; |
||
| 185 | echo "<td class='head' align='center'>" . $iValue->itemid() . '</td>'; |
||
| 186 | echo "<td class='even' align='left'>" . $categoryObj->name() . '</td>'; |
||
| 187 | echo "<td class='even' align='left'>" . $iValue->getitemLink() . '</td>'; |
||
| 188 | echo "<td class='even' align='center'>" . $iValue->getDatesub('s') . '</td>'; |
||
| 189 | echo "<td class='even' align='center'> $modify $delete </td>"; |
||
| 190 | echo '</tr>'; |
||
| 191 | } |
||
| 192 | } else { |
||
| 193 | $itemid = -1; |
||
| 194 | echo '<tr>'; |
||
| 195 | echo "<td class='head' align='center' colspan= '7'>" . \_AM_PUBLISHER_NOITEMS . '</td>'; |
||
| 196 | echo '</tr>'; |
||
| 197 | } |
||
| 198 | echo "</table>\n"; |
||
| 199 | echo "<br>\n"; |
||
| 200 | $parentid = Request::getInt('parentid', 0, 'GET'); |
||
| 201 | $pagenavExtraArgs = "op=mod&categoryid=$selCat&parentid=$parentid"; |
||
| 202 | \xoops_load('XoopsPageNav'); |
||
| 203 | $pagenav = new \XoopsPageNav($totalitems, $helper->getConfig('idxcat_perpage'), $startitem, 'startitem', $pagenavExtraArgs); |
||
| 204 | echo '<div style="text-align:right;">' . $pagenav->renderNav() . '</div>'; |
||
| 205 | echo "<input type='button' name='button' onclick=\"location='item.php?op=mod&categoryid=" . $selCat . "'\" value='" . \_AM_PUBLISHER_CREATEITEM . "'> "; |
||
| 206 | echo '</div>'; |
||
| 207 | } |
||
| 208 | //end of fx2024 code |
||
| 209 | } |
||
| 210 | |||
| 211 | // ====================== START =================================== |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Standard functions |
||
| 215 | * |
||
| 216 | * @param mixed $bytes |
||
| 217 | * @param mixed $precision |
||
| 218 | */ |
||
| 219 | |||
| 220 | /** |
||
| 221 | * This function transforms a numerical size (like 2048) to a letteral size (like 2MB) |
||
| 222 | * |
||
| 223 | * @param int $bytes numerical size |
||
| 224 | * @param int $precision |
||
| 225 | * |
||
| 226 | * @return string letteral size |
||
| 227 | **/ |
||
| 228 | public static function bytesToSize1000($bytes, $precision = 2) |
||
| 229 | { |
||
| 230 | // human readable format -- powers of 1000 |
||
| 231 | $unit = ['b', 'kb', 'mb', 'gb', 'tb', 'pb', 'eb']; |
||
| 232 | |||
| 233 | return @\round($bytes / \pow(1000, $i = \floor(\log($bytes, 1000))), $precision) . ' ' . $unit[(int)$i]; |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @param int $bytes |
||
| 238 | * @param int $precision |
||
| 239 | * |
||
| 240 | * @return string |
||
| 241 | */ |
||
| 242 | public static function bytesToSize1024($bytes, $precision = 2) |
||
| 243 | { |
||
| 244 | if (0 === $bytes) { |
||
| 245 | return 0; |
||
| 246 | } |
||
| 247 | |||
| 248 | // human readable format -- powers of 1024 |
||
| 249 | $unit = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB']; |
||
| 250 | |||
| 251 | $i = \floor(\log($bytes, 1024)); |
||
| 252 | |||
| 253 | return @\round($bytes / (\pow(1024, $i)), $precision) . ' ' . $unit[(int)$i]; |
||
| 254 | |||
| 255 | |||
| 256 | |||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * This function transforms the php.ini notation for numbers (like '2M') to an integer (2*1024*1024 in this case) |
||
| 261 | * |
||
| 262 | * @param string $size letteral size |
||
| 263 | * |
||
| 264 | * @return int numerical size |
||
| 265 | **/ |
||
| 266 | public static function sizeToBytes1024($size) |
||
| 267 | { |
||
| 268 | $l = mb_substr($size, -1); |
||
| 269 | $ret = mb_substr($size, 0, -1); |
||
| 270 | switch (mb_strtoupper($l)) { |
||
| 271 | case 'P': |
||
| 272 | case 'p': |
||
| 273 | $ret *= 1024; |
||
| 274 | break; |
||
| 275 | case 'T': |
||
| 276 | case 't': |
||
| 277 | $ret *= 1024; |
||
| 278 | break; |
||
| 279 | case 'G': |
||
| 280 | case 'g': |
||
| 281 | $ret *= 1024; |
||
| 282 | break; |
||
| 283 | case 'M': |
||
| 284 | case 'm': |
||
| 285 | $ret *= 1024; |
||
| 286 | break; |
||
| 287 | case 'K': |
||
| 288 | case 'k': |
||
| 289 | $ret *= 1024; |
||
| 290 | break; |
||
| 291 | } |
||
| 292 | |||
| 293 | return $ret; |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Filesystem functions |
||
| 298 | * |
||
| 299 | * @param mixed $path |
||
| 300 | * @param mixed $level |
||
| 301 | */ |
||
| 302 | |||
| 303 | /** |
||
| 304 | * This function will read the full structure of a directory. |
||
| 305 | * It's recursive because it doesn't stop with the one directory, |
||
| 306 | * it just keeps going through all of the directories in the folder you specify. |
||
| 307 | * |
||
| 308 | * @param string $path path to the directory to make |
||
| 309 | * @param int $level |
||
| 310 | * |
||
| 311 | * @return array |
||
| 312 | */ |
||
| 313 | public static function getDir($path = '.', $level = 0) |
||
| 314 | { |
||
| 315 | $ret = []; |
||
| 316 | $ignore = ['cgi-bin', '.', '..']; |
||
| 317 | // Directories to ignore when listing output. Many hosts will deny PHP access to the cgi-bin. |
||
| 318 | $dirHandler = @\opendir($path); |
||
| 319 | // Open the directory to the handle $dirHandler |
||
| 320 | while (false !== ($file = \readdir($dirHandler))) { |
||
| 321 | // Loop through the directory |
||
| 322 | if (!\in_array($file, $ignore)) { |
||
| 323 | // Check that this file is not to be ignored |
||
| 324 | $spaces = \str_repeat(' ', $level * 4); |
||
| 325 | // Just to add spacing to the list, to better show the directory tree. |
||
| 326 | if (\is_dir("$path/$file")) { |
||
| 327 | // Its a directory, so we need to keep reading down... |
||
| 328 | $ret[] = "<strong>{$spaces} {$file}</strong>"; |
||
| 329 | $ret = \array_merge($ret, self::getDir($path . \DIRECTORY_SEPARATOR . $file, $level + 1)); |
||
| 330 | // Re-call this same function but on a new directory. |
||
| 331 | // this is what makes function recursive. |
||
| 332 | } else { |
||
| 333 | $ret[] = "{$spaces} {$file}"; |
||
| 334 | // Just print out the filename |
||
| 335 | } |
||
| 336 | } |
||
| 337 | } |
||
| 338 | \closedir($dirHandler); |
||
| 339 | |||
| 340 | // close the directory handle |
||
| 341 | return $ret; |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Create a new directory that contains the file index.html |
||
| 346 | * |
||
| 347 | * @param string $dir path to the directory to make |
||
| 348 | * @param int $perm mode |
||
| 349 | * @param bool $create_index if true create index.html |
||
| 350 | * |
||
| 351 | * @return bool Returns true on success or false on failure |
||
| 352 | * @throws \Exception |
||
| 353 | * @throws \RuntimeException |
||
| 354 | */ |
||
| 355 | public static function makeDir($dir, $perm = 0777, $create_index = true) |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @param string $path |
||
| 376 | * |
||
| 377 | * @return array |
||
| 378 | */ |
||
| 379 | public static function getFiles($path = '.') |
||
| 380 | { |
||
| 381 | $files = []; |
||
| 382 | $dir = \opendir($path); |
||
| 383 | while (false !== ($file = \readdir($dir))) { |
||
| 384 | if (\is_file($path . $file)) { |
||
| 385 | if ('.' !== $file && '..' !== $file && 'blank.png' !== $file && 'index.html' !== $file) { |
||
| 386 | $files[] = $file; |
||
| 387 | } |
||
| 388 | } |
||
| 389 | } |
||
| 390 | |||
| 391 | return $files; |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Copy a file |
||
| 396 | * |
||
| 397 | * @param string $source is the original directory |
||
| 398 | * @param string $destination is the destination directory |
||
| 399 | * |
||
| 400 | * @return bool Returns true on success or false on failure |
||
| 401 | */ |
||
| 402 | /* |
||
| 403 | public static function copyFile($source, $destination) |
||
| 404 | { |
||
| 405 | // Simple copy for a file |
||
| 406 | if (is_file($source)) { |
||
| 407 | return copy($source, $destination); |
||
| 408 | } else { |
||
| 409 | return false; |
||
| 410 | } |
||
| 411 | } |
||
| 412 | */ |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Copy a directory and its contents |
||
| 416 | * |
||
| 417 | * @param string $source is the original directory |
||
| 418 | * @param string $destination is the destination directory |
||
| 419 | * |
||
| 420 | * @return bool Returns true on success or false on failure |
||
| 421 | * @throws \Exception |
||
| 422 | * @throws \RuntimeException |
||
| 423 | */ |
||
| 424 | public static function copyDir($source, $destination) |
||
| 425 | { |
||
| 426 | if (!$dirHandler = \opendir($source)) { |
||
| 427 | return false; |
||
| 428 | } |
||
| 429 | if (\mkdir($destination) || \is_dir($destination)) { |
||
| 430 | throw new \RuntimeException('The directory ' . $destination . ' could not be created.'); |
||
| 431 | } |
||
| 432 | while (false !== ($file = \readdir($dirHandler))) { |
||
| 433 | if (('.' !== $file) && ('..' !== $file)) { |
||
| 434 | if (\is_dir("{$source}/{$file}")) { |
||
| 435 | if (!self::copyDir("{$source}/{$file}", "{$destination}/{$file}")) { |
||
| 436 | return false; |
||
| 437 | } |
||
| 438 | } else { |
||
| 439 | if (!\copy("{$source}/{$file}", "{$destination}/{$file}")) { |
||
| 440 | return false; |
||
| 441 | } |
||
| 442 | } |
||
| 443 | } |
||
| 444 | } |
||
| 445 | \closedir($dirHandler); |
||
| 446 | |||
| 447 | return true; |
||
| 448 | } |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Delete a file |
||
| 452 | * |
||
| 453 | * @param string $path is the file absolute path |
||
| 454 | * |
||
| 455 | * @return bool Returns true on success or false on failure |
||
| 456 | */ |
||
| 457 | public static function delFile($path) |
||
| 458 | { |
||
| 459 | if (\is_file($path)) { |
||
| 460 | @\chmod($path, 0777); |
||
| 461 | |||
| 462 | return @\unlink($path); |
||
| 463 | } |
||
| 464 | |||
| 465 | return false; |
||
| 466 | } |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Delete a empty/not empty directory |
||
| 470 | * |
||
| 471 | * @param string $dir path to the directory to delete |
||
| 472 | * @param bool $if_not_empty if false it delete directory only if false |
||
| 473 | * |
||
| 474 | * @return bool Returns true on success or false on failure |
||
| 475 | */ |
||
| 476 | public static function delDir($dir, $if_not_empty = true) |
||
| 477 | { |
||
| 478 | if (!\file_exists($dir)) { |
||
| 479 | return true; |
||
| 480 | } |
||
| 481 | if ($if_not_empty) { |
||
| 482 | if (!\is_dir($dir)) { |
||
| 483 | return \unlink($dir); |
||
| 484 | } |
||
| 485 | foreach (\scandir($dir, \SCANDIR_SORT_NONE) as $item) { |
||
| 486 | if ('.' === $item || '..' === $item) { |
||
| 487 | continue; |
||
| 488 | } |
||
| 489 | if (!self::delDir("{$dir}/{$item}")) { |
||
| 490 | return false; |
||
| 491 | } |
||
| 492 | } |
||
| 493 | } |
||
| 494 | // NOP |
||
| 495 | |||
| 496 | return \rmdir($dir); |
||
| 497 | } |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Module functions |
||
| 501 | * |
||
| 502 | * @param mixed $dirname |
||
| 503 | */ |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Check if a module exist and return module verision |
||
| 507 | * |
||
| 508 | * @param string $dirname |
||
| 509 | * |
||
| 510 | * @return bool, integer false if module not installed or not active, module version if installed |
||
| 511 | * |
||
| 512 | * @access public |
||
| 513 | * @author luciorota |
||
| 514 | */ |
||
| 515 | public static function checkModule($dirname) |
||
| 516 | { |
||
| 517 | if (!\xoops_isActiveModule($dirname)) { |
||
| 518 | return false; |
||
| 519 | } |
||
| 520 | /** @var \XoopsModuleHandler $moduleHandler */ |
||
| 521 | $moduleHandler = \xoops_getHandler('module'); |
||
| 522 | $module = $moduleHandler->getByDirname($dirname); |
||
| 523 | |||
| 524 | return $module->getVar('version'); |
||
| 525 | } |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Recursively sort categories by level and weight |
||
| 529 | * |
||
| 530 | * @param int $pid |
||
| 531 | * @param int $level |
||
| 532 | * |
||
| 533 | * @return array array of arrays: 'pid', 'cid', 'level', 'category' as array |
||
| 534 | * |
||
| 535 | * @access public |
||
| 536 | * @author luciorota |
||
| 537 | */ |
||
| 538 | public static function sortCategories($pid = 0, $level = 0) |
||
| 539 | { |
||
| 540 | $helper = Helper::getInstance(); |
||
| 541 | |||
| 542 | $sorted = []; |
||
| 543 | $criteria = new \CriteriaCompo(); |
||
| 544 | $criteria->add(new \Criteria('pid', $pid)); |
||
| 545 | $criteria->setSort('weight'); |
||
| 546 | $criteria->setOrder('ASC'); |
||
| 547 | $subCategoryObjs = $helper->getHandler('Category')->getObjects($criteria); |
||
| 548 | if (\count($subCategoryObjs) > 0) { |
||
| 549 | ++$level; |
||
| 550 | foreach ($subCategoryObjs as $subCategoryObj) { |
||
| 551 | $pid = $subCategoryObj->getVar('pid'); |
||
| 552 | $cid = $subCategoryObj->getVar('cid'); |
||
| 553 | $sorted[] = ['pid' => $pid, 'cid' => $cid, 'level' => $level, 'category' => $subCategoryObj->toArray()]; |
||
| 554 | if (false !== ($subSorted = self::sortCategories($cid, $level))) { |
||
| 555 | $sorted = \array_merge($sorted, $subSorted); |
||
| 556 | } |
||
| 557 | } |
||
| 558 | } |
||
| 559 | |||
| 560 | return $sorted; |
||
| 561 | } |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Create download by letter choice bar/menu |
||
| 565 | * updated starting from this idea https://xoops.org/modules/news/article.php?storyid=6497 |
||
| 566 | * |
||
| 567 | * @return string html |
||
| 568 | * |
||
| 569 | * @access public |
||
| 570 | * @author luciorota |
||
| 571 | */ |
||
| 572 | public static function lettersChoice() |
||
| 609 | } |
||
| 610 | |||
| 611 | /** |
||
| 612 | * Checks if a user is admin of Wfdownloads |
||
| 613 | * |
||
| 614 | * @return bool |
||
| 615 | */ |
||
| 616 | public static function userIsAdmin() |
||
| 617 | { |
||
| 618 | $helper = Helper::getInstance(); |
||
| 619 | |||
| 620 | static $wfdownloads_isAdmin; |
||
| 621 | if (null !== $wfdownloads_isAdmin) { |
||
| 622 | return $wfdownloads_isAdmin; |
||
| 623 | } |
||
| 624 | $wfdownloads_isAdmin = (!\is_object($GLOBALS['xoopsUser'])) ? false : $GLOBALS['xoopsUser']->isAdmin($helper->getModule()->getVar('mid')); |
||
| 625 | |||
| 626 | return $wfdownloads_isAdmin; |
||
| 627 | } |
||
| 628 | |||
| 629 | public static function getCpHeader() |
||
| 630 | { |
||
| 631 | \xoops_cp_header(); |
||
| 632 | } |
||
| 633 | |||
| 634 | /** |
||
| 635 | * @param bool $withLink |
||
| 636 | * |
||
| 637 | * @return string |
||
| 638 | */ |
||
| 639 | public static function moduleHome($withLink = true) |
||
| 640 | { |
||
| 641 | $helper = Helper::getInstance(); |
||
| 642 | |||
| 643 | $wfdownloadsModuleName = $helper->getModule()->getVar('name'); |
||
| 644 | if (!$withLink) { |
||
| 645 | return $wfdownloadsModuleName; |
||
| 646 | } |
||
| 647 | |||
| 648 | return '<a href="' . WFDOWNLOADS_URL . '/">{$wfdownloadsModuleName}</a>'; |
||
| 649 | } |
||
| 650 | |||
| 651 | /** |
||
| 652 | * Detemines if a table exists in the current db |
||
| 653 | * |
||
| 654 | * @param string $table the table name (without XOOPS prefix) |
||
| 655 | * |
||
| 656 | * @return bool True if table exists, false if not |
||
| 657 | * |
||
| 658 | * @access public |
||
| 659 | * @author xhelp development team |
||
| 660 | */ |
||
| 661 | public static function tableExists($table) |
||
| 662 | { |
||
| 663 | $bRetVal = false; |
||
| 664 | //Verifies that a MySQL table exists |
||
| 665 | $GLOBALS['xoopsDB'] = \XoopsDatabaseFactory::getDatabaseConnection(); |
||
| 666 | $realName = $GLOBALS['xoopsDB']->prefix($table); |
||
| 667 | |||
| 668 | $sql = 'SHOW TABLES FROM ' . XOOPS_DB_NAME; |
||
| 669 | $ret = $GLOBALS['xoopsDB']->queryF($sql); |
||
| 670 | while (list($m_table) = $GLOBALS['xoopsDB']->fetchRow($ret)) { |
||
| 671 | if ($m_table == $realName) { |
||
| 672 | $bRetVal = true; |
||
| 673 | break; |
||
| 674 | } |
||
| 675 | } |
||
| 676 | $GLOBALS['xoopsDB']->freeRecordSet($ret); |
||
| 677 | |||
| 678 | return $bRetVal; |
||
| 679 | } |
||
| 680 | |||
| 681 | /** |
||
| 682 | * Gets a value from a key in the xhelp_meta table |
||
| 683 | * |
||
| 684 | * @param string $key |
||
| 685 | * |
||
| 686 | * @return string $value |
||
| 687 | * |
||
| 688 | * @access public |
||
| 689 | * @author xhelp development team |
||
| 690 | */ |
||
| 691 | public static function getMeta($key) |
||
| 692 | { |
||
| 693 | $GLOBALS['xoopsDB'] = \XoopsDatabaseFactory::getDatabaseConnection(); |
||
| 694 | $sql = \sprintf('SELECT metavalue FROM `%s` WHERE metakey=%s', $GLOBALS['xoopsDB']->prefix('wfdownloads_meta'), $GLOBALS['xoopsDB']->quoteString($key)); |
||
| 695 | $ret = $GLOBALS['xoopsDB']->query($sql); |
||
| 696 | if (!$ret) { |
||
| 697 | $value = false; |
||
| 698 | } else { |
||
| 699 | [$value] = $GLOBALS['xoopsDB']->fetchRow($ret); |
||
| 700 | } |
||
| 701 | |||
| 702 | return $value; |
||
| 703 | } |
||
| 704 | |||
| 705 | /** |
||
| 706 | * Sets a value for a key in the xhelp_meta table |
||
| 707 | * |
||
| 708 | * @param string $key |
||
| 709 | * @param string $value |
||
| 710 | * |
||
| 711 | * @return bool true if success, false if failure |
||
| 712 | * |
||
| 713 | * @access public |
||
| 714 | * @author xhelp development team |
||
| 715 | */ |
||
| 716 | public static function setMeta($key, $value) |
||
| 717 | { |
||
| 718 | $GLOBALS['xoopsDB'] = \XoopsDatabaseFactory::getDatabaseConnection(); |
||
| 719 | if (false !== ($ret = self::getMeta($key))) { |
||
| 720 | $sql = \sprintf('UPDATE `%s` SET metavalue = %s WHERE metakey = %s', $GLOBALS['xoopsDB']->prefix('wfdownloads_meta'), $GLOBALS['xoopsDB']->quoteString($value), $GLOBALS['xoopsDB']->quoteString($key)); |
||
| 721 | } else { |
||
| 722 | $sql = \sprintf('INSERT INTO `%s` (metakey, metavalue) VALUES (%s, %s)', $GLOBALS['xoopsDB']->prefix('wfdownloads_meta'), $GLOBALS['xoopsDB']->quoteString($key), $GLOBALS['xoopsDB']->quoteString($value)); |
||
| 723 | } |
||
| 724 | $ret = $GLOBALS['xoopsDB']->queryF($sql); |
||
| 725 | if (!$ret) { |
||
| 726 | return false; |
||
| 727 | } |
||
| 728 | |||
| 729 | return true; |
||
| 730 | } |
||
| 731 | |||
| 732 | /** |
||
| 733 | * @param $name |
||
| 734 | * @param $value |
||
| 735 | * @param int|float $time |
||
| 736 | */ |
||
| 737 | public static function setCookieVar($name, $value, $time = 0) |
||
| 738 | { |
||
| 739 | if (0 == $time) { |
||
| 740 | $time = \time() + 3600 * 24 * 365; |
||
| 741 | //$time = ''; |
||
| 742 | } |
||
| 743 | setcookie($name, $value, $time, '/', \ini_get('session.cookie_domain'), \ini_get('session.cookie_secure'), \ini_get('session.cookie_httponly')); |
||
| 744 | } |
||
| 745 | |||
| 746 | /** |
||
| 747 | * @param $name |
||
| 748 | * @param string $default |
||
| 749 | * |
||
| 750 | * @return string |
||
| 751 | */ |
||
| 752 | public static function getCookieVar($name, $default = '') |
||
| 753 | { |
||
| 754 | if (isset($_COOKIE[$name]) && ($_COOKIE[$name] > '')) { |
||
| 755 | return $_COOKIE[$name]; |
||
| 756 | } |
||
| 757 | |||
| 758 | return $default; |
||
| 759 | } |
||
| 760 | |||
| 761 | /** |
||
| 762 | * @return array |
||
| 763 | */ |
||
| 764 | public static function getCurrentUrls() |
||
| 765 | { |
||
| 766 | $http = (false === mb_strpos(XOOPS_URL, 'https://')) ? 'http://' : 'https://'; |
||
| 767 | $phpSelf = $_SERVER['SCRIPT_NAME']; |
||
| 768 | $httpHost = $_SERVER['HTTP_HOST']; |
||
| 769 | $queryString = $_SERVER['QUERY_STRING']; |
||
| 770 | |||
| 771 | if ('' !== $queryString) { |
||
| 772 | $queryString = '?' . $queryString; |
||
| 773 | } |
||
| 774 | $currentURL = $http . $httpHost . $phpSelf . $queryString; |
||
| 775 | $urls = []; |
||
| 776 | $urls['http'] = $http; |
||
| 777 | $urls['httphost'] = $httpHost; |
||
| 778 | $urls['phpself'] = $phpSelf; |
||
| 779 | $urls['querystring'] = $queryString; |
||
| 780 | $urls['full'] = $currentURL; |
||
| 781 | |||
| 782 | return $urls; |
||
| 783 | } |
||
| 784 | |||
| 785 | /** |
||
| 786 | * @return mixed |
||
| 787 | */ |
||
| 788 | public static function getCurrentPage() |
||
| 789 | { |
||
| 790 | $urls = self::getCurrentUrls(); |
||
| 791 | |||
| 792 | return $urls['full']; |
||
| 793 | } |
||
| 794 | |||
| 795 | /** |
||
| 796 | * @param array $errors |
||
| 797 | * |
||
| 798 | * @return string |
||
| 799 | */ |
||
| 800 | public static function formatErrors($errors = []) |
||
| 801 | { |
||
| 802 | $ret = ''; |
||
| 803 | //mb foreach ($errors as $key => $value) { |
||
| 804 | foreach ($errors as $value) { |
||
| 805 | $ret .= "<br> - {$value}"; |
||
| 806 | } |
||
| 807 | |||
| 808 | return $ret; |
||
| 809 | } |
||
| 810 | |||
| 811 | // TODO : The SEO feature is not fully implemented in the module... |
||
| 812 | |||
| 813 | /** |
||
| 814 | * @param $op |
||
| 815 | * @param $id |
||
| 816 | * @param string $title |
||
| 817 | * |
||
| 818 | * @return string |
||
| 819 | */ |
||
| 820 | public static function generateSeoUrl($op, $id, $title = '') |
||
| 821 | { |
||
| 822 | if (\defined('SEO_ENABLED')) { |
||
| 823 | if (SEO_ENABLED === 'rewrite') { |
||
| 824 | // generate SEO url using htaccess |
||
| 825 | return XOOPS_URL . "/wfdownloads.${op}.${id}/" . self::getSeoTitle($title); |
||
| 826 | } |
||
| 827 | |||
| 828 | if (SEO_ENABLED === 'path-info') { |
||
| 829 | // generate SEO url using path-info |
||
| 830 | return XOOPS_URL . "/modules/wfdownloads/seo.php/${op}.${id}/" . self::getSeoTitle($title); |
||
| 831 | } |
||
| 832 | exit('Unknown SEO method.'); |
||
| 833 | } |
||
| 834 | // generate classic url |
||
| 835 | switch ($op) { |
||
| 836 | case 'category': |
||
| 837 | return XOOPS_URL . "/modules/wfdownloads/${op}.php?categoryid=${id}"; |
||
| 838 | case 'item': |
||
| 839 | case 'print': |
||
| 840 | return XOOPS_URL . "/modules/wfdownloads/${op}.php?itemid=${id}"; |
||
| 841 | default: |
||
| 842 | exit('Unknown SEO operation.'); |
||
| 843 | } |
||
| 844 | } |
||
| 845 | |||
| 846 | /** |
||
| 847 | * @param string $title |
||
| 848 | * @return string |
||
| 849 | */ |
||
| 850 | public static function getSeoTitle($title = '') |
||
| 851 | { |
||
| 852 | $words = \preg_split('/[^0-9a-z.]+/', mb_strtolower($title), -1, \PREG_SPLIT_NO_EMPTY); |
||
| 853 | if (\count($words) > 0) { |
||
| 854 | return \implode('-', $words) . '.html'; |
||
| 855 | } |
||
| 856 | |||
| 857 | return ''; |
||
| 858 | } |
||
| 859 | |||
| 860 | /** |
||
| 861 | * save_Permissions() |
||
| 862 | * |
||
| 863 | * @param $groups |
||
| 864 | * @param $id |
||
| 865 | * @param $permName |
||
| 866 | * |
||
| 867 | * @return bool |
||
| 868 | */ |
||
| 869 | public static function savePermissions($groups, $id, $permName) |
||
| 870 | { |
||
| 871 | $helper = Helper::getInstance(); |
||
| 872 | |||
| 873 | $id = (int)$id; |
||
| 874 | $result = true; |
||
| 875 | $mid = $helper->getModule()->mid(); |
||
| 876 | $grouppermHandler = \xoops_getHandler('groupperm'); |
||
| 877 | // First, if the permissions are already there, delete them |
||
| 878 | $grouppermHandler->deleteByModule($mid, $permName, $id); |
||
| 879 | // Save the new permissions |
||
| 880 | if (\is_array($groups)) { |
||
| 881 | foreach ($groups as $group_id) { |
||
| 882 | $grouppermHandler->addRight($permName, $id, $group_id, $mid); |
||
| 883 | } |
||
| 884 | } |
||
| 885 | |||
| 886 | return $result; |
||
| 887 | } |
||
| 888 | |||
| 889 | /** |
||
| 890 | * toolbar() |
||
| 891 | * |
||
| 892 | * @return string |
||
| 893 | */ |
||
| 894 | public static function toolbar() |
||
| 895 | { |
||
| 896 | $helper = Helper::getInstance(); |
||
| 897 | |||
| 898 | $isSubmissionAllowed = false; |
||
| 899 | if (\is_object($GLOBALS['xoopsUser']) |
||
| 900 | && (\_WFDOWNLOADS_SUBMISSIONS_DOWNLOAD == $helper->getConfig('submissions') |
||
| 901 | || \_WFDOWNLOADS_SUBMISSIONS_BOTH == $helper->getConfig('submissions'))) { |
||
| 902 | $groups = $GLOBALS['xoopsUser']->getGroups(); |
||
| 903 | if (\count(\array_intersect($helper->getConfig('submitarts'), $groups)) > 0) { |
||
| 904 | $isSubmissionAllowed = true; |
||
| 905 | } |
||
| 906 | } elseif (!\is_object($GLOBALS['xoopsUser']) && (\_WFDOWNLOADS_ANONPOST_DOWNLOAD == $helper->getConfig('anonpost') || \_WFDOWNLOADS_ANONPOST_BOTH == $helper->getConfig('anonpost'))) { |
||
| 907 | $isSubmissionAllowed = true; |
||
| 908 | } |
||
| 909 | $toolbar = '[ '; |
||
| 910 | if (true === $isSubmissionAllowed) { |
||
| 911 | $category_suffix = Request::getInt('cid', '', 'GET'); //Added by Lankford |
||
| 912 | $toolbar .= "<a href='submit.php{$category_suffix}'>" . \_MD_WFDOWNLOADS_SUBMITDOWNLOAD . '</a> | '; |
||
| 913 | } |
||
| 914 | $toolbar .= "<a href='newlist.php'>" . \_MD_WFDOWNLOADS_LATESTLIST . '</a>'; |
||
| 915 | $toolbar .= ' | '; |
||
| 916 | $toolbar .= "<a href='topten.php?list=hit'>" . \_MD_WFDOWNLOADS_POPULARITY . '</a>'; |
||
| 917 | if ($helper->getConfig('enable_ratings')) { |
||
| 918 | $toolbar .= ' | '; |
||
| 919 | $toolbar .= "<a href='topten.php?list=rate'>" . \_MD_WFDOWNLOADS_TOPRATED . '</a>'; |
||
| 920 | } |
||
| 921 | $toolbar .= ' ]'; |
||
| 922 | |||
| 923 | return $toolbar; |
||
| 924 | } |
||
| 925 | |||
| 926 | /** |
||
| 927 | * displayicons() |
||
| 928 | * |
||
| 929 | * @param $time |
||
| 930 | * @param int $status |
||
| 931 | * @param int $counter |
||
| 932 | * |
||
| 933 | * @return string |
||
| 934 | */ |
||
| 935 | public static function displayIcons($time, $status = \_WFDOWNLOADS_STATUS_WAITING, $counter = 0) |
||
| 936 | { |
||
| 937 | $helper = Helper::getInstance(); |
||
| 938 | |||
| 939 | $new = ''; |
||
| 940 | $pop = ''; |
||
| 941 | $newdate = (\time() - (86400 * $helper->getConfig('daysnew'))); |
||
| 942 | $popdate = (\time() - (86400 * $helper->getConfig('daysupdated'))); |
||
| 943 | |||
| 944 | if (\_WFDOWNLOADS_DISPLAYICONS_NO != $helper->getConfig('displayicons')) { |
||
| 945 | if ($newdate < $time) { |
||
| 946 | if ((int)$status > \_WFDOWNLOADS_STATUS_APPROVED) { |
||
| 947 | if (\_WFDOWNLOADS_DISPLAYICONS_ICON == $helper->getConfig('displayicons')) { |
||
| 948 | $new = ' <img src=' . XOOPS_URL . '/modules/' . WFDOWNLOADS_DIRNAME . "/assets/images/icon/update.gif alt='' align ='absmiddle'>"; |
||
| 949 | } |
||
| 950 | if (\_WFDOWNLOADS_DISPLAYICONS_TEXT == $helper->getConfig('displayicons')) { |
||
| 951 | $new = '<i>' . \_WFDOWNLOADS_MD_UPDATED . '</i>'; |
||
| 952 | } |
||
| 953 | } else { |
||
| 954 | if (\_WFDOWNLOADS_DISPLAYICONS_ICON == $helper->getConfig('displayicons')) { |
||
| 955 | $new = ' <img src=' . XOOPS_URL . '/modules/' . WFDOWNLOADS_DIRNAME . "/assets/images/icon/newred.gif alt='' align ='absmiddle'>"; |
||
| 956 | } |
||
| 957 | if (\_WFDOWNLOADS_DISPLAYICONS_TEXT == $helper->getConfig('displayicons')) { |
||
| 958 | $new = '<i>' . \_WFDOWNLOADS_MD_NEW . '</i>'; |
||
| 959 | } |
||
| 960 | } |
||
| 961 | } |
||
| 962 | if ($popdate < $time) { |
||
| 963 | if ($counter >= $helper->getConfig('popular')) { |
||
| 964 | if (\_WFDOWNLOADS_DISPLAYICONS_ICON == $helper->getConfig('displayicons')) { |
||
| 965 | $pop = ' <img src =' . XOOPS_URL . '/modules/' . WFDOWNLOADS_DIRNAME . "/assets/images/icon/pop.gif alt='' align ='absmiddle'>"; |
||
| 966 | } |
||
| 967 | if (\_WFDOWNLOADS_DISPLAYICONS_TEXT == $helper->getConfig('displayicons')) { |
||
| 968 | $pop = '<i>' . \_WFDOWNLOADS_MD_POPULAR . '</i>'; |
||
| 969 | } |
||
| 970 | } |
||
| 971 | } |
||
| 972 | } |
||
| 973 | $icons = "{$new} {$pop}"; |
||
| 974 | |||
| 975 | return $icons; |
||
| 976 | } |
||
| 977 | |||
| 978 | //if (!function_exists('convertorderbyin')) { |
||
| 979 | // Reusable Link Sorting Functions |
||
| 980 | |||
| 981 | /** |
||
| 982 | * convertorderbyin() |
||
| 983 | * |
||
| 984 | * @param $orderby |
||
| 985 | * |
||
| 986 | * @return string |
||
| 987 | */ |
||
| 988 | public static function convertorderbyin($orderby) |
||
| 989 | { |
||
| 990 | switch (\trim($orderby)) { |
||
| 991 | case 'titleA': |
||
| 992 | $orderby = 'title ASC'; |
||
| 993 | break; |
||
| 994 | case 'titleD': |
||
| 995 | $orderby = 'title DESC'; |
||
| 996 | break; |
||
| 997 | case 'dateA': |
||
| 998 | $orderby = 'published ASC'; |
||
| 999 | break; |
||
| 1000 | case 'dateD': |
||
| 1001 | $orderby = 'published DESC'; |
||
| 1002 | break; |
||
| 1003 | case 'hitsA': |
||
| 1004 | $orderby = 'hits ASC'; |
||
| 1005 | break; |
||
| 1006 | case 'hitsD': |
||
| 1007 | $orderby = 'hits DESC'; |
||
| 1008 | break; |
||
| 1009 | case 'ratingA': |
||
| 1010 | $orderby = 'rating ASC'; |
||
| 1011 | break; |
||
| 1012 | case 'ratingD': |
||
| 1013 | $orderby = 'rating DESC'; |
||
| 1014 | break; |
||
| 1015 | case 'sizeD': |
||
| 1016 | $orderby = 'size DESC'; |
||
| 1017 | break; |
||
| 1018 | case 'sizeA': |
||
| 1019 | $orderby = 'size ASC'; |
||
| 1020 | break; |
||
| 1021 | default: |
||
| 1022 | $orderby = 'published DESC'; |
||
| 1023 | break; |
||
| 1024 | } |
||
| 1025 | |||
| 1026 | return $orderby; |
||
| 1027 | } |
||
| 1028 | |||
| 1029 | //} |
||
| 1030 | |||
| 1031 | //if (!function_exists('convertorderbytrans')) { |
||
| 1032 | |||
| 1033 | /** |
||
| 1034 | * @param $orderby |
||
| 1035 | * |
||
| 1036 | * @return string |
||
| 1037 | */ |
||
| 1038 | public static function convertorderbytrans($orderby) |
||
| 1039 | { |
||
| 1040 | if ('title ASC' === $orderby) { |
||
| 1041 | $orderbyTrans = \_MD_WFDOWNLOADS_TITLEATOZ; |
||
| 1042 | } |
||
| 1043 | if ('title DESC' === $orderby) { |
||
| 1044 | $orderbyTrans = \_MD_WFDOWNLOADS_TITLEZTOA; |
||
| 1045 | } |
||
| 1046 | if ('published ASC' === $orderby) { |
||
| 1047 | $orderbyTrans = \_MD_WFDOWNLOADS_DATEOLD; |
||
| 1048 | } |
||
| 1049 | if ('published DESC' === $orderby) { |
||
| 1050 | $orderbyTrans = \_MD_WFDOWNLOADS_DATENEW; |
||
| 1051 | } |
||
| 1052 | if ('hits ASC' === $orderby) { |
||
| 1053 | $orderbyTrans = \_MD_WFDOWNLOADS_POPULARITYLTOM; |
||
| 1054 | } |
||
| 1055 | if ('hits DESC' === $orderby) { |
||
| 1056 | $orderbyTrans = \_MD_WFDOWNLOADS_POPULARITYMTOL; |
||
| 1057 | } |
||
| 1058 | if ('rating ASC' === $orderby) { |
||
| 1059 | $orderbyTrans = \_MD_WFDOWNLOADS_RATINGLTOH; |
||
| 1060 | } |
||
| 1061 | if ('rating DESC' === $orderby) { |
||
| 1062 | $orderbyTrans = \_MD_WFDOWNLOADS_RATINGHTOL; |
||
| 1063 | } |
||
| 1064 | if ('size ASC' === $orderby) { |
||
| 1065 | $orderbyTrans = \_MD_WFDOWNLOADS_SIZELTOH; |
||
| 1066 | } |
||
| 1067 | if ('size DESC' === $orderby) { |
||
| 1068 | $orderbyTrans = \_MD_WFDOWNLOADS_SIZEHTOL; |
||
| 1069 | } |
||
| 1070 | |||
| 1071 | return $orderbyTrans; |
||
| 1072 | } |
||
| 1073 | |||
| 1074 | //} |
||
| 1075 | |||
| 1076 | //if (!function_exists('convertorderbyout')) { |
||
| 1077 | |||
| 1078 | /** |
||
| 1079 | * @param $orderby |
||
| 1080 | * |
||
| 1081 | * @return string |
||
| 1082 | */ |
||
| 1083 | public static function convertorderbyout($orderby) |
||
| 1084 | { |
||
| 1085 | if ('title ASC' === $orderby) { |
||
| 1086 | $orderby = 'titleA'; |
||
| 1087 | } |
||
| 1088 | if ('title DESC' === $orderby) { |
||
| 1089 | $orderby = 'titleD'; |
||
| 1090 | } |
||
| 1091 | if ('published ASC' === $orderby) { |
||
| 1092 | $orderby = 'dateA'; |
||
| 1093 | } |
||
| 1094 | if ('published DESC' === $orderby) { |
||
| 1095 | $orderby = 'dateD'; |
||
| 1096 | } |
||
| 1097 | if ('hits ASC' === $orderby) { |
||
| 1098 | $orderby = 'hitsA'; |
||
| 1099 | } |
||
| 1100 | if ('hits DESC' === $orderby) { |
||
| 1101 | $orderby = 'hitsD'; |
||
| 1102 | } |
||
| 1103 | if ('rating ASC' === $orderby) { |
||
| 1104 | $orderby = 'ratingA'; |
||
| 1105 | } |
||
| 1106 | if ('rating DESC' === $orderby) { |
||
| 1107 | $orderby = 'ratingD'; |
||
| 1108 | } |
||
| 1109 | if ('size ASC' === $orderby) { |
||
| 1110 | $orderby = 'sizeA'; |
||
| 1111 | } |
||
| 1112 | if ('size DESC' === $orderby) { |
||
| 1113 | $orderby = 'sizeD'; |
||
| 1114 | } |
||
| 1115 | |||
| 1116 | return $orderby; |
||
| 1117 | } |
||
| 1118 | |||
| 1119 | //} |
||
| 1120 | |||
| 1121 | /** |
||
| 1122 | * updaterating() |
||
| 1123 | * |
||
| 1124 | * @param $lid |
||
| 1125 | */ |
||
| 1126 | public static function updateRating($lid) |
||
| 1127 | { |
||
| 1128 | $helper = Helper::getInstance(); |
||
| 1129 | |||
| 1130 | $ratingObjs = $helper->getHandler('Rating')->getObjects(new \Criteria('lid', (int)$lid)); |
||
| 1131 | $ratings_count = \count($ratingObjs); |
||
| 1132 | $totalRating = 0; |
||
| 1133 | foreach ($ratingObjs as $ratingObj) { |
||
| 1134 | $totalRating += $ratingObj->getVar('rating'); |
||
| 1135 | } |
||
| 1136 | $averageRating = $totalRating / $ratings_count; |
||
| 1137 | $averageRating = \number_format($averageRating, 4); |
||
| 1138 | $downloadObj = $helper->getHandler('Download')->get($lid); |
||
| 1139 | $downloadObj->setVar('rating', $averageRating); |
||
| 1140 | $downloadObj->setVar('votes', $ratings_count); |
||
| 1141 | $helper->getHandler('Download')->insert($downloadObj); |
||
| 1142 | } |
||
| 1143 | |||
| 1144 | /** |
||
| 1145 | * categoriesCount() |
||
| 1146 | * |
||
| 1147 | * @return int |
||
| 1148 | */ |
||
| 1149 | public static function categoriesCount() |
||
| 1150 | { |
||
| 1151 | $grouppermHandler = \xoops_getHandler('groupperm'); |
||
| 1152 | $helper = Helper::getInstance(); |
||
| 1153 | $groups = \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getGroups() : [0 => XOOPS_GROUP_ANONYMOUS]; |
||
| 1154 | $allowedDownCategoriesIds = $grouppermHandler->getItemIds('WFDownCatPerm', $groups, $helper->getModule()->mid()); |
||
| 1155 | |||
| 1156 | return \count($allowedDownCategoriesIds); |
||
| 1157 | } |
||
| 1158 | |||
| 1159 | /** |
||
| 1160 | * getTotalDownloads() |
||
| 1161 | * |
||
| 1162 | * @param int|array $cids (array of integer) |
||
| 1163 | * |
||
| 1164 | * @return bool number of items in items table that are associated with a given table $table id |
||
| 1165 | */ |
||
| 1166 | public static function getTotalDownloads($cids = 0) |
||
| 1167 | { |
||
| 1168 | $helper = Helper::getInstance(); |
||
| 1169 | |||
| 1170 | $criteria = new \CriteriaCompo(new \Criteria('offline', false)); |
||
| 1171 | $criteria->add(new \Criteria('published', 0, '>')); |
||
| 1172 | $criteria->add(new \Criteria('published', \time(), '<=')); |
||
| 1173 | $expiredCriteria = new \CriteriaCompo(new \Criteria('expired', 0)); |
||
| 1174 | $expiredCriteria->add(new \Criteria('expired', \time(), '>='), 'OR'); |
||
| 1175 | $criteria->add($expiredCriteria); |
||
| 1176 | if ($cids && \is_array($cids)) { |
||
| 1177 | $criteria->add(new \Criteria('cid', '(' . \implode(',', $cids) . ')', 'IN')); |
||
| 1178 | } elseif ($cids > 0) { |
||
| 1179 | $criteria->add(new \Criteria('cid', (int)$cids)); |
||
| 1180 | } else { |
||
| 1181 | return false; |
||
| 1182 | } |
||
| 1183 | $criteria->setGroupBy('cid'); |
||
| 1184 | $info['published'] = $helper->getHandler('Download')->getMaxPublishdate($criteria); |
||
| 1185 | $info['count'] = $helper->getHandler('Download')->getCount($criteria); |
||
| 1186 | |||
| 1187 | return $info; |
||
| 1188 | } |
||
| 1189 | |||
| 1190 | /** |
||
| 1191 | * @return string |
||
| 1192 | */ |
||
| 1193 | public static function headerImage() |
||
| 1194 | { |
||
| 1195 | $helper = Helper::getInstance(); |
||
| 1196 | |||
| 1197 | $image = ''; |
||
| 1198 | $result = $GLOBALS['xoopsDB']->query('SELECT indeximage, indexheading FROM ' . $GLOBALS['xoopsDB']->prefix('wfdownloads_indexpage') . ' '); |
||
| 1199 | [$indexImage, $indexHeading] = $GLOBALS['xoopsDB']->fetchrow($result); |
||
| 1200 | if (!empty($indeximage)) { |
||
| 1201 | $image = self::displayImage($indexImage, 'index.php', $helper->getConfig('mainimagedir'), $indexHeading); |
||
| 1202 | } |
||
| 1203 | |||
| 1204 | return $image; |
||
| 1205 | } |
||
| 1206 | |||
| 1207 | /** |
||
| 1208 | * @param string $image |
||
| 1209 | * @param string $href |
||
| 1210 | * @param string $imgSource |
||
| 1211 | * @param string $altText |
||
| 1212 | * |
||
| 1213 | * @return string |
||
| 1214 | */ |
||
| 1215 | public static function displayImage($image = '', $href = '', $imgSource = '', $altText = '') |
||
| 1216 | { |
||
| 1217 | $helper = Helper::getInstance(); |
||
| 1218 | |||
| 1219 | $showImage = ''; |
||
| 1220 | |||
| 1221 | // Check to see if link is given |
||
| 1222 | if ($href) { |
||
| 1223 | $showImage = "<a href='{$href}'>"; |
||
| 1224 | } |
||
| 1225 | // checks to see if the file is valid else displays default blank image |
||
| 1226 | if (!\is_dir(XOOPS_ROOT_PATH . "/{$imgSource}/{$image}") && \is_dir(XOOPS_ROOT_PATH . "/{$imgSource}/{$image}")) { |
||
| 1227 | $showImage .= "<img src='" . XOOPS_URL . "/{$imgSource}/{$image}' border='0' alt='{$altText}'>"; |
||
| 1228 | } else { |
||
| 1229 | if ($GLOBALS['xoopsUser'] && $GLOBALS['xoopsUser']->isAdmin($helper->getModule()->mid())) { |
||
| 1230 | $showImage .= "<img src='" . XOOPS_URL . '/modules/' . WFDOWNLOADS_DIRNAME . "/assets/images/brokenimg.png' alt='" . \_MD_WFDOWNLOADS_ISADMINNOTICE . "'>"; |
||
| 1231 | } else { |
||
| 1232 | $showImage .= "<img src='" . XOOPS_URL . '/modules/' . WFDOWNLOADS_DIRNAME . "/assets/images/blank.png' alt='{$altText}'>"; |
||
| 1233 | } |
||
| 1234 | } |
||
| 1235 | if ($href) { |
||
| 1236 | $showImage .= '</a>'; |
||
| 1237 | } |
||
| 1238 | \clearstatcache(); |
||
| 1239 | |||
| 1240 | return $showImage; |
||
| 1241 | } |
||
| 1242 | |||
| 1243 | /** |
||
| 1244 | * createThumb() |
||
| 1245 | * |
||
| 1246 | * @param $imgName |
||
| 1247 | * @param $imgPath |
||
| 1248 | * @param $imgSavePath |
||
| 1249 | * @param int $width |
||
| 1250 | * @param int $height |
||
| 1251 | * @param int $quality |
||
| 1252 | * @param bool|int $update |
||
| 1253 | * @param int $aspect |
||
| 1254 | * |
||
| 1255 | * @return string |
||
| 1256 | */ |
||
| 1257 | public static function createThumb($imgName, $imgPath, $imgSavePath, $width = 100, $height = 100, $quality = 100, $update = false, $aspect = 1) |
||
| 1258 | { |
||
| 1259 | $helper = Helper::getInstance(); |
||
| 1260 | |||
| 1261 | // Paths |
||
| 1262 | if (false === $helper->getConfig('usethumbs')) { |
||
| 1263 | $imageURL = XOOPS_URL . "/{$imgPath}/{$imgName}"; |
||
| 1264 | |||
| 1265 | return $imageURL; |
||
| 1266 | } |
||
| 1267 | $imagePath = XOOPS_ROOT_PATH . "/{$imgPath}/{$imgName}"; |
||
| 1268 | $saveFile = "{$imgPath}/{$imgSavePath}/{$width}x{$height}_{$imgName}"; |
||
| 1269 | $savePath = XOOPS_ROOT_PATH . '/' . $saveFile; |
||
| 1270 | // Return the image if no update and image exists |
||
| 1271 | if (false === $update && \file_exists($savePath)) { |
||
| 1272 | return XOOPS_URL . '/' . $saveFile; |
||
| 1273 | } |
||
| 1274 | // Original image info |
||
| 1275 | [$origWidth, $origHeight, $type, $attr] = \getimagesize($imagePath, $info); |
||
| 1276 | switch ($type) { |
||
| 1277 | case 1: |
||
| 1278 | # GIF image |
||
| 1279 | if (\function_exists('imagecreatefromgif')) { |
||
| 1280 | $img = @\imagecreatefromgif($imagePath); |
||
| 1281 | } else { |
||
| 1282 | $img = @\imagecreatefrompng($imagePath); |
||
| 1283 | } |
||
| 1284 | break; |
||
| 1285 | case 2: |
||
| 1286 | # JPEG image |
||
| 1287 | $img = @\imagecreatefromjpeg($imagePath); |
||
| 1288 | break; |
||
| 1289 | case 3: |
||
| 1290 | # PNG image |
||
| 1291 | $img = @\imagecreatefrompng($imagePath); |
||
| 1292 | break; |
||
| 1293 | default: |
||
| 1294 | return $imagePath; |
||
| 1295 | break; |
||
| 1296 | } |
||
| 1297 | if (!empty($img)) { |
||
| 1298 | // Get original image size and scale ratio |
||
| 1299 | $scale = $origWidth / $origHeight; |
||
| 1300 | if ($width / $height > $scale) { |
||
| 1301 | $width = $height * $scale; |
||
| 1302 | } else { |
||
| 1303 | $height = $width / $scale; |
||
| 1304 | } |
||
| 1305 | // Create a new temporary image |
||
| 1306 | if (\function_exists('imagecreatetruecolor')) { |
||
| 1307 | $tempImg = \imagecreatetruecolor($width, $height); |
||
| 1308 | } else { |
||
| 1309 | $tempImg = \imagecreate($width, $height); |
||
| 1310 | } |
||
| 1311 | // Copy and resize old image into new image |
||
| 1312 | \imagecopyresampled($tempImg, $img, 0, 0, 0, 0, $width, $height, $origWidth, $origHeight); |
||
| 1313 | \imagedestroy($img); |
||
| 1314 | \flush(); |
||
| 1315 | $img = $tempImg; |
||
| 1316 | } |
||
| 1317 | switch ($type) { |
||
| 1318 | case 1: |
||
| 1319 | default: |
||
| 1320 | # GIF image |
||
| 1321 | if (\function_exists('imagegif')) { |
||
| 1322 | \imagegif($img, $savePath); |
||
| 1323 | } else { |
||
| 1324 | \imagepng($img, $savePath); |
||
| 1325 | } |
||
| 1326 | break; |
||
| 1327 | case 2: |
||
| 1328 | # JPEG image |
||
| 1329 | \imagejpeg($img, $savePath, $quality); |
||
| 1330 | break; |
||
| 1331 | case 3: |
||
| 1332 | # PNG image |
||
| 1333 | \imagepng($img, $savePath); |
||
| 1334 | break; |
||
| 1335 | } |
||
| 1336 | \imagedestroy($img); |
||
| 1337 | \flush(); |
||
| 1338 | |||
| 1339 | return XOOPS_URL . '/' . $saveFile; |
||
| 1340 | } |
||
| 1341 | |||
| 1342 | /** |
||
| 1343 | * isNewImage() |
||
| 1344 | * |
||
| 1345 | * @param int $published date |
||
| 1346 | * |
||
| 1347 | * @return array 'image', 'alttext', 'days' number of days between $published and now |
||
| 1348 | **/ |
||
| 1349 | public static function isNewImage($published) |
||
| 1350 | { |
||
| 1351 | if ($published <= 0) { |
||
| 1352 | $indicator['image'] = 'assets/images/icon/download.gif'; |
||
| 1353 | $indicator['alttext'] = \_MD_WFDOWNLOADS_NO_FILES; |
||
| 1354 | $indicator['days'] = null; |
||
| 1355 | } else { |
||
| 1356 | $days = (int)((\time() - $published) / 86400); // number of days between $published and now |
||
| 1357 | $indicator['days'] = $days; |
||
| 1358 | switch ($days) { |
||
| 1359 | case 0: |
||
| 1360 | // today |
||
| 1361 | $indicator['image'] = 'assets/images/icon/download1.gif'; |
||
| 1362 | $indicator['alttext'] = \_MD_WFDOWNLOADS_TODAY; |
||
| 1363 | break; |
||
| 1364 | case 1: |
||
| 1365 | case 2: |
||
| 1366 | // less than 3 days |
||
| 1367 | $indicator['image'] = 'assets/images/icon/download2.gif'; |
||
| 1368 | $indicator['alttext'] = \_MD_WFDOWNLOADS_THREE; |
||
| 1369 | break; |
||
| 1370 | case 3: |
||
| 1371 | case 4: |
||
| 1372 | case 5: |
||
| 1373 | case 6: |
||
| 1374 | // less than 7 days |
||
| 1375 | $indicator['image'] = 'assets/images/icon/download3.gif'; |
||
| 1376 | $indicator['alttext'] = \_MD_WFDOWNLOADS_NEWTHIS; |
||
| 1377 | break; |
||
| 1378 | case 7: |
||
| 1379 | default: |
||
| 1380 | // more than a week |
||
| 1381 | $indicator['image'] = 'assets/images/icon/download4.gif'; |
||
| 1382 | $indicator['alttext'] = \_MD_WFDOWNLOADS_NEWLAST; |
||
| 1383 | break; |
||
| 1384 | } |
||
| 1385 | } |
||
| 1386 | |||
| 1387 | return $indicator; |
||
| 1388 | } |
||
| 1389 | |||
| 1390 | // GetDownloadTime() |
||
| 1391 | // This function is used to show some different download times |
||
| 1392 | // BCMATH-Support in PHP needed! |
||
| 1393 | // (c)02.04.04 by St@neCold, [email protected], http://www.csgui.de |
||
| 1394 | |||
| 1395 | /** |
||
| 1396 | * @param int $size |
||
| 1397 | * @param int $gmodem |
||
| 1398 | * @param int $gisdn |
||
| 1399 | * @param int $gdsl |
||
| 1400 | * @param int $gslan |
||
| 1401 | * @param int $gflan |
||
| 1402 | * |
||
| 1403 | * @return string |
||
| 1404 | */ |
||
| 1405 | public static function getDownloadTime($size = 0, $gmodem = 1, $gisdn = 1, $gdsl = 1, $gslan = 0, $gflan = 0) |
||
| 1406 | { |
||
| 1407 | $aflag = []; |
||
| 1408 | $amtime = []; |
||
| 1409 | $artime = []; |
||
| 1410 | $ahtime = []; |
||
| 1411 | $asout = []; |
||
| 1412 | $aflag = [$gmodem, $gisdn, $gdsl, $gslan, $gflan]; |
||
| 1413 | $amtime = [$size / 6300 / 60, $size / 7200 / 60, $size / 86400 / 60, $size / 1125000 / 60, $size / 11250000 / 60]; |
||
| 1414 | $amname = ['Modem(56k)', 'ISDN(64k)', 'DSL(768k)', 'LAN(10M)', 'LAN(100M']; |
||
| 1415 | for ($i = 0; $i < 5; ++$i) { |
||
| 1416 | $artime[$i] = ($amtime[$i] % 60); |
||
| 1417 | } |
||
| 1418 | for ($i = 0; $i < 5; ++$i) { |
||
| 1419 | $ahtime[$i] = \sprintf(' %2.0f', $amtime[$i] / 60); |
||
| 1420 | } |
||
| 1421 | if ($size <= 0) { |
||
| 1422 | $dltime = 'N/A'; |
||
| 1423 | } else { |
||
| 1424 | for ($i = 0; $i < 5; ++$i) { |
||
| 1425 | if (!$aflag[$i]) { |
||
| 1426 | $asout[$i] = ''; |
||
| 1427 | } else { |
||
| 1428 | if (($amtime[$i] * 60) < 1) { |
||
| 1429 | $asout[$i] = \sprintf(' : %4.2fs', $amtime[$i] * 60); |
||
| 1430 | } else { |
||
| 1431 | if ($amtime[$i] < 1) { |
||
| 1432 | $asout[$i] = \sprintf(' : %2.0fs', \round($amtime[$i] * 60)); |
||
| 1433 | } else { |
||
| 1434 | if (0 == $ahtime[$i]) { |
||
| 1435 | $asout[$i] = \sprintf(' : %5.1fmin', $amtime[$i]); |
||
| 1436 | } else { |
||
| 1437 | $asout[$i] = \sprintf(' : %2.0fh%2.0fmin', $ahtime[$i], $artime[$i]); |
||
| 1438 | } |
||
| 1439 | } |
||
| 1440 | } |
||
| 1441 | $asout[$i] = '<b>' . $amname[$i] . '</b>' . $asout[$i]; |
||
| 1442 | if ($i < 4) { |
||
| 1443 | $asout[$i] .= ' | '; |
||
| 1444 | } |
||
| 1445 | } |
||
| 1446 | } |
||
| 1447 | $dltime = ''; |
||
| 1448 | for ($i = 0; $i < 5; ++$i) { |
||
| 1449 | $dltime .= $asout[$i]; |
||
| 1450 | } |
||
| 1451 | } |
||
| 1452 | |||
| 1453 | return $dltime; |
||
| 1454 | } |
||
| 1455 | |||
| 1456 | /** |
||
| 1457 | * @param $haystack |
||
| 1458 | * @param $needle |
||
| 1459 | * |
||
| 1460 | * @return string |
||
| 1461 | */ |
||
| 1462 | public static function strrrchr($haystack, $needle) |
||
| 1463 | { |
||
| 1464 | return mb_substr($haystack, 0, mb_strpos($haystack, $needle) + 1); |
||
| 1465 | } |
||
| 1466 | |||
| 1467 | /** |
||
| 1468 | * @param $fileName |
||
| 1469 | * @param bool $isAdmin |
||
| 1470 | * |
||
| 1471 | * @return array |
||
| 1472 | */ |
||
| 1473 | public static function allowedMimetypes($fileName, $isAdmin = true) |
||
| 1474 | { |
||
| 1475 | $helper = Helper::getInstance(); |
||
| 1476 | |||
| 1477 | $ext = \ltrim(mb_strrchr($fileName, '.'), '.'); |
||
| 1478 | $criteria = new \CriteriaCompo(new \Criteria('mime_ext', mb_strtolower($ext))); |
||
| 1479 | if ($isAdmin) { |
||
| 1480 | $criteria->add(new \Criteria('mime_admin', true)); |
||
| 1481 | } else { |
||
| 1482 | $criteria->add(new \Criteria('mime_user', true)); |
||
| 1483 | } |
||
| 1484 | $ret = []; |
||
| 1485 | if (false !== ($mimetypeObjs = $helper->getHandler('Mimetype')->getObjects($criteria))) { |
||
| 1486 | $mimetypeObj = $mimetypeObjs[0]; |
||
| 1487 | if (null !== $mimetypeObj) { |
||
| 1488 | $ret = \explode(' ', $mimetypeObj->getVar('mime_types')); |
||
| 1489 | } |
||
| 1490 | } |
||
| 1491 | return $ret; |
||
| 1492 | } |
||
| 1493 | |||
| 1494 | /** |
||
| 1495 | * @param $size_str |
||
| 1496 | * |
||
| 1497 | * @return int |
||
| 1498 | */ |
||
| 1499 | public static function returnBytes($size_str) |
||
| 1500 | { |
||
| 1501 | switch (mb_substr($size_str, -1)) { |
||
| 1502 | case 'M': |
||
| 1503 | case 'm': |
||
| 1504 | return (int)$size_str * 1048576; |
||
| 1505 | case 'K': |
||
| 1506 | case 'k': |
||
| 1507 | return (int)$size_str * 1024; |
||
| 1508 | case 'G': |
||
| 1509 | case 'g': |
||
| 1510 | return (int)$size_str * 1073741824; |
||
| 1511 | default: |
||
| 1512 | return $size_str; |
||
| 1513 | } |
||
| 1514 | } |
||
| 1515 | |||
| 1516 | /** |
||
| 1517 | * uploading() |
||
| 1518 | * |
||
| 1519 | * @param string $filename |
||
| 1520 | * @param string $uploadDirectory |
||
| 1521 | * @param array $allowedMimetypes |
||
| 1522 | * @param string $redirectURL |
||
| 1523 | * @param int $num |
||
| 1524 | * @param bool $redirect |
||
| 1525 | * @param bool $isAdmin |
||
| 1526 | * @param bool $onlyImages |
||
| 1527 | * |
||
| 1528 | * @return array |
||
| 1529 | **/ |
||
| 1530 | public static function uploading( |
||
| 1531 | $filename, |
||
| 1532 | $uploadDirectory = 'uploads', |
||
| 1533 | $allowedMimetypes = [], |
||
| 1534 | $redirectURL = 'index.php', |
||
| 1535 | $num = 0, |
||
| 1536 | $redirect = false, |
||
| 1537 | $isAdmin = true, |
||
| 1538 | $onlyImages = false |
||
| 1539 | ) { |
||
| 1540 | $helper = Helper::getInstance(); |
||
| 1541 | $file = []; |
||
| 1542 | if (empty($allowedMimetypes)) { |
||
| 1543 | $allowedMimetypes = self::allowedMimetypes($_FILES['userfile']['name'], $isAdmin); |
||
| 1544 | } |
||
| 1545 | if (empty($allowedMimetypes)) { |
||
| 1546 | $errors = 'MIME type not allowed'; |
||
| 1547 | \redirect_header($redirectURL, 4, $errors); |
||
| 1548 | } |
||
| 1549 | $uploadDirectory .= '/'; |
||
| 1550 | $file_name = $_FILES['userfile']['name']; |
||
| 1551 | //Admin can upload files of any size |
||
| 1552 | if (self::userIsAdmin()) { |
||
| 1553 | $maxFileSize = self::returnBytes(\ini_get('upload_max_filesize')); |
||
| 1554 | } else { |
||
| 1555 | $maxFileSize = $helper->getConfig('maxfilesize'); |
||
| 1556 | } |
||
| 1557 | $maxImageWidth = $helper->getConfig('maximgwidth'); |
||
| 1558 | $maxImageHeight = $helper->getConfig('maximgheight'); |
||
| 1559 | // TODO: use Xoops XoopsMediaUploader class |
||
| 1560 | if ($onlyImages) { |
||
| 1561 | // require_once XOOPS_ROOT_PATH . '/modules/wfdownloads/class/img_uploader.php'; |
||
| 1562 | //xoops_load('XoopsMediaUploader'); |
||
| 1563 | $uploader = new Wfdownloads\MediaImgUploader($uploadDirectory, $allowedMimetypes, $maxFileSize, $maxImageWidth, $maxImageHeight); |
||
| 1564 | } else { |
||
| 1565 | require_once XOOPS_ROOT_PATH . '/class/uploader.php'; |
||
| 1566 | //xoops_load('XoopsMediaUploader'); |
||
| 1567 | $uploader = new \XoopsMediaUploader($uploadDirectory, $allowedMimetypes, $maxFileSize, $maxImageWidth, $maxImageHeight); |
||
| 1568 | } |
||
| 1569 | // $uploader->noAdminSizeCheck(1); |
||
| 1570 | if ($uploader->fetchMedia($_POST['xoops_upload_file'][0])) { |
||
| 1571 | if (!$uploader->upload()) { |
||
| 1572 | $errors = $uploader->getErrors(); |
||
| 1573 | \unlink($uploadDirectory . $uploader->savedFileName); |
||
| 1574 | \redirect_header($redirectURL, 4, $errors); |
||
| 1575 | } else { |
||
| 1576 | if ($redirect) { |
||
| 1577 | \redirect_header($redirectURL, 4, \_AM_WFDOWNLOADS_UPLOADFILE); |
||
| 1578 | } else { |
||
| 1579 | if (\is_file($uploader->savedDestination)) { |
||
| 1580 | // $file['url'] = XOOPS_URL . '/' . $uploadDirectory . '/'; |
||
| 1581 | $file['filename'] = mb_strtolower($uploader->savedFileName); |
||
| 1582 | $file['filetype'] = $_FILES['userfile']['type']; |
||
| 1583 | $file['size'] = \filesize($uploadDirectory . mb_strtolower($uploader->savedFileName)); |
||
| 1584 | } |
||
| 1585 | |||
| 1586 | return $file; |
||
| 1587 | } |
||
| 1588 | } |
||
| 1589 | } else { |
||
| 1590 | $errors = $uploader->getErrors(); |
||
| 1591 | \unlink($uploadDirectory . $uploader->savedFileName); |
||
| 1592 | \redirect_header($redirectURL, 4, $errors); |
||
| 1593 | } |
||
| 1594 | |||
| 1595 | return null; |
||
| 1596 | } |
||
| 1597 | |||
| 1598 | /** |
||
| 1599 | * @param $filePath |
||
| 1600 | * @param bool $isBinary |
||
| 1601 | * @param bool $retBytes |
||
| 1602 | * |
||
| 1603 | * @return bool|int|mixed |
||
| 1604 | */ |
||
| 1605 | public static function download($filePath, $isBinary = true, $retBytes = true) |
||
| 1606 | { |
||
| 1607 | // how many bytes per chunk |
||
| 1608 | //$chunkSize = 1 * (1024 * 1024); |
||
| 1609 | $chunkSize = 8 * (1024 * 1024); //8MB (highest possible fread length) |
||
| 1610 | $buffer = ''; |
||
| 1611 | $bytesCounter = 0; |
||
| 1612 | |||
| 1613 | if ($isBinary) { |
||
| 1614 | $handler = \fopen($filePath, 'rb'); |
||
| 1615 | } else { |
||
| 1616 | $handler = \fopen($filePath, 'rb'); |
||
| 1617 | } |
||
| 1618 | if (false === $handler) { |
||
| 1619 | return false; |
||
| 1620 | } |
||
| 1621 | while (!\feof($handler)) { |
||
| 1622 | $buffer = \fread($handler, $chunkSize); |
||
| 1623 | echo $buffer; |
||
| 1624 | \ob_flush(); |
||
| 1625 | \flush(); |
||
| 1626 | if ($retBytes) { |
||
| 1627 | $bytesCounter += mb_strlen($buffer); |
||
| 1628 | } |
||
| 1629 | } |
||
| 1630 | $status = \fclose($handler); |
||
| 1631 | if ($retBytes && $status) { |
||
| 1632 | return $bytesCounter; // return num. bytes delivered like readfile() does. |
||
| 1633 | } |
||
| 1634 | |||
| 1635 | return $status; |
||
| 1636 | } |
||
| 1637 | |||
| 1638 | // IN PROGRESS |
||
| 1639 | // IN PROGRESS |
||
| 1640 | // IN PROGRESS |
||
| 1641 | |||
| 1642 | /** |
||
| 1643 | * @param $filePath |
||
| 1644 | * @param $fileMimetype |
||
| 1645 | * @author Jack Mason |
||
| 1646 | * @website volunteer @ http://www.osipage.com, web access application and bookmarking tool. |
||
| 1647 | * @copyright Free script, use anywhere as you like, no attribution required |
||
| 1648 | * @created 2014 |
||
| 1649 | * The script is capable of downloading really large files in PHP. Files greater than 2GB may fail in 32-bit windows or similar system. |
||
| 1650 | * All incorrect headers have been removed and no nonsense code remains in this script. Should work well. |
||
| 1651 | * The best and most recommended way to download files with PHP is using xsendfile, learn |
||
| 1652 | * more here: https://tn123.org/mod_xsendfile/ |
||
| 1653 | * |
||
| 1654 | */ |
||
| 1655 | public static function largeDownload($filePath, $fileMimetype) |
||
| 1656 | { |
||
| 1657 | /* You may need these ini settings too */ |
||
| 1658 | \set_time_limit(0); |
||
| 1659 | \ini_set('memory_limit', '512M'); |
||
| 1660 | if (!empty($filePath)) { |
||
| 1661 | $fileInfo = \pathinfo($filePath); |
||
| 1662 | $fileName = $fileInfo['basename']; |
||
| 1663 | $fileExtension = $fileInfo['extension']; |
||
| 1664 | $default_contentType = 'application/octet-stream'; |
||
| 1665 | // to find and use specific content type, check out this IANA page : http://www.iana.org/assignments/media-types/media-types.xhtml |
||
| 1666 | $contentType = $default_contentType; |
||
| 1667 | if ($fileMimetype = !'') { |
||
| 1668 | $contentType = $fileMimetype; |
||
| 1669 | } |
||
| 1670 | if (\is_file($filePath)) { |
||
| 1671 | $size = \filesize($filePath); |
||
| 1672 | $offset = 0; |
||
| 1673 | $length = $size; |
||
| 1674 | //HEADERS FOR PARTIAL DOWNLOAD FACILITY BEGINS |
||
| 1675 | if (Request::hasVar('HTTP_RANGE', 'SERVER')) { |
||
| 1676 | \preg_match('/bytes=(\d+)-(\d+)?/', $_SERVER['HTTP_RANGE'], $matches); |
||
| 1677 | $offset = (int)$matches[1]; |
||
| 1678 | $length = (int)$matches[2] - $offset; |
||
| 1679 | $fhandle = \fopen($filePath, 'rb'); |
||
| 1680 | \fseek($fhandle, $offset); // seek to the requested offset, this is 0 if it's not a partial content request |
||
| 1681 | $data = \fread($fhandle, $length); |
||
| 1682 | \fclose($fhandle); |
||
| 1683 | \header('HTTP/1.1 206 Partial Content'); |
||
| 1684 | \header('Content-Range: bytes ' . $offset . '-' . ($offset + $length) . '/' . $size); |
||
| 1685 | }//HEADERS FOR PARTIAL DOWNLOAD FACILITY BEGINS |
||
| 1686 | //USUAL HEADERS FOR DOWNLOAD |
||
| 1687 | \header('Content-Disposition: attachment;filename=' . $fileName); |
||
| 1688 | \header('Content-Type: ' . $contentType); |
||
| 1689 | \header('Accept-Ranges: bytes'); |
||
| 1690 | \header('Pragma: public'); |
||
| 1691 | \header('Expires: -1'); |
||
| 1692 | \header('Cache-Control: no-cache'); |
||
| 1693 | \header('Cache-Control: public, must-revalidate, post-check=0, pre-check=0'); |
||
| 1694 | \header('Content-Length: ' . \filesize($filePath)); |
||
| 1695 | $chunksize = 8 * (1024 * 1024); //8MB (highest possible fread length) |
||
| 1696 | if ($size > $chunksize) { |
||
| 1697 | $handle = \fopen($_FILES['file']['tmp_name'], 'rb'); |
||
| 1698 | $buffer = ''; |
||
| 1699 | while (!\feof($handle) && (\CONNECTION_NORMAL === \connection_status())) { |
||
| 1700 | $buffer = \fread($handle, $chunksize); |
||
| 1701 | print $buffer; |
||
| 1702 | \ob_flush(); |
||
| 1703 | \flush(); |
||
| 1704 | } |
||
| 1705 | if (\CONNECTION_NORMAL !== \connection_status()) { |
||
| 1706 | //TODO traslation |
||
| 1707 | echo 'Connection aborted'; |
||
| 1708 | } |
||
| 1709 | \fclose($handle); |
||
| 1710 | } else { |
||
| 1711 | \ob_clean(); |
||
| 1712 | \flush(); |
||
| 1713 | \readfile($filePath); |
||
| 1714 | } |
||
| 1715 | } else { |
||
| 1716 | //TODO traslation |
||
| 1717 | echo 'File does not exist!'; |
||
| 1718 | } |
||
| 1719 | } else { |
||
| 1720 | //TODO traslation |
||
| 1721 | echo 'There is no file to download!'; |
||
| 1722 | } |
||
| 1723 | } |
||
| 1724 | |||
| 1725 | /** |
||
| 1726 | * @param $selectedForumId |
||
| 1727 | * |
||
| 1728 | * @return int |
||
| 1729 | */ |
||
| 1730 | public static function getForum($selectedForumId) |
||
| 1731 | { |
||
| 1732 | $selectedForumId = (int)$selectedForumId; |
||
| 1733 | echo "<select name='forumid'>"; |
||
| 1734 | echo "<option value='0'>----------------------</option>"; |
||
| 1735 | $result = $GLOBALS['xoopsDB']->query('SELECT forum_name, forum_id FROM ' . $GLOBALS['xoopsDB']->prefix('bb_forums') . ' ORDER BY forum_id'); |
||
| 1736 | while (list($forumName, $forumId) = $GLOBALS['xoopsDB']->fetchRow($result)) { |
||
| 1737 | $optionSelected = ''; |
||
| 1738 | if ($forumId == $selectedForumId) { |
||
| 1739 | $optionSelected = "selected='selected'"; |
||
| 1740 | } |
||
| 1741 | echo "<option value='{$forumId}' {$optionSelected}>{$forumName}</option>"; |
||
| 1742 | } |
||
| 1743 | echo '</select></div>'; |
||
| 1744 | |||
| 1745 | return $selectedForumId; |
||
| 1746 | } |
||
| 1747 | |||
| 1748 | /** |
||
| 1749 | * @param $serverURL |
||
| 1750 | * |
||
| 1751 | * @return bool |
||
| 1752 | */ |
||
| 1753 | public static function mirrorOnline($serverURL) |
||
| 1754 | { |
||
| 1755 | $fp = @\fsockopen($serverURL, 80, $errno, $errstr, 5); |
||
| 1756 | if (!$fp) { |
||
| 1757 | $isOnline = false; |
||
| 1758 | } else { |
||
| 1759 | $isOnline = true; |
||
| 1760 | \fclose($fp); |
||
| 1761 | } |
||
| 1762 | |||
| 1763 | return $isOnline; |
||
| 1764 | } |
||
| 1765 | |||
| 1766 | /** |
||
| 1767 | * truncateHtml can truncate a string up to a number of characters while preserving whole words and HTML tags |
||
| 1768 | * www.gsdesign.ro/blog/cut-html-string-without-breaking-the-tags |
||
| 1769 | * www.cakephp.org |
||
| 1770 | * |
||
| 1771 | * @param string $text String to truncate. |
||
| 1772 | * @param int $length Length of returned string, including ellipsis. |
||
| 1773 | * @param string $ending Ending to be appended to the trimmed string. |
||
| 1774 | * @param bool $exact If false, $text will not be cut mid-word |
||
| 1775 | * @param bool $considerHtml If true, HTML tags would be handled correctly |
||
| 1776 | * |
||
| 1777 | * @return string Trimmed string. |
||
| 1778 | */ |
||
| 1779 | public static function truncateHtml($text, $length = 100, $ending = '...', $exact = false, $considerHtml = true) |
||
| 1780 | { |
||
| 1781 | if ($considerHtml) { |
||
| 1782 | // if the plain text is shorter than the maximum length, return the whole text |
||
| 1783 | if (mb_strlen(\preg_replace('/<.*?' . '>/', '', $text)) <= $length) { |
||
| 1784 | return $text; |
||
| 1785 | } |
||
| 1786 | // splits all html-tags to scanable lines |
||
| 1787 | \preg_match_all('/(<.+?' . '>)?([^<>]*)/s', $text, $lines, \PREG_SET_ORDER); |
||
| 1788 | $total_length = mb_strlen($ending); |
||
| 1789 | $open_tags = []; |
||
| 1790 | $truncate = ''; |
||
| 1791 | foreach ($lines as $line_matchings) { |
||
| 1792 | // if there is any html-tag in this line, handle it and add it (uncounted) to the output |
||
| 1793 | if (!empty($line_matchings[1])) { |
||
| 1794 | // if it's an "empty element" with or without xhtml-conform closing slash |
||
| 1795 | if (\preg_match('/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is', $line_matchings[1])) { |
||
| 1796 | // do nothing |
||
| 1797 | // if tag is a closing tag |
||
| 1798 | } elseif (\preg_match('/^<\s*\/([^\s]+?)\s*>$/s', $line_matchings[1], $tag_matchings)) { |
||
| 1799 | // delete tag from $open_tags list |
||
| 1800 | $pos = \array_search($tag_matchings[1], $open_tags, true); |
||
| 1801 | if (false !== $pos) { |
||
| 1802 | unset($open_tags[$pos]); |
||
| 1803 | } |
||
| 1804 | // if tag is an opening tag |
||
| 1805 | } elseif (\preg_match('/^<\s*([^\s>!]+).*?' . '>$/s', $line_matchings[1], $tag_matchings)) { |
||
| 1806 | // add tag to the beginning of $open_tags list |
||
| 1807 | \array_unshift($open_tags, mb_strtolower($tag_matchings[1])); |
||
| 1808 | } |
||
| 1809 | // add html-tag to $truncate'd text |
||
| 1810 | $truncate .= $line_matchings[1]; |
||
| 1811 | } |
||
| 1812 | // calculate the length of the plain text part of the line; handle entities as one character |
||
| 1813 | $content_length = mb_strlen(\preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', ' ', $line_matchings[2])); |
||
| 1814 | if ($total_length + $content_length > $length) { |
||
| 1815 | // the number of characters which are left |
||
| 1816 | $left = $length - $total_length; |
||
| 1817 | $entities_length = 0; |
||
| 1818 | // search for html entities |
||
| 1819 | if (\preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', $line_matchings[2], $entities, \PREG_OFFSET_CAPTURE)) { |
||
| 1820 | // calculate the real length of all entities in the legal range |
||
| 1821 | foreach ($entities[0] as $entity) { |
||
| 1822 | if ($left >= $entity[1] + 1 - $entities_length) { |
||
| 1823 | $left--; |
||
| 1824 | $entities_length += mb_strlen($entity[0]); |
||
| 1825 | } else { |
||
| 1826 | // no more characters left |
||
| 1827 | break; |
||
| 1828 | } |
||
| 1829 | } |
||
| 1830 | } |
||
| 1831 | $truncate .= mb_substr($line_matchings[2], 0, $left + $entities_length); |
||
| 1832 | // maximum lenght is reached, so get off the loop |
||
| 1833 | break; |
||
| 1834 | } |
||
| 1835 | $truncate .= $line_matchings[2]; |
||
| 1836 | $total_length += $content_length; |
||
| 1837 | |||
| 1838 | // if the maximum length is reached, get off the loop |
||
| 1839 | if ($total_length >= $length) { |
||
| 1840 | break; |
||
| 1841 | } |
||
| 1842 | } |
||
| 1843 | } else { |
||
| 1844 | if (mb_strlen($text) <= $length) { |
||
| 1845 | return $text; |
||
| 1846 | } |
||
| 1847 | $truncate = mb_substr($text, 0, $length - mb_strlen($ending)); |
||
| 1848 | } |
||
| 1849 | // if the words shouldn't be cut in the middle... |
||
| 1850 | if (!$exact) { |
||
| 1851 | // ...search the last occurance of a space... |
||
| 1852 | $spacepos = mb_strrpos($truncate, ' '); |
||
| 1853 | if (null !== $spacepos) { |
||
| 1854 | // ...and cut the text in this position |
||
| 1855 | $truncate = mb_substr($truncate, 0, $spacepos); |
||
| 1856 | } |
||
| 1857 | } |
||
| 1858 | // add the defined ending to the text |
||
| 1859 | $truncate .= $ending; |
||
| 1860 | if ($considerHtml) { |
||
| 1861 | // close all unclosed html-tags |
||
| 1862 | foreach ($open_tags as $tag) { |
||
| 1863 | $truncate .= '</' . $tag . '>'; |
||
| 1864 | } |
||
| 1865 | } |
||
| 1866 | |||
| 1867 | return $truncate; |
||
| 1868 | } |
||
| 1869 | |||
| 1870 | // Swish-e support EXPERIMENTAL |
||
| 1871 | |||
| 1872 | /** |
||
| 1873 | * php4swish-e 1.1, a web search interface for the swish-e search engine. |
||
| 1874 | * swish-e is a popular open-source search engine that runs on many platforms. |
||
| 1875 | * More information on swish-e is available at swish-e.org. |
||
| 1876 | * This code has been thoroughly tested and is ready for production |
||
| 1877 | * on any UNIX or Linux server that has the swish-e search engine installed. |
||
| 1878 | * You are free to modify this code as you see fit. |
||
| 1879 | * You must specify the path of swish-e ($swish) and |
||
| 1880 | * the location of the search index file ($swisheIndexFilePath). |
||
| 1881 | * You will also need to change the default index file served if it is not index.php. |
||
| 1882 | * If you want the meta description information to display completely, |
||
| 1883 | * be sure the <meta description... information is on *one* line for each web page. |
||
| 1884 | * If you wish to allow external search forms to call this script, be sure to set the |
||
| 1885 | * form's action attribute to whatever you name this file. |
||
| 1886 | * Suggestions for enhancements are welcome. |
||
| 1887 | */ |
||
| 1888 | public static function checkSwishe() |
||
| 1889 | { |
||
| 1890 | $helper = Helper::getInstance(); |
||
| 1891 | |||
| 1892 | // Get the location of the document repository (the index files are located in the root) |
||
| 1893 | $swisheDocPath = $helper->getConfig('uploaddir'); |
||
| 1894 | // Get the location of the SWISH-E executable |
||
| 1895 | $swisheExePath = $helper->getConfig('swishe_exe_path'); |
||
| 1896 | // check if _binfilter.sh exists |
||
| 1897 | if (!\is_file("{$swisheDocPath}/_binfilter.sh")) { |
||
| 1898 | return false; |
||
| 1899 | } |
||
| 1900 | // check if swish-e.conf exists |
||
| 1901 | if (!\is_file("{$swisheDocPath}/swish-e.conf")) { |
||
| 1902 | return false; |
||
| 1903 | } |
||
| 1904 | // check if swish-e.exe exists |
||
| 1905 | if (!\is_file("{$swisheExePath}/swish-e.exe")) { |
||
| 1906 | return false; |
||
| 1907 | } |
||
| 1908 | |||
| 1909 | return true; |
||
| 1910 | } |
||
| 1911 | |||
| 1912 | public static function swishe_config() |
||
| 1954 | } |
||
| 1955 | |||
| 1956 | /** |
||
| 1957 | * @param $swisheQueryWords |
||
| 1958 | * |
||
| 1959 | * @return array|bool |
||
| 1960 | */ |
||
| 1961 | public static function searchSwishe($swisheQueryWords) |
||
| 1962 | { |
||
| 1963 | /** |
||
| 1964 | * @param $str |
||
| 1965 | * @param $num_chars |
||
| 1966 | * @return string |
||
| 1967 | */ |
||
| 1968 | function strright($str, $num_chars) |
||
| 1973 | } |
||
| 1974 | |||
| 1975 | $helper = Helper::getInstance(); |
||
| 1976 | |||
| 1977 | $ret = false; |
||
| 1978 | // IN PROGRESS |
||
| 1979 | // IN PROGRESS |
||
| 1980 | // IN PROGRESS |
||
| 1981 | $swisheQueryWords = \stripslashes($swisheQueryWords); |
||
| 1982 | if (mb_strlen($swisheQueryWords) > 2) { |
||
| 1983 | //print "<BR>SEARCH!"; |
||
| 1984 | // Get the first word in $swisheQueryWords and use it for the $summary_query. |
||
| 1985 | // IN PROGRESS |
||
| 1986 | // IN PROGRESS |
||
| 1987 | // IN PROGRESS |
||
| 1988 | $summary_query = \str_replace('"', ' ', $swisheQueryWords); |
||
| 1989 | $summary_query = \trim($summary_query); |
||
| 1990 | $summary_query_e = \explode(' ', $summary_query); |
||
| 1991 | $summary_query = \trim($summary_query_e[0]); |
||
| 1992 | $summary_query = \rtrim($summary_query, '*'); |
||
| 1993 | |||
| 1994 | //print "<BR>SQ: ".$summary_query; |
||
| 1995 | |||
| 1996 | // Get the location of the document repository (the index files are located in the root) |
||
| 1997 | $swisheDocPath = $helper->getConfig('uploaddir'); |
||
| 1998 | $swisheDocPath_strlen = mb_strlen($helper->getConfig('swishe_doc_path')); |
||
| 1999 | |||
| 2000 | // Get the location of the SWISH-E executable |
||
| 2001 | $swisheExePath = $helper->getConfig('swishe_exe_path'); |
||
| 2002 | |||
| 2003 | // Get search query |
||
| 2004 | $swisheQueryWords = \escapeshellcmd($swisheQueryWords); // escape potentially malicious shell commands |
||
| 2005 | $swisheQueryWords = \stripslashes($swisheQueryWords); // remove backslashes from search query |
||
| 2006 | $swisheQueryWords = \preg_replace('#("|\')#', '', $swisheQueryWords); // remove quotes from search query |
||
| 2007 | $swisheCommand = "{$swisheExePath}/swish-e"; // path of swish-e command |
||
| 2008 | $swisheIndexFilePath = "{$swisheDocPath}/index.swish-e"; // path of swish-e index file |
||
| 2009 | $swisheSearchParams = ''; // Additional search parameters |
||
| 2010 | $swisheSearchParams .= '-H1'; // -H1 : print standard result header (default). |
||
| 2011 | if (0 != $helper->getConfig('swishe_search_limit')) { |
||
| 2012 | $swisheSearchParams .= " -m{$helper->getConfig('swishe_search_limit')}"; // -m *number* (max results) |
||
| 2013 | } |
||
| 2014 | |||
| 2015 | // Opens a pipe to swish-e |
||
| 2016 | $swishePipeHandler = \popen("{$swisheCommand} -w {$swisheQueryWords} -f {$swisheIndexFilePath} {$swisheSearchParams}", 'r'); |
||
| 2017 | if (!$swishePipeHandler) { |
||
| 2018 | exit('The search request generated an error...Please try again.'); |
||
| 2019 | } |
||
| 2020 | \trigger_error("{$swisheCommand} -w {$swisheQueryWords} -f {$swisheIndexFilePath} {$swisheSearchParams}"); |
||
| 2021 | |||
| 2022 | $line_cnt = 1; |
||
| 2023 | // loop through each line of the pipe result (i.e. swish-e output) to find hit number |
||
| 2024 | while (false !== ($nline = @\fgets($swishePipeHandler, 1024))) { |
||
| 2025 | if (4 == $line_cnt) { |
||
| 2026 | $num_line = $nline; |
||
| 2027 | break; // grab the 4th line, which contains the number of hits returned |
||
| 2028 | } |
||
| 2029 | ++$line_cnt; |
||
| 2030 | } |
||
| 2031 | |||
| 2032 | // strip out all but the number of hits |
||
| 2033 | //$num_results = preg_replace('/# Number of hits: /', '', $num_line); |
||
| 2034 | |||
| 2035 | //$table_header_flag = false; |
||
| 2036 | //$disp_nff_flag = true; |
||
| 2037 | |||
| 2038 | $ret = []; |
||
| 2039 | while (false !== ($line = @\fgets($swishePipeHandler, 4096))) { |
||
| 2040 | // loop through each line of the pipe result (i.e. swish-e output) |
||
| 2041 | if (\preg_match("/^(\d+)\s+(\S+)\s+\"(.*)\"\s+(\d+)/", $line)) { |
||
| 2042 | // Skip commented-out lines and the last line |
||
| 2043 | $line = \explode('"', $line); // split the string into an array by quotation marks |
||
| 2044 | $line[1] = \preg_replace('/[[:blank:]]/', '%%', $line[1]); // replace every space with %% for the phrase in quotation marks |
||
| 2045 | $line = \implode('"', $line); // collapse the array into a string |
||
| 2046 | $line = \preg_replace('/[[:blank:]]/', "\t", $line); // replace every space with a tab |
||
| 2047 | |||
| 2048 | [$relevance, $result_url, $result_title, $file_size] = \explode("\t", $line); // split the line into an array by tabs; assign variable names to each column |
||
| 2049 | $relevance /= 10; // format relevance as a percentage for search results |
||
| 2050 | $full_path_and_file = $result_url; |
||
| 2051 | $result_url = \trim(mb_substr($result_url, $swisheDocPath_strlen - 1, mb_strlen($result_url))); |
||
| 2052 | $file_path = strright($result_url, mb_strlen($result_url) - 2); |
||
| 2053 | // $file_path2 = substr($result_url, (strlen($result_url) - (strlen($result_url) - 2)),strlen($result_url)); |
||
| 2054 | $ret[] = [ |
||
| 2055 | 'relevance' => $relevance, |
||
| 2056 | 'result_url' => $result_url, |
||
| 2057 | 'result_title' => $result_title, |
||
| 2058 | 'file_size' => $file_size, |
||
| 2059 | 'file_path' => $file_path, |
||
| 2060 | ]; |
||
| 2061 | } |
||
| 2062 | } |
||
| 2063 | // close the shell pipe |
||
| 2064 | \pclose($swishePipeHandler); |
||
| 2065 | } |
||
| 2066 | |||
| 2067 | return $ret; |
||
| 2068 | } |
||
| 2069 | |||
| 2070 | // Swish-e support EXPERIMENTAL |
||
| 2071 | |||
| 2072 | // =========================================================== |
||
| 2073 | |||
| 2074 | |||
| 2075 | /** |
||
| 2076 | * @param $document |
||
| 2077 | * |
||
| 2078 | * @return mixed |
||
| 2079 | */ |
||
| 2080 | public static function convertHtml2text($document) |
||
| 2125 | } |
||
| 2126 | |||
| 2127 | /** |
||
| 2128 | * @param array|string $tableName |
||
| 2129 | * @param int $id_field |
||
| 2130 | * @param int $id |
||
| 2131 | * |
||
| 2132 | * @return mixed |
||
| 2133 | */ |
||
| 2134 | public static function cloneRecord($tableName, $id_field, $id) |
||
| 2135 | { |
||
| 2136 | $new_id = false; |
||
| 2137 | $table = $GLOBALS['xoopsDB']->prefix($tableName); |
||
| 2138 | // copy content of the record you wish to clone |
||
| 2139 | $tempTable = $GLOBALS['xoopsDB']->fetchArray($GLOBALS['xoopsDB']->query("SELECT * FROM $table WHERE $id_field='$id' "), MYSQLI_ASSOC) or exit('Could not select record'); |
||
| 2140 | // set the auto-incremented id's value to blank. |
||
| 2141 | unset($tempTable[$id_field]); |
||
| 2142 | // insert cloned copy of the original record |
||
| 2143 | $result = $GLOBALS['xoopsDB']->queryF("INSERT INTO $table (" . implode(', ', array_keys($tempTable)) . ") VALUES ('" . implode("', '", array_values($tempTable)) . "')") or exit ($GLOBALS['xoopsDB']->error()); |
||
| 2144 | |||
| 2145 | if ($result) { |
||
| 2146 | // Return the new id |
||
| 2147 | $new_id = $GLOBALS['xoopsDB']->getInsertId(); |
||
| 2148 | } |
||
| 2149 | return $new_id; |
||
| 2150 | } |
||
| 2151 | } |
||
| 2152 |