Skip to content

Commit

Permalink
login issues fiexed, site search, owner sees draft list
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Roberts committed Sep 30, 2014
1 parent d60bccc commit ca8f971
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 50 deletions.
58 changes: 45 additions & 13 deletions blog/controller/auth/login.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ public function index() {
// make sure they actually submitted something
if(!empty($me)){

//clean up the url given to us, just in case
$me = trim($me);
if(strpos($me, 'http') !== 0){
$me = 'http://'.$me;
}
$me = $this->normalize_url($me);

//look up user's auth provider
$auth_endpoint = IndieAuth\Client::discoverAuthorizationEndpoint($me);
Expand All @@ -39,19 +35,23 @@ public function index() {

$redir_url = $this->url->link('auth/login/callback', ($controller ? 'c='.$controller : ''), '');
if($scope){
// if a scope is given we are actually looking to get a token
$redir_url = $this->url->link('auth/login/tokencallback', ($controller ? 'c='.$controller : ''), '');
}

//build our get request
$trimmed_me = trim($me, '/'); //in case we get it back without the /
$data_array = array(
'me' => $me,
'redirect_uri' => $redir_url,
'response_type' => 'code',
'state' => substr(md5($me.$this->url->link('')),0,8),
'response_type' => 'id',
'state' => substr(md5($trimmed_me.$this->url->link('')),0,8),
'client_id' => $this->url->link('')
);
$this->log->write(print_r($data_array,true));
if($scope){
$data_array['scope'] = $scope;
$data_array['response_type'] = 'code';
}

$get_data = http_build_query($data_array);
Expand Down Expand Up @@ -79,16 +79,28 @@ public function callback() {
$redir_url = $this->url->link('auth/login/callback', 'c='.$this->request->get['c'], '');
}

$me = $this->request->get['me'];
$me = $this->normalize_url($this->request->get['me']);
$code = $this->request->get['code'];
$state = (isset($this->request->get['state']) ? $this->request->get['state'] : null);

$this->log->write('callback received ...');
$this->log->write(print_r($this->request->get,true));

$result = $this->confirm_auth($me, $code, $redir_url, $state);

if($result){
// we successfullly confirmed auth
$this->session->data['user_site'] = $this->request->get['me'];
$this->session->data['success'] = "You are now logged in as ".$this->request->get['me'];
$this->session->data['success'] = "You are now logged in as ".$me;

$token_user = str_replace(array('http://', 'https://'),array('',''), $me);

$myself = trim($this->normalize_url(HTTP_SERVER),'/');
$myself = trim(str_replace(array('http://', 'https://'),array('',''), $myself), '/');

if($token_user == $myself) {
$this->session->data['is_owner'] = true;
}
}

$this->response->redirect($url);
Expand All @@ -108,11 +120,12 @@ public function tokencallback() {
$redir_url = $this->url->link('auth/login/tokencallback', 'c='.$this->request->get['c'], '');
}

$me = $this->request->get['me'];
$me = $this->normalize_url($this->request->get['me']);
$code = $this->request->get['code'];
$state = (isset($this->request->get['state']) ? $this->request->get['state'] : null);

$result = $this->confirm_auth($me, $code, $redir_url, $state);
$this->log->write($result);

if($result){
// we successfullly confirmed auth
Expand Down Expand Up @@ -146,6 +159,8 @@ private function confirm_auth( $me, $code, $redir, $state = null ) {
}

$post_data = http_build_query($post_array);
$this->log->write('post_data: '.$post_data);
$this->log->write('endpoint: '.$auth_endpoint);

$ch = curl_init($auth_endpoint);

Expand All @@ -159,11 +174,19 @@ private function confirm_auth( $me, $code, $redir, $state = null ) {

$results = array();
parse_str($response, $results);

$this->log->write('endpoint_response: '.$response);
//$this->log->write(print_r($results, true));

$results['me'] = $this->normalize_url($results['me']);

$trimmed_me = trim($me, '/');
$trimmed_result_me = trim($results['me'], '/');

if($state){
return ($results['me'] == $me && $state == substr(md5($me.$client_id),0,8));
//$this->log->write('state = '.$state. ' ' .substr(md5($trimmed_me.$client_id),0,8));
return ($trimmed_result_me == $trimmed_me && $state == substr(md5($trimmed_me.$client_id),0,8));
} else {
return $results['me'] == $me ;
return $trimmed_result_me == $trimmed_me ;
}

}
Expand Down Expand Up @@ -207,5 +230,14 @@ private function get_token( $me, $code, $redir, $state = null ) {

return $results;
}


private function normalize_url($url) {
$url = trim($url);
if(strpos($url, 'http') !== 0){
$url = 'http://'.$url;
}
return $url;
}
}
?>
1 change: 1 addition & 0 deletions blog/controller/auth/logout.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class ControllerAuthLogout extends Controller {
public function index() {
unset($this->session->data['user_site']);
unset($this->session->data['token']);
unset($this->session->data['is_owner']);
$this->session->data['success'] = "Logged out";
$this->response->redirect($this->url->link(''));
}
Expand Down
3 changes: 2 additions & 1 deletion blog/controller/blog/article.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ public function index() {
$this->document->addMeta('twitter:card', 'summary');
$this->document->addMeta('twitter:title', $short_title);
$this->document->addMeta('twitter:description', $description);
$this->document->addMeta('twitter:image', '/image/static/icon_200.jpg');

$this->document->addMeta('og:type', 'article');
$this->document->addMeta('og:title', $short_title);
$this->document->addMeta('og:description', $description);
$this->document->addMeta('og:image', '/image/static/icon_128.jpg');
$this->document->addMeta('og:image', '/image/static/icon_200.jpg');

$data['header'] = $this->load->controller('common/header');
$data['footer'] = $this->load->controller('common/footer');
Expand Down
3 changes: 2 additions & 1 deletion blog/controller/blog/note.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ public function index() {
$this->document->addMeta('twitter:card', 'summary');
$this->document->addMeta('twitter:title', $short_title);
$this->document->addMeta('twitter:description', $description);
$this->document->addMeta('twitter:image', '/image/static/icon_200.jpg');

$this->document->addMeta('og:type', 'article');
$this->document->addMeta('og:title', $short_title);
$this->document->addMeta('og:description', $description);
$this->document->addMeta('og:image', '/image/static/icon_128.jpg');
$this->document->addMeta('og:image', '/image/static/icon_200.jpg');

$data['header'] = $this->load->controller('common/header');
$data['footer'] = $this->load->controller('common/footer');
Expand Down
3 changes: 2 additions & 1 deletion blog/controller/blog/photo.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ public function index() {
$this->document->addMeta('twitter:card', 'summary');
$this->document->addMeta('twitter:title', $short_title);
$this->document->addMeta('twitter:description', $description);
$this->document->addMeta('twitter:image', '/image/static/icon_200.jpg');

$this->document->addMeta('og:type', 'article');
$this->document->addMeta('og:title', $short_title);
$this->document->addMeta('og:description', $description);
$this->document->addMeta('og:image', '/image/static/icon_128.jpg');
$this->document->addMeta('og:image', '/image/static/icon_200.jpg');

$data['header'] = $this->load->controller('common/header');
$data['footer'] = $this->load->controller('common/footer');
Expand Down
16 changes: 7 additions & 9 deletions blog/controller/common/footer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function index() {
$data['login'] = $this->url->link('auth/login');

$data['google_analytics_id'] = GOOGLE_ANALYTICS_ID;
$data['sitesearch'] = trim(str_replace(array('http://','https://'),array('',''), HTTP_SERVER), '/');

$data['mylinks'] = array();

Expand All @@ -28,23 +29,20 @@ public function index() {
'target' => $result['target']);
}

if($this->session->data['is_owner']){
$this->load->model('blog/post');
$data['recent_drafts'] = $this->model_blog_post->getRecentDrafts(10);
}

$this->load->model('blog/mention');
$data['recent_mentions'] = array();
foreach ($this->model_blog_mention->getRecentMentions(10) as $result) {
$data['recent_mentions'][] = $result;
}
$data['recent_mentions'] = $this->model_blog_mention->getRecentMentions(10);

$this->load->model('blog/like');
$data['likes'] = array();
$data['likes'] = $this->model_blog_like->getGenericLikes();
$data['like_count'] = $this->model_blog_like->getGenericLikeCount();
foreach ($this->model_blog_like->getGenericLikes() as $result) {
$data['likes'][] = $result;
}

$this->load->model('blog/post');
$data['recent_posts'] = array();

foreach ($this->model_blog_post->getRecentPosts(10) as $result) {
if(empty($result['title'])){
if($result['post_type'] == 'photo'){
Expand Down
22 changes: 18 additions & 4 deletions blog/controller/common/home.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
<?php
class ControllerCommonHome extends Controller {
public function index() {
$this->document->setTitle($this->config->get('config_meta_title'));
$this->document->setDescription($this->config->get('config_meta_description'));
$short_title = SITE_TITLE;
$description = "OpenBlog site: ".SITE_TITLE;

$this->document->setTitle($short_title);
$this->document->setDescription($description);

$this->document->addMeta('twitter:card', 'summary');
$this->document->addMeta('twitter:title', $short_title);
$this->document->addMeta('twitter:description', $description);
$this->document->addMeta('twitter:image', '/image/static/icon_200.jpg');

$this->document->addMeta('og:type', 'website');
$this->document->addMeta('og:title', $short_title);
$this->document->addMeta('og:description', $description);
$this->document->addMeta('og:image', '/image/static/icon_200.jpg');

$data['header'] = $this->load->controller('common/header');
$data['footer'] = $this->load->controller('common/footer');
Expand All @@ -12,6 +25,7 @@ public function index() {
$this->load->model('blog/category');
$this->load->model('blog/comment');
$this->load->model('blog/like');


$data['posts'] = array();

Expand All @@ -28,7 +42,7 @@ public function index() {
$data['posts'][] = array_merge($result, array(
'body_html' => html_entity_decode($result['body']),
'author' => $author,
'author_image' => '/image/static/icon_128.jpg',
'author_image' => '/image/static/icon_200.jpg',
'categories' => $categories,
'comment_count' => $comment_count,
'like_count' => $like_count
Expand All @@ -45,7 +59,7 @@ public function index() {
$data['side_posts'][] = array_merge($result, array(
'body_html' => html_entity_decode(isset($result['excerpt']) ? $result['excerpt']. '...' : $result['body']),
'author' => $author,
'author_image' => '/image/static/icon_128.jpg',
'author_image' => '/image/static/icon_200.jpg',
'categories' => $categories,
'comment_count' => $comment_count,
'like_count' => $like_count
Expand Down
26 changes: 8 additions & 18 deletions blog/controller/micropub/receive.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
class ControllerMicropubReceive extends Controller {
public function index() {
$this->log->write('good');
$headers = apache_request_headers();
if(isset($this->request->post['access_token']) || isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION']) || isset($headers['Authorization'])){
$token = $this->request->post['access_token'];
Expand All @@ -19,32 +18,24 @@ public function index() {


if(!empty($auth_info) && in_array('post', explode(' ', $auth_info['scope']))) {
$this->log->write('good 2');

$token_user = str_replace(array('http://', 'https://'),array('',''), $auth_info['user']);
$myself = str_replace(array('http://', 'https://'),array('',''), HTTP_SERVER);

if($token_user == $myself || $token_user.'/' == $myself || $token_user == $myself .'/' ) {
$this->log->write('good 3');

//$this->log->write(print_r($this->request->post, true));
if(isset($this->request->post['operation']) && strtolower($this->request->post['operation']) == 'delete'){
$this->log->write('debug 4');
$this->deletePost();
} elseif(isset($this->request->post['operation']) && strtolower($this->request->post['operation']) == 'undelete'){
$this->log->write('debug 5');
$this->undeletePost();
} elseif(isset($this->request->post['type']) && $this->request->post['type'] == 'article'){
$this->log->write('debug 6');
$this->createArticle();
} elseif(isset($_FILES['photo']) && !empty($_FILES['photo'])){
$this->log->write('debug 7');
$this->createPhoto();
} elseif(isset($this->request->post['operation']) && strtolower($this->request->post['operation']) == 'edit'){
$this->log->write('debug 8');
$this->editPost();
} else {
$this->log->write('debug 9');
$this->createNote();
}

Expand All @@ -63,7 +54,7 @@ public function index() {
}

private function undeletePost(){
$this->log->write('called undeletePost()');
//$this->log->write('called undeletePost()');
$post = $this->getPostByURL($this->request->post['url']);
if($post && isset($this->request->post['syndication'])){
$this->load->model('blog/post');
Expand All @@ -78,7 +69,7 @@ private function undeletePost(){
}

private function deletePost(){
$this->log->write('called deletePost()');
//$this->log->write('called deletePost()');
$post = $this->getPostByURL($this->request->post['url']);
if($post && isset($this->request->post['syndication'])){
$this->load->model('blog/post');
Expand All @@ -98,7 +89,7 @@ private function deletePost(){
}

private function editPost(){
$this->log->write('called editPost()');
//$this->log->write('called editPost()');
$post = $this->getPostByURL($this->request->post['url']);
if($post && isset($this->request->post['syndication'])){
$this->load->model('blog/post');
Expand All @@ -112,7 +103,7 @@ private function editPost(){
}

private function createNote(){
$this->log->write('called createNote()');
//$this->log->write('called createNote()');
$this->load->model('blog/note');
$data = array();
$data['body'] = $this->request->post['content'];
Expand Down Expand Up @@ -148,9 +139,9 @@ private function createNote(){
}
}

$this->log->write(print_r($data,true));
//$this->log->write(print_r($data,true));
$note_id = $this->model_blog_note->newNote($data);
$this->log->write($note_id);
//$this->log->write($note_id);
$this->cache->delete('posts');
$this->cache->delete('notes');

Expand All @@ -174,9 +165,8 @@ private function createNote(){
}

private function createArticle(){
$this->log->write('called createArticle()');
//$this->log->write('called createArticle()');
//$this->log->write($this->request->post['content']);
//$this->log->write('called createArticle');
$this->load->model('blog/article');
$data = array();
$data['body'] = $this->request->post['content'];
Expand Down Expand Up @@ -233,7 +223,7 @@ private function createArticle(){
}

private function createPhoto(){
$this->log->write('called createPhoto()');
//$this->log->write('called createPhoto()');
$upload_shot = $_FILES['photo'];

if( $upload_shot['error'] == 0) {
Expand Down
Loading

0 comments on commit ca8f971

Please sign in to comment.