UsrBrowserAgent()   B
last analyzed

Complexity

Conditions 8
Paths 7

Size

Total Lines 26
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 8
eloc 23
c 3
b 0
f 0
nc 7
nop 2
dl 0
loc 26
rs 8.4444
1
<?php
2
/*
3
*******************************************************
4
***													***
5
*** backpack										***
6
*** Cedric MONTUY pour CHG-WEB                      ***
7
*** Original author : Yoshi Sakai					***
8
***													***
9
*******************************************************
10
*/
11
12
use Xmf\Module\Admin;
13
/** @var Admin $adminObject */
14
15
ini_set('memory_limit', '20M');
16
17
require_once __DIR__ . '/admin_header.php';
18
xoops_cp_header();
19
$adminObject = Admin::getInstance();
20
$adminObject->displayNavigation(basename(__FILE__));
21
22
require dirname(__DIR__) . '/include/ext2mime.php';        // Load the decode array of extension to MIME
23
24
$fpathname   = htmlspecialchars(rawurldecode($_GET['url']), ENT_QUOTES);
25
$dl_filename = $fpathname;
26
if (defined('XOOPS_VAR_PATH')) {
27
    $backup_dir = XOOPS_VAR_PATH . '/caches/';
28
} else {
29
    $backup_dir = XOOPS_ROOT_PATH . '/cache/';
30
}
31
$fpathname = $backup_dir . $fpathname;
32
ob_clean();
33
if (!file_exists($fpathname)) {
34
    if (file_exists($fpathname . '.log')) {
35
        echo '<strong>Already downloaded by </strong>';
36
        $fp = fopen($fpathname . '.log', 'r');
37
        while (!feof($fp)) {
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of feof() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

37
        while (!feof(/** @scrutinizer ignore-type */ $fp)) {
Loading history...
38
            $line = fgets($fp);
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of fgets() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
            $line = fgets(/** @scrutinizer ignore-type */ $fp);
Loading history...
39
            echo $line . '<br>';
40
        }
41
        fclose($fp);
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
        fclose(/** @scrutinizer ignore-type */ $fp);
Loading history...
42
        exit();
43
    }
44
    print('Error - ' . $fpathname . ' does not exist.');
45
    return;
46
}
47
$browser = $version = 0;
48
UsrBrowserAgent($browser, $version);
49
ignore_user_abort();
50
51
$fnamedotpos = strrpos($dl_filename, '.');
52
$fext        = substr($dl_filename, $fnamedotpos + 1);
53
$ctype       = $ext2mime[$fext] ?? 'application/octet-stream-dummy';
54
if ('gz' == $fext) {
55
    $content_encoding = 'x-gzip';
56
}
57
//echo $fext.$ctype; exit();
58
if ('IE' == $browser && (ini_get('zlib.output_compression'))) {
59
    ini_set('zlib.output_compression', 'Off');
60
}
61
//if (!empty($content_encoding)) {
62
//    header('Content-Encoding: ' . $content_encoding);
63
//}
64
header('Content-Transfer-Encoding: binary');
65
header('Content-Length: ' . filesize($fpathname));
66
header('Content-type: ' . $ctype);
67
header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
68
header('Last-Modified: ' . date('D M j G:i:s T Y'));
69
header('Content-Disposition: attachment; filename="' . $dl_filename . '"');
70
//header("Content-Disposition: inline; filename=" . $dl_filename);
71
header('x-extension: ' . $ctype);
72
73
if ('IE' == $browser) {
74
    header('Pragma: public');
75
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
76
} else {
77
    header('Pragma: no-cache');
78
}
79
80
$fp = fopen($fpathname, 'r');
81
while (!feof($fp)) {
82
    $buffer = fread($fp, 1024 * 6); //speed-limit 64kb/s
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of fread() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

82
    $buffer = fread(/** @scrutinizer ignore-type */ $fp, 1024 * 6); //speed-limit 64kb/s
Loading history...
83
    print $buffer;
84
    flush();
85
    ob_flush();
86
    usleep(10000);
87
}
88
fclose($fp);
89
//
90
// Save download log
91
//
92
if ($xoopsUser) {
93
    $uname = $xoopsUser->getVar('uname');
94
} else {
95
    $uname = 'Anonymous';
96
}
97
$str     = $uname . ',' . date('Y-m-d H:i:s', time());
98
$postlog = $fpathname . '.log';
99
$fp      = fopen($postlog, 'a');
100
fwrite($fp, $str . "\n");
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

100
fwrite(/** @scrutinizer ignore-type */ $fp, $str . "\n");
Loading history...
101
fclose($fp);
102
unlink($fpathname);
103
//xoops_cp_footer();
104
//
105
// Check User Browser
106
// @TODO - add check for Microsoft Edge browser
107
//
108
function UsrBrowserAgent(&$browser, &$version)
109
{
110
    if (preg_match('@Opera(/| )([0-9].[0-9]{1,2})@', $_SERVER['HTTP_USER_AGENT'], $log_version)) {
111
        $version = $log_version[2];
112
        $browser = 'OPERA';
113
    } elseif (preg_match('@MSIE ([0-9].[0-9]{1,2})@', $_SERVER['HTTP_USER_AGENT'], $log_version)) {
114
        $version = $log_version[1];
115
        $browser = 'IE';
116
    } elseif (preg_match('@OmniWeb/([0-9].[0-9]{1,2})@', $_SERVER['HTTP_USER_AGENT'], $log_version)) {
117
        $version = $log_version[1];
118
        $browser = 'OMNIWEB';
119
    } elseif (preg_match('@(Konqueror/)(.*)(;)@', $_SERVER['HTTP_USER_AGENT'], $log_version)) {
120
        $version = $log_version[2];
121
        $browser = 'KONQUEROR';
122
    } elseif (preg_match('@Mozilla/([0-9].[0-9]{1,2})@', $_SERVER['HTTP_USER_AGENT'], $log_version)
123
              && preg_match('@Safari/([0-9]*)@', $_SERVER['HTTP_USER_AGENT'], $log_version2)) {
124
        $version = $log_version[1] . '.' . $log_version2[1];
125
        $browser = 'SAFARI';
126
    } elseif (preg_match('@Mozilla/([0-9].[0-9]{1,2})@', $_SERVER['HTTP_USER_AGENT'], $log_version)) {
127
        $version = $log_version[1];
128
        $browser = 'MOZILLA';
129
    } else {
130
        $version = 0;
131
        $browser = 'OTHER';
132
    }
133
    return $browser;
134
}
135