buncha fuckery

This commit is contained in:
flash 2015-07-05 02:03:15 +02:00
parent 8bea865ba1
commit 7086c8c9a6
11 changed files with 200 additions and 53 deletions

View file

@ -30,7 +30,8 @@
"20150701", "20150701",
"20150702", "20150702",
"20150703", "20150703",
"20150704" "20150704",
"20150705"
] ]
@ -1445,6 +1446,35 @@
"change": "Removed ignore lines for Misaki files." "change": "Removed ignore lines for Misaki files."
} }
],
"20150705": [
{
"type": "ADD",
"change": "Added count functionality to database wrapper."
},
{
"type": "UPD",
"change": "Make forum index page fully functional."
},
{
"type": "UPD",
"change": "Add user data and post linking to viewforum."
},
{
"type": "ADD",
"change": "Add 'x time ago' generator function."
},
{
"type": "FIX",
"change": "Fix bug while decoding the country name JSON with systems that don't default to UTF-8."
},
{
"type": "REM",
"change": "Removed some columns and replaced them with live stats."
}
] ]
} }

View file

@ -1,6 +1,6 @@
<?php <?php
/* /*
* Database engine container * Database wrapper container
*/ */
namespace Sakura; namespace Sakura;
@ -11,17 +11,17 @@ class Database {
private static $_DATABASE; private static $_DATABASE;
// Initialisation function // Initialisation function
public static function init($engine) { public static function init($wrapper) {
// Make the engine class name lowercase // Make the wrapper class name lowercase
$engine = __NAMESPACE__ .'\DBWrapper\\'. strtolower($engine); $wrapper = __NAMESPACE__ .'\DBWrapper\\'. strtolower($wrapper);
// Check if the class exists // Check if the class exists
if(!class_exists($engine)) if(!class_exists($wrapper))
trigger_error('Failed to load database driver', E_USER_ERROR); trigger_error('Failed to load database wrapper', E_USER_ERROR);
// Initialise SQL engine // Initialise SQL wrapper
self::$_DATABASE = new $engine; self::$_DATABASE = new $wrapper;
} }
@ -53,4 +53,11 @@ class Database {
} }
// Count from database
public static function count($table, $data, $prefix = null) {
return self::$_DATABASE->count($table, $data, $prefix);
}
} }

View file

