Filesystem::doGetUserHomeDir()   C
last analyzed

Complexity

Conditions 8
Paths 7

Size

Total Lines 35
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 0
cts 23
cp 0
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 15
nc 7
nop 0
crap 72
1
<?php
2
/**
3
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2011, Peter Gribanov
7
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
8
 */
9
namespace AnimeDb\Bundle\AppBundle\Util;
10
11
use Patchwork\Utf8;
12
13
class Filesystem
14
{
15
    /**
16
     * @var int
17
     */
18
    const FILE = 1;
19
20
    /**
21
     * @var int
22
     */
23
    const DIRECTORY = 2;
24
25
    /**
26
     * Gets the name of the owner of the current PHP script.
27
     *
28
     * @return string
29
     */
30
    public static function getUserName()
31
    {
32
        return get_current_user() ?: getenv('USERNAME');
33
    }
34
35
    /**
36
     * Get user home directory.
37
     *
38
     * @return string
39
     */
40
    public static function getUserHomeDir()
41
    {
42
        return self::getRealPath(self::doGetUserHomeDir());
43
    }
44
45
    /**
46
     * @return string
47
     */
48
    private static function doGetUserHomeDir()
49
    {
50
        // have home env var
51
        if ($home = getenv('HOME')) {
52
            return $home;
53
        }
54
55
        // *nix os
56
        if (!defined('PHP_WINDOWS_VERSION_BUILD')) {
57
            return '/home/'.self::getUserName();
58
        }
59
60
        // have drive and path env vars
61
        if (getenv('HOMEDRIVE') && getenv('HOMEPATH')) {
62
            return iconv('cp1251', 'utf-8', getenv('HOMEDRIVE').getenv('HOMEPATH'));
63
        }
64
65
        // Windows
66
        if ($username = self::getUserName()) {
67
            $username = iconv('cp1251', 'utf-8', $username);
68
            // is Vista or older
69
            if (is_dir($win7path = 'C:\Users\\'.$username.'\\')) {
70
                return $win7path;
71
            }
72
73
            return 'C:\Documents and Settings\\'.$username.'\\';
74
        }
75
76
        // is Vista or older
77
        if (is_dir('C:\Users\\')) {
78
            return 'C:\Users\\';
79
        }
80
81
        return 'C:\Documents and Settings\\';
82
    }
83
84
    /**
85
     * List files and directories inside the specified path.
86
     *
87
     * @param string $path
88
     * @param int $filter
89
     * @param int $order
90
     *
91
     * @return array
92
     */
93
    public static function scandir($path, $filter = 0, $order = SCANDIR_SORT_ASCENDING)
94
    {
95
        if (!$filter || (
96
            ($filter & self::FILE) != self::FILE &&
97
            ($filter & self::DIRECTORY) != self::DIRECTORY
98
        )) {
99
            $filter = self::FILE | self::DIRECTORY;
100
        }
101
        // add slash if need
102
        $path = self::getRealPath($path);
103
        // wrap path for current fs
104
        $wrap = Utf8::wrapPath($path);
105
106
        // scan directory
107
        $folders = [];
108
        foreach (new \DirectoryIterator($wrap) as $file) {
109
            /* @var $file \SplFileInfo */
110
            try {
111
                if (
112
                    $file->getFilename()[0] != '.' &&
113
                    substr($file->getFilename(), -1) != '~' &&
114
                    $file->getFilename() != 'pagefile.sys' && // failed read C:\pagefile.sys
115
                    $file->isReadable() &&
116
                    (
117
                        (($filter & self::FILE) == self::FILE && $file->isFile()) ||
118
                        (($filter & self::DIRECTORY) == self::DIRECTORY && $file->isDir())
119
                    )
120
                ) {
121
                    $folders[$file->getFilename()] = [
122
                        'name' => $file->getFilename(),
123
                        'path' => $path.$file->getFilename().DIRECTORY_SEPARATOR,
124
                    ];
125
                }
126
            } catch (\Exception $e) {
127
                // ignore all errors
128
            }
129
        }
130
131
        // order files
132
        if ($order == SCANDIR_SORT_ASCENDING) {
133
            ksort($folders);
134
        } elseif ($order == SCANDIR_SORT_DESCENDING) {
135
            ksort($folders);
136
            $folders = array_reverse($folders);
137
        }
138
139
        // add link on parent folder
140
        if (substr_count($path, DIRECTORY_SEPARATOR) > 1) {
141
            $pos = strrpos(substr($path, 0, -1), DIRECTORY_SEPARATOR) + 1;
142
            array_unshift($folders, [
143
                'name' => '..',
144
                'path' => substr($path, 0, $pos),
145
            ]);
146
        }
147
148
        return array_values($folders);
149
    }
150
151
    /**
152
     * @param string $path
153
     *
154
     * @return string
155
     */
156
    public static function getRealPath($path)
157
    {
158
        $path = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $path);
159
160
        return rtrim($path, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
161
    }
162
}
163