Proposal for an additional core standard function : load_library

Proposal for an additional core standard function : load_library

Valery Fremaux
Vastuste arv 2
Hi all,

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".
This allows proposing very contextual libraries, that might be moved to more central place, up to core libs, without breacking any previous implementation.

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);
}
}
Keskmine hinnang: -
Vastuses Valery Fremaux

Re: Proposal for an additional core standard function : load_library

Tim Hunt
Core developers pilt Documentation writers pilt Particularly helpful Moodlers pilt Peer reviewers pilt Plugin developers pilt
I don't see the point of this. Just do

require_once($CFG->dirroot . '/mod/mymod/customlib.php');

or whatever for now.

If we ever need to move it, it will later be very easy to do a fulltext search-and-replace to change that line to the new location.
Vastuses Tim Hunt

Re: Proposal for an additional core standard function : load_library

Valery Fremaux
Hi Tim, this was pointed out as many of my custom modules share a set of generic libs such as filesystemlib.php for having abstract high level access to filesystem (that would now obsolete in 2.0 !) , mailtemplatelib.php for simple notification message contruction, and some other.

In such an intermediary situation, (not unitary development, but still not core shared), the solution might be usefull to many customizers...

No problem anyway... silmapilgutus