Location: PHPKode > projects > PWhere > pwhere-0.4/pwhere-manager.php
<?php

/*
 Copyright 2008-2009 - Domenico Ferrari <hide@address.com>

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

error_reporting(E_ALL);

include("pwhere-conf.php");
include("lang/$LANG.php");
//$DrivePropWin="cscript ".$PWhereBase."\\getlabel.vbs //nologo "; // Windows get disk label
//$DriveListWin="cscript ".$PWhereBase."\\getdrives.vbs //nologo "; // Windows get drives list

/*
* SQL functions for MySQL db type
*/
if($dbtype=="MYSQL") {
function db_connect() {
	global $dbhost, $dbuser, $dbpass, $dbname;
	$c=mysql_connect($dbhost, $dbuser, $dbpass);
	mysql_select_db($dbname, $c);
	return $c;
}
function db_query($q,$c) { return mysql_query($q,$c); }
function db_escape_string($s,$c) { return mysql_real_escape_string($s,$c); }
function db_fetch_array($r) { return mysql_fetch_array($r); }
function db_num_rows($r) { return mysql_num_rows($r); }
function db_error($c) { return mysql_error($c); }
function db_close($c) { return mysql_close($c); }
function db_begin_tran($c) { return mysql_query("BEGIN",$c); }
function db_commit_tran($c) { return mysql_query("COMMIT",$c); }
function db_rollback_tran($c) { return mysql_query("ROLLBACK",$c); }
} elseif($dbtype=="SQLITE") {
/*
* SQL functions for Sqlite db type
*/
function db_connect() {
	global $dbfile;
	return sqlite_open($dbfile);
}
function db_query($q,$c) { return sqlite_query($q,$c); }
function db_escape_string($s,$c) { return sqlite_escape_string($s,$c); }
function db_fetch_array($r) { return sqlite_fetch_array($r); }
function db_num_rows($r) { return sqlite_num_rows($r); }
function db_error($c) { return sqlite_error_string(sqlite_last_error($c)); }
function db_close($c) { return sqlite_close($c); }
function db_begin_tran($c) { return true; }
function db_commit_tran($c) { return true; }
function db_rollback_tran($c) { return true; }
} else {
	die(_ERR_NODBTYPE);
}

function myescape($s)
{
	global $conn;

	if(!get_magic_quotes_gpc()) {
		return db_escape_string($s,$conn);
	}
	
	return $s;
}

/*
* Evaluate the permissions of $path
*/
function getFullPermissions($path)
{
	$perms = fileperms($path);

	if (($perms & 0xC000) == 0xC000) {
		// Socket
		$info = 's';
	} elseif (($perms & 0xA000) == 0xA000) {
		// Symbolic Link
		$info = 'l';
	} elseif (($perms & 0x8000) == 0x8000) {
		// Regular
		$info = '-';
	} elseif (($perms & 0x6000) == 0x6000) {
		// Block special
		$info = 'b';
	} elseif (($perms & 0x4000) == 0x4000) {
		// Directory
		$info = 'd';
	} elseif (($perms & 0x2000) == 0x2000) {
		// Character special
		$info = 'c';
	} elseif (($perms & 0x1000) == 0x1000) {
		// FIFO pipe
		$info = 'p';
	} else {
		// Unknown
		$info = 'u';
	}

	// Owner
	$info .= (($perms & 0x0100) ? 'r' : '-');
	$info .= (($perms & 0x0080) ? 'w' : '-');
	$info .= (($perms & 0x0040) ?
		(($perms & 0x0800) ? 's' : 'x' ) :
		(($perms & 0x0800) ? 'S' : '-'));

	// Group
	$info .= (($perms & 0x0020) ? 'r' : '-');
	$info .= (($perms & 0x0010) ? 'w' : '-');
	$info .= (($perms & 0x0008) ?
		(($perms & 0x0400) ? 's' : 'x' ) :
		(($perms & 0x0400) ? 'S' : '-'));

	// World
	$info .= (($perms & 0x0004) ? 'r' : '-');
	$info .= (($perms & 0x0002) ? 'w' : '-');
	$info .= (($perms & 0x0001) ?
		(($perms & 0x0200) ? 't' : 'x' ) :
		(($perms & 0x0200) ? 'T' : '-'));

	return $info;
} // getFullPermissions

