~azzar1/unity/add-show-desktop-key

443 by dcoles
Added Forum application along with unmodifed version of phpBB3 "Olympus" 3.0.0
1
<?php
2
/**
3
*
4
* @package acp
5
* @version $Id: acp_search.php,v 1.38 2007/10/05 14:36:32 acydburn Exp $
6
* @copyright (c) 2005 phpBB Group
7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
8
*
9
*/
10
11
/**
12
* @ignore
13
*/
14
if (!defined('IN_PHPBB'))
15
{
16
	exit;
17
}
18
19
/**
20
* @package acp
21
*/
22
class acp_search
23
{
24
	var $u_action;
25
	var $state;
26
	var $search;
27
	var $max_post_id;
28
	var $batch_size = 100;
29
30
	function main($id, $mode)
31
	{
32
		global $user;
33
34
		$user->add_lang('acp/search');
35
36
		// For some this may be of help...
37
		@ini_set('memory_limit', '128M');
38
39
		switch ($mode)
40
		{
41
			case 'settings':
42
				$this->settings($id, $mode);
43
			break;
44
45
			case 'index':
46
				$this->index($id, $mode);
47
			break;
48
		}
49
	}
50
51
	function settings($id, $mode)
52
	{
53
		global $db, $user, $auth, $template, $cache;
54
		global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
55
56
		$submit = (isset($_POST['submit'])) ? true : false;
57
58
		$search_types = $this->get_search_types();
59
60
		$settings = array(
61
			'search_interval'			=> 'float',
62
			'search_anonymous_interval'	=> 'float',
63
			'load_search'				=> 'bool',
64
			'limit_search_load'			=> 'float',
65
			'min_search_author_chars'	=> 'integer',
66
			'search_store_results'		=> 'integer',
67
		);
68
69
		$search = null;
70
		$error = false;
71
		$search_options = '';
72
		foreach ($search_types as $type)
73
		{
74
			if ($this->init_search($type, $search, $error))
75
			{
76
				continue;
77
			}
78
79
			$name = ucfirst(strtolower(str_replace('_', ' ', $type)));
80
			$selected = ($config['search_type'] == $type) ? ' selected="selected"' : '';
81
			$search_options .= '<option value="' . $type . '"' . $selected . '>' . $name . '</option>';
82
83
			if (method_exists($search, 'acp'))
84
			{
85
				$vars = $search->acp();
86
87
				if (!$submit)
88
				{
89
					$template->assign_block_vars('backend', array(
90
						'NAME'		=> $name,
91
						'SETTINGS'	=> $vars['tpl'])
92
					);
93
				}
94
				else if (is_array($vars['config']))
95
				{
96
					$settings = array_merge($settings, $vars['config']);
97
				}
98
			}
99
		}
100
		unset($search);
101
		unset($error);
102
103
		$cfg_array = (isset($_REQUEST['config'])) ? request_var('config', array('' => ''), true) : array();
104
		$updated = request_var('updated', false);
105
106
		foreach ($settings as $config_name => $var_type)
107
		{
108
			if (!isset($cfg_array[$config_name]))
109
			{
110
				continue;
111
			}
112
113
			// e.g. integer:4:12 (min 4, max 12)
114
			$var_type = explode(':', $var_type);
115
116
			$config_value = $cfg_array[$config_name];
117
			settype($config_value, $var_type[0]);
118
119
			if (isset($var_type[1]))
120
			{
121
				$config_value = max($var_type[1], $config_value);
122
			}
123
124
			if (isset($var_type[2]))
125
			{
126
				$config_value = min($var_type[2], $config_value);
127
			}
128
129
			// only change config if anything was actually changed
130
			if ($submit && ($config[$config_name] != $config_value))
131
			{
132
				set_config($config_name, $config_value);
133
				$updated = true;
134
			}
135
		}
136
137
		if ($submit)
138
		{
139
			$extra_message = '';
140
			if ($updated)
141
			{
142
				add_log('admin', 'LOG_CONFIG_SEARCH');
143
			}
144
145
			if (isset($cfg_array['search_type']) && in_array($cfg_array['search_type'], $search_types, true) && ($cfg_array['search_type'] != $config['search_type']))
146
			{
147
				$search = null;
148
				$error = false;
149
150
				if (!$this->init_search($cfg_array['search_type'], $search, $error))
151
				{
152
					if (confirm_box(true))
153
					{
154
						if (!method_exists($search, 'init') || !($error = $search->init()))
155
						{
156
							set_config('search_type', $cfg_array['search_type']);
157
158
							if (!$updated)
159
							{
160
								add_log('admin', 'LOG_CONFIG_SEARCH');
161
							}
162
							$extra_message = '<br />' . $user->lang['SWITCHED_SEARCH_BACKEND'] . '<br /><a href="' . append_sid("{$phpbb_admin_path}index.$phpEx", 'i=search&amp;mode=index') . '">&raquo; ' . $user->lang['GO_TO_SEARCH_INDEX'] . '</a>';
163
						}
164
						else
165
						{
166
							trigger_error($error . adm_back_link($this->u_action), E_USER_WARNING);
167
						}
168
					}
169
					else
170
					{
171
						confirm_box(false, $user->lang['CONFIRM_SEARCH_BACKEND'], build_hidden_fields(array(
172
							'i'			=> $id,
173
							'mode'		=> $mode,
174
							'submit'	=> true,
175
							'updated'	=> $updated,
176
							'config'	=> array('search_type' => $cfg_array['search_type']),
177
						)));
178
					}
179
				}
180
				else
181
				{
182
					trigger_error($error . adm_back_link($this->u_action), E_USER_WARNING);
183
				}
184
			}
185
186
			trigger_error($user->lang['CONFIG_UPDATED'] . $extra_message . adm_back_link($this->u_action));
187
		}
188
		unset($cfg_array);
189
190
		$this->tpl_name = 'acp_search';
191
		$this->page_title = 'ACP_SEARCH_SETTINGS';
192
193
		$template->assign_vars(array(
194
			'LIMIT_SEARCH_LOAD'		=> (float) $config['limit_search_load'],
195
			'MIN_SEARCH_AUTHOR_CHARS'	=> (int) $config['min_search_author_chars'],
196
			'SEARCH_INTERVAL'		=> (float) $config['search_interval'],
197
			'SEARCH_GUEST_INTERVAL'	=> (float) $config['search_anonymous_interval'],
198
			'SEARCH_STORE_RESULTS'	=> (int) $config['search_store_results'],
199
200
			'S_SEARCH_TYPES'		=> $search_options,
201
			'S_YES_SEARCH'			=> (bool) $config['load_search'],
202
			'S_SETTINGS'			=> true,
203
204
			'U_ACTION'				=> $this->u_action)
205
		);
206
	}
207
208
	function index($id, $mode)
209
	{
210
		global $db, $user, $auth, $template, $cache;
211
		global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
212
213
		if (isset($_REQUEST['action']) && is_array($_REQUEST['action']))
214
		{
215
			$action = request_var('action', array('' => false));
216
			$action = key($action);
217
		}
218
		else
219
		{
220
			$action = request_var('action', '');
221
		}
222
		$this->state = explode(',', $config['search_indexing_state']);
223
224
		if (isset($_POST['cancel']))
225
		{
226
			$action = '';
227
			$this->state = array();
228
			$this->save_state();
229
		}
230
231
		if ($action)
232
		{
233
			switch ($action)
234
			{
235
				case 'progress_bar':
236
					$type = request_var('type', '');
237
					$this->display_progress_bar($type);
238
				break;
239
240
				case 'delete':
241
					$this->state[1] = 'delete';
242
				break;
243
244
				case 'create':
245
					$this->state[1] = 'create';
246
				break;
247
248
				default:
249
					trigger_error('NO_ACTION', E_USER_ERROR);
250
				break;
251
			}
252
253
			if (empty($this->state[0]))
254
			{
255
				$this->state[0] = request_var('search_type', '');
256
			}
257
258
			$this->search = null;
259
			$error = false;
260
			if ($this->init_search($this->state[0], $this->search, $error))
261
			{
262
				trigger_error($error . adm_back_link($this->u_action), E_USER_WARNING);
263
			}
264
			$name = ucfirst(strtolower(str_replace('_', ' ', $this->state[0])));
265
266
			$action = &$this->state[1];
267
268
			$this->max_post_id = $this->get_max_post_id();
269
270
			$post_counter = (isset($this->state[2])) ? $this->state[2] : 0;
271
			$this->state[2] = &$post_counter;
272
			$this->save_state();
273
274
			switch ($action)
275
			{
276
				case 'delete':
277
					if (method_exists($this->search, 'delete_index'))
278
					{
279
						// pass a reference to myself so the $search object can make use of save_state() and attributes
280
						if ($error = $this->search->delete_index($this, append_sid("{$phpbb_admin_path}index.$phpEx", "i=$id&mode=$mode&action=delete", false)))
281
						{
282
							$this->state = array('');
283
							$this->save_state();
284
							trigger_error($error . adm_back_link($this->u_action) . $this->close_popup_js(), E_USER_WARNING);
285
						}
286
					}
287
					else
288
					{
289
						$starttime = explode(' ', microtime());
290
						$starttime = $starttime[1] + $starttime[0];
291
						$row_count = 0;
292
						while (still_on_time() && $post_counter <= $this->max_post_id)
293
						{
294
							$sql = 'SELECT post_id, poster_id, forum_id
295
								FROM ' . POSTS_TABLE . '
296
								WHERE post_id >= ' . (int) ($post_counter + 1) . '
297
									AND post_id <= ' . (int) ($post_counter + $this->batch_size);
298
							$result = $db->sql_query($sql);
299
300
							$ids = $posters = $forum_ids = array();
301
							while ($row = $db->sql_fetchrow($result))
302
							{
303
								$ids[] = $row['post_id'];
304
								$posters[] = $row['poster_id'];
305
								$forum_ids[] = $row['forum_id'];
306
							}
307
							$db->sql_freeresult($result);
308
							$row_count += sizeof($ids);
309
310
							if (sizeof($ids))
311
							{
312
								$this->search->index_remove($ids, $posters, $forum_ids);
313
							}
314
315
							$post_counter += $this->batch_size;
316
						}
317
						// save the current state
318
						$this->save_state();
319
320
						if ($post_counter <= $this->max_post_id)
321
						{
322
							$mtime = explode(' ', microtime());
323
							$totaltime = $mtime[0] + $mtime[1] - $starttime;
324
							$rows_per_second = $row_count / $totaltime;
325
							meta_refresh(1, append_sid($this->u_action . '&amp;action=delete&amp;skip_rows=' . $post_counter));
326
							trigger_error(sprintf($user->lang['SEARCH_INDEX_DELETE_REDIRECT'], $post_counter, $row_count, $rows_per_second));
327
						}
328
					}
329
330
					$this->search->tidy();
331
332
					$this->state = array('');
333
					$this->save_state();
334
335
					add_log('admin', 'LOG_SEARCH_INDEX_REMOVED', $name);
336
					trigger_error($user->lang['SEARCH_INDEX_REMOVED'] . adm_back_link($this->u_action) . $this->close_popup_js());
337
				break;
338
339
				case 'create':
340
					if (method_exists($this->search, 'create_index'))
341
					{
342
						// pass a reference to acp_search so the $search object can make use of save_state() and attributes
343
						if ($error = $this->search->create_index($this, append_sid("{$phpbb_admin_path}index.$phpEx", "i=$id&mode=$mode&action=create", false)))
344
						{
345
							$this->state = array('');
346
							$this->save_state();
347
							trigger_error($error . adm_back_link($this->u_action) . $this->close_popup_js(), E_USER_WARNING);
348
						}
349
					}
350
					else
351
					{
352
						$sql = 'SELECT forum_id, enable_indexing
353
							FROM ' . FORUMS_TABLE;
354
						$result = $db->sql_query($sql, 3600);
355
356
						while ($row = $db->sql_fetchrow($result))
357
						{
358
							$forums[$row['forum_id']] = (bool) $row['enable_indexing'];
359
						}
360
						$db->sql_freeresult($result);
361
362
						$starttime = explode(' ', microtime());
363
						$starttime = $starttime[1] + $starttime[0];
364
						$row_count = 0;
365
						while (still_on_time() && $post_counter <= $this->max_post_id)
366
						{
367
							$sql = 'SELECT post_id, post_subject, post_text, poster_id, forum_id
368
								FROM ' . POSTS_TABLE . '
369
								WHERE post_id >= ' . (int) ($post_counter + 1) . '
370
									AND post_id <= ' . (int) ($post_counter + $this->batch_size);
371
							$result = $db->sql_query($sql);
372
373
							while ($row = $db->sql_fetchrow($result))
374
							{
375
								// Indexing enabled for this forum or global announcement?
376
								// Global announcements get indexed by default.
377
								if (!$row['forum_id'] || (isset($forums[$row['forum_id']]) && $forums[$row['forum_id']]))
378
								{
379
									$this->search->index('post', $row['post_id'], $row['post_text'], $row['post_subject'], $row['poster_id'], $row['forum_id']);
380
								}
381
								$row_count++;
382
							}
383
							$db->sql_freeresult($result);
384
385
							$post_counter += $this->batch_size;
386
						}
387
						// save the current state
388
						$this->save_state();
389
390
						// pretend the number of posts was as big as the number of ids we indexed so far
391
						// just an estimation as it includes deleted posts
392
						$num_posts = $config['num_posts'];
393
						$config['num_posts'] = min($config['num_posts'], $post_counter);
394
						$this->search->tidy();
395
						$config['num_posts'] = $num_posts;
396
397
						if ($post_counter <= $this->max_post_id)
398
						{
399
							$mtime = explode(' ', microtime());
400
							$totaltime = $mtime[0] + $mtime[1] - $starttime;
401
							$rows_per_second = $row_count / $totaltime;
402
							meta_refresh(1, append_sid($this->u_action . '&amp;action=create&amp;skip_rows=' . $post_counter));
403
							trigger_error(sprintf($user->lang['SEARCH_INDEX_CREATE_REDIRECT'], $post_counter, $row_count, $rows_per_second));
404
						}
405
					}
406
407
					$this->search->tidy();
408
409
					$this->state = array('');
410
					$this->save_state();
411
412
					add_log('admin', 'LOG_SEARCH_INDEX_CREATED', $name);
413
					trigger_error($user->lang['SEARCH_INDEX_CREATED'] . adm_back_link($this->u_action) . $this->close_popup_js());
414
				break;
415
			}
416
		}
417
418
		$search_types = $this->get_search_types();
419
420
		$search = null;
421
		$error = false;
422
		$search_options = '';
423
		foreach ($search_types as $type)
424
		{
425
			if ($this->init_search($type, $search, $error) || !method_exists($search, 'index_created'))
426
			{
427
				continue;
428
			}
429
430
			$name = ucfirst(strtolower(str_replace('_', ' ', $type)));
431
432
			$data = array();
433
			if (method_exists($search, 'index_stats'))
434
			{
435
				$data = $search->index_stats();
436
			}
437
438
			$statistics = array();
439
			foreach ($data as $statistic => $value)
440
			{
441
				$n = sizeof($statistics);
442
				if ($n && sizeof($statistics[$n - 1]) < 3)
443
				{
444
					$statistics[$n - 1] += array('statistic_2' => $statistic, 'value_2' => $value);
445
				}
446
				else
447
				{
448
					$statistics[] = array('statistic_1' => $statistic, 'value_1' => $value);
449
				}
450
			}
451
452
			$template->assign_block_vars('backend', array(
453
				'L_NAME'			=> $name,
454
				'NAME'				=> $type,
455
456
				'S_ACTIVE'			=> ($type == $config['search_type']) ? true : false,
457
				'S_HIDDEN_FIELDS'	=> build_hidden_fields(array('search_type' => $type)),
458
				'S_INDEXED'			=> (bool) $search->index_created(),
459
				'S_STATS'			=> (bool) sizeof($statistics))
460
			);
461
462
			foreach ($statistics as $statistic)
463
			{
464
				$template->assign_block_vars('backend.data', array(
465
					'STATISTIC_1'	=> $statistic['statistic_1'],
466
					'VALUE_1'		=> $statistic['value_1'],
467
					'STATISTIC_2'	=> (isset($statistic['statistic_2'])) ? $statistic['statistic_2'] : '',
468
					'VALUE_2'		=> (isset($statistic['value_2'])) ? $statistic['value_2'] : '')
469
				);
470
			}
471
		}
472
		unset($search);
473
		unset($error);
474
		unset($statistics);
475
		unset($data);
476
477
		$this->tpl_name = 'acp_search';
478
		$this->page_title = 'ACP_SEARCH_INDEX';
479
480
		$template->assign_vars(array(
481
			'S_INDEX'				=> true,
482
			'U_ACTION'				=> $this->u_action,
483
			'U_PROGRESS_BAR'		=> append_sid("{$phpbb_admin_path}index.$phpEx", "i=$id&amp;mode=$mode&amp;action=progress_bar"),
484
			'UA_PROGRESS_BAR'		=> addslashes(append_sid("{$phpbb_admin_path}index.$phpEx", "i=$id&amp;mode=$mode&amp;action=progress_bar")),
485
		));
486
487
		if (isset($this->state[1]))
488
		{
489
			$template->assign_vars(array(
490
				'S_CONTINUE_INDEXING'	=> $this->state[1],
491
				'U_CONTINUE_INDEXING'	=> $this->u_action . '&amp;action=' . $this->state[1],
492
				'L_CONTINUE'			=> ($this->state[1] == 'create') ? $user->lang['CONTINUE_INDEXING'] : $user->lang['CONTINUE_DELETING_INDEX'],
493
				'L_CONTINUE_EXPLAIN'	=> ($this->state[1] == 'create') ? $user->lang['CONTINUE_INDEXING_EXPLAIN'] : $user->lang['CONTINUE_DELETING_INDEX_EXPLAIN'])
494
			);
495
		}
496
	}
497
498
	function display_progress_bar($type)
499
	{
500
		global $template, $user;
501
502
		$l_type = ($type == 'create') ? 'INDEXING_IN_PROGRESS' : 'DELETING_INDEX_IN_PROGRESS';
503
504
		adm_page_header($user->lang[$l_type]);
505
506
		$template->set_filenames(array(
507
			'body'	=> 'progress_bar.html')
508
		);
509
510
		$template->assign_vars(array(
511
			'L_PROGRESS'			=> $user->lang[$l_type],
512
			'L_PROGRESS_EXPLAIN'	=> $user->lang[$l_type . '_EXPLAIN'])
513
		);
514
515
		adm_page_footer();
516
	}
517
518
	function close_popup_js()
519
	{
520
		return "<script type=\"text/javascript\">\n" .
521
			"<!--\n" .
522
			"	close_waitscreen = 1;\n" .
523
			"//-->\n" .
524
			"</script>\n";
525
	}
526
527
	function get_search_types()
528
	{
529
		global $phpbb_root_path, $phpEx;
530
531
		$search_types = array();
532
533
		$dp = @opendir($phpbb_root_path . 'includes/search');
534
535
		if ($dp)
536
		{
537
			while (($file = readdir($dp)) !== false)
538
			{
539
				if ((preg_match('#\.' . $phpEx . '$#', $file)) && ($file != "search.$phpEx"))
540
				{
541
					$search_types[] = preg_replace('#^(.*?)\.' . $phpEx . '$#', '\1', $file);
542
				}
543
			}
544
			closedir($dp);
545
546
			sort($search_types);
547
		}
548
549
		return $search_types;
550
	}
551
552
	function get_max_post_id()
553
	{
554
		global $db;
555
556
		$sql = 'SELECT MAX(post_id) as max_post_id
557
			FROM '. POSTS_TABLE;
558
		$result = $db->sql_query($sql);
559
		$max_post_id = (int) $db->sql_fetchfield('max_post_id');
560
		$db->sql_freeresult($result);
561
562
		return $max_post_id;
563
	}
564
565
	function save_state($state = false)
566
	{
567
		if ($state)
568
		{
569
			$this->state = $state;
570
		}
571
572
		ksort($this->state);
573
574
		set_config('search_indexing_state', implode(',', $this->state));
575
	}
576
577
	/**
578
	* Initialises a search backend object
579
	*
580
	* @return false if no error occurred else an error message
581
	*/
582
	function init_search($type, &$search, &$error)
583
	{
584
		global $phpbb_root_path, $phpEx, $user;
585
586
		if (!preg_match('#^\w+$#', $type) || !file_exists("{$phpbb_root_path}includes/search/$type.$phpEx"))
587
		{
588
			$error = $user->lang['NO_SUCH_SEARCH_MODULE'];
589
			return $error;
590
		}
591
592
		include_once("{$phpbb_root_path}includes/search/$type.$phpEx");
593
594
		if (!class_exists($type))
595
		{
596
			$error = $user->lang['NO_SUCH_SEARCH_MODULE'];
597
			return $error;
598
		}
599
600
		$error = false;
601
		$search = new $type($error);
602
603
		return $error;
604
	}
605
}
606
607
?>