Show
Ignore:
Timestamp:
08/13/08 19:20:57 (5 months ago)
Author:
arthus
Message:

Started work on rewriting... not anywhere near stable

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • plugins/blogroll/trunk/blogroll.plugin.php

    r639 r744  
    77 * 
    88 * @todo Update wiki docs, and inline code docs 
    9  * @todo Fix css/layout, it's a bit "hacky hacky kluge" right now 
    10  * @todo Create .pot file for translations 
    119 */ 
    12  
    13 require_once "blogs.php"; 
    14 require_once "bloginfo.php"; 
    15 require_once "blog.php"; 
    16 require_once "blogrollopmlhandler.php"; 
    1710 
    1811class Blogroll extends Plugin 
    1912{ 
    20     const VERSION= '0.5-beta'; 
    21     const DB_VERSION= 003; 
     13    const VERSION= '1.0-alpha'; 
    2214 
    2315    public function info() 
     
    3325        ); 
    3426    } 
    35  
    36     public function action_plugin_activation( $file ) 
    37     { 
    38         if ( $file == str_replace( '\\','/', $this->get_file() ) ) { 
    39             DB::register_table( 'blogroll' ); 
    40             DB::register_table( 'bloginfo' ); 
    41             DB::register_table( 'tag2blog' ); 
    42  
    43             if ( ! CronTab::get_cronjob( 'blogroll:update' ) ) { 
    44                 CronTab::add_hourly_cron( 'blogroll:update', 'blogroll_update_cron', 'Updates the blog updated timestamp from weblogs.com' ); 
    45             } 
    46  
    47             Options::set( 'blogroll__db_version', self::DB_VERSION ); 
    48             Options::set( 'blogroll__use_updated', true ); 
    49             Options::set( 'blogroll__max_links', '10' ); 
    50             Options::set( 'blogroll__sort_by', 'id' ); 
    51             Options::set( 'blogroll__direction', 'ASC' ); 
    52             Options::set( 'blogroll__list_title', 'Blogroll' ); 
    53  
    54             if ( $this->install_db_tables() ) { 
    55                 Session::notice( _t( 'Created the Blogroll database tables.', 'blogroll' ) ); 
    56             } 
    57             else { 
    58                 Session::error( _t( 'Could not install Blogroll database tables.', 'blogroll' ) ); 
    59             } 
    60         } 
    61     } 
    62  
    63     public function action_plugin_deactivation( $file ) 
    64     { 
    65         if ( $file == str_replace( '\\','/', $this->get_file() ) ) { 
    66             CronTab::delete_cronjob( 'blogroll:update' ); 
    67             // should we remove the tables here? 
    68         } 
    69     } 
    70  
    71     public function install_db_tables() 
    72     { 
    73         switch ( DB::get_driver_name() ) { 
    74             case 'mysql': 
    75                 $schema= "CREATE TABLE " . DB::table('blogroll') . " ( 
    76                 id INT UNSIGNED NOT NULL AUTO_INCREMENT, 
    77                 name VARCHAR(255) NOT NULL, 
    78                 url VARCHAR(255) NOT NULL, 
    79                 feed VARCHAR(255) NOT NULL, 
    80                 owner VARCHAR(255) NOT NULL, 
    81                 updated VARCHAR(12) NOT NULL, 
    82                 rel VARCHAR(255) NOT NULL, 
    83                 description TEXT, 
    84                 UNIQUE KEY id (id) 
    85                 ); 
    86                 CREATE TABLE " . DB::table('bloginfo') . " ( 
    87                 blog_id INT UNSIGNED NOT NULL, 
    88                 name VARCHAR(255) NOT NULL, 
    89                 type SMALLINT UNSIGNED NOT NULL DEFAULT 0, 
    90                 value TEXT, 
    91                 PRIMARY KEY (blog_id,name) 
    92                 ); 
    93                 CREATE TABLE  " . DB::table('tag2blog') . " ( 
    94                 tag_id INT UNSIGNED NOT NULL, 
    95                 blog_id INT UNSIGNED NOT NULL, 
    96                 PRIMARY KEY (tag_id,blog_id), 
    97                 KEY blog_id (blog_id) 
    98                 );"; 
    99                 break; 
    100             case 'sqlite': 
    101                 $schema= "CREATE TABLE " . DB::table('blogroll') . " ( 
    102                 id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 
    103                 name VARCHAR(255) NOT NULL, 
    104                 url VARCHAR(255) NOT NULL, 
    105                 feed VARCHAR(255) NOT NULL, 
    106                 owner VARCHAR(255) NOT NULL, 
    107                 updated VARCHAR(12) NOT NULL, 
    108                 rel VARCHAR(255) NOT NULL, 
    109                 description TEXT 
    110                 ); 
    111                 CREATE TABLE " . DB::table('bloginfo') . " ( 
    112                 blog_id INTEGER UNSIGNED NOT NULL, 
    113                 name VARCHAR(255) NOT NULL, 
    114                 type SMALLINT UNSIGNED NOT NULL DEFAULT 0, 
    115                 value TEXT, 
    116                 PRIMARY KEY (blog_id, name) 
    117                 ); 
    118                 CREATE TABLE " . DB::table('tag2blog') . " ( 
    119                 tag_id INTEGER UNSIGNED NOT NULL, 
    120                 blog_id INTEGER UNSIGNED NOT NULL, 
    121                 PRIMARY KEY (tag_id, blog_id) 
    122                 ); 
    123                 CREATE INDEX IF NOT EXISTS tag2blog_blog_id ON " . DB::table('tag2blog') . "(blog_id);"; 
    124                 break; 
    125         } 
    126         return DB::dbdelta( $schema ); 
    127     } 
    128  
    129     public function action_update_check() 
    130     { 
    131         Update::add( 'blogroll', '0420cf10-db83-11dc-95ff-0800200c9a66',  $this->info->version ); 
    132     } 
    133  
    134     public function action_init() 
    135     { 
    136         DB::register_table( 'blogroll' ); 
    137         DB::register_table( 'bloginfo' ); 
    138         DB::register_table( 'tag2blog' ); 
    139  
    140         if ( Options::get( 'blogroll__db_version' ) && self::DB_VERSION > Options::get( 'blogroll__db_version' ) ) { 
    141             $this->install_db_tables(); 
    142             EventLog::log( 'Updated Blogroll.' ); 
    143             Options::set( 'blogroll__db_version', self::DB_VERSION ); 
    144         } 
    145     } 
    146  
    147     public function filter_plugin_config( $actions, $plugin_id ) 
    148     { 
    149  
    150         if ( $this->plugin_id() == $plugin_id ){ 
    151             $actions[]= _t( 'Configure', 'blogroll' ); 
    152         } 
    153         return $actions; 
    154     } 
    155  
    156     public function action_plugin_ui( $plugin_id, $action ) 
    157     { 
    158         if ( $this->plugin_id() == $plugin_id ) { 
    159             switch ( $action ) { 
    160                 case _t( 'Configure', 'blogroll' ): 
    161                     $form= new FormUI( 'blogroll' ); 
    162  
    163                     $title= $form->append( 'text', 'list_title', 'option:blogroll__list_title', _t( 'List title: ', 'blogroll' ) ); 
    164  
    165                     $max= $form->append( 'text', 'max_links', 'option:blogroll__max_links', _t( 'Max. displayed links: ', 'blogroll') ); 
    166  
    167                     $sort_bys= array_merge( 
    168                         array_combine( array_keys( Blog::default_fields() ), array_map( 'ucwords', array_keys( Blog::default_fields() ) ) ), 
    169                         array( 'random' => _t('Randomly', 'blogroll') ) 
    170                         ); 
    171                     $sortby= $form->append( 'select', 'sort_by', 'option:blogroll__sort_by', _t( 'Sort By: ', 'blogroll'), $sort_bys ); 
    172  
    173                     $orders= array( 'ASC' => _t('Ascending' ,'blogroll'), 'DESC' => _t('Descending' ,'blogroll') ); 
    174                     $order= $form->append( 'select', 'direction', 'option:blogroll__direction', _t( 'Order: ', 'blogroll'), $orders ); 
    175  
    176                     $update= $form->append( 'checkbox', 'use_update', 'option:blogroll__use_update', _t( 'Use Weblogs.com to get updates? ', 'blogroll') ); 
    177  
    178                     $form->append( 'submit', 'save', 'Save' ); 
    179                     $form->out(); 
    180                     break; 
    181             } 
    182         } 
    183     } 
    184  
     27     
     28    public function action_init() { 
     29        Post::add_new_type('link'); 
     30        $this->add_template('blogroll_manage', dirname(__FILE__) . '/templates/blogroll_manage.php'); 
     31    } 
     32     
    18533    public function filter_adminhandler_post_loadplugins_main_menu( $menus ) 
    18634    { 
    18735        $menus['blogroll_manage'] =  array( 'url' => URL::get( 'admin', 'page=blogroll_manage'), 'title' => _t('Manage Blogroll'), 'text' => _t('Manage Blogroll'), 'selected' => false, 'hotkey' => 'B' ); 
    188         $menus['blogroll_publish'] =  array( 'url' => URL::get( 'admin', 'page=blogroll_publish'), 'title' => _t('Publish Blogroll'), 'text' => _t('Publish Blogroll'), 'selected' => false ); 
    18936        return $menus; 
    19037    } 
    191  
    192     public function action_admin_theme_post_blogroll_manage( $handler, $theme ) 
    193     { 
    194         extract( $handler->handler_vars ); 
    195  
    196         if ( isset( $change ) && isset( $blog_ids ) ) { 
    197             $count= count( $blog_ids ); 
    198             $blog_ids= (array) $blog_ids; 
    199  
    200             switch ( $change ) { 
    201                 case 'delete': 
    202                     foreach ( $blog_ids as $blog_id ) { 
    203                         $blog= Blog::get( $blog_id ); 
    204                         $blog->delete(); 
    205                     } 
    206                     Session::notice( sprintf( _n('Deleted %d blog', 'Deleted %d blogs', $count, 'blogroll'), $count ) ); 
     38     
     39    public function action_admin_theme_get_blogroll_manage( $handler, $theme ) { 
     40        $theme->admin_page= 'Manage Blogroll'; 
     41        $theme->admin_title= 'Manage Blogroll'; 
     42         
     43        $form= new FormUI('blogroll'); 
     44        $form->class[]= 'create'; 
     45         
     46        $tabs= $form->append('tabs', 'opml_tabs'); 
     47         
     48        $opml= $tabs->append('fieldset', 'opmltab', _t('Import/Export OPML')); 
     49         
     50        $theme->form= $form; 
     51         
     52    } 
     53     
     54    public function action_publish_post($post, $form) { 
     55        if($post->content_type != Post::type('link')) return; 
     56         
     57        $this->action_form_publish($form, $post); 
     58                 
     59        if($form->quick_url->value != '') { 
     60            $data= $this->get_info_from_url($form->quick_url->value); 
     61             
     62            $post->title= $data['name']; 
     63            $post->info->url= $data['url']; 
     64            $post->content= $data['description']; 
     65            $post->info->feedurl= $data['feed']; 
     66            $post->slug= Utils::slugify($data['name']); 
     67             
     68        } else { 
     69            $post->info->url= $form->url->value; 
     70            $post->info->feedurl= $form->feedurl->value; 
     71            $post->info->ownername= $form->ownername->value; 
     72            $post->info->relationship= $form->relationship->value; 
     73        }    
     74 
     75        // exit(); 
     76    } 
     77     
     78    public function action_form_publish($form, $post) {  
     79         
     80        if($post->content_type != Post::type('link')) return; 
     81         
     82        // Quick link button to automagically discover info 
     83        $quicklink_controls= $form->append('tabs', 'quicklink_controls'); 
     84         
     85        $quicklink_tab= $quicklink_controls->append('fieldset', 'quicklink_tab', _t('Quick Link')); 
     86        $quicklink_wrapper= $quicklink_tab->append('wrapper', 'quicklink_wrapper'); 
     87        $quicklink_wrapper->class='container'; 
     88         
     89        $quicklink_wrapper->append('text', 'quick_url', 'null:null', _t('Quick URL'), 'tabcontrol_text'); 
     90        $quicklink_wrapper->append('static', 'quick_url_info', '<p class="column span-15">Enter a url or feed url and other information will be automatically discovered.</p>'); 
     91        $quicklink_wrapper->append('submit', 'addquick', _t('Add'), 'admincontrol_submit'); 
     92         
     93        $quicklink_controls->move_before($quicklink_controls, $form); 
     94         
     95        // Remove fields we don't need 
     96        $form->silos->remove(); 
     97        $form->comments_enabled->remove(); 
     98        $form->newslug->remove(); 
     99        if($form->post_permalink != NULL) $form->post_permalink->remove(); 
     100 
     101        // Add the url field 
     102        $form->append('text', 'url', 'null:null', _t('URL'), 'admincontrol_text'); 
     103        $form->url->class= 'important'; 
     104        $form->url->tabindex = 2; 
     105        $form->url->value = $post->info->url; 
     106        $form->url->move_after($form->title); 
     107         
     108        // Retitle fields 
     109        $form->title->caption= _t('Blog Name'); 
     110        $form->content->caption= _t('Description'); 
     111        $form->content->tabindex= 3; 
     112        $form->tags->tabindex= 4; 
     113         
     114        // Create the extras splitter & fields 
     115        $extras = $form->settings; 
     116 
     117        $extras->append('text', 'feedurl', 'null:null', _t('Feed URL'), 'tabcontrol_text'); 
     118        $extras->feedurl->value = $post->info->feedurl; 
     119         
     120        $extras->append('text', 'ownername', 'null:null', _t('Owner Name'), 'tabcontrol_text'); 
     121        $extras->ownername->value = $post->info->ownername; 
     122         
     123        $extras->append('select', 'relationship', 'null:null', _t('Relationship'), $this->get_relationships(), 'tabcontrol_select'); 
     124        $extras->relationship->value = $post->info->relationship; 
     125 
     126    } 
     127     
     128    public static function get_info_from_url( $url ) 
     129    {        
     130        $info= array(); 
     131        $data= RemoteRequest::get_contents( $url ); 
     132        $feed= self::get_feed_location( $data, $url ); 
     133         
     134        if ( $feed ) { 
     135            $info['feed']= $feed; 
     136            $data= RemoteRequest::get_contents( $feed ); 
     137        } 
     138        else { 
     139            $info['feed']= $url; 
     140        } 
     141        // try and parse the xml 
     142        try { 
     143            $xml= new SimpleXMLElement( $data ); 
     144            switch ( $xml->getName() ) { 
     145                case 'RDF': 
     146                case 'rss': 
     147                    $info['name']= (string) $xml->channel->title; 
     148                    $info['url']= (string) $xml->channel->link; 
     149                    if ( (string) $xml->channel->description ) $info['description']= (string) $xml->channel->description; 
    207150                    break; 
    208                 case 'auto_update': 
    209                     foreach ( $blog_ids as $blog_id ) { 
    210                         $blog= Blog::get( $blog_id ); 
    211                         if ( $info= Blogs::get_info_from_url( $blog->feed?$blog->feed:$blog->url ) ) { 
    212                             foreach ( $info as $key => $value ) { 
    213                                 $value= trim( $value ); 
    214                                 if ( $value ) { 
    215                                     $blog->$key= $value; 
    216                                 } 
    217                             } 
    218                             $blog->update(); 
    219                         } 
    220                         else { 
    221                             Session::error( sprintf( _t('Could not fetch info for %s', 'blogroll'), $blog->name ) ); 
    222                             $count--; 
     151                case 'feed': 
     152                    $info['name']= (string) $xml->title; 
     153                    if ( (string) $xml->subtitle ) $info['description']= (string) $xml->subtitle; 
     154                    foreach ( $xml->link as $link ) { 
     155                        $atts= $link->attributes(); 
     156                        if ( $atts['rel'] == 'alternate' ) { 
     157                            $info['url']= (string) $atts['href']; 
     158                            break; 
    223159                        } 
    224160                    } 
    225                     Session::notice( sprintf( _n('Automatically updated %d blog', 'Automatically updated %d blogs', $count, 'blogroll'), $count ) ); 
    226161                    break; 
    227162            } 
    228163        } 
    229         elseif ( !empty( $opml_file ) || ( isset( $_FILES['userfile'] ) && is_uploaded_file( $_FILES['userfile']['tmp_name'] ) ) ) { 
    230             $file= !empty( $opml_file ) ? RemoteRequest::get_contents( $opml_file ) : file_get_contents( $_FILES['userfile']['tmp_name'] ); 
    231             try { 
    232                 $xml= new SimpleXMLElement( $file ); 
    233                 $count= $this->import_opml( $xml->body ); 
    234                 Session::notice( sprintf( _n('Imported %d blog from %s', 'Imported %d blogs from %s', $count, 'blogroll'), $count, (string) $xml->head->title ) ); 
     164        catch ( Exception $e ) { 
     165            return array(); 
     166        } 
     167        return $info; 
     168    } 
     169     
     170    public static function get_feed_location( $html, $url ) 
     171    { 
     172        preg_match_all( '/<link\s+(.*?)\s*\/?>/si', $html, $matches ); 
     173        $links= $matches[1]; 
     174        $final_links= array(); 
     175        $href= ''; 
     176        $link_count= count( $links ); 
     177        for( $n= 0; $n < $link_count; $n++ ) { 
     178            $attributes= preg_split('/\s+/s', $links[$n]); 
     179            foreach ( $attributes as $attribute ) { 
     180                $att= preg_split( '/\s*=\s*/s', $attribute, 2 ); 
     181                if ( isset( $att[1] ) ) { 
     182                    $att[1]= preg_replace( '/([\'"]?)(.*)\1/', '$2', $att[1] ); 
     183                    $final_link[strtolower( $att[0] )]= $att[1]; 
     184                } 
    235185            } 
    236             catch ( Exception $e ) { 
    237                 Session::error( _t('Sorry, could not parse that OPML file. It may be malformed.', 'blogroll') ); 
     186            $final_links[$n]= $final_link; 
     187        } 
     188        for ( $n= 0; $n < $link_count; $n++ ) { 
     189            if ( isset($final_links[$n]['rel']) && strtolower( $final_links[$n]['rel'] ) == 'alternate' ) { 
     190                if ( isset($final_links[$n]['type']) && in_array( strtolower( $final_links[$n]['type'] ), array( 'application/rss+xml', 'application/atom+xml', 'text/xml' ) ) ) { 
     191                    $href= $final_links[$n]['href']; 
     192                } 
     193                if ( $href ) { 
     194                    if ( strstr( $href, "http://" ) !== false ) { 
     195                        $full_url= $href; 
     196                    } 
     197                    else { 
     198                        $url_parts= parse_url( $url ); 
     199                        $full_url= "http://$url_parts[host]"; 
     200                        if ( isset( $url_parts['port'] ) ) { 
     201                            $full_url.= ":$url_parts[port]"; 
     202                        } 
     203                        if ( $href{0} != '/' ) { 
     204                            $full_url.= dirname( $url_parts['path'] ); 
     205                            if ( substr( $full_url, -1 ) != '/' ) { 
     206                                $full_url.= '/'; 
     207                            } 
     208                        } 
     209                        $full_url.= $href; 
     210                    } 
     211                    return $full_url; 
     212                } 
    238213            } 
    239214        } 
    240  
    241         Utils::redirect( URL::get( 'admin', 'page=blogroll_manage' ) ); 
    242         exit; 
    243     } 
    244  
    245     public function action_admin_theme_post_blogroll_publish( $handler, $theme ) 
    246     { 
    247         $params= array_intersect_key( $handler->handler_vars, array_flip( array('name', 'url', 'feed', 'description', 'owner', 'tags') ) ); 
    248         extract( $handler->handler_vars ); 
    249  
    250         if ( !empty( $quick_link ) ) { 
    251             $link= $quick_link; 
    252             if ( strpos( $quick_link, 'http://' ) !== 0 ) { 
    253                 $quick_link= 'http://' . $quick_link; 
    254             } 
    255             if ( $info= Blogs::get_info_from_url( $quick_link ) ) { 
    256                 $params= array_merge( $params, $info ); 
    257             } 
    258             else { 
    259                 $_POST['url']= $quick_link; 
    260                 $_POST['feed']= $quick_link; 
    261                 Session::add_to_set( 'last_form_data', $_POST, 'get' ); 
    262                 Session::error( sprintf( _t('Could not fetch info from %s. Please enter the information manually.', 'blogroll'), $quick_link ) ); 
    263                 Utils::redirect( URL::get( 'admin', 'page=blogroll_publish' ) ); 
    264                 exit; 
    265             } 
    266         } 
    267  
    268         if ( ( empty( $params['name'] ) || empty( $params['url'] ) ) ) { 
    269             Session::error( _t('Blog Name and URL are required feilds.', 'blogroll') ); 
    270             Session::add_to_set( 'last_form_data', $_POST, 'get' ); 
    271         } 
    272         else { 
    273             if ( !empty( $id ) ) { 
    274                 $blog= Blog::get( $id ); 
    275                 foreach ( $params as $key => $value ) { 
    276                     $blog->$key= $value; 
    277                 } 
    278                 $blog->update(); 
    279                 Session::notice( sprintf( _t('Updated blog %s', 'blogroll'), $blog->name ) ); 
    280                 Session::add_to_set( 'last_form_data', array_merge( $_POST, $params ), 'get' ); 
    281             } 
    282             elseif ( $params ) { 
    283                 $blog= new Blog( $params ); 
    284                 if ( $blog->insert() ) { 
    285                     Session::notice( sprintf( _t('Successfully added blog %s', 'blogroll'), $blog->name ) ); 
    286                     $_POST['id']= $blog->id; 
    287                 } 
    288                 else { 
    289                     Session::notice( sprintf( _t( 'Could not add blog %s', 'blogroll'), $blog->name ) ); 
    290                 } 
    291                 Session::add_to_set( 'last_form_data', $_POST, 'get' ); 
    292             } 
    293         } 
    294  
    295         if ( !empty( $quick_link ) && !empty( $redirect_to ) ) { 
    296             $msg= sprintf( _t('Successfully added blog %s. Now going back.', 'blogroll'), htmlspecialchars( $blog->name ) ); 
    297             echo "<html><head></head><body onload=\"alert('$msg');location.href='$redirect_to';\">"; 
    298             Session::messages_out(); 
    299             echo "</body></html>"; 
    300             exit; 
    301         } 
    302  
    303         Utils::redirect( URL::get( 'admin', 'page=blogroll_publish' ) ); 
    304         exit; 
    305     } 
    306  
    307     public function action_admin_theme_get_blogroll_manage( $handler, $theme ) 
    308     { 
    309         Stack::add( 'admin_stylesheet', array( $this->get_url() . '/templates/blogroll.css', 'screen' ) ); 
    310         $theme->feed_icon= $this->get_url() . '/templates/feed.png'; 
    311  
    312         $theme->display( 'blogroll_manage' ); 
    313         exit; 
    314     } 
    315  
    316     public function action_admin_theme_get_blogroll_publish( $handler, $theme ) 
    317     { 
    318         Stack::add( 'admin_stylesheet', array( $this->get_url() . '/templates/blogroll.css', 'screen' ) ); 
    319         extract(  $handler->handler_vars ); 
    320  
    321         if ( !empty( $quick_link_bookmarklet ) ) { 
    322             Session::add_to_set( 'last_form_data', array('quick_link'=>$quick_link_bookmarklet, 'redirect_to'=>$quick_link_bookmarklet), 'post' ); 
    323             Utils::redirect( URL::get( 'admin', 'page=blogroll_publish' ) ); 
    324             exit; 
    325         } 
    326  
    327         if ( !empty( $id ) ) { 
    328             $blog= Blog::get( $id ); 
    329             $theme->tags= htmlspecialchars( Utils::implode_quoted( ',', $blog->tags ) ); 
    330         } 
    331         else { 
    332             $blog= new Blog; 
    333             $theme->tags= ''; 
    334         } 
    335         foreach ( $blog->to_array() as $key => $value ) { 
    336             $theme->$key= $value; 
    337         } 
    338  
    339         $theme->relationships= Plugins::filter( 'blogroll_relationships', array('external'=>'External', 'nofollow'=>'Nofollow', 'bookmark'=>'Bookmark') ); 
    340         $controls= array( 
    341             'Extras' => $theme->fetch( 'blogroll_publish_extras' ), 
    342             'Tags' => $theme->fetch( 'publish_tags' ), 
     215        return false; 
     216    } 
     217     
     218    private function get_relationships() { 
     219        return array( 
     220            'external' => 'External', 
     221            'nofollow' => 'Nofollow', 
     222            'bookmark' => 'Bookmark' 
    343223        ); 
    344         $theme->controls= Plugins::filter( 'blogroll_controls', $controls, $blog ); 
    345         $theme->display( 'blogroll_publish' ); 
    346         exit; 
    347     } 
    348  
    349     public function filter_available_templates( $templates, $class ) { 
    350         $templates= array_merge( $templates, array('blogroll_manage','blogroll_publish','blogroll','blogroll_publish_extras') ); 
    351         return $templates; 
    352     } 
    353  
    354     public function filter_include_template_file( $template_path, $template_name, $class ) 
    355     { 
    356         if ( ! file_exists( $template_path ) ) { 
    357             switch ( $template_name ) { 
    358                 case 'blogroll_manage': 
    359                     return dirname( __FILE__ ) . '/templates/blogroll_manage.php'; 
    360                 case 'blogroll_publish_extras': 
    361                     return dirname( __FILE__ ) . '/templates/blogroll_publish_extras.php'; 
    362                 case 'blogroll_publish': 
    363                     return dirname( __FILE__ ) . '/templates/blogroll_publish.php'; 
    364                 case 'blogroll': 
    365                     return dirname( __FILE__ ) . '/templates/blogroll.php'; 
    366             } 
    367         } 
    368         return $template_path; 
    369     } 
    370  
    371     public function theme_show_blogroll( $theme, $user_params= array() ) 
    372     { 
    373         $theme->blogroll_title= Options::get( 'blogroll__list_title' ); 
    374  
    375         // Build the params array to pass it to the get() method 
    376         $order_by= Options::get( 'blogroll__sort_by' ); 
    377         $direction= Options::get( 'blogroll__direction'); 
    378  
    379         $params= array( 
    380             'limit' => Options::get( 'blogroll__max_links' ), 
    381             'order_by' => $order_by . ' ' . $direction, 
    382         ); 
    383  
    384         $theme->blogs= Blogs::get( $params ); 
    385  
    386         return $theme->fetch( 'blogroll' ); 
    387     } 
    388  
    389     public function filter_blogroll_update_cron( $success ) 
    390     { 
    391         if ( Options::get( 'blogroll__use_updated' ) ) { 
    392             $request= new RemoteRequest( 'http://www.weblogs.com/rssUpdates/changes.xml', 'GET' ); 
    393             $request->add_header( array( 'If-Modified-Since', Options::get('blogroll__last_update') ) ); 
    394             if ( $request->execute() ) { 
    395                 try { 
    396                     $xml= new SimpleXMLElement( $request->get_response_body() ); 
    397                 } 
    398                 catch ( Exception $e ) { 
    399                     // log the failure here! 
    400                 } 
    401                 $atts= $xml->attributes(); 
    402                 $updated= strtotime( (string) $atts['updated'] ); 
    403                 foreach ( $xml->weblog as $weblog ) { 
    404                     $atts= $weblog->attributes(); 
    405                     $match= array(); 
    406                     $match['url']= (string) $atts['url']; 
    407                     $match['feed']= (string) $atts['rssUrl']; 
    408                     $update= $updated - (int) $atts['when']; 
    409                     if ( DB::exists( DB::table( 'blogroll' ), $match ) ) { 
    410                         DB::update( DB::table( 'blogroll' ), array( 'updated' => $update ), $match ); 
    411                     } 
    412                 } 
    413                 Options::set( 'blogroll__last_update', gmdate( 'D, d M Y G:i:s e' ) ); 
    414             } 
    415             return true; 
    416         } 
    417         else { 
    418             return false; 
    419         } 
    420     } 
    421  
    422     public function filter_habminbar( $menu ) 
    423     { 
    424         $menu['blogroll']= array( 'Blogroll', URL::get( 'admin', 'page=blogroll_publish' ) ); 
    425         return $menu; 
    426     } 
    427  
    428     public function filter_rewrite_rules( $rules ) 
    429     { 
    430         $rules[] = new RewriteRule(array( 
    431             'name' => 'blogroll_opml', 
    432             'parse_regex' => '/^blogroll\/opml\/?$/i', 
    433             'build_str' => 'blogroll/opml', 
    434             'handler' => 'BlogrollOPMLHandler', 
    435             'action' => 'blogroll_opml', 
    436             'priority' => 2, 
    437             'rule_class' => RewriteRule::RULE_PLUGIN, 
    438             'is_active' => 1, 
    439             'description' => 'Rewrite for Blogroll OPML feed.' 
    440         )); 
    441         return $rules; 
    442     } 
    443  
    444     private function import_opml( SimpleXMLElement $xml ) 
    445     { 
    446         $count= 0; 
    447         foreach ( $xml->outline as $outline ) { 
    448             $atts= (array) $outline->attributes(); 
    449             $params= $this->map_opml_atts( $atts['@attributes'] ); 
    450             if ( isset( $params['url'] ) && isset( $params['name'] ) ) { 
    451                 $blog= new Blog( $params ); 
    452                 $blog->insert(); 
    453                 $count++; 
    454             } 
    455             if ( $outline->children() ) { 
    456                 $count+= $this->import_opml( $outline ); 
    457             } 
    458         } 
    459         return $count; 
    460     } 
    461  
    462     private function map_opml_atts( $atts ) 
    463     { 
    464         $atts= array_map( 'strval', $atts ); 
    465         $valid_atts= array_intersect_key( $atts, array_flip( array('name', 'url', 'feed', 'description', 'owner', 'updated') ) ); 
    466         foreach ( $atts as $key => $val ) { 
    467             switch ( $key ) { 
    468                 case 'htmlUrl': 
    469                     $valid_atts['url']= $atts['htmlUrl']; 
    470                     break; 
    471                 case 'xmlUrl': 
    472                     $valid_atts['feed']= $atts['xmlUrl']; 
    473                     break; 
    474                 case 'text': 
    475                     $valid_atts['name']= $atts['text']; 
    476                     break; 
    477             } 
    478         } 
    479         return $valid_atts; 
    480     } 
     224    } 
     225 
    481226} 
    482227?>