forked from projectsend/projectsend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.php
More file actions
255 lines (217 loc) · 7.28 KB
/
common.php
File metadata and controls
255 lines (217 loc) · 7.28 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
/**
* Common information used on all clients templates.
* Avoids the need to define all of this when creating a new template.
*
* @package ProjectSend
* @subpackage Templates
*/
/**
* Since the header.php file is shared between the back-end and the
* templates, it's necessary to define the allowed levels, or else
* the files list will not be available.
*/
$allowed_levels = array(9,8,7,0);
include_once ROOT_DIR.DS.'templates'.DS.'session_check.php';
$template = get_option('selected_clients_template');
/**
* Loads a language file from the current template folder based on
* the system options.
*/
$lang = (isset($_SESSION['lang'])) ? $_SESSION['lang'] : SITE_LANG;
if(!isset($ld)) { $ld = 'cftp_admin'; }
$mo_file = ROOT_DIR.DS."templates".DS.$template.DS."lang".DS."{$lang}.mo";
ProjectSend\Classes\I18n::LoadDomain($mo_file, $ld);
$this_template = BASE_URI.'templates/'.$template.'/';
/**
* URI to the default template CSS file.
*/
if (file_exists(ROOT_DIR.DS."templates".DS.$template.DS."main.css")) {
$this_template_css = BASE_URI.'templates/'.$template.'/main.css';
}
global $dbh;
/**
* Get all the client's information
*/
$client_info = get_client_by_username($this_user);
/**
* Get the list of different groups the client belongs to.
*/
$get_groups = new ProjectSend\Classes\MembersActions;
$found_groups = $get_groups->client_get_groups([
'client_id' => $client_info['id'],
'return' => 'list',
]);
/**
* Define the arrays so they can't be empty
*/
$found_all_files_array = array();
/**
* Get files uploaded by this user
*/
$files_query = "SELECT * FROM " . TABLE_FILES . " WHERE user_id=:user_id";
$files_sql = $dbh->prepare($files_query);
$files_sql->bindParam(':user_id', $client_info['id'], PDO::PARAM_INT);
$files_sql->execute();
$files_sql->setFetchMode(PDO::FETCH_ASSOC);
while ( $row_files = $files_sql->fetch() ) {
$found_all_files_array[] = $row_files['id'];
}
/**
* Get files assigned directly to the client
* Construct the query first.
*/
$files_query = "SELECT id, file_id, client_id, group_id FROM " . TABLE_FILES_RELATIONS . " WHERE (client_id = :id";
if (!empty($found_groups)) {
$files_query .= " OR FIND_IN_SET(group_id, :groups)";
}
$files_query .= ") AND hidden = '0'";
$files_sql = $dbh->prepare($files_query);
$files_sql->bindParam(':id', $client_info['id'], PDO::PARAM_INT);
if (!empty($found_groups)) {
$files_sql->bindParam(':groups', $found_groups);
}
$files_sql->execute();
$files_sql->setFetchMode(PDO::FETCH_ASSOC);
while ( $row_files = $files_sql->fetch() ) {
if (!is_null($row_files['client_id'])) {
$found_all_files_array[] = $row_files['file_id'];
}
if (!is_null($row_files['group_id'])) {
$found_all_files_array[] = $row_files['file_id'];
}
}
$found_unique_files_ids = implode(',', array_unique($found_all_files_array));
/**
* Make an array of the categories containing the
* files found for this account.
*/
$cat_ids = array();
$file_ids = array();
$files_keep = array();
$sql_sentence = "SELECT file_id, cat_id FROM " . TABLE_CATEGORIES_RELATIONS . " WHERE FIND_IN_SET(file_id, :files)";
$sql_client_categories = $dbh->prepare( $sql_sentence );
$sql_client_categories->bindParam(':files', $found_unique_files_ids);
$sql_client_categories->execute();
$sql_client_categories->setFetchMode(PDO::FETCH_ASSOC);
while ( $row = $sql_client_categories->fetch() ) {
$cat_ids[$row['cat_id']] = $row['cat_id'];
$files_keep[$row['file_id']][] = $row['cat_id'];
}
if ( !empty( $cat_ids ) ) {
$get_categories = get_categories(
array(
'id' => $cat_ids,
'is_tree' => true
)
);
}
/**
* With the categories generated, keep only the files
* that are assigned to the selected one.
*/
if ( !empty( $category_filter ) && $category_filter != '0' ) {
$filtered_file_ids = [];
foreach ( $files_keep as $keep_file_id => $keep_cat_ids ) {
if ( in_array( $category_filter, $keep_cat_ids ) ) {
$filtered_file_ids[] = $keep_file_id;
}
}
$ids_to_search = implode(',', $filtered_file_ids);
}
else {
$ids_to_search = $found_unique_files_ids;
}
/** Create the files list */
$available_files = [];
$my_files = [];
if (!empty($found_all_files_array)) {
$f = 0;
$files_query = "SELECT * FROM " . TABLE_FILES . " WHERE FIND_IN_SET(id,:search_ids)";
$params = array(
':search_ids' => $ids_to_search,
);
/** Add the search terms */
if ( isset($_GET['search']) && !empty($_GET['search']) ) {
$files_query .= " AND (filename LIKE :title OR description LIKE :description)";
$no_results_error = 'search';
$params[':title'] = '%'.$_GET['search'].'%';
$params[':description'] = '%'.$_GET['search'].'%';
}
/**
* Add the order.
* Defaults to order by: timestamp, order: DESC (shows last uploaded files first)
*/
$files_query .= sql_add_order( TABLE_FILES, 'timestamp', 'desc' );
/**
* Pre-query to count the total results
*/
$count_sql = $dbh->prepare( $files_query );
$count_sql->execute($params);
$count_for_pagination = 0;
while ( $data = $count_sql->fetch() ) {
/** Does it expire? */
$counts = true;
if ($data['expires'] == '1') {
if (time() > strtotime($data['expiry_date'])) {
if (get_option('expired_files_hide') == '1') {
$counts = false;
}
}
}
if ($counts) { $count_for_pagination++; }
}
//$count_for_pagination = $count_sql->rowCount();
/**
* Repeat the query but this time, limited by pagination
*/
if (TEMPLATE_RESULTS_PER_PAGE > 0) {
$files_query .= " LIMIT :limit_start, :limit_number";
}
$sql_files = $dbh->prepare( $files_query );
if (TEMPLATE_RESULTS_PER_PAGE > 0) {
$pagination_page = ( isset( $_GET["page"] ) ) ? $_GET["page"] : 1;
$pagination_start = ( $pagination_page - 1 ) * TEMPLATE_RESULTS_PER_PAGE;
$params[':limit_start'] = $pagination_start;
$params[':limit_number'] = TEMPLATE_RESULTS_PER_PAGE;
}
$sql_files->execute( $params );
$count = $sql_files->rowCount();
$sql_files->setFetchMode(PDO::FETCH_ASSOC);
while ( $data = $sql_files->fetch() ) {
$add_file = true;
$expired = false;
/** Does it expire? */
if ($data['expires'] == '1') {
if (time() > strtotime($data['expiry_date'])) {
if (get_option('expired_files_hide') == '1') {
$add_file = false;
}
$expired = true;
}
}
/** Make the list of files */
if ($add_file == true) {
$available_files[] = $data['id'];
// Leaving this here in case a custom template is using this array
$pathinfo = pathinfo($data['url']);
$my_files[$f] = array(
//'origin' => $origin,
'id' => $data['id'],
'url' => $data['url'],
'save_as' => (!empty( $data['original_url'] ) ) ? $data['original_url'] : $data['url'],
'extension' => strtolower($pathinfo['extension']),
'name' => $data['filename'],
'description' => $data['description'],
'timestamp' => $data['timestamp'],
'expires' => $data['expires'],
'expiry_date' => $data['expiry_date'],
'expired' => $expired,
);
// End deprecated array block
$f++;
}
}
}
/** Get the url for the logo from "Branding" */
$logo_file_info = generate_logo_url();