includes/functions_content.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Sat, 10 Mar 2012 00:53:57 +0400
changeset 17 049ab39a9b9c
parent 0 ceef6d64c21d
permissions -rw-r--r--
Show gamers online everywhere
indvdum@0
     1
<?php
indvdum@0
     2
/**
indvdum@0
     3
*
indvdum@0
     4
* @package phpBB3
indvdum@0
     5
* @version $Id$
indvdum@0
     6
* @copyright (c) 2005 phpBB Group
indvdum@0
     7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
indvdum@0
     8
*
indvdum@0
     9
*/
indvdum@0
    10
indvdum@0
    11
/**
indvdum@0
    12
* @ignore
indvdum@0
    13
*/
indvdum@0
    14
if (!defined('IN_PHPBB'))
indvdum@0
    15
{
indvdum@0
    16
	exit;
indvdum@0
    17
}
indvdum@0
    18
indvdum@0
    19
/**
indvdum@0
    20
* gen_sort_selects()
indvdum@0
    21
* make_jumpbox()
indvdum@0
    22
* bump_topic_allowed()
indvdum@0
    23
* get_context()
indvdum@0
    24
* decode_message()
indvdum@0
    25
* strip_bbcode()
indvdum@0
    26
* generate_text_for_display()
indvdum@0
    27
* generate_text_for_storage()
indvdum@0
    28
* generate_text_for_edit()
indvdum@0
    29
* make_clickable_callback()
indvdum@0
    30
* make_clickable()
indvdum@0
    31
* censor_text()
indvdum@0
    32
* bbcode_nl2br()
indvdum@0
    33
* smiley_text()
indvdum@0
    34
* parse_attachments()
indvdum@0
    35
* extension_allowed()
indvdum@0
    36
* truncate_string()
indvdum@0
    37
* get_username_string()
indvdum@0
    38
* class bitfield
indvdum@0
    39
*/
indvdum@0
    40
indvdum@0
    41
/**
indvdum@0
    42
* Generate sort selection fields
indvdum@0
    43
*/
indvdum@0
    44
function gen_sort_selects(&$limit_days, &$sort_by_text, &$sort_days, &$sort_key, &$sort_dir, &$s_limit_days, &$s_sort_key, &$s_sort_dir, &$u_sort_param, $def_st = false, $def_sk = false, $def_sd = false)
indvdum@0
    45
{
indvdum@0
    46
	global $user;
indvdum@0
    47
indvdum@0
    48
	$sort_dir_text = array('a' => $user->lang['ASCENDING'], 'd' => $user->lang['DESCENDING']);
indvdum@0
    49
indvdum@0
    50
	$sorts = array(
indvdum@0
    51
		'st'	=> array(
indvdum@0
    52
			'key'		=> 'sort_days',
indvdum@0
    53
			'default'	=> $def_st,
indvdum@0
    54
			'options'	=> $limit_days,
indvdum@0
    55
			'output'	=> &$s_limit_days,
indvdum@0
    56
		),
indvdum@0
    57
indvdum@0
    58
		'sk'	=> array(
indvdum@0
    59
			'key'		=> 'sort_key',
indvdum@0
    60
			'default'	=> $def_sk,
indvdum@0
    61
			'options'	=> $sort_by_text,
indvdum@0
    62
			'output'	=> &$s_sort_key,
indvdum@0
    63
		),
indvdum@0
    64
indvdum@0
    65
		'sd'	=> array(
indvdum@0
    66
			'key'		=> 'sort_dir',
indvdum@0
    67
			'default'	=> $def_sd,
indvdum@0
    68
			'options'	=> $sort_dir_text,
indvdum@0
    69
			'output'	=> &$s_sort_dir,
indvdum@0
    70
		),
indvdum@0
    71
	);
indvdum@0
    72
	$u_sort_param  = '';
indvdum@0
    73
indvdum@0
    74
	foreach ($sorts as $name => $sort_ary)
indvdum@0
    75
	{
indvdum@0
    76
		$key = $sort_ary['key'];
indvdum@0
    77
		$selected = $$sort_ary['key'];
indvdum@0
    78
indvdum@0
    79
		// Check if the key is selectable. If not, we reset to the default or first key found.
indvdum@0
    80
		// This ensures the values are always valid. We also set $sort_dir/sort_key/etc. to the
indvdum@0
    81
		// correct value, else the protection is void. ;)
indvdum@0
    82
		if (!isset($sort_ary['options'][$selected]))
indvdum@0
    83
		{
indvdum@0
    84
			if ($sort_ary['default'] !== false)
indvdum@0
    85
			{
indvdum@0
    86
				$selected = $$key = $sort_ary['default'];
indvdum@0
    87
			}
indvdum@0
    88
			else
indvdum@0
    89
			{
indvdum@0
    90
				@reset($sort_ary['options']);
indvdum@0
    91
				$selected = $$key = key($sort_ary['options']);
indvdum@0
    92
			}
indvdum@0
    93
		}
indvdum@0
    94
indvdum@0
    95
		$sort_ary['output'] = '<select name="' . $name . '" id="' . $name . '">';
indvdum@0
    96
		foreach ($sort_ary['options'] as $option => $text)
indvdum@0
    97
		{
indvdum@0
    98
			$sort_ary['output'] .= '<option value="' . $option . '"' . (($selected == $option) ? ' selected="selected"' : '') . '>' . $text . '</option>';
indvdum@0
    99
		}
indvdum@0
   100
		$sort_ary['output'] .= '</select>';
indvdum@0
   101
indvdum@0
   102
		$u_sort_param .= ($selected !== $sort_ary['default']) ? ((strlen($u_sort_param)) ? '&amp;' : '') . "{$name}={$selected}" : '';
indvdum@0
   103
	}
indvdum@0
   104
indvdum@0
   105
	return;
indvdum@0
   106
}
indvdum@0
   107
indvdum@0
   108
/**
indvdum@0
   109
* Generate Jumpbox
indvdum@0
   110
*/
indvdum@0
   111
function make_jumpbox($action, $forum_id = false, $select_all = false, $acl_list = false, $force_display = false)
indvdum@0
   112
{
indvdum@0
   113
	global $config, $auth, $template, $user, $db;
indvdum@0
   114
indvdum@0
   115
	// We only return if the jumpbox is not forced to be displayed (in case it is needed for functionality)
indvdum@0
   116
	if (!$config['load_jumpbox'] && $force_display === false)
indvdum@0
   117
	{
indvdum@0
   118
		return;
indvdum@0
   119
	}
indvdum@0
   120
indvdum@0
   121
	$sql = 'SELECT forum_id, forum_name, parent_id, forum_type, left_id, right_id
indvdum@0
   122
		FROM ' . FORUMS_TABLE . '
indvdum@0
   123
		ORDER BY left_id ASC';
indvdum@0
   124
	$result = $db->sql_query($sql, 600);
indvdum@0
   125
indvdum@0
   126
	$right = $padding = 0;
indvdum@0
   127
	$padding_store = array('0' => 0);
indvdum@0
   128
	$display_jumpbox = false;
indvdum@0
   129
	$iteration = 0;
indvdum@0
   130
indvdum@0
   131
	// Sometimes it could happen that forums will be displayed here not be displayed within the index page
indvdum@0
   132
	// This is the result of forums not displayed at index, having list permissions and a parent of a forum with no permissions.
indvdum@0
   133
	// If this happens, the padding could be "broken"
indvdum@0
   134
indvdum@0
   135
	while ($row = $db->sql_fetchrow($result))
indvdum@0
   136
	{
indvdum@0
   137
		if ($row['left_id'] < $right)
indvdum@0
   138
		{
indvdum@0
   139
			$padding++;
indvdum@0
   140
			$padding_store[$row['parent_id']] = $padding;
indvdum@0
   141
		}
indvdum@0
   142
		else if ($row['left_id'] > $right + 1)
indvdum@0
   143
		{
indvdum@0
   144
			// Ok, if the $padding_store for this parent is empty there is something wrong. For now we will skip over it.
indvdum@0
   145
			// @todo digging deep to find out "how" this can happen.
indvdum@0
   146
			$padding = (isset($padding_store[$row['parent_id']])) ? $padding_store[$row['parent_id']] : $padding;
indvdum@0
   147
		}
indvdum@0
   148
indvdum@0
   149
		$right = $row['right_id'];
indvdum@0
   150
indvdum@0
   151
		if ($row['forum_type'] == FORUM_CAT && ($row['left_id'] + 1 == $row['right_id']))
indvdum@0
   152
		{
indvdum@0
   153
			// Non-postable forum with no subforums, don't display
indvdum@0
   154
			continue;
indvdum@0
   155
		}
indvdum@0
   156
indvdum@0
   157
		if (!$auth->acl_get('f_list', $row['forum_id']))
indvdum@0
   158
		{
indvdum@0
   159
			// if the user does not have permissions to list this forum skip
indvdum@0
   160
			continue;
indvdum@0
   161
		}
indvdum@0
   162
indvdum@0
   163
		if ($acl_list && !$auth->acl_gets($acl_list, $row['forum_id']))
indvdum@0
   164
		{
indvdum@0
   165
			continue;
indvdum@0
   166
		}
indvdum@0
   167
indvdum@0
   168
		if (!$display_jumpbox)
indvdum@0
   169
		{
indvdum@0
   170
			$template->assign_block_vars('jumpbox_forums', array(
indvdum@0
   171
				'FORUM_ID'		=> ($select_all) ? 0 : -1,
indvdum@0
   172
				'FORUM_NAME'	=> ($select_all) ? $user->lang['ALL_FORUMS'] : $user->lang['SELECT_FORUM'],
indvdum@0
   173
				'S_FORUM_COUNT'	=> $iteration)
indvdum@0
   174
			);
indvdum@0
   175
indvdum@0
   176
			$iteration++;
indvdum@0
   177
			$display_jumpbox = true;
indvdum@0
   178
		}
indvdum@0
   179
indvdum@0
   180
		$template->assign_block_vars('jumpbox_forums', array(
indvdum@0
   181
			'FORUM_ID'		=> $row['forum_id'],
indvdum@0
   182
			'FORUM_NAME'	=> $row['forum_name'],
indvdum@0
   183
			'SELECTED'		=> ($row['forum_id'] == $forum_id) ? ' selected="selected"' : '',
indvdum@0
   184
			'S_FORUM_COUNT'	=> $iteration,
indvdum@0
   185
			'S_IS_CAT'		=> ($row['forum_type'] == FORUM_CAT) ? true : false,
indvdum@0
   186
			'S_IS_LINK'		=> ($row['forum_type'] == FORUM_LINK) ? true : false,
indvdum@0
   187
			'S_IS_POST'		=> ($row['forum_type'] == FORUM_POST) ? true : false)
indvdum@0
   188
		);
indvdum@0
   189
indvdum@0
   190
		for ($i = 0; $i < $padding; $i++)
indvdum@0
   191
		{
indvdum@0
   192
			$template->assign_block_vars('jumpbox_forums.level', array());
indvdum@0
   193
		}
indvdum@0
   194
		$iteration++;
indvdum@0
   195
	}
indvdum@0
   196
	$db->sql_freeresult($result);
indvdum@0
   197
	unset($padding_store);
indvdum@0
   198
indvdum@0
   199
	$template->assign_vars(array(
indvdum@0
   200
		'S_DISPLAY_JUMPBOX'	=> $display_jumpbox,
indvdum@0
   201
		'S_JUMPBOX_ACTION'	=> $action)
indvdum@0
   202
	);
indvdum@0
   203
indvdum@0
   204
	return;
indvdum@0
   205
}
indvdum@0
   206
