diff -r 5f9b76ecccf0 -r 7c67caaafdeb web/common.php --- a/web/common.php Thu Dec 02 13:52:58 2021 +0100 +++ b/web/common.php Mon Nov 20 18:10:58 2023 +0100 @@ -4,6 +4,7 @@ require __DIR__ . '/vendor/autoload.php'; use Abraham\TwitterOAuth\TwitterOAuth; +use Fundevogel\Mastodon\Api; /** * Base configuration @@ -77,6 +78,10 @@ include_once dirname(__FILE__)."/$req_rep/traduction.php"; } +$appCacheHandle = new SQLite3(dirname(__FILE__)."/data/app_cache.db"); +$appCacheHandle->query("CREATE TABLE IF NOT EXISTS apps (id INTEGER PRIMARY KEY, domain TEXT UNIQUE NOT NULL, app_key TEXT UNIQUE NOT NULL, app_secret TEXT NOT NULL)"); + + /** * Start up the ol' session engine @@ -89,20 +94,38 @@ include_once dirname(__FILE__).'/config.php'; -$get_twitter_request_token = function () use ($req_rep) { +$get_social_request_token = function ($loginDomain, $config) use ($req_rep, $appCacheHandle) { + + $socialNetwork = $config['social_network']; + + if($socialNetwork == "Twitter") { - $twitterClient = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET); - $token = $twitterClient->oauth('oauth/request_token', array('oauth_callback' => URL_ROOT."callback.php?rep=$req_rep")); + $twitterClient = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET); + $token = $twitterClient->oauth('oauth/request_token', array('oauth_callback' => URL_ROOT."callback.php?rep=$req_rep")); + + $_SESSION['TWITTER_REQUEST_TOKEN'] = serialize($token); - $_SESSION['TWITTER_REQUEST_TOKEN'] = serialize($token); + /** + * Now redirect user to Twitter site so they can log in and + * approve our access + */ - /** - * Now redirect user to Twitter site so they can log in and - * approve our access - */ - $url = $twitterClient->url('oauth/authorize', array('oauth_token' => $token['oauth_token'])); - header("Location: ".$url); - die(); + $url = $twitterClient->url('oauth/authorize', array('oauth_token' => $token['oauth_token'])); + header("Location: ".$url); + die(); + } else if($socialNetwork == "Mastodon") { + $mastodonApi = new Api($loginDomain); + + $cachedApp = $appCacheHandle->querySingle(""); + $statement = $cachedApp->prepare('SELECT * FROM apps WHERE domain = :domain;'); + $statement->bindValue(':domain', $loginDomain); + +$result = $statement->execute(); + // Create app + $appsDef = $mastodonApi->apps()->create("PolemicToot", URL_ROOT."callback.php?rep=$req_rep", 'read write push'); + var_dump($appsDef); + //$mastodonApi->logIn(); + } }; /**