Passed
Push — master ( 52bf93...8fa3ac )
by Kishan
02:14
created

zipper.php (1 issue)

Labels
Severity
1
<?php
2
3
/** 
4
 * Copyright 2018 Social Manager.
5
 * PHP version 7.2.8
6
 *
7
 * It will Zip the file
8
 * 
9
 * @category Album_Manager
10
 * @package  Zipper
11
 * @author   Kishan Jasani <[email protected]>
12
 * @license  https://localhost/SocialManager/privacy_policy/privacy_policy.php 
13
 * @link     ""
14
 * 
15
 * You are hereby granted a non-exclusive, worldwide, royalty-free license to
16
 * use, copy, modify, and distribute this software in source code or binary
17
 * form for use in connection with the web services and APIs provided by
18
 * Kishan Jasani.
19
 *
20
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND
21
 */
22
$zip_folder = "";
23
/** 
24
 * Starting of zipping folders recursively 
25
 */
26
27
class Zipper
28
{
29
    /**
30
     * It will load the file which willl zip
31
     * 
32
     * @param String $source Source of the folder
33
     * 
34
     * @return ""
35
     */
36
    public function loadZipFiles($source) 
37
    {
38
        if (!file_exists($source)) {
39
            return false;
40
        }
41
        $source = str_replace('\\', '/', realpath($source));
42
        $a = array();
43
        if (is_dir($source) === true) {
44
            $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
45
            foreach ($files as $file) {
46
                $file = str_replace('\\', '/', $file);
47
                // Ignore "." and ".." folders
48
                if (in_array(substr($file, strrpos($file, '/') + 1), array('.', '..'))) {
49
                                    continue;
50
                }
51
                $file = realpath($file);
52
                if (is_dir($file) === true) {
53
                    $a[] = array(
54
                    'type' => 'dir',
55
                    'source' => str_replace($source . '/', '', $file . '/'),
56
                    'file' => $file,
57
                    'size' => 0
58
                    );
59
                } else if (is_file($file) === true) {
60
                    $src = str_replace($source . '/', '', $file);
61
                    $size = filesize($file);
62
                    $a[] = array(
63
                    'type' => 'file',
64
                    'source' => $src,
65
                    'file' => $file,
66
                    'size' => false != $size ? $size : 16000 
67
                    );
68
                }
69
            }
70
        }
71
        return $a;
72
    }
73
    /**
74
     * It will process the zip
75
     * 
76
     * @param String $foldercontent content of the folder
77
     * @param String $folder        folder name
78
     * @param String $maxsize       maximum size of the folder
79
     * 
80
     * @return ""
81
     */
82
    public function processZip($foldercontent, $folder, $maxsize)
83
    {
84
        $split = array();
85
        $splits = 1;
86
        $t = 0;
87
        // Determine how many zip files to create
88
        if (isset($foldercontent)) {
89
            foreach ($foldercontent as $entry) {
90
                $t = $t + $entry['size'];
91
                if ($entry['type'] == 'dir') {
92
                    $lastdir = $entry;
93
                }
94
                if ($t >= $maxsize) {
95
                    $splits++;
96
                    $t = 0;
97
                    // create lastdir in next archive, in case files still exist
98
                    // even if the next file is not in this archive it doesn't hurt
99
                    if ($lastdir !== '') {
100
                        $split[$splits][] = $lastdir;
101
                    }
102
                }
103
                $split[$splits][] = $entry;
104
            }
105
            // delete the $foldercontent array
106
            unset($foldercontent);
107
            // Create the folder to put the zip files in
108
            $date = new DateTime();
109
            $tS = $date->format('YmdHis');
110
            // Process the splits
111
            foreach ($split as $idx => $sp) {
112
                // create the zip file
113
                $zip = new ZipArchive();
114
                $destination = $folder . '.zip';
115
                if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
116
                    return false;
117
                }
118
                $i = 1;
119
                $dir = "";
120
                foreach ($sp as $entry) {
121
                    if ($entry['type'] === 'dir') {
122
                        $dir = explode('\\', $entry['file']);
123
                        $zip->addEmptyDir(end($dir));
124
                    } else {
125
                        $zip->addFromString(
126
                            end($dir) . '/' . $i . '.jpg', 
127
                            file_get_contents($entry['file'])
128
                        );
129
                        $i++;
130
                    }
131
                }
132
                $zip->close();
133
            }
134
            return array(
135
                'splits' => count($split),
136
                'foldername' => ''
137
            );
138
        }
139
    }
140
    /**
141
     * It will get the memory limit
142
     * 
143
     * @return ""
144
     */
145
    public function getMemoryLimit()
146
    {
147
        $memory_limit = ini_get('memory_limit');
148
        if (preg_match('/^(\d+)(.)$/', $memory_limit, $matches)) {
149
            if ($matches[2] == 'M') {
150
                $memory_limit = $matches[1] * 1024 * 1024; // nnnM -> nnn MB
151
            } else if ($matches[2] == 'K') {
152
                $memory_limit = $matches[1] * 1024; // nnnK -> nnn KB
153
            }
154
        }
155
        return $memory_limit;
156
    }
157
158
    /**
159
     * It will Make the zip
160
     * 
161
     * @param String $album_download_directory folder will be zip 
162
     * 
163
     * @return ""
164
     */
165
    public function makeZip($album_download_directory) 
166
    {
167
        $zipfilename = "";
168
        if (isset($album_download_directory)) {
169
            //$zipfilename = 'libs/resources'.DIRECTORY_SEPARATOR.'albums'.DIRECTORY_SEPARATOR.'fb-album_'.date("Y-m-d").'_'.date("H-i-s");
170
            $zipfilename = 'public/fb-album_' . date("Y-m-d") . '_' . date("H-i-s");
171
            // name of folder starting from the root of the webserver
172
            // as in Wordpress /wp-content/themes/ (end on backslash)
173
            $folder = dirname($_SERVER['PHP_SELF']) . '/' . $album_download_directory;
174
            // Server Root
175
            $root = $_SERVER["DOCUMENT_ROOT"];
176
            // source of the folder to unpack
177
            $sourcedir = $root . $folder; // target directory
178
            // Don't use more than half the memory limit
179
            $memory_limit = $this->getMemoryLimit();
180
            $maxsize = $memory_limit / 2;
181
            // Is zipping possible on the server ?
182
            if (!extension_loaded('zip')) {
183
                echo 'Zipping not possible on this server';
184
                exit;
185
            }
186
            // Get the files to zip
187
            $foldercontent = $this->loadZipFiles($sourcedir);
188
            if ($foldercontent === false) {
189
                echo 'Something went wrong gathering the file entries';
190
                exit;
191
            }
192
            // Process the files to zip
193
            $zip = $this->processZip($foldercontent, $zipfilename, $maxsize);
194
            if ($zip === false) {
195
                echo 'Something went wrong zipping the files';
196
            }    
197
            // clear the stat cache (created by filesize command)
198
            clearstatcache();
199
            include_once 'Unlink_Directory.php';
200
            $unlink_directory = new Unlink_Directory();
0 ignored issues
show
The type Unlink_Directory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
201
            $unlink_directory->removeDirectory($album_download_directory);
202
        }
203
        return $zipfilename;
204
    }
205
    /**
206
     * It will get the zip file
207
     * 
208
     * @param String $album_download_directory folder will be zip 
209
     * 
210
     * @return ""
211
     */
212
    public function getZip($album_download_directory) 
213
    {
214
        $response = '<span style="color: #ffffff;">Sorry due to some reasons albums is not downloaded.</span>';
215
        if (isset($album_download_directory)) {
216
            $zip_folder = $this->makeZip($album_download_directory);
217
            if (!empty($zip_folder)) {
218
                $response = '<a href="' . $zip_folder . '.zip" id="download-link" target="_blank" class="btn" >Download Zip Folder</a>';
219
            }
220
        }
221
        return $response;
222
    }
223
}
224
?>