<?php defined('SYSPATH') OR die('No direct access to this file is allowed.');
/**
* Site class, stores site-wide settings.
*
* @package Noostr
* @subpackage Classes
*/
class Site {
private $data = array();
public $admin = false;
public $querystring = array();
public $servername = array();
public $uri = array();
public function __construct($array) {
$this->load($array);
if (isset($_SERVER["HTTP_REFERER"])) {
$this->data['referrer'] = strtolower($_SERVER["HTTP_REFERER"]);
} else {
$this->data['referrer'] = '';
}
$this->seturi();
$this->setdomain();
if ($this->admin) {
$this->data['startpage'] = 'dashboard';
}
if ($this->data['referrer'] == '' && !defined('INSTALL')) {
if ($this->admin) {
$this->data['referrer'] = HTTP.URL.PORT.'/'.ADMIN.'/'.$this->data['startpage'];
} else {
$this->data['referrer'] = HTTP.URL.PORT.'/'.$this->data['startpage'];
}
}
}
public function load($array) {
for ($i = 0, $c = count($array); $i < $c; $i++) {
$this->data[$array[$i]['variable']] = $array[$i]['value'];
$this->data[$array[$i]['variable'].'_group'] = $array[$i]['group'];
$this->data[$array[$i]['variable'].'_type'] = $array[$i]['type'];
$this->data[$array[$i]['variable'].'_values'] = $array[$i]['values'];
$this->data[$array[$i]['variable'].'_description'] = $array[$i]['description'];
}
//print_r ($this->data);
}
public function __set($name, $value = '') {
$this->data[$name] = $value;
}
public function __get($name) {
$return = '';
if (is_array($this->data)) {
if (array_key_exists($name, $this->data)) {
$return = $this->data[$name];
if (isset($this->data[$name.'_type'])) {
switch ($this->data[$name.'_type']) {
case 'bool':
$return = (bool)$return;
break;
case 'int':
$return = (int)$return;
break;
case 'float':
$return = (float)$return;
break;
}
} else {
$return = (string)$return;
}
}
}
return $return;
}
/**
* Populates the "uri" array with the current requested path, splitting on the
* '/' character.
*/
public function seturi() {
$this->uri = explode('/', PATH);
// Remove blank members of the array.
for ($i = 0, $c = count($this->uri); $i < $c; $i++) {
if ($this->uri[$i] == '') {
unset($this->uri[$i]);
}
}
// Re-index the array so keys are sequential, starting at zero.
$this->uri = array_values($this->uri);
// If, for some reason, the final is not an array, force it.
if (!is_array($this->uri)) {
$this->uri = array($this->uri);
}
// Finally, check for "admin" as the first part of the path
if (isset($this->uri[0]) && strtolower($this->uri[0]) == strtolower(ADMIN)) {
$this->admin = true;
$this->style = ADMIN;
}
}
/**
* Returns the requested portion of the path. Returns entire path by default.
*
* @param int $s Start position in the "uri" array (default 0)
* @param int $l Number of array items to retrieve (default -1)
* @return string Requested portion of path
*/
public function geturi($s = 0, $l = -1) {
$result = '';
if ($l < 0) {
$l = count($this->uri);
}
for ($i = $s; $i < $l; $i++) {
if (isset($this->uri[$i])) {
$result .= '/'.$this->uri[$i];
}
}
if ($result == '') {
// $result = '/';
}
return $result;
}
/**
* Stores the querystring as an associative array.
*
* @param string $querystring A querystring
*/
public function setquerystring($querystring) {
parse_str($querystring, $this->querystring);
if (isset($this->querystring['loginreturn']) && $this->querystring['loginreturn'] != '') {
$this->data['referrer'] = HTTP.URL.PORT.'/'.$this->querystring['loginreturn'];
}
}
public function getquerystring($name) {
$return = null;
if (isset($this->querystring) && is_array($this->querystring) && isset($this->querystring[$name]) && $this->querystring[$name] != null && $this->querystring[$name] != '') {
$return = $this->querystring[$name];
}
return $return;
}
/**
* Stores the domain in an array, splitting on the '.' character.
*/
public function setdomain() {
$this->servername = explode('.', URL);
$this->servername = array_reverse($this->servername);
if (!isset($this->servername[2]) || $this->servername[2] == '') {
// force the "www" if it's missing
//$this->servername[2] = 'www';
}
}
/**
* Returns requested parts of the domain. Returns entire domain by default.
*
* @param int $s Start position in the "servername" array (default 0)
* @param int $l Number of parts to return (default -1)
* @return string Requested part of domain
*/
public function getdomain($s = 0, $l = -1) {
$result = '';
if ($l < 0) {
$l = count($this->servername);
}
for ($i = $l - 1; $i >= $s; $i--) {
$result .= $this->servername[$i];
if ($i > $s) {
$result .= '.';
}
}
return $result;
}
/**
* Returns the portion of the domain to the left of the root domain. For
* example, if the site URL is "www.mysite.com", this returns "www".
*
* @return string
*/
public function getvanitydomain() {
return $this->getdomain(2, count($this->servername));
}
/**
* Returns the root domain and TLD of the current domain. For example, if the
* site URL is "www.mysite.com", this returns "mysite.com".
*
* @return string
*/
public function getrootdomain() {
return $this->getdomain(0, 2);
}
public function tpl_getinfo() {
return $this->data;
}
public function tpl_getquerystringinfo() {
return $this->querystring;
}
}