1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48:
49: class ModRewriteUrlStack {
50:
51: 52: 53: 54: 55:
56: private static $_instance;
57:
58: 59: 60: 61: 62:
63: private $_oDb;
64:
65: 66: 67: 68: 69:
70: private $_aUrls = array();
71:
72: 73: 74: 75: 76:
77: private $_aStack = array();
78:
79: 80: 81: 82: 83:
84: private $_aConParams = array(
85: 'idcat' => 1, 'idart' => 1, 'lang' => 1, 'idcatlang' => 1, 'idcatart' => 1, 'idartlang' => 1
86: );
87:
88: 89: 90: 91: 92:
93: private $_aTab;
94:
95: 96: 97: 98: 99:
100: private $_idLang;
101:
102: 103: 104:
105: private function __construct() {
106: global $cfg, $lang;
107: $this->_oDb = cRegistry::getDb();
108: $this->_aTab = $cfg['tab'];
109: $this->_idLang = $lang;
110: }
111:
112: 113: 114: 115: 116:
117: public static function getInstance() {
118: if (self::$_instance == NULL) {
119: self::$_instance = new ModRewriteUrlStack();
120: }
121: return self::$_instance;
122: }
123:
124: 125: 126: 127: 128:
129: public function add($url) {
130: $url = ModRewrite::urlPreClean($url);
131: if (isset($this->_aUrls[$url])) {
132: return;
133: }
134:
135: $aUrl = $this->_extractUrl($url);
136:
137:
138: foreach ($aUrl['params'] as $p => $v) {
139: if (!isset($this->_aConParams[$p])) {
140: unset($aUrl['params'][$p]);
141: } else {
142: $aUrl['params'][$p] = (int) $v;
143: }
144: }
145:
146:
147: if ((int) mr_arrayValue($aUrl['params'], 'lang') == 0) {
148: $aUrl['params']['lang'] = $this->_idLang;
149: }
150:
151: $sStackId = $this->_makeStackId($aUrl['params']);
152: $this->_aUrls[$url] = $sStackId;
153: $this->_aStack[$sStackId] = array('params' => $aUrl['params']);
154: }
155:
156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169:
170: public function getPrettyUrlParts($url) {
171: $url = ModRewrite::urlPreClean($url);
172: if (!isset($this->_aUrls[$url])) {
173: $this->add($url);
174: }
175:
176: $sStackId = $this->_aUrls[$url];
177: if (!isset($this->_aStack[$sStackId]['urlpath'])) {
178: $this->_chunkSetPrettyUrlParts($sStackId);
179: }
180: $aPretty = array(
181: 'urlpath' => $this->_aStack[$sStackId]['urlpath'],
182: 'urlname' => $this->_aStack[$sStackId]['urlname']
183: );
184: return $aPretty;
185: }
186:
187: 188: 189: 190: 191: 192: 193:
194: private function ($url) {
195: $aUrl = @parse_url($url);
196: if (isset($aUrl['query'])) {
197: $aUrl['query'] = str_replace('&', '&', $aUrl['query']);
198: parse_str($aUrl['query'], $aUrl['params']);
199: }
200: if (!isset($aUrl['params']) && !is_array($aUrl['params'])) {
201: $aUrl['params'] = array();
202: }
203: return $aUrl;
204: }
205:
206: 207: 208: 209: 210: 211: 212:
213: private function _makeStackId(array $aParams) {
214:
215: if ((int) mr_arrayValue($aParams, 'idart') > 0) {
216: $sStackId = 'idart_' . $aParams['idart'] . '_lang_' . $aParams['lang'];
217: } elseif ((int) mr_arrayValue($aParams, 'idartlang') > 0) {
218: $sStackId = 'idartlang_' . $aParams['idartlang'];
219: } elseif ((int) mr_arrayValue($aParams, 'idcatart') > 0) {
220: $sStackId = 'idcatart_' . $aParams['idcatart'] . '_lang_' . $aParams['lang'];
221: } elseif ((int) mr_arrayValue($aParams, 'idcat') > 0) {
222: $sStackId = 'idcat_' . $aParams['idcat'] . '_lang_' . $aParams['lang'];
223: } elseif ((int) mr_arrayValue($aParams, 'idcatlang') > 0) {
224: $sStackId = 'idcatlang_' . $aParams['idcatlang'];
225: } else {
226: $sStackId = 'lang_' . $aParams['lang'];
227: }
228: return $sStackId;
229: }
230:
231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241:
242: private function _chunkSetPrettyUrlParts($sStackId) {
243:
244: $aStack = array();
245: foreach ($this->_aStack as $stackId => $item) {
246: if (!isset($item['urlpath'])) {
247:
248: $aStack[$stackId] = $item;
249: }
250: }
251:
252:
253: $sWhere = '';
254: foreach ($aStack as $stackId => $item) {
255:
256: if ($stackId == $sStackId) {
257: $aP = $item['params'];
258: if ((int)mr_arrayValue($aP, 'idart') > 0) {
259: $sWhere .= '(al.idart = ' . $aP['idart'] . ' AND al.idlang = ' . $aP['lang'] . ') OR ';
260: } elseif ((int)mr_arrayValue($aP, 'idartlang') > 0) {
261: $sWhere .= '(al.idartlang = ' . $aP['idartlang'] . ') OR ';
262: } elseif ((int)mr_arrayValue($aP, 'idcat') > 0) {
263: $sWhere .= '(cl.idcat = ' . $aP['idcat'] . ' AND cl.idlang = ' . $aP['lang'] . ' AND cl.startidartlang = al.idartlang) OR ';
264: } elseif ((int)mr_arrayValue($aP, 'idcatart') > 0) {
265: $sWhere .= '(ca.idcatart = ' . $aP['idcatart'] . ' AND ca.idart = al.idart AND al.idlang = ' . $aP['lang'] . ') OR ';
266: } elseif ((int)mr_arrayValue($aP, 'idcatlang') > 0) {
267: $sWhere .= '(cl.idcatlang = ' . $aP['idcatlang'] . ' AND cl.startidartlang = al.idartlang) OR ';
268: }
269: }
270: }
271: if ($sWhere == '') {
272: return;
273: }
274: $sWhere = cString::getPartOfString($sWhere, 0, -4);
275: $sWhere = str_replace(' OR ', " OR \n", $sWhere);
276:
277:
278: $sql = <<<SQL
279: SELECT
280: al.idartlang, al.idart, al.idlang as lang, al.urlname, cl.idcatlang, cl.idcat,
281: cl.urlpath, ca.idcatart
282: FROM
283: {$this->_aTab['art_lang']} AS al, {$this->_aTab['cat_lang']} AS cl, {$this->_aTab['cat_art']} AS ca
284: WHERE
285: al.idart = ca.idart AND
286: ca.idcat = cl.idcat AND
287: al.idlang = cl.idlang AND
288: ($sWhere)
289: SQL;
290: ModRewriteDebugger::add($sql, 'ModRewriteUrlStack->_chunkSetPrettyUrlParts() $sql');
291:
292: $aNewStack = array();
293:
294:
295: $aFields = array('', 'idart', 'idartlang', 'idcatart', 'idcat');
296:
297: $this->_oDb->query($sql);
298: while ($this->_oDb->nextRecord()) {
299: $aRS = $this->_oDb->getRecord();
300:
301:
302: foreach ($aFields as $field) {
303: if (isset($aRS[$field])) {
304:
305: unset($aRS[$field]);
306: }
307: $rsStackID = $this->_makeStackId($aRS);
308: if (isset($aStack[$rsStackID])) {
309:
310: $aNewStack[$rsStackID]['urlpath'] = $aRS['urlpath'];
311: $aNewStack[$rsStackID]['urlname'] = $aRS['urlname'];
312: break;
313: }
314: }
315: }
316: ModRewriteDebugger::add($aNewStack, 'ModRewriteUrlStack->_chunkSetPrettyUrlParts() $aNewStack');
317:
318:
319: $this->_aStack = array_merge($this->_aStack, $aNewStack);
320: }
321:
322: }
323: