<?php
del_dir(ROOT_PATH . ADMIN_PATH . '/include/kindeditor');
del_dir(ROOT_PATH . 'include/smarty');
del_dir(ROOT_PATH . 'include/upload.class.php');

global $dou;

// 升级产品图片到新文件系统
if ($dou->table_exist('product')) {
    $query_product = $dou->query("SELECT id, image, add_time FROM " . $dou->table('product') . " ORDER BY id ASC");
    while ($row = $dou->fetch_array($query_product)) {
        $file_number = $dou->get_one("SELECT number FROM " . $dou->table('file') . " WHERE module = 'product' AND item_id = '$row[id]'");

        // 如果不存在文件库中则写入
        if (!$file_number && $row['image']) {
            $number = create_file_number();
            $size = file_exists(ROOT_PATH . $row['image']) ? filesize(ROOT_PATH . $row['image']) : 0;
            clearstatcache();

            // 缩略图
            $image = explode(".", $row['image']);
            $thumb = $image[0] . "_thumb." . $image[1];
            $thumb_size = file_exists(ROOT_PATH . $thumb) ? filesize(ROOT_PATH . $thumb) : 0;
            clearstatcache();

            // 写入文件数据
            $dou->query("INSERT INTO " . $dou->table('file') . " (id, number, file, module, item_id, type, size, thumb_size, action_time, add_time)" . " VALUES (NULL, '$number', '$row[image]', 'product', '$row[id]', 'main', '$size', '$thumb_size', '$row[add_time]',  '$row[add_time]')");

            // 写入新文件地址
            $dou->query("UPDATE " . $dou->table('product') . " SET image = '$number' WHERE id = '$row[id]'");
        }
        $file_number = '';
    }
}

// 升级文章图片到新文件系统
if ($dou->table_exist('article')) {
    $query_article = $dou->query("SELECT id, image, add_time FROM " . $dou->table('article') . " ORDER BY id ASC");
    while ($row = $dou->fetch_array($query_article)) {
        $file_number = $dou->get_one("SELECT number FROM " . $dou->table('file') . " WHERE module = 'article' AND item_id = '$row[id]'");

        // 如果不存在文件库中则写入
        if (!$file_number && $row['image']) {
            $number = create_file_number();
            $size = file_exists(ROOT_PATH . $row['image']) ? filesize(ROOT_PATH . $row['image']) : 0;
            clearstatcache();

            // 写入文件数据
            $dou->query("INSERT INTO " . $dou->table('file') . " (id, number, file, module, item_id, type, size, thumb_size, action_time, add_time)" . " VALUES (NULL, '$number', '$row[image]', 'article', '$row[id]', 'main', '$size', '0', '$row[add_time]',  '$row[add_time]')");

            // 写入新文件地址
            $dou->query("UPDATE " . $dou->table('article') . " SET image = '$number' WHERE id = '$row[id]'");
        }
        $file_number = '';
    }
}

// 升级幻灯图片到新文件系统
$query_show = $dou->query("SELECT id, show_img FROM " . $dou->table('show') . " ORDER BY id ASC");
while ($row = $dou->fetch_array($query_show)) {
    $file_number = $dou->get_one("SELECT number FROM " . $dou->table('file') . " WHERE module = 'show' AND item_id = '$row[id]'");
    $action_time = $add_time = time();
    
    // 如果不存在文件库中则写入
    if (!$file_number && $row['show_img']) {
        $number = create_file_number();
        $size = file_exists(ROOT_PATH . $row['show_img']) ? filesize(ROOT_PATH . $row['show_img']) : 0;
        clearstatcache();

        // 写入文件数据
        $dou->query("INSERT INTO " . $dou->table('file') . " (id, number, file, module, item_id, type, size, thumb_size, action_time, add_time)" . " VALUES (NULL, '$number', '$row[show_img]', 'show', '$row[id]', 'main', '$size', '$thumb_size', '$action_time',  '$add_time')");

        // 写入新文件地址
        $dou->query("UPDATE " . $dou->table('show') . " SET show_img = '$number' WHERE id = '$row[id]'");
    }
    $file_number = '';
}

// 将system.dou改成system.php
if (file_exists($system_file_old = ROOT_PATH . 'data/system.dou')) {
    $system_file_new = ROOT_PATH . 'data/system.php';
    $module = $GLOBALS['_MODULE']; // 读取模块列表
    $open_file = file($system_file_old);
    foreach ((array) $open_file as $line) {
        $line = trim($line);
        if (strpos($line, ':') !== false) {
            $system_file_old_content .= $line . "\r\n";
        }
    }

    // 系统文件内容
    $content = '<?php' . "\r\n";
    $content .= "'\r\n";
    $content .= $system_file_old_content;
    $content .= "\r\n";
    $content .= "'\r\n";
    $content .= '?>';

    // 将系统文件内容写入
    file_put_contents($system_file_new, $content);

    // 删除旧的系统文件
    @unlink($system_file_old);
 
    // 生成install.lock文件
    $install_lock = fopen(ROOT_PATH . 'data/install.lock', "w+");
    fwrite($install_lock, "DOUPHP INSTALLED");
}

/**
 * +----------------------------------------------------------
 * 创建一个随机且不重复的文件编号
 * +----------------------------------------------------------
 * $length 长度
 * +----------------------------------------------------------
 */
function create_file_number($length = 7) {
    // 字符集，可任意添加你需要的字符
    $chars = 'abcdefghijklmnopqrstuvwxyz123456789';

    $number = '';
    for ( $i = 0; $i < $length; $i++ ) {
        $number .= $chars[mt_rand(0, strlen($chars) - 1)];
    }
    $number = $number . '.file';

    if ($GLOBALS['dou']->value_exist('file', 'number', $number)) {
        create_file_number();
    } else {
        return $number;
    }
}

/**
 * +----------------------------------------------------------
 * 删除目录及目录下所有子目录和文件
 * +----------------------------------------------------------
 * $dir 要删除的目录
 * +----------------------------------------------------------
 */
function del_dir($dir) {
    if ($handle = @opendir($dir)) {
       // 删除目录下子目录和文件
       while (false !== ($item = @readdir($handle))) {  
           if ($item != '.' && $item != '..') {  
               if (is_dir( "$dir/$item")) {  
                   del_dir("$dir/$item");  
               } else {  
                   @unlink("$dir/$item");  
               }  
           }  
       }  
       closedir($handle);

       // 删除目录本身
       @rmdir($dir);  
    } 
}
?>