knowndiaspora/Pages/Callback.php

67 lines
3.6 KiB
PHP
Raw Normal View History

2015-01-17 23:12:58 +01:00
<?php
/**
2015-01-17 23:21:25 +01:00
* Diaspora pages
2015-01-17 23:12:58 +01:00
*/
2015-01-17 23:21:25 +01:00
namespace IdnoPlugins\Diaspora\Pages {
2015-01-17 23:12:58 +01:00
/**
2015-01-17 23:21:25 +01:00
* Default class to serve the Diaspora callback
2015-01-17 23:12:58 +01:00
*/
class Callback extends \Idno\Common\Page
{
function getContent()
{
$this->gatekeeper(); // Logged-in users only
2015-01-17 23:21:25 +01:00
if ($diaspora = \Idno\Core\site()->plugins()->get('Diaspora')) {
if ($diasporaAPI = $diaspora->connect()) {
/* @var \IdnoPlugins\diaspora\diasporaAPI $diasporaAPI */
if ($session = $diasporaAPI->getSessionOnLogin()) {
2015-01-17 23:12:58 +01:00
$user = \Idno\Core\site()->session()->currentUser();
$access_token = $session->getToken();
2015-01-17 23:21:25 +01:00
$diasporaAPI->setAccessToken($access_token);
if ($person = $diasporaAPI->api('/me','GET')) {
2015-01-17 23:12:58 +01:00
$name = $person['response']->getProperty('name');
$id = $person['response']->getProperty('id');
2015-01-17 23:21:25 +01:00
$user->diaspora[$id] = ['id' => $id, 'access_token' => $access_token, 'name' => $name];
\Idno\Core\site()->syndication()->registerServiceAccount('diaspora', $id, $name);
2015-01-17 23:12:58 +01:00
if (\Idno\Core\site()->config()->multipleSyndicationAccounts()) {
2015-01-17 23:21:25 +01:00
if ($companies = $diasporaAPI->api('/me/accounts','GET')) {
2015-01-17 23:12:58 +01:00
if (!empty($companies['response'])) {
foreach($companies['response']->asArray() as $company_container) {
foreach($company_container as $company) {
$company = (array) $company;
if ($perms = $company['perms']) {
if (in_array('CREATE_CONTENT', $perms) && !empty($company['name'])) {
$id = $company['id'];
$name = $company['name'];
$access_token = $company['access_token'];
2015-01-17 23:21:25 +01:00
$user->diaspora[$id] = ['id' => $id, 'access_token' => $access_token, 'name' => $name, 'page' => true];
\Idno\Core\site()->syndication()->registerServiceAccount('diaspora', $id, $name);
2015-01-17 23:12:58 +01:00
}
}
}
}
}
}
}
} else {
2015-01-17 23:21:25 +01:00
$user->diaspora = array('access_token' => $access_token);
2015-01-17 23:12:58 +01:00
}
$user->save();
}
}
}
if (!empty($_SESSION['onboarding_passthrough'])) {
unset($_SESSION['onboarding_passthrough']);
$this->forward(\Idno\Core\site()->config()->getURL() . 'begin/connect-forwarder');
}
2015-01-17 23:21:25 +01:00
$this->forward(\Idno\Core\site()->config()->getDisplayURL() . 'account/diaspora/');
2015-01-17 23:12:58 +01:00
}
}
2015-01-17 23:21:25 +01:00
}