PageRenderTime 37ms CodeModel.GetById 21ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/php/photobucket_external_repository_manager_connector.class.php

https://bitbucket.org/chamilo/chamilo-ext-repo-photobucket-dev/
PHP | 393 lines | 323 code | 51 blank | 19 comment | 28 complexity | 11b60854d48dfb906c3f2a9722325ee9 MD5 | raw file
  1<?php
  2namespace common\extensions\external_repository_manager\implementation\photobucket;
  3
  4use common\libraries;
  5
  6use common\libraries\Request;
  7use common\libraries\Path;
  8use common\libraries\Session;
  9use common\libraries\Utilities;
 10use common\libraries\Redirect;
 11use common\libraries\ArrayResultSet;
 12
 13use common\extensions\external_repository_manager\ExternalRepositoryManagerConnector;
 14use common\extensions\external_repository_manager\ExternalRepositoryObject;
 15
 16use repository\RepositoryDataManager;
 17use repository\ExternalSetting;
 18use repository\ExternalUserSetting;
 19
 20use PBAPI;
 21
 22require_once dirname(__FILE__) . '/photobucket_external_repository_object.class.php';
 23require_once 'OAuth/Request.php';
 24require_once Path :: get_plugin_path(__NAMESPACE__) . 'PBAPI/PBAPI.php';
 25/**
 26 *
 27 * @author magali.gillard
 28 * key : 149830482
 29 * secret : 410277f61d5fc4b01a9b9e763bf2e97b
 30 */
 31class PhotobucketExternalRepositoryManagerConnector extends ExternalRepositoryManagerConnector
 32{
 33    private $photobucket;
 34    private $consumer;
 35    private $key;
 36    private $secret;
 37    private $photobucket_session;
 38
 39    function __construct($external_repository_instance)
 40    {
 41        parent :: __construct($external_repository_instance);
 42        
 43        $this->key = ExternalSetting :: get('consumer_key', $this->get_external_repository_instance_id());
 44        $this->secret = ExternalSetting :: get('consumer_secret', $this->get_external_repository_instance_id());
 45        $url = ExternalSetting :: get('url', $this->get_external_repository_instance_id());
 46        $this->login();
 47    }
 48
 49    function login()
 50    {
 51        $this->consumer = new PBAPI($this->key, $this->secret);
 52        $this->consumer->setResponseParser('simplexmlarray');
 53        
 54        $this->photobucket_session = unserialize(ExternalUserSetting :: get('session', $this->get_external_repository_instance_id()));
 55        $oauth_access_token = $this->photobucket_session['photobucket_access_token'];
 56        
 57        $oauth_request_token = Session :: retrieve('photobucket_request_token');
 58        if (! $oauth_access_token)
 59        {
 60            if (! $oauth_request_token)
 61            {
 62                $this->consumer->login('request')->post()->loadTokenFromResponse();
 63                Session :: register('photobucket_request_token', serialize($this->consumer->getOauthToken()));
 64                $this->consumer->goRedirect('login', Redirect :: current_url());
 65            }
 66            else
 67            {
 68                $oauth_request_token = unserialize($oauth_request_token);
 69                $this->consumer->setOAuthToken($oauth_request_token->getKey(), $oauth_request_token->getSecret());
 70                
 71                $this->consumer->login('access')->post()->loadTokenFromResponse();
 72                
 73                $session_array = array();
 74                $session_array['photobucket_access_token'] = $this->consumer->getOAuthToken();
 75                $session_array['photobucket_username'] = $this->consumer->getUsername();
 76                $session_array['photobucket_subdomain'] = $this->consumer->getSubdomain();
 77                $session_array = serialize($session_array);
 78                
 79                $setting = RepositoryDataManager :: get_instance()->retrieve_external_setting_from_variable_name('session', $this->get_external_repository_instance_id());
 80                $user_setting = new ExternalUserSetting();
 81                $user_setting->set_setting_id($setting->get_id());
 82                $user_setting->set_user_id(Session :: get_user_id());
 83                $user_setting->set_value($session_array);
 84                if ($user_setting->create())
 85                {
 86                    Session :: unregister('photobucket_request_token');
 87                }
 88            }
 89        
 90        }
 91        else
 92        {
 93            $username = $this->photobucket_session['photobucket_username'];
 94            $subdomain = $this->photobucket_session['photobucket_subdomain'];
 95            
 96            $this->consumer->setOAuthToken($oauth_access_token->getKey(), $oauth_access_token->getSecret(), $username);
 97            $this->consumer->setSubdomain($subdomain);
 98        }
 99    }
100
101    //Only image for the moment
102    function retrieve_external_repository_objects($condition, $order_property, $offset, $count)
103    {
104        $response = $this->retrieve_photos($condition, $order_property, $offset, $count);
105        $objects = array();
106        
107        foreach ($response['media'] as $media)
108        {
109            $objects[] = $this->set_photo_object($media);
110        }
111        return new ArrayResultSet($objects);
112    }
113
114    function retrieve_photos($condition, $order_property, $offset, $count)
115    {
116        $feed_type = Request :: get(PhotobucketExternalRepositoryManager :: PARAM_FEED_TYPE);
117        
118        $offset = (($offset - ($offset % $count)) / $count) + 1;
119        if (is_null($condition))
120        {
121            $condition = '';
122        }
123        
124        switch ($feed_type)
125        {
126            case PhotobucketExternalRepositoryManager :: FEED_TYPE_GENERAL :
127                $response = $this->consumer->search($condition, array('num' => $count, 'perpage' => $count, 
128                        'page' => $offset, 'secondaryperpage' => 1))->get()->getParsedResponse(true);
129                if ($condition)
130                {
131                    $response = $response['result']['primary'];
132                }
133                else
134                {
135                    $response = $response['result'];
136                }
137                
138                break;
139            case PhotobucketExternalRepositoryManager :: FEED_TYPE_MY_PHOTOS :
140                if ($condition)
141                {
142                    $response = $this->consumer->search($condition, array('num' => $count, 'perpage' => $count, 
143                            'page' => $offset, 'secondaryperpage' => 1))->get()->getParsedResponse(true);
144                    $response = $response['result']['primary'];
145                }
146                else
147                {
148                    $response = $this->consumer->user($this->photobucket_session['photobucket_username'])->search($condition, array(
149                            'perpage' => $count, 'page' => $offset, 'type' => 'image'))->get()->getParsedResponse(true);
150                    if ($response['_attribs']['totalresults'] == 1)
151                    {
152                        $response['media'] = array($response['media']);
153                    }
154                }
155                break;
156            default :
157                if ($condition)
158                {
159                    $response = $this->consumer->search($condition, array('num' => $count, 'perpage' => $count, 
160                            'page' => $offset, 'secondaryperpage' => 1))->get()->getParsedResponse(true);
161                    $response = $response['result']['primary'];
162                }
163                else
164                {
165                    $response = $this->consumer->user($this->photobucket_session['photobucket_username'])->search($condition, array(
166                            'perpage' => $count, 'page' => $offset, 'type' => 'image'))->get()->getParsedResponse(true);
167                    if ($response['_attribs']['totalresults'] == 1)
168                    {
169                        $response['media'] = array($response['media']);
170                    }
171                }
172                break;
173        }
174        return $response;
175    }
176
177    function retrieve_external_repository_object($id)
178    {
179        $data = $this->consumer->media(urldecode($id))->get()->getParsedResponse(true);
180        
181        return $this->set_photo_object($data['media']);
182    }
183
184    function set_photo_object($data)
185    {
186        $object = new PhotobucketExternalRepositoryObject();
187        $object->set_id(urlencode($data['url']));
188        $object->set_title($data['title']);
189        $object->set_description($data['description']);
190        $object->set_url($data['url']);
191        $object->set_thumbnail($data['thumb']);
192        
193        $object->set_owner_id($data['_attribs']['username']);
194        $object->set_created($data['_attribs']['uploaddate']);
195        $object->set_modified($data['_attribs']['uploaddate']);
196        $object->set_type(Utilities :: camelcase_to_underscores($data['_attribs']['type']));
197        $object->set_rights($this->determine_rights($data));
198        
199        $tags = array();
200        if (array_key_exists('_attribs', $data['tag']))
201        {
202            $data['tag'] = array($data['tag']);
203        }
204        
205        if (count($data['tag']) > 0)
206        {
207            foreach ($data['tag'] as $tag)
208            {
209                $tags[] = $tag['_attribs']['tag'];
210            }
211        }
212        //        elseif (count($data['media']['tag']) == 1)
213        //        {
214        //            $tags[] = $data['tag']['_attribs']['tag'];
215        //        }
216        $object->set_tags($tags);
217        
218        return $object;
219    }
220
221    function count_external_repository_objects($condition)
222    {
223        $feed_type = Request :: get(PhotobucketExternalRepositoryManager :: PARAM_FEED_TYPE);
224        
225        if (is_null($condition))
226        {
227            $condition = '';
228        }
229        
230        switch ($feed_type)
231        {
232            case PhotobucketExternalRepositoryManager :: FEED_TYPE_GENERAL :
233                
234                if ($condition)
235                {
236                    $response = $this->consumer->search($condition, array('num' => 1, 'perpage' => 1, 'page' => 1, 
237                            'secondaryperpage' => 1))->get()->getParsedResponse(true);
238                    return $response['result']['_attribs']['totalresults'];
239                }
240                else
241                {
242                    return 900;
243                }
244                
245                break;
246            case PhotobucketExternalRepositoryManager :: FEED_TYPE_MY_PHOTOS :
247                if ($condition)
248                {
249                    $response = $this->consumer->search($condition, array('num' => 1, 'perpage' => 1, 'page' => 1, 
250                            'secondaryperpage' => 1))->get()->getParsedResponse(true);
251                    return $response['result']['_attribs']['totalresults'];
252                }
253                else
254                {
255                    $response = $this->consumer->user($this->photobucket_session['photobucket_username'])->get()->getParsedResponse(true);
256                    return $response['total_pictures'];
257                }
258                
259                break;
260            default :
261                if ($condition)
262                {
263                    $response = $this->consumer->search($condition, array('num' => 1, 'perpage' => 1, 'page' => 1, 
264                            'secondaryperpage' => 1))->get()->getParsedResponse(true);
265                    return $response['result']['_attribs']['totalresults'];
266                }
267                else
268                {
269                    $response = $this->consumer->user($this->photobucket_session['photobucket_username'])->get()->getParsedResponse(true);
270                    return $response['total_pictures'];
271                }
272                break;
273        }
274    
275    }
276
277    /**
278     * @param array $values
279     * @return boolean
280     */
281    function update_external_repository_object($values)
282    {
283        while ($data = $this->consumer->media(urldecode($values[PhotobucketExternalRepositoryObject :: PROPERTY_ID]))->tag()->get()->getParsedResponse(true))
284        {
285            $response = $this->consumer->media(urldecode($values[PhotobucketExternalRepositoryObject :: PROPERTY_ID]))->tag($data['tagid'])->delete()->getParsedResponse(true);
286        }
287        
288        $response = $this->consumer->media(urldecode($values[PhotobucketExternalRepositoryObject :: PROPERTY_ID]))->title(array(
289                'title' => $values[PhotobucketExternalRepositoryObject :: PROPERTY_TITLE]))->put()->getParsedResponse(true);
290        if (! $response)
291        {
292            return false;
293        }
294        else
295        {
296            $response = $this->consumer->media(urldecode($values[PhotobucketExternalRepositoryObject :: PROPERTY_ID]))->description(array(
297                    'description' => $values[PhotobucketExternalRepositoryObject :: PROPERTY_DESCRIPTION]))->put()->getParsedResponse(true);
298            if (! $response)
299            {
300                return false;
301            }
302        }
303        if ($values[PhotobucketExternalRepositoryObject :: PROPERTY_TAGS])
304        {
305            $tags = explode(',', $values[PhotobucketExternalRepositoryObject :: PROPERTY_TAGS]);
306            
307            foreach ($tags as $tag)
308            {
309                $response = $this->consumer->media(urldecode($values[PhotobucketExternalRepositoryObject :: PROPERTY_ID]))->tag(array(
310                        'tag' => $tag, 'topleftx' => 0, 'toplefty' => 0, 'bottomrightx' => 0, 'bottomrighty' => 0))->post()->getParsedResponse(true);
311                if (! $response)
312                {
313                    return false;
314                }
315            }
316        }
317        
318        return true;
319    }
320
321    function delete_external_repository_object($id)
322    {
323        $response = $this->consumer->media($id)->delete()->getParsedResponse(true);
324        if ($response['deleted'] == 1)
325        {
326            return true;
327        }
328        return false;
329    }
330
331    function create_external_repository_object($values, $file)
332    {
333        $photo = base64_encode(file_get_contents($file['tmp_name']));
334        $tags = explode(',', $values[PhotobucketExternalRepositoryObject :: PROPERTY_TAGS]);
335        $session = unserialize(ExternalUserSetting :: get('session', $this->get_external_repository_instance_id(), Session :: get_user_id()));
336        $response = $this->consumer->album($session['photobucket_username'])->upload(array('type' => 'base64', 
337                'filename' => $file['name'], 'uploadfile' => $photo, 
338                'title' => $values[PhotobucketExternalRepositoryObject :: PROPERTY_TITLE], 
339                'description' => $values[PhotobucketExternalRepositoryObject :: PROPERTY_DESCRIPTION]))->post()->getParsedResponse(true);
340        
341        foreach ($tags as $tag)
342        {
343            $this->consumer->media(urlencode($response['url']))->tag(array('tag' => $tag, 'topleftx' => 0, 
344                    'toplefty' => 0, 'bottomrightx' => 0, 'bottomrighty' => 0))->post()->getParsedResponse(true);
345        }
346        return urlencode($response['url']);
347    
348    }
349
350    function export_external_repository_object($object)
351    {
352        $photo = base64_encode(file_get_contents($object->get_full_path()));
353        
354        $response = $this->consumer->album($this->photobucket_session['photobucket_username'])->upload(array(
355                'type' => 'base64', 'filename' => $object->get_filename(), 'uploadfile' => $photo, 
356                'title' => $object->get_title(), 'description' => $object->get_description()))->post()->getParsedResponse(true);
357        
358        return urlencode($response['url']);
359    }
360
361    function determine_rights($photo)
362    {
363        $rights = array();
364        if ($this->photobucket_session['photobucket_username'] == $photo['_attribs']['username'])
365        {
366            
367            $rights[ExternalRepositoryObject :: RIGHT_USE] = true;
368            $rights[ExternalRepositoryObject :: RIGHT_EDIT] = true;
369            $rights[ExternalRepositoryObject :: RIGHT_DELETE] = true;
370            $rights[ExternalRepositoryObject :: RIGHT_DOWNLOAD] = true;
371        }
372        else
373        {
374            $rights[ExternalRepositoryObject :: RIGHT_USE] = true;
375            $rights[ExternalRepositoryObject :: RIGHT_EDIT] = false;
376            $rights[ExternalRepositoryObject :: RIGHT_DELETE] = false;
377            $rights[ExternalRepositoryObject :: RIGHT_DOWNLOAD] = false;
378        }
379        return $rights;
380    }
381
382    /**
383     * @param string $query
384     * @return string
385     */
386    static 
387
388    function translate_search_query($query)
389    {
390        return $query;
391    }
392}
393?>