FileChecker   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 108
rs 10
c 0
b 0
f 0
wmc 14
lcom 0
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B getFileStatus() 0 39 6
A copyFile() 0 7 1
A compareFiles() 0 16 5
A fileExists() 0 4 1
A setFilePermissions() 0 6 1
1
<?php
2
3
namespace XoopsModules\Smallworld\Common;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
/**
16
 * Smallworld module
17
 *
18
 * @copyright       XOOPS Project (https://xoops.org)
19
 * @license         GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
20
 * @author          Xoops Development Team
21
 */
22
23
use Xmf\Request;
24
use XoopsModules\Smallworld;
25
use XoopsModules\Smallworld\Constants;
26
27
//defined('XOOPS_ROOT_PATH') || die('XOOPS root path not defined');
28
29
require_once dirname(dirname(dirname(dirname(__DIR__)))) . '/mainfile.php';
30
$moduleDirName      = basename(dirname(dirname(__DIR__)));
31
$moduleDirNameUpper = mb_strtoupper($moduleDirName);
32
xoops_loadLanguage('filechecker', $moduleDirName);
33
34
/**
35
 * Class FileChecker
36
 * check status of a directory
37
 */
38
class FileChecker
39
{
40
    /**
41
     * @param string      $file_path
42
     * @param string|null $original_file_path
43
     * @param string      $redirectFile
44
     * @return bool|string
45
     */
46
    public static function getFileStatus($file_path, $original_file_path, $redirectFile)
47
    {
48
        global $pathIcon16;
49
50
        if (empty($file_path)) {
51
            return false;
52
        }
53
        if (null === $redirectFile) {
54
            $redirectFile = $_SERVER['SCRIPT_NAME'];
55
        }
56
        $moduleDirName      = basename(dirname(dirname(__DIR__)));
57
        $moduleDirNameUpper = mb_strtoupper($moduleDirName);
58
        if (null === $original_file_path) {
59
            if (self::fileExists($file_path)) {
60
                $path_status = "<img src='$pathIcon16/1.png' >";
61
                $path_status .= "$file_path (" . constant('CO_' . $moduleDirNameUpper . '_' . 'FC_AVAILABLE') . ') ';
62
            } else {
63
                $path_status = "<img src='$pathIcon16/0.png' >";
64
                $path_status .= "$file_path (" . constant('CO_' . $moduleDirNameUpper . '_' . 'FC_NOTAVAILABLE') . ') ';
65
            }
66
        } else {
67
            if (self::compareFiles($file_path, $original_file_path)) {
68
                $path_status = "<img src='$pathIcon16/1.png' >";
69
                $path_status .= "$file_path (" . constant('CO_' . $moduleDirNameUpper . '_' . 'FC_AVAILABLE') . ') ';
70
            } else {
71
                $path_status = "<img src='$pathIcon16/0.png' >";
72
                $path_status .= "$file_path (" . constant('CO_' . $moduleDirNameUpper . '_' . 'FC_NOTAVAILABLE') . ') ';
73
                $path_status .= "<form action='" . $_SERVER['SCRIPT_NAME'] . "' method='post'>";
74
                $path_status .= "<input type='hidden' name='op' value='copyfile'>";
75
                $path_status .= "<input type='hidden' name='file_path' value='$file_path'>";
76
                $path_status .= "<input type='hidden' name='original_file_path' value='$original_file_path'>";
77
                $path_status .= "<input type='hidden' name='redirect' value='$redirectFile'>";
78
                $path_status .= "<button class='submit' onClick='this.form.submit();'>" . constant('CO_' . $moduleDirNameUpper . '_' . 'FC_CREATETHEFILE') . '</button>';
79
                $path_status .= '</form>';
80
            }
81
        }
82
83
        return $path_status;
84
    }
85
86
    /**
87
     * @param   $source_path
88
     * @param   $destination_path
89
     *
90
     * @return bool
91
     */
92
    public static function copyFile($source_path, $destination_path)
93
    {
94
        $source_path      = str_replace('..', '', $source_path);
95
        $destination_path = str_replace('..', '', $destination_path);
96
97
        return @copy($source_path, $destination_path);
98
    }
99
100
    /**
101
     * @param   $file1_path
102
     * @param   $file2_path
103
     *
104
     * @return bool
105
     */
106
    public static function compareFiles($file1_path, $file2_path)
107
    {
108
        if (!self::fileExists($file1_path) || !self::fileExists($file2_path)) {
109
            return false;
110
        }
111
        if (filetype($file1_path) !== filetype($file2_path)) {
112
            return false;
113
        }
114
        if (filesize($file1_path) !== filesize($file2_path)) {
115
            return false;
116
        }
117
        $crc1 = mb_strtoupper(dechex(crc32(file_get_contents($file1_path))));
118
        $crc2 = mb_strtoupper(dechex(crc32(file_get_contents($file2_path))));
119
120
        return !($crc1 !== $crc2);
121
    }
122
123
    /**
124
     * @param   $file_path
125
     *
126
     * @return bool
127
     */
128
    public static function fileExists($file_path)
129
    {
130
        return is_file($file_path);
131
    }
132
133
    /**
134
     * @param     $target
135
     * @param int $mode
136
     *
137
     * @return bool
138
     */
139
    public static function setFilePermissions($target, $mode = 0777)
140
    {
141
        $target = str_replace('..', '', $target);
142
143
        return @chmod($target, (int)$mode);
144
    }
145
}
146
147
$op = Request::getString('op', '', 'POST');
148
switch ($op) {
149 View Code Duplication
    case 'copyfile':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
150
        if (\Xmf\Request::hasVar('original_file_path', 'POST')) {
151
            $original_file_path = $_POST['original_file_path'];
152
        }
153
        if (\Xmf\Request::hasVar('file_path', 'POST')) {
154
            $file_path = $_POST['file_path'];
155
        }
156
        if (\Xmf\Request::hasVar('redirect', 'POST')) {
157
            $redirect = $_POST['redirect'];
158
        }
159
        $msg = FileChecker::copyFile($original_file_path, $file_path) ? constant('CO_' . $moduleDirNameUpper . '_' . 'FC_FILECOPIED') : constant('CO_' . $moduleDirNameUpper . '_' . 'FC_FILENOTCOPIED');
160
        redirect_header($redirect, Constants::REDIRECT_DELAY_MEDIUM, $msg . ': ' . $file_path);
161
        break;
162
}
163