Projekt

Allgemein

Profil

GX-Bug #60710 » GMSEOBoost.php

Moritz Bunjes, 09.01.2019 19:33

 
1
<?php
2
/* --------------------------------------------------------------
3
   GMSEOBoost.php 2019-01-09
4
   Gambio GmbH
5
   http://www.gambio.de
6
   Copyright (c) 2019 Gambio GmbH
7
   Released under the GNU General Public License (Version 2)
8
   [http://www.gnu.org/licenses/gpl-2.0.html]
9
   --------------------------------------------------------------
10

    
11

    
12
   based on:
13
   (c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
14
   (c) 2002-2003 osCommerce(boxes.php,v 1.32 2003/05/27); www.oscommerce.com
15
   (c) 2003	 nextcommerce (boxes.php,v 1.11 2003/08/13); www.nextcommerce.org
16
   (c) 2003 XT-Commerce - community made shopping http://www.xt-commerce.com ($Id: boxes.php 899 2005-04-29 02:40:57Z hhgag $)
17

    
18
   Released under the GNU General Public License
19
   ---------------------------------------------------------------------------------------*/
20

    
21
/**
22
 * Class GMSEOBoost
23
 */
24
class GMSEOBoost_ORIGIN implements UrlKeywordsRepairerInterface
25
{
26
	public $boost_products   = false;
27
	public $boost_categories = false;
28
	public $boost_content    = false;
29
	
30
	/**
31
	 * @var LanguageProviderInterface
32
	 */
33
	protected $languageProvider;
34
	
35
	/**
36
	 * @var DataCache
37
	 */
38
	protected $dataCache;
39
	
40
	/**
41
	 * @var array
42
	 */
43
	protected $seoBoostCache;
44
	
45
	/**
46
	 * @var CategoryReadServiceInterface $urlRewritesReader
47
	 */
48
	protected $categoryUrlRewritesReader;
49
	
50
	/**
51
	 * @var ProductReadServiceInterface $urlRewritesReader
52
	 */
53
	protected $productUrlRewritesReader;
54
	
55
	/**
56
	 * @var UrlRewriteStorage $urlRewritesReader
57
	 */
58
	protected $contentUrlRewritesReader;
59
	
60
	
61
	public static function &get_instance()
62
	{
63
		static $s_instance;
64
		
65
		if($s_instance === null)
66
		{
67
			$s_instance = MainFactory::create_object('GMSEOBoost');
68
		}
69
		
70
		return $s_instance;
71
	}
72
	
73
	
74
	public function __construct()
75
	{
76
		$this->boost_products   = gm_get_conf('GM_SEO_BOOST_PRODUCTS') === 'true';
77
		$this->boost_categories = gm_get_conf('GM_SEO_BOOST_CATEGORIES') === 'true';
78
		$this->boost_content    = gm_get_conf('GM_SEO_BOOST_CONTENT') === 'true';
79
	}
80
	
81
	
82
	public function get_current_boost_url()
83
	{
84
		$t_output_url  = '';
85
		$t_language_id = false;
86
		
87
		if(xtc_not_null($_GET['gm_boosted_product']))
88
		{
89
			$t_boosted_name = xtc_db_prepare_input($_GET['gm_boosted_product']);
90
			$t_products_id  = (int)$this->get_products_id_by_boost($t_boosted_name);
91
			if($t_products_id != 0)
92
			{
93
				$t_output_url = $this->get_boosted_product_url($t_products_id, $t_boosted_name, $t_language_id,
94
				                                               $_GET['gm_boosted_product']);
95
			}
96
		}
97
		
98
		if(xtc_not_null($_GET['gm_boosted_category']))
99
		{
100
			$t_boosted_name  = xtc_db_prepare_input($_GET['gm_boosted_category']);
101
			$t_categories_id = (int)$this->get_categories_id_by_boost($t_boosted_name);
102
			if($t_categories_id != 0)
103
			{
104
				$t_output_url = $this->get_boosted_category_url($t_categories_id, $t_language_id,
105
				                                                $_GET['gm_boosted_category']);
106
			}
107
		}
108
		
109
		return $t_output_url;
110
	}
111
	
112
	
113
	public function get_content_id_by_content_group($p_content_group, $p_languages_id = false)
114
	{
115
		$t_content_id = 0;
116
		
117
		$c_content_group = (int)$p_content_group;
118
		$c_languages_id  = (int)$p_languages_id;
119
		if($p_languages_id === false)
120
		{
121
			$c_languages_id = (int)$_SESSION['languages_id'];
122
		}
123
		
124
		$t_result = xtc_db_query("SELECT content_id
125
									FROM " . TABLE_CONTENT_MANAGER . "
126
									WHERE
127
										content_group = '" . $c_content_group . "' AND
128
										languages_id = '" . $c_languages_id . "'
129
									LIMIT 1");
130
		if(xtc_db_num_rows($t_result) == 1)
131
		{
132
			$t_result_array = xtc_db_fetch_array($t_result);
133
			$t_content_id   = (int)$t_result_array['content_id'];
134
		}
135
		
136
		return $t_content_id;
137
	}
138
	
139
	
140
	public function get_content_group_by_content_id($p_content_id)
141
	{
142
		$t_content_group = 0;
143
		$c_content_id    = (int)$p_content_id;
144
		
145
		$t_result = xtc_db_query("SELECT content_group
146
									FROM " . TABLE_CONTENT_MANAGER . "
147
									WHERE
148
										content_id = '" . $c_content_id . "'
149
									LIMIT 1");
150
		if(xtc_db_num_rows($t_result) == 1)
151
		{
152
			$t_result_array  = xtc_db_fetch_array($t_result);
153
			$t_content_group = (int)$t_result_array['content_group'];
154
		}
155
		
156
		return $t_content_group;
157
	}
158
	
159
	
160
	public function get_content_coID_by_boost($boosted_name, $language_id = false)
161
	{
162
		$coID = 0;
163
		
164
		$languageId = ($language_id !== false) ? (int)$language_id : (int)$_SESSION['languages_id'];
165
		if($languageId === 0)
166
		{
167
			$languageId = $this->_getDefaultLanguageId();
168
		}
169
		
170
		$cacheKey = 'co-id-' . $languageId . '-' . $boosted_name;
171
		
172
		if(array_key_exists($cacheKey, $this->_getSeoBoostCache()))
173
		{
174
			return $this->_getSeoBoostCache()[$cacheKey];
175
		}
176
		
177
		$urlRewrites = $this->_getContentUrlReader()->findByRewriteUrl(new NonEmptyStringType($boosted_name));
178
		
179
		if($urlRewrites->count())
180
		{
181
			/** @var UrlRewrite $urlRewrite */
182
			$urlRewrite = array_shift($urlRewrites->getArray());
183
			$coID       = $urlRewrite->getContentId();
184
		}
185
		else
186
		{
187
			if($language_id === false)
188
			{
189
				$boosted_name = basename($boosted_name);
190
				
191
				$language_id = (int)$_SESSION['languages_id'];
192
				
193
				$result = xtc_db_query('SELECT
194
										content_group,
195
										languages_id
196
									FROM content_manager
197
									WHERE
198
									content_position NOT LIKE "elements_%"	AND
199
									gm_url_keywords = ' . $this->v_binary_string . '"'
200
				                       . mysqli_real_escape_string($GLOBALS['db_link'], $boosted_name) . '"');
201
				while($result_array = xtc_db_fetch_array($result))
202
				{
203
					$coID = $result_array['content_group'];
204
					
205
					if($result_array['languages_id'] == $language_id)
206
					{
207
						break;
208
					}
209
				}
210
			}
211
			else
212
			{
213
				$result = xtc_db_query('SELECT content_group
214
									FROM content_manager
215
									WHERE
216
										gm_url_keywords = ' . $this->v_binary_string . '"'
217
				                       . ((isset($GLOBALS["___mysqli_ston"])
218
				                           && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"],
219
				                                                                                                 $boosted_name) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.",
220
				                                                                                                                                  E_USER_ERROR)) ? "" : ""))
221
				                       . '" AND
222
										languages_id = "' . (int)$language_id . '"');
223
				if(mysqli_num_rows($result) > 0)
224
				{
225
					$coID = $this->_mysqlResult($result, 0, 'content_group');
226
				}
227
			}
228
		}
229
		
230
		$this->_writeCache($cacheKey, $coID);
231
		
232
		return $coID;
233
	}
234
	
235
	
236
	public function get_products_id_by_boost($boosted_name, $language_id = false)
237
	{
238
		$products_id = 0;
239
		
240
		$boosted_name = ltrim($boosted_name, '/');
241
		
242
		$languageId = ($language_id !== false) ? (int)$language_id : (int)$_SESSION['languages_id'];
243
		if($languageId === 0)
244
		{
245
			$languageId = $this->_getDefaultLanguageId();
246
		}
247
		
248
		$cacheKey = 'p-id-' . $languageId . '-' . $boosted_name;
249
		
250
		if(array_key_exists($cacheKey, $this->_getSeoBoostCache()))
251
		{
252
			return $this->_getSeoBoostCache()[$cacheKey];
253
		}
254
		
255
		$urlRewrites = $this->_getProductUrlReader()
256
		                    ->findUrlRewritesByRewriteUrl(new NonEmptyStringType($boosted_name));
257
		
258
		if($urlRewrites->count())
259
		{
260
			/** @var UrlRewrite $urlRewrite */
261
			$urlRewrite  = array_shift($urlRewrites->getArray());
262
			$products_id = $urlRewrite->getContentId();
263
		}
264
		else
265
		{
266
			$boosted_name = basename($boosted_name);
267
			
268
			if($language_id === false)
269
			{
270
				$language_id = (int)$_SESSION['languages_id'];
271
				
272
				$result = xtc_db_query('SELECT
273
										products_id,
274
										language_id
275
									FROM products_description
276
									WHERE
277
										gm_url_keywords = ' . $this->v_binary_string . '"'
278
				                       . ((isset($GLOBALS["___mysqli_ston"])
279
				                           && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"],
280
				                                                                                                 $boosted_name) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.",
281
				                                                                                                                                  E_USER_ERROR)) ? "" : ""))
282
				                       . '"');
283
				while($result_array = xtc_db_fetch_array($result))
284
				{
285
					$products_id = $result_array['products_id'];
286
					
287
					if($result_array['language_id'] == $language_id)
288
					{
289
						break;
290
					}
291
				}
292
			}
293
			else
294
			{
295
				$result = xtc_db_query('SELECT products_id
296
									FROM products_description
297
									WHERE
298
										gm_url_keywords = ' . $this->v_binary_string . '"'
299
				                       . ((isset($GLOBALS["___mysqli_ston"])
300
				                           && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"],
301
				                                                                                                 $boosted_name) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.",
302
				                                                                                                                                  E_USER_ERROR)) ? "" : ""))
303
				                       . '" AND
304
										language_id = "' . (int)$language_id . '"');
305
				if(mysqli_num_rows($result) > 0)
306
				{
307
					$products_id = $this->_mysqlResult($result, 0, 'products_id');
308
				}
309
			}
310
		}
311
		
312
		$this->_writeCache($cacheKey, $products_id);
313
		
314
		return $products_id;
315
	}
316
	
317
	
318
	public function get_categories_id_by_boost($boosted_name, $language_id = false)
319
	{
320
		if(strlen($boosted_name) && substr($boosted_name, -1) === '/')
321
		{
322
			$boosted_name = substr($boosted_name, 0, -1);
323
		}
324
		
325
		$categories_id = 0;
326
		
327
		$languageId = ($language_id !== false) ? (int)$language_id : (int)$_SESSION['languages_id'];
328
		if($languageId === 0)
329
		{
330
			$languageId = $this->_getDefaultLanguageId();
331
		}
332
		
333
		$cacheKey = 'c-id-' . $languageId . '-' . $boosted_name;
334
		
335
		if(array_key_exists($cacheKey, $this->_getSeoBoostCache()))
336
		{
337
			return $this->_getSeoBoostCache()[$cacheKey];
338
		}
339
		
340
		$urlRewrites = $this->_getCategoryUrlReader()
341
		                    ->findUrlRewritesByRewriteUrl(new NonEmptyStringType($boosted_name));
342
		
343
		if($urlRewrites->count())
344
		{
345
			/** @var UrlRewrite $urlRewrite */
346
			$urlRewrite    = array_shift($urlRewrites->getArray());
347
			$categories_id = $urlRewrite->getContentId();
348
		}
349
		else
350
		{
351
			$boosted_name = basename($boosted_name);
352
			
353
			if($language_id === false)
354
			{
355
				$language_id = (int)$_SESSION['languages_id'];
356
				
357
				$result = xtc_db_query('SELECT
358
										categories_id,
359
										language_id
360
									FROM categories_description
361
									WHERE
362
										gm_url_keywords = ' . $this->v_binary_string . '"'
363
				                       . ((isset($GLOBALS["___mysqli_ston"])
364
				                           && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"],
365
				                                                                                                 $boosted_name) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.",
366
				                                                                                                                                  E_USER_ERROR)) ? "" : ""))
367
				                       . '"');
368
				while($result_array = xtc_db_fetch_array($result))
369
				{
370
					$categories_id = $result_array['categories_id'];
371
					
372
					if($result_array['language_id'] == $language_id)
373
					{
374
						break;
375
					}
376
				}
377
			}
378
			else
379
			{
380
				$result = xtc_db_query('SELECT categories_id
381
									FROM categories_description
382
									WHERE
383
										gm_url_keywords = ' . $this->v_binary_string . '"'
384
				                       . ((isset($GLOBALS["___mysqli_ston"])
385
				                           && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"],
386
				                                                                                                 $boosted_name) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.",
387
				                                                                                                                                  E_USER_ERROR)) ? "" : ""))
388
				                       . '" AND
389
										language_id = "' . (int)$language_id . '"');
390
				if(mysqli_num_rows($result) > 0)
391
				{
392
					$categories_id = $this->_mysqlResult($result, 0, 'categories_id');
393
				}
394
			}
395
		}
396
		
397
		$this->_writeCache($cacheKey, $categories_id);
398
		
399
		return $categories_id;
400
	}
401
	
402
	
403
	public function get_boosted_content_url($p_content_id, $p_language_id = false, $p_item_name = '')
404
	{
405
		$languageId = ($p_language_id !== false) ? (int)$p_language_id : (int)$_SESSION['languages_id'];
406
		if($languageId === 0)
407
		{
408
			$languageId = $this->_getDefaultLanguageId();
409
		}
410
		
411
		$cacheKey = 'co-url-' . $p_content_id . '-' . $languageId . '-' . $p_item_name;
412
		
413
		if(array_key_exists($cacheKey, $this->_getSeoBoostCache()))
414
		{
415
			return $this->_getSeoBoostCache()[$cacheKey];
416
		}
417
		
418
		$languageId = ($p_language_id !== false) ? (int)$p_language_id : (int)$_SESSION['languages_id'];
419
		if($languageId === 0)
420
		{
421
			$languageId = $this->_getDefaultLanguageId();
422
		}
423
		
424
		$t_content_group = $this->get_content_group_by_content_id((int)$p_content_id);
425
		
426
		if($this->_contentHasUrlRewrite($t_content_group, $languageId))
427
		{
428
			$urlRewrites = $this->_getContentUrlReader()->get(new IdType($t_content_group));
429
			
430
			if($urlRewrites->count())
431
			{
432
				$languageCode = $this->_getLanguageProvider()->getCodeById(new IdType($languageId))->asString();
433
				$urlRewrite   = $urlRewrites->keyExists($languageCode) ? $urlRewrites->getValue($languageCode) : array_shift($urlRewrites->getArray());
434
				
435
				if(gm_get_conf('USE_SEO_BOOST_LANGUAGE_CODE') == 'true')
436
				{
437
					$url = strtolower($languageCode) . '/info/' . $urlRewrite->getRewriteUrl() . '.html';
438
				}
439
				else
440
				{
441
					$url = 'info/' . $urlRewrite->getRewriteUrl() . '.html';
442
				}
443
				
444
				$this->_writeCache($cacheKey, $url);
445
				
446
				return $url;
447
			}	
448
		}
449
		
450
		$t_language_data_array = $this->get_language_data('content', $t_content_group, $p_language_id, $p_item_name);
451
		
452
		if(gm_get_conf('USE_SEO_BOOST_LANGUAGE_CODE') == 'true')
453
		{
454
			$t_language_data_array['code'] .= '/';
455
		}
456
		else
457
		{
458
			$t_language_data_array['code'] = '';
459
		}
460
		
461
		$result = xtc_db_query('SELECT
462
									content_title 	AS content_title,
463
									content_heading AS content_heading,
464
									gm_url_keywords AS gm_url_keywords
465
								FROM content_manager
466
								WHERE
467
									content_position NOT LIKE "elements_%"	AND
468
									content_id		= "' . (int)$p_content_id . '" AND
469
									languages_id 	= "' . (int)$t_language_data_array['language_id'] . '"');
470
		if(xtc_db_num_rows($result) == 0)
471
		{
472
			$this->_writeCache($cacheKey, false);
473
			
474
			return false;
475
		}
476
		
477
		$data = xtc_db_fetch_array($result);
478
		
479
		$link_name = $this->clean_name($data['gm_url_keywords']);
480
		$renewed   = false;
481
		
482
		if($link_name == '')
483
		{
484
			$link_name = $this->clean_keyword($data['content_heading']);
485
			$renewed   = true;
486
		}
487
		elseif($data['gm_url_keywords'] !== $link_name)
488
		{
489
			$renewed = true;
490
		}
491
		
492
		if($link_name == '')
493
		{
494
			$link_name = $this->clean_keyword($data['content_title']);
495
			$renewed   = true;
496
		}
497
		if($link_name == '')
498
		{
499
			$link_name = 'info-content-' . ((isset($GLOBALS["___mysqli_ston"])
500
			                                 && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"],
501
			                                                                                                       $p_content_id) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.",
502
			                                                                                                                                        E_USER_ERROR)) ? "" : ""));
