<commit_before><?php

namespace App\Controllers;
use App\CoreUtils;
use App\CSRFProtection;
use App\HTTP;
use App\Input;
use App\Logs;
use App\Pagination;
use App\Permission;
use App\Posts;
use App\RegExp;
use App\Response;
use App\Users;

class AdminController extends Controller {
	public $do = 'admin';

	function __construct(){
		parent::__construct();

		if (!Permission::sufficient('staff'))
			CoreUtils::notFound();
	}

	function index(){
		CoreUtils::loadPage(array(
			'title' => 'Admin Area',
			'do-css',
			'js' => array('Sortable',$this->do),
		), $this);
	}

	function logs(){
		global $currentUser, $Database, $LogItems, $Pagination;

		$type = Logs::validateRefType('type', true, true);
		if (isset($_GET['type']) && preg_match(new RegExp('/^[a-z_]+$/'), $_GET['type']) && isset(Logs::$LOG_DESCRIPTION[$_GET['type']]))
			$type = $_GET['type'];

		if (!isset($_GET['by']))
			$by = null;
		else switch(strtolower(CoreUtils::trim($_GET['by']))){
			case 'me':
			case 'you':
				$initiator = $currentUser->id;
				$by = 'you';
			break;
			case 'web server':
				$initiator = 0;
				$by = 'Web server';
			break;
			default:
				$by = Users::validateName('by', null, true);
				if (isset($by)){
					$by = Users::get($by, 'name', 'id,name');
					$initiator = $by->id;
					$by = $initiator === $currentUser->id ? 'me' : $by->name;
				}
		};

		$Pagination = new Pagination('admin/logs', 20, $Database->count('log'));

		$title = '';
		$q = array();
		if (isset($_GET['js']))
			$q[] = 'js='.$_GET['js'];
		if (isset($type)){
			$Database->where('reftype', $type);
			if (isset($q)){
				$q[] = "type=$type";
				$title .= Logs::$LOG_DESCRIPTION[$type].' entries ';
			}
		}
		else if (isset($q))
			$q[] = 'type='.CoreUtils::FIXPATH_EMPTY;
		if (isset($initiator)){
			$_params = $initiator === 0 ? array('"initiator" IS NULL') : array('initiator',$initiator);
			$Database->where(...$_params);
			if (isset($q) && isset($by)){
				$q[] = "by=$by";
				$title .= (!isset($type)?'Entries ':'')."by $by ";
			}
		}
		else if (isset($q))
			$q[] = 'by='.CoreUtils::FIXPATH_EMPTY;


		$heading = 'Global logs';
		if (!empty($title))
			$title .= '- ';
		$title .= "Page {$Pagination->page} - $heading";
		CoreUtils::fixPath("/admin/logs/{$Pagination->page}".(!empty($q)?'?'.implode('&',$q):''));

		$LogItems = $Database
			->orderBy('timestamp')
			->orderBy('entryid')
			->get('log', $Pagination->getLimit());

		if (isset($_GET['js']))
			$Pagination->respond(Logs::getTbody($LogItems), '#logs tbody');

		CoreUtils::loadPage(array(
			'heading' => $heading,
			'title' => $title,
			'view' => "{$this->do}-logs",
			'css' => "{$this->do}-logs",
			'js' => array("{$this->do}-logs", 'paginate'),
			'import' => [<commit_msg>Fix incorrect pagination output when logs are filtered<commit_after><?php

namespace App\Controllers;
use App\CoreUtils;
use App\CSRFProtection;
use App\HTTP;
use App\Input;
use App\Logs;
use App\Pagination;
use App\Permission;
use App\Posts;
use App\RegExp;
use App\Response;
use App\Users;

class AdminController extends Controller {
	public $do = 'admin';

	function __construct(){
		parent::__construct();

		if (!Permission::sufficient('staff'))
			CoreUtils::notFound();
	}

	function index(){
		CoreUtils::loadPage(array(
			'title' => 'Admin Area',
			'do-css',
			'js' => array('Sortable',$this->do),
		), $this);
	}

	function logs(){
		global $currentUser, $Database, $LogItems, $Pagination;

		$type = Logs::validateRefType('type', true, true);
		if (isset($_GET['type']) && preg_match(new RegExp('/^[a-z_]+$/'), $_GET['type']) && isset(Logs::$LOG_DESCRIPTION[$_GET['type']]))
			$type = $_GET['type'];

		if (!isset($_GET['by']))
			$by = null;
		else switch(strtolower(CoreUtils::trim($_GET['by']))){
			case 'me':
			case 'you':
				$initiator = $currentUser->id;
				$by = 'you';
			break;
			case 'web server':
				$initiator = 0;
				$by = 'Web server';
			break;
			default:
				$by = Users::validateName('by', null, true);
				if (isset($by)){
					$by = Users::get($by, 'name', 'id,name');
					$initiator = $by->id;
					$by = $initiator === $currentUser->id ? 'me' : $by->name;
				}
		};


		$title = '';
		$whereArgs = [];
		$q = array();
		if (isset($_GET['js']))
			$q[] = 'js='.$_GET['js'];
		if (isset($type)){
			$whereArgs[] = array('reftype', $type);
			if (isset($q)){
				$q[] = "type=$type";
				$title .= Logs::$LOG_DESCRIPTION[$type].' entries ';
			}
		}
		else if (isset($q))
			$q[] = 'type='.CoreUtils::FIXPATH_EMPTY;
		if (isset($initiator)){
			$_params = $initiator === 0 ? array('"initiator" IS NULL') : array('initiator',$initiator);
			$whereArgs[] = $_params;
			if (isset($q) && isset($by)){
				$q[] = "by=$by";
				$title .= (!isset($type)?'Entries ':'')."by $by ";
			}
		}
		else if (isset($q))
			$q[] = 'by='.CoreUtils::FIXPATH_EMPTY;

		foreach ($whereArgs as $arg)
			$Database->where(...$arg);
		$Pagination = new Pagination('admin/logs', 20, $Database->count('log'));
		$heading = 'Global logs';
		if (!empty($title))
			$title .= '- ';
		$title .= "Page {$Pagination->page} - $heading";
		CoreUtils::fixPath("/admin/logs/{$Pagination->page}".(!empty($q)?'?'.implode('&',$q):''));

		foreach ($whereArgs as $arg)
			$Database->where(...$arg);
		$LogItems = $Database
			->orderBy('timestamp')
			->orderBy('entryid')
			->get('log', $Pagination->getLimit());

		if (isset($_GET['js']))
			$Pagination->respond(Logs::getTbody($LogItems), '#logs tbody');

		CoreUtils::loadPage(array(
			'heading' => $heading,
			'title' => $title,
			'view' => "{$this->do}-logs",
			'css' => "{$this->do}-logs",
			'js' => array("{$this->do}-logs", 'paginate'),
			'import' => [
{'fraction_non_alphanumeric': 0.24474660074165636, 'fraction_numerical': 0.0014126787921596327, 'mean_word_length': 4.007957559681698, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 55, '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': '2883376', 'n_tokens_mistral': 2403, 'n_tokens_neox': 2087, 'n_words': 412}