<?php
	/**************************************************************************\
	* Simple Groupware 0.724                                                   *
	* http://www.simple-groupware.de                                           *
	* Copyright (C) 2002-2011 by Thomas Bley                                   *
	* ------------------------------------------------------------------------ *
	*  This program is free software; you can redistribute it and/or           *
	*  modify it under the terms of the GNU General Public License Version 2   *
	*  as published by the Free Software Foundation; only version 2            *
	*  of the License, no later version.                                       *
	*                                                                          *
	*  This program is distributed in the hope that it will be useful,         *
	*  but WITHOUT ANY WARRANTY; without even the implied warranty of          *
	*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            *
	*  GNU General Public License for more details.                            *
	*                                                                          *
	*  You should have received a copy of the GNU General Public License       *
	*  Version 2 along with this program; if not, write to the Free Software   *
	*  Foundation, Inc., 59 Temple Place - Suite 330, Boston,                  *
	*  MA  02111-1307, USA.                                                    *
	\**************************************************************************/

// see https://developer.mozilla.org/en/Offline_resources_in_Firefox
// TODO2 images in cms??
// TODO2 fix resize iframe with images (graphviz)

define("NOCONTENT",true);
define("NOSESSION",true);
require("index.php");

sys_check_auth();
login_browser_detect();

$folder_offline = db_select_value("simple_sys_tree","id","anchor=@anchor@",array("anchor"=>"offline_".$_SESSION["username"]));
$rows = db_select("simple_offline","*","folder=@folder@","id asc","",array("folder"=>(int)$folder_offline));
if (!is_array($rows)) exit("{t}No entries found.{/t}");

if (isset($_REQUEST["manifest"])) {
  $files = array(
	"ext/lib/json/json.js",
	"ext/js/functions.js?".CORE_VERSION,
	"images.php?css_style=core&browser=".sys::$browser["name"]."&".CORE_VERSION,
	"ext/images/empty.gif",
	"ext/icons/lock.gif",
	"ext/icons/lock_open.gif",
	"ext/icons/download.gif",
	"ext/icons/contact.gif",
	"ext/icons/link.gif",
	"ext/icons/link_ext.gif",
	"ext/icons/link_mail.gif",
	"ext/icons/phone.gif"
  );
  foreach ($rows as $row) {
    $row = populate_row($row);
	if (empty($row["folder"])) continue;
	$files[] = $row["url"];

	$sgsml = new sgsml($row["folder"],$row["view"],array(),false);
	$width = isset($sgsml->current_view["IMAGE_WIDTH"])?$sgsml->current_view["IMAGE_WIDTH"]:"";
	$height = isset($sgsml->current_view["IMAGE_HEIGHT"])?$sgsml->current_view["IMAGE_HEIGHT"]:"";
	$preview = isset($sgsml->current_view["SHOW_PREVIEW"])?$sgsml->current_view["SHOW_PREVIEW"]:"";

	$key = array_search("id in (@item@)",$sgsml->where);
	if ($key !== false) unset($sgsml->where[$key]);

	$fields = array_merge(
	  $sgsml->get_fields_by_type(array("files","spreadsheet","graphviz")),
	  array("id")
	);

	if ($sgsml->tname=="simple_portal") {
	  $data_rows = $sgsml->get_rows(array("url"));
	  foreach ($data_rows as $row) $files[] = $row["url"]."&iframe=1";
	}
	if (count($fields)>1) {
	  $data_rows = $sgsml->get_rows($fields);
	  
	  foreach ($data_rows as $data_row) {
	    foreach ($fields as $field) {
		  if ($data_row[$field]=="") continue;
		  $type = $sgsml->fields[$field]["SIMPLE_TYPE"];
		  
		  if ($type=="files") {
			$items = explode("|",trim($data_row[$field],"|"));

			foreach ($items as $key=>$file) {
			  if (!file_exists($file)) continue;
			  $url = "download.php?folder2=".rawurlencode($row["folder"])."&view2=".$row["view"]."&filename=".rawurlencode(modify::basename($file))."&field=".$field."&item[]=".$data_row["id"]."&subitem=".$key;
		      if ($row["sync_files"]) $files[] = $url."&dispo=noinline";

			  if ($preview and modify::image_file($file)) {
			    if ($row["sync_files"]) $files[] = $url;
			    $files[] = $url."&image_width=".$width."&image_height=".$height;
			  }
			  if ($preview and modify::iframe_file($file)) $files[] = modify::previewlink($file);
			}
		  } else {
		    if (!is_call_type($type)) break;
		    $url = call_type($type, "offline_get_urls", $data_row[$field]);
		    if (!empty($url)) $files = array_merge($files, $url);
  } } } } }
  header("Content-type: text/cache-manifest");
  echo "CACHE MANIFEST\n";
  echo "# ".substr(sys_date("Y-m-d-H-i",NOW+600),0,-1)."0\n"; // re-sync: 10 minutes
  echo implode("\n",array_unique($files));
} else {
  foreach ($rows as $key=>$row) $rows[$key] = populate_row($row);
  uasort($rows, "sort_rows");  
  
  sys::$smarty->assign("sync", isset($_REQUEST["sync"]) ? 1 : 0);
  sys::$smarty->assign("sys", array(
    "app_title"=>APP_TITLE,
	"username"=>$_SESSION["username"],
	"browser"=>sys::$browser,
	"style"=>$_SESSION["style"]
  ));
  sys::$smarty->assign("rows", $rows);
  sys::$smarty->display("offline.tpl");
}

function sort_rows($a, $b) {
  if ($a["path"]==$b["path"]) return 0;
  if ($a["path"]<$b["path"]) return -1; else return 1;
}

function populate_row($row) {
  $row["view"] = "display";
  $row["folder"] = 0;
  if (($pos=strpos($row["url"],"?"))) {
    $url = array();
	parse_str(substr($row["url"],$pos+1),$url);
	$row = array_merge($row,$url);
  }
  $row["path"] = modify::getpath($row["folder"]);
  return $row;
}