PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/resources/classes/Core.class.php

https://gitlab.com/adamlwalker/generatedata
PHP | 511 lines | 266 code | 61 blank | 184 comment | 44 complexity | 9efbc1fe6591958c214a4f5c8edc52fd MD5 | raw file
  1. <?php
  2. /**
  3. * Called on script load for any Data Generator page. It does all the heavy lifting and initializes various
  4. * general vars and data for the script. It's used instead of the global namespace to store info needed throughout
  5. * the code; the class is static so we don't need to re-instantiate it all over the place, and force it to
  6. * reload/re-instantiate the various components.
  7. *
  8. * @author Ben Keen <ben.keen@gmail.com>
  9. * @package Core
  10. */
  11. class Core {
  12. // overridable settings that the user may define in settings.php
  13. private static $demoMode = false;
  14. private static $dbHostname;
  15. private static $dbName;
  16. private static $dbUsername;
  17. private static $dbPassword;
  18. private static $dbTablePrefix = "gd_";
  19. private static $encryptionSalt;
  20. private static $errorReporting = 1;
  21. private static $maxGeneratedRows = 100000;
  22. private static $defaultNumRows = 100;
  23. private static $maxDemoModeRows = 100;
  24. private static $defaultLanguageFile = "en";
  25. private static $defaultExportType = "HTML";
  26. private static $defaultCountryPlugins = array();
  27. private static $defaultTheme = "classic";
  28. private static $enableSmartySecurity = true;
  29. private static $useMinifiedResources = false;
  30. private static $pluginSettings = array();
  31. private static $timeout = 300; // 5 minutes
  32. // non-overridable settings
  33. private static $version = "3.1.4";
  34. private static $releaseDate = "2014-09-06";
  35. private static $minimumPHPVersion = "5.3.0";
  36. private static $minimumMySQLVersion = "4.1.3";
  37. private static $settingsFileExists = false;
  38. private static $dataTypeGroups = array("human_data", "geo", "credit_card_data", "text", "numeric", "math", "other");
  39. private static $continents = array("africa", "asia", "central_america", "europe", "north_america", "oceania", "south_america");
  40. private static $isLoggedIn = false;
  41. // left as public, because they're often modified / accessed, and it's just too fussy otherwise. The
  42. // PHPDoc helps my IDE figure out what the hell each of them are
  43. /**
  44. * @var Language
  45. */
  46. public static $language;
  47. /**
  48. * @var Database
  49. */
  50. public static $db;
  51. /**
  52. * @var Translations
  53. */
  54. public static $translations;
  55. /**
  56. * @var Account
  57. */
  58. public static $user;
  59. /**
  60. * @var GeoData
  61. */
  62. public static $geoData;
  63. /**
  64. * @var Smarty
  65. */
  66. public static $smarty;
  67. public static $dataTypePlugins;
  68. public static $exportTypePlugins;
  69. /**
  70. * @var CountryPlugin
  71. */
  72. public static $countryPlugins;
  73. public static $allowThemes = false;
  74. /**
  75. * Core::init()
  76. *
  77. * Our initialization function. This is called on all page requests to initialize the Core
  78. * object. Since it's also used during installation (when the database and/or plugins haven't been
  79. * installed), the optional parameter controls whether or not the database object, plugins, sessions and user
  80. * should be initialized. Different call contexts require different initialization.
  81. *
  82. * @access public
  83. * @static
  84. * @param string $runtimeContext This determines the context in which the Core is being initialized. This
  85. * info is used to let plugins instantiate themselves differently, as well as prevent the loading
  86. * of incomplete parts of the script.<br />
  87. * <b>installation</b>: a fresh installation, DB not installed yet<br />
  88. * <b>installationDatabaseReady</b>: during installation after the DB has been installed<br />
  89. * <b>ui</b>: (default) for the main generator page<br />
  90. * <b>generation</b>: when we're in the process of creating actual data
  91. * <b>resetPlugins</b>: initialized everything except the plugins, which may be safely reset
  92. */
  93. public static function init($runtimeContext = "ui") {
  94. self::loadSettingsFile();
  95. error_reporting(self::$errorReporting);
  96. // ensure the timezone is set. This is an arbitrary value (well, I live in Vancouver!) but it prevents warnings
  97. if (ini_get("date.timezone") == "") {
  98. ini_set("date.timezone", "Canada/Vancouver");
  99. }
  100. self::$translations = new Translations();
  101. // the order is significant in all of this
  102. if ($runtimeContext != "installation") {
  103. self::initDatabase();
  104. if (in_array($runtimeContext, array("installationDatabaseReady", "ui", "generation", "resetPlugins"))) {
  105. self::initSessions();
  106. }
  107. $dbDefaultLanguage = Settings::getSetting("defaultLanguage");
  108. if (!empty($dbDefaultLanguage)) {
  109. self::$defaultLanguageFile = $dbDefaultLanguage;
  110. }
  111. }
  112. self::$language = new Language(self::$defaultLanguageFile);
  113. self::initSmarty();
  114. if ($runtimeContext == "generation") {
  115. self::initGeoData();
  116. }
  117. if ($runtimeContext == "ui" || $runtimeContext == "generation") {
  118. self::initCountries();
  119. self::initExportTypes($runtimeContext);
  120. self::initDataTypes($runtimeContext);
  121. }
  122. if (in_array($runtimeContext, array("ui", "generation", "resetPlugins"))) {
  123. self::initUser();
  124. }
  125. set_time_limit(self::$timeout);
  126. }
  127. /**
  128. * Core::loadSettingsFile()
  129. *
  130. * Attempts to load the settings file. If successful, it updates the various private member vars
  131. * with whatever's been defined.
  132. * @access private
  133. */
  134. private static function loadSettingsFile() {
  135. $settingsFilePath = realpath(__DIR__ . "/../../settings.php");
  136. if (file_exists($settingsFilePath)) {
  137. self::$settingsFileExists = true;
  138. require_once($settingsFilePath);
  139. self::$demoMode = (isset($demoMode)) ? $demoMode : null;
  140. self::$dbHostname = (isset($dbHostname)) ? $dbHostname : null;
  141. self::$dbName = (isset($dbName)) ? $dbName : null;
  142. self::$dbUsername = (isset($dbUsername)) ? $dbUsername : null;
  143. self::$dbPassword = (isset($dbPassword)) ? $dbPassword : null;
  144. self::$dbTablePrefix = (isset($dbTablePrefix)) ? $dbTablePrefix : null;
  145. self::$encryptionSalt = (isset($encryptionSalt)) ? $encryptionSalt : null;
  146. self::$pluginSettings = (isset($pluginSettings)) ? $pluginSettings : array();
  147. if (isset($demoMode)) {
  148. self::$demoMode = $demoMode;
  149. }
  150. if (isset($errorReporting)) {
  151. self::$errorReporting = $errorReporting;
  152. }
  153. if (isset($maxGeneratedRows)) {
  154. self::$maxGeneratedRows = $maxGeneratedRows;
  155. }
  156. if (isset($defaultNumRows)) {
  157. self::$defaultNumRows = $defaultNumRows;
  158. }
  159. if (isset($maxDemoModeRows)) {
  160. self::$maxDemoModeRows = $maxDemoModeRows;
  161. }
  162. if (isset($defaultLanguageFile)) {
  163. self::$defaultLanguageFile = $defaultLanguageFile;
  164. }
  165. if (isset($enableSmartySecurity)) {
  166. self::$enableSmartySecurity = $enableSmartySecurity;
  167. }
  168. if (isset($useMinifiedResources)) {
  169. self::$useMinifiedResources = $useMinifiedResources;
  170. }
  171. if (isset($pluginSettings)) {
  172. self::$pluginSettings = $pluginSettings;
  173. }
  174. // TODO temporary, during alpha dev
  175. if (isset($allowThemes)) {
  176. self::$allowThemes = $allowThemes;
  177. }
  178. }
  179. }
  180. /**
  181. * @access public
  182. */
  183. public static function getDefaultExportType() {
  184. return self::$defaultExportType;
  185. }
  186. /**
  187. * TODO Yuck! Why does this return a boolean as a frickin' string?! Was I drunk?
  188. * @access public
  189. */
  190. public static function checkDemoMode() {
  191. return (self::$demoMode) ? "true" : "false";
  192. }
  193. /**
  194. * @access public
  195. */
  196. public static function checkAllowMultiUserAnonymousUse() {
  197. $allowAnonymousAccessSetting = Settings::getSetting("allowAnonymousAccess");
  198. return ($allowAnonymousAccessSetting == "yes");
  199. }
  200. /**
  201. * @access public
  202. */
  203. public static function getDefaultCountryPlugins() {
  204. return self::$defaultCountryPlugins;
  205. }
  206. /**
  207. * @access public
  208. */
  209. public static function getHostname() {
  210. return self::$dbHostname;
  211. }
  212. /**
  213. * @access public
  214. */
  215. public static function getDbName() {
  216. return self::$dbName;
  217. }
  218. /**
  219. * @access public
  220. */
  221. public static function getDbUsername() {
  222. return self::$dbUsername;
  223. }
  224. /**
  225. * @access public
  226. */
  227. public static function getDbPassword() {
  228. return self::$dbPassword;
  229. }
  230. /**
  231. * @access public
  232. */
  233. public static function getDbTablePrefix() {
  234. return self::$dbTablePrefix;
  235. }
  236. /**
  237. * @access public
  238. */
  239. public static function getMaxGeneratedRows() {
  240. return self::$maxGeneratedRows;
  241. }
  242. /**
  243. * @access public
  244. */
  245. public static function getEncryptionSalt() {
  246. return self::$encryptionSalt;
  247. }
  248. /**
  249. * @access public
  250. */
  251. public static function getDefaultNumRows() {
  252. return self::$defaultNumRows;
  253. }
  254. /**
  255. * @access public
  256. */
  257. public static function getMaxDemoModeRows() {
  258. return self::$maxDemoModeRows;
  259. }
  260. /**
  261. * @access public
  262. */
  263. public static function getVersion() {
  264. return self::$version;
  265. }
  266. /**
  267. * @access public
  268. */
  269. public static function checkSettingsFileExists() {
  270. return self::$settingsFileExists;
  271. }
  272. /**
  273. * Full installation of the program is determined by (a) the settings.php file existing and (b)
  274. * the "installationComplete" setting value existing in the database. Note: this function assumes
  275. * the database connection in Core::$db has already been created.
  276. * @access public
  277. */
  278. public static function checkIsInstalled() {
  279. if (!self::$settingsFileExists) {
  280. return false;
  281. }
  282. $installationComplete = Settings::getSetting("installationComplete");
  283. if (!isset($installationComplete) || $installationComplete == "no") {
  284. return false;
  285. }
  286. return true;
  287. }
  288. /**
  289. * @access public
  290. */
  291. public static function checkIsLoggedIn() {
  292. if (!isset($_SESSION["account_id"])) {
  293. self::$isLoggedIn = false;
  294. }
  295. return self::$isLoggedIn;
  296. }
  297. /**
  298. * @access public
  299. */
  300. public static function getDefaultLanguageFile() {
  301. return self::$defaultLanguageFile;
  302. }
  303. /**
  304. * @access public
  305. */
  306. public static function getDataTypeGroups() {
  307. return self::$dataTypeGroups;
  308. }
  309. /**
  310. * @access public
  311. */
  312. public static function getContinents() {
  313. return self::$continents;
  314. }
  315. /**
  316. * Returns the minimum PHP version required to run this script. Used during installation to ensure the
  317. * server environment is adequate.
  318. * @access public
  319. */
  320. public static function getMinimumPHPVersion() {
  321. return self::$minimumPHPVersion;
  322. }
  323. /**
  324. * Returns the minimum MySQL version required to run this script. Used during installation to ensure the
  325. * server environment is adequate.
  326. * @access public
  327. */
  328. public static function getMinimumMySQLVersion() {
  329. return self::$minimumMySQLVersion;
  330. }
  331. /**
  332. * Returns a boolean signifying whether we should use the minified + bundled resources generated via Grunt.
  333. * @return bool
  334. */
  335. public static function isUsingMinifiedResources() {
  336. return self::$useMinifiedResources;
  337. }
  338. /**
  339. * Used during the installation process only: it returns the default theme for new installations.
  340. * @access public
  341. */
  342. public static function getDefaultTheme() {
  343. return self::$defaultTheme;
  344. }
  345. /**
  346. * Added in 3.1.4. This allows any plugins to have custom settings defined in $pluginSettings. This
  347. * function returns null if no settings exist for the plugin, or whatever settings have been provided.
  348. * @param $pluginType
  349. * @param $pluginFolder
  350. * @return mixed
  351. */
  352. public static function getPluginSettings($pluginType, $pluginFolder) {
  353. if (!array_key_exists($pluginType, self::$pluginSettings)) {
  354. return null;
  355. }
  356. if (!array_key_exists($pluginFolder, self::$pluginSettings[$pluginType])) {
  357. return null;
  358. }
  359. return self::$pluginSettings[$pluginType][$pluginFolder];
  360. }
  361. public static function isSmartySecurityEnabled() {
  362. return self::$enableSmartySecurity;
  363. }
  364. // ------------------ private methods ------------------
  365. /**
  366. * Initializes the Smarty object used for things like rendering the Smarty templates found in
  367. * /resources/templates/ - and for other misc uses.
  368. * @access private
  369. */
  370. private function initSmarty() {
  371. self::$smarty = new SecureSmarty();
  372. self::$smarty->template_dir = realpath(__DIR__ . "/../templates/");
  373. self::$smarty->compile_dir = realpath(__DIR__ . "/../../cache/");
  374. self::$smarty->assign("version", self::getVersion());
  375. self::$smarty->assign("samePage", Utils::getCleanPhpSelf());
  376. }
  377. /**
  378. * Called by Core::init(), this initializes Core::$countryPlugins.
  379. * @access private
  380. */
  381. private static function initCountries() {
  382. if (!Core::$settingsFileExists) {
  383. return;
  384. }
  385. self::$countryPlugins = CountryPluginHelper::getCountryPlugins();
  386. }
  387. /**
  388. * This function returns the actual data populated in the database by the Country plugins. It
  389. * returns an array of country data, contains regions and cities.
  390. * @access private
  391. */
  392. private static function initGeoData() {
  393. self::$geoData = new GeoData();
  394. }
  395. /**
  396. * Initializes the Database object and stores it in Core::$db.
  397. * @access private
  398. */
  399. private static function initDatabase() {
  400. if (Core::$settingsFileExists) {
  401. self::$db = new Database();
  402. }
  403. }
  404. /**
  405. * Called by Core::init(), this initializes Core::$dataTypePlugins.
  406. * @access private
  407. */
  408. private static function initDataTypes($runtimeContext) {
  409. if (!Core::$settingsFileExists) {
  410. return;
  411. }
  412. // parse the Data Types folder and identify those plugins that are available
  413. self::$dataTypePlugins = DataTypePluginHelper::getDataTypePlugins($runtimeContext);
  414. }
  415. /**
  416. * Called by Core::init(), this initializes Core::$exportTypePlugins.
  417. * @access private
  418. */
  419. private static function initExportTypes($runtimeContext) {
  420. if (!Core::$settingsFileExists) {
  421. return;
  422. }
  423. self::$exportTypePlugins = ExportTypePluginHelper::getExportTypePlugins($runtimeContext);
  424. }
  425. /**
  426. * Initializes the current logged in user and stores their Account object in Core::$user.
  427. * @param bool $bypass
  428. */
  429. public static function initUser($bypass = false) {
  430. if ($bypass || self::checkIsInstalled()) {
  431. $setup = Settings::getSetting("userAccountSetup");
  432. if ($setup == "anonymousAdmin") {
  433. self::$user = new Account($setup);
  434. self::$isLoggedIn = true;
  435. } else {
  436. if (isset($_SESSION["account_id"])) {
  437. self::$user = new Account($_SESSION["account_id"]);
  438. self::$isLoggedIn = true;
  439. }
  440. }
  441. }
  442. }
  443. public static function initSessions() {
  444. if (session_id() == '') {
  445. new SessionManager();
  446. session_start();
  447. header("Cache-control: private");
  448. }
  449. }
  450. }