Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 08-26-2009, 11:52 AM
saltedm8 saltedm8 is offline
 
Join Date: May 2008
Posts: 84
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default switch off and on plugin via php upon condition

I have this script

PHP Code:
<?php
class myTwit{
    var 
$user false// User to show public posts
    
var $cacheFile 'twitcache.txt'// File to save local cache of Twitter update
    
var $cachExpire 300// Seconds that cache is classed as "old"
    
var $myTwitHeader true;
    var 
$postLimit 20// 20 = max
    
var $debug false;
    
    function 
printError($message){
        print 
htmlspecialchars($message);
        exit;
    }
    function 
debugMsg($message){
        if (
$this->debug == true) print htmlspecialchars($message).'<br />';
    }
    function 
formatDays($val$qty){
        if (
$val 1) return $val.' '.$qty.'s ago';
        else return 
$val.' '.$qty.' ago';
    }
    
    function 
intoRelativeTime($seconds){
        if ((
$seconds 60 60 24) > 1) return $this->formatDays(round($seconds 60 60 24), 'day');
        elseif ((
$seconds 60 60) > 1) return 'about '.$this->formatDays(round($seconds 60 60), 'hour');
        else if ((
$seconds 60 ) > 1) return 'about '.$this->formatDays(round($seconds 60), 'minute');
        else return 
'about '.round($seconds).' seconds ago';
    }

    function 
linkURLs($text){
        
$in=array( '`((?:https?|ftp)://\S+[[:alnum:]]/?)`si''`((?<!//)(www\.\S+[[:alnum:]]/?))`si' );
        
$out=array( '<a href="$1" target="_blank">$1</a> ''<a href="http://$1" target="_blank">$1</a>' );
        
$text preg_replace($in,$out,$text);
        
$text preg_replace('/@([a-zA-Z0-9-]+)/','@<a href="http://twitter.com/$1" target="_blank">$1</a>',$text);
        return 
$text;
    }
    
    function 
checkCacheFile(){
        if ( (@
filemtime($this->cacheFile) < (mktime() - $this->cachExpire) ) || (!is_file($this->cacheFile)) ){
            
$this->debugMsg('Cache file outdated');
            
$this->updateCache();
        } else {
            
$this->debugMsg('Cache file still valid');
        }
    }
    
    function 
updateCache(){
        
$uri 'http://twitter.com/statuses/user_timeline/'.$this->user.'.json';
        
$req = new HTTPRequest($uri);
        
$tmpdata $req->DownloadToString();
        
$resp json_decode($tmpdatatrue);
        if (isset(
$resp['error'])) $this->printError('Error getting information from Twitter ['.$resp['error'].']. Please check the username ('.$this->user.')');
        elseif (!
is_array($resp)) $this->printError('Error getting information from Twitter. File is not JSON.');
        
$handle = @fopen($this->cacheFile'w');
        if (!
$handle$this->printError('Could not write to cache file: '.$this->cacheFile.'. Please check read/write permissions.');
        
fwrite($handle$tmpdata);
        
fclose($handle);
        
$this->debugMsg('Updated cache file: '.$this->cacheFile);
    }
    
    function 
readCache(){
        if( 
false == ($this->jsonData = @file_get_contents$this->cacheFile )))
            
$this->printError('Could not read cache file: '.$this->cacheFile);
    }
    
    function 
initMyTwit(){
        if (!
is_string($this->user)) $this->printError('Please set a user.');
        
$this->checkCacheFile();
        
$this->readCache();
        
$this->jsonArray json_decode($this->jsonDatatrue);
        
$output '<ul class="twitbox">';
        if (
$this->myTwitHeader && isset($this->jsonArray[0])){
            
$output .= ' <li class="mytwitHead"><img src="'.$this->jsonArray[0]['user']['profile_image_url'].'" alt="'.$this->user.'" />
            <div><a href="http://twitter.com/'
.$this->user.'" target="_blank">'.$this->user.'</a><br />
            '
.$this->jsonArray[0]['user']['followers_count'].' followers</div>
            </li>'
;
        }        
        for(
$x=0$x count($this->jsonArray) && $x $this->postLimit$x++){
            
$seconds_ago mktime() - strtotime($this->jsonArray[$x]['created_at']);
            
$ts strtotime($this->jsonArray[$x]['created_at'])+$this->jsonArray[$x]['user']['utc_offset'];
            
$cur_ts mktime();
            
$output .= '<li class="twit">'.$this->linkURLs(htmlspecialchars($this->jsonArray[$x]['text'])).
            
' <span class="twhen">by <a href="http://twitter.com/'.$this->jsonArray[$x]['user']['screen_name'].'" target="_blank">'.$this->jsonArray[$x]['user']['screen_name'].'</a> '.
            
$this->intoRelativeTime($seconds_ago)."</span></li>\n";
        }
        
$output .= '</ul>';
        
$this->myTwitData $output;
    }    
}


