Optimize R2
jdarwood007

jdarwood007 commited on 2019-12-24 19:14:24
Showing 1 changed files, with 168 additions and 49 deletions.

... ...
@@ -41,6 +41,8 @@ class SFS
41 41
 	private $search_params_column = '';
42 42
 	private $search_params_string = null;
43 43
 	private $search_params_type = null;
44
+	private $canDeleteLogs = false;
45
+	private $logSearch = array();
44 46
 
45 47
 	/**
46 48
 	 * @var int How long we disable removing logs.
... ...
@@ -402,15 +404,14 @@ class SFS
402 404
 
403 405
 		loadLanguage('Modlog');
404 406
 
405
-		$context['url_start'] = $this->adminLogURL;
407
+		$context['form_url'] = $this->adminLogURL;
408
+		$context['log_url'] = $this->adminLogURL;
406 409
 		$context['page_title'] = $this->txt('sfs_admin_logs');
407
-		$context['can_delete'] = allowedTo('admin_forum');
410
+		$this->canDeleteLogs = allowedTo('admin_forum');
408 411
 
409 412
 		// Remove all..
410
-		if (isset($_POST['removeall']) && $context['can_delete'])
411
-			$this->removeAllLogs();
412
-		elseif (!empty($_POST['remove']) && isset($_POST['delete']) && $context['can_delete'])
413
-			$this->removeLogs(array_unique($_POST['delete']));
413
+		if ((isset($_POST['removeall']) || isset($_POST['delete'])) && $this->canDeleteLogs)
414
+			$this->handleLogDeletes();
414 415
 
415 416
 		$sort_types = array(
416 417
 			'id_type' =>'l.id_type',
... ...
@@ -426,7 +427,7 @@ class SFS
426 427
 		$context['order'] = isset($_REQUEST['sort']) && isset($sort_types[$_REQUEST['sort']]) ? $_REQUEST['sort'] : 'time';
427 428
 
428 429
 		// Handle searches.
429
-		$this->handleLogSearch();
430
+		$this->handleLogSearch($context['log_url']);
430 431
 
431 432
 		require_once($sourcedir . '/Subs-List.php');
432 433
 
... ...
@@ -436,22 +437,10 @@ class SFS
436 437
 			'width' => '100%',
437 438
 			'items_per_page' => '50',
438 439
 			'no_items_label' => $this->txt('sfs_log_no_entries_found'),
439
-			'base_href' => $context['url_start'] . (!empty($context['search_params']) ? ';params=' . $context['search_params'] : ''),
440
+			'base_href' => $context['log_url'],
440 441
 			'default_sort_col' => 'time',
441
-			'get_items' => array(
442
-				'function' => array($this, 'getSFSLogEntries'),
443
-				'params' => array(
444
-					(!empty($this->search_params['string']) ? ' INSTR({raw:sql_type}, {string:search_string})' : ''),
445
-					array('sql_type' => $this->search_params_column, 'search_string' => $this->search_params['string']),
446
-				),
447
-			),
448
-			'get_count' => array(
449
-				'function' => array($this, 'getSFSLogEntriesCount'),
450
-				'params' => array(
451
-					(!empty($this->search_params['string']) ? ' INSTR({raw:sql_type}, {string:search_string})' : ''),
452
-					array('sql_type' => $this->search_params_column, 'search_string' => $this->search_params['string']),
453
-				),
454
-			),
442
+			'get_items' => $this->loadLogsGetItems(),
443
+			'get_count' => $this->loadLogsGetCount(),
455 444
 			// This assumes we are viewing by user.
456 445
 			'columns' => array(
457 446
 				'type' => $this->loadLogsColumnType(),
... ...
@@ -467,7 +456,7 @@ class SFS
467 456
 				'delete' => $this->loadLogsColumnDelete(),
468 457
 			),
469 458
 			'form' => array(
470
-				'href' => $context['url_start'],
459
+				'href' => $context['form_url'],
471 460
 				'include_sort' => true,
472 461
 				'include_start' => true,
473 462
 				'hidden_fields' => array(
... ...
@@ -476,15 +465,7 @@ class SFS
476 465
 				),
477 466
 			),
478 467
 			'additional_rows' => array(
479
-				array(
480
-					'position' => 'below_table_data',
481
-					'value' => '
482
-						' . $this->txt('sfs_log_search') . ' (' . $context['search']['label'] . '):
483
-						<input type="text" name="search" size="18" value="' . $smcFunc['htmlspecialchars']($context['search']['string']) . '" class="input_text" /> <input type="submit" name="is_search" value="' . $this->txt('modlog_go') . '" class="button_submit" />
484
-						' . ($context['can_delete'] ? ' |
485
-							<input type="submit" name="remove" value="' . $this->txt('modlog_remove') . '" class="button_submit" />
486
-							<input type="submit" name="removeall" value="' . $this->txt('modlog_removeall') . '" class="button_submit" />' : ''),
487
-				),
468
+				$this->loadLogsGetAddtionalRow(),
488 469
 			),
489 470
 		);
490 471
 
... ...
@@ -497,6 +478,88 @@ class SFS
497 478
 		return array();
498 479
 	}
499 480
 
481
+	/**
482
+	 * Handle when we want to delete a log and what to do.
483
+	 *
484
+	 * @internal
485
+	 * @CalledIn SMF2.0, SMF 2.1
486
+	 * @version 1.1
487
+	 * @since 1.1
488
+	 * @return void Nothing is returned, the logs are deleted as requested and admin redirected.
489
+	 */
490
+	private function handleLogDeletes(): void
491
+	{
492
+		if (isset($_POST['removeall']) && $this->canDeleteLogs)
493
+			$this->removeAllLogs();
494
+		elseif (!empty($_POST['remove']) && isset($_POST['delete']) && $this->canDeleteLogs)
495
+			$this->removeLogs(array_unique($_POST['delete']));
496
+	}
497
+
498
+	/**
499
+	 * loadLogs - Get Items.
500
+	 *
501
+	 * @internal
502
+	 * @CalledIn SMF2.0, SMF 2.1
503
+	 * @version 1.1
504
+	 * @since 1.1
505
+	 * @return array The options for the get_items
506
+	 */
507
+	private function loadLogsGetItems(): array
508
+	{
509
+		return array(
510
+			'function' => array($this, 'getSFSLogEntries'),
511
+			'params' => array(
512
+				(!empty($this->search_params['string']) ? ' INSTR({raw:sql_type}, {string:search_string})' : ''),
513
+				array('sql_type' => $this->search_params_column, 'search_string' => $this->search_params['string']),
514
+			),
515
+		);
516
+	}
517
+
518
+	/**
519
+	 * loadLogs - Get Count.
520
+	 *
521
+	 * @internal
522
+	 * @CalledIn SMF2.0, SMF 2.1
523
+	 * @version 1.1
524
+	 * @since 1.1
525
+	 * @return array The options for the get_items
526
+	 */
527
+	private function loadLogsGetCount(): array
528
+	{
529
+		return array(
530
+			'function' => array($this, 'getSFSLogEntriesCount'),
531
+			'params' => array(
532
+				(!empty($this->search_params['string']) ? ' INSTR({raw:sql_type}, {string:search_string})' : ''),
533
+				array('sql_type' => $this->search_params_column, 'search_string' => $this->search_params['string']),
534
+			),
535
+		);
536
+	}
537
+
538
+	/**
539
+	 * loadLogs - Load an additional row, for mostly deleting stuff.
540
+	 *
541
+	 * @internal
542
+	 * @CalledIn SMF2.0, SMF 2.1
543
+	 * @version 1.1
544
+	 * @since 1.1
545
+	 * @return array The options for the get_items
546
+	 */
547
+	private function loadLogsGetAddtionalRow(): array
548
+	{
549
+		global $smcFunc;
550
+
551
+		return array(
552
+			'position' => 'below_table_data',
553
+			'value' => '
554
+				' . $this->txt('sfs_log_search') . ' (' . $this->logSearch['label'] . '):
555
+				<input type="text" name="search" size="18" value="' . $smcFunc['htmlspecialchars']($this->logSearch['string']) . '" class="input_text" /> <input type="submit" name="is_search" value="' . $this->txt('modlog_go') . '" class="button_submit" />
556
+				' . ($this->canDeleteLogs ? ' |
557
+					<input type="submit" name="remove" value="' . $this->txt('modlog_remove') . '" class="button_submit" />
558
+					<input type="submit" name="removeall" value="' . $this->txt('modlog_removeall') . '" class="button_submit" />' : ''),
559
+		);
560
+	}
561
+
562
+
500 563
 	/**
501 564
 	 * loadLogs - Column - Type.
502 565
 	 *
... ...
@@ -506,7 +569,7 @@ class SFS
506 569
 	 * @since 1.1
507 570
 	 * @return array The options for the column
508 571
 	 */
