<?php
/**
 * API: loading user preferences and data
 *
 * Usage: /egroupware/api/user.php?user=123
 *
 * @link www.egroupware.org
 * @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
 * @package api
 * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
 * @version $Id$
 */

use EGroupware\AiTools\Prompts;
use EGroupware\Api;
use EGroupware\AiTools;

// switch evtl. set output-compression off, as we cant calculate a Content-Length header with transparent compression
ini_set('zlib.output_compression', 0);

$GLOBALS['egw_info'] = array(
	'flags' => array(
		'currentapp' => 'api',
		'noheader' => true,
		'nocachecontrol' => true,
	)
);

include '../header.inc.php';

// release session, as we dont need it and it blocks parallel requests
$GLOBALS['egw']->session->commit_session();

$preferences = [];
foreach($GLOBALS['egw_info']['user']['preferences'] /*['addressbook', 'notifications', 'status', 'filemanager']*/ as $app => $prefs)
{
	if ($app === 'common')
	{
		$prefs['timezoneoffset'] = -Api\DateTime::$user_timezone->getOffset(new Api\DateTime('now')) / 60;
		$preferences[$app] = $prefs;
	}
	elseif (!empty($GLOBALS['egw_info']['user']['apps'][$app]))
	{
		$preferences[$app] = $prefs;
	}
}
$user = $GLOBALS['egw']->accounts->json($GLOBALS['egw_info']['user']['account_id']);
// add prompts if user has run-rights for AiTools
if (!empty($GLOBALS['egw_info']['user']['apps']['aitools']) && class_exists('\\EGroupware\\AiTools\\Prompts'))
{
	try {
		$prompts = (new AiTools\Bo())->get_predefined_prompts(false);
	}
	catch (\Throwable $e) {
		// ignore not configured / installed
	}
}
// use an etag over preferences, user-data and prompts
$etag = '"' . Api\Framework::user_etag($user, $prompts??'') . '"';

// headers to allow caching, egw_framework specifies etag on url to force reload, even with Expires header
Api\Session::cache_control(86400);	// cache for 1 day
Header('Content-Type: text/javascript; charset=utf-8');
Header('ETag: '.$etag);

// if servers send a If-None-Match header, response with 304 Not Modified, if etag matches
if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag)
{
	header("HTTP/1.1 304 Not Modified");
	exit;
}

$content = 'egw.set_user('.$user.", egw && egw.window !== window);\n";
foreach($preferences as $app => $data)
{
	$content .= 'egw.set_preferences('.json_encode($data).', '.json_encode($app).", egw && egw.window !== window);\n";
}

// add prompts if user has run-rights for AiTools
foreach(array_keys($prompts??[]) as $sub)
{
	if (isset($prompts[$sub]['children']))
	{
		$prompts[$sub]['children'] = array_values($prompts[$sub]['children']);
	}
}
if (!empty($prompts))
{
	$content .= 'egw.set_prompts('.json_encode(array_values($prompts)).");\n";
}

// we run our own gzip compression, to set a correct Content-Length of the encoded content
if (in_array('gzip', explode(',',$_SERVER['HTTP_ACCEPT_ENCODING'])) && function_exists('gzencode'))
{
	$content = gzencode($content);
	header('Content-Encoding: gzip');
}

// Content-Lenght header is important, otherwise browsers dont cache!
Header('Content-Length: '.bytes($content));
echo $content;