PageRenderTime 471ms CodeModel.GetById 117ms app.highlight 231ms RepoModel.GetById 109ms app.codeStats 0ms

/source/admincp/admincp_cloudaddons.php

https://github.com/jinbo51/DiscuzX
PHP | 209 lines | 194 code | 9 blank | 6 comment | 58 complexity | e2a3d817147df44e1cea5a2902dba439 MD5 | raw file
  1<?php
  2
  3/**
  4 *      [Discuz!] (C)2001-2099 Comsenz Inc.
  5 *      This is NOT a freeware, use is subject to license terms
  6 *
  7 *      $Id: admincp_cloudaddons.php 33369 2013-06-03 05:00:29Z andyzheng $
  8 */
  9if(!defined('IN_DISCUZ') || !defined('IN_ADMINCP')) {
 10	exit('Access Denied');
 11}
 12
 13require_once libfile('function/cloudaddons');
 14
 15cpheader();
 16
 17if(!$admincp->isfounder) {
 18	cpmsg('noaccess_isfounder', '', 'error');
 19}
 20
 21if(!$operation) {
 22
 23	cloudaddons_check();
 24	shownav('cloudaddons');
 25	$extra = '';
 26	if(!empty($_GET['id'])) {
 27		$extra .= '&mod=app&ac=item&id='.rawurlencode($_GET['id']);
 28	}
 29	if(!empty($_GET['extra'])) {
 30		$extra .= '&'.addslashes($_GET['extra']);
 31	}
 32	$url = cloudaddons_url($extra);
 33	echo '<script type="text/javascript">location.href=\''.$url.'\';</script>';
 34
 35} elseif($operation == 'download') {
 36	$step = intval($_GET['step']);
 37	$addoni = intval($_GET['i']);
 38	$uniqueid = $_G['setting']['siteuniqueid'] ? $_G['setting']['siteuniqueid'] : C::t('common_setting')->fetch('siteuniqueid');
 39	if(!$_GET['md5hash'] || md5($_GET['addonids'].md5($uniqueid.$_GET['timestamp'])) != $_GET['md5hash']) {
 40		cpmsg('cloudaddons_validator_error', '', 'error');
 41	}
 42	$addonids = explode(',', $_GET['addonids']);
 43	list($_GET['key'], $_GET['type'], $_GET['rid']) = explode('.', isset($addonids[$addoni]) ? $addonids[$addoni] : $addonids[0]);
 44	if($step == 0) {
 45		cpmsg('cloudaddons_downloading', "action=cloudaddons&operation=download&addonids=$_GET[addonids]&i=$addoni&step=1&md5hash=".$_GET['md5hash'].'&timestamp='.$_GET['timestamp'], 'loading', array('addonid' => $_GET['key'].'.'.$_GET['type']), '<div>0%</div>', FALSE);
 46	} elseif($step == 1) {
 47		$packnum = isset($_GET['num']) ? $_GET['num'] : 0;
 48		$tmpdir = DISCUZ_ROOT.'./data/download/'.$_GET['rid'];
 49		$end = '';
 50		$md5tmp = DISCUZ_ROOT.'./data/download/'.$_GET['rid'].'.md5';
 51		if($packnum) {
 52			list($md5total, $md5s) = unserialize(implode('', @file($md5tmp)));
 53			dmkdir($tmpdir, 0777, false);
 54		} else {
 55			dir_clear($tmpdir);
 56			@unlink($md5tmp);
 57			dmkdir($tmpdir, 0777, false);
 58			$md5total = '';
 59			$md5s = array();
 60		}
 61		$data = cloudaddons_open('&mod=app&ac=download&rid='.$_GET['rid'].'&packnum='.$packnum);
 62		$_GET['importtxt'] = $data;
 63		$array = getimportdata('Discuz! File Pack');
 64		if(!$array['Status']) {
 65			list($_cur, $_max) = explode('/', $array['part']);
 66			$percent = intval($_cur/$_max * 100);
 67			if($array['type'] != $_GET['type'] || $array['key'] != $_GET['key'] || !$array['files']) {
 68				dir_clear($tmpdir);
 69				@unlink($md5tmp);
 70				cloudaddons_faillog($_GET['rid'], 100);
 71				cpmsg('cloudaddons_download_error', '', 'error', array('ErrorCode' => 100));
 72			}
 73			foreach($array['files'] as $file => $data) {
 74				$filename = $tmpdir.'/'.$file.'._addons_';
 75				$dirname = dirname($filename);
 76				dmkdir($dirname, 0777, false);
 77				$fp = fopen($filename, !$data['Part'] ? 'w' : 'a');
 78				if(!$fp) {
 79					dir_clear($tmpdir);
 80					@unlink($md5tmp);
 81					cloudaddons_faillog($_GET['rid'], 101);
 82					cpmsg('cloudaddons_download_write_error', '', 'error');
 83				}
 84				fwrite($fp, gzuncompress(base64_decode($data['Data'])));
 85				fclose($fp);
 86				if($data['MD5']) {
 87					$md5total .= $data['MD5'];
 88					$md5s[$filename] = $data['MD5'];
 89				}
 90			}
 91			$fp = fopen($md5tmp, 'w');
 92			fwrite($fp, serialize(array($md5total, $md5s)));
 93			fclose($fp);
 94		} elseif($array['Status'] == 'Error') {
 95			dir_clear($tmpdir);
 96			@unlink($md5tmp);
 97			cloudaddons_faillog($_GET['rid'], $array['ErrorCode']);
 98			cpmsg('cloudaddons_install_error', '', 'error', array('ErrorCode' => $array['ErrorCode']));
 99		} else {
100			foreach($md5s as $file => $md5) {
101				if($md5 != md5_file($file)) {
102					dir_clear($tmpdir);
103					@unlink($md5tmp);
104					cloudaddons_faillog($_GET['rid'], 102);
105					cpmsg('cloudaddons_download_error', '', 'error', array('ErrorCode' => 102));
106				}
107			}
108			@unlink($md5tmp);
109			$end = rawurlencode(cloudaddons_http_build_query($array));
110		}
111		if(!$end) {
112			$packnum++;
113			cpmsg('cloudaddons_downloading', "action=cloudaddons&operation=download&addonids=$_GET[addonids]&i=$addoni&step=1&md5hash=".$_GET['md5hash'].'&timestamp='.$_GET['timestamp'].'&num='.$packnum, 'loading', array('addonid' => $_GET['key'].'.'.$_GET['type']), '<div>'.$percent.'%</div>', FALSE);
114		} else {
115			if($md5total !== '' && md5($md5total) !== cloudaddons_md5($_GET['key'].'_'.$_GET['rid'])) {
116				dir_clear($tmpdir);
117				@unlink($md5tmp);
118				cloudaddons_faillog($_GET['rid'], 105);
119				cpmsg('cloudaddons_download_error', '', 'error', array('ErrorCode' => 105));
120			}
121			cpmsg('cloudaddons_installing', "action=cloudaddons&operation=download&addonids=$_GET[addonids]&i=$addoni&end=$end&step=2&md5hash=".$_GET['md5hash'].'&timestamp='.$_GET['timestamp'], 'loading', array('addonid' => $_GET['key'].'.'.$_GET['type']), FALSE);
122		}
123	} elseif($step == 2) {
124		$tmpdir = DISCUZ_ROOT.'./data/download/'.$_GET['rid'];
125		if(!file_exists($tmpdir)) {
126			dir_clear($tmpdir);
127			cloudaddons_faillog($_GET['rid'], 103);
128			cpmsg('cloudaddons_download_error', '', 'error', array('ErrorCode' => 103));
129		}
130		$typedir = array(
131		    'plugin' => 'source/plugin',
132		    'template' => 'template',
133		    'pack' => '.',
134		);
135		if(!$typedir[$_GET['type']]) {
136			dir_clear($tmpdir);
137			cloudaddons_faillog($_GET['rid'], 104);
138			cpmsg('cloudaddons_download_error', '', 'error', array('ErrorCode' => 104));
139		}
140		if($_GET['type'] != 'pack') {
141			$descdir = DISCUZ_ROOT.$typedir[$_GET['type']].'/';
142			$subdir = $_GET['key'];
143		} else {
144			$descdir = DISCUZ_ROOT;
145			$subdir = '';
146		}
147		$unwriteabledirs = cloudaddons_dirwriteable($descdir, $subdir, $tmpdir);
148		if($unwriteabledirs) {
149			if(!submitcheck('settingsubmit')) {
150				showtips(cplang('cloudaddons_unwriteabledirs', array('basedir' => $typedir[$_GET['type']] != '.' ? $typedir[$_GET['type']] : '/', 'unwriteabledirs' => implode(', ', $unwriteabledirs))));
151				siteftp_form("cloudaddons&operation=download&addonids=$_GET[addonids]&i=$addoni&end=".rawurlencode($_GET['end'])."&step=2&md5hash=".$_GET['md5hash'].'&timestamp='.$_GET['timestamp']);
152				exit;
153			} else {
154				siteftp_check($_GET['siteftp'], $typedir[$_GET['type']]);
155			}
156		}
157		$descdir .= $subdir;
158		cloudaddons_comparetree($tmpdir, $descdir, $tmpdir, $_GET['key'].'.'.$_GET['type'], 1);
159		if(!empty($_G['treeop']['oldchange']) && empty($_GET['confirmed'])) {
160			cpmsg('cloudaddons_install_files_changed', '', 'form', array('files' => implode('<br />', $_G['treeop']['oldchange'])));
161		}
162		cloudaddons_copytree($tmpdir, $descdir);
163		cloudaddons_savemd5($_GET['key'].'.'.$_GET['type'], $_GET['end'], $_G['treeop']['md5']);
164		cloudaddons_deltree($tmpdir);
165		if(count($addonids) - 1 > $addoni) {
166			$addoni++;
167			cpmsg('cloudaddons_downloading', "action=cloudaddons&operation=download&addonids=$_GET[addonids]&i=$addoni&step=1&md5hash=".$_GET['md5hash'].'&timestamp='.$_GET['timestamp'], 'loading', array('addonid' => $_GET['key'].'.'.$_GET['type']), FALSE);
168		}
169		list($_GET['key'], $_GET['type'], $_GET['rid']) = explode('.', $addonids[0]);
170		cloudaddons_downloadlog($_GET['key'].'.'.$_GET['type']);
171		if($_GET['type'] == 'plugin') {
172			$plugin = C::t('common_plugin')->fetch_by_identifier($_GET['key']);
173			if(!$plugin['pluginid']) {
174				dheader('location: '.ADMINSCRIPT.'?action=plugins&operation=import&dir='.$_GET['key']);
175			} else {
176				dheader('location: '.ADMINSCRIPT.'?action=plugins&operation=upgrade&pluginid='.$plugin['pluginid']);
177			}
178		} elseif($_GET['type'] == 'template') {
179			dheader('location: '.ADMINSCRIPT.'?action=styles&operation=import&dir='.$_GET['key']);
180		} else {
181			cloudaddons_validator($_GET['key'].'.pack');
182			cloudaddons_installlog($_GET['key'].'.pack');
183			if(file_exists(DISCUZ_ROOT.'./data/addonpack/'.$_GET['key'].'.php')) {
184				dheader('location: '.$_G['siteurl'].'data/addonpack/'.$_GET['key'].'.php');
185			}
186			cpmsg('cloudaddons_pack_installed', '', 'succeed');
187		}
188	}
189}
190
191function dir_clear($dir) {
192	if($directory = @dir($dir)) {
193		while($entry = $directory->read()) {
194			if($entry == '.' || $entry == '..') {
195				continue;
196			}
197			$filename = $dir.'/'.$entry;
198			if(is_file($filename)) {
199				@unlink($filename);
200			} else {
201				dir_clear($filename);
202			}
203		}
204		$directory->close();
205		@rmdir($dir);
206	}
207}
208
209?>