Blame Extras/phpBB/3.0.4/includes/acp/acp_icons.php

4c79b5
4c79b5
/**
4c79b5
*
4c79b5
* @package acp
4c79b5
* @version $Id: acp_icons.php 8974 2008-10-06 13:23:41Z acydburn $
4c79b5
* @copyright (c) 2005 phpBB Group
4c79b5
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
4c79b5
*
4c79b5
*/
4c79b5
4c79b5
/**
4c79b5
* @ignore
4c79b5
*/
4c79b5
if (!defined('IN_PHPBB'))
4c79b5
{
4c79b5
	exit;
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* @todo [smilies] check regular expressions for special char replacements (stored specialchared in db)
4c79b5
* @package acp
4c79b5
*/
4c79b5
class acp_icons
4c79b5
{
4c79b5
	var $u_action;
4c79b5
4c79b5
	function main($id, $mode)
4c79b5
	{
4c79b5
		global $db, $user, $auth, $template, $cache;
4c79b5
		global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
4c79b5
4c79b5
		$user->add_lang('acp/posting');
4c79b5
4c79b5
		// Set up general vars
4c79b5
		$action = request_var('action', '');
4c79b5
		$action = (isset($_POST['add'])) ? 'add' : $action;
4c79b5
		$action = (isset($_POST['edit'])) ? 'edit' : $action;
4c79b5
		$action = (isset($_POST['import'])) ? 'import' : $action;
4c79b5
		$icon_id = request_var('id', 0);
4c79b5
4c79b5
		$mode = ($mode == 'smilies') ? 'smilies' : 'icons';
4c79b5
4c79b5
		$this->tpl_name = 'acp_icons';
4c79b5
4c79b5
		// What are we working on?
4c79b5
		switch ($mode)
4c79b5
		{
4c79b5
			case 'smilies':
4c79b5
				$table = SMILIES_TABLE;
4c79b5
				$lang = 'SMILIES';
4c79b5
				$fields = 'smiley';
4c79b5
				$img_path = $config['smilies_path'];
4c79b5
			break;
4c79b5
4c79b5
			case 'icons':
4c79b5
				$table = ICONS_TABLE;
4c79b5
				$lang = 'ICONS';
4c79b5
				$fields = 'icons';
4c79b5
				$img_path = $config['icons_path'];
4c79b5
			break;
4c79b5
		}
4c79b5
4c79b5
		$this->page_title = 'ACP_' . $lang;
4c79b5
4c79b5
		// Clear some arrays
4c79b5
		$_images = $_paks = array();
4c79b5
		$notice = '';
4c79b5
4c79b5
		// Grab file list of paks and images
4c79b5
		if ($action == 'edit' || $action == 'add' || $action == 'import')
4c79b5
		{
4c79b5
			$imglist = filelist($phpbb_root_path . $img_path, '');
4c79b5
4c79b5
			foreach ($imglist as $path => $img_ary)
4c79b5
			{
4c79b5
				if (empty($img_ary))
4c79b5
				{
4c79b5
					continue;
4c79b5
				}
4c79b5
4c79b5
				asort($img_ary, SORT_STRING);
4c79b5
4c79b5
				foreach ($img_ary as $img)
4c79b5
				{
4c79b5
					$img_size = getimagesize($phpbb_root_path . $img_path . '/' . $path . $img);
4c79b5
4c79b5
					if (!$img_size[0] || !$img_size[1] || strlen($img) > 255)
4c79b5
					{
4c79b5
						continue;
4c79b5
					}
4c79b5
4c79b5
					$_images[$path . $img]['file'] = $path . $img;
4c79b5
					$_images[$path . $img]['width'] = $img_size[0];
4c79b5
					$_images[$path . $img]['height'] = $img_size[1];
4c79b5
				}
4c79b5
			}
4c79b5
			unset($imglist);
4c79b5
4c79b5
			if ($dir = @opendir($phpbb_root_path . $img_path))
4c79b5
			{
4c79b5
				while (($file = readdir($dir)) !== false)
4c79b5
				{
4c79b5
					if (is_file($phpbb_root_path . $img_path . '/' . $file) && preg_match('#\.pak$#i', $file))
4c79b5
					{
4c79b5
						$_paks[] = $file;
4c79b5
					}
4c79b5
				}
4c79b5
				closedir($dir);
4c79b5
4c79b5
				if (!empty($_paks))
4c79b5
				{
4c79b5
					asort($_paks, SORT_STRING);
4c79b5
				}
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		// What shall we do today? Oops, I believe that's trademarked ...
4c79b5
		switch ($action)
4c79b5
		{
4c79b5
			case 'edit':
4c79b5
				unset($_images);
4c79b5
				$_images = array();
4c79b5
4c79b5
			// no break;
4c79b5
4c79b5
			case 'add':
4c79b5
4c79b5
				$smilies = $default_row = array();
4c79b5
				$smiley_options = $order_list = $add_order_list = '';
4c79b5
4c79b5
				if ($action == 'add' && $mode == 'smilies')
4c79b5
				{
4c79b5
					$sql = 'SELECT *
4c79b5
						FROM ' . SMILIES_TABLE . '
4c79b5
						ORDER BY smiley_order';
4c79b5
					$result = $db->sql_query($sql);
4c79b5
4c79b5
					while ($row = $db->sql_fetchrow($result))
4c79b5
					{
4c79b5
						if (empty($smilies[$row['smiley_url']]))
4c79b5
						{
4c79b5
							$smilies[$row['smiley_url']] = $row;
4c79b5
						}
4c79b5
					}
4c79b5
					$db->sql_freeresult($result);
4c79b5
4c79b5
					if (sizeof($smilies))
4c79b5
					{
4c79b5
						foreach ($smilies as $row)
4c79b5
						{
4c79b5
							$selected = false;
4c79b5
4c79b5
							if (!$smiley_options)
4c79b5
							{
4c79b5
								$selected = true;
4c79b5
								$default_row = $row;
4c79b5
							}
4c79b5
							$smiley_options .= '<option value="' . $row['smiley_url'] . '"' . (($selected) ? ' selected="selected"' : '') . '>' . $row['smiley_url'] . '</option>';
4c79b5
4c79b5
							$template->assign_block_vars('smile', array(
4c79b5
								'SMILEY_URL'	=> addslashes($row['smiley_url']),
4c79b5
								'CODE'			=> addslashes($row['code']),
4c79b5
								'EMOTION'		=> addslashes($row['emotion']),
4c79b5
								'WIDTH'			=> $row['smiley_width'],
4c79b5
								'HEIGHT'		=> $row['smiley_height'],
4c79b5
								'ORDER'			=> $row['smiley_order'] + 1,
4c79b5
							));
4c79b5
						}
4c79b5
					}
4c79b5
				}
4c79b5
				
4c79b5
				$sql = "SELECT *
4c79b5
					FROM $table
4c79b5
					ORDER BY {$fields}_order " . (($icon_id || $action == 'add') ? 'DESC' : 'ASC');
4c79b5
				$result = $db->sql_query($sql);
4c79b5
				
4c79b5
				$data = array();
4c79b5
				$after = false;
4c79b5
				$display = 0;
4c79b5
				$order_lists = array('', '');
4c79b5
				$add_order_lists = array('', '');
4c79b5
				$display_count = 0;
4c79b5
				
4c79b5
				while ($row = $db->sql_fetchrow($result))
4c79b5
				{
4c79b5
					if ($action == 'add')
4c79b5
					{
4c79b5
						unset($_images[$row[$fields . '_url']]);
4c79b5
					}
4c79b5
4c79b5
4c79b5
					if ($row[$fields . '_id'] == $icon_id)
4c79b5
					{
4c79b5
						$after = true;
4c79b5
						$display = $row['display_on_posting'];
4c79b5
						$data[$row[$fields . '_url']] = $row;
4c79b5
					}
4c79b5
					else
4c79b5
					{
4c79b5
						if ($action == 'edit' && !$icon_id)
4c79b5
						{
4c79b5
							$data[$row[$fields . '_url']] = $row;
4c79b5
						}
4c79b5
4c79b5
						$selected = '';
4c79b5
						if (!empty($after))
4c79b5
						{
4c79b5
							$selected = ' selected="selected"';
4c79b5
							$after = false;
4c79b5
						}
4c79b5
						if ($row['display_on_posting'])
4c79b5
						{
4c79b5
							$display_count++;
4c79b5
						}
4c79b5
						$after_txt = ($mode == 'smilies') ? $row['code'] : $row['icons_url'];
4c79b5
						$order_lists[$row['display_on_posting']] = '<option value="' . ($row[$fields . '_order'] + 1) . '"' . $selected . '>' . sprintf($user->lang['AFTER_' . $lang], ' -> ' . $after_txt) . '</option>' . $order_lists[$row['display_on_posting']];
4c79b5
4c79b5
						if (!empty($default_row))
4c79b5
						{
4c79b5
							$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], ' -> ' . $after_txt) . '</option>' . $add_order_lists[$row['display_on_posting']];
4c79b5
						}
4c79b5
					}
4c79b5
				}
4c79b5
				$db->sql_freeresult($result);
4c79b5
4c79b5
				$order_list = '<option value="1"' . ((!isset($after)) ? ' selected="selected"' : '') . '>' . $user->lang['FIRST'] . '</option>';
4c79b5
				$add_order_list = '<option value="1">' . $user->lang['FIRST'] . '</option>';
4c79b5
4c79b5
				if ($action == 'add')
4c79b5
				{
4c79b5
					$data = $_images;
4c79b5
				}
4c79b5
4c79b5
				$colspan = (($mode == 'smilies') ? '7' : '5');
4c79b5
				$colspan += ($icon_id) ? 1 : 0;
4c79b5
				$colspan += ($action == 'add') ? 2 : 0;
4c79b5
				
4c79b5
				$template->assign_vars(array(
4c79b5
					'S_EDIT'		=> true,
4c79b5
					'S_SMILIES'		=> ($mode == 'smilies') ? true : false,
4c79b5
					'S_ADD'			=> ($action == 'add') ? true : false,
4c79b5
					
4c79b5
					'S_ORDER_LIST_DISPLAY'		=> $order_list . $order_lists[1],
4c79b5
					'S_ORDER_LIST_UNDISPLAY'	=> $order_list . $order_lists[0],
4c79b5
					'S_ORDER_LIST_DISPLAY_COUNT'	=> $display_count + 1,
4c79b5
4c79b5
					'L_TITLE'		=> $user->lang['ACP_' . $lang],
4c79b5
					'L_EXPLAIN'		=> $user->lang['ACP_' . $lang . '_EXPLAIN'],
4c79b5
					'L_CONFIG'		=> $user->lang[$lang . '_CONFIG'],
4c79b5
					'L_URL'			=> $user->lang[$lang . '_URL'],
4c79b5
					'L_LOCATION'	=> $user->lang[$lang . '_LOCATION'],
4c79b5
					'L_WIDTH'		=> $user->lang[$lang . '_WIDTH'],
4c79b5
					'L_HEIGHT'		=> $user->lang[$lang . '_HEIGHT'],
4c79b5
					'L_ORDER'		=> $user->lang[$lang . '_ORDER'],
4c79b5
					'L_NO_ICONS'	=> $user->lang['NO_' . $lang . '_' . strtoupper($action)],
4c79b5
4c79b5
					'COLSPAN'		=> $colspan,
4c79b5
					'ID'			=> $icon_id,
4c79b5
4c79b5
					'U_BACK'		=> $this->u_action,
4c79b5
					'U_ACTION'		=> $this->u_action . '&action=' . (($action == 'add') ? 'create' : 'modify'),
4c79b5
				));
4c79b5
4c79b5
				foreach ($data as $img => $img_row)
4c79b5
				{
4c79b5
					$template->assign_block_vars('items', array(
4c79b5
						'IMG'		=> $img,
4c79b5
						'A_IMG'		=> addslashes($img),
4c79b5
						'IMG_SRC'	=> $phpbb_root_path . $img_path . '/' . $img,
4c79b5
4c79b5
						'CODE'		=> ($mode == 'smilies' && isset($img_row['code'])) ? $img_row['code'] : '',
4c79b5
						'EMOTION'	=> ($mode == 'smilies' && isset($img_row['emotion'])) ? $img_row['emotion'] : '',
4c79b5
4c79b5
						'S_ID'				=> (isset($img_row[$fields . '_id'])) ? true : false,
4c79b5
						'ID'				=> (isset($img_row[$fields . '_id'])) ? $img_row[$fields . '_id'] : 0,
4c79b5
						'WIDTH'				=> (!empty($img_row[$fields .'_width'])) ? $img_row[$fields .'_width'] : $img_row['width'],
4c79b5
						'HEIGHT'			=> (!empty($img_row[$fields .'_height'])) ? $img_row[$fields .'_height'] : $img_row['height'],
4c79b5
						'POSTING_CHECKED'	=> (!empty($img_row['display_on_posting']) || $action == 'add') ? ' checked="checked"' : '',
4c79b5
					));
4c79b5
				}
4c79b5
4c79b5
				// Ok, another row for adding an addition code for a pre-existing image...
4c79b5
				if ($action == 'add' && $mode == 'smilies' && sizeof($smilies))
4c79b5
				{
4c79b5
					$template->assign_vars(array(
4c79b5
						'S_ADD_CODE'		=> true,
4c79b5
4c79b5
						'S_IMG_OPTIONS'		=> $smiley_options,
4c79b5
						
4c79b5
						'S_ADD_ORDER_LIST_DISPLAY'		=> $add_order_list . $add_order_lists[1],
4c79b5
						'S_ADD_ORDER_LIST_UNDISPLAY'	=> $add_order_list . $add_order_lists[0],
4c79b5
						
4c79b5
						'IMG_SRC'			=> $phpbb_root_path . $img_path . '/' . $default_row['smiley_url'],
4c79b5
						'IMG_PATH'			=> $img_path,
4c79b5
						'PHPBB_ROOT_PATH'	=> $phpbb_root_path,
4c79b5
4c79b5
						'CODE'				=> $default_row['code'],
4c79b5
						'EMOTION'			=> $default_row['emotion'],
4c79b5
4c79b5
						'WIDTH'				=> $default_row['smiley_width'],
4c79b5
						'HEIGHT'			=> $default_row['smiley_height'],
4c79b5
					));
4c79b5
				}
4c79b5
4c79b5
				return;
4c79b5
	
4c79b5
			break;
4c79b5
4c79b5
			case 'create':
4c79b5
			case 'modify':
4c79b5
4c79b5
				// Get items to create/modify
4c79b5
				$images = (isset($_POST['image'])) ? array_keys(request_var('image', array('' => 0))) : array();
4c79b5
				
4c79b5
				// Now really get the items
4c79b5
				$image_id		= (isset($_POST['id'])) ? request_var('id', array('' => 0)) : array();
4c79b5
				$image_order	= (isset($_POST['order'])) ? request_var('order', array('' => 0)) : array();
4c79b5
				$image_width	= (isset($_POST['width'])) ? request_var('width', array('' => 0)) : array();
4c79b5
				$image_height	= (isset($_POST['height'])) ? request_var('height', array('' => 0)) : array();
4c79b5
				$image_add		= (isset($_POST['add_img'])) ? request_var('add_img', array('' => 0)) : array();
4c79b5
				$image_emotion	= utf8_normalize_nfc(request_var('emotion', array('' => ''), true));
4c79b5
				$image_code		= utf8_normalize_nfc(request_var('code', array('' => ''), true));
4c79b5
				$image_display_on_posting = (isset($_POST['display_on_posting'])) ? request_var('display_on_posting', array('' => 0)) : array();
4c79b5
4c79b5
				// Ok, add the relevant bits if we are adding new codes to existing emoticons...
4c79b5
				if (!empty($_POST['add_additional_code']))
4c79b5
				{
4c79b5
					$add_image			= request_var('add_image', '');
4c79b5
					$add_code			= utf8_normalize_nfc(request_var('add_code', '', true));
4c79b5
					$add_emotion		= utf8_normalize_nfc(request_var('add_emotion', '', true));
4c79b5
4c79b5
					if ($add_image && $add_emotion && $add_code)
4c79b5
					{
4c79b5
						$images[] = $add_image;
4c79b5
						$image_add[$add_image] = true;
4c79b5
4c79b5
						$image_code[$add_image] = $add_code;
4c79b5
						$image_emotion[$add_image] = $add_emotion;
4c79b5
						$image_width[$add_image] = request_var('add_width', 0);
4c79b5
						$image_height[$add_image] = request_var('add_height', 0);
4c79b5
4c79b5
						if (!empty($_POST['add_display_on_posting']))
4c79b5
						{
4c79b5
							$image_display_on_posting[$add_image] = 1;
4c79b5
						}
4c79b5
4c79b5
						$image_order[$add_image] = request_var('add_order', 0);
4c79b5
					}
4c79b5
				}
4c79b5
4c79b5
				$icons_updated = 0;
4c79b5
				$errors = array();
4c79b5
				foreach ($images as $image)
4c79b5
				{
4c79b5
					if ($mode == 'smilies' && ($image_emotion[$image] == '' || $image_code[$image] == ''))
4c79b5
					{
4c79b5
						$errors[$image] = 'SMILIE_NO_' . (($image_emotion[$image] == '') ? 'EMOTION' : 'CODE');
4c79b5
					}
4c79b5
					else if ($action == 'create' && !isset($image_add[$image]))
4c79b5
					{
4c79b5
						// skip images where add wasn't checked
4c79b5
					}
4c79b5
					else
4c79b5
					{
4c79b5
						if ($image_width[$image] == 0 || $image_height[$image] == 0)
4c79b5
						{
4c79b5
							$img_size = getimagesize($phpbb_root_path . $img_path . '/' . $image);
4c79b5
							$image_width[$image] = $img_size[0];
4c79b5
							$image_height[$image] = $img_size[1];
4c79b5
						}
4c79b5
4c79b5
						$img_sql = array(
4c79b5
							$fields . '_url'		=> $image,
4c79b5
							$fields . '_width'		=> $image_width[$image],
4c79b5
							$fields . '_height'		=> $image_height[$image],
4c79b5
							'display_on_posting'	=> (isset($image_display_on_posting[$image])) ? 1 : 0,
4c79b5
						);
4c79b5
4c79b5
						if ($mode == 'smilies')
4c79b5
						{
4c79b5
							$img_sql = array_merge($img_sql, array(
4c79b5
								'emotion'	=> $image_emotion[$image],
4c79b5
								'code'		=> $image_code[$image])
4c79b5
							);
4c79b5
						}
4c79b5
4c79b5
						// Image_order holds the 'new' order value
4c79b5
						if (!empty($image_order[$image]))
4c79b5
						{
4c79b5
							$img_sql = array_merge($img_sql, array(
4c79b5
								$fields . '_order'	=>	$image_order[$image])
4c79b5
							);
4c79b5
4c79b5
							// Since we always add 'after' an item, we just need to increase all following + the current by one
4c79b5
							$sql = "UPDATE $table
4c79b5
								SET {$fields}_order = {$fields}_order + 1
4c79b5
								WHERE {$fields}_order >= {$image_order[$image]}";
4c79b5
							$db->sql_query($sql);
4c79b5
4c79b5
							// If we adjust the order, we need to adjust all other orders too - they became inaccurate...
4c79b5
							foreach ($image_order as $_image => $_order)
4c79b5
							{
4c79b5
								if ($_image == $image)
4c79b5
								{
4c79b5
									continue;
4c79b5
								}
4c79b5
4c79b5
								if ($_order >= $image_order[$image])
4c79b5
								{
4c79b5
									$image_order[$_image]++;
4c79b5
								}
4c79b5
							}
4c79b5
						}
4c79b5
4c79b5
						if ($action == 'modify'  && !empty($image_id[$image]))
4c79b5
						{
4c79b5
							$sql = "UPDATE $table
4c79b5
								SET " . $db->sql_build_array('UPDATE', $img_sql) . "
4c79b5
								WHERE {$fields}_id = " . $image_id[$image];
4c79b5
							$db->sql_query($sql);
4c79b5
							$icons_updated++;
4c79b5
						}
4c79b5
						else if ($action !== 'modify')
4c79b5
						{
4c79b5
							$sql = "INSERT INTO $table " . $db->sql_build_array('INSERT', $img_sql);
4c79b5
							$db->sql_query($sql);
4c79b5
							$icons_updated++;
4c79b5
						}
4c79b5
						
4c79b5
 					}
4c79b5
				}
4c79b5
				
4c79b5
				$cache->destroy('_icons');
4c79b5
				$cache->destroy('sql', $table);
4c79b5
				
4c79b5
				$level = E_USER_NOTICE;
4c79b5
				switch ($icons_updated)
4c79b5
				{
4c79b5
					case 0:
4c79b5
						$suc_lang = "{$lang}_NONE";
4c79b5
						$level = E_USER_WARNING;
4c79b5
						break;
4c79b5
						
4c79b5
					case 1:
4c79b5
						$suc_lang = "{$lang}_ONE";
4c79b5
						break;
4c79b5
						
4c79b5
					default:
4c79b5
						$suc_lang = $lang;
4c79b5
				}
4c79b5
				$errormsgs = '';
4c79b5
				foreach ($errors as $img => $error)
4c79b5
				{
4c79b5
					$errormsgs .= '
' . sprintf($user->lang[$error], $img);
4c79b5
				}
4c79b5
				if ($action == 'modify')
4c79b5
				{
4c79b5
					trigger_error($user->lang[$suc_lang . '_EDITED'] . $errormsgs . adm_back_link($this->u_action), $level);
4c79b5
				}
4c79b5
				else
4c79b5
				{
4c79b5
					trigger_error($user->lang[$suc_lang . '_ADDED'] . $errormsgs . adm_back_link($this->u_action), $level);
4c79b5
				}
4c79b5
4c79b5
			break;
4c79b5
4c79b5
			case 'import':
4c79b5
4c79b5
				$pak = request_var('pak', '');
4c79b5
				$current = request_var('current', '');
4c79b5
4c79b5
				if ($pak != '')
4c79b5
				{
4c79b5
					$order = 0;
4c79b5
4c79b5
					if (!($pak_ary = @file($phpbb_root_path . $img_path . '/' . $pak)))
4c79b5
					{
4c79b5
						trigger_error($user->lang['PAK_FILE_NOT_READABLE'] . adm_back_link($this->u_action), E_USER_WARNING);
4c79b5
					}
4c79b5
4c79b5
					// Make sure the pak_ary is valid
4c79b5
					foreach ($pak_ary as $pak_entry)
4c79b5
					{
4c79b5
						if (preg_match_all("#'(.*?)', ?#", $pak_entry, $data))
4c79b5
						{
4c79b5
							if ((sizeof($data[1]) != 4 && $mode == 'icons') ||
4c79b5
								((sizeof($data[1]) != 6 || (empty($data[1][4]) || empty($data[1][5]))) && $mode == 'smilies' ))
4c79b5
							{
4c79b5
								trigger_error($user->lang['WRONG_PAK_TYPE'] . adm_back_link($this->u_action), E_USER_WARNING);
4c79b5
							}
4c79b5
						}
4c79b5
						else
4c79b5
						{
4c79b5
							trigger_error($user->lang['WRONG_PAK_TYPE'] . adm_back_link($this->u_action), E_USER_WARNING);
4c79b5
						}
4c79b5
					}
4c79b5
4c79b5
4c79b5
					// The user has already selected a smilies_pak file
4c79b5
					if ($current == 'delete')
4c79b5
					{
4c79b5
						switch ($db->sql_layer)
4c79b5
						{
4c79b5
							case 'sqlite':
4c79b5
							case 'firebird':
4c79b5
								$db->sql_query('DELETE FROM ' . $table);
4c79b5
							break;
4c79b5
4c79b5
							default:
4c79b5
								$db->sql_query('TRUNCATE TABLE ' . $table);
4c79b5
							break;
4c79b5
						}
4c79b5
4c79b5
						switch ($mode)
4c79b5
						{
4c79b5
							case 'smilies':
4c79b5
							break;
4c79b5
4c79b5
							case 'icons':
4c79b5
								// Reset all icon_ids
4c79b5
								$db->sql_query('UPDATE ' . TOPICS_TABLE . ' SET icon_id = 0');
4c79b5
								$db->sql_query('UPDATE ' . POSTS_TABLE . ' SET icon_id = 0');
4c79b5
							break;
4c79b5
						}
4c79b5
					}
4c79b5
					else
4c79b5
					{
4c79b5
						$cur_img = array();
4c79b5
4c79b5
						$field_sql = ($mode == 'smilies') ? 'code' : 'icons_url';
4c79b5
4c79b5
						$sql = "SELECT $field_sql
4c79b5
							FROM $table";
4c79b5
						$result = $db->sql_query($sql);
4c79b5
4c79b5
						while ($row = $db->sql_fetchrow($result))
4c79b5
						{
4c79b5
							++$order;
4c79b5
							$cur_img[$row[$field_sql]] = 1;
4c79b5
						}
4c79b5
						$db->sql_freeresult($result);
4c79b5
					}
4c79b5
4c79b5
					foreach ($pak_ary as $pak_entry)
4c79b5
					{
4c79b5
						$data = array();
4c79b5
						if (preg_match_all("#'(.*?)', ?#", $pak_entry, $data))
4c79b5
						{
4c79b5
							if ((sizeof($data[1]) != 4 && $mode == 'icons') ||
4c79b5
								(sizeof($data[1]) != 6 && $mode == 'smilies'))
4c79b5
							{
4c79b5
								trigger_error($user->lang['WRONG_PAK_TYPE'] . adm_back_link($this->u_action), E_USER_WARNING);
4c79b5
							}
4c79b5
4c79b5
							// Stripslash here because it got addslashed before... (on export)
4c79b5
							$img = stripslashes($data[1][0]);
4c79b5
							$width = stripslashes($data[1][1]);
4c79b5
							$height = stripslashes($data[1][2]);
4c79b5
							$display_on_posting = stripslashes($data[1][3]);
4c79b5
4c79b5
							if (isset($data[1][4]) && isset($data[1][5]))
4c79b5
							{
4c79b5
								$emotion = stripslashes($data[1][4]);
4c79b5
								$code = stripslashes($data[1][5]);
4c79b5
							}
4c79b5
4c79b5
							if ($current == 'replace' &&
4c79b5
								(($mode == 'smilies' && !empty($cur_img[$code])) ||
4c79b5
								($mode == 'icons' && !empty($cur_img[$img]))))
4c79b5
							{
4c79b5
								$replace_sql = ($mode == 'smilies') ? $code : $img;
4c79b5
								$sql = array(
4c79b5
									$fields . '_url'		=> $img,
4c79b5
									$fields . '_height'		=> (int) $height,
4c79b5
									$fields . '_width'		=> (int) $width,
4c79b5
									'display_on_posting'	=> (int) $display_on_posting,
4c79b5
								);
4c79b5
4c79b5
								if ($mode == 'smilies')
4c79b5
								{
4c79b5
									$sql = array_merge($sql, array(
4c79b5
										'emotion'				=> $emotion,
4c79b5
									));
4c79b5
								}
4c79b5
4c79b5
								$sql = "UPDATE $table SET " . $db->sql_build_array('UPDATE', $sql) . "
4c79b5
									WHERE $field_sql = '" . $db->sql_escape($replace_sql) . "'";
4c79b5
								$db->sql_query($sql);
4c79b5
							}
4c79b5
							else
4c79b5
							{
4c79b5
								++$order;
4c79b5
4c79b5
								$sql = array(
4c79b5
									$fields . '_url'	=> $img,
4c79b5
									$fields . '_height'	=> (int) $height,
4c79b5
									$fields . '_width'	=> (int) $width,
4c79b5
									$fields . '_order'	=> (int) $order,
4c79b5
									'display_on_posting'=> (int) $display_on_posting,
4c79b5
								);
4c79b5
4c79b5
								if ($mode == 'smilies')
4c79b5
								{
4c79b5
									$sql = array_merge($sql, array(
4c79b5
										'code'				=> $code,
4c79b5
										'emotion'			=> $emotion,
4c79b5
									));
4c79b5
								}
4c79b5
								$db->sql_query("INSERT INTO $table " . $db->sql_build_array('INSERT', $sql));
4c79b5
							}
4c79b5
						}
4c79b5
					}
4c79b5
4c79b5
					$cache->destroy('_icons');
4c79b5
					$cache->destroy('sql', $table);
4c79b5
4c79b5
					trigger_error($user->lang[$lang . '_IMPORT_SUCCESS'] . adm_back_link($this->u_action));
4c79b5
				}
4c79b5
				else
4c79b5
				{
4c79b5
					$pak_options = '';
4c79b5
4c79b5
					foreach ($_paks as $pak)
4c79b5
					{
4c79b5
						$pak_options .= '<option value="' . $pak . '">' . htmlspecialchars($pak) . '</option>';
4c79b5
					}
4c79b5
4c79b5
					$template->assign_vars(array(
4c79b5
						'S_CHOOSE_PAK'		=> true,
4c79b5
						'S_PAK_OPTIONS'		=> $pak_options,
4c79b5
4c79b5
						'L_TITLE'			=> $user->lang['ACP_' . $lang],
4c79b5
						'L_EXPLAIN'			=> $user->lang['ACP_' . $lang . '_EXPLAIN'],
4c79b5
						'L_NO_PAK_OPTIONS'	=> $user->lang['NO_' . $lang . '_PAK'],
4c79b5
						'L_CURRENT'			=> $user->lang['CURRENT_' . $lang],
4c79b5
						'L_CURRENT_EXPLAIN'	=> $user->lang['CURRENT_' . $lang . '_EXPLAIN'],
4c79b5
						'L_IMPORT_SUBMIT'	=> $user->lang['IMPORT_' . $lang],
4c79b5
4c79b5
						'U_BACK'		=> $this->u_action,
4c79b5
						'U_ACTION'		=> $this->u_action . '&action=import',
4c79b5
						)
4c79b5
					);
4c79b5
				}
4c79b5
			break;
4c79b5
4c79b5
			case 'export':
4c79b5
4c79b5
				$this->page_title = 'EXPORT_' . $lang;
4c79b5
				$this->tpl_name = 'message_body';
4c79b5
4c79b5
				$template->assign_vars(array(
4c79b5
					'MESSAGE_TITLE'		=> $user->lang['EXPORT_' . $lang],
4c79b5
					'MESSAGE_TEXT'		=> sprintf($user->lang['EXPORT_' . $lang . '_EXPLAIN'], '', ''),
4c79b5
4c79b5
					'S_USER_NOTICE'		=> true,
4c79b5
					)
4c79b5
				);
4c79b5
4c79b5
				return;
4c79b5
4c79b5
			break;
4c79b5
4c79b5
			case 'send':
4c79b5
4c79b5
				$sql = "SELECT *
4c79b5
					FROM $table
4c79b5
					ORDER BY {$fields}_order";
4c79b5
				$result = $db->sql_query($sql);
4c79b5
4c79b5
				$pak = '';
4c79b5
				while ($row = $db->sql_fetchrow($result))
4c79b5
				{
4c79b5
					$pak .= "'" . addslashes($row[$fields . '_url']) . "', ";
4c79b5
					$pak .= "'" . addslashes($row[$fields . '_width']) . "', ";
4c79b5
					$pak .= "'" . addslashes($row[$fields . '_height']) . "', ";
4c79b5
					$pak .= "'" . addslashes($row['display_on_posting']) . "', ";
4c79b5
4c79b5
					if ($mode == 'smilies')
4c79b5
					{
4c79b5
						$pak .= "'" . addslashes($row['emotion']) . "', ";
4c79b5
						$pak .= "'" . addslashes($row['code']) . "', ";
4c79b5
					}
4c79b5
4c79b5
					$pak .= "\n";
4c79b5
				}
4c79b5
				$db->sql_freeresult($result);
4c79b5
4c79b5
				if ($pak != '')
4c79b5
				{
4c79b5
					garbage_collection();
4c79b5
4c79b5
					header('Pragma: public');
4c79b5
4c79b5
					// Send out the Headers
4c79b5
					header('Content-Type: text/x-delimtext; name="' . $mode . '.pak"');
4c79b5
					header('Content-Disposition: inline; filename="' . $mode . '.pak"');
4c79b5
					echo $pak;
4c79b5
4c79b5
					flush();
4c79b5
					exit;
4c79b5
				}
4c79b5
				else
4c79b5
				{
4c79b5
					trigger_error($user->lang['NO_' . strtoupper($fields) . '_EXPORT'] . adm_back_link($this->u_action), E_USER_WARNING);
4c79b5
				}
4c79b5
4c79b5
			break;
4c79b5
4c79b5
			case 'delete':
4c79b5
4c79b5
				if (confirm_box(true))
4c79b5
				{
4c79b5
					$sql = "DELETE FROM $table
4c79b5
						WHERE {$fields}_id = $icon_id";
4c79b5
					$db->sql_query($sql);
4c79b5
4c79b5
					switch ($mode)
4c79b5
					{
4c79b5
						case 'smilies':
4c79b5
						break;
4c79b5
4c79b5
						case 'icons':
4c79b5
							// Reset appropriate icon_ids
4c79b5
							$db->sql_query('UPDATE ' . TOPICS_TABLE . "
4c79b5
								SET icon_id = 0
4c79b5
								WHERE icon_id = $icon_id");
4c79b5
4c79b5
							$db->sql_query('UPDATE ' . POSTS_TABLE . "
4c79b5
								SET icon_id = 0
4c79b5
								WHERE icon_id = $icon_id");
4c79b5
						break;
4c79b5
					}
4c79b5
4c79b5
					$notice = $user->lang[$lang . '_DELETED'];
4c79b5
4c79b5
					$cache->destroy('_icons');
4c79b5
					$cache->destroy('sql', $table);
4c79b5
				}
4c79b5
				else
4c79b5
				{
4c79b5
					confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
4c79b5
						'i'			=> $id,
4c79b5
						'mode'		=> $mode,
4c79b5
						'id'		=> $icon_id,
4c79b5
						'action'	=> 'delete',
4c79b5
					)));
4c79b5
				}
4c79b5
4c79b5
			break;
4c79b5
4c79b5
			case 'move_up':
4c79b5
			case 'move_down':
4c79b5
4c79b5
				// Get current order id...
4c79b5
				$sql = "SELECT {$fields}_order as current_order
4c79b5
					FROM $table
4c79b5
					WHERE {$fields}_id = $icon_id";
4c79b5
				$result = $db->sql_query($sql);
4c79b5
				$current_order = (int) $db->sql_fetchfield('current_order');
4c79b5
				$db->sql_freeresult($result);
4c79b5
4c79b5
				if ($current_order == 0 && $action == 'move_up')
4c79b5
				{
4c79b5
					break;
4c79b5
				}
4c79b5
4c79b5
				// on move_down, switch position with next order_id...
4c79b5
				// on move_up, switch position with previous order_id...
4c79b5
				$switch_order_id = ($action == 'move_down') ? $current_order + 1 : $current_order - 1;
4c79b5
4c79b5
				//
4c79b5
				$sql = "UPDATE $table
4c79b5
					SET {$fields}_order = $current_order
4c79b5
					WHERE {$fields}_order = $switch_order_id
4c79b5
						AND {$fields}_id <> $icon_id";
4c79b5
				$db->sql_query($sql);
4c79b5
4c79b5
				// Only update the other entry too if the previous entry got updated
4c79b5
				if ($db->sql_affectedrows())
4c79b5
				{
4c79b5
					$sql = "UPDATE $table
4c79b5
						SET {$fields}_order = $switch_order_id
4c79b5
						WHERE {$fields}_order = $current_order
4c79b5
							AND {$fields}_id = $icon_id";
4c79b5
					$db->sql_query($sql);
4c79b5
				}
4c79b5
4c79b5
				$cache->destroy('_icons');
4c79b5
				$cache->destroy('sql', $table);
4c79b5
4c79b5
			break;
4c79b5
		}
4c79b5
4c79b5
		// By default, check that image_order is valid and fix it if necessary
4c79b5
		$sql = "SELECT {$fields}_id AS order_id, {$fields}_order AS fields_order
4c79b5
			FROM $table
4c79b5
			ORDER BY display_on_posting DESC, {$fields}_order";
4c79b5
		$result = $db->sql_query($sql);
4c79b5
4c79b5
		if ($row = $db->sql_fetchrow($result))
4c79b5
		{
4c79b5
			$order = 0;
4c79b5
			do
4c79b5
			{
4c79b5
				++$order;
4c79b5
				if ($row['fields_order'] != $order)
4c79b5
				{
4c79b5
					$db->sql_query("UPDATE $table
4c79b5
						SET {$fields}_order = $order
4c79b5
						WHERE {$fields}_id = " . $row['order_id']);
4c79b5
				}
4c79b5
			}
4c79b5
			while ($row = $db->sql_fetchrow($result));
4c79b5
		}
4c79b5
		$db->sql_freeresult($result);
4c79b5
4c79b5
		$template->assign_vars(array(
4c79b5
			'L_TITLE'			=> $user->lang['ACP_' . $lang],
4c79b5
			'L_EXPLAIN'			=> $user->lang['ACP_' . $lang . '_EXPLAIN'],
4c79b5
			'L_IMPORT'			=> $user->lang['IMPORT_' . $lang],
4c79b5
			'L_EXPORT'			=> $user->lang['EXPORT_' . $lang],
4c79b5
			'L_NOT_DISPLAYED'	=> $user->lang[$lang . '_NOT_DISPLAYED'],
4c79b5
			'L_ICON_ADD'		=> $user->lang['ADD_' . $lang],
4c79b5
			'L_ICON_EDIT'		=> $user->lang['EDIT_' . $lang],
4c79b5
4c79b5
			'NOTICE'			=> $notice,
4c79b5
			'COLSPAN'			=> ($mode == 'smilies') ? 5 : 3,
4c79b5
4c79b5
			'S_SMILIES'			=> ($mode == 'smilies') ? true : false,
4c79b5
4c79b5
			'U_ACTION'			=> $this->u_action,
4c79b5
			'U_IMPORT'			=> $this->u_action . '&action=import',
4c79b5
			'U_EXPORT'			=> $this->u_action . '&action=export',
4c79b5
			)
4c79b5
		);
4c79b5
4c79b5
		$spacer = false;
4c79b5
4c79b5
		$sql = "SELECT *
4c79b5
			FROM $table
4c79b5
			ORDER BY {$fields}_order ASC";
4c79b5
		$result = $db->sql_query($sql);
4c79b5
4c79b5
		while ($row = $db->sql_fetchrow($result))
4c79b5
		{
4c79b5
			$alt_text = ($mode == 'smilies') ? $row['code'] : '';
4c79b5
4c79b5
			$template->assign_block_vars('items', array(
4c79b5
				'S_SPACER'		=> (!$spacer && !$row['display_on_posting']) ? true : false,
4c79b5
				'ALT_TEXT'		=> $alt_text,
4c79b5
				'IMG_SRC'		=> $phpbb_root_path . $img_path . '/' . $row[$fields . '_url'],
4c79b5
				'WIDTH'			=> $row[$fields . '_width'],
4c79b5
				'HEIGHT'		=> $row[$fields . '_height'],
4c79b5
				'CODE'			=> (isset($row['code'])) ? $row['code'] : '',
4c79b5
				'EMOTION'		=> (isset($row['emotion'])) ? $row['emotion'] : '',
4c79b5
				'U_EDIT'		=> $this->u_action . '&action=edit&id=' . $row[$fields . '_id'],
4c79b5
				'U_DELETE'		=> $this->u_action . '&action=delete&id=' . $row[$fields . '_id'],
4c79b5
				'U_MOVE_UP'		=> $this->u_action . '&action=move_up&id=' . $row[$fields . '_id'],
4c79b5
				'U_MOVE_DOWN'	=> $this->u_action . '&action=move_down&id=' . $row[$fields . '_id'])
4c79b5
			);
4c79b5
4c79b5
			if (!$spacer && !$row['display_on_posting'])
4c79b5
			{
4c79b5
				$spacer = true;
4c79b5
			}
4c79b5
		}
4c79b5
		$db->sql_freeresult($result);
4c79b5
	}
4c79b5
}
4c79b5
4c79b5
?>