indvdum@0
   207
/**
indvdum@0
   208
* Bump Topic Check - used by posting and viewtopic
indvdum@0
   209
*/
indvdum@0
   210
function bump_topic_allowed($forum_id, $topic_bumped, $last_post_time, $topic_poster, $last_topic_poster)
indvdum@0
   211
{
indvdum@0
   212
	global $config, $auth, $user;
indvdum@0
   213
indvdum@0
   214
	// Check permission and make sure the last post was not already bumped
indvdum@0
   215
	if (!$auth->acl_get('f_bump', $forum_id) || $topic_bumped)
indvdum@0
   216
	{
indvdum@0
   217
		return false;
indvdum@0
   218
	}
indvdum@0
   219
indvdum@0
   220
	// Check bump time range, is the user really allowed to bump the topic at this time?
indvdum@0
   221
	$bump_time = ($config['bump_type'] == 'm') ? $config['bump_interval'] * 60 : (($config['bump_type'] == 'h') ? $config['bump_interval'] * 3600 : $config['bump_interval'] * 86400);
indvdum@0
   222
indvdum@0
   223
	// Check bump time
indvdum@0
   224
	if ($last_post_time + $bump_time > time())
indvdum@0
   225
	{
indvdum@0
   226
		return false;
indvdum@0
   227
	}
indvdum@0
   228
indvdum@0
   229
	// Check bumper, only topic poster and last poster are allowed to bump
indvdum@0
   230
	if ($topic_poster != $user->data['user_id'] && $last_topic_poster != $user->data['user_id'])
indvdum@0
   231
	{
indvdum@0
   232
		return false;
indvdum@0
   233
	}
indvdum@0
   234
indvdum@0
   235
	// A bump time of 0 will completely disable the bump feature... not intended but might be useful.
indvdum@0
   236
	return $bump_time;
indvdum@0
   237
}
indvdum@0
   238
indvdum@0
   239
/**
indvdum@0
   240
* Generates a text with approx. the specified length which contains the specified words and their context
indvdum@0
   241
*
indvdum@0
   242
* @param	string	$text	The full text from which context shall be extracted
indvdum@0
   243
* @param	string	$words	An array of words which should be contained in the result, has to be a valid part of a PCRE pattern (escape with preg_quote!)
indvdum@0
   244
* @param	int		$length	The desired length of the resulting text, however the result might be shorter or longer than this value
indvdum@0
   245
*
indvdum@0
   246
* @return	string			Context of the specified words separated by "..."
indvdum@0
   247
*/
indvdum@0
   248
function get_context($text, $words, $length = 400)
indvdum@0
   249
{
indvdum@0
   250
	// first replace all whitespaces with single spaces
indvdum@0
   251
	$text = preg_replace('/ +/', ' ', strtr($text, "\t\n\r\x0C ", '     '));
indvdum@0
   252
indvdum@0
   253
	// we need to turn the entities back into their original form, to not cut the message in between them
indvdum@0
   254
	$entities = array('&lt;', '&gt;', '&#91;', '&#93;', '&#46;', '&#58;', '&#058;');
indvdum@0
   255
	$characters = array('<', '>', '[', ']', '.', ':', ':');
indvdum@0
   256
	$text = str_replace($entities, $characters, $text);
indvdum@0
   257
indvdum@0
   258
	$word_indizes = array();
indvdum@0
   259
	if (sizeof($words))
indvdum@0
   260
	{
indvdum@0
   261
		$match = '';
indvdum@0
   262
		// find the starting indizes of all words
indvdum@0
   263
		foreach ($words as $word)
indvdum@0
   264
		{
indvdum@0
   265
			if ($word)
indvdum@0
   266
			{
indvdum@0
   267
				if (preg_match('#(?:[^\w]|^)(' . $word . ')(?:[^\w]|$)#i', $text, $match))
indvdum@0
   268
				{
indvdum@0
   269
					if (empty($match[1]))
indvdum@0
   270
					{
indvdum@0
   271
						continue;
indvdum@0
   272
					}
indvdum@0
   273
indvdum@0
   274
					$pos = utf8_strpos($text, $match[1]);
indvdum@0
   275
					if ($pos !== false)
indvdum@0
   276
					{
indvdum@0
   277
						$word_indizes[] = $pos;
indvdum@0
   278
					}
indvdum@0
   279
				}
indvdum@0
   280
			}
indvdum@0
   281
		}
indvdum@0
   282
		unset($match);
indvdum@0
   283
indvdum@0
   284
		if (sizeof($word_indizes))
indvdum@0
   285
		{
indvdum@0
   286
			$word_indizes = array_unique($word_indizes);
indvdum@0
   287
			sort($word_indizes);
indvdum@0
   288
indvdum@0
   289
			$wordnum = sizeof($word_indizes);
indvdum@0
   290
			// number of characters on the right and left side of each word
indvdum@0
   291
			$sequence_length = (int) ($length / (2 * $wordnum)) - 2;
indvdum@0
   292
			$final_text = '';
indvdum@0
   293
			$word = $j = 0;
indvdum@0
   294
			$final_text_index = -1;
indvdum@0
   295
indvdum@0
   296
			// cycle through every character in the original text
indvdum@0
   297
			for ($i = $word_indizes[$word], $n = utf8_strlen($text); $i < $n; $i++)
indvdum@0
   298
			{
indvdum@0
   299
				// if the current position is the start of one of the words then append $sequence_length characters to the final text
indvdum@0
   300
				if (isset($word_indizes[$word]) && ($i == $word_indizes[$word]))
indvdum@0
   301
				{
indvdum@0
   302
					if ($final_text_index < $i - $sequence_length - 1)
indvdum@0
   303
					{
indvdum@0
   304
						$final_text .= '... ' . preg_replace('#^([^ ]*)#', '', utf8_substr($text, $i - $sequence_length, $sequence_length));
indvdum@0
   305
					}
indvdum@0
   306
					else
indvdum@0
   307
					{
indvdum@0
   308
						// if the final text is already nearer to the current word than $sequence_length we only append the text
indvdum@0
   309
						// from its current index on and distribute the unused length to all other sequenes
indvdum@0
   310
						$sequence_length += (int) (($final_text_index - $i + $sequence_length + 1) / (2 * $wordnum));
indvdum@0
   311
						$final_text .= utf8_substr($text, $final_text_index + 1, $i - $final_text_index - 1);
indvdum@0
   312
					}
indvdum@0
   313
					$final_text_index = $i - 1;
indvdum@0
   314
indvdum@0
   315
					// add the following characters to the final text (see below)
indvdum@0
   316
					$word++;
indvdum@0
   317
					$j = 1;
indvdum@0
   318
				}
indvdum@0
   319
indvdum@0
   320
				if ($j > 0)
indvdum@0
   321
				{
indvdum@0
   322
					// add the character to the final text and increment the sequence counter
indvdum@0
   323
					$final_text .= utf8_substr($text, $i, 1);
indvdum@0
   324
					$final_text_index++;
indvdum@0
   325
					$j++;
indvdum@0
   326
indvdum@0
   327
					// if this is a whitespace then check whether we are done with this sequence
indvdum@0
   328
					if (utf8_substr($text, $i, 1) == ' ')
indvdum@0
   329
					{
indvdum@0
   330
						// only check whether we have to exit the context generation completely if we haven't already reached the end anyway
indvdum@0
   331
						if ($i + 4 < $n)
indvdum@0
   332
						{
indvdum@0
   333
							if (($j > $sequence_length && $word >= $wordnum) || utf8_strlen($final_text) > $length)
indvdum@0
   334
							{
indvdum@0
   335
								$final_text .= ' ...';
indvdum@0
   336
								break;
indvdum@0
   337
							}
indvdum@0
   338
						}
indvdum@0
   339
						else
indvdum@0
   340
						{
indvdum@0
   341
							// make sure the text really reaches the end
indvdum@0
   342
							$j -= 4;
indvdum@0
   343
						}
indvdum@0
   344
indvdum@0
   345
						// stop context generation and wait for the next word
indvdum@0
   346
						if ($j > $sequence_length)
indvdum@0
   347
						{
indvdum@0
   348
							$j = 0;
indvdum@0
   349
						}
indvdum@0
   350
					}
indvdum@0
   351
				}
indvdum@0
   352
			}
indvdum@0
   353
			return str_replace($characters, $entities, $final_text);
indvdum@0
   354
		}
indvdum@0
   355
	}
indvdum@0
   356
indvdum@0
   357
	if (!sizeof($words) || !sizeof($word_indizes))
indvdum@0
   358
	{
indvdum@0
   359
		return str_replace($characters, $entities, ((utf8_strlen($text) >= $length + 3) ? utf8_substr($text, 0, $length) . '...' : $text));
indvdum@0
   360
	}
indvdum@0
   361
}
indvdum@0
   362
indvdum@0
   363
/**
indvdum@0
   364
* Decode text whereby text is coming from the db and expected to be pre-parsed content
indvdum@0
   365
* We are placing this outside of the message parser because we are often in need of it...
indvdum@0
   366
*/
indvdum@0
   367
function decode_message(&$message, $bbcode_uid = '')
indvdum@0
   368
{
indvdum@0
   369
	global $config;
indvdum@0
   370
indvdum@0
   371
	if ($bbcode_uid)
indvdum@0
   372
	{
indvdum@0
   373
		$match = array('<br />', "[/*:m:$bbcode_uid]", ":u:$bbcode_uid", ":o:$bbcode_uid", ":$bbcode_uid");
indvdum@0
   374
		$replace = array("\n", '', '', '', '');
indvdum@0
   375
	}
indvdum@0
   376
	else
indvdum@0
   377
	{
indvdum@0
   378
		$match = array('<br />');
indvdum@0
   379
		$replace = array("\n");
indvdum@0
   380
	}
indvdum@0
   381
indvdum@0
   382
	$message = str_replace($match, $replace, $message);
indvdum@0
   383
indvdum@0
   384
	$match = get_preg_expression('bbcode_htm');
indvdum@0
   385
	$replace = array('\1', '\1', '\2', '\1', '', '');
indvdum@0
   386
indvdum@0
   387
	$message = preg_replace($match, $replace, $message);
indvdum@0
   388
}
indvdum@0
   389
indvdum@0
   390
/**
indvdum@0
   391
* Strips all bbcode from a text and returns the plain content
indvdum@0
   392
*/
indvdum@0
   393
function strip_bbcode(&$text, $uid = '')
indvdum@0
   394
{
indvdum@0
   395
	if (!$uid)
indvdum@0
   396
	{
indvdum@0
   397
		$uid = '[0-9a-z]{5,}';
indvdum@0
   398
	}
indvdum@0
   399
indvdum@0
   400
	$text = preg_replace("#\[\/?[a-z0-9\*\+\-]+(?:=(?:&quot;.*&quot;|[^\]]*))?(?::[a-z])?(\:$uid)\]#", ' ', $text);
indvdum@0
   401
indvdum@0
   402
	$match = get_preg_expression('bbcode_htm');
indvdum@0
   403
	$replace = array('\1', '\1', '\2', '\1', '', '');
indvdum@0
   404
indvdum@0
   405
	$text = preg_replace($match, $replace, $text);
indvdum@0
   406
}
indvdum@0
   407
indvdum@0
   408
/**
indvdum@0
   409
* For display of custom parsed text on user-facing pages
indvdum@0
   410
* Expects $text to be the value directly from the database (stored value)
indvdum@0
   411
*/
indvdum@0
   412
function generate_text_for_display($text, $uid, $bitfield, $flags)
indvdum@0
   413
{
indvdum@0
   414
	static $bbcode;
indvdum@0
   415
indvdum@0
   416
	if (!$text)
indvdum@0
   417
	{
indvdum@0
   418
		return '';
indvdum@0
   419
	}
indvdum@0
   420
indvdum@0
   421
	$text = censor_text($text);
indvdum@0
   422
indvdum@0
   423
	// Parse bbcode if bbcode uid stored and bbcode enabled
indvdum@0
   424
	if ($uid && ($flags & OPTION_FLAG_BBCODE))
indvdum@0
   425
	{
indvdum@0
   426
		if (!class_exists('bbcode'))
indvdum@0
   427
		{
indvdum@0
   428
			global $phpbb_root_path, $phpEx;
indvdum@0
   429
			include($phpbb_root_path . 'includes/bbcode.' . $phpEx);
indvdum@0
   430
		}
indvdum@0
   431
indvdum@0
   432
		if (empty($bbcode))
indvdum@0
   433
		{
indvdum@0
   434
			$bbcode = new bbcode($bitfield);
indvdum@0
   435
		}
indvdum@0
   436
		else
indvdum@0
   437
		{
indvdum@0
   438
			$bbcode->bbcode($bitfield);
indvdum@0
   439
		}
indvdum@0
   440
indvdum@0
   441
		$bbcode->bbcode_second_pass($text, $uid);
indvdum@0
   442
	}
indvdum@0
   443
indvdum@0
   444
	$text = bbcode_nl2br($text);
indvdum@0
   445
	$text = smiley_text($text, !($flags & OPTION_FLAG_SMILIES));
indvdum@0
   446
indvdum@0
   447
	return $text;
indvdum@0
   448
}
indvdum@0
   449
indvdum@0
   450
/**
indvdum@0
   451
* For parsing custom parsed text to be stored within the database.
indvdum@0
   452
* This function additionally returns the uid and bitfield that needs to be stored.
indvdum@0
   453
* Expects $text to be the value directly from request_var() and in it's non-parsed form
indvdum@0
   454
*/
indvdum@0
   455
function generate_text_for_storage(&$text, &$uid, &$bitfield, &$flags, $allow_bbcode = false, $allow_urls = false, $allow_smilies = false)
indvdum@0
   456
{
indvdum@0
   457
	global $phpbb_root_path, $phpEx;
indvdum@0
   458
indvdum@0
   459
	$uid = $bitfield = '';
indvdum@0
   460
	$flags = (($allow_bbcode) ? OPTION_FLAG_BBCODE : 0) + (($allow_smilies) ? OPTION_FLAG_SMILIES : 0) + (($allow_urls) ? OPTION_FLAG_LINKS : 0);
indvdum@0
   461
indvdum@0
   462
	if (!$text)
indvdum@0
   463
	{
indvdum@0
   464
		return;
indvdum@0
   465
	}
indvdum@0
   466
indvdum@0
   467
	if (!class_exists('parse_message'))
indvdum@0
   468
	{
indvdum@0
   469
		include($phpbb_root_path . 'includes/message_parser.' . $phpEx);
indvdum@0
   470
	}
indvdum@0
   471
indvdum@0
   472
	$message_parser = new parse_message($text);
indvdum@0
   473
	$message_parser->parse($allow_bbcode, $allow_urls, $allow_smilies);
indvdum@0
   474
indvdum@0
   475
	$text = $message_parser->message;
indvdum@0
   476
	$uid = $message_parser->bbcode_uid;
indvdum@0
   477
indvdum@0
   478
	// If the bbcode_bitfield is empty, there is no need for the uid to be stored.
indvdum@0
   479
	if (!$message_parser->bbcode_bitfield)
indvdum@0
   480
	{
indvdum@0
   481
		$uid = '';
indvdum@0
   482
	}
indvdum@0
   483
indvdum@0
   484
	$bitfield = $message_parser->bbcode_bitfield;
indvdum@0
   485
indvdum@0
   486
	return;
indvdum@0
   487
}
indvdum@0
   488
indvdum@0
   489
/**
indvdum@0
   490
* For decoding custom parsed text for edits as well as extracting the flags
indvdum@0
   491
* Expects $text to be the value directly from the database (pre-parsed content)
indvdum@0
   492
*/
indvdum@0
   493
function generate_text_for_edit($text, $uid, $flags)
indvdum@0
   494
{
indvdum@0
   495
	global $phpbb_root_path, $phpEx;
indvdum@0
   496
indvdum@0
   497
	decode_message($text, $uid);
indvdum@0
   498
indvdum@0
   499
	return array(
indvdum@0
   500
		'allow_bbcode'	=> ($flags & OPTION_FLAG_BBCODE) ? 1 : 0,
indvdum@0
   501
		'allow_smilies'	=> ($flags & OPTION_FLAG_SMILIES) ? 1 : 0,
indvdum@0
   502
		'allow_urls'	=> ($flags & OPTION_FLAG_LINKS) ? 1 : 0,
indvdum@0
   503
		'text'			=> $text
indvdum@0
   504
	);
indvdum@0
   505
}
indvdum@0
   506
indvdum@0
   507
/**
indvdum@0
   508
* A subroutine of make_clickable used with preg_replace
indvdum@0
   509
* It places correct HTML around an url, shortens the displayed text
indvdum@0
   510
* and makes sure no entities are inside URLs
indvdum@0
   511
*/
indvdum@0
   512
