MDL-22755, create filesystem plugin needs moodle/site:config capability, and a few coding style fixing

This commit is contained in:
Dongsheng Cai 2010-06-22 07:40:30 +00:00
parent 54632b02b2
commit 49d20def30
7 changed files with 142 additions and 121 deletions

View File

@ -1096,43 +1096,43 @@ class phpFlickr {
* @return boolean
*/
function upload ($photo, $title = null, $description = null, $tags = null, $is_public = null, $is_friend = null, $is_family = null) {
function upload ($photo, $title = null, $description = null, $tags = null, $is_public = null, $is_friend = null, $is_family = null) {
global $SESSION;
$args = array("async" => 1, "api_key" => $this->api_key, "title" => $title, "description" => $description, "tags" => $tags, "is_public" => $is_public, "is_friend" => $is_friend, "is_family" => $is_family);
if (!empty($this->email)) {
$args = array_merge($args, array("email" => $this->email));
}
if (!empty($this->password)) {
$args = array_merge($args, array("password" => $this->password));
}
$args = array("async" => 1, "api_key" => $this->api_key, "title" => $title, "description" => $description, "tags" => $tags, "is_public" => $is_public, "is_friend" => $is_friend, "is_family" => $is_family);
if (!empty($this->email)) {
$args = array_merge($args, array("email" => $this->email));
}
if (!empty($this->password)) {
$args = array_merge($args, array("password" => $this->password));
}
// TODO:
// should we request a token if it is not valid?
if (!empty($this->token)) {
$args = array_merge($args, array("auth_token" => $this->token));
}
if (!empty($this->token)) {
$args = array_merge($args, array("auth_token" => $this->token));
}
ksort($args);
$auth_sig = "";
foreach ($args as $key => $data) {
if ($data !== null) {
$auth_sig .= $key . $data;
ksort($args);
$auth_sig = "";
foreach ($args as $key => $data) {
if ($data !== null) {
$auth_sig .= $key . $data;
} else {
unset($args[$key]);
}
}
if (!empty($this->secret)) {
$api_sig = md5($this->secret . $auth_sig);
}
}
if (!empty($this->secret)) {
$api_sig = md5($this->secret . $auth_sig);
$args['api_sig'] = $api_sig;
}
}
$photo = realpath($photo);
$photo = realpath($photo);
$args['photo'] = '@'.$photo;
if ($response = $this->curl->post($this->Upload, $args)) {
if ($response = $this->curl->post($this->Upload, $args)) {
return true;
} else {
} else {
return false;
}
}
}
}
?>

View File

@ -150,30 +150,44 @@ class repository_filesystem extends repository {
return $ret;
}
public function instance_config_form($mform) {
global $CFG;
$path = $CFG->dataroot . '/repository/';
if (!is_dir($path)) {
mkdir($path);
}
if ($handle = opendir($path)) {
$fieldname = get_string('path', 'repository_filesystem');
$choices = array();
while (false !== ($file = readdir($handle))) {
if (is_dir($path.$file) && $file != '.' && $file!= '..') {
$choices[$file] = $file;
$fieldname = '';
global $CFG, $PAGE;
if (has_capability('moodle/site:config', $PAGE->context)) {
$path = $CFG->dataroot . '/repository/';
if (!is_dir($path)) {
mkdir($path);
}
if ($handle = opendir($path)) {
$fieldname = get_string('path', 'repository_filesystem');
$choices = array();
while (false !== ($file = readdir($handle))) {
if (is_dir($path.$file) && $file != '.' && $file!= '..') {
$choices[$file] = $file;
$fieldname = '';
}
}
if (empty($choices)) {
$mform->addElement('static', '', '', get_string('nosubdir', 'repository_filesystem', $path));
} else {
$mform->addElement('select', 'fs_path', $fieldname, $choices);
$mform->addElement('static', null, '', get_string('information','repository_filesystem', $path));
}
closedir($handle);
}
if (empty($choices)) {
$mform->addElement('static', '', '', get_string('nosubdir', 'repository_filesystem', $path));
} else {
$mform->addElement('select', 'fs_path', $fieldname, $choices);
$mform->addElement('static', null, '', get_string('information','repository_filesystem', $path));
}
closedir($handle);
} else {
$mform->addElement('static', null, '', get_string('nopermissions', 'error', get_string('configplugin', 'repository_filesystem')));
return false;
}
}
public function supported_returntypes() {
return FILE_INTERNAL;
}
public static function create($type, $userid, $context, $params, $readonly=0) {
global $PAGE;
if (has_capability('moodle/site:config', $PAGE->context)) {
return parent::create($type, $userid, $context, $params, $readonly);
} else {
require_capability('moodle/site:config', $PAGE->context);
return false;
}
}
}

View File

@ -18,8 +18,8 @@
/**
* repository_flickr_public class
* This one is used to create public repository
* You can set up a public account in admin page, so everyone can
* access photos in this public account
* You can set up a public account in admin page, so everyone can access
* flickr photos from this plugin
*
* @since 2.0
* @package moodlecore
@ -35,6 +35,42 @@ class repository_flickr_public extends repository {
private $flickr;
public $photos;
/**
* constructor method
*
* @global object $CFG
* @global object $SESSION
* @param int $repositoryid
* @param int $context
* @param array $options
* @param boolean $readonly
*/
public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array(), $readonly=0) {
global $CFG, $SESSION;
parent::__construct($repositoryid, $context, $options,$readonly);
$this->api_key = $this->get_option('api_key');
$this->flickr = new phpFlickr($this->api_key);
$this->flickr_account = $this->get_option('email_address');
$account = optional_param('flickr_account', '', PARAM_RAW);
$fulltext = optional_param('flickr_fulltext', '', PARAM_RAW);
if (empty($fulltext)) {
$fulltext = optional_param('s', '', PARAM_RAW);
}
$tag = optional_param('flickr_tag', '', PARAM_RAW);
$license = optional_param('flickr_license', '', PARAM_RAW);
$this->sess_account = 'flickr_public_'.$this->id.'_account';
$this->sess_tag = 'flickr_public_'.$this->id.'_tag';
$this->sess_text = 'flickr_public_'.$this->id.'_text';
if (!empty($account) or !empty($fulltext) or !empty($tag) or !empty($license)) {
$SESSION->{$this->sess_tag} = $tag;
$SESSION->{$this->sess_text} = $fulltext;
$SESSION->{$this->sess_account} = $account;
}
}
/**
* save api_key in config table
* @param array $options
@ -77,43 +113,7 @@ class repository_flickr_public extends repository {
}
/**
* constructor method
*
* @global object $CFG
* @global object $SESSION
* @param int $repositoryid
* @param int $context
* @param array $options
* @param boolean $readonly
*/
public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array(), $readonly=0) {
global $CFG, $SESSION;
parent::__construct($repositoryid, $context, $options,$readonly);
$this->api_key = $this->get_option('api_key');
$this->flickr = new phpFlickr($this->api_key);
$this->flickr_account = $this->get_option('email_address');
$account = optional_param('flickr_account', '', PARAM_RAW);
$fulltext = optional_param('flickr_fulltext', '', PARAM_RAW);
if (empty($fulltext)) {
$fulltext = optional_param('s', '', PARAM_RAW);
}
$tag = optional_param('flickr_tag', '', PARAM_RAW);
$license = optional_param('flickr_license', '', PARAM_RAW);
$this->sess_account = 'flickr_public_'.$this->id.'_account';
$this->sess_tag = 'flickr_public_'.$this->id.'_tag';
$this->sess_text = 'flickr_public_'.$this->id.'_text';
if (!empty($account) or !empty($fulltext) or !empty($tag) or !empty($license)) {
$SESSION->{$this->sess_tag} = $tag;
$SESSION->{$this->sess_text} = $fulltext;
$SESSION->{$this->sess_account} = $account;
}
}
/**
* check flickr account
* check if flickr account
* @return boolean
*/
public function check_login() {
@ -469,7 +469,7 @@ class repository_flickr_public extends repository {
/**
* Names of the plugin settings
* @return array
* @return array
*/
public static function get_type_option_names() {
return array('api_key');

View File

@ -1272,7 +1272,7 @@ abstract class repository {
* @param integer $readonly whether to create it readonly or not (defaults to not)
* @return mixed
*/
final public static function create($type, $userid, $context, $params, $readonly=0) {
public static function create($type, $userid, $context, $params, $readonly=0) {
global $CFG, $DB;
$params = (array)$params;
require_once($CFG->dirroot . '/repository/'. $type . '/repository.class.php');
@ -1613,7 +1613,6 @@ abstract class repository {
return $str;
}
}
}
/**
@ -1642,17 +1641,7 @@ class repository_exception extends moodle_exception {
final class repository_instance_form extends moodleform {
protected $instance;
protected $plugin;
public function definition() {
global $CFG;
// type of plugin, string
$this->plugin = $this->_customdata['plugin'];
$this->typeid = $this->_customdata['typeid'];
$this->contextid = $this->_customdata['contextid'];
$this->instance = (isset($this->_customdata['instance'])
&& is_subclass_of($this->_customdata['instance'], 'repository'))
? $this->_customdata['instance'] : null;
protected function add_defaults() {
$mform =& $this->_form;
$strrequired = get_string('required');
@ -1669,17 +1658,35 @@ final class repository_instance_form extends moodleform {
$mform->addElement('text', 'name', get_string('name'), 'maxlength="100" size="30"');
$mform->addRule('name', $strrequired, 'required', null, 'client');
}
public function definition() {
global $CFG;
// type of plugin, string
$this->plugin = $this->_customdata['plugin'];
$this->typeid = $this->_customdata['typeid'];
$this->contextid = $this->_customdata['contextid'];
$this->instance = (isset($this->_customdata['instance'])
&& is_subclass_of($this->_customdata['instance'], 'repository'))
? $this->_customdata['instance'] : null;
$mform =& $this->_form;
$this->add_defaults();
//add fields
if (!$this->instance) {
$result = repository::static_function($this->plugin, 'instance_config_form', $mform);
}
else {
if ($result === false) {
$mform->removeElement('name');
}
} else {
$data = array();
$data['name'] = $this->instance->name;
if (!$this->instance->readonly) {
$result = $this->instance->instance_config_form($mform);
if ($result === false) {
$mform->removeElement('name');
}
// and set the data if we have some.
foreach ($this->instance->get_instance_option_names() as $config) {
if (!empty($this->instance->options[$config])) {
@ -1692,7 +1699,11 @@ final class repository_instance_form extends moodleform {
$this->set_data($data);
}
$this->add_action_buttons(true, get_string('save','repository'));
if ($result === false) {
$mform->addElement('cancel');
} else {
$this->add_action_buttons(true, get_string('save','repository'));
}
}
public function validation($data) {

View File

@ -65,37 +65,36 @@ if ($usercourseid !== SITEID) {
$url->param('usercourseid', $usercourseid);
}
$PAGE->set_url($url);
require_login(SITEID, false);
$context = get_context_instance_by_id($contextid);
$PAGE->set_url($url);
$PAGE->set_context($context);
/// Security: make sure we're allowed to do this operation
if ($context->contextlevel == CONTEXT_COURSE) {
$pagename = get_string("repositorycourse",'repository');
// If the user is allowed to edit this course, he's allowed to edit list of repository instances
require_capability('moodle/course:update', $context);
if ( !$course = $DB->get_record('course', array('id'=>$context->instanceid))) {
print_error('invalidcourseid');
}
require_login($course, false);
// If the user is allowed to edit this course, he's allowed to edit list of repository instances
require_capability('moodle/course:update', $context);
} else if ($context->contextlevel == CONTEXT_USER) {
require_login();
$pagename = get_string("personalrepositories",'repository');
//is the user looking at its own repository instances
if ($USER->id != $context->instanceid){
print_error('notyourinstances', 'repository');
}
$user = $USER;
$PAGE->set_pagelayout('mydashboard');
} else {
print_error('invalidcontext');
}
/// Security: we cannot perform any action if the type is not visible or if the context has been disabled
if (!empty($new)){
$type = repository::get_type_by_typename($new);
@ -139,10 +138,7 @@ if ($context->contextlevel == CONTEXT_USER) {
}
}
echo $OUTPUT->heading($pagename);
$return = true;
if (!empty($edit) || !empty($new)) {
if (!empty($edit)) {
$instance = repository::get_instance($edit);
@ -186,15 +182,13 @@ if (!empty($edit) || !empty($new)) {
}
if ($success) {
$savedstr = get_string('configsaved', 'repository');
//echo $OUTPUT->header();
echo $OUTPUT->heading($savedstr);
redirect($baseurl, $savedstr, 3);
redirect($baseurl);
} else {
print_error('instancenotsaved', 'repository', $baseurl);
}
exit;
} else { // Display the form
// echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('configplugin', 'repository_'.$plugin));
$OUTPUT->box_start();
$mform->display();

View File

@ -33,7 +33,7 @@ class repository_wikimedia extends repository {
public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
parent::__construct($repositoryid, $context, $options);
$this->keyword = optional_param('wikimedia_keyword', '', PARAM_RAW);
if (empty($this->keyword)) {
if (empty($this->keyword)) {
$this->keyword = optional_param('s', '', PARAM_RAW);
}
}

View File

@ -21,7 +21,7 @@
*
* @author Dongsheng Cai <dongsheng@moodle.com>, Raul Kern <raunator@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
*/
define('WIKIMEDIA_THUMBS_PER_PAGE', 24);
define('WIKIMEDIA_FILE_NS', 6);
@ -30,10 +30,12 @@ define('WIKIMEDIA_IMAGE_SIDE_LENGTH', 1024);
class wikimedia {
private $_conn = null;
private $_param = array();
public function __construct($url = '') {
if (empty($url)) {
$this->api = 'http://commons.wikimedia.org/w/api.php';
} else {
$this->api = $url;
}
$this->_param['format'] = 'php';
$this->_param['redirects'] = true;
@ -108,7 +110,7 @@ class wikimedia {
*/
public function get_thumb_url($image_url, $orig_width, $orig_height, $thumb_width=75) {
global $OUTPUT;
if ($orig_width <= $thumb_width AND $orig_height <= $thumb_width) {
return $image_url;
} else {
@ -116,7 +118,7 @@ class wikimedia {
$commons_main_dir = 'http://upload.wikimedia.org/wikipedia/commons/';
if ($image_url) {
$short_path = str_replace($commons_main_dir, '', $image_url);
$extension = pathinfo($short_path, PATHINFO_EXTENSION);
$extension = pathinfo($short_path, PATHINFO_EXTENSION);
if (strcmp($extension, 'gif') == 0) { //no thumb for gifs
return $OUTPUT->pix_url(file_extension_icon('xx.jpg', 32));
}