aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/includes/functions.php
blob: 931eec8d7303b4bae51e329644d9995406617384 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
<?php
/***************************************************************************
 *                               functions.php
 *                            -------------------
 *   begin                : Saturday, Feb 13, 2001
 *   copyright            : (C) 2001 The phpBB Group
 *   email                : support@phpbb.com
 *
 *   $Id$
 *
 *
 ***************************************************************************/


/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *
 ***************************************************************************/

function get_db_stat($mode)
{
	global $db;

	switch($mode){
		case 'postcount':
			$sql = "SELECT count(*) AS total
				FROM ".POSTS_TABLE;
		break;

		case 'usercount':
			$sql = "SELECT count(*) AS total
						FROM ". USERS_TABLE ."
						WHERE user_id <> ".ANONYMOUS."
							AND user_level <> ".DELETED;
		break;

		case 'newestuser':
			$sql = "SELECT user_id, username
						FROM ".USERS_TABLE."
						WHERE user_id <> " . ANONYMOUS. "
							AND user_level <> ". DELETED ."
						ORDER BY user_id DESC
						LIMIT 1";
		break;

		case 'usersonline':
			$sql = "SELECT COUNT(*) AS online
				FROM ".SESSIONS_TABLE;
			break;
	}


	if(!$result = $db->sql_query($sql))
	{
		return 'ERROR';
	}
	else
	{
		$row = $db->sql_fetchrow($result);
		if($mode == 'newestuser')
		{
			return($row);
		}
		else if($mode == "usersonline")
		{
			return ($row['online']);
		}
		else
		{
			return($row['total']);
		}
	}
}

function get_userdata_from_id($userid) 
{
	global $db;

	$sql = "SELECT * 
		FROM ".USERS_TABLE." 
		WHERE user_id = $userid";
	if(!$result = $db->sql_query($sql)) 
	{
		$userdata = array("error" => "1");
		return ($userdata);
	}
	if($db->sql_numrows($result))
	{
		$myrow = $db->sql_fetchrowset($result);
		return($myrow[0]);
	}
	else
	{
		$userdata = array("error" => "1");
		return ($userdata);
	}
}

function get_userdata($username) {

	global $db;

	$sql = "SELECT * 
		FROM ".USERS_TABLE." 
		WHERE username = '$username' 
			AND user_level != ".DELETED;
	if(!$result = $db->sql_query($sql))
	{
		$userdata = array("error" => "1");
	}

	if($db->sql_numrows($result))
	{
		$myrow = $db->sql_fetchrowset($result);
		return($myrow[0]);
	}
	else
	{
		$userdata = array("error" => "1");
		return ($userdata);
	}
}

function make_jumpbox()
{
	global $db;
	global $l_jumpto, $l_noforums, $l_nocategories;

	$sql = "SELECT c.cat_id, c.cat_title, c.cat_order
		FROM ".CATEGORIES_TABLE." c, ".FORUMS_TABLE." f
		WHERE f.cat_id = c.cat_id
		GROUP BY c.cat_id, c.cat_title, c.cat_order
		ORDER BY c.cat_order";
	if(!$q_categories = $db->sql_query($sql))
	{
		error_die(SQL_QUERY, "Couldn't obtain category list.", __LINE__, __FILE__);
	}

	$total_categories = $db->sql_numrows();
	if($total_categories)
	{
		$category_rows = $db->sql_fetchrowset($q_categories);

		$limit_forums = "";

		$sql = "SELECT *
			FROM ".FORUMS_TABLE."
			ORDER BY cat_id, forum_order";
		if(!$q_forums = $db->sql_query($sql))
		{
			error_die(SQL_QUERY, "Couldn't obtain forums information.", __LINE__, __FILE__);
		}
		$total_forums = $db->sql_numrows($q_forums);
		$forum_rows = $db->sql_fetchrowset($q_forums);

		$boxstring = '';
		for($i = 0; $i < $total_categories; $i++)
		{
			$boxstring .= "<option value=\"-1\">&nbsp;</option>\n";
			$boxstring .= "<option value=\"-1\">".stripslashes($category_rows[$i]['cat_title'])."</option>\n";
			$boxstring .= "<option value=\"-1\">----------------</option>\n";

			if($total_forums)
			{
				for($y = 0; $y < $total_forums; $y++)
				{
					if(  $forum_rows[$y]['cat_id'] == $category_rows[$i]['cat_id'] )
					{
						$name = stripslashes($forum_rows[$y]['forum_name']);
						$boxstring .=  "<option value=\"".$forum_rows[$y]['forum_id']."\">$name</option>\n";
					}
				}
			}
			else
			{
				$boxstring .= "<option value=\"-1\">-- ! No Forums ! --</option>\n";
			}
		}
	}
	else
	{
		$boxstring .= "<option value=\"-1\">-- ! No Categories ! --</option>\n";
	}

	return($boxstring);
}

//
// Initialise user settings on page load
function init_userprefs($userdata)
{
	global $board_config, $theme, $template, $lang, $phpEx;

	if(!$board_config['override_user_themes'])
	{
		if(($userdata['user_id'] != ANONYMOUS || $userdata['user_id'] != DELETED) && $userdata['user_theme'])
		{
			$theme = setuptheme($userdata['user_theme']);
		}
		else
		{
			$theme = setuptheme($board_config['default_theme']);
		}
	}
	else
	{
		$theme = setuptheme($board_config['override_user_themes']);
	}
	if($userdata['user_lang'] != '')
	{
		$board_config['default_lang'] = $userdata['user_lang'];
	}
	if($userdata['user_dateformat'] != '')
	{
		$board_config['default_dateformat'] = $userdata['user_dateformat'];
	}
	if(isset($userdata['user_timezone']))
	{
		$board_config['default_timezone'] = $userdata['user_timezone'];
	}
	// Setup user's Template
	if($userdata['user_template'] != '')
	{
		$template = new Template("templates/".$userdata['user_template']);
	}
	else
	{
		$template = new Template("templates/".$board_config['default_template']);
	}

	//
	// This is currently worthless since all the individual
	// language variables will only be locally defined in this
	// function and not accessible to the board code globally.
	// This will be fixed by moving all $l_xxxx vars into a single
	// $lang[''] array
	//
	if( file_exists("language/lang_".$board_config['default_lang'].".".$phpEx) )
	{
		include('language/lang_'.$board_config['default_lang'].'.'.$phpEx);
	}
	else
	{
		include('language/lang_english.'.$phpEx);
	}

	return;
}

function setuptheme($theme)
{
	global $db;

	$sql = "SELECT *
		FROM ".THEMES_TABLE."
		WHERE themes_id = $theme";

	if(!$result = $db->sql_query($sql))
	{
		return(0);
	}
	if(!$myrow = $db->sql_fetchrow($result))
	{
		return(0);
	}
	return($myrow);
}

function generate_activation_key()
{
	$chars = array(
		"a","A","b","B","c","C","d","D","e","E","f","F","g","G","h","H","i","I","j","J",
		"k","K","l","L","m","M","n","N","o","O","p","P","q","Q","r","R","s","S","t","T",
		"u","U","v","V","w","W","x","X","y","Y","z","Z","1","2","3","4","5","6","7","8",
		"9","0"
	);

	$max_elements = count($chars) - 1;
	srand((double)microtime()*1000000);

	$act_key = '';
	for($i = 0; $i < 8; $i++)
	{
		$act_key .= $chars[rand(0,$max_elements)];
	}
	$act_key_md = md5($act_key);

	return($act_key_md);
}

