126 lines
3.9 KiB
PHP
126 lines
3.9 KiB
PHP
<?php
|
|
/** \file include\twitter.php
|
|
* \brief DI webinterface twitter interface
|
|
* \author Rob Schalken, Core|Vision
|
|
* \version 1.0
|
|
* \date 17-10-2008
|
|
*
|
|
* This file contains the twitter functionality
|
|
*/
|
|
|
|
/**
|
|
* Retrieve public timeline without authentification
|
|
*
|
|
* Inputs:
|
|
* - user: Twitter user name
|
|
*
|
|
* Return: array (Text/user)
|
|
*/
|
|
function TwitterPublicTimeline($user) {
|
|
$result = "";
|
|
$file = session_save_path() . "/twitter_" . $user;
|
|
|
|
// We use cache to speed up the site
|
|
if ((file_exists($file)) && (filesize($file))) {
|
|
// Open handle
|
|
$handle = fopen($file, "r");
|
|
|
|
// Read file
|
|
$result = unserialize(fread($handle, filesize($file)));
|
|
|
|
// Close handle
|
|
fclose($handle);
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
|
|
/**
|
|
* Retrieve public timeline without authentification and store in cache
|
|
*
|
|
* Inputs:
|
|
* - user: Twitter user name
|
|
* - limit: Max number of tweets
|
|
*
|
|
* Return: nothing
|
|
*/
|
|
function TwitterUpdateCache($user, $limit = 5) {
|
|
$result = "";
|
|
$file = session_save_path() . "/twitter_" . $user;
|
|
|
|
// Update twitter (only when not exists or older then 5 minutes)
|
|
if ((!file_exists($file)) || (filemtime($file) <= (time()-5*60))) {
|
|
require_once("twitteroauth.php");
|
|
|
|
$consumerkey = "CJGpplH745pbLECF99qOg6YEV";
|
|
$consumersecret = "C6PMfEZ4NNE1z4oXrELTn9iFPI1UhGU9wviQ02cpFVN9FdzsJ7";
|
|
$accesstoken = "284679282-eJhOtRUULUuVEVQMeYsHIcWVwWINPFv7PtPddeqy";
|
|
$accesstokensecret = "GAm2iKChM57zLcla4Atx3kwbxxT6p5wn4iWThaxAl8";
|
|
|
|
$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
|
|
$contents = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=" . $user . "&count=" . $limit ."&include_rts=true&contributor_details=true&disable_cache=" . microtime_float());
|
|
|
|
if (is_array($contents)) {
|
|
$contents = object_to_array($contents);
|
|
$result = array();
|
|
|
|
foreach($contents as $item)
|
|
{
|
|
// Default values
|
|
$user_name = $item['user']['name'];
|
|
|
|
// Retweeted?
|
|
if ((isset($item['retweeted_status']['user']['name'])) && (strlen($item['retweeted_status']['user']['name']))) {
|
|
$user_name = $item['retweeted_status']['user']['name'];
|
|
}
|
|
|
|
// Get text
|
|
$stat = $item['text'];
|
|
|
|
// Parse text
|
|
$split = preg_split('/\s/',$stat);
|
|
$line = "";
|
|
|
|
foreach ($split as $word)
|
|
{
|
|
if (preg_match('/^@/',$word)) {
|
|
// Could be an Retweet, removed ':'
|
|
$link = str_replace(":", "", $word);
|
|
|
|
$line .= " "."<a target=\"_blank\" href=\"http://www.twitter.com/".substr($link,1)."\">".$word."</a>";
|
|
}
|
|
else if (preg_match('/^#/',$word)) {
|
|
$line .= " "."<a target=\"_blank\" href=\"http://www.twitter.com/search/realtime?q=".substr($word,1)."\">".$word."</a>";
|
|
}
|
|
else if (preg_match('/^http:\/\//',$word)) {
|
|
$line .= " "."<a target=\"_blank\" href=\"".$word."\">".$word."</a>";
|
|
}
|
|
else {
|
|
$line .= " ".$word;
|
|
}
|
|
}
|
|
|
|
array_push($result, array("text" => $line, "user" => $user_name));
|
|
}
|
|
|
|
if (!empty($result)) {
|
|
// Open handle
|
|
$handle = fopen($file, "w");
|
|
|
|
// Write file
|
|
fwrite($handle, serialize($result));
|
|
|
|
// Close handle
|
|
fclose($handle);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
|
|
$connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
|
|
return $connection;
|
|
}
|
|
?>
|