<?php 
/**
* @version		$Id: auth.php 2556 2024-04-20 10:22:21Z IOS $
* @package		Elxis
* @subpackage	Component Extensions Manager
* @copyright	Copyright (c) 2006-2024 Elxis CMS (https://www.elxis.org). All rights reserved.
* @license		Elxis Public License ( https://www.elxis.org/elxis-public-license.html )
* @author		Elxis Team ( https://www.elxis.org )
* @description 	Elxis CMS is free software. Read the license for copyright notices and details
*/

defined('_ELXIS_') or die ('Direct access to this location is not allowed');


class authExtmanagerController extends extmanagerController {

	/*********************/
	/* MAGIC CONSTRUCTOR */
	/*********************/
	public function __construct($view=null, $task='', $model=null) {
		parent::__construct($view, $task, $model);
	}


	/**************************************************/
	/* PREPARE TO DISPLAY AUTHENTICATION METHODS LIST */
	/**************************************************/
	public function listauth() {
		$this->listExtensions('auth');//base
	}


	/**************************************/
	/* SET AUTHENTICATION METHOD ORDERING */
	/**************************************/
	public function setordering() {
		$this->setExtensionOrdering('auth');//base
	}


	/**********************************************/
	/* TOGGLE AUTH METHOD'S PUBLISH STATUS (ICON) */
	/**********************************************/
	public function toggleauth() {
		$this->toggleExtension('auth');//base
	}


	/********************************/
	/* DELETE AUTHENTICATION METHOD */
	/********************************/
	public function deleteauth() {
		$this->deleteExtension('auth');//base
	}


	/******************************/
	/* EDIT AUTHENTICATION METHOD */
	/******************************/
	public function editauth() {
		$this->editExtension('auth');//base
	}


	/******************************/
	/* SAVE AUTHENTICATION METHOD */
	/******************************/
	public function saveauth() {
		$elxis = eFactory::getElxis();
		$eLang = eFactory::getLang();
		$eSession = eFactory::getSession();

		$sess_token = trim($eSession->get('token_fmextedit'));
		$token = trim($elxis->sanitizeInput('token', INPUT_POST, true, false));
		if (($token == '') || ($sess_token == '') || ($sess_token != $token)) {
			exitPage::make('403', 'CEXT-0015', $eLang->get('REQDROPPEDSEC'));
		}

		$task = isset($_POST['task']) ? trim($_POST['task']) : 'save';
		$id = isset($_POST['id']) ? (int)$_POST['id'] : 0;
		if ($id < 1) {
			$link = $elxis->makeAURL('extmanager:auth/');
			$elxis->redirect($link, 'Authentication method not found!', true);
		}

		if ($elxis->acl()->check('com_extmanager', 'auth', 'edit') < 1) {
			$msg = $eLang->get('NOTALLOWACTION');
			$link = $elxis->makeAURL('extmanager:/');
			$elxis->redirect($link, $msg, true);
		}

		$row = new authenticationDbTable();
		if (!$row->load($id)) {
			$link = $elxis->makeAURL('extmanager:auth/');
			$elxis->redirect($link, 'Authentication method found!', true);
		}

		$old_ordering = $row->ordering;
		$row->ordering = isset($_POST['ordering']) ? (int)$_POST['ordering'] : 0;
		$row->published = isset($_POST['published']) ? (int)$_POST['published'] : 0;
		$row->title = $elxis->sanitizeInput('title', INPUT_POST, true, false);

		$xmlfile = ELXIS_PATH.'/components/com_user/auth/'.$row->auth.'/'.$row->auth.'.auth.xml';
		if (file_exists($xmlfile)) {
			elxisLoader::loadFile('includes/libraries/elxis/parameters.class.php');
			$params = new elxisParameters('', $xmlfile, 'auth');
			$parr = (isset($_POST['params'])) ? $_POST['params'] : array();
			$row->params = $params->toJSONString($parr);
			unset($params);
		} else {
			$row->params = null;
		}

		if (!$row->update()) {
			$redirurl = $elxis->makeAURL('extmanager:auth/edit.html?id='.$id);
			$elxis->redirect($redirurl, $row->getErrorMsg(), true);
		}

		if ($old_ordering <> $row->ordering) { $row->reorder(); }

		$eSession->set('token_fmextedit');

		if (isset($_POST['onsave'])) {
			$onsave = trim($_POST['onsave']);
			$onsave = ltrim($_POST['onsave'], '/');
			$xmldir = dirname($xmlfile).'/';
			if (($onsave != '') && file_exists($xmldir.$onsave) && is_file($xmldir.$onsave)) {
				include($xmldir.$onsave);
			}
		}

		$p = array();
		if ($task == 'apply') {
			$p[] = 'id='.$id;
			if (isset($_POST['tabopen'])) {
				$v = (int)$_POST['tabopen'];
				if ($v > 0) { $p[] = 'tabopen='.$v; }
			}
		}
		if (isset($_POST['page'])) {
			$v = (int)$_POST['page'];
			if ($v > 1) { $p[] = 'page='.$v; }
		}
		if (isset($_POST['sn'])) {
			$v = trim($_POST['sn']);
			if ($v != '') { $p[] = 'sn='.$v; }
		}
		if (isset($_POST['so'])) {
			$v = trim($_POST['so']);
			if ($v != '') { $p[] = 'so='.$v; }
		}
		if (isset($_POST['lpsection'])) {
			$v = trim($_POST['lpsection']);
			if ($v != '') { $p[] = 'section='.$v; }
		}

		$redirurl = $elxis->makeAURL('extmanager:auth/');
		if ($task == 'apply') { $redirurl .= 'edit.html'; }
		if ($p) { $redirurl .= '?'.implode('&', $p); }
		$elxis->redirect($redirurl, $eLang->get('ITEM_SAVED'));
	}

}

?>