MDL-14978 removed obsoleted isadmin()

This commit is contained in:
skodak 2008-05-25 10:08:05 +00:00
parent 72f563b91c
commit e3c7f155d8
23 changed files with 432 additions and 263 deletions

View file

@ -79,7 +79,7 @@
} }
} }
} else { // if upgrading from 1.6 or below } else { // if upgrading from 1.6 or below
if (isadmin() && moodle_needs_upgrading()) { if (is_siteadmin() && moodle_needs_upgrading()) {
redirect($CFG->wwwroot .'/'. $CFG->admin .'/index.php'); redirect($CFG->wwwroot .'/'. $CFG->admin .'/index.php');
} }
} }

View file

@ -514,7 +514,7 @@ function has_any_capability($capabilities, $context, $userid=NULL, $doanything=t
* - moodle/site:doanything * - moodle/site:doanything
* *
* @param int $userid * @param int $userid
* @returns bool $isadmin * @returns bool true is user can administer server settings
*/ */
function is_siteadmin($userid) { function is_siteadmin($userid) {
global $CFG, $DB; global $CFG, $DB;
@ -532,8 +532,7 @@ function is_siteadmin($userid) {
HAVING SUM(rc.permission) > 0"; HAVING SUM(rc.permission) > 0";
$params = array($userid, 'moodle/site:config', 'moodle/legacy:admin', 'moodle/site:doanything'); $params = array($userid, 'moodle/site:config', 'moodle/legacy:admin', 'moodle/site:doanything');
$isadmin = $DB->record_exists_sql($sql, $params); return $DB->record_exists_sql($sql, $params);
return $isadmin;
} }
function get_course_from_path ($path) { function get_course_from_path ($path) {

View file

@ -35,38 +35,6 @@
* @package moodlecore * @package moodlecore
*/ */
/**
* Determines if a user an admin
*
* @uses $USER
* @param int $userid The id of the user as is found in the 'user' table
* @staticvar array $admins List of users who have been found to be admins by user id
* @staticvar array $nonadmins List of users who have been found not to be admins by user id
* @return bool
*/
function isadmin($userid=0) {
global $USER, $CFG;
if (empty($CFG->rolesactive)) { // Then the user is likely to be upgrading NOW
if (!$userid) {
if (empty($USER->id)) {
return false;
}
if (!empty($USER->admin)) {
return true;
}
$userid = $USER->id;
}
return record_exists('user_admins', 'userid', $userid);
}
$context = get_context_instance(CONTEXT_SYSTEM);
return has_capability('moodle/legacy:admin', $context, $userid, false);
}
/** /**
* Determines if a user is a teacher (or better) * Determines if a user is a teacher (or better)
* *

View file

@ -1477,7 +1477,7 @@ function feedback_get_groupid($course, $cm) {
$groupmode = groupmode($course, $cm); $groupmode = groupmode($course, $cm);
//get groupid //get groupid
if($groupmode > 0 && !isadmin()) { if($groupmode > 0 && !has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) {
if($mygroupid = mygroupid($course->id)) { if($mygroupid = mygroupid($course->id)) {
return $mygroupid[0]; //get the first groupid return $mygroupid[0]; //get the first groupid
} }

View file

@ -114,7 +114,7 @@
//get students in conjunction with groupmode //get students in conjunction with groupmode
if($groupmode > 0) { if($groupmode > 0) {
if($SESSION->feedback->lstgroupid == -2) { if($SESSION->feedback->lstgroupid == -2) {
if(isadmin()) { if(has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) {
$mygroupid = false; $mygroupid = false;
$SESSION->feedback->lstgroupid = false; $SESSION->feedback->lstgroupid = false;
}else{ }else{

View file

@ -34,7 +34,7 @@
error(get_string('globalsearchdisabled', 'search')); error(get_string('globalsearchdisabled', 'search'));
} }
if (!isadmin()) { if (!has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) {
error(get_string('beadmin', 'search'), "$CFG->wwwroot/login/index.php"); error(get_string('beadmin', 'search'), "$CFG->wwwroot/login/index.php");
} }

View file

@ -31,7 +31,7 @@
error(get_string('globalsearchdisabled', 'search')); error(get_string('globalsearchdisabled', 'search'));
} }
if (!isadmin()) { if (!has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) {
error(get_string('beadmin', 'search'), "$CFG->wwwroot/login/index.php"); error(get_string('beadmin', 'search'), "$CFG->wwwroot/login/index.php");
} //if } //if

View file

@ -166,7 +166,8 @@ function forum_get_discussions_fast($forum_id) {
$timelimit=''; $timelimit='';
if (!empty($CFG->forum_enabletimedposts)) { if (!empty($CFG->forum_enabletimedposts)) {
if (!((isadmin() and !empty($CFG->admineditalways)) || isteacher(get_field('forum', 'course', 'id', $forum_id)))) { if (!((has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))
and !empty($CFG->admineditalways)) || isteacher(get_field('forum', 'course', 'id', $forum_id)))) {
$now = time(); $now = time();
$timelimit = " AND ((d.timestart = 0 OR d.timestart <= '$now') AND (d.timeend = 0 OR d.timeend > '$now')"; $timelimit = " AND ((d.timestart = 0 OR d.timestart <= '$now') AND (d.timeend = 0 OR d.timeend > '$now')";
if (!empty($USER->id)) { if (!empty($USER->id)) {
@ -302,7 +303,8 @@ function forum_check_text_access($path, $itemtype, $this_id, $user, $group_id, $
* @param string $title * @param string $title
*/ */
function forum_link_post_processing($title){ function forum_link_post_processing($title){
return mb_convert_encoding($title, 'UTF-8', 'auto'); // return mb_convert_encoding($title, 'UTF-8', 'auto');
return mb_convert_encoding($title, 'auto', 'UTF-8');
} }
?> ?>

View file

@ -18,11 +18,11 @@
* @param object $resource * @param object $resource
* @uses CFG, USER * @uses CFG, USER
*/ */
function get_text_for_indexing_doc(&$resource){ function get_text_for_indexing_doc(&$resource, $directfile = ''){
global $CFG, $USER; global $CFG, $USER;
// SECURITY : do not allow non admin execute anything on system !! // SECURITY : do not allow non admin execute anything on system !!
if (!isadmin($USER->id)) return; if (!has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) return;
$moodleroot = (@$CFG->block_search_usemoodleroot) ? "{$CFG->dirroot}/" : '' ; $moodleroot = (@$CFG->block_search_usemoodleroot) ? "{$CFG->dirroot}/" : '' ;
@ -30,11 +30,14 @@ function get_text_for_indexing_doc(&$resource){
if (!empty($CFG->block_search_word_to_text_cmd)){ if (!empty($CFG->block_search_word_to_text_cmd)){
if (!file_exists("{$moodleroot}{$CFG->block_search_word_to_text_cmd}")){ if (!file_exists("{$moodleroot}{$CFG->block_search_word_to_text_cmd}")){
mtrace('Error with MSWord to text converter command : exectuable not found.'); mtrace('Error with MSWord to text converter command : exectuable not found.');
} else {
if ($directfile == ''){
$file = escapeshellarg("{$CFG->dataroot}/{$resource->course}/{$resource->reference}");
} else {
$file = escapeshellarg("{$CFG->dataroot}/{$directfile}");
} }
else{
$file = escapeshellarg($CFG->dataroot.'/'.$resource->course.'/'.$resource->reference);
$command = trim($CFG->block_search_word_to_text_cmd); $command = trim($CFG->block_search_word_to_text_cmd);
$text_converter_cmd = "{$moodleroot}{$command} $file"; $text_converter_cmd = "{$moodleroot}{$command} -m UTF-8.txt $file";
if ($CFG->block_search_word_to_text_env){ if ($CFG->block_search_word_to_text_env){
putenv($CFG->block_search_word_to_text_env); putenv($CFG->block_search_word_to_text_env);
} }
@ -42,14 +45,12 @@ function get_text_for_indexing_doc(&$resource){
$result = shell_exec($text_converter_cmd); $result = shell_exec($text_converter_cmd);
if ($result){ if ($result){
return mb_convert_encoding($result, 'UTF8', 'auto'); return mb_convert_encoding($result, 'UTF8', 'auto');
} } else {
else{
mtrace('Error with MSWord to text converter command : execution failed. '); mtrace('Error with MSWord to text converter command : execution failed. ');
return ''; return '';
} }
} }
} } else {
else {
mtrace('Error with MSWord to text converter command : command not set up. Execute once search block configuration.'); mtrace('Error with MSWord to text converter command : command not set up. Execute once search block configuration.');
return ''; return '';
} }

View file

@ -17,14 +17,18 @@
* @param object $resource * @param object $resource
* @uses CFG, USER * @uses CFG, USER
*/ */
function get_text_for_indexing_htm(&$resource){ function get_text_for_indexing_htm(&$resource, $directfile = ''){
global $CFG, $USER; global $CFG, $USER;
// SECURITY : do not allow non admin execute anything on system !! // SECURITY : do not allow non admin execute anything on system !!
if (!isadmin($USER->id)) return; if (!has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) return;
// just get text // just get text
if ($directfile == ''){
$text = implode('', file("{$CFG->dataroot}/{$resource->course}/{$resource->reference}")); $text = implode('', file("{$CFG->dataroot}/{$resource->course}/{$resource->reference}"));
} else {
$text = implode('', file("{$CFG->dataroot}/{$directfile}"));
}
// extract keywords and other interesting meta information and put it back as real content for indexing // extract keywords and other interesting meta information and put it back as real content for indexing
if (preg_match('/(.*)<meta ([^>]*)>(.*)/is', $text, $matches)){ if (preg_match('/(.*)<meta ([^>]*)>(.*)/is', $text, $matches)){
@ -40,7 +44,7 @@ function get_text_for_indexing_htm(&$resource){
$text = preg_replace("/<[^>]*>/", '', $text); $text = preg_replace("/<[^>]*>/", '', $text);
$text = preg_replace("/<!--[^>]*-->/", '', $text); $text = preg_replace("/<!--[^>]*-->/", '', $text);
$text = html_entity_decode($text, ENT_COMPAT, 'UTF-8'); $text = html_entity_decode($text, ENT_COMPAT, 'UTF-8');
$text = mb_convert_encoding($text, 'UTF-8', 'AUTO'); $text = mb_convert_encoding($text, 'UTF-8', 'auto');
/* /*
* debug code for tracing input * debug code for tracing input

View file

@ -17,11 +17,11 @@
* @param object $resource * @param object $resource
* @uses CFG, USER * @uses CFG, USER
*/ */
function get_text_for_indexing_pdf(&$resource){ function get_text_for_indexing_pdf(&$resource, $directfile = ''){
global $CFG, $USER; global $CFG, $USER;
// SECURITY : do not allow non admin execute anything on system !! // SECURITY : do not allow non admin execute anything on system !!
if (!isadmin($USER->id)) return; if (!has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) return;
// adds moodle root switch if none was defined // adds moodle root switch if none was defined
if (!isset($CFG->block_search_usemoodleroot)){ if (!isset($CFG->block_search_usemoodleroot)){
@ -34,23 +34,24 @@ function get_text_for_indexing_pdf(&$resource){
if (!empty($CFG->block_search_pdf_to_text_cmd)){ if (!empty($CFG->block_search_pdf_to_text_cmd)){
preg_match("/^\S+/", $CFG->block_search_pdf_to_text_cmd, $matches); preg_match("/^\S+/", $CFG->block_search_pdf_to_text_cmd, $matches);
if (!file_exists("{$moodleroot}{$matches[0]}")){ if (!file_exists("{$moodleroot}{$matches[0]}")){
mtrace('Error with pdf to text converter command : exectuable not found at '.$moodleroot.$matches[0]); mtrace('Error with pdf to text converter command : executable not found at '.$moodleroot.$matches[0]);
} else {
if ($directfile == ''){
$file = escapeshellarg("{$CFG->dataroot}/{$resource->course}/{$resource->reference}");
} else {
$file = escapeshellarg("{$CFG->dataroot}/{$directfile}");
} }
else{
$file = escapeshellarg($CFG->dataroot.'/'.$resource->course.'/'.$resource->reference);
$command = trim($CFG->block_search_pdf_to_text_cmd); $command = trim($CFG->block_search_pdf_to_text_cmd);
$text_converter_cmd = "{$moodleroot}{$command} $file -"; $text_converter_cmd = "{$moodleroot}{$command} $file -";
$result = shell_exec($text_converter_cmd); $result = shell_exec($text_converter_cmd);
if ($result){ if ($result){
return $result; return $result;
} } else {
else{
mtrace('Error with pdf to text converter command : execution failed for '.$text_converter_cmd.'. Check for execution permission on pdf converter executable.'); mtrace('Error with pdf to text converter command : execution failed for '.$text_converter_cmd.'. Check for execution permission on pdf converter executable.');
return ''; return '';
} }
} }
} } else {
else {
mtrace('Error with pdf to text converter command : command not set up. Execute once search block configuration.'); mtrace('Error with pdf to text converter command : command not set up. Execute once search block configuration.');
return ''; return '';
} }

View file

@ -31,15 +31,19 @@
* @param object $resource * @param object $resource
* @uses CFG, USER * @uses CFG, USER
*/ */
function get_text_for_indexing_ppt(&$resource){ function get_text_for_indexing_ppt(&$resource, $directfile = ''){
global $CFG, $USER; global $CFG, $USER;
$indextext = null; $indextext = null;
// SECURITY : do not allow non admin execute anything on system !! // SECURITY : do not allow non admin execute anything on system !!
if (!isadmin($USER->id)) return; if (!has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) return;
if ($directfile == ''){
$text = implode('', file("{$CFG->dataroot}/{$resource->course}/{$resource->reference}")); $text = implode('', file("{$CFG->dataroot}/{$resource->course}/{$resource->reference}"));
} else {
$text = implode('', file("{$CFG->dataroot}/{$directfile}"));
}
$remains = $text; $remains = $text;
$fragments = array(); $fragments = array();

View file

@ -0,0 +1,69 @@
<?php
/**
* Global Search Engine for Moodle
*
* @package search
* @category core
* @subpackage document_wrappers
* @author Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
* @date 2008/03/31
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*
* @note : The Adobe SWF Converters library is not GPL, although it can be of free use in some
* situations. This file is provided for convenience, but should use having a glance at
* {@link http://www.adobe.com/licensing/developer/}
*
* this is a format handler for getting text out of a proprietary binary format
* so it can be indexed by Lucene search engine
*/
/**
* @param object $resource
* @uses CFG, USER
*/
function get_text_for_indexing_swf(&$resource, $directfile = ''){
global $CFG, $USER;
// SECURITY : do not allow non admin execute anything on system !!
if (!has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) return;
// adds moodle root switch if none was defined
if (!isset($CFG->block_search_usemoodleroot)){
set_config('block_search_usemoodleroot', 1);
}
$moodleroot = ($CFG->block_search_usemoodleroot) ? "{$CFG->dirroot}/" : '' ;
// just call pdftotext over stdout and capture the output
if (!empty($CFG->block_search_pdf_to_text_cmd)){
if (!file_exists("{$moodleroot}{$command}")){
mtrace('Error with swf to text converter command : executable not found as '.$moodleroot.$command);
} else {
if ($directfile == ''){
$file = escapeshellarg("{$CFG->dataroot}/{$resource->course}/{$resource->reference}");
} else {
$file = escapeshellarg("{$CFG->dataroot}/{$directfile}");
}
$command = trim($CFG->block_search_swf_to_text_cmd);
$text_converter_cmd = "{$moodleroot}{$command} -t $file";
$result = shell_exec($text_converter_cmd);
// result is in html. We must strip it off
$result = preg_replace("/<[^>]*>/", '', $result);
$result = preg_replace("/<!--[^>]*-->/", '', $result);
$result = html_entity_decode($result, ENT_COMPAT, 'UTF-8');
$result = mb_convert_encoding($result, 'UTF-8', 'auto');
if ($result){
return $result;
} else {
mtrace('Error with swf to text converter command : execution failed for '.$text_converter_cmd.'. Check for execution permission on swf converter executable.');
return '';
}
}
} else {
mtrace('Error with swf to text converter command : command not set up. Execute once search block configuration.');
return '';
}
}
?>

View file

@ -17,14 +17,19 @@
* @param object $resource * @param object $resource
* @uses CFG, USER * @uses CFG, USER
*/ */
function get_text_for_indexing_txt(&$resource){ function get_text_for_indexing_txt(&$resource, $directfile = ''){
global $CFG, $USER; global $CFG, $USER;
// SECURITY : do not allow non admin execute anything on system !! // SECURITY : do not allow non admin execute anything on system !!
if (!isadmin($USER->id)) return; if (!has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) return;
// just try to get text empirically from ppt binary flow // just try to get text empirically from ppt binary flow
if ($directfile == ''){
$text = implode('', file("{$CFG->dataroot}/{$resource->course}/{$resource->reference}")); $text = implode('', file("{$CFG->dataroot}/{$resource->course}/{$resource->reference}"));
} else {
$text = implode('', file("{$CFG->dataroot}/{$directfile}"));
}
if (!empty($CFG->block_search_limit_index_body)){ if (!empty($CFG->block_search_limit_index_body)){
$text = shorten($text, $CFG->block_search_limit_index_body); $text = shorten($text, $CFG->block_search_limit_index_body);
} }

View file

@ -21,7 +21,7 @@ function get_text_for_indexing_xml(&$resource, $directfile = ''){
global $CFG, $USER; global $CFG, $USER;
// SECURITY : do not allow non admin execute anything on system !! // SECURITY : do not allow non admin execute anything on system !!
if (!isadmin($USER->id)) return; if (!has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) return;
// just get text // just get text
if ($directfile == ''){ if ($directfile == ''){

View file

@ -334,7 +334,7 @@ function user_check_text_access($path, $itemtype, $this_id, $user, $group_id, $c
$userrecord = get_record('user', 'id', $this_id); $userrecord = get_record('user', 'id', $this_id);
// we cannot see nothing from unconfirmed users // we cannot see nothing from unconfirmed users
if (!$userrecord->confirmed and !isadmin()){ if (!$userrecord->confirmed and !has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))){
if (!empty($CFG->search_access_debug)) echo "search reject : unconfirmed user "; if (!empty($CFG->search_access_debug)) echo "search reject : unconfirmed user ";
return false; return false;
} }

View file

@ -35,6 +35,10 @@
*/ */
require_once('../config.php'); require_once('../config.php');
require_once("$CFG->dirroot/search/lib.php"); require_once("$CFG->dirroot/search/lib.php");
//require_once("debugging.php");
$separator = (array_key_exists('WINDIR', $_SERVER)) ? ';' : ':' ;
ini_set('include_path', $CFG->dirroot.'\search'.$separator.ini_get('include_path'));
/// only administrators can index the moodle installation, because access to all pages is required /// only administrators can index the moodle installation, because access to all pages is required
@ -44,7 +48,7 @@ require_once("$CFG->dirroot/search/lib.php");
error(get_string('globalsearchdisabled', 'search')); error(get_string('globalsearchdisabled', 'search'));
} }
if (!isadmin()) { if (!has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) {
error(get_string('beadmin', 'search'), "$CFG->wwwroot/login/index.php"); error(get_string('beadmin', 'search'), "$CFG->wwwroot/login/index.php");
} }
@ -59,6 +63,13 @@ require_once("$CFG->dirroot/search/lib.php");
exit(0); exit(0);
} }
/// check for php5 (lib.php)
if (!search_check_php5()) {
mtrace("Sorry, global search requires PHP 5.0.0 or later (currently using version ".phpversion().")");
exit(0);
}
//php5 found, continue including php5-only files //php5 found, continue including php5-only files
//require_once("$CFG->dirroot/search/Zend/Search/Lucene.php"); //require_once("$CFG->dirroot/search/Zend/Search/Lucene.php");
require_once("$CFG->dirroot/search/indexlib.php"); require_once("$CFG->dirroot/search/indexlib.php");
@ -86,13 +97,16 @@ require_once("$CFG->dirroot/search/lib.php");
mtrace("Data directory ($index_path) does not exist, attempting to create."); mtrace("Data directory ($index_path) does not exist, attempting to create.");
if (!mkdir($index_path)) { if (!mkdir($index_path)) {
search_pexit("Error creating data directory at: $index_path. Please correct."); search_pexit("Error creating data directory at: $index_path. Please correct.");
} else { }
else {
mtrace("Directory successfully created."); mtrace("Directory successfully created.");
} }
} else { }
else {
mtrace("Using $index_path as data directory."); mtrace("Using $index_path as data directory.");
} }
Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8());
$index = new Zend_Search_Lucene($index_path, true); $index = new Zend_Search_Lucene($index_path, true);
/* /*
@ -116,31 +130,37 @@ require_once("$CFG->dirroot/search/lib.php");
// * mod_iterator // * mod_iterator
// * mod_get_content_for_index // * mod_get_content_for_index
//are the sole basis for including a module in the index at the moment. //are the sole basis for including a module in the index at the moment.
$searchables = array();
/// collects modules $searchables = search_collect_searchables();
if ($mods = get_records('modules', '', '', '', 'id,name')) { /// start indexation
$searchables = array_merge($searchables, $mods);
}
mtrace(count($searchables).' modules found.');
// collects blocks as indexable information may be found in blocks either
if ($blocks = get_records('block', '', '', '', 'id,name')) {
// prepend the "block_" prefix to discriminate document type plugins
foreach(array_keys($blocks) as $aBlockId){
$blocks[$aBlockId]->name = 'block_'.$blocks[$aBlockId]->name;
}
$searchables = array_merge($searchables, $blocks);
mtrace(count($blocks).' blocks found.');
}
/// add virtual modules onto the back of the array
$searchables = array_merge($searchables, search_get_additional_modules());
if ($searchables){ if ($searchables){
foreach ($searchables as $mod) { foreach ($searchables as $mod) {
$key = 'search_in_'.$mod->name;
if (isset($CFG->$key) && !$CFG->$key) {
mtrace("module $key has been administratively disabled. Skipping...\n");
continue;
}
if ($mod->location == 'internal'){
$class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php'; $class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
} else {
$class_file = $CFG->dirroot.'/'.$mod->location.'/'.$mod->name.'/search_document.php';
}
/*
if (!file_exists($class_file)){
if (defined("PATH_FOR_SEARCH_TYPE_{$mod->name}")){
eval("\$pluginpath = PATH_FOR_SEARCH_TYPE_{$mod->name}");
$class_file = "{$CFG->dirroot}/{$pluginpath}/searchlib.php";
} else {
mtrace ("No search document found for plugin {$mod->name}. Ignoring.");
continue;
}
}
*/
if (file_exists($class_file)) { if (file_exists($class_file)) {
include_once($class_file); include_once($class_file);
@ -187,6 +207,8 @@ require_once("$CFG->dirroot/search/lib.php");
mtrace("-- $counter documents indexed"); mtrace("-- $counter documents indexed");
mtrace("done.\n"); mtrace("done.\n");
} }
} else {
mtrace ("No search document found for plugin {$mod->name}. Ignoring.");
} }
} }
} }

View file

@ -18,7 +18,11 @@
* includes and requires * includes and requires
*/ */
require_once('../config.php'); require_once('../config.php');
require_once("$CFG->dirroot/search/lib.php"); require_once("{$CFG->dirroot}/search/lib.php");
/// makes inclusions of the Zend Engine more reliable
$separator = (array_key_exists('WINDIR', $_SERVER)) ? ';' : ':' ;
ini_set('include_path', $CFG->dirroot.'\search'.$separator.ini_get('include_path'));
/// check global search is enabled /// check global search is enabled
@ -28,10 +32,18 @@ require_once("$CFG->dirroot/search/lib.php");
error(get_string('globalsearchdisabled', 'search')); error(get_string('globalsearchdisabled', 'search'));
} }
if (!isadmin()) { if (!has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) {
error(get_string('beadmin', 'search'), "$CFG->wwwroot/login/index.php"); error(get_string('beadmin', 'search'), "$CFG->wwwroot/login/index.php");
} }
/// check for php5 (lib.php)
if (!search_check_php5()) {
$phpversion = phpversion();
mtrace("Sorry, global search requires PHP 5.0.0 or later (currently using version ".phpversion().")");
exit(0);
}
require_once("$CFG->dirroot/search/indexlib.php"); require_once("$CFG->dirroot/search/indexlib.php");
$indexinfo = new IndexInfo(); $indexinfo = new IndexInfo();

View file

@ -50,6 +50,7 @@
/// check for php5, but don't die yet (see line 52) /// check for php5, but don't die yet (see line 52)
if ($check = search_check_php5()) {
require_once("{$CFG->dirroot}/search/querylib.php"); require_once("{$CFG->dirroot}/search/querylib.php");
$page_number = optional_param('page', -1, PARAM_INT); $page_number = optional_param('page', -1, PARAM_INT);
@ -57,10 +58,19 @@
$advanced = (optional_param('a', '0', PARAM_INT) == '1') ? true : false; $advanced = (optional_param('a', '0', PARAM_INT) == '1') ? true : false;
$query_string = optional_param('query_string', '', PARAM_CLEAN); $query_string = optional_param('query_string', '', PARAM_CLEAN);
/**
* discard harmfull searches
*/
if (preg_match("/^[\*\?]+$/", $query_string)){
$query_string = '';
$error = get_string('fullwildcardquery','search');
}
if ($pages && isset($_SESSION['search_advanced_query'])) { if ($pages && isset($_SESSION['search_advanced_query'])) {
// if both are set, then we are busy browsing through the result pages of an advanced query // if both are set, then we are busy browsing through the result pages of an advanced query
$adv = unserialize($_SESSION['search_advanced_query']); $adv = unserialize($_SESSION['search_advanced_query']);
} else if ($advanced) { } elseif ($advanced) {
// otherwise we are dealing with a new advanced query // otherwise we are dealing with a new advanced query
unset($_SESSION['search_advanced_query']); unset($_SESSION['search_advanced_query']);
session_unregister('search_advanced_query'); session_unregister('search_advanced_query');
@ -132,7 +142,9 @@
} }
//run the query against the index //run the query against the index
Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8());
$sq = new SearchQuery($query_string, $page_number, 10, false); $sq = new SearchQuery($query_string, $page_number, 10, false);
}
if (!$site = get_site()) { if (!$site = get_site()) {
redirect("index.php"); redirect("index.php");
@ -153,6 +165,17 @@
print_header("$strsearch", "$site->fullname" , $navigation, "", "", true, "&nbsp;", navmenu($site)); print_header("$strsearch", "$site->fullname" , $navigation, "", "", true, "&nbsp;", navmenu($site));
} }
//keep things pretty, even if php5 isn't available
if (!$check) {
print_heading(search_check_php5(true));
print_footer();
exit(0);
}
if (!empty($error)){
notice ($error);
}
print_box_start(); print_box_start();
print_heading($strquery); print_heading($strquery);
@ -168,7 +191,6 @@
} }
} }
?> ?>
<form id="query" method="get" action="query.php"> <form id="query" method="get" action="query.php">
<?php <?php
if (!$advanced) { if (!$advanced) {
@ -178,7 +200,8 @@
<a href="query.php?a=1"><?php print_string('advancedsearch', 'search') ?></a> | <a href="query.php?a=1"><?php print_string('advancedsearch', 'search') ?></a> |
<a href="stats.php"><?php print_string('statistics', 'search') ?></a> <a href="stats.php"><?php print_string('statistics', 'search') ?></a>
<?php <?php
} else { }
else {
print_box_start(); print_box_start();
?> ?>
<input type="hidden" name="a" value="<?php print $advanced; ?>"/> <input type="hidden" name="a" value="<?php print $advanced; ?>"/>
@ -267,7 +290,8 @@
if ($sq->is_valid_index()) { if ($sq->is_valid_index()) {
//use cached variable to show up-to-date index size (takes deletions into account) //use cached variable to show up-to-date index size (takes deletions into account)
print $CFG->search_index_size; print $CFG->search_index_size;
} else { }
else {
print "0"; print "0";
} }
@ -275,7 +299,7 @@
print_string('documents', 'search'); print_string('documents', 'search');
print '.'; print '.';
if (!$sq->is_valid_index() and isadmin()) { if (!$sq->is_valid_index() and has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) {
print '<p>' . get_string('noindexmessage', 'search') . '<a href="indexersplash.php">' . get_string('createanindex', 'search')."</a></p>\n"; print '<p>' . get_string('noindexmessage', 'search') . '<a href="indexersplash.php">' . get_string('createanindex', 'search')."</a></p>\n";
} }

View file

@ -377,7 +377,7 @@ class SearchQuery {
* course related checks * course related checks
*/ */
// admins can see everything, anyway. // admins can see everything, anyway.
if (isadmin()){ if (has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))){
return true; return true;
} }

View file

@ -30,9 +30,13 @@ require_once("{$CFG->dirroot}/search/lib.php");
error(get_string('globalsearchdisabled', 'search')); error(get_string('globalsearchdisabled', 'search'));
} }
/// check for php5, but don't die yet
if ($check = search_check_php5()) {
require_once("{$CFG->dirroot}/search/indexlib.php"); require_once("{$CFG->dirroot}/search/indexlib.php");
$indexinfo = new IndexInfo(); $indexinfo = new IndexInfo();
}
if (!$site = get_site()) { if (!$site = get_site()) {
redirect("index.php"); redirect("index.php");
@ -54,6 +58,12 @@ require_once("{$CFG->dirroot}/search/lib.php");
/// keep things pretty, even if php5 isn't available /// keep things pretty, even if php5 isn't available
if (!$check) {
print_heading(search_check_php5(true));
print_footer();
exit(0);
}
print_box_start(); print_box_start();
print_heading($strquery); print_heading($strquery);
@ -67,7 +77,7 @@ require_once("{$CFG->dirroot}/search/lib.php");
/// this table is only for admins, shows index directory size and location /// this table is only for admins, shows index directory size and location
if (isadmin()) { if (has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) {
$datadirectorystr = get_string('datadirectory', 'search'); $datadirectorystr = get_string('datadirectory', 'search');
$inindexdirectorystr = get_string('filesinindexdirectory', 'search'); $inindexdirectorystr = get_string('filesinindexdirectory', 'search');
$totalsizestr = get_string('totalsize', 'search'); $totalsizestr = get_string('totalsize', 'search');
@ -137,7 +147,7 @@ require_once("{$CFG->dirroot}/search/lib.php");
/// add extra fields if we're admin /// add extra fields if we're admin
if (isadmin()) { if (has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) {
//don't want to confuse users if the two totals don't match (hint: they should) //don't want to confuse users if the two totals don't match (hint: they should)
$table->data[] = array($documentsinindexstr, $indexinfo->indexcount); $table->data[] = array($documentsinindexstr, $indexinfo->indexcount);

View file

@ -4,64 +4,114 @@
* Carries out some basic function/file existence tests - the search module * Carries out some basic function/file existence tests - the search module
* is expected to exist, along with the db schema files and the search data * is expected to exist, along with the db schema files and the search data
* directory. * directory.
* */ **/
@set_time_limit(0); @set_time_limit(0);
@ob_implicit_flush(true); @ob_implicit_flush(true);
@ob_end_flush(); @ob_end_flush();
require_once('../../config.php'); require_once('../../config.php');
require_once("$CFG->dirroot/search/lib.php"); require_once("$CFG->dirroot/search/lib.php");
require_login(); /// makes inclusions of the Zend Engine more reliable
$separator = (array_key_exists('WINDIR', $_SERVER)) ? ';' : ':' ;
ini_set('include_path', $CFG->dirroot.'\search'.$separator.ini_get('include_path'));
$strsearch = get_string('search', 'search'); require_login();
$strquery = get_string('stats');
$navlinks[] = array('name' => $strsearch, 'link' => "../index.php", 'type' => 'misc'); if (empty($CFG->enableglobalsearch)) {
$navlinks[] = array('name' => $strquery, 'link' => "../stats.php", 'type' => 'misc'); error('Global searching is not enabled.');
$navlinks[] = array('name' => get_string('runindexertest','search'), 'link' => null, 'type' => 'misc'); }
$navigation = build_navigation($navlinks);
$site = get_site();
print_header("$strsearch", "$site->fullname" , $navigation, "", "", true, "&nbsp;", navmenu($site));
if (empty($CFG->enableglobalsearch)) { if (!has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) {
print_error('Global searching is not enabled.'); error("You need to be an admin user to use this page.", "$CFG->wwwroot/login/index.php");
} } //if
if (!isadmin()) { mtrace('<pre>Server Time: '.date('r',time()));
print_error("You need to be an admin user to use this page.", '', "$CFG->wwwroot/login/index.php"); mtrace("Testing global search capabilities:\n");
} //if
mtrace('<pre>Server Time: '.date('r',time())); $phpversion = phpversion();
mtrace("Testing global search capabilities:\n");
$phpversion = phpversion(); if (!search_check_php5()) {
mtrace("ERROR: PHP 5.0.0 or later required (currently using version $phpversion).");
exit(0);
} else {
mtrace("Success: PHP 5.0.0 or later is installed ($phpversion).\n");
} //else
//fix paths for testing //fix paths for testing
set_include_path(get_include_path().":../"); set_include_path(get_include_path().":../");
require_once("$CFG->dirroot/search/Zend/Search/Lucene.php"); require_once("$CFG->dirroot/search/Zend/Search/Lucene.php");
mtrace("Checking activity modules:\n"); mtrace("Checking activity modules:\n");
//the presence of the required search functions - //the presence of the required search functions -
// * mod_iterator // * mod_iterator
// * mod_get_content_for_index // * mod_get_content_for_index
//are the sole basis for including a module in the index at the moment. //are the sole basis for including a module in the index at the moment.
if ($mods = get_records_select('modules')) { /// get all installed modules
$mods = array_merge($mods, search_get_additional_modules()); if ($mods = get_records('modules', '', '', 'name', 'id,name')){
foreach ($mods as $mod) { $searchabletypes = array_values(search_get_document_types());
foreach($mods as $mod){
if (in_array($mod->name, $searchabletypes)){
$mod->location = 'internal';
$searchables[] = $mod;
} else {
$documentfile = $CFG->dirroot."/mod/{$mod->name}/search_document.php";
$mod->location = 'mod';
if (file_exists($documentfile)){
$searchables[] = $mod;
}
}
}
mtrace(count($searchables).' modules to search in / '.count($mods).' modules found.');
}
/// collects blocks as indexable information may be found in blocks either
if ($blocks = get_records('block', '', '', 'name', 'id,name')) {
$blocks_searchables = array();
// prepend the "block_" prefix to discriminate document type plugins
foreach($blocks as $block){
$block->dirname = $block->name;
$block->name = 'block_'.$block->name;
if (in_array('SEARCH_TYPE_'.strtoupper($block->name), $searchabletypes)){
$mod->location = 'internal';
$blocks_searchables[] = $block;
} else {
$documentfile = $CFG->dirroot."/blocks/{$block->dirname}/search_document.php";
if (file_exists($documentfile)){
$mod->location = 'blocks';
$blocks_searchables[] = $block;
}
}
}
mtrace(count($blocks_searchables).' blocks to search in / '.count($blocks).' blocks found.');
$searchables = array_merge($searchables, $blocks_searchables);
}
/// add virtual modules onto the back of the array
$additional = search_get_additional_modules();
mtrace(count($additional).' additional to search in.');
$searchables = array_merge($searchables, $additional);
foreach ($searchables as $mod) {
if ($mod->location == 'internal'){
$class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php'; $class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
} else {
$class_file = $CFG->dirroot.'/'.$mod->location.'/'.$mod->name.'/search_document.php';
}
if (file_exists($class_file)) { if (file_exists($class_file)) {
include_once($class_file); include_once($class_file);
if (!defined('SEARCH_TYPE_'.strtoupper($mod->name))) { if (!defined('SEARCH_TYPE_'.strtoupper($mod->name))) {
mtrace("ERROR: Constant 'SEARCH_TYPE_".strtoupper($mod->name)."' is not defined in /search/lib.php"); mtrace("ERROR: Constant 'SEARCH_TYPE_".strtoupper($mod->name)."' is not defined in search/searchtypes.php or in module");
continue; continue;
} //if }
$iter_function = $mod->name.'_iterator'; $iter_function = $mod->name.'_iterator';
$index_function = $mod->name.'_get_content_for_index'; $index_function = $mod->name.'_get_content_for_index';
@ -74,26 +124,24 @@ if ($mods = get_records_select('modules')) {
mtrace("Success: '$mod->name' module seems to be ready for indexing."); mtrace("Success: '$mod->name' module seems to be ready for indexing.");
} else { } else {
mtrace("ERROR: $index_function() doesn't seem to be returning an array."); mtrace("ERROR: $index_function() doesn't seem to be returning an array.");
} //else }
} else { } else {
mtrace("ERROR: $iter_function() doesn't seem to be returning an object array."); mtrace("ERROR: $iter_function() doesn't seem to be returning an object array.");
} //else }
} else { } else {
mtrace("ERROR: $iter_function() and/or $index_function() does not exist in $class_file"); mtrace("ERROR: $iter_function() and/or $index_function() does not exist in $class_file");
} //else }
} else { } else {
mtrace("Notice: $class_file does not exist, this module will not be indexed."); mtrace("Notice: $class_file does not exist, this module will not be indexed.");
} //else }
} //foreach }
} //if
//finished modules //finished modules
mtrace("\nFinished checking activity modules."); mtrace("\nFinished checking activity modules.");
//now blocks... //now blocks...
// //
mtrace("<br/><a href='../index.php'>Back to query page</a> or <a href='../indexersplash.php'>Start indexing</a>."); mtrace("<br/><a href='../index.php'>Back to query page</a> or <a href='../indexersplash.php'>Start indexing</a>.");
mtrace('</pre>'); mtrace('</pre>');
print_footer();
?> ?>

View file

@ -34,7 +34,7 @@
error(get_string('globalsearchdisabled', 'search')); error(get_string('globalsearchdisabled', 'search'));
} }
if (!isadmin()) { if (!has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) {
error(get_string('beadmin', 'search'), "$CFG->wwwroot/login/index.php"); error(get_string('beadmin', 'search'), "$CFG->wwwroot/login/index.php");
} }