PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/init.php

https://gitlab.com/x33n/ampache
PHP | 261 lines | 165 code | 39 blank | 57 comment | 33 complexity | 2404b3b42bd0d027dd76f8618572baed MD5 | raw file
  1. <?php
  2. /* vim:set softtabstop=4 shiftwidth=4 expandtab: */
  3. /**
  4. *
  5. * LICENSE: GNU General Public License, version 2 (GPLv2)
  6. * Copyright 2001 - 2015 Ampache.org
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License v2
  10. * as published by the Free Software Foundation
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. *
  21. */
  22. // Use output buffering, this gains us a few things and
  23. // fixes some CSS issues
  24. ob_start();
  25. $ampache_path = dirname(__FILE__);
  26. $prefix = realpath($ampache_path . "/../");
  27. require_once $prefix . '/lib/init-tiny.php';
  28. // Explicitly load and enable the custom session handler.
  29. // Relying on autoload may not always load it before sessiony things are done.
  30. require_once $prefix . '/lib/class/session.class.php';
  31. Session::_auto_init();
  32. // Set up for redirection on important error cases
  33. $path = get_web_path();
  34. $path = $http_type . $_SERVER['HTTP_HOST'] . $path;
  35. // Check to make sure the config file exists. If it doesn't then go ahead and
  36. // send them over to the install script.
  37. $results = array();
  38. if (!file_exists($configfile)) {
  39. $link = $path . '/install.php';
  40. } else {
  41. // Make sure the config file is set up and parsable
  42. $results = @parse_ini_file($configfile);
  43. if (!count($results)) {
  44. $link = $path . '/test.php?action=config';
  45. }
  46. }
  47. // Verify that a few important but commonly disabled PHP functions exist and
  48. // that we're on a usable version
  49. if (!check_php()) {
  50. $link = $path . '/test.php';
  51. }
  52. // Do the redirect if we can't continue
  53. if (!empty($link)) {
  54. header ("Location: $link");
  55. exit();
  56. }
  57. $results['load_time_begin'] = $load_time_begin;
  58. /** This is the version.... fluf nothing more... **/
  59. $results['version'] = '3.8.0-develop';
  60. $results['int_config_version'] = '27';
  61. if (!empty($results['force_ssl'])) {
  62. $http_type = 'https://';
  63. }
  64. if ($ow_config) {
  65. foreach ($ow_config as $key => $value) {
  66. $results[$key] = $value;
  67. }
  68. }
  69. $results['raw_web_path'] = $results['web_path'];
  70. if (empty($results['http_host'])) {
  71. $results['http_host'] = $_SERVER['HTTP_HOST'];
  72. }
  73. if (empty($results['local_web_path'])) {
  74. $results['local_web_path'] = $http_type . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $results['raw_web_path'];
  75. }
  76. $results['web_path'] = $http_type . $results['http_host'] . $results['web_path'];
  77. $results['http_port'] = (!empty($results['http_port'])) ? $results['http_port'] : $http_port;
  78. $results['site_charset'] = $results['site_charset'] ?: 'UTF-8';
  79. $results['raw_web_path'] = $results['raw_web_path'] ?: '/';
  80. $results['max_upload_size'] = $results['max_upload_size'] ?: 1048576;
  81. $_SERVER['SERVER_NAME'] = $_SERVER['SERVER_NAME'] ?: '';
  82. if (isset($results['user_ip_cardinality']) && !$results['user_ip_cardinality']) {
  83. $results['user_ip_cardinality'] = 42;
  84. }
  85. /* Variables needed for Auth class */
  86. $results['cookie_path'] = $results['raw_web_path'];
  87. $results['cookie_domain'] = $results['http_port'];
  88. $results['cookie_life'] = $results['session_cookielife'];
  89. $results['cookie_secure'] = $results['session_cookiesecure'];
  90. // Library and module includes we can't do with the autoloader
  91. require_once $prefix . '/modules/getid3/getid3.php';
  92. require_once $prefix . '/modules/phpmailer/class.phpmailer.php';
  93. require_once $prefix . '/modules/phpmailer/class.smtp.php';
  94. require_once $prefix . '/modules/infotools/AmazonSearchEngine.class.php';
  95. require_once $prefix . '/modules/musicbrainz/MusicBrainz.php';
  96. require_once $prefix . '/modules/musicbrainz/Exception.php';
  97. require_once $prefix . '/modules/musicbrainz/Clients/MbClient.php';
  98. require_once $prefix . '/modules/musicbrainz/Clients/RequestsMbClient.php';
  99. require_once $prefix . '/modules/musicbrainz/Artist.php';
  100. require_once $prefix . '/modules/musicbrainz/Filters/AbstractFilter.php';
  101. require_once $prefix . '/modules/musicbrainz/Filters/FilterInterface.php';
  102. require_once $prefix . '/modules/musicbrainz/Filters/ArtistFilter.php';
  103. require_once $prefix . '/modules/ampacheapi/AmpacheApi.lib.php';
  104. require_once $prefix . '/modules/EchoNest/Autoloader.php';
  105. EchoNest_Autoloader::register();
  106. /* Temp Fixes */
  107. $results = Preference::fix_preferences($results);
  108. AmpConfig::set_by_array($results, true);
  109. // Modules (These are conditionally included depending upon config values)
  110. if (AmpConfig::get('ratings')) {
  111. require_once $prefix . '/lib/rating.lib.php';
  112. }
  113. /* Set a new Error Handler */
  114. $old_error_handler = set_error_handler('ampache_error_handler');
  115. /* Check their PHP Vars to make sure we're cool here */
  116. $post_size = @ini_get('post_max_size');
  117. if (substr($post_size,strlen($post_size)-1,strlen($post_size)) != 'M') {
  118. /* Sane value time */
  119. ini_set('post_max_size','8M');
  120. }
  121. // In case the local setting is 0
  122. ini_set('session.gc_probability','5');
  123. if (!isset($results['memory_limit']) ||
  124. (UI::unformat_bytes($results['memory_limit']) < UI::unformat_bytes('32M'))
  125. ) {
  126. $results['memory_limit'] = '32M';
  127. }
  128. set_memory_limit($results['memory_limit']);
  129. /**** END Set PHP Vars ****/
  130. // If we want a session
  131. if (!defined('NO_SESSION') && AmpConfig::get('use_auth')) {
  132. /* Verify their session */
  133. if (!Session::exists('interface', $_COOKIE[AmpConfig::get('session_name')])) {
  134. if (!Session::auth_remember()) {
  135. Auth::logout($_COOKIE[AmpConfig::get('session_name')]);
  136. exit;
  137. }
  138. }
  139. // This actually is starting the session
  140. Session::check();
  141. /* Create the new user */
  142. $GLOBALS['user'] = User::get_from_username($_SESSION['userdata']['username']);
  143. /* If the user ID doesn't exist deny them */
  144. if (!$GLOBALS['user']->id && !AmpConfig::get('demo_mode')) {
  145. Auth::logout(session_id());
  146. exit;
  147. }
  148. /* Load preferences and theme */
  149. $GLOBALS['user']->update_last_seen();
  150. } elseif (!AmpConfig::get('use_auth')) {
  151. $auth['success'] = 1;
  152. $auth['username'] = '-1';
  153. $auth['fullname'] = "Ampache User";
  154. $auth['id'] = -1;
  155. $auth['offset_limit'] = 50;
  156. $auth['access'] = AmpConfig::get('default_auth_level') ? User::access_name_to_level(AmpConfig::get('default_auth_level')) : '100';
  157. if (!Session::exists('interface', $_COOKIE[AmpConfig::get('session_name')])) {
  158. Session::create_cookie();
  159. Session::create($auth);
  160. Session::check();
  161. $GLOBALS['user'] = new User($auth['username']);
  162. $GLOBALS['user']->username = $auth['username'];
  163. $GLOBALS['user']->fullname = $auth['fullname'];
  164. $GLOBALS['user']->access = intval($auth['access']);
  165. } else {
  166. Session::check();
  167. if ($_SESSION['userdata']['username']) {
  168. $GLOBALS['user'] = User::get_from_username($_SESSION['userdata']['username']);
  169. } else {
  170. $GLOBALS['user'] = new User($auth['username']);
  171. $GLOBALS['user']->id = -1;
  172. $GLOBALS['user']->username = $auth['username'];
  173. $GLOBALS['user']->fullname = $auth['fullname'];
  174. $GLOBALS['user']->access = intval($auth['access']);
  175. }
  176. if (!$GLOBALS['user']->id AND !AmpConfig::get('demo_mode')) {
  177. Auth::logout(session_id()); exit;
  178. }
  179. $GLOBALS['user']->update_last_seen();
  180. }
  181. }
  182. // If Auth, but no session is set
  183. else {
  184. if (isset($_REQUEST['sid'])) {
  185. session_name(AmpConfig::get('session_name'));
  186. session_id(scrub_in($_REQUEST['sid']));
  187. session_start();
  188. $GLOBALS['user'] = new User($_SESSION['userdata']['uid']);
  189. } else {
  190. $GLOBALS['user'] = new User();
  191. }
  192. } // If NO_SESSION passed
  193. // Load the Preferences from the database
  194. Preference::init();
  195. if (session_id()) {
  196. Session::extend(session_id());
  197. // We only need to create the tmp playlist if we have a session
  198. $GLOBALS['user']->load_playlist();
  199. }
  200. /* Add in some variables for ajax done here because we need the user */
  201. AmpConfig::set('ajax_url', AmpConfig::get('web_path') . '/server/ajax.server.php', true);
  202. AmpConfig::set('ajax_server', AmpConfig::get('web_path') . '/server', true);
  203. // Load gettext mojo
  204. load_gettext();
  205. /* Set CHARSET */
  206. header ("Content-Type: text/html; charset=" . AmpConfig::get('site_charset'));
  207. /* Clean up a bit */
  208. unset($array);
  209. unset($results);
  210. /* Check to see if we need to perform an update */
  211. if (!defined('OUTDATED_DATABASE_OK')) {
  212. if (Update::need_update()) {
  213. header("Location: " . AmpConfig::get('web_path') . "/update.php");
  214. exit();
  215. }
  216. }
  217. // For the XMLRPC stuff
  218. $GLOBALS['xmlrpc_internalencoding'] = AmpConfig::get('site_charset');
  219. // If debug is on GIMMIE DA ERRORS
  220. if (AmpConfig::get('debug')) {
  221. error_reporting(E_ALL);
  222. }