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