Project

General

Profile

1
<?php
2
/**
3
 *
4
 * @category        admin
5
 * @package         media
6
 * @author          Ryan Djurovich,WebsiteBaker Project
7
 * @copyright       2009-2013, WebsiteBaker Org. e.V.
8
 * @link            http://www.websitebaker.org/
9
 * @license         http://www.gnu.org/licenses/gpl.html
10
 * @platform        WebsiteBaker 2.8.x
11
 * @requirements    PHP 5.2.2 and higher
12
 * @version         $Id: upload.php 2098 2014-02-11 01:37:03Z darkviper $
13
 * @filesource      $HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/admin/media/upload.php $
14
 * @lastmodified    $Date: 2014-02-11 02:37:03 +0100 (Tue, 11 Feb 2014) $
15
 *
16
 */
17

    
18
if(!defined('WB_URL'))
19
{
20
    $config_file = realpath('../../config.php');
21
    if(file_exists($config_file) && !defined('WB_URL'))
22
    {
23
    	require($config_file);
24
    }
25
}
26
//if(!class_exists('admin', false)){ include(WB_PATH.'/framework/class.admin.php'); }
27
$oTrans = Translate::getInstance();
28
$oTrans->enableAddon('admin\\media');
29

    
30
$modulePath = dirname(__FILE__);
31

    
32
//include_once('resize_img.php');
33
include_once($modulePath.'/parameters.php');
34

    
35
// suppress to print the header, so no new FTAN will be set
36
$admin = new admin('Media', 'media_upload', false);
37

    
38
if( !$admin->checkFTAN() )
39
{
40
	$admin->print_header();
41
	$admin->print_error($oTrans->MESSAGE_GENERIC_SECURITY_ACCESS );
42
}
43
// After check print the header
44
$admin->print_header();
45

    
46
// Target location
47
$requestMethod = '_'.strtoupper($_SERVER['REQUEST_METHOD']);
48
$target = (isset(${$requestMethod}['target'])) ? ${$requestMethod}['target'] : '';
49

    
50
// Include the WB functions file
51
if(!function_exists('directory_list')) { require(WB_PATH.'/framework/functions.php'); }
52

    
53
$directory = ($target == '/') ?  '' : $target;
54
$dirlink = 'index.php?dir='.$directory;
55
$rootlink = 'index.php?dir=';
56

    
57
// Check to see if target contains ../
58
if (!check_media_path($target, false))
59
{
60
	$admin->print_error($oTrans->MESSAGE_MEDIA_TARGET_DOT_DOT_SLASH );
61
}
62

    
63
// Create relative path of the target location for the file
64
$relative = WB_PATH.$target.'/';
65
$resizepath = str_replace(array('/',' '),'_',$target);
66

    
67
// Find out whether we should replace files or give an error
68
$overwrite = ($admin->get_post('overwrite') != '') ? true : false;
69

    
70
$file_extension_string = '';
71
// Get list of file types to which we're supposed to append 'txt'
72
$sql = 'SELECT `value` FROM  `'.TABLE_PREFIX. 'settings` '.
73
       'WHERE `name`=\'rename_files_on_upload\'';
