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