PHP Classes

File: includes/siteload.php

Recommend this page to a friend!
  Classes of Barton Phillips  >  PHP Site Class  >  includes/siteload.php  >  Download  
File: includes/siteload.php
Role: Auxiliary script
Content type: text/plain
Description: Auxiliary script
Class: PHP Site Class
Create database driven Web sites
Author: By
Last change: added function_exists to the two functions
Date: 5 months ago
Size: 4,562 bytes


Class file image Download
// BLP 2021-03-09 -- Removed via comment logic for $__info to support altorouter.php
// BLP 2021-02-20 -- Added function stripComments($x) to remove comments '/*', '#' and '//' from
// json file.
// BLP 2018-04-20 -- Add $__info so altorouter.php can change things.
// This is the site loader for Composer based sites using mysitemap.json
// We first get the vendor/autoload.php
// and then we get the DOCUMENT_ROOT and PHP_SELF
// we combine DOCUMENT_ROOT and the dirname(PHP_SELF) and '/mysitemap.json'
// and return the mysitemap.json file for the site.

// Function to search for the mysitemap.json
// We pass the $mydir to the function. This is from 'SCRIPT_FILENAME' which has the absolute path
// to the target with the full DOCUMENT_ROOT plus the directory path from the docroot to the
// target.
// For example DOCUMENT_ROOT + /path/target
// So we take this and if we do not find the files at first we do a $mysite = dirname($mysite) and
// then do a chdir($mysite); This may not be DOCUMENT_ROOT + ... but may the REAL path. For Example
// if we did a symlink to DOCUMENT_ROOT + 'weewx/index.php' and the symlink were
// /extra/weewx then if we did a chdir('..') we would get to /extra which is wrong.
// What we want is /var/www/weewx to /var/wwww.

if(!function_exists("findsitemap")) {
findsitemap($mydir) {

file_exists($mydir . "/mysitemap.json")) {
// return stripComments(file_get_contents($mydir . "/mysitemap.json"));
$ret = stripComments(file_get_contents($mydir . "/mysitemap.json"));
//echo "ret: |$ret|<br>";
return $ret;
    } else {
// If we didn't find the mysitemap.json then have we reached to docroot? Or have we reached the
      // root. We should actually never reach the root.

      //echo "mydir: $mydir<br>";

if($docroot == $mydir || '/' == $mydir) {

// We are not at the root so do $mydir = dirname($mydir). For example if $mydir is
      // '/var/www/weewx' it becomes '/var/www'
      //echo "mydir: $mydir\n";
$mydir = dirname($mydir);
//chdir($mydir); // This will change the dir to something under the docroot
      // Recurse
return findsitemap($mydir);

// BLP 2021-02-20 -- Added this to remove any comments that may be in my 'mystiemap.json'

if(!function_exists("stripComments")) {
stripComments($x) {
$pat = '~".*?"(*SKIP)(*FAIL)|(?://[^\n]*)|(?:#[^\n]*)|(?:/\*.*?\*/)~s';
preg_replace($pat, "", $x);

// We are in 'vendor/bartonlp/site-class/includes' so we want to go back three directories to load
// autoload.php

require_once(__DIR__ ."/../../../autoload.php");

$old = error_reporting(E_ALL & ~(E_NOTICE | E_WARNING | E_STRICT));

// Now check to see if we have a DOCUMENT_ROOT or VIRTUALHOST_DOCUMENT_ROOT.
// If we DON't we will use PWD which should be and if SCRIPT_FILENAME is not dot (.)
// then we add it to PWD.
// This is for CLI files. For regular PHP via apache we just use the ROOT.

// This is a CLI program
  // Is SCRIPT_FILENAME an absolute path?
if(strpos($_SERVER['SCRIPT_FILENAME'], "/") === 0) {
// First character is a / so absoulte path
$mydir = dirname($_SERVER['SCRIPT_FILENAME']);
  } else {
// SCRIPT_FILENAME is NOT an absolute path
    // Use PWD and then look at SCRIPT_FILENAME
$mydir = $_SERVER['PWD'];
// If SCRIPT_FILENAME start with a dot (.) then we are in the target dir so do nothing.
    // Else we use the dirname() and append it to mydir.
if(($x = dirname($_SERVER['SCRIPT_FILENAME'])) != '.') {
$mydir .= "/$x";
} else {
// Normal apache program
  // The SCRIPT_FILENAME is always an absolute path
$mydir = dirname($_SERVER['SCRIPT_FILENAME']);


$_site = json_decode(findsitemap($mydir));

/* BLP 2021-03-09 -- No longer use PUG
// BLP 2018-04-20 -- Add $__info. For altorout.php
foreach($__info as $k=>$v) {
  $_site->$k = $v;
// BLP 2018-04-20 -- end of add

if(!$_site) {
$_site =<<<EOF
<h1>NO 'mysitemap.json' Found</h1>
<p>To run
{$_SERVER['PHP_SELF']} you must have a 'mysitemap.json' somewhere within the Document Root.</p>
error_log("ERROR: siteload.php. No 'mysitemap.json' found in " . getcwd() . " for file {$_SERVER['PHP_SELF']}");

For more information send a message to info at phpclasses dot org.