PHP Classes

File: aksara/Helpers/url_helper.php

Recommend this page to a friend!
  Packages of Aby Dahana   Aksara   aksara/Helpers/url_helper.php   Download  
File: aksara/Helpers/url_helper.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Aksara
A CodeIgniter based API and CRUD generator
Author: By
Last change: Page Builder: Finalized component precision styling, fixed property TypeErrors, synchronized media previews, and optimized layout spacing across Card, Alert, and CTA components. (CS-Fix included)
Date: 3 days ago
Size: 7,387 bytes
 

Contents

Class file image Download
<?php

/**
 * This file is part of Aksara CMS, both framework and publishing
 * platform.
 *
 * @author Aby Dahana <abydahana@gmail.com>
 * @copyright (c) Aksara Laboratory <https://aksaracms.com>
 * @license MIT License
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the LICENSE.txt file.
 *
 * When the signs come, those who don't believe at "that time"
 * will have only two choices, commit suicide or become brutal.
 */

use CodeIgniter\HTTP\SiteURI;

if (!
function_exists('base_url')) {
   
/**
     * Base URL
     *
     * Create a local URL based on your basepath.
     * Segments can be passed in as a string or an array, same as site_url
     * or a URL to a file can be passed in, e.g. to an image file.
     *
     * @param string $path
     */
   
function base_url(string|array|null $path = null, ?array $params = []): string
   
{
       
$request = service('request');

        if (
is_array($path)) {
           
$path = implode('/', array_values($path));
        }

        if (
is_object($params)) {
           
$params = (array) $params;
        }

        if (!
is_array($params)) {
           
$params = [];
        }

        if (
$params || ('preview-theme' == $request->getGet('aksara_mode') && sha1($request->getGet('aksara_theme') . ENCRYPTION_KEY . get_userdata('session_generated')) == $request->getGet('integrity_check') && is_dir(ROOTPATH . 'themes/' . $request->getGet('aksara_theme')))) {
           
$params = array_merge($request->getGet(), $params);
        }

        if (! empty(
$params)) {
           
// Unset old token
           
unset($params['aksara']);

           
$query_params = [];

            foreach (
$params as $key => $val) {
                if (!
$val || in_array($key, $params) && ! $params[$key]) {
                    continue;
                }

               
$query_params[$key] = $val;
            }

           
// Generate token
           
$token = generate_token($path, $query_params);

            if (
$query_params && $token) {
               
$query_params = array_merge(['aksara' => $token], $query_params);
            }

           
$query_string = ($query_params ? '?' . str_replace(['%7B', '%7D'], ['{', '}'], http_build_query($query_params)) : '');
        } else {
           
$query_string = '';
        }

       
$currentURI = service('request')->getUri();

       
assert($currentURI instanceof SiteURI);

        if ((
service('request')->getServer('HTTP_MOD_REWRITE') && strtolower(service('request')->getServer('HTTP_MOD_REWRITE')) == 'on') || (function_exists('apache_get_modules') && in_array('mod_rewrite', apache_get_modules())) || ($path && file_exists(FCPATH . $path))) {
           
$final_url = $currentURI->baseUrl(($path ? rtrim($path, '/') : '')) . $query_string;
        } else {
           
$final_url = $currentURI->baseUrl((config('App')->indexPage ? config('App')->indexPage . '/' : null) . ($path ? rtrim($path, '/') : '')) . $query_string;
        }

        return
str_replace(['%7B', '%7D'], ['{', '}'], $final_url);
    }
}

if (!
function_exists('current_page')) {
   
/**
     * Current Page
     *
     * Get the current page URL and add the add extra parameter
     * on it.
     *
     * @param string $method
     */
   
function current_page(string|array|null $method = null, ?array $params = [], ?string $unset = null): string
   
{
        if (
is_object($params)) {
           
$params = (array) $params;
        }
        if (!
is_array($params)) {
           
$params = [];
        }

        if (
$unset && isset($params[$unset])) {
            unset(
$params[$unset]);
        }

       
$params = array_merge(service('request')->getGet(), $params);

        if (! empty(
$params)) {
           
// Unset old token
           
unset($params['aksara']);

           
$query_params = [];

            foreach (
$params as $key => $val) {
                if (!
$val || in_array($key, $params) && ! $params[$key]) {
                    continue;
                }

               
$query_params[$key] = $val;
            }

           
// Generate token
           
$token = generate_token(uri_string() . ($method ? '/' . $method : null), $query_params);

            if (
$query_params && $token) {
               
$query_params = array_merge(['aksara' => $token], $query_params);
            }

           
$query_string = ($query_params ? '?' . str_replace(['%7B', '%7D'], ['{', '}'], http_build_query($query_params)) : '');
           
$final_url = base_url(uri_string()) . ($method ? '/' . $method : null) . $query_string;
        } else {
           
$final_url = base_url(uri_string()) . ($method ? '/' . $method : null);
        }

        return
str_replace(['%7B', '%7D'], ['{', '}'], $final_url);
    }
}

if (!
function_exists('go_to')) {
   
/**
     * Go To
     *
     * Generate the next page from the current page and add
     * extra parameter on it.
     *
     * @param string $method
     */
   
function go_to(string|array|null $method = null, array $params = []): string
   
{
        if (
is_array($method)) {
           
$method = implode('/', $method);
        }

        if (
is_object($params)) {
           
$params = (array) $params;
        }

        if (!
is_array($params)) {
           
$params = [];
        }

       
$slug = strtolower(str_replace('\\', '/', service('router')->controllerName()));
       
$slug = preg_replace(['/\/aksara\/modules\//', '/\/modules\//', '/\/controllers\//'], ['', '', '/'], $slug, 1);

       
$destructure = explode('/', $slug ?? '');

       
$final_slug = [];
       
$previous_segment = null;

        foreach (
$destructure as $key => $val) {
            if (
$val != $previous_segment) {
               
$final_slug[] = $val;
            }

           
$previous_segment = $val;
        }

       
$final_slug = implode('/', $final_slug);

       
$params = array_merge(service('request')->getGet(), $params);

        if (! empty(
$params)) {
           
// Unset old token
           
unset($params['aksara']);

           
$query_params = [];

            foreach (
$params as $key => $val) {
                if (!
$val || in_array($key, $params) && ! $params[$key]) {
                    continue;
                }

               
$query_params[$key] = $val;
            }

           
// Generate token
           
$token = generate_token($final_slug . ($method ? '/' . $method : null), $query_params);

            if (
$query_params && $token) {
               
$query_params = array_merge(['aksara' => $token], $query_params);
            }

           
$query_string = ($query_params ? '?' . str_replace(['%7B', '%7D'], ['{', '}'], http_build_query($query_params)) : '');
           
$uri = $final_slug . ($method ? '/' . $method : null);
        } else {
           
$query_string = '';
           
$uri = $final_slug . ($method ? '/' . $method : null);
        }

        return
str_replace(['%7B', '%7D'], ['{', '}'], base_url($uri) . $query_string);
    }
}

if (!
function_exists('asset_url')) {
   
/**
     * Asset URL
     *
     * Create a local URL based on your basepath.
     * Segments can be passed in as a string or an array, same as site_url
     * or a URL to a file can be passed in, e.g. to an image file.
     */
   
function asset_url(string $file): string
   
{
        return
base_url('assets/' . $file);
    }
}