<?php

require_once('violetUtil.class.php');
date_default_timezone_set("Asia/Ho_Chi_Minh");
createPackage();

function createHtmlFile($type, $scVer, $appName, $path)
{
  if ($type==3) $html = file_get_contents(VIOLET_APP_DIR."/Support/Scorm/Scorm{$scVer}/Scorm.html");
  else $html = file_get_contents(VIOLET_APP_DIR.'/Frame/Player.html');
  $html = str_replace('{Application Title}', $appName, $html);
  file_put_contents($path.'Index.html', $html);
}

function createManifestFile($scVer, $appName, $title, $orgXml, $resList, $path)
{
  for ($i = 0; $i < count($resList); $i++) $resList[$i] = '<file href="'.$resList[$i].'" />';
  $key = array(
    '{Application Title}', '{Lesson Title}', '{Lesson Organization}', '{Resources List}',
    '{MANIFEST UUID}', '{ORGANIZATION UUID}', '{ITEM UUID}', '{RESOURCE UUID}');
  $replace = array(
    $appName, $title, $orgXml, join("\r\n      ", $resList),
    'MANIFEST-01', 'ORGANIZATION-01', 'ITEM-01', 'RESOURCE-01');

  $scorm = file_get_contents(VIOLET_APP_DIR."/Support/Scorm/Scorm{$scVer}/imsmanifest.xml");
  $scorm = str_replace($key, $replace, $scorm);
  file_put_contents($path.'imsmanifest.xml', $scorm);
}

function createPackage()
{
  $userId = $_REQUEST['userid'];
  $appName = $_REQUEST['name'];
  $type = $_REQUEST['type'];
  $dataNum = $_REQUEST['dataNum'];
  $commonNum = $_REQUEST['commonNum'];
  $fileList = $_REQUEST['fileList'];
  if ($type == 1) $player = $_REQUEST['packagePlayer'];
  if ($type == 3) {
    $scVer = $_REQUEST['scormVersion'];
    $scTitle = $_REQUEST['scormTitle'];
    $scOrgXml = $_REQUEST['scormOrgData'];
  }
  $content = $_REQUEST['dataXml'];

  $fileList = explode(',', $fileList);
  $packFileList = array();
  $errList = array();

  // create package dir and create scenario file
  $timeStamp = date('YmdHis');
  $dest = violetUtil::getTempPath('packages')."/Package_{$timeStamp}_{$userId}_{$appName}/";
  if (file_exists($dest)) return;
  mkdir($dest, 0777, true);
  if ($commonNum > 0) mkdir($dest.'Common', 0777);
  if ($dataNum > 0) mkdir($dest.'Data', 0777);
  file_put_contents($dest.'Scenario.xvl', $content);

  // copy player file
  if ($type == 1) { // EXE for Win, Mac and Linux
    $dotPos = strrpos($player, '.');
    if ($dotPos === false) $ext = '';
    else $ext = substr($player, $dotPos);
    $playerSrc = VIOLET_APP_DIR.'/Frame/'.$player;
    if ($ext != '.zip') {
      $playerDst = $dest.$appName.$ext;
      copy($playerSrc, $playerDst);
      if ($ext == '') chmod($playerDst, 0755);
    } else {
      $playerDst = $dest.$appName.'.app';
      violetUtil::Unzip($playerSrc, $playerDst);
      chmod($playerDst.'/Contents/MacOS/Flash Player', 0755);
    }
  }
  else { // HTML + SWF for web base
    createHtmlFile($type, ($type==3? $scVer: ""), $appName, $dest);
    copy(VIOLET_APP_DIR.'/Frame/Player.swf', $dest.'Player.swf');
    array_push($packFileList, "Index.html");
    array_push($packFileList, "Player.swf");
    array_push($packFileList, "Scenario.xvl");
  }

  // copy list of data files + common files
  for ($i = 0; $i < count($fileList); $i += 2) {
    $data = file_get_contents(str_replace(' ', '%20', $fileList[$i]));
    if (!$data) array_push($errList, $fileList[$i]);
    else {
      file_put_contents($dest.$fileList[$i+1], $data);
      array_push($packFileList, $fileList[$i+1]);
    }
  }

  // create imsmanifest.xml and copy SCORM files
  if ($type == 3) {
    createManifestFile($scVer, $appName, $scTitle, $scOrgXml, $packFileList, $dest);
    $src = VIOLET_APP_DIR."/Support/Scorm/Scorm$scVer/";
    violetUtil::copyAllFile('*.xsd', $src, $dest);
    violetUtil::copyAllFile('*.dtd', $src, $dest);
  }

  // create zip package
  $zipFileName = substr($dest, 0, -1).'.zip';
  violetUtil::createZip($dest, $zipFileName);
  violetUtil::cleanDirectory($dest);

  echo "<url id='$timeStamp' />";
}