function saveCat()
{
	echo "save";
}

function parseRange($range)
{
	$range=substr($range,6);
	$seppos=strpos($range, '-');
	$start=substr($range,0,$seppos);
	$end=substr($range,$seppos+1);
	return array($start,$end);
}

function upPath($path)
{
  $pos=strrpos($path, '/', -2);
  return substr($path, 0, $pos);
}

// search database for files matching the search criteria
function searchDb()
{
	global $conn;

        $nomefile=$_GET["nomefile"];
	$filename=str_replace("*", "%", $nomefile);
	$sqlwhere="files.category=categories.num AND files.disk=disks.name".
		" AND files.name LIKE '%$filename%'";

	$rs=db_query("SELECT COUNT(*) AS totalrows FROM files, categories, disks WHERE $sqlwhere", $conn);
	$row=db_fetch_array($rs);
	$totalrows=$row["totalrows"];

	$range=(isset($_SERVER["HTTP_RANGE"]) ? $_SERVER["HTTP_RANGE"]: "items=0-1000");
	list($start,$end)=parseRange($range);
	$rs=db_query("SELECT files.*, categories.name AS catname, categories.num AS catid,".
		" disks.iddisk AS iddisk FROM files, categories, disks".
		" WHERE $sqlwhere ORDER BY files.category, files.disk LIMIT $start,".($end-$start+1), $conn);
	$row=db_fetch_array($rs);
	$i=0;
	$data=array();
	while($row)
	{
		$diskid=$row["iddisk"];
		// spezza il percorso e genera i link
		$arpath=explode("/", $row["location"]);
		$location="<A HREF=\"?fnc=lista&diskid=$diskid&percorso=/\">/&nbsp;</A>&nbsp;";
		$strpath="/";
		for($i=1; $i<count($arpath)-1; $i++) { // salto il primo e l'ultimo perche' sempre vuoti /dir1/dir2/
			$strpath.=$arpath[$i]."/";
			$location.="<A HREF=\"?fnc=lista&diskid=$diskid&percorso=$strpath\">".$arpath[$i]."/&nbsp;</A>&nbsp;";
		}

		$d="{name: '".$row["name"]."', ";
		$d.="path: '".$location."', ";
		$d.="size: '".$row["size"]."', ";
		$d.="disk: '".$row["disk"]."', ";
		$d.="cat: '".$row["catname"]."', ";
//		echo "<TD><A HREF=\"?fnc=lista&diskid=$diskid\">".$row["disk"]."</A></TD>\n";
//		echo "<TD><A HREF=\"?fnc=dischi&catid=".$row["catid"]."\">".
//			$row["catname"]."</A></TD>\n";

		$d.="}";
		$data[]=$d;

		$row=db_fetch_array($rs);
		$i++;
	}

	// send data to client
	header("Content-Range: items $start-".($i-1)."/$totalrows");

	// open json data
	echo "[\n";
	// return data to client
	if(count($data)>0) {
		echo $data[0];
		for($i=1; $i<count($data); $i++)
			echo ",\n".$data[$i];
	}

	// close json data
	echo "\n]\n";

	return;
}

