mambax7 /
publisher
| 1 | <?php |
||
| 2 | |||
| 3 | namespace XoopsModules\Publisher; |
||
| 4 | |||
| 5 | /* |
||
| 6 | * $Id |
||
| 7 | * Module: Publisher |
||
| 8 | * Author: Sudhaker Raj <http://xoops.biz> |
||
| 9 | * Licence: GNU |
||
| 10 | */ |
||
| 11 | |||
| 12 | // define PUBLISHER_SEO_ENABLED in mainfile.php, possible values |
||
| 13 | // are "rewrite" & "path-info" |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Create a title for the shortUrl field of an article |
||
| 17 | * |
||
| 18 | * @credit psylove |
||
| 19 | * |
||
| 20 | * @var string $title title of the article |
||
| 21 | * @var string $withExt do we add an html extension or not |
||
| 22 | * @return string sort_url for the article |
||
| 23 | */ |
||
| 24 | |||
| 25 | use XoopsModules\Publisher; |
||
| 26 | |||
| 27 | // defined('XOOPS_ROOT_PATH') || die('Restricted access'); |
||
| 28 | |||
| 29 | require_once dirname(__DIR__) . '/include/common.php'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Class Seo |
||
| 33 | */ |
||
| 34 | class Seo |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @param string $title |
||
| 38 | * @param bool $withExt |
||
| 39 | * |
||
| 40 | * @return mixed|string |
||
| 41 | */ |
||
| 42 | public static function getTitle($title = '', $withExt = true) |
||
| 43 | { |
||
| 44 | |||
| 45 | /** |
||
| 46 | * if XOOPS ML is present, let's sanitize the title with the current language |
||
| 47 | */ |
||
| 48 | $myts = \MyTextSanitizer::getInstance(); |
||
| 49 | if (method_exists($myts, 'formatForML')) { |
||
| 50 | $title = $myts->formatForML($title); |
||
| 51 | } |
||
| 52 | |||
| 53 | // Transformation de la chaine en minuscule |
||
| 54 | // Codage de la chaine afin d'éviter les erreurs 500 en cas de caractères imprévus |
||
| 55 | $title = rawurlencode(mb_strtolower($title)); |
||
| 56 | |||
| 57 | // Transformation des ponctuations |
||
| 58 | $pattern = [ |
||
| 59 | '/%09/', // Tab |
||
| 60 | '/%20/', // Space |
||
| 61 | '/%21/', // ! |
||
| 62 | '/%22/', // " |
||
| 63 | '/%23/', // # |
||
| 64 | '/%25/', // % |
||
| 65 | '/%26/', // & |
||
| 66 | '/%27/', // ' |
||
| 67 | '/%28/', // ( |
||
| 68 | '/%29/', // ) |
||
| 69 | '/%2C/', // , |
||
| 70 | '/%2F/', // / |
||
| 71 | '/%3A/', // : |
||
| 72 | '/%3B/', // ; |
||
| 73 | '/%3C/', // < |
||
| 74 | '/%3D/', // = |
||
| 75 | '/%3E/', // > |
||
| 76 | '/%3F/', // ? |
||
| 77 | '/%40/', // @ |
||
| 78 | '/%5B/', // [ |
||
| 79 | '/%5C/', // \ |
||
| 80 | '/%5D/', // ] |
||
| 81 | '/%5E/', // ^ |
||
| 82 | '/%7B/', // { |
||
| 83 | '/%7C/', // | |
||
| 84 | '/%7D/', // } |
||
| 85 | '/%7E/', // ~ |
||
| 86 | "/\./", // . |
||
| 87 | ]; |
||
| 88 | $repPattern = ['-', '-', '', '', '', '-100', '', '-', '', '', '', '-', '', '', '', '-', '', '', '-at-', '', '-', '', '-', '', '-', '', '-', '']; |
||
| 89 | $title = preg_replace($pattern, $repPattern, $title); |
||
| 90 | |||
| 91 | // Transformation des caractères accentués |
||
| 92 | // è é ê ë ç à â ä î ï ù ü û ô ö |
||
| 93 | $pattern = ['/%B0/', '/%E8/', '/%E9/', '/%EA/', '/%EB/', '/%E7/', '/%E0/', '/%E2/', '/%E4/', '/%EE/', '/%EF/', '/%F9/', '/%FC/', '/%FB/', '/%F4/', '/%F6/']; |
||
| 94 | $repPattern = ['-', 'e', 'e', 'e', 'e', 'c', 'a', 'a', 'a', 'i', 'i', 'u', 'u', 'u', 'o', 'o']; |
||
| 95 | $title = preg_replace($pattern, $repPattern, $title); |
||
| 96 | |||
| 97 | if (count($title) > 0) { |
||
| 98 | if ($withExt) { |
||
| 99 | $title .= '.html'; |
||
| 100 | } |
||
| 101 | |||
| 102 | return $title; |
||
| 103 | } |
||
| 104 | |||
| 105 | return ''; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param $op |
||
| 110 | * @param $id |
||
| 111 | * @param string $shortUrl |
||
| 112 | * |
||
| 113 | * @return string |
||
| 114 | */ |
||
| 115 | public static function generateUrl($op, $id, $shortUrl = '') |
||
| 116 | { |
||
| 117 | /** @var Publisher\Helper $helper */ |
||
| 118 | $helper = Publisher\Helper::getInstance(); |
||
| 119 | if ('none' !== $helper->getConfig('seo_url_rewrite')) { |
||
| 120 | if (!empty($shortUrl)) { |
||
| 121 | $shortUrl .= '.html'; |
||
| 122 | } |
||
| 123 | |||
| 124 | if ('htaccess' === $helper->getConfig('seo_url_rewrite')) { |
||
| 125 | // generate SEO url using htaccess |
||
| 126 | return XOOPS_URL . '/' . $helper->getConfig('seo_module_name') . ".${op}.${id}/${shortUrl}"; |
||
| 127 | } |
||
| 128 | |||
| 129 | if ('path-info' === $helper->getConfig('seo_url_rewrite')) { |
||
| 130 | // generate SEO url using path-info |
||
| 131 | return PUBLISHER_URL . "/index.php/${op}.${id}/${shortUrl}"; |
||
| 132 | } |
||
| 133 | exit('Unknown SEO method.'); |
||
|
0 ignored issues
–
show
|
|||
| 134 | } |
||
| 135 | // generate classic url |
||
| 136 | switch ($op) { |
||
| 137 | case 'category': |
||
| 138 | return PUBLISHER_URL . "/${op}.php?categoryid=${id}"; |
||
| 139 | case 'item': |
||
| 140 | case 'print': |
||
| 141 | return PUBLISHER_URL . "/${op}.php?itemid=${id}"; |
||
| 142 | default: |
||
| 143 | exit('Unknown SEO operation.'); |
||
| 144 | } |
||
| 145 | } |
||
| 146 | } |
||
| 147 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.