PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/index.php

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