509
-	function loadLogsColumnType(): array
572
+	private function loadLogsColumnType(): array
510 573
 	{
511 574
 		return array(
512 575
 			'header' => array(
... ...
@@ -531,7 +594,7 @@ class SFS
531 594
 	 * @since 1.1
532 595
 	 * @return array The options for the column
533 596
 	 */
534
-	function loadLogsColumnTime(): array
597
+	private function loadLogsColumnTime(): array
535 598
 	{
536 599
 		return array(
537 600
 			'header' => array(
... ...
@@ -558,7 +621,7 @@ class SFS
558 621
 	 * @since 1.1
559 622
 	 * @return array The options for the column
560 623
 	 */
561
-	function loadLogsColumnURL(): array
624
+	private function loadLogsColumnURL(): array
562 625
 	{
563 626
 		return array(
564 627
 			'header' => array(
... ...
@@ -586,7 +649,7 @@ class SFS
586 649
 	 * @since 1.1
587 650
 	 * @return array The options for the column
588 651
 	 */
589
-	function loadLogsColumnMember(): array
652
+	private function loadLogsColumnMember(): array
590 653
 	{
591 654
 		return array(
592 655
 			'header' => array(
... ...
@@ -613,7 +676,7 @@ class SFS
613 676
 	 * @since 1.1
614 677
 	 * @return array The options for the column
615 678
 	 */
616
-	function loadLogsColumnUsername(): array
679
+	private function loadLogsColumnUsername(): array
617 680
 	{
618 681
 		return array(
619 682
 			'header' => array(
... ...
@@ -640,7 +703,7 @@ class SFS
640 703
 	 * @since 1.1
641 704
 	 * @return array The options for the column
642 705
 	 */
643
-	function loadLogsColumnEmail(): array
706
+	private function loadLogsColumnEmail(): array
644 707
 	{
645 708
 		return array(
646 709
 			'header' => array(
... ...
@@ -668,7 +731,7 @@ class SFS
668 731
 	 * @since 1.1
669 732
 	 * @return array The options for the column
670 733
 	 */
671
-	function loadLogsColumnIP(bool $ip2 = false): array
734
+	private function loadLogsColumnIP(bool $ip2 = false): array
672 735
 	{
673 736
 		return array(
674 737
 			'header' => array(
... ...
@@ -695,7 +758,7 @@ class SFS
695 758
 	 * @since 1.1
696 759
 	 * @return array The options for the column
697 760
 	 */
698
-	function loadLogsColumnChecks(): array
761
+	private function loadLogsColumnChecks(): array
699 762
 	{
700 763
 		return array(
701 764
 			'header' => array(
... ...
@@ -720,7 +783,7 @@ class SFS
720 783
 	 * @since 1.1
721 784
 	 * @return array The options for the column
722 785
 	 */
723
-	function loadLogsColumnResult(): array
786
+	private function loadLogsColumnResult(): array
724 787
 	{
725 788
 		return array(
726 789
 			'header' => array(
... ...
@@ -745,7 +808,7 @@ class SFS
745 808
 	 * @since 1.1
746 809
 	 * @return array The options for the column
747 810
 	 */
748
-	function loadLogsColumnDelete(): array
811
+	private function loadLogsColumnDelete(): array
749 812
 	{
750 813
 		return array(
751 814
 			'header' => array(
... ...
@@ -1063,7 +1126,31 @@ class SFS
1063 1126
 
1064 1127
 		// Posting?
1065 1128
 		if ($thisVerification['id'] == 'post' && in_array('post', $options))
1129
+			return $this->checkVerificationTestPosts();
1130
+		// reporting topics is only for guests.
1131
+		elseif ($thisVerification['id'] == 'report' && in_array('report', $options))
1132
+			return $this->checkVerificationTestReport();
1133
+		// We should avoid this on searches, as we can only send ips.
1134
+		elseif ($thisVerification['id'] == 'search' && in_array('search', $options) && ($user_info['is_guest'] || empty($user_info['posts']) || $user_info['posts'] < $modSettings['sfs_verfOptMemPostThreshold']))
1135
+			return $this->checkVerificationTestSearch();
1136
+
1137
+		// Others areas.  We have to play a guessing game here.
1138
+		return $this->checkVerificationTestExtra($thisVerification);
1139
+	}
1140
+
1141
+	/**
1142
+	 * Test for a standard post.
1143
+	 *
1144
+	 * @internal
1145
+	 * @CalledIn SMF 2.0, SMF 2.1
1146
+	 * @version 1.1
1147
+	 * @since 1.1
1148
+	 * @return bool True is success, no other bool is expeicifcly defined yet.
1149
+	 */
1150
+	private function checkVerificationTestPosts(): bool
1066 1151
 	{
1152
+		global $user_info, $modSettings;
1153
+
1067 1154
 		// Guests!
1068 1155
 		if ($user_info['is_guest'])
1069 1156
 		{
... ...
@@ -1089,8 +1176,17 @@ class SFS
1089 1176
 		else
1090 1177
 			return true;
1091 1178
 	}
1092
-		// reporting topics is only for guests.
1093
-		elseif ($thisVerification['id'] == 'report' && in_array('report', $options))
1179
+
1180
+	/**
1181
+	 * Test for a report.
1182
+	 *
1183
+	 * @internal
1184
+	 * @CalledIn SMF 2.0, SMF 2.1
1185
+	 * @version 1.1
1186
+	 * @since 1.1
1187
+	 * @return bool True is success, no other bool is expeicifcly defined yet.
1188
+	 */
1189
+	private function checkVerificationTestReport(): bool
1094 1190
 	{
1095 1191
 		$email = !isset($_POST['email']) ? '' : trim($_POST['email']);
1096 1192
 
... ...
@@ -1100,8 +1196,17 @@ class SFS
1100 1196
 			array('ip' => $user_info['ip2']),
1101 1197
 		), 'post');
1102 1198
 	}
1103
-		// We should avoid this on searches, as we can only send ips.
1104
-		elseif ($thisVerification['id'] == 'search' && in_array('search', $options) && ($user_info['is_guest'] || empty($user_info['posts']) || $user_info['posts'] < $modSettings['sfs_verfOptMemPostThreshold']))
1199
+
1200
+	/**
1201
+	 * Test for a Search.
1202
+	 *
1203
+	 * @internal
1204
+	 * @CalledIn SMF 2.0, SMF 2.1
1205
+	 * @version 1.1
1206
+	 * @since 1.1
1207
+	 * @return bool True is success, no other bool is expeicifcly defined yet.
1208
+	 */
1209
+	private function checkVerificationTestSearch(): bool
1105 1210
 	{
1106 1211
 		return $this->sfsCheck(array(
1107 1212
 			array('ip' => $user_info['ip']),
... ...
@@ -1109,7 +1214,17 @@ class SFS
1109 1214
 		), 'search');
1110 1215
 	}
1111 1216
 
1112
-		// Others areas.  We have to play a guessing game here.
1217
+	/**
1218
+	 * Test for extras, customizations and other areas that we want to tie in.
1219
+	 *
1220
+	 * @internal
1221
+	 * @CalledIn SMF 2.0, SMF 2.1
1222
+	 * @version 1.1
1223
+	 * @since 1.1
1224
+	 * @return bool True is success, no other bool is expeicifcly defined yet.
1225
+	 */
1226
+	private function checkVerificationTestExtra(array $thisVerification): bool
1227
+	{
1113 1228
 		foreach ($this->extraVerificationOptions as $option)
1114 1229
 		{
1115 1230
 			// Not a match.
... ...
@@ -1821,13 +1936,14 @@ class SFS
1821 1936
 	/**
1822 1937
 	 * Handle searching for logs.
1823 1938
 	 *
1939
+	 * @param string $url The base_href
1824 1940
 	 * @internal
1825 1941
 	 * @CalledIn SMF 2.0, SMF 2.1
1826 1942
 	 * @version 1.0
1827 1943
 	 * @since 1.0
1828 1944
 	 * @return void No return is generated here.
1829 1945
 	 */
1830
-	private function handleLogSearch(): void
1946
+	private function handleLogSearch(string &$string): void
1831 1947
 	{
1832 1948
 		global $context, $txt;
1833 1949
 
... ...
@@ -1867,11 +1983,14 @@ class SFS
1867 1983
 
1868 1984
 		// Setup the search context.
1869 1985
 		$context['search_params'] = empty($this->search_params['string']) ? '' : base64_encode(json_encode($this->search_params));
1870
-		$context['search'] = array(
1986
+		$this->logSearch = array(
1871 1987
 			'string' => $this->search_params['string'],
1872 1988
 			'type' => $this->search_params['type'],
1873 1989
 			'label' => $searchTypes[$this->search_params_type]['label'],
1874 1990
 		);
1991
+
1992
+		if (!empty($context['search_params']))
1993
+			$url .= ';params=' . $context['search_params'];
1875 1994
 	}
1876 1995
 
1877 1996
 	/**
1878 1997