503
			$renewed   = true;
504
		}
505
		
506
		if($renewed)
507
		{
508
			xtc_db_query('UPDATE content_manager
509
							SET	gm_url_keywords = "' . ((isset($GLOBALS["___mysqli_ston"])
510
			                                                && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"],
511
			                                                                                                                      $link_name) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.",
512
			                                                                                                                                                    E_USER_ERROR)) ? "" : ""))
513
			             . '"
514
							WHERE content_id = "' . (int)$p_content_id . '"');
515
			
516
			$this->repair('contents');
517
			
518
			$t_sql    = "SELECT gm_url_keywords
519
						FROM content_manager
520
						WHERE content_id = '" . (int)$p_content_id . "'";
521
			$t_result = xtc_db_query($t_sql);
522
			if(xtc_db_num_rows($t_result) == 1)
523
			{
524
				$t_result_array = xtc_db_fetch_array($t_result);
525
				$link_name      = $t_result_array['gm_url_keywords'];
526
			}
527
		}
528
		
529
		$link = $t_language_data_array['code'] . 'info/' . $link_name . '.html';
530
		
531
		$this->_writeCache($cacheKey, $link);
532
		
533
		return $link;
534
	}
535
	
536
	
537
	public function get_boosted_product_url($p_pID, $p_pName = '', $p_language_id = false, $p_url_keywords = '')
538
	{
539
		static $boostedUrls = [];
540
		
541
		$languageId = ($p_language_id !== false) ? (int)$p_language_id : (int)$_SESSION['languages_id'];
542
		if($languageId === 0)
543
		{
544
			$languageId = $this->_getDefaultLanguageId();
545
		}
546
		
547
		$cacheKey = 'p-url-' . $p_pID . '-' . $languageId . '-' . $p_pName . '-' . $p_url_keywords;
548
		
549
		if(array_key_exists($cacheKey, $boostedUrls))
550
		{
551
			return $boostedUrls[$cacheKey];
552
		}
553
		
554
		if(array_key_exists($cacheKey, $this->_getSeoBoostCache()))
555
		{
556
			return $this->_getSeoBoostCache()[$cacheKey];
557
		}
558
		
559
		if($this->_productHasUrlRewrite($p_pID, $languageId))
560
		{
561
			$urlRewrites = $this->_getProductUrlReader()->getRewriteUrls(new IdType((int)$p_pID));
562
			
563
			if($urlRewrites->count())
564
			{
565
				$languageCode = $this->_getLanguageProvider()->getCodeById(new IdType($languageId))->asString();
566
				$urlRewrite   = $urlRewrites->keyExists($languageCode) ? $urlRewrites->getValue($languageCode) : array_shift($urlRewrites->getArray());
567
				
568
				if(gm_get_conf('USE_SEO_BOOST_LANGUAGE_CODE') === 'true')
569
				{
570
					$boostedUrls[$cacheKey] = strtolower($languageCode) . '/' . $urlRewrite->getRewriteUrl() . '.html';
571
				}
572
				else
573
				{
574
					$boostedUrls[$cacheKey] = $urlRewrite->getRewriteUrl() . '.html';
575
				}
576
				
577
				$this->_writeCache($cacheKey, $boostedUrls[$cacheKey]);
578
				
579
				return $boostedUrls[$cacheKey];
580
			}
581
		}
582
		
583
		$t_language_data_array = $this->get_language_data('product', $p_pID, $p_language_id, $p_url_keywords);
584
		
585
		if(gm_get_conf('USE_SEO_BOOST_LANGUAGE_CODE') === 'true')
586
		{
587
			$t_language_data_array['code'] .= '/';
588
		}
589
		else
590
		{
591
			$t_language_data_array['code'] = '';
592
		}
593
		
594
		$p_pName = $this->get_coolerized_product_name($p_pID, $t_language_data_array['language_id']);
595
		$boostedUrls[$cacheKey]  = $t_language_data_array['code'];
596
		
597
		if(gm_get_conf('GM_SEO_BOOST_SHORT_URLS') === 'false')
598
		{
599
			$t_path = $this->get_product_path($p_pID, $t_language_data_array['language_id']);
600
			
601
			if($t_path != '')
602
			{
603
				$boostedUrls[$cacheKey] .= $t_path . '/';
604
			}
605
		}
606
		
607
		$boostedUrls[$cacheKey] .= $p_pName;
608
		$boostedUrls[$cacheKey] .= '.html';
609
		
610
		$this->_writeCache($cacheKey, $boostedUrls[$cacheKey]);
611
		
612
		return $boostedUrls[$cacheKey];
613
	}
614
	
615
	
616
	public function get_boosted_category_url($p_cID, $p_language_id = false, $p_item_name = '')
617
	{
618
		if(strlen($p_item_name) && substr($p_item_name, -1) === '/')
619
		{
620
			$p_item_name = substr($p_item_name, 0, -1);
621
		}
622
		
623
		$languageId = ($p_language_id !== false) ? (int)$p_language_id : (int)$_SESSION['languages_id'];
624
		if($languageId === 0)
625
		{
626
			$languageId = $this->_getDefaultLanguageId();
627
		}
628
		
629
		$cacheKey = 'c-url-' . $p_cID . '-' . $languageId . '-' . $p_item_name;
630
		
631
		if(array_key_exists($cacheKey, $this->_getSeoBoostCache()))
632
		{
633
			return $this->_getSeoBoostCache()[$cacheKey];
634
		}
635
		
636
		if($this->_categoryHasUrlRewrite($p_cID, $languageId))
637
		{
638
			$urlRewrites = $this->_getCategoryUrlReader()->getRewriteUrls(new IdType((int)$p_cID));
639
			
640
			if($urlRewrites->count())
641
			{
642
				$languageCode = $this->_getLanguageProvider()->getCodeById(new IdType($languageId))->asString();
643
				$urlRewrite   = $urlRewrites->keyExists($languageCode) ? $urlRewrites->getValue($languageCode) : array_shift($urlRewrites->getArray());
644
				
645
				if(gm_get_conf('USE_SEO_BOOST_LANGUAGE_CODE') === 'true')
646
				{
647
					$url = strtolower($languageCode) . '/' . $urlRewrite->getRewriteUrl() . '/';
648
				}
649
				else
650
				{
651
					$url = $urlRewrite->getRewriteUrl() . '/';
652
				}
653
				
654
				$this->_writeCache($cacheKey, $url);
655
				
656
				return $url;
657
			}
658
		}
659
		
660
		$t_language_data_array = $this->get_language_data('category', $p_cID, $p_language_id, $p_item_name);
661
		
662
		if(gm_get_conf('USE_SEO_BOOST_LANGUAGE_CODE') === 'true')
663
		{
664
			$t_language_data_array['code'] .= '/';
665
		}
666
		else
667
		{
668
			$t_language_data_array['code'] = '';
669
		}
670
		
671
		$t_link = $t_language_data_array['code'] . $this->get_full_categories_names($p_cID,
672
		                                                                            $t_language_data_array['language_id']);
673
		$t_link .= '/';
674
		
675
		$this->_writeCache($cacheKey, $t_link);
676
		
677
		return $t_link;
678
	}
679
	
680
	
681
	// $p_item_type: 'product' | 'category' | 'content'
682
	public function get_language_data($p_item_type, $p_item_id, $p_language_id = false, $p_item_name = '')
683
	{
684
		static $languageData = [];
685
		
686
		$key = $p_item_type . (int)$p_item_id . (int)$p_language_id . $p_item_name;
687
		
688
		if(array_key_exists($key, $languageData))
689
		{
690
			$GLOBALS['o_1'] += (microtime(true)-$t);
691
			return $languageData[$key];
692
		}
693
		
694
		if($p_item_name === '' && !empty($p_language_id))
695
		{
696
			$languageId = new IdType((int)$p_language_id);
697
			$languageData[$key] = $this->_getLanguageData($languageId);
698
			
699
			return $languageData[$key];
700
		}
701
		
702
		if(strlen($p_item_name) && substr($p_item_name, -1) === '/')
703
		{
704
			$p_item_name = substr($p_item_name, 0, -1);
705
		}
706
		
707
		$c_item_id   = (int)$p_item_id;
708
		$c_item_name = xtc_db_input($p_item_name);
709
		
710
		// URL Rewrites START
711
		if(($p_item_type === 'product' && $this->_productHasUrlRewrite($c_item_id, (int)$p_language_id))
712
		   || ($p_item_type === 'category' && $this->_categoryHasUrlRewrite($c_item_id, (int)$p_language_id))
713
		   || ($p_item_type === 'content' && $this->_contentHasUrlRewrite($c_item_id, (int)$p_language_id)))
714
		{
715
			switch($p_item_type)
716
			{
717
				case 'product':
718
					$urlRewritesReader = $this->_getProductUrlReader();
719
					break;
720
				
721
				case 'category':
722
					$urlRewritesReader = $this->_getCategoryUrlReader();
723
					break;
724
				
725
				case 'content':
726
					$urlRewritesReader = $this->_getContentUrlReader();
727
					break;
728
				
729
				default:
730
					$urlRewritesReader = $this->_getUrlRewriteReader($p_item_type);
731
			}
732
			
733
			if($p_item_type === 'product' || $p_item_type === 'category')
734
			{
735
				if(is_string($p_item_name) && $p_item_name !== '')
736
				{
737
					$urlRewrites = $urlRewritesReader->findUrlRewritesByRewriteUrl(new NonEmptyStringType($p_item_name));
738
				}
739
				else
740
				{
741
					$urlRewrites = $urlRewritesReader->getRewriteUrls(new IdType($c_item_id));
742
				}
743
			}
744
			elseif($p_item_type === 'content')
745
			{
746
				if(is_string($p_item_name) && $p_item_name !== '')
747
				{
748
					$urlRewrites = $urlRewritesReader->findByRewriteUrl(new NonEmptyStringType($p_item_name));
749
				}
750
				else
751
				{
752
					$urlRewrites = $urlRewritesReader->get(new IdType($c_item_id));
753
				}
754
			}
755
			
756
			if(isset($urlRewrites) && $urlRewrites->count())
757
			{
758
				$languageId = ($p_language_id !== false) ? (int)$p_language_id : (int)$_SESSION['languages_id'];
759
				if(!empty($languageId))
760
				{
761
					$languageId = new IdType($languageId);
762
				}
763
				else
764
				{
765
					$languageId = new IdType($this->_getLanguageProvider()->getDefaultLanguageId());
766
				}
767
				
768
				/** @var UrlRewrite $urlRewrite */
769
				foreach($urlRewrites as $urlRewrite)
770
				{
771
					if($languageId->asInt() === $urlRewrite->getLanguageId())
772
					{
773
						$languageData[$key] =& $this->_getLanguageData($languageId);
774
						
775
						return $languageData[$key];
776
					}
777
				}
778
				
779
				$urlRewrite = array_shift($urlRewrites->getArray());
780
				$languageId = new IdType($urlRewrite->getLanguageId());
781
				
782
				$languageData[$key] =& $this->_getLanguageData($languageId);
783
				
784
				return $languageData[$key];
785
			}
786
		}
787
		// URL Rewrites END
788
		
789
		$languageData[$key]         = [];
790
		$t_item_language_data_array = $this->get_language_item_data($p_item_type, $c_item_id, $c_item_name,
791
		                                                            $p_language_id);
792
		foreach($t_item_language_data_array as $t_current_language_data)
793
		{
794
			if($t_current_language_data['language_id'] == $_SESSION['languages_id'])
795
			{
796
				$languageData[$key] = $t_current_language_data;
797
			}
798
		}
799
		
800
		if(empty($languageData[$key]))
801
		{
802
			$languageData[$key] = array_shift($t_item_language_data_array);
803
		}
804
		
805
		
806
		return $languageData[$key];
807
	}
808
	
809
	
810
	// $p_item_type: 'product' | 'category' | 'content'
811
	public function get_language_item_data($p_item_type, $p_item_id, $p_item_name, $p_language_id = false)
812
	{
813
		switch($p_item_type)
814
		{
815
			case 'product':
816
				$t_table_name           = 'products_description';
817
				$t_item_id_name         = 'products_id';
818
				$t_language_id_name     = 'language_id';
819
				$t_gm_url_keywords_name = 'gm_url_keywords';
820
				break;
821
			case 'category':
822
				$t_table_name           = 'categories_description';
823
				$t_item_id_name         = 'categories_id';
824
				$t_language_id_name     = 'language_id';
825
				$t_gm_url_keywords_name = 'gm_url_keywords';
826
				break;
827
			case 'content':
828
				$t_table_name           = 'content_manager';
829
				$t_item_id_name         = 'content_group';
830
				$t_language_id_name     = 'languages_id';
831
				$t_gm_url_keywords_name = 'gm_url_keywords';
832
				break;
833
			default:
834
				return array();
835
		}
836
		
837
		if($p_language_id === false)
838
		{
839
			$t_sql_language_condition = '';
840
		}
841
		else
842
		{
843
			$t_sql_language_condition = ' AND item_table.' . $t_language_id_name . ' = "' . (int)$p_language_id . '"';
844
		}
845
		
846
		$c_item_name = $this->clean_name(basename($p_item_name));
847
		
848
		if(empty($c_item_name))
849
		{
850
			$t_sql_item_name_condition = '';
851
		}
852
		else
853
		{
854
			$t_sql_item_name_condition = ' AND item_table.' . $t_gm_url_keywords_name . ' LIKE "' . $c_item_name . '"';
855
		}
856
		
857
		$t_query  = 'SELECT
858
						l.languages_id AS language_id,
859
						l.code AS code,
860
						l.directory,
861
						l.language_charset
862
					FROM
863
						' . $t_table_name . ' item_table
864
					LEFT JOIN
865
						languages l ON (l.languages_id = item_table.' . $t_language_id_name . ')
866
					WHERE
867
						item_table.' . $t_item_id_name . ' = ' . $p_item_id . '
868
						' . $t_sql_language_condition . '
869
						' . $t_sql_item_name_condition . '
870
					ORDER BY
871
						l.sort_order';
872
		$t_result = xtc_db_query($t_query);
873
		
874
		$t_language_item_data_array = array();
875
		
876
		while($t_row = xtc_db_fetch_array($t_result))
877
		{
878
			$t_language_item_data_array[] = $t_row;
879
		}
880
		
881
		if(empty($t_language_item_data_array))
882
		{
883
			$t_language_item_data_array[] = array(
884
				'language_id'      => (int)$_SESSION['languages_id'],
885
				'code'             => $_SESSION['language_code'],
886
				'directory'        => $_SESSION['directory'],
887
				'language_charset' => $_SESSION['language_charset']
888
			);
889
		}
890
		
891
		return $t_language_item_data_array;
892
	}
893
	
894
	
895
	public function get_coolerized_product_name($pID, $language_id = false)
896
	{
897
		if($language_id === false)
898
		{
899
			$language_id = (int)$_SESSION['languages_id'];
900
		}
901
		
902
		$result = xtc_db_query('SELECT
903
									products_name,
904
									gm_url_keywords
905
								FROM products_description
906
								WHERE
907
									products_id = "' . (int)$pID . '" AND
908
									language_id = "' . (int)$language_id . '"');
909
		$data   = xtc_db_fetch_array($result);
910
		
911
		$link_name = $this->clean_name($data['gm_url_keywords']);
912
		$renewed   = false;
913
		
914
		if($link_name == '')
915
		{
916
			$link_name = $this->clean_keyword($data['products_name']);
917
			$renewed   = true;
918
		}
919
		elseif($data['gm_url_keywords'] !== $link_name)
920
		{
921
			$renewed = true;
922
		}
923
		
924
		if($renewed)
925
		{
926
			xtc_db_query('UPDATE products_description
927
							SET	gm_url_keywords = "' . ((isset($GLOBALS["___mysqli_ston"])
928
			                                                && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"],
929
			                                                                                                                      $link_name) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.",
930
			                                                                                                                                                    E_USER_ERROR)) ? "" : ""))
931
			             . '"
932
							WHERE
933
								products_id = "' . (int)$pID . '" AND
934
								language_id = "' . (int)$language_id . '"');
935
			
936
			$this->repair('products', (int)$pID);
937
			
938
			$t_sql    = "SELECT gm_url_keywords
939
						FROM products_description
940
						WHERE
941
							products_id = '" . (int)$pID . "' AND
942
							language_id = '" . (int)$language_id . "'";
943
			$t_result = xtc_db_query($t_sql);
944
			if(xtc_db_num_rows($t_result) == 1)
945
			{
946
				$t_result_array = xtc_db_fetch_array($t_result);
947
				$link_name      = $t_result_array['gm_url_keywords'];
948
			}
949
		}
950
		
951
		$pName = $this->clean_name($link_name);
952
		return $pName;
953
	}
954
	
955
	
956
	public function get_product_path($products_id, $languages_id = false)
