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: function prResolvePathViaURLNames($path) {
37: global $cfg, $lang, $client;
38:
39: $handle = startTiming('prResolvePathViaURLNames', array($path));
40:
41:
42: $db = cRegistry::getDb();
43: $categories = array();
44: $results = array();
45:
46:
47: $path = cString::toLowerCase(str_replace(' ', '', $path));
48: $pathresolveHeapcacheEnabled = cRegistry::getConfigValue('pathresolve_heapcache');
49:
50:
51: if ($pathresolveHeapcacheEnabled === true) {
52: $oPathresolveCacheColl = new cApiPathresolveCacheCollection();
53: $oPathresolveCache = $oPathresolveCacheColl->fetchLatestByPathAndLanguage($path, $lang);
54: if (is_object($oPathresolveCache)) {
55: if ($oPathresolveCache->isCacheTimeExpired()) {
56: $cacheIdcat = $oPathresolveCache->get('idcat');
57: $oPathresolveCacheColl->delete($oPathresolveCache->get('idpathresolvecache'));
58: return $cacheIdcat;
59: }
60: }
61: }
62:
63:
64:
65: $sql = "SELECT * FROM " . $cfg["tab"]["cat_tree"] . " AS A, " . $cfg["tab"]["cat"] . " AS B, " . $cfg["tab"]["cat_lang"] . " AS C WHERE A.idcat=B.idcat AND B.idcat=C.idcat AND C.idlang=" . (int) $lang . "
66: AND C.visible = 1 AND B.idclient=" . (int) $client . " ORDER BY A.idtree";
67: $db->query($sql);
68:
69: $catpath = array();
70: while ($db->nextRecord()) {
71: $cat_str = '';
72: prCreateURLNameLocationString($db->f('idcat'), '/', $cat_str, false, '', 0, 0, true, true);
73:
74:
75: $catpath[$db->f('idcat')] = $cat_str;
76: $catnames[$db->f('idcat')] = $db->f('name');
77: $catlevels[$db->f('idcat')] = $db->f('level');
78: }
79:
80:
81: $percent = 0;
82: foreach ($catpath as $key => $value) {
83: $value = cString::toLowerCase(str_replace(' ', '', $value));
84:
85: similar_text($value, $path, $percent);
86:
87: $firstpath = cString::findFirstPos($value, '/');
88:
89: if ($firstpath !== 0) {
90: $xpath = cString::getPartOfString($value, $firstpath);
91: $ypath = cString::getPartOfString($path, 0, cString::getStringLength($path) - 1);
92: if ($xpath == $ypath) {
93: $results[$key] = 100;
94: } else {
95: $results[$key] = $percent;
96: }
97: } else {
98: $results[$key] = $percent;
99: }
100: }
101:
102: arsort($results, SORT_NUMERIC);
103: reset($results);
104:
105: endAndLogTiming($handle);
106:
107: if ($pathresolveHeapcacheEnabled === true) {
108: $oPathresolveCacheColl = new cApiPathresolveCacheCollection();
109: $oPathresolveCache = $oPathresolveCacheColl->create($path, key($results), $lang, time());
110: }
111:
112: return (int) key($results);
113: }
114:
115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133:
134: function prResolvePathViaCategoryNames($path, &$iLangCheck) {
135: global $cfg, $lang, $client;
136:
137: $handle = startTiming('prResolvePathViaCategoryNames', array($path));
138:
139:
140: $db = cRegistry::getDb();
141: $categories = array();
142: $results = array();
143: $iLangCheckOrg = $iLangCheck;
144:
145:
146: if (preg_match('/^\/(.*)\/$/', $path, $aResults)) {
147: $aResult = explode('/', $aResults[1]);
148: } elseif (preg_match('/^\/(.*)$/', $path, $aResults)) {
149: $aResult = explode('/', $aResults[1]);
150: } else {
151: $aResults[1] = $path;
152: }
153:
154: $aResults[1] = cString::toLowerCase(preg_replace('/-/', ' ', $aResults[1]));
155:
156:
157: $aPathsToCompare = explode('/', $aResults[1]);
158: $iCountPath = count($aPathsToCompare);
159:
160:
161: $iLangCheck = 0;
162:
163:
164: $path = cString::toLowerCase(str_replace(' ', '', $path));
165:
166:
167:
168: $sql = "SELECT * FROM " . $cfg["tab"]["cat_tree"] . " AS A, " . $cfg["tab"]["cat"] . " AS B, " . $cfg["tab"]["cat_lang"] . " AS C WHERE A.idcat=B.idcat AND B.idcat=C.idcat
169: AND C.visible = 1 AND B.idclient= " . (int) $client . " ORDER BY A.idtree";
170: $db->query($sql);
171:
172: $catpath = array();
173: $arrLangMatches = array();
174:
175: while ($db->nextRecord()) {
176: $cat_str = '';
177: $aTemp = '';
178: $iFor = 0;
179: $bLang = false;
180:
181:
182: conCreateLocationString($db->f('idcat'), '/', $cat_str, false, '', 0, $db->f('idlang'));
183:
184: $catpath[$db->f('idcat')] = $cat_str;
185: $catnames[$db->f('idcat')] = $db->f('name');
186: $catlevels[$db->f('idcat')] = $db->f('level');
187:
188:
189: $aTemp = cString::toLowerCase($cat_str);
190: $aDBToCompare = explode('/', $aTemp);
191: $iCountDB = count($aDBToCompare);
192: $iCountDBFor = $iCountDB - 1;
193:
194: ($iCountDB > $iCountPath) ? $iFor = $iCountPath : $iFor = $iCountDB;
195: $iCountM = $iFor - 1;
196:
197: for ($i = 0; $i < $iFor; $i++) {
198: if ($aPathsToCompare[$iCountM] == $aDBToCompare[$iCountDBFor]) {
199: $bLang = true;
200: } else {
201: $bLang = false;
202: }
203: $iCountM--;
204: $iCountDBFor--;
205:
206: if ($i == $iFor - 1 && $bLang) {
207: $iLangCheck = $db->f('idlang');
208: $arrLangMatches[] = $iLangCheck;
209: }
210: }
211: }
212:
213:
214: if ($iLangCheckOrg == 0) {
215: if (in_array($lang, $arrLangMatches)) {
216: $iLangCheck = $lang;
217: }
218: }
219:
220:
221: $percent = 0;
222: foreach ($catpath as $key => $value) {
223: $value = cString::toLowerCase(str_replace(' ', '', $value));
224: similar_text($value, $path, $percent);
225: $results[$key] = $percent;
226: }
227:
228: foreach ($catnames as $key => $value) {
229: $value = cString::toLowerCase(str_replace(' ', '', $value));
230: similar_text($value, $path, $percent);
231:
232:
233: $percent = $percent * $catlevels[$key];
234:
235: if ($results[$key] > $percent) {
236: $results[$key] = $percent;
237: }
238: }
239:
240: arsort($results, SORT_NUMERIC);
241: reset($results);
242:
243: endAndLogTiming($handle);
244: return (int) key($results);
245: }
246:
247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271:
272: function prCreateURLNameLocationString($idcat, $seperator, & $cat_str, $makeLink = false, $linkClass = '', $firstTreeElementToUse = 0, $uselang = 0, $final = true, $usecache = false) {
273: global $cfg, $client, $cfgClient, $lang, $sess, $_URLlocationStringCache;
274:
275: if ($final == true) {
276: $cat_str = '';
277: }
278:
279: if ($idcat == 0) {
280: $cat_str = i18n("Lost and found");
281: return;
282: }
283:
284: if ($uselang == 0) {
285: $uselang = $lang;
286: }
287:
288: if ($final == true && $usecache == true) {
289: if (!is_array($_URLlocationStringCache)) {
290: $_URLlocationStringCache = prGetCacheFileContent($client, $uselang);
291: }
292:
293: if (array_key_exists($idcat, $_URLlocationStringCache)) {
294: if ($_URLlocationStringCache[$idcat]['expires'] > time()) {
295: $cat_str = $_URLlocationStringCache[$idcat]['name'];
296: return;
297: }
298: }
299: }
300:
301: $db = cRegistry::getDb();
302:
303: $sql = "SELECT
304: a.urlname AS urlname,
305: a.name AS name,
306: a.idcat AS idcat,
307: b.parentid AS parentid,
308: c.level as level,
309: d.idtpl as idtpl
310: FROM
311: " . $cfg["tab"]["cat_lang"] . " AS a
312: LEFT JOIN " . $cfg["tab"]["tpl_conf"] . " AS d on a.idtplcfg = d.idtplcfg,
313: " . $cfg["tab"]["cat"] . " AS b,
314: " . $cfg["tab"]["cat_tree"] . " AS c
315: WHERE
316: a.idlang = " . (int) $uselang . " AND
317: b.idclient = " . (int) $client . " AND
318: b.idcat = " . (int) $idcat . " AND
319: a.idcat = b.idcat AND
320: c.idcat = b.idcat";
321:
322: $db->query($sql);
323: $db->nextRecord();
324:
325: if ($db->f('level') >= $firstTreeElementToUse) {
326: $name = $db->f('urlname');
327:
328: if (trim($name) == '') {
329: $name = $db->f('name');
330: }
331:
332: $parentid = $db->f('parentid');
333: $idtpl = (int) $db->f('idtpl');
334:
335:
336:
337: if ($makeLink == true) {
338: $linkUrl = $sess->url("front_content.php?idcat=$idcat&idtpl=$idtpl");
339: $name = '<a href="' . $linkUrl . '" class="' . $linkClass . '">' . $name . '</a>';
340: }
341:
342: $tmp_cat_str = $name . $seperator . $cat_str;
343: $cat_str = $tmp_cat_str;
344: }
345:
346: if ($parentid != 0) {
347: prCreateURLNameLocationString($parentid, $seperator, $cat_str, $makeLink, $linkClass, $firstTreeElementToUse, $uselang, false, $usecache);
348: } else {
349: $sep_length = cString::getStringLength($seperator);
350: $str_length = cString::getStringLength($cat_str);
351: $tmp_length = $str_length - $sep_length;
352: $cat_str = cString::getPartOfString($cat_str, 0, $tmp_length);
353: }
354:
355: if ($final == true && $usecache == true) {
356: $_URLlocationStringCache[$idcat]['name'] = $cat_str;
357: $_URLlocationStringCache[$idcat]['expires'] = time() + 3600;
358:
359: prWriteCacheFileContent($_URLlocationStringCache, $client, $uselang);
360: }
361: }
362:
363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375:
376: function prWriteCacheFileContent($data, $client, $lang) {
377: global $cfgClient;
378:
379: $path = $cfgClient[$client]['cache']['path'];
380: $filename = "locationstring-url-cache-$lang.txt";
381:
382: $res = false;
383: if (is_writable($path)) {
384: $res = cFileHandler::write($path . $filename, serialize($data));
385: }
386:
387: return ($res) ? true : false;
388: }
389:
390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401:
402: function prGetCacheFileContent($client, $lang) {
403: global $cfgClient;
404:
405: $path = $cfgClient[$client]['cache']['path'];
406: $filename = "locationstring-url-cache-$lang.txt";
407:
408: if (cFileHandler::exists($path . $filename)) {
409: $data = unserialize(cFileHandler::read($path . $filename));
410: } else {
411: $data = array();
412: }
413:
414: return (is_array($data)) ? $data : array();
415: }
416:
417: 418: 419: 420: 421: 422: 423: 424:
425: function prDeleteCacheFileContent($client, $lang) {
426: global $cfgClient;
427:
428: $path = $cfgClient[$client]['cache']['path'];
429: $filename = "locationstring-url-cache-$lang.txt";
430:
431: $res = false;
432: if (is_writable($path . $filename)) {
433: $res = @unlink($path . $filename);
434: }
435:
436: return ($res) ? true : false;
437: }
438: