Skip to content

Instantly share code, notes, and snippets.

@digitalbricks
Last active July 5, 2023 05:12
Show Gist options
  • Save digitalbricks/85c0c013d6d216640dbc5ff3c0910eaf to your computer and use it in GitHub Desktop.
Save digitalbricks/85c0c013d6d216640dbc5ff3c0910eaf to your computer and use it in GitHub Desktop.
WBCE CMS permalinks
<?php
/**
* @const string WB_URL
* @const string PAGES_DIRECTORY
* @const string PAGE_EXTENSION
* @const string TP alias for TABLE_PREFIX
*/
/**
* USAGE: put this file in the root directory of your WBCE installation,
* then call it like this: http://yourdomain.com/perma.php?p=123
* where 123 is the page id of the page you want to redirect to.
* (and perma.php is the name of this file)
*/
// load config file to access WB constants
require_once('config.php');
// this is the fallback url if no page is found
$fallback_url = WB_URL;
// check if page id is given and numeric, redirect to fallback if not
if(!array_key_exists('p',$_GET) || !is_numeric($_GET['p'])){
sendRedirect($fallback_url);
}
// get page id
$page_id = intval($_GET['p']);
// query database for given page (thanks to @stefanek)
// @credits: https://forum.wbce.org/viewtopic.php?pid=42377#p42377
$queryPage = "SELECT * FROM `{TP}pages` WHERE `page_id` = {$page_id}";
$data = $database->get_array($queryPage);
if(!array_key_exists(0, $data)){
sendRedirect($fallback_url);
}
$page_info = $data[0];
// check if page is visible, redirect to fallback if not
if($page_info['visibility'] != 'public'){
sendRedirect($fallback_url);
}
// if not exited yet, we can built the URL
$segments = $page_info['link'];
$url = WB_URL.PAGES_DIRECTORY.$segments.PAGE_EXTENSION;
// finally redirect to the page
sendRedirect($url);
/**
* sendRedirect
* Sends a redirect header and exits the script.
*
* @param string $url
* @param bool $permanent true = 301, false = 302
* @return void
*/
function sendRedirect(string $url, bool $permanent = true)
{
if (headers_sent() === false){
header('Location: ' . $url, true, ($permanent === true) ? 301 : 302);
}
exit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment