From a55d94d4acce3e303c2848b1daa9d1bdf920e2b4 Mon Sep 17 00:00:00 2001 From: nicolasconnault Date: Sat, 25 Oct 2008 13:35:20 +0000 Subject: [PATCH] MDL-16341 Almost finished portfolio flickr plugin. Modified phpflickr->request method so it can upload files, but having permission issue (only read) --- lang/en_utf8/portfolio_flickr.php | 14 +++ lib/flickrlib.php | 22 ++++- lib/portfolio/plugin.php | 14 +-- portfolio/type/flickr/lib.php | 156 +++++++++++++++++++++++++++++- 4 files changed, 192 insertions(+), 14 deletions(-) diff --git a/lang/en_utf8/portfolio_flickr.php b/lang/en_utf8/portfolio_flickr.php index dc4a5ab8dc..a7e7b8568a 100644 --- a/lang/en_utf8/portfolio_flickr.php +++ b/lang/en_utf8/portfolio_flickr.php @@ -5,4 +5,18 @@ $string['err_noapikey'] = 'There is no API Key configured for this plugin. You $string['noauthtoken'] = 'Could not retrieve an authentication token for use in this session'; $string['pluginname'] = 'Flickr online photo management system'; $string['sharedsecret'] = 'Your shared secret string (will be provided with your generated API key)'; +$string['title'] = 'Title'; +$string['ispublic'] = 'Public (anyone can see them)'; +$string['isfriend'] = 'Visible to friends'; +$string['isfamily'] = 'Visible to family'; +$string['safetylevel'] = 'Safety level'; +$string['safe'] = 'Safe'; +$string['moderate'] = 'Moderate'; +$string['restricted'] = 'Restricted'; +$string['contenttype'] = 'Content types'; +$string['photo'] = 'Photos'; +$string['screenshot'] = 'Screenshots'; +$string['other'] = 'Art, illustration, CGI, or other non-photographic images'; +$string['hidefrompublicsearches'] = 'Hide these images from public searches?'; +$string['set'] = 'Set'; ?> diff --git a/lib/flickrlib.php b/lib/flickrlib.php index e4efd4cbe9..f5bcc0729c 100755 --- a/lib/flickrlib.php +++ b/lib/flickrlib.php @@ -70,6 +70,11 @@ class phpFlickr { function request ($command, $args = array()) { + if ($command == 'upload') { + $filecontent = $args['photo']; + unset($args['photo']); + } + //Sends a request to Flickr's REST endpoint via POST. if (substr($command,0,7) != "flickr.") { $command = "flickr." . $command; @@ -77,23 +82,32 @@ class phpFlickr { //Process arguments, including method and login data. $args = array_merge(array("method" => $command, "format" => "php_serial", "api_key" => $this->api_key), $args); + if (!empty($this->token)) { $args = array_merge($args, array("auth_token" => $this->token)); - } elseif (!empty($this->token)) { - $args = array_merge($args, array("auth_token" => $this->token)); } + ksort($args); $auth_sig = ""; foreach ($args as $key => $data) { - $auth_sig .= $key . $data; + if ($key != 'photo') { + $auth_sig .= $key . $data; + } } + if (!empty($this->secret)) { $api_sig = md5($this->secret . $auth_sig); $args['api_sig'] = $api_sig; } //$this->req->addHeader("Connection", "Keep-Alive"); - $ret = $this->curl->post($this->REST, $args); + if ($command != 'flickr.upload') { + $ret = $this->curl->post($this->REST, $args); + } else { + $ret = $this->curl->post($this->Upload, $args); + print_object($ret);die(); + } + $this->parsed_response = $this->clean_text_nodes(unserialize($ret)); if ($this->parsed_response['stat'] == 'fail') { if ($this->die_on_error) die("The Flickr API returned the following error: #{$this->parsed_response['code']} - {$this->parsed_response['message']}"); diff --git a/lib/portfolio/plugin.php b/lib/portfolio/plugin.php index 508da426f6..dd81994c99 100644 --- a/lib/portfolio/plugin.php +++ b/lib/portfolio/plugin.php @@ -37,32 +37,32 @@ abstract class portfolio_plugin_base { /** - * boolean * whether this object needs writing out to the database + * @var boolean $dirty */ protected $dirty; /** - * integer * id of instance + * @var integer $id */ protected $id; /** - * string * name of instance + * @var string $name */ protected $name; /** - * string * plugin this instance belongs to + * @var string $plugin */ protected $plugin; /** - * boolean * whether this instance is visible or not + * @var boolean $visible */ protected $visible; @@ -239,7 +239,7 @@ abstract class portfolio_plugin_base { foreach ($config as $key => $value) { if (!in_array($key, $allowed)) { $a = (object)array('property' => $key, 'class' => get_class($this)); - throw new portfolio_export_exception($this->get('exporter'), 'invalidexportproperty', 'portfolio', $this->get_return_url(), $a); + throw new portfolio_export_exception($this->get('exporter'), 'invalidexportproperty', 'portfolio', null, $a); } $this->exportconfig[$key] = $value; } @@ -261,7 +261,7 @@ abstract class portfolio_plugin_base { ); if (!in_array($key, $allowed)) { $a = (object)array('property' => $key, 'class' => get_class($this)); - throw new portfolio_export_exception($this->get('exporter'), 'invalidexportproperty', 'portfolio', $this->get_return_url(), $a); + throw new portfolio_export_exception($this->get('exporter'), 'invalidexportproperty', 'portfolio', null, $a); } if (!array_key_exists($key, $this->exportconfig)) { return null; diff --git a/portfolio/type/flickr/lib.php b/portfolio/type/flickr/lib.php index fde801c681..4e8e6e6aa6 100755 --- a/portfolio/type/flickr/lib.php +++ b/portfolio/type/flickr/lib.php @@ -5,6 +5,8 @@ require_once($CFG->libdir.'/flickrlib.php'); class portfolio_plugin_flickr extends portfolio_plugin_push_base { private $flickr; + private $token; + private $raw_sets; public static function supported_formats() { return array(PORTFOLIO_FORMAT_IMAGE); @@ -15,11 +17,31 @@ class portfolio_plugin_flickr extends portfolio_plugin_push_base { } public function prepare_package() { - $this->flickr = new phpFlickr($this->get_config('apikey'), $this->get_config('sharedsecret')); + } public function send_package() { - throw new portfolio_plugin_exception('notimplemented', 'portfolio', null, 'flickr'); + foreach ($this->exporter->get_tempfiles() as $file) { + // @TODO get max size from flickr people_getUploadStatus + $filesize = $file->get_filesize(); + + if ($file->is_valid_image()) { + $return = $this->flickr->request ('upload', array('photo' => $file->get_content(), + 'title' => $this->get_export_config('title'), + 'description' => $this->get_export_config('description'), + 'tags' => $this->get_export_config('tags'), + 'is_public' => $this->get_export_config('is_public'), + 'is_friend' => $this->get_export_config('is_friend'), + 'is_family' => $this->get_export_config('is_family'))); + + // TODO if a set was requested, attach the photo to that set + if ($return && $this->get_export_config('set')) { + // + } + // DEBUG!!! + var_dump($return);die(); + } + } } public static function allows_multiple() { @@ -27,7 +49,8 @@ class portfolio_plugin_flickr extends portfolio_plugin_push_base { } public function get_continue_url() { - return 'http://www.flickr.com/files#0:f:' . $this->get_export_config('folder'); + // return $this->flickr->urls_getUserPhotos(); + return "http://www.flickr.com/tools/uploader_edit.gne?ids="; // Add ids of uploaded files } public function expected_time($callertime) { @@ -50,4 +73,131 @@ class portfolio_plugin_flickr extends portfolio_plugin_push_base { $mform->addRule('sharedsecret', $strrequired, 'required', null, 'client'); } + public function has_export_config() { + return true; + } + + public function get_allowed_user_config() { + return array('authtoken', 'nsid'); + } + + public function steal_control($stage) { + if ($stage != PORTFOLIO_STAGE_CONFIG) { + return false; + } + if ($this->token) { + return false; + } + + $token = $this->get_user_config('authtoken', $this->get('user')->id); + $nsid = $this->get_user_config('nsid', $this->get('user')->id); + + $this->flickr = new phpFlickr($this->get_config('apikey'), $this->get_config('sharedsecret'), $token); + + if (!empty($token)) { + $this->token = $token; + $this->flickr = new phpFlickr($this->get_config('apikey'), $this->get_config('sharedsecret'), $token); + return false; + } + return $this->flickr->auth('write'); + } + + public function post_control($stage, $params) { + if ($stage != PORTFOLIO_STAGE_CONFIG) { + return; + } + if (!array_key_exists('frob', $params) || empty($params['frob'])) { + throw new portfolio_plugin_exception('noauthtoken', 'portfolio_flickr'); + } + + $this->flickr = new phpFlickr($this->get_config('apikey'), $this->get_config('sharedsecret')); + + $auth_info = $this->flickr->auth_getToken($params['frob']); + + $this->set_user_config(array('authtoken' => $auth_info['token'], 'nsid' => $auth_info['user']['nsid']), $this->get('user')->id); + } + + public function export_config_form(&$mform) { + $mform->addElement('text', 'plugin_title', get_string('title', 'portfolio_flickr')); + $mform->addElement('textarea', 'plugin_description', get_string('description')); + $mform->addElement('text', 'plugin_tags', get_string('tags')); + $mform->addElement('checkbox', 'plugin_is_public', get_string('ispublic', 'portfolio_flickr')); + $mform->addElement('checkbox', 'plugin_is_family', get_string('isfamily', 'portfolio_flickr')); + $mform->addElement('checkbox', 'plugin_is_friend', get_string('isfriend', 'portfolio_flickr')); + + $mform->disabledIf('plugin_is_friend', 'plugin_is_public', 'checked'); + $mform->disabledIf('plugin_is_family', 'plugin_is_public', 'checked'); + + $safety_levels = array(1 => $this->get_export_value_name('safety_level', 1), + 2 => $this->get_export_value_name('safety_level', 2), + 3 => $this->get_export_value_name('safety_level', 3)); + + $content_types = array(1 => $this->get_export_value_name('content_type', 1), + 2 => $this->get_export_value_name('content_type', 2), + 3 => $this->get_export_value_name('content_type', 3)); + + $hidden_values = array(1,2); + + $mform->addElement('select', 'plugin_safety_level', get_string('safetylevel', 'portfolio_flickr'), $safety_levels); + $mform->addElement('select', 'plugin_content_type', get_string('contenttype', 'portfolio_flickr'), $content_types); + $mform->addElement('advcheckbox', 'plugin_hidden', get_string('hidefrompublicsearches', 'portfolio_flickr'), get_string('yes'), null, $hidden_values); + + $mform->setDefaults(array('plugin_is_public' => true)); + + $sets = $this->get_sets(); + + if (!empty($sets)) { + $sets[0] = '----'; + $mform->addElement('select', 'plugin_set', get_string('set', 'portfolio_flickr'), $sets); + } + } + + private function get_sets() { + if (empty($this->raw_sets)) { + $this->raw_sets = $this->flickr->photosets_getList(); + } + + $sets = array(); + foreach ($this->raw_sets['photoset'] as $set_data) { + $sets[$set_data['id']] = $set_data['title']; + } + return $sets; + } + + public function get_allowed_export_config() { + return array('set', 'title', 'description', 'tags', 'is_public', 'is_family', 'is_friend', 'safety_level', 'content_type', 'hidden'); + } + + public function get_export_summary() { + return array(get_string('set', 'portfolio_flickr') => $this->get_export_value_name('set', $this->get_export_config('set')), + get_string('title', 'portfolio_flickr') => $this->get_export_config('title'), + get_string('description') => $this->get_export_config('description'), + get_string('tags') => $this->get_export_config('tags'), + get_string('ispublic', 'portfolio_flickr') => $this->get_export_value_name('is_public', $this->get_export_config('is_public')), + get_string('isfamily', 'portfolio_flickr') => $this->get_export_value_name('is_family', $this->get_export_config('is_family')), + get_string('isfriend', 'portfolio_flickr') => $this->get_export_value_name('is_friend', $this->get_export_config('is_friend')), + get_string('safetylevel', 'portfolio_flickr') => $this->get_export_value_name('safety_level', $this->get_export_config('safety_level')), + get_string('contenttype', 'portfolio_flickr') => $this->get_export_value_name('content_type', $this->get_export_config('content_type')), + get_string('hidefrompublicsearches', 'portfolio_flickr') => $this->get_export_value_name('hidden', $this->get_export_config('hidden'))); + } + + private function get_export_value_name($param, $value) { + $params = array('set' => $this->get_sets(), + 'is_public' => array(0 => get_string('no'), 1 => get_string('yes')), + 'is_family' => array(0 => get_string('no'), 1 => get_string('yes')), + 'is_friend' => array(0 => get_string('no'), 1 => get_string('yes')), + 'safety_level' => array(1 => get_string('safe', 'portfolio_flickr'), + 2 => get_string('moderate', 'portfolio_flickr'), + 3 => get_string('restricted', 'portfolio_flickr')), + 'content_type' => array(1 => get_string('photo', 'portfolio_flickr'), + 2 => get_string('screenshot', 'portfolio_flickr'), + 3 => get_string('other', 'portfolio_flickr')), + 'hidden' => array(1 => get_string('no'), 2 => get_string('yes'))); + + if (isset($params[$param][$value])) { + return $params[$param][$value]; + } else { + return '-'; + } + } } -- 2.39.5