function make_clickable_callback($type, $whitespace, $url, $relative_url, $class)
indvdum@0
   513
{
indvdum@0
   514
	$orig_url		= $url;
indvdum@0
   515
	$orig_relative	= $relative_url;
indvdum@0
   516
	$append			= '';
indvdum@0
   517
	$url			= htmlspecialchars_decode($url);
indvdum@0
   518
	$relative_url	= htmlspecialchars_decode($relative_url);
indvdum@0
   519
indvdum@0
   520
	// make sure no HTML entities were matched
indvdum@0
   521
	$chars = array('<', '>', '"');
indvdum@0
   522
	$split = false;
indvdum@0
   523
indvdum@0
   524
	foreach ($chars as $char)
indvdum@0
   525
	{
indvdum@0
   526
		$next_split = strpos($url, $char);
indvdum@0
   527
		if ($next_split !== false)
indvdum@0
   528
		{
indvdum@0
   529
			$split = ($split !== false) ? min($split, $next_split) : $next_split;
indvdum@0
   530
		}
indvdum@0
   531
	}
indvdum@0
   532
indvdum@0
   533
	if ($split !== false)
indvdum@0
   534
	{
indvdum@0
   535
		// an HTML entity was found, so the URL has to end before it
indvdum@0
   536
		$append			= substr($url, $split) . $relative_url;
indvdum@0
   537
		$url			= substr($url, 0, $split);
indvdum@0
   538
		$relative_url	= '';
indvdum@0
   539
	}
indvdum@0
   540
	else if ($relative_url)
indvdum@0
   541
	{
indvdum@0
   542
		// same for $relative_url
indvdum@0
   543
		$split = false;
indvdum@0
   544
		foreach ($chars as $char)
indvdum@0
   545
		{
indvdum@0
   546
			$next_split = strpos($relative_url, $char);
indvdum@0
   547
			if ($next_split !== false)
indvdum@0
   548
			{
indvdum@0
   549
				$split = ($split !== false) ? min($split, $next_split) : $next_split;
indvdum@0
   550
			}
indvdum@0
   551
		}
indvdum@0
   552
indvdum@0
   553
		if ($split !== false)
indvdum@0
   554
		{
indvdum@0
   555
			$append			= substr($relative_url, $split);
indvdum@0
   556
			$relative_url	= substr($relative_url, 0, $split);
indvdum@0
   557
		}
indvdum@0
   558
	}
indvdum@0
   559
indvdum@0
   560
	// if the last character of the url is a punctuation mark, exclude it from the url
indvdum@0
   561
	$last_char = ($relative_url) ? $relative_url[strlen($relative_url) - 1] : $url[strlen($url) - 1];
indvdum@0
   562
indvdum@0
   563
	switch ($last_char)
indvdum@0
   564
	{
indvdum@0
   565
		case '.':
indvdum@0
   566
		case '?':
indvdum@0
   567
		case '!':
indvdum@0
   568
		case ':':
indvdum@0
   569
		case ',':
indvdum@0
   570
			$append = $last_char;
indvdum@0
   571
			if ($relative_url)
indvdum@0
   572
			{
indvdum@0
   573
				$relative_url = substr($relative_url, 0, -1);
indvdum@0
   574
			}
indvdum@0
   575
			else
indvdum@0
   576
			{
indvdum@0
   577
				$url = substr($url, 0, -1);
indvdum@0
   578
			}
indvdum@0
   579
		break;
indvdum@0
   580
indvdum@0
   581
		// set last_char to empty here, so the variable can be used later to
indvdum@0
   582
		// check whether a character was removed
indvdum@0
   583
		default:
indvdum@0
   584
			$last_char = '';
indvdum@0
   585
		break;
indvdum@0
   586
	}
indvdum@0
   587
indvdum@0
   588
	$short_url = (strlen($url) > 55) ? substr($url, 0, 39) . ' ... ' . substr($url, -10) : $url;
indvdum@0
   589
indvdum@0
   590
	switch ($type)
indvdum@0
   591
	{
indvdum@0
   592
		case MAGIC_URL_LOCAL:
indvdum@0
   593
			$tag			= 'l';
indvdum@0
   594
			$relative_url	= preg_replace('/[&?]sid=[0-9a-f]{32}$/', '', preg_replace('/([&?])sid=[0-9a-f]{32}&/', '$1', $relative_url));
indvdum@0
   595
			$url			= $url . '/' . $relative_url;
indvdum@0
   596
			$text			= $relative_url;
indvdum@0
   597
indvdum@0
   598
			// this url goes to http://domain.tld/path/to/board/ which
indvdum@0
   599
			// would result in an empty link if treated as local so
indvdum@0
   600
			// don't touch it and let MAGIC_URL_FULL take care of it.
indvdum@0
   601
			if (!$relative_url)
indvdum@0
   602
			{
indvdum@0
   603
				return $whitespace . $orig_url . '/' . $orig_relative; // slash is taken away by relative url pattern
indvdum@0
   604
			}
indvdum@0
   605
		break;
indvdum@0
   606
indvdum@0
   607
		case MAGIC_URL_FULL:
indvdum@0
   608
			$tag	= 'm';
indvdum@0
   609
			$text	= $short_url;
indvdum@0
   610
		break;
indvdum@0
   611
indvdum@0
   612
		case MAGIC_URL_WWW:
indvdum@0
   613
			$tag	= 'w';
indvdum@0
   614
			$url	= 'http://' . $url;
indvdum@0
   615
			$text	= $short_url;
indvdum@0
   616
		break;
indvdum@0
   617
indvdum@0
   618
		case MAGIC_URL_EMAIL:
indvdum@0
   619
			$tag	= 'e';
indvdum@0
   620
			$text	= $short_url;
indvdum@0
   621
			$url	= 'mailto:' . $url;
indvdum@0
   622
		break;
indvdum@0
   623
	}
indvdum@0
   624
indvdum@0
   625
	$url	= htmlspecialchars($url);
indvdum@0
   626
	$text	= htmlspecialchars($text);
indvdum@0
   627
	$append	= htmlspecialchars($append);
indvdum@0
   628
indvdum@0
   629
	$html	= "$whitespace<!-- $tag --><a$class href=\"$url\">$text</a><!-- $tag -->$append";
indvdum@0
   630
indvdum@0
   631
	return $html;
indvdum@0
   632
}
indvdum@0
   633
indvdum@0
   634
/**
indvdum@0
   635
* make_clickable function
indvdum@0
   636
*
indvdum@0
   637
* Replace magic urls of form http://xxx.xxx., www.xxx. and xxx@xxx.xxx.
indvdum@0
   638
* Cuts down displayed size of link if over 50 chars, turns absolute links
indvdum@0
   639
* into relative versions when the server/script path matches the link
indvdum@0
   640
*/
indvdum@0
   641
function make_clickable($text, $server_url = false, $class = 'postlink')
indvdum@0
   642
{
indvdum@0
   643
	if ($server_url === false)
indvdum@0
   644
	{
indvdum@0
   645
		$server_url = generate_board_url();
indvdum@0
   646
	}
indvdum@0
   647
indvdum@0
   648
	static $magic_url_match;
indvdum@0
   649
	static $magic_url_replace;
indvdum@0
   650
	static $static_class;
indvdum@0
   651
indvdum@0
   652
	if (!is_array($magic_url_match) || $static_class != $class)
indvdum@0
   653
	{
indvdum@0
   654
		$static_class = $class;
indvdum@0
   655
		$class = ($static_class) ? ' class="' . $static_class . '"' : '';
indvdum@0
   656
		$local_class = ($static_class) ? ' class="' . $static_class . '-local"' : '';
indvdum@0
   657
indvdum@0
   658
		$magic_url_match = $magic_url_replace = array();
indvdum@0
   659
		// Be sure to not let the matches cross over. ;)
indvdum@0
   660
indvdum@0
   661
		// relative urls for this board
indvdum@0
   662
		$magic_url_match[] = '#(^|[\n\t (>.])(' . preg_quote($server_url, '#') . ')/(' . get_preg_expression('relative_url_inline') . ')#ie';
indvdum@0
   663
		$magic_url_replace[] = "make_clickable_callback(MAGIC_URL_LOCAL, '\$1', '\$2', '\$3', '$local_class')";
indvdum@0
   664
indvdum@0
   665
		// matches a xxxx://aaaaa.bbb.cccc. ...
indvdum@0
   666
		$magic_url_match[] = '#(^|[\n\t (>.])(' . get_preg_expression('url_inline') . ')#ie';
indvdum@0
   667
		$magic_url_replace[] = "make_clickable_callback(MAGIC_URL_FULL, '\$1', '\$2', '', '$class')";
indvdum@0
   668
indvdum@0
   669
		// matches a "www.xxxx.yyyy[/zzzz]" kinda lazy URL thing
indvdum@0
   670
		$magic_url_match[] = '#(^|[\n\t (>])(' . get_preg_expression('www_url_inline') . ')#ie';
indvdum@0
   671
		$magic_url_replace[] = "make_clickable_callback(MAGIC_URL_WWW, '\$1', '\$2', '', '$class')";
indvdum@0
   672
indvdum@0
   673
		// matches an email@domain type address at the start of a line, or after a space or after what might be a BBCode.
indvdum@0
   674
		$magic_url_match[] = '/(^|[\n\t (>])(' . get_preg_expression('email') . ')/ie';
indvdum@0
   675
		$magic_url_replace[] = "make_clickable_callback(MAGIC_URL_EMAIL, '\$1', '\$2', '', '')";
indvdum@0
   676
	}
indvdum@0
   677
indvdum@0
   678
	return preg_replace($magic_url_match, $magic_url_replace, $text);
indvdum@0
   679
}
indvdum@0
   680
indvdum@0
   681
/**
indvdum@0
   682
* Censoring
indvdum@0
   683
*/
indvdum@0
   684
function censor_text($text)
indvdum@0
   685
{
indvdum@0
   686
	static $censors;
indvdum@0
   687
indvdum@0
   688
	// Nothing to do?
indvdum@0
   689
	if ($text === '')
indvdum@0
   690
	{
indvdum@0
   691
		return '';
indvdum@0
   692
	}
indvdum@0
   693
indvdum@0
   694
	// We moved the word censor checks in here because we call this function quite often - and then only need to do the check once
indvdum@0
   695
	if (!isset($censors) || !is_array($censors))
indvdum@0
   696
	{
indvdum@0
   697
		global $config, $user, $auth, $cache;
indvdum@0
   698
indvdum@0
   699
		// We check here if the user is having viewing censors disabled (and also allowed to do so).
indvdum@0
   700
		if (!$user->optionget('viewcensors') && $config['allow_nocensors'] && $auth->acl_get('u_chgcensors'))
indvdum@0
   701
		{
indvdum@0
   702
			$censors = array();
indvdum@0
   703
		}
indvdum@0
   704
		else
indvdum@0
   705
		{
indvdum@0
   706
			$censors = $cache->obtain_word_list();
indvdum@0
   707
		}
indvdum@0
   708
	}
indvdum@0
   709
indvdum@0
   710
	if (sizeof($censors))
indvdum@0
   711
	{
indvdum@0
   712
		return preg_replace($censors['match'], $censors['replace'], $text);
indvdum@0
   713
	}
indvdum@0
   714
indvdum@0
   715
	return $text;
indvdum@0
   716
}
indvdum@0
   717
indvdum@0
   718
/**
indvdum@0
   719
* custom version of nl2br which takes custom BBCodes into account
indvdum@0
   720
*/
indvdum@0
   721
function bbcode_nl2br($text)
indvdum@0
   722
{
indvdum@0
   723
	// custom BBCodes might contain carriage returns so they
indvdum@0
   724
	// are not converted into <br /> so now revert that
indvdum@0
   725
	$text = str_replace(array("\n", "\r"), array('<br />', "\n"), $text);
indvdum@0
   726
	return $text;
indvdum@0
   727
}
indvdum@0
   728
indvdum@0
   729
/**
indvdum@0
   730
* Smiley processing
indvdum@0
   731
*/
indvdum@0
   732
function smiley_text($text, $force_option = false)
indvdum@0
   733
{
indvdum@0
   734
	global $config, $user, $phpbb_root_path;
indvdum@0
   735
indvdum@0
   736
	if ($force_option || !$config['allow_smilies'] || !$user->optionget('viewsmilies'))
indvdum@0
   737
	{
indvdum@0
   738
		return preg_replace('#<!\-\- s(.*?) \-\-><img src="\{SMILIES_PATH\}\/.*? \/><!\-\- s\1 \-\->#', '\1', $text);
indvdum@0
   739
	}
indvdum@0
   740
	else
indvdum@0
   741
	{
indvdum@0
   742
		$root_path = (defined('PHPBB_USE_BOARD_URL_PATH') && PHPBB_USE_BOARD_URL_PATH) ? generate_board_url() . '/' : $phpbb_root_path;
indvdum@0
   743
		return preg_replace('#<!\-\- s(.*?) \-\-><img src="\{SMILIES_PATH\}\/(.*?) \/><!\-\- s\1 \-\->#', '<img src="' . $root_path . $config['smilies_path'] . '/\2 />', $text);
indvdum@0
   744
	}
indvdum@0
   745
}
indvdum@0
   746
indvdum@0
   747
/**
indvdum@0
   748
* General attachment parsing
indvdum@0
   749
*
indvdum@0
   750
* @param mixed $forum_id The forum id the attachments are displayed in (false if in private message)
indvdum@0
   751
* @param string &$message The post/private message
indvdum@0
   752
* @param array &$attachments The attachments to parse for (inline) display. The attachments array will hold templated data after parsing.
indvdum@0
   753
* @param array &$update_count The attachment counts to be updated - will be filled
indvdum@0
   754
* @param bool $preview If set to true the attachments are parsed for preview. Within preview mode the comments are fetched from the given $attachments array and not fetched from the database.
indvdum@0
   755
*/
indvdum@0
   756
function parse_attachments($forum_id, &$message, &$attachments, &$update_count, $preview = false)
indvdum@0
   757
{
indvdum@0
   758
	if (!sizeof($attachments))
indvdum@0
   759
	{
indvdum@0
   760
		return;
indvdum@0
   761
	}
indvdum@0
   762
indvdum@0
   763
	global $template, $cache, $user;
indvdum@0
   764
	global $extensions, $config, $phpbb_root_path, $phpEx;
indvdum@0
   765
indvdum@0
   766
	//
indvdum@0
   767
	$compiled_attachments = array();
indvdum@0
   768
indvdum@0
   769
	if (!isset($template->filename['attachment_tpl']))
indvdum@0
   770
	{
indvdum@0
   771
		$template->set_filenames(array(
indvdum@0
   772
			'attachment_tpl'	=> 'attachment.html')
indvdum@0
   773
		);
indvdum@0
   774
	}
indvdum@0
   775
indvdum@0
   776
	if (empty($extensions) || !is_array($extensions))
indvdum@0
   777
	{
indvdum@0
   778
		$extensions = $cache->obtain_attach_extensions($forum_id);
indvdum@0
   779
	}
indvdum@0
   780
indvdum@0
   781
	// Look for missing attachment information...
indvdum@0
   782
	$attach_ids = array();
indvdum@0
   783
	foreach ($attachments as $pos => $attachment)
indvdum@0
   784
	{
indvdum@0
   785
		// If is_orphan is set, we need to retrieve the attachments again...
indvdum@0
   786
		if (!isset($attachment['extension']) && !isset($attachment['physical_filename']))
indvdum@0
   787
		{
indvdum@0
   788
			$attach_ids[(int) $attachment['attach_id']] = $pos;
indvdum@0
   789
		}
indvdum@0
   790
	}
indvdum@0
   791
indvdum@0
   792
	// Grab attachments (security precaution)
indvdum@0
   793
	if (sizeof($attach_ids))
indvdum@0
   794
	{
indvdum@0
   795
		global $db;
indvdum@0
   796
indvdum@0
   797
		$new_attachment_data = array();
indvdum@0
   798
indvdum@0
   799
		$sql = 'SELECT *
indvdum@0
   800
			FROM ' . ATTACHMENTS_TABLE . '
indvdum@0
   801
			WHERE ' . $db->sql_in_set('attach_id', array_keys($attach_ids));
indvdum@0
   802
		$result = $db->sql_query($sql);
indvdum@0
   803
indvdum@0
   804
		while ($row = $db->sql_fetchrow($result))
indvdum@0
   805
		{
indvdum@0
   806
			if (!isset($attach_ids[$row['attach_id']]))
indvdum@0
   807
			{
indvdum@0
   808
				continue;
indvdum@0
   809
			}
indvdum@0
   810
indvdum@0
   811
			// If we preview attachments we will set some retrieved values here
indvdum@0
   812
			if ($preview)
indvdum@0
   813
			{
indvdum@0
   814
				$row['attach_comment'] = $attachments[$attach_ids[$row['attach_id']]]['attach_comment'];
indvdum@0
   815
			}
indvdum@0
   816
indvdum@0
   817
			$new_attachment_data[$attach_ids[$row['attach_id']]] = $row;
indvdum@0
   818
		}
indvdum@0
   819
		$db->sql_freeresult($result);
indvdum@0
   820
indvdum@0
   821
		$attachments = $new_attachment_data;
indvdum@0
   822
		unset($new_attachment_data);
indvdum@0
   823
	}
indvdum@0
   824
indvdum@0
   825
	// Sort correctly
indvdum@0
   826
	if ($config['display_order'])
indvdum@0
   827
	{
indvdum@0
   828
		// Ascending sort
indvdum@0
   829
		krsort($attachments);
indvdum@0
   830
	}
indvdum@0
   831
	else
indvdum@0
   832
	{
indvdum@0
   833
		// Descending sort
indvdum@0
   834
		ksort($attachments);
indvdum@0
   835
	}
indvdum@0
   836
indvdum@0
   837
	foreach ($attachments as $attachment)
indvdum@0
   838
	{
indvdum@0
   839
		if (!sizeof($attachment))
indvdum@0
   840
		{
indvdum@0
   841
			continue;
indvdum@0
   842
		}
indvdum@0
   843
indvdum@0
   844
		// We need to reset/empty the _file block var, because this function might be called more than once
indvdum@0
   845
		$template->destroy_block_vars('_file');
indvdum@0
   846
indvdum@0
   847
		$block_array = array();
indvdum@0
   848
indvdum@0
   849
		// Some basics...
indvdum@0
   850
		$attachment['extension'] = strtolower(trim($attachment['extension']));
indvdum@0
   851
		$filename = $phpbb_root_path . $config['upload_path'] . '/' . utf8_basename($attachment['physical_filename']);
indvdum@0
   852
		$thumbnail_filename = $phpbb_root_path . $config['upload_path'] . '/thumb_' . utf8_basename($attachment['physical_filename']);
indvdum@0
   853
indvdum@0
   854
		$upload_icon = '';
indvdum@0
   855
indvdum@0
   856
		if (isset($extensions[$attachment['extension']]))
indvdum@0
   857
		{
indvdum@0
   858
			if ($user->img('icon_topic_attach', '') && !$extensions[$attachment['extension']]['upload_icon'])
indvdum@0
   859
			{
indvdum@0
   860
				$upload_icon = $user->img('icon_topic_attach', '');
indvdum@0
   861
			}
indvdum@0
   862
			else if ($extensions[$attachment['extension']]['upload_icon'])
indvdum@0
   863
			{
indvdum@0
   864
				$upload_icon = '<img src="' . $phpbb_root_path . $config['upload_icons_path'] . '/' . trim($extensions[$attachment['extension']]['upload_icon']) . '" alt="" />';
indvdum@0
   865
			}
indvdum@0
   866
		}
indvdum@0
   867
indvdum@0
   868
		$filesize = get_formatted_filesize($attachment['filesize'], false);
indvdum@0
   869
indvdum@0
   870
		$comment = bbcode_nl2br(censor_text($attachment['attach_comment']));
indvdum@0
   871
indvdum@0
   872
		$block_array += array(
indvdum@0
   873
			'UPLOAD_ICON'		=> $upload_icon,
indvdum@0
   874
			'FILESIZE'			=> $filesize['value'],
indvdum@0
   875
			'SIZE_LANG'			=> $filesize['unit'],
indvdum@0
   876
			'DOWNLOAD_NAME'		=> utf8_basename($attachment['real_filename']),
indvdum@0
   877
			'COMMENT'			=> $comment,
indvdum@0
   878
		);
indvdum@0
   879
indvdum@0
   880
		$denied = false;
indvdum@0
   881
indvdum@0
   882
		if (!extension_allowed($forum_id, $attachment['extension'], $extensions))
indvdum@0
   883
		{
indvdum@0
   884
			$denied = true;
indvdum@0
   885
indvdum@0
   886
			$block_array += array(
indvdum@0
   887
				'S_DENIED'			=> true,
indvdum@0
   888
				'DENIED_MESSAGE'	=> sprintf($user->lang['EXTENSION_DISABLED_AFTER_POSTING'], $attachment['extension'])
indvdum@0
   889
			);
indvdum@0
   890
		}
indvdum@0
   891
indvdum@0
   892
		if (!$denied)
indvdum@0
   893
		{
indvdum@0
   894
			$l_downloaded_viewed = $download_link = '';
indvdum@0
   895
			$display_cat = $extensions[$attachment['extension']]['display_cat'];
indvdum@0
   896
indvdum@0
   897
			if ($display_cat == ATTACHMENT_CATEGORY_IMAGE)
indvdum@0
   898
			{
indvdum@0
   899
				if ($attachment['thumbnail'])
indvdum@0
   900
				{
indvdum@0
   901
					$display_cat = ATTACHMENT_CATEGORY_THUMB;
indvdum@0
   902
				}
indvdum@0
   903
				else
indvdum@0
   904
				{
indvdum@0
   905
					if ($config['img_display_inlined'])
indvdum@0
   906
					{
indvdum@0
   907
						if ($config['img_link_width'] || $config['img_link_height'])
indvdum@0
   908
						{
indvdum@0
   909
							$dimension = @getimagesize($filename);
indvdum@0
   910
indvdum@0
   911
							// If the dimensions could not be determined or the image being 0x0 we display it as a link for safety purposes
indvdum@0
   912
							if ($dimension === false || empty($dimension[0]) || empty($dimension[1]))
indvdum@0
   913
							{
indvdum@0
   914
								$display_cat = ATTACHMENT_CATEGORY_NONE;
indvdum@0
   915
							}
indvdum@0
   916
							else
indvdum@0
   917
							{
indvdum@0
   918
								$display_cat = ($dimension[0] <= $config['img_link_width'] && $dimension[1] <= $config['img_link_height']) ? ATTACHMENT_CATEGORY_IMAGE : ATTACHMENT_CATEGORY_NONE;
indvdum@0
   919
							}
indvdum@0
   920
						}
indvdum@0
   921
					}
indvdum@0
   922
					else
indvdum@0
   923
					{
indvdum@0
   924
						$display_cat = ATTACHMENT_CATEGORY_NONE;
indvdum@0
   925
					}
indvdum@0
   926
				}
indvdum@0
   927
			}
indvdum@0
   928
indvdum@0
   929
			// Make some descisions based on user options being set.
indvdum@0
   930
			if (($display_cat == ATTACHMENT_CATEGORY_IMAGE || $display_cat == ATTACHMENT_CATEGORY_THUMB) && !$user->optionget('viewimg'))
indvdum@0
   931
			{
indvdum@0
   932
				$display_cat = ATTACHMENT_CATEGORY_NONE;
indvdum@0
   933
			}
indvdum@0
   934
indvdum@0
   935
			if ($display_cat == ATTACHMENT_CATEGORY_FLASH && !$user->optionget('viewflash'))
indvdum@0
   936
			{
indvdum@0
   937
				$display_cat = ATTACHMENT_CATEGORY_NONE;
indvdum@0
   938
			}
indvdum@0
   939
indvdum@0
   940
			$download_link = append_sid("{$phpbb_root_path}download/file.$phpEx", 'id=' . $attachment['attach_id']);
indvdum@0
   941
indvdum@0
   942
			switch ($display_cat)
indvdum@0
   943
			{
indvdum@0
   944
				// Images
indvdum@0
   945
				case ATTACHMENT_CATEGORY_IMAGE:
indvdum@0
   946
					$l_downloaded_viewed = 'VIEWED_COUNT';
indvdum@0
   947
					$inline_link = append_sid("{$phpbb_root_path}download/file.$phpEx", 'id=' . $attachment['attach_id']);
indvdum@0
   948
					$download_link .= '&amp;mode=view';
indvdum@0
   949
indvdum@0
   950
					$block_array += array(
indvdum@0
   951
						'S_IMAGE'		=> true,
indvdum@0
   952
						'U_INLINE_LINK'		=> $inline_link,
indvdum@0
   953
					);
indvdum@0
   954
indvdum@0
   955
					$update_count[] = $attachment['attach_id'];
indvdum@0
   956
				break;
indvdum@0
   957
indvdum@0
   958
				// Images, but display Thumbnail
indvdum@0
   959
				case ATTACHMENT_CATEGORY_THUMB:
indvdum@0
   960
					$l_downloaded_viewed = 'VIEWED_COUNT';
indvdum@0
   961
					$thumbnail_link = append_sid("{$phpbb_root_path}download/file.$phpEx", 'id=' . $attachment['attach_id'] . '&amp;t=1');
indvdum@0
   962
					$download_link .= '&amp;mode=view';
indvdum@0
   963
indvdum@0
   964
					$block_array += array(
indvdum@0
   965
						'S_THUMBNAIL'		=> true,
indvdum@0
   966
						'THUMB_IMAGE'		=> $thumbnail_link,
indvdum@0
   967
					);
indvdum@0
   968
indvdum@0
   969
					$update_count[] = $attachment['attach_id'];
indvdum@0
   970
				break;
indvdum@0
   971
indvdum@0
   972
				// Windows Media Streams
indvdum@0
   973
				case ATTACHMENT_CATEGORY_WM:
indvdum@0
   974
					$l_downloaded_viewed = 'VIEWED_COUNT';
indvdum@0
   975
indvdum@0
   976
					// Giving the filename directly because within the wm object all variables are in local context making it impossible
indvdum@0
   977
					// to validate against a valid session (all params can differ)
indvdum@0
   978
					// $download_link = $filename;
indvdum@0
   979
indvdum@0
   980
					$block_array += array(
indvdum@0
   981
						'U_FORUM'		=> generate_board_url(),
indvdum@0
   982
						'ATTACH_ID'		=> $attachment['attach_id'],
indvdum@0
   983
						'S_WM_FILE'		=> true,
indvdum@0
   984
					);
indvdum@0
   985
indvdum@0
   986
					// Viewed/Heared File ... update the download count
indvdum@0
   987
					$update_count[] = $attachment['attach_id'];
indvdum@0
   988
				break;
indvdum@0
   989
indvdum@0
   990
				// Real Media Streams
indvdum@0
   991
				case ATTACHMENT_CATEGORY_RM:
indvdum@0
   992
				case ATTACHMENT_CATEGORY_QUICKTIME:
indvdum@0
   993
					$l_downloaded_viewed = 'VIEWED_COUNT';
indvdum@0
   994
indvdum@0
   995
					$block_array += array(
indvdum@0
   996
						'S_RM_FILE'			=> ($display_cat == ATTACHMENT_CATEGORY_RM) ? true : false,
indvdum@0
   997
						'S_QUICKTIME_FILE'	=> ($display_cat == ATTACHMENT_CATEGORY_QUICKTIME) ? true : false,
indvdum@0
   998
						'U_FORUM'			=> generate_board_url(),
indvdum@0
   999
						'ATTACH_ID'			=> $attachment['attach_id'],
indvdum@0
  1000
					);
indvdum@0
  1001
indvdum@0
  1002
					// Viewed/Heared File ... update the download count
indvdum@0
  1003
					$update_count[] = $attachment['attach_id'];
indvdum@0
  1004
				break;
indvdum@0
  1005
indvdum@0
  1006
				// Macromedia Flash Files
indvdum@0
  1007
				case ATTACHMENT_CATEGORY_FLASH:
indvdum@0
  1008
					list($width, $height) = @getimagesize($filename);
indvdum@0
  1009
indvdum@0
  1010
					$l_downloaded_viewed = 'VIEWED_COUNT';
indvdum@0
  1011
indvdum@0
  1012
					$block_array += array(
indvdum@0
  1013
						'S_FLASH_FILE'	=> true,
indvdum@0
  1014
						'WIDTH'			=> $width,
indvdum@0
  1015
						'HEIGHT'		=> $height,
indvdum@0
  1016
						'U_VIEW_LINK'	=> $download_link . '&amp;view=1',
indvdum@0
  1017
					);
indvdum@0
  1018
indvdum@0
  1019
					// Viewed/Heared File ... update the download count
indvdum@0
  1020
					$update_count[] = $attachment['attach_id'];
indvdum@0
  1021
				break;
indvdum@0
  1022
indvdum@0
  1023
				default:
indvdum@0
  1024
					$l_downloaded_viewed = 'DOWNLOAD_COUNT';
indvdum@0
  1025
indvdum@0
  1026
					$block_array += array(
indvdum@0
  1027
						'S_FILE'		=> true,
indvdum@0
  1028
					);
indvdum@0
  1029
				break;
indvdum@0
  1030
			}
indvdum@0
  1031
indvdum@0
  1032
			$l_download_count = (!isset($attachment['download_count']) || $attachment['download_count'] == 0) ? $user->lang[$l_downloaded_viewed . '_NONE'] : (($attachment['download_count'] == 1) ? sprintf($user->lang[$l_downloaded_viewed], $attachment['download_count']) : sprintf($user->lang[$l_downloaded_viewed . 'S'], $attachment['download_count']));
indvdum@0
  1033
indvdum@0
  1034
			$block_array += array(
indvdum@0
  1035
				'U_DOWNLOAD_LINK'		=> $download_link,
indvdum@0
  1036
				'L_DOWNLOAD_COUNT'		=> $l_download_count
indvdum@0
  1037
			);
indvdum@0
  1038
		}
indvdum@0
  1039
indvdum@0
  1040
		$template->assign_block_vars('_file', $block_array);
indvdum@0
  1041
indvdum@0
  1042
		$compiled_attachments[] = $template->assign_display('attachment_tpl');
indvdum@0
  1043
	}
indvdum@0
  1044
indvdum@0
  1045
	$attachments = $compiled_attachments;
indvdum@0
  1046
	unset($compiled_attachments);
indvdum@0
  1047
indvdum@0
  1048
	$tpl_size = sizeof($attachments);
indvdum@0
  1049
indvdum@0
  1050
	$unset_tpl = array();
indvdum@0
  1051
indvdum@0
  1052
	preg_match_all('#<!\-\- ia([0-9]+) \-\->(.*?)<!\-\- ia\1 \-\->#', $message, $matches, PREG_PATTERN_ORDER);
indvdum@0
  1053
indvdum@0
  1054
	$replace = array();
indvdum@0
  1055
	foreach ($matches[0] as $num => $capture)
indvdum@0
  1056
	{
indvdum@0
  1057
		// Flip index if we are displaying the reverse way
indvdum@0
  1058
		$index = ($config['display_order']) ? ($tpl_size-($matches[1][$num] + 1)) : $matches[1][$num];
indvdum@0
  1059
indvdum@0
  1060
		$replace['from'][] = $matches[0][$num];
indvdum@0
  1061
		$replace['to'][] = (isset($attachments[$index])) ? $attachments[$index] : sprintf($user->lang['MISSING_INLINE_ATTACHMENT'], $matches[2][array_search($index, $matches[1])]);
indvdum@0
  1062
indvdum@0
  1063
		$unset_tpl[] = $index;
indvdum@0
  1064
	}
indvdum@0
  1065
indvdum@0
  1066
	if (isset($replace['from']))
indvdum@0
  1067
	{
indvdum@0
  1068
		$message = str_replace($replace['from'], $replace['to'], $message);
indvdum@0
  1069
	}
indvdum@0
  1070
indvdum@0
  1071
	$unset_tpl = array_unique($unset_tpl);
indvdum@0
  1072
indvdum@0
  1073
	// Needed to let not display the inlined attachments at the end of the post again
indvdum@0
  1074
	foreach ($unset_tpl as $index)
indvdum@0
  1075
	{
indvdum@0
  1076
		unset($attachments[$index]);
indvdum@0
  1077
	}
indvdum@0
  1078
}
indvdum@0
  1079
indvdum@0
  1080
/**
indvdum@0
  1081
* Check if extension is allowed to be posted.
indvdum@0
  1082
*
indvdum@0
  1083
* @param mixed $forum_id The forum id to check or false if private message
indvdum@0
  1084
* @param string $extension The extension to check, for example zip.
indvdum@0
  1085
* @param array &$extensions The extension array holding the information from the cache (will be obtained if empty)
indvdum@0
  1086
*
indvdum@0
  1087
* @return bool False if the extension is not allowed to be posted, else true.
indvdum@0
  1088
*/
indvdum@0
  1089
function extension_allowed($forum_id, $extension, &$extensions)
indvdum@0
  1090
{
indvdum@0
  1091
	if (empty($extensions))
indvdum@0
  1092
	{
indvdum@0
  1093
		global $cache;
indvdum@0
  1094
		$extensions = $cache->obtain_attach_extensions($forum_id);
indvdum@0
  1095
	}
indvdum@0
  1096
indvdum@0
  1097
	return (!isset($extensions['_allowed_'][$extension])) ? false : true;
indvdum@0
  1098
}
indvdum@0
  1099
indvdum@0
  1100
/**
indvdum@0
  1101
* Truncates string while retaining special characters if going over the max length
indvdum@0
  1102
* The default max length is 60 at the moment
indvdum@0
  1103
* The maximum storage length is there to fit the string within the given length. The string may be further truncated due to html entities.
indvdum@0
  1104
* For example: string given is 'a "quote"' (length: 9), would be a stored as 'a &quot;quote&quot;' (length: 19)
indvdum@0
  1105
*
indvdum@0
  1106
* @param string $string The text to truncate to the given length. String is specialchared.
indvdum@0
  1107
* @param int $max_length Maximum length of string (multibyte character count as 1 char / Html entity count as 1 char)
indvdum@0
  1108
* @param int $max_store_length Maximum character length of string (multibyte character count as 1 char / Html entity count as entity chars).
indvdum@0
  1109
* @param bool $allow_reply Allow Re: in front of string 
indvdum@0
  1110
* 	NOTE: This parameter can cause undesired behavior (returning strings longer than $max_store_length) and is deprecated. 
indvdum@0
  1111
* @param string $append String to be appended
indvdum@0
  1112
*/
indvdum@0
  1113
function truncate_string($string, $max_length = 60, $max_store_length = 255, $allow_reply = false, $append = '')
indvdum@0
  1114
{
indvdum@0
  1115
	$chars = array();
indvdum@0
  1116
indvdum@0
  1117
	$strip_reply = false;
indvdum@0
  1118
	$stripped = false;
indvdum@0
  1119
	if ($allow_reply && strpos($string, 'Re: ') === 0)
indvdum@0
  1120
	{
indvdum@0
  1121
		$strip_reply = true;
indvdum@0
  1122
		$string = substr($string, 4);
indvdum@0
  1123
	}
indvdum@0
  1124
indvdum@0
  1125
	$_chars = utf8_str_split(htmlspecialchars_decode($string));
indvdum@0
  1126
	$chars = array_map('utf8_htmlspecialchars', $_chars);
indvdum@0
  1127
indvdum@0
  1128
	// Now check the length ;)
indvdum@0
  1129
	if (sizeof($chars) > $max_length)
indvdum@0
  1130
	{
indvdum@0
  1131
		// Cut off the last elements from the array
indvdum@0
  1132
		$string = implode('', array_slice($chars, 0, $max_length - utf8_strlen($append)));
indvdum@0
  1133
		$stripped = true;
indvdum@0
  1134
	}
indvdum@0
  1135
indvdum@0
  1136
	// Due to specialchars, we may not be able to store the string...
indvdum@0
  1137
	if (utf8_strlen($string) > $max_store_length)
indvdum@0
  1138
	{
indvdum@0
  1139
		// let's split again, we do not want half-baked strings where entities are split
indvdum@0
  1140
		$_chars = utf8_str_split(htmlspecialchars_decode($string));
indvdum@0
  1141
		$chars = array_map('utf8_htmlspecialchars', $_chars);
indvdum@0
  1142
indvdum@0
  1143
		do
indvdum@0
  1144
		{
indvdum@0
  1145
			array_pop($chars);
indvdum@0
  1146
			$string = implode('', $chars);
indvdum@0
  1147
		}
indvdum@0
  1148
		while (!empty($chars) && utf8_strlen($string) > $max_store_length);
indvdum@0
  1149
	}
indvdum@0
  1150
indvdum@0
  1151
	if ($strip_reply)
indvdum@0
  1152
	{
indvdum@0
  1153
		$string = 'Re: ' . $string;
indvdum@0
  1154
	}
indvdum@0
  1155
indvdum@0
  1156
	if ($append != '' && $stripped)
indvdum@0
  1157
	{
indvdum@0
  1158
		$string = $string . $append;
indvdum@0
  1159
	}
indvdum@0
  1160
indvdum@0
  1161
	return $string;
indvdum@0
  1162
}
indvdum@0
  1163
indvdum@0
  1164
/**
indvdum@0
  1165
* Get username details for placing into templates.
indvdum@0
  1166
* This function caches all modes on first call, except for no_profile and anonymous user - determined by $user_id.
indvdum@0
  1167
*
indvdum@0
  1168
* @param string $mode Can be profile (for getting an url to the profile), username (for obtaining the username), colour (for obtaining the user colour), full (for obtaining a html string representing a coloured link to the users profile) or no_profile (the same as full but forcing no profile link)
indvdum@0
  1169
* @param int $user_id The users id
indvdum@0
  1170
* @param string $username The users name
indvdum@0
  1171
* @param string $username_colour The users colour
indvdum@0
  1172
* @param string $guest_username optional parameter to specify the guest username. It will be used in favor of the GUEST language variable then.
indvdum@0
  1173
* @param string $custom_profile_url optional parameter to specify a profile url. The user id get appended to this url as &amp;u={user_id}
indvdum@0
  1174
*
indvdum@0
  1175
* @return string A string consisting of what is wanted based on $mode.
indvdum@0
  1176
* @author BartVB, Acyd Burn
indvdum@0
  1177
*/
indvdum@0
  1178
