summaryrefslogtreecommitdiff
path: root/public_html/class/vfsdata.php
blob: 1d35384d6794bff2f65d1c5677c22f185a749afe (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php

class vfsdata {

	protected $folder_path;
	protected $current_user;
	protected $userid;

	public function __construct($user, $folderpath){
		$this->current_user = $user;
		$this->folder_path  = $folderpath;

		return true;	
	}

	public function setFolderPath($folder){
		$this->folder_path = $folder;
	}

	public function setCurrentUser($user){
		$this->current_user = $user;
	}

	protected function _getRoot($user, $folderpath, $visbility){
		$folder_array = explode('/', $this->folder_path);

		$sql 	 = $vfsdb->prepare("SELECT id FROM files WHERE parent=0 AND type='DIRECTORY' and owner = %s " . $visibility . ";", $this->owner);
		$root_db = $vfsdb->doQuery($sql);
		$root_ar = $root_db->fetch_array(MYSQLI_ASSOC);
		$root_id = $root_ar["id"];

		return $root_id;
	}

	public function getId($user = NULL, $folder = NULL){
		global $vfsuser;

		if ( is_null($user) )
			$user = $this->current_user;

		if ( is_null($folder) )
			$folder = $this->folder_path;

		$this->userid = $vfsuser->getUserId();

		# checks if the current user is the actual owner
		if ( $vfsuser->isLoggedIn() && $_SESSION["userid"] == $this->userid )
			$visibility = "";
		else
			$visibility = " AND visibility='PUBLIC'";

		$tmp_length = count($folder);

		if ( empty($folder[$tmp_length-1]) )
			$length = $tmp_length-1;
		else
			$length = $tmp_length;

		$parentdir = $this->_getRoot($user, $folder, $visibility);

		if ( empty($folder[0]) )
			return $parentdir;

		for ( $i=0; $i<$length, $i++ ) {
			$sql = $vfsdb->prepare("SELECT id, parent FROM files WHERE owner = %d AND parent = %d AND name = %s ".$visibility. ";", $this->userid, $parentdir, $folder[$i]);
			$parentdir_db = $vfsdb->doQuery($sql);

			$prim_id = $parentdir_db->fetch_array(MYSQLI_ASSOC);

			if ( $parentdir != $prim_id["parent"] )
				return false;

			$parentdir = $prim_id["parent"];
		}

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