]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-14978 removed obsoleted isadmin()
authorskodak <skodak>
Sun, 25 May 2008 10:08:05 +0000 (10:08 +0000)
committerskodak <skodak>
Sun, 25 May 2008 10:08:05 +0000 (10:08 +0000)
23 files changed:
index.php
lib/accesslib.php
lib/deprecatedlib.php
mod/feedback/lib.php
mod/feedback/show_entries.php
search/add.php
search/delete.php
search/documents/forum_document.php
search/documents/physical_doc.php
search/documents/physical_htm.php
search/documents/physical_pdf.php
search/documents/physical_ppt.php
search/documents/physical_swf.php [new file with mode: 0644]
search/documents/physical_txt.php
search/documents/physical_xml.php
search/documents/user_document.php
search/indexer.php
search/indexersplash.php
search/query.php
search/querylib.php
search/stats.php
search/tests/index.php
search/update.php

index 6a66bb1e2ca6314502d224d62a71fc0e2c39cef8..93644e2e5653e1e5d6c7acf86fb2cbbc993b0e71 100644 (file)
--- 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');
         }
     }
index 87c56ff2f4d3bd73a9d6fd7a543cda5278677870..427b67874b4fcd13c856c44f4c68172a9e42dd8c 100755 (executable)
@@ -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) {
index aa35cebd9fac46817d0091e71cbaffdc5046e2a9..28f987748a0cac8b4acf13a8525e5a9e05c366f9 100644 (file)
  * @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)
  *
index 1b1999c4919e3af664af3791ab8c0149f1981637..8f61d0daeb34ee7728eb8044efdb9482587847a9 100644 (file)
@@ -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
         }
index d6ee4cd3962d4b8b69649320828d291b929d04f7..62e91bf22f949e2ad4b548e211cd1157e709863f 100644 (file)
             //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{
index 69e3e80c580d00eea5343e9bf68895731a688574..be33ab3494a3890b3fcea3eb1400498ae31cbfd3 100644 (file)
@@ -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");
     } 
     
index 2e844d4aa9c7ccb61c407ae2ff02972450ad6f9f..c269f6661049dca02a28fc111122be86390a6227 100644 (file)
@@ -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
     
index 3cbe2e108a3476dc4375fd6864ee9dac7041a416..a26afd4a4ced146ad98fbfe4fc2924420e38fe2b 100644 (file)
@@ -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
index ca053bde51d6eb4c41353f90f64398e19401b91c..9586ff12cc55e51ff3575ea794377b30286efc5e 100644 (file)
 * @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 '';
     }
index 76d6073dd6901bba5b934b95814894343a38ab96..aa3e6bc0f11245e8c9e91589fea60b044c95e7f7 100644 (file)
 * @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('/(.*)<meta ([^>]*)>(.*)/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
index 291e0f4a05c684224b1f4e9c03a83a679afcd950..347b61f975e44bc251d13d47e6a8529d3b10a44e 100644 (file)
 * @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 '';
     }
index 2de768ffaf872df36371c10807ff9b49def29eb9..840d7dff266a7ef17565b9bda58e9ec9f123e3ea 100644 (file)
 * @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 (file)
index 0000000..b6d3344
--- /dev/null
@@ -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 '';
+    }
+}
+?>
\ No newline at end of file
index b20be4152f0858e6353f0a6c37351e34a184ca6d..4917f032b6742bfe5f2b3cfb32fd93d024e193ba 100644 (file)
 * @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);
     }
index 85f9ba8f721ec8528806d99740aafc2bea67047f..68e39c5c2bb841a40379a242d628adf1eb062f09 100644 (file)
@@ -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 == ''){
index 668f87c2022ec68487e4f0d4b7d9ac95bdc9ed8f..e85c2c4a5c328d6c1b453d05bbd1a52aa46a322b 100644 (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);
 
         // 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;
         }
index a48d505ab4eeda4b4674e66cb23f0642c153580b..95443167c2a3446451facc8fdf57e7cf596174c0 100644 (file)
 */
 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.");
             }
         }
     }
index bd8b9208f323aea1193cc8fd083fa704210a79ab..01f1238c4a475f854e503d4e063ce752d6bde4bf 100644 (file)
 * 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();
     
index 38ae1b76f14c3f75d11104a85029a4b1e6d2e28b..b9024166e7925eb8f26999358f835e24e1364615 100644 (file)
     
 /// 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");
     } 
         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_heading($strquery);
     
         } 
     }
     ?>
-    
     <form id="query" method="get" action="query.php">
     <?php 
     if (!$advanced) { 
         <a href="query.php?a=1"><?php print_string('advancedsearch', 'search') ?></a> |
         <a href="stats.php"><?php print_string('statistics', 'search') ?></a>
     <?php 
-    } else {
+    } 
+    else {
         print_box_start();
       ?>
         <input type="hidden" name="a" value="<?php print $advanced; ?>"/>
     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";
     } 
     
     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 '<p>' . get_string('noindexmessage', 'search') . '<a href="indexersplash.php">' . get_string('createanindex', 'search')."</a></p>\n";
     } 
     
index b6cf820ac76dd0849449269e303e1a01a5faf809..3683628f50e0681aa954b512cdab0482b239a2c3 100644 (file)
@@ -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;
       }
             
index 58edf1160799fffd0fec3f0d0304220a56ff4f33..651780f3a7fef643bacc0b945f9e9f2429d77470 100644 (file)
@@ -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);
         
index 19b36915e5bac649a9654600cb337f10d6aa5193..1ba5e14544c6850a4e73139cd3f5b01fa0246b49 100644 (file)
 <?php
 
-  /* Used to test if modules/blocks are ready to included in the search index.
-   * 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
-   * directory.
-   */
+    /* Used to test if modules/blocks are ready to included in the search index.
+    * 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
+    * directory.
+    **/
 
-@set_time_limit(0);
-@ob_implicit_flush(true);
-@ob_end_flush();
+    @set_time_limit(0);
+    @ob_implicit_flush(true);
+    @ob_end_flush();
 
-require_once('../../config.php');
-require_once("$CFG->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, "&nbsp;", 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('<pre>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('<pre>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("<br/><a href='../index.php'>Back to query page</a> or <a href='../indexersplash.php'>Start indexing</a>.");
-mtrace('</pre>');
-print_footer();
-?>
+        } 
+    } 
+    
+    //finished modules
+    mtrace("\nFinished checking activity modules.");
+    
+    //now blocks...
+    //
+    
+    mtrace("<br/><a href='../index.php'>Back to query page</a> or <a href='../indexersplash.php'>Start indexing</a>.");
+    mtrace('</pre>');
+?>
\ No newline at end of file
index 1592f8c02b408e06f0bedda1299122eb263180bd..10a88241eb0e9d1a417128dd20255487288383d4 100644 (file)
@@ -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");
     }