<?php

/************************************************************************/
/* DOCEBO CORE - Framework												*/
/* ============================================							*/
/*																		*/
/* Copyright (c) 2006													*/
/* http://www.docebo.com												*/
/*																		*/
/* This program is free software. You can redistribute it and/or modify	*/
/* it under the terms of the GNU General Public License as published by	*/
/* the Free Software Foundation; either version 2 of the License.		*/
/************************************************************************/

/**
 * @package course management
 * @subpackage course catalogue
 * @category ajax server
 * @version $Id:$
 */

if(isset($_REQUEST['GLOBALS'])) die('GLOBALS overwrite attempt detected');
if(!defined("IN_DOCEBO")) define("IN_DOCEBO", true);

$path_to_root = '../..';

// prepare refer ------------------------------------------------------------------

require_once(dirname(__FILE__).'/'.$path_to_root.'/config.php');

require_once($GLOBALS['where_config'].'/config.php');
/*
if ($GLOBALS["where_cms_relative"] != false)
	$GLOBALS["where_cms_relative"]=$path_to_root.'/'.$GLOBALS["where_cms_relative"];

if ($GLOBALS["where_kms_relative"] != false)
	$GLOBALS["where_kms_relative"]=$path_to_root.'/'.$GLOBALS["where_kms_relative"];

if ($GLOBALS["where_lms_relative"] != false)
	$GLOBALS["where_lms_relative"]=$path_to_root.'/'.$GLOBALS["where_lms_relative"];

if ($GLOBALS["where_framework_relative"] != false)
	$GLOBALS["where_framework_relative"]=$path_to_root.'/'.$GLOBALS["where_framework_relative"];

*/
if ($GLOBALS["where_files_relative"] != false) {
	$GLOBALS["where_files_relative"]=$path_to_root.'/'.$GLOBALS["where_files_relative"];
}
ob_start();

// connect to database -------------------------------------------------------------------

$GLOBALS['dbConn'] = mysql_connect($GLOBALS['dbhost'], $GLOBALS['dbuname'], $GLOBALS['dbpass']);
if( !$GLOBALS['dbConn'] )
	die( "Can't connect to db. Check configurations" );

if( !mysql_select_db($dbname, $GLOBALS['dbConn']) )
	die( "Database not found. Check configurations" );

@mysql_query("SET NAMES '".$GLOBALS['db_conn_names']."'", $GLOBALS['dbConn']);
@mysql_query("SET CHARACTER SET '".$GLOBALS['db_conn_char_set']."'", $GLOBALS['dbConn']);

// load lms setting ------------------------------------------------------------------
require_once($GLOBALS['where_framework'].'/setting.php');
require_once($GLOBALS['where_lms'].'/setting.php');

session_name("docebo_session");
session_start();

// load regional setting --------------------------------------------------------------
require_once($GLOBALS['where_framework']."/lib/lib.regset.php");
$GLOBALS['regset'] = new RegionalSettings();

// load current user from session -----------------------------------------------------
require_once($GLOBALS['where_framework'].'/lib/lib.user.php');
$GLOBALS['current_user'] =& DoceboUser::createDoceboUserFromSession('public_area');

require_once($GLOBALS['where_framework'].'/lib/lib.lang.php');
require_once($GLOBALS['where_framework'].'/lib/lib.template.php');
require_once($GLOBALS['where_framework'].'/lib/lib.utils.php');

require_once($GLOBALS['where_lms'].'/lib/lib.permission.php');

// security check --------------------------------------------------------------------

chkInput($_GET);
chkInput($_POST);
chkInput($_COOKIE);

$GLOBALS['operation_result'] = '';
function docebo_cout($string) { $GLOBALS['operation_result'] .= $string; }

// here all the specific code ==========================================================

$op = importVar('op');

