<?
class soft extends action {
	function dlist()
	{
		$this->init_tpl();
		$limit			=		10;
		$page   			=  (int)$_GET['page']?(int)$_GET['page']:1;
		$this_page     =  ($page-1)*$limit;
		$sql				=		"SELECT * FROM {$this->db->db_pre}software WHERE is_stop	=0 ";
		$count_sql		=		"SELECT COUNT(software_id) as count FROM {$this->db->db_pre}software WHERE is_stop	=0 ";	
		//all category
		require_once(ROOT_PATH.'admin/includes/category.class.php');
		$category_obj		=		new category;	
		$this->tpl->out['category_res']		=	$category_obj->get_category('software');	
		
		
		if($cat_id=(int)$_GET['cat_id']){
			$cat_id_str	=	$cat_id.',';
			foreach($this->tpl->out['category_res'] as $cat_val){
				if($cat_val['parent']['cat_id']==$cat_id&&$cat_val['children']){
					foreach($cat_val['children'] as $c_cat_val){
						$cat_id_str.=$c_cat_val['cat_id'].',';	
					}
					break;
				}
			}
			$cat_id_str	=	substr($cat_id_str,0,-1);
			$sql					.=		" AND cat_id in ($cat_id_str)";
			$count_sql			.=		" AND cat_id in ($cat_id_str)";
		}
		if($software_name=$_GET['software_name']){
			$sql					.=		" AND software_name LIKE '%".$this->db->like_escape($software_name)."%'";
			$count_sql			.=		" AND software_name LIKE '%".$this->db->like_escape($software_name)."%'";
		}
		if($is_index_commend=(int)$_GET['is_index_commend']){
			$sql					.=		" AND is_index_commend ='$is_index_commend'";
			$count_sql			.=		" AND is_index_commend ='$is_index_commend'";
		}
		if($is_elite_commend	=	(int)$_GET['is_elite_commend']){
			$sql					.=		" AND is_elite_commend ='$is_elite_commend'";
			$count_sql			.=		" AND is_elite_commend ='$is_elite_commend'";
		}
	
		if($is_cat_commend=(int)$_GET['is_cat_commend']){
			$sql					.=		" AND is_cat_commend ='$is_cat_commend'";
			$count_sql			.=		" AND is_cat_commend ='$is_cat_commend'";
		}
		if($is_channel_commend=(int)$_GET['is_channel_commend']){
			$sql					.=		" AND is_channel_commend ='$is_channel_commend'";
			$count_sql			.=		" AND is_channel_commend ='$is_channel_commend'";
		}	
	
		$sql	.=" ORDER BY add_time DESC  LIMIT $this_page,$limit ";
		$soft_res		=		$this->db->getRecords($sql);	
		

		if($soft_res){	
			foreach($soft_res as $soft_key=>$soft_v){
				foreach($this->tpl->out['category_res'] as $val){
					if($val['parent']['cat_id']==$soft_v['cat_id']){
						$soft_res[$soft_key]['cat_name']	=	$val['parent']['cat_name'];
					}elseif($val['children']){
						foreach($val['children'] as $c_val){
							if($c_val['cat_id']==$soft_v['cat_id']){
								$soft_res[$soft_key]['cat_name']	=	$val['parent']['cat_name'].' / '.$c_val['cat_name'];
							}
						}
					
					}
				}	
				$soft_res[$soft_key]['software_icon']	=	dir_to_url($soft_v['software_icon']);
			}
		}
		
		$this->tpl->out['soft_arr']	=		$soft_res;	
		$count			=		$this->db->getNumber($count_sql);
		$this->tpl->out['page_arr']	=		pagination_arr($page,$count,$limit,10,'page');

		$this->tpl->display("soft.dlist.html");
	}	

	function addsoft()
	{
		$this->init_tpl();
		create_html_editor('software_desc','',$this->tpl->tpl);
		
		require_once(ROOT_PATH.'admin/includes/category.class.php');
		$category_obj		=		new category;	
		$this->tpl->out['category_res']			=	$category_obj->get_category('software');	
		
		$this->tpl->display("soft.addsoft.html");	
	}
	
	function save_addsoft()
	{
		if($_SERVER['REQUEST_METHOD']!='POST'){
			$this->addsoft();
			return;
		}
		global $_ADMIN_LANG;
		if(!($insert_arr		=	$this->check_submit_software())){
			$this->system_message($_ADMIN_LANG['adding_software_failure'],'error',array($_ADMIN_LANG['back_to_the_sheet_page']=>'javascript:history.go(-1);'),$this->message_info);
			exit;		
		}
		$software_id	=	$this->db->addRecord("{$this->db->db_pre}software",$insert_arr);
		if($software_id){
			foreach($_FILES['software_img_url']['name'] as $key=>$val){
					if(!$val||$_FILES['software_img_url']['error'][$key]){
						continue;
					}	
					$file_arr	=		array(
						'name'		=>	$val,
						'type'		=>	$_FILES['software_img_url']['type'][$key],
						'tmp_name'	=> $_FILES['software_img_url']['tmp_name'][$key],
						'error'		=> $_FILES['software_img_url']['error'][$key],
						'size'		=> $_FILES['software_img_url']['size'][$key],
					);		
					if((!$_POST['software_auto_thumb'][$key])&&$_FILES['software_thumb_url']['name'][$key]&&!$_FILES['software_thumb_url']['error'][$key]){
						$file_thumb_arr	=	array(
							'name'		=>	$_FILES['software_thumb_url']['name'][$key],
							'type'		=>	$_FILES['software_thumb_url']['type'][$key],
							'tmp_name'	=> $_FILES['software_thumb_url']['tmp_name'][$key],
							'error'		=> $_FILES['software_thumb_url']['error'][$key],
							'size'		=> $_FILES['software_thumb_url']['size'][$key],		
						);	
					}else{
						$file_thumb_arr	=	'';
					}
					$this->save_software_image($software_id,$insert_arr['software_name'].$insert_arr['software_version'],$file_arr,$file_thumb_arr,$_POST['software_img_bewrite'][$key],$_POST['image_sort_order'][$key]);
			}	
			$this->system_message($_ADMIN_LANG['adding_software_success'],'succeed',array($_ADMIN_LANG['continue_adding']=>'index.php?act=soft.addsoft',$_ADMIN_LANG['check_software_list']=>'index.php?act=soft.dlist'));
		}
	}
	
	function check_submit_software()
	{
		$software_id				=	isset($_GET['software_id'])?(int)$_GET['software_id']:0;
		if(!($software_name		=	$_POST['software_name'])){
			return false;	
		}
		if(!($software_version	=	$_POST['software_version'])){
			return false;	
		}	

		if($this->db->getNumber("SELECT  software_id  FROM  {$this->db->db_pre}software WHERE software_name='".$this->db->escape($software_name)."' AND software_version='".$this->db->escape($software_version)."' AND  software_id!=$software_id LIMIT 1")){
				$this->message_info			=				str_replace('$1',$software_name.' '.$software_version,$_ADMIN_LANG['software_exist']);
				return false;
		}	
		
		$downlink				=	($_POST['downlink']=='http://'||empty($_POST['downlink']))?'':$_POST['downlink'];			
		if(!$downlink){
			return false;
		}
		
		


		if($rel_software_id	=	$_POST['target_select']){
			$rel_software_id	=	array_unique($rel_software_id);	
			$rel_software_id	=	array_map('intval',$rel_software_id);
			$rel_software_id	=	implode(',',$rel_software_id);
		}else{
			$rel_software_id	=	'';
		}	
		
		//faq
		if($software_faq_question			=	$_POST['software_faq']['question']){
			$software_faq						=	array();
			foreach($software_faq_question as $key=>$val){
				if($val&&$_POST['software_faq']['answer'][$key]){
					$software_faq[]		=		array(
						'question'		=>	$val,
						'answer'			=>	$_POST['software_faq']['answer'][$key],
					);
				}
			}
			$software_faq			=	serialize($software_faq);
		}
		//pad
		$pad_check				=	$_POST['pad_check'];
		$padlink					=	$this->pad_check($pad_check,$_POST['padlink'],$software_name.$software_version);
		if($_POST['new_features']){
			foreach($_POST['new_features'] as $key=>$val){
				if(!$val){
					unset($_POST['new_features'][$key]);
				}
			}
			$new_features	=	serialize($_POST['new_features']);
		}
		if($_POST['add_time']){
			$tmp_arr		=	explode('-',$_POST['add_time']);
			$add_time	=	mktime(0,0,0,$tmp_arr[1],$tmp_arr[2],$tmp_arr[0]);
		}else{
			$add_time	=	time();
		}

		$sql_data_arr	=	array(
			'software_name'			=>	$software_name,
			'software_version'		=>	$_POST['software_version'],
			'software_brief'			=>	$_POST['software_brief'],
			'downlink'					=>	$downlink,
			'buylink'					=>	($_POST['buylink']=='http://'||empty($_POST['buylink']))?'':$_POST['buylink'],
			'cat_id'						=>	(int)$_POST['cat_id'],
			'os'							=>	$_POST['os'],
			'requirements'				=>	$_POST['requirements'],
			'license'					=>	$_POST['license'],
			'price'						=>	$_POST['price'],
			'limitations'				=>	$_POST['limitations'],
			'developer'					=>	$_POST['developer'],
			'home_page'					=>	($_POST['home_page']=='http://'||empty($_POST['home_page']))?'':$_POST['home_page'],	
			'software_desc'			=>	$_POST['software_desc'],
			'rel_software_id'			=>	$rel_software_id,
			'size'						=>	$_POST['size'],
			'seo_title'					=>	$_POST['seo_title'],
			'seo_keywords'				=>	$_POST['seo_keywords'],
			'seo_desc'					=>	$_POST['seo_desc'],
			'padlink'					=>	$padlink,
			'software_faq'				=>	$software_faq,
			'new_features'				=>	$new_features,
			'add_time'					=>	$add_time,
			'is_index_commend'		=>	(int)$_POST['is_index_commend'],
			'is_channel_commend'		=>	(int)$_POST['is_channel_commend'],
			'is_cat_commend'			=>	(int)$_POST['is_cat_commend'],
			'last_update'				=>	time(),
			'is_elite_commend'		=>	(int)$_POST['is_elite_commend'],
		);
	
		if(!empty($_FILES['software_icon']['name'])){
			$file_arr		=	$_FILES['software_icon'];
			if($file_arr['error']==0){	
				require_once(ROOT_PATH.'includes/glo_images.php');
				$images_obj		=		 new glo_images;
				$image_name								=		$this->make_file_name($software_name.$software_version,'icon');
				$sql_data_arr['software_icon']	=		$images_obj->upload_img($file_arr,'software_icon',$image_name);	
			}
		}
		if(!empty($_FILES['software_list_image']['name'])){
			$file_arr		=	$_FILES['software_list_image'];
			if($file_arr['error']==0){
				require_once(ROOT_PATH.'includes/glo_images.php');
				$images_obj		=		 new glo_images;
				$image_name		=		$this->make_file_name($software_name.$software_version,'list_image');
			
				$list_img_size				=	$this->db->getString("SELECT image_size FROM {$this->db->db_pre}site_config WHERE name='list_thumb'");
				$list_img_size				=	explode('X',$list_img_size);
				$list_img_width			=	$list_img_size[0];
				$list_img_height			=	$list_img_size[1];
		
				$sql_data_arr['software_list_image']			=	$images_obj->upload_img($file_arr,'software_list_image',$image_name,$list_img_width,$list_img_height);	
			}
		}

		return $sql_data_arr;	
	}

	function make_file_name($name,$type)
	{
		$name	=	name_from_url($name.'_'.$type.'_'.str_replace('.','',get_millisecond()));
		return $name;
	}

	function updatesoft()
	{
		$this->init_tpl();
		$software_id	=	(int)$_GET['software_id'];
		$software_res	=	$this->db->getRecord("SELECT a.*,GROUP_CONCAT(c.sort_order) as image_sort_order,GROUP_CONCAT( c.image_thumb_path SEPARATOR ',' ) as image_thumb_path,GROUP_CONCAT( c.image_id SEPARATOR ',' ) as image_id , GROUP_CONCAT( image_bewrite SEPARATOR ',' ) as image_bewrite, GROUP_CONCAT( c.image_path SEPARATOR ',' ) as image_path FROM {$this->db->db_pre}software a  LEFT JOIN {$this->db->db_pre}images c ON a.software_id=c.software_id  WHERE a.software_id='".$software_id."' GROUP BY c.software_id LIMIT 1");					
		if($soft_id_str	=	$software_res['rel_software_id']){
			$rel_software	=	$this->db->getRecords(" SELECT software_id,software_name,software_version FROM {$this->db->db_pre}software WHERE software_id IN ($soft_id_str)");
			if($rel_software){
				$this->tpl->out['rel_software']	=	$rel_software;
			}
		}
		$image_id_arr			=	explode(',',$software_res['image_id']);
		$image_path_arr			=	explode(',',$software_res['image_path']);
		$image_bewrite_arr	=	explode(',',$software_res['image_bewrite']);
		$image_thumb_path_arr	=	explode(',',$software_res['image_thumb_path']);
		$image_sort_order_arr=	explode(',',$software_res['image_sort_order']);
		$image_arr				=	array();
		
		
		$img_thumb_size	=	$this->db->getString(" SELECT image_size FROM {$this->db->db_pre}site_config WHERE name='img_thumb'");
		$img_thumb_size	=	explode('X',$img_thumb_size);
		$this->tpl->out['img_thumb_width']		=	$img_thumb_size[0];
		$this->tpl->out['img_thumb_height']		=	$img_thumb_size[1];
		
		foreach($image_id_arr as $key=> $val){
			if(!$val){
				continue;
			}
			$image_arr[$val]	=	array(
				'image_url'			=>	dir_to_url($image_path_arr[$key]),
				'image_bewrite'	=>	$image_bewrite_arr[$key],		
				'image_thumb_url'	=>	dir_to_url($image_thumb_path_arr[$key]),
				'image_sort_order'=>	$image_sort_order_arr[$key],
			);	
		}

		create_html_editor('software_desc',$software_res['software_desc'],$this->tpl->tpl);
		if(preg_match('/^(http:\/\/|https:\/\/).+/si',$software_res['padlink'])){
			$software_res['pad_check']	=	'url';
		}elseif($software_res['padlink']){
			$software_res['pad_check']	=	'file';
			$software_res['padlink_url']	=	dir_to_url($software_res['padlink']);
		}else{
			$software_res['pad_check']	=	'auto';
		}
		$software_res['software_list_image']	=	dir_to_url($software_res['software_list_image']);
		$software_res['software_icon']			=	dir_to_url($software_res['software_icon']);
		$software_res['software_faq']				=	unserialize($software_res['software_faq']);
		$software_res['new_features']				=	unserialize($software_res['new_features']);
		$this->tpl->out['software_res']	=	$software_res;
		$this->tpl->out['image_arr']		=	$image_arr;
	
		require_once(ROOT_PATH.'admin/includes/category.class.php');
		$category_obj		=		new category;	
		$this->tpl->out['category_res']		=	$category_obj->get_category('software');	

		$this->tpl->display("soft.updatesoft.html");		
	}

	function save_updatesoft()
	{
		global $_ADMIN_LANG;
		if($_SERVER['REQUEST_METHOD']!='POST'){
			$this->updatesoft();
			return;
		}
		if(!($update_arr		=	$this->check_submit_software())){
			$this->system_message($_ADMIN_LANG['software_modification_failure'],'error',array($_ADMIN_LANG['back_to_the_sheet_page']=>'javascript:history.go(-1);'),$this->message_info);
			exit;		
		}
		$software_id		=	(int)$_GET['software_id'];
		$this->db->updateRecord("{$this->db->db_pre}software","software_id='".$software_id."'",$update_arr);
		if($software_id){
				$old_image_bewrite		=	$_POST['old_image_bewrite'];
				$old_image_sort_order	=	$_POST['old_image_sort_order'];
				if($old_image_bewrite){
					foreach($old_image_bewrite as $b_key =>$b_val){
						$this->db->updateRecord("{$this->db->db_pre}images","image_id='".$b_key."'",array('image_bewrite'=>$b_val,'sort_order'=>$old_image_sort_order[$b_key]));
					}
				}

			//add image
			foreach($_FILES['software_img_url']['name'] as $key=>$val){
					if(!$val||$_FILES['software_img_url']['error'][$key]){
						continue;
					}	
					$file_arr	=		array(
						'name'		=>	$val,
						'type'		=>	$_FILES['software_img_url']['type'][$key],
						'tmp_name'	=> $_FILES['software_img_url']['tmp_name'][$key],
						'error'		=> $_FILES['software_img_url']['error'][$key],
						'size'		=> $_FILES['software_img_url']['size'][$key],
					);		
					if((!$_POST['software_auto_thumb'][$key])&&$_FILES['software_thumb_url']['name'][$key]&&!$_FILES['software_thumb_url']['error'][$key]){
						$file_thumb_arr	=	array(
							'name'		=>	$_FILES['software_thumb_url']['name'][$key],
							'type'		=>	$_FILES['software_thumb_url']['type'][$key],
							'tmp_name'	=> $_FILES['software_thumb_url']['tmp_name'][$key],
							'error'		=> $_FILES['software_thumb_url']['error'][$key],
							'size'		=> $_FILES['software_thumb_url']['size'][$key],		
						);	
					}else{
						$file_thumb_arr	=	'';
					}
					$this->save_software_image($software_id,$update_arr['software_name'].$update_arr['software_version'],$file_arr,$file_thumb_arr,$_POST['software_img_bewrite'][$key],$_POST['image_sort_order'][$key]);
			}
			$this->system_message($_ADMIN_LANG['software_modification_success'],'succeed',array($_ADMIN_LANG['continue_modification']=>'index.php?act=soft.updatesoft&software_id='.$software_id,$_ADMIN_LANG['check_software_list']=>'index.php?act=soft.dlist'));
		}	
	
	}

	function save_software_image($software_id,$name,$file_arr,$file_thumb_arr='',$software_img_bewrite='',$image_sort_order=0)
	{
		require_once(ROOT_PATH.'includes/glo_images.php');
		static $img_thumb_width,$img_thumb_height;
		if(!$img_thumb_width||!$img_thumb_height){
			$img_thumb_size	=	$this->db->getString(" SELECT image_size FROM {$this->db->db_pre}site_config WHERE name='img_thumb'");
			$img_thumb_size	=	explode('X',$img_thumb_size);
			$img_thumb_width	=	$img_thumb_size[0];
			$img_thumb_height	=	$img_thumb_size[1];
		}
		$images_obj				=	new glo_images;	
		$image_name		=		$this->make_file_name($name,'image');
		$software_img			=	$images_obj->upload_img($file_arr,'software_image',$image_name);	
		if(!$file_thumb_arr){
			$file_arr['tmp_name']	=	UPLOAD_DIR.$software_img;
			$image_name		=		$this->make_file_name($name,'thumb');
			$software_thumb_img	=	$images_obj->upload_img($file_arr,'software_thumb_image',$image_name,$img_thumb_width,$img_thumb_height);	
		}else{
			$image_name				=	$this->make_file_name($name,'thumb');
			$software_thumb_img	=	$images_obj->upload_img($file_thumb_arr,'software_thumb_image',$image_name,$img_thumb_width,$img_thumb_height);	
		}	
		$insert_arr				=	array(
			'software_id'		=>	$software_id,
			'image_path'			=>	$software_img,
			'image_bewrite'	=>	$software_img_bewrite,
			'image_thumb_path'	=>	$software_thumb_img,
			'sort_order'		=>	$image_sort_order,
		);
		require_once(ROOT_PATH.'admin/includes/images.class.php');
		$images_handle_obj	=	new images;
		return $images_handle_obj->save_addimage($insert_arr);
	}

	function callback_dlist()
	{
		$this->init_tpl();
		
		$limit			=		10;
		$page   			=  (int)$_GET['page']?(int)$_GET['page']:1;
		$this_page     =  ($page-1)*$limit;	
		
		$sql				=		"SELECT * FROM {$this->db->db_pre}software WHERE is_stop=1 ";		
		$count_sql		=		"SELECT COUNT(software_id) AS count FROM  {$this->db->db_pre}software WHERE is_stop=1";	

		if($software_name	=	$_GET['software_name']){
			$sql			.=		" AND software_name LIKE '%".$this->db->like_escape($software_name)."%'";
			$count_sql	.=		" AND software_name LIKE '%".$this->db->like_escape($software_name)."%'";
		}
		$sql	.=" ORDER BY last_update  DESC  LIMIT $this_page,$limit ";

		$soft_res	=	$this->db->getRecords($sql);
		if($soft_res){
			foreach($soft_res as $key=>$val){
				$soft_res[$key]['software_icon']			=	dir_to_url($val['software_icon']);
				$soft_res[$key]['software_list_image']	=	dir_to_url($val['software_list_image']);
			}
		}
		$this->tpl->out['soft_arr']	=		$soft_res;	
		$count			=		$this->db->getNumber($count_sql);
		$this->tpl->out['page_arr']	=		pagination_arr($page,$count,$limit,10,'page');

		$this->tpl->display("soft.callback_dlist.html");
	}

	function batch_handle()
	{
		$software_id_arr	=	array_map('intval',$_POST['checkboxes']);
		$software_id_str	=	implode(',',$software_id_arr);
		global $_ADMIN_LANG;
		if($_POST['handle_type']=='go_cat'){
			$handle_value	=	(int)$_POST['target_cat'];
			$handle_type	=	'cat_id';
		}elseif($_POST['handle_type']=='is_del'){
			$result			=	$this->del_soft($software_id_str);
			if(!$result){
				$this->system_message($_ADMIN_LANG['batch_deletion_failure'],'error',array($_ADMIN_LANG['return']=>'javascript:history.go(-1);'));
				exit;		
			}else{
				$this->system_message($_ADMIN_LANG['batch_process_success'],'success',array($_ADMIN_LANG['back_to_list_page']=>'index.php?act=soft.dlist'));
				exit;		
			}	
			return;
		}else{
			$handle_type		=	'`'.preg_replace(array('/^no_/','/`/'),array('',''),$_POST['handle_type']).'`';	
			$handle_value		=	(int)$_POST['handle_value'];
		}
			$result	=	$this->db->updateRecord($this->db->db_pre.'software'," software_id in (".$software_id_str.")",array($handle_type=>$handle_value,'last_update'=>time()));
			if(!$result){
				$this->system_message($_ADMIN_LANG['batch_process_failure'],'error',array($_ADMIN_LANG['return']=>'javascript:history.go(-1);'));
				exit;		
			}else{
				$this->system_message($_ADMIN_LANG['batch_process_success'],'success',array($_ADMIN_LANG['back_to_list_page']=>'index.php?act=soft.dlist'));
				exit;		
			}
	}	


	function del_soft($soft_id)
	{
		if(strstr($soft_id,',')){
			$where_sql	=	" software_id in ({$soft_id})";
		}else{
			$where_sql	=	" software_id = ".(int)$soft_id;
		}
		$software_res	=	$this->db->getRecord("SELECT software_icon,software_list_image FROM {$this->db->db_pre}software WHERE {$where_sql}");
		$result			=	$this->db->deleteRecord($this->db->db_pre."software", $where_sql);
		if($result){
			@unlink(UPLOAD_DIR.$software_res['software_icon']);
			@unlink(UPLOAD_DIR.$software_res['software_list_image']);
			$image_arr		=	$this->db->getRecords("SELECT image_thumb_path,image_path FROM {$this->db->db_pre}images WHERE {$where_sql} ");
			if($image_arr){
				foreach($image_arr as $val){
					@unlink(UPLOAD_DIR.$val['image_thumb_path']);
					@unlink(UPLOAD_DIR.$val['image_path']);
				}
			}	
			$result	=	$this->db->deleteRecord("{$this->db->db_pre}images", $where_sql);
		}	
		return $result;

	}
	
	function is_handle()
	{
		$software_id	=	(int)$_GET['software_id'];
		$type				='`'.str_replace('`','',$_GET['handle_type']).'`';		
		$is_handle		=	(int)$_GET['is_handle'];	
		if($this->db->updateRecord("{$this->db->db_pre}software"," software_id =$software_id",array($type=>$is_handle,'last_update'=>time()))){					echo json_return("ok");
					exit;
		}				
	}

	function search_soft()
	{
		if(!$_GET['is_ajax']){
			return;
		}
		$sql				=		"SELECT software_name,software_version,software_id FROM {$this->db->db_pre}software WHERE is_stop	=0 ";
		if($cat_id=(int)$_GET['cat_id']){
			$sql.=" AND cat_id IN ( SELECT cat_id FROM {$this->db->db_pre}category WHERE cat_id='$cat_id' OR parent_id='$cat_id') ";
		}
		if($software_name=$_GET['name']){
			$sql.=" AND software_name LIKE '%".$software_name."%'";	
		}
		$software_res		=		$this->db->getRecords($sql);
		if($software_res){
			foreach($software_res as $key=>$val){
				$software_res[$key]['name']		=		$val['software_name'].' '.$val['software_version'];
			}
		}
		echo json_return($software_res);
		exit;	
	}

	function pad_check($pad_check,$padlink,$name)
	{
		switch ($pad_check){

			case 'url'	:
				break;
			
			case 'file'	:
					$file_arr		=	$_FILES['padlink'];
					if($file_arr['error']==0){
						require_once(ROOT_PATH.'includes/glo_upload.php');
						$upload_obj			=	new glo_upload();
						$padlink				=	$upload_obj->upload_file($file_arr,'PAD',$this->make_file_name($name,'pad').'.xml');
					}
				break;

			case 'auto':
					$padlink	=	'';
				break;	
		
		}	
		return $padlink;
	}

	function addpad()
	{
		$this->init_tpl();
		require_once(ROOT_PATH.'admin/includes/category.class.php');
		$category_obj		=		new category;	
		$this->tpl->out['category_res']		=	$category_obj->get_category('software');	
	
		$this->tpl->display('soft.addpad.html');
	}

	function save_addpad()
	{
		global $_ADMIN_LANG;
		
		$cat_id			=		(int)$_GET['cat_id']?(int)$_GET['cat_id']:(int)$_POST['cat_id'];
		if((int)$_GET['is_link']){
			$pad_link			=		$_POST['pad_link'];
			session_write_close();	//php session file_get_conents bug?
			$pad_str				=		@file_get_contents($pad_link);
			if($name	=	$this->get_pad_data($pad_str,$pad_link,(int)$cat_id)){
				$this->system_message(str_replace('$1',$name,$_ADMIN_LANG['pad_import_success']),'success',array());
			}else{
				$this->system_message(str_replace('$1',$pad_link,$_ADMIN_LANG['pad_import_failed']),'error',array(),$this->message_info);
			}
		}elseif((int)$_GET['is_file']){
			$file_arr	=	array(
					'name'		=>	$_FILES['pad_file_name']['name'],
					'type'		=>	$_FILES['pad_file_name']['type'],
					'tmp_name'	=>	$_FILES['pad_file_name']['tmp_name'],
					'error'		=>	$_FILES['pad_file_name']['error'],
					'size'		=>	$_FILES['pad_file_name']['size'],
				);
				$pad_str			=		file_get_contents($file_arr['tmp_name']);	
				if($file_arr['error']==0&&$name	=	$this->get_pad_data($pad_str,$file_arr,(int)$cat_id)){
					$this->system_message(str_replace('$1',$name,$_ADMIN_LANG['pad_import_success']),'success',array());
				}else{
					$this->system_message(str_replace('$1',$file_arr['name'],$_ADMIN_LANG['pad_import_failed']),'error',array(),$this->message_info);
				}
		}else{
			$this->system_message('','error',array());
		}
	}

	function get_pad_data($pad_str,$pad_file_arr,$cat_id)
	{
		require_once(ROOT_PATH.'includes/glo_upload.php');
		$upload_obj			=	new glo_upload();
		global $_ADMIN_LANG;
		if(!$_ADMIN_LANG){
			global $glo_lang;
			require_once(ROOT_PATH.'admin/lang/'.$glo_lang.'.php');
		}	

		$sql_data	=	array(
			'cat_id'	=>	$cat_id,	
		);
	
		preg_match('|<Program_Name>(.*?)</Program_Name>|si',$pad_str,$software_name);
			$sql_data['software_name']		=	$software_name[1];
		

		preg_match('|<Program_Version>(.*?)</Program_Version>|si',$pad_str,$software_version);
			$sql_data['software_version']	=	$software_version[1];

		if(!$software_name[1]||!$software_version[1]||!$cat_id){
			$this->message_info		=		$_ADMIN_LANG['parse_pad_error'];
			return false;
		}else{
			$res		=	$this->db->getNumber(" SELECT software_id FROM {$this->db->db_pre}software WHERE software_name='".$this->db->escape($sql_data['software_name'])."' AND software_version='".$this->db->escape($sql_data['software_version'])."'");
			if($res){
				$this->message_info			=				str_replace('$1',$sql_data['software_name'].' '.$sql_data['software_version'],$_ADMIN_LANG['software_exist']);
				return false;
			}	
		}	
		
		preg_match('|<Program_Type>(.*?)</Program_Type>|si',$pad_str,$license);
			$sql_data['license']				=	$license[1];

		preg_match('|<Program_OS_Support>(.*?)</Program_OS_Support>|si',$pad_str,$os);
			$sql_data['os']					=	$os[1];

		preg_match('|<Program_System_Requirements>(.*?)</Program_System_Requirements>|si',$pad_str,$requirements);
			$sql_data['requirements']		=	$requirements[1];

		preg_match('|<File_Size_MB>(.*?)</File_Size_MB>|si',$pad_str,$size);
			$sql_data['size']					=	$size[1];

		preg_match('|<Author_First_Name>(.*?)</Author_First_Name>\s+?<Author_Last_Name>(.*?)</Author_Last_Name>|si',$pad_str,$developer);
			$sql_data['developer']			=	$developer[1].' '.$developer[2];
		
		preg_match('|<Application_Info_URL>(.*?)</Application_Info_URL>|si',$pad_str,$home_page);
			$sql_data['home_page']			=	$home_page[1];

		preg_match('|<Primary_Download_URL>(.*?)</Primary_Download_URL>|si',$pad_str,$downlink);
			$sql_data['downlink']			=	$downlink[1];

		preg_match('|<Application_Order_URL>(.*?)</Application_Order_URL>|si',$pad_str,$buylink);
			$sql_data['buylink']				=	$buylink[1];

		preg_match('|<Program_Cost_Dollars>(.*?)</Program_Cost_Dollars>|si',$pad_str,$price_1);
		preg_match('|<Program_Cost_Other_Code>(.*?)</Program_Cost_Other_Code>|si',$pad_str,$price_2);
		$sql_data['price']	=	$price_1[1].' '.$price_2[1];
		
		preg_match('|<Application_Icon_URL>(.*?)</Application_Icon_URL>|si',$pad_str,$software_icon);
			if($software_icon[1]){
				$ext		=		file_ext_name($software_icon[1]);	
				$sql_data['software_icon']		=	$upload_obj->copy_distance_link($software_icon[1],'software_icon',$this->make_file_name($software_name[1].$software_version[1],'icon').'.'.$ext);
			}
		preg_match_all('|<Application_Screenshot_URL>(.*?)</Application_Screenshot_URL>|si',$pad_str,$image_arr);
			$image_arr	=	$image_arr[1];
			if($image_arr){
				$software_list_image	=	$image_arr[0];
			}
			if($software_list_image){
				require_once(ROOT_PATH.'includes/glo_images.php');
				$images_obj		=		 new glo_images;
				
				$ext				=	file_ext_name($software_list_image);
				$image_name		=		$this->make_file_name($software_name[1],'list_image');
			
				static $list_img_width		 = '';
				static $list_img_height		 = '';
				if(!$list_img_width||!$list_img_height){
					$list_img_size				=	$this->db->getString("SELECT image_size FROM {$this->db->db_pre}site_config WHERE name='list_thumb'");
					$list_img_size				=	explode('X',$list_img_size);
					$list_img_width			=	$list_img_size[0];
					$list_img_height			=	$list_img_size[1];
				}
				$file_arr['tmp_name']			=	$upload_obj->copy_distance_link($software_list_image,'tmp',$image_name.'.'.$ext);
				if($file_arr['tmp_name']){
					$file_arr['tmp_name']			=	UPLOAD_DIR.$file_arr['tmp_name'];
					$file_arr['name']					=	$image_name.'.'.$ext;					
					$file_arr['size']					=	filesize($file_arr['tmp_name']);
					$file_arr['type']					=	'image/'.$ext;

					$sql_data['software_list_image']			=	$images_obj->upload_img($file_arr,'software_list_image',$image_name,$list_img_width,$list_img_height);	
					@unlink($file_arr['tmp_name']);
				}
			}

		preg_match('|<Keywords>(.*?)</Keywords>|si',$pad_str,$seo_keywords);
			$sql_data['seo_keywords']			=	$seo_keywords[1];
		
		preg_match('|<Char_Desc_250>(.*?)</Char_Desc_250>|si',$pad_str,$seo_desc);
			$sql_data['seo_desc']	=	$seo_desc[1];

		preg_match('|<Char_Desc_250>(.*?)</Char_Desc_250>|si',$pad_str,$software_brief);
			$sql_data['software_brief']		=	$software_brief[1];

		preg_match('|<Char_Desc_2000>(.*?)</Char_Desc_2000>|si',$pad_str,$software_desc);
			$sql_data['software_desc']			=	nl2br($software_desc[1]);

		if(is_array($pad_file_arr)){	
			$sql_data['padlink']			=	$upload_obj->upload_file($pad_file_arr,'PAD',$this->make_file_name($software_name[1].$software_version[1],'pad').'.xml');
		}elseif(is_string($pad_file_arr)){
			$sql_data['padlink']	=	$pad_file_arr;
		}

		$software_id	=	$this->db->addRecord("{$this->db->db_pre}software",$sql_data);
		if($software_id){
			if($image_arr){
				foreach($image_arr as $val){
					$ext									=		file_ext_name($val);
					$image_name							=		$this->make_file_name($software_name[1],'thumb');
					$file_arr['tmp_name']			=	$upload_obj->copy_distance_link($val,'tmp',$image_name.'.'.$ext);
					if($file_arr['tmp_name']){
						$file_arr['tmp_name']			=	UPLOAD_DIR.$file_arr['tmp_name'];
						$file_arr['name']					=	$image_name.'.'.$ext;					
						$file_arr['size']					=	filesize($file_arr['tmp_name']);
						$file_arr['type']					=	'image/'.$ext;	
					
						$this->save_software_image($software_id,$sql_data['software_name'].$sql_data['software_version'],$file_arr);
					}
				}
			}				
			return $software_name[1].' '.$software_version[1];
		}else{
			$res		=	$this->db->getNumber(" SELECT software_id FROM {$this->db->db_pre}software WHERE software_name='".$this->db->escape($sql_data['software_name'])."' AND software_version='".$this->db->escape($sql_data['software_version'])."'");
			if($res){
				$this->message_info			=				str_replace('$1',$sql_data['software_name'].' '.$sql_data['software_version'],$_ADMIN_LANG['software_exist']);
			}				
			@unlink(UPLOAD_DIR.$sql_data['software_icon']);
			@unlink(UPLOAD_DIR.$sql_data['software_list_image']);
			@unlink(UPLOAD_DIR.$sql_data['padlink']);
			return false;
		}		
	
	}
	
}	

