includes/acp/acp_icons.php
changeset 0 ceef6d64c21d
equal deleted inserted replaced
-1:000000000000 0:ceef6d64c21d
       
     1 <?php
       
     2 /**
       
     3 *
       
     4 * @package acp
       
     5 * @version $Id$
       
     6 * @copyright (c) 2005 phpBB Group
       
     7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
       
     8 *
       
     9 */
       
    10 
       
    11 /**
       
    12 * @ignore
       
    13 */
       
    14 if (!defined('IN_PHPBB'))
       
    15 {
       
    16 	exit;
       
    17 }
       
    18 
       
    19 /**
       
    20 * @todo [smilies] check regular expressions for special char replacements (stored specialchared in db)
       
    21 * @package acp
       
    22 */
       
    23 class acp_icons
       
    24 {
       
    25 	var $u_action;
       
    26 
       
    27 	function main($id, $mode)
       
    28 	{
       
    29 		global $db, $user, $auth, $template, $cache;
       
    30 		global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
       
    31 
       
    32 		$user->add_lang('acp/posting');
       
    33 
       
    34 		// Set up general vars
       
    35 		$action = request_var('action', '');
       
    36 		$action = (isset($_POST['add'])) ? 'add' : $action;
       
    37 		$action = (isset($_POST['edit'])) ? 'edit' : $action;
       
    38 		$action = (isset($_POST['import'])) ? 'import' : $action;
       
    39 		$icon_id = request_var('id', 0);
       
    40 
       
    41 		$mode = ($mode == 'smilies') ? 'smilies' : 'icons';
       
    42 
       
    43 		$this->tpl_name = 'acp_icons';
       
    44 
       
    45 		// What are we working on?
       
    46 		switch ($mode)
       
    47 		{
       
    48 			case 'smilies':
       
    49 				$table = SMILIES_TABLE;
       
    50 				$lang = 'SMILIES';
       
    51 				$fields = 'smiley';
       
    52 				$img_path = $config['smilies_path'];
       
    53 			break;
       
    54 
       
    55 			case 'icons':
       
    56 				$table = ICONS_TABLE;
       
    57 				$lang = 'ICONS';
       
    58 				$fields = 'icons';
       
    59 				$img_path = $config['icons_path'];
       
    60 			break;
       
    61 		}
       
    62 
       
    63 		$this->page_title = 'ACP_' . $lang;
       
    64 
       
    65 		// Clear some arrays
       
    66 		$_images = $_paks = array();
       
    67 		$notice = '';
       
    68 
       
    69 		// Grab file list of paks and images
       
    70 		if ($action == 'edit' || $action == 'add' || $action == 'import')
       
    71 		{
       
    72 			$imglist = filelist($phpbb_root_path . $img_path, '');
       
    73 
       
    74 			foreach ($imglist as $path => $img_ary)
       
    75 			{
       
    76 				if (empty($img_ary))
       
    77 				{
       
    78 					continue;
       
    79 				}
       
    80 
       
    81 				asort($img_ary, SORT_STRING);
       
    82 
       
    83 				foreach ($img_ary as $img)
       
    84 				{
       
    85 					$img_size = getimagesize($phpbb_root_path . $img_path . '/' . $path . $img);
       
    86 
       
    87 					if (!$img_size[0] || !$img_size[1] || strlen($img) > 255)
       
    88 					{
       
    89 						continue;
       
    90 					}
       
    91 
       
    92 					// adjust the width and height to be lower than 128px while perserving the aspect ratio (for icons)
       
    93 					if ($mode == 'icons')
       
    94 					{
       
    95 						if ($img_size[0] > 127 && $img_size[0] > $img_size[1])
       
    96 						{
       
    97 							$img_size[1] = (int) ($img_size[1] * (127 / $img_size[0]));
       
    98 							$img_size[0] = 127;
       
    99 						}
       
   100 						else if ($img_size[1] > 127)
       
   101 						{
       
   102 							$img_size[0] = (int) ($img_size[0] * (127 / $img_size[1]));
       
   103 							$img_size[1] = 127;
       
   104 						}
       
   105 					}
       
   106 
       
   107 					$_images[$path . $img]['file'] = $path . $img;
       
   108 					$_images[$path . $img]['width'] = $img_size[0];
       
   109 					$_images[$path . $img]['height'] = $img_size[1];
       
   110 				}
       
   111 			}
       
   112 			unset($imglist);
       
   113 
       
   114 			if ($dir = @opendir($phpbb_root_path . $img_path))
       
   115 			{
       
   116 				while (($file = readdir($dir)) !== false)
       
   117 				{
       
   118 					if (is_file($phpbb_root_path . $img_path . '/' . $file) && preg_match('#\.pak$#i', $file))
       
   119 					{
       
   120 						$_paks[] = $file;
       
   121 					}
       
   122 				}
       
   123 				closedir($dir);
       
   124 
       
   125 				if (!empty($_paks))
       
   126 				{
       
   127 					asort($_paks, SORT_STRING);
       
   128 				}
       
   129 			}
       
   130 		}
       
   131 
       
   132 		// What shall we do today? Oops, I believe that's trademarked ...
       
   133 		switch ($action)
       
   134 		{
       
   135 			case 'edit':
       
   136 				unset($_images);
       
   137 				$_images = array();
       
   138 
       
   139 			// no break;
       
   140 
       
   141 			case 'add':
       
   142 
       
   143 				$smilies = $default_row = array();
       
   144 				$smiley_options = $order_list = $add_order_list = '';
       
   145 
       
   146 				if ($action == 'add' && $mode == 'smilies')
       
   147 				{
       
   148 					$sql = 'SELECT *
       
   149 						FROM ' . SMILIES_TABLE . '
       
   150 						ORDER BY smiley_order';
       
   151 					$result = $db->sql_query($sql);
       
   152 
       
   153 					while ($row = $db->sql_fetchrow($result))
       
   154 					{
       
   155 						if (empty($smilies[$row['smiley_url']]))
       
   156 						{
       
   157 							$smilies[$row['smiley_url']] = $row;
       
   158 						}
       
   159 					}
       
   160 					$db->sql_freeresult($result);
       
   161 
       
   162 					if (sizeof($smilies))
       
   163 					{
       
   164 						foreach ($smilies as $row)
       
   165 						{
       
   166 							$selected = false;
       
   167 
       
   168 							if (!$smiley_options)
       
   169 							{
       
   170 								$selected = true;
       
   171 								$default_row = $row;
       
   172 							}
       
   173 							$smiley_options .= '<option value="' . $row['smiley_url'] . '"' . (($selected) ? ' selected="selected"' : '') . '>' . $row['smiley_url'] . '</option>';
       
   174 
       
   175 							$template->assign_block_vars('smile', array(
       
   176 								'SMILEY_URL'	=> addslashes($row['smiley_url']),
       
   177 								'CODE'			=> addslashes($row['code']),
       
   178 								'EMOTION'		=> addslashes($row['emotion']),
       
   179 								'WIDTH'			=> $row['smiley_width'],
       
   180 								'HEIGHT'		=> $row['smiley_height'],
       
   181 								'ORDER'			=> $row['smiley_order'] + 1,
       
   182 							));
       
   183 						}
       
   184 					}
       
   185 				}
       
   186 
       
   187 				$sql = "SELECT *
       
   188 					FROM $table
       
   189 					ORDER BY {$fields}_order " . (($icon_id || $action == 'add') ? 'DESC' : 'ASC');
       
   190 				$result = $db->sql_query($sql);
       
   191 
       
   192 				$data = array();
       
   193 				$after = false;
       
   194 				$display = 0;
       
   195 				$order_lists = array('', '');
       
   196 				$add_order_lists = array('', '');
       
   197 				$display_count = 0;
       
   198 
       
   199 				while ($row = $db->sql_fetchrow($result))
       
   200 				{
       
   201 					if ($action == 'add')
       
   202 					{
       
   203 						unset($_images[$row[$fields . '_url']]);
       
   204 					}
       
   205 
       
   206 
       
   207 					if ($row[$fields . '_id'] == $icon_id)
       
   208 					{
       
   209 						$after = true;
       
   210 						$display = $row['display_on_posting'];
       
   211 						$data[$row[$fields . '_url']] = $row;
       
   212 					}
       
   213 					else
       
   214 					{
       
   215 						if ($action == 'edit' && !$icon_id)
       
   216 						{
       
   217 							$data[$row[$fields . '_url']] = $row;
       
   218 						}
       
   219 
       
   220 						$selected = '';
       
   221 						if (!empty($after))
       
   222 						{
       
   223 							$selected = ' selected="selected"';
       
   224 							$after = false;
       
   225 						}
       
   226 						if ($row['display_on_posting'])
       
   227 						{
       
   228 							$display_count++;
       
   229 						}
       
   230 						$after_txt = ($mode == 'smilies') ? $row['code'] : $row['icons_url'];
       
   231 						$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']];
       
   232 
       
   233 						if (!empty($default_row))
       
   234 						{
       
   235 							$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']];
       
   236 						}
       
   237 					}
       
   238 				}
       
   239 				$db->sql_freeresult($result);
       
   240 
       
   241 				$order_list = '<option value="1"' . ((!isset($after)) ? ' selected="selected"' : '') . '>' . $user->lang['FIRST'] . '</option>';
       
   242 				$add_order_list = '<option value="1">' . $user->lang['FIRST'] . '</option>';
       
   243 
       
   244 				if ($action == 'add')
       
   245 				{
       
   246 					$data = $_images;
       
   247 				}
       
   248 
       
   249 				$colspan = (($mode == 'smilies') ? 7 : 5);
       
   250 				$colspan += ($icon_id) ? 1 : 0;
       
   251 				$colspan += ($action == 'add') ? 2 : 0;
       
   252 
       
   253 				$template->assign_vars(array(
       
   254 					'S_EDIT'		=> true,
       
   255 					'S_SMILIES'		=> ($mode == 'smilies') ? true : false,
       
   256 					'S_ADD'			=> ($action == 'add') ? true : false,
       
   257 
       
   258 					'S_ORDER_LIST_DISPLAY'		=> $order_list . $order_lists[1],
       
   259 					'S_ORDER_LIST_UNDISPLAY'	=> $order_list . $order_lists[0],
       
   260 					'S_ORDER_LIST_DISPLAY_COUNT'	=> $display_count + 1,
       
   261 
       
   262 					'L_TITLE'		=> $user->lang['ACP_' . $lang],
       
   263 					'L_EXPLAIN'		=> $user->lang['ACP_' . $lang . '_EXPLAIN'],
       
   264 					'L_CONFIG'		=> $user->lang[$lang . '_CONFIG'],
       
   265 					'L_URL'			=> $user->lang[$lang . '_URL'],
       
   266 					'L_LOCATION'	=> $user->lang[$lang . '_LOCATION'],
       
   267 					'L_WIDTH'		=> $user->lang[$lang . '_WIDTH'],
       
   268 					'L_HEIGHT'		=> $user->lang[$lang . '_HEIGHT'],
       
   269 					'L_ORDER'		=> $user->lang[$lang . '_ORDER'],
       
   270 					'L_NO_ICONS'	=> $user->lang['NO_' . $lang . '_' . strtoupper($action)],
       
   271 
       
   272 					'COLSPAN'		=> $colspan,
       
   273 					'ID'			=> $icon_id,
       
   274 
       
   275 					'U_BACK'		=> $this->u_action,
       
   276 					'U_ACTION'		=> $this->u_action . '&amp;action=' . (($action == 'add') ? 'create' : 'modify'),
       
   277 				));
       
   278 
       
   279 				foreach ($data as $img => $img_row)
       
   280 				{
       
   281 					$template->assign_block_vars('items', array(
       
   282 						'IMG'		=> $img,
       
   283 						'A_IMG'		=> addslashes($img),
       
   284 						'IMG_SRC'	=> $phpbb_root_path . $img_path . '/' . $img,
       
   285 
       
   286 						'CODE'		=> ($mode == 'smilies' && isset($img_row['code'])) ? $img_row['code'] : '',
       
   287 						'EMOTION'	=> ($mode == 'smilies' && isset($img_row['emotion'])) ? $img_row['emotion'] : '',
       
   288 
       
   289 						'S_ID'				=> (isset($img_row[$fields . '_id'])) ? true : false,
       
   290 						'ID'				=> (isset($img_row[$fields . '_id'])) ? $img_row[$fields . '_id'] : 0,
       
   291 						'WIDTH'				=> (!empty($img_row[$fields .'_width'])) ? $img_row[$fields .'_width'] : $img_row['width'],
       
   292 						'HEIGHT'			=> (!empty($img_row[$fields .'_height'])) ? $img_row[$fields .'_height'] : $img_row['height'],
       
   293 						'POSTING_CHECKED'	=> (!empty($img_row['display_on_posting']) || $action == 'add') ? ' checked="checked"' : '',
       
   294 					));
       
   295 				}
       
   296 
       
   297 				// Ok, another row for adding an addition code for a pre-existing image...
       
   298 				if ($action == 'add' && $mode == 'smilies' && sizeof($smilies))
       
   299 				{
       
   300 					$template->assign_vars(array(
       
   301 						'S_ADD_CODE'		=> true,
       
   302 
       
   303 						'S_IMG_OPTIONS'		=> $smiley_options,
       
   304 
       
   305 						'S_ADD_ORDER_LIST_DISPLAY'		=> $add_order_list . $add_order_lists[1],
       
   306 						'S_ADD_ORDER_LIST_UNDISPLAY'	=> $add_order_list . $add_order_lists[0],
       
   307 
       
   308 						'IMG_SRC'			=> $phpbb_root_path . $img_path . '/' . $default_row['smiley_url'],
       
   309 						'IMG_PATH'			=> $img_path,
       
   310 						'PHPBB_ROOT_PATH'	=> $phpbb_root_path,
       
   311 
       
   312 						'CODE'				=> $default_row['code'],
       
   313 						'EMOTION'			=> $default_row['emotion'],
       
   314 
       
   315 						'WIDTH'				=> $default_row['smiley_width'],
       
   316 						'HEIGHT'			=> $default_row['smiley_height'],
       
   317 					));
       
   318 				}
       
   319 
       
   320 				return;
       
   321 
       
   322 			break;
       
   323 
       
   324 			case 'create':
       
   325 			case 'modify':
       
   326 
       
   327 				// Get items to create/modify
       
   328 				$images = (isset($_POST['image'])) ? array_keys(request_var('image', array('' => 0))) : array();
       
   329 
       
   330 				// Now really get the items
       
   331 				$image_id		= (isset($_POST['id'])) ? request_var('id', array('' => 0)) : array();
       
   332 				$image_order	= (isset($_POST['order'])) ? request_var('order', array('' => 0)) : array();
       
   333 				$image_width	= (isset($_POST['width'])) ? request_var('width', array('' => 0)) : array();
       
   334 				$image_height	= (isset($_POST['height'])) ? request_var('height', array('' => 0)) : array();
       
   335 				$image_add		= (isset($_POST['add_img'])) ? request_var('add_img', array('' => 0)) : array();
       
   336 				$image_emotion	= utf8_normalize_nfc(request_var('emotion', array('' => ''), true));
       
   337 				$image_code		= utf8_normalize_nfc(request_var('code', array('' => ''), true));
       
   338 				$image_display_on_posting = (isset($_POST['display_on_posting'])) ? request_var('display_on_posting', array('' => 0)) : array();
       
   339 
       
   340 				// Ok, add the relevant bits if we are adding new codes to existing emoticons...
       
   341 				if (!empty($_POST['add_additional_code']))
       
   342 				{
       
   343 					$add_image			= request_var('add_image', '');
       
   344 					$add_code			= utf8_normalize_nfc(request_var('add_code', '', true));
       
   345 					$add_emotion		= utf8_normalize_nfc(request_var('add_emotion', '', true));
       
   346 
       
   347 					if ($add_image && $add_emotion && $add_code)
       
   348 					{
       
   349 						$images[] = $add_image;
       
   350 						$image_add[$add_image] = true;
       
   351 
       
   352 						$image_code[$add_image] = $add_code;
       
   353 						$image_emotion[$add_image] = $add_emotion;
       
   354 						$image_width[$add_image] = request_var('add_width', 0);
       
   355 						$image_height[$add_image] = request_var('add_height', 0);
       
   356 
       
   357 						if (!empty($_POST['add_display_on_posting']))
       
   358 						{
       
   359 							$image_display_on_posting[$add_image] = 1;
       
   360 						}
       
   361 
       
   362 						$image_order[$add_image] = request_var('add_order', 0);
       
   363 					}
       
   364 				}
       
   365 
       
   366 				if ($mode == 'smilies' && $action == 'create')
       
   367 				{
       
   368 					$smiley_count = $this->item_count($table);
       
   369 
       
   370 					$addable_smileys_count = sizeof($images);
       
   371 					foreach ($images as $image)
       
   372 					{
       
   373 						if (!isset($image_add[$image]))
       
   374 						{
       
   375 							--$addable_smileys_count;
       
   376 						}
       
   377 					}
       
   378 
       
   379 					if ($smiley_count + $addable_smileys_count > SMILEY_LIMIT)
       
   380 					{
       
   381 						trigger_error(sprintf($user->lang['TOO_MANY_SMILIES'], SMILEY_LIMIT) . adm_back_link($this->u_action), E_USER_WARNING);
       
   382 					}
       
   383 				}
       
   384 
       
   385 				$icons_updated = 0;
       
   386 				$errors = array();
       
   387 				foreach ($images as $image)
       
   388 				{
       
   389 					if ($mode == 'smilies' && ($image_emotion[$image] == '' || $image_code[$image] == ''))
       
   390 					{
       
   391 						$errors[$image] = 'SMILIE_NO_' . (($image_emotion[$image] == '') ? 'EMOTION' : 'CODE');
       
   392 					}
       
   393 					else if ($action == 'create' && !isset($image_add[$image]))
       
   394 					{
       
   395 						// skip images where add wasn't checked
       
   396 					}
       
   397 					else if (!file_exists($phpbb_root_path . $img_path . '/' . $image))
       
   398 					{
       
   399 						$errors[$image] = 'SMILIE_NO_FILE';
       
   400 					}
       
   401 					else
       
   402 					{
       
   403 						if ($image_width[$image] == 0 || $image_height[$image] == 0)
       
   404 						{
       
   405 							$img_size = getimagesize($phpbb_root_path . $img_path . '/' . $image);
       
   406 							$image_width[$image] = $img_size[0];
       
   407 							$image_height[$image] = $img_size[1];
       
   408 						}
       
   409 
       
   410 						// Adjust image width/height for icons
       
   411 						if ($mode == 'icons')
       
   412 						{
       
   413 							if ($image_width[$image] > 127 && $image_width[$image] > $image_height[$image])
       
   414 							{
       
   415 								$image_height[$image] = (int) ($image_height[$image] * (127 / $image_width[$image]));
       
   416 								$image_width[$image] = 127;
       
   417 							}
       
   418 							else if ($image_height[$image] > 127)
       
   419 							{
       
   420 								$image_width[$image] = (int) ($image_width[$image] * (127 / $image_height[$image]));
       
   421 								$image_height[$image] = 127;
       
   422 							}
       
   423 						}
       
   424 
       
   425 						$img_sql = array(
       
   426 							$fields . '_url'		=> $image,
       
   427 							$fields . '_width'		=> $image_width[$image],
       
   428 							$fields . '_height'		=> $image_height[$image],
       
   429 							'display_on_posting'	=> (isset($image_display_on_posting[$image])) ? 1 : 0,
       
   430 						);
       
   431 
       
   432 						if ($mode == 'smilies')
       
   433 						{
       
   434 							$img_sql = array_merge($img_sql, array(
       
   435 								'emotion'	=> $image_emotion[$image],
       
   436 								'code'		=> $image_code[$image])
       
   437 							);
       
   438 						}
       
   439 
       
   440 						// Image_order holds the 'new' order value
       
   441 						if (!empty($image_order[$image]))
       
   442 						{
       
   443 							$img_sql = array_merge($img_sql, array(
       
   444 								$fields . '_order'	=>	$image_order[$image])
       
   445 							);
       
   446 
       
   447 							// Since we always add 'after' an item, we just need to increase all following + the current by one
       
   448 							$sql = "UPDATE $table
       
   449 								SET {$fields}_order = {$fields}_order + 1
       
   450 								WHERE {$fields}_order >= {$image_order[$image]}";
       
   451 							$db->sql_query($sql);
       
   452 
       
   453 							// If we adjust the order, we need to adjust all other orders too - they became inaccurate...
       
   454 							foreach ($image_order as $_image => $_order)
       
   455 							{
       
   456 								if ($_image == $image)
       
   457 								{
       
   458 									continue;
       
   459 								}
       
   460 
       
   461 								if ($_order >= $image_order[$image])
       
   462 								{
       
   463 									$image_order[$_image]++;
       
   464 								}
       
   465 							}
       
   466 						}
       
   467 
       
   468 						if ($action == 'modify'  && !empty($image_id[$image]))
       
   469 						{
       
   470 							$sql = "UPDATE $table
       
   471 								SET " . $db->sql_build_array('UPDATE', $img_sql) . "
       
   472 								WHERE {$fields}_id = " . $image_id[$image];
       
   473 							$db->sql_query($sql);
       
   474 							$icons_updated++;
       
   475 						}
       
   476 						else if ($action !== 'modify')
       
   477 						{
       
   478 							$sql = "INSERT INTO $table " . $db->sql_build_array('INSERT', $img_sql);
       
   479 							$db->sql_query($sql);
       
   480 							$icons_updated++;
       
   481 						}
       
   482 
       
   483  					}
       
   484 				}
       
   485 
       
   486 				$cache->destroy('_icons');
       
   487 				$cache->destroy('sql', $table);
       
   488 
       
   489 				$level = E_USER_NOTICE;
       
   490 				switch ($icons_updated)
       
   491 				{
       
   492 					case 0:
       
   493 						$suc_lang = "{$lang}_NONE";
       
   494 						$level = E_USER_WARNING;
       
   495 						break;
       
   496 
       
   497 					case 1:
       
   498 						$suc_lang = "{$lang}_ONE";
       
   499 						break;
       
   500 
       
   501 					default:
       
   502 						$suc_lang = $lang;
       
   503 				}
       
   504 				$errormsgs = '';
       
   505 				foreach ($errors as $img => $error)
       
   506 				{
       
   507 					$errormsgs .= '<br />' . sprintf($user->lang[$error], $img);
       
   508 				}
       
   509 				if ($action == 'modify')
       
   510 				{
       
   511 					trigger_error($user->lang[$suc_lang . '_EDITED'] . $errormsgs . adm_back_link($this->u_action), $level);
       
   512 				}
       
   513 				else
       
   514 				{
       
   515 					trigger_error($user->lang[$suc_lang . '_ADDED'] . $errormsgs . adm_back_link($this->u_action), $level);
       
   516 				}
       
   517 
       
   518 			break;
       
   519 
       
   520 			case 'import':
       
   521 
       
   522 				$pak = request_var('pak', '');
       
   523 				$current = request_var('current', '');
       
   524 
       
   525 				if ($pak != '')
       
   526 				{
       
   527 					$order = 0;
       
   528 
       
   529 					if (!($pak_ary = @file($phpbb_root_path . $img_path . '/' . $pak)))
       
   530 					{
       
   531 						trigger_error($user->lang['PAK_FILE_NOT_READABLE'] . adm_back_link($this->u_action), E_USER_WARNING);
       
   532 					}
       
   533 
       
   534 					// Make sure the pak_ary is valid
       
   535 					foreach ($pak_ary as $pak_entry)
       
   536 					{
       
   537 						if (preg_match_all("#'(.*?)', ?#", $pak_entry, $data))
       
   538 						{
       
   539 							if ((sizeof($data[1]) != 4 && $mode == 'icons') ||
       
   540 								((sizeof($data[1]) != 6 || (empty($data[1][4]) || empty($data[1][5]))) && $mode == 'smilies' ))
       
   541 							{
       
   542 								trigger_error($user->lang['WRONG_PAK_TYPE'] . adm_back_link($this->u_action), E_USER_WARNING);
       
   543 							}
       
   544 						}
       
   545 						else
       
   546 						{
       
   547 							trigger_error($user->lang['WRONG_PAK_TYPE'] . adm_back_link($this->u_action), E_USER_WARNING);
       
   548 						}
       
   549 					}
       
   550 
       
   551 					// The user has already selected a smilies_pak file
       
   552 					if ($current == 'delete')
       
   553 					{
       
   554 						switch ($db->sql_layer)
       
   555 						{
       
   556 							case 'sqlite':
       
   557 							case 'firebird':
       
   558 								$db->sql_query('DELETE FROM ' . $table);
       
   559 							break;
       
   560 
       
   561 							default:
       
   562 								$db->sql_query('TRUNCATE TABLE ' . $table);
       
   563 							break;
       
   564 						}
       
   565 
       
   566 						switch ($mode)
       
   567 						{
       
   568 							case 'smilies':
       
   569 							break;
       
   570 
       
   571 							case 'icons':
       
   572 								// Reset all icon_ids
       
   573 								$db->sql_query('UPDATE ' . TOPICS_TABLE . ' SET icon_id = 0');
       
   574 								$db->sql_query('UPDATE ' . POSTS_TABLE . ' SET icon_id = 0');
       
   575 							break;
       
   576 						}
       
   577 					}
       
   578 					else
       
   579 					{
       
   580 						$cur_img = array();
       
   581 
       
   582 						$field_sql = ($mode == 'smilies') ? 'code' : 'icons_url';
       
   583 
       
   584 						$sql = "SELECT $field_sql
       
   585 							FROM $table";
       
   586 						$result = $db->sql_query($sql);
       
   587 
       
   588 						while ($row = $db->sql_fetchrow($result))
       
   589 						{
       
   590 							++$order;
       
   591 							$cur_img[$row[$field_sql]] = 1;
       
   592 						}
       
   593 						$db->sql_freeresult($result);
       
   594 					}
       
   595 
       
   596 					if ($mode == 'smilies')
       
   597 					{
       
   598 						$smiley_count = $this->item_count($table);
       
   599 						if ($smiley_count + sizeof($pak_ary) > SMILEY_LIMIT)
       
   600 						{
       
   601 							trigger_error(sprintf($user->lang['TOO_MANY_SMILIES'], SMILEY_LIMIT) . adm_back_link($this->u_action), E_USER_WARNING);
       
   602 						}
       
   603 					}
       
   604 
       
   605 					foreach ($pak_ary as $pak_entry)
       
   606 					{
       
   607 						$data = array();
       
   608 						if (preg_match_all("#'(.*?)', ?#", $pak_entry, $data))
       
   609 						{
       
   610 							if ((sizeof($data[1]) != 4 && $mode == 'icons') ||
       
   611 								(sizeof($data[1]) != 6 && $mode == 'smilies'))
       
   612 							{
       
   613 								trigger_error($user->lang['WRONG_PAK_TYPE'] . adm_back_link($this->u_action), E_USER_WARNING);
       
   614 							}
       
   615 
       
   616 							// Stripslash here because it got addslashed before... (on export)
       
   617 							$img = stripslashes($data[1][0]);
       
   618 							$width = stripslashes($data[1][1]);
       
   619 							$height = stripslashes($data[1][2]);
       
   620 							$display_on_posting = stripslashes($data[1][3]);
       
   621 
       
   622 							if (isset($data[1][4]) && isset($data[1][5]))
       
   623 							{
       
   624 								$emotion = stripslashes($data[1][4]);
       
   625 								$code = stripslashes($data[1][5]);
       
   626 							}
       
   627 
       
   628 							if ($current == 'replace' &&
       
   629 								(($mode == 'smilies' && !empty($cur_img[$code])) ||
       
   630 								($mode == 'icons' && !empty($cur_img[$img]))))
       
   631 							{
       
   632 								$replace_sql = ($mode == 'smilies') ? $code : $img;
       
   633 								$sql = array(
       
   634 									$fields . '_url'		=> $img,
       
   635 									$fields . '_height'		=> (int) $height,
       
   636 									$fields . '_width'		=> (int) $width,
       
   637 									'display_on_posting'	=> (int) $display_on_posting,
       
   638 								);
       
   639 
       
   640 								if ($mode == 'smilies')
       
   641 								{
       
   642 									$sql = array_merge($sql, array(
       
   643 										'emotion'				=> $emotion,
       
   644 									));
       
   645 								}
       
   646 
       
   647 								$sql = "UPDATE $table SET " . $db->sql_build_array('UPDATE', $sql) . "
       
   648 									WHERE $field_sql = '" . $db->sql_escape($replace_sql) . "'";
       
   649 								$db->sql_query($sql);
       
   650 							}
       
   651 							else
       
   652 							{
       
   653 								++$order;
       
   654 
       
   655 								$sql = array(
       
   656 									$fields . '_url'	=> $img,
       
   657 									$fields . '_height'	=> (int) $height,
       
   658 									$fields . '_width'	=> (int) $width,
       
   659 									$fields . '_order'	=> (int) $order,
       
   660 									'display_on_posting'=> (int) $display_on_posting,
       
   661 								);
       
   662 
       
   663 								if ($mode == 'smilies')
       
   664 								{
       
   665 									$sql = array_merge($sql, array(
       
   666 										'code'				=> $code,
       
   667 										'emotion'			=> $emotion,
       
   668 									));
       
   669 								}
       
   670 								$db->sql_query("INSERT INTO $table " . $db->sql_build_array('INSERT', $sql));
       
   671 							}
       
   672 						}
       
   673 					}
       
   674 
       
   675 					$cache->destroy('_icons');
       
   676 					$cache->destroy('sql', $table);
       
   677 
       
   678 					trigger_error($user->lang[$lang . '_IMPORT_SUCCESS'] . adm_back_link($this->u_action));
       
   679 				}
       
   680 				else
       
   681 				{
       
   682 					$pak_options = '';
       
   683 
       
   684 					foreach ($_paks as $pak)
       
   685 					{
       
   686 						$pak_options .= '<option value="' . $pak . '">' . htmlspecialchars($pak) . '</option>';
       
   687 					}
       
   688 
       
   689 					$template->assign_vars(array(
       
   690 						'S_CHOOSE_PAK'		=> true,
       
   691 						'S_PAK_OPTIONS'		=> $pak_options,
       
   692 
       
   693 						'L_TITLE'			=> $user->lang['ACP_' . $lang],
       
   694 						'L_EXPLAIN'			=> $user->lang['ACP_' . $lang . '_EXPLAIN'],
       
   695 						'L_NO_PAK_OPTIONS'	=> $user->lang['NO_' . $lang . '_PAK'],
       
   696 						'L_CURRENT'			=> $user->lang['CURRENT_' . $lang],
       
   697 						'L_CURRENT_EXPLAIN'	=> $user->lang['CURRENT_' . $lang . '_EXPLAIN'],
       
   698 						'L_IMPORT_SUBMIT'	=> $user->lang['IMPORT_' . $lang],
       
   699 
       
   700 						'U_BACK'		=> $this->u_action,
       
   701 						'U_ACTION'		=> $this->u_action . '&amp;action=import',
       
   702 						)
       
   703 					);
       
   704 				}
       
   705 			break;
       
   706 
       
   707 			case 'export':
       
   708 
       
   709 				$this->page_title = 'EXPORT_' . $lang;
       
   710 				$this->tpl_name = 'message_body';
       
   711 
       
   712 				$template->assign_vars(array(
       
   713 					'MESSAGE_TITLE'		=> $user->lang['EXPORT_' . $lang],
       
   714 					'MESSAGE_TEXT'		=> sprintf($user->lang['EXPORT_' . $lang . '_EXPLAIN'], '<a href="' . $this->u_action . '&amp;action=send">', '</a>'),
       
   715 
       
   716 					'S_USER_NOTICE'		=> true,
       
   717 					)
       
   718 				);
       
   719 
       
   720 				return;
       
   721 
       
   722 			break;
       
   723 
       
   724 			case 'send':
       
   725 
       
   726 				$sql = "SELECT *
       
   727 					FROM $table
       
   728 					ORDER BY {$fields}_order";
       
   729 				$result = $db->sql_query($sql);
       
   730 
       
   731 				$pak = '';
       
   732 				while ($row = $db->sql_fetchrow($result))
       
   733 				{
       
   734 					$pak .= "'" . addslashes($row[$fields . '_url']) . "', ";
       
   735 					$pak .= "'" . addslashes($row[$fields . '_width']) . "', ";
       
   736 					$pak .= "'" . addslashes($row[$fields . '_height']) . "', ";
       
   737 					$pak .= "'" . addslashes($row['display_on_posting']) . "', ";
       
   738 
       
   739 					if ($mode == 'smilies')
       
   740 					{
       
   741 						$pak .= "'" . addslashes($row['emotion']) . "', ";
       
   742 						$pak .= "'" . addslashes($row['code']) . "', ";
       
   743 					}
       
   744 
       
   745 					$pak .= "\n";
       
   746 				}
       
   747 				$db->sql_freeresult($result);
       
   748 
       
   749 				if ($pak != '')
       
   750 				{
       
   751 					garbage_collection();
       
   752 
       
   753 					header('Pragma: public');
       
   754 
       
   755 					// Send out the Headers
       
   756 					header('Content-Type: text/x-delimtext; name="' . $mode . '.pak"');
       
   757 					header('Content-Disposition: inline; filename="' . $mode . '.pak"');
       
   758 					echo $pak;
       
   759 
       
   760 					flush();
       
   761 					exit;
       
   762 				}
       
   763 				else
       
   764 				{
       
   765 					trigger_error($user->lang['NO_' . strtoupper($fields) . '_EXPORT'] . adm_back_link($this->u_action), E_USER_WARNING);
       
   766 				}
       
   767 
       
   768 			break;
       
   769 
       
   770 			case 'delete':
       
   771 
       
   772 				if (confirm_box(true))
       
   773 				{
       
   774 					$sql = "DELETE FROM $table
       
   775 						WHERE {$fields}_id = $icon_id";
       
   776 					$db->sql_query($sql);
       
   777 
       
   778 					switch ($mode)
       
   779 					{
       
   780 						case 'smilies':
       
   781 						break;
       
   782 
       
   783 						case 'icons':
       
   784 							// Reset appropriate icon_ids
       
   785 							$db->sql_query('UPDATE ' . TOPICS_TABLE . "
       
   786 								SET icon_id = 0
       
   787 								WHERE icon_id = $icon_id");
       
   788 
       
   789 							$db->sql_query('UPDATE ' . POSTS_TABLE . "
       
   790 								SET icon_id = 0
       
   791 								WHERE icon_id = $icon_id");
       
   792 						break;
       
   793 					}
       
   794 
       
   795 					$notice = $user->lang[$lang . '_DELETED'];
       
   796 
       
   797 					$cache->destroy('_icons');
       
   798 					$cache->destroy('sql', $table);
       
   799 				}
       
   800 				else
       
   801 				{
       
   802 					confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
       
   803 						'i'			=> $id,
       
   804 						'mode'		=> $mode,
       
   805 						'id'		=> $icon_id,
       
   806 						'action'	=> 'delete',
       
   807 					)));
       
   808 				}
       
   809 
       
   810 			break;
       
   811 
       
   812 			case 'move_up':
       
   813 			case 'move_down':
       
   814 
       
   815 				// Get current order id...
       
   816 				$sql = "SELECT {$fields}_order as current_order
       
   817 					FROM $table
       
   818 					WHERE {$fields}_id = $icon_id";
       
   819 				$result = $db->sql_query($sql);
       
   820 				$current_order = (int) $db->sql_fetchfield('current_order');
       
   821 				$db->sql_freeresult($result);
       
   822 
       
   823 				if ($current_order == 0 && $action == 'move_up')
       
   824 				{
       
   825 					break;
       
   826 				}
       
   827 
       
   828 				// on move_down, switch position with next order_id...
       
   829 				// on move_up, switch position with previous order_id...
       
   830 				$switch_order_id = ($action == 'move_down') ? $current_order + 1 : $current_order - 1;
       
   831 
       
   832 				//
       
   833 				$sql = "UPDATE $table
       
   834 					SET {$fields}_order = $current_order
       
   835 					WHERE {$fields}_order = $switch_order_id
       
   836 						AND {$fields}_id <> $icon_id";
       
   837 				$db->sql_query($sql);
       
   838 
       
   839 				// Only update the other entry too if the previous entry got updated
       
   840 				if ($db->sql_affectedrows())
       
   841 				{
       
   842 					$sql = "UPDATE $table
       
   843 						SET {$fields}_order = $switch_order_id
       
   844 						WHERE {$fields}_order = $current_order
       
   845 							AND {$fields}_id = $icon_id";
       
   846 					$db->sql_query($sql);
       
   847 				}
       
   848 
       
   849 				$cache->destroy('_icons');
       
   850 				$cache->destroy('sql', $table);
       
   851 
       
   852 			break;
       
   853 		}
       
   854 
       
   855 		// By default, check that image_order is valid and fix it if necessary
       
   856 		$sql = "SELECT {$fields}_id AS order_id, {$fields}_order AS fields_order
       
   857 			FROM $table
       
   858 			ORDER BY display_on_posting DESC, {$fields}_order";
       
   859 		$result = $db->sql_query($sql);
       
   860 
       
   861 		if ($row = $db->sql_fetchrow($result))
       
   862 		{
       
   863 			$order = 0;
       
   864 			do
       
   865 			{
       
   866 				++$order;
       
   867 				if ($row['fields_order'] != $order)
       
   868 				{
       
   869 					$db->sql_query("UPDATE $table
       
   870 						SET {$fields}_order = $order
       
   871 						WHERE {$fields}_id = " . $row['order_id']);
       
   872 				}
       
   873 			}
       
   874 			while ($row = $db->sql_fetchrow($result));
       
   875 		}
       
   876 		$db->sql_freeresult($result);
       
   877 
       
   878 		$template->assign_vars(array(
       
   879 			'L_TITLE'			=> $user->lang['ACP_' . $lang],
       
   880 			'L_EXPLAIN'			=> $user->lang['ACP_' . $lang . '_EXPLAIN'],
       
   881 			'L_IMPORT'			=> $user->lang['IMPORT_' . $lang],
       
   882 			'L_EXPORT'			=> $user->lang['EXPORT_' . $lang],
       
   883 			'L_NOT_DISPLAYED'	=> $user->lang[$lang . '_NOT_DISPLAYED'],
       
   884 			'L_ICON_ADD'		=> $user->lang['ADD_' . $lang],
       
   885 			'L_ICON_EDIT'		=> $user->lang['EDIT_' . $lang],
       
   886 
       
   887 			'NOTICE'			=> $notice,
       
   888 			'COLSPAN'			=> ($mode == 'smilies') ? 5 : 3,
       
   889 
       
   890 			'S_SMILIES'			=> ($mode == 'smilies') ? true : false,
       
   891 
       
   892 			'U_ACTION'			=> $this->u_action,
       
   893 			'U_IMPORT'			=> $this->u_action . '&amp;action=import',
       
   894 			'U_EXPORT'			=> $this->u_action . '&amp;action=export',
       
   895 			)
       
   896 		);
       
   897 
       
   898 		$spacer = false;
       
   899 		$pagination_start = request_var('start', 0);
       
   900 
       
   901 		$item_count = $this->item_count($table);
       
   902 
       
   903 		$sql = "SELECT *
       
   904 			FROM $table
       
   905 			ORDER BY {$fields}_order ASC";
       
   906 		$result = $db->sql_query_limit($sql, $config['smilies_per_page'], $pagination_start);
       
   907 
       
   908 		while ($row = $db->sql_fetchrow($result))
       
   909 		{
       
   910 			$alt_text = ($mode == 'smilies') ? $row['code'] : '';
       
   911 
       
   912 			$template->assign_block_vars('items', array(
       
   913 				'S_SPACER'		=> (!$spacer && !$row['display_on_posting']) ? true : false,
       
   914 				'ALT_TEXT'		=> $alt_text,
       
   915 				'IMG_SRC'		=> $phpbb_root_path . $img_path . '/' . $row[$fields . '_url'],
       
   916 				'WIDTH'			=> $row[$fields . '_width'],
       
   917 				'HEIGHT'		=> $row[$fields . '_height'],
       
   918 				'CODE'			=> (isset($row['code'])) ? $row['code'] : '',
       
   919 				'EMOTION'		=> (isset($row['emotion'])) ? $row['emotion'] : '',
       
   920 				'U_EDIT'		=> $this->u_action . '&amp;action=edit&amp;id=' . $row[$fields . '_id'],
       
   921 				'U_DELETE'		=> $this->u_action . '&amp;action=delete&amp;id=' . $row[$fields . '_id'],
       
   922 				'U_MOVE_UP'		=> $this->u_action . '&amp;action=move_up&amp;id=' . $row[$fields . '_id'] . '&amp;start=' . $pagination_start,
       
   923 				'U_MOVE_DOWN'	=> $this->u_action . '&amp;action=move_down&amp;id=' . $row[$fields . '_id'] . '&amp;start=' . $pagination_start,
       
   924 			));
       
   925 
       
   926 			if (!$spacer && !$row['display_on_posting'])
       
   927 			{
       
   928 				$spacer = true;
       
   929 			}
       
   930 		}
       
   931 		$db->sql_freeresult($result);
       
   932 
       
   933 		$template->assign_var('PAGINATION',
       
   934 			generate_pagination($this->u_action, $item_count, $config['smilies_per_page'], $pagination_start, true)
       
   935 		);
       
   936 	}
       
   937 
       
   938 	/**
       
   939 	 * Returns the count of smilies or icons in the database
       
   940 	 *
       
   941 	 * @param string $table The table of items to count.
       
   942 	 * @return int number of items
       
   943 	 */
       
   944 	/* private */ function item_count($table)
       
   945 	{
       
   946 		global $db;
       
   947 
       
   948 		$sql = "SELECT COUNT(*) AS item_count
       
   949 			FROM $table";
       
   950 		$result = $db->sql_query($sql);
       
   951 		$item_count = (int) $db->sql_fetchfield('item_count');
       
   952 		$db->sql_freeresult($result);
       
   953 
       
   954 		return $item_count;
       
   955 	}
       
   956 }
       
   957 
       
   958 ?>