source: extensions/instagram2piwigo/include/Instagram/CurrentUser.php @ 19561

Last change on this file since 19561 was 19561, checked in by mistic100, 11 years ago

first commit

File size: 11.2 KB
Line 
1<?php
2
3/**
4* Instagram PHP
5* @author Galen Grover <galenjr@gmail.com>
6* @license http://opensource.org/licenses/mit-license.php The MIT License
7*/
8
9
10include_once(INSTAGRAM_ROOT.'/Core/Proxy.php');
11include_once(INSTAGRAM_ROOT.'/Collection/MediaCollection.php');
12include_once(INSTAGRAM_ROOT.'/Collection/LikedMediaCollection.php');
13include_once(INSTAGRAM_ROOT.'/Collection/UserCollection.php');
14include_once(INSTAGRAM_ROOT.'/User.php');
15include_once(INSTAGRAM_ROOT.'/Comment.php');
16include_once(INSTAGRAM_ROOT.'/Media.php');
17
18
19/**
20 * Current User
21 *
22 * Holds the currently logged in user
23 *
24 * @see Instagram->getCurrentUser()
25 * {@link https://github.com/galen/PHP-Instagram-API/blob/master/Examples/current_user.php}
26 * {@link http://galengrover.com/projects/PHP-Instagram-API/Examples/?example=current_user.php}
27 */
28class Instagram_CurrentUser extends Instagram_User {
29
30    /**
31     * Holds relationship info for the current user
32     *
33     * @access protected
34     * @var array
35     */
36    protected $relationships = array();
37
38    /**
39     * Holds liked info for the current user
40     *
41     * Current user likes are stored in media objects
42     * If a media is liked after a media has been fetched the like will not be a part of the media object
43     *
44     * @access protected
45     * @var array
46     */
47    protected $liked = array();
48
49    /**
50     * Holds unliked info for the current user
51     *
52     * @access protected
53     * @var array
54     */
55    protected $unliked = array();
56
57    /**
58     * Get the API ID
59     *
60     * @return string Returns "self"
61     * @access public
62     */
63    public function getApiId() {
64        return 'self';
65    }
66
67    /**
68     * Does the current use like a media
69     *
70     * @param Instagram_Media $media Media to query for a like from the current user
71     * @access public
72     */
73    public function likes( Instagram_Media $media ) {
74        return
75            isset( $this->liked[$media->getId()] ) ||
76            (
77                isset( $media->getData()->user_has_liked ) &&
78                (bool)$media->getData()->user_has_liked === true &&
79                !isset( $this->unliked[$media->getId()] )
80            );
81    }
82
83    /**
84     * Add like from current user
85     *
86     * @param Instagram_Media|string $media Media to add a like to from the current Instagram_User
87     * @access public
88     */
89    public function addLike( $media ) {
90        if ( $media instanceof Instagram_Media ) {
91            $media = $media->getId();
92        }
93        $this->proxy->like( $media );
94        unset( $this->unliked[$media] );
95        $this->liked[$media] = true;
96    }
97
98    /**
99     * Delete like from current user
100     *
101     * @param Instagram_Media|string $media Media to delete a like to from the current user
102     * @access public
103     */
104    public function deleteLike( $media ) {
105        if ( $media instanceof Instagram_Media ) {
106            $media = $media->getId();
107        }
108        $this->proxy->unLike( $media );
109        unset( $this->liked[$media] );
110        $this->unliked[$media] = true;
111    }
112
113    /**
114     * Add a comment
115     *
116     * @param \Instagram\Media|string Media to add a comment to
117     * @param string $text Comment text
118     * @access public
119     */
120    public function addMediaComment( $media, $text ) {
121        if ( $media instanceof Instagram_Media ) {
122            $media = $media->getId();
123        }
124        $this->proxy->addMediaComment( $media, $text );
125    }
126
127    /**
128     * Delete a comment
129     *
130     * @param Instagram_Media|string Media to delete a comment from
131     * @param string $text Instagram_Comment text
132     * @access public
133     */
134    public function deleteMediaComment( $media, $comment ) {
135        if ( $media instanceof Instagram_Media ) {
136            $media = $media->getId();
137        }
138        if ( $comment instanceof Instagram_Comment ) {
139            $comment = $comment->getId();
140        }
141        $this->proxy->deleteMediaComment( $media, $comment );
142    }
143
144    /**
145     * Update relationship with a user
146     *
147     * Internal function that updates relationships
148     *
149     * @see Instagram_CurrentUser::follow
150     * @see Instagram_CurrentUser::unFollow
151     * @see Instagram_CurrentUser::getRelationship
152     * @see Instagram_CurrentUser::approveFollowRequest
153     * @see Instagram_CurrentUser::ignoreFollowRequest
154     * @see Instagram_CurrentUser::block
155     * @see Instagram_CurrentUser::unblock
156     *
157     * @param Instagram_User|string Instagram_User object or user id whose relationship you'd like to update
158     * @access protected
159     */
160    protected function updateRelationship( $user ) {
161        if ( $user instanceof Instagram_User ) {
162            $user = $user->getId();
163        }
164        if ( !isset( $this->relationships[ $user ] ) ) {
165            $this->relationships[ $user ] = $this->proxy->getRelationshipToCurrentUser( $user );
166        }
167    }
168
169    /**
170     * Follow Instagram_User
171     *
172     * @param Instagram_User|string Instagram_User object or Instagram_User id who should be followed
173     * @return boolean
174     */
175    public function follow( $user ) {
176        if ( $user instanceof Instagram_User ) {
177            $user = $user->getId();
178        }
179        $this->updateRelationship( $user );
180        $response = $this->proxy->modifyRelationship( $user, 'follow' );
181        foreach( $response as $r => $v ) {
182            $this->relationships[ $user ]->$r = $v;
183        }
184        return true;
185    }
186
187    /**
188     * Approve follower
189     *
190     * @param Instagram_User|string $user Instagram_User object or Instagram_User id who should be approved for following
191     * @return boolean
192     */
193    public function approveFollowRequest( $user ) {
194        if ( $user instanceof Instagram_User ) {
195            $user = $user->getId();
196        }
197        $this->updateRelationship( $user ); 
198        $response = $this->proxy->modifyRelationship( $user, 'approve' );
199        foreach( $response as $r => $v ) {
200            $this->relationships[ $user ]->$r = $v;
201        }
202        return true;
203    }
204
205    /**
206     * Ignore follower
207     *
208     * @param Instagram_User|string $user Instagram_User object or user id who should be ignored
209     * @return boolean
210     */
211    public function ignoreFollowRequest( $user ) {
212        if ( $user instanceof Instagram_User ) {
213            $user = $user->getId();
214        }
215        $this->updateRelationship( $user ); 
216        $response = $this->proxy->modifyRelationship( $user, 'ignore' );
217        foreach( $response as $r => $v ) {
218            $this->relationships[ $user ]->$r = $v;
219        }
220        return true;
221    }
222
223    /**
224     * Unfollow user
225     *
226     * @param Instagram_User|string $user Instagram_User object or user id who should be unfollowed
227     * @return boolean
228     */
229    public function unFollow( $user ) {
230        if ( $user instanceof Instagram_User ) {
231            $user = $user->getId();
232        }
233        $this->updateRelationship( $user ); 
234        $response = $this->proxy->modifyRelationship( $user, 'unfollow' );
235        foreach( $response as $r => $v ) {
236            $this->relationships[ $user ]->$r = $v;
237        }
238        return true;
239    }
240
241    /**
242     * Block user
243     *
244     * @param Instagram_User|string $user Instagram_User object or user id who should be blocked
245     * @return boolean
246     */
247    public function block( $user ) {
248        if ( $user instanceof Instagram_User ) {
249            $user = $user->getId();
250        }
251        $this->updateRelationship( $user ); 
252        $response = $this->proxy->modifyRelationship( $user, 'block' );
253        foreach( $response as $r => $v ) {
254            $this->relationships[ $user ]->$r = $v;
255        }
256        return true;
257    }
258
259    /**
260     * Unblock user
261     *
262     * @param Instagram_User|string $user Instagram_User object or user id who should be unblocked
263     * @return boolean
264     */
265    public function unblock( $user ) {
266        if ( $user instanceof Instagram_User ) {
267            $user = $user->getId();
268        }
269        $this->updateRelationship( $user ); 
270        $response = $this->proxy->modifyRelationship( $user, 'unblock' );
271        foreach( $response as $r => $v ) {
272            $this->relationships[ $user ]->$r = $v;
273        }
274        return true;
275    }
276
277    /**
278     * Get relationship
279     *
280     * Get the complete relationship to a user
281     *
282     * @param Instagram_User|string $user Instagram_User object or user id to get the relationship details of
283     * @return StdClass
284     */
285    public function getRelationship( $user ) {
286        if ( $user instanceof Instagram_User ) {
287            $user = $user->getId();
288        }
289        $this->updateRelationship( $user );
290        return $this->relationships[ $user ];
291    }
292
293    /**
294     * Get follow request
295     *
296     * Get the users that have requested to follow the current user
297     *
298     * @return Instagram_Collection_UserCollection
299     */
300    public function getFollowRequests() {
301        return new Instagram_Collection_UserCollection( $this->proxy->getFollowRequests(), $this->proxy );
302    }
303
304    /**
305     * Check following status
306     *
307     * Check if hte current user is following a user
308     *
309     * @param \Instagram\User|string $user User object or user id to check the following status of
310     * @return boolean
311     */
312    public function isBlocking( $user ) {
313        if ( $user instanceof Instagram_User ) {
314            $user = $user->getId();
315        }
316        return $this->getRelationship( $user )->incoming_status == 'blocked_by_you';
317    }
318
319    /**
320     * Check following status
321     *
322     * Check if hte current Instagram_User is following a Instagram_User
323     *
324     * @param Instagram_User|string $user Instagram_User object or user id to check the following status of
325     * @return boolean
326     */
327    public function isFollowing( $user ) {
328        if ( $user instanceof Instagram_User ) {
329            $user = $user->getId();
330        }
331        return $this->getRelationship( $user )->outgoing_status == 'follows';
332    }
333
334    /**
335     * Check followed by status
336     *
337     * Check if the current user is followed by a user
338     *
339     * @param Instagram_User|string $user Instagram_User object or user id to check the followed by status of
340     * @return boolean
341     */
342    public function isFollowedBy( $user ) {
343        if ( $user instanceof Instagram_User ) {
344            $user = $user->getId();
345        }
346        return $this->getRelationship( $user )->incoming_status == 'followed_by';
347    }
348
349    /**
350     * Get the current user's feed
351     *
352     * This can be paginated with the next_max_id param obtained from Instagram_Collection_MediaCollection->getNext()
353     *
354     * @param array $params Optional params to pass to the endpoint
355     * @return Instagram_Collection_MediaCollection
356     * @access public
357     */
358    public function getFeed( array $params = null ) {
359        return new Instagram_Collection_MediaCollection( $this->proxy->getFeed( $params ), $this->proxy );
360    }
361
362    /**
363     * Get the current user's liked media
364     *
365     * This can be paginated with the next_max_like_id param obtained from Instagram_Collection_MediaCollection->getNext()
366     *
367     * @param array $params Optional params to pass to the endpoint
368     * @return Instagram_Collection_MediaCollection
369     * @access public
370     */
371    public function getLikedMedia( array $params = null ) {
372        return  new Instagram_Collection_LikedMediaCollection( $this->proxy->getLikedMedia( $params ), $this->proxy );
373    }
374
375}
376?>
Note: See TracBrowser for help on using the repository browser.