| 1 | <?php |
|---|
| 2 | # -- BEGIN LICENSE BLOCK ---------------------------------- |
|---|
| 3 | # This file is part of dcLibFoursquare, a plugin for Dotclear 2. |
|---|
| 4 | # |
|---|
| 5 | # Copyright (c) 2009-2011 JC Denis and contributors |
|---|
| 6 | # jcdenis@gdwd.com |
|---|
| 7 | # |
|---|
| 8 | # Licensed under the GPL version 2.0 license. |
|---|
| 9 | # A copy of this license is available in LICENSE file or at |
|---|
| 10 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
|---|
| 11 | # -- END LICENSE BLOCK ------------------------------------ |
|---|
| 12 | |
|---|
| 13 | if (!defined('DC_RC_PATH')){return;} |
|---|
| 14 | |
|---|
| 15 | # Add foursquare to plugin soCialMe (reader part) |
|---|
| 16 | class foursquareSoCialMeReaderService extends soCialMeService |
|---|
| 17 | { |
|---|
| 18 | protected $part = 'reader'; |
|---|
| 19 | |
|---|
| 20 | protected $define = array( |
|---|
| 21 | 'id' => 'foursquare', |
|---|
| 22 | 'name' => 'Foursquare', |
|---|
| 23 | 'home' => 'http://foursquare.com', |
|---|
| 24 | 'icon' => 'pf=dcLibFoursquare/icon.png' |
|---|
| 25 | ); |
|---|
| 26 | |
|---|
| 27 | protected $actions = array( |
|---|
| 28 | 'playServerScript' => true, |
|---|
| 29 | 'playWidgetContent' => true, |
|---|
| 30 | 'playPageContent' => true |
|---|
| 31 | ); |
|---|
| 32 | |
|---|
| 33 | private $oauth = false; |
|---|
| 34 | private $cache_timeout = 300; //5 minutes |
|---|
| 35 | private $chekins_returned = 20; |
|---|
| 36 | |
|---|
| 37 | protected function init() |
|---|
| 38 | { |
|---|
| 39 | # read facebook app settings for admin side |
|---|
| 40 | $oauth_settings = foursquareUtils::decodeApp('admin'); |
|---|
| 41 | |
|---|
| 42 | # Required plugin oAuthManager |
|---|
| 43 | # Used name of parent plugin |
|---|
| 44 | if (!empty($oauth_settings['client_id']) && soCialMeUtils::checkPlugin('oAuthManager','0.2-alpha1')) |
|---|
| 45 | { |
|---|
| 46 | $this->oauth = oAuthClient::load($this->core,'foursquare', |
|---|
| 47 | array( |
|---|
| 48 | 'user_id' => null, |
|---|
| 49 | 'plugin_id' => 'soCialMeReader', |
|---|
| 50 | 'plugin_name' => __('SoCialMe Reader'), |
|---|
| 51 | 'token' => $oauth_settings['client_id'], //app_id |
|---|
| 52 | 'secret' => $oauth_settings['client_secret'] //app_secret |
|---|
| 53 | ) |
|---|
| 54 | ); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | if (false === $this->oauth) |
|---|
| 58 | { |
|---|
| 59 | $this->available = false; |
|---|
| 60 | return false; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | $this->available = true; |
|---|
| 64 | return true; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | public function adminSave($service_id,$admin_url) |
|---|
| 68 | { |
|---|
| 69 | if (!$this->available || $service_id != $this->id) return; |
|---|
| 70 | |
|---|
| 71 | $request_step = !empty($_REQUEST['step']) ? $_REQUEST['step'] : null; |
|---|
| 72 | |
|---|
| 73 | if (!$request_step) |
|---|
| 74 | { |
|---|
| 75 | return; |
|---|
| 76 | } |
|---|
| 77 | elseif ($request_step == 'request') |
|---|
| 78 | { |
|---|
| 79 | $this->oauth->getRequestToken($admin_url.'&step=callback'); |
|---|
| 80 | } |
|---|
| 81 | elseif ($request_step == 'callback' && !empty($_REQUEST['code'])) |
|---|
| 82 | { |
|---|
| 83 | $this->oauth->getAccessToken($admin_url.'&step=callback','json'); |
|---|
| 84 | } |
|---|
| 85 | elseif ($request_step == 'clean') |
|---|
| 86 | { |
|---|
| 87 | $this->oauth->removeToken(); |
|---|
| 88 | } |
|---|
| 89 | return; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | public function adminForm($service_id,$admin_url) |
|---|
| 93 | { |
|---|
| 94 | if (!$this->available) |
|---|
| 95 | { |
|---|
| 96 | $res = '<p>'.sprintf(__('In order to use %s on your blog, a super admin must register an %s app.'),$this->oauth->config('client_name')).'</p>'; |
|---|
| 97 | } |
|---|
| 98 | else |
|---|
| 99 | { |
|---|
| 100 | $admin_url = str_replace('&','&',$admin_url); |
|---|
| 101 | |
|---|
| 102 | $res = '<p>'; |
|---|
| 103 | if ($this->oauth->state() == 1) |
|---|
| 104 | { |
|---|
| 105 | $res .= '<a class="button" href="'.$admin_url.'&step=clean">'.sprintf(__('Something went wrong, clean acces of %s from %s'),$this->oauth->config('plugin_name'),$this->oauth->config('client_name')).'</a>'; |
|---|
| 106 | } |
|---|
| 107 | elseif ($this->oauth->state() == 2) |
|---|
| 108 | { |
|---|
| 109 | $user = $this->oauth->info('name'); |
|---|
| 110 | if ($user) |
|---|
| 111 | { |
|---|
| 112 | $res .= '<p>'.sprintf(__('Your are connected as "%s"'),$user).'</p>'; |
|---|
| 113 | } |
|---|
| 114 | $res .= '<a class="button" href="'.$admin_url.'&step=clean">'.sprintf(__('Disconnet %s from %s'),$this->oauth->config('plugin_name'),$this->oauth->config('client_name')).'</a>'; |
|---|
| 115 | } |
|---|
| 116 | elseif ($this->oauth->state() == 0) |
|---|
| 117 | { |
|---|
| 118 | $res .= '<a class="button" href="'.$admin_url.'&step=request">'.sprintf(__('Connect %s to %s'),$this->oauth->config('plugin_name'),$this->oauth->config('client_name')).'</a>'; |
|---|
| 119 | } |
|---|
| 120 | $res .= '</p>'; |
|---|
| 121 | } |
|---|
| 122 | return $res; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | # Put last user checkins into cache file |
|---|
| 126 | public function playServerScript($available) |
|---|
| 127 | { |
|---|
| 128 | if (!$this->available || $this->oauth->state() != 2) return; |
|---|
| 129 | |
|---|
| 130 | # |
|---|
| 131 | # Cache for user checkins |
|---|
| 132 | # |
|---|
| 133 | |
|---|
| 134 | # cache filename |
|---|
| 135 | $file_user_checkins = $this->core->blog->id.$this->id.'user_checkins'; |
|---|
| 136 | |
|---|
| 137 | # check cache expiry |
|---|
| 138 | if((isset($available['Widget']) && in_array($this->id,$available['Widget']) |
|---|
| 139 | || isset($available['Page']) && in_array($this->id,$available['Page'])) |
|---|
| 140 | && soCialMeCacheFile::expired($file_user_checkins,'enc',$this->cache_timeout)) |
|---|
| 141 | { |
|---|
| 142 | # call API |
|---|
| 143 | $params = array( |
|---|
| 144 | 'limit' => (integer) $this->chekins_returned |
|---|
| 145 | ); |
|---|
| 146 | $rsp = foursquareUtils::api($this->oauth,'users/self/checkins',$params); |
|---|
| 147 | //echo '<pre style="text-align:left;">'.print_r($rsp,true).'</pre>';exit(1); |
|---|
| 148 | if ($rsp && $rsp->checkins->count) |
|---|
| 149 | { |
|---|
| 150 | $rs = $rsp->checkins->items; |
|---|
| 151 | |
|---|
| 152 | # Parse response |
|---|
| 153 | $records = null; |
|---|
| 154 | $i = 0; |
|---|
| 155 | foreach($rs as $record) |
|---|
| 156 | { |
|---|
| 157 | $records[$i]['service'] = $this->id; |
|---|
| 158 | $records[$i]['author'] = $this->oauth->info('name'); |
|---|
| 159 | $records[$i]['source_name'] = $this->name; |
|---|
| 160 | $records[$i]['source_url'] = $this->home; |
|---|
| 161 | $records[$i]['source_icon'] = $this->icon; |
|---|
| 162 | |
|---|
| 163 | $records[$i]['me'] = true; |
|---|
| 164 | $records[$i]['date'] = $record->createdAt; |
|---|
| 165 | $records[$i]['url'] = 'http://foursquare.com/user/'.$this->oauth->info('id').'/checkin/'.$record->id; |
|---|
| 166 | |
|---|
| 167 | if ($record->venue) |
|---|
| 168 | { |
|---|
| 169 | $records[$i]['title'] = sprintf(__('%s went through %s'),$this->oauth->info('name'),$record->venue->name); |
|---|
| 170 | } |
|---|
| 171 | if ($record->shout) |
|---|
| 172 | { |
|---|
| 173 | $records[$i]['content'] = $record->shout; |
|---|
| 174 | } |
|---|
| 175 | elseif ($record->venue && $record->venue->location) |
|---|
| 176 | { |
|---|
| 177 | $content = array(); |
|---|
| 178 | if ($record->venue->location->address) $content[] = $record->venue->location->address; |
|---|
| 179 | if ($record->venue->location->address) $content[] = $record->venue->location->city; |
|---|
| 180 | |
|---|
| 181 | $records[$i]['content'] = implode(', ',$content); |
|---|
| 182 | } |
|---|
| 183 | if ($record->venue && $record->venue->categories) |
|---|
| 184 | { |
|---|
| 185 | $records[$i]['avatar'] = $record->venue->categories[0]->icon; |
|---|
| 186 | $records[$i]['icon'] = $record->venue->categories[0]->icon; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | $i++; |
|---|
| 190 | } |
|---|
| 191 | # Create cache file |
|---|
| 192 | if (!empty($records)) { |
|---|
| 193 | soCialMeCacheFile::write($file_user_checkins,'enc',soCialMeUtils::encode($records)); |
|---|
| 194 | } |
|---|
| 195 | } |
|---|
| 196 | } |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | public function playWidgetContent() |
|---|
| 200 | { |
|---|
| 201 | return self::parseContent(); |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | public function playPageContent() |
|---|
| 205 | { |
|---|
| 206 | return self::parseContent(); |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | private function parseContent() |
|---|
| 210 | { |
|---|
| 211 | if (!$this->available) return; |
|---|
| 212 | # cache filename |
|---|
| 213 | $file = $this->core->blog->id.$this->id.'user_checkins'; |
|---|
| 214 | # Read cache content |
|---|
| 215 | $content = soCialMeCacheFile::read($file,'enc'); |
|---|
| 216 | if (empty($content)) return; |
|---|
| 217 | # Parse content |
|---|
| 218 | return soCialMeUtils::decode($content); |
|---|
| 219 | } |
|---|
| 220 | } |
|---|
| 221 | ?> |
|---|