<commit_before><?php

require_once('include/socgraph.php');
require_once('include/dir_fns.php');
require_once('include/widgets.php');
require_once('include/bbcode.php');

function directory_init(&$a) {
	$a->set_pager_itemspage(60);

	if(x($_GET,'ignore')) {
		q("insert into xign ( uid, xchan ) values ( %d, '%s' ) ",
			intval(local_user()),
			dbesc($_GET['ignore'])
		);
		goaway(z_root() . '/directory?suggest=1');
	}

	$observer = get_observer_hash();
	$global_changed = false;
	$safe_changed = false;
	$pubforums_changed = false;

	if(array_key_exists('safe',$_REQUEST)) {
		$safemode = intval($_REQUEST['safe']);
		$safe_changed = true;
	}
	if($safe_changed) {
		$_SESSION['safemode'] = $safemode;
		if($observer)
			set_xconfig($observer,'directory','safemode',$safemode);
	}


	if(array_key_exists('pubforums',$_REQUEST)) {
		$pubforums = intval($_REQUEST['pubforums']);
		$pubforums_changed = true;
	}
	if($pubforums_changed) {
		$_SESSION['pubforums'] = $pubforums;
		if($observer)
			set_xconfig($observer,'directory','pubforums',$pubforums);
	}
}

function directory_content(&$a) {

	if((get_config('system','block_public')) && (! local_user()) && (! remote_channel())) {
		notice( t('Public access denied.') . EOL);
		return;
	}

	$observer = get_observer_hash();

	$globaldir = get_directory_setting($observer, 'globaldir');
	// override your personal global search pref if we're doing a navbar search of the directory
	if(intval($_REQUEST['navsearch']))
		$globaldir = 1;

	$safe_mode = get_directory_setting($observer, 'safemode');

	$pubforums = get_directory_setting($observer, 'pubforums');

	$o = '';
	nav_set_selected('directory');

	if(x($_POST,'search'))
		$search = notags(trim($_POST['search']));
	else
		$search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');


	if(strpos($search,'=') && local_user() && get_pconfig(local_user(),'feature','expert'))
		$advanced = $search;


	$keywords = (($_GET['keywords']) ? $_GET['keywords'] : '');

	// Suggest channels if no search terms or keywords are given
	$suggest = (local_user() && x($_REQUEST,'suggest')) ? $_REQUEST['suggest'] : '';

	if($suggest) {

		$r = suggestion_query(local_user(),get_observer_hash());

		// Remember in which order the suggestions were
		$addresses = array();
		$common = array();
		$index = 0;
		foreach($r as $rr) {
			$common[$rr['xchan_addr']] = $rr['total'];
			$addresses[$rr['xchan_addr']] = $index++;
		}

		// Build query to get info about suggested people
		$advanced = '';
		foreach(array_keys($addresses) as $address) {
			$advanced .= "address=\"$address\" ";
		}
		// Remove last space in the advanced query
		$advanced = rtrim($advanced);

	}

	$tpl = get_markup_template('directory_header.tpl');

	$dirmode = intval(get_config('system','directory_mode'));

	if(($dirmode == DIRECTORY_MODE_PRIMARY) || ($dirmode == DIRECTORY_MODE_STANDALONE)) {
		$url = z_root() . '/dirsearch';
	}
	if(! $url) {
		$directory = find_upstream_directory($dirmode);
		$url = $directory['url'] . '/dirsearch';
	}

	$token = get_config('system','realm_token');


	logger('mod_directory: URL = ' . $url, LOGGER_DEBUG);

	$contacts = array();

	if(local_user()) {
		$x = q("select abook_xchan from abook where abook_channel = %d",
			intval(local_user())
		);
		if($x) {
			foreach($x as $xx)
				$contacts[] = $xx['abook_xchan'];
		}
	}

	if($url) {
		// We might want to make the tagadelic count (&kw=) configurable or turn it off completely.

		$numtags = get_config('system','directorytags');

		$kw = ((intval($numtags)) ? $numtags : 50);
		$query = $url . '?f=&kw=' . $kw . (($safe_mode != 1) ? '&safe=' . $safe_mode : '');

		if($token)
			$query .= '&t=' . $token;

		if(! $globaldir)
			$query .= '&hub=' . get_app()->get_hostname();

		if($search)
			$query .= '&name=' . urlencode($search) . '&keywords=' . urlencode($search);
		if(strpos($search,'@'))
			$query .= '&address=' . urlencode($search);
		if($keywords)
			$query .= '&keywords=' . urlencode($keywords);
		if($advanced)
			$query .= '&query=' . urlencode($advanced);
		if(! is_null($pubforums))
			$query .= '&pubforums=' . intval($pubforums);

		$directory_sort_order = get_config('system','directory_sort_order');
		if(! $directory_sort_order)
			$directory_sort_order = 'date';

		$sort_order  = ((x($_REQUEST,'order')) ? $_REQUEST['order'] : $directory_sort_order);

		if($sort_order)
			$query .= '&order=' . urlencode($sort_order);
			
		if($a->pager['page'] != 1)
			$query .= '&p=' . $a->pager['page'];

		logger('mod_directory: query: ' . $query);<commit_msg>May as well clean out some more stuff while I'm in there.<commit_after><?php

require_once('include/socgraph.php');
require_once('include/dir_fns.php');
require_once('include/widgets.php');
require_once('include/bbcode.php');

function directory_init(&$a) {
	$a->set_pager_itemspage(60);

	$observer = get_observer_hash();
	$global_changed = false;
	$safe_changed = false;
	$pubforums_changed = false;

	if(array_key_exists('safe',$_REQUEST)) {
		$safemode = intval($_REQUEST['safe']);
		$safe_changed = true;
	}
	if($safe_changed) {
		$_SESSION['safemode'] = $safemode;
		if($observer)
			set_xconfig($observer,'directory','safemode',$safemode);
	}


	if(array_key_exists('pubforums',$_REQUEST)) {
		$pubforums = intval($_REQUEST['pubforums']);
		$pubforums_changed = true;
	}
	if($pubforums_changed) {
		$_SESSION['pubforums'] = $pubforums;
		if($observer)
			set_xconfig($observer,'directory','pubforums',$pubforums);
	}
}

function directory_content(&$a) {

	$observer = get_observer_hash();

	$safe_mode = get_directory_setting($observer, 'safemode');

	$pubforums = get_directory_setting($observer, 'pubforums');

	$o = '';
	nav_set_selected('directory');

	if(x($_POST,'search'))
		$search = notags(trim($_POST['search']));
	else
		$search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');

		$advanced = $search;


	$keywords = (($_GET['keywords']) ? $_GET['keywords'] : '');

	$tpl = get_markup_template('directory_header.tpl');

	$url = z_root() . '/dirsearch';

	if(! $url) {
		info('Something went horribly wrong');
		return;
	}

	$token = get_config('system','realm_token');

	logger('mod_directory: URL = ' . $url, LOGGER_DEBUG);

	$contacts = array();

	if($url) {
		// We might want to make the tagadelic count (&kw=) configurable or turn it off completely.

		$numtags = get_config('system','directorytags');

		$kw = ((intval($numtags)) ? $numtags : 50);
		$query = $url . '?f=&kw=' . $kw . (($safe_mode != 1) ? '&safe=' . $safe_mode : '');

		if($token)
			$query .= '&t=' . $token;

		if($search)
			$query .= '&name=' . urlencode($search) . '&keywords=' . urlencode($search);
		if(strpos($search,'@'))
			$query .= '&address=' . urlencode($search);
		if($keywords)
			$query .= '&keywords=' . urlencode($keywords);
		if($advanced)
			$query .= '&query=' . urlencode($advanced);
		if(! is_null($pubforums))
			$query .= '&pubforums=' . intval($pubforums);

		$directory_sort_order = get_config('system','directory_sort_order');
		if(! $directory_sort_order)
			$directory_sort_order = 'date';

		$sort_order  = ((x($_REQUEST,'order')) ? $_REQUEST['order'] : $directory_sort_order);

		if($sort_order)
			$query .= '&order=' . urlencode($sort_order);
			
		if($a->pager['page'] != 1)
			$query .= '&p=' . $a->pager['page'];

		logger('mod_directory: query: ' . $query);
{'fraction_non_alphanumeric': 0.20827118644067796, 'fraction_numerical': 0.002033898305084746, 'mean_word_length': 5.0310711365494685, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, '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': '2529964', 'n_tokens_mistral': 2921, 'n_tokens_neox': 2638, 'n_words': 461}