Advertisement
Guest User

Untitled

a guest
Jul 10th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.43 KB | None | 0 0
  1. <?PHP
  2. // The Config class provides a single object to store your application's settings.
  3. // Define your settings as public members. (We've already setup the standard options
  4. // required for the Database and Auth classes.) Then, assign values to those settings
  5. // inside the "location" functions. This allows you to have different configuration
  6. // options depending on the server environment you're running on. Ex: local, staging,
  7. // and production.
  8.  
  9. class Config
  10. {
  11. // Singleton object. Leave $me alone.
  12. private static $me;
  13.  
  14. // Add your server hostnames to the appropriate arrays. ($_SERVER['HTTP_HOST'])
  15. private $productionServers = array($_SERVER['HTTP_HOST']);
  16. private $stagingServers = array($_SERVER['HTTP_HOST']);
  17. private $localServers = array($_SERVER['HTTP_HOST']);
  18.  
  19. // Standard Config Options...
  20.  
  21. // ...For Auth Class
  22. public $authDomain; // Domain to set for the cookie
  23. public $authSalt; // Can be any random string of characters
  24.  
  25. // ...For Database Class
  26. public $dbReadHost; // Database read-only server
  27. public $dbWriteHost; // Database read/write server
  28. public $dbName;
  29. public $dbReadUsername;
  30. public $dbWriteUsername;
  31. public $dbReadPassword;
  32. public $dbWritePassword;
  33.  
  34. public $dbOnError; // What do do on a database error (see class.database.php for details)
  35. public $dbEmailOnError; // Email an error report on error?
  36.  
  37. // Add your config options here...
  38. public $useDBSessions; // Set to true to store sessions in the database
  39.  
  40. // Singleton constructor
  41. private function __construct()
  42. {
  43. $this->everywhere();
  44.  
  45. $i_am_here = $this->whereAmI();
  46.  
  47. if('production' == $i_am_here)
  48. $this->production();
  49. elseif('staging' == $i_am_here)
  50. $this->staging();
  51. elseif('local' == $i_am_here)
  52. $this->local();
  53. elseif('shell' == $i_am_here)
  54. $this->shell();
  55. else
  56. die('<h1>Where am I?</h1> <p>You need to setup your server names in <code>class.config.php</code></p>
  57. <p><code>$_SERVER[\'HTTP_HOST\']</code> reported <code>' . $_SERVER['HTTP_HOST'] . '</code></p>');
  58. }
  59.  
  60. // Get Singleton object
  61. public static function getConfig()
  62. {
  63. if(is_null(self::$me))
  64. self::$me = new Config();
  65. return self::$me;
  66. }
  67.  
  68. // Allow access to config settings statically.
  69. // Ex: Config::get('some_value')
  70. public static function get($key)
  71. {
  72. return self::$me->$key;
  73. }
  74.  
  75. // Add code to be run on all servers
  76. private function everywhere()
  77. {
  78. // Store sesions in the database?
  79. $this->useDBSessions = false;
  80.  
  81. // Settings for the Auth class
  82. $this->authDomain = $_SERVER['HTTP_HOST'];
  83. $this->authSalt = '';
  84. }
  85.  
  86. // Add code/variables to be run only on production servers
  87. private function production()
  88. {
  89. ini_set('display_errors', '0');
  90.  
  91. define('WEB_ROOT', '/');
  92.  
  93. $this->dbReadHost = 'localhost';
  94. $this->dbWriteHost = 'localhost';
  95. $this->dbName = '';
  96. $this->dbReadUsername = '';
  97. $this->dbWriteUsername = '';
  98. $this->dbReadPassword = '';
  99. $this->dbWritePassword = '';
  100. $this->dbOnError = '';
  101. $this->dbEmailOnError = false;
  102. }
  103.  
  104. // Add code/variables to be run only on staging servers
  105. private function staging()
  106. {
  107. ini_set('display_errors', '1');
  108. ini_set('error_reporting', E_ALL);
  109.  
  110. define('WEB_ROOT', '');
  111.  
  112. $this->dbReadHost = 'localhost';
  113. $this->dbWriteHost = 'localhost';
  114. $this->dbName = '';
  115. $this->dbReadUsername = '';
  116. $this->dbWriteUsername = '';
  117. $this->dbReadPassword = '';
  118. $this->dbWritePassword = '';
  119. $this->dbOnError = 'die';
  120. $this->dbEmailOnError = false;
  121. }
  122.  
  123. // Add code/variables to be run only on local (testing) servers
  124. private function local()
  125. {
  126. ini_set('display_errors', '1');
  127. ini_set('error_reporting', E_ALL);
  128.  
  129. define('WEB_ROOT', '');
  130.  
  131. $this->dbReadHost = 'localhost';
  132. $this->dbWriteHost = 'localhost';
  133. $this->dbName = '';
  134. $this->dbReadUsername = '';
  135. $this->dbWriteUsername = '';
  136. $this->dbReadPassword = '';
  137. $this->dbWritePassword = '';
  138. $this->dbOnError = 'die';
  139. $this->dbEmailOnError = false;
  140. }
  141.  
  142. // Add code/variables to be run only on when script is launched from the shell
  143. private function shell()
  144. {
  145. ini_set('display_errors', '1');
  146. ini_set('error_reporting', E_ALL);
  147.  
  148. define('WEB_ROOT', '');
  149.  
  150. $this->dbReadHost = 'localhost';
  151. $this->dbWriteHost = 'localhost';
  152. $this->dbName = '';
  153. $this->dbReadUsername = '';
  154. $this->dbWriteUsername = '';
  155. $this->dbReadPassword = '';
  156. $this->dbWritePassword = '';
  157. $this->dbOnError = false;
  158. $this->dbEmailOnError = true;
  159. }
  160.  
  161. public function whereAmI()
  162. {
  163. for($i = 0; $i < count($this->productionServers); $i++)
  164. if(preg_match($this->productionServers[$i], getenv('HTTP_HOST')) === 1)
  165. return 'production';
  166.  
  167. for($i = 0; $i < count($this->stagingServers); $i++)
  168. if(preg_match($this->stagingServers[$i], getenv('HTTP_HOST')) === 1)
  169. return 'staging';
  170.  
  171. for($i = 0; $i < count($this->localServers); $i++)
  172. if(preg_match($this->localServers[$i], getenv('HTTP_HOST')) === 1)
  173. return 'local';
  174.  
  175. if(isset($_ENV['SHELL']))
  176. return 'shell';
  177.  
  178. return false;
  179. }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement