<?php

  /*上传图片*/
@ require ('readonly/function/confirm_power.php');
define('POWER', confirm_power());


//删除目录
function deldir($dir) {
  if (empty($dir) || !file_exists($dir)) return;
  if ($fp = @opendir($dir)) {
    while (false !== ($file = @readdir($fp))) {
      if ($file != '.' && $file != '..') {
        if (is_dir($dir.'/'.$file)) {
          deldir($dir.'/'.$file);
        } else {
          @unlink($dir.'/'.$file);
        }
      }
    }
    if (readdir($fp) == false) {
      @closedir($fp);
      if (@rmdir($dir)) {
        return true;
      }
    }
  }
}


if (POWER != 5) {
  err('该命令必须以基本管理员身份登陆！请重登陆');
}

if ($_POST['act'] == 'del' && !empty($_POST['style_dir']) && file_exists('writable/css/style/'.$_POST['style_dir'].'')) {
  deldir('writable/css/style/'.$_POST['style_dir'].'');
  alert('风格删除成功。', 'webmaster_central.php?get=style');
}



if ((string)$_POST['styletype'] == '1') {
  if ($_POST['styletype_text'] == '' || !preg_match('/^([^\x00-\x7f]|\w|\.){3,45}$/', $_POST['styletype_text'])) {
    err('若选择自创类型不能空写！（中文、英文、数字）');
  }
  $styletype = $_POST['styletype_text'];
} else {
  if (empty($_POST['styletype'])) {
    err('请选择风格类型！');
  }
  $styletype = $_POST['styletype'];
}

if (empty($_POST['stylecolor'])) {
  err('请选择风格色调！');
}
if ($_POST['styletitle'] == '' || !preg_match('/^([^\x00-\x7f]|\w|\.){3,45}$/', $_POST['styletitle'])) {
  err('风格命名不能空写！（中文、英文、数字）');
}

//die($styletype.'-'.$_POST['styletitle']);

if (!empty($_POST['act'])) {
  $e_act = 'edit';
  $e_out = '编辑';
  list($old_type, $old_color, $old_title, $old_dir) = @explode('_', $_POST['act']);
  if (/*$old_type == $styletype && */$old_color == $_POST['stylecolor']/* && $old_title == $_POST['styletitle']*/) {
    $style_dir = 'writable/css/style/bj_'.$old_color.'_'.$old_dir;
  } else {
    //$style_dir = 'writable/css/style/bj_'.$_POST['stylecolor'].'_'.(abs(count(@glob('cache/bj_'.$_POST['stylecolor'].'_*'))) + 1);
    $style_dir_old = 'writable/css/style/bj_'.$old_color.'_'.$old_dir;
    $style_dir = 'writable/css/style/bj_'.$_POST['stylecolor'].'_'.gmdate("YmdHis", time() + (floatval($web["time_pos"]) * 3600));
    @rename($style_dir_old, $style_dir);
  }

} else {
  $e_out = '创建';
  //$style_dir = 'writable/css/style/bj_'.$_POST['stylecolor'].'_'.(abs(count(@glob('cache/bj_'.$_POST['stylecolor'].'_*'))) + 1);
  $style_dir = 'writable/css/style/bj_'.$_POST['stylecolor'].'_'.gmdate("YmdHis", time() + (floatval($web["time_pos"]) * 3600));
}

if (!file_exists($style_dir)) {
  if (!@mkdir($style_dir)) {
    err('尝试创建风格目录['.$style_dir.']失败！可能空间权限不足');
  }
}
//@chmod($style_dir, 0777);
eval('@chmod($style_dir, 0'.$web['chmod'].');');
@ require ('readonly/function/img.php');
/*
[global_value] => 2M
[local_value] => 2M
[access] => 6
*/

$err = '';

if (!empty($_POST['act'])) {
  $up_stylebackground = $up_stylethumb = 0;
  if (!is_array($_FILES['stylebackground']) || !$_FILES['stylebackground']['size']) {
    $up_stylebackground = 1;
  }
  if (!is_array($_FILES['stylethumb']) || !$_FILES['stylethumb']['size']) {
    $up_stylethumb = 1;
  }
}


$inis = ini_get_all();
$uploadmax = $inis['upload_max_filesize'];
$max_size = 100 * 10000;

if (!isset($up_stylebackground) || (isset($up_stylebackground) && $up_stylebackground == 0)) {
  if (!is_array($_FILES['stylebackground']) || !$_FILES['stylebackground']['size']) {
    err('提示：上传背景图片出现空值！原因分析：1、上传图片为空 2、图片超过系统最大上传限量'.$uploadmax['global_value'].'（'.$uploadmax['local_value'].'）');
  }
  if ($_FILES['stylebackground']['size'] > $max_size) {
    err('提示：上传的背景图片请小于'.($max_size/1000).'KB');
  }
  if(!preg_match('/\.(gif|jpg|png)$/i', $_FILES['stylebackground']['name'], $format_background)){
    err('提示：上传背景图片格式限（gif|jpg|png）');
  }
  if ($fp = @fopen($_FILES['stylebackground']['tmp_name'], 'rb')) {
    $img_contents = @fread($fp, $_FILES['stylebackground']['size']);
    @fclose($fp);
  } else {
    $img_contents = @file_get_contents($_FILES['stylebackground']['tmp_name']);
  }
  if (preg_match('/<\?php|eval|POST|base64_decode|base64_encode/i', $img_contents, $m_err)) {
    err('提示！禁止提交。该文件含有禁止的代码'.str_replace('?', '\?', $m_err[0]).'。');
  }
  if (move_uploaded_file($_FILES['stylebackground']['tmp_name'], $style_dir.'/body.'.$format_background[1])) {
    typeto($style_dir.'/body.'.$format_background[1], 'jpg');
    //err('图片上传成功！', 'ok');
  } else {
    $err .= '提示：背景图片上传出现错误！';
  }
}
if (!isset($up_stylethumb) || (isset($up_stylethumb) && $up_stylethumb == 0)) {
  if (!is_array($_FILES['stylethumb']) || !$_FILES['stylethumb']['size']) {
    err('提示：上传缩略图片出现空值！原因分析：1、上传图片为空 2、图片超过系统最大上传限量'.$uploadmax['global_value'].'（'.$uploadmax['local_value'].'）');
  }
  if ($_FILES['stylethumb']['size'] > $max_size) {
    err('提示：上传的缩略图片请小于'.($max_size/1000).'KB');
  }
  if(!preg_match('/\.(gif|jpg|png)$/i',$_FILES['stylethumb']['name'], $format_thumb)){
    err('提示：上传缩略图片格式限（gif|jpg|png）');
  }
  if ($fp = @fopen($_FILES['stylethumb']['tmp_name'], 'rb')) {
    $img_contents = @fread($fp, $_FILES['stylethumb']['size']);
    @fclose($fp);
  } else {
    $img_contents = @file_get_contents($_FILES['stylethumb']['tmp_name']);
  }
  if (preg_match('/<\?php|eval|POST|base64_decode|base64_encode/i', $img_contents, $m_err)) {
    err('提示！禁止提交。该文件含有禁止的代码'.str_replace('?', '\?', $m_err[0]).'。');
  }
  if (move_uploaded_file($_FILES['stylethumb']['tmp_name'], $style_dir.'/thumb.'.$format_thumb[1])) {
    $style_thumb = typeto($style_dir.'/thumb.'.$format_thumb[1], 'jpg');
    if (!run_img_resize($style_thumb, $style_thumb, 0, 0, 136, 92, false, false, $web['pic_quality'])) {
      err('提示：缩略图片生成出现错误！');
    }
    //err('图片上传成功！', 'ok');
  } else {
    $err .= '提示：缩略图片上传出现错误！';
  }
}



if ($format_background[1] != 'jpg') {
  @unlink($style_dir.'/body.'.$format_background[1]);
}
if ($format_thumb[1] != 'jpg') {
  @unlink($style_dir.'/thumb.'.$format_thumb[1]);
}





if ($err == '') {
  @copy('writable/css/style/color_'.$_POST['stylecolor'].'/css_bj_base.css', $style_dir.'/css.css');
@ require ('readonly/function/write_file.php');
  write_file($style_dir.'/title.txt', $styletype.'-'.$_POST['styletitle']);
  deldir($style_dir_old);
  alert('风格'.$e_out.'成功。', 'webmaster_central.php?get=style&act='.$e_act.'');
} else {
  err($err);
}

?>