| 29 | | $q= ''; |
| 30 | | $q= "CREATE TABLE " . DB::table('l_source') . "( |
| 31 | | id INTEGER NOT NULL AUTOINCREMENT, |
| 32 | | title VARCHAR(255) NOT NULL, |
| 33 | | profile_url VARCHAR(255) NOT NULL, |
| 34 | | feed_url VARCHAR(255) NOT NULL, |
| 35 | | favicon VARCHAR(255) NOT NULL, |
| 36 | | CREATE UNIQUE INDEX IF NOT EXISTS id ON ". DB::table('l_source') . "(id); |
| 37 | | );"; |
| 38 | | |
| 39 | | $q .= "CREATE TABLE " . DB::table('l_data') . "( |
| | 29 | $q = "CREATE TABLE " . DB::table('l_data') . "( |
| 51 | | $q= ''; |
| 52 | | $q= "CREATE TABLE " . DB::table('l_source') . "( |
| 53 | | id INT UNSIGNED NOT NULL AUTO_INCREMENT, |
| 54 | | title VARCHAR(255) NOT NULL, |
| 55 | | profile_url VARCHAR(255) NOT NULL, |
| 56 | | feed_url VARCHAR(255) NOT NULL, |
| 57 | | favicon VARCHAR(255) NOT NULL, |
| 58 | | UNIQUE KEY id (id) |
| 59 | | );"; |
| 60 | | $q .= "CREATE TABLE " . DB::table('l_data') . "( |
| | 42 | $q = "CREATE TABLE " . DB::table('l_data') . "( |
| | 130 | } |
| | 131 | |
| | 132 | public function insert($entries = array()) { |
| | 133 | foreach($entries as $entry) { |
| | 134 | DB::insert(DB::table('l_data'), $entry); |
| | 135 | } |
| | 136 | } |
| | 137 | |
| | 138 | public function get_entries($type = 'any', $offset = 0, $number = 20) { |
| | 139 | $query= ''; |
| | 140 | $query.= 'SELECT * FROM ' . DB::table('l_data'); |
| | 141 | |
| | 142 | if($type != 'any') { |
| | 143 | $query.= " WHERE name= '$type'"; |
| | 144 | } |
| | 145 | |
| | 146 | $query.= ' ORDER BY date DESC'; |
| | 147 | $query.= ' LIMIT ' . $offset . ', ' . $number; |
| | 148 | $results = DB::get_results( $query ); |
| | 149 | |
| | 150 | return $results; |
| 163 | | |
| 164 | | private function fetch_remote_file( $file ) { |
| 165 | | |
| 166 | | $path = parse_url( $file ); |
| 167 | | |
| 168 | | if ($fs = @fsockopen($path['host'], isset($path['port'])?$path['port']:80)) { |
| 169 | | |
| 170 | | $header = "GET " . $path['path'] . " HTTP/1.0\r\nHost: " . $path['host'] . "\r\n\r\n"; |
| 171 | | |
| 172 | | fwrite($fs, $header); |
| 173 | | |
| 174 | | $buffer = ''; |
| 175 | | |
| 176 | | while ($tmp = fread($fs, 1024)) { $buffer .= $tmp; } |
| 177 | | |
| 178 | | preg_match('/HTTP\/[0-9\.]{1,3} ([0-9]{3})/', $buffer, $http); |
| 179 | | preg_match('/Location: (.*)/', $buffer, $redirect); |
| 180 | | |
| 181 | | if (isset($redirect[1]) && $file != trim($redirect[1])) { return self::fetch_remote_file(trim($redirect[1])); } |
| 182 | | |
| 183 | | if (isset($http[1]) && $http[1] == 200) { return substr($buffer, strpos($buffer, "\r\n\r\n") +4); } else { return false; } |
| 184 | | |
| 185 | | } else { return false; } |
| 186 | | |
| 187 | | } |
| 188 | | |
| 189 | | private function favicache( $feed, $name ) { |
| 190 | | $folder= '/user/plugins/lifestream/images/'; |
| 191 | | |
| 192 | | if ( !is_dir( ABSPATH . $folder ) ) { |
| 193 | | mkdir( ABSPATH . $folder, 0777 ); |
| 194 | | } |
| 195 | | |
| 196 | | $url= parse_url( $feed ); |
| 197 | | |
| 198 | | $cache= self::fetch_remote_file( 'http://' . $url['host'] . '/favicon.ico' ); |
| 199 | | |
| 200 | | if ( !$cache ) { |
| 201 | | preg_match( '/<link.*(?:rel="icon" href="(.*)"|href="(.*)" rel="icon").*>/U', |
| 202 | | self::fetch_remote_file( $_POST['link_url'] ), $matches ); |
| 203 | | $cache= self::fetch_remote_file( $matches[1] ); |
| 204 | | } |
| 205 | | |
| 206 | | if ( $cache ) { |
| 207 | | file_put_contents( HABARI_PATH . '/' . $folder . md5( $url['host'] ) . '.ico', $cache ); |
| 208 | | $icon= get_option( 'siteurl' ) . $folder . md5( $url['host'] ) . '.ico'; |
| 209 | | } elseif( is_file( HABARI_PATH . '/' . 'user/plugins/lifestream/images/icon.gif' ) ) { |
| 210 | | $icon= Site::get_url( 'habari' ) . 'user/plugins/lifestream/images/icon.gif'; |
| 211 | | } |
| 212 | | |
| 213 | | $wpdb->query( 'UPDATE `' . DB::table( 'l_source' ) . '` SET `favicon` = "' . $icon . '" WHERE `title` = "' . $name . '"' ); |
| 214 | | return false; |
| 215 | | } |
| 216 | | |
| 217 | | /** |
| 218 | | * Grab all the sources we have stored in the db. |
| 219 | | * <code> |
| 220 | | * foreach( $streams->collect() as $source ) { |
| 221 | | * echo $source->title; |
| 222 | | * } |
| 223 | | * </code> |
| 224 | | */ |
| 225 | | public function collect() { |
| 226 | | $sources= DB::get_results( "SELECT * FROM " . DB::table('l_data') ); |
| 227 | | if( is_array( $sources ) ) { |
| 228 | | return $sources; |
| 229 | | } else { |
| 230 | | return array(); |
| 231 | | } |
| 232 | | } |
| 233 | | |
| 234 | | private function get_feeds() { |
| 235 | | $this->config= simplexml_load_file( dirname( __FILE__ ) . '/lifestream.config.xml' ); |
| | 175 | |
| | 176 | |
| | 177 | |
| | 178 | public function fetch_feeds() { |
| 243 | | $this->stream_contents[$date]['name']= (string) $name; |
| 244 | | $this->stream_contents[$date]['title']= $entry->get_title(); |
| 245 | | $this->stream_contents[$date]['link']= $entry->get_permalink(); |
| 246 | | $this->stream_contents[$date]['date']= strtotime( substr( $entry->get_date(), 0, 25 ) ); |
| | 186 | $data['name']= (string) $name; |
| | 187 | $data['content']= $entry->get_title(); |
| | 188 | $data['link']= $entry->get_permalink(); |
| | 189 | $data['date']= strtotime( substr( $entry->get_date(), 0, 25 ) ); |
| 256 | | |
| 257 | | public function archive_feeds() { |
| 258 | | foreach( self::get_feeds() as $archive ) { |
| 259 | | if( !DB::exists( DB::table('l_data'), array( 'link' => $archive['link'], 'date' => $archive['date'] ) ) ) { |
| 260 | | $insert= array(); |
| 261 | | $insert['name']= addslashes( $archive['name'] ); |
| 262 | | $insert['content']= addslashes( $archive['title'] ); |
| 263 | | $insert['date']= $archive['date']; |
| 264 | | $insert['link']= $archive['link']; |
| 265 | | $insert['enabled']= 1; |
| 266 | | return DB::insert( DB::table( 'l_data' ), $insert ); |
| 267 | | } |
| 268 | | } |
| 269 | | } |
| 270 | | |
| 271 | | /** |
| 272 | | * Method to grab all of our lifestream data from the DB. |
| 273 | | * <code> |
| 274 | | * foreach( $streams->show_streams() as $stream ) { |
| 275 | | * // do something clever |
| 276 | | * } |
| 277 | | * </code> |
| 278 | | */ |
| 279 | | public function show_streams() { |
| 280 | | $show= DB::get_results( "SELECT * FROM " . DB::table( 'l_data' ) . " WHERE enabled = 1 ORDER BY date DESC" ); |
| 281 | | return $show; |
| 282 | | } |
| 283 | | |
| 284 | | public function source( $name ) { |
| 285 | | $which= DB::get_results( "SELECT profile_url, favicon FROM " . DB::table( 'l_source' ) ." WHERE title = '$name'" ); |
| 286 | | return $which[0]; |
| 287 | | } |
| 288 | | |
| 289 | | public function legend_types() { |
| 290 | | $types= DB::get_results( "SELECT * FROM " . DB::table( 'l_source' ) ." ORDER BY title" ); |
| 291 | | return $types; |
| 292 | | } |
| | 206 | |