jhuesser/bzu-jodel-clone

View on GitHub
jodels.php

Summary

Maintainability
A
1 hr
Test Coverage
<?php
session_start();
require 'functions/jodelmeta.php';
//Set default values for head & load it
$title = "Posts | SocialDomayn";
$stylesheet = "jodel.css";
include 'functions/header.php';
//Load API functions
require 'functions/apicalls.php';
$config = require('config.php');
require 'functions/votes.php';
require 'functions/usermanipulation.php';
require 'functions/admintools.php';
$apiroot = $config->apiUrl;
$baseurl = $config->baseUrl;
$uploaddir = $config->image_upload_dir;
$mainaction = true;
if(!isset($_SESSION['userid'])) {
header('Location: ' . $config->baseUrl . 'login.php');
 
}
 
//get ID of the user
$userid = $_SESSION['userid'];
//Get data about the user and save it in session values
$callurl = $apiroot . "jodlers?transform=1&filter=jodlerID,eq," . $userid;
$userjson = getCall($callurl);
$user = json_decode($userjson, true);
foreach($user['jodlers'] as $jodler){
$karma = $jodler['karma'];
$accstate = $jodler['account_state'];
}
$_SESSION['karma'] = $karma;
$_SESSION['acctype'] = $accstate;
 
//If a user is a normal user and has enough reputation, promote to mod
if($karma >= $config->karma_calc['promote_mod'] && $accstate == 1){
//set dummy caps for manipulateUserFunction
$mycaps = array();
$mycaps['promote_to_mod'] = true;
$updated = manipulateUser($userid, 2, $mycaps);
//destroy caps to aviod abuse
unset($mycaps);
 
}
 
if(isset($_GET['deletepost'])){
$post2delete = $_GET['deletepost'];
deletePost($post2delete);
header('Location: ' . $baseurl . 'jodels.php');
}
 
//if joels.php?upvotejodel=$jodelID is called, upvote it
if(isset($_GET['upvotejodel'])){
$mainaction = false;
voteJodel( $_GET['upvotejodel'], "up");
}
 
