<? /*

Styles editor script for Intellect Board 2 Project

(C) 2004, XXXX Pro, United Open Project
Visit us online: http://intboard.ru

*/

if (!$IBOARD) die("Hack attempt!");

function st_select() {
  $link = $GLOBALS['link'];
  $sql="SELECT * FROM ".$GLOBALS['DBprefix']."StyleSet";
  $res=db_query($sql,$link);
  st_select_form_start();
  while ($sdata=db_fetch_array($res)) {
    st_select_form_entry($sdata);
  }
  st_select_form_end();
  $sql = "SELECT st_file,st_name FROM ".$GLOBALS['DBprefix']."StyleSet";
  $styleselect = build_select($sql);
  st_create_form($styleselect);
  st_delete_form($styleselect);
}

function st_list() {
  $style=getvar("style");
  if (strpos($style,".")!==false || strpos($style,"/")!==false) error(MSG_e_st_invalidname);
  $fsize=filesize($GLOBALS['opt_dir']."/styles/$style/$style.css");
  $fh=fopen($GLOBALS['opt_dir']."/styles/$style/$style.css","r");
  if (!$fh) error(MSG_e_style_badfile);
  $buffer=fread($fh,$fsize);
  fclose($fh);
  $stylelist=explode("}",$buffer);

  $fh=fopen($GLOBALS['opt_dir']."/langs/".$GLOBALS['inuser']['ln_file']."/$style.dat","r");
  if (!$fh) error(MSG_e_style_badfile);
  while (!feof($fh)) {
    $buffer=fgets($fh);
    list($tmp1,$tmp2)=explode("|",$buffer);
    $tmp1=trim($tmp1);
    $tmp2=trim($tmp2);
    $styledescr[$tmp1]=$tmp2;
  }
  fclose($fh);

  st_list_start();
  foreach ($stylelist as $curstyle) {
    list($stylename,$srtyledata)=explode("{",$curstyle);
    $stylename=trim($stylename);
    $styledata=trim($styledata);
    if ($stylename) st_list_entry($stylename,$styledata,$styledescr[$stylename],$style);
  }
  st_list_end();
  st_replace_form();
}

function st_edit() {
  $link=$GLOBALS['link'];
  $style=getvar("style");
  $fsize=filesize($GLOBALS['opt_dir']."/styles/$style/$style.css");
  $fh=fopen($GLOBALS['opt_dir']."/styles/$style/$style.css","r");
  if (!$fh) error(MSG_e_style_badfile);
  $buffer=fread($fh,$fsize);
  fclose($fh);
  $stylelist=explode("}",$buffer);

  foreach ($stylelist as $curstyle) {
    list($stylename,$styledata)=explode("{",$curstyle);
    $styleset[$stylename]=$styledata;
    $stylename=trim($stylename);
    $styledata=trim($styledata);
    if ($stylename==getvar("name")) {
      st_edit_start($style,$stylename);
      $styles = explode(";",$styledata);
      $defstyles=array("font-family","font-style","font-variant","font-weight","font-size","color","background-color","text-decoration","text-align","border","text-transform","margin","padding");
      foreach ($styles as $curstyle) {
        list($stname,$stdata)=explode(":",$curstyle);
        $st[strtolower(trim($stname))]=trim($stdata);
      }
      foreach ($defstyles as $curstyle) {
        st_edit_entry($curstyle,$st[$curstyle],constant("MSG_css_".str_replace("-","_",$curstyle)));
        unset($st[$curstyle]);
      }
      foreach ($st as $curstyle=>$curdata) {
        if ($curstyle) st_edit_entry($curstyle,$curdata);
      }
      st_edit_end();
    }
  }
}

function st_save() {
  check_post();
  $style=getvar("style");
  if (strpos($style,".")!==false || strpos($style,"/")!==false) error(MSG_e_st_invalidname);
  $name=getvar("name");
  $fsize=filesize($GLOBALS['opt_dir']."/styles/$style/$style.css");
  $fh=fopen($GLOBALS['opt_dir']."/styles/$style/$style.css","r");
  if (!$fh) error(MSG_e_style_badfile);
  $buffer=fread($fh,$fsize);
  fclose($fh);
  $stylelist=explode("}",$buffer);

  $buffer="";
  foreach ($stylelist as $curstyle) {
    list($stylename,$srtyledata)=explode("{",$curstyle);
    $stylename=trim($stylename);
    if ($stylename) {
      if ($stylename!=getvar("name")) $buffer.="$curstyle}";
      else {
        $buffer.="\n$name { ";
        $st=$_POST['st'];
        foreach ($st as $stname=>$stdata) if ($stname) $buffer.="$stname: $stdata; ";
        $buffer.=getvar("more")."} ";
      }
    }
  }

  $fh=fopen($GLOBALS['opt_dir']."/styles/$style/$style.css","w");
  fwrite($fh,$buffer,strlen($buffer));
  fclose($fh);
  ad_message(MSG_st_saved,MSG_st_return,"index.php?m=styles&a=st_list&style=$style");
}

function st_delete() {
  check_post();
  $style=getvar("style");
  if (strpos($style,".")!==false || strpos($style,"/")!==false) error(MSG_e_st_invalidname);
  $name=getvar("name");
  $fsize=filesize($GLOBALS['opt_dir']."/styles/$style/$style.css");
  $fh=fopen($GLOBALS['opt_dir']."/styles/$style/$style.css","r");
  if (!$fh) error(MSG_e_style_badfile);
  $buffer=fread($fh,$fsize);
  fclose($fh);
  $stylelist=explode("}",$buffer);

  $buffer="";
  foreach ($stylelist as $curstyle) {
    list($stylename,$srtyledata)=explode("{",$curstyle);
    $stylename=trim($stylename);
    if ($stylename && $stylename!=getvar("name")) $buffer.="$curstyle}";
  }

  $fh=fopen($GLOBALS['opt_dir']."/styles/$style/$style.css","w");
  fwrite($fh,$buffer,strlen($buffer));
  fclose($fh);
  ad_message(MSG_st_deleted,MSG_st_return,"index.php?m=styles&a=st_list&style=$style");
}

function st_create_set() {
  check_post();
  $style=getvar("st_parent");
  $newstyle=getvar("st_file");
  $newname=getvar("st_name");
  if (strpos($newstyle,".")!==false || strpos($newstyle,"/")!==false) error(MSG_e_st_invalidname);
  if (strpos($style,".")!==false || strpos($style,"/")!==false) error(MSG_e_st_invalidname);
  $link=$GLOBALS['link'];

  if (is_dir($GLOBALS['opt_dir']."/styles/$newstyle")) error(MSG_e_st_alreadyexists);
  mkdir($GLOBALS['opt_dir']."/styles/$newstyle");
  mkdir($GLOBALS['opt_dir']."/styles/$newstyle/admin");
  if (!is_dir($GLOBALS['opt_dir']."/styles/$newstyle")) error(MSG_e_st_createdir);
  copy($GLOBALS['opt_dir']."/styles/$style/".$style.".css",$GLOBALS['opt_dir']."/styles/$newstyle/".$newstyle.".css");

  $dir=opendir($GLOBALS['opt_dir']."/styles/$style");
  while ($curfile=readdir($dir)) {
    if (is_file($GLOBALS['opt_dir']."/styles/$style/".$curfile) && strpos($curfile,".php")===false && $curfile!=$style.".css") {
      copy($GLOBALS['opt_dir']."/styles/$style/".$curfile,$GLOBALS['opt_dir']."/styles/$newstyle/".$curfile);
    }
  }
  closedir($dir);

  $sql = "SELECT ln_file FROM ".$GLOBALS['DBprefix']."Language";
  $res = db_query($sql,$link);
  while ($lnfile=db_fetch_row($res)) {
    if (file_exists($GLOBALS['opt_dir']."/langs/".$lnfile[0]."/$style.dat")) {
      copy($GLOBALS['opt_dir']."/langs/".$lnfile[0]."/$style.dat",$GLOBALS['opt_dir']."/langs/".$lnfile[0]."/$newstyle.dat");
    }
  }
  db_free_result($res);

  $sqldata = build_sql("st_");
  $sql = "INSERT INTO ".$GLOBALS['DBprefix']."StyleSet SET st_show=1, $sqldata";
  $res = db_query($sql,$link);
  ad_message(MSG_st_created,MSG_st_return,"index.php?m=styles&a=st_select");
}

function st_replace() {
  check_post();
  $style=getvar("style");
  if (strpos($style,".")!==false || strpos($style,"/")!==false) error(MSG_e_st_invalidname);
  $oldcolor = getvar("oldcolor");
  $newcolor = getvar("newcolor");
  $fsize=filesize($GLOBALS['opt_dir']."/styles/$style/$style.css");
  $fh=fopen($GLOBALS['opt_dir']."/styles/$style/$style.css","r");
  if (!$fh) error(MSG_e_style_badfile);
  $buffer=fread($fh,$fsize);
  fclose($fh);

  $buffer=str_replace("#".$oldcolor,"#".$newcolor,$buffer);

  $fh=fopen($GLOBALS['opt_dir']."/styles/$style/$style.css","w");
  fwrite($fh,$buffer,strlen($buffer));
  fclose($fh);
  ad_message(MSG_st_replaced,MSG_st_return,"index.php?m=styles&a=st_select");
}

function st_confirm_set() {
  $params['style']=getvar("style");
  confirm("styles","st_delete_set",$params,MSG_st_confirm_set,"index.php?m=styles&a=st_select");
}

function st_confirm() {
  $params['style']=getvar("style");
  $params['name']=getvar("name");
  confirm("styles","st_delete",$params,MSG_st_confirm,"index.php?m=styles&a=st_edit&style=$style");
}

function st_delete_set() {
  check_post();
  $style=getvar("style");
  if (strpos($style,".")!==false || strpos($style,"/")!==false) error(MSG_e_st_invalidname);
  $link=$GLOBALS['link'];

  $sql = "SELECT COUNT(*) FROM ".$GLOBALS['DBprefix']."StyleSet";
  $res = db_query($sql,$link);
  $count=db_fetch_row($res);
  db_free_result($res);
  if ($count[0]==1) error(MSG_e_st_last);

  $dir=opendir($GLOBALS['opt_dir']."/styles/$style/admin");
  while ($curfile=readdir($dir)) {
    if (is_file($GLOBALS['opt_dir']."/styles/$style/admin/".$curfile)) unlink($GLOBALS['opt_dir']."/styles/$style/admin/".$curfile);
  }
  closedir($dir);
  rmdir($GLOBALS['opt_dir']."/styles/$style/admin");

  $dir=opendir($GLOBALS['opt_dir']."/styles/$style");
  while ($curfile=readdir($dir)) {
    if (is_file($GLOBALS['opt_dir']."/styles/$style/".$curfile)) unlink($GLOBALS['opt_dir']."/styles/$style/".$curfile);
  }
  closedir($dir);
  rmdir($GLOBALS['opt_dir']."/styles/$style");

  $sql = "DELETE FROM ".$GLOBALS['DBprefix']."StyleSet WHERE st_file=\"$style\"";
  $res = db_query($sql,$link);
  ad_message(MSG_st_deleted,MSG_st_return,"index.php?m=styles&a=st_select");
}

function st_hide() {
  $style=getvar("style");
  $link=$GLOBALS['link'];
  $show = getvar("st_show");

  $sql = "UPDATE ".$GLOBALS['DBprefix']."StyleSet SET st_show=\"$show\" WHERE st_file=\"$style\"";
  $res = db_query($sql,$link);
  ad_message(MSG_st_changed,MSG_st_return,"index.php?m=styles&a=st_select");
}

function st_css() {
  $style=getvar('style');
  if (strpos($style,".")!==false || strpos($style,"/")!==false) error(MSG_e_st_invalidname);
  $fh=fopen($GLOBALS['opt_dir']."/styles/$style/$style.css","r");
  $stdata=fread($fh,filesize($GLOBALS['opt_dir']."/styles/$style/$style.css")+1);
  fclose($fh);
  st_edit_form($style,$stdata);
}

function st_save_css() {
  check_post();
  $style=getvar('style');
  if (strpos($style,".")!==false || strpos($style,"/")!==false) error(MSG_e_st_invalidname);
  $stdata=getvar('stdata_text');
  $fh=fopen($GLOBALS['opt_dir']."/styles/$style/$style.css","w");
  fputs($fh,$stdata);
  fclose($fh);
  ad_message(MSG_st_css_saved,MSG_st_return,"index.php?m=styles&a=st_select");
}