This repository has been archived on 2024-06-26. You can view files and clone it, but cannot push or open issues or pull requests.
sakura/libraries/Controllers/UserController.php

163 lines
4 KiB
PHP
Raw Normal View History

2016-02-04 20:56:40 +00:00
<?php
/**
* Holds the user page controllers.
2016-02-05 11:20:33 +00:00
*
2016-02-04 20:56:40 +00:00
* @package Sakura
*/
namespace Sakura\Controllers;
use Sakura\Config;
2016-02-18 23:28:44 +00:00
use Sakura\DB;
2016-03-26 16:36:58 +00:00
use Sakura\Notification;
use Sakura\Perms\Site;
2016-02-05 11:20:33 +00:00
use Sakura\Rank;
use Sakura\Router;
2016-02-04 20:56:40 +00:00
use Sakura\Template;
2016-02-27 17:28:45 +00:00
use Sakura\User;
2016-02-04 20:56:40 +00:00
use Sakura\Utils;
/**
* Everything that is just for serving user data.
2016-02-05 11:20:33 +00:00
*
2016-02-04 20:56:40 +00:00
* @package Sakura
* @author Julian van de Groep <me@flash.moe>
*/
2016-02-27 16:46:16 +00:00
class UserController extends Controller
2016-02-04 20:56:40 +00:00
{
/**
2016-02-05 11:20:33 +00:00
* Display the profile of a user.
*
2016-02-04 20:56:40 +00:00
* @param mixed $id The user ID.
2016-02-05 11:20:33 +00:00
*
2016-02-04 20:56:40 +00:00
* @return bool|string The profile page.
*/
2016-02-14 22:46:07 +00:00
public function profile($id = 0)
2016-02-04 20:56:40 +00:00
{
global $currentUser;
// Get the user's context
2016-02-27 17:28:45 +00:00
$profile = User::construct($id);
2016-02-04 20:56:40 +00:00
// If the user id is zero check if there was a namechange
if ($profile->id == 0) {
// Fetch from username_history
2016-02-25 16:06:29 +00:00
$check = DB::table('username_history')
->where('username_old_clean', Utils::cleanString($id, true, true))
->orderBy('change_id', 'desc')
->get();
2016-02-05 11:20:33 +00:00
2016-02-04 20:56:40 +00:00
// Redirect if so
if ($check) {
2016-03-27 21:18:57 +00:00
$message = "This user changed their username! Redirecting you to their new profile.";
$redirect = Router::route('user.profile', $check[0]->user_id);
Template::vars(compact('message', 'redirect'));
2016-02-04 20:56:40 +00:00
// Print page contents
return Template::render('global/information');
}
}
// Set parse variables
2016-03-27 21:18:57 +00:00
Template::vars(compact('profile'));
2016-02-04 20:56:40 +00:00
// Print page contents
return Template::render('main/profile');
}
2016-02-05 11:20:33 +00:00
/**
* Display the memberlist.
*
* @param int $rank Optional rank ID.
*
* @return bool|string The memberlist.
*/
2016-02-27 16:46:16 +00:00
public function members($rank = null)
2016-02-04 20:56:40 +00:00
{
global $currentUser;
// Check permission
2016-03-26 16:36:58 +00:00
if (!$currentUser->permission(Site::VIEW_MEMBERLIST)) {
2016-02-04 20:56:40 +00:00
return Template::render('global/restricted');
}
2016-02-27 16:46:16 +00:00
// Get all ranks
$getRanks = DB::table('ranks')
->get(['rank_id']);
// Define variable
$ranks = [];
// Add the empty rank
$ranks[0] = Rank::construct(0);
// Reorder shit
foreach ($getRanks as $sortRank) {
$ranks[$sortRank->rank_id] = Rank::construct($sortRank->rank_id);
}
// Get the active rank
$rank = array_key_exists($rank, $ranks) ? $rank : ($rank ? 0 : 2);
2016-03-27 21:18:57 +00:00
// Get members per page
$membersPerPage = Config::get('members_per_page');
2016-02-04 20:56:40 +00:00
// Set parse variables
2016-03-27 21:18:57 +00:00
Template::vars(compact('ranks', 'rank', 'membersPerPage'));
2016-02-04 20:56:40 +00:00
// Render the template
return Template::render('main/memberlist');
}
2016-03-26 16:36:58 +00:00
2016-03-27 21:18:57 +00:00
/**
* Get the notification JSON object for the currently authenticated user.
*
* @return string The JSON object.
*/
2016-03-26 16:36:58 +00:00
public function notifications()
{
// TODO: add friend on/offline messages
global $currentUser;
// Set json content type
header('Content-Type: application/json; charset=utf-8');
return json_encode(
$currentUser->notifications(),
JSON_FORCE_OBJECT | JSON_NUMERIC_CHECK | JSON_BIGINT_AS_STRING
);
}
2016-03-27 21:18:57 +00:00
/**
* Mark a notification as read.
*
* @param int The ID of the notification.
*
* @return string Not entirely set on this one yet but 1 for success and 0 for fail.
*/
2016-03-26 16:36:58 +00:00
public function markNotification($id = 0)
{
global $currentUser;
// Check permission
if ($currentUser->permission(Site::DEACTIVATED)) {
return '0';
}
// Create the notification object
$alert = new Notification($id);
// Verify that the currently authed user is the one this alert is for
if ($alert->user !== $currentUser->id) {
return '0';
}
// Toggle the read status and save
$alert->toggleRead();
$alert->save();
return '1';
}
2016-02-04 20:56:40 +00:00
}