Commit 444882d6 authored by Robert Lyon's avatar Robert Lyon Committed by Gerrit Code Review
Browse files

Adding back in some needed files for webservices (Bug #1396845)



They are needed for the webservices logs search.  Have removed the bits
that are not needed now that we use autoselect for some of the
functionality.

Change-Id: I11480476d624883ff68ae8fbe33a1b4e6afb58d2
Signed-off-by: Robert Lyon's avatarRobert Lyon <robertl@catalyst.net.nz>
parent b4b8463c
{include file="header.tpl"}
<form action="{$WWWROOT}webservice/admin/webservicelogs.php" method="post">
<form id="logsearchform" action="{$WWWROOT}webservice/admin/webservicelogs.php" method="post">
<div class="searchform">
<label>{str tag='userauth' section='auth.webservice'}:</label>
<input type="text" name="userquery" id="query"{if $search->userquery} value="{$search->userquery}"{/if}>
......@@ -79,5 +79,12 @@
</div>
</div>
</form>
<script type="text/javascript">
// to clear any offset when submitting form again
jQuery(function() {
jQuery('#logsearchform').submit(function(e) {
jQuery('.currentoffset').attr('value', 0);
});
});
</script>
{include file="footer.tpl"}
/**
* 'Speeds up' the user search if the user has javascript enabled in their
* browser
*
* @package mahara
* @subpackage auth-webservice
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
* @copyright For copyright information on Mahara, please see the README file distributed with this software.
*
*/
function UserSearch() {
var self = this;
this.init = function () {
self.rewriteInitials();
self.rewriteQueryButton();
self.rewritePaging();
self.params = {};
}
this.rewriteInitials = function() {
forEach(getElementsByTagAndClassName('span', 'first-initial', 'firstnamelist'), function(i) {
self.rewriteInitial('f', i);
});
forEach(getElementsByTagAndClassName('span', 'last-initial', 'lastnamelist'), function(i) {
self.rewriteInitial('l', i);
});
}
this.rewriteInitial = function(t, i) {
connect(i, 'onclick', partial(self.searchInitial, t));
}
this.searchInitial = function(initialtype, e) {
// Clear all search params except for the other initial
if (initialtype == 'f') {
if (self.params.l) {
self.params = {'l' : self.params.l};
}
else {
self.params = {};
}
forEach(getElementsByTagAndClassName('span', 'selected', 'firstnamelist'), function (i) {
removeElementClass(i, 'selected');
});
}
else if (initialtype == 'l') {
if (self.params.f) {
self.params = {'f' : self.params.f};
}
else {
self.params = {};
}
forEach(getElementsByTagAndClassName('span', 'selected', 'lastnamelist'), function (i) {
removeElementClass(i, 'selected');
});
}
addElementClass(this, 'selected');
if (!hasElementClass(this, 'all')) {
self.params[initialtype] = scrapeText(this).replace(/\s+/g, '');
}
self.doSearch();
e.stop();
};
this.rewritePaging = function() {
forEach(getElementsByTagAndClassName('span', 'pagination', 'searchresults'), function(i) {
connect(i, 'onclick', self.changePage);
});
}
this.changePage = function(e) {
var children = getElementsByTagAndClassName('a', null, this);
if (children.length == 1) {
var linkparams = parseQueryString(getNodeAttribute(children[0], 'href'));
self.params.offset = linkparams.offset;
self.doSearch();
}
e.stop();
}
this.rewriteQueryButton = function() {
connect($('query-button'), 'onclick', self.newQuery);
}
this.newQuery = function(e) {
self.params = {};
self.params.query = $('query').value;
self.doSearch();
e.stop();
}
this.doSearch = function() {
self.params.action = 'search';
self.params.token = $('token').value;
self.params.suid = $('suid').value;
self.params.ouid = $('ouid').value;
sendjsonrequest('search.json.php', self.params, 'POST', function(data) {
$('results').innerHTML = data.data;
self.rewritePaging();
});
}
addLoadEvent(self.init);
}
userSearch = new UserSearch();
<?php
/**
*
* @package mahara
* @subpackage auth-webservice
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
* @copyright For copyright information on Mahara, please see the README file distributed with this software.
*
*/
define('INTERNAL', 1);
define('JSON', 1);
define('INSTITUTIONALADMIN', 1);
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
$action = param_variable('action');
if ($action == 'search') {
require_once('webservicessearchlib.php');
$params = new StdClass;
$params->query = trim(param_variable('query', ''));
$params->institution = param_alphanum('institution', null);
$params->f = param_alpha('f', null);
$params->l = param_alpha('l', null);
$params->institution_requested = param_alphanum('institution_requested', null);
$offset = param_integer('offset', 0);
$limit = param_integer('limit', 10);
$sortby = param_alpha('sortby', 'firstname');
$sortdir = param_alpha('sortdir', 'asc');
$token = param_variable('token', '');
$suid = param_variable('suid', '');
$ouid = param_variable('ouid', '');
$params->sortby = $sortby;
$params->sortdir = $sortdir;
$params->offset = $offset;
$params->limit = $limit;
json_headers();
if (param_boolean('raw', false)) {
$data = get_admin_user_search_results($params, $offset, $limit, $sortby, $sortdir);
}
else {
$data['data'] = build_webservice_user_search_results($params, $offset, $limit, $sortby, $sortdir);
}
$data['error'] = false;
$data['message'] = null;
echo json_encode($data);
exit;
}
......@@ -45,7 +45,7 @@ else {
$search->institution_requested = param_alphanum('institution_requested', 'all');
}
$smarty = smarty(array(get_config('wwwroot') . 'webservice/admin/js/usersearch.js'), array('<link rel="stylesheet" type="text/css" href="' . $THEME->get_url('style/webservice.css', false, 'auth/webservice') . '">',));
$smarty = smarty(array(), array('<link rel="stylesheet" type="text/css" href="' . $THEME->get_url('style/webservice.css', false, 'auth/webservice') . '">',));
safe_require('auth', 'webservice');
$smarty->assign('search', $search);
......
<?php
/**
*
* @package mahara
* @subpackage auth-webservice
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
* @copyright For copyright information on Mahara, please see the README file distributed with this software.
*
*/
defined('INTERNAL') || die();
require_once('searchlib.php');
require_once('user.php');
function build_webservice_log_search_results($search, $offset, $limit, $sortby, $sortdir) {
global $THEME;
$THEME->templatedirs[]= get_config('docroot') . 'auth/webservice/theme/raw/';
$results = get_log_search_results($search, $offset, $limit, $sortby, $sortdir);
$params = array();
foreach ($search as $k => $v) {
if (!empty($v)) {
$params[] = $k . '=' . $v;
}
}
$searchurl = get_config('wwwroot') . 'webservice/admin/webservicelogs.php?' . join('&', $params) . '&limit=' . $limit;
$pagination = $results['pagination'] = build_pagination(array(
'id' => 'admin_usersearch_pagination',
'class' => 'center',
'url' => $searchurl,
'count' => $results['count'],
'limit' => $limit,
'setlimit' => true,
'jumplinks' => 8,
'numbersincludeprevnext' => 2,
'offset' => $offset,
'datatable' => 'searchresults',
'jsonscript' => 'webservice/admin/logsearch.json.php',
));
$cols = array(
'username' => array('name' => get_string('userauth', 'auth.webservice'),
'template' => 'auth:webservice:username.tpl',
'class' => 'center',
'sort' => true),
'institution' => array('name' => get_string('institution'), 'sort' => true),
'protocol' => array('name' => get_string('protocol', 'auth.webservice'), 'sort' => true),
'auth' => array('name' => get_string('authtype', 'auth.webservice'), 'sort' => true),
'functionname' => array('name' => get_string('function', 'auth.webservice'), 'sort' => true),
'timetaken' => array('name' => get_string('timetaken', 'auth.webservice'), 'sort' => true),
'timelogged' => array('name' => get_string('timelogged', 'auth.webservice'), 'sort' => true),
'info' => array('name' => get_string('info', 'auth.webservice'), 'class' => 'webservicelogs-info'),
);
$smarty = smarty_core();
$smarty->assign_by_ref('results', $results);
$smarty->assign_by_ref('institutions', $institutions);
$smarty->assign('searchurl', $searchurl);
$smarty->assign('sortby', $sortby);
$smarty->assign('sortdir', $sortdir);
$smarty->assign('limitoptions', array(10, 50, 100, 200, 500));
$smarty->assign('pagebaseurl', $searchurl . '&sortby=' . $sortby . '&sortdir=' . $sortdir);
$smarty->assign('cols', $cols);
return array($smarty->fetch('searchresulttable.tpl'), $cols, array(
'url' => $searchurl . '&sortby=' . $sortby . '&sortdir=' . $sortdir,
'sortby' => $search->sortby,
'sortdir' => $search->sortdir
), $pagination);
}
/**
* Split a query string into search terms.
*
* Contents of double-quoted strings are counted as a single term,
* '"' can be entered as '\"', '\' as '\\'.
*/
function split_query_string($query) {
$terms = array();
// Split string on unescaped double quotes
$quotesplit = preg_split('/(?<!\\\)(\")/', $query, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
$inphrase = false;
foreach ($quotesplit as $q) {
if ($q == '"') {
$inphrase = !$inphrase;
continue;
}
// Remove escaping
$q = preg_replace(array('/\x5C(?!\x5C)/u', '/\x5C\x5C/u'), array('','\\'), $q);
if ($inphrase) {
if ($trimmed = trim($q)) {
$terms[] = $trimmed;
}
}
else {
// Split unquoted sequences on spaces
foreach (preg_split('/\s+/', $q, -1, PREG_SPLIT_NO_EMPTY) as $word) {
if ($word) {
$terms[] = $word;
}
}
}
}
return $terms;
}
function get_log_search_results($search, $offset, $limit) {
$sort = 'TRUE';
if (preg_match('/^[a-zA-Z_0-9"]+$/', $search->sortby)) {
$sort = $search->sortby;
if (strtoupper($search->sortdir) != 'DESC') {
$sort .= ' ASC';
}
else {
$sort .= ' DESC';
}
}
$where = '';
$ilike = db_ilike();
$wheres = array();
$params = array();
if ($search->protocol != 'all') {
$wheres[]= ' el.protocol = ? ';
$params[] = $search->protocol;
}
if ($search->authtype != 'all') {
$wheres[]= ' el.auth = ? ';
$params[] = $search->authtype;
}
if ($search->institution != 'all') {
$wheres[]= ' el.institution = ? ';
$params[] = $search->institution;
}
if ($search->onlyerrors == 1) {
$wheres[]= ' TRIM(el.info) > \' \' ';
}
if ($search->userquery) {
$userwheres = array();
$terms = split_query_string(strtolower(trim($search->userquery)));
foreach ($terms as $term) {
foreach (array('u.username', 'u.firstname', 'u.lastname') as $tests) {
$userwheres[]= ' ' . $tests . ' ' . $ilike . ' \'%' . addslashes($term) . '%\'';
}
}
if (!empty($userwheres)) {
$wheres[]= ' ( ' . implode(' OR ', $userwheres) . ' ) ';
}
}
if ($search->functionquery) {
$functionwheres = array();
$terms = split_query_string(strtolower(trim($search->functionquery)));
foreach ($terms as $term) {
$functionwheres[]= ' el.functionname ' . $ilike . ' \'%' . addslashes($term) . '%\'';
}
if (!empty($functionwheres)) {
$wheres[]= ' ( ' . implode(' OR ', $functionwheres) . ' ) ';
}
}
if (empty($wheres)) {
$wheres[]= ' TRUE ';
}
$where = ' WHERE ' . implode(' AND ', $wheres);
$count = count_records_sql('
SELECT COUNT(*)
FROM {external_services_logs} el
JOIN {usr} u
ON el.userid = u.id
' . $where, $params);
$data = get_records_sql_array('
SELECT u.username,
u.firstname,
u.lastname,
u.email,
el.*
FROM {external_services_logs} el
JOIN {usr} u
ON el.userid = u.id
' . $where . ' ORDER BY ' . $sort, $params, $offset);
$results = array(
'count' => $count,
'limit' => $limit,
'offset' => $offset,
'data' => array(),
);
if (!empty($data)) {
foreach ($data as $row) {
$row->timelogged = format_date($row->timelogged, 'strftimedatetime');
$row->institution = institution_display_name($row->institution);
$results['data'][] = (array) $row;
}
}
return $results;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment