boot.php - code cleanup + doxygen

This commit is contained in:
rabuzarus 2015-12-24 01:31:17 +01:00
parent 7faa111c9b
commit 40b0d383d5

303
boot.php
View file

@ -1,4 +1,21 @@
<?php <?php
/** @file boot.php
*
* This file defines some global constants and includes the central App class.
*/
/**
* Friendica
*
* Friendica is a communications platform for integrated social communications
* utilising decentralised communications and linkage to several indie social
* projects - as well as popular mainstream providers.
*
* Our mission is to free our friends and families from the clutches of
* data-harvesting corporations, and pave the way to a future where social
* communications are free and open and flow between alternate providers as
* easily as email does today.
*/
require_once('include/config.php'); require_once('include/config.php');
require_once('include/network.php'); require_once('include/network.php');
@ -358,7 +375,8 @@ function startup() {
* *
* class: App * class: App
* *
* Our main application structure for the life of this page * @brief Our main application structure for the life of this page.
*
* Primarily deals with the URL that got us here * Primarily deals with the URL that got us here
* and tries to make some sense of it, and * and tries to make some sense of it, and
* stores our page contents and config storage * stores our page contents and config storage
@ -366,9 +384,7 @@ function startup() {
* before we spit the page out. * before we spit the page out.
* *
*/ */
class App {
if(! class_exists('App')) {
class App {
public $module_loaded = false; public $module_loaded = false;
public $query_string; public $query_string;
@ -417,10 +433,12 @@ if(! class_exists('App')) {
public $theme_thread_allow = true; public $theme_thread_allow = true;
public $theme_events_in_profile = true; public $theme_events_in_profile = true;
// An array for all theme-controllable parameters /**
// Mostly unimplemented yet. Only options 'stylesheet' and * @brief An array for all theme-controllable parameters
// beyond are used. *
* Mostly unimplemented yet. Only options 'template_engine' and
* beyond are used.
*/
public $theme = array( public $theme = array(
'sourcename' => '', 'sourcename' => '',
'videowidth' => 425, 'videowidth' => 425,
@ -431,9 +449,13 @@ if(! class_exists('App')) {
'template_engine' => 'smarty3', 'template_engine' => 'smarty3',
); );
// array of registered template engines ('name'=>'class name') /**
* @brief An array of registered template engines ('name'=>'class name')
*/
public $template_engines = array(); public $template_engines = array();
// array of instanced template engines ('name'=>'instance') /**
* @brief An array of instanced template engines ('name'=>'instance')
*/
public $template_engine_instance = array(); public $template_engine_instance = array();
private $ldelim = array( private $ldelim = array(
@ -457,6 +479,9 @@ if(! class_exists('App')) {
private $cached_profile_image; private $cached_profile_image;
private $cached_profile_picdate; private $cached_profile_picdate;
/**
* App constructor.
*/
function __construct() { function __construct() {
global $default_timezone; global $default_timezone;
@ -851,8 +876,10 @@ if(! class_exists('App')) {
/** /**
* register template engine class * @brief Register template engine class
* if $name is "", is used class static property $class::$name *
* If $name is "", is used class static property $class::$name
*
* @param string $class * @param string $class
* @param string $name * @param string $name
*/ */
@ -869,8 +896,10 @@ if(! class_exists('App')) {
} }
/** /**
* return template engine instance. If $name is not defined, * @brief Return template engine instance.
* return engine defined by theme, or default *
* If $name is not defined, return engine defined by theme,
* or default
* *
* @param strin $name Template engine name * @param strin $name Template engine name
* @return object Template Engine instance * @return object Template Engine instance
@ -899,6 +928,11 @@ if(! class_exists('App')) {
echo "template engine <tt>$template_engine</tt> is not registered!\n"; killme(); echo "template engine <tt>$template_engine</tt> is not registered!\n"; killme();
} }
/**
* @brief Returns the active template engine.
*
* @return string
*/
function get_template_engine() { function get_template_engine() {
return $this->theme['template_engine']; return $this->theme['template_engine'];
} }
@ -947,28 +981,34 @@ if(! class_exists('App')) {
return($this->is_friendica_app); return($this->is_friendica_app);
} }
}
} }
// retrieve the App structure /**
// useful in functions which require it but don't get it passed to them * @brief Retrieve the App structure
*
if(! function_exists('get_app')) { * Useful in functions which require it but don't get it passed to them
function get_app() { */
function get_app() {
global $a; global $a;
return $a; return $a;
} }
};
// Multi-purpose function to check variable state. /**
// Usage: x($var) or $x($array,'key') * @brief Multi-purpose function to check variable state.
// returns false if variable/key is not set *
// if variable is set, returns 1 if has 'non-zero' value, otherwise returns 0. * Usage: x($var) or $x($array, 'key')
// e.g. x('') or x(0) returns 0; *
* returns false if variable/key is not set
if(! function_exists('x')) { * if variable is set, returns 1 if has 'non-zero' value, otherwise returns 0.
function x($s,$k = NULL) { * e.g. x('') or x(0) returns 0;
*
* @param string|array $s variable to check
* @param string $k key inside the array to check
*
* @return bool|int
*/
function x($s,$k = NULL) {
if($k != NULL) { if($k != NULL) {
if((is_array($s)) && (array_key_exists($k,$s))) { if((is_array($s)) && (array_key_exists($k,$s))) {
if($s[$k]) if($s[$k])
@ -986,21 +1026,19 @@ if(! function_exists('x')) {
} }
return false; return false;
} }
}
} }
// called from db initialisation if db is dead.
if(! function_exists('system_unavailable')) { /**
function system_unavailable() { * @brief Called from db initialisation if db is dead.
*/
function system_unavailable() {
include('system_unavailable.php'); include('system_unavailable.php');
system_down(); system_down();
killme(); killme();
}
} }
function clean_urls() { function clean_urls() {
global $a; global $a;
// if($a->config['system']['clean_urls']) // if($a->config['system']['clean_urls'])
@ -1016,17 +1054,36 @@ function z_path() {
return $base; return $base;
} }
/**
* @brief Returns the baseurl.
*
* @see App::get_baseurl()
*
* @return string
*/
function z_root() { function z_root() {
global $a; global $a;
return $a->get_baseurl(); return $a->get_baseurl();
} }
/**
* @brief Return absolut URL for given $path.
*
* @param string $path
*
* @return string
*/
function absurl($path) { function absurl($path) {
if(strpos($path,'/') === 0) if(strpos($path,'/') === 0)
return z_path() . $path; return z_path() . $path;
return $path; return $path;
} }
/**
* @brief Function to check if request was an AJAX (xmlhttprequest) request.
*
* @return boolean
*/
function is_ajax() { function is_ajax() {
return (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'); return (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
} }
@ -1044,13 +1101,11 @@ function check_db() {
} }
/**
* Sets the base url for use in cmdline programs which don't have
// Sets the base url for use in cmdline programs which don't have * $_SERVER variables
// $_SERVER variables */
function check_url(&$a) {
if(! function_exists('check_url')) {
function check_url(&$a) {
$url = get_config('system','url'); $url = get_config('system','url');
@ -1066,14 +1121,13 @@ if(! function_exists('check_url')) {
$url = set_config('system','url',$a->get_baseurl()); $url = set_config('system','url',$a->get_baseurl());
return; return;
}
} }
// Automatic database updates /**
* @brief Automatic database updates
if(! function_exists('update_db')) { */
function update_db(&$a) { function update_db(&$a) {
$build = get_config('system','build'); $build = get_config('system','build');
if(! x($build)) if(! x($build))
$build = set_config('system','build',DB_UPDATE_VERSION); $build = set_config('system','build',DB_UPDATE_VERSION);
@ -1132,10 +1186,9 @@ if(! function_exists('update_db')) {
} }
return; return;
}
} }
if(!function_exists('run_update_function')){
function run_update_function($x) { function run_update_function($x) {
if(function_exists('update_' . $x)) { if(function_exists('update_' . $x)) {
// There could be a lot of processes running or about to run. // There could be a lot of processes running or about to run.
@ -1174,12 +1227,10 @@ if(!function_exists('run_update_function')){
return true; return true;
} }
return true; return true;
}
} }
if(! function_exists('check_plugins')) { function check_plugins(&$a) {
function check_plugins(&$a) {
/** /**
* *
@ -1234,7 +1285,6 @@ if(! function_exists('check_plugins')) {
load_hooks(); load_hooks();
return; return;
}
} }
function get_guid($size=16, $prefix = "") { function get_guid($size=16, $prefix = "") {
@ -1256,12 +1306,18 @@ function get_guid($size=16, $prefix = "") {
} }
} }
// wrapper for adding a login box. If $register == true provide a registration /**
// link. This will most always depend on the value of $a->config['register_policy']. * @brief Wrapper for adding a login box.
// returns the complete html for inserting into the page *
* @param bool $register
if(! function_exists('login')) { * If $register == true provide a registration link.
function login($register = false, $hiddens=false) { * This will most always depend on the value of $a->config['register_policy'].
* @param bool $hiddens
*
* @return string
* Returns the complete html for inserting into the page
*/
function login($register = false, $hiddens=false) {
$a = get_app(); $a = get_app();
$o = ""; $o = "";
$reg = false; $reg = false;
@ -1320,75 +1376,69 @@ if(! function_exists('login')) {
call_hooks('login_hook',$o); call_hooks('login_hook',$o);
return $o; return $o;
}
} }
// Used to end the current process, after saving session state. /**
* @brief Used to end the current process, after saving session state.
if(! function_exists('killme')) { */
function killme() { function killme() {
session_write_close(); session_write_close();
exit; exit;
}
} }
// redirect to another URL and terminate this process. /**
* @brief Redirect to another URL and terminate this process.
if(! function_exists('goaway')) { */
function goaway($s) { function goaway($s) {
header("Location: $s"); header("Location: $s");
killme(); killme();
}
} }
// Returns the uid of locally logged in user or false. /**
* @brief Returns the user id of locally logged in user or false.
if(! function_exists('local_user')) { *
function local_user() { * @return int|bool user id or false
*/
function local_user() {
if((x($_SESSION,'authenticated')) && (x($_SESSION,'uid'))) if((x($_SESSION,'authenticated')) && (x($_SESSION,'uid')))
return intval($_SESSION['uid']); return intval($_SESSION['uid']);
return false; return false;
}
} }
// Returns contact id of authenticated site visitor or false /**
/* @brief Returns contact id of authenticated site visitor or false
if(! function_exists('remote_user')) { *
function remote_user() { * @return int|bool visitor_id or false
*/
function remote_user() {
if((x($_SESSION,'authenticated')) && (x($_SESSION,'visitor_id'))) if((x($_SESSION,'authenticated')) && (x($_SESSION,'visitor_id')))
return intval($_SESSION['visitor_id']); return intval($_SESSION['visitor_id']);
return false; return false;
}
} }
// contents of $s are displayed prominently on the page the next time /**
// a page is loaded. Usually used for errors or alerts. * @brief Show an error message to user.
if(! function_exists('notice')) {
/**
* Show an error message to user.
* *
* This function save text in session, to be shown to the user at next page load * This function save text in session, to be shown to the user at next page load
* *
* @param string $s - Text of notice * @param string $s - Text of notice
*/ */
function notice($s) { function notice($s) {
$a = get_app(); $a = get_app();
if(! x($_SESSION,'sysmsg')) $_SESSION['sysmsg'] = array(); if(! x($_SESSION,'sysmsg')) $_SESSION['sysmsg'] = array();
if($a->interactive) if($a->interactive)
$_SESSION['sysmsg'][] = $s; $_SESSION['sysmsg'][] = $s;
}
} }
if(! function_exists('info')) {
/** /**
* Show an info message to user. * @brief Show an info message to user.
* *
* This function save text in session, to be shown to the user at next page load * This function save text in session, to be shown to the user at next page load
* *
* @param string $s - Text of notice * @param string $s - Text of notice
*/ */
function info($s) { function info($s) {
$a = get_app(); $a = get_app();
if (local_user() AND get_pconfig(local_user(),'system','ignore_info')) if (local_user() AND get_pconfig(local_user(),'system','ignore_info'))
@ -1397,17 +1447,17 @@ if(! function_exists('info')) {
if(! x($_SESSION,'sysmsg_info')) $_SESSION['sysmsg_info'] = array(); if(! x($_SESSION,'sysmsg_info')) $_SESSION['sysmsg_info'] = array();
if($a->interactive) if($a->interactive)
$_SESSION['sysmsg_info'][] = $s; $_SESSION['sysmsg_info'][] = $s;
}
} }
// wrapper around config to limit the text length of an incoming message /**
* @brief Wrapper around config to limit the text length of an incoming message
if(! function_exists('get_max_import_size')) { *
function get_max_import_size() { * @return int
*/
function get_max_import_size() {
global $a; global $a;
return ((x($a->config,'max_import_size')) ? $a->config['max_import_size'] : 0 ); return ((x($a->config,'max_import_size')) ? $a->config['max_import_size'] : 0 );
}
} }
/** /**
@ -1423,9 +1473,7 @@ if(! function_exists('get_max_import_size')) {
* *
* $cmd and string args are surrounded with "" * $cmd and string args are surrounded with ""
*/ */
function proc_run($cmd){
if(! function_exists('proc_run')) {
function proc_run($cmd){
$a = get_app(); $a = get_app();
@ -1508,11 +1556,9 @@ if(! function_exists('proc_run')) {
proc_close(proc_open('cmd /c start /b ' . $cmdline,array(),$foo,dirname(__FILE__))); proc_close(proc_open('cmd /c start /b ' . $cmdline,array(),$foo,dirname(__FILE__)));
else else
proc_close(proc_open($cmdline." &",array(),$foo,dirname(__FILE__))); proc_close(proc_open($cmdline." &",array(),$foo,dirname(__FILE__)));
}
} }
if(! function_exists('current_theme')) { function current_theme(){
function current_theme(){
$app_base_themes = array('duepuntozero', 'dispy', 'quattro'); $app_base_themes = array('duepuntozero', 'dispy', 'quattro');
$a = get_app(); $a = get_app();
@ -1586,15 +1632,16 @@ if(! function_exists('current_theme')) {
if(count($fallback)) if(count($fallback))
return (str_replace('view/theme/','', substr($fallback[0],0,-10))); return (str_replace('view/theme/','', substr($fallback[0],0,-10)));
}
} }
/* /**
* Return full URL to theme which is currently in effect. * @brief Return full URL to theme which is currently in effect.
* Provide a sane default if nothing is chosen or the specified theme does not exist. *
*/ * Provide a sane default if nothing is chosen or the specified theme does not exist.
if(! function_exists('current_theme_url')) { *
function current_theme_url() { * @return string
*/
function current_theme_url() {
global $a; global $a;
$t = current_theme(); $t = current_theme();
@ -1604,11 +1651,9 @@ if(! function_exists('current_theme_url')) {
return($a->get_baseurl() . '/view/theme/' . $t . '/style.pcss' . $opts); return($a->get_baseurl() . '/view/theme/' . $t . '/style.pcss' . $opts);
return($a->get_baseurl() . '/view/theme/' . $t . '/style.css'); return($a->get_baseurl() . '/view/theme/' . $t . '/style.css');
}
} }
if(! function_exists('feed_birthday')) { function feed_birthday($uid,$tz) {
function feed_birthday($uid,$tz) {
/** /**
* *
@ -1653,11 +1698,14 @@ if(! function_exists('feed_birthday')) {
} }
return $birthday; return $birthday;
}
} }
if(! function_exists('is_site_admin')) { /**
function is_site_admin() { * @brief Check if current user has admin role.
*
* @return bool true if user is an admin
*/
function is_site_admin() {
$a = get_app(); $a = get_app();
$adminlist = explode(",", str_replace(" ", "", $a->config['admin_email'])); $adminlist = explode(",", str_replace(" ", "", $a->config['admin_email']));
@ -1666,12 +1714,10 @@ if(! function_exists('is_site_admin')) {
if(local_user() && x($a->user,'email') && x($a->config,'admin_email') && in_array($a->user['email'], $adminlist)) if(local_user() && x($a->user,'email') && x($a->config,'admin_email') && in_array($a->user['email'], $adminlist))
return true; return true;
return false; return false;
}
} }
if(! function_exists('load_contact_links')) { function load_contact_links($uid) {
function load_contact_links($uid) {
$a = get_app(); $a = get_app();
@ -1693,15 +1739,16 @@ if(! function_exists('load_contact_links')) {
$a->contacts = $ret; $a->contacts = $ret;
return; return;
}
} }
/** /**
* returns querystring as string from a mapped array * @brief Returns querystring as string from a mapped array.
* *
* @param params Array * @param array $params mapped array with query parameters
* @return string * @param string $name of parameter, default null
*/ *
* @return string
*/
function build_querystring($params, $name=null) { function build_querystring($params, $name=null) {
$ret = ""; $ret = "";
foreach($params as $key=>$val) { foreach($params as $key=>$val) {