Skip to content
This repository has been archived by the owner on May 7, 2021. It is now read-only.

Commit

Permalink
Many minor and some major fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanlesage committed Sep 11, 2016
1 parent 6c42117 commit 58ed306
Show file tree
Hide file tree
Showing 29 changed files with 1,113 additions and 293 deletions.
70 changes: 4 additions & 66 deletions app/Http/Controllers/AjaxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function uploadMedia()
$fcontents);

if($success) {
return response()->json(['Upload successful'], 200);
return response()->json(['Upload successful'], 200);
}
else {
return response()->json(['Failed to move file to directory'], 500);
Expand All @@ -65,65 +65,20 @@ public function getPageContent($id, $raw = "no")
try {
$page = Page::findOrFail($id);

$page->content = Markdown::convertToHtml($page->content);

return response()->json([$page->content, 200]);
return response()->json([$page->html()->content, 200]);
} catch (ModelNotFoundException $e) {
return response()->json(["Couldn't find page!", 404]);
}
}
else { // "Nice" HTML with variables parsed
try {
$page = Page::findOrFail($id);

$page->content = Markdown::convertToHtml($page->content);
$page = Page::findOrFail($id)->parsed();

} catch (ModelNotFoundException $e) {
return response()->json(["Couldn't find page!", 404]);
}

// TODO: Export this whole bunch of code

// BEGIN DISPLAY WIKILINKS: Preg-replace the Wikilinks
$pattern = "/\[\[(.*?)\]\]/i";
$page->content = preg_replace_callback($pattern, function($matches) {
// Replace the Link text with the title of each found wikilink

if(strpos($matches[1], '|') > 0)
{
// The second part is what we need
$text = explode('|', $matches[1]);
$matches[1] = $text[0];
if(strlen($text[1]) == 0) {
$text = $matches[1];
}
else {
$text = $text[1];
}
}
else {
// If no linktext was given, just display the match.
$text = $matches[1];
}

// Now that we for sure have only the slug in $matches[1], get the page
$page = Page::where('slug', $matches[1])->get()->first();

if($page !== null) {
// We got a match -> replace the link

if($text === $matches[1]) {
$text = $page->title;
}

return "<a href=\"" . url('/') . "/$matches[1]\">" . $text . "</a>";
}
else {
// No page with this name exists -> link to create page
return "<a class=\"broken\" href=\"" . url('/') . "/create/$matches[1]\" title=\"Create this page\">$text</a>";
}
}, $page->content);
// END DISPLAY WIKILINKS


// Prepare possible variables and replace them
// Available variables: %wordcount%, %pagecount%
Expand All @@ -137,23 +92,6 @@ public function getPageContent($id, $raw = "no")
return number_format($this->pagecount());
}, $page->content);

// Labels
$page->content = preg_replace_callback("(%label\|(.+?)%)", function($matches) {
return '<span class="label-primary">' . $matches[1] . '</span>';
}, $page->content);
$page->content = preg_replace_callback("(%success\|(.+?)%)", function($matches) {
return '<span class="label-success">' . $matches[1] . '</span>';
}, $page->content);
$page->content = preg_replace_callback("(%warning\|(.+?)%)", function($matches) {
return '<span class="label-warning">' . $matches[1] . '</span>';
}, $page->content);
$page->content = preg_replace_callback("(%error\|(.+?)%)", function($matches) {
return '<span class="label-error">' . $matches[1] . '</span>';
}, $page->content);
$page->content = preg_replace_callback("(%muted\|(.+?)%)", function($matches) {
return '<span class="label-muted">' . $matches[1] . '</span>';
}, $page->content);

return response()->json([$page->content, 200]);
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Auth/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function __construct()
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'name' => 'required|max:255|unique:users',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
]);
Expand Down
196 changes: 171 additions & 25 deletions app/Http/Controllers/Backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,29 @@
use App\Http\Requests;

use App\PageIndex;
use App\User;

use Auth;

use Validator;

use Illuminate\Support\Facades\Hash;