function encode_ip($dotquad_ip)
{
	$ip_sep = explode(".", $dotquad_ip);
	return (sprintf("%02x%02x%02x%02x", $ip_sep[0], $ip_sep[1], $ip_sep[2], $ip_sep[3]));

//	$ip_p = (!empty($dotquad_proxy_ip)) ? explode(".", $dotquad_proxy_ip) : explode(".", "0.0.0.0");

//	return (sprintf("%03d.%03d.%03d.%03d:%03d.%03d.%03d.%03d", $ip[0], $ip[1], $ip[2], $ip[3], $ip_p[0], $ip_p[1], $ip_p[2], $ip_p[3]));

//	return (( $ip_sep[0] * 0xFFFFFF + $ip_sep[0] ) + ( $ip_sep[1] *   0xFFFF + $ip_sep[1] ) + ( $ip_sep[2] *     0xFF + $ip_sep[2] ) + ( $ip_sep[3] ) );
}

function decode_ip($int_ip)
{
	$hexipbang = explode(".",chunk_split($int_ip, 2, "."));
	return hexdec($hexipbang[0]).".".hexdec($hexipbang[1]).".".hexdec($hexipbang[2]).".".hexdec($hexipbang[3]);

//	list($ip['remote'], $ip['forwarded']) = explode(":", $c_ip);
//	return sprintf( "%d.%d.%d.%d", ( ( $int_ip >> 24 ) & 0xFF ), ( ( $int_ip >> 16 ) & 0xFF ), ( ( $int_ip >>  8 ) & 0xFF ), ( ( $int_ip       ) & 0xFF ) );
}

//
// Create date/time from format and timezone
//
function create_date($format, $gmepoch, $tz)
{
	return (gmdate($format, $gmepoch + (3600 * $tz)));
}

//
// Create a GMT timestamp
//
function get_gmt_ts()
{
	$time = time();
	return($time);
}

//
// Pagination routine, generates
// page number sequence
//
function generate_pagination($base_url, $num_items, $per_page, $start_item, $add_prevnext_text = TRUE)
{

	global $l_prevpage, $l_nextpage;

	$total_pages = ceil($num_items/$per_page);
	if($total_pages == 1)
	{
		return "";
	}

	$on_page = floor($start_item/$per_page) + 1;

	$page_string = "";

	$this_block_start = ($on_page < 10) ? 1 : floor($on_page/10) * 10;
	$this_block_end = ($on_page < 10) ? 9 : $this_block_start + 9;
	if($this_block_end > $total_pages)
	{
		$this_block_end = $total_pages;
	}

	for($i = $this_block_start; $i <= $this_block_end; $i++)
	{
		$page_string .= ($i == $on_page) ? "<b>$i</b>" : "<a href=\"".append_sid($base_url."&start=".(($i-1)*$per_page))."\">$i</a>";
		if($i <  $this_block_end)
		{
			$page_string .= ", ";
		}
	}

	if($this_block_start > 1)
	{
		$page_string_prepend = "";
		for($i = 0; $i < $this_block_start; $i+=10)
		{
			$page_string_prepend .= "<a href=\"".append_sid($base_url."&start=".($i*$per_page))."\">" . ( ($i == 0) ? ($i+1) : $i) . " - " . ($i+9) . "</a>, ";
		}

		$page_string = $page_string_prepend . $page_string;
	}

	if($this_block_end < $total_pages)
	{
		$page_string_append = ", ";
		for($i = $this_block_end + 1; $i < $total_pages; $i+=10)
		{
			$page_string_append .= "<a href=\"".append_sid($base_url."&start=".(($i*$per_page) - $per_page))."\">" . ( ($i == 0) ? ($i+1) : $i) . " - " . ((($i+9) < $total_pages) ? ($i+9) : $total_pages) ."</a>";
			if($i < $total_pages - 10)
			{
				$page_string_append .= ", ";
			}
		}

		$page_string .= $page_string_append;
	}

	if($add_prevnext_text)
	{
		if($on_page > 1)
		{
			$page_string = " <a href=\"".append_sid($base_url."&start=".(($on_page-2) * $per_page))."\">Previous</a>&nbsp;&nbsp;" . $page_string;
		}
		if($on_page < $total_pages)
		{
			$page_string .= "&nbsp;&nbsp;<a href=\"".append_sid($base_url."&start=".($on_page * $per_page))."\">Next</a>";
		}
	}

	return $page_string;

}

?>