Failed Conditions
Push — issue#765 ( 54a2c2...25bda7 )
by Guilherme
09:33
created

getTempNam()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 6
nc 3
nop 1
dl 0
loc 9
rs 8.8571
c 0
b 0
f 0
1
<?php
2
class Zip_Manager extends \ZipArchive
3
{
4
5
    const CHMOD = 0755;
6
7
    public function filteredExtractTo($directory, array $filters = null)
8
    {
9
        if (count($filters) === 0) {
10
            return $this->extractTo($directory);
11
        }
12
13
        $this->createDir($directory);
14
15
        $copySource = 'zip://'.$this->filename.'#';
16
        for ($i = 0; $i < $this->numFiles; $i++) {
17
            $entry = $this->getNameIndex($i);
18
            $filename = basename($entry);
19
20
            if ($this->matchFileToFilter($filename, $filters)) {
21
                $base = dirname($entry);
22
                $newPath = $directory.DIRECTORY_SEPARATOR.$base.DIRECTORY_SEPARATOR;
23
                $this->createDir($newPath);
24
25
                // extract file
26
                copy($copySource.$entry, $newPath.$filename);
27
            }
28
        }
29
    }
30
31
    protected function createDir($path)
32
    {
33
        if (!is_dir($path)) {
34
            if (!mkdir($path, self::CHMOD, true)) {
35
                throw new Exception('unable to create path '.$path);
36
            }
37
        }
38
    }
39
40
    protected function matchFileToFilter($filename, array $filters)
41
    {
42
        $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
43
        if (in_array($ext, array_map('strtolower', $filters))) {
44
45
            return true;
46
        }
47
48
        foreach ($filters as $i => $filter) {
49
50
            if (!ctype_alnum($filter[0]) && preg_match($filter, $filename)) {
51
                return true;
52
            }
53
        }
54
        return false;
55
    }
56
}
57
58
function getTempNam($prefix = "php")
59
{
60
    $tmp = getenv('TMPDIR');
61
    if ($tmp && @is_writable($tmp)) {
62
        $tmpDir = $tmp;
63
    } elseif (function_exists('sys_get_temp_dir') && @is_writable(sys_get_temp_dir())) {
64
        $tmpDir = sys_get_temp_dir();
65
    }
66
    return tempnam($tmpDir, $prefix);
67
}
68
69
function file_get_contents_curl($url, $file, $referer = null)
70
{
71
    $ch = curl_init();
72
    // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
73
    $f = fopen($file, 'wb');
74
    if (!$f) {
75
        return false;
76
    }
77
    curl_setopt($ch, CURLOPT_FILE, $f);
78
    curl_setopt($ch, CURLOPT_HEADER, 0);
79
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
80
    curl_setopt($ch, CURLOPT_ENCODING, '');
81
    curl_setopt($ch, CURLOPT_URL, $url);
82
    if ($referer) {
83
        curl_setopt($ch, CURLOPT_REFERER, $referer);
84
    }
85
    $result = curl_exec($ch);
86
    if (!$result) {
87
        echo curl_error($ch);
88
    }
89
    curl_close($ch);
90
    fclose($f);
91
    return $result;
92
}
93
94
function getPDOConnection($config)
95
{
96
    $db_driver = $config['database_driver'];
97
    $db_host = $config['database_host'];
98
    $db_name = $config['database_name'];
99
    $db_user = $config['database_user'];
100
    $db_pass = $config['database_password'];
101
    $db_port = $config['database_port'];
102
    $pdo = new PDO("$db_driver:host=$db_host;port=$db_port;dbname=$db_name", $db_user, $db_pass);
103
    $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
104
    $pdo->query("SET NAMES 'UTF8'");
105
    return $pdo;
106
}
107
function trim2($s)
108
{
109
    $s = trim($s, " \t\n\r\0\x0B-");
110
    if ('' === $s) {
111
        return null;
112
    } else {
113
        return $s;
114
    }
115
}
116
function utf8_encode_recursivo($in)
117
{
118
    if (is_array($in)) {
119
        foreach ($in as $key => $value) {
120
            $out[utf8_encode_recursivo($key)] = utf8_encode_recursivo($value);
121
        }
122
    } elseif (is_string($in)) {
123
        if (!mb_check_encoding($in, 'UTF-8')
124
            OR !($in === mb_convert_encoding(mb_convert_encoding($in, 'UTF-32', 'UTF-8'), 'UTF-8', 'UTF-32'))) {
125
                $in = mb_convert_encoding($in, 'UTF-8');
126
            }
127
            return $in;
128
    } else {
129
        return $in;
130
    }
131
    return $out;
132
}
133