Completed
Push — master ( 26776f...d9604e )
by Michael
11:31
created

viewFile.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 81 and the first side effect is on line 3.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
//include('header.php');
3
require('../../mainfile.php');
4
5
if (!defined('XHELP_CONSTANTS_INCLUDED')) {
6
    include_once(XOOPS_ROOT_PATH.'/modules/xhelp/include/constants.php');
7
}
8
9
include_once(XHELP_BASE_PATH.'/functions.php');
10
11
if(!$xoopsUser) {
12
    redirect_header(XOOPS_URL .'/user.php?xoops_redirect='.htmlencode($xoopsRequestUri), 3);
13
}
14
15
if(isset($_GET['id'])){
16
    $xhelp_id = intval($_GET['id']);
17
}
18
19
$viewFile = false;
20
21
$hFiles   =& xhelpGetHandler('file');
22
$hTicket  =& xhelpGetHandler('ticket');
23
$hStaff   =& xhelpGetHandler('staff');
24
$file     =& $hFiles->get($xhelp_id);
25
$mimeType = $file->getVar('mimetype');
26
$ticket   =& $hTicket->get($file->getVar('ticketid'));
27
28
$filename_full = $file->getVar('filename');
29 View Code Duplication
if($file->getVar('responseid') > 0){
30
    $removeText = $file->getVar('ticketid')."_".$file->getVar('responseid')."_";
31
} else {
32
    $removeText = $file->getVar('ticketid')."_";
33
}
34
$filename = str_replace($removeText, '', $filename_full);
35
36
//Security:
37
// Only Staff Members, Admins, or ticket Submitter should be able to see file
38
if (_userAllowed($ticket, $xoopsUser)) {
39
    $viewFile = true;
40
} elseif ($hStaff->isStaff($xoopsUser->getVar('uid'))) {
41
    $viewFile = true;
42
} elseif ($xoopsUser->isAdmin($xoopsModule->getVar('mid'))) {
43
    $viewFile = true;
44
}
45
46
if (!$viewFile) {
47
    redirect_header(XHELP_BASE_URL.'/index.php', 3, _NOPERM);
48
}
49
50
//Check if the file exists
51
$fileAbsPath = XHELP_UPLOAD_PATH . '/'. $filename_full;
52
if (!file_exists($fileAbsPath)) {
53
    redirect_header(XHELP_BASE_URL.'/index.php', 3, _XHELP_NO_FILES_ERROR);
54
    exit();
55
}
56
57
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
58
header("Cache-Control: private",false);
59
header("Content-Transfer-Encoding: binary");
60
header("Content-Length: ".filesize($fileAbsPath));
61
62
if(isset($mimeType)) {
63
    header("Content-Type: " . $mimeType);
64
} else {
65
    header("Content-Type: application/octet-stream");
66
}
67
68
// Add Header to set filename
69
header("Content-Disposition: attachment; filename=" . $filename);
70
71
// Open the file
72
if(isset($mimeType) && strstr($mimeType, "text/")) {
73
    $fp = fopen($fileAbsPath, "r");
74
} else {
75
    $fp = fopen($fileAbsPath, "rb");
76
}
77
78
// Write file to browser
79
fpassthru($fp);
80
81
function _userAllowed(&$ticket, &$user) {
82
    $emails =& $ticket->getEmails(true);
83
    foreach($emails as $email) {
84
        if ($email->getVar('email') == $user->getVar('email')) {
85
            return true;
86
        }
87
    }
88
89
    return false;
90
}
91