<commit_before><?php
/**
 * Administration panel
 *
 * @package Cotonti
 * @version 0.9.0
 * @author Cotonti Team
 * @copyright Copyright (c) Cotonti Team 2008-2012
 * @license BSD
 */

(defined('COT_CODE') && defined('COT_ADMIN')) or die('Wrong URL.');

list($usr['auth_read'], $usr['auth_write'], $usr['isadmin']) = cot_auth('admin', 'a');
cot_block($usr['isadmin']);

require_once cot_incfile('extrafields');
require_once cot_incfile('auth');
require_once cot_incfile('structure');

$id = cot_import('id', 'G', 'INT');
$al = cot_import('al', 'G', 'ALP');
$c = cot_import('c', 'G', 'TXT');
list($pg, $d, $durl) = cot_import_pagenav('d', $cfg['maxrowsperpage']);
$mode = cot_import('mode', 'G', 'ALP');

$t = new XTemplate(cot_tplfile(array('admin', 'structure', $n), 'core'));

/* === Hook === */
foreach (cot_getextplugins('admin.structure.first') as $pl)
{
	include $pl;
}
/* ===== */

if (empty($n))
{
	$adminpath[] = array(cot_url('admin', 'm=structure'), $L['Structure']);
	// Show available module list
	if(is_array($modules_structure) && count($modules_structure) == 1)
	{
		cot_redirect(cot_url('admin', 'm=structure&n='.$modules_structure[0], '', true));
	}
	if (is_array($modules_structure) && count($modules_structure) > 0)
	{
		foreach ($modules_structure as $code)
		{
			$ext_info = cot_get_extensionparams($code, true);
			$t->assign(array(
				'ADMIN_STRUCTURE_EXT_URL' => cot_url('admin', 'm=structure&n='.$code),
				'ADMIN_STRUCTURE_EXT_ICO' => $ext_info['icon'],
				'ADMIN_STRUCTURE_EXT_NAME' => $ext_info['name']
			));
			$t->parse('LIST.ADMIN_STRUCTURE_EXT');
		}
	}
	else
	{
		$t->parse('LIST.ADMIN_STRUCTURE_EMPTY');
	}

	$t->assign(array(
		'ADMIN_STRUCTURE_EXFLDS_URL' => cot_url('admin', 'm=extrafields')
	));
	$t->parse('LIST');
	$adminmain = $t->text('LIST');
}
else
{
	// Edit structure for a module
	if (file_exists(cot_incfile($n, 'module')))
	{
		require_once cot_incfile($n, 'module');
	}
	if (empty($adminhelp))
	{
		$adminhelp = $L['adm_help_structure'];
	}

	if ($a == 'update' && !empty($_POST))
	{
		$rstructurecode = cot_import('rstructurecode', 'P', 'ARR');
		$rstructurepath = cot_import('rstructurepath', 'P', 'ARR');
		$rstructuretitle = cot_import('rstructuretitle', 'P', 'ARR');
		$rstructuredesc = cot_import('rstructuredesc', 'P', 'ARR');
		$rstructureicon = cot_import('rstructureicon', 'P', 'ARR');
		$rstructurelocked = cot_import('rstructurelocked', 'P', 'ARR');

		foreach ($cot_extrafields[$db_structure] as $exfld)
		{
			if ($exfld['field_type'] != 'file' || $exfld['field_type'] != 'filesize')
			{
				$rstructureextrafieldsarr[$exfld['field_name']] = cot_import('rstructure'.$exfld['field_name'], 'P', 'ARR');
			}
			elseif($exfld['field_type'] == 'file')
			{
				// TODO FIXME!
				//$rstructureextrafieldsarr[$exfld['field_name']] = cot_import_filesarray('rstructure'.$exfld['field_name']);
			}
		}

		$rtplmodearr = cot_import('rstructuretplmode', 'P', 'ARR');
		$rtplforcedarr = cot_import('rstructuretplforced', 'P', 'ARR');
		$rtplquickarr = cot_import('rstructuretplquick', 'P', 'ARR');

		/* === Hook === */
		foreach (cot_getextplugins('admin.structure.update.first') as $pl)
		{
			include $pl;
		}
		/* ===== */

		foreach ($rstructurecode as $i => $k)
		{
			$oldrow = $db->query("SELECT * FROM $db_structure WHERE structure_id=".(int)$i)->fetch();
			$rstructure['structure_code'] = cot_import($rstructurecode[$i], 'D', 'TXT');
			$rstructure['structure_path'] = cot_import($rstructurepath[$i], 'D', 'TXT');
			$rstructure['structure_title'] = cot_import($rstructuretitle[$i], 'D', 'TXT');
			$rstructure['structure_desc'] = cot_import($rstructuredesc[$i], 'D', 'TXT');
			$rstructure['structure_icon'] = cot_import($rstructureicon[$i], 'D', 'TXT');
			if (cot_import($rstructurelocked[$i], 'D', 'BOL') != null)
			{
				$rstructure['structure_locked'] = (cot_import($rstructurelocked[$i], 'D', 'BOL')) ? 1 : 0;
			}

			foreach ($cot_extrafields[$db_structure] as $exfld)
			{
				$rstructure['structure_'.$exfld['field_name']] = cot_import_extrafields($rstructureextrafieldsarr[$exfld['field_name']][$i], $exfld, 'D', $oldrow['structure_'.$exfld['field_name']]);
			}

			$rtplmode = cot_import($rtplmodearr[$i], 'D', 'INT');
			$rtplquick = cot_import($rtplquickarr[$i], 'D', 'TXT');

			$rstructure['structure_tpl'] = null;
			if (!empty($rtplquick) || empty($rtplmode))
			{
				$rstructure['structure_tpl'] = $rtplquick;
			}
			elseif ($rtplmode == 3)
			{
				$rstructure['structure_tpl'] = cot_import($rtplforcedarr[$i], 'D', 'TXT');
			}
			elseif ($rtplmode == 2)
			{
				$rstructure['structure_tpl'] = 'same_as_parent';
			}
			elseif ($rtplmode == 1)
			{
				$rstructure['structure_tpl'] = '';
			}
			if (!cot_error_found())
			{
				$res = cot_structure_update($n, $i, $oldrow, $rstructure);
				if (is_array($res))
				{
					cot_error($res[0], $res[1]);
				}
			}
		}
		cot_extrafield_movefiles();
		cot_auth_clear('all');
		if ($cache)
		{
			$cache->clear();
		}

		/* === Hook === */
		foreach (cot_getextplugins('admin.structure.update.done') as $pl)
		{
			include $pl;
		}
		/* ===== */

		cot_message('Updated');
		//cot_redirect(cot_url('admin', 'm=structure&n='.$n.'&mode='.$mode.'&d='.$durl, '', true));
	}
	elseif ($a == 'add' && !empty($_POST))
	{
		$rstructure['structure_code'] = cot_import('rstructurecode', 'P', 'TXT');
		$rstructure['structure_path'] = cot_import('rstructurepath', 'P', 'TXT');
		$rstructure['structure_title'] = cot_import('rstructuretitle', 'P', 'TXT');
		$rstructure['structure_desc'] = cot_import('rstructuredesc', 'P', 'TXT');
		$rstructure['structure_icon'] = cot_import('rstructureicon', 'P', 'TXT');
		$rstructure['structure_locked'] = (cot_import('rstructurelocked', 'P', 'BOL')) ? 1 : 0;
		$rstructure['structure_area'] = $n;
<commit_msg>Fixed #949.<commit_after><?php
/**
 * Administration panel
 *
 * @package Cotonti
 * @version 0.9.0
 * @author Cotonti Team
 * @copyright Copyright (c) Cotonti Team 2008-2012
 * @license BSD
 */

(defined('COT_CODE') && defined('COT_ADMIN')) or die('Wrong URL.');

list($usr['auth_read'], $usr['auth_write'], $usr['isadmin']) = cot_auth('admin', 'a');
cot_block($usr['isadmin']);

require_once cot_incfile('extrafields');
require_once cot_incfile('auth');
require_once cot_incfile('structure');

$id = cot_import('id', 'G', 'INT');
$al = cot_import('al', 'G', 'ALP');
$c = cot_import('c', 'G', 'TXT');
list($pg, $d, $durl) = cot_import_pagenav('d', $cfg['maxrowsperpage']);
$mode = cot_import('mode', 'G', 'ALP');

$t = new XTemplate(cot_tplfile(array('admin', 'structure', $n), 'core'));

/* === Hook === */
foreach (cot_getextplugins('admin.structure.first') as $pl)
{
	include $pl;
}
/* ===== */

if (empty($n))
{
	$adminpath[] = array(cot_url('admin', 'm=structure'), $L['Structure']);
	// Show available module list
	if(is_array($modules_structure) && count($modules_structure) == 1)
	{
		cot_redirect(cot_url('admin', 'm=structure&n='.$modules_structure[0], '', true));
	}
	if (is_array($modules_structure) && count($modules_structure) > 0)
	{
		foreach ($modules_structure as $code)
		{
			$ext_info = cot_get_extensionparams($code, true);
			$t->assign(array(
				'ADMIN_STRUCTURE_EXT_URL' => cot_url('admin', 'm=structure&n='.$code),
				'ADMIN_STRUCTURE_EXT_ICO' => $ext_info['icon'],
				'ADMIN_STRUCTURE_EXT_NAME' => $ext_info['name']
			));
			$t->parse('LIST.ADMIN_STRUCTURE_EXT');
		}
	}
	else
	{
		$t->parse('LIST.ADMIN_STRUCTURE_EMPTY');
	}

	$t->assign(array(
		'ADMIN_STRUCTURE_EXFLDS_URL' => cot_url('admin', 'm=extrafields')
	));
	$t->parse('LIST');
	$adminmain = $t->text('LIST');
}
else
{
	// Edit structure for a module
	if (file_exists(cot_incfile($n, 'module')))
	{
		require_once cot_incfile($n, 'module');
	}
	if (empty($adminhelp))
	{
		$adminhelp = $L['adm_help_structure'];
	}

	if ($a == 'update' && !empty($_POST))
	{
		$rstructurecode = cot_import('rstructurecode', 'P', 'ARR');
		$rstructurepath = cot_import('rstructurepath', 'P', 'ARR');
		$rstructuretitle = cot_import('rstructuretitle', 'P', 'ARR');
		$rstructuredesc = cot_import('rstructuredesc', 'P', 'ARR');
		$rstructureicon = cot_import('rstructureicon', 'P', 'ARR');
		$rstructurelocked = cot_import('rstructurelocked', 'P', 'ARR');

		foreach ($cot_extrafields[$db_structure] as $exfld)
		{
			if ($exfld['field_type'] != 'file' || $exfld['field_type'] != 'filesize')
			{
				$rstructureextrafieldsarr[$exfld['field_name']] = cot_import('rstructure'.$exfld['field_name'], 'P', 'ARR');
			}
			elseif($exfld['field_type'] == 'file')
			{
				// TODO FIXME!
				//$rstructureextrafieldsarr[$exfld['field_name']] = cot_import_filesarray('rstructure'.$exfld['field_name']);
			}
		}

		$rtplmodearr = cot_import('rstructuretplmode', 'P', 'ARR');
		$rtplforcedarr = cot_import('rstructuretplforced', 'P', 'ARR');
		$rtplquickarr = cot_import('rstructuretplquick', 'P', 'ARR');

		/* === Hook === */
		foreach (cot_getextplugins('admin.structure.update.first') as $pl)
		{
			include $pl;
		}
		/* ===== */

		foreach ($rstructurecode as $i => $k)
		{
			$oldrow = $db->query("SELECT * FROM $db_structure WHERE structure_id=".(int)$i)->fetch();
			$rstructure['structure_code'] = cot_import($rstructurecode[$i], 'D', 'TXT');
			$rstructure['structure_path'] = cot_import($rstructurepath[$i], 'D', 'TXT');
			$rstructure['structure_title'] = cot_import($rstructuretitle[$i], 'D', 'TXT');
			$rstructure['structure_desc'] = cot_import($rstructuredesc[$i], 'D', 'TXT');
			$rstructure['structure_icon'] = cot_import($rstructureicon[$i], 'D', 'TXT');
			if (cot_import($rstructurelocked[$i], 'D', 'BOL') != null)
			{
				$rstructure['structure_locked'] = (cot_import($rstructurelocked[$i], 'D', 'BOL')) ? 1 : 0;
			}

			foreach ($cot_extrafields[$db_structure] as $exfld)
			{
				$rstructure['structure_'.$exfld['field_name']] = cot_import_extrafields($rstructureextrafieldsarr[$exfld['field_name']][$i], $exfld, 'D', $oldrow['structure_'.$exfld['field_name']]);
			}

			$rtplmode = cot_import($rtplmodearr[$i], 'D', 'INT');
			$rtplquick = cot_import($rtplquickarr[$i], 'D', 'TXT');

			$rstructure['structure_tpl'] = null;
			if (!empty($rtplquick) || empty($rtplmode))
			{
				$rstructure['structure_tpl'] = $rtplquick;
			}
			elseif ($rtplmode == 3)
			{
				$rstructure['structure_tpl'] = cot_import($rtplforcedarr[$i], 'D', 'TXT');
			}
			elseif ($rtplmode == 2)
			{
				$rstructure['structure_tpl'] = 'same_as_parent';
			}
			elseif ($rtplmode == 1)
			{
				$rstructure['structure_tpl'] = '';
			}
			if (!cot_error_found())
			{
				$res = cot_structure_update($n, $i, $oldrow, $rstructure);
				if (is_array($res))
				{
					cot_error($res[0], $res[1]);
				}
			}
		}
		cot_extrafield_movefiles();
		cot_auth_clear('all');
		if ($cache)
		{
			$cache->clear();
		}

		/* === Hook === */
		foreach (cot_getextplugins('admin.structure.update.done') as $pl)
		{
			include $pl;
		}
		/* ===== */

		if (!cot_error_found())
		{
			cot_message('Updated');
		}
		//cot_redirect(cot_url('admin', 'm=structure&n='.$n.'&mode='.$mode.'&d='.$durl, '', true));
	}
	elseif ($a == 'add' && !empty($_POST))
	{
		$rstructure['structure_code'] = cot_import('rstructurecode', 'P', 'TXT');
		$rstructure['structure_path'] = cot_import('rstructurepath', 'P', 'TXT');
		$rstructure['structure_title'] = cot_import('rstructuretitle', 'P', 'TXT');
		$rstructure['structure_desc'] = cot_import('rstructuredesc', 'P', 'TXT');
		$rstructure['structure_icon'] = cot_import('rstructureicon', 'P', 'TXT');
		$rstructure['structure_locked'] = (cot_import('rstructurelocked', 'P', 'BOL')) ? 1 : 0;
		$rstructure['structure_area'] = $n;

{'fraction_non_alphanumeric': 0.22501078050884002, 'fraction_numerical': 0.004225959465286762, 'mean_word_length': 5.371428571428571, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2625840', 'n_tokens_mistral': 4826, 'n_tokens_neox': 4387, 'n_words': 677}