@ -46,6 +46,16 @@ class Forum {
// For link and reg. forum add it to the category // For link and reg. forum add it to the category
$return[$forum['forum_category']]['forums'][$forum['forum_id']] = $forum; $return[$forum['forum_category']]['forums'][$forum['forum_id']] = $forum;
// Get the topic count
$return[$forum['forum_category']]['forums'][$forum['forum_id']]['topic_count'] = Database::count('topics', [
'forum_id' => [$forum['forum_id'], '=']
])[0];
// Get the post count
$return[$forum['forum_category']]['forums'][$forum['forum_id']]['post_count'] = Database::count('posts', [
'forum_id' => [$forum['forum_id'], '=']
])[0];
// Get last post in forum // Get last post in forum
$lastPost = Database::fetch('posts', false, [ $lastPost = Database::fetch('posts', false, [
'forum_id' => [$forum['forum_id'], '='] 'forum_id' => [$forum['forum_id'], '=']
@ -53,8 +63,10 @@ class Forum {
// Add last poster data and the details about the post as well // Add last poster data and the details about the post as well
$return[$forum['forum_category']]['forums'][$forum['forum_id']]['last_poster'] = [ $return[$forum['forum_category']]['forums'][$forum['forum_id']]['last_poster'] = [
'post' => $lastPost,
'user' => ($_LAST_POSTER = Users::getUser($lastPost['poster_id'])), 'user' => ($_LAST_POSTER = Users::getUser($lastPost['poster_id'])),
'rank' => Users::getRank($_LAST_POSTER['rank_main']) 'rank' => Users::getRank($_LAST_POSTER['rank_main']),
'elap' => Main::timeElapsed($lastPost['post_time'])
]; ];
} }
@ -114,6 +126,7 @@ class Forum {
], ['post_id', true]); ], ['post_id', true]);
$forum['forums'][$key]['last_poster'] = [ $forum['forums'][$key]['last_poster'] = [
'post' => $lastPost,
'user' => ($_LAST_POSTER = Users::getUser($lastPost['poster_id'])), 'user' => ($_LAST_POSTER = Users::getUser($lastPost['poster_id'])),
'rank' => Users::getRank($_LAST_POSTER['rank_main']) 'rank' => Users::getRank($_LAST_POSTER['rank_main'])
]; ];
@ -121,29 +134,7 @@ class Forum {
} }
// Lastly grab the topics for this forum // Lastly grab the topics for this forum
$forum['topics'] = Database::fetch('topics', true, [ $forum['topics'] = self::getTopics($forum['forum']['forum_id']);
'forum_id' => [$id, '=']
]);
// Get the userdata related to first and last posts
foreach($forum['topics'] as $key => $topic) {
// Get last post in forum
$firstPost = Database::fetch('posts', false, [
'topic_id' => [$topic['topic_id'], '=']
]);
$forum['topics'][$key]['first_poster'] = [
'user' => ($_FIRST_POSTER = Users::getUser($firstPost['topic_first_poster_id'])),
'rank' => Users::getRank($_FIRST_POSTER['rank_main'])
];
$forum['topics'][$key]['last_poster'] = [
'user' => ($_LAST_POSTER = Users::getUser($topic['topic_last_poster_id'])),
'rank' => Users::getRank($_LAST_POSTER['rank_main'])
];
}
// Return the forum/category // Return the forum/category
return $forum; return $forum;
@ -161,14 +152,33 @@ class Forum {
// Get the userdata related to last posts // Get the userdata related to last posts
foreach($topics as $key => $topic) { foreach($topics as $key => $topic) {
// Get the reply count
$topics[$key]['reply_count'] = Database::count('posts', [
'topic_id' => [$topic['topic_id'], '=']
])[0];
// Get first post in topics
$firstPost = Database::fetch('posts', false, [
'topic_id' => [$topic['topic_id'], '=']
]);
$topics[$key]['first_poster'] = [ $topics[$key]['first_poster'] = [
'user' => ($_FIRST_POSTER = Users::getUser($topic['topic_first_poster_id'])), 'post' => $firstPost,
'rank' => Users::getRank($_FIRST_POSTER['rank_main']) 'user' => ($_FIRST_POSTER = Users::getUser($firstPost['poster_id'])),
'rank' => Users::getRank($_FIRST_POSTER['rank_main']),
'elap' => Main::timeElapsed($firstPost['post_time'])
]; ];
// Get last post in topics
$lastPost = Database::fetch('posts', false, [
'topic_id' => [$topic['topic_id'], '=']
], ['post_id', true]);
$topics[$key]['last_poster'] = [ $topics[$key]['last_poster'] = [
'user' => ($_LAST_POSTER = Users::getUser($topic['topic_last_poster_id'])), 'post' => $lastPost,
'rank' => Users::getRank($_LAST_POSTER['rank_main']) 'user' => ($_LAST_POSTER = Users::getUser($lastPost['poster_id'])),
'rank' => Users::getRank($_LAST_POSTER['rank_main']),
'elap' => Main::timeElapsed($lastPost['post_time'])
]; ];
} }
@ -203,15 +213,27 @@ class Forum {
// Store the topic info // Store the topic info
$topic['topic'] = $topicInfo; $topic['topic'] = $topicInfo;
// Get first post in topics
$firstPost = Database::fetch('posts', false, [
'topic_id' => [$topic['topic']['topic_id'], '=']
]);
// Get the data of the first poster // Get the data of the first poster
$topic['topic']['first_poster'] = [ $topic['topic']['first_poster'] = [
'user' => ($_FIRST_POSTER = Users::getUser($topic['topic']['topic_first_poster_id'])), 'post' => $firstPost,
'user' => ($_FIRST_POSTER = Users::getUser($firstPost['poster_id'])),
'rank' => Users::getRank($_FIRST_POSTER['rank_main']) 'rank' => Users::getRank($_FIRST_POSTER['rank_main'])
]; ];
// Get last post in topics
$lastPost = Database::fetch('posts', false, [
'topic_id' => [$topic['topic']['topic_id'], '=']
], ['post_id', true]);
// Get the data of the last poster // Get the data of the last poster
$topic['topic']['last_poster'] = [ $topic['topic']['last_poster'] = [
'user' => ($_LAST_POSTER = Users::getUser($topic['topic']['topic_last_poster_id'])), 'post' => $lastPost,
'user' => ($_LAST_POSTER = Users::getUser($lastPost['poster_id'])),
'rank' => Users::getRank($_LAST_POSTER['rank_main']) 'rank' => Users::getRank($_LAST_POSTER['rank_main'])
]; ];
@ -223,7 +245,7 @@ class Forum {
// Add post and metadata to the global storage array // Add post and metadata to the global storage array
$topic['posts'][$post['post_id']] = array_merge($post, [ $topic['posts'][$post['post_id']] = array_merge($post, [
'is_op' => ($post['poster_id'] == $topic['topic']['topic_first_poster_id'] ? '1' : '0'), 'is_op' => ($post['poster_id'] == $firstPost['poster_id'] ? '1' : '0'),
'user' => ($_POSTER = Users::getUser($post['poster_id'])), 'user' => ($_POSTER = Users::getUser($post['poster_id'])),
'rank' => Users::getRank($_POSTER['rank_main']), 'rank' => Users::getRank($_POSTER['rank_main']),
'country' => Main::getCountryName($_POSTER['country']), 'country' => Main::getCountryName($_POSTER['country']),

View file

@ -518,7 +518,7 @@ class Main {
public static function getCountryName($code) { public static function getCountryName($code) {
// Parse JSON file // Parse JSON file
$iso3166 = json_decode(file_get_contents(ROOT .'_sakura/'. Configuration::getLocalConfig('data', 'iso3166')), true); $iso3166 = json_decode(utf8_encode(file_get_contents(ROOT .'_sakura/'. Configuration::getLocalConfig('data', 'iso3166'))), true);
// Check if key exists // Check if key exists
if(array_key_exists($code, $iso3166)) if(array_key_exists($code, $iso3166))
@ -674,4 +674,43 @@ class Main {
} }
// Time elapsed
public static function timeElapsed($timestamp) {
// Subtract the entered timestamp from the current timestamp
$time = time() - $timestamp;
// If the new timestamp is below 1 return a standard string
if($time < 1)
return 'Just now';
// Array containing time "types"
$times = [
365 * 24 * 60 * 60 => 'year',
30 * 24 * 60 * 60 => 'month',
24 * 60 * 60 => 'day',
60 * 60 => 'hour',
60 => 'minute',
1 => 'second'
];
foreach($times as $secs => $str) {
// Do a devision to check if the given timestamp fits in the current "type"
$calc = $time / $secs;
if($calc >= 1) {
// Round the number
$round = round($calc);
// Return the string
return $round .' '. $times[$secs] . ($round == 1 ? '' : 's') .' ago';
}
}
}
} }

View file

@ -308,4 +308,52 @@ class MySQL {
} }
// Count data from the database
public function count($table, $data, $prefix = null) {
// Begin preparation of the statement
$prepare = 'SELECT COUNT(*) FROM `' . ($prefix ? $prefix : Configuration::getLocalConfig('database', 'prefix')) . $table . '`';
// If $data is set and is an array continue
if(is_array($data)) {
$prepare .= ' WHERE';
foreach($data as $key => $value) {
$prepare .= ' `'. $key .'` '. $value[1] .' :'. $key . ($key == key(array_slice($data, -1, 1, true)) ? '' : ' AND');
// Unset variables to be safe
unset($key);
unset($value);
}
}
// Add the finishing semicolon
$prepare .= ';';
// Actually prepare the preration
$query = $this->sql->prepare($prepare);
// Bind those parameters if $data is an array that is
if(is_array($data)) {
foreach($data as $key => $value) {
$query->bindParam(':'. $key, $value[0]);
// Unset variables to be safe
unset($key);
unset($value);
}
}
// Execute the prepared statements with parameters bound
$query->execute();
// Return the output
return $query->fetch(PDO::FETCH_BOTH);
}
} }

View file

@ -8,7 +8,7 @@
namespace Sakura; namespace Sakura;
// Define Sakura version // Define Sakura version
define('SAKURA_VERSION', '20150704'); define('SAKURA_VERSION', '20150705');
define('SAKURA_VLABEL', 'Eminence'); define('SAKURA_VLABEL', 'Eminence');
define('SAKURA_STABLE', false); define('SAKURA_STABLE', false);
define('SAKURA_COLOUR', '#6C3082'); define('SAKURA_COLOUR', '#6C3082');
@ -19,6 +19,9 @@ define('ROOT', str_replace(basename(__DIR__), '', dirname(__FILE__)));
// Error Reporting: 0 for production and -1 for testing // Error Reporting: 0 for production and -1 for testing
error_reporting(SAKURA_STABLE ? 0 : -1); error_reporting(SAKURA_STABLE ? 0 : -1);
// Set internal encoding method
mb_internal_encoding('utf-8');
// Include libraries // Include libraries
require_once ROOT .'_sakura/vendor/autoload.php'; require_once ROOT .'_sakura/vendor/autoload.php';
require_once ROOT .'_sakura/components/Main.php'; require_once ROOT .'_sakura/components/Main.php';

View file

@ -23,13 +23,13 @@
</td> </td>
{% if forum.forum_type != 2 %} {% if forum.forum_type != 2 %}
<td class="forumCountColumn"> <td class="forumCountColumn">
<div class="topics" title="Amount of topics in this forum.">{{ forum.forum_topics }}</div> <div class="topics" title="Amount of topics in this forum.">{{ forum.topic_count }}</div>
<div class="posts" title="Amount of posts in this forum.">{{ forum.forum_posts }}</div> <div class="posts" title="Amount of posts in this forum.">{{ forum.post_count }}</div>
</td> </td>
<td class="forumLastColumn"> <td class="forumLastColumn">
<div> <div>
{% if forum.forum_last_post_id %} {% if forum.last_poster.user.id %}
Last post in <a href="//{{ sakura.urls.main }}/forum/thread/{{ forum.forum_last_post_id }}" class="default">Thread with an obnoxiously long fucking title</a><br />12 years ago by {% if forum.last_poster.user.id %}<a href="//{{ sakura.urls.main }}/u/{{ forum.last_poster.user.id }}" class="default" style="color: {% if forum.last_poster.user.name_colour %}{{ forum.last_poster.user.name_colour }}{% else %}{{ forum.last_poster.rank.colour }}{% endif %};">{{ forum.last_poster.user.username }}</a>{% else %}[deleted user]{% endif %} <a href="/forum/post/{{ forum.forum_last_post_id }}" class="default fa fa-tag"></a> <a href="//{{ sakura.urls.main }}/forum/thread/{{ forum.last_poster.post.topic_id }}" class="default">{{ forum.last_poster.post.post_subject }}</a><br />{{ forum.last_poster.elap }} by {% if forum.last_poster.user.id %}<a href="//{{ sakura.urls.main }}/u/{{ forum.last_poster.user.id }}" class="default" style="color: {% if forum.last_poster.user.name_colour %}{{ forum.last_poster.user.name_colour }}{% else %}{{ forum.last_poster.rank.colour }}{% endif %};">{{ forum.last_poster.user.username }}</a>{% else %}[deleted user]{% endif %} <a href="/forum/post/{{ forum.last_poster.post.post_id }}#p{{ forum.last_poster.post.post_id }}" class="default fa fa-tag"></a>
{% else %} {% else %}
There are no posts in this forum.<br />&nbsp; There are no posts in this forum.<br />&nbsp;
{% endif %} {% endif %}

View file

@ -13,7 +13,7 @@
{% endif %} {% endif %}
</td> </td>
<td class="topicCounts"> <td class="topicCounts">
<div class="replies" title="Amount of replies to this topic.">{{ topic.topic_replies }}</div> <div class="replies" title="Amount of replies to this topic.">{{ topic.reply_count }}</div>
<div class="views" title="Amount of times this topic has been viewed.">{{ topic.topic_views }}</div> <div class="views" title="Amount of times this topic has been viewed.">{{ topic.topic_views }}</div>
</td> </td>
<td class="topicLast"> <td class="topicLast">
@ -21,7 +21,7 @@
<a href="/u/{{ topic.last_poster.user.id }}" class="default" style="color: {% if topic.last_poster.user.name_colour %}{{ topic.last_poster.user.name_colour }}{% else %}{{ topic.last_poster.rank.colour }}{% endif %};">{{ topic.last_poster.user.username }}</a> <a href="/u/{{ topic.last_poster.user.id }}" class="default" style="color: {% if topic.last_poster.user.name_colour %}{{ topic.last_poster.user.name_colour }}{% else %}{{ topic.last_poster.rank.colour }}{% endif %};">{{ topic.last_poster.user.username }}</a>
{% else %} {% else %}
[deleted user] [deleted user]
{% endif %} <a href="#" class="default fa fa-tag"></a><br /> {% endif %} <a href="/forum/post/{{ topic.last_poster.post.post_id }}#p{{ topic.last_poster.post.post_id }}" class="default fa fa-tag"></a><br />
2000 years ago {{ topic.last_poster.elap }}
</td> </td>
</tr> </tr>

View file

@ -14,7 +14,7 @@
<img src="//{{ sakura.urls.content }}/images/tenshi.png" alt="Tenshi"{% if not post.is_premium %} style="opacity: 0;"{% endif %} /> <img src="//{{ sakura.urls.content }}/images/flags/{% if post.user.country|lower == 'eu' %}europeanunion{% else %}{{ post.user.country|lower }}{% endif %}.png" alt="{{ post.country }}" /> <img src="//{{ sakura.urls.content }}/images/tenshi.png" alt="Tenshi"{% if not post.is_premium %} style="opacity: 0;"{% endif %} /> <img src="//{{ sakura.urls.content }}/images/flags/{% if post.user.country|lower == 'eu' %}europeanunion{% else %}{{ post.user.country|lower }}{% endif %}.png" alt="{{ post.country }}" />
<div class="actions"> <div class="actions">
{% if user.data.id == post.user.id %} {% if user.data.id == post.user.id %}
<a class="fa fa-pencil-square-o" title="Edit this post" href="#"></a> <a class="fa fa-pencil-square-o" title="Edit this post" href="/forum/post/{{ post.post_id }}/edit"></a>
{% else %} {% else %}
{% if post.is_friend != 0 %}<a class="fa fa-{% if post.is_friend == 2 %}heart{% else %}star{% endif %}" title="You are friends"></a>{% endif %} {% if post.is_friend != 0 %}<a class="fa fa-{% if post.is_friend == 2 %}heart{% else %}star{% endif %}" title="You are friends"></a>{% endif %}
<a class="fa fa-user-{% if post.is_friend == 0 %}plus{% else %}times{% endif %}" title="{% if post.is_friend == 0 %}Add {{ post.user.username }} as a friend{% else %}Remove friend{% endif %}" href="//{{ sakura.urls.main }}/friends?{% if post.is_friend == 0 %}add{% else %}remove{% endif %}={{ post.user.id }}&amp;session={{ php.sessionid }}&amp;time={{ php.time }}&amp;redirect={{ sakura.currentpage }}"></a> <a class="fa fa-user-{% if post.is_friend == 0 %}plus{% else %}times{% endif %}" title="{% if post.is_friend == 0 %}Add {{ post.user.username }} as a friend{% else %}Remove friend{% endif %}" href="//{{ sakura.urls.main }}/friends?{% if post.is_friend == 0 %}add{% else %}remove{% endif %}={{ post.user.id }}&amp;session={{ php.sessionid }}&amp;time={{ php.time }}&amp;redirect={{ sakura.currentpage }}"></a>

View file

@ -76,6 +76,7 @@ RewriteRule ^forum/([0-9]+)/new/?$ posting.php?f=$1 [L,QSA]
RewriteRule ^forum/(thread|topic|[0-9+])/([0-9]+)/?$ viewtopic.php?t=$2 [L,QSA] RewriteRule ^forum/(thread|topic|[0-9+])/([0-9]+)/?$ viewtopic.php?t=$2 [L,QSA]
RewriteRule ^forum/(thread|topic|[0-9+])/([0-9]+)/reply/?$ posting.php?t=$2 [L,QSA] RewriteRule ^forum/(thread|topic|[0-9+])/([0-9]+)/reply/?$ posting.php?t=$2 [L,QSA]
RewriteRule ^forum/post/([0-9]+)/?$ viewtopic.php?p=$1 [L,QSA] RewriteRule ^forum/post/([0-9]+)/?$ viewtopic.php?p=$1 [L,QSA]
RewriteRule ^forum/post/([0-9]+)/edit/?$ posting.php?p=$1&edit=$1 [L,QSA]
RewriteRule ^forum/post/([0-9]+)/(quote|reply)/?$ posting.php?p=$1&quote=$1 [L,QSA] RewriteRule ^forum/post/([0-9]+)/(quote|reply)/?$ posting.php?p=$1&quote=$1 [L,QSA]
# Management # Management

View file

@ -36,8 +36,5 @@ $renderData = array_merge($renderData, $topic, [
] ]
]); ]);
//header('Content-Type: text/plain');
//print_r($renderData);exit;
// Print page contents // Print page contents
print Templates::render('forum/viewtopic.tpl', $renderData); print Templates::render('forum/viewtopic.tpl', $renderData);