falling on this heavy issue to manage new generic, but non core libraries, and willing to foresee eventual propagation of those libraries to more contral locations, I searched for a "strategized" function for locating and including a library, and didn't find anyone (1.9, not seeked in 2.0) :
Result is a new function that could locate and include a lib, according to a successive guessing strategy :
- The general $CFG->libdir overrides all other locations (core central lib)
- The local is seeked for the lib
- The local/lib location is examined
- The local cascade (local, local/local etc.) is seeked while there is some, each time asking second time for embedded "very local" lib subdir.
- Finally, will the path realtively from the location we are be considered as "last chance".
There might though be some security considerations that would need some debate. I didn't performed any security focussed brainstorm on it... welcome to volounteers !!
/**
* quite a standard function : allows loading a library from several locations in Moodle
* scans for general libs, local and local cascade locations, and local and cascade "lib" sublocation
* finally, and even if library has backdirs segments (..), tests from actual location.
*
* @param string $library the library filename
* @uses $CFG
*/
function load_library($library){
global $CFG;
if (strstr("..", $library) === false){
if (file_exists($CFG->libdir.'/'.$library)){
include_once($CFG->libdir.'/'.$library);
} else {
$path = 'local';
$increment = '/local';
while (is_dir($CFG->dirroot.'/'.$path)){
if (file_exists($CFG->dirroot.'/'.$path.'/'.$library)){
include_once($CFG->dirroot.'/'.$path.'/'.$library);
return;
} elseif (file_exists($CFG->dirroot.'/'.$path.'/lib/'.$library)){
include_once($CFG->dirroot.'/'.$path.'/lib/'.$library);
return;
}
$path .= $increment;
}
}
}
if(file_exists($library)){
include_once($library);
}
}