includes/acp/acp_icons.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/includes/acp/acp_icons.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,958 @@
     1.4 +<?php
     1.5 +/**
     1.6 +*
     1.7 +* @package acp
     1.8 +* @version $Id$
     1.9 +* @copyright (c) 2005 phpBB Group
    1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
    1.11 +*
    1.12 +*/
    1.13 +
    1.14 +/**
    1.15 +* @ignore
    1.16 +*/
    1.17 +if (!defined('IN_PHPBB'))
    1.18 +{
    1.19 +	exit;
    1.20 +}
    1.21 +
    1.22 +/**
    1.23 +* @todo [smilies] check regular expressions for special char replacements (stored specialchared in db)
    1.24 +* @package acp
    1.25 +*/
    1.26 +class acp_icons
    1.27 +{
    1.28 +	var $u_action;
    1.29 +
    1.30 +	function main($id, $mode)
    1.31 +	{
    1.32 +		global $db, $user, $auth, $template, $cache;
    1.33 +		global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
    1.34 +
    1.35 +		$user->add_lang('acp/posting');
    1.36 +
    1.37 +		// Set up general vars
    1.38 +		$action = request_var('action', '');
    1.39 +		$action = (isset($_POST['add'])) ? 'add' : $action;
    1.40 +		$action = (isset($_POST['edit'])) ? 'edit' : $action;
    1.41 +		$action = (isset($_POST['import'])) ? 'import' : $action;
    1.42 +		$icon_id = request_var('id', 0);
    1.43 +
    1.44 +		$mode = ($mode == 'smilies') ? 'smilies' : 'icons';
    1.45 +
    1.46 +		$this->tpl_name = 'acp_icons';
    1.47 +
    1.48 +		// What are we working on?
    1.49 +		switch ($mode)
    1.50 +		{
    1.51 +			case 'smilies':
    1.52 +				$table = SMILIES_TABLE;
    1.53 +				$lang = 'SMILIES';
    1.54 +				$fields = 'smiley';
    1.55 +				$img_path = $config['smilies_path'];
    1.56 +			break;
    1.57 +
    1.58 +			case 'icons':
    1.59 +				$table = ICONS_TABLE;
    1.60 +				$lang = 'ICONS';
    1.61 +				$fields = 'icons';
    1.62 +				$img_path = $config['icons_path'];
    1.63 +			break;
    1.64 +		}
    1.65 +
    1.66 +		$this->page_title = 'ACP_' . $lang;
    1.67 +
    1.68 +		// Clear some arrays
    1.69 +		$_images = $_paks = array();
    1.70 +		$notice = '';
    1.71 +
    1.72 +		// Grab file list of paks and images
    1.73 +		if ($action == 'edit' || $action == 'add' || $action == 'import')
    1.74 +		{
    1.75 +			$imglist = filelist($phpbb_root_path . $img_path, '');
    1.76 +
    1.77 +			foreach ($imglist as $path => $img_ary)
    1.78 +			{
    1.79 +				if (empty($img_ary))
    1.80 +				{
    1.81 +					continue;
    1.82 +				}
    1.83 +
    1.84 +				asort($img_ary, SORT_STRING);
    1.85 +
    1.86 +				foreach ($img_ary as $img)
    1.87 +				{
    1.88 +					$img_size = getimagesize($phpbb_root_path . $img_path . '/' . $path . $img);
    1.89 +
    1.90 +					if (!$img_size[0] || !$img_size[1] || strlen($img) > 255)
    1.91 +					{
    1.92 +						continue;
    1.93 +					}
    1.94 +
    1.95 +					// adjust the width and height to be lower than 128px while perserving the aspect ratio (for icons)
    1.96 +					if ($mode == 'icons')
    1.97 +					{
    1.98 +						if ($img_size[0] > 127 && $img_size[0] > $img_size[1])
    1.99 +						{
   1.100 +							$img_size[1] = (int) ($img_size[1] * (127 / $img_size[0]));
   1.101 +							$img_size[0] = 127;
   1.102 +						}
   1.103 +						else if ($img_size[1] > 127)
   1.104 +						{
   1.105 +							$img_size[0] = (int) ($img_size[0] * (127 / $img_size[1]));
   1.106 +							$img_size[1] = 127;
   1.107 +						}
   1.108 +					}
   1.109 +
   1.110 +					$_images[$path . $img]['file'] = $path . $img;
   1.111 +					$_images[$path . $img]['width'] = $img_size[0];
   1.112 +					$_images[$path . $img]['height'] = $img_size[1];
   1.113 +				}
   1.114 +			}
   1.115 +			unset($imglist);
   1.116 +
   1.117 +			if ($dir = @opendir($phpbb_root_path . $img_path))
   1.118 +			{
   1.119 +				while (($file = readdir($dir)) !== false)
   1.120 +				{
   1.121 +					if (is_file($phpbb_root_path . $img_path . '/' . $file) && preg_match('#\.pak$#i', $file))
   1.122 +					{
   1.123 +						$_paks[] = $file;
   1.124 +					}
   1.125 +				}
   1.126 +				closedir($dir);
   1.127 +
   1.128 +				if (!empty($_paks))
   1.129 +				{
   1.130 +					asort($_paks, SORT_STRING);
   1.131 +				}
   1.132 +			}
   1.133 +		}
   1.134 +
   1.135 +		// What shall we do today? Oops, I believe that's trademarked ...
   1.136 +		switch ($action)
   1.137 +		{
   1.138 +			case 'edit':
   1.139 +				unset($_images);
   1.140 +				$_images = array();
   1.141 +
   1.142 +			// no break;
   1.143 +
   1.144 +			case 'add':
   1.145 +
   1.146 +				$smilies = $default_row = array();
   1.147 +				$smiley_options = $order_list = $add_order_list = '';
   1.148 +
   1.149 +				if ($action == 'add' && $mode == 'smilies')
   1.150 +				{
   1.151 +					$sql = 'SELECT *
   1.152 +						FROM ' . SMILIES_TABLE . '
   1.153 +						ORDER BY smiley_order';
   1.154 +					$result = $db->sql_query($sql);
   1.155 +
   1.156 +					while ($row = $db->sql_fetchrow($result))
   1.157 +					{
   1.158 +						if (empty($smilies[$row['smiley_url']]))
   1.159 +						{
   1.160 +							$smilies[$row['smiley_url']] = $row;
   1.161 +						}
   1.162 +					}
   1.163 +					$db->sql_freeresult($result);
   1.164 +
   1.165 +					if (sizeof($smilies))
   1.166 +					{
   1.167 +						foreach ($smilies as $row)
   1.168 +						{
   1.169 +							$selected = false;
   1.170 +
   1.171 +							if (!$smiley_options)
   1.172 +							{
   1.173 +								$selected = true;
   1.174 +								$default_row = $row;
   1.175 +							}
   1.176 +							$smiley_options .= '<option value="' . $row['smiley_url'] . '"' . (($selected) ? ' selected="selected"' : '') . '>' . $row['smiley_url'] . '</option>';
   1.177 +
   1.178 +							$template->assign_block_vars('smile', array(
   1.179 +								'SMILEY_URL'	=> addslashes($row['smiley_url']),
   1.180 +								'CODE'			=> addslashes($row['code']),
   1.181 +								'EMOTION'		=> addslashes($row['emotion']),
   1.182 +								'WIDTH'			=> $row['smiley_width'],
   1.183 +								'HEIGHT'		=> $row['smiley_height'],
   1.184 +								'ORDER'			=> $row['smiley_order'] + 1,
   1.185 +							));
   1.186 +						}
   1.187 +					}
   1.188 +				}
   1.189 +
   1.190 +				$sql = "SELECT *
   1.191 +					FROM $table
   1.192 +					ORDER BY {$fields}_order " . (($icon_id || $action == 'add') ? 'DESC' : 'ASC');
   1.193 +				$result = $db->sql_query($sql);
   1.194 +
   1.195 +				$data = array();
   1.196 +				$after = false;
   1.197 +				$display = 0;
   1.198 +				$order_lists = array('', '');
   1.199 +				$add_order_lists = array('', '');
   1.200 +				$display_count = 0;
   1.201 +
   1.202 +				while ($row = $db->sql_fetchrow($result))
   1.203 +				{
   1.204 +					if ($action == 'add')
   1.205 +					{
   1.206 +						unset($_images[$row[$fields . '_url']]);
   1.207 +					}
   1.208 +
   1.209 +
   1.210 +					if ($row[$fields . '_id'] == $icon_id)
   1.211 +					{
   1.212 +						$after = true;
   1.213 +						$display = $row['display_on_posting'];
   1.214 +						$data[$row[$fields . '_url']] = $row;
   1.215 +					}
   1.216 +					else
   1.217 +					{
   1.218 +						if ($action == 'edit' && !$icon_id)
   1.219 +						{
   1.220 +							$data[$row[$fields . '_url']] = $row;
   1.221 +						}
   1.222 +
   1.223 +						$selected = '';
   1.224 +						if (!empty($after))
   1.225 +						{
   1.226 +							$selected = ' selected="selected"';
   1.227 +							$after = false;
   1.228 +						}
   1.229 +						if ($row['display_on_posting'])
   1.230 +						{
   1.231 +							$display_count++;
   1.232 +						}
   1.233 +						$after_txt = ($mode == 'smilies') ? $row['code'] : $row['icons_url'];
   1.234 +						$order_lists[$row['display_on_posting']] = '<option value="' . ($row[$fields . '_order'] + 1) . '"' . $selected . '>' . sprintf($user->lang['AFTER_' . $lang], ' -&gt; ' . $after_txt) . '</option>' . $order_lists[$row['display_on_posting']];
   1.235 +
   1.236 +						if (!empty($default_row))
   1.237 +						{
   1.238 +							$add_order_lists[$row['display_on_posting']] = '<option value="' . ($row[$fields . '_order'] + 1) . '"' . (($row[$fields . '_id'] == $default_row['smiley_id']) ? ' selected="selected"' : '') . '>' . sprintf($user->lang['AFTER_' . $lang], ' -&gt; ' . $after_txt) . '</option>' . $add_order_lists[$row['display_on_posting']];
   1.239 +						}
   1.240 +					}
   1.241 +				}
   1.242 +				$db->sql_freeresult($result);
   1.243 +
   1.244 +				$order_list = '<option value="1"' . ((!isset($after)) ? ' selected="selected"' : '') . '>' . $user->lang['FIRST'] . '</option>';
   1.245 +				$add_order_list = '<option value="1">' . $user->lang['FIRST'] . '</option>';
   1.246 +
   1.247 +				if ($action == 'add')
   1.248 +				{
   1.249 +					$data = $_images;
   1.250 +				}
   1.251 +
   1.252 +				$colspan = (($mode == 'smilies') ? 7 : 5);
   1.253 +				$colspan += ($icon_id) ? 1 : 0;
   1.254 +				$colspan += ($action == 'add') ? 2 : 0;
   1.255 +
   1.256 +				$template->assign_vars(array(
   1.257 +					'S_EDIT'		=> true,
   1.258 +					'S_SMILIES'		=> ($mode == 'smilies') ? true : false,
   1.259 +					'S_ADD'			=> ($action == 'add') ? true : false,
   1.260 +
   1.261 +					'S_ORDER_LIST_DISPLAY'		=> $order_list . $order_lists[1],
   1.262 +					'S_ORDER_LIST_UNDISPLAY'	=> $order_list . $order_lists[0],
   1.263 +					'S_ORDER_LIST_DISPLAY_COUNT'	=> $display_count + 1,
   1.264 +
   1.265 +					'L_TITLE'		=> $user->lang['ACP_' . $lang],
   1.266 +					'L_EXPLAIN'		=> $user->lang['ACP_' . $lang . '_EXPLAIN'],
   1.267 +					'L_CONFIG'		=> $user->lang[$lang . '_CONFIG'],
   1.268 +					'L_URL'			=> $user->lang[$lang . '_URL'],
   1.269 +					'L_LOCATION'	=> $user->lang[$lang . '_LOCATION'],
   1.270 +					'L_WIDTH'		=> $user->lang[$lang . '_WIDTH'],
   1.271 +					'L_HEIGHT'		=> $user->lang[$lang . '_HEIGHT'],
   1.272 +					'L_ORDER'		=> $user->lang[$lang . '_ORDER'],
   1.273 +					'L_NO_ICONS'	=> $user->lang['NO_' . $lang . '_' . strtoupper($action)],
   1.274 +
   1.275 +					'COLSPAN'		=> $colspan,
   1.276 +					'ID'			=> $icon_id,
   1.277 +
   1.278 +					'U_BACK'		=> $this->u_action,
   1.279 +					'U_ACTION'		=> $this->u_action . '&amp;action=' . (($action == 'add') ? 'create' : 'modify'),
   1.280 +				));
   1.281 +
   1.282 +				foreach ($data as $img => $img_row)
   1.283 +				{
   1.284 +					$template->assign_block_vars('items', array(
   1.285 +						'IMG'		=> $img,
   1.286 +						'A_IMG'		=> addslashes($img),
   1.287 +						'IMG_SRC'	=> $phpbb_root_path . $img_path . '/' . $img,
   1.288 +
   1.289 +						'CODE'		=> ($mode == 'smilies' && isset($img_row['code'])) ? $img_row['code'] : '',
   1.290 +						'EMOTION'	=> ($mode == 'smilies' && isset($img_row['emotion'])) ? $img_row['emotion'] : '',
   1.291 +
   1.292 +						'S_ID'				=> (isset($img_row[$fields . '_id'])) ? true : false,
   1.293 +						'ID'				=> (isset($img_row[$fields . '_id'])) ? $img_row[$fields . '_id'] : 0,
   1.294 +						'WIDTH'				=> (!empty($img_row[$fields .'_width'])) ? $img_row[$fields .'_width'] : $img_row['width'],
   1.295 +						'HEIGHT'			=> (!empty($img_row[$fields .'_height'])) ? $img_row[$fields .'_height'] : $img_row['height'],
   1.296 +						'POSTING_CHECKED'	=> (!empty($img_row['display_on_posting']) || $action == 'add') ? ' checked="checked"' : '',
   1.297 +					));
   1.298 +				}
   1.299 +
   1.300 +				// Ok, another row for adding an addition code for a pre-existing image...
   1.301 +				if ($action == 'add' && $mode == 'smilies' && sizeof($smilies))
   1.302 +				{
   1.303 +					$template->assign_vars(array(
   1.304 +						'S_ADD_CODE'		=> true,
   1.305 +
   1.306 +						'S_IMG_OPTIONS'		=> $smiley_options,
   1.307 +
   1.308 +						'S_ADD_ORDER_LIST_DISPLAY'		=> $add_order_list . $add_order_lists[1],
   1.309 +						'S_ADD_ORDER_LIST_UNDISPLAY'	=> $add_order_list . $add_order_lists[0],
   1.310 +
   1.311 +						'IMG_SRC'			=> $phpbb_root_path . $img_path . '/' . $default_row['smiley_url'],
   1.312 +						'IMG_PATH'			=> $img_path,
   1.313 +						'PHPBB_ROOT_PATH'	=> $phpbb_root_path,
   1.314 +
   1.315 +						'CODE'				=> $default_row['code'],
   1.316 +						'EMOTION'			=> $default_row['emotion'],
   1.317 +
   1.318 +						'WIDTH'				=> $default_row['smiley_width'],
   1.319 +						'HEIGHT'			=> $default_row['smiley_height'],
   1.320 +					));
   1.321 +				}
   1.322 +
   1.323 +				return;
   1.324 +
   1.325 +			break;
   1.326 +
   1.327 +			case 'create':
   1.328 +			case 'modify':
   1.329 +
   1.330 +				// Get items to create/modify
   1.331 +				$images = (isset($_POST['image'])) ? array_keys(request_var('image', array('' => 0))) : array();
   1.332 +
   1.333 +				// Now really get the items
   1.334 +				$image_id		= (isset($_POST['id'])) ? request_var('id', array('' => 0)) : array();
   1.335 +				$image_order	= (isset($_POST['order'])) ? request_var('order', array('' => 0)) : array();
   1.336 +				$image_width	= (isset($_POST['width'])) ? request_var('width', array('' => 0)) : array();
   1.337 +				$image_height	= (isset($_POST['height'])) ? request_var('height', array('' => 0)) : array();
   1.338 +				$image_add		= (isset($_POST['add_img'])) ? request_var('add_img', array('' => 0)) : array();
   1.339 +				$image_emotion	= utf8_normalize_nfc(request_var('emotion', array('' => ''), true));
   1.340 +				$image_code		= utf8_normalize_nfc(request_var('code', array('' => ''), true));
   1.341 +				$image_display_on_posting = (isset($_POST['display_on_posting'])) ? request_var('display_on_posting', array('' => 0)) : array();
   1.342 +
   1.343 +				// Ok, add the relevant bits if we are adding new codes to existing emoticons...
   1.344 +				if (!empty($_POST['add_additional_code']))
   1.345 +				{
   1.346 +					$add_image			= request_var('add_image', '');
   1.347 +					$add_code			= utf8_normalize_nfc(request_var('add_code', '', true));
   1.348 +					$add_emotion		= utf8_normalize_nfc(request_var('add_emotion', '', true));
   1.349 +
   1.350 +					if ($add_image && $add_emotion && $add_code)
   1.351 +					{
   1.352 +						$images[] = $add_image;
   1.353 +						$image_add[$add_image] = true;
   1.354 +
   1.355 +						$image_code[$add_image] = $add_code;
   1.356 +						$image_emotion[$add_image] = $add_emotion;
   1.357 +						$image_width[$add_image] = request_var('add_width', 0);
   1.358 +						$image_height[$add_image] = request_var('add_height', 0);
   1.359 +
   1.360 +						if (!empty($_POST['add_display_on_posting']))
   1.361 +						{
   1.362 +							$image_display_on_posting[$add_image] = 1;
   1.363 +						}
   1.364 +
   1.365 +						$image_order[$add_image] = request_var('add_order', 0);
   1.366 +					}
   1.367 +				}
   1.368 +
   1.369 +				if ($mode == 'smilies' && $action == 'create')
   1.370 +				{
   1.371 +					$smiley_count = $this->item_count($table);
   1.372 +
   1.373 +					$addable_smileys_count = sizeof($images);
   1.374 +					foreach ($images as $image)
   1.375 +					{
   1.376 +						if (!isset($image_add[$image]))
   1.377 +						{
   1.378 +							--$addable_smileys_count;
   1.379 +						}
   1.380 +					}
   1.381 +
   1.382 +					if ($smiley_count + $addable_smileys_count > SMILEY_LIMIT)
   1.383 +					{
   1.384 +						trigger_error(sprintf($user->lang['TOO_MANY_SMILIES'], SMILEY_LIMIT) . adm_back_link($this->u_action), E_USER_WARNING);
   1.385 +					}
   1.386 +				}
   1.387 +
   1.388 +				$icons_updated = 0;
   1.389 +				$errors = array();
   1.390 +				foreach ($images as $image)
   1.391 +				{
   1.392 +					if ($mode == 'smilies' && ($image_emotion[$image] == '' || $image_code[$image] == ''))
   1.393 +					{
   1.394 +						$errors[$image] = 'SMILIE_NO_' . (($image_emotion[$image] == '') ? 'EMOTION' : 'CODE');
   1.395 +					}
   1.396 +					else if ($action == 'create' && !isset($image_add[$image]))
   1.397 +					{
   1.398 +						// skip images where add wasn't checked
   1.399 +					}
   1.400 +					else if (!file_exists($phpbb_root_path . $img_path . '/' . $image))
   1.401 +					{
   1.402 +						$errors[$image] = 'SMILIE_NO_FILE';
   1.403 +					}
   1.404 +					else
   1.405 +					{
   1.406 +						if ($image_width[$image] == 0 || $image_height[$image] == 0)
   1.407 +						{
   1.408 +							$img_size = getimagesize($phpbb_root_path . $img_path . '/' . $image);
   1.409 +							$image_width[$image] = $img_size[0];
   1.410 +							$image_height[$image] = $img_size[1];
   1.411 +						}
   1.412 +
   1.413 +						// Adjust image width/height for icons
   1.414 +						if ($mode == 'icons')
   1.415 +						{
   1.416 +							if ($image_width[$image] > 127 && $image_width[$image] > $image_height[$image])
   1.417 +							{
   1.418 +								$image_height[$image] = (int) ($image_height[$image] * (127 / $image_width[$image]));
   1.419 +								$image_width[$image] = 127;
   1.420 +							}
   1.421 +							else if ($image_height[$image] > 127)
   1.422 +							{
   1.423 +								$image_width[$image] = (int) ($image_width[$image] * (127 / $image_height[$image]));
   1.424 +								$image_height[$image] = 127;
   1.425 +							}
   1.426 +						}
   1.427 +
   1.428 +						$img_sql = array(
   1.429 +							$fields . '_url'		=> $image,
   1.430 +							$fields . '_width'		=> $image_width[$image],
   1.431 +							$fields . '_height'		=> $image_height[$image],
   1.432 +							'display_on_posting'	=> (isset($image_display_on_posting[$image])) ? 1 : 0,
   1.433 +						);
   1.434 +
   1.435 +						if ($mode == 'smilies')
   1.436 +						{
   1.437 +							$img_sql = array_merge($img_sql, array(
   1.438 +								'emotion'	=> $image_emotion[$image],
   1.439 +								'code'		=> $image_code[$image])
   1.440 +							);
   1.441 +						}
   1.442 +
   1.443 +						// Image_order holds the 'new' order value
   1.444 +						if (!empty($image_order[$image]))
   1.445 +						{
   1.446 +							$img_sql = array_merge($img_sql, array(
   1.447 +								$fields . '_order'	=>	$image_order[$image])
   1.448 +							);
   1.449 +
   1.450 +							// Since we always add 'after' an item, we just need to increase all following + the current by one
   1.451 +							$sql = "UPDATE $table
   1.452 +								SET {$fields}_order = {$fields}_order + 1
   1.453 +								WHERE {$fields}_order >= {$image_order[$image]}";
   1.454 +							$db->sql_query($sql);
   1.455 +
   1.456 +							// If we adjust the order, we need to adjust all other orders too - they became inaccurate...
   1.457 +							foreach ($image_order as $_image => $_order)
   1.458 +							{
   1.459 +								if ($_image == $image)
   1.460 +								{
   1.461 +									continue;
   1.462 +								}
   1.463 +
   1.464 +								if ($_order >= $image_order[$image])
   1.465 +								{
   1.466 +									$image_order[$_image]++;
   1.467 +								}
   1.468 +							}
   1.469 +						}
   1.470 +
   1.471 +						if ($action == 'modify'  && !empty($image_id[$image]))
   1.472 +						{
   1.473 +							$sql = "UPDATE $table
   1.474 +								SET " . $db->sql_build_array('UPDATE', $img_sql) . "
   1.475 +								WHERE {$fields}_id = " . $image_id[$image];
   1.476 +							$db->sql_query($sql);
   1.477 +							$icons_updated++;
   1.478 +						}
   1.479 +						else if ($action !== 'modify')
   1.480 +						{
   1.481 +							$sql = "INSERT INTO $table " . $db->sql_build_array('INSERT', $img_sql);
   1.482 +							$db->sql_query($sql);
   1.483 +							$icons_updated++;
   1.484 +						}
   1.485 +
   1.486 + 					}
   1.487 +				}
   1.488 +
   1.489 +				$cache->destroy('_icons');
   1.490 +				$cache->destroy('sql', $table);
   1.491 +
   1.492 +				$level = E_USER_NOTICE;
   1.493 +				switch ($icons_updated)
   1.494 +				{
   1.495 +					case 0:
   1.496 +						$suc_lang = "{$lang}_NONE";
   1.497 +						$level = E_USER_WARNING;
   1.498 +						break;
   1.499 +
   1.500 +					case 1:
   1.501 +						$suc_lang = "{$lang}_ONE";
   1.502 +						break;
   1.503 +
   1.504 +					default:
   1.505 +						$suc_lang = $lang;
   1.506 +				}
   1.507 +				$errormsgs = '';
   1.508 +				foreach ($errors as $img => $error)
   1.509 +				{
   1.510 +					$errormsgs .= '<br />' . sprintf($user->lang[$error], $img);
   1.511 +				}
   1.512 +				if ($action == 'modify')
   1.513 +				{
   1.514 +					trigger_error($user->lang[$suc_lang . '_EDITED'] . $errormsgs . adm_back_link($this->u_action), $level);
   1.515 +				}
   1.516 +				else
   1.517 +				{
   1.518 +					trigger_error($user->lang[$suc_lang . '_ADDED'] . $errormsgs . adm_back_link($this->u_action), $level);
   1.519 +				}
   1.520 +
   1.521 +			break;
   1.522 +
   1.523 +			case 'import':
   1.524 +
   1.525 +				$pak = request_var('pak', '');
   1.526 +				$current = request_var('current', '');
   1.527 +
   1.528 +				if ($pak != '')
   1.529 +				{
   1.530 +					$order = 0;
   1.531 +
   1.532 +					if (!($pak_ary = @file($phpbb_root_path . $img_path . '/' . $pak)))
   1.533 +					{
   1.534 +						trigger_error($user->lang['PAK_FILE_NOT_READABLE'] . adm_back_link($this->u_action), E_USER_WARNING);
   1.535 +					}
   1.536 +
   1.537 +					// Make sure the pak_ary is valid
   1.538 +					foreach ($pak_ary as $pak_entry)
   1.539 +					{
   1.540 +						if (preg_match_all("#'(.*?)', ?#", $pak_entry, $data))
   1.541 +						{
   1.542 +							if ((sizeof($data[1]) != 4 && $mode == 'icons') ||
   1.543 +								((sizeof($data[1]) != 6 || (empty($data[1][4]) || empty($data[1][5]))) && $mode == 'smilies' ))
   1.544 +							{
   1.545 +								trigger_error($user->lang['WRONG_PAK_TYPE'] . adm_back_link($this->u_action), E_USER_WARNING);
   1.546 +							}
   1.547 +						}
   1.548 +						else
   1.549 +						{
   1.550 +							trigger_error($user->lang['WRONG_PAK_TYPE'] . adm_back_link($this->u_action), E_USER_WARNING);
   1.551 +						}
   1.552 +					}
   1.553 +
   1.554 +					// The user has already selected a smilies_pak file
   1.555 +					if ($current == 'delete')
   1.556 +					{
   1.557 +						switch ($db->sql_layer)
   1.558 +						{
   1.559 +							case 'sqlite':
   1.560 +							case 'firebird':
   1.561 +								$db->sql_query('DELETE FROM ' . $table);
   1.562 +							break;
   1.563 +
   1.564 +							default:
   1.565 +								$db->sql_query('TRUNCATE TABLE ' . $table);
   1.566 +							break;
   1.567 +						}
   1.568 +
   1.569 +						switch ($mode)
   1.570 +						{
   1.571 +							case 'smilies':
   1.572 +							break;
   1.573 +
   1.574 +							case 'icons':
   1.575 +								// Reset all icon_ids
   1.576 +								$db->sql_query('UPDATE ' . TOPICS_TABLE . ' SET icon_id = 0');
   1.577 +								$db->sql_query('UPDATE ' . POSTS_TABLE . ' SET icon_id = 0');
   1.578 +							break;
   1.579 +						}
   1.580 +					}
   1.581 +					else
   1.582 +					{
   1.583 +						$cur_img = array();
   1.584 +
   1.585 +						$field_sql = ($mode == 'smilies') ? 'code' : 'icons_url';
   1.586 +
   1.587 +						$sql = "SELECT $field_sql
   1.588 +							FROM $table";
   1.589 +						$result = $db->sql_query($sql);
   1.590 +
   1.591 +						while ($row = $db->sql_fetchrow($result))
   1.592 +						{
   1.593 +							++$order;
   1.594 +							$cur_img[$row[$field_sql]] = 1;
   1.595 +						}
   1.596 +						$db->sql_freeresult($result);
   1.597 +					}
   1.598 +
   1.599 +					if ($mode == 'smilies')
   1.600 +					{
   1.601 +						$smiley_count = $this->item_count($table);
   1.602 +						if ($smiley_count + sizeof($pak_ary) > SMILEY_LIMIT)
   1.603 +						{
   1.604 +							trigger_error(sprintf($user->lang['TOO_MANY_SMILIES'], SMILEY_LIMIT) . adm_back_link($this->u_action), E_USER_WARNING);
   1.605 +						}
   1.606 +					}
   1.607 +
   1.608 +					foreach ($pak_ary as $pak_entry)
   1.609 +					{
   1.610 +						$data = array();
   1.611 +						if (preg_match_all("#'(.*?)', ?#", $pak_entry, $data))
   1.612 +						{
   1.613 +							if ((sizeof($data[1]) != 4 && $mode == 'icons') ||
   1.614 +								(sizeof($data[1]) != 6 && $mode == 'smilies'))
   1.615 +							{
   1.616 +								trigger_error($user->lang['WRONG_PAK_TYPE'] . adm_back_link($this->u_action), E_USER_WARNING);
   1.617 +							}
   1.618 +
   1.619 +							// Stripslash here because it got addslashed before... (on export)
   1.620 +							$img = stripslashes($data[1][0]);
   1.621 +							$width = stripslashes($data[1][1]);
   1.622 +							$height = stripslashes($data[1][2]);
   1.623 +							$display_on_posting = stripslashes($data[1][3]);
   1.624 +
   1.625 +							if (isset($data[1][4]) && isset($data[1][5]))
   1.626 +							{
   1.627 +								$emotion = stripslashes($data[1][4]);
   1.628 +								$code = stripslashes($data[1][5]);
   1.629 +							}
   1.630 +
   1.631 +							if ($current == 'replace' &&
   1.632 +								(($mode == 'smilies' && !empty($cur_img[$code])) ||
   1.633 +								($mode == 'icons' && !empty($cur_img[$img]))))
   1.634 +							{
   1.635 +								$replace_sql = ($mode == 'smilies') ? $code : $img;
   1.636 +								$sql = array(
   1.637 +									$fields . '_url'		=> $img,
   1.638 +									$fields . '_height'		=> (int) $height,
   1.639 +									$fields . '_width'		=> (int) $width,
   1.640 +									'display_on_posting'	=> (int) $display_on_posting,
   1.641 +								);
   1.642 +
   1.643 +								if ($mode == 'smilies')
   1.644 +								{
   1.645 +									$sql = array_merge($sql, array(
   1.646 +										'emotion'				=> $emotion,
   1.647 +									));
   1.648 +								}
   1.649 +
   1.650 +								$sql = "UPDATE $table SET " . $db->sql_build_array('UPDATE', $sql) . "
   1.651 +									WHERE $field_sql = '" . $db->sql_escape($replace_sql) . "'";
   1.652 +								$db->sql_query($sql);
   1.653 +							}
   1.654 +							else
   1.655 +							{
   1.656 +								++$order;
   1.657 +
   1.658 +								$sql = array(
   1.659 +									$fields . '_url'	=> $img,
   1.660 +									$fields . '_height'	=> (int) $height,
   1.661 +									$fields . '_width'	=> (int) $width,
   1.662 +									$fields . '_order'	=> (int) $order,
   1.663 +									'display_on_posting'=> (int) $display_on_posting,
   1.664 +								);
   1.665 +
   1.666 +								if ($mode == 'smilies')
   1.667 +								{
   1.668 +									$sql = array_merge($sql, array(
   1.669 +										'code'				=> $code,
   1.670 +										'emotion'			=> $emotion,
   1.671 +									));
   1.672 +								}
   1.673 +								$db->sql_query("INSERT INTO $table " . $db->sql_build_array('INSERT', $sql));
   1.674 +							}
   1.675 +						}
   1.676 +					}
   1.677 +
   1.678 +					$cache->destroy('_icons');
   1.679 +					$cache->destroy('sql', $table);
   1.680 +
   1.681 +					trigger_error($user->lang[$lang . '_IMPORT_SUCCESS'] . adm_back_link($this->u_action));
   1.682 +				}
   1.683 +				else
   1.684 +				{
   1.685 +					$pak_options = '';
   1.686 +
   1.687 +					foreach ($_paks as $pak)
   1.688 +					{
   1.689 +						$pak_options .= '<option value="' . $pak . '">' . htmlspecialchars($pak) . '</option>';
   1.690 +					}
   1.691 +
   1.692 +					$template->assign_vars(array(
   1.693 +						'S_CHOOSE_PAK'		=> true,
   1.694 +						'S_PAK_OPTIONS'		=> $pak_options,
   1.695 +
   1.696 +						'L_TITLE'			=> $user->lang['ACP_' . $lang],
   1.697 +						'L_EXPLAIN'			=> $user->lang['ACP_' . $lang . '_EXPLAIN'],
   1.698 +						'L_NO_PAK_OPTIONS'	=> $user->lang['NO_' . $lang . '_PAK'],
   1.699 +						'L_CURRENT'			=> $user->lang['CURRENT_' . $lang],
   1.700 +						'L_CURRENT_EXPLAIN'	=> $user->lang['CURRENT_' . $lang . '_EXPLAIN'],
   1.701 +						'L_IMPORT_SUBMIT'	=> $user->lang['IMPORT_' . $lang],
   1.702 +
   1.703 +						'U_BACK'		=> $this->u_action,
   1.704 +						'U_ACTION'		=> $this->u_action . '&amp;action=import',
   1.705 +						)
   1.706 +					);
   1.707 +				}
   1.708 +			break;
   1.709 +
   1.710 +			case 'export':
   1.711 +
   1.712 +				$this->page_title = 'EXPORT_' . $lang;
   1.713 +				$this->tpl_name = 'message_body';
   1.714 +
   1.715 +				$template->assign_vars(array(
   1.716 +					'MESSAGE_TITLE'		=> $user->lang['EXPORT_' . $lang],
   1.717 +					'MESSAGE_TEXT'		=> sprintf($user->lang['EXPORT_' . $lang . '_EXPLAIN'], '<a href="' . $this->u_action . '&amp;action=send">', '</a>'),
   1.718 +
   1.719 +					'S_USER_NOTICE'		=> true,
   1.720 +					)
   1.721 +				);
   1.722 +
   1.723 +				return;
   1.724 +
   1.725 +			break;
   1.726 +
   1.727 +			case 'send':
   1.728 +
   1.729 +				$sql = "SELECT *
   1.730 +					FROM $table
   1.731 +					ORDER BY {$fields}_order";
   1.732 +				$result = $db->sql_query($sql);
   1.733 +
   1.734 +				$pak = '';
   1.735 +				while ($row = $db->sql_fetchrow($result))
   1.736 +				{
   1.737 +					$pak .= "'" . addslashes($row[$fields . '_url']) . "', ";
   1.738 +					$pak .= "'" . addslashes($row[$fields . '_width']) . "', ";
   1.739 +					$pak .= "'" . addslashes($row[$fields . '_height']) . "', ";
   1.740 +					$pak .= "'" . addslashes($row['display_on_posting']) . "', ";
   1.741 +
   1.742 +					if ($mode == 'smilies')
   1.743 +					{
   1.744 +						$pak .= "'" . addslashes($row['emotion']) . "', ";
   1.745 +						$pak .= "'" . addslashes($row['code']) . "', ";
   1.746 +					}
   1.747 +
   1.748 +					$pak .= "\n";
   1.749 +				}
   1.750 +				$db->sql_freeresult($result);
   1.751 +
   1.752 +				if ($pak != '')
   1.753 +				{
   1.754 +					garbage_collection();
   1.755 +
   1.756 +					header('Pragma: public');
   1.757 +
   1.758 +					// Send out the Headers
   1.759 +					header('Content-Type: text/x-delimtext; name="' . $mode . '.pak"');
   1.760 +					header('Content-Disposition: inline; filename="' . $mode . '.pak"');
   1.761 +					echo $pak;
   1.762 +
   1.763 +					flush();
   1.764 +					exit;
   1.765 +				}
   1.766 +				else
   1.767 +				{
   1.768 +					trigger_error($user->lang['NO_' . strtoupper($fields) . '_EXPORT'] . adm_back_link($this->u_action), E_USER_WARNING);
   1.769 +				}
   1.770 +
   1.771 +			break;
   1.772 +
   1.773 +			case 'delete':
   1.774 +
   1.775 +				if (confirm_box(true))
   1.776 +				{
   1.777 +					$sql = "DELETE FROM $table
   1.778 +						WHERE {$fields}_id = $icon_id";
   1.779 +					$db->sql_query($sql);
   1.780 +
   1.781 +					switch ($mode)
   1.782 +					{
   1.783 +						case 'smilies':
   1.784 +						break;
   1.785 +
   1.786 +						case 'icons':
   1.787 +							// Reset appropriate icon_ids
   1.788 +							$db->sql_query('UPDATE ' . TOPICS_TABLE . "
   1.789 +								SET icon_id = 0
   1.790 +								WHERE icon_id = $icon_id");
   1.791 +
   1.792 +							$db->sql_query('UPDATE ' . POSTS_TABLE . "
   1.793 +								SET icon_id = 0
   1.794 +								WHERE icon_id = $icon_id");
   1.795 +						break;
   1.796 +					}
   1.797 +
   1.798 +					$notice = $user->lang[$lang . '_DELETED'];
   1.799 +
   1.800 +					$cache->destroy('_icons');
   1.801 +					$cache->destroy('sql', $table);
   1.802 +				}
   1.803 +				else
   1.804 +				{
   1.805 +					confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
   1.806 +						'i'			=> $id,
   1.807 +						'mode'		=> $mode,
   1.808 +						'id'		=> $icon_id,
   1.809 +						'action'	=> 'delete',
   1.810 +					)));
   1.811 +				}
   1.812 +
   1.813 +			break;
   1.814 +
   1.815 +			case 'move_up':
   1.816 +			case 'move_down':
   1.817 +
   1.818 +				// Get current order id...
   1.819 +				$sql = "SELECT {$fields}_order as current_order
   1.820 +					FROM $table
   1.821 +					WHERE {$fields}_id = $icon_id";
   1.822 +				$result = $db->sql_query($sql);
   1.823 +				$current_order = (int) $db->sql_fetchfield('current_order');
   1.824 +				$db->sql_freeresult($result);
   1.825 +
   1.826 +				if ($current_order == 0 && $action == 'move_up')
   1.827 +				{
   1.828 +					break;
   1.829 +				}
   1.830 +
   1.831 +				// on move_down, switch position with next order_id...
   1.832 +				// on move_up, switch position with previous order_id...
   1.833 +				$switch_order_id = ($action == 'move_down') ? $current_order + 1 : $current_order - 1;
   1.834 +
   1.835 +				//
   1.836 +				$sql = "UPDATE $table
   1.837 +					SET {$fields}_order = $current_order
   1.838 +					WHERE {$fields}_order = $switch_order_id
   1.839 +						AND {$fields}_id <> $icon_id";
   1.840 +				$db->sql_query($sql);
   1.841 +
   1.842 +				// Only update the other entry too if the previous entry got updated
   1.843 +				if ($db->sql_affectedrows())
   1.844 +				{
   1.845 +					$sql = "UPDATE $table
   1.846 +						SET {$fields}_order = $switch_order_id
   1.847 +						WHERE {$fields}_order = $current_order
   1.848 +							AND {$fields}_id = $icon_id";
   1.849 +					$db->sql_query($sql);
   1.850 +				}
   1.851 +
   1.852 +				$cache->destroy('_icons');
   1.853 +				$cache->destroy('sql', $table);
   1.854 +
   1.855 +			break;
   1.856 +		}
   1.857 +
   1.858 +		// By default, check that image_order is valid and fix it if necessary
   1.859 +		$sql = "SELECT {$fields}_id AS order_id, {$fields}_order AS fields_order
   1.860 +			FROM $table
   1.861 +			ORDER BY display_on_posting DESC, {$fields}_order";
   1.862 +		$result = $db->sql_query($sql);
   1.863 +
   1.864 +		if ($row = $db->sql_fetchrow($result))
   1.865 +		{
   1.866 +			$order = 0;
   1.867 +			do
   1.868 +			{
   1.869 +				++$order;
   1.870 +				if ($row['fields_order'] != $order)
   1.871 +				{
   1.872 +					$db->sql_query("UPDATE $table
   1.873 +						SET {$fields}_order = $order
   1.874 +						WHERE {$fields}_id = " . $row['order_id']);
   1.875 +				}
   1.876 +			}
   1.877 +			while ($row = $db->sql_fetchrow($result));
   1.878 +		}
   1.879 +		$db->sql_freeresult($result);
   1.880 +
   1.881 +		$template->assign_vars(array(
   1.882 +			'L_TITLE'			=> $user->lang['ACP_' . $lang],
   1.883 +			'L_EXPLAIN'			=> $user->lang['ACP_' . $lang . '_EXPLAIN'],
   1.884 +			'L_IMPORT'			=> $user->lang['IMPORT_' . $lang],
   1.885 +			'L_EXPORT'			=> $user->lang['EXPORT_' . $lang],
   1.886 +			'L_NOT_DISPLAYED'	=> $user->lang[$lang . '_NOT_DISPLAYED'],
   1.887 +			'L_ICON_ADD'		=> $user->lang['ADD_' . $lang],
   1.888 +			'L_ICON_EDIT'		=> $user->lang['EDIT_' . $lang],
   1.889 +
   1.890 +			'NOTICE'			=> $notice,
   1.891 +			'COLSPAN'			=> ($mode == 'smilies') ? 5 : 3,
   1.892 +
   1.893 +			'S_SMILIES'			=> ($mode == 'smilies') ? true : false,
   1.894 +
   1.895 +			'U_ACTION'			=> $this->u_action,
   1.896 +			'U_IMPORT'			=> $this->u_action . '&amp;action=import',
   1.897 +			'U_EXPORT'			=> $this->u_action . '&amp;action=export',
   1.898 +			)
   1.899 +		);
   1.900 +
   1.901 +		$spacer = false;
   1.902 +		$pagination_start = request_var('start', 0);
   1.903 +
   1.904 +		$item_count = $this->item_count($table);
   1.905 +
   1.906 +		$sql = "SELECT *
   1.907 +			FROM $table
   1.908 +			ORDER BY {$fields}_order ASC";
   1.909 +		$result = $db->sql_query_limit($sql, $config['smilies_per_page'], $pagination_start);
   1.910 +
   1.911 +		while ($row = $db->sql_fetchrow($result))
   1.912 +		{
   1.913 +			$alt_text = ($mode == 'smilies') ? $row['code'] : '';
   1.914 +
   1.915 +			$template->assign_block_vars('items', array(
   1.916 +				'S_SPACER'		=> (!$spacer && !$row['display_on_posting']) ? true : false,
   1.917 +				'ALT_TEXT'		=> $alt_text,
   1.918 +				'IMG_SRC'		=> $phpbb_root_path . $img_path . '/' . $row[$fields . '_url'],
   1.919 +				'WIDTH'			=> $row[$fields . '_width'],
   1.920 +				'HEIGHT'		=> $row[$fields . '_height'],
   1.921 +				'CODE'			=> (isset($row['code'])) ? $row['code'] : '',
   1.922 +				'EMOTION'		=> (isset($row['emotion'])) ? $row['emotion'] : '',
   1.923 +				'U_EDIT'		=> $this->u_action . '&amp;action=edit&amp;id=' . $row[$fields . '_id'],
   1.924 +				'U_DELETE'		=> $this->u_action . '&amp;action=delete&amp;id=' . $row[$fields . '_id'],
   1.925 +				'U_MOVE_UP'		=> $this->u_action . '&amp;action=move_up&amp;id=' . $row[$fields . '_id'] . '&amp;start=' . $pagination_start,
   1.926 +				'U_MOVE_DOWN'	=> $this->u_action . '&amp;action=move_down&amp;id=' . $row[$fields . '_id'] . '&amp;start=' . $pagination_start,
   1.927 +			));
   1.928 +
   1.929 +			if (!$spacer && !$row['display_on_posting'])
   1.930 +			{
   1.931 +				$spacer = true;
   1.932 +			}
   1.933 +		}
   1.934 +		$db->sql_freeresult($result);
   1.935 +
   1.936 +		$template->assign_var('PAGINATION',
   1.937 +			generate_pagination($this->u_action, $item_count, $config['smilies_per_page'], $pagination_start, true)
   1.938 +		);
   1.939 +	}
   1.940 +
   1.941 +	/**
   1.942 +	 * Returns the count of smilies or icons in the database
   1.943 +	 *
   1.944 +	 * @param string $table The table of items to count.
   1.945 +	 * @return int number of items
   1.946 +	 */
   1.947 +	/* private */ function item_count($table)
   1.948 +	{
   1.949 +		global $db;
   1.950 +
   1.951 +		$sql = "SELECT COUNT(*) AS item_count
   1.952 +			FROM $table";
   1.953 +		$result = $db->sql_query($sql);
   1.954 +		$item_count = (int) $db->sql_fetchfield('item_count');
   1.955 +		$db->sql_freeresult($result);
   1.956 +
   1.957 +		return $item_count;
   1.958 +	}
   1.959 +}
   1.960 +
   1.961 +?>
   1.962 \ No newline at end of file