CommonPermissions   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 82
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A explainPerms() 0 19 1
A numericalPermissionsArray() 0 11 1
A matchFirstFlagSingle() 0 10 3
A firstFlagCodeArray() 0 10 1
A firstFlagDescriptionArray() 0 13 1
1
<?php
2
3
/**
4
 *
5
 * The MIT License (MIT)
6
 *
7
 * Copyright (c) 2016 Daniel Popiniuc
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
10
 * of this software and associated documentation files (the "Software"), to deal
11
 * in the Software without restriction, including without limitation the rights
12
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 * copies of the Software, and to permit persons to whom the Software is
14
 * furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in all
17
 * copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
 * SOFTWARE.
26
 *
27
 */
28
29
namespace danielgp\common_lib;
30
31
/**
32
 * usefull functions to get quick results
33
 *
34
 * @author Daniel Popiniuc
35
 */
36
trait CommonPermissions
37
{
38
39
    /**
40
     * Returns an array with meaningfull content of permissions
41
     *
42
     * @param int $permissionNumber
43
     * @return array
44
     */
45
    public function explainPerms($permissionNumber)
46
    {
47
        $firstFlag            = $this->matchFirstFlagSingle($permissionNumber);
48
        $permissionsString    = substr(sprintf('%o', $permissionNumber), -4);
49
        $numericalPermissions = $this->numericalPermissionsArray();
50
        return [
51
            'Permissions' => $permissionNumber,
52
            'Code'        => $permissionsString,
53
            'First'       => $firstFlag,
54
            'Overall'     => implode('', [
55
                $firstFlag['code'],
56
                $numericalPermissions[substr($permissionsString, 1, 1)]['code'],
57
                $numericalPermissions[substr($permissionsString, 2, 1)]['code'],
58
                $numericalPermissions[substr($permissionsString, 3, 1)]['code'],
59
            ]),
60
            'First'       => $firstFlag,
61
            'Owner'       => $numericalPermissions[substr($permissionsString, 1, 1)],
62
            'Group'       => $numericalPermissions[substr($permissionsString, 2, 1)],
63
            'World/Other' => $numericalPermissions[substr($permissionsString, 3, 1)],
64
        ];
65
    }
66
67
    private function firstFlagCodeArray()
68
    {
69
        return [
70
            0x8000 => '-',
71
            0x6000 => 'b',
72
            0x2000 => 'c',
73
            0x4000 => 'd',
74
            0xA000 => 'l',
75
            0x1000 => 'p',
76
            0xC000 => 's',
77
        ];
78
    }
79
80
    private function firstFlagDescriptionArray($codeFlag)
81
    {
82
        $aCodes = [
83
            '-' => 'Regular',
84
            'b' => 'Block special',
85
            'c' => 'Character special',
86
            'd' => 'Directory',
87
            'l' => 'Symbolic Link',
88
            'p' => 'FIFO pipe',
89
            's' => 'Socket',
90
            'w' => 'Whiteout',
91
        ];
92
        return ['code' => $codeFlag, 'name' => $aCodes[$codeFlag]];
93
    }
94
95
    private function matchFirstFlagSingle($permissionNumber)
96
    {
97
        $aCodes      = $this->firstFlagCodeArray();
98
        $matchedCode = 'w';
99
        foreach ($aCodes as $key => $value) {
100
            if (($permissionNumber & $key) == $key) {
101
                $matchedCode = $value;
102
            }
103
        }
104
        return $this->firstFlagDescriptionArray($matchedCode);
105
    }
106
107
    private function numericalPermissionsArray()
108
    {
109
        return [
110
            0 => ['code' => '---', 'name' => 'none'],
111
            1 => ['code' => '--x', 'name' => 'execute only'],
112
            2 => ['code' => '-w-', 'name' => 'write only'],
113
            3 => ['code' => '-wx', 'name' => 'write and execute'],
114
            4 => ['code' => 'r--', 'name' => 'read only'],
115
            5 => ['code' => 'r-x', 'name' => 'read and execute'],
116
            6 => ['code' => 'rw-', 'name' => 'read and write'],
117
            7 => ['code' => 'rwx', 'name' => 'read, write and execute'],
118
        ];
119
    }
120
}
121