<?php
// +---------------------------------------------+
// |     Copyright  2003 - 2005 weenCompany      |
// |     http://www.weentech.com                 |
// |     This file may not be redistributed.     |
// +---------------------------------------------+

define('IN_ADMIN', true);
define('IN_WEENCOMPANY', true);

$rootpath = "./../";

include($rootpath . 'includes/core.php');


$language       = $_POST['language'];
$charset        = 'UTF-8';
$version        = $_POST['version'];
$author         = $_POST['author'];
$includedefault = $_POST['includedefault'];

$getversion = $DB->query_first("SELECT value FROM " . TABLE_PREFIX . "mainsettings WHERE  title = 'version'");
$sdversion  = $getversion['value'];

$comments = "// weenCompany $sdversion 前臺語言 - $language \r\n";

if(strlen($author))
  $comments .= "// 由 $author 導出.\r\n\r\n";
else
  $comments .= "\r\n";

// write the start of the file
$languagefile = "<"."?php\r\n\r\n$comments\$charset = '$charset';\r\n\$version = '$version';";

// loop through all the modules and write out their language
$modules = $DB->query("SELECT moduleid, name FROM " . TABLE_PREFIX . "modules ORDER BY moduleid");

while($module = $DB->fetch_array($modules))
{
  $phrases = $DB->query("SELECT moduleid, varname, defaultphrase, customphrase FROM " . TABLE_PREFIX . "phrases WHERE moduleid = '" . $module['moduleid'] . "'");

  if( ($rows = $DB->get_num_rows($phrases)) > 0)
  {
    // moduleid 1 is used for weenCompany phrases
    if($module['moduleid'] == 1)
      $module['name'] = '系統程序';
    else
      $module['name'] = 'Module: ' . $module['name'];

    // open array
    $languagefile .= "\r\n\r\n// " . $module['name'] . "(" . $module['moduleid'] . ") \r\n\$sdphrases[" . $module['moduleid'] . "] = array \r\n( \r\n";

    while($phrase = $DB->fetch_array($phrases))
    {
      // write custom phrase if not empty
      if(strlen($phrase['customphrase']))
      {
        $languagefile .= "  '" . $phrase['varname'] . "'\t\t=>'" . str_replace("'", "\'", $phrase['customphrase']) . "',\r\n";
      }
      else if($includedefault == 1)
      {
        $languagefile .= "  '" . $phrase['varname'] . "'\t\t=>'" . str_replace("'", "\'", $phrase['defaultphrase']) . "',\r\n";
      }
    }

    // get rid of the last comma before closing the array
    $languagefile = substr($languagefile, 0, -3) . "\r\n";

    // close array
    $languagefile .= ");";

  } // end if the module has has phrase rows

} // end of while modules

// write the end of the file
$languagefile .= "\r\n\r\n?".">";



// path to custom language file
$filepath = "languages/custom_lang.php";

// chmod file
if(!is_writable($filepath))
  @chmod($filepath, 0776);

if(is_writable($filepath))
{
  // open the file
  if(!$handle = fopen($filepath, "w"))
  {
    echo "無法打開文件: $filepath";
    exit;
  }

  // write language file
  if(fwrite($handle, $languagefile) === FALSE)
  {
    echo "無法寫入文件: $filepath";
    exit;
  }

  // close file
  fclose($handle);

  // get file information
  $filelength = filesize($filepath);
  $filename   = basename($filepath);

  // begin writing headers
  header("Pragma: public");
  header("Expires: 0");
  header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  header("Cache-Control: public");
  header("Content-Description: File Transfer");
  header("Content-type: text/plain");

  // force the download
  $header="Content-Disposition: attachment; filename=".$filename.";";
  header($header );
  header("Content-Transfer-Encoding: binary");
  header("Content-Length: ".$filelength);
  @readfile($filepath);
}
else
{
  echo "文件 <b>$filepath</b> 不存在或不可寫!";
}




?>
