1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35:
36: class cUriBuilderMR extends cUriBuilder {
37:
38: 39: 40: 41: 42:
43: private static $_instance;
44:
45: 46: 47: 48: 49:
50: private $_sAmp = '&';
51:
52: 53: 54: 55: 56:
57: private $_bIsXHTML = false;
58:
59: 60: 61: 62: 63:
64: private $_bMREnabled = false;
65:
66: 67: 68: 69: 70:
71: private $_aMrCfg = null;
72:
73: 74: 75:
76: private function __construct() {
77: $this->sHttpBasePath = '';
78: if (ModRewrite::isEnabled()) {
79: $this->_aMrCfg = ModRewrite::getConfig();
80: $this->_bMREnabled = true;
81: $this->_bIsXHTML = (getEffectiveSetting('generator', 'xhtml', 'false') == 'false') ? false : true;
82: $this->_sAmp = ($this->_bIsXHTML) ? '&' : '&';
83: }
84: }
85:
86: 87: 88: 89: 90:
91: public static function getInstance() {
92: if (self::$_instance == null) {
93: self::$_instance = new self();
94: }
95: return self::$_instance;
96: }
97:
98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108:
109: public function buildUrl(array $params, $bUseAbsolutePath = false) {
110: ModRewriteDebugger::add($params, 'cUriBuilderMR::buildUrl() $params');
111: $urlDebug = array();
112: $urlDebug['in'] = $params;
113:
114: $url = self::_buildUrl($params);
115:
116: $urlPrefix = '';
117: if ($bUseAbsolutePath) {
118: $hmlPath = cRegistry::getFrontendUrl();
119: $aComp = parse_url($hmlPath);
120: $urlPrefix = $aComp['scheme'] . '://' . $aComp['host'];
121: if (mr_arrayValue($aComp, 'port', '') !== '') {
122: $urlPrefix .= ':' . $aComp['port'];
123: }
124: }
125:
126: $this->sUrl = $urlPrefix . $url;
127:
128: $urlDebug['out'] = $this->sUrl;
129: ModRewriteDebugger::add($urlDebug, 'cUriBuilderMR::buildUrl() in -> out');
130: }
131:
132: 133: 134: 135: 136: 137:
138: private function _buildUrl(array $aParams) {
139:
140: if (isset($aParams['changelang'])) {
141: $aParams['lang'] = $aParams['changelang'];
142: }
143:
144:
145: $sQuery = http_build_query($aParams);
146:
147:
148: $oMRUrlStack = ModRewriteUrlStack::getInstance();
149: $aPretty = $oMRUrlStack->getPrettyUrlParts('front_content.php?' . $sQuery);
150:
151:
152: $sQuery = $this->_createUrlQueryPart($aParams);
153:
154:
155: $aParts = array();
156:
157:
158: if ($param = $this->_getClientParameter($aParams)) {
159: $aParts[] = $param;
160: }
161:
162:
163: if ($param = $this->_getLanguageParameter($aParams)) {
164: $aParts[] = $param;
165: }
166:
167:
168: $sPath = $this->_getPath($aPretty);
169: if ($sPath !== '') {
170: $aParts[] = $sPath;
171: }
172: $sPath = implode('/', $aParts) . '/';
173:
174:
175: $sArticle = $this->_getArticleName($aPretty, $aParams);
176:
177: if ($sArticle !== '') {
178: $sFileExt = $this->_aMrCfg['file_extension'];
179: } else {
180: $sFileExt = '';
181: }
182:
183: $sPathAndArticle = $sPath . $sArticle . $sFileExt;
184: if ($this->_aMrCfg['use_lowercase_uri'] == 1) {
185:
186: $sPathAndArticle = strtolower($sPathAndArticle);
187: }
188:
189:
190: $sUrl = $sPathAndArticle . $sQuery;
191:
192:
193: $sUrl = mr_removeMultipleChars('/', $sUrl);
194: if (substr($sUrl, -2) == '?=') {
195: $sUrl = substr_replace($sUrl, '', -2);
196: }
197:
198:
199: $sUrl = ModRewriteUrlUtil::getInstance()->toModRewriteUrl($sUrl);
200:
201: return mr_removeMultipleChars('/', $this->_aMrCfg['rootdir'] . $sUrl);
202: }
203:
204: 205: 206: 207: 208: 209: 210: 211:
212: private function _createUrlQueryPart(array $aArgs) {
213:
214:
215: $aIgnoredParams = array(
216: 'idcat', 'idart', 'lang', 'client', 'idcatart', 'idartlang'
217: );
218: if ($this->_aMrCfg['use_language'] == 1) {
219: $aIgnoredParams[] = 'changelang';
220: }
221: if ($this->_aMrCfg['use_client'] == 1) {
222: $aIgnoredParams[] = 'changeclient';
223: }
224:
225:
226: $sQuery = '';
227: foreach ($aArgs as $p => $v) {
228: if (!in_array($p, $aIgnoredParams)) {
229:
230:
231: $p = urlencode(urldecode($p));
232: if (is_array($v)) {
233:
234: foreach ($v as $p2 => $v2) {
235: $p2 = urlencode(urldecode($p2));
236: $v2 = urlencode(urldecode($v2));
237: $sQuery .= $p . '[' . $p2 . ']=' . $v2 . $this->_sAmp;
238: }
239: } else {
240: $v = urlencode(urldecode($v));
241: $sQuery .= $p . '=' . $v . $this->_sAmp;
242: }
243: }
244: }
245: if (strlen($sQuery) > 0) {
246: $sQuery = '?' . substr($sQuery, 0, -strlen($this->_sAmp));
247: }
248: return $sQuery;
249: }
250:
251: 252: 253: 254: 255: 256:
257: private function _getClientParameter(array $aArgs) {
258: global $client;
259:
260:
261: if ($this->_aMrCfg['use_client'] == 1) {
262: $iChangeClient = (isset($aArgs['changeclient'])) ? (int) $aArgs['changeclient'] : 0;
263: $idclient = ($iChangeClient > 0) ? $iChangeClient : $client;
264: if ($this->_aMrCfg['use_client_name'] == 1) {
265: return urlencode(ModRewrite::getClientName($idclient));
266: } else {
267: return $idclient;
268: }
269: }
270: return null;
271: }
272:
273: 274: 275: 276: 277: 278:
279: private function _getLanguageParameter(array $aArgs) {
280: global $lang;
281:
282:
283: if ($this->_aMrCfg['use_language'] == 1) {
284: $iChangeLang = (isset($aArgs['changelang'])) ? (int) $aArgs['changelang'] : 0;
285: $idlang = ($iChangeLang > 0) ? $iChangeLang : $lang;
286: if ($this->_aMrCfg['use_language_name'] == 1) {
287: return urlencode(ModRewrite::getLanguageName($idlang));
288: } else {
289: return $idlang;
290: }
291: }
292: return null;
293: }
294:
295: 296: 297: 298: 299: 300:
301: private function _getPath(array $aPretty) {
302: $sPath = (isset($aPretty['urlpath'])) ? $aPretty['urlpath'] : '';
303:
304:
305: if ($this->_aMrCfg['startfromroot'] == 0 && (strlen($sPath) > 0)) {
306:
307: $aCategories = explode('/', $sPath);
308:
309:
310: array_shift($aCategories);
311:
312:
313: $sPath = implode('/', $aCategories);
314: }
315:
316: return $sPath;
317: }
318:
319: 320: 321: 322: 323: 324: 325:
326: private function _getArticleName(array $aPretty, array $aArgs) {
327: $sArticle = (isset($aPretty['urlname'])) ? $aPretty['urlname'] : '';
328:
329: $iIdCat = (isset($aArgs['idcat'])) ? (int) $aArgs['idcat'] : 0;
330: $iIdCatLang = (isset($aArgs['idcatlang'])) ? (int) $aArgs['idcatlang'] : 0;
331: $iIdCatArt = (isset($aArgs['idcatart'])) ? (int) $aArgs['idcatart'] : 0;
332: $iIdArt = (isset($aArgs['idart'])) ? (int) $aArgs['idart'] : 0;
333: $iIdArtLang = (isset($aArgs['idartlang'])) ? (int) $aArgs['idartlang'] : 0;
334:
335:
336: if (($iIdCat > 0 || $iIdCatLang > 0) && $iIdCatArt == 0 && $iIdArt == 0 && $iIdArtLang == 0) {
337: $sArticle = '';
338: if ($this->_aMrCfg['add_startart_name_to_url']) {
339: if ($this->_aMrCfg['default_startart_name'] !== '') {
340:
341: $sArticle = $this->_aMrCfg['default_startart_name'];
342: } else {
343: $sArticle = (isset($aPretty['urlname'])) ? $aPretty['urlname'] : '';
344: }
345: } else {
346:
347: $sArticle = '';
348: }
349: }
350:
351: return $sArticle;
352: }
353:
354: }
355: