<?php
/**
 *      [Dzz!] (C)2010-2011 dzz.cc
 *      This is NOT a freeware, use is subject to license terms
 *
 * ============================================================================
 * 版权所有 2010-2011 Dzz!Desktop，并保留所有权利。
 * 网站地址: http://dzz.cc；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式、任何目的的再发布。
 * ============================================================================
 */
if(!defined('IN_DISCUZ') || !defined('IN_ADMINCP')) {
	exit('Access Denied');
}
$refer=dreferer();
dzz_cpheader();

	if(!$operation) {
		if(submitcheck('huandengsubmit')) {
			$tids=$_GET['tid'];
			foreach($tids as $tid){
				DB::query("update ".DB::table('dzz_recomment')." set `disp`='{$_POST['disp'][$tid]}',`available`='{$_POST['available'][$tid]}' where tid ='{$tid}'");
			}
			$dids=$_GET['delete'];
			if($dids){
				$query=DB::query("select img from ".DB::table('dzz_recomment')." where tid IN (".dimplode($dids).")");
				while($value=DB::fetch($query)){
					if($value['img']) @unlink(DISCUZ_ROOT.'./'.$value['img']);
				}
				DB::query("delete from ".DB::table('dzz_recomment')." where tid IN (".dimplode($dids).")");
			}
			cpmsg($lang['do_success'], 'action=huandeng', 'succeed');
		}
		$huandeng=array();
		$query=DB::query("select * from ".DB::table('dzz_recomment')." where type=0 order by disp");
		while($value=dstripslashes(DB::fetch($query))){
			$value['dateline']=dgmdate($value['dateline'],'Y-m-d');
			$huandeng[]=$value;
		}
	} elseif($operation == 'add') {
		if(submitcheck('addsubmit')) {
			$appid=intval($_GET['appid']);
			if(!DB::result_first("select COUNT(*) from ".DB::table('dzz_apps')." where appid='{$appid}'")){
				cpmsg($lang['extended_huandeng_appid_error'], '', 'error');	
			}
			$setarr=array();
			if($_FILES['iconnew']) {
				//print_r($_FILES['iconnew']);
				//cpmsg('dfdsfd');
				if($_FILES['iconnew']['tmp_name']){
					// upload_to_appico($_FILES['iconnew'],$appurl);
					 $ext=(strtolower(substr(strrchr($_FILES['iconnew']['name'], '.'), 1, 10)));
					$imgext  = array('jpg', 'jpeg', 'gif', 'png', 'bmp','ico');
					if(!in_array($ext, $imgext)){
						cpmsg("'jpg', 'jpeg', 'gif', 'png', 'bmp','ico'",'','error');
					}
					$source=$_FILES['iconnew']['tmp_name'];
					$subdir = $subdir1 = $subdir2 = '';
					$subdir1 = date('Ym');
					$subdir2 = date('d');
					$subdir = $subdir1.'/'.$subdir2.'/';
					$target=$_G['setting']['attachdir'].'appimg/'.$subdir.''.date('His').''.strtolower(random(16)).'.'.$ext;
					$target1=str_replace($_G['setting']['attachdir'],$_G['setting']['attachurl'],$target);
					$targetpath = dirname($target);
					dmkdir($targetpath);
					if(save_to_local($source, $target)){
						$img= $target1;
						require_once libfile('class/image');
						$image = new image();
						$result = $image->Thumb($img, '', 514, 191, 2,1);
					}
				}
			} else {
				if($_GET['iconnew']){
					if($img=imagetolocal($_GET['iconnew'])){
						require_once libfile('class/image');
						$image = new image();
						$img=$_G['setting']['attachurl'].$img;
						$result = $image->Thumb($img, '', 514, 191, 2,1);
					}
				}
			}
			$setarr['title']=getstr($_GET['title']);
			$setarr['appid']=$appid;
			$setarr['available']=intval($_GET['available']);
			if($img) $setarr['img']=$img;
			$setarr['dateline']=$_G['timestamp'];
			DB::insert('dzz_recomment',daddslashes($setarr));
			cpmsg($lang['do_success'], 'action=huandeng', 'succeed');
		}
	} elseif($operation == 'edit') {
		$tid=intval($_GET['tid']);
		$arr=dstripslashes(DB::fetch_first("select * from ".DB::table('dzz_recomment')." where tid='{$tid}'"));
		if(!$arr){
				cpmsg($lang['extended_huandeng_not_exist'], '', 'error');
		}
		if(submitcheck('editsubmit')) {
			$appid=intval($_GET['appid']);
			
			if(!DB::result_first("select COUNT(*) from ".DB::table('dzz_apps')." where appid='{$appid}'")){
				cpmsg($lang['extended_huandeng_appid_error'], '', 'error');	
			}
			
			$setarr=array();
			if($_FILES['iconnew']) {
				if($_FILES['iconnew']['tmp_name']){
					if($arr['img']){
						 $target1=$arr['img'];
						 $target=str_replace($_G['setting']['attachurl'],$_G['setting']['attachdir'],$target1);
					}else{
						 $ext=(strtolower(substr(strrchr($_FILES['iconnew']['name'], '.'), 1, 10)));
						$imgext  = array('jpg', 'jpeg', 'gif', 'png', 'bmp','ico');
						if(!in_array($ext, $imgext)){
							cpmsg("'jpg', 'jpeg', 'gif', 'png', 'bmp','ico'",'','error');
						}
						$subdir = $subdir1 = $subdir2 = '';
						$subdir1 = date('Ym');
						$subdir2 = date('d');
						$subdir = $subdir1.'/'.$subdir2.'/';
						$target=$_G['setting']['attachdir'].'appimg/'.$subdir.''.date('His').''.strtolower(random(16)).'.'.$ext;
						$target1=str_replace($_G['setting']['attachdir'],$_G['setting']['attachurl'],$target);
						$targetpath = dirname($target);
						dmkdir($targetpath);
					}
					if(save_to_local($_FILES['iconnew']['tmp_name'], $target)){
						require_once libfile('class/image');
						$image = new image();
						$result = $image->Thumb($target1, '', 514, 191, 2,1);
						$arr['img']=$target1;
					}
				}
			} else {
				if($_GET['iconnew'] && $arr['img']!=$_GET['iconnew']){
					if($arr['img']){
						$data =dzz_file_get_contents($_GET['iconnew']);
						file_put_contents($arr['img'], $data);
					}else{
						$data =dzz_file_get_contents($_GET['iconnew']);
						$ext=(strtolower(substr(strrchr($_GET['iconnew'], '.'), 1, 10)));
						$imgext  = array('jpg', 'jpeg', 'gif', 'png', 'bmp','ico');
						if(!in_array($ext, $imgext)){
							cpmsg("'jpg', 'jpeg', 'gif', 'png', 'bmp','ico'",'','error');
						}
						
						$subdir = $subdir1 = $subdir2 = '';
						$subdir1 = date('Ym');
						$subdir2 = date('d');
						$subdir = $subdir1.'/'.$subdir2.'/';
						$target=$_G['setting']['attachdir'].'appimg/'.$subdir.''.date('His').''.strtolower(random(16)).'.'.$ext;
						$target1=str_replace($_G['setting']['attachdir'],$_G['setting']['attachurl'],$target);
						$targetpath = dirname($target);
						dmkdir($targetpath);
						file_put_contents($target, $data);
						$arr['img']=$target1;
					}
					require_once libfile('class/image');
					$image = new image();
					$result = $image->Thumb($arr['img'], '', 514, 191, 2,1);
					
				}
			}
			$setarr['title']=getstr($_GET['title']);
			$setarr['appid']=$appid;
			$setarr['available']=intval($_GET['available']);
			$setarr['img']=$arr['img'];
			DB::update('dzz_recomment',daddslashes($setarr),"tid='{$tid}'");
			cpmsg($lang['do_success'], 'action=huandeng', 'succeed');
		}
	}
include dzztemplate('admin/dzz_huandeng');
dzzfooter();
?>