session.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * Part of the Fuel framework.
  4. *
  5. * @package Fuel
  6. * @version 1.5
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2013 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Fuel\Tasks;
  13. /**
  14. * Sessions DB Table Task
  15. *
  16. * @PullRequest https://github.com/fuel/core/pull/786
  17. *
  18. * Run this task to set add/remove/clear the db sessions table
  19. * Table name will be generated from your config file.
  20. * for your app. This could be expanded in app/tasks for application specific stuff.
  21. *
  22. * @package Fuel
  23. * @version 1.1
  24. * @author Daniel Berry
  25. * @license MIT License
  26. *
  27. * Usage:
  28. * php oil r session = will prompt with menu
  29. * php oil r session:create = create the db table.
  30. * php oil r session:remove = remove the sessions table
  31. * php oil r session:clear = clear the sessions table
  32. */
  33. class Session
  34. {
  35. // default function if no command is selected. Provided user with menu
  36. public static function run()
  37. {
  38. // Prompt the user with menu options
  39. $option = \Cli::prompt('What would you like to do?', array('create','remove', 'clear', 'help'));
  40. switch($option)
  41. {
  42. case "create":
  43. return static::create();
  44. break;
  45. case "remove":
  46. return static::remove();
  47. break;
  48. case "clear":
  49. return static::clear();
  50. break;
  51. default:
  52. return static::help();
  53. break;
  54. }
  55. }
  56. /**
  57. * create the sessions table
  58. * php oil r session:create
  59. */
  60. public static function create()
  61. {
  62. // load session config
  63. \Config::load('session', true);
  64. if (\Config::get('session.driver') != 'db')
  65. {
  66. // prompt the user to confirm they want to remove the table.
  67. $continue = \Cli::prompt(\Cli::color('Your current driver type is not set db. Would you like to continue and add the sessions table anyway?', 'yellow'), array('y','n'));
  68. if ($continue === 'n')
  69. {
  70. return \Cli::color('Database sessions table was not created.', 'red');
  71. }
  72. }
  73. // create the session table using the table name from the config file
  74. \DBUtil::create_table(\Config::get('session.db.table'), array(
  75. 'session_id' => array('constraint' => 40, 'type' => 'varchar'),
  76. 'previous_id' => array('constraint' => 40, 'type' => 'varchar'),
  77. 'user_agent' => array('type' => 'text', 'null' => false),
  78. 'ip_hash' => array('constraint' => 32, 'type' => 'char'),
  79. 'created' => array('constraint' => 10, 'type' => 'int', 'unsigned' => true),
  80. 'updated' => array('constraint' => 10, 'type' => 'int', 'unsigned' => true),
  81. 'payload' => array('type' => 'longtext'),
  82. ), array('session_id'), false, 'InnoDB', \Config::get('db.default.charset'));
  83. // make previous_id a unique_key. speeds up query and prevents duplicate id's
  84. \DBUtil::create_index(\Config::get('session.db.table'), 'previous_id', 'previous_id', 'unique');
  85. if (\Config::get('session.driver') === 'db')
  86. {
  87. // return success message.
  88. return \Cli::color('Success! Your session table has been created!', 'green');
  89. }
  90. else
  91. {
  92. // return success message notifying that the driver is not db.
  93. return \Cli::color('Success! Your session table has been created! Your current session driver type is set to '.\Config::get('session.driver').'. In order to use the table you just created to manage your sessions, you will need to set your driver type to "db" in your session config file.', 'green');
  94. }
  95. }
  96. /**
  97. * remove the sessions table
  98. * php oil r session:remove
  99. */
  100. public static function remove()
  101. {
  102. // load session config
  103. \Config::load('session', true);
  104. // prompt the user to confirm they want to remove the table.
  105. $iamsure = \Cli::prompt('Are you sure you want to delete the sessions table?', array('y','n'));
  106. // if they are sure, then let's drop it
  107. if ($iamsure === 'y')
  108. {
  109. \DBUtil::drop_table(\Config::get('session.db.table'));
  110. return \Cli::color('Session database table deleted.', 'green');
  111. }
  112. // if we made it to here, than that means the user said no.
  113. return \Cli::color('Session database table was not deleted.', 'red');
  114. }
  115. /**
  116. * clear the sessions table
  117. * php oil r session:clear
  118. */
  119. public static function clear()
  120. {
  121. // load session config
  122. \Config::load('session', true);
  123. // prompt the user to confirm they want to clear the table.
  124. $iamsure = \Cli::prompt('Are you sure you want to clear the sessions table?', array('y','n'));
  125. // if they are sure, then let's drop it
  126. if ($iamsure === 'y')
  127. {
  128. \DBUtil::truncate_table(\Config::get('session.db.table'));
  129. return \Cli::color('Session database table successfully truncated.', 'green');
  130. }
  131. // if we made it to here, than that means the user said no.
  132. return \Cli::color('Session database table was not cleared.', 'red');
  133. }
  134. /**
  135. * Shows basic help instructions for using migrate in oil
  136. */
  137. public static function help()
  138. {
  139. echo <<<HELP
  140. Usage:
  141. php oil refine session
  142. Description:
  143. The session task will create the necessary db tables.
  144. Examples:
  145. php oil r session:create
  146. php oil r session:remove
  147. php oil r session:clear
  148. php oil r session:help
  149. HELP;
  150. }
  151. }
  152. /* End of file tasks/session.php */