class 
HTTPRequest{
    var 
$_fp;            // HTTP socket
    
var $_url;        // full URL
    
var $_host;    // HTTP host
    
var $_protocol;    // protocol (HTTP/HTTPS)
    
var $_uri;        // request URI
    
var $_port;    // port
    // scan url
    
function _scan_url(){
            
$req $this->_url;
            
$pos strpos($req'://');
            
$this->_protocol strtolower(substr($req0$pos));
            
$req substr($req$pos+3);
            
$pos strpos($req'/');
            if(
$pos === false)
                    
$pos strlen($req);
            
$host substr($req0$pos);
            if(
strpos($host':') !== false)        {
                    list(
$this->_host$this->_port) = explode(':'$host);
            }else{
                    
$this->_host $host;
                    
$this->_port = ($this->_protocol == 'https') ? 443 80;
            }
            
$this->_uri substr($req$pos);
            if(
$this->_uri == '')
                
$this->_uri '/';
    }
    
// constructor
    
function HTTPRequest($url){
            
$this->_url $url;
            
$this->_scan_url();
    }
    
// download URL to string
    
function DownloadToString(){
        
$crlf "\r\n";
        
// generate request
        
$req 'GET ' $this->_uri ' HTTP/1.0' $crlf
                
.    'Host: ' $this->_host $crlf
                
.       'User-Agent: PHP' $crlf
                
.    $crlf;
        
// fetch
        
$this->_fp = @fsockopen(($this->_protocol == 'https' 'ssl://' '') . $this->_host$this->_port);
        if (
$this->_fp){
            
fwrite($this->_fp$req);
            
$response "";
            while(
is_resource($this->_fp) && $this->_fp && !feof($this->_fp))
                    
$response .= fread($this->_fp1024);
            
fclose($this->_fp);
            
// split header and body
            
$pos strpos($response$crlf $crlf);
            if(
$pos === false)
                    return(
$response);
            
$header substr($response0$pos);
            
$body substr($response$pos strlen($crlf));
            
// parse headers
            
$headers = array();
            
$lines explode($crlf$header);
            foreach(
$lines as $line)
                    if((
$pos strpos($line':')) !== false)
                            
$headers[strtolower(trim(substr($line0$pos)))] = trim(substr($line$pos+1));
            
// redirection?
            
if(isset($headers['location'])){
                    
$http = new HTTPRequest($headers['location']);
                    return(
$http->DownloadToString($http));
            } else{
                    return(
$body);
            }
        } else {
            return 
false;
        }
    }
}
?>
and I am calling it using this

PHP Code:
ob_start();
include(
DIR '/twittercall.php');
$twitter = new myTwit();
$twitter->user 'webprocafe';
$twitter->cacheFile 'twitcache.txt';
$twitter->postLimit 5;
$twitter->initMyTwit();
print 
$twitter->myTwitData;
$latest_twit ob_get_contents();
ob_end_clean(); 
when it cant get the json file for any reason it displays

Quote:
Error getting information from Twitter. File is not JSON.
but unfortunately its making the homepage blank with only that text..

I was wondering if anyone can work out how to make it switch off the plugin instead if it goes down, and then when it comes back up again to switch the plugin on

or.. more preferred would be to make it so it does not make the homepage blank, and only display that message in the section that the feed is sitting in... - it should be under 'latest tweets' on our homepage - link in signature

thank you
Reply With Quote
  #2  
Old 08-30-2009, 09:10 AM
saltedm8 saltedm8 is offline
 
Join Date: May 2008
Posts: 84
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

B.U.M.P

please give me some suggestions, thanks
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 06:35 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.03561 seconds
  • Memory Usage 2,286KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (2)bbcode_php
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (2)post_thanks_box
  • (2)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (2)post_thanks_postbit_info
  • (2)postbit
  • (2)postbit_onlinestatus
  • (2)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete