<?php
if ( !defined( "SAXUE_CORE_INCLUDE" ) ) {
		include "../core.php";
} 
saxue_checkpower( 'cloudaddons' );

define( 'CLOUDADDONS_WEBSITE_URL', 'http://addon.saxue.com' );
if ( !isset( $_REQUEST['action'] ) ) {
		$_REQUEST['action'] = '';
} 
include_once( SAXUE_DATA_PATH . "/version.php" );
include_once( SAXUE_ROOT_PATH . "/common/funaddon.php" );
$param = array();
$param['siteurl'] = rawurlencode( SAXUE_ADMIN_URL );
$param['version'] = $saxueVersion['edition'];
$param['release'] = $saxueVersion['release'];
if ( SAXUE_LICENSE != '' ) {
		$_license = explode( '|', base64_decode( SAXUE_LICENSE ) );
		if ( is_array( $_license ) && count( $_license ) == 2 ) {
				$param['licensecode'] = $_license[1];
		}
}
$param = http_build_query( $param );
$sitehash = md5( $param );

if ( empty( $_REQUEST['action'] ) ) {
		$url = CLOUDADDONS_WEBSITE_URL . '/?' . $param . '&sitehash=' . $sitehash;
		if ( !empty( $_GET['id'] ) ) {
				$url .= '&id=' . $_GET['id'];
		}
		echo '<script type="text/javascript">location.href=\'' . $url . '\';</script>';
} elseif ( $_REQUEST['action'] == 'download' ) {
		if ( !$_GET['sitehash'] || $_GET['sitehash'] != $sitehash ) {
				saxue_printfail( '校验失败，您无法下载此应用' );
		}
		$key = trim( $_GET['key'] );
		if ( empty( $key ) ) {
				saxue_printfail( '应用标识错误' );
		}
		$step = intval( $_GET['step'] );
		@set_time_limit( 0 );
		if ( $step == 0 ) {
				$id = intval( $_GET['id'] );
				saxue_run( '应用 ' . $key . '.' . $_GET['type'] . ' 下载中，请稍候 ......', '?action=download&key=' . $key . '&type=' . $_GET['type'] . '&id=' . $id . '&sitehash=' . $sitehash );
				$url = CLOUDADDONS_WEBSITE_URL . '/download.php?' . $param . '&sitehash=' . $sitehash . '&id=' . $id;
				$data = saxue_sockopen( $url, 999 );
				if ( empty( $data ) || strlen( $data ) < 20 ) {
						ob_clean();
						saxue_printfail( '应用下载错误（' . $data . '）' );
				}
				$zipfile = SAXUE_DATA_PATH . '/download/addon/';
				saxue_checkdir( $zipfile, true );
				$zipfile .= $key . '.zip';
				saxue_writefile( $zipfile, $data );
				saxue_jumppage( '?action=download&step=1&key=' . $key . '&type=' . $_GET['type'] . '&sitehash=' . $sitehash );
		} elseif ( $step == 1 ) {
				saxue_run( '应用 ' . $key . '.' . $_GET['type'] . ' 解压中，请稍候 ......', '?action=download&step=1&key=' . $key . '&type=' . $_GET['type'] . '&sitehash=' . $sitehash );
				include_once( SAXUE_ROOT_PATH . "/lib/util/pclzip.php" );
				$zipfile = SAXUE_DATA_PATH . '/download/addon/' . $key . '.zip';
				$unzipto = SAXUE_WEB_PATH;
				if ( $_GET['type'] != 'templates' ) {
						$unzipto .= '/plugin/';
				}
				$archive = new PclZip( $zipfile );
				if ( $archive -> extract( PCLZIP_OPT_PATH, $unzipto, PCLZIP_OPT_REPLACE_NEWER ) == 0 ) {
						saxue_delfile( $zipfile );
						ob_clean();
						saxue_printfail( '应用解压错误：<br>' . $archive -> errorInfo( true ) );
				}
				saxue_delfile( $zipfile );
				ob_clean();
				if ( $_GET['type'] == 'templates' ) {
						if ( isset( $saxuePlugin['template'] ) ) {
								saxue_jumppage( 'plugin.php', '模板解压完成，请进入模版中心设置模版 ......', 1 );
						}
						saxue_jumppage( 'language.php', '模板解压完成，请手工设置模板目录和风格目录 ......', 1 );
				}
				if ( isset( $saxuePlugin[$key] ) ) {
						saxue_jumppage( 'plugin.php?action=update&identifier=' . $key, '开始升级应用，请稍候 ......', 1 );
				}
				saxue_jumppage( 'plugin.php?action=install&dir=' . $key, '开始安装应用，请稍候 ......', 1 );
		}
}