Location: PHPKode > projects > chillyCMS > chillyCMS/admin/insertfiles.include.php
<?php
//#################################################################################################
//	Insert download links to media files helper functions
//#################################################################################################
//	chillyCMS - Content Management System
//	Copyright (C) 2008
//	Stefanie Wiegand <hide@address.com> & Johannes Cox <hide@address.com>
//	
//	This program is licensed under the GPL 3.0 license. For more information see LICENSE.txt.
//#################################################################################################
//	insert_files()
//	show_files($folder)
//	folderselect_recursive()
//#################################################################################################
defined('DOIT') or die('Restricted access');

//Insert pictures form/////////////////////////////////////////////////////////////////////////////
function insert_files() {
	global $showfolder,$mysession,$is_admin,$l_edit,$l_gen,$page;
	
	$allfiles = "<div class='popup'><br /><h3 class='center'>$l_edit[lbl_file]</h3>".
	"<a class=\"info\" title=\"".$l_edit["lbl_howto"]."\"></a><br />";
	$folders=array();
	$path="../media";
	$folders[]=$path;
	folderselect_recursive($path,$folders);
	$sql="select name from system_groups where";
	foreach ($mysession->user->gids as $gid) {
		$sql.=" gid=$gid or";
	}
	$sql=substr($sql,0,-3);
	$page->query($sql);
	$result = $page->db->getdata();
	$okgroups=array();
	foreach ($result as $group) {
		$okgroups[]=$group["name"];
	}
	$allfiles .= "<form style='margin:0 0 0 10px;' method='post' action='insertfiles.site.php'>".
	"<select name='folder' size='1' class='button'>";
	sort($folders);
	foreach ($folders as $folder) {
		$folderend=explode("/",$folder);
		$folderend=array_pop($folderend);
		//either a group the user is in or the media folder
		if (in_array($folderend,$okgroups) or $folderend=="media" or $is_admin) {
			if ($showfolder==$folder) {
				$allfiles .= "<option selected='selected'>$folder</option>";
			} else {
				$allfiles .= "<option>$folder</option>";
			}
		}
	}
	$allfiles .= "</select><input type='submit' class='button' value='$l_gen[lbl_show]'></input></form>";
	$allfiles .= show_files($showfolder);
	$allfiles .= "</div>";
	
	return $allfiles;
}
//Show links to all items /////////////////////////////////////////////////////////////////////////
function show_files($path) {
	global $showfolder,$mysession,$is_admin,$l_tools,$l_edit,$page;
	
	//Open selected folder
	$fh = opendir($path);
	$verzeichnisinhalt = array();
	while (true == ($file = readdir($fh))) {
		if (!is_dir($path."/".$file)) {
			$verzeichnisinhalt[] = $file;	//TODO: nur erlaubte dateitypen!
		}
	}
	
	//Check if the directory is a groupfolder
	$thisfolder=explode("/",substr($path,0,-1));
	$foldername=array_pop($thisfolder);
	$sql = "select u.uid from system_groups as g, system_users as u where g.name='$foldername' ".
	"and g.moderator=u.uid limit 1";
	$page->query($sql);
	$uid=$page->db->getdata();

	//The moderator's uid
	$moduid=$uid["uid"];
	$is_mod=false;
	if ($moduid == $mysession->user->uid) { $is_mod=true; }
	
	//append / if required
	if (substr($path,-1)!="/") { $path.="/"; }
	sort($verzeichnisinhalt);
	$showfiles = "\t\t<div>\n".
	"\t\t\t<table class='downloadlinklist' cellspacing='0'>\n".
	"<tr><th width='70%'>$l_edit[lbl_name]</th><th colspan='2' class='center'>$l_tools[lbl_filesize]</th></tr>";
	$style="odd";
	foreach ($verzeichnisinhalt as $vi) {
		$showfiles .= "\t\t\t\t<tr class='$style'>\n";
			$filesize=round(filesize($path.$vi)/1000,0);
			if ($filesize<1000) {
				$displaysize=$filesize;
				$unit = "kB";
			} else {
				$displaysize=round($filesize/1000,2);
				$unit = "MB";
			}
			//make name short enough not to destroy the layout
			$vi_short = $vi;
			if (strlen($vi_short)>=20) { $vi_short = substr($vi_short,0,17).'...'; }
				
			$showfiles .= "\t\t\t\t\t<td class='left'>".
			"<a class=\"downloadlink\" href=\"".$path.$vi."\" alt=\"".$vi."\" title=\"".$vi."\">".$vi_short.
			"</a></td><td class='right'>$displaysize</td><td width='20'>$unit</td>\n".
		"\t\t\t\t</tr>\n";
		if ($style=="odd") { $style="even"; } else { $style="odd"; }
	}
	$showfiles .= "\t\t\t</table>\n".
	"\t\t</div>\n".
	"\t<div class='clr'></div>\n".
	"\t</div>\n";
	
	return $showfiles;
}
//Show folder select///////////////////////////////////////////////////////////////////////////////
function folderselect_recursive($path,&$folders) {
	global $mysession,$is_admin,$page;
	$result = false;
	$handle = opendir($path);
	//does the directory exist and can it be accessed?
	if ($handle) {
		//is there any content?
		while (false !== ($file = readdir($handle))) {
			//file is not the folder itself or the parent folder
			if ($file != "." && $file != "..") {
				$name = $path."/".$file;
				$printname=substr($name,strlen($path)+1);
				
				//"file" is a folder
				if (is_dir($name)) {
					array_push($folders,$name);
					
					//is the user allowed to see the folder? if not stop recursing into it
					$foldername=explode("/",$name);
					$myfolder=array_pop($foldername);
					$page->query("select gid from system_groups where name='$myfolder' limit 1");
					$result = $page->db->getdata();

					//If the user is in that group show him the folder
					if ($is_admin or $result>2) {
						$ar = folderselect_recursive($name,$folders);
					}
				}
			}
		}	
	}
	//close directory and return the contents
	closedir($handle);
	return $result; 
}
?>
Return current item: chillyCMS