switch($op) {
	
	case "confirm_repo_delete" : {
		
		require_once($GLOBALS["where_lms"]."/lib/lib.light_repo.php");
		
		$lang =& DoceboLanguage::createInstance( 'standard', 'framework');
		$lang->setGlobal();
		$lang =& DoceboLanguage::createInstance("light_repo", "lms");
		
		$file_man 	= new LightRepoManager( getLogUserId(), $_SESSION['idCourse'] );
	
		$id_repo = importVar('id_repo', true, 0);
	
		// recovering file repository information
		$repo = false;
		$repo = $file_man->getRepoDetails($id_repo);
		
		$value = array(
			'id_repo' 		=> $id_repo,
			'win_title' 	=> $lang->def('_DEL_REPOSITORY'),
			'win_text' 		=> str_replace('[repo_title]', $repo[LR_TITLE], $lang->def('_DEL_REPOSITORY_TEXT')),
			'win_confirm' 	=> $lang->def('_CONFIRM'),
			'win_undo' 		=> $lang->def('_UNDO')
		);
		
		require_once($GLOBALS['where_framework'].'/lib/lib.json.php');
		$json = new Services_JSON();
		$output = $json->encode($value);
  		docebo_cout($output);
	};break;
	case "delete_repo" : {
		
		require_once($GLOBALS["where_lms"]."/lib/lib.light_repo.php");
		
		$lang =& DoceboLanguage::createInstance( 'standard', 'framework');
		$lang->setGlobal();
		$lang =& DoceboLanguage::createInstance("light_repo", "lms");
		
		$id_repo = importVar('id_repo', true, 0);
		$value = array('result' => false, 'id_repo' => $id_repo , 'result_text' => '');
		
		if(!checkPerm('mod', true, 'light_repo')) { $value['result_text'] = 'You can\'t access !'; }
		else {
			
			$file_man = new LightRepoManager( getLogUserId(), $_SESSION['idCourse'] );
			if($file_man->deleteRepo($id_repo)) {
				$value['result'] = true;
				$value['result_text'] = $lang->def('_OK_DEL_REPO');
			} else $value['result_text'] = $lang->def('_ERR_DEL_REPO');
		}
		
		require_once($GLOBALS['where_framework'].'/lib/lib.json.php');
		$json = new Services_JSON();
		$output = $json->encode($value);
  		docebo_cout($output);
	};break;
	
	/* file delete */
	
	case "confirm_file_delete" : {
		
		require_once($GLOBALS["where_lms"]."/lib/lib.light_repo.php");
		
		$lang =& DoceboLanguage::createInstance( 'standard', 'framework');
		$lang->setGlobal();
		$lang =& DoceboLanguage::createInstance("light_repo", "lms");
		
		$file_man 	= new LightRepoManager( getLogUserId(), $_SESSION['idCourse'] );
	
		$id_file = importVar('id_file', true, 0);
	
		// recovering file repository information
		$file = false;
		$file = $file_man->getFileInfo($id_file);
		
		$value = array(
			'id_file' 		=> $id_file,
			'win_title' 	=> $lang->def('_DEL_FILE'),
			'win_text' 		=> str_replace('[file_title]', implode( '_', array_slice(explode('_', $file[LR_FILE_NAME]), 3) ), $lang->def('_DEL_FILE_TEXT')),
			'win_confirm' 	=> $lang->def('_CONFIRM'),
			'win_undo' 		=> $lang->def('_UNDO')
		);
		
		require_once($GLOBALS['where_framework'].'/lib/lib.json.php');
		$json = new Services_JSON();
		$output = $json->encode($value);
  		docebo_cout($output);
	};break;
	case "delete_file" : {
		
		require_once($GLOBALS["where_lms"]."/lib/lib.light_repo.php");
		
		$lang =& DoceboLanguage::createInstance( 'standard', 'framework');
		$lang->setGlobal();
		$lang =& DoceboLanguage::createInstance("light_repo", "lms");
		
		$id_file = importVar('id_file', true, 0);
		$value = array('result' => false, 'id_file' => $id_file , 'result_text' => '');
		
		$file_man = new LightRepoManager( getLogUserId(), $_SESSION['idCourse'] );
		if($file_man->deleteFile($id_file)) {
			$value['result'] = true;
			$value['result_text'] = $lang->def('_OK_DEL_FILE');
		} else {
			$value['result_text'] = $lang->def('_ERR_DEL_FILE');
		}
		
		require_once($GLOBALS['where_framework'].'/lib/lib.json.php');
		$json = new Services_JSON();
		$output = $json->encode($value);
  		docebo_cout($output);
	};break;
}

// =====================================================================================

// close database connection

mysql_close($GLOBALS['dbConn']);

ob_clean();
echo $GLOBALS['operation_result'];
ob_end_flush();

?>