74
if( ($file_extension_string = $database->get_one($sql))=='' ) {
75
//    $aResult = $oRes->fetchRow(MYSQL_ASSOC);
76
//    $file_extension_string = $aResult['value'];
77

    
78
}
79

    
80
$file_extensions=explode(",",$file_extension_string);
81
// get from settings and add to forbidden list
82
$forbidden_file_types  = preg_replace( '/\s*[,;\|#]\s*/','|',RENAME_FILES_ON_UPLOAD);
83
// Loop through the files
84
$good_uploads = 0;
85
$sum_dirs = 0;
86
$sum_files = 0;
87

    
88
for($count = 1; $count <= 10; $count++)
89
{
90
	// If file was upload to tmp
91
	if(isset($_FILES["file$count"]['name']))
92
	{
93
		// Remove bad characters
94
		$filename = trim(media_filename($_FILES["file$count"]['name']),'.') ;
95
		// Check if there is still a filename left
96
		// if($filename != '') {
97
		$info = pathinfo($filename);
98
		$ext = isset($info['extension']) ? $info['extension'] : '';
99

    
100
		if ( ($filename != '') && !preg_match("/" . $forbidden_file_types . "$/i", $ext) )
101
		{
102
			// Move to relative path (in media folder)
103
			if(file_exists($relative.$filename) AND $overwrite == true) {
104
				if(move_uploaded_file($_FILES["file$count"]['tmp_name'], $relative.$filename)) {
105
					$good_uploads++;
106
					$sum_files++;
107
					// Chmod the uploaded file
108
					change_mode($relative.$filename);
109
				}
110
			} elseif(!file_exists($relative.$filename)) {
111
				if(move_uploaded_file($_FILES["file$count"]['tmp_name'], $relative.$filename)) {
112
					$good_uploads++;
113
					$sum_files++;
114
					// Chmod the uploaded file
115
					change_mode($relative.$filename);
116
				}
117
			}
118

    
119

    
120
			if(file_exists($relative.$filename)) {
121

    
122
                $ImgWidth  = isset($pathsettings[$resizepath]['width'])  ? intval($pathsettings[$resizepath]['width'])  : null;
123
                $ImgHeigth = isset($pathsettings[$resizepath]['height']) ? intval($pathsettings[$resizepath]['height']) : null;
124

    
125
				if ($ImgWidth!=null || $ImgHeigth!=null ) {
126
                    if(!class_exists('PhpThumbFactory', false)){ include($modulePath.'/inc/ThumbLib.inc.php'); }
127
                	$oImage = PhpThumbFactory::create($relative.$filename);
128
                    $aOldSize = $oImage->getCurrentDimensions();
129
                    $ImgPercent = 50;
130

    
131
    				if ($ImgWidth!=null && $ImgHeigth==null ) {
132
                        $ImgPercent =  $ImgWidth*100/$aOldSize['width'];
133
                        $ImgHeigth = $ImgWidth;
134
                    } elseif( $ImgWidth==null && $ImgHeigth!=null ) {
135
                        $ImgPercent =  $ImgHeigth*100/$aOldSize['height'];
136
                        $ImgWidth = $ImgHeigth;
137
                    } else {
138
                        $ImgPercent = $ImgWidth*100/$aOldSize['width'];
139
                    }
140
                    $oImage->resize($ImgWidth,$ImgHeigth)->save($relative.$filename);
141
//                    $oImage->resizePercent($ImgPercent)->save($relative.$filename);
142
//                    $oImage->adaptiveResize($ImgWidth,$ImgHeigth)->save($relative.$filename);
143
//                    $oImage->save($relative.$filename);
144
				}
145

    
146
			}
147

    
148
			// store file name of first file for possible unzip action
149
			if ($count == 1) {
150
				$filename1 = $relative . $filename;
151
			}
152
		}
153
	}
154
}
155
/*
156
 * Callback function to skip files in black-list
157
 */
158
function pclzipCheckValidFile($p_event, &$p_header)
159
{
160
    //  return 1;
161
// Check for potentially malicious files
162
	$forbidden_file_types  = preg_replace( '/\s*[,;\|#]\s*/','|',RENAME_FILES_ON_UPLOAD);
163
	$info = pathinfo($p_header['filename']);
164
	$ext = isset($info['extension']) ? $info['extension'] : '';
165
	$dots = (substr($info['basename'], 0, 1) == '.') || (substr($info['basename'], -1, 1) == '.');
166
	if( !preg_match('/'.$forbidden_file_types.'$/i', $ext) && $dots != '.' )
167
	{	// ----- allowed file types are extracted
168
	  return 1;
169
	}else
170
	{	// ----- all other files are skiped
171
	  return 0;
172
	}
173
}
174
/* ********************************* */
175

    
176
// If the user chose to unzip the first file, unzip into the current folder
177
if (isset($_POST['unzip']) && isset($filename1) && file_exists($filename1) ) {
178
	// Required to unzip file.
179
	require_once(WB_PATH.'/include/pclzip/pclzip.lib.php');
180
	$archive = new PclZip($filename1);
181
	$list = $archive->extract(PCLZIP_OPT_PATH, $relative,PCLZIP_CB_PRE_EXTRACT, 'pclzipCheckValidFile');
182

    
183
	if($list == 0) {
184
		// error while trying to extract the archive (most likely wrong format)
185
		$admin->print_error('UNABLE TO UNZIP FILE' . $archive -> errorInfo(true));
186
	}
187
	$sum_files = 0;
188
	// rename executable files!
189
	foreach ($list as $key => $val) {
190
	    if( ($val['folder'] ) && change_mode($val['filename']) ) {
191
		   $sum_dirs++;
192
		} elseif( is_writable($val['filename']) && ($val['status'] == 'ok') && change_mode($val['filename']) )  {
193
			$sum_files++;
194
		}
195
	}
196
	if (isset($_POST['delzip'])) { unlink($filename1); }
197
	$dir = dirname($filename1);
198
    if(file_exists($dir)) {
199
		$array = createFolderProtectFile($dir);
200
    }
201
}
202
unset($list);
203

    
204
if($sum_files == 1) {
205
	$admin->print_success($sum_files.' '.$oTrans->MESSAGE_MEDIA_SINGLE_UPLOADED );
206
} elseif($sum_files > 1) {
207
	$admin->print_success($sum_files.' '.$oTrans->MESSAGE_MEDIA_UPLOADED );
208
} else {
209

    
210
	if(file_exists($relative.$filename)) {
211
    	$admin->print_error($oTrans->MESSAGE_MEDIA_FILE_EXISTS );
212
    } else {
213
    	$admin->print_error($oTrans->MESSAGE_MEDIA_NO_FILE_UPLOADED );
214
    }
215
}
216

    
217
// Print admin
218
$admin->print_footer();
(14-14/14)