<?php
-
+/**
+ * @file src/App.php
+ */
namespace Friendica;
use Friendica\Core\Cache;
use Friendica\Core\Config;
+use Friendica\Core\L10n;
use Friendica\Core\PConfig;
use Friendica\Core\System;
+use Friendica\Database\DBM;
+use dba;
use Detection\MobileDetect;
*/
class App
{
+ const MODE_NORMAL = 0;
+ const MODE_INSTALL = 1;
+ const MODE_MAINTENANCE = 2;
+
public $module_loaded = false;
public $module_class = null;
public $query_string;
public $argv;
public $argc;
public $module;
+ public $mode = App::MODE_NORMAL;
public $pager;
public $strings;
public $basepath;
public $hooks;
public $timezone;
public $interactive = true;
- public $plugins;
- public $plugins_admin = [];
+ public $addons;
+ public $addons_admin = [];
public $apps = [];
public $identities;
public $is_mobile = false;
$this->performance['start'] = microtime(true);
$this->performance['database'] = 0;
$this->performance['database_write'] = 0;
+ $this->performance['cache'] = 0;
+ $this->performance['cache_write'] = 0;
$this->performance['network'] = 0;
$this->performance['file'] = 0;
$this->performance['rendering'] = 0;
$this->callstack['database'] = [];
$this->callstack['database_write'] = [];
+ $this->callstack['cache'] = [];
+ $this->callstack['cache_write'] = [];
$this->callstack['network'] = [];
$this->callstack['file'] = [];
$this->callstack['rendering'] = [];
$this->process_id = uniqid('log', true);
- startup();
+ set_time_limit(0);
+
+ // This has to be quite large to deal with embedded private photos
+ ini_set('pcre.backtrack_limit', 500000);
$this->scheme = 'http';
. $this->basepath . DIRECTORY_SEPARATOR . 'library' . PATH_SEPARATOR
. $this->basepath);
-
- if (is_array($_SERVER['argv']) && $_SERVER['argc'] > 1 && substr(end($_SERVER['argv']), 0, 4) == 'http') {
- $this->set_baseurl(array_pop($_SERVER['argv']));
- $_SERVER['argc'] --;
- }
-
if ((x($_SERVER, 'QUERY_STRING')) && substr($_SERVER['QUERY_STRING'], 0, 9) === 'pagename=') {
$this->query_string = substr($_SERVER['QUERY_STRING'], 9);
$this->is_friendica_app = ($_SERVER['HTTP_USER_AGENT'] == 'Apache-HttpClient/UNAVAILABLE (java 1.4)');
// Register template engines
- $dc = get_declared_classes();
- foreach ($dc as $k) {
- if (in_array('Friendica\Render\ITemplateEngine', class_implements($k))) {
- $this->register_template_engine($k);
- }
- }
+ $this->register_template_engine('Friendica\Render\FriendicaSmartyEngine');
+
+ /**
+ * Load the configuration file which contains our DB credentials.
+ * Ignore errors. If the file doesn't exist or is empty, we are running in
+ * installation mode. *
+ */
+ $this->mode = ((file_exists('.htconfig.php') && filesize('.htconfig.php')) ? App::MODE_NORMAL : App::MODE_INSTALL);
+
self::$a = $this;
}
'$baseurl' => $this->get_baseurl(),
'$local_user' => local_user(),
'$generator' => 'Friendica' . ' ' . FRIENDICA_VERSION,
- '$delitem' => t('Delete this item?'),
- '$showmore' => t('show more'),
- '$showfewer' => t('show fewer'),
+ '$delitem' => L10n::t('Delete this item?'),
+ '$showmore' => L10n::t('show more'),
+ '$showfewer' => L10n::t('show fewer'),
'$update_interval' => $interval,
'$shortcut_icon' => $shortcut_icon,
'$touch_icon' => $touch_icon,
$this->template_engines[$name] = $class;
} else {
echo "template engine <tt>$class</tt> cannot be registered without a name.\n";
- killme();
+ die();
}
}
return;
}
- // If the last worker fork was less than 2 seconds before then don't fork another one.
- // This should prevent the forking of masses of workers.
- $cachekey = 'app:proc_run:started';
- $result = Cache::get($cachekey);
-
- if (!is_null($result) && ( time() - $result) < 2) {
- return;
- }
-
- // Set the timestamp of the last proc_run
- Cache::set($cachekey, time(), CACHE_MINUTE);
-
- array_unshift($args, ((x($this->config, 'php_path')) && (strlen($this->config['php_path'])) ? $this->config['php_path'] : 'php'));
-
- // add baseurl to args. cli scripts can't construct it
- $args[] = $this->get_baseurl();
+ array_unshift($args, $this->getConfigValue('config', 'php_path', 'php'));
for ($x = 0; $x < count($args); $x ++) {
$args[$x] = escapeshellarg($args[$x]);
return;
}
- if (Config::get('system', 'proc_windows')) {
+ if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$resource = proc_open('cmd /c start /b ' . $cmdline, [], $foo, $this->get_basepath());
} else {
$resource = proc_open($cmdline . ' &', [], $foo, $this->get_basepath());
return true;
}
+
+ /**
+ * @param string $cat Config category
+ * @param string $k Config key
+ * @param mixed $default Default value if it isn't set
+ */
+ public function getConfigValue($cat, $k, $default = null)
+ {
+ $return = $default;
+
+ if ($cat === 'config') {
+ if (isset($this->config[$k])) {
+ $return = $this->config[$k];
+ }
+ } else {
+ if (isset($this->config[$cat][$k])) {
+ $return = $this->config[$cat][$k];
+ }
+ }
+
+ return $return;
+ }
+
+ /**
+ * Sets a value in the config cache. Accepts raw output from the config table
+ *
+ * @param string $cat Config category
+ * @param string $k Config key
+ * @param mixed $v Value to set
+ */
+ public function setConfigValue($cat, $k, $v)
+ {
+ // Only arrays are serialized in database, so we have to unserialize sparingly
+ $value = is_string($v) && preg_match("|^a:[0-9]+:{.*}$|s", $v) ? unserialize($v) : $v;
+
+ if ($cat === 'config') {
+ $this->config[$k] = $value;
+ } else {
+ if (!isset($this->config[$cat])) {
+ $this->config[$cat] = [];
+ }
+
+ $this->config[$cat][$k] = $value;
+ }
+ }
+
+ /**
+ * Deletes a value from the config cache
+ *
+ * @param string $cat Config category
+ * @param string $k Config key
+ */
+ public function deleteConfigValue($cat, $k)
+ {
+ if ($cat === 'config') {
+ if (isset($this->config[$k])) {
+ unset($this->config[$k]);
+ }
+ } else {
+ if (isset($this->config[$cat][$k])) {
+ unset($this->config[$cat][$k]);
+ }
+ }
+ }
+
+
+ /**
+ * Retrieves a value from the user config cache
+ *
+ * @param int $uid User Id
+ * @param string $cat Config category
+ * @param string $k Config key
+ * @param mixed $default Default value if key isn't set
+ */
+ public function getPConfigValue($uid, $cat, $k, $default = null)
+ {
+ $return = $default;
+
+ if (isset($this->config[$uid][$cat][$k])) {
+ $return = $this->config[$uid][$cat][$k];
+ }
+
+ return $return;
+ }
+
+ /**
+ * Sets a value in the user config cache
+ *
+ * Accepts raw output from the pconfig table
+ *
+ * @param int $uid User Id
+ * @param string $cat Config category
+ * @param string $k Config key
+ * @param mixed $v Value to set
+ */
+ public function setPConfigValue($uid, $cat, $k, $v)
+ {
+ // Only arrays are serialized in database, so we have to unserialize sparingly
+ $value = is_string($v) && preg_match("|^a:[0-9]+:{.*}$|s", $v) ? unserialize($v) : $v;
+
+ if (!isset($this->config[$uid])) {
+ $this->config[$uid] = [];
+ }
+
+ if (!isset($this->config[$uid][$cat])) {
+ $this->config[$uid][$cat] = [];
+ }
+
+ $this->config[$uid][$cat][$k] = $value;
+ }
+
+ /**
+ * Deletes a value from the user config cache
+ *
+ * @param int $uid User Id
+ * @param string $cat Config category
+ * @param string $k Config key
+ */
+ public function deletePConfigValue($uid, $cat, $k)
+ {
+ if (isset($this->config[$uid][$cat][$k])) {
+ unset($this->config[$uid][$cat][$k]);
+ }
+ }
+
+ /**
+ * Generates the site's default sender email address
+ *
+ * @return string
+ */
+ public function getSenderEmailAddress()
+ {
+ $sender_email = Config::get('config', 'sender_email');
+ if (empty($sender_email)) {
+ $hostname = $this->get_hostname();
+ if (strpos($hostname, ':')) {
+ $hostname = substr($hostname, 0, strpos($hostname, ':'));
+ }
+
+ $sender_email = 'noreply@' . $hostname;
+ }
+
+ return $sender_email;
+ }
+
+ /**
+ * @note Checks, if the App is in the Maintenance-Mode
+ *
+ * @return boolean
+ */
+ public function checkMaintenanceMode()
+ {
+ if (Config::get('system', 'maintenance')) {
+ $this->mode = App::MODE_MAINTENANCE;
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * Returns the current theme name.
+ *
+ * @return string
+ */
+ public function getCurrentTheme()
+ {
+ if ($this->mode == App::MODE_INSTALL) {
+ return '';
+ }
+
+ //// @TODO Compute the current theme only once (this behavior has
+ /// already been implemented, but it didn't work well -
+ /// https://github.com/friendica/friendica/issues/5092)
+ $this->computeCurrentTheme();
+
+ return $this->current_theme;
+ }
+
+ /**
+ * Computes the current theme name based on the node settings, the user settings and the device type
+ *
+ * @throws Exception
+ */
+ private function computeCurrentTheme()
+ {
+ $system_theme = Config::get('system', 'theme');
+ if (!$system_theme) {
+ throw new Exception(L10n::t('No system theme config value set.'));
+ }
+
+ // Sane default
+ $this->current_theme = $system_theme;
+
+ $allowed_themes = explode(',', Config::get('system', 'allowed_themes', $system_theme));
+
+ $page_theme = null;
+ // Find the theme that belongs to the user whose stuff we are looking at
+ if ($this->profile_uid && ($this->profile_uid != local_user())) {
+ // Allow folks to override user themes and always use their own on their own site.
+ // This works only if the user is on the same server
+ $user = dba::selectFirst('user', ['theme'], ['uid' => $this->profile_uid]);
+ if (DBM::is_result($user) && !PConfig::get(local_user(), 'system', 'always_my_theme')) {
+ $page_theme = $user['theme'];
+ }
+ }
+
+ $user_theme = defaults($_SESSION, 'theme', $system_theme);
+ // Specific mobile theme override
+ if (($this->is_mobile || $this->is_tablet) && defaults($_SESSION, 'show-mobile', true)) {
+ $system_mobile_theme = Config::get('system', 'mobile-theme');
+ $user_mobile_theme = defaults($_SESSION, 'mobile-theme', $system_mobile_theme);
+
+ // --- means same mobile theme as desktop
+ if (!empty($user_mobile_theme) && $user_mobile_theme !== '---') {
+ $user_theme = $user_mobile_theme;
+ }
+ }
+
+ if ($page_theme) {
+ $theme_name = $page_theme;
+ } else {
+ $theme_name = $user_theme;
+ }
+
+ if ($theme_name
+ && in_array($theme_name, $allowed_themes)
+ && (file_exists('view/theme/' . $theme_name . '/style.css')
+ || file_exists('view/theme/' . $theme_name . '/style.php'))
+ ) {
+ $this->current_theme = $theme_name;
+ }
+ }
+
+ /**
+ * @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.
+ *
+ * @return string
+ */
+ public function getCurrentThemeStylesheetPath()
+ {
+ return Core\Theme::getStylesheetPath($this->getCurrentTheme());
+ }
}