Skip to content

Commit

Permalink
MDL-15352: first version of remote moodle plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
jerome committed Oct 22, 2008
1 parent 422639d commit d3dfe9a
Show file tree
Hide file tree
Showing 4 changed files with 495 additions and 4 deletions.
2 changes: 1 addition & 1 deletion mnet/xmlrpc/client.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ function permission_to_call($mnet_peer) {
global $DB, $CFG;

// Executing any system method is permitted.
$system_methods = array('system/listMethods', 'system/methodSignature', 'system/methodHelp', 'system/listServices');
$system_methods = array('system/listMethods', 'system/methodSignature', 'system/methodHelp', 'system/listServices', 'system/listFiles', 'system/retrieveFile');
if (in_array($this->method, $system_methods) ) {
return true;
}
Expand Down
174 changes: 171 additions & 3 deletions mnet/xmlrpc/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,12 @@ function mnet_server_dispatch($payload) {
xmlrpc_server_register_method($xmlrpcserver, 'system.listServices', 'mnet_system');
xmlrpc_server_register_method($xmlrpcserver, 'system/listServices', 'mnet_system');

xmlrpc_server_register_method($xmlrpcserver, 'system.listFiles', 'mnet_system');
xmlrpc_server_register_method($xmlrpcserver, 'system/listFiles', 'mnet_system');

xmlrpc_server_register_method($xmlrpcserver, 'system.retrieveFile', 'mnet_system');
xmlrpc_server_register_method($xmlrpcserver, 'system/retrieveFile', 'mnet_system');

xmlrpc_server_register_method($xmlrpcserver, 'system.keyswap', 'mnet_keyswap');
xmlrpc_server_register_method($xmlrpcserver, 'system/keyswap', 'mnet_keyswap');

Expand All @@ -424,9 +430,13 @@ function mnet_server_dispatch($payload) {
$method == 'system/methodSignature' ||
$method == 'system.methodHelp' ||
$method == 'system/methodHelp' ||
$method == 'system.listServices' ||
$method == 'system/listServices' ||
$method == 'system.keyswap' ||
$method == 'system.listServices' ||
$method == 'system/listServices' ||
$method == 'system.listFiles' ||
$method == 'system/listFiles' ||
$method == 'system.retrieveFile' ||
$method == 'system/retrieveFile' ||
$method == 'system.keyswap' ||
$method == 'system/keyswap') {

$response = xmlrpc_server_call_method($xmlrpcserver, $payload, $MNET_REMOTE_CLIENT, array("encoding" => "utf-8"));
Expand Down Expand Up @@ -688,10 +698,168 @@ function mnet_system($method, $params, $hostinfo) {
}

return $services;
} elseif ('system/retrieveFile' == $method) {
global $DB, $USER;

$USER = $DB->get_record('user',array('username' => $params[0], 'mnethostid' => $hostinfo->id));
$pathnamehash = $params[1];
$fs = get_file_storage();

$sf = $fs->get_file_by_hash($pathnamehash);

$contents = base64_encode($sf->get_content());

return array($contents, $sf->get_filename());
} elseif ('system/listFiles' == $method) {

global $DB, $USER;

$USER = $DB->get_record('user',array('username' => $params[0], 'mnethostid' => $hostinfo->id));

$ret = array();
$search = '';
// no login required
$ret['nologin'] = true;
// todo: link to file manager
$ret['manage'] = $CFG->wwwroot .'/files/index.php'; // temporary

$browser = get_file_browser();
$itemid = null;
$filename = null;
$filearea = null;
$path = '/';
$ret['dynload'] = false;

if ($fileinfo = $browser->get_file_info(get_system_context(), $filearea, $itemid, $path, $filename)) {

$ret['path'] = array();
$params = $fileinfo->get_params();
$filearea = $params['filearea'];
//todo: fix this call, and similar ones here and in build_tree - encoding path works only for real folders
$ret['path'][] = _encode_path($filearea, $path, $fileinfo->get_visible_name());
if ($fileinfo->is_directory()) {
$level = $fileinfo->get_parent();
while ($level) {
$params = $level->get_params();
$ret['path'][] = _encode_path($params['filearea'], $params['filepath'], $level->get_visible_name());
$level = $level->get_parent();
}
}
$filecount = build_tree($fileinfo, $search, $ret['dynload'], $ret['list']);
$ret['path'] = array_reverse($ret['path']);
} else {
// throw some "context/filearea/item/path/file not found" exception?
}

if (empty($ret['list'])) {
throw new repository_exception('emptyfilelist', 'repository_local');
} else {
return $ret;
}
}
exit(mnet_server_fault(7019, 'nosuchfunction'));
}

/**
*
* @param <type> $filearea
* @param <type> $path
* @param <type> $visiblename
* @return <type>
*/
function _encode_path($filearea, $path, $visiblename) {
return array('path'=>serialize(array($filearea, $path)), 'name'=>$visiblename);
}

/**
* Builds a tree of files, to be used by get_listing(). This function is
* then called recursively.
*
* @param $fileinfo an object returned by file_browser::get_file_info()
* @param $search searched string
* @param $dynamicmode bool no recursive call is done when in dynamic mode
* @param $list - the array containing the files under the passed $fileinfo
* @returns int the number of files found
*
* todo: take $search into account, and respect a threshold for dynamic loading
*/
function build_tree($fileinfo, $search, $dynamicmode, &$list) {
global $CFG;

$filecount = 0;
$children = $fileinfo->get_children();

foreach ($children as $child) {
$filename = $child->get_visible_name();
$filesize = $child->get_filesize();
$filesize = $filesize ? display_size($filesize) : '';
$filedate = $child->get_timemodified();
$filedate = $filedate ? userdate($filedate) : '';
$filetype = $child->get_mimetype();

if ($child->is_directory()) {
$path = array();
$level = $child->get_parent();
while ($level) {
$params = $level->get_params();
$path[] = _encode_path($params['filearea'], $params['filepath'], $level->get_visible_name());
$level = $level->get_parent();
}

$tmp = array(
'title' => $child->get_visible_name(),
'size' => 0,
'date' => $filedate,
'path' => array_reverse($path),
'thumbnail' => $CFG->pixpath .'/f/folder.gif'
);


$_search = $search;
if ($search && stristr($tmp['title'], $search) !== false) {
$_search = false;
}
$tmp['children'] = array();
$_filecount = build_tree($child, $_search, $dynamicmode, $tmp['children']);
if ($search && $_filecount) {
$tmp['expanded'] = 1;
}



if (!$search || $_filecount || (stristr($tmp['title'], $search) !== false)) {
$list[] = $tmp;
$filecount += $_filecount;
}

} else { // not a directory
// skip the file, if we're in search mode and it's not a match
if ($search && (stristr($filename, $search) === false)) {
continue;
}

//retrieve the stored file id
$fs = get_file_storage();
$params = $child->get_params();

$pathnamehash = $fs->get_pathname_hash($params['contextid'], $params['filearea'], $params['itemid'], $params['filepath'], $params['filename']);


$list[] = array(
'title' => $filename,
'size' => $filesize,
'date' => $filedate,
'source' => $pathnamehash,
'thumbnail' => $CFG->pixpath .'/f/'. mimeinfo_from_type("icon", $filetype)
);

$filecount++;
}
}

return $filecount;
}

/**
* Initialize the object (if necessary), execute the method or function, and
* return the response
Expand Down
Binary file added repository/remotemoodle/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit d3dfe9a

Please sign in to comment.