Update Koneko integration

This commit is contained in:
2025-06-24 09:23:51 -06:00
parent 46cb20ff09
commit 46baa99c0c
3271 changed files with 193256 additions and 4017 deletions

View File

@ -0,0 +1,93 @@
<?php
/**
* @author Albert Kozlowski <vojant@gmail.com>
* @license MIT License
* @link https://github.com/vojant/Twitter-php
*/
namespace TwitterPhp;
use \TwitterPhp\Connection\Application;
use \TwitterPhp\Connection\User;
require_once 'connection/ConnectionAbstract.php';
require_once 'connection/Application.php';
require_once 'connection/User.php';
/**
* Class TwitterRestApiException
*/
class RestApiException extends \Exception {};
/**
* Class RestApi
* @package TwitterPhp
*/
class RestApi
{
/**
* @var string
*/
private $_consumerKey;
/**
* @var string
*/
private $_consumerSecret;
/**
* @var string
*/
private $_accessToken;
/**
* @var string
*/
private $_accessTokenSecret;
/**
* @param string $consumerKey
* @param string $consumerSecret
* @param null|string $accessToken
* @param null|string $accessTokenSecret
* @throws TwitterRestApiException
*/
public function __construct($consumerKey,$consumerSecret,$accessToken = null,$accessTokenSecret = null)
{
if (!function_exists('curl_init')) {
throw new TwitterRestApiException('You must have the cURL extension enabled to use this library');
}
$this->_consumerKey = $consumerKey;
$this->_consumerSecret = $consumerSecret;
$this->_accessToken = $accessToken;
$this->_accessTokenSecret = $accessTokenSecret;
}
/**
* Connect to Twitter API as application.
* @link https://dev.twitter.com/docs/auth/application-only-auth
*
* @return \TwitterPhp\Connection\Application
*/
public function connectAsApplication()
{
return new Application($this->_consumerKey,$this->_consumerSecret);
}
/**
* Connect to Twitter API as user.
* @link https://dev.twitter.com/docs/auth/oauth/single-user-with-examples
*
* @return \TwitterPhp\Connection\User
* @throws TwitterRestApiException
*/
public function connectAsUser()
{
if (!$this->_accessToken || !$this->_accessTokenSecret) {
throw new TwitterRestApiException('Missing ACCESS_TOKEN OR ACCESS_TOKEN_SECRET');
}
return new User($this->_consumerKey,$this->_consumerSecret,$this->_accessToken,$this->_accessTokenSecret);
}
}

View File

@ -0,0 +1,122 @@
<?php
include 'RestApi.php';
/**
* Twitter
*
* with help of the API this class delivers all kind of tweeted images from twitter
*
* @package socialstreams
* @subpackage socialstreams/twitter
* @author ThemePunch <info@themepunch.com>
*/
class TP_twitter {
/**
* Consumer Key
*
* @since 1.0.0
* @access private
* @var string $consumer_key Consumer Key
*/
private $consumer_key;
/**
* Consumer Secret
*
* @since 1.0.0
* @access private
* @var string $consumer_secret Consumer Secret
*/
private $consumer_secret;
/**
* Access Token
*
* @since 1.0.0
* @access private
* @var string $access_token Access Token
*/
private $access_token;
/**
* Access Token Secret
*
* @since 1.0.0
* @access private
* @var string $access_token_secret Access Token Secret
*/
private $access_token_secret;
/**
* Initialize the class and set its properties.
*
* @since 1.0.0
* @param string $api_key flickr API key.
*/
public function __construct($consumer_key,$consumer_secret,$access_token,$access_token_secret) {
$this->consumer_key = $consumer_key;
$this->consumer_secret = $consumer_secret;
$this->access_token = $access_token;
$this->access_token_secret = $access_token_secret;
}
/**
* Get Tweets
*
* @since 1.0.0
* @param string $twitter_account Twitter account without trailing @ char
*/
public function get_public_photos($twitter_account){
$twitter = new \TwitterPhp\RestApi($this->consumer_key,$this->consumer_secret,$this->access_token,$this->access_token_secret);
/*
* Connect as application
* https://dev.twitter.com/docs/auth/application-only-auth
*/
$connection = $twitter->connectAsApplication();
/*
* Collection of the most recent Tweets posted by the user indicated by the screen_name, without replies
* https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline
*/
$tweets = $connection->get('/statuses/user_timeline',array('screen_name' => $twitter_account, 'entities' => 1, 'trim_user' => 0 , 'exclude_replies' => 'true'));
//var_dump($tweets);
return $tweets;
}
/**
* Find Key in array and return value (multidim array possible)
*
* @since 1.0.0
* @param string $key Needle
* @param array $form Haystack
*/
public static function array_find_element_by_key($key, $form) {
if (array_key_exists($key, $form)) {
$ret =& $form[$key];
return $ret;
}
foreach ($form as $k => $v) {
if (is_array($v)) {
$ret =TP_twitter::array_find_element_by_key($key, $form[$k]);
if ($ret) {
return $ret;
}
}
}
return FALSE;
}
/**
* Prepare output array $stream
*
* @since 1.0.0
* @param string $tweets Twitter Output Data
*/
public static function makeClickableLinks($s) {
return preg_replace('@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@', '<a href="$1" target="_blank">$1</a>', $s);
}
}
?>