function get_username_string($mode, $user_id, $username, $username_colour = '', $guest_username = false, $custom_profile_url = false)
indvdum@0
  1179
{
indvdum@0
  1180
	static $_profile_cache;
indvdum@0
  1181
indvdum@0
  1182
	// We cache some common variables we need within this function
indvdum@0
  1183
	if (empty($_profile_cache))
indvdum@0
  1184
	{
indvdum@0
  1185
		global $phpbb_root_path, $phpEx;
indvdum@0
  1186
indvdum@0
  1187
		$_profile_cache['base_url'] = append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=viewprofile&amp;u={USER_ID}');
indvdum@0
  1188
		$_profile_cache['tpl_noprofile'] = '{USERNAME}';
indvdum@0
  1189
		$_profile_cache['tpl_noprofile_colour'] = '<span style="color: {USERNAME_COLOUR};" class="username-coloured">{USERNAME}</span>';
indvdum@0
  1190
		$_profile_cache['tpl_profile'] = '<a href="{PROFILE_URL}">{USERNAME}</a>';
indvdum@0
  1191
		$_profile_cache['tpl_profile_colour'] = '<a href="{PROFILE_URL}" style="color: {USERNAME_COLOUR};" class="username-coloured">{USERNAME}</a>';
indvdum@0
  1192
	}
indvdum@0
  1193
indvdum@0
  1194
	global $user, $auth;
indvdum@0
  1195
indvdum@0
  1196
	// This switch makes sure we only run code required for the mode
indvdum@0
  1197
	switch ($mode)
indvdum@0
  1198
	{
indvdum@0
  1199
		case 'full':
indvdum@0
  1200
		case 'no_profile':
indvdum@0
  1201
		case 'colour':
indvdum@0
  1202
indvdum@0
  1203
			// Build correct username colour
indvdum@0
  1204
			$username_colour = ($username_colour) ? '#' . $username_colour : '';
indvdum@0
  1205
indvdum@0
  1206
			// Return colour
indvdum@0
  1207
			if ($mode == 'colour')
indvdum@0
  1208
			{
indvdum@0
  1209
				return $username_colour;
indvdum@0
  1210
			}
indvdum@0
  1211
indvdum@0
  1212
		// no break;
indvdum@0
  1213
indvdum@0
  1214
		case 'username':
indvdum@0
  1215
indvdum@0
  1216
			// Build correct username
indvdum@0
  1217
			if ($guest_username === false)
indvdum@0
  1218
			{
indvdum@0
  1219
				$username = ($username) ? $username : $user->lang['GUEST'];
indvdum@0
  1220
			}
indvdum@0
  1221
			else
indvdum@0
  1222
			{
indvdum@0
  1223
				$username = ($user_id && $user_id != ANONYMOUS) ? $username : ((!empty($guest_username)) ? $guest_username : $user->lang['GUEST']);
indvdum@0
  1224
			}
indvdum@0
  1225
indvdum@0
  1226
			// Return username
indvdum@0
  1227
			if ($mode == 'username')
indvdum@0
  1228
			{
indvdum@0
  1229
				return $username;
indvdum@0
  1230
			}
indvdum@0
  1231
indvdum@0
  1232
		// no break;
indvdum@0
  1233
indvdum@0
  1234
		case 'profile':
indvdum@0
  1235
indvdum@0
  1236
			// Build correct profile url - only show if not anonymous and permission to view profile if registered user
indvdum@0
  1237
			// For anonymous the link leads to a login page.
indvdum@0
  1238
			if ($user_id && $user_id != ANONYMOUS && ($user->data['user_id'] == ANONYMOUS || $auth->acl_get('u_viewprofile')))
indvdum@0
  1239
			{
indvdum@0
  1240
				$profile_url = ($custom_profile_url !== false) ? $custom_profile_url . '&amp;u=' . (int) $user_id : str_replace(array('={USER_ID}', '=%7BUSER_ID%7D'), '=' . (int) $user_id, $_profile_cache['base_url']);
indvdum@0
  1241
			}
indvdum@0
  1242
			else
indvdum@0
  1243
			{
indvdum@0
  1244
				$profile_url = '';
indvdum@0
  1245
			}
indvdum@0
  1246
indvdum@0
  1247
			// Return profile
indvdum@0
  1248
			if ($mode == 'profile')
indvdum@0
  1249
			{
indvdum@0
  1250
				return $profile_url;
indvdum@0
  1251
			}
indvdum@0
  1252
indvdum@0
  1253
		// no break;
indvdum@0
  1254
	}
indvdum@0
  1255
indvdum@0
  1256
	if (($mode == 'full' && !$profile_url) || $mode == 'no_profile')
indvdum@0
  1257
	{
indvdum@0
  1258
		return str_replace(array('{USERNAME_COLOUR}', '{USERNAME}'), array($username_colour, $username), (!$username_colour) ? $_profile_cache['tpl_noprofile'] : $_profile_cache['tpl_noprofile_colour']);
indvdum@0
  1259
	}
indvdum@0
  1260
indvdum@0
  1261
	return str_replace(array('{PROFILE_URL}', '{USERNAME_COLOUR}', '{USERNAME}'), array($profile_url, $username_colour, $username), (!$username_colour) ? $_profile_cache['tpl_profile'] : $_profile_cache['tpl_profile_colour']);
indvdum@0
  1262
}
indvdum@0
  1263
indvdum@0
  1264
/**
indvdum@0
  1265
* @package phpBB3
indvdum@0
  1266
*/
indvdum@0
  1267
class bitfield
indvdum@0
  1268
{
indvdum@0
  1269
	var $data;
indvdum@0
  1270
indvdum@0
  1271
	function bitfield($bitfield = '')
indvdum@0
  1272
	{
indvdum@0
  1273
		$this->data = base64_decode($bitfield);
indvdum@0
  1274
	}
indvdum@0
  1275
indvdum@0
  1276
	/**
indvdum@0
  1277
	*/
indvdum@0
  1278
	function get($n)
indvdum@0
  1279
	{
indvdum@0
  1280
		// Get the ($n / 8)th char
indvdum@0
  1281
		$byte = $n >> 3;
indvdum@0
  1282
indvdum@0
  1283
		if (strlen($this->data) >= $byte + 1)
indvdum@0
  1284
		{
indvdum@0
  1285
			$c = $this->data[$byte];
indvdum@0
  1286
indvdum@0
  1287
			// Lookup the ($n % 8)th bit of the byte
indvdum@0
  1288
			$bit = 7 - ($n & 7);
indvdum@0
  1289
			return (bool) (ord($c) & (1 << $bit));
indvdum@0
  1290
		}
indvdum@0
  1291
		else
indvdum@0
  1292
		{
indvdum@0
  1293
			return false;
indvdum@0
  1294
		}
indvdum@0
  1295
	}
indvdum@0
  1296
indvdum@0
  1297
	function set($n)
indvdum@0
  1298
	{
indvdum@0
  1299
		$byte = $n >> 3;
indvdum@0
  1300
		$bit = 7 - ($n & 7);
indvdum@0
  1301
indvdum@0
  1302
		if (strlen($this->data) >= $byte + 1)
indvdum@0
  1303
		{
indvdum@0
  1304
			$this->data[$byte] = $this->data[$byte] | chr(1 << $bit);
indvdum@0
  1305
		}
indvdum@0
  1306
		else
indvdum@0
  1307
		{
indvdum@0
  1308
			$this->data .= str_repeat("\0", $byte - strlen($this->data));
indvdum@0
  1309
			$this->data .= chr(1 << $bit);
indvdum@0
  1310
		}
indvdum@0
  1311
	}
indvdum@0
  1312
indvdum@0
  1313
	function clear($n)
indvdum@0
  1314
	{
indvdum@0
  1315
		$byte = $n >> 3;
indvdum@0
  1316
indvdum@0
  1317
		if (strlen($this->data) >= $byte + 1)
indvdum@0
  1318
		{
indvdum@0
  1319
			$bit = 7 - ($n & 7);
indvdum@0
  1320
			$this->data[$byte] = $this->data[$byte] &~ chr(1 << $bit);
indvdum@0
  1321
		}
indvdum@0
  1322
	}
indvdum@0
  1323
indvdum@0
  1324
	function get_blob()
indvdum@0
  1325
	{
indvdum@0
  1326
		return $this->data;
indvdum@0
  1327
	}
indvdum@0
  1328
indvdum@0
  1329
	function get_base64()
indvdum@0
  1330
	{
indvdum@0
  1331
		return base64_encode($this->data);
indvdum@0
  1332
	}
indvdum@0
  1333
indvdum@0
  1334
	function get_bin()
indvdum@0
  1335
	{
indvdum@0
  1336
		$bin = '';
indvdum@0
  1337
		$len = strlen($this->data);
indvdum@0
  1338
indvdum@0
  1339
		for ($i = 0; $i < $len; ++$i)
indvdum@0
  1340
		{
indvdum@0
  1341
			$bin .= str_pad(decbin(ord($this->data[$i])), 8, '0', STR_PAD_LEFT);
indvdum@0
  1342
		}
indvdum@0
  1343
indvdum@0
  1344
		return $bin;
indvdum@0
  1345
	}
indvdum@0
  1346
indvdum@0
  1347
	function get_all_set()
indvdum@0
  1348
	{
indvdum@0
  1349
		return array_keys(array_filter(str_split($this->get_bin())));
indvdum@0
  1350
	}
indvdum@0
  1351
indvdum@0
  1352
	function merge($bitfield)
indvdum@0
  1353
	{
indvdum@0
  1354
		$this->data = $this->data | $bitfield->get_blob();
indvdum@0
  1355
	}
indvdum@0
  1356
}
indvdum@0
  1357
indvdum@0
  1358
?>