// load files and return json data
function loadFiles()
{
	global $conn, $RowsPerPage;

	$diskid=$_GET["diskid"];
	$rs=db_query("SELECT disks.name AS name, disks.volume AS volume, categories.name AS catname,".
		" categories.num AS catid".
		" FROM disks,categories WHERE disks.category=categories.num".
		" AND iddisk=".$diskid, $conn);
	$row=db_fetch_array($rs);
	$diskname=$row["name"];

	$path="/";
	if(isset($_GET["path"]))
		$path=$_GET["path"];

	$sqlwhere="disk='".$diskname."' AND category=".$row["catid"].
		" AND location='".$path."' AND (name<>'.' AND name<>'..')";

	$rs=db_query("SELECT COUNT(*) AS totalrows FROM files WHERE $sqlwhere", $conn);
	$row=db_fetch_array($rs);

	$totalrows=$row["totalrows"];

	$range=(isset($_SERVER["HTTP_RANGE"]) ? $_SERVER["HTTP_RANGE"]: "items=0-1000");
	list($start,$end)=parseRange($range);
	$rs=db_query("SELECT * FROM files WHERE $sqlwhere ORDER BY location, name LIMIT $start, ".($end-$start+1), $conn);
	$row=db_fetch_array($rs);
	$i=0;
	$data=array();
	// if at the top of the recordset and not at the root level, insert a record to change to parent directory
	if($start==0 && $path!="/") {
		$i++;
		$data[]="{name: \"..\", directory: true, path: \"".upPath($path)."/\", size: 0}";
	}
	while($row)
	{
		$d="{name: \"".$row["name"]."\", ";
		if($row["rights"][0]=='d') {
			$d.="directory: true, ";
			$d.="path: \"".$row["location"].$row["name"]."/\", ";
		} else {
			$d.="directory: false, ";
			$d.="path: \"\", ";
		}
		$d.="size: ".$row["size"]."";
/*
		if($row["rights"][0]=='d')
			echo "<TD class=\"$tdclass\"><A HREF=\"?fnc=lista&diskid=$diskid&percorso=$percorso".
				$row["name"]."/\">".$row["name"]."</A></TD>\n";
*/

		$d.='}';
		$data[]=$d;

		$i++;
		$row=db_fetch_array($rs);
	}

	// send data to client
	header("Content-Range: items $start-".($i-1)."/$totalrows");

	// open json data
	echo "[\n";
	// return data to client
	if(count($data)>0) {
		echo $data[0];
		for($i=1; $i<count($data); $i++)
			echo ",\n".$data[$i];
	}

	// close json data
	echo "\n]\n";

	return;
}

// load disks and return json data
function loadDisks()
{
	global $conn;

	// add 0 to prevent SQL injection
	$catid=$_GET["catid"]+0;

	// collect data

	// get the total number of records
	$rs=db_query("SELECT COUNT(*) AS numrows FROM disks WHERE category=$catid", $conn);
	$row=db_fetch_array($rs);
	if($row)
		$totalrows=$row["numrows"];

	// List categories
	list($start,$end)=parseRange($_SERVER["HTTP_RANGE"]);
	$rs=db_query("SELECT * FROM disks WHERE category=".$catid.
		" ORDER BY name LIMIT $start, ".($end-$start+1), $conn);
	$row=db_fetch_array($rs);
	$i=0;
	$data=array();
	while($row)
	{
		$d='{id: '.$row["iddisk"].', ';
		$d.='name: "'.$row["name"].'", ';
		$d.='num: "'.$row["num"].'", ';
		$d.='date: "'.$row["date"].'", ';
		$d.='label: "'.$row["volume"].'", ';
		$d.='usage: "'.$row["full"].'", ';
		$d.='free: "'.$row["free"].'", ';
		$d.='descr: "'.$row["description"].'", ';
		$d.="modifyUrl: '<A HREF=\"?fnc=dischi&act=modifica&catid=".
			"$catid&diskid=".$row["iddisk"]."\">"._STR_MODIFY."</A>', ";
		$d.="deleteUrl: '<A HREF=\"?fnc=dischi&act=elimina&catid=$catid&diskid=".
			$row["iddisk"]."\" OnClick=\"return confirm(\'".
			_STR_DISK_CONFIRM_DELETE."?\');\">"._STR_DELETE."</A>'";
/*
		$d.="modifyUrl: '<A HREF=\"?fnc=categorie&act=modifica&catid=".
				$row["num"]."\">"._STR_MODIFY."</A>', ";
		// controlla  se la categoria e' vuota
		$rsfull=db_query("SELECT * FROM disks WHERE category=".$row["num"], $conn);
		$rowfull=db_fetch_array($rsfull);
		if($rowfull===FALSE) {
			$d.="deleteUrl: '<A HREF=\"?fnc=categorie&act=elimina&catid=".
				$row["num"]."\">"._STR_DELETE."</A>'";
		} else {
			$d.="deleteUrl: '"._STR_DELETE."'";
		}
*/
		$d.='}';
		$data[]=$d;
/*
		echo "<TD class=\"$tdclass\"><A HREF=\"?fnc=dischi&act=modifica&catid=$catid&diskid=".
			$row["iddisk"]."\">"._STR_MODIFY."</A></TD>\n";
		echo "<TD class=\"$tdclass\"><A HREF=\"?fnc=dischi&act=elimina&catid=$catid&diskid=".
			$row["iddisk"]."\" OnClick=\"return confirm('".
			_STR_DISK_CONFIRM_DELETE."?');\">"._STR_DELETE."</A></TD>\n";
*/

		$row=db_fetch_array($rs);
		$i++;
	}

	// send data to client
	header("Content-Range: items $start-".($i-1)."/$totalrows");

	// open json data
	echo "[\n";
	// return data to client
	if(count($data)>0) {
		echo $data[0];
		for($i=1; $i<count($data); $i++)
			echo ",\n".$data[$i];
	}

	// close json data
	echo "\n]\n";

	return;
}


