'dmls97d8j3i9tn7av8y71m9eb55vrtj4',
* 'auth_token'=>'', 'path_root'=>'/');
* $repo = new repository_xxx($options);
* // print login page or a link to redirect to another page
* $repo->print_login();
* // call get_listing, and print result
* $repo->print_listing();
* // print a search box
* $repo->print_search();
*
* @version 1.0 dev
* @package repository
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
require_once(dirname(dirname(__FILE__)) . '/config.php');
require_once(dirname(dirname(__FILE__)).'/lib/filelib.php');
abstract class repository {
protected $options;
public $name;
public $context;
public $repositoryid;
public $listing;
/**
* Take an array as a parameter, which contains necessary information
* of repository.
*
* @param string $parent The parent path, this parameter must
* not be the folder name, it may be a identification of folder
* @param string $search The text will be searched.
* @return array the list of files, including meta infomation
*/
public function __construct($repositoryid, $contextid = SITEID, $options = array()){
$this->repositoryid = $repositoryid;
$this->context = get_context_instance_by_id($contextid);
$this->options = array();
if (is_array($options)) {
$options = array_merge(repository_get_option($repositoryid), $options);
foreach ($options as $n => $v) {
$this->options[$n] = $v;
}
}
}
public function __set($name, $value) {
$this->options[$name] = $value;
}
public function __get($name) {
if (array_key_exists($name, $this->options)){
return $this->options[$name];
}
trigger_error('Undefined property: '.$name, E_USER_NOTICE);
return null;
}
public function __isset($name) {
return isset($this->options[$name]);
}
public function __toString() {
return 'Repository class: '.__CLASS__;
}
/**
* Given a URL, get a file from there.
* @param string $url the url of file
* @param string $file save location
*/
public function get_file($url, $file = '') {
global $CFG;
if (!file_exists($CFG->dataroot.'/repository/download')) {
mkdir($CFG->dataroot.'/repository/download/', 0777, true);
}
if(is_dir($CFG->dataroot.'/repository/download')) {
$dir = $CFG->dataroot.'/repository/download/';
}
if(empty($file)) {
$file = uniqid('repo').'_'.time().'.tmp';
}
if(file_exists($dir.$file)){
$file = uniqid('m').$file;
}
$fp = fopen($dir.$file, 'w');
$c = new curl;
$c->download(array(
array('url'=>$url, 'file'=>$fp)
));
return $dir.$file;
}
/**
* Given a path, and perhaps a search, get a list of files.
*
* @param string $parent The parent path, this parameter can
* a folder name, or a identification of folder
* @param string $search The text will be searched.
* @return array the list of files, including meta infomation
*/
abstract public function get_listing($parent = '/', $search = '');
/**
* Print a list or return string
*
* @param string $list
* $list = array(
* array('title'=>'moodle.txt', 'size'=>12, 'source'=>'url', 'date'=>''),
* array('title'=>'repository.txt', 'size'=>32, 'source'=>'', 'date'=>''),
* array('title'=>'forum.txt', 'size'=>82, 'source'=>'', 'date'=>''),
* );
*
* @param boolean $print if printing the listing directly
*
*/
public function print_listing($listing = array(), $print=true) {
if(empty($listing)){
$listing = $this->get_listing();
}
if (empty($listing)) {
$str = '';
} else {
$count = 0;
$str = '
';
}
if ($print){
echo $str;
return null;
} else {
return $str;
}
}
/**
* Show the login screen, if required
* This is an abstract function, it must be overriden.
*
*/
abstract public function print_login();
/**
* Show the search screen, if required
*
* @return null
*/
abstract public function print_search();
/**
* Cache login details for repositories
*
* @param string $username
* @param string $password
* @param string $userid The id of specific user
* @return int Id of the record
*/
public function store_login($username = '', $password = '', $userid = 1) {
global $DB;
$repository = new stdclass;
if (!empty($this->repositoryid)) {
$repository->id = $this->repositoryid;
} else {
$repository->userid = $userid;
$repository->repositorytype = $this->type;
$repository->contextid = $this->context->id;
}
if ($entry = $DB->get_record('repository', $repository)) {
$repository->id = $entry->id;
$repository->username = $username;
$repository->password = $password;
return $DB->update_record('repository', $repository);
} else {
$repository->username = $username;
$repository->password = $password;
return $DB->insert_record('repository', $repository);
}
return false;
}
/**
* Defines operations that happen occasionally on cron
*
*/
public function cron() {
return true;
}
}
/**
* exception class for repository api
*
*/
class repository_exception extends moodle_exception {
}
/**
* Listing object describing a listing of files and directories
*/
abstract class repository_listing {
}
/**
* Save settings for repository instance
*
* @param int repository Id
* @param int from 1 to 5
* @param array settings
* @return int Id of the record
*/
function repository_set_option($id, $position, $config = array()){
global $DB;
$repository = new stdclass;
$position = (int)$position;
$config = serialize($config);
if( $position < 1 || $position > 5){
print_error('invalidoption', 'repository', '', $position);
}
if ($entry = $DB->get_record('repository', array('id'=>$id))) {
$option = 'data'.$position;
$repository->id = $entry->id;
$repository->$option = $config;
return $DB->update_record('repository', $repository);
}
return false;
}
/**
* Get settings for repository instance
*
* @param int repository Id
* @return array Settings
*/
function repository_get_option($id){
global $DB;
$entry = $DB->get_record('repository', array('id'=>$id));
if (!empty($entry->visible)) {
$ret['visible'] = 1;
} else {
$ret['visible'] = 0;
}
for ($i=1;$i<6;$i++) {
$field = 'data'.$i;
$data = unserialize($entry->$field);
if (!empty($data)) {
if (is_array($data)) {
$ret = array_merge($ret, $data);
}
}
}
return $ret;
}
/**
* Get user's repositories
*
* @param object context object
* @return array repository list
*/
function repository_user_instances($context){
global $DB, $CFG, $USER;
$params = array();
$sql = 'SELECT * FROM {repository} r WHERE ';
$sql .= ' (r.userid = 0 or r.userid = ?) ';
$params[] = $USER->id;
if($context->id == SITEID) {
$sql .= 'AND (r.contextid = ?)';
$params[] = SITEID;
} else {
$sql .= 'AND (r.contextid = ? or r.contextid = ?)';
$params[] = SITEID;
$params[] = $context->id;
}
if(!$repos = $DB->get_records_sql($sql, $params)) {
$repos = array();
}
return $repos;
}
/**
* Get single repository instance
*
* @param int repository id
* @return object repository instance
*/
function repository_instance($id){
global $DB, $CFG;
if (!$instance = $DB->get_record('repository', array('id' => $id))) {
return false;
}
require_once($CFG->dirroot . '/repository/'. $instance->repositorytype
. '/repository.class.php');
$classname = 'repository_' . $instance->repositorytype;
return new $classname($instance->id, $instance->contextid);
}
/**
* Get list of repository plugin
*
* @return array repository plugin list
*/
function repository_get_plugins(){
global $CFG;
$repo = $CFG->dirroot.'/repository/';
$ret = array();
if($dir = opendir($repo)){
while (false !== ($file = readdir($dir))) {
if(is_dir($file) && $file != '.' && $file != '..'
&& file_exists($repo.$file.'/repository.class.php')){
require_once($repo.$file.'/version.php');
$ret[] = array('name'=>$plugin->name,
'version'=>$plugin->version,
'path'=>$repo.$file,
'settings'=>file_exists($repo.$file.'/settings.php'));
}
}
}
return $ret;
}
/**
* Move file from download folder to file pool using FILE API
*
* @param string file path in download folder
* @param string file name
* @param int itemid to identify a file in filepool
* @return array information of file in file pool
*/
function move_to_filepool($path, $name, $itemid) {
global $DB, $CFG, $USER;
$context = get_context_instance(CONTEXT_USER, $USER->id);
$entry = new object();
$entry->filearea = 'user_draft';
$entry->contextid = $context->id;
$entry->filename = $name;
$entry->filepath = '/';
$entry->timecreated = time();
$entry->timemodified = time();
if(is_numeric($itemid)) {
$entry->itemid = $itemid;
} else {
$entry->itemid = 0;
}
$entry->mimetype = mimeinfo('type', $path);
$entry->userid = $USER->id;
$fs = get_file_storage();
$browser = get_file_browser();
if ($file = $fs->create_file_from_pathname($entry, $path)) {
$ret = $browser->get_file_info($context, $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename());
return array('url'=>$ret->get_url(),'id'=>$file->get_itemid());
} else {
return null;
}
}
/**
* Return javascript to create file picker to browse repositories
*
* @param object context
* @return array
*/
function get_repository_client($context){
global $CFG, $USER;
$suffix = uniqid();
$strsubmit = get_string('submit', 'repository');
$strlistview = get_string('listview', 'repository');
$strthumbview = get_string('thumbview', 'repository');
$strsearch = get_string('search', 'repository');
$strlogout = get_string('logout', 'repository');
$strloading = get_string('loading', 'repository');
$strtitle = get_string('title', 'repository');
$filename = get_string('filename', 'repository');
$strsync = get_string('sync', 'repository');
$strdownload = get_string('download', 'repository');
$strback = get_string('back', 'repository');
$strclose = get_string('close', 'repository');
$strdownload = get_string('downloadsucc', 'repository');
$strnoenter = get_string('noenter', 'repository');
$strsearching = get_string('searching', 'repository');
$stradd = get_string('add', 'repository');
$js = <<
#list-$suffix{line-height: 1.5em}
#list-$suffix a{ padding: 3px }
#list-$suffix li a:hover{ background: gray; color:white; }
#repo-list-$suffix .repo-name{}
#repo-list-$suffix li{margin-bottom: 1em}
#paging-$suffix{margin:10px 5px; clear:both}
#paging-$suffix a{padding: 4px; border: 1px solid gray}
#panel-$suffix{padding:0;margin:0; text-align:left;}
.file_name{color:green;}
.file_date{color:blue}
.file_size{color:gray}
.grid{width:80px; float:left;text-align:center;}
.grid img{display:block}
.grid div{width: 80px; height: 36px; overflow: hidden}
.repo-opt{font-size: 10px;}
EOD;
$html = <<
EOD;
return array('html'=>$html, 'js'=>$js, 'suffix'=>$suffix);
}