| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | class TwitterSilo extends Plugin implements MediaSilo |
|---|
| 6 | { |
|---|
| 7 | const SILO_NAME = 'Twitter'; |
|---|
| 8 | |
|---|
| 9 | protected $Twitter; |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | public function info() { |
|---|
| 15 | return array( |
|---|
| 16 | 'name' => 'Twitter Tweet Silo', |
|---|
| 17 | 'version' => '1.0', |
|---|
| 18 | 'url' => 'http://seancoates.com/habari', |
|---|
| 19 | 'author' => 'Sean Coates', |
|---|
| 20 | 'authorurl' => 'http://seancoates.com/', |
|---|
| 21 | 'license' => 'Apache License 2.0', |
|---|
| 22 | 'description' => 'Simple Twitter Silo', |
|---|
| 23 | 'copyright' => '2008', |
|---|
| 24 | ); |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | public function silo_info() { |
|---|
| 33 | return array( 'name' => self::SILO_NAME ); |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | public function silo_dir( $path ) { |
|---|
| 42 | switch ( strtok( $path, '/' ) ) { |
|---|
| 43 | case '': |
|---|
| 44 | return array( |
|---|
| 45 | new MediaAsset(self::SILO_NAME . '/mine/', true), |
|---|
| 46 | new MediaAsset(self::SILO_NAME . '/friends/', true), |
|---|
| 47 | new MediaAsset(self::SILO_NAME . '/custom/', true), |
|---|
| 48 | ); |
|---|
| 49 | break; |
|---|
| 50 | |
|---|
| 51 | case 'custom': |
|---|
| 52 | return array( |
|---|
| 53 | new MediaAsset( |
|---|
| 54 | self::SILO_NAME . '/mine/custom', |
|---|
| 55 | false, |
|---|
| 56 | array( |
|---|
| 57 | 'url' => 'http://twitter.com/home', |
|---|
| 58 | 'filetype' => 'twittertweetcustom', |
|---|
| 59 | ) |
|---|
| 60 | ), |
|---|
| 61 | ); |
|---|
| 62 | break; |
|---|
| 63 | |
|---|
| 64 | case 'mine': |
|---|
| 65 | return $this->get_mine(); |
|---|
| 66 | break; |
|---|
| 67 | |
|---|
| 68 | case 'friends': |
|---|
| 69 | $friend = strtok( '/' ); |
|---|
| 70 | if ( $friend === false ) { |
|---|
| 71 | return $this->get_friends(); |
|---|
| 72 | } else { |
|---|
| 73 | return $this->get_friend_tweets( $friend ); |
|---|
| 74 | } |
|---|
| 75 | break; |
|---|
| 76 | } |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | public function silo_get( $path, $qualities = null ) { |
|---|
| 86 | return MediaAsset('foo', false); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | public function silo_put( $path, $filedata ) {} |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | public function silo_delete( $path ) {} |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | public function silo_highlights() {} |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | public function silo_permissions( $path ) {} |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | public function action_admin_footer( $theme ) { |
|---|
| 118 | |
|---|
| 119 | if ( Controller::get_var( 'page' ) == 'publish' ) { |
|---|
| 120 | ?><script type="text/javascript"> |
|---|
| 121 | function inject_tweet(text, url, user, img) { |
|---|
| 122 | habari.editor.insertSelection('<!-- TWEET --><div class="twitter-tweet"><div class="tweet-text"><a href="' + url + '"><img src="' + img + '" class="tweet-image" /></a>' + text + '</div><div class="tweet-author"><a href="' + url + '">' + user + '</a></div></div><!-- /TWEET -->'); |
|---|
| 123 | } |
|---|
| 124 | //$('.media_controls').css('display', 'none'); |
|---|
| 125 | habari.media.output.twittertweet = {'Insert': function(fileindex, fileobj) { |
|---|
| 126 | inject_tweet(fileobj.tweet_text, fileobj.url, fileobj.tweet_user, fileobj.tweet_user_img); |
|---|
| 127 | }} |
|---|
| 128 | habari.media.preview.twittertweet = function(fileindex, fileobj) { |
|---|
| 129 | return '<div class="mediatitle"><a href="' + fileobj.url + '" target="_new" class="medialink">media</a>' + fileobj.tweet_user_screen_name + '</div>' + fileobj.tweet_text_short; |
|---|
| 130 | } |
|---|
| 131 | habari.media.output.twittertweetcustom = {'Insert': function(fileindex, fileobj) { |
|---|
| 132 | $.get("/auth_ajax/tweetcustom?tweet=" + escape($('#tweetcustom').val()), function( data ){ |
|---|
| 133 | if (data) { |
|---|
| 134 | inject_tweet(data.text, 'http://twitter.com/' + escape(data.user.screen_name) + '/statuses/' + escape(data.id), data.user.screen_name, data.user.profile_image_url); |
|---|
| 135 | } |
|---|
| 136 | }, {}, 'json' ); |
|---|
| 137 | }} |
|---|
| 138 | habari.media.preview.twittertweetcustom = function(fileindex, fileobj) { |
|---|
| 139 | return '<div class="mediatitle">CUSTOM</div>Tweet ID/URL: <input id="tweetcustom" type="text" />'; |
|---|
| 140 | } |
|---|
| 141 | </script><?php |
|---|
| 142 | } |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | public function action_auth_ajax_tweetcustom( $handler ) { |
|---|
| 147 | $tweet = isset( $_GET['tweet'] ) ? $_GET['tweet'] : ''; |
|---|
| 148 | $tweet = preg_replace( '@http://(www\.)?twitter.com/([^/]+)/([^/]+)/([0-9]+)@', '$4', $tweet ); |
|---|
| 149 | if ( ctype_digit( $tweet ) ) { |
|---|
| 150 | $ret = self::twitter_status( $tweet ); |
|---|
| 151 | } else { |
|---|
| 152 | $ret = false; |
|---|
| 153 | } |
|---|
| 154 | echo json_encode( $ret ); |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | public function theme_header() { |
|---|
| 158 | |
|---|
| 159 | return '<link rel="stylesheet" type="text/css" media="screen" href="' |
|---|
| 160 | . $this->get_url( true ) . 'twittersilo.css" />'; |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | protected function get_mine() { |
|---|
| 164 | return $this->to_assets( self::twitter_mine(), 'mine' ); |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | protected function get_friend_tweets ( $id ) { |
|---|
| 168 | return $this->to_assets( self::twitter_friend_tweets( $id ), 'friends' ); |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | protected function get_friends() { |
|---|
| 172 | $friends = array(); |
|---|
| 173 | $friendsObj = self::twitter_friends(); |
|---|
| 174 | foreach ($friendsObj as $friend) { |
|---|
| 175 | $friends[] = new MediaAsset(self::SILO_NAME . '/friends/' . $friend->screen_name, true); |
|---|
| 176 | } |
|---|
| 177 | return $friends; |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | protected function to_assets( $objs, $type ) { |
|---|
| 181 | foreach ($objs as $obj) { |
|---|
| 182 | $tweets[] = new MediaAsset( |
|---|
| 183 | self::SILO_NAME . '/' . $type . '/' . $obj->user->name . '/' . $obj->id, |
|---|
| 184 | false, |
|---|
| 185 | array( |
|---|
| 186 | 'tweet_id' => $obj->id, |
|---|
| 187 | 'url' => 'http://twitter.com/' . $obj->user->screen_name . '/statuses/' . $obj->id, |
|---|
| 188 | 'tweet_user' => (string) $obj->user->name, |
|---|
| 189 | 'tweet_user_img' => (string) $obj->user->profile_image_url, |
|---|
| 190 | 'tweet_user_screen_name' => (string) $obj->user->screen_name, |
|---|
| 191 | 'tweet_text_short' => substr( $obj->text, 0, 20 ) . ( strlen( $obj->text ) > 20 ? '...' : '' ), |
|---|
| 192 | 'tweet_text' => (string) $obj->text, |
|---|
| 193 | 'filetype' => 'twittertweet', |
|---|
| 194 | ) |
|---|
| 195 | ); |
|---|
| 196 | } |
|---|
| 197 | return $tweets; |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | public function filter_plugin_config($actions, $plugin_id) |
|---|
| 209 | { |
|---|
| 210 | $actions[] = 'Configure'; |
|---|
| 211 | return $actions; |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | public function action_plugin_ui($plugin_id, $action) |
|---|
| 221 | { |
|---|
| 222 | if ($plugin_id == $this->plugin_id()){ |
|---|
| 223 | switch ($action){ |
|---|
| 224 | case 'Configure': |
|---|
| 225 | $ui = new FormUI( strtolower( get_class( $this ) ) ); |
|---|
| 226 | $ui->append( 'text', 'twitter_user', 'option:twittersilo__user', _t( 'Twitter Username:' ) ); |
|---|
| 227 | $ui->append( 'password', 'twitter_pass', 'option:twittersilo__pass', _t( 'Twitter Password:' ) ); |
|---|
| 228 | $ui->append('submit', 'save', _t( 'Save' ) ); |
|---|
| 229 | $ui->set_option('success_message', _t('Options saved')); |
|---|
| 230 | $ui->out(); |
|---|
| 231 | break; |
|---|
| 232 | } |
|---|
| 233 | } |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | protected static function twitter_fetch ( $url ) { |
|---|
| 237 | if ( $user = Options::get( 'twittersilo__user' ) ) { |
|---|
| 238 | |
|---|
| 239 | $tweetURL = preg_replace( |
|---|
| 240 | '@^(https?)://@', |
|---|
| 241 | '$1://' . urlencode( $user ) . ':' . Options::get('twittersilo__pass') .'@', |
|---|
| 242 | $url |
|---|
| 243 | ); |
|---|
| 244 | } else { |
|---|
| 245 | $tweetURL = $url; |
|---|
| 246 | } |
|---|
| 247 | if ( $result = @file_get_contents( $tweetURL ) ) { |
|---|
| 248 | return json_decode( $result ); |
|---|
| 249 | } else { |
|---|
| 250 | return false; |
|---|
| 251 | } |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | protected static function twitter_status( $id ) { |
|---|
| 255 | return self::twitter_fetch( 'http://twitter.com/statuses/show/' . ((int)$id) . '.json' ); |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | protected static function twitter_mine( ) { |
|---|
| 259 | return self::twitter_fetch( 'http://twitter.com/statuses/user_timeline.json' ); |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | protected static function twitter_friend_tweets( $id ) { |
|---|
| 263 | return self::twitter_fetch( 'http://twitter.com/statuses/user_timeline/'. urlencode( $id ) .'.json' ); |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | protected static function twitter_friends( ) { |
|---|
| 267 | return self::twitter_fetch( 'http://twitter.com/statuses/friends.json' ); |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | ?> |
|---|