Templating and Account settings page

This commit is contained in:
Phyks 2015-01-18 00:20:22 +01:00
parent 1238e052ff
commit e6bb69d9d3
11 changed files with 35 additions and 380 deletions

View File

@ -1,6 +1,6 @@
<?php
namespace IdnoPlugins\Diaspora {
namespace IdnoPlugins\KnownDiaspora {
class Main extends \Idno\Common\Plugin
{
@ -9,31 +9,23 @@
function registerPages()
{
// Deauth URL
\Idno\Core\site()->addPageHandler('facebook/deauth', '\IdnoPlugins\Facebook\Pages\Deauth');
// Register the callback URL
\Idno\Core\site()->addPageHandler('facebook/callback', '\IdnoPlugins\Facebook\Pages\Callback');
// Register admin settings
\Idno\Core\site()->addPageHandler('admin/facebook', '\IdnoPlugins\Facebook\Pages\Admin');
// Register settings page
\Idno\Core\site()->addPageHandler('account/facebook', '\IdnoPlugins\Facebook\Pages\Account');
\Idno\Core\site()->addPageHandler('account/diaspora', '\IdnoPlugins\KnownDiaspora\Pages\Account');
/** Template extensions */
// Add menu items to account & administration screens
\Idno\Core\site()->template()->extendTemplate('admin/menu/items', 'admin/facebook/menu');
\Idno\Core\site()->template()->extendTemplate('account/menu/items', 'account/facebook/menu');
\Idno\Core\site()->template()->extendTemplate('onboarding/connect/networks', 'onboarding/connect/facebook');
// Add menu items to account screens
\Idno\Core\site()->template()->extendTemplate('account/menu/items', 'account/diaspora/menu');
}
function registerEventHooks()
{
/*function registerEventHooks()
{ // TODO
\Idno\Core\site()->syndication()->registerService('facebook', function () {
return $this->hasFacebook();
\Idno\Core\site()->syndication()->registerService('diaspora', function () {
return $this->hasDiaspora();
}, array('note', 'article', 'image', 'media','rsvp', 'bookmark'));
if ($this->hasFacebook()) {
if (is_array(\Idno\Core\site()->session()->currentUser()->facebook) && !array_key_exists('access_token', \Idno\Core\site()->session()->currentUser()->facebook)) {
if ($this->hasDiaspora()) {
if (is_array(\Idno\Core\site()->session()->currentUser()->diaspora) && !array_key_exists('access_token', \Idno\Core\site()->session()->currentUser()->facebook)) {
foreach(\Idno\Core\site()->session()->currentUser()->facebook as $username => $details) {
\Idno\Core\site()->syndication()->registerServiceAccount('facebook', $username, $details['name']);
}
@ -180,66 +172,7 @@
}
}
});
}
/**
* Retrieve the URL to authenticate with Facebook
* @return string
*/
function getAuthURL()
{
$facebook = $this;
if ($facebookAPI = $facebook->connect()) {
return $facebookAPI->getLoginUrl();
}
return '';
}
/**
* Connect to Facebook
* @return bool|FacebookAPI
*/
function connect($account_id = '')
{
if (!empty(\Idno\Core\site()->config()->facebook)) {
require_once(dirname(__FILE__) . '/external/facebook-sdk/autoload.php');
\Facebook\FacebookSession::setDefaultApplication(
\Idno\Core\site()->config()->facebook['appId'],
\Idno\Core\site()->config()->facebook['secret']
);
$facebookAPI = new FacebookAPI();
if (!empty($account_id)) {
if (!empty(\Idno\Core\site()->session()->currentUser()->facebook[$account_id])) {
$facebookAPI->setAccessToken(\Idno\Core\site()->session()->currentUser()->facebook[$account_id]['access_token']);
$this->endpoint = $account_id;
return $facebookAPI;
}
} else {
if (!empty(\Idno\Core\site()->session()->currentUser()->facebook['access_token'])) {
$facebookAPI->setAccessToken(\Idno\Core\site()->session()->currentUser()->facebook['access_token']);
}
return $facebookAPI; // This needs to return even if we haven't set the user token yet, for the auth callback
}
}
return false;
}
/**
* Can the current user use Twitter?
* @return bool
*/
function hasFacebook()
{
if (!\Idno\Core\site()->session()->currentUser()) {
return false;
}
return \Idno\Core\site()->session()->currentUser()->facebook;
}
}*/
}
}

View File

@ -4,7 +4,7 @@
* Diaspora pages
*/
namespace IdnoPlugins\Diaspora\Pages {
namespace IdnoPlugins\KnownDiaspora\Pages {
/**
* Default class to serve Diaspora-related account settings
@ -14,23 +14,36 @@
function getContent()
{
$this->gatekeeper(); // Logged-in users only
if ($diaspora = \Idno\Core\site()->plugins()->get('Diaspora')) {
$login_url = $diaspora->getAuthURL();
if(isset($_GET['remove'])) {
\Idno\Core\site()->config->config['diaspora'] = [
'diaspora_username' => '',
'diaspora_password' => '',
'diaspora_pod' => ''
];
\Idno\Core\site()->config()->save();
\Idno\Core\site()->session()->addMessage('Your Diaspora credentials were removed.');
$this->forward(\Idno\Core\site()->config()->getDisplayURL() . 'account/diaspora/');
}
$this->gatekeeper(); // Logged-in users only
$t = \Idno\Core\site()->template();
$body = $t->__(array('login_url' => $login_url))->draw('account/diaspora');
$body = $t->__(array())->draw('account/diaspora');
$t->__(array('title' => 'Diaspora', 'body' => $body))->drawPage();
}
function postContent() {
$this->gatekeeper(); // Logged-in users only
if (($this->getInput('remove'))) {
$user = \Idno\Core\site()->session()->currentUser();
$user->diaspora = array();
$user->save();
\Idno\Core\site()->session()->addMessage('Your Diaspora settings have been removed from your account.');
$pod = $this->getInput('pod');
$username = $this->getInput('user');
$password = $this->getInput('pass');
\Idno\Core\site()->config->config['diaspora'] = [
'diaspora_username' => $username,
'diaspora_pod' => $pod
];
if (empty(\Idno\Core\site()->config->config['diaspora']['diaspora_password']) || !empty($password) ) {
\Idno\Core\site()->config->config['diaspora']['diaspora_password'] = $password;
}
\Idno\Core\site()->config()->save();
\Idno\Core\site()->session()->addMessage('Your Diaspora credentials were saved.');
$this->forward(\Idno\Core\site()->config()->getDisplayURL() . 'account/diaspora/');
}

View File

@ -1,38 +0,0 @@
<?php
/**
* Diaspora pages
*/
namespace IdnoPlugins\Diaspora\Pages {
/**
* Default class to serve Diaspora settings in administration
*/
class Admin extends \Idno\Common\Page
{
function getContent()
{
$this->adminGatekeeper(); // Admins only
$t = \Idno\Core\site()->template();
$body = $t->draw('admin/diaspora');
$t->__(array('title' => 'Diaspora', 'body' => $body))->drawPage();
}
function postContent() {
$this->adminGatekeeper(); // Admins only
$appId = $this->getInput('appId');
$secret = $this->getInput('secret');
\Idno\Core\site()->config->config['diaspora'] = array(
'appId' => $appId,
'secret' => $secret
);
\Idno\Core\site()->config()->save();
\Idno\Core\site()->session()->addMessage('Your Diaspora application details were saved.');
$this->forward(\Idno\Core\site()->config()->getDisplayURL() . 'admin/diaspora/');
}
}
}

View File

@ -1,66 +0,0 @@
<?php
/**
* Diaspora pages
*/
namespace IdnoPlugins\Diaspora\Pages {
/**
* Default class to serve the Diaspora callback
*/
class Callback extends \Idno\Common\Page
{
function getContent()
{
$this->gatekeeper(); // Logged-in users only
if ($diaspora = \Idno\Core\site()->plugins()->get('Diaspora')) {
if ($diasporaAPI = $diaspora->connect()) {
/* @var \IdnoPlugins\diaspora\diasporaAPI $diasporaAPI */
if ($session = $diasporaAPI->getSessionOnLogin()) {
$user = \Idno\Core\site()->session()->currentUser();
$access_token = $session->getToken();
$diasporaAPI->setAccessToken($access_token);
if ($person = $diasporaAPI->api('/me','GET')) {
$name = $person['response']->getProperty('name');
$id = $person['response']->getProperty('id');
$user->diaspora[$id] = ['id' => $id, 'access_token' => $access_token, 'name' => $name];
\Idno\Core\site()->syndication()->registerServiceAccount('diaspora', $id, $name);
if (\Idno\Core\site()->config()->multipleSyndicationAccounts()) {
if ($companies = $diasporaAPI->api('/me/accounts','GET')) {
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'];
$user->diaspora[$id] = ['id' => $id, 'access_token' => $access_token, 'name' => $name, 'page' => true];
\Idno\Core\site()->syndication()->registerServiceAccount('diaspora', $id, $name);
}
}
}
}
}
}
}
} else {
$user->diaspora = array('access_token' => $access_token);
}
$user->save();
}
}
}
if (!empty($_SESSION['onboarding_passthrough'])) {
unset($_SESSION['onboarding_passthrough']);
$this->forward(\Idno\Core\site()->config()->getURL() . 'begin/connect-forwarder');
}
$this->forward(\Idno\Core\site()->config()->getDisplayURL() . 'account/diaspora/');
}
}
}

View File

@ -1,5 +1,5 @@
[Plugin description]
name = "Diaspora"
name = "KnownDiaspora"
version = 0.1
author = "Lucas Verney (Phyks)"
author_email = "phyks@phyks.me"

View File

@ -1,110 +0,0 @@
<div class="row">
<div class="span10 offset1">
<?= $this->draw('account/menu') ?>
<h1>Facebook</h1>
</div>
</div>
<div class="row">
<div class="span10 offset1">
<?php
if (empty(\Idno\Core\site()->session()->currentUser()->facebook)) {
?>
<div class="control-group">
<div class="controls-config">
<div class="row">
<div class="span6">
<p>
Easily share pictures, updates, and posts to Facebook.</p>
<p>
With Facebook connected, you can cross-post content that you publish publicly on
your site.
</p>
</div>
</div>
<div class="social span6">
<p>
<a href="<?= $vars['login_url'] ?>" class="connect fb">Connect Facebook</a>
</p>
</div>
</div>
</div>
<?php
} else {
?>
<div class="control-group">
<div class="controls-config">
<div class="row">
<div class="span6">
<p>
Your account is currently connected to Facebook.
Public updates, pictures, and posts that you publish
here can be cross-posted to Facebook.
</p>
<?php
if ($accounts = \Idno\Core\site()->syndication()->getServiceAccounts('facebook')) {
foreach ($accounts as $id => $account) {
?>
<div class="social">
<form action="<?= \Idno\Core\site()->config()->getDisplayURL() ?>facebook/deauth"
class="form-horizontal" method="post">
<p>
<input type="hidden" name="remove" value="<?= $account['username'] ?>"/>
<button type="submit"
class="connect fb connected"><?= $account['name'] ?> (Disconnect)
</button>
<?= \Idno\Core\site()->actions()->signForm('/account/facebook/') ?>
</p>
</form>
<?php
}
} else {
?>
</div>
<div class="social">
<form action="<?= \Idno\Core\site()->config()->getDisplayURL() ?>facebook/deauth"
class="form-horizontal" method="post">
<p>
<input type="hidden" name="remove" value="1"/>
<button type="submit" class="connect fb connected">Disconnect Facebook
</button>
<?= \Idno\Core\site()->actions()->signForm('/account/facebook/') ?>
</p>
</form>
<?php
}
?>
</div>
<p>
<a href="<?= $vars['login_url'] ?>" ><icon class="icon-plus"></icon> Add another Facebook account</a>
</p>
</div>
</div>
</div>
</div>
<?php
}
?>
</div>
</div>

View File

@ -1 +0,0 @@
<li <?php if ($_SERVER['REQUEST_URI'] == '/account/facebook/') echo 'class="active"'; ?>><a href="/account/facebook/">Facebook</a></li>

View File

@ -1,55 +0,0 @@
<div class="row">
<div class="span10 offset1">
<?=$this->draw('admin/menu')?>
<h1>Facebook configuration</h1>
</div>
</div>
<div class="row">
<div class="span10 offset1">
<form action="<?=\Idno\Core\site()->config()->getURL()?>admin/facebook/" class="form-horizontal" method="post">
<div class="control-group">
<div class="controls-config">
<p>
To begin using Facebook, <a href="https://developers.facebook.com/apps" target="_blank">create a new application in
the Facebook apps portal</a>.</p>
<p>
Mark the Platform as <strong>Website</strong>, and use <strong><?=\Idno\Core\site()->config()->url?></strong>
as the site URL. Be sure to also include an email address. Then, click <em>Status &amp; Review</em>, and make the app public.
</p>
<p>
Once you've finished, fill in the details below. You can then <a href="<?=\Idno\Core\site()->config()->getURL()?>account/facebook/">connect your Facebook account</a>.
</p>
</div>
</div>
<div class="control-group">
<label class="control-label" for="name">App ID</label>
<div class="controls">
<input type="text" id="name" placeholder="App ID" class="span6" name="appId" value="<?=htmlspecialchars(\Idno\Core\site()->config()->facebook['appId'])?>" >
</div>
</div>
<div class="control-group">
<label class="control-label" for="name">App secret</label>
<div class="controls">
<input type="text" id="name" placeholder="App secret" class="span6" name="secret" value="<?=htmlspecialchars(\Idno\Core\site()->config()->facebook['secret'])?>" >
</div>
</div>
<div class="control-group">
<p>
After the Facebook application is configured, you must connect under account Settings.
</p>
</div>
<div class="control-group">
<div class="controls-save">
<button type="submit" class="btn btn-primary">Save settings</button>
</div>
</div>
<?= \Idno\Core\site()->actions()->signForm('/admin/facebook/')?>
</form>
</div>
</div>

View File

@ -1 +0,0 @@
<li <?php if ($_SERVER['REQUEST_URI'] == '/admin/facebook/') echo 'class="active"'; ?>><a href="<?=\Idno\Core\site()->config()->getURL()?>admin/facebook/">Facebook</a></li>

View File

@ -1 +0,0 @@
<i class="icon-facebook"></i>

View File

@ -1,19 +0,0 @@
<?php
if ($facebook = \Idno\Core\site()->plugins()->get('Facebook')) {
if (empty(\Idno\Core\site()->session()->currentUser()->facebook)) {
$login_url = $facebook->getAuthURL();
} else {
$login_url = \Idno\Core\site()->config()->getURL() . 'facebook/deauth';
}
}
?>
<div class="social">
<a href="<?=$login_url?>" class="connect fb <?php
if (!empty(\Idno\Core\site()->session()->currentUser()->facebook)) { echo 'connected'; }
?>" target="_top">Facebook<?php if (!empty(\Idno\Core\site()->session()->currentUser()->facebook)) { echo ' - connected!'; } ?></a><br>
<label class="control-label">Share pictures, updates, and posts to Facebook.</label>
</div>