-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit-file.php
executable file
·574 lines (512 loc) · 21.1 KB
/
edit-file.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
<?php
/**
* Edit a file name or description.
* Files can only be edited by the uploader and level 9 or 8 users.
*
* @package ProjectSend
*/
define('IS_FILE_EDITOR', true);
$load_scripts = array(
'datepicker',
'chosen',
'ckeditor'
);
$allowed_levels = array(9,8,7,0);
require_once('sys.includes.php');
//Add a session check here
if(!check_for_session()) {
header("location:" . BASE_URI . "index.php");
}
$active_nav = 'files';
$page_title = __('Edit file','cftp_admin');
include('header.php');
define('CAN_INCLUDE_FILES', true);
/**
* The file's id is passed on the URI.
*/
if (!empty($_GET['file_id'])) {
$this_file_id = $_GET['file_id'];
}
/** Fill the users array that will be used on the notifications process */
$users = array();
$statement = $dbh->prepare("SELECT id, name, level FROM " . TABLE_USERS . " ORDER BY name ASC");
$statement->execute();
$statement->setFetchMode(PDO::FETCH_ASSOC);
while( $row = $statement->fetch() ) {
$users[$row["id"]] = $row["name"];
if ($row["level"] == '0') {
$clients[$row["id"]] = $row["name"];
}
}
/** Fill the groups array that will be used on the form */
$groups = array();
$statement = $dbh->prepare("SELECT id, name FROM " . TABLE_GROUPS . " ORDER BY name ASC");
$statement->execute();
$statement->setFetchMode(PDO::FETCH_ASSOC);
while( $row = $statement->fetch() ) {
$groups[$row["id"]] = $row["name"];
}
/** Fill the categories array that will be used on the form */
$categories = array();
$get_categories = get_categories();
/**
* Get the user level to determine if the uploader is a
* system user or a client.
*/
$current_level = get_current_user_level();
//echo '<pre>'; print_r($_POST); echo '</pre>'; // DEBUG
?>
<div class="col-xs-12">
<?php
/**
* Show an error message if no ID value is passed on the URI.
*/
if(empty($this_file_id)) {
$no_results_error = 'no_id_passed';
}
else {
$sql = $dbh->prepare("SELECT * FROM " . TABLE_FILES . " WHERE id = :id");
$sql->bindParam(':id', $this_file_id, PDO::PARAM_INT);
$sql->execute();
/**
* Count the files assigned to this client. If there is none, show
* an error message.
*/
$count = $sql->rowCount();
if ( $count == 0 ) {
$no_results_error = 'id_not_exists';
}
/**
* Continue if client exists and has files under his account.
*/
$sql->setFetchMode(PDO::FETCH_ASSOC);
while( $row = $sql->fetch() ) {
$edit_file_info['url'] = $row['url'];
$edit_file_info['id'] = $row['id'];
$edit_file_allowed = array(7,0);
if (in_session_or_cookies($edit_file_allowed)) {
if ($row['uploader'] != $global_user) {
$no_results_error = 'not_uploader';
}
}
}
}
/** Show the error if it is defined */
if (isset($no_results_error)) {
switch ($no_results_error) {
case 'no_id_passed':
$no_results_message = __('Please go to the clients or groups administration page, select "Manage files" from any client and then click on "Edit" on any file to return here.','cftp_admin');
break;
case 'id_not_exists':
$no_results_message = __('There is not file with that ID number.','cftp_admin');
break;
case 'not_uploader':
$no_results_message = __("You don't have permission to edit this file.",'cftp_admin');
break;
}
?>
<div class="whiteform whitebox whitebox_text">
<?php echo $no_results_message; ?>
</div>
<?php
}
else {
/**
* See what clients or groups already have this file assigned.
*/
$file_on_clients = array();
$file_on_groups = array();
if ( isset($_POST['submit'] ) ) {
$assignments = $dbh->prepare("SELECT file_id, client_id, group_id FROM " . TABLE_FILES_RELATIONS . " WHERE file_id = :id");
$assignments->bindParam(':id', $this_file_id, PDO::PARAM_INT);
$assignments->execute();
if ($assignments->rowCount() > 0) {
while ( $assignment_row = $assignments->fetch() ) {
if (!empty($assignment_row['client_id'])) {
$file_on_clients[] = $assignment_row['client_id'];
}
elseif (!empty($assignment_row['group_id'])) {
$file_on_groups[] = $assignment_row['group_id'];
}
}
}
$n = 0;
foreach ($_POST['file'] as $file) {
$n++;
if(!empty($file['name'])) {
/**
* If the uploader is a client, set the "client" var to the current
* uploader username, since the "client" field is not posted.
*/
if ($current_level == 0) {
$file['assignments'] = 'c'.$global_user;
}
$this_upload = new PSend_Upload_File();
/**
* Unassigned files are kept as orphans and can be related
* to clients or groups later.
*/
/** Add to the database for each client / group selected */
$add_arguments = array(
'file_original' => $edit_file_info['url'],
'name' => $file['name'],
'description' => $file['description'],
'uploader' => $global_user,
'uploader_id' => CURRENT_USER_ID,
'expiry_date' => $file['expiry_date']
);
/** Set notifications to YES by default */
$send_notifications = true;
if (!empty($file['hidden'])) {
$add_arguments['hidden'] = $file['hidden'];
$send_notifications = false;
}
if ($current_level != 0) {
if (!empty($file['expires'])) {
$add_arguments['expires'] = '1';
}
if (!empty($file['public'])) {
$add_arguments['public'] = '1';
}
if (!empty($file['assignments'])) {
/**
* Remove already assigned clients/groups from the list.
* Only adds assignments to the NEWLY selected ones.
*/
$full_list = $file['assignments'];
foreach ($file_on_clients as $this_client) { $compare_clients[] = 'c'.$this_client; }
foreach ($file_on_groups as $this_group) { $compare_groups[] = 'g'.$this_group; }
if (!empty($compare_clients)) {
$full_list = array_diff($full_list,$compare_clients);
}
if (!empty($compare_groups)) {
$full_list = array_diff($full_list,$compare_groups);
}
$add_arguments['assign_to'] = $full_list;
/**
* On cleaning the DB, only remove the clients/groups
* That just have been deselected.
*/
$clean_who = $file['assignments'];
}
else {
$clean_who = 'All';
}
/** CLEAN deletes the removed users/groups from the assignments table */
if ($clean_who == 'All') {
$clean_all_arguments = array(
'owner_id' => CURRENT_USER_ID, /** For the log */
'file_id' => $this_file_id,
'file_name' => $file['name']
);
$clean_assignments = $this_upload->clean_all_assignments($clean_all_arguments);
}
else {
$clean_arguments = array (
'owner_id' => CURRENT_USER_ID, /** For the log */
'file_id' => $this_file_id,
'file_name' => $file['name'],
'assign_to' => $clean_who,
'current_clients' => $file_on_clients,
'current_groups' => $file_on_groups
);
$clean_assignments = $this_upload->clean_assignments($clean_arguments);
}
$categories_arguments = array(
'file_id' => $this_file_id,
'categories' => !empty( $file['categories'] ) ? $file['categories'] : '',
);
$this_upload->upload_save_categories( $categories_arguments );
}
/** Uploader is a client */
if ($current_level == 0) {
$add_arguments['assign_to'] = array('c'.CURRENT_USER_ID);
$add_arguments['hidden'] = '0';
$add_arguments['uploader_type'] = 'client';
$action_log_number = 33;
}
else {
$add_arguments['uploader_type'] = 'user';
$action_log_number = 32;
}
/**
* 1- Add the file to the database
*/
$process_file = $this_upload->upload_add_to_database($add_arguments);
if($process_file['database'] == true) {
$add_arguments['new_file_id'] = $process_file['new_file_id'];
$add_arguments['all_users'] = $users;
$add_arguments['all_groups'] = $groups;
if ($current_level != 0) {
/**
* 2- Add the assignments to the database
*/
$process_assignment = $this_upload->upload_add_assignment($add_arguments);
/**
* 3- Hide for everyone if checked
*/
if (!empty($file['hideall'])) {
$this_file = new FilesActions();
$hide_file = $this_file->hide_for_everyone($this_file_id);
}
/**
* 4- Add the notifications to the database
*/
if ($send_notifications == true) {
$process_notifications = $this_upload->upload_add_notifications($add_arguments);
}
}
$new_log_action = new LogActions();
$log_action_args = array(
'action' => $action_log_number,
'owner_id' => CURRENT_USER_ID,
'owner_user' => $global_user,
'affected_file' => $process_file['new_file_id'],
'affected_file_name' => $file['name']
);
$new_record_action = $new_log_action->log_action_save($log_action_args);
$msg = __('The file has been edited succesfuly.','cftp_admin');
echo system_message('ok',$msg);
include(ROOT_DIR.'/upload-send-notifications.php');
}
}
}
}
/** Validations OK, show the editor */
?>
<form action="edit-file.php?file_id=<?php echo filter_var($this_file_id,FILTER_VALIDATE_INT); ?>" method="post" name="edit_file" id="edit_file">
<div class="container-fluid">
<?php
/** Reconstruct the current assignments arrays */
$file_on_clients = array();
$file_on_groups = array();
$assignments = $dbh->prepare("SELECT file_id, client_id, group_id FROM " . TABLE_FILES_RELATIONS . " WHERE file_id = :id");
$assignments->bindParam(':id', $this_file_id, PDO::PARAM_INT);
$assignments->execute();
if ($assignments->rowCount() > 0) {
while ( $assignment_row = $assignments->fetch() ) {
if (!empty($assignment_row['client_id'])) {
$file_on_clients[] = $assignment_row['client_id'];
}
elseif (!empty($assignment_row['group_id'])) {
$file_on_groups[] = $assignment_row['group_id'];
}
}
}
/** Get the current assigned categories */
$current_categories = array();
$current_statemente = $dbh->prepare("SELECT cat_id FROM " . TABLE_CATEGORIES_RELATIONS . " WHERE file_id = :id");
$current_statemente->bindParam(':id', $this_file_id, PDO::PARAM_INT);
$current_statemente->execute();
if ($current_statemente->rowCount() > 0) {
while ( $assignment_row = $current_statemente->fetch() ) {
$current_categories[] = $assignment_row['cat_id'];
}
}
$i = 1;
$statement = $dbh->prepare("SELECT * FROM " . TABLE_FILES . " WHERE id = :id");
$statement->bindParam(':id', $this_file_id, PDO::PARAM_INT);
$statement->execute();
while( $row = $statement->fetch() ) {
$file_name_title = (!empty( $row['original_url'] ) ) ? $row['original_url'] : $row['url'];
?>
<div class="file_editor <?php if ($i%2) { echo 'f_e_odd'; } ?>">
<div class="row">
<div class="col-sm-12">
<div class="file_number">
<p><span class="glyphicon glyphicon-saved" aria-hidden="true"></span><?php echo html_output($file_name_title); ?></p>
</div>
</div>
</div>
<div class="row edit_files">
<div class="col-sm-12">
<div class="row edit_files_blocks">
<div class="<?php echo ($global_level != 0 || CLIENTS_CAN_SET_EXPIRATION_DATE == '1') ? 'col-sm-6 col-md-3' : 'col-sm-12 col-md-12'; ?> column">
<div class="file_data">
<div class="row">
<div class="col-sm-12">
<h3><?php _e('File information', 'cftp_admin');?></h3>
<div class="form-group">
<label><?php _e('Title', 'cftp_admin');?></label>
<input type="text" name="file[<?php echo $i; ?>][name]" value="<?php echo html_output($row['filename']); ?>" class="form-control file_title" placeholder="<?php _e('Enter here the required file title.', 'cftp_admin');?>" />
</div>
<div class="form-group">
<label><?php _e('Description', 'cftp_admin');?></label>
<textarea name="file[<?php echo $i; ?>][description]" class="<?php if ( DESCRIPTIONS_USE_CKEDITOR == 1 ) { echo 'ckeditor'; } ?> form-control" placeholder="<?php _e('Optionally, enter here a description for the file.', 'cftp_admin');?>"><?php echo (!empty($row['description'])) ? html_output($row['description']) : ''; ?></textarea>
</div>
</div>
</div>
</div>
</div>
<?php
/** The following options are available to users or client if clients_can_set_expiration_date set. */
if ($global_level != 0 || CLIENTS_CAN_SET_EXPIRATION_DATE == '1' ) {
?>
<div class="col-sm-6 col-md-3 column_even column">
<div class="file_data">
<?php
/**
* Only show the EXPIRY options if the current
* uploader is a system user or client if clients_can_set_expiration_date is set.
*/
if (!empty($row['expiry_date'])) {
$expiry_date = date('d-m-Y', strtotime($row['expiry_date']));
}
?>
<h3><?php _e('Expiration date', 'cftp_admin');?></h3>
<div class="form-group">
<label for="file[<?php echo $i; ?>][expires_date]"><?php _e('Select a date', 'cftp_admin');?></label>
<div class="input-group date-container">
<input type="text" class="date-field form-control datapick-field" readonly id="file[<?php echo $i; ?>][expiry_date]" name="file[<?php echo $i; ?>][expiry_date]" value="<?php echo (!empty($expiry_date)) ? $expiry_date : date('d-m-Y'); ?>" />
<div class="input-group-addon">
<i class="glyphicon glyphicon-time"></i>
</div>
</div>
</div>
<div class="checkbox">
<label for="exp_checkbox">
<input type="checkbox" id="exp_checkbox" name="file[<?php echo $i; ?>][expires]" value="1" <?php if ($row['expires']) { ?>checked="checked"<?php } ?> /> <?php _e('File expires', 'cftp_admin');?>
</label>
</div>
<?php
/** The following options are available to users only */
if ($global_level != 0) {
?>
<div class="divider"></div>
<h3><?php _e('Public downloading', 'cftp_admin');?></h3>
<div class="checkbox">
<label for="pub_checkbox">
<input type="checkbox" id="pub_checkbox" name="file[<?php echo $i; ?>][public]" value="1" <?php if ($row['public_allow']) { ?>checked="checked"<?php } ?> /> <?php _e('Allow public downloading of this file.', 'cftp_admin');?>
</label>
</div>
<div class="divider"></div>
<h3><?php _e('Public URL', 'cftp_admin');?></h3>
<div class="public_url">
<div class="form-group">
<textarea class="form-control" readonly><?php echo BASE_URI; ?>download.php?id=<?php echo $row['id']; ?>&token=<?php echo html_output($row['public_token']); ?></textarea>
</div>
</div>
<?php
} /** Close $current_level check */
?>
</div>
</div>
<?php
} /** Close $current_level check */
?>
<?php
/** The following options are available to users only */
if ($global_level != 0) {
?>
<div class="col-sm-6 col-md-3 assigns column">
<div class="file_data">
<?php
/**
* Only show the CLIENTS select field if the current
* uploader is a system user, and not a client.
*/
?>
<h3><?php _e('Assignations', 'cftp_admin');?></h3>
<label><?php _e('Assign this file to', 'cftp_admin');?>:</label>
<select multiple="multiple" name="file[<?php echo $i; ?>][assignments][]" class="form-control chosen-select" data-placeholder="<?php _e('Select one or more options. Type to search.', 'cftp_admin');?>">
<optgroup label="<?php _e('Clients', 'cftp_admin');?>">
<?php
/**
* The clients list is generated early on the file so the
* array doesn't need to be made once on every file.
*/
foreach($clients as $client => $client_name) {
?>
<option value="<?php echo 'c'.$client; ?>"<?php if (in_array($client,$file_on_clients)) { echo ' selected="selected"'; } ?>>
<?php echo $client_name; ?>
</option>
<?php
}
?>
</optgroup>
<optgroup label="<?php _e('Groups', 'cftp_admin');?>">
<?php
/**
* The groups list is generated early on the file so the
* array doesn't need to be made once on every file.
*/
foreach($groups as $group => $group_name) {
?>
<option value="<?php echo 'g'.$group; ?>"<?php if (in_array($group,$file_on_groups)) { echo ' selected="selected"'; } ?>>
<?php echo $group_name; ?>
</option>
<?php
}
?>
</optgroup>
</select>
<div class="list_mass_members">
<a href="#" class="btn btn-xs btn-primary add-all" data-type="assigns"><?php _e('Add all','cftp_admin'); ?></a>
<a href="#" class="btn btn-xs btn-primary remove-all" data-type="assigns"><?php _e('Remove all','cftp_admin'); ?></a>
</div>
<div class="divider"></div>
<div class="checkbox">
<label for="hid_checkbox">
<input type="checkbox" id="hid_checkbox" name="file[<?php echo $i; ?>][hidden]" value="1" /> <?php _e('Mark as hidden (will not send notifications) for new assigned clients and groups.', 'cftp_admin');?>
</label>
</div>
<div class="checkbox">
<label for="hid_existing_checkbox">
<input type="checkbox" id="hid_existing_checkbox" name="file[<?php echo $i; ?>][hideall]" value="1" /> <?php _e('Hide from every already assigned clients and groups.', 'cftp_admin');?>
</label>
</div>
</div>
</div>
<div class="col-sm-6 col-md-3 categories column">
<div class="file_data">
<h3><?php _e('Categories', 'cftp_admin');?></h3>
<label><?php _e('Add to', 'cftp_admin');?>:</label>
<select multiple="multiple" name="file[<?php echo $i; ?>][categories][]" class="form-control chosen-select" data-placeholder="<?php _e('Select one or more options. Type to search.', 'cftp_admin');?>">
<?php
/**
* The categories list is generated early on the file so the
* array doesn't need to be made once on every file.
*/
echo generate_categories_options( $get_categories['arranged'], 0, $current_categories );
?>
</select>
<div class="list_mass_members">
<a href="#" class="btn btn-xs btn-primary add-all" data-type="categories"><?php _e('Add all','cftp_admin'); ?></a>
<a href="#" class="btn btn-xs btn-primary remove-all" data-type="categories"><?php _e('Remove all','cftp_admin'); ?></a>
</div>
</div>
</div>
<?php
} /** Close $current_level check */
?>
</div>
</div>
</div>
</div>
<?php
}
?>
<div class="after_form_buttons">
<a href="<?php echo BASE_URI; ?>manage-files.php" name="cancel" class="btn btn-default btn-wide"><?php _e('Cancel','cftp_admin'); ?></a>
<button type="submit" name="submit" class="btn btn-wide btn-primary"><?php _e('Save','cftp_admin'); ?></button>
</div>
</div>
</form>
<?php
}
?>
</div>
<script type="text/javascript">
$(document).ready(function() {
$("form").submit(function() {
clean_form(this);
$(this).find('input[name$="[name]"]').each(function() {
is_complete($(this)[0],'<?php echo $validation_no_title; ?>');
});
// show the errors or continue if everything is ok
if (show_form_errors() == false) { return false; }
});
});
</script>
<?php
include('footer.php');