summaryrefslogtreecommitdiff
path: root/www/functions/func_select.php
blob: af7b239451172c406d5999d339efcd7faaa255f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
function select_file_id($db, $owner, $folder_path){

	if($_SESSION["login"] && $_SESSION["userid"] == $owner){ 	// TODO: Check if loged in user really the user who does the query - fix 12.3.14
		$share="";						// to print all files, even hidden ones
	} else {
		$share =" AND share='PUBLIC'";				// just use files with the correct permissions
	}

	$folder_array_unsafe = explode("/",$folder_path);
	$length = count($folder_array_unsafe);

	$root_db = $db->query("SELECT id FROM files WHERE parent=0 AND owner=" . SQLite3::escapeString($owner) . " AND folder='DIRECTORY' " . $share . ";");
	$root_ar = $root_db->fetchArray(SQLITE3_NUM);
	$root_id = $root_ar[0];
	if(empty($root_ar[0])){
		failure("Seems like the user doesn't want to show his tree: " . $root_id);
	}
	$parentdir = SQLite3::escapeString($root_id);

	if(empty($folder_array_unsafe[0])){
		return $root_id;		// returns the primary key from the root dir
	}

	for($i=0; $i<$length; $i++){

		$parentdir_db = $db->query("SELECT id, parent FROM files WHERE owner=" . $owner . $share . " AND parent=" . $parentdir . " AND name='" . SQLite3::escapeString($folder_array_unsafe[$i]) . "';");

		$prim_id = $parentdir_db->fetchArray(SQLITE3_NUM);

		if(empty($prim_id[0])){
			return $parentdir;	//TODO; Return false because file not found
		}

		if($parentdir != $prim_id[1]){

			$wrong_folder = $folder_array_unsafe[$i];
			$working_path[0] = $wrong_folder;

			for($j=0; $j<$i; $j++){
				$working_path[$j] = $folder_array_unsafe[$j];
			}

			get_404($working_path, $wrong_folder);
			return false;
		}

		$parentdir = $prim_id[0];
	}

	return $parentdir;		// returns the primary key from the last entry in the folder array

}