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

/protected/commands/CiiSetupCommand.php

https://github.com/charlesportwoodii/CiiMS
PHP | 167 lines | 137 code | 16 blank | 14 comment | 14 complexity | 56137f3afaccaa484f7654cb2233770e MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. class CiiSetupCommand extends CConsoleCommand
  3. {
  4. public function run($args=array())
  5. {
  6. if (!isset($args[0]))
  7. return $this->showCommands();
  8. switch ($args[0])
  9. {
  10. case "generatehash":
  11. $this->generateHash(isset($args[1]) ? $args[1] : false);
  12. break;
  13. case "generatefirstuser":
  14. $this->generateFirstUser($args[1], $args[2]);
  15. break;
  16. default:
  17. $this->showCommands();
  18. }
  19. }
  20. // The list of available commands
  21. private function showCommands()
  22. {
  23. $this->log("CiiSetupCommand: A command line helper for automating installation of CiiMS.");
  24. $this->log("===============================================================");
  25. $this->log("Usage:");
  26. $this->log(" php protected/yiic.php ciisetup [arg1] [arg2] [arg3] [...] [argn]\n");
  27. $this->log("Arguments:");
  28. $this->log(" generatehash Generates a hash for CiiMS to use for user data");
  29. $this->log(" if '1' is passed as the second arguement, it will generate/update config/params.php");
  30. $this->log(" generatefirstuser <email> <password>");
  31. $this->log(" Creates a new admin user for the site, using the provided email and passwword");
  32. $this->log(" This will also run generatehash if Yii::app()->params['encryptionKey'] is not defined");
  33. $this->log(" This command can only be used in headless setups, and will not create an admin user if one already exists");
  34. $this->log();
  35. }
  36. /**
  37. * Simple logging command to make life easier
  38. * @param string $message The message we want to output
  39. */
  40. private function log($message="")
  41. {
  42. echo $message . "\n";
  43. }
  44. /** ======================================== **/
  45. /**
  46. * Generates a new encryption key
  47. * @param $overrideConfig boolean Whether or not to generate the config file
  48. * If set to true, protected/config/params.php will be generated/updated
  49. * With the newly generated hash.
  50. */
  51. private function generateHash($overrideConfig = false)
  52. {
  53. $hash = mb_strimwidth(hash("sha512", hash("sha512", hash("whirlpool", md5(time() . md5(time())))) . hash("sha512", time()) . time()), 0, 120);
  54. if ($overrideConfig)
  55. {
  56. // Params File Template
  57. $paramsTemplate = array(
  58. 'yiiPath' => NULL,
  59. 'encryptionKey' => NULL,
  60. 'debug' => 0,
  61. 'trace' => 0
  62. );
  63. $params = $paramsTemplate;
  64. $paramsFile = __DIR__ . '/../config/params.php';
  65. // If the params file already exists, import it
  66. if (file_exists($paramsFile))
  67. $params = CMap::mergeArray($paramsTemplate, require $paramsFile);
  68. if (empty($params))
  69. $params = $paramsTemplate;
  70. $params['encryptionKey'] = $hash;
  71. $fh = fopen($paramsFile, 'w+');
  72. fwrite($fh, "<?php return array(\n");
  73. foreach ($params as $key=>$value)
  74. {
  75. if (is_int($value))
  76. fwrite($fh, " '$key' => " . (int)$value . ",\n");
  77. elseif(is_bool($value))
  78. {
  79. if ($value)
  80. fwrite($fh, " '$key' => true,\n");
  81. else
  82. fwrite($fh, " '$key' => false,\n");
  83. }
  84. else
  85. fwrite($fh, " '$key' => '$value',\n");
  86. }
  87. fwrite($fh, ");");
  88. fclose($fh);
  89. $this->log("An encryption key has been added to protected/config/params.php.");
  90. }
  91. else
  92. {
  93. $this->log("Please add the following to your protected/config/main.php file's params section:");
  94. $this->log("'encryptionKey' => '$hash'");
  95. }
  96. return $hash;
  97. }
  98. /**
  99. * Generates a new admin user if one does not already exist
  100. * @param $username string The email address of the user
  101. * @param $password string The password for this new user
  102. */
  103. private function generateFirstUser($username, $password)
  104. {
  105. if (Yii::app()->params['encryptionKey'] == NULL)
  106. Yii::app()->params['encryptionKey'] = $this->generateHash(true);
  107. Yii::import('application.models.Users');
  108. $count = Users::model()->count();
  109. if ($count != 0)
  110. return $this->log('Admin user already exists, aborting generation');
  111. $connection = Yii::app()->db;
  112. $connection->createCommand('INSERT INTO users (id, email, password, firstName, lastName, displayName, user_role, status, created, updated) VALUES (NULL, :email, :password, NULL, NULL, "administrator", 9, 1, UTC_TIMESTAMP(), UTC_TIMESTAMP())')
  113. ->bindParam(':email', $username)
  114. ->bindParam(':password', $this->getEncryptedPassword($username, $password, Yii::app()->params['encryptionKey']))
  115. ->execute();
  116. return $this->log("A new admin user has been created");
  117. }
  118. /**
  119. * Generates the appropriate hash for the user
  120. * @param $email string The user's email address
  121. * @param $password string The user's password
  122. * @param $hash string The provided password
  123. */
  124. private function encryptHash($email, $password, $_dbsalt)
  125. {
  126. return mb_strimwidth(hash("sha512", hash("sha512", hash("whirlpool", md5($password . md5($email)))) . hash("sha512", md5($password . md5($_dbsalt))) . $_dbsalt), 0, 64);
  127. }
  128. /**
  129. * Generates an encrypted password for the suer
  130. * @param $email string The user's email address
  131. * @param $password string The user's password
  132. * @param $hash string The provided password
  133. */
  134. public function getEncryptedPassword($email, $password, $hash)
  135. {
  136. if (!function_exists('password_hash'))
  137. require_once YiiBase::getPathOfAlias('ext.bcrypt.bcrypt').'.php';
  138. $hash = $this->encryptHash($email, $password, $hash);
  139. return password_hash($hash, PASSWORD_BCRYPT, array('cost' => 13));
  140. }
  141. }