View File

@ -0,0 +1,80 @@
<?php
namespace TwitterPhp\Connection;
use TwitterPhp\RestApiException;
class Application extends Base
{
/**
* @var string
*/
private $_consumerKey;
/**
* @var string
*/
private $_consumerSecret;
/**
* @var string
*/
private $_bearersToken = null;
/**
* @param string $consumerKey
* @param string $consumerSecret
*/
public function __construct($consumerKey,$consumerSecret)
{
$this->_consumerKey = $consumerKey;
$this->_consumerSecret = $consumerSecret;
}
/**
* @param string $url
* @param array $parameters
* @param $method
* @return array
*/
protected function _buildHeaders($url,array $parameters = null,$method)
{
return $headers = array(
"Authorization: Bearer " . $this->_getBearerToken()
);
}
/**
* Get Bearer token
*
* @link https://dev.twitter.com/docs/auth/application-only-auth
*
* @throws \TwitterPhp\RestApiException
* @return string
*/
private function _getBearerToken() {
if (!$this->_bearersToken) {
$token = urlencode($this->_consumerKey) . ':' . urlencode($this->_consumerSecret);
$token = base64_encode($token);
$headers = array(
"Authorization: Basic " . $token
);
$options = array (
CURLOPT_URL => self::TWITTER_API_AUTH_URL,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => "grant_type=client_credentials"
);
$response = $this->_callApi($options);
if (isset($response["token_type"]) && $response["token_type"] == 'bearer') {
$this->_bearersToken = $response["access_token"];
} else {
throw new RestApiException('Error while getting access token');
}
}
return $this->_bearersToken;
}
}

View File