// load Categories data and send them to client as a json page
function loadCat()
{
	global $conn;

	// collect data

	// get the total number of records
	$rs=db_query("SELECT COUNT(*) AS numrows FROM categories", $conn);
	$row=db_fetch_array($rs);
	if($row)
		$totalrows=$row["numrows"];

	// List categories
	$range=isset($_SERVER["HTTP_RANGE"]) ? $_SERVER["HTTP_RANGE"]: "items 0-$totalrows" ;
	list($start,$end)=parseRange($range);
	$rs=db_query("SELECT * FROM categories ORDER BY name LIMIT $start, ".($end-$start+1), $conn);
	$row=db_fetch_array($rs);
	$i=0;
	$data=array();
	while($row)
	{
		$d='{"id": '.$row["num"].', ';
		$d.='"name": "'.$row["name"].'", ';
		$d.='"descr": "'.$row["description"].'", ';
		$d.="\"modifyUrl\": \"<A HREF=\\\"?fnc=categorie&act=modifica&catid=".
				$row["num"]."\\\">"._STR_MODIFY."</A>\", ";
		// controlla  se la categoria e' vuota
		$rsfull=db_query("SELECT * FROM disks WHERE category=".$row["num"], $conn);
		$rowfull=db_fetch_array($rsfull);
		if($rowfull===FALSE) {
			$d.="\"deleteUrl\": \"<A HREF=\\\"?fnc=categorie&act=elimina&catid=".
				$row["num"]."\\\">"._STR_DELETE."</A>\"";
		} else {
			$d.="\"deleteUrl\": \""._STR_DELETE."\"";
		}
		$d.='}';
		$data[]=$d;

		$row=db_fetch_array($rs);
		$i++;
	}

	// send data to client
	header("Content-Range: items $start-".($i-1)."/$totalrows");

	// open json data
	//echo "{\"totalRecords\": $totalrows, \"records\":";
	echo "[\n";
	echo $data[0];
	for($i=1; $i<count($data); $i++)
		echo ",\n".$data[$i];
	// close json data
	echo "\n]";
	//echo "}";
	echo "\n";

	return;
}

$fnc=$_GET["fnc"];

$conn=db_connect();

if($fnc=="catmanager") {
	switch($_SERVER["REQUEST_METHOD"]) {
		case "GET":
			loadCat();
			break;
		case "PUT":
			saveCat();
			break;
		default:
			echo _ERR_FUNC;
			break;
	}
}

if($fnc=="diskmanager") {
	switch($_SERVER["REQUEST_METHOD"]) {
		case "GET":
			loadDisks();
			break;
		default:
			echo _ERR_FUNC;
			break;
	}
}

if($fnc=="filemanager") {
	switch($_SERVER["REQUEST_METHOD"]) {
		case "GET":
			loadFiles();
			break;
		default:
			echo _ERR_FUNC;
			break;
	}
}

if($fnc=="resmanager") {
	switch($_SERVER["REQUEST_METHOD"]) {
		case "GET":
			searchDb();
			break;
		default:
			echo _ERR_FUNC;
			break;
	}
}

//echo $_SERVER["REQUEST_METHOD"]."\n";
//phpinfo();

db_close($conn);

?>

Return current item: PWhere