PageRenderTime 25ms CodeModel.GetById 4ms RepoModel.GetById 0ms app.codeStats 0ms

/index.php

https://github.com/soonick/poMMo
PHP | 225 lines | 148 code | 18 blank | 59 comment | 23 complexity | 9022eb301413b991fcf7b28976b879cc MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * Original Code Copyright (C) 2005, 2006, 2007, 2008 Brice Burgess <bhb@iceburg.net>
  4. * released originally under GPLV2
  5. *
  6. * This file is part of poMMo.
  7. *
  8. * poMMo is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * poMMo is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with Pommo. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. * This fork is from https://github.com/soonick/poMMo
  22. * Please see docs/contribs for Contributors
  23. *
  24. */
  25. /**********************************
  26. INITIALIZATION METHODS
  27. *********************************/
  28. require ('bootstrap.php');
  29. Pommo::init(array('authLevel' => 0));
  30. $logger = Pommo::$_logger;
  31. /**********************************
  32. SETUP TEMPLATE, PAGE
  33. *********************************/
  34. require_once(Pommo::$_baseDir.'classes/Pommo_Template.php');
  35. $view = new Pommo_Template();
  36. $fatal_error = null;
  37. // Check we have the mysql module installed
  38. if (!extension_loaded('mysql')) {
  39. $fatal_error[] = "Php mysql module is not installed.";
  40. }
  41. // Check if module is installed
  42. if (!extension_loaded('gettext')) {
  43. $fatal_error[] = "Php gettext module is not installed.";
  44. }
  45. //Check write permission to the cache directory
  46. if (!is_writable(dirname(__FILE__).'/cache')) {
  47. $fatal_error[] = "The cache directory needs to be writable";
  48. }
  49. if ($fatal_error) {
  50. $view->assign('errors', $fatal_error);
  51. $view->display('message');
  52. exit();
  53. }
  54. // log the user out if requested
  55. if (isset($_GET['logout']))
  56. {
  57. Pommo::$_auth->logout();
  58. header('Location: ' . Pommo::$_http . Pommo::$_baseUrl . 'index.php');
  59. }
  60. // check if user is already logged in
  61. if (Pommo::$_hasConfigFile && Pommo::$_auth->isAuthenticated())
  62. {
  63. // If user is authenticated (has logged in), redirect to admin.php
  64. Pommo::redirect(Pommo::$_http . Pommo::$_baseUrl . 'admin.php');
  65. }
  66. // Log in attempt. Authenticate.
  67. elseif (isset($_POST['submit'])
  68. && !empty($_POST['username'])
  69. && !empty($_POST['password']))
  70. {
  71. require_once Pommo::$_baseDir.'classes/Pommo_User.php';
  72. $user = new Pommo_user();
  73. if ($user->login($_POST['username'], $_POST['password']))
  74. {
  75. // don't perform maintenance if accessing support area
  76. if(!isset($_GET['referer'])
  77. || !basename($_GET['referer']) == 'support.php')
  78. {
  79. // login success. Perform maintenance, set auth, redirect to referer
  80. require_once(Pommo::$_baseDir.'classes/Pommo_Helper_Maintenance.php');
  81. Pommo_Helper_Maintenance::perform();
  82. }
  83. Pommo::$_auth->login($_POST['username']);
  84. Pommo::redirect(Pommo::$_http.$_POST['referer']);
  85. }
  86. else
  87. {
  88. $logger->addMsg(Pommo::_T('Failed login attempt. Try again.'));
  89. }
  90. }
  91. elseif (!empty ($_POST['resetPassword']))
  92. {
  93. // TODO -- visit this function later
  94. // Check if a reset password request has been received
  95. // check that captcha matched
  96. if (!isset($_POST['captcha']))
  97. {
  98. // generate captcha
  99. $captcha = substr(md5(rand()), 0, 4);
  100. $view->assign('captcha', $captcha);
  101. }
  102. elseif ($_POST['captcha'] == $_POST['realdeal'])
  103. {
  104. // user inputted captcha matched. Reset password
  105. require_once(Pommo::$_baseDir.'classes/Pommo_Pending.php');
  106. require_once(Pommo::$_baseDir.'classes/Pommo_Helper_Messages.php');
  107. // see if there is already a pending request for the administrator
  108. // [subscriber id == 0]
  109. if (Pommo_Pending::isPending(0))
  110. {
  111. $input = urlencode(serialize(array('adminID' => TRUE,
  112. 'Email' => Pommo::$_config['admin_email'])));
  113. Pommo::redirect(Pommo::$_http . Pommo::$_baseUrl
  114. .'pending.php?input='.$input);
  115. }
  116. // create a password change request, send confirmation mail
  117. $subscriber = array('id' => 0);
  118. $code = Pommo_Pending::add($subscriber,'password');
  119. Pommo_Helper_Messages::sendMessage(
  120. array('to' => Pommo::$_config['admin_email'],
  121. 'code' => $code, 'type' => 'password'));
  122. $view->assign('captcha',FALSE);
  123. }
  124. else
  125. {
  126. // captcha did not match
  127. $logger->addMsg(Pommo::_T('Captcha did not match. Try again.'));
  128. }
  129. }
  130. elseif (!Pommo::$_hasConfigFile && $_POST['configure'])
  131. {
  132. // Try to connect to database with data entered from the user.
  133. // I am not using /inc/classes/db.php because it kills the proccess when
  134. // connection is not possible
  135. // TODO: db.php shouldnt kill the process
  136. $link = @mysql_connect($_POST['dbhost'], $_POST['dbuser'], $_POST['dbpass']);
  137. if (!$link)
  138. {
  139. // Could not connect
  140. $configErrors[] = 'Could not connect to host. Check your settings
  141. and try again.';
  142. }
  143. else
  144. {
  145. if (!@mysql_select_db($_POST['dbname'], $link))
  146. {
  147. // Database does not exist. Lets try to create it.
  148. if (!mysql_query('CREATE DATABASE '.$_POST['dbname'], $link))
  149. {
  150. $configErrors[] = 'Database does not exist. And the provided
  151. user does not have the necessary permissions to create
  152. it. You will have to create it manually first.';
  153. }
  154. }
  155. }
  156. // If there were no errors then try to create the file
  157. if (!$configErrors)
  158. {
  159. // I am sure there must be better ways to do this, but this works
  160. // for now.
  161. // TODO: If there is a better method change this, if not. Delete
  162. // this line.
  163. $handle = @fopen('config.php', 'w');
  164. if (!$handle)
  165. {
  166. $configErrors[] = 'Script was not able to create config.php
  167. file. You should assign write permission for this script
  168. to pommo root folder or create config.php yourself.';
  169. }
  170. else
  171. {
  172. $string = '<?php die(); /* DO NOT REMOVE THIS LINE! */ ?>'.
  173. PHP_EOL.PHP_EOL
  174. .'[db_hostname] = '.$_POST['dbhost'].PHP_EOL
  175. .'[db_username] = '.$_POST['dbuser'].PHP_EOL
  176. .'[db_password] = '.$_POST['dbpass'].PHP_EOL
  177. .'[db_database] = '.$_POST['dbname'].PHP_EOL
  178. .'[db_prefix] = pommo_'.PHP_EOL
  179. .PHP_EOL
  180. .'[lang] = en'.PHP_EOL
  181. .'[debug] = off'.PHP_EOL
  182. .'[verbosity] = 3'.PHP_EOL
  183. .'[date_format] = 1'.PHP_EOL;
  184. fwrite($handle, $string);
  185. fclose($handle);
  186. $redir = Pommo::$_baseUrl.'install.php';
  187. header('Location: '.$redir);
  188. exit();
  189. }
  190. }
  191. }
  192. if (Pommo::$_hasConfigFile)
  193. {
  194. // referer (used to return user to requested page upon login success)
  195. $view->assign('referer',
  196. (isset($_REQUEST['referer']) ?
  197. $_REQUEST['referer'] : Pommo::$_baseUrl.'admin.php'));
  198. $view->display('index');
  199. }
  200. else
  201. {
  202. $view->assign('messages', $configErrors);
  203. $view->assign('dbhost', $_POST['dbhost']);
  204. $view->assign('dbname', $_POST['dbname']);
  205. $view->assign('dbuser', $_POST['dbuser']);
  206. $view->display('configure');
  207. }