957
	{
958
		if($languages_id === false)
959
		{
960
			$languages_id = (int)$_SESSION['languages_id'];
961
		}
962
		
963
		$out = '';
964
		
965
		$result = xtc_db_query("SELECT
966
									categories_id
967
								FROM
968
									products_to_categories AS p2c
969
								WHERE
970
									p2c.products_id = '" . (int)$products_id . "'
971
									AND p2c.categories_id != 0
972
								ORDER BY categories_id ASC
973
								LIMIT 1");
974
		if(xtc_db_num_rows($result) > 0)
975
		{
976
			$data = xtc_db_fetch_array($result);
977
			$out  = $this->get_full_categories_names($data['categories_id'], $languages_id);
978
		}
979
		
980
		return $out;
981
	}
982
	
983
	
984
	public function get_full_categories_names($categories_id, $languages_id = false)
985
	{
986
		if($languages_id === false)
987
		{
988
			$languages_id = (int)$_SESSION['languages_id'];
989
		}
990
		
991
		$result = xtc_db_query('SELECT
992
									c.parent_id AS parent_id,
993
									cd.categories_name 	AS categories_name,
994
									cd.gm_url_keywords 	AS gm_url_keywords
995
								FROM
996
									categories AS c
997
									LEFT JOIN categories_description AS cd USING (categories_id)
998
								WHERE
999
									cd.categories_id 	= "' . (int)$categories_id . '" AND
1000
									cd.language_id 		= "' . (int)$languages_id . '"');
1001
		$data   = xtc_db_fetch_array($result);
1002
		
1003
		$link_name = $this->clean_name($data['gm_url_keywords']);
1004
		$renewed   = false;
1005
		
1006
		if($link_name == '')
1007
		{
1008
			$link_name = $this->clean_keyword($data['categories_name']);
1009
			$renewed   = true;
1010
		}
1011
		elseif($data['gm_url_keywords'] !== $link_name)
1012
		{
1013
			$renewed = true;
1014
		}
1015
		
1016
		if($renewed)
1017
		{
1018
			xtc_db_query('UPDATE categories_description
1019
							SET	gm_url_keywords = "' . $link_name . '"
1020
							WHERE
1021
								categories_id 	= "' . (int)$categories_id . '" AND
1022
								language_id 	= "' . (int)$languages_id . '"');
1023
			
1024
			$this->repair('categories');
1025
			
1026
			$t_sql    = "SELECT gm_url_keywords
1027
						FROM categories_description
1028
						WHERE
1029
							categories_id = '" . (int)$categories_id . "' AND
1030
							language_id = '" . (int)$languages_id . "'";
1031
			$t_result = xtc_db_query($t_sql);
1032
			if(xtc_db_num_rows($t_result) == 1)
1033
			{
1034
				$t_result_array = xtc_db_fetch_array($t_result);
1035
				$link_name      = $t_result_array['gm_url_keywords'];
1036
			}
1037
		}
1038
		
1039
		if($link_name != '')
1040
		{
1041
			$out = $link_name;
1042
		}
1043
		
1044
		if($data['parent_id'] != 0)
1045
		{
1046
			$parent = $this->get_full_categories_names($data['parent_id'], $languages_id);
1047
			$out    = $parent . '/' . $out;
1048
		}
1049
		
1050
		return $out;
1051
	}
1052
	
1053
	
1054
	public function repair($p_type = 'all', $p_id = 0)
1055
	{
1056
		if($p_type == 'all' || $p_type == 'products')
1057
		{
1058
			if((int)$p_id  > 0)
1059
			{
1060
				$t_get_empty_keywords_ids = xtc_db_query("SELECT products_id
1061
															FROM " . TABLE_PRODUCTS_DESCRIPTION . "
1062
															WHERE
1063
															(gm_url_keywords = '' OR LENGTH(gm_url_keywords) >= 255)
1064
															AND products_id = '" . $p_id . "'
1065
															", 'db_link', false);
1066
				
1067
				$t_get_double_keywords_ids = xtc_db_query("SELECT COUNT(`gm_url_keywords`) AS `numberOfUrlKeywords`
1068
															FROM `products_description`
1069
															GROUP BY `gm_url_keywords`, `language_id`
1070
															HAVING `numberOfUrlKeywords` > 1
1071
															", 'db_link', false);
1072
			}
1073
			if((int)$p_id <= 0 || xtc_db_num_rows($t_get_empty_keywords_ids) > 0 || xtc_db_num_rows($t_get_double_keywords_ids) > 0)
1074
			{
1075
				
1076
				$t_get_languages_ids = xtc_db_query("SELECT languages_id
1077
													FROM " . TABLE_LANGUAGES . "", 'db_link', false);
1078
				while($t_result_array = xtc_db_fetch_array($t_get_languages_ids))
1079
				{
1080
					$c_languages_id = (int)$t_result_array['languages_id'];
1081
					
1082
					$t_get_empty_keywords = xtc_db_query("SELECT
1083
															products_id,
1084
															products_name
1085
														FROM " . TABLE_PRODUCTS_DESCRIPTION . "
1086
														WHERE
1087
															(gm_url_keywords = '' OR LENGTH(gm_url_keywords) >= 255)
1088
															AND language_id = '" . $c_languages_id . "'", 'db_link',
1089
					                                     false);
1090
					while($t_product_array = xtc_db_fetch_array($t_get_empty_keywords))
1091
					{
1092
						$c_cleaned_name = $this->clean_keyword($t_product_array['products_name']);
1093
						if(strlen_wrapper($c_cleaned_name . '-' . $t_product_array['products_id']) >= 255)
1094
						{
1095
							$c_cleaned_name = substr_wrapper($c_cleaned_name, 0, 100);
1096
						}
1097
						
1098
						if($c_cleaned_name != '')
1099
						{
1100
							$t_update = xtc_db_query("UPDATE " . TABLE_PRODUCTS_DESCRIPTION . "
1101
													SET gm_url_keywords = '" . $c_cleaned_name . "'
1102
													WHERE
1103
														products_id = '" . (int)$t_product_array['products_id'] . "'
1104
														AND language_id = '" . $c_languages_id . "'", 'db_link', false);
1105
						}
1106
						else
1107
						{
1108
							$t_update = xtc_db_query("UPDATE " . TABLE_PRODUCTS_DESCRIPTION . "
1109
													SET gm_url_keywords = 'product-"
1110
							                         . (int)$t_product_array['products_id'] . "'
1111
													WHERE
1112
														products_id = '" . (int)$t_product_array['products_id'] . "'
1113
														AND language_id = '" . $c_languages_id . "'", 'db_link', false);
1114
						}
1115
					}
1116
					
1117
					$t_found_double_keywords = true;
1118
					
1119
					while($t_found_double_keywords)
1120
					{
1121
						$t_products_array = [];
1122
						$t_found          = false;
1123
						
1124
						$t_get_double_keywords = xtc_db_query("SELECT DISTINCT
1125
																a.products_id,
1126
																a.gm_url_keywords
1127
															FROM " . TABLE_PRODUCTS_DESCRIPTION . " a
1128
															LEFT JOIN " . TABLE_PRODUCTS_DESCRIPTION . " AS b ON ("
1129
						                                      . $this->v_binary_string . "a.gm_url_keywords = "
1130
						                                      . $this->v_binary_string . "b.gm_url_keywords)
1131
															WHERE
1132
																a.products_id != b.products_id
1133
																AND a.language_id = '" . $c_languages_id . "'
1134
																AND b.language_id = '" . $c_languages_id . "'
1135
															ORDER BY a.products_id", 'db_link', false);
1136
						
1137
						while($t_result_array = xtc_db_fetch_array($t_get_double_keywords))
1138
						{
1139
							$t_found                                          = true;
1140
							$t_products_array[$t_result_array['products_id']] = $t_result_array['gm_url_keywords'];
1141
						}
1142
						
1143
						if(!$t_found)
1144
						{
1145
							$t_found_double_keywords = false;
1146
						}
1147
						else
1148
						{
1149
							$t_cleared_keywords_array = [];
1150
							
1151
							foreach($t_products_array AS $t_products_id => $t_gm_url_keywords)
1152
							{
1153
								$t_keys_array = [];
1154
								$t_keys_array = array_keys($t_products_array, $t_gm_url_keywords);
1155
								
1156
								for($i = 1; $i < count($t_keys_array); $i++)
1157
								{
1158
									if(!in_array($t_gm_url_keywords, $t_cleared_keywords_array))
1159
									{
1160
										$t_new_url_keyword = preg_replace('/(.+?)(-[0-9]+)$/', "$1",
1161
										                                  $t_gm_url_keywords);
1162
										$c_new_url_keyword = ((isset($GLOBALS["___mysqli_ston"]) && is_object($GLOBALS["___mysqli_ston"]))
1163
															? mysqli_real_escape_string($GLOBALS["___mysqli_ston"], $t_new_url_keyword)
1164
															: ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.",
1165
															                  E_USER_ERROR))
1166
																? ""
1167
																: ""));
1168
										
1169
										$t_sql_products_id = $t_keys_array[$i];
1170
										if($c_new_url_keyword . '-' . $t_keys_array[$i] == $t_gm_url_keywords)
1171
										{
1172
											$t_sql_products_id = $t_keys_array[0];
1173
										}
1174
										
1175
										$t_update = xtc_db_query("UPDATE " . TABLE_PRODUCTS_DESCRIPTION . "
1176
																SET gm_url_keywords = '" . $c_new_url_keyword . "-"
1177
										                         . (int)$t_sql_products_id . "'
1178
																WHERE
1179
																	products_id = '" . (int)$t_sql_products_id . "'
1180
																	AND language_id = '" . $c_languages_id . "'",
1181
										                         'db_link', false);
1182
									}
1183
								}
1184
								
1185
								$t_cleared_keywords_array[] = $t_gm_url_keywords;
1186
							}
1187
						}
1188
					}
1189
				}
1190
			}
1191
		}
1192
		
1193
		if($p_type == 'all' || $p_type == 'categories')
1194
		{
1195
			$t_get_languages_ids = xtc_db_query("SELECT languages_id
1196
													FROM " . TABLE_LANGUAGES . "", 'db_link', false);
1197
			while($t_result_array = xtc_db_fetch_array($t_get_languages_ids))
1198
			{
1199
				$c_languages_id = (int)$t_result_array['languages_id'];
1200
				
1201
				$t_get_empty_keywords = xtc_db_query("SELECT
1202
															categories_id,
1203
															categories_name
1204
														FROM " . TABLE_CATEGORIES_DESCRIPTION . "
1205
														WHERE
1206
															(gm_url_keywords = '' OR LENGTH(gm_url_keywords) >= 255)
1207
															AND language_id = '" . $c_languages_id . "'", 'db_link',
1208
				                                     false);
1209
				while($t_category_array = xtc_db_fetch_array($t_get_empty_keywords))
1210
				{
1211
					$c_cleaned_name = $this->clean_keyword($t_category_array['categories_name']);
1212
					if(strlen_wrapper($c_cleaned_name . '-' . $t_category_array['categories_id']) >= 255)
1213
					{
1214
						$c_cleaned_name = substr_wrapper($c_cleaned_name, 0, 100);
1215
					}
1216
					
1217
					if($c_cleaned_name != '')
1218
					{
1219
						xtc_db_query("UPDATE " . TABLE_CATEGORIES_DESCRIPTION . "
1220
													SET gm_url_keywords = '" . $c_cleaned_name . "'
1221
													WHERE
1222
														categories_id = '" . (int)$t_category_array['categories_id'] . "'
1223
														AND language_id = '" . $c_languages_id . "'", 'db_link', false);
1224
					}
1225
					else
1226
					{
1227
						xtc_db_query("UPDATE " . TABLE_CATEGORIES_DESCRIPTION . "
1228
													SET gm_url_keywords = 'category-"
1229
						             . (int)$t_category_array['categories_id'] . "'
1230
													WHERE
1231
														categories_id = '" . (int)$t_category_array['categories_id'] . "'
1232
														AND language_id = '" . $c_languages_id . "'", 'db_link', false);
1233
					}
1234
				}
1235
				
1236
				$t_found_double_keywords = true;
1237
				while($t_found_double_keywords)
1238
				{
1239
					$t_categories_array = array();
1240
					$t_found            = false;
1241
					
1242
					$t_get_double_keywords = xtc_db_query("SELECT DISTINCT
1243
																a.categories_id,
1244
																a.gm_url_keywords
1245
															FROM " . TABLE_CATEGORIES_DESCRIPTION . " a
1246
															LEFT JOIN " . TABLE_CATEGORIES_DESCRIPTION . " AS b ON ("
1247
					                                      . $this->v_binary_string . "a.gm_url_keywords = "
1248
					                                      . $this->v_binary_string . "b.gm_url_keywords)
1249
															WHERE
1250
																a.categories_id != b.categories_id
1251
																AND a.language_id = '" . $c_languages_id . "'
1252
																AND b.language_id = '" . $c_languages_id . "'
1253
															ORDER BY a.categories_id ASC", 'db_link', false);
1254
					while($t_result_array = xtc_db_fetch_array($t_get_double_keywords))
1255
					{
1256
						$t_found                                              = true;
1257
						$t_categories_array[$t_result_array['categories_id']] = $t_result_array['gm_url_keywords'];
1258
					}
1259
					
1260
					if(!$t_found)
1261
					{
1262
						$t_found_double_keywords = false;
1263
					}
1264
					else
1265
					{
1266
						$t_cleared_keywords_array = array();
1267
						
1268
						foreach($t_categories_array AS $t_categories_id => $t_gm_url_keywords)
1269
						{
1270
							$t_keys_array = array_keys($t_categories_array, $t_gm_url_keywords);
1271
							
1272
							for($i = 1; $i < count($t_keys_array); $i++)
1273
							{
1274
								if(!in_array($t_gm_url_keywords, $t_cleared_keywords_array))
1275
								{
1276
									$t_new_url_keyword = preg_replace('/(.+?)(-[0-9]+)$/', "$1", $t_gm_url_keywords);
1277
									$c_new_url_keyword = ((isset($GLOBALS["___mysqli_ston"])
1278
									                       && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"],
1279
									                                                                                             $t_new_url_keyword) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.",
1280
									                                                                                                                                   E_USER_ERROR)) ? "" : ""));
1281
									
1282
									$t_sql_categories_id = $t_keys_array[$i];
1283
									if($c_new_url_keyword . '-' . $t_keys_array[$i] == $t_gm_url_keywords)
1284
									{
1285
										$t_sql_categories_id = $t_keys_array[0];
1286
									}
1287
									
1288
									xtc_db_query("UPDATE " . TABLE_CATEGORIES_DESCRIPTION . "
1289
																SET gm_url_keywords = '" . $c_new_url_keyword . "-"
1290
									             . (int)$t_sql_categories_id . "'
1291
																WHERE
1292
																	categories_id = '" . (int)$t_sql_categories_id . "'
1293
																	AND language_id = '" . $c_languages_id . "'",
1294
									             'db_link', false);
1295
								}
1296
							}
1297
							
1298
							$t_cleared_keywords_array[] = $t_gm_url_keywords;
1299
						}
1300
					}
1301
				}
1302
			}
1303
		}
1304
		
1305
		if($p_type == 'all' || $p_type == 'contents')
1306
		{
1307
			$t_get_languages_ids = xtc_db_query("SELECT languages_id
1308
													FROM " . TABLE_LANGUAGES . "", 'db_link', false);
1309
			while($t_result_array = xtc_db_fetch_array($t_get_languages_ids))
1310
			{
1311
				$c_languages_id = (int)$t_result_array['languages_id'];
1312
				
1313
				$t_get_empty_keywords = xtc_db_query("SELECT
1314
															content_id,
1315
															content_group,
1316
															content_title,
1317
															content_heading
1318
														FROM " . TABLE_CONTENT_MANAGER . "
1319
														WHERE
1320
															(gm_url_keywords = '' OR LENGTH(gm_url_keywords) >= 255)
1321
															AND languages_id = '" . $c_languages_id . "'", 'db_link',
1322
				                                     false);
1323
				while($t_content_array = xtc_db_fetch_array($t_get_empty_keywords))
1324
				{
1325
					$c_cleaned_content_heading = $this->clean_keyword($t_content_array['content_heading']);
1326
					if(strlen_wrapper($c_cleaned_content_heading . '-' . $t_content_array['content_id']) >= 255)
1327
					{
1328
						$c_cleaned_content_heading = substr_wrapper($c_cleaned_content_heading, 0, 100);
1329
					}
1330
					
1331
					$c_cleaned_content_title = $this->clean_keyword($t_content_array['content_title']);
1332
					if(strlen_wrapper($c_cleaned_content_title . '-' . $t_content_array['content_id']) >= 255)
1333
					{
1334
						$c_cleaned_content_title = substr_wrapper($c_cleaned_content_title, 0, 100);
1335
					}
1336
					
1337
					if($c_cleaned_content_heading != '')
1338
					{
1339
						xtc_db_query("UPDATE " . TABLE_CONTENT_MANAGER . "
1340
													SET gm_url_keywords = '" . $c_cleaned_content_heading . "'
1341
													WHERE
1342
														content_group = '" . (int)$t_content_array['content_group'] . "'
1343
														AND languages_id = '" . $c_languages_id . "'", 'db_link',
1344
						                         false);
1345
					}
1346
					elseif($c_cleaned_content_title != '')
1347
					{
1348
						xtc_db_query("UPDATE " . TABLE_CONTENT_MANAGER . "
1349
													SET gm_url_keywords = '" . $c_cleaned_content_title . "'
1350
													WHERE
1351
														content_group = '" . (int)$t_content_array['content_group'] . "'
1352
														AND languages_id = '" . $c_languages_id . "'", 'db_link',
1353
						                         false);
1354
					}
1355
					else
1356
					{
1357
						xtc_db_query("UPDATE " . TABLE_CONTENT_MANAGER . "
1358
													SET gm_url_keywords = 'info-content-"
1359
						                         . (int)$t_content_array['content_id'] . "'
1360
													WHERE
1361
														content_group = '" . (int)$t_content_array['content_group'] . "'
1362
														AND languages_id = '" . $c_languages_id . "'", 'db_link',
1363
						                         false);
1364
					}
1365
				}
1366
				
1367
				$t_found_double_keywords = true;
1368
				while($t_found_double_keywords)
1369
				{
1370
					$t_content_array = array();
1371
					$t_found         = false;
1372
					
1373
					$t_get_double_keywords = xtc_db_query("SELECT DISTINCT
1374
																a.content_group,
1375
																a.gm_url_keywords
1376
															FROM " . TABLE_CONTENT_MANAGER . " a
1377
															LEFT JOIN " . TABLE_CONTENT_MANAGER . " AS b ON ("
1378
					                                      . $this->v_binary_string . "a.gm_url_keywords = "
1379
					                                      . $this->v_binary_string . "b.gm_url_keywords)
1380
															WHERE
1381
																a.content_group != b.content_group
1382
																AND a.languages_id = '" . $c_languages_id . "'
1383
																AND b.languages_id = '" . $c_languages_id . "'
1384
															ORDER BY a.content_id ASC", 'db_link', false);
1385
					while($t_result_array = xtc_db_fetch_array($t_get_double_keywords))
1386
					{
1387
						$t_found                                           = true;
1388
						$t_content_array[$t_result_array['content_group']] = $t_result_array['gm_url_keywords'];
1389
					}
1390
					
1391
					if(!$t_found)
1392
					{
1393
						$t_found_double_keywords = false;
1394
					}
1395
					else
1396
					{
1397
						$t_cleared_keywords_array = array();
1398
						
1399
						foreach($t_content_array as $t_content_group => $t_gm_url_keywords)
1400
						{
1401
							$t_keys_array = array_keys($t_content_array, $t_gm_url_keywords);
1402
							
1403
							for($i = 1; $i < count($t_keys_array); $i++)
1404
							{
1405
								if(!in_array($t_gm_url_keywords, $t_cleared_keywords_array))
1406
								{
1407
									xtc_db_query("UPDATE " . TABLE_CONTENT_MANAGER . "
1408
																SET gm_url_keywords = CONCAT(gm_url_keywords, '-"
1409
									                         . $t_keys_array[$i] . "')
1410
																WHERE
1411
																	content_group = '" . $t_keys_array[$i] . "'
1412
																	AND languages_id = '" . $c_languages_id . "'",
1413
									                         'db_link', false);
1414
								}
1415
							}
1416
							
1417
							$t_cleared_keywords_array[] = $t_gm_url_keywords;
1418
						}
1419
					}
1420
				}
1421
			}
1422
		}
1423
	}
1424
	
1425
	
1426
	public function clean_name($p_string, $p_strip_only_illegal_characters = false)
1427
	{
1428
		return xtc_cleanName($p_string);
1429
	}
1430
	
1431
	
1432
	public function clean_keyword($string)
1433
	{
1434
		$search  = array('ä', 'Ä', 'ö', 'Ö', 'ü', 'Ü', '&auml;', '&Auml;', '&ouml;', '&Ouml;', '&uuml;', '&Uuml;', 'ß', '&szlig;');
1435
		$replace = array('ae', 'Ae', 'oe', 'Oe', 'ue', 'Ue', 'ae', 'Ae', 'oe', 'Oe', 'ue', 'Ue', 'ss', 'ss');
1436
		$string  = str_replace($search, $replace, $string);
1437
		
1438
		$search  = array('А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ё', 'Ж', 'З', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ъ', 'Ы', 'Ь', 'Э', 'Ю', 'Я', 'а', 'б', 'в', 'г', 'д', 'е', 'ё', 'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ы', 'ь', 'э', 'ю', 'я');
1439
		$replace = array('A', 'B', 'W', 'G', 'D', 'Ie', 'Io', 'Z', 'Z', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'F', 'Ch', 'C', 'Tch', 'Sh', 'Shtch', '', 'Y', '', 'E', 'Iu', 'Ia', 'a', 'b', 'w', 'g', 'd', 'ie', 'io', 'z', 'z', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'f', 'ch', 'c', 'tch', 'sh', 'shtch', '', 'y', '', 'e', 'iu', 'ia');
1440
		$string  = str_replace($search, $replace, $string);
1441
		
1442
		$search  = array('Á', 'À', 'Â', 'Ä', 'Ă', 'Ā', 'Ã', 'Å', 'Ą', 'Æ', 'Ć', 'Ċ', 'Ĉ', 'Č', 'Ç', 'Ď', 'Đ', 'Ð', 'É', 'È', 'Ė', 'Ê', 'Ë', 'Ě', 'Ē', 'Ę', 'Ə', 'Ġ', 'Ĝ', 'Ğ', 'Ģ', 'á', 'à', 'â', 'ä', 'ă', 'ā', 'ã', 'å', 'ą', 'æ', 'ć', 'ċ', 'ĉ', 'č', 'ç', 'ď', 'đ', 'ð', 'é', 'è', 'ė', 'ê', 'ë', 'ě', 'ē', 'ę', 'ə', 'ġ', 'ĝ', 'ğ', 'ģ', 'Ĥ', 'Ħ', 'I', 'Í', 'Ì', 'İ', 'Î', 'Ï', 'Ī', 'Į', 'IJ', 'Ĵ', 'Ķ', 'Ļ', 'Ł', 'Ń', 'Ň', 'Ñ', 'Ņ', 'Ó', 'Ò', 'Ô', 'Ö', 'Õ', 'Ő', 'Ø', 'Ơ', 'Œ', 'ĥ', 'ħ', 'ı', 'í', 'ì', 'i', 'î', 'ï', 'ī', 'į', 'ij', 'ĵ', 'ķ', 'ļ', 'ł', 'ń', 'ň', 'ñ', 'ņ', 'ó', 'ò', 'ô', 'ö', 'õ', 'ő', 'ø', 'ơ', 'œ', 'Ŕ', 'Ř', 'Ś', 'Ŝ', 'Š', 'Ş', 'Ť', 'Ţ', 'Þ', 'Ú', 'Ù', 'Û', 'Ü', 'Ŭ', 'Ū', 'Ů', 'Ų', 'Ű', 'Ư', 'Ŵ', 'Ý', 'Ŷ', 'Ÿ', 'Ź', 'Ż', 'Ž', 'ŕ', 'ř', 'ś', 'ŝ', 'š', 'ş', 'ß', 'ť', 'ţ', 'þ', 'ú', 'ù', 'û', 'ü', 'ŭ', 'ū', 'ů', 'ų', 'ű', 'ư', 'ŵ', 'ý', 'ŷ', 'ÿ', 'ź', 'ż', 'ž');
1443
		$replace = array('A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'AE', 'C', 'C', 'C', 'C', 'C', 'D', 'D', 'D', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'G', 'G', 'G', 'G', 'G', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'c', 'c', 'c', 'c', 'd', 'd', 'd', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'g', 'g', 'g', 'g', 'g', 'H', 'H', 'I', 'I', 'I', 'I', 'I', 'I', 'I', 'I', 'IJ', 'J', 'K', 'L', 'L', 'N', 'N', 'N', 'N', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'CE', 'h', 'h', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'ij', 'j', 'k', 'l', 'l', 'n', 'n', 'n', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'R', 'R', 'S', 'S', 'S', 'S', 'T', 'T', 'T', 'U', 'U', 'U', 'U', 'U', 'U', 'U', 'U', 'U', 'U', 'W', 'Y', 'Y', 'Y', 'Z', 'Z', 'Z', 'r', 'r', 's', 's', 's', 's', 'B', 't', 't', 'b', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'w', 'y', 'y', 'y', 'z', 'z', 'z');
1444
		$string  = str_replace($search, $replace, $string);
1445
		
1446
		$string = strtolower($string);
1447
		
1448
		$string = preg_replace('/[^a-z0-9]/', '-', $string);
1449
		$string = preg_replace('/--+/', '-', $string);
1450
		$string = preg_replace('/^-(.*)/', "$1", $string);
1451
		$string = preg_replace('/(.*)-$/', "$1", $string);
1452
		
1453
		return $string;
1454
	}
1455
	
1456
	
1457
	protected function _writeCache($key, $value)
1458
	{
1459
		$this->_getSeoBoostCache()[$key] = $value;
1460
		$this->_updateCache();
1461
	}
1462
	
1463
	
1464
	protected function _updateCache()
1465
	{
1466
		$this->_getDataCache()->set_data('seo_boost_cache', $this->_getSeoBoostCache(), true);
1467
	}
1468
	
1469
	
1470
	protected function _getUrlRewriteReader($p_contentType)
1471
	{
1472
		switch($p_contentType)
1473
		{
1474
			case 'product':
1475
				$urlRewritesReader = StaticGXCoreLoader::getService('ProductRead');
1476
				break;
1477
			case 'category':
1478
				$urlRewritesReader = StaticGXCoreLoader::getService('CategoryRead');
1479
				break;
1480
			case 'content':
1481
				$db                           = StaticGXCoreLoader::getDatabaseQueryBuilder();
1482
				$urlRewriteStorageContentType = new NonEmptyStringType('content');
1483
				$urlRewritesReader            = MainFactory::create('UrlRewriteStorage', $urlRewriteStorageContentType,
1484
				                                                    $db, $this->_getLanguageProvider());
1485
				break;
1486
			default:
1487
				throw new InvalidArgumentException('GMSEOBoost: Unsupported content type for URL rewrites given. '
1488
				                                   . 'Supported content types are \'product\', \'category\' and \'content\'. Got '
1489
				                                   . gettype($p_contentType) . '): ' . $p_contentType);
1490
		}
1491
		
1492
		return $urlRewritesReader;
1493
	}
1494
	
1495
	
1496
	protected function _mysqlResult($result, $row, $field)
1497
	{
1498
		$result->data_seek($row);
1499
		$datarow = $result->fetch_array();
1500
		
1501
		return $datarow[$field];
1502
	}
1503
	
1504
	
1505
	protected function _getLanguageProvider()
1506
	{
1507
		if($this->languageProvider === null)
1508
		{
1509
			$this->languageProvider = MainFactory::create('LanguageProvider',
1510
			                                              StaticGXCoreLoader::getDatabaseQueryBuilder());
1511
		}
1512
		
1513
		return $this->languageProvider;
1514
	}
1515
	
1516
	
1517
	protected function _getProductUrlReader()
1518
	{
1519
		if($this->productUrlRewritesReader === null)
1520
		{
1521
			$this->productUrlRewritesReader = $this->_getUrlRewriteReader('product');
1522
		}
1523
		
1524
		return $this->productUrlRewritesReader;
1525
	}
1526
	
1527
	
1528
	protected function _getCategoryUrlReader()
1529
	{
1530
		if($this->categoryUrlRewritesReader === null)
1531
		{
1532
			$this->categoryUrlRewritesReader = $this->_getUrlRewriteReader('category');
1533
		}
1534
		
1535
		return $this->categoryUrlRewritesReader;
1536
	}
1537
	
1538
	
1539
	protected function _getContentUrlReader()
1540
	{
1541
		if($this->contentUrlRewritesReader === null)
1542
		{
1543
			$this->contentUrlRewritesReader = $this->_getUrlRewriteReader('content');
1544
		}
1545
		
1546
		return $this->contentUrlRewritesReader;
1547
	}
1548
	
1549
	
1550
	protected function _getDataCache()
1551
	{
1552
		if($this->dataCache === null)
1553
		{
1554
			$this->dataCache = DataCache::get_instance();
1555
		}
1556
		
1557
		return $this->dataCache;
1558
	}
1559
	
1560
	
1561
	protected function _getSeoBoostCache()
1562
	{
1563
		if($this->seoBoostCache === null)
1564
		{
1565
			$this->seoBoostCache = [];
1566
			
1567
			if($this->_getDataCache()->key_exists('seo_boost_cache', true))
1568
			{
1569
				$this->seoBoostCache = $this->_getDataCache()->get_data('seo_boost_cache') ? : [];
1570
			}
1571
		}
1572
		
1573
		return $this->seoBoostCache;
1574
	}
1575
	
1576
	
1577
	protected function _getDefaultLanguageId()
1578
	{
1579
		static $languageId;
1580
		
1581
		if($languageId === null)
1582
		{
1583
			$languageId = $this->_getLanguageProvider()->getDefaultLanguageId();
1584
		}
1585
		
1586
		return $languageId;
1587
	}
1588
	
1589
	
1590
	public function _getLanguageData(IdType $languageId)
1591
	{
1592
		static $languageDataArray = [];
1593
		
1594
		if(!array_key_exists($languageId->asInt(), $languageDataArray))
1595
		{
1596
			$languageDataArray[$languageId->asInt()] = [
1597
				'language_id'      => $languageId->asInt(),
1598
				'code'             => strtolower($this->_getLanguageProvider()->getCodeById($languageId)->asString()),
1599
				'directory'        => $this->_getLanguageProvider()->getDirectoryById($languageId),
1600
				'language_charset' => $this->_getLanguageProvider()->getCharsetById($languageId)
1601
			];
1602
		}
1603
		
1604
		return $languageDataArray[$languageId->asInt()];
1605
	}
1606
	
1607
	
1608
	/**
1609
	 * Checks if product has url rewrite data
1610
	 *
1611
	 * @param $id         int ID of the product
1612
	 * @param $languageId int ID of the used language
1613
	 *
1614
	 * @return bool
1615
	 */
1616
	public function _productHasUrlRewrite($id, $languageId = 0)
1617
	{
1618
		static $urlRewriteProductIds;
1619
		$cacheKey = 'urlRewriteProductIds';
1620
		
1621
		if($urlRewriteProductIds === null)
1622
		{
1623
			if($this->_getDataCache()->key_exists($cacheKey, true))
1624
			{
1625
				$urlRewriteProductIds = $this->_getDataCache()->get_data($cacheKey);
1626
			}
1627
			else
1628
			{
1629
				$urlRewriteProductIds = [];
1630
				
1631
				$query  = 'SELECT `content_id`, `language_id` FROM `url_rewrites` WHERE `content_type` = "product"';
1632
				$result = xtc_db_query($query);
1633
				while($row = xtc_db_fetch_array($result))
1634
				{
1635
					$contentId                                            = (int)$row['content_id'];
1636
					$contentLanguageId                                    = (int)$row['language_id'];
1637
					$urlRewriteProductIds[$contentId][$contentLanguageId] = null;
1638
				}
1639
				
1640
				$this->_getDataCache()->set_data($cacheKey, $urlRewriteProductIds, true);
1641
			}
1642
		}
1643
		
1644
		return array_key_exists($id, $urlRewriteProductIds) && array_key_exists($languageId, $urlRewriteProductIds[$id]);
1645
	}
1646
	
1647
	
1648
	/**
1649
	 * Checks if category has url rewrite data
1650
	 *
1651
	 * @param $id         int ID of the category
1652
	 * @param $languageId int ID of the used language
1653
	 *
1654
	 * @return bool
1655
	 */
1656
	public function _categoryHasUrlRewrite($id, $languageId = 0)
1657
	{
1658
		static $urlRewriteCategoryIds;
1659
		$cacheKey = 'urlRewriteCategoryIds';
1660
		
1661
		if($urlRewriteCategoryIds === null)
1662
		{
1663
			if($this->_getDataCache()->key_exists($cacheKey, true))
1664
			{
1665
				$urlRewriteCategoryIds = $this->_getDataCache()->get_data($cacheKey);
1666
			}
1667
			else
1668
			{
1669
				$urlRewriteCategoryIds = [];
1670
				
1671
				$query  = 'SELECT `content_id`, `language_id` FROM `url_rewrites` WHERE `content_type` = "category"';
1672
				$result = xtc_db_query($query);
1673
				while($row = xtc_db_fetch_array($result))
1674
				{
1675
					$contentId                                             = (int)$row['content_id'];
1676
					$contentLanguageId                                     = (int)$row['language_id'];
1677
					$urlRewriteCategoryIds[$contentId][$contentLanguageId] = null;
1678
				}
1679
				
1680
				$this->_getDataCache()->set_data($cacheKey, $urlRewriteCategoryIds, true);
1681
			}
1682
		}
1683
		
1684
		return array_key_exists($id, $urlRewriteCategoryIds) && array_key_exists($languageId, $urlRewriteCategoryIds[$id]);
1685
	}
1686
	
1687
	
1688
	/**
1689
	 * Checks if content has url rewrite data
1690
	 *
1691
	 * @param $id         int ID of the content
1692
	 * @param $languageId int ID of the used language
1693
	 *
1694
	 * @return bool
1695
	 */
1696
	public function _contentHasUrlRewrite($id, $languageId = 0)
1697
	{
1698
		static $urlRewriteContentIds;
1699
		$cacheKey = 'urlRewriteContentIds';
1700
		
1701
		if($urlRewriteContentIds === null)
1702
		{
1703
			if($this->_getDataCache()->key_exists($cacheKey, true))
1704
			{
1705
				$urlRewriteContentIds = $this->_getDataCache()->get_data($cacheKey);
1706
			}
1707
			else
1708
			{
1709
				$urlRewriteContentIds = [];
1710
				
1711
				$query  = 'SELECT `content_id`, `language_id` FROM `url_rewrites` WHERE `content_type` = "content"';
1712
				$result = xtc_db_query($query);
1713
				while($row = xtc_db_fetch_array($result))
1714
				{
1715
					$contentId                                            = (int)$row['content_id'];
1716
					$contentLanguageId                                    = (int)$row['language_id'];
1717
					$urlRewriteContentIds[$contentId][$contentLanguageId] = null;
1718
				}
1719
				
1720
				$this->_getDataCache()->set_data($cacheKey, $urlRewriteContentIds, true);
1721
			}
1722
		}
1723
		
1724
		return array_key_exists($id, $urlRewriteContentIds) && array_key_exists($languageId, $urlRewriteContentIds[$id]);
1725
	}
1726
}
1727

    
1728
MainFactory::load_origin_class('GMSEOBoost');
    (1-1/1)