initial conversion to static app class

This commit is contained in:
redmatrix 2016-03-31 13:00:04 -07:00
parent 1cd3b41825
commit 256c228efd

396
boot.php
View File

@ -653,61 +653,63 @@ class ZotlabsAutoloader {
*/ */
class App { class App {
public $install = false; // true if we are installing the software public static $install = false; // true if we are installing the software
public $account = null; // account record of the logged-in account public static $account = null; // account record of the logged-in account
public $channel = null; // channel record of the current channel of the logged-in account public static $channel = null; // channel record of the current channel of the logged-in account
public $observer = null; // xchan record of the page observer public static $observer = null; // xchan record of the page observer
public $profile_uid = 0; // If applicable, the channel_id of the "page owner" public static $profile_uid = 0; // If applicable, the channel_id of the "page owner"
public $poi = null; // "person of interest", generally a referenced connection public static $poi = null; // "person of interest", generally a referenced connection
private $oauth_key = null; // consumer_id of oauth request, if used private static $oauth_key = null; // consumer_id of oauth request, if used
public $layout = array(); // Comanche parsed template public static $layout = array(); // Comanche parsed template
public $pdl = null; // Comanche page description public static $pdl = null; // Comanche page description
private $perms = null; // observer permissions private static $perms = null; // observer permissions
private $widgets = array(); // widgets for this page private static $widgets = array(); // widgets for this page
public $groups; public static $groups;
public $language; public static $language;
public $module_loaded = false; public static $module_loaded = false;
public $query_string; public static $query_string;
public $config; // config cache public static $config; // config cache
public $page; public static $page;
public $profile; public static $profile;
public $user; public static $user;
public $cid; public static $cid;
public $contact; public static $contact;
public $contacts; public static $contacts;
public $content; public static $content;
public $data = array(); public static $data = array();
public $error = false; public static $error = false;
public $cmd; public static $cmd;
public $argv; public static $argv;
public $argc; public static $argc;
public $module; public static $module;
public $pager; public static $pager;
public $strings; public static $strings;
public $hooks; public static $hooks;
public $timezone; public static $timezone;
public $interactive = true; public static $interactive = true;
public $plugins; public static $plugins;
private $apps = array(); private static $apps = array();
public $identities; public static $identities;
public $css_sources = array(); public static $css_sources = array();
public $js_sources = array(); public static $js_sources = array();
public $theme_info = array(); public static $theme_info = array();
public $is_sys = false; public static $is_sys = false;
public $nav_sel; public static $nav_sel;
public static $is_mobile = false;
public static $is_tablet = false;
public $category; public static $category;
// Allow themes to control internal parameters // Allow themes to control internal parameters
// by changing App values in theme.php // by changing App values in theme.php
public $sourcename = ''; public static $sourcename = '';
public $videowidth = 425; public static $videowidth = 425;
public $videoheight = 350; public static $videoheight = 350;
public $force_max_items = 0; public static $force_max_items = 0;
public $theme_thread_allow = true; public static $theme_thread_allow = true;
/** /**
* @brief An array for all theme-controllable parameters * @brief An array for all theme-controllable parameters
@ -715,7 +717,7 @@ class App {
* Mostly unimplemented yet. Only options 'template_engine' and * Mostly unimplemented yet. Only options 'template_engine' and
* beyond are used. * beyond are used.
*/ */
private $theme = array( private static $theme = array(
'sourcename' => '', 'sourcename' => '',
'videowidth' => 425, 'videowidth' => 425,
'videoheight' => 350, 'videoheight' => 350,
@ -728,33 +730,33 @@ class App {
/** /**
* @brief An array of registered template engines ('name'=>'class name') * @brief An array of registered template engines ('name'=>'class name')
*/ */
public $template_engines = array(); public static $template_engines = array();
/** /**
* @brief An array of instanced template engines ('name'=>'instance') * @brief An array of instanced template engines ('name'=>'instance')
*/ */
public $template_engine_instance = array(); public static $template_engine_instance = array();
private $ldelim = array( private static $ldelim = array(
'internal' => '', 'internal' => '',
'smarty3' => '{{' 'smarty3' => '{{'
); );
private $rdelim = array( private static $rdelim = array(
'internal' => '', 'internal' => '',
'smarty3' => '}}' 'smarty3' => '}}'
); );
// These represent the URL which was used to access the page // These represent the URL which was used to access the page
private $scheme; private static $scheme;
private $hostname; private static $hostname;
private $path; private static $path;
// This is our standardised URL - regardless of what was used // This is our standardised URL - regardless of what was used
// to access the page // to access the page
private $baseurl; private static $baseurl;
private $meta; private static $meta;
/** /**
* App constructor. * App constructor.
@ -763,11 +765,11 @@ class App {
// we'll reset this after we read our config file // we'll reset this after we read our config file
date_default_timezone_set('UTC'); date_default_timezone_set('UTC');
$this->config = array('system'=>array()); self::$config = array('system'=>array());
$this->page = array(); self::$page = array();
$this->pager= array(); self::$pager= array();
$this->query_string = ''; self::$query_string = '';
startup(); startup();
@ -778,64 +780,64 @@ class App {
. 'library/langdet' . PATH_SEPARATOR . 'library/langdet' . PATH_SEPARATOR
. '.' ); . '.' );
$this->scheme = 'http'; self::$scheme = 'http';
if(x($_SERVER,'HTTPS') && $_SERVER['HTTPS']) if(x($_SERVER,'HTTPS') && $_SERVER['HTTPS'])
$this->scheme = 'https'; self::$scheme = 'https';
elseif(x($_SERVER,'SERVER_PORT') && (intval($_SERVER['SERVER_PORT']) == 443)) elseif(x($_SERVER,'SERVER_PORT') && (intval($_SERVER['SERVER_PORT']) == 443))
$this->scheme = 'https'; self::$scheme = 'https';
if(x($_SERVER,'SERVER_NAME')) { if(x($_SERVER,'SERVER_NAME')) {
$this->hostname = $_SERVER['SERVER_NAME']; self::$hostname = $_SERVER['SERVER_NAME'];
if(x($_SERVER,'SERVER_PORT') && $_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443) if(x($_SERVER,'SERVER_PORT') && $_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443)
$this->hostname .= ':' . $_SERVER['SERVER_PORT']; self::$hostname .= ':' . $_SERVER['SERVER_PORT'];
/** /**
* Figure out if we are running at the top of a domain * Figure out if we are running at the top of a domain
* or in a sub-directory and adjust accordingly * or in a sub-directory and adjust accordingly
*/ */
$path = trim(dirname($_SERVER['SCRIPT_NAME']),'/\\'); $path = trim(dirname($_SERVER['SCRIPT_NAME']),'/\\');
if(isset($path) && strlen($path) && ($path != $this->path)) if(isset($path) && strlen($path) && ($path != self::$path))
$this->path = $path; self::$path = $path;
} }
set_include_path("include/$this->hostname" . PATH_SEPARATOR . get_include_path()); set_include_path("include/self::$hostname" . PATH_SEPARATOR . get_include_path());
if((x($_SERVER,'QUERY_STRING')) && substr($_SERVER['QUERY_STRING'], 0, 2) === "q=") { if((x($_SERVER,'QUERY_STRING')) && substr($_SERVER['QUERY_STRING'], 0, 2) === "q=") {
$this->query_string = substr($_SERVER['QUERY_STRING'], 2); self::$query_string = substr($_SERVER['QUERY_STRING'], 2);
// removing trailing / - maybe a nginx problem // removing trailing / - maybe a nginx problem
if (substr($this->query_string, 0, 1) == "/") if (substr(self::$query_string, 0, 1) == "/")
$this->query_string = substr($this->query_string, 1); self::$query_string = substr(self::$query_string, 1);
} }
if(x($_GET,'q')) if(x($_GET,'q'))
$this->cmd = trim($_GET['q'],'/\\'); self::$cmd = trim($_GET['q'],'/\\');
// unix style "homedir" // unix style "homedir"
if(substr($this->cmd, 0, 1) === '~') if(substr(self::$cmd, 0, 1) === '~')
$this->cmd = 'channel/' . substr($this->cmd, 1); self::$cmd = 'channel/' . substr(self::$cmd, 1);
/* /*
* Break the URL path into C style argc/argv style arguments for our * Break the URL path into C style argc/argv style arguments for our
* modules. Given "http://example.com/module/arg1/arg2", $this->argc * modules. Given "http://example.com/module/arg1/arg2", self::$argc
* will be 3 (integer) and $this->argv will contain: * will be 3 (integer) and self::$argv will contain:
* [0] => 'module' * [0] => 'module'
* [1] => 'arg1' * [1] => 'arg1'
* [2] => 'arg2' * [2] => 'arg2'
* *
* There will always be one argument. If provided a naked domain * There will always be one argument. If provided a naked domain
* URL, $this->argv[0] is set to "home". * URL, self::$argv[0] is set to "home".
*/ */
$this->argv = explode('/', $this->cmd); self::$argv = explode('/', self::$cmd);
$this->argc = count($this->argv); self::$argc = count(self::$argv);
if ((array_key_exists('0', $this->argv)) && strlen($this->argv[0])) { if ((array_key_exists('0', self::$argv)) && strlen(self::$argv[0])) {
$this->module = str_replace(".", "_", $this->argv[0]); self::$module = str_replace(".", "_", self::$argv[0]);
$this->module = str_replace("-", "_", $this->module); self::$module = str_replace("-", "_", self::$module);
} else { } else {
$this->argc = 1; self::$argc = 1;
$this->argv = array('home'); self::$argv = array('home');
$this->module = 'home'; self::$module = 'home';
} }
/* /*
@ -843,22 +845,22 @@ class App {
* pagination * pagination
*/ */
$this->pager['page'] = ((x($_GET,'page') && intval($_GET['page']) > 0) ? intval($_GET['page']) : 1); self::$pager['page'] = ((x($_GET,'page') && intval($_GET['page']) > 0) ? intval($_GET['page']) : 1);
$this->pager['itemspage'] = 60; self::$pager['itemspage'] = 60;
$this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage']; self::$pager['start'] = (self::$pager['page'] * self::$pager['itemspage']) - self::$pager['itemspage'];
if($this->pager['start'] < 0) if(self::$pager['start'] < 0)
$this->pager['start'] = 0; self::$pager['start'] = 0;
$this->pager['total'] = 0; self::$pager['total'] = 0;
/* /*
* Detect mobile devices * Detect mobile devices
*/ */
$mobile_detect = new Mobile_Detect(); $mobile_detect = new Mobile_Detect();
$this->is_mobile = $mobile_detect->isMobile(); self::$is_mobile = $mobile_detect->isMobile();
$this->is_tablet = $mobile_detect->isTablet(); self::$is_tablet = $mobile_detect->isTablet();
$this->head_set_icon('/images/hz-32.png'); self::head_set_icon('/images/hz-32.png');
BaseObject::set_app($this); BaseObject::set_app($this);
@ -868,146 +870,146 @@ class App {
$dc = get_declared_classes(); $dc = get_declared_classes();
foreach ($dc as $k) { foreach ($dc as $k) {
if (in_array("ITemplateEngine", class_implements($k))){ if (in_array("ITemplateEngine", class_implements($k))){
$this->register_template_engine($k); self::register_template_engine($k);
} }
} }
spl_autoload_register('ZotlabsAutoloader::loader'); spl_autoload_register('ZotlabsAutoloader::loader');
$this->meta= new Zotlabs\Web\HttpMeta(); self::$meta= new Zotlabs\Web\HttpMeta();
} }
function get_baseurl($ssl = false) { public static function get_baseurl($ssl = false) {
if(is_array($this->config) if(is_array(self::$config)
&& array_key_exists('system',$this->config) && array_key_exists('system',self::$config)
&& is_array($this->config['system']) && is_array(self::$config['system'])
&& array_key_exists('baseurl',$this->config['system']) && array_key_exists('baseurl',self::$config['system'])
&& strlen($this->config['system']['baseurl'])) { && strlen(self::$config['system']['baseurl'])) {
$url = $this->config['system']['baseurl']; $url = self::$config['system']['baseurl'];
$url = trim($url,'\\/'); $url = trim($url,'\\/');
return $url; return $url;
} }
$scheme = $this->scheme; $scheme = self::$scheme;
$this->baseurl = $scheme . "://" . $this->hostname . ((isset($this->path) && strlen($this->path)) ? '/' . $this->path : '' ); self::$baseurl = $scheme . "://" . self::$hostname . ((isset(self::$path) && strlen(self::$path)) ? '/' . self::$path : '' );
return $this->baseurl; return self::$baseurl;
} }
function set_baseurl($url) { public static function set_baseurl($url) {
if(is_array($this->config) if(is_array(self::$config)
&& array_key_exists('system',$this->config) && array_key_exists('system',self::$config)
&& is_array($this->config['system']) && is_array(self::$config['system'])
&& array_key_exists('baseurl',$this->config['system']) && array_key_exists('baseurl',self::$config['system'])
&& strlen($this->config['system']['baseurl'])) { && strlen(self::$config['system']['baseurl'])) {
$url = $this->config['system']['baseurl']; $url = self::$config['system']['baseurl'];
$url = trim($url,'\\/'); $url = trim($url,'\\/');
} }
$parsed = @parse_url($url); $parsed = @parse_url($url);
$this->baseurl = $url; self::$baseurl = $url;
if($parsed) { if($parsed) {
$this->scheme = $parsed['scheme']; self::$scheme = $parsed['scheme'];
$this->hostname = $parsed['host']; self::$hostname = $parsed['host'];
if(x($parsed,'port')) if(x($parsed,'port'))
$this->hostname .= ':' . $parsed['port']; self::$hostname .= ':' . $parsed['port'];
if(x($parsed,'path')) if(x($parsed,'path'))
$this->path = trim($parsed['path'],'\\/'); self::$path = trim($parsed['path'],'\\/');
} }
} }
function get_scheme() { public static function get_scheme() {
return $this->scheme; return self::$scheme;
} }
function get_hostname() { public static function get_hostname() {
return $this->hostname; return self::$hostname;
} }
function set_hostname($h) { public static function set_hostname($h) {
$this->hostname = $h; self::$hostname = $h;
} }
function set_path($p) { public static function set_path($p) {
$this->path = trim(trim($p), '/'); self::$path = trim(trim($p), '/');
} }
function get_path() { public static function get_path() {
return $this->path; return self::$path;
} }
function set_account($acct) { public static function set_account($acct) {
$this->account = $acct; self::$account = $acct;
} }
function get_account() { public static function get_account() {
return $this->account; return self::$account;
} }
function set_channel($channel) { public static function set_channel($channel) {
$this->channel = $channel; self::$channel = $channel;
} }
function get_channel() { public static function get_channel() {
return $this->channel; return self::$channel;
} }
function set_observer($xchan) { public static function set_observer($xchan) {
$this->observer = $xchan; self::$observer = $xchan;
} }
function get_observer() { public static function get_observer() {
return $this->observer; return self::$observer;
} }
function set_perms($perms) { public static function set_perms($perms) {
$this->perms = $perms; self::$perms = $perms;
} }
function get_perms() { public static function get_perms() {
return $this->perms; return self::$perms;
} }
function set_oauth_key($consumer_id) { public static function set_oauth_key($consumer_id) {
$this->oauth_key = $consumer_id; self::$oauth_key = $consumer_id;
} }
function get_oauth_key() { public static function get_oauth_key() {
return $this->oauth_key; return self::$oauth_key;
} }
function get_apps() { public static function get_apps() {
return $this->apps; return self::$apps;
} }
function set_apps($arr) { public static function set_apps($arr) {
$this->apps = $arr; self::$apps = $arr;
} }
function set_groups($g) { public static function set_groups($g) {
$this->groups = $g; self::$groups = $g;
} }
function get_groups() { public static function get_groups() {
return $this->groups; return self::$groups;
} }
function set_pager_total($n) { public static function set_pager_total($n) {
$this->pager['total'] = intval($n); self::$pager['total'] = intval($n);
} }
function set_pager_itemspage($n) { public static function set_pager_itemspage($n) {
$this->pager['itemspage'] = ((intval($n) > 0) ? intval($n) : 0); self::$pager['itemspage'] = ((intval($n) > 0) ? intval($n) : 0);
$this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage']; self::$pager['start'] = (self::$pager['page'] * self::$pager['itemspage']) - self::$pager['itemspage'];
} }
function build_pagehead() { public static function build_pagehead() {
$user_scalable = ((local_channel()) ? get_pconfig(local_channel(),'system','user_scalable') : 1); $user_scalable = ((local_channel()) ? get_pconfig(local_channel(),'system','user_scalable') : 1);
if ($user_scalable === false) if ($user_scalable === false)
@ -1021,36 +1023,36 @@ class App {
if($interval < 10000) if($interval < 10000)
$interval = 80000; $interval = 80000;
if(! x($this->page,'title')) if(! x(self::$page,'title'))
$this->page['title'] = $this->config['system']['sitename']; self::$page['title'] = self::$config['system']['sitename'];
if(! $this->meta->get_field('og:title')) if(! self::$meta->get_field('og:title'))
$this->meta->set('og:title',$this->page['title']); self::$meta->set('og:title',self::$page['title']);
$this->meta->set('generator', Zotlabs\Project\System::get_platform_name()); self::$meta->set('generator', Zotlabs\Project\System::get_platform_name());
/* put the head template at the beginning of page['htmlhead'] /* put the head template at the beginning of page['htmlhead']
* since the code added by the modules frequently depends on it * since the code added by the modules frequently depends on it
* being first * being first
*/ */
$tpl = get_markup_template('head.tpl'); $tpl = get_markup_template('head.tpl');
$this->page['htmlhead'] = replace_macros($tpl, array( self::$page['htmlhead'] = replace_macros($tpl, array(
'$preload_images' => $preload_images, '$preload_images' => $preload_images,
'$user_scalable' => $user_scalable, '$user_scalable' => $user_scalable,
'$baseurl' => $this->get_baseurl(), '$baseurl' => self::get_baseurl(),
'$local_channel' => local_channel(), '$local_channel' => local_channel(),
'$metas' => $this->meta->get(), '$metas' => self::$meta->get(),
'$update_interval' => $interval, '$update_interval' => $interval,
'$icon' => head_get_icon(), '$icon' => head_get_icon(),
'$head_css' => head_get_css(), '$head_css' => head_get_css(),
'$head_js' => head_get_js(), '$head_js' => head_get_js(),
'$js_strings' => js_strings(), '$js_strings' => js_strings(),
'$zid' => get_my_address(), '$zid' => get_my_address(),
'$channel_id' => $this->profile['uid'], '$channel_id' => self::$profile['uid'],
)) . $this->page['htmlhead']; )) . self::$page['htmlhead'];
// always put main.js at the end // always put main.js at the end
$this->page['htmlhead'] .= head_get_main_js(); self::$page['htmlhead'] .= head_get_main_js();
} }
/** /**
@ -1059,7 +1061,7 @@ class App {
* @param string $class * @param string $class
* @param string $name * @param string $name
*/ */
function register_template_engine($class, $name = '') { public static function register_template_engine($class, $name = '') {
if ($name === ""){ if ($name === ""){
$v = get_class_vars( $class ); $v = get_class_vars( $class );
if(x($v, "name")) $name = $v['name']; if(x($v, "name")) $name = $v['name'];
@ -1068,7 +1070,7 @@ class App {
echo "template engine <tt>$class</tt> cannot be registered without a name.\n"; echo "template engine <tt>$class</tt> cannot be registered without a name.\n";
killme(); killme();
} }
$this->template_engines[$name] = $class; self::$template_engines[$name] = $class;
} }
/** /**
@ -1079,23 +1081,23 @@ class App {
* *
* @return object Template Engine instance * @return object Template Engine instance
*/ */
function template_engine($name = ''){ public static function template_engine($name = ''){
if ($name !== "") { if ($name !== "") {
$template_engine = $name; $template_engine = $name;
} else { } else {
$template_engine = 'smarty3'; $template_engine = 'smarty3';
if (x($this->theme, 'template_engine')) { if (x(self::$theme, 'template_engine')) {
$template_engine = $this->theme['template_engine']; $template_engine = self::$theme['template_engine'];
} }
} }
if (isset($this->template_engines[$template_engine])){ if (isset(self::$template_engines[$template_engine])){
if(isset($this->template_engine_instance[$template_engine])){ if(isset(self::$template_engine_instance[$template_engine])){
return $this->template_engine_instance[$template_engine]; return self::$template_engine_instance[$template_engine];
} else { } else {
$class = $this->template_engines[$template_engine]; $class = self::$template_engines[$template_engine];
$obj = new $class; $obj = new $class;
$this->template_engine_instance[$template_engine] = $obj; self::$template_engine_instance[$template_engine] = $obj;
return $obj; return $obj;
} }
} }
@ -1108,28 +1110,28 @@ class App {
* *
* @return string * @return string
*/ */
function get_template_engine() { public static function get_template_engine() {
return $this->theme['template_engine']; return self::$theme['template_engine'];
} }
function set_template_engine($engine = 'smarty3') { public static function set_template_engine($engine = 'smarty3') {
$this->theme['template_engine'] = $engine; self::$theme['template_engine'] = $engine;
} }
function get_template_ldelim($engine = 'smarty3') { public static function get_template_ldelim($engine = 'smarty3') {
return $this->ldelim[$engine]; return self::$ldelim[$engine];
} }
function get_template_rdelim($engine = 'smarty3') { public static function get_template_rdelim($engine = 'smarty3') {
return $this->rdelim[$engine]; return self::$rdelim[$engine];
} }
function head_set_icon($icon) { public static function head_set_icon($icon) {
$this->data['pageicon'] = $icon; self::$data['pageicon'] = $icon;
} }
function head_get_icon() { public static function head_get_icon() {
$icon = $this->data['pageicon']; $icon = self::$data['pageicon'];
if(! strpos($icon,'://')) if(! strpos($icon,'://'))
$icon = z_root() . $icon; $icon = z_root() . $icon;
return $icon; return $icon;