@ -0,0 +1,120 @@
<?php
namespace TwitterPhp\Connection;
/**
* Class Base
* @package TwitterPhp
* @subpackage Connection
*/
abstract class Base
{
/**
* Url for Twitter api
*/
const TWITTER_API_URL = 'https://api.twitter.com';
/**
* Twitter URL that authenticates bearer tokens
*/
const TWITTER_API_AUTH_URL = 'https://api.twitter.com/oauth2/token/';
/**
* Version of Twitter api
*/
const TWITTER_API_VERSION = '1.1';
/**
* Timeout value for curl connections
*/
const DEFAULT_TIMEOUT = 10;
/**
* METHOD GET
*/
const METHOD_GET = 'GET';
/**
* METHOD POST
*/
const METHOD_POST = 'POST';
/**
* @param string $url
* @param array $parameters
* @param $method
* @return array
*/
abstract protected function _buildHeaders($url,array $parameters = null,$method);
/**
* Do GET request to Twitter api
*
* @link https://dev.twitter.com/docs/api/1.1
*
* @param $resource
* @param array $parameters
* @return mixed
*/
public function get($resource, array $parameters = array())
{
$url = $this->_prepareUrl($resource);
$headers = $this->_buildHeaders($url,$parameters,self::METHOD_GET);
$url = $url . '?' . http_build_query($parameters);
$curlParams = array (
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => $headers
);
return $this->_callApi($curlParams);
}
/**
* Do POST request to Twitter api
*
* @link https://dev.twitter.com/docs/api/1.1
*
* @param $resource
* @param array $parameters
* @return mixed
*/
public function post($resource, array $parameters = array())
{
$url = $this->_prepareUrl($resource);
$headers = $this->_buildHeaders($url,$parameters,self::METHOD_POST);
$curlParams = array (
CURLOPT_URL => $url,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $parameters,
CURLOPT_HTTPHEADER => $headers
);
return $this->_callApi($curlParams);
}
/**
* Call Twitter api
*
* @param array $params
* @return array
*/
protected function _callApi(array $params)
{
$curl = curl_init();
curl_setopt_array($curl,$params);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, self::DEFAULT_TIMEOUT);
$response = curl_exec($curl);
return json_decode($response,true);
}
/**
* @param string $resource
* @return string
*/
private function _prepareUrl($resource)
{
return self::TWITTER_API_URL . '/' . self::TWITTER_API_VERSION . '/' . ltrim($resource,'/') . '.json';
}
}

View File

@ -0,0 +1,92 @@
<?php
namespace TwitterPhp\Connection;
class User extends Base
{
/**
* @var string
*/
private $_consumerKey;
/**
* @var string
*/
private $_consumerSecret;
/**
* @var string
*/
private $_accessToken;
/**
* @var string
*/
private $_accessTokenSecret;
/**
* @param string $consumerKey
* @param string $consumerSecret
* @param string $accessToken
* @param string $accessTokenSecret
*/
public function __construct($consumerKey,$consumerSecret,$accessToken,$accessTokenSecret)
{
$this->_consumerKey = $consumerKey;
$this->_consumerSecret = $consumerSecret;
$this->_accessToken = $accessToken;
$this->_accessTokenSecret = $accessTokenSecret;
}
/**
* @param string $url
* @param array $parameters
* @param $method
* @return array
*/
protected function _buildHeaders($url,array $parameters = null,$method)
{
$oauthHeaders = array(
'oauth_version' => '1.0',
'oauth_consumer_key' => $this->_consumerKey,
'oauth_nonce' => time(),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_token' => $this->_accessToken,
'oauth_timestamp' => time()
);
$data = $oauthHeaders;
if ($method == self::METHOD_GET) {
$data = array_merge($oauthHeaders,$parameters);
}
$oauthHeaders['oauth_signature'] = $this->_buildOauthSignature($url,$data,$method);
ksort($oauthHeaders);
$oauthHeader = array();
foreach($oauthHeaders as $key => $value) {
$oauthHeader[] = $key . '="' . rawurlencode($value) . '"';
}
$headers[] = 'Authorization: OAuth ' . implode(', ', $oauthHeader);
return $headers;
}
/**
* @param $url
* @param array $params
* @param $method
* @return string
*/
private function _buildOauthSignature($url,array $params,$method)
{
ksort($params);
$sortedParams = array();
foreach($params as $key=>$value) {
$sortedParams[] = $key . '=' . $value;
}
$signatureBaseString = $method . "&" . rawurlencode($url) . '&' . rawurlencode(implode('&', $sortedParams));
$compositeKey = rawurlencode($this->_consumerSecret) . '&' . rawurlencode($this->_accessTokenSecret);
return base64_encode(hash_hmac('sha1', $signatureBaseString, $compositeKey, true));
}
}