<?php
/* Ulyxex version 1.6.7.8 ***************/
/* code http://ulyxex.logz.org **********/
/* Andre Lozano http://andre-lozano.org */
class ViewFiles extends Admin {
	public string $dir;
	public string $Select;
	
	function __construct($dir=null){
		$this->dir = $dir;
		$this->h = New Htmlz();
		$this->t = New Translate();
		if (isset($_GET['pg'])) {
			$this->pagePos = $this->h->num($_GET['pg']);
		} else {
			$this->pagePos = 0;
		}
		if ($_SESSION['level'] < 2) {
			$this->Select = SELECT.FILES.".ID,".FILES.".FILENAME,".USERS.".USERNAME".FROM.FILES.INNERJOIN.USERS._ON_.FILES.".USERID = ".USERS.".ID";
		} else {
			$_[] = "USERID='".$_SESSION['userid']."'";
			$query = $this->q(SELECT."ID,USERNAME".FROM.USERS.WHERE." USEROWNER='".$_SESSION['userid']."'");
			while ( $line = $this->fetch($query) ){
				$_[] = "USERID='".$line['ID']."'";
			}
			$this->Select = SELECT.FILES.".ID,".FILES.".FILENAME,".USERS.".USERNAME".FROM.FILES.INNERJOIN.USERS._ON_.FILES.".USERID = ".USERS.".ID".$this->whereOr($_);
		}
	}
	public function update_ftp_files(){
		$h = $this->h;$t = $this->t;
		$data = "";
		$values = $_POST;
		foreach($values as $k=>$v){
			if ( preg_match("/delete_/",$k) ){
				$file = $_POST['file_'.substr($k,7)];
				@unlink($file);
			} elseif ( preg_match("/add_/",$k) ){
				$now = time();
				$date = date("Y-m-d H:i:s",$now);
				$query = INSERTINTO.FILES." (FILENAME,USERID,DATEFILE) VALUES (".
										"'".$_POST['file_'.substr($k,4)]."',".
										$h->num($_POST['userid']).",'".
										$date."');";				
				$query = $this->q($query);
			}
		}
		if (!empty($_FILES['addfile']["name"])) {
			$fileName = $_FILES['addfile']["name"];
			$ext = $h->get_ext($fileName);
			if ( !in_array($ext, $h->extUploadAllowed()) && $_SESSION['level'] > 1) exit("file extension not allowed ! ".$ext);// accept all file in level 1
			$sendFile = move_uploaded_file($_FILES['addfile']["tmp_name"],"files/".$fileName);
		}
		return $h->h3($t->w("some vals been modified !"),$h->v("class","warning"));
	}
	public function notInDataBase(){
		// warning ! this function return all the files in directory with no limit
		$user = $this->user_params($_SESSION['userid'])["USERNAME"];
		$total = 0;
		$h = $this->h;$t = $this->t;
		$files = array();
		$filesNotInBase = array();
		$_ = array();
		$query = SELECT."FILENAME".FROM.FILES;
		$query = $this->q($query);
		while ( $line = $this->fetch($query) ) {
			$files[] = $line["FILENAME"];
		}
		$handle = opendir($this->dir);
		$i = 0;
		while ($file = readdir($handle)){
			$i++;
			if( // don't show this files
				preg_match("/^\./",$file) || // don't show .htaccess etc.
				$file == "index.php" ||
				$file == "index.htm"
				){
				continue;
			} else {
				if (!in_array($this->dir.$file,$files)) {
					$total++;
					$id = rand(10000,99999);
					$size = $h->octets(filesize($this->dir.$file));
					$params = stat($this->dir.$file);
					$date = date("Y-m-d",$params[9]);
					$_[] = $h->p(
						$h->input("hidden","file_".$id,$this->dir.$file).$this->dashframe($date).
						$this->dashframe(
							" ".$h->lbl($t->wr("delete"),"delete_".$id).
							$h->input("checkbox","delete_".$id,"on")
						,$h->alt($t->wz("delete").$file)).
						$this->dashframe(
							" ".$h->lbl($t->wr("assign"),"add_".$id).
							$h->input(
								"checkbox","add_".$id,"on"
							)
						,$h->alt(
							$t->wz("assign").$file
							//~ ."\n".$t->wz("to user").$user
						)).
						" ".
						$h->ahref($this->dir.$file,$file,$h->v("target","blank").$h->alt($t->wz("show").$file))." ~".$size
						,$h->v("class","linebtns")
					);
				}
			}
			//~ if ($i > 250) break;
		} // end while
		closedir($handle);
		
		$filesNotInBase = $h->h3($t->wr("list of uploaded files by FTP").$h->ahref("javascript:window.close();",$t->w("close tab"),$h->v("class","linkbtn")));
		$filesNotInBase .= $h->p($h->lbl($t->wr("add file ftp"),"addfile",$h->v("style","font-weight:bold;")).$h->input("file","addfile","",$h->v("size","30")));
		$filesNotInBase .= $h->h3($t->w("unassigned files ftp"));
		$filesNotInBase .= $h->p($t->wlr("total files").": ".$total);
		$filesNotInBase .= $h->input("hidden","update",1);
		$filesNotInBase .= $h->p($h->input("submit",null,$t->w("update")));
		$filesNotInBase .= $h->p($h->lbl($t->wr("choose user"),"userid").$this->array_users(1));
		//~ $filesNotInBase .= $h->p($h->input("text","user",$user,$h->v("readonly","readonly")));
		
		$in = ($this->pagePos < 2)?0:$this->pagePos-1;
		$begin = $in*ITEMS;
		$max = ITEMS;
		if ($max > $total) $max = $total;
		
		$filesItems = array_slice($_,$begin,$max);
		$filesNotInBase .= implode(" ",$filesItems);
		$filesNotInBase .= $h->p($h->input("submit",null,$t->w("update")));
		$filesNotInBase = $h->form($filesNotInBase,"ftp_files","post","admin.php?action=ftp_files","multipart/form-data");
		$filesNotInBase .= $this->next_pages($total);
		return $filesNotInBase;
	}
	public function next_pages($total){
		$h = $this->h;$t = $this->t;
		$pagesNumbers = new NextPages($total,"action=ftp_files");
		$result = $h->p($pagesNumbers->numbers(),$h->v("class","pagenumbers"));
		$result .= $h->p("(".$t->wr("total files")."= ".$total.")",$h->v("style","text-align:center"));
		return $result;
	}
}
?>

page url