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

/phpshop2/app/controllers/install_controller.php

http://phpshop.googlecode.com/
PHP | 415 lines | 291 code | 52 blank | 72 comment | 46 complexity | 15b2840d026636c77d1ac40bd534f1e5 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /* SVN FILE: $Id: install_controller.php 418 2008-01-31 22:37:17Z pablo $ */
  3. /**
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * phpShop(tm) : A Simple Shopping Cart <http://www.phpshop.org/>
  8. * Copyright 1998-2008, Edikon Corporation
  9. * 3455 Peachtree Road Suite 500
  10. * Atlanta, Georgia 30326
  11. *
  12. * Licensed under The GNU General Public License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @filesource
  16. * @copyright Copyright 1998-2008, Edikon Corporation
  17. * @link http://www.edikon.com/ phpShop(tm) Project
  18. * @package phpshop
  19. * @subpackage phpshop.app.controllers
  20. * @since phpShop(tm)
  21. * @version $Revision:$
  22. * @modifiedby $LastChangedBy:$
  23. * @lastmodified $Date:$
  24. * @license http://www.opensource.org/licenses/gpl-license.php The GNU General Public License
  25. */
  26. /**
  27. * InstallController for phpShop
  28. *
  29. * The controller class to install the phpShop application.
  30. * Adapted this this file from - Cheesecake Photoblog
  31. * http://cakeforge.org/projects/cheesecake/
  32. *
  33. */
  34. class InstallController extends AppController
  35. {
  36. var $autoRender = false;
  37. var $uses = array();
  38. function beforeFilter() {
  39. $this->components = array();
  40. }
  41. function afterFilter() {}
  42. function index() {
  43. $this->step1();
  44. }
  45. /*
  46. * check filesystem
  47. */
  48. function step1() {
  49. //check for database.php file,if file exits then display a message to user.
  50. if ($this->__dbFileExists()){
  51. echo $this->render(null,null,'db_error');
  52. exit;
  53. }
  54. if (!function_exists('gd_info'))
  55. {
  56. echo $this->render(null,null,'gd_error');
  57. exit;
  58. }
  59. if($this->__testFileSystem()) {
  60. echo $this->redirect('/install/step2');
  61. exit;
  62. }
  63. }
  64. /*
  65. * get the database details from user and create the database.php file
  66. */
  67. function step2() {
  68. if (!$this->__dbFileExists()) {
  69. $this->errors = '';
  70. if($this->__createDatabase()) {
  71. $this->redirect('/install/step3');
  72. }
  73. }
  74. echo $this->render(null,null,'db_error');
  75. }
  76. /*
  77. * get admin details and create tables.
  78. */
  79. function step3() {
  80. if ($this->__createUser()) {
  81. echo $this->render(null, null, 'success');
  82. exit;
  83. }
  84. }
  85. function __createUser()
  86. {
  87. $this->errors= '';
  88. @ $values['username']= ($_POST['username']) ? $_POST['username'] : '';
  89. @ $values['password']= ($_POST['password']) ? $_POST['password'] : '';
  90. @ $values['name']= ($_POST['name']) ? $_POST['name'] : '';
  91. @ $values['email']= ($_POST['email']) ? $_POST['email'] : '';
  92. if (isset ($_POST['username']) && !empty ($_POST))
  93. {
  94. if (trim($_POST['username'] == '' || $_POST['password'] == ''))
  95. {
  96. $this->errors= "It is much better for you to provide a 'username' and a 'password' for the admin.<br /><br />";
  97. }
  98. elseif (trim($_POST['email']) == '')
  99. {
  100. $this->errors .= "Please provide email address";
  101. }
  102. elseif (!preg_match('/^([_a-zA-Z0-9.]+@[-a-zA-Z0-9]+(\.[-a-zA-Z0-9]+)+)*$/', $_POST['email']))
  103. {
  104. $this->errors .= "Please provide valid email address";
  105. }
  106. else
  107. {
  108. $this->__createTables();
  109. return true;
  110. exit;
  111. }
  112. }
  113. $this->set('values', $values);
  114. $this->set('errors', $this->errors);
  115. $this->set(array (
  116. 'title' => 'Install phpShop'
  117. ));
  118. echo $this->render(null, null, 'admin');
  119. exit;
  120. }
  121. function __createDatabase()
  122. {
  123. @ $values['dbserver']= ($_POST['dbserver']) ? $_POST['dbserver'] : 'localhost';
  124. @ $values['dbname']= ($_POST['dbname']) ? $_POST['dbname'] : '';
  125. @ $values['dbuser']= ($_POST['dbuser']) ? $_POST['dbuser'] : '';
  126. @ $values['dbpass']= ($_POST['dbpass']) ? $_POST['dbpass'] : '';
  127. @ $values['table_prefix']= ($_POST['table_prefix']) ? $_POST['table_prefix'] : 'ps_';
  128. if (isset ($_POST) && !empty ($_POST))
  129. {
  130. $this->errors= '';
  131. $this->__testSqlConnection();
  132. if ($this->errors == '')
  133. {
  134. $this->__createDatabaseConfigFile();
  135. if ($this->errors == '')
  136. {
  137. return true;
  138. }
  139. }
  140. }
  141. $this->set('values', $values);
  142. $this->set('errors', $this->errors);
  143. $this->set(array (
  144. 'title' => 'Install phpShop'
  145. ));
  146. echo $this->render(null, null, 'database');
  147. exit;
  148. }
  149. function __testFileSystem()
  150. {
  151. $configDir= ROOT . '/app/config/';
  152. $tmpDir= ROOT . '/app/tmp/';
  153. $photosDir= WWW_ROOT . 'img/products';
  154. $thumbnailsDir= WWW_ROOT . 'img/thumbnails';
  155. $categoriesDir= WWW_ROOT . 'img/categories';
  156. $writableDirs= array (
  157. $configDir,
  158. $tmpDir,
  159. $photosDir,
  160. $thumbnailsDir,
  161. $categoriesDir
  162. );
  163. $areNotWriteable= array ();
  164. foreach ($writableDirs as $dir)
  165. {
  166. if (!is_writable($dir))
  167. {
  168. $areNotWriteable[]= $dir;
  169. }
  170. }
  171. if (count($areNotWriteable))
  172. {
  173. $this->set('areNotWriteable', $areNotWriteable);
  174. $this->set(array (
  175. 'title' => 'Install phpShop'
  176. ));
  177. echo $this->render(null, null, 'fileSystem');
  178. exit;
  179. }
  180. return true;
  181. }
  182. function __dbFileExists() {
  183. $db_file = ROOT.'/app/config/database.php';
  184. if (file_exists($db_file)) {
  185. return true;
  186. }
  187. return false;
  188. }
  189. function __testSqlConnection()
  190. {
  191. if (isset ($_POST) && !empty ($_POST))
  192. {
  193. if (!function_exists('mysql_connect'))
  194. {
  195. $this->errors .= "PHP does not have MySQL support enabled.";
  196. }
  197. elseif (!$connect_id= @ mysql_connect($_POST['dbserver'], $_POST['dbuser'], $_POST['dbpass']))
  198. {
  199. $this->errors .= "Could not create a mySQL connection, please check the values entered<br />MySQL error was : " . mysql_error() . "<br /><br />";
  200. }
  201. elseif (!mysql_select_db($_POST['dbname'], $connect_id))
  202. {
  203. $this->errors .= "MySQL database called '{$_POST['dbname']}' could not be connected using the details provided, please check the values entered for these are correct";
  204. }
  205. }
  206. }
  207. function __createDatabaseConfigFile()
  208. {
  209. $db_file= ROOT . '/app/config/database.php';
  210. $config_file= '/app/config/database.php';
  211. @ unlink($db_file);
  212. $config= $this->__buildCfgFile();
  213. if ($fd= @ fopen($db_file, 'wb'))
  214. {
  215. fwrite($fd, $config);
  216. fclose($fd);
  217. }
  218. else
  219. {
  220. $this->errors .= "<hr /><br />Unable to write config file '{$config_file}'<br /><br />";
  221. }
  222. }
  223. // ---------------------- CONFIGURATION FILE TEMPLATE ---------------------- //
  224. function __buildCfgFile()
  225. {
  226. return<<<EOT
  227. <?php
  228. class DATABASE_CONFIG
  229. {
  230. var \$default = array('driver' => 'mysql',
  231. 'connect' => 'mysql_connect',
  232. 'host' => '{$_POST['dbserver']}',
  233. 'login' => '{$_POST['dbuser']}',
  234. 'password' => '{$_POST['dbpass']}',
  235. 'database' => '{$_POST['dbname']}',
  236. 'prefix' => '{$_POST['table_prefix']}');
  237. }
  238. define('PS_INSTALLED', 1);
  239. ?>
  240. EOT;
  241. }
  242. function __createTables()
  243. {
  244. App::import('Model', 'ConnectionManager');
  245. $db= & ConnectionManager :: getDataSource('default');
  246. $prefix= $db->config['prefix'];
  247. $sqlFile= ROOT . DS . APP_DIR . DS . 'config/sql/phpshop.sql';
  248. $sql= file_get_contents($sqlFile);
  249. $sql .= "INSERT INTO `ps_users` (`username`, `password`, `role`, `name`, `email`, `created`, `modified`) VALUES ('{$_POST['username']}','{$_POST['password']}','Admin','{$_POST['name']}','{$_POST['email']}',NOW(),NOW());";
  250. $sql_query= preg_replace('/ps_/', $prefix, $sql);
  251. $sql_query= $this->__removeRemarks($sql_query);
  252. $sql_query= $this->__splitSqlFile($sql_query, ';');
  253. foreach ($sql_query as $q)
  254. {
  255. $db->query($q);
  256. }
  257. }
  258. // __removeRemarks will strip the sql comment lines out of an uploaded sql file
  259. //Adapted this function from the - Coppermine Picture Gallery http://coppermine.sf.net
  260. function __removeRemarks($sql)
  261. {
  262. $lines= explode("\n", $sql);
  263. // try to keep mem. use down
  264. $sql= "";
  265. $linecount= count($lines);
  266. $output= "";
  267. for ($i= 0; $i < $linecount; $i++)
  268. {
  269. if (($i != ($linecount -1)) || (strlen($lines[$i]) > 0))
  270. {
  271. if (isset ($lines[$i][0]) && $lines[$i][0] != "#")
  272. {
  273. $output .= $lines[$i] . "\n";
  274. }
  275. else
  276. {
  277. $output .= "\n";
  278. }
  279. // Trading a bit of speed for lower mem. use here.
  280. $lines[$i]= "";
  281. }
  282. }
  283. return $output;
  284. }
  285. // __splitSqlFile will split an uploaded sql file into single sql statements.
  286. // Note: expects trim() to have already been run on $sql.
  287. //Adapted this function from the - Coppermine Picture Gallery http://coppermine.sf.net
  288. function __splitSqlFile($sql, $delimiter)
  289. {
  290. // Split up our string into "possible" SQL statements.
  291. $tokens= explode($delimiter, $sql);
  292. // try to save mem.
  293. $sql= "";
  294. $output= array ();
  295. // we don't actually care about the matches preg gives us.
  296. $matches= array ();
  297. // this is faster than calling count($oktens) every time thru the loop.
  298. $token_count= count($tokens);
  299. for ($i= 0; $i < $token_count; $i++)
  300. {
  301. // Don't wanna add an empty string as the last thing in the array.
  302. if (($i != ($token_count -1)) || (strlen($tokens[$i] > 0)))
  303. {
  304. // This is the total number of single quotes in the token.
  305. $total_quotes= preg_match_all("/'/", $tokens[$i], $matches);
  306. // Counts single quotes that are preceded by an odd number of backslashes,
  307. // which means they're escaped quotes.
  308. $escaped_quotes= preg_match_all("/(?<!\\\\)(\\\\\\\\)*\\\\'/", $tokens[$i], $matches);
  309. $unescaped_quotes= $total_quotes - $escaped_quotes;
  310. // If the number of unescaped quotes is even, then the delimiter did NOT occur inside a string literal.
  311. if (($unescaped_quotes % 2) == 0)
  312. {
  313. // It's a complete sql statement.
  314. $output[]= $tokens[$i];
  315. // save memory.
  316. $tokens[$i]= "";
  317. }
  318. else
  319. {
  320. // incomplete sql statement. keep adding tokens until we have a complete one.
  321. // $temp will hold what we have so far.
  322. $temp= $tokens[$i] . $delimiter;
  323. // save memory..
  324. $tokens[$i]= "";
  325. // Do we have a complete statement yet?
  326. $complete_stmt= false;
  327. for ($j= $i +1;(!$complete_stmt && ($j < $token_count)); $j++)
  328. {
  329. // This is the total number of single quotes in the token.
  330. $total_quotes= preg_match_all("/'/", $tokens[$j], $matches);
  331. // Counts single quotes that are preceded by an odd number of backslashes,
  332. // which means they're escaped quotes.
  333. $escaped_quotes= preg_match_all("/(?<!\\\\)(\\\\\\\\)*\\\\'/", $tokens[$j], $matches);
  334. $unescaped_quotes= $total_quotes - $escaped_quotes;
  335. if (($unescaped_quotes % 2) == 1)
  336. {
  337. // odd number of unescaped quotes. In combination with the previous incomplete
  338. // statement(s), we now have a complete statement. (2 odds always make an even)
  339. $output[]= $temp . $tokens[$j];
  340. // save memory.
  341. $tokens[$j]= "";
  342. $temp= "";
  343. // exit the loop.
  344. $complete_stmt= true;
  345. // make sure the outer loop continues at the right point.
  346. $i= $j;
  347. }
  348. else
  349. {
  350. // even number of unescaped quotes. We still don't have a complete statement.
  351. // (1 odd and 1 even always make an odd)
  352. $temp .= $tokens[$j] . $delimiter;
  353. // save memory.
  354. $tokens[$j]= "";
  355. }
  356. } // for..
  357. } // else
  358. }
  359. }
  360. return $output;
  361. }
  362. }
  363. ?>