Advertisement
Guest User

Untitled

a guest
Nov 4th, 2017
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.78 KB | None | 0 0
  1. <?php
  2. /**
  3. * Project: MyAAC
  4. * Automatic Account Creator for Open Tibia Servers
  5. * File: index.php
  6. *
  7. * This is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This software 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 GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * @package MyAAC
  22. * @author Slawkens <slawkens@gmail.com>
  23. * @copyright 2017 MyAAC
  24. * @version 0.6.6
  25. * @link http://my-aac.org
  26. */
  27.  
  28. // uncomment if your php.ini have display_errors disabled and you want to see errors
  29. // ini_set('display_errors', 1);
  30. // ini_set('display_startup_errors', 1);
  31. // error_reporting(E_ALL);
  32.  
  33. if(preg_match("/^(.*)\.(gif|jpg|jpeg|tiff|bmp|css|js|less|map|html|php|zip|rar|gz)$/i", $_SERVER['REQUEST_URI'])) {
  34. header("HTTP/1.0 404 Not Found");
  35. exit;
  36. }
  37.  
  38. require_once('common.php');
  39. require_once(BASE . 'config.local.php');
  40.  
  41. if(file_exists(BASE . 'install') && (!isset($config['installed']) || !$config['installed']))
  42. {
  43. header('Location: ' . BASE_URL . 'install/');
  44. die('Setup detected that <b>install/</b> directory exists. Please visit <a href="' . BASE_URL . 'install">this</a> url to start MyAAC Installation.<br/>Delete <b>install/</b> directory if you already installed MyAAC.<br/>Remember to REFRESH this page when you\'re done!');
  45. }
  46.  
  47. require_once(SYSTEM . 'functions.php');
  48.  
  49. $uri = $_SERVER['REQUEST_URI'];
  50.  
  51. $tmp = BASE_DIR;
  52. if(!empty($tmp))
  53. $uri = str_replace(BASE_DIR . '/', '', $uri);
  54. else
  55. $uri = str_replace_first('/', '', $uri);
  56.  
  57. $uri = str_replace(array('index.php/', '?'), '', $uri);
  58. define('URI', $uri);
  59.  
  60. $found = false;
  61. if(empty($uri) || isset($_REQUEST['template'])) {
  62. $_REQUEST['p'] = 'news';
  63. $found = true;
  64. }
  65. else if(preg_match("/^[A-Za-z0-9-_%\'+]+\.png$/i", $uri)) {
  66. $tmp = explode('.', $uri);
  67. $_REQUEST['name'] = urldecode($tmp[0]);
  68.  
  69. chdir(TOOLS . 'signature');
  70. include(TOOLS . 'signature/index.php');
  71. exit();
  72. }
  73. else if(!preg_match('/[^A-z0-9_\-]/', $uri) && file_exists(SYSTEM . 'pages/' . $uri . '.php')) {
  74. $_REQUEST['p'] = $uri;
  75. $found = true;
  76. }
  77. else {
  78. $rules = array(
  79. '/^account\/manage\/?$/' => array('subtopic' => 'accountmanagement'),
  80. '/^account\/create\/?$/' => array('subtopic' => 'createaccount'),
  81. '/^account\/lost\/?$/' => array('subtopic' => 'lostaccount'),
  82. '/^account\/logout\/?$/' => array('subtopic' => 'accountmanagement', 'action' => 'logout'),
  83. '/^account\/password\/?$/' => array('subtopic' => 'accountmanagement', 'action' => 'change_password'),
  84. '/^account\/register\/?$/' => array('subtopic' => 'accountmanagement', 'action' => 'register'),
  85. '/^account\/register\/new\/?$/' => array('subtopic' => 'accountmanagement', 'action' => 'register_new'),
  86. '/^account\/email\/?$/' => array('subtopic' => 'accountmanagement', 'action' => 'change_email'),
  87. '/^account\/info\/?$/' => array('subtopic' => 'accountmanagement', 'action' => 'change_info'),
  88. '/^account\/character\/create\/?$/' => array('subtopic' => 'accountmanagement', 'action' => 'create_character'),
  89. '/^account\/character\/name\/?$/' => array('subtopic' => 'accountmanagement', 'action' => 'change_name'),
  90. '/^account\/character\/sex\/?$/' => array('subtopic' => 'accountmanagement', 'action' => 'change_sex'),
  91. '/^account\/character\/delete\/?$/' => array('subtopic' => 'accountmanagement', 'action' => 'delete_character'),
  92. '/^account\/character\/comment\/[A-Za-z]+\/?$/' => array('subtopic' => 'accountmanagement', 'action' => 'change_comment', 'name' => '$3'),
  93. '/^account\/character\/comment\/?$/' => array('subtopic' => 'accountmanagement', 'action' => 'change_comment'),
  94. '/^characters\/[A-Za-z0-9-_%+\']+$/' => array('subtopic' => 'characters', 'name' => '$1'),
  95. '/^commands\/add\/?$/' => array('subtopic' => 'commands', 'action' => 'add'),
  96. '/^commands\/edit\/?$/' => array('subtopic' => 'commands', 'action' => 'edit'),
  97. '/^faq\/add\/?$/' => array('subtopic' => 'faq', 'action' => 'add'),
  98. '/^faq\/edit\/?$/' => array('subtopic' => 'faq', 'action' => 'edit'),
  99. '/^forum\/add_board\/?$/' => array('subtopic' => 'forum', 'action' => 'add_board'),#
  100. '/^forum\/edit_board\/?$/' => array('subtopic' => 'forum', 'action' => 'edit_board'),
  101. '/^forum\/board\/[0-9]+\/?$/' => array('subtopic' => 'forum', 'action' => 'show_board', 'id' => '$2'),
  102. '/^forum\/board\/[0-9]+\/[0-9]+\/?$/' => array('subtopic' => 'forum', 'action' => 'show_board', 'id' => '$2', 'page' => '$3'),
  103. '/^forum\/thread\/[0-9]+\/?$/' => array('subtopic' => 'forum', 'action' => 'show_thread', 'id' => '$2'),
  104. '/^forum\/thread\/[0-9]+\/[0-9]+\/?$/' => array('subtopic' => 'forum', 'action' => 'show_thread', 'id' => '$2', 'page' => '$3'),
  105. '/^gallery\/add\/?$/' => array('subtopic' => 'gallery', 'action' => 'add'),
  106. '/^gallery\/edit\/?$/' => array('subtopic' => 'gallery', 'action' => 'edit'),
  107. '/^gallery\/[0-9]+\/?$/' => array('subtopic' => 'gallery', 'image' => '$1'),
  108. '/^gifts\/history\/?$/' => array('subtopic' => 'gifts', 'action' => 'show_history'),
  109. '/^guilds\/[A-Za-z0-9-_%+\']+$/' => array('subtopic' => 'guilds', 'action' => 'show', 'guild' => '$1'),
  110. '/^highscores\/[A-Za-z0-9-_]+\/[A-Za-z0-9-_]+\/[0-9]+\/?$/' => array('subtopic' => 'highscores', 'list' => '$1', 'vocation' => '$2', 'page' => '$3'),
  111. '/^highscores\/[A-Za-z0-9-_]+\/[0-9]+\/?$/' => array('subtopic' => 'highscores', 'list' => '$1', 'page' => '$2'),
  112. '/^highscores\/[A-Za-z0-9-_]+\/[A-Za-z0-9-_]+\/?$/' => array('subtopic' => 'highscores', 'list' => '$1', 'vocation' => '$2'),
  113. '/^highscores\/[A-Za-z0-9-_\']+\/?$/' => array('subtopic' => 'highscores', 'list' => '$1'),
  114. '/^news\/add\/?$/' => array('subtopic' => 'news', 'action' => 'add'),
  115. '/^news\/edit\/?$/' => array('subtopic' => 'news', 'action' => 'edit'),
  116. '/^news\/archive\/?$/' => array('subtopic' => 'newsarchive'),
  117. '/^news\/archive\/[0-9]+\/?$/' => array('subtopic' => 'newsarchive', 'id' => '$2'),
  118. '/^polls\/[0-9]+\/?$/' => array('subtopic' => 'polls', 'id' => '$1'),
  119. '/^spells\/[A-Za-z0-9-_%]+\/[A-Za-z0-9-_]+\/?$/' => array('subtopic' => 'spells', 'vocation' => '$1', 'order' => '$2'),
  120. '/^gifts\/history\/?$/' => array('subtopic' => 'gifts', 'action' => 'show_history'),
  121. );
  122.  
  123. foreach($rules as $rule => $redirect) {
  124. if (preg_match($rule, $uri)) {
  125. $tmp = explode('/', $uri);
  126. foreach($redirect as $key => $value) {
  127.  
  128. if(strpos($value, '$') !== false) {
  129. $value = str_replace('$' . $value[1], $tmp[$value[1]], $value);
  130. }
  131.  
  132. $_REQUEST[$key] = $value;
  133. $_GET[$key] = $value;
  134. }
  135.  
  136. $found = true;
  137. break;
  138. }
  139. }
  140.  
  141. if(!$found)
  142. $_REQUEST['p'] = $uri;
  143. }
  144.  
  145. // define page visited, so it can be used within events system
  146. $page = isset($_REQUEST['subtopic']) ? $_REQUEST['subtopic'] : (isset($_REQUEST['p']) ? $_REQUEST['p'] : '');
  147. if(empty($page) || preg_match('/[^A-z0-9\/_\-]/', $page)) {
  148. if(!$found)
  149. $page = '404';
  150. else
  151. $page = 'news';
  152. }
  153.  
  154. $page = strtolower($page);
  155. define('PAGE', $page);
  156.  
  157. $template_place_holders = array();
  158.  
  159. require_once(SYSTEM . 'init.php');
  160. require_once(SYSTEM . 'template.php');
  161. require_once(SYSTEM . 'login.php');
  162. require_once(SYSTEM . 'status.php');
  163.  
  164. $twig->addGlobal('config', $config);
  165. $twig->addGlobal('status', $status);
  166.  
  167. // database migrations
  168. $tmp = '';
  169. if(fetchDatabaseConfig('database_version', $tmp)) { // we got version
  170. $tmp = (int)$tmp;
  171. if($tmp < DATABASE_VERSION) { // import if older
  172. for($i = $tmp + 1; $i <= DATABASE_VERSION; $i++) {
  173. $file = SYSTEM . 'migrations/' . $i . '.php';
  174. if(file_exists($file)) {
  175. require($file);
  176. }
  177. }
  178.  
  179. updateDatabaseConfig('database_version', DATABASE_VERSION);
  180. }
  181. }
  182. else { // register first version
  183. for($i = 1; $i <= DATABASE_VERSION; $i++) {
  184. require(SYSTEM . 'migrations/' . $i . '.php');
  185. }
  186.  
  187. registerDatabaseConfig('database_version', DATABASE_VERSION);
  188. }
  189.  
  190. // event system
  191. require_once(SYSTEM . 'hooks.php');
  192. $hooks = new Hooks();
  193. $hooks->load();
  194. $hooks->trigger(HOOK_STARTUP);
  195.  
  196. if($config['views_counter'])
  197. require_once(SYSTEM . 'counter.php');
  198.  
  199. if($config['visitors_counter'])
  200. {
  201. require_once(SYSTEM . 'libs/visitors.php');
  202. $visitors = new Visitors($config['visitors_counter_ttl']);
  203. }
  204.  
  205. // page content loading
  206. if(!isset($content[0]))
  207. $content = '';
  208. $load_it = true;
  209.  
  210. // check if site has been closed
  211. $site_closed = false;
  212. if(fetchDatabaseConfig('site_closed', $site_closed)) {
  213. $site_closed = ($site_closed == 1);
  214. if($site_closed) {
  215. if(!admin())
  216. {
  217. $title = getDatabaseConfig('site_closed_title');
  218. $content .= '<p class="note">' . getDatabaseConfig('site_closed_message') . '</p><br/>';
  219. $load_it = false;
  220. }
  221.  
  222. if(!$logged)
  223. {
  224. ob_start();
  225. require(SYSTEM . 'pages/accountmanagement.php');
  226. $content .= ob_get_contents();
  227. ob_end_clean();
  228. $load_it = false;
  229. }
  230. }
  231. }
  232. define('SITE_CLOSED', $site_closed);
  233.  
  234. // backward support for gesior
  235. if($config['backward_support']) {
  236. define('INITIALIZED', true);
  237. $SQL = $db;
  238. $layout_header = template_header();
  239. $layout_name = $template_path;
  240. $news_content = '';
  241. $tickers_content = '';
  242. $subtopic = PAGE;
  243. $main_content = '';
  244.  
  245. $config['access_admin_panel'] = 2;
  246. $group_id_of_acc_logged = 0;
  247. if($logged && $account_logged)
  248. $group_id_of_acc_logged = $account_logged->getGroupId();
  249.  
  250. $config['site'] = &$config;
  251. $config['server'] = &$config['lua'];
  252. $config['site']['shop_system'] = $config['gifts_system'];
  253.  
  254. if(!isset($config['vdarkborder']))
  255. $config['vdarkborder'] = '#505050';
  256. if(!isset($config['darkborder']))
  257. $config['darkborder'] = '#D4C0A1';
  258. if(!isset($config['lightborder']))
  259. $config['lightborder'] = '#F1E0C6';
  260.  
  261. $config['site']['download_page'] = true;
  262. $config['site']['serverinfo_page'] = true;
  263. $config['site']['screenshot_page'] = true;
  264.  
  265. if($config['forum'] != '')
  266. $config['forum_link'] = (strtolower($config['forum']) == 'site' ? getLink('forum') : $config['forum']);
  267.  
  268. foreach($status as $key => $value)
  269. $config['status']['serverStatus_' . $key] = $value;
  270. }
  271.  
  272. if($load_it)
  273. {
  274. if(SITE_CLOSED && admin())
  275. $content .= '<p class="note">Site is under maintenance (closed mode). Only privileged users can see it.</p>';
  276.  
  277. if($config['backward_support'])
  278. require(SYSTEM . 'compat_pages.php');
  279.  
  280. $ignore = false;
  281.  
  282. $logged_access = 1;
  283. if($logged && $account_logged && $account_logged->isLoaded()) {
  284. $logged_access = $account_logged->getAccess();
  285. }
  286.  
  287. $query =
  288. $db->query(
  289. 'SELECT `id`, `title`, `body`, `php`, `hidden`' .
  290. ' FROM `' . TABLE_PREFIX . 'pages`' .
  291. ' WHERE `name` LIKE ' . $db->quote($page) . ' AND `hidden` != 1 AND `access` <= ' . $db->quote($logged_access));
  292. if($query->rowCount() > 0) // found page
  293. {
  294. $ignore = true;
  295. $query = $query->fetch();
  296. $title = $query['title'];
  297.  
  298. if($query['php'] == '1') // execute it as php code
  299. {
  300. $tmp = substr($query['body'], 0, 10);
  301. if(($pos = strpos($tmp, '<?php')) !== false) {
  302. $tmp = preg_replace('/<\?php/', '', $query['body'], 1);
  303. }
  304. else if(($pos = strpos($tmp, '<?')) !== false) {
  305. $tmp = preg_replace('/<\?/', '', $query['body'], 1);
  306. }
  307. else
  308. $tmp = $query['body'];
  309.  
  310. $php_errors = array();
  311. function error_handler($errno, $errstr) {
  312. global $php_errors;
  313. $php_errors[] = array('errno' => $errno, 'errstr' => $errstr);
  314. }
  315. set_error_handler('error_handler');
  316.  
  317. ob_start();
  318. eval($tmp);
  319. $content .= ob_get_contents();
  320. ob_end_clean();
  321.  
  322. restore_error_handler();
  323. if(isset($php_errors[0]) && superAdmin()) {
  324. var_dump($php_errors);
  325. }
  326. }
  327. else
  328. $content .= $query['body']; // plain html
  329.  
  330. if(hasFlag(FLAG_CONTENT_PAGES) || superAdmin()) {
  331. $content = $twig->render('admin.pages.links.html.twig', array(
  332. 'page' => array('id' => $query['id'], 'hidden' => $query['hidden'])
  333. )) . $content;
  334. }
  335. }
  336. else
  337. {
  338. $file = SYSTEM . 'pages/' . $page . '.php';
  339. if(!@file_exists($file))
  340. {
  341. $page = '404';
  342. $file = SYSTEM . 'pages/404.php';
  343. }
  344. }
  345.  
  346. ob_start();
  347. if($hooks->trigger(HOOK_BEFORE_PAGE)) {
  348. if(!$ignore)
  349. require($file);
  350. }
  351.  
  352. if($config['backward_support'] && isset($main_content[0]))
  353. $content .= $main_content;
  354.  
  355. $content .= ob_get_contents();
  356. ob_end_clean();
  357. $hooks->trigger(HOOK_AFTER_PAGE);
  358. }
  359.  
  360. if($config['backward_support']) {
  361. $main_content = $content;
  362. if(!isset($title))
  363. $title = ucfirst($page);
  364.  
  365. $topic = $title;
  366. }
  367.  
  368. $title_full = (isset($title) ? $title . $config['title_separator'] : '') . $config['lua']['serverName'];
  369. if(file_exists($template_path . '/index.php'))
  370. require($template_path . '/index.php');
  371. else if(file_exists($template_path . '/template.php')) // deprecated
  372. require($template_path . '/template.php');
  373. else if($config['backward_support'] && file_exists($template_path . '/layout.php'))
  374. {
  375. require($template_path . '/layout.php');
  376. }
  377. else
  378. {
  379. // TODO: save more info to log file
  380. die('ERROR: Cannot load template.');
  381. }
  382.  
  383. echo '<!-- MyAAC ' . MYAAC_VERSION . ' :: http://www.my-aac.org/ -->' . "\n";
  384. if(($config['debug_level'] & 1) == 1)
  385. echo '<!-- Generated in :: ' . round(microtime(true) - START_TIME, 4) . ' -->';
  386.  
  387. if(($config['debug_level'] & 2) == 2)
  388. echo "\n" . '<!-- Queries done :: ' . $db->queries() . ' -->';
  389.  
  390. if(($config['debug_level'] & 4) == 4 && function_exists('memory_get_peak_usage'))
  391. echo "\n" . '<!-- Peak memory usage: ' . convert_bytes(memory_get_peak_usage(true)) . ' -->';
  392.  
  393. $hooks->trigger(HOOK_FINISH);
  394. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement