MDL-15402: fix a bug with parameter in the delete URL (wrong cleaning setting) +...
[moodle.git] / repository / flickr / repository.class.php
CommitLineData
36ea9a6c 1<?php
2/**
6a391ebf 3 * repository_flickr class
539b3d13 4 * This plugin is used to access user's private flickr repository
36ea9a6c 5 *
539b3d13 6 * @author Dongsheng Cai <dongsheng@moodle.com>
7 * @version $Id$
36ea9a6c 8 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9 */
10
539b3d13 11require_once($CFG->libdir.'/flickrlib.php');
6a391ebf 12
13class repository_flickr extends repository{
14 private $flickr;
15 public $photos;
19add4c0 16
27051e43 17 public function set_option($options = array()){
18 if (!empty($options['api_key'])) {
70d91d90 19 set_config('api_key', trim($options['api_key']), 'flickr');
27051e43 20 }
7fab56b7 21 if (!empty($options['secret'])) {
22 set_config('secret', trim($options['secret']), 'flickr');
23 }
27051e43 24 unset($options['api_key']);
7fab56b7 25 unset($options['secret']);
27051e43 26 $ret = parent::set_option($options);
27 return $ret;
28 }
29
30 public function get_option($config = ''){
31 if($config==='api_key'){
70d91d90 32 return trim(get_config('flickr', 'api_key'));
7fab56b7 33 } elseif ($config ==='secret') {
34 return trim(get_config('flickr', 'secret'));
27051e43 35 } else {
70d91d90 36 $options['api_key'] = trim(get_config('flickr', 'api_key'));
7fab56b7 37 $options['secret'] = trim(get_config('flickr', 'secret'));
27051e43 38 }
39 $options = parent::get_option($config);
40 return $options;
41 }
42
455860ce 43 public function global_search(){
44 global $SESSION;
8988c36b 45 if (empty($this->token)) {
455860ce 46 return false;
47 } else {
48 return true;
49 }
50 }
51
36ea9a6c 52 public function __construct($repositoryid, $context = SITEID, $options = array()){
d72ff7a2 53 global $SESSION, $action, $CFG;
6a391ebf 54 $options['page'] = optional_param('p', 1, PARAM_INT);
36ea9a6c 55 parent::__construct($repositoryid, $context, $options);
8988c36b 56
57 $this->setting = 'flickr_';
58
eb9aa683 59 $this->api_key = $this->get_option('api_key');
7fab56b7 60 $this->secret = $this->get_option('secret');
8988c36b 61
62 $this->token = get_user_preferences($this->setting, '');
63 $this->nsid = get_user_preferences($this->setting.'_nsid', '');
64
65 $this->flickr = new phpFlickr($this->api_key, $this->secret, $this->token);
6a391ebf 66
67 $reset = optional_param('reset', 0, PARAM_INT);
8988c36b 68 if(empty($this->token)){
69 $frob = optional_param('frob', '', PARAM_RAW);
70 if(!empty($frob)){
71 $auth_info = $this->flickr->auth_getToken($frob);
72 $this->token = $auth_info['token'];
73 $this->nsid = $auth_info['user']['nsid'];
74 set_user_preference($this->setting, $auth_info['token']);
75 set_user_preference($this->setting.'_nsid', $auth_info['user']['nsid']);
76 $this->perm = $auth_info['token'];
77 }else{
78 $action = 'login';
79 }
80 }
6a391ebf 81 if(!empty($reset)) {
8988c36b 82 set_user_preference($this->setting, '');
83 set_user_preference($this->setting.'_nsid', '');
84 $this->token = '';
85 $this->nsid = '';
86 $action = 'login';
6a391ebf 87 }
88
8988c36b 89 if(!empty($this->token)) {
90 if(empty($action)){
6a391ebf 91 $action = 'list';
92 }
93 } else {
8988c36b 94 $action = 'login';
6a391ebf 95 }
36ea9a6c 96 }
0eb58cf4 97 public function print_login($ajax = true){
36ea9a6c 98 global $SESSION;
8988c36b 99 if(!empty($this->token)){
100 }
101 if(empty($this->token)) {
6a391ebf 102 if($ajax){
103 $ret = array();
8988c36b 104 $popup_btn = new stdclass;
105 $popup_btn->type = 'popup';
106 $popup_btn->url = $this->flickr->auth();
107 $ret['login'] = array($popup_btn);
6a391ebf 108 return $ret;
6a391ebf 109 }
110 } else {
111 return $this->get_listing();
112 }
36ea9a6c 113 }
6a391ebf 114 public function get_listing($path = '1', $search = ''){
36ea9a6c 115 global $SESSION;
2b9feb5f 116 $nsid = get_user_preferences($this->setting.'_nsid');
8988c36b 117 $photos_url = $this->flickr->urls_getUserPhotos($nsid);
6a391ebf 118
119 if(!empty($search)) {
8988c36b 120 $photos = $this->flickr->photos_search(array(
2b9feb5f 121 'user_id'=>$nsid,
8988c36b 122 'per_page'=>25,
123 'page'=>$path,
124 'extras'=>'original_format',
125 'text'=>$search
126 ));
6a391ebf 127 } elseif(!empty($path) && empty($search)) {
8988c36b 128 $photos = $this->flickr->photos_search(array(
129 'user_id'=>$nsid,
130 'per_page'=>25,
131 'page'=>$path,
132 'extras'=>'original_format'
133 ));
6a391ebf 134 }
d05ef4a3 135 $ret = array();
136 $ret['manage'] = $photos_url;
137 $ret['list'] = array();
138 $ret['pages'] = $photos['pages'];
139 if(is_int($path) && $path <= $ret['pages']) {
140 $ret['page'] = $path;
6a391ebf 141 } else {
d05ef4a3 142 $ret['page'] = 1;
6a391ebf 143 }
8988c36b 144 if(!empty($photos['photo'])){
145 foreach ($photos['photo'] as $p) {
146 if(empty($p['title'])) {
147 $p['title'] = get_string('notitle', 'repository_flickr');
148 }
149 if (isset($p['originalformat'])) {
150 $format = $p['originalformat'];
151 } else {
152 $format = 'jpg';
153 }
154 $ret['list'][] =
155 array('title'=>$p['title'].'.'.$format,'source'=>$p['id'],'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'), 'date'=>'', 'size'=>'unknown', 'url'=>$photos_url.$p['id']);
733cfd80 156 }
6a391ebf 157 }
bf1fccf0 158 if(empty($ret)) {
6a391ebf 159 throw new repository_exception('nullphotolist', 'repository_flickr');
bf1fccf0 160 } else {
161 return $ret;
162 }
36ea9a6c 163 }
164 public function print_listing(){
539b3d13 165 return false;
36ea9a6c 166 }
167 public function print_search(){
2b9feb5f 168 echo '<input name="s" value="" />';
169 parent::print_search();
170 return true;
36ea9a6c 171 }
6a391ebf 172 public function get_file($photo_id, $file = ''){
173 global $CFG;
174 $result = $this->flickr->photos_getSizes($photo_id);
175 $url = '';
176 if(!empty($result[4])) {
177 $url = $result[4]['source'];
178 } elseif(!empty($result[3])) {
179 $url = $result[3]['source'];
180 } elseif(!empty($result[2])) {
181 $url = $result[2]['source'];
182 }
183 if (!file_exists($CFG->dataroot.'/repository/download')) {
184 mkdir($CFG->dataroot.'/repository/download/', 0777, true);
185 }
186 if(is_dir($CFG->dataroot.'/repository/download')) {
187 $dir = $CFG->dataroot.'/repository/download/';
188 }
189
190 if(empty($file)) {
191 $file = $photo_id.'_'.time().'.jpg';
192 }
193 if(file_exists($dir.$file)) {
194 $file = uniqid('m').$file;
195 }
196 $fp = fopen($dir.$file, 'w');
197 $c = new curl;
198 $c->download(array(
199 array('url'=>$url, 'file'=>$fp)
200 ));
201 return $dir.$file;
202 }
4a65c39a 203 public static function has_admin_config() {
204 return true;
205 }
a6600395 206
faaa613d 207 public static function has_multiple_instances() {
7fab56b7 208 return false;
faaa613d 209 }
210
211 public static function has_instance_config() {
7892948d 212 return false;
0868f619 213 }
214
215 public function instance_config_form(&$mform) {
216 $mform->addElement('text', 'email_address', get_string('emailaddress', 'repository_flickr'));
3701fd35 217 $mform->addRule('email_address', get_string('required'), 'required', null, 'client');
0868f619 218 }
219
220 public static function get_instance_option_names(){
221 return array('email_address');
faaa613d 222 }
223
4a65c39a 224 public function admin_config_form(&$mform) {
7fab56b7 225 global $CFG;
336bb44b 226 $api_key = get_config('flickr', 'api_key');
7892948d 227 $secret = get_config('flickr', 'secret');
228
229 //retrieve the flickr instances
230 $instances = repository_get_instances(array(),null,false,"flickr");
231 if (empty($instances)){
232 $callbackurl = get_string("callbackwarning","repository_flickr");
233 }
234 else {
235 $callbackurl = $CFG->wwwroot.'/repository/ws.php?callback=yes&repo_id='.$instances[0]->id;
236 }
237
336bb44b 238 if (empty($api_key)) {
239 $api_key = '';
240 }
0868f619 241 if (empty($secret)) {
242 $secret = '';
243 }
7892948d 244
4a65c39a 245 $strrequired = get_string('required');
0868f619 246 $mform->addElement('text', 'api_key', get_string('apikey', 'repository_flickr'), array('value'=>$api_key,'size' => '40'));
247 $mform->addElement('text', 'secret', get_string('secret', 'repository_flickr'), array('value'=>$secret,'size' => '40'));
7fab56b7 248 $mform->addElement('static', 'callbackurl', get_string('callbackurl', 'repository_flickr'), $callbackurl);
0868f619 249
4a65c39a 250 $mform->addRule('api_key', $strrequired, 'required', null, 'client');
0868f619 251 $mform->addRule('secret', $strrequired, 'required', null, 'client');
4a65c39a 252 }
0868f619 253
a6600395 254 public static function get_admin_option_names(){
7fab56b7 255 return array('api_key', 'secret');
4a65c39a 256 }
a6600395 257
36ea9a6c 258}