//if jodels.php?downvotejodel=$jodelID ist called, downvote post
if(isset($_GET['downvotejodel'])){
$mainaction = false;
voteJodel( $_GET['downvotejodel'], "down");
}
if($mainaction == true){
//If jodels.php?sort=$sort is called, post should be sorted
if(isset($_GET['sort'])){
$parameter = $_GET['sort'];
//check how posts should be sorted
switch ($parameter){
case "latest":
$sort = "latest";
break;
case "hot":
$sort = "hot";
break;
case "popular":
$sort = "popular";
break;
case "my":
$sort = "my";
break;
case "mycomms":
$sort= "mycomms";
break;
case "myvotes":
$sort = "myvotes";
break;
default:
$sort = "latest";
}
}
?>
<!-- Top / Main Navigation -->
<div id="top"></div>
<ul class="nav justify-content-center">
<!-- Refresh page -->
<li class="nav-item">
<a class="nav-link" href="javascript:window.location.reload();"><i class="fa fa-refresh" aria-hidden="true"></i></a>
</li>
<!-- Latest posts -->
<li class="nav-item">
<a class="nav-link <?php if($sort == 'latest' || !isset($sort)){ echo 'active';}?>" href="?sort=latest"><?php echo $config->app_vocabulary['latest'];?></a>
</li>
<!-- Most comments -->
<li class="nav-item">
<a class="nav-link <?php if($sort == 'hot'){ echo 'active';}?>" href="?sort=hot"><?php echo $config->app_vocabulary['hotest'];?></a>
</li>
<!-- most votes -->
<li class="nav-item">
<a class="nav-link <?php if($sort == 'popular'){ echo 'active';}?>" href="?sort=popular"><?php echo $config->app_vocabulary['popular'];?></a>
</li>
<!-- user profile -->
<li class="nav-item">
<a class="nav-link <?php if($sort == 'my' || $sort =='mycomms' || $sort =='myvotes'){ echo 'active';}?>" href="user.php"><i class="fa fa-user" aria-hidden="true"></i><?php echo $karma;?></a>
</li>
</ul>
<!-- must check in stylesheet -->
<div class="test"></div>
<?php
if(isset($_SESSION['errorMsg'])) {
?>
<div class="alert alert-danger" role="alert">
<strong>Holy guacamole!</strong> <?php echo $_SESSION['errorMsg'];?>
</div>
<?php
}
 
//set up url where posts are stored
$postsUrl = $apiroot . "jodeldata?transform=1";
//setup post filter (as selected in main menu)
switch ($sort){
case "latest":
$filter = "";
break;
case "hot":
$filter = "&order=comments_cnt,desc";
break;
case "popular":
$filter = "&order=votes_cnt,desc";
break;
case "my":
$filter="&filter=jodlerIDFK,eq," . $userid;
break;
Similar blocks of code found in 2 locations. Consider refactoring.
case "mycomms":
$commenturl = $apiroot . "comments?transform=1,filter=jodlerIDFK,eq," . $userid;
$commentsjson = getCall($commenturl);
$comments = json_decode($commentsjson, true);
$filter = "";
foreach($comments['comments'] as $comment){
$filter .= "&filter[]=jodelID,eq," . $comment['jodelIDFK'];
}
$filter .= "&satisfy=any";
break;
Similar blocks of code found in 2 locations. Consider refactoring.
case "myvotes":
$voteurl = $apiroot . "jodelvotes?transform=1&userIDFK,eq," . $userid;
$votesjson = getCall($voteurl);
$votes = json_decode($votesjson, true);
$filter = "";
foreach($votes['jodelvotes'] as $vote){
$filter .= "&filter[]=jodelID,eq," . $vote['jodelIDFK'];
}
$filter .= "&satisfy=any";
break;
default:
$filter = "";
}
//setup api call with filter
$jodelsUrl = $postsUrl . $filter;
$posts = getCall($jodelsUrl);
$postdata = json_decode($posts, true);
//process posts
 
/*
$reportjson = getCall($apiroot . "reports?transform=1");
$reports = json_decode($reportjson, true);
$postAndResons = array();
foreach($reports['reports'] as $report){
$postAndResons[$report['jodelIDFK']] = $report['abuseIDFK'];
}
*/
 
if(file_exists('user/notice.txt')){
$notice = file_get_contents('user/notice.txt');
?>
<div class="card card-inverse mb-3 text-center" style="background-color: grey;">
<div class="card-block">
<blockquote class="card-blockquote">
<?php
echo $notice;
?>
</blockquote>
</div>
</div>
<?php
}
 
foreach($postdata['jodeldata'] as $post){
if($post['score'] <= $config->postmeta['needed_score_mod'] /* && $postAndResons[$post['jodelID']] != $config->postmeta['system_mod_id']*/){
$reported = reportContent( "post", $post['jodelID'], $config->postmeta['system_mod_id']);
}
 
//setup layout
if($post['votes_cnt'] > $config->postmeta['needed_downvotes']){
?>
<div class="card card-inverse mb-3 text-center" id="<?php echo $post['jodelID'];?>" style="background-color: #<?php echo $post['colorhex'];?>;">
<div class="card-block">
<blockquote class="card-blockquote">
<?php
//post isn't downvoted
if(!isset($post['path'])){
echo $post['jodel'];
} else {
echo '<br><img src="' . $uploaddir . $post['path'] . '" alt="jodelimage">';
}
?>
<!-- voting and number of votes -->
<div class="jodelvotes">
<a href="?upvotejodel=<?php echo $post['jodelID'];?>"<i class="fa fa-angle-up" aria-hidden="true"></i></a><br>
<?php echo $post['votes_cnt'] . "<br>";?>
<a href="?downvotejodel=<?php echo $post['jodelID'];?>"<i class="fa fa-angle-down" aria-hidden="true"></i></a>
</div>
<div class="clear"></div>
<!-- end voting and number of votes -->
<!-- post metadata -->
<div class="jodelmeta">
<?php
$timeago = jodelage($post['createdate']);
?>
<?php echo " ";?><i class="fa fa-clock-o" aria-hidden="true"></i><span id="<?php echo 'time-' . $post['jodelID'];?>"><?php echo $timeago;?></span>
<?php echo " " ;?><a href="comments.php?showcomment=<?php echo $post['jodelID'];?>"><i class="fa fa-comment" aria-hidden="true"></i><?php echo $post['comments_cnt'];?></a>
<?php if ($post['account_state'] == 4){echo '<i class="adminmark fa fa-check-square" aria-hidden="true"></i>';}?>
<?php if ($post['jodlerIDFK'] == $userid){ echo '<a href="?deletepost=' . $post['jodelID'] . '"><i class="fa fa-trash-o" aria-hidden="true"></i></a>';}?>
<!-- end post metadata -->
</blockquote>
</div> <!-- end post card somewhere here -->
</div><?php
}
 
}
 
?>
<!-- new post button -->
<div class="newpost"><a href="new.php"><i class="fa fa-plus-circle" aria-hidden="true"></a></i></div>
 
<?php
//include footer
include 'functions/footer.php';
}