-
Notifications
You must be signed in to change notification settings - Fork 31
/
upchunk.php
110 lines (93 loc) · 2.82 KB
/
upchunk.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/**************************
Coppermine Photo Gallery
**************************
Copyright (c) 2003-2016 Coppermine Dev Team
v1.0 originally written by Gregory Demar
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3
as published by the Free Software Foundation.
************************************
Coppermine version: 1.6.03
$HeadURL$
************************************/
if (!defined('IN_COPPERMINE')) die('Not in Coppermine...');
class UpChunkObj
{
protected $ckid;
protected $ckpath;
protected $dstpath;
protected $filename;
protected $totalchunks;
public function __construct ($p)
{
global $CONFIG;
$this->ckid = $p->getEscaped('identifier');
$this->filename = $p->getEscaped('filename');
$this->totalchunks = $p->getInt('totalChunks');
$this->dstpath = $CONFIG['fullpath'] . $CONFIG['userpics'] . (USER_ID + FIRST_USER_CAT) . '/';
$this->ckpath = $this->dstpath . $this->ckid;
// create the temporary directory
if ($this->ckid && !is_dir($this->ckpath)) {
mkdir($this->ckpath, 0777, true);
upldLog('created chunk dir: '.$this->ckpath);
}
}
public function addChunk ($file, $cnkn)
{
$dest = $this->ckpath.'/'.$this->filename.'.part'.$cnkn;
if (!move_uploaded_file($file, $dest)) {
upldLog('failed to place chunk: '.$dest);
errorOut(sprintf($GLOBALS['lang_plugin_upload_h5a']['muf_err'], $cnkn, $this->filename, $file, $dest));
}
upldLog('placed chunk: '.$dest);
if ($cnkn == $this->totalchunks) {
// count all the parts of this file
$total_files = 0;
foreach(scandir($this->ckpath) as $filepart) {
if (stripos($filepart, $this->filename) !== false) {
$total_files++;
}
}
if ($total_files !== $this->totalchunks) errorOut($GLOBALS['lang_plugin_upload_h5a']['miss_chnk']);
return true;
}
return false;
}
public function combineTo ($dest)
{
// create the final destination file
if (($fp = @fopen($dest, 'w')) !== false) {
for ($i=1; $i<=$this->totalchunks; $i++) {
fwrite($fp, file_get_contents($this->ckpath.'/'.$this->filename.'.part'.$i));
}
fclose($fp);
} else {
upldLog('failed to open destination file: '.$dest);
errorOut(sprintf($GLOBALS['lang_plugin_upload_h5a']['dest_fail'], $dest));
}
upldLog('combined chunks: '.$dest);
}
public function cleanup ()
{
if ($this->ckid) $this->rrmdir($this->ckpath);
upldLog('chunks cleared: '.$this->ckpath);
}
private function rrmdir ($dir) {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != '.' && $object != '..') {
if (filetype($dir . '/' . $object) == 'dir') {
$this->rrmdir($dir . '/' . $object);
} else {
unlink($dir . '/' . $object);
}
}
}
reset($objects);
rmdir($dir);
}
}
}
//EOF