<?php
if ( !defined( "SAXUE_CORE_INCLUDE" ) ) {
		include "../core.php";
} 
saxue_checkpower( 'plugin' );
define( 'CLOUDADDONS_WEBSITE_URL', 'http://addon.saxue.com' );
if ( !isset( $_REQUEST['action'] ) ) $_REQUEST['action'] = '';
include_once( SAXUE_ROOT_PATH . "/common/cache.php" );
include_once( SAXUE_ROOT_PATH . "/common/function.php" );
include_once( SAXUE_ROOT_PATH . "/model/plugin.php" );
$data_handler = &saxuepluginhandler :: getinstance( "saxuepluginhandler" );
$_plugindir = SAXUE_WEB_PATH . DIRECTORY_SEPARATOR . 'plugin';
switch ( $_REQUEST['action'] ) {
		// 安装
		case 'install' :
				$dir = $_GET['dir'];
				if ( empty( $dir ) || !is_dir( $_plugindir . DIRECTORY_SEPARATOR . $dir ) ) {
						saxue_printfail( '插件目录不存在' );
				}
				$configfile = $_plugindir . DIRECTORY_SEPARATOR . $dir . '/config.php';
				$installfile = $_plugindir . DIRECTORY_SEPARATOR . $dir . '/install.php';
				$upgradefile = $_plugindir . DIRECTORY_SEPARATOR . $dir . '/upgrade.php';
				if ( !file_exists( $configfile ) ) {
						saxue_printfail( '插件配置文件不存在' );
				}
				include( $configfile );
				if ( 0 < $data_handler -> getcount( new criteria( 'identifier', $pluginConfigs['identifier'] ) ) ) {
						saxue_printfail( '您已安装过该插件' );
				}
				if ( file_exists( $installfile ) ) {
						@include $installfile;
				}
				$pluginConfigs['dir'] = $dir;
				if ( !empty( $pluginConfigs['menu'] ) && is_array( $pluginConfigs['menu'] ) ) {
						$pluginConfigs['menu'] = serialize( $pluginConfigs['menu'] );
				}
				$data = $data_handler -> create( true, $pluginConfigs );
				if ( !$data_handler -> insert( $data ) ) {
						saxue_printfail( '插件安装失败' );
				} 
				if ( file_exists( $installfile ) ) {
						saxue_delfile( $installfile );
				}
				if ( file_exists( $upgradefile ) ) {
						saxue_delfile( $upgradefile );
				}
				saxue_delfile( $configfile );
				cache_plugin();
				saxue_jumppage( 'plugin.php', '插件安装成功' );
				break;
		// 更新
		case 'update' :
				$identifier = trim( $_REQUEST['identifier'] );
				if ( !isset( $saxuePlugin[$identifier] ) ) {
						saxue_printfail( '插件不存在' );
				}
				$configfile = $_plugindir . DIRECTORY_SEPARATOR . $saxuePlugin[$identifier]['dir'] . '/config.php';
				$installfile = $_plugindir . DIRECTORY_SEPARATOR . $saxuePlugin[$identifier]['dir'] . '/install.php';
				$upgradefile = $_plugindir . DIRECTORY_SEPARATOR . $saxuePlugin[$identifier]['dir'] . '/upgrade.php';
				if ( !file_exists( $configfile ) ) {
						saxue_printfail( '插件配置文件不存在' );
				}
				include( $configfile );
				$upret = false;
				if ( isset( $pluginConfigs['releases'] ) && $saxuePlugin[$identifier]['releases'] < $pluginConfigs['releases'] ) {
						if ( file_exists( $upgradefile ) ) {
								@include $upgradefile;
						}
						if ( !empty( $pluginConfigs['menu'] ) && is_array( $pluginConfigs['menu'] ) ) {
								$pluginConfigs['menu'] = serialize( $pluginConfigs['menu'] );
						}
						$data_handler -> updatefields( $pluginConfigs, 'pluginid=' . $saxuePlugin[$identifier]['pluginid'] );
						$upret = true;
				}
				if ( file_exists( $installfile ) ) {
						saxue_delfile( $installfile );
				}
				if ( file_exists( $upgradefile ) ) {
						saxue_delfile( $upgradefile );
				}
				saxue_delfile( $configfile );
				if ( $upret ) {
						saxue_jumppage( 'plugin.php', '插件已升级到最新版本 V' . $pluginConfigs['version'] . ' R' . $pluginConfigs['releases'] );
				} else {
						saxue_jumppage( 'plugin.php', '此插件已更新到最新版本' );
				}
				break;
		// 升级
		case 'upgrade' :
				$identifier = trim( $_REQUEST['identifier'] );
				if ( !isset( $saxuePlugin[$identifier] ) ) {
						saxue_printfail( '插件不存在' );
				}
				include_once( SAXUE_ROOT_PATH . "/common/funaddon.php" );
				$url = CLOUDADDONS_WEBSITE_URL . '/getdownload.php?action=upgrade&key=' . $identifier . '&releases=' . $saxuePlugin[$identifier]['releases'];
				$data = saxue_sockopen( $url, 999 );
				if ( empty( $data ) ) {
						saxue_printfail( '无法连接应用中心，请稍后再试' );
				}
				$data = json_decode( $data, TRUE );
				switch ( $data['flag'] ) {
						case 0 :
								saxue_printfail( $data['msg'] );
						case 1 :
								saxue_msgwin( '更新提示', $data['msg'] );
						case 2 :
								saxue_msgwin( '更新提示', '发现此插件最新版本 V' . $data['version'] . ' R' . $data['releases'] . '<br><br><a href="cloudaddons.php?id=' . $identifier . '">升级此插件</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="plugin.php">暂不升级</a>' );
				}
				break;
		// 卸载
		case 'uninstall' :
				$identifier = trim( $_REQUEST['identifier'] );
				if ( !isset( $saxuePlugin[$identifier] ) ) {
						saxue_printfail( '插件不存在' );
				}
				if ( $saxuePlugin[$identifier]['status'] ) {
						saxue_printfail( '请关闭插件后再卸载' );
				}
				$uninstallfile = $_plugindir . DIRECTORY_SEPARATOR . $saxuePlugin[$identifier]['dir'] . '/uninstall.php';
				if ( file_exists( $uninstallfile ) ) {
						@include $uninstallfile;
				}
				$data_handler -> delete( $saxuePlugin[$identifier]['pluginid'] );
				saxue_delfolder( $_plugindir . DIRECTORY_SEPARATOR . $saxuePlugin[$identifier]['dir'] );
				cache_plugin();
				saxue_jumppage( 'plugin.php', '插件卸载成功' );
				break;
		// 开启
		case 'open' :
				$identifier = trim( $_REQUEST['identifier'] );
				if ( !isset( $saxuePlugin[$identifier] ) ) {
						saxue_printfail( '插件不存在' );
				}
				$data_handler -> updatefields( 'status=1', 'pluginid=' . $saxuePlugin[$identifier]['pluginid'] );
				cache_plugin();
				saxue_jumppage( 'plugin.php', '插件已启用' );
				break;
		// 关闭
		case 'close' :
				$identifier = trim( $_REQUEST['identifier'] );
				if ( !isset( $saxuePlugin[$identifier] ) ) {
						saxue_printfail( '插件不存在' );
				}
				$data_handler -> updatefields( 'status=0', 'pluginid=' . $saxuePlugin[$identifier]['pluginid'] );
				cache_plugin();
				saxue_jumppage( 'plugin.php', '插件已关闭' );
				break;
		// 缓存
		case 'cache':
				cache_plugin();
				saxue_jumppage( 'plugin.php', '插件缓存更新完成' );
				break;
} 
$rows = $plugins = array();
foreach ( $saxuePlugin as $k => $v ) {
		$plugins[] = $v['dir'];
		if ( $v['status'] == 1 ) {
				$rows['open'][$k] = $v;
		} else {
				$rows['close'][$k] = $v;
		} 
}
$k = 0;
$_handle = @opendir( $_plugindir );
while ( $_file = @readdir( $_handle ) ) {
		if ( $_file != "." && $_file != ".." && is_dir( $_plugindir . DIRECTORY_SEPARATOR . $_file ) && !in_array( $_file, $plugins ) ) {
				if ( is_file( $_plugindir . DIRECTORY_SEPARATOR . $_file . '/config.php' ) ) {
						include( $_plugindir . DIRECTORY_SEPARATOR . $_file . '/config.php' );
						$rows['uninstall'][$k] = $pluginConfigs;
						$rows['uninstall'][$k]['dir'] = $_file;
						$rows['uninstall'][$k]['path'] = '/plugin/' . $rows['uninstall'][$k]['dir'];
						++$k;
				}
		} 
} 
@closedir( $_handle );
include_once( SAXUE_ADMIN_PATH . "/header.php" );
$saxueTpl -> assign( "rows", $rows );
$saxueTset['saxue_contents_template'] = SAXUE_ROOT_PATH . "/templates/admin/plugin.html";
include_once( SAXUE_ADMIN_PATH . "/footer.php" );