class Backend extends Controller
{
public function __construct()
{
if(env('AUTH_ACTIVE', true)) {
$this->middleware('auth');
}
}

/**
* Displays an initial "dashboard"
* @return View The dashboard view
*/
public function index()
{
{ // dd(session()->all());
$page = new \stdClass();
$stats = new \stdClass();
$page->title = "Dashboard";
Expand Down Expand Up @@ -162,16 +174,17 @@ public function backupDatabase()
->header('Content-Disposition', 'attachment; filename="' . env('DB_DATABASE', '') . '"');
}

public function flushViews()
public function flushCache()
{
// Run the artisan command
// Run the artisan commands
Artisan::call('view:clear');
Artisan::call('cache:clear');

// And return
return redirect('/admin');
}

public function logs($flags = "tail")
public function logs($file = "current", $flags = "tail")
{
$page = new \stdClass();
$page->title = 'Logs';
Expand All @@ -183,34 +196,165 @@ public function logs($flags = "tail")
$latest = "";

// Get the logfile with the newest information
foreach($logfiles as $file) {
foreach($logfiles as &$f) {
// Exclude any non-log files
if(substr($file, -3) !== "log") {
if(substr($f, -3) !== "log") {
continue;
}

if(File::lastModified($file) > $lastmod) {
$lastmod = File::lastModified($file);
$latest = $file;
if(File::lastModified($f) > $lastmod) {
$lastmod = File::lastModified($f);
// Replace the name for better readability in the view
$f = File::name($f) . '.' . File::extension($f);
$latest = $f;
}
}

// Now extract the tail of the latest logfile (if $flags != "all")
if($flags == "tail") {
$logcontent = File::get($latest);
$tail = preg_split("/((\r?\n)|(\r\n?))/", $logcontent);
// Get only the last 40 lines
$firstline = sizeof($tail) - 40;
$tail = array_slice($tail, $firstline);

for($i = 0; $i < sizeof($tail); $i++) {
$lines[$i] = new \stdClass();
$lines[$i]->number = $firstline + $i;
$lines[$i]->contents = $tail[$i];
$theFile = storage_path() . '/logs/' . (($file == 'current') ? $latest : $file);

$logcontent = File::get($theFile);
$logcontent = preg_split("/((\r?\n)|(\r\n?))/", $logcontent);

$logBegin = ($flags == 'tail') ? sizeof($logcontent) - 40 : 0;

$firstline = $logBegin;
$logcontent = array_slice($logcontent, $logBegin);
$lines = [];

for($i = 0; $i < sizeof($logcontent); $i++) {
$lines[$i] = new \stdClass();
$lines[$i]->number = $logBegin + $i;
$lines[$i]->contents = $logcontent[$i];
}

$theFile = File::name($theFile) . '.' . File::extension($theFile);

return view('admin.logs', compact('logfiles', 'lines', 'page', 'theFile'));
}

public function getToken()
{
$page = new \stdClass();
$page->title = trans('ui.backend.settings.token_title');

$tokenfile = storage_path() . '/app/token.txt';

if(!File::exists($tokenfile)) {
File::put($tokenfile, '');
}

$token = [];
foreach(preg_split("/((\r?\n)|(\r\n?))/", File::get($tokenfile)) as $line) {
if(strpos($line, '=') <= 0) {
continue;
}

$line = explode("=", $line);

$token[] = ['token' => $line[0], 'uses' => $line[1]];
}

return view('admin.logs', compact('logfiles', 'lines', 'page'));
return view('admin.token', compact('page', 'token'));
}

public function postToken(Request $request)
{
$tokenfile = storage_path() . '/app/token.txt';
$token = [];

if(!File::exists($tokenfile)) {
File::put($tokenfile, '');
}

// First lets check whether we should create new token
if($request->has('reg_token') && intval($request->reg_token) > 0) {
$uses = ($request->reg_token_uses > 0) ? $request->reg_token_uses : 1;

for($i = 0; $i < $request->reg_token; $i++) {
$token[] = ['token' => md5(random_bytes(32)), 'uses' => $uses];
}
}

// Now our other token with updated uses
if($request->has('token')) {
for($i = 0; $i < sizeof($request->token); $i++) { //foreach($request->token as $key => $t) {
$token[] = ['token' => $request->token[$i], 'uses' => $request->uses[$i]];
}
}

// Unset all token without uses
foreach($token as $key => $t) {
if($t['uses'] <= 0) {
unset($token[$key]);
}
}

// Now just save the token.
foreach($token as $key => $t) {
$token[$key] = $t['token'] . '=' . $t['uses'];
}

File::put($tokenfile, implode("\n", $token));

return redirect('/admin/token');
}

public function getAccount()
{
$page = new \stdClass();
$page->title = 'Account';

if(!Auth::check()) {
return redirect('/admin'); // Return non-logged-in users as they won't see anything here.
}

return view ('admin.account', compact('page'));
}

public function postAccount(Request $request)
{
if(!Auth::check()) {
return redirect('/admin'); // Cannot update info for no user
}

// Check password
if(!Auth::attempt(['email' => Auth::user()->email, 'password' => $request->old_password])) {
return redirect('/admin/account')->withInput()->withErrors(['old_password' => 'Your old password was wrong']);
}

$validator = Validator::make($request->all(), [
'name' => 'required|max:255|unique:users,name,'. Auth::user()->id,
'email' => 'required|email|max:255|unique:users,email,' . Auth::user()->id,
'old_password' => 'required|min:6',
'password' => 'min:6|confirmed',
]);

if($validator->fails()) {
return redirect('/admin/account')->withInput()->withErrors($validator);
}

$user = User::find(Auth::user()->id);

$user->name = $request->name;
$user->email = $request->email;

$user->save();

return redirect('/admin/account');
}

public function regenerateToken()
{
if(!Auth::check()) {
return redirect('/admin'); // Cannot regenerate a token for no user
}

$user = User::find(Auth::user()->id);

$user->api_token = Hash::make(random_bytes(32)); // For now only a simple token
$user->save();

return redirect('/admin/account');
}

/************************************
Expand Down Expand Up @@ -253,10 +397,12 @@ public function countIndexedWords()
*/
public function getCacheSize()
{
$files = File::allFiles(storage_path() . '/framework/views');

$size = 0;
foreach($files as $file) {
foreach(File::allFiles(storage_path() . '/framework/views') as $file) {
$size += File::size($file);
}

foreach(File::allFiles(storage_path() . '/framework/cache') as $file) {
$size += File::size($file);
}

Expand Down
Loading

0 comments on commit 58ed306

Please sign in to comment.