<commit_before><?php
function network_init(&$a) {
	if(! local_user()) {
		notice( t('Permission denied.') . EOL);
		return;
	}

	$is_a_date_query = false;

	if($a->argc > 1) {
		for($x = 1; $x < $a->argc; $x ++) {
			if(is_a_date_arg($a->argv[$x])) {
				$is_a_date_query = true;
				break;
			}
		}
	}

	// convert query string to array. remove friendica args
	$query_array = array();
	$query_string = str_replace($a->cmd."?", "", $a->query_string);
	parse_str($query_string, $query_array);
	array_shift($query_array);


	// fetch last used network view and redirect if needed
	if(! $is_a_date_query) {
		$sel_tabs = network_query_get_sel_tab($a);
		$sel_nets = network_query_get_sel_net();
		$sel_groups = network_query_get_sel_group($a);
		$last_sel_tabs = get_pconfig(local_user(), 'network.view','tab.selected');
		$last_sel_nets = get_pconfig(local_user(), 'network.view', 'net.selected');
		$last_sel_groups = get_pconfig(local_user(), 'network.view', 'group.selected');

		$remember_tab = ($sel_tabs[0] === 'active' && is_array($last_sel_tabs) && $last_sel_tabs[0] !== 'active');
		$remember_net = ($sel_nets === false && $last_sel_nets && $last_sel_nets !== 'all');
		$remember_group = ($sel_groups === false && $last_sel_groups && $last_sel_groups != 0);

		$net_baseurl = '/network';
		$net_args = array();

		if($remember_group) {
			$net_baseurl .= '/' . $last_sel_groups; // Note that the group number must come before the "/new" tab selection
		}
		else if($sel_groups !== false) {
			$net_baseurl .= '/' . $sel_groups;
		}

		if($remember_tab) {
			// redirect if current selected tab is '/network' and
			// last selected tab is _not_ '/network?f=&order=comment'.
			// and this isn't a date query

			$tab_baseurls = array(
				'',		//all
				'',		//postord
				'',		//conv
				'/new',		//new
				'',		//starred
				'',		//bookmarked
				'',		//spam
			);
			$tab_args = array(
				'f=&order=comment',	//all
				'f=&order=post',	//postord
				'f=&conv=1',		//conv
				'',			//new
				'f=&star=1',		//starred
				'f=&bmark=1',		//bookmarked
				'f=&spam=1',		//spam
			);

			$k = array_search('active', $last_sel_tabs);

			$net_baseurl .= $tab_baseurls[$k];

			// parse out tab queries
			$dest_qa = array();
			$dest_qs = $tab_args[$k];
			parse_str( $dest_qs, $dest_qa);
			$net_args = array_merge($net_args, $dest_qa);
		}
		else if($sel_tabs[4] === 'active') {
			// The '/new' tab is selected
			$net_baseurl .= '/new';
		}

		if($remember_net) {
			$net_args['nets'] = $last_sel_nets;
		}
		else if($sel_nets!==false) {
			$net_args['nets'] = $sel_nets;
		}

		if($remember_tab || $remember_net || $remember_group) {
			$net_args = array_merge($query_array, $net_args);
			$net_queries = build_querystring($net_args);

			$redir_url = ($net_queries ? $net_baseurl."?".$net_queries : $net_baseurl);

			goaway($a->get_baseurl() . $redir_url);
		}
	}

	if(x($_GET['nets']) && $_GET['nets'] === 'all')
		unset($_GET['nets']);

	$group_id = (($a->argc > 1 && is_numeric($a->argv[1])) ? intval($a->argv[1]) : 0);

	set_pconfig(local_user(), 'network.view', 'group.selected', $group_id);

	require_once('include/group.php');
	require_once('include/contact_widgets.php');
	require_once('include/items.php');

	if(! x($a->page,'aside'))
		$a->page['aside'] = '';

	$search = ((x($_GET,'search')) ? escape_tags($_GET['search']) : '');

	if(x($_GET,'save')) {
		$r = q("SELECT * FROM `search` WHERE `uid` = %d AND `term` = '%s' LIMIT 1",
			intval(local_user()),
			dbesc($search)
		);
		if(! count($r)) {
			q("INSERT INTO `search` ( `uid`,`term` ) VALUES ( %d, '%s') ",
				intval(local_user()),
				dbesc($search)
			);
		}
	}
	if(x($_GET,'remove')) {
		q("DELETE FROM `search` WHERE `uid` = %d AND `term` = '%s'",
			intval(local_user()),
			dbesc($search)
		);
	}

	// search terms header
	if(x($_GET,'search')) {
		$a->page['content'] .= replace_macros(get_markup_template("section_title.tpl"),array(
			'$title' => sprintf( t('Search Results For: %s'), $search)
		));
	}

	$a->page['aside'] .= (feature_enabled(local_user(),'groups') ? group_side('network/0','network',true,$group_id) : '');
	$a->page['aside'] .= posted_date_widget($a->get_baseurl() . '/network',local_user(),false);
	$a->page['aside'] .= networks_widget($a->get_baseurl(true) . '/network',(x($_GET, 'nets') ? $_GET['nets'] : ''));
	$a->page['aside'] .= saved_searches($search);
	$a->page['aside'] .= fileas_widget($a->get_baseurl(true) . '/network',(x($_GET, 'file') ? $_GET['file'] : ''));

}

function saved_searches($search) {

	if(! feature_enabled(local_user(),'savedsearch'))
		return '';

	$a = get_app();

	$srchurl = '/network?f='
		. ((x($_GET,'cid'))   ? '&cid='   . $_GET['cid']   : '')
		. ((x($_GET,'star'))  ? '&star='  . $_GET['star']  : '')
		. ((x($_GET,'bmark')) ? '&bmark=' . $_GET['bmark'] : '')
		. ((x($_GET,'conv'))  ? '&conv='  . $_GET['conv']  : '')
		. ((x($_GET,'nets'))  ? '&nets='  . $_GET['nets']  : '')
		. ((x($_GET,'cmin'))  ? '&cmin='  . $_GET['cmin']  : '')
		. ((x($_GET,'cmax'))  ? '&cmax='  . $_GET['cmax']  : '')<commit_msg>Show the profile in the network view when viewing single contacts<commit_after><?php
function network_init(&$a) {
	if(! local_user()) {
		notice( t('Permission denied.') . EOL);
		return;
	}

	$is_a_date_query = false;

	if($a->argc > 1) {
		for($x = 1; $x < $a->argc; $x ++) {
			if(is_a_date_arg($a->argv[$x])) {
				$is_a_date_query = true;
				break;
			}
		}
	}

	// convert query string to array. remove friendica args
	$query_array = array();
	$query_string = str_replace($a->cmd."?", "", $a->query_string);
	parse_str($query_string, $query_array);
	array_shift($query_array);


	// fetch last used network view and redirect if needed
	if(! $is_a_date_query) {
		$sel_tabs = network_query_get_sel_tab($a);
		$sel_nets = network_query_get_sel_net();
		$sel_groups = network_query_get_sel_group($a);
		$last_sel_tabs = get_pconfig(local_user(), 'network.view','tab.selected');
		$last_sel_nets = get_pconfig(local_user(), 'network.view', 'net.selected');
		$last_sel_groups = get_pconfig(local_user(), 'network.view', 'group.selected');

		$remember_tab = ($sel_tabs[0] === 'active' && is_array($last_sel_tabs) && $last_sel_tabs[0] !== 'active');
		$remember_net = ($sel_nets === false && $last_sel_nets && $last_sel_nets !== 'all');
		$remember_group = ($sel_groups === false && $last_sel_groups && $last_sel_groups != 0);

		$net_baseurl = '/network';
		$net_args = array();

		if($remember_group) {
			$net_baseurl .= '/' . $last_sel_groups; // Note that the group number must come before the "/new" tab selection
		}
		else if($sel_groups !== false) {
			$net_baseurl .= '/' . $sel_groups;
		}

		if($remember_tab) {
			// redirect if current selected tab is '/network' and
			// last selected tab is _not_ '/network?f=&order=comment'.
			// and this isn't a date query

			$tab_baseurls = array(
				'',		//all
				'',		//postord
				'',		//conv
				'/new',		//new
				'',		//starred
				'',		//bookmarked
				'',		//spam
			);
			$tab_args = array(
				'f=&order=comment',	//all
				'f=&order=post',	//postord
				'f=&conv=1',		//conv
				'',			//new
				'f=&star=1',		//starred
				'f=&bmark=1',		//bookmarked
				'f=&spam=1',		//spam
			);

			$k = array_search('active', $last_sel_tabs);

			$net_baseurl .= $tab_baseurls[$k];

			// parse out tab queries
			$dest_qa = array();
			$dest_qs = $tab_args[$k];
			parse_str( $dest_qs, $dest_qa);
			$net_args = array_merge($net_args, $dest_qa);
		}
		else if($sel_tabs[4] === 'active') {
			// The '/new' tab is selected
			$net_baseurl .= '/new';
		}

		if($remember_net) {
			$net_args['nets'] = $last_sel_nets;
		}
		else if($sel_nets!==false) {
			$net_args['nets'] = $sel_nets;
		}

		if($remember_tab || $remember_net || $remember_group) {
			$net_args = array_merge($query_array, $net_args);
			$net_queries = build_querystring($net_args);

			$redir_url = ($net_queries ? $net_baseurl."?".$net_queries : $net_baseurl);

			goaway($a->get_baseurl() . $redir_url);
		}
	}

	if(x($_GET['nets']) && $_GET['nets'] === 'all')
		unset($_GET['nets']);

	$group_id = (($a->argc > 1 && is_numeric($a->argv[1])) ? intval($a->argv[1]) : 0);

	set_pconfig(local_user(), 'network.view', 'group.selected', $group_id);

	require_once('include/group.php');
	require_once('include/contact_widgets.php');
	require_once('include/items.php');

	if(! x($a->page,'aside'))
		$a->page['aside'] = '';

	$search = ((x($_GET,'search')) ? escape_tags($_GET['search']) : '');

	if(x($_GET,'save')) {
		$r = q("SELECT * FROM `search` WHERE `uid` = %d AND `term` = '%s' LIMIT 1",
			intval(local_user()),
			dbesc($search)
		);
		if(! count($r)) {
			q("INSERT INTO `search` ( `uid`,`term` ) VALUES ( %d, '%s') ",
				intval(local_user()),
				dbesc($search)
			);
		}
	}
	if(x($_GET,'remove')) {
		q("DELETE FROM `search` WHERE `uid` = %d AND `term` = '%s'",
			intval(local_user()),
			dbesc($search)
		);
	}

	// search terms header
	if(x($_GET,'search')) {
		$a->page['content'] .= replace_macros(get_markup_template("section_title.tpl"),array(
			'$title' => sprintf( t('Search Results For: %s'), $search)
		));
	}

	$a->page['aside'] .= (feature_enabled(local_user(),'groups') ? group_side('network/0','network',true,$group_id) : '');
	$a->page['aside'] .= posted_date_widget($a->get_baseurl() . '/network',local_user(),false);
	$a->page['aside'] .= networks_widget($a->get_baseurl(true) . '/network',(x($_GET, 'nets') ? $_GET['nets'] : ''));
	$a->page['aside'] .= saved_searches($search);
	$a->page['aside'] .= fileas_widget($a->get_baseurl(true) . '/network',(x($_GET, 'file') ? $_GET['file'] : ''));

	if(x($_GET['cid']) && intval($_GET['cid']) != 0) {
		$r = q("SELECT * FROM `contact` WHERE `id` = %d",
			intval($_GET['cid']));
		if ($r) {
			$a->page['aside'] = "";
			profile_load($a, "", 0, $r[0]);
		}
	}
}

function saved_searches($search) {

	if(! feature_enabled(local_user(),'savedsearch'))
		return '';

	$a = get_app();

	$srchurl = '/network?f='
		. ((x($_GET,'cid'))   ? '&cid='   . $_GET['cid']   : '')
		. ((x($_GET,'star'))  ? '&star='  . $_GET['star']  : '')
		. ((x($_GET,'bmark')) ? '&bmark=' . $_GET['bmark'] : '')
		. ((x($_GET,'conv'))  ? '&conv='  . $_GET['conv']  : '')
		. ((x($_GET,'nets'))  ? '&nets='  . $_GET['nets']  : '')
		. ((x($_GET,'cmin'))  ? '&cmin='  . $_GET['cmin']  : '')
		. ((x($_GET,'cmax'))  ? '&cmax='  . $_GET['cmax']  : '')
{'fraction_non_alphanumeric': 0.2411566265060241, 'fraction_numerical': 0.0033734939759036144, 'mean_word_length': 3.945662535748332, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 50, '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': '2077940', 'n_tokens_mistral': 4568, 'n_tokens_neox': 4075, 'n_words': 681}