-
Notifications
You must be signed in to change notification settings - Fork 61
/
check_structure.php
151 lines (130 loc) · 5.66 KB
/
check_structure.php
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/**
* Licensed to The Apereo Foundation under one or more contributor license
* agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
* The Apereo Foundation licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
*
* Check structure of the workspace of a user
*
* @author Tom Reijnders
* @version 1.0
* @package
*/
require_once(dirname(__FILE__) . "/config.php");
require_once($xerte_toolkits_site->php_library_path . "user_library.php");
function require_auth() {
global $xerte_toolkits_site;
#TODO use hash for authentication
header('Cache-Control: no-cache, must-revalidate, max-age=0');
$has_supplied_credentials = !(empty($_SERVER['PHP_AUTH_USER']) && empty($_SERVER['PHP_AUTH_PW']));
$is_not_authenticated = (
!$has_supplied_credentials ||
$_SERVER['PHP_AUTH_USER'] != $xerte_toolkits_site->admin_username ||
hash('sha256', $_SERVER['PHP_AUTH_PW']) != $xerte_toolkits_site->admin_password);
if ($is_not_authenticated) {
header('HTTP/1.1 401 Authorization Required');
header('WWW-Authenticate: Basic realm="Access denied"');
header('WWW-Authenticate: Basic realm="Check structure of ' . $xerte_toolkits_site->site_name . '"');
header('HTTP/1.0 401 Unauthorized');
echo '{"error" : "You do not have permission to retrieve this information"}';
exit;
}
return true;
}
// Authentication
$full_access = false;
// Admin user
if (is_user_admin()){
$full_access = true;
}
else
{
$full_access = require_auth();
}
function checkOrphan($folder, $folders)
{
$folder_parent = $folder['folder_parent'];
if ($folder_parent == 0) {
return false;
}
$folder_is_orphan = true;
foreach ($folders as $folder) {
if ($folder['folder_id'] == $folder_parent) {
$folder_is_orphan = false;
}
}
return $folder_is_orphan;
}
function getFolderDetails($folder)
{
global $xerte_toolkits_site;
$sql = "SELECT * FROM {$xerte_toolkits_site->database_table_prefix}folderdetails fd, logindetails ld WHERE fd.folder_id = ? and fd.login_id = ld.login_id";
$params = array($folder['folder_id']);
$folderdetails = db_query_one($sql, $params);
$sql = "SELECT * FROM {$xerte_toolkits_site->database_table_prefix}folderdetails fd, logindetails ld WHERE fd.folder_id = ? and fd.login_id = ld.login_id";
$params = array($folder['folder_parent']);
$parent_folder_details = db_query_one($sql, $params);
$details = array();
$details['folder_name'] = $folderdetails['folder_name'];
$details['role'] = $folder['role'];
$details['folder_owner'] = $folderdetails['firstname'] . " " . $folderdetails['surname'] . "(" . $folderdetails['username'] . ")";
$details['folder_parent'] = $parent_folder_details['folder_name'] . "(" . $parent_folder_details['folder_id'] . ")";
$details['folder_parent_owner'] = $parent_folder_details['firstname'] . " " . $parent_folder_details['surname'] . "(" . $parent_folder_details['username'] . ")";
return $details;
}
$fix = false;
if (isset($_GET['fix'])) {
$fix = true;
}
// Get the user id
if (isset($_GET['username'])) {
$username = x_clean_input($_GET['username']);
// Get the user id
$rowid = $row = db_query_one("SELECT login_id FROM {$xerte_toolkits_site->database_table_prefix}logindetails WHERE username = ?", array($username));
$user_id = $row['login_id'];
// Get root folder of user $user_id
$root_folder = get_user_root_folder_id_by_id($user_id);
// Get all the folders the user has access to
$sql = "SELECT * FROM {$xerte_toolkits_site->database_table_prefix}folderrights WHERE login_id = ?";
$params = array($user_id);
$folders = db_query($sql, $params);
$found = false;
foreach ($folders as $folder) {
$folder_is_orphan = checkOrphan($folder, $folders);
if ($folder_is_orphan) {
$found = true;
echo "Orphaned folder: " . $folder['folder_id'] . "<br>";
$details = getFolderDetails($folder);
echo "Details: <br><ul>";
echo "<li>Folder name : " . $details['folder_name'] . "</li>";
echo "<li>Role : " . $details['role'] . "</li>";
echo "<li>Folder shared by : " . $details['folder_owner'] . "</li>";
echo "<li>Folder parent : " . $details['folder_parent'] . "</li>";
echo "<li>Folder parent owner : " . $details['folder_parent_owner'] . "</li></ul>";
if ($fix)
{
echo "Fixing orphaned folder: {$details['folder_name']} (id={$folder['folder_id']}) and placing it at the root folder of user {$username} (id={$root_folder}) <br>";
$sql = "update {$xerte_toolkits_site->database_table_prefix}folderrights set folder_parent=? WHERE folder_id = ? and login_id = ?";
$params = array($root_folder, $folder['folder_id'], $user_id);
db_query($sql, $params);
echo "Orphaned folder fixed: " . $folder['folder_id'] . "<br>";
}
}
}
if (!$found) {
echo "No orphaned folders found for user {$username}";
}
}