<?php

/*
*	Copyright VeryIDE,2007-2010
*	http://www.veryide.com/
*
*	$Id: veryide_files.php,v2 19:47 2008-2-21 Lay $
*/

require '../../include/loader.php';

html_start("更新管理 - VeryIDE");
?>

<!--body start-->
    
	<script type="text/javascript">
        //if( parent.parent.Mo.store.model == "modern" ){
                Mo("#wrapper").attr({"class":"snugly"});
        //}
        </script>
    
	<?php
	
		//更新基准目录
		$base = Database :: direct('update');
		
		$action = getgpc('action');
		$update = getgpc("update");

        if($action){
                
		//连接数据库
		System :: connect();
		
		switch($action){
	
				case "update":

						//检查权限
						$func = "sys-update-add";
						System :: check_func( $func, false );
		
						$res = Database :: update( $update );
						
						switch( $res ){
							
							case 'lock':
								echo '<div id="state" class="failure">抱歉！安装已经存在。更新于早前已经安装，并在使用中：<span class="text-key">'.$update.'</span></div>';					
							break;
							
							case 'stop':
								echo '<div id="state" class="failure">抱歉！安装更新失败。以下是本错误信息详细报告：</div>';					
							break;
							
							case 'done':
								echo '<div id="state">恭喜！成功安装更新：'.$update.'</div>';
							break;
							
						}
		
			break;
			
			case "execute":
	
				//检查权限
				$func = "sys-update-sql";
				System :: check_func( $func, false );
			
				$res = Database :: query( stripslashes( $_POST['sql'] ) );
				
				if( $res['error'] == 0 ){
					
					echo '<div id="state">恭喜！成功执行自定义 SQL 语句。</div>';
				
					//写入日志
					System :: insert_event($func,time(),time(),"执行查询：".$_POST['sql'] );
					
					//更新模块
					Module :: search();
							
				}else{
					
					echo '<div id="state" class="failure">抱歉！执行SQL语句失败。以下是本错误信息详细报告：</div>';
					
					//trigger_error(str_replace('class="error"','class="text-no"',preg_replace('/<h4>(.*?)<\/h4>/','',$array[1])), E_USER_ERROR);
				}
			
			break;
		}
				
		//关闭数据库
		System :: connect();
        
    }
    
    ?>

	<div id="box">
        
        <div class="item">数据更新清单</div>

    <table width="100%" border="0" cellpadding="1" cellspacing="1" class="table">
      <tr class="title">
        <td>ID</td>
        <td>更新名称</td>
        <td>适用版本</td>
        <td>更新状态</td>
        <td>发布时间</td>
        <td width="70">执行操作</td>
      </tr>
    <?php
	
        $list = loop_file( $base, array(), array('sql') );
        
        $x = 1;
    
        foreach( $list as $file ){
            
                //锁文件
                $lock = str_replace(".sql",".lock",$file);
                $val = file_exists($base.$lock);                
                
                //版本号
                $php = sreadfile($base.$file);
                
                preg_match("/Version: ([\d\.].*)/i", $php, $mathes);
                
                if(!empty($mathes[1])){
                    $ver = '<span class="text-yes">'.$mathes[1].'</span>';					
                }else{
                    $ver = '<em>未知</em>';	
                }
            
                echo ("<tr class='". zebra( $i, array( "line" , "band" ) ) ."'>");
                echo ('<td>'.( $x++ ).'</td>');
                echo ('<td class="text-yes">'.$file.'</td>');
                echo ('<td>'.$ver.'</td>');
                
                if ($val) {
                    echo ("<td>于 <span class='text-key'>".date("Y-m-d H:i:s",filemtime($base.$lock))."</span> 安装此更新</td>");
                    echo ("<td>".date("Y-m-d H:i:s",filemtime($base.$file))."</td>");
                }else{
                    echo ("<td><em>未安装此更新</em></td>");
                    echo ("<td>".date("Y-m-d H:i:s",filemtime($base.$file))."</td>");
                }
                
                if(!$val){

                    echo '
                    <td width="70"><input name="" value="安装" type="button" class="button" onclick="location.href=\'?action=update&update='.$file.'\';" /></td>';
                
                }else{
                    echo '<td>无</td>';
                }
                
                echo ("</tr>");
                
        }
        
        if( count( $list ) == 0 ){
                echo '<tr>     <td colspan="6" class="notice">更新清单暂时没有记录</td>      </tr>';
        }
    
    ?>
        
	</table>
    
	<?php
    if( System :: check_func('sys-update-sql') ){
    ?>
    
        <div class="item">执行 SQL 查询</div>
    
        <form action="" method="post" name="from1" mode="edit" verify="true">
        
        <table width="100%" cellpadding="0" cellspacing="0" class="form">
        
            <tr><td colspan="2" class="section">注意: 为确保升级成功，请不要修改官方发布的 SQL 语句任何部分。</td></tr>
            
        	<tr>
            	<td width="60"><strong>升级语句：</strong></td>
                <td>
					<textarea name="sql" style="width:700px;" rows="6" id="sql"></textarea>
                </td>
            </tr>
            
            <tr>
                <td></td>
                <td>
                    <input name="action" type="hidden" id="action" value="execute" />
                    <input type="submit" name="Submit" value="执行此语句" class="submit" />
                </td>
            </tr>
        
        </table>
        
        </form>
    
	<?php
    }
    ?>
    
    	</div>
	
</div>

<!--body end-->

<?php html_close();?>