Skip to content

Commit

Permalink
feat. seo configuration (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
nivcoo authored Dec 28, 2020
1 parent bef9efb commit d700e52
Show file tree
Hide file tree
Showing 14 changed files with 547 additions and 24 deletions.
13 changes: 13 additions & 0 deletions app/Config/Schema/schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,19 @@ public function after($event = array(), $install = false, $updateContent = array
'tableParameters' => array('charset' => 'latin1', 'collate' => 'latin1_swedish_ci', 'engine' => 'InnoDB')
);

public $seo = array(
'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => 20, 'unsigned' => false, 'key' => 'primary'),
'title' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255, 'collate' => 'latin1_swedish_ci', 'charset' => 'latin1'),
'description' => array('type' => 'text', 'null' => true, 'default' => null, 'collate' => 'latin1_swedish_ci', 'charset' => 'latin1'),
'favicon_url' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255, 'collate' => 'latin1_swedish_ci', 'charset' => 'latin1'),
'img_url' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255, 'collate' => 'latin1_swedish_ci', 'charset' => 'latin1'),
'page' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255, 'collate' => 'latin1_swedish_ci', 'charset' => 'latin1'),
'indexes' => array(
'PRIMARY' => array('column' => 'id', 'unique' => 1)
),
'tableParameters' => array('charset' => 'latin1', 'collate' => 'latin1_swedish_ci', 'engine' => 'InnoDB')
);

public $servers = array(
'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'unsigned' => true, 'key' => 'primary'),
'name' => array('type' => 'string', 'null' => false, 'length' => 20, 'collate' => 'latin1_swedish_ci', 'charset' => 'latin1'),
Expand Down
70 changes: 49 additions & 21 deletions app/Controller/AppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,16 @@ public function beforeFilter()
if ($event->isStopped())
return $event->result;
}
$LoginCondition = ($this->here != "/login") || !$this->EyPlugin->isInstalled('phpierre.signinup');
$LoginCondition = ($this->here != "/login") || !$this->EyPlugin->isInstalled('phpierre.signinup');
// Maintenance / Bans
// lowercase to avoid errors when the controller is called with uppercase
// lowercase to avoid errors when the controller is called with uppercase
$this->params['controller'] = strtolower($this->params['controller']);
$this->params['action'] = strtolower($this->params['action']);
if ($this->isConnected AND $this->User->getKey('rank') == 5 AND $this->params['controller'] != "maintenance" AND $this->params['action'] != "logout" AND $this->params['controller'] != "api")
if ($this->isConnected and $this->User->getKey('rank') == 5 and $this->params['controller'] != "maintenance" and $this->params['action'] != "logout" and $this->params['controller'] != "api")
$this->redirect(array('controller' => 'maintenance', 'action' => 'index/banned', 'plugin' => false, 'admin' => false));
else if ($this->params['controller'] != "user" && $this->params['controller'] != "maintenance" && $this->Configuration->getKey('maintenance') != '0' && !$this->Permissions->can('BYPASS_MAINTENANCE') && $LoginCondition)
$this->redirect(array('controller' => 'maintenance', 'action' => 'index', 'plugin' => false, 'admin' => false));

}

public function __initConfiguration()
Expand Down Expand Up @@ -128,7 +129,7 @@ public function __initConfiguration()
// Variables
$google_analytics = $this->Configuration->getKey('google_analytics');
$configuration_end_code = $this->Configuration->getKey('end_layout_code');
$condition = $this->Configuration->getKey('condition');
$condition = $this->Configuration->getKey('condition');

$this->loadModel('SocialButton');
$findSocialButtons = $this->SocialButton->find('all');
Expand All @@ -138,7 +139,7 @@ public function __initConfiguration()

$this->set(compact(
'reCaptcha',
'condition',
'condition',
'website_name',
'theme_config',
'facebook_link',
Expand Down Expand Up @@ -216,16 +217,16 @@ public function __initAdminNavbar()
'permission' => 'MANAGE_CONFIGURATION',
'route' => ['controller' => 'configuration', 'action' => 'index', 'admin' => true, 'plugin' => false]
],
'STATS__TITLE' => [
'icon' => 'far fa-chart-bar',
'permission' => 'VIEW_STATISTICS',
'route' => ['controller' => 'statistics', 'action' => 'index', 'admin' => true, 'plugin' => false]
],
'MAINTENANCE__TITLE' => [
'icon' => 'fas fa-hand-paper',
'permission' => 'MANAGE_MAINTENANCE',
'route' => ['controller' => 'maintenance', 'action' => 'index', 'admin' => true, 'plugin' => false]
],
'STATS__TITLE' => [
'icon' => 'far fa-chart-bar',
'permission' => 'VIEW_STATISTICS',
'route' => ['controller' => 'statistics', 'action' => 'index', 'admin' => true, 'plugin' => false]
],
'MAINTENANCE__TITLE' => [
'icon' => 'fas fa-hand-paper',
'permission' => 'MANAGE_MAINTENANCE',
'route' => ['controller' => 'maintenance', 'action' => 'index', 'admin' => true, 'plugin' => false]
],
]
],
'GLOBAL__CUSTOMIZE' => [
Expand All @@ -246,6 +247,11 @@ public function __initAdminNavbar()
'permission' => 'MANAGE_NAV',
'route' => ['controller' => 'navbar', 'action' => 'index', 'admin' => true, 'plugin' => false]
],
'SEO__TITLE' => [
'icon' => 'fab fa-google',
'permission' => 'MANAGE_SEO',
'route' => ['controller' => 'seo', 'action' => 'index', 'admin' => true, 'plugin' => false]
],
'MOTD__TITLE' => [
'icon' => 'fas fa-sort-amount-up-alt',
'permission' => 'MANAGE_MOTD',
Expand Down Expand Up @@ -321,7 +327,8 @@ public function __initAdminNavbar()

// Functions
if (!function_exists('addToNav')) {
function addToArrayAt($where, $index, $array) {
function addToArrayAt($where, $index, $array)
{
return array_slice($where, 0, $index, true) +
$array +
array_slice($where, $index, count($where) - $index, true);
Expand Down Expand Up @@ -421,17 +428,17 @@ public function __initServerInfos()
'{ONLINE}' => @$server_infos['GET_PLAYER_COUNT'],
'{ONLINE_LIMIT}' => @$server_infos['GET_MAX_PLAYERS']
)), 'server_infos' => $server_infos]);

}

public function __initWebsiteInfos()
{
$this->loadModel('User');
$this->loadModel('Visit');
$users_count = $this->User->find('count');
$users_last = $this->User->find('first', array('order' =>'created DESC'));
$users_last = $this->User->find('first', array('order' => 'created DESC'));
$users_last = $users_last['User'];
$users_count_today = $this->User->find('count', array('conditions' => array('created LIKE' => date('Y-m-d').'%')));
$users_count_today = $this->User->find('count', array('conditions' => array('created LIKE' => date('Y-m-d') . '%')));
$visits_count = $this->Visit->getVisitsCount();
$visits_count_today = $this->Visit->getVisitsByDay(date('Y-m-d'))['count'];
$admin_dark_mode = $this->Cookie->read('use_admin_dark_mode');
Expand All @@ -441,6 +448,7 @@ public function __initWebsiteInfos()

public function beforeRender()
{
$this->__initSeoConfiguration();
$event = new CakeEvent('onLoadPage', $this, $this->request->data);
$this->getEventManager()->dispatch($event);
if ($event->isStopped()) {
Expand All @@ -457,6 +465,25 @@ public function beforeRender()
}
}
$this->__setTheme();


}

public function __initSeoConfiguration()
{
$this->loadModel('Seo');
$default = $this->Seo->find('first', ["conditions" => ['page' => null]])['Seo'];
$current_url = $this->here;
$get_page = $this->Seo->find('first', ["conditions" => ['page' => $current_url]])['Seo'];
$seo_config['title'] = (!empty($get_page['title'])) ? $get_page['title'] : $default['title'];
$seo_config['description'] = (!empty($get_page['description'])) ? $get_page['description'] : $default['description'];
$seo_config['img_url'] = (!empty($get_page['img_url'])) ? $get_page['img_url'] : $default['img_url'];
$seo_config['favicon_url'] = (!empty($get_page['favicon_url'])) ? $get_page['favicon_url'] : $default['favicon_url'];
$seo_config['img_url'] = (empty($seo_config['img_url'])) ? $seo_config['favicon_url'] : $seo_config['img_url'];
$seo_config['title'] = str_replace(["{TITLE}", "{WEBSITE_NAME}"], [$this->viewVars['title_for_layout'], $this->viewVars['website_name']], $seo_config['title']);

$this->set(compact('seo_config'));

}

public function afterFilter()
Expand All @@ -467,11 +494,12 @@ public function afterFilter()
$this->__setTheme();
return $event->result;
}

}

protected function __setTheme()
{
if (!isset($this->params['prefix']) OR $this->params['prefix'] !== "admin")
if (!isset($this->params['prefix']) or $this->params['prefix'] !== "admin")
$this->theme = Configure::read('theme');
}

Expand Down
1 change: 1 addition & 0 deletions app/Controller/Component/PermissionsComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class PermissionsComponent extends CakeObject
'MANAGE_SLIDER',
'MANAGE_PAGE',
'MANAGE_NAV',
'MANAGE_SEO',
'BYPASS_MAINTENANCE',
'MANAGE_MAINTENANCE',
'MANAGE_CONFIGURATION',
Expand Down
179 changes: 179 additions & 0 deletions app/Controller/SeoController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
<?php

class SeoController extends AppController
{

public $components = array('Session');

public function admin_index()
{
if (!$this->isConnected || !$this->Permissions->can('MANAGE_SEO'))
throw new ForbiddenException();
$this->set('title_for_layout', $this->Lang->get('SEO__TITLE'));
$this->layout = 'admin';
$this->loadModel('Seo');
$default = $this->Seo->find('first', ["conditions" => ['page' => null]])['Seo'];

$seo_other = $this->Seo->find('all', ["conditions" => ['NOT' => ['page' => null]]]);
$this->set(compact('default', 'seo_other'));
}

public function admin_edit_default()
{
if (!$this->isConnected || !$this->Permissions->can('MANAGE_SEO') || !$this->request->is('post'))
throw new ForbiddenException();
$this->autoRender = false;
$this->response->type('json');
$this->loadModel('Seo');

$default = $this->Seo->find('first', ["conditions" => ['page' => null]])['Seo'];

if (!$this->request->data['img_edit']) {
$already_uploaded = (isset($this->request->data['img-uploaded']));
if ($already_uploaded) {
$this->request->data['favicon_url'] = Router::url('/') . 'img' . DS . 'uploads' . DS . $this->request->data['img-uploaded'];
} else {
$isValidImg = $this->Util->isValidImage($this->request, array('png', 'jpg', 'jpeg'));
if (!$isValidImg['status']) {
$this->response->body(json_encode(array('statut' => false, 'msg' => $isValidImg['msg'])));
return;
} else {
$infos = $isValidImg['infos'];
}

$time = date('Y-m-d_His');

$url_img = WWW_ROOT . 'img' . DS . 'uploads' . DS . 'favicons' . DS . $time . '.' . $infos['extension'];

if (!$this->Util->uploadImage($this->request, $url_img)) {
$this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('FORM__ERROR_WHEN_UPLOAD'))));
return;
}
$this->request->data['favicon_url'] = Router::url('/') . 'img' . DS . 'uploads' . DS . 'favicons' . DS . $time . '.' . $infos['extension'];
}


}


$this->Seo->read(null, $default['id']);
$this->Seo->set($this->request->data);
$this->Seo->save();

$this->response->body(json_encode(array('statut' => true, 'msg' => $this->Lang->get('SEO__EDIT_SUCCESS'))));

}


public function admin_add()
{
if (!$this->isConnected || !$this->Permissions->can('MANAGE_SEO'))
throw new ForbiddenException();
$this->set('title_for_layout', $this->Lang->get('SEO__TITLE'));
$this->layout = 'admin';
if ($this->request->is('post')) {
$this->autoRender = false;
$this->response->type('json');
if (!$this->request->data['img_edit']) {
$already_uploaded = (isset($this->request->data['img-uploaded']));
if ($already_uploaded) {
$this->request->data['favicon_url'] = Router::url('/') . 'img' . DS . 'uploads' . DS . $this->request->data['img-uploaded'];
} else {
$isValidImg = $this->Util->isValidImage($this->request, array('png', 'jpg', 'jpeg'));
if (!$isValidImg['status']) {
$this->response->body(json_encode(array('statut' => false, 'msg' => $isValidImg['msg'])));
return;
} else {
$infos = $isValidImg['infos'];
}

$time = date('Y-m-d_His');

$url_img = WWW_ROOT . 'img' . DS . 'uploads' . DS . 'favicons' . DS . $time . '.' . $infos['extension'];

if (!$this->Util->uploadImage($this->request, $url_img)) {
$this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('FORM__ERROR_WHEN_UPLOAD'))));
return;
}
$this->request->data['favicon_url'] = Router::url('/') . 'img' . DS . 'uploads' . DS . 'favicons' . DS . $time . '.' . $infos['extension'];
}
}

if (empty($this->request->data['page']) || (empty($this->request->data['title']) && empty($this->request->data['description']) && empty($this->request->data['favicon_url']) && empty($this->request->data['img-url'])))
return $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('ERROR__FILL_ALL_FIELDS'))));
$this->Seo->create();
$this->Seo->set($this->request->data);
$this->Seo->save();

$this->response->body(json_encode(array('statut' => true, 'msg' => $this->Lang->get('SEO__PAGE_ADD_SUCCESS'))));

}

}


public function admin_edit($id = false)
{
if (!$this->isConnected || !$this->Permissions->can('MANAGE_SEO') || !$id)
throw new ForbiddenException();
$this->set('title_for_layout', $this->Lang->get('SEO__TITLE'));
$this->layout = 'admin';
$this->loadModel('Seo');
$page = $this->Seo->find('first', ["conditions" => ['id' => $id]])["Seo"];
$this->set(compact('page'));

if ($this->request->is('post')) {
$this->autoRender = false;
$this->response->type('json');

if (!$this->request->data['img_edit']) {
$already_uploaded = (isset($this->request->data['img-uploaded']));
if ($already_uploaded) {
$this->request->data['favicon_url'] = Router::url('/') . 'img' . DS . 'uploads' . DS . $this->request->data['img-uploaded'];
} else {
$isValidImg = $this->Util->isValidImage($this->request, array('png', 'jpg', 'jpeg'));
if (!$isValidImg['status']) {
$this->response->body(json_encode(array('statut' => false, 'msg' => $isValidImg['msg'])));
return;
} else {
$infos = $isValidImg['infos'];
}

$time = date('Y-m-d_His');

$url_img = WWW_ROOT . 'img' . DS . 'uploads' . DS . 'favicons' . DS . $time . '.' . $infos['extension'];

if (!$this->Util->uploadImage($this->request, $url_img)) {
$this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('FORM__ERROR_WHEN_UPLOAD'))));
return;
}
$this->request->data['favicon_url'] = Router::url('/') . 'img' . DS . 'uploads' . DS . 'favicons' . DS . $time . '.' . $infos['extension'];
}
}

if (empty($this->request->data['page']) || (empty($this->request->data['title']) && empty($this->request->data['description']) && empty($this->request->data['favicon_url']) && empty($this->request->data['img-url'])))
return $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('ERROR__FILL_ALL_FIELDS'))));


$this->Seo->read(null, $page['id']);
$this->Seo->set($this->request->data);
$this->Seo->save();

$this->response->body(json_encode(array('statut' => true, 'msg' => $this->Lang->get('SEO__EDIT_SUCCESS'))));
}

}

public function admin_delete($id = false)
{
$this->autoRender = false;
if (!$this->isConnected || !$this->Permissions->can('MANAGE_SEO') || !$id)
throw new ForbiddenException();

$this->loadModel('Seo');
$this->Seo->delete($id);
$this->Session->setFlash($this->Lang->get('SEO__PAGE_DELETE_SUCCESS'), 'default.success');
$this->redirect(array('controller' => 'seo', 'action' => 'index', 'admin' => true));
}

}
5 changes: 5 additions & 0 deletions app/Model/Seo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
class Seo extends AppModel {

public $useTable = "seo";
}
10 changes: 7 additions & 3 deletions app/View/Layouts/default.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="Eywek">

<title><?= (isset($title_for_layout)) ? $title_for_layout : 'Error' ?> - <?= (isset($website_name)) ? $website_name : 'MineWeb' ?></title>
<title><?= $seo_config['title'] ?></title>
<link rel="icon" type="image/png" href="<?= $seo_config['favicon_url'] ?>"/>
<meta name="title" content="<?= $seo_config['title'] ?>">
<meta property="og:title" content="<?= $seo_config['title'] ?>">
<meta name="description" content="<?= $seo_config['description'] ?>">
<meta property="og:description" content="<?= $seo_config['description'] ?>">
<meta property="og:image" content="<?= $seo_config['img_url'] ?>">

<!-- Font Awesome 5 -->
<script src="https://kit.fontawesome.com/fb032ab5a6.js" crossorigin="anonymous"></script>
Expand All @@ -21,7 +26,6 @@
<link href='https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,700italic,400,300,700' rel='stylesheet' type='text/css'>
<?= $this->Html->script('jquery-1.11.0.js') ?>
<?= $this->Html->script('easy_paginate.js') ?>
<link rel="icon" type="image/png" href="<?= (isset($theme_config) && isset($theme_config['favicon_url'])) ? $theme_config['favicon_url'] : '' ?>" />

<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
Expand Down
Loading

0 comments on commit d700e52

Please sign in to comment.