<?php
@ini_set('memory_limit', '-1');
@set_time_limit(0);
require_once MODEPATH.'./includes/image.func.php';
require_once MODEPATH.'./'.$adminpath.'/makehtml.func.php';
$sorts=$fsorts=$uploads=array();
$query = $db -> query("SELECT * FROM {$tablepre}picsort ORDER BY ordermode ");
while($sort = $db -> fetch_array($query)){
	$sorts[$sort['id']]=$sort;
	$fsorts[$sort['upid']][$sort['id']]=$sort;
}
if($menu == 'check') {
	$fromfloder = '/import';
	if ($dh = @opendir(MODEPATH.'.'.$fromfloder)) {
		while (($folder = readdir($dh)) !== false) { //&& !file_exists(MODEPATH.'.'.$fromfloder. '/' . $folder.'/ischeck')
			if ($folder != "." && $folder != "..") {				
				if(is_dir(MODEPATH.'.'.$fromfloder. '/' . $folder) && !file_exists(MODEPATH.'.'.$fromfloder. '/' . $folder.'/ischeck')) {
					$path  = $fromfloder. '/' . $folder;
					$images = filter(@serialize(checkdir($path)),1);
					$db -> query("INSERT INTO {$tablepre}import (pname,folder,images) VALUES('$folder','$path','$images')");
					writetofile(MODEPATH.'.'.$fromfloder. '/' . $folder.'/ischeck',1);
				}   
			}
		}
		closedir($dh);
	}
	cpmsg("succeeded",'index.php?action=importpic');
}else{
	if(cpchecksubmit('import')) {
	
		$newid = $newids ? $newids : $newid;
		if(empty($newid) || !$sorts[$newid]){
			cpmsg('sort_must');
		}
		if(is_array($delete)) {
			$ids = idarray($delete);
			$query = $db -> query("SELECT * FROM {$tablepre}import WHERE fid IN($ids)");
			$attach_subdir = 'day_'.date('Ymd');
			if(!is_dir(MODEPATH.'./'.$attachdir.'/'. $attach_subdir.'/')) {
				@mkdir(MODEPATH.'./'.$attachdir.'/'. $attach_subdir.'/', 0777);
				@fclose(@fopen(MODEPATH.'./'.$attachdir.'/'. $attach_subdir.'/index.htm', 'w'));
			}
				
			while($pic = $db -> fetch_array($query)) {
					$images =checkdir($pic['folder']);	
					$i = 0;
					$newfaces = $upids=$common='';
					foreach($images as $key => $img){		
						$ext = strtolower(fileext($img));
						if(in_array($ext, array('png','jpg','gif','jpeg','bmp'))) {
							$list = explode('/',$img);
							$filename = $list[count($list)-1];										
							$attachment=date('Ymd').'_'.substr(md5($filename.microtime()), 6).random(6).'.'.$ext;
							$target = MODEPATH.'./'.$attachdir.'/'. $attach_subdir.'/'.stripslashes($attachment);					
							@copy(MODEPATH.$img,$target);			
							$filesize = @filesize($target);							
							if($filesize > 0) {
								$i++;
								$face='';
								if($gd){
									$face = $attach['path'].$attach_subdir.'/face_'.random(6).'.'.$ext;
									imageresize($target,$sppicw,$sppich,MODEPATH.'./'.$attachdir.'/'.$face) ;				
									if($mark != 'off'){
										imgwatermark($target, $mark,MODEPATH.'./'.$attachdir.'/'.$wartermark,$markview);
									}	
									if($i==1) {
										$newfaces=$attach['path'].$attach_subdir.'/face_'.random(6).'.'.$ext;
										@copy(MODEPATH.'./'.$attachdir.'/'.$face,MODEPATH.'./'.$attachdir.'/'.$newfaces);
									}						
									
								}
								$attachment = $attach_subdir.'/'.$attachment;
								$db -> query("INSERT INTO {$tablepre}uploads  (mid, type, filename, filesize, filetype, face, attachment, description, uptime) VALUES ( '$mm_mid', '$ext', '$filename', '$filesize', '$ext', '$face', '$attachment', '$pic[pname]', '$time') ");
								$upids.=$common.$db -> insert_id();
								$common=',';
							}else{
								@unlink($target);
							}
									
						}
					}
					$db -> query("UPDATE {$tablepre}import SET isim='1' WHERE  fid='$pic[fid]'");
					$db -> query("INSERT INTO {$tablepre}pic (id,pname, face,postime, attachment) VALUES('$newid',  '$pic[pname]', '$newfaces', '$time', '$upids')");
					$pid = $db -> insert_id();
					if($upids) {
						$db -> query("UPDATE {$tablepre}uploads SET pid='$pid' WHERE uid IN($upids)");
					}
					makepage($pid);
				  makepic($pid);
				  
				}
				makeindex();
				cpmsg('succeeded', 'index.php?action=importpic');
		}else{
			cpmsg('error');
		}
	}elseif(cpchecksubmit('dels')){
		if(is_array($delete)) {
			$ids = idarray($delete);
			$query = $db -> query("SELECT * FROM {$tablepre}import WHERE fid IN($ids)");		
			while($pic = $db -> fetch_array($query)) {	
				clearfolder($pic['folder']);
				@rmdir(MODEPATH.$pic['folder']);
			}
			$db -> query("DELETE FROM {$tablepre}import WHERE  fid IN($ids)");
			cpmsg('succeeded', 'index.php?action=importpic');	
		}else{
			cpmsg('error');
		}
	}elseif($del){
		$pic = $db -> queryarray("SELECT * FROM {$tablepre}import WHERE fid='$del'");
		clearfolder($pic['folder']);
		@rmdir(MODEPATH.$pic['folder']);
		$db -> query("DELETE FROM {$tablepre}import WHERE  fid='$del'");
		cpmsg('succeeded', 'index.php?action=importpic');	
	}else{
		$page = ispage($page) ? $page : 1;
		$pagenum = 20;
		$start_limit = ($page-1) * $pagenum;	
		$pics = array();
		$query = $db -> query("SELECT COUNT(*) FROM {$tablepre}import");
		$lines = $db -> result($query, 0);
		$paging = paging($lines, $pagenum, $page, 'index.php?action=importpic');
		$query = $db -> query("SELECT * FROM {$tablepre}import LIMIT $start_limit, $pagenum");
		while($pic = $db -> fetch_array($query)){
			$pic['pics']=@count(@unserialize($pic['images']));
			$pics[$pic['fid']] = $pic;
		}
		$select = sortselect($fsorts);
		include theme('importpic', 'themes/admin', 'admin');
	}
}
?>