From: skodak Date: Sun, 25 May 2008 10:08:05 +0000 (+0000) Subject: MDL-14978 removed obsoleted isadmin() X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=e3c7f155d88312312ce65617626d5a3a79833077;p=moodle.git MDL-14978 removed obsoleted isadmin() --- diff --git a/index.php b/index.php index 6a66bb1e2c..93644e2e56 100644 --- a/index.php +++ b/index.php @@ -79,7 +79,7 @@ } } } 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'); } } diff --git a/lib/accesslib.php b/lib/accesslib.php index 87c56ff2f4..427b67874b 100755 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -514,7 +514,7 @@ function has_any_capability($capabilities, $context, $userid=NULL, $doanything=t * - moodle/site:doanything * * @param int $userid - * @returns bool $isadmin + * @returns bool true is user can administer server settings */ function is_siteadmin($userid) { global $CFG, $DB; @@ -532,8 +532,7 @@ function is_siteadmin($userid) { HAVING SUM(rc.permission) > 0"; $params = array($userid, 'moodle/site:config', 'moodle/legacy:admin', 'moodle/site:doanything'); - $isadmin = $DB->record_exists_sql($sql, $params); - return $isadmin; + return $DB->record_exists_sql($sql, $params); } function get_course_from_path ($path) { diff --git a/lib/deprecatedlib.php b/lib/deprecatedlib.php index aa35cebd9f..28f987748a 100644 --- a/lib/deprecatedlib.php +++ b/lib/deprecatedlib.php @@ -35,38 +35,6 @@ * @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) * diff --git a/mod/feedback/lib.php b/mod/feedback/lib.php index 1b1999c491..8f61d0daeb 100644 --- a/mod/feedback/lib.php +++ b/mod/feedback/lib.php @@ -1477,7 +1477,7 @@ function feedback_get_groupid($course, $cm) { $groupmode = groupmode($course, $cm); //get groupid - if($groupmode > 0 && !isadmin()) { + if($groupmode > 0 && !has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) { if($mygroupid = mygroupid($course->id)) { return $mygroupid[0]; //get the first groupid } diff --git a/mod/feedback/show_entries.php b/mod/feedback/show_entries.php index d6ee4cd396..62e91bf22f 100644 --- a/mod/feedback/show_entries.php +++ b/mod/feedback/show_entries.php @@ -114,7 +114,7 @@ //get students in conjunction with groupmode if($groupmode > 0) { if($SESSION->feedback->lstgroupid == -2) { - if(isadmin()) { + if(has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) { $mygroupid = false; $SESSION->feedback->lstgroupid = false; }else{ diff --git a/search/add.php b/search/add.php index 69e3e80c58..be33ab3494 100644 --- a/search/add.php +++ b/search/add.php @@ -34,7 +34,7 @@ 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"); } diff --git a/search/delete.php b/search/delete.php index 2e844d4aa9..c269f66610 100644 --- a/search/delete.php +++ b/search/delete.php @@ -31,7 +31,7 @@ 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"); } //if diff --git a/search/documents/forum_document.php b/search/documents/forum_document.php index 3cbe2e108a..a26afd4a4c 100644 --- a/search/documents/forum_document.php +++ b/search/documents/forum_document.php @@ -166,7 +166,8 @@ function forum_get_discussions_fast($forum_id) { $timelimit=''; 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(); $timelimit = " AND ((d.timestart = 0 OR d.timestart <= '$now') AND (d.timeend = 0 OR d.timeend > '$now')"; if (!empty($USER->id)) { @@ -302,7 +303,8 @@ function forum_check_text_access($path, $itemtype, $this_id, $user, $group_id, $ * @param string $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'); } ?> \ No newline at end of file diff --git a/search/documents/physical_doc.php b/search/documents/physical_doc.php index ca053bde51..9586ff12cc 100644 --- a/search/documents/physical_doc.php +++ b/search/documents/physical_doc.php @@ -18,11 +18,11 @@ * @param object $resource * @uses CFG, USER */ -function get_text_for_indexing_doc(&$resource){ +function get_text_for_indexing_doc(&$resource, $directfile = ''){ global $CFG, $USER; // 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}/" : '' ; @@ -30,11 +30,14 @@ function get_text_for_indexing_doc(&$resource){ if (!empty($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.'); - } - else{ - $file = escapeshellarg($CFG->dataroot.'/'.$resource->course.'/'.$resource->reference); + } else { + if ($directfile == ''){ + $file = escapeshellarg("{$CFG->dataroot}/{$resource->course}/{$resource->reference}"); + } else { + $file = escapeshellarg("{$CFG->dataroot}/{$directfile}"); + } $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){ 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); if ($result){ return mb_convert_encoding($result, 'UTF8', 'auto'); - } - else{ + } else { mtrace('Error with MSWord to text converter command : execution failed. '); return ''; } } - } - else { + } else { mtrace('Error with MSWord to text converter command : command not set up. Execute once search block configuration.'); return ''; } diff --git a/search/documents/physical_htm.php b/search/documents/physical_htm.php index 76d6073dd6..aa3e6bc0f1 100644 --- a/search/documents/physical_htm.php +++ b/search/documents/physical_htm.php @@ -17,14 +17,18 @@ * @param object $resource * @uses CFG, USER */ -function get_text_for_indexing_htm(&$resource){ +function get_text_for_indexing_htm(&$resource, $directfile = ''){ global $CFG, $USER; // 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 - $text = implode('', file("{$CFG->dataroot}/{$resource->course}/{$resource->reference}")); + if ($directfile == ''){ + $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 if (preg_match('/(.*)]*)>(.*)/is', $text, $matches)){ @@ -40,7 +44,7 @@ function get_text_for_indexing_htm(&$resource){ $text = preg_replace("/<[^>]*>/", '', $text); $text = preg_replace("//", '', $text); $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 diff --git a/search/documents/physical_pdf.php b/search/documents/physical_pdf.php index 291e0f4a05..347b61f975 100644 --- a/search/documents/physical_pdf.php +++ b/search/documents/physical_pdf.php @@ -17,11 +17,11 @@ * @param object $resource * @uses CFG, USER */ -function get_text_for_indexing_pdf(&$resource){ +function get_text_for_indexing_pdf(&$resource, $directfile = ''){ global $CFG, $USER; // 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 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)){ preg_match("/^\S+/", $CFG->block_search_pdf_to_text_cmd, $matches); if (!file_exists("{$moodleroot}{$matches[0]}")){ - mtrace('Error with pdf to text converter command : exectuable not found at '.$moodleroot.$matches[0]); - } - else{ - $file = escapeshellarg($CFG->dataroot.'/'.$resource->course.'/'.$resource->reference); + 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}"); + } $command = trim($CFG->block_search_pdf_to_text_cmd); $text_converter_cmd = "{$moodleroot}{$command} $file -"; $result = shell_exec($text_converter_cmd); if ($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.'); return ''; } } - } - else { + } else { mtrace('Error with pdf to text converter command : command not set up. Execute once search block configuration.'); return ''; } diff --git a/search/documents/physical_ppt.php b/search/documents/physical_ppt.php index 2de768ffaf..840d7dff26 100644 --- a/search/documents/physical_ppt.php +++ b/search/documents/physical_ppt.php @@ -31,15 +31,19 @@ * @param object $resource * @uses CFG, USER */ -function get_text_for_indexing_ppt(&$resource){ +function get_text_for_indexing_ppt(&$resource, $directfile = ''){ global $CFG, $USER; $indextext = null; // 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; - $text = implode('', file("{$CFG->dataroot}/{$resource->course}/{$resource->reference}")); + if ($directfile == ''){ + $text = implode('', file("{$CFG->dataroot}/{$resource->course}/{$resource->reference}")); + } else { + $text = implode('', file("{$CFG->dataroot}/{$directfile}")); + } $remains = $text; $fragments = array(); diff --git a/search/documents/physical_swf.php b/search/documents/physical_swf.php new file mode 100644 index 0000000000..b6d33445a0 --- /dev/null +++ b/search/documents/physical_swf.php @@ -0,0 +1,69 @@ + 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 ''; + } +} +?> \ No newline at end of file diff --git a/search/documents/physical_txt.php b/search/documents/physical_txt.php index b20be4152f..4917f032b6 100644 --- a/search/documents/physical_txt.php +++ b/search/documents/physical_txt.php @@ -17,14 +17,19 @@ * @param object $resource * @uses CFG, USER */ -function get_text_for_indexing_txt(&$resource){ +function get_text_for_indexing_txt(&$resource, $directfile = ''){ global $CFG, $USER; // 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 - $text = implode('', file("{$CFG->dataroot}/{$resource->course}/{$resource->reference}")); + if ($directfile == ''){ + $text = implode('', file("{$CFG->dataroot}/{$resource->course}/{$resource->reference}")); + } else { + $text = implode('', file("{$CFG->dataroot}/{$directfile}")); + } + if (!empty($CFG->block_search_limit_index_body)){ $text = shorten($text, $CFG->block_search_limit_index_body); } diff --git a/search/documents/physical_xml.php b/search/documents/physical_xml.php index 85f9ba8f72..68e39c5c2b 100644 --- a/search/documents/physical_xml.php +++ b/search/documents/physical_xml.php @@ -21,7 +21,7 @@ function get_text_for_indexing_xml(&$resource, $directfile = ''){ global $CFG, $USER; // 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 if ($directfile == ''){ diff --git a/search/documents/user_document.php b/search/documents/user_document.php index 668f87c202..e85c2c4a5c 100644 --- a/search/documents/user_document.php +++ b/search/documents/user_document.php @@ -334,7 +334,7 @@ function user_check_text_access($path, $itemtype, $this_id, $user, $group_id, $c $userrecord = get_record('user', 'id', $this_id); // 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 "; return false; } diff --git a/search/indexer.php b/search/indexer.php index a48d505ab4..95443167c2 100644 --- a/search/indexer.php +++ b/search/indexer.php @@ -35,6 +35,10 @@ */ require_once('../config.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 @@ -44,7 +48,7 @@ require_once("$CFG->dirroot/search/lib.php"); 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"); } @@ -59,6 +63,13 @@ require_once("$CFG->dirroot/search/lib.php"); 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 //require_once("$CFG->dirroot/search/Zend/Search/Lucene.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."); if (!mkdir($index_path)) { search_pexit("Error creating data directory at: $index_path. Please correct."); - } else { + } + else { mtrace("Directory successfully created."); } - } else { + } + else { 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); /* @@ -116,31 +130,37 @@ require_once("$CFG->dirroot/search/lib.php"); // * mod_iterator // * mod_get_content_for_index //are the sole basis for including a module in the index at the moment. - $searchables = array(); - -/// collects modules - if ($mods = get_records('modules', '', '', '', 'id,name')) { - $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 = search_collect_searchables(); + +/// start indexation - $searchables = array_merge($searchables, search_get_additional_modules()); if ($searchables){ foreach ($searchables as $mod) { - $class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php'; + + $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'; + } 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)) { include_once($class_file); @@ -187,6 +207,8 @@ require_once("$CFG->dirroot/search/lib.php"); mtrace("-- $counter documents indexed"); mtrace("done.\n"); } + } else { + mtrace ("No search document found for plugin {$mod->name}. Ignoring."); } } } diff --git a/search/indexersplash.php b/search/indexersplash.php index bd8b9208f3..01f1238c4a 100644 --- a/search/indexersplash.php +++ b/search/indexersplash.php @@ -18,7 +18,11 @@ * includes and requires */ 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 @@ -28,10 +32,18 @@ require_once("$CFG->dirroot/search/lib.php"); 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"); } +/// 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"); $indexinfo = new IndexInfo(); diff --git a/search/query.php b/search/query.php index 38ae1b76f1..b9024166e7 100644 --- a/search/query.php +++ b/search/query.php @@ -50,90 +50,102 @@ /// check for php5, but don't die yet (see line 52) - require_once("{$CFG->dirroot}/search/querylib.php"); - - $page_number = optional_param('page', -1, PARAM_INT); - $pages = ($page_number == -1) ? false : true; - $advanced = (optional_param('a', '0', PARAM_INT) == '1') ? true : false; - $query_string = optional_param('query_string', '', PARAM_CLEAN); - - if ($pages && isset($_SESSION['search_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']); - } else if ($advanced) { - // otherwise we are dealing with a new advanced query - unset($_SESSION['search_advanced_query']); - session_unregister('search_advanced_query'); - - // chars to strip from strings (whitespace) - $chars = " \t\n\r\0\x0B,-+"; - - // retrieve advanced query variables - $adv->mustappear = trim(optional_param('mustappear', '', PARAM_CLEAN), $chars); - $adv->notappear = trim(optional_param('notappear', '', PARAM_CLEAN), $chars); - $adv->canappear = trim(optional_param('canappear', '', PARAM_CLEAN), $chars); - $adv->module = optional_param('module', '', PARAM_CLEAN); - $adv->title = trim(optional_param('title', '', PARAM_CLEAN), $chars); - $adv->author = trim(optional_param('author', '', PARAM_CLEAN), $chars); - } - - if ($advanced) { - //parse the advanced variables into a query string - //TODO: move out to external query class (QueryParse?) - - $query_string = ''; - - // get all available module types - $module_types = array_merge(array('all'), array_values(search_get_document_types())); - $adv->module = in_array($adv->module, $module_types) ? $adv->module : 'all'; - - // convert '1 2' into '+1 +2' for required words field - if (strlen(trim($adv->mustappear)) > 0) { - $query_string = ' +'.implode(' +', preg_split("/[\s,;]+/", $adv->mustappear)); - } - - // convert '1 2' into '-1 -2' for not wanted words field - if (strlen(trim($adv->notappear)) > 0) { - $query_string .= ' -'.implode(' -', preg_split("/[\s,;]+/", $adv->notappear)); - } - - // this field is left untouched, apart from whitespace being stripped - if (strlen(trim($adv->canappear)) > 0) { - $query_string .= ' '.implode(' ', preg_split("/[\s,;]+/", $adv->canappear)); - } + if ($check = search_check_php5()) { + require_once("{$CFG->dirroot}/search/querylib.php"); + + $page_number = optional_param('page', -1, PARAM_INT); + $pages = ($page_number == -1) ? false : true; + $advanced = (optional_param('a', '0', PARAM_INT) == '1') ? true : false; + $query_string = optional_param('query_string', '', PARAM_CLEAN); - // add module restriction - $doctypestr = get_string('doctype', 'search'); - $titlestr = get_string('title', 'search'); - $authorstr = get_string('author', 'search'); - if ($adv->module != 'all') { - $query_string .= " +{$doctypestr}:".$adv->module; - } + /** + * discard harmfull searches + */ + if (preg_match("/^[\*\?]+$/", $query_string)){ + $query_string = ''; + $error = get_string('fullwildcardquery','search'); + } - // create title search string - if (strlen(trim($adv->title)) > 0) { - $query_string .= " +{$titlestr}:".implode(" +{$titlestr}:", preg_split("/[\s,;]+/", $adv->title)); + + if ($pages && isset($_SESSION['search_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']); + } elseif ($advanced) { + // otherwise we are dealing with a new advanced query + unset($_SESSION['search_advanced_query']); + session_unregister('search_advanced_query'); + + // chars to strip from strings (whitespace) + $chars = " \t\n\r\0\x0B,-+"; + + // retrieve advanced query variables + $adv->mustappear = trim(optional_param('mustappear', '', PARAM_CLEAN), $chars); + $adv->notappear = trim(optional_param('notappear', '', PARAM_CLEAN), $chars); + $adv->canappear = trim(optional_param('canappear', '', PARAM_CLEAN), $chars); + $adv->module = optional_param('module', '', PARAM_CLEAN); + $adv->title = trim(optional_param('title', '', PARAM_CLEAN), $chars); + $adv->author = trim(optional_param('author', '', PARAM_CLEAN), $chars); } - - // create author search string - if (strlen(trim($adv->author)) > 0) { - $query_string .= " +{$authorstr}:".implode(" +{$authorstr}:", preg_split("/[\s,;]+/", $adv->author)); + + if ($advanced) { + //parse the advanced variables into a query string + //TODO: move out to external query class (QueryParse?) + + $query_string = ''; + + // get all available module types + $module_types = array_merge(array('all'), array_values(search_get_document_types())); + $adv->module = in_array($adv->module, $module_types) ? $adv->module : 'all'; + + // convert '1 2' into '+1 +2' for required words field + if (strlen(trim($adv->mustappear)) > 0) { + $query_string = ' +'.implode(' +', preg_split("/[\s,;]+/", $adv->mustappear)); + } + + // convert '1 2' into '-1 -2' for not wanted words field + if (strlen(trim($adv->notappear)) > 0) { + $query_string .= ' -'.implode(' -', preg_split("/[\s,;]+/", $adv->notappear)); + } + + // this field is left untouched, apart from whitespace being stripped + if (strlen(trim($adv->canappear)) > 0) { + $query_string .= ' '.implode(' ', preg_split("/[\s,;]+/", $adv->canappear)); + } + + // add module restriction + $doctypestr = get_string('doctype', 'search'); + $titlestr = get_string('title', 'search'); + $authorstr = get_string('author', 'search'); + if ($adv->module != 'all') { + $query_string .= " +{$doctypestr}:".$adv->module; + } + + // create title search string + if (strlen(trim($adv->title)) > 0) { + $query_string .= " +{$titlestr}:".implode(" +{$titlestr}:", preg_split("/[\s,;]+/", $adv->title)); + } + + // create author search string + if (strlen(trim($adv->author)) > 0) { + $query_string .= " +{$authorstr}:".implode(" +{$authorstr}:", preg_split("/[\s,;]+/", $adv->author)); + } + + // save our options if the query is valid + if (!empty($query_string)) { + $_SESSION['search_advanced_query'] = serialize($adv); + } } - - // save our options if the query is valid - if (!empty($query_string)) { - $_SESSION['search_advanced_query'] = serialize($adv); + + // normalise page number + if ($page_number < 1) { + $page_number = 1; } + + //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); } - - // normalise page number - if ($page_number < 1) { - $page_number = 1; - } - - //run the query against the index - $sq = new SearchQuery($query_string, $page_number, 10, false); - + if (!$site = get_site()) { redirect("index.php"); } @@ -153,6 +165,17 @@ print_header("$strsearch", "$site->fullname" , $navigation, "", "", true, " ", 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_heading($strquery); @@ -168,7 +191,6 @@ } } ?> -
| @@ -267,7 +290,8 @@ if ($sq->is_valid_index()) { //use cached variable to show up-to-date index size (takes deletions into account) print $CFG->search_index_size; - } else { + } + else { print "0"; } @@ -275,7 +299,7 @@ print_string('documents', 'search'); 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 '

' . get_string('noindexmessage', 'search') . '' . get_string('createanindex', 'search')."

\n"; } diff --git a/search/querylib.php b/search/querylib.php index b6cf820ac7..3683628f50 100644 --- a/search/querylib.php +++ b/search/querylib.php @@ -377,7 +377,7 @@ class SearchQuery { * course related checks */ // admins can see everything, anyway. - if (isadmin()){ + if (has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))){ return true; } diff --git a/search/stats.php b/search/stats.php index 58edf11607..651780f3a7 100644 --- a/search/stats.php +++ b/search/stats.php @@ -30,9 +30,13 @@ require_once("{$CFG->dirroot}/search/lib.php"); error(get_string('globalsearchdisabled', 'search')); } - require_once("{$CFG->dirroot}/search/indexlib.php"); - - $indexinfo = new IndexInfo(); +/// check for php5, but don't die yet + + if ($check = search_check_php5()) { + require_once("{$CFG->dirroot}/search/indexlib.php"); + + $indexinfo = new IndexInfo(); + } if (!$site = get_site()) { redirect("index.php"); @@ -54,6 +58,12 @@ require_once("{$CFG->dirroot}/search/lib.php"); /// 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_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 - if (isadmin()) { + if (has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) { $datadirectorystr = get_string('datadirectory', 'search'); $inindexdirectorystr = get_string('filesinindexdirectory', 'search'); $totalsizestr = get_string('totalsize', 'search'); @@ -137,7 +147,7 @@ require_once("{$CFG->dirroot}/search/lib.php"); /// 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) $table->data[] = array($documentsinindexstr, $indexinfo->indexcount); diff --git a/search/tests/index.php b/search/tests/index.php index 19b36915e5..1ba5e14544 100644 --- a/search/tests/index.php +++ b/search/tests/index.php @@ -1,99 +1,147 @@ dirroot/search/lib.php"); + require_once('../../config.php'); + require_once("$CFG->dirroot/search/lib.php"); -require_login(); - -$strsearch = get_string('search', 'search'); -$strquery = get_string('stats'); - -$navlinks[] = array('name' => $strsearch, 'link' => "../index.php", 'type' => 'misc'); -$navlinks[] = array('name' => $strquery, 'link' => "../stats.php", 'type' => 'misc'); -$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, " ", navmenu($site)); - -if (empty($CFG->enableglobalsearch)) { - print_error('Global searching is not enabled.'); -} - -if (!isadmin()) { - print_error("You need to be an admin user to use this page.", '', "$CFG->wwwroot/login/index.php"); -} //if - -mtrace('
Server Time: '.date('r',time()));
-mtrace("Testing global search capabilities:\n");
-
-$phpversion = phpversion();
+/// 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'));
 
-//fix paths for testing
-set_include_path(get_include_path().":../");
-require_once("$CFG->dirroot/search/Zend/Search/Lucene.php");
-
-mtrace("Checking activity modules:\n");
-
-//the presence of the required search functions -
-// * mod_iterator
-// * mod_get_content_for_index
-//are the sole basis for including a module in the index at the moment.
-
-if ($mods = get_records_select('modules')) {
-    $mods = array_merge($mods, search_get_additional_modules());
+    require_login();
+    
+    if (empty($CFG->enableglobalsearch)) {
+      error('Global searching is not enabled.');
+    }
+    
+    if (!has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) {
+      error("You need to be an admin user to use this page.", "$CFG->wwwroot/login/index.php");
+    } //if
+    
+    mtrace('
Server Time: '.date('r',time()));
+    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
+    set_include_path(get_include_path().":../");
+    require_once("$CFG->dirroot/search/Zend/Search/Lucene.php");
+    
+    mtrace("Checking activity modules:\n");
+    
+    //the presence of the required search functions -
+    // * mod_iterator
+    // * mod_get_content_for_index
+    //are the sole basis for including a module in the index at the moment.
+    
+/// get all installed modules
+    if ($mods = get_records('modules', '', '', 'name', 'id,name')){
 
-    foreach ($mods as $mod) {
-        $class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
+        $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';
+        } else {
+            $class_file = $CFG->dirroot.'/'.$mod->location.'/'.$mod->name.'/search_document.php';
+        }
+    
         if (file_exists($class_file)) {
             include_once($class_file);
-
+    
             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;
-            } //if
-
+            }
+            
             $iter_function = $mod->name.'_iterator';
             $index_function = $mod->name.'_get_content_for_index';
-
+            
             if (function_exists($index_function) && function_exists($iter_function)) {
                 if (is_array($iter_function())) {
                     $documents = $index_function(array_pop($iter_function()));
-
+            
                     if (is_array($documents)) {
                         mtrace("Success: '$mod->name' module seems to be ready for indexing.");
                     } else {
                         mtrace("ERROR: $index_function() doesn't seem to be returning an array.");
-                    } //else
+                    } 
                 } else {
                     mtrace("ERROR: $iter_function() doesn't seem to be returning an object array.");
-                } //else
+                } 
             } else {
                 mtrace("ERROR: $iter_function() and/or $index_function() does not exist in $class_file");
-            } //else
+            } 
         } else {
             mtrace("Notice: $class_file does not exist, this module will not be indexed.");
-        } //else
-    } //foreach
-} //if
-
-//finished modules
-mtrace("\nFinished checking activity modules.");
-
-//now blocks...
-//
-
-mtrace("
Back to query page or Start indexing."); -mtrace('
'); -print_footer(); -?> + } + } + + //finished modules + mtrace("\nFinished checking activity modules."); + + //now blocks... + // + + mtrace("
Back to query page or Start indexing."); + mtrace('
'); +?> \ No newline at end of file diff --git a/search/update.php b/search/update.php index 1592f8c02b..10a88241eb 100644 --- a/search/update.php +++ b/search/update.php @@ -34,7 +34,7 @@ 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"); }