PHP Classes

File: server.php

Recommend this page to a friend!
  Classes of Joaquin   Laravel Valet Linux   server.php   Download  
File: server.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Laravel Valet Linux
Run Laravel environment under a Linux machine
Author: By
Last change: Update of server.php
Date: 5 years ago
Size: 7,403 bytes
 

Contents

Class file image Download
<?php

/**
 * Define the user's "~/.valet" path.
 */

define('VALET_HOME_PATH', posix_getpwuid(fileowner(__FILE__))['dir'].'/.valet');
define('VALET_STATIC_PREFIX', '41c270e4-5535-4daa-b23e-c269744c2f45');

/**
 * Show the Valet 404 "Not Found" page.
 */
function show_valet_404()
{
   
http_response_code(404);
    require
__DIR__.'/cli/templates/404.html';
    exit;
}

/**
 * @param $domain string Domain to filter
 *
 * @return string Filtered domain (without xip.io feature)
 */
function valet_support_xip_io($domain)
{
    if (
substr($domain, -7) === '.xip.io') {
       
// support only ip v4 for now
       
$domainPart = explode('.', $domain);
        if (
count($domainPart) > 6) {
           
$domain = implode('.', array_reverse(array_slice(array_reverse($domainPart), 6)));
        }
    }

    return
$domain;
}

/**
 * Convert absolute path to slug.
 *
 * @param string $path
 * @return string Slug version of last folder name
 */
function valet_path_to_slug($path) {
   
$replace = [
       
'&lt;' => '', '&gt;' => '', '&#039;' => '', '&amp;' => '',
       
'&quot;' => '', 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä'=> 'Ae',
       
'&Auml;' => 'A', 'Å' => 'A', '?' => 'A', '?' => 'A', '?' => 'A', 'Æ' => 'Ae',
       
'Ç' => 'C', '?' => 'C', '?' => 'C', '?' => 'C', '?' => 'C', '?' => 'D', '?' => 'D',
       
'Ð' => 'D', 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', '?' => 'E',
       
'?' => 'E', '?' => 'E', '?' => 'E', '?' => 'E', '?' => 'G', '?' => 'G',
       
'?' => 'G', '?' => 'G', '?' => 'H', '?' => 'H', 'Ì' => 'I', 'Í' => 'I',
       
'Î' => 'I', 'Ï' => 'I', '?' => 'I', '?' => 'I', '?' => 'I', '?' => 'I',
       
'?' => 'I', '?' => 'IJ', '?' => 'J', '?' => 'K', '?' => 'K', '?' => 'K',
       
'?' => 'K', '?' => 'K', '?' => 'K', 'Ñ' => 'N', '?' => 'N', '?' => 'N',
       
'?' => 'N', '?' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O',
       
'Ö' => 'Oe', '&Ouml;' => 'Oe', 'Ø' => 'O', '?' => 'O', '?' => 'O', '?' => 'O',
       
'?' => 'OE', '?' => 'R', '?' => 'R', '?' => 'R', '?' => 'S', '?' => 'S',
       
'?' => 'S', '?' => 'S', '?' => 'S', '?' => 'T', '?' => 'T', '?' => 'T',
       
'?' => 'T', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'Ue', '?' => 'U',
       
'&Uuml;' => 'Ue', '?' => 'U', '?' => 'U', '?' => 'U', '?' => 'U', '?' => 'U',
       
'?' => 'W', 'Ý' => 'Y', '?' => 'Y', '?' => 'Y', '?' => 'Z', '?' => 'Z',
       
'?' => 'Z', 'Þ' => 'T', 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a',
       
'ä' => 'ae', '&auml;' => 'ae', 'å' => 'a', '?' => 'a', '?' => 'a', '?' => 'a',
       
'æ' => 'ae', 'ç' => 'c', '?' => 'c', '?' => 'c', '?' => 'c', '?' => 'c',
       
'?' => 'd', '?' => 'd', 'ð' => 'd', 'è' => 'e', 'é' => 'e', 'ê' => 'e',
       
'ë' => 'e', '?' => 'e', '?' => 'e', '?' => 'e', '?' => 'e', '?' => 'e',
       
'?' => 'f', '?' => 'g', '?' => 'g', '?' => 'g', '?' => 'g', '?' => 'h',
       
'?' => 'h', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', '?' => 'i',
       
'?' => 'i', '?' => 'i', '?' => 'i', '?' => 'i', '?' => 'ij', '?' => 'j',
       
'?' => 'k', '?' => 'k', '?' => 'l', '?' => 'l', '?' => 'l', '?' => 'l',
       
'?' => 'l', 'ñ' => 'n', '?' => 'n', '?' => 'n', '?' => 'n', '?' => 'n',
       
'?' => 'n', 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'oe',
       
'&ouml;' => 'oe', 'ø' => 'o', '?' => 'o', '?' => 'o', '?' => 'o', '?' => 'oe',
       
'?' => 'r', '?' => 'r', '?' => 'r', '?' => 's', 'ù' => 'u', 'ú' => 'u',
       
'û' => 'u', 'ü' => 'ue', '?' => 'u', '&uuml;' => 'ue', '?' => 'u', '?' => 'u',
       
'?' => 'u', '?' => 'u', '?' => 'u', '?' => 'w', 'ý' => 'y', 'ÿ' => 'y',
       
'?' => 'y', '?' => 'z', '?' => 'z', '?' => 'z', 'þ' => 't', 'ß' => 'ss',
       
'?' => 'ss', '??' => 'iy', '?' => 'A', '?' => 'B', '?' => 'V', '?' => 'G',
       
'?' => 'D', '?' => 'E', '?' => 'YO', '?' => 'ZH', '?' => 'Z', '?' => 'I',
       
'?' => 'Y', '?' => 'K', '?' => 'L', '?' => 'M', '?' => 'N', '?' => 'O',
       
'?' => 'P', '?' => 'R', '?' => 'S', '?' => 'T', '?' => 'U', '?' => 'F',
       
'?' => 'H', '?' => 'C', '?' => 'CH', '?' => 'SH', '?' => 'SCH', '?' => '',
       
'?' => 'Y', '?' => '', '?' => 'E', '?' => 'YU', '?' => 'YA', '?' => 'a',
       
'?' => 'b', '?' => 'v', '?' => 'g', '?' => 'd', '?' => 'e', '?' => 'yo',
       
'?' => 'zh', '?' => 'z', '?' => 'i', '?' => 'y', '?' => 'k', '?' => 'l',
       
'?' => 'm', '?' => 'n', '?' => 'o', '?' => 'p', '?' => 'r', '?' => 's',
       
'?' => 't', '?' => 'u', '?' => 'f', '?' => 'h', '?' => 'c', '?' => 'ch',
       
'?' => 'sh', '?' => 'sch', '?' => '', '?' => 'y', '?' => '', '?' => 'e',
       
'?' => 'yu', '?' => 'ya'
   
];

   
// make a human readable string
   
$slug = strtr(basename($path), $replace);

   
// replace non letter or digits by -
   
$slug = preg_replace('~[^\\pL\d.]+~u', '-', $slug);

   
// trim
   
$slug = trim($slug, '-');

   
// remove unwanted characters
   
$slug = preg_replace('~[^-\w.]+~', '', $slug);

    return
strtolower($slug);
}

/**
 * Load the Valet configuration.
 */
$valetConfig = json_decode(
   
file_get_contents(VALET_HOME_PATH.'/config.json'), true
);

/**
 * Parse the URI and site / host for the incoming request.
 */
$uri = urldecode(
   
explode("?", $_SERVER['REQUEST_URI'])[0]
);

$siteName = basename(
   
// Filter host to support xip.io feature
   
valet_support_xip_io(explode(':',strtolower($_SERVER['HTTP_HOST']))[0]),
   
'.'.$valetConfig['domain']
);

if (
strpos($siteName, 'www.') === 0) {
   
$siteName = substr($siteName, 4);
}

/**
 * Determine the fully qualified path to the site.
 */
$valetSitePath = null;

foreach (
$valetConfig['paths'] as $path) {
   
$domain = ($pos = strrpos($siteName, '.')) !== false
               
? substr($siteName, $pos+1)
                :
null;

    foreach (
glob($path . '/*', GLOB_ONLYDIR) as $dirPath) {
       
$slug = valet_path_to_slug($dirPath);

        if (
$slug == $siteName || $slug == $domain) {
           
$valetSitePath = $dirPath;

            break
2;
        }
    }
}

if (
is_null($valetSitePath)) {
   
show_valet_404();
}

/**
 * Find the appropriate Valet driver for the request.
 */
$valetDriver = null;

require
__DIR__.'/cli/drivers/require.php';

$valetDriver = ValetDriver::assign($valetSitePath, $siteName, $uri);

if (!
$valetDriver) {
   
show_valet_404();
}

/**
 * Overwrite the HTTP host for Ngrok.
 */
if (isset($_SERVER['HTTP_X_ORIGINAL_HOST'])) {
   
$_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_ORIGINAL_HOST'];
}

/**
 * Allow driver to mutate incoming URL.
 */
$uri = $valetDriver->mutateUri($uri);

/**
 * Determine if the incoming request is for a static file.
 */
$isPhpFile = pathinfo($uri, PATHINFO_EXTENSION) === 'php';

if (
$uri !== '/' && ! $isPhpFile && $staticFilePath = $valetDriver->isStaticFile($valetSitePath, $siteName, $uri)) {
    return
$valetDriver->serveStaticFile($staticFilePath, $valetSitePath, $siteName, $uri);
}

/**
 * Attempt to dispatch to a front controller.
 */
$frontControllerPath = $valetDriver->frontControllerPath(
   
$valetSitePath, $siteName, $uri
);

if (!
$frontControllerPath) {
   
show_valet_404();
}

chdir(dirname($frontControllerPath));

require
$frontControllerPath;