<commit_before><?php
/*
 * WiND - Wireless Nodes Database
 *
 * Copyright (C) 2005-2013 	by WiND Contributors (see AUTHORS.txt)
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

function redirect($url, $sec=0, $exit=TRUE) {
	global $main;
	$sec = (integer)($sec);
	if ($main->message->show && $main->message->forward != $url) {
		if ($main->message->forward == '') $main->message->forward = $url;
		return;
	}
	if (@preg_match('/Microsoft|WebSTAR|Xitami/', getenv('SERVER_SOFTWARE')) || @preg_match('/Safari/', $_SERVER['HTTP_USER_AGENT']) || $sec>0) {
		header("Refresh: $sec; URL=".html_entity_decode($url));
		$main->html->head->add_meta("$sec; url=$url", "", "refresh");
	} else {
		header("Location: ".html_entity_decode($url));		
	}
	if ($exit && !$main->message->show) {
		exit;
	}
}

/**
 * @brief Check if the current request is made by an ajax script
 */
function is_ajax_request() {
	if(!empty($_SERVER['HTTP_X_REQUESTED_WITH'])
			&& strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
	{
		return true;
	} else {
		return false;
	}
}

/**
 * @brief Get a title for the current user based on username, name and surname.
 * If it is possible it will prefer "name surname" otherwise it will fallback
 * to username
 */
function get_user_title() {
	global $main;
	
	if (!$main->userdata->logged)
		return "Anonymous";
	
	$title_tokens = array();
	
	// Try to get tokens from name surname
	if (! empty($main->userdata->info['name']))
		$title_tokens[] = $main->userdata->info['name'];
	if (! empty($main->userdata->info['surname']))
		$title_tokens[] = $main->userdata->info['surname'];
	
	// If we didn't find any token we add username
	if (empty($title_tokens)){
		$title_tokens[] = $main->userdata->info['username'];
	}
	
	return implode(" ", $title_tokens); 
}

/**
 * @brief Get query string of the request
 * @param string $htmlspecialchars
 * @return Ambigous <string, unknown>
 */
function get_qs($htmlspecialchars=TRUE) {
	$ret = $_SERVER['QUERY_STRING'];
	return ($htmlspecialchars?htmlspecialchars($ret):$ret);
}

/**
 * @brief Get a request parameter from query string.
 * Depending the type of the key it will try to do sanitization and security
 * checking. Specifically for 'page' and 'subpage' it will check first that it exists.
 * @param string $key The key of the entry to fethch
 * @return Ambigous <string, unknown>
 */
function get($key) {
	global $page_admin, $main;
	
	$ret = isset($_GET[$key])?$_GET[$key]:"";
	
	switch ($key) {
		case 'page':
			$valid_array = getdirlist(ROOT_PATH."includes/pages/");
			array_unshift($valid_array, 'startup');
			break;
		case 'subpage':
			$valid_array = getdirlist(ROOT_PATH."includes/pages/".get('page').'/', FALSE, TRUE);
			for ($key=0;$key<count($valid_array);$key++) {
				$valid_array[$key] = basename($valid_array[$key], '.php');
				
				if (substr($valid_array[$key], 0, strlen(get('page'))+1) != get('page').'_') {
					array_splice($valid_array, $key, 1);
					$key--;
				} else {
					$valid_array[$key] = substr($valid_array[$key], strlen(get('page'))+1);
				}
			}
			array_unshift($valid_array, '');
			break;
	}
	if (isset($valid_array) && !in_array($ret, $valid_array)) $ret = $valid_array[0];
	return $ret;
}

function getdirlist($dirName, $dirs=TRUE, $files=FALSE) { 
	$d = dir($dirName);
	$a = array();
	while($entry = $d->read()) { 
		if ($entry != "." && $entry != "..") { 
			if (is_dir($dirName."/".$entry)) { 
				if ($dirs==TRUE) array_push($a, $entry); 
			} else { 
				if ($files==TRUE) array_push($a, $entry); 
			} 
		} 
	} 
	$d->close();
	return $a;
} 

/**<commit_msg>Fix: possible sql injection on 'node' parameter. Closes #8<commit_after><?php
/*
 * WiND - Wireless Nodes Database
 *
 * Copyright (C) 2005-2013 	by WiND Contributors (see AUTHORS.txt)
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

function redirect($url, $sec=0, $exit=TRUE) {
	global $main;
	$sec = (integer)($sec);
	if ($main->message->show && $main->message->forward != $url) {
		if ($main->message->forward == '') $main->message->forward = $url;
		return;
	}
	if (@preg_match('/Microsoft|WebSTAR|Xitami/', getenv('SERVER_SOFTWARE')) || @preg_match('/Safari/', $_SERVER['HTTP_USER_AGENT']) || $sec>0) {
		header("Refresh: $sec; URL=".html_entity_decode($url));
		$main->html->head->add_meta("$sec; url=$url", "", "refresh");
	} else {
		header("Location: ".html_entity_decode($url));		
	}
	if ($exit && !$main->message->show) {
		exit;
	}
}

/**
 * @brief Check if the current request is made by an ajax script
 */
function is_ajax_request() {
	if(!empty($_SERVER['HTTP_X_REQUESTED_WITH'])
			&& strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
	{
		return true;
	} else {
		return false;
	}
}

/**
 * @brief Get a title for the current user based on username, name and surname.
 * If it is possible it will prefer "name surname" otherwise it will fallback
 * to username
 */
function get_user_title() {
	global $main;
	
	if (!$main->userdata->logged)
		return "Anonymous";
	
	$title_tokens = array();
	
	// Try to get tokens from name surname
	if (! empty($main->userdata->info['name']))
		$title_tokens[] = $main->userdata->info['name'];
	if (! empty($main->userdata->info['surname']))
		$title_tokens[] = $main->userdata->info['surname'];
	
	// If we didn't find any token we add username
	if (empty($title_tokens)){
		$title_tokens[] = $main->userdata->info['username'];
	}
	
	return implode(" ", $title_tokens); 
}

/**
 * @brief Get query string of the request
 * @param string $htmlspecialchars
 * @return Ambigous <string, unknown>
 */
function get_qs($htmlspecialchars=TRUE) {
	$ret = $_SERVER['QUERY_STRING'];
	return ($htmlspecialchars?htmlspecialchars($ret):$ret);
}

/**
 * @brief Get a request parameter from query string.
 * Depending the type of the key it will try to do sanitization and security
 * checking. Specifically for 'page' and 'subpage' it will check first that it exists.
 * @param string $key The key of the entry to fethch
 * @return Ambigous <string, unknown>
 */
function get($key) {
	global $page_admin, $main;
	
	$ret = isset($_GET[$key])?$_GET[$key]:"";
	
	switch ($key) {
		case 'page':
			$valid_array = getdirlist(ROOT_PATH."includes/pages/");
			array_unshift($valid_array, 'startup');
			break;
		case 'subpage':
			$valid_array = getdirlist(ROOT_PATH."includes/pages/".get('page').'/', FALSE, TRUE);
			for ($key=0;$key<count($valid_array);$key++) {
				$valid_array[$key] = basename($valid_array[$key], '.php');
				
				if (substr($valid_array[$key], 0, strlen(get('page'))+1) != get('page').'_') {
					array_splice($valid_array, $key, 1);
					$key--;
				} else {
					$valid_array[$key] = substr($valid_array[$key], strlen(get('page'))+1);
				}
			}
			array_unshift($valid_array, '');
		case 'node':
			$ret = intval($ret);
			break;
	}
	if (isset($valid_array) && !in_array($ret, $valid_array))
		$ret = $valid_array[0];
	return $ret;
}

function getdirlist($dirName, $dirs=TRUE, $files=FALSE) { 
	$d = dir($dirName);
	$a = array();
	while($entry = $d->read()) { 
		if ($entry != "." && $entry != "..") { 
			if (is_dir($dirName."/".$entry)) { 
				if ($dirs==TRUE) array_push($a, $entry); 
			} else { 
				if ($files==TRUE) array_push($a, $entry); 
			} 
		} 
	} 
	$d->close();
	return $a;
} 

/**
{'fraction_non_alphanumeric': 0.17489421720733428, 'fraction_numerical': 0.004113775270333803, 'mean_word_length': 4.169501822600243, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, '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': '6679402', 'n_tokens_mistral': 3065, 'n_tokens_neox': 2826, 'n_words': 790}