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