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