DBAuthAdapter.class.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /**
  3. * This is the packaged database authorization adapter. This adapter requires the following
  4. * settings to be included in the application.ini or the auth.ini file:
  5. *
  6. * enabled - Set to 1 or 0 to enable or disable authentication. 1 is the default setting, if excluded.
  7. * adapter - Tells the Staple_Main class which AuthAdapter to load.
  8. * authtable - Specifies the database table where auth credentials reside.
  9. * uidfield - Defines the username or user identifer field.
  10. * pwfield - Defines the password field.
  11. * pwenctype - The type of encryption used on the password. Values include 'MD5', 'SHA1', 'AES', and 'none'.
  12. * rolefield - (optional) This field specifies the database table that holds the access level. If no field is provided or it is null, 1 will be returned.
  13. *
  14. * @author Ironpilot
  15. * @copyright Copywrite (c) 2011, STAPLE CODE
  16. *
  17. * This file is part of the STAPLE Framework.
  18. *
  19. * The STAPLE Framework is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU Lesser General Public License as published by the
  21. * Free Software Foundation, either version 3 of the License, or (at your option)
  22. * any later version.
  23. *
  24. * The STAPLE Framework is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  26. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  27. * more details.
  28. *
  29. * You should have received a copy of the GNU Lesser General Public License
  30. * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
  31. *
  32. */
  33. class Staple_DBAuthAdapter implements Staple_AuthAdapter
  34. {
  35. /**
  36. * Settings Array
  37. * @deprecated
  38. * @var array
  39. */
  40. private $_settings = array();
  41. /**
  42. * Store the user identifier. Usually the username.
  43. * @var string
  44. */
  45. private $uid;
  46. /**
  47. *
  48. * The constructor loads and checks the adapter configuration.
  49. * @throws Exception
  50. */
  51. public function __construct()
  52. {
  53. if(file_exists(CONFIG_ROOT.'application.ini'))
  54. {
  55. $curConfig = parse_ini_file(CONFIG_ROOT.'application.ini',true);
  56. if($this->checkConfig($curConfig['auth']))
  57. {
  58. $this->_settings = $curConfig['auth'];
  59. }
  60. }
  61. elseif(file_exists(CONFIG_ROOT.'auth.ini'))
  62. {
  63. $curConfig = parse_ini_file(CONFIG_ROOT.'auth.ini');
  64. if($this->checkConfig($curConfig))
  65. {
  66. $this->_settings = $curConfig;
  67. }
  68. }
  69. else
  70. {
  71. throw new Exception('Staple_DBAuthAdapter critical failure.',500);
  72. }
  73. }
  74. /**
  75. * getAuth checks the database for valid credentials and returns true if they are found.
  76. * @param array $cred
  77. * @return bool
  78. * @see Staple_AuthAdapter::getAuth()
  79. */
  80. public function getAuth($cred)
  81. {
  82. if($this->checkConfig($this->_settings))
  83. {
  84. if(array_key_exists('username', $cred) AND array_key_exists('password', $cred))
  85. {
  86. $db = Staple_DB::get();
  87. $this->uid = $cred['username'];
  88. switch($this->_settings['pwenctype'])
  89. {
  90. case 'MD5':
  91. $pass = md5($cred['password']);
  92. break;
  93. case 'SHA1':
  94. $pass =sha1($cred['password']);
  95. break;
  96. //case 'AES':
  97. // $pass = Staple_Encrypt::AES_encrypt(($cred['password']),'');
  98. // break;
  99. default:
  100. $pass = $cred['password'];
  101. }
  102. $sql = 'SELECT '.$db->real_escape_string($this->_settings['uidfield']).','.$db->real_escape_string($this->_settings['pwfield']).'
  103. FROM '.$db->real_escape_string($this->_settings['authtable']).'
  104. WHERE '.$db->real_escape_string($this->_settings['uidfield']).' = '.
  105. '\''.$db->real_escape_string($cred['username']).'\'
  106. AND '.$db->real_escape_string($this->_settings['pwfield']).' = '.
  107. '\''.$db->real_escape_string($pass).'\';';
  108. if(($result = $db->query($sql)) !== false)
  109. {
  110. $myrow = $result->fetch_array();
  111. //Secondary check to make sure the results did not differ from MySQL's response.
  112. if($myrow[$this->_settings['uidfield']] == $this->uid && $myrow[$this->_settings['pwfield']] == $pass)
  113. {
  114. return true;
  115. }
  116. }
  117. }
  118. }
  119. return false;
  120. }
  121. /**
  122. * Gets the access level for the supplied $uid.
  123. * @param string $uid
  124. * @return int
  125. * @see Staple_AuthAdapter::getLevel()
  126. */
  127. public function getLevel($uid)
  128. {
  129. if($this->checkConfig($this->_settings))
  130. {
  131. if(array_key_exists('rolefield', $this->_settings))
  132. {
  133. $db = Staple_DB::get();
  134. $sql = 'SELECT '.$db->real_escape_string($this->_settings['rolefield']).'
  135. FROM '.$db->real_escape_string($this->_settings['authtable']).'
  136. WHERE '.$db->real_escape_string($this->_settings['uidfield']).' = '.
  137. '\''.$db->real_escape_string($uid).'\';';
  138. $result = $db->query($sql);
  139. if($result !== false)
  140. {
  141. $myrow = $result->fetch_array();
  142. $level = (int)$myrow[$this->_settings['rolefield']];
  143. if($level < 0)
  144. {
  145. return 0;
  146. }
  147. else
  148. {
  149. return $level;
  150. }
  151. }
  152. else
  153. {
  154. return 0;
  155. }
  156. }
  157. else
  158. {
  159. return 1;
  160. }
  161. }
  162. }
  163. /**
  164. *
  165. * Checks the configuration fields for validity
  166. * @param array $config
  167. * @throws Exception
  168. */
  169. protected function checkConfig(array $config)
  170. {
  171. $keys = array('enabled','adapter','authtable','uidfield','pwfield','pwenctype');
  172. foreach($keys as $value)
  173. {
  174. if(!array_key_exists($value, $config))
  175. {
  176. throw new Exception('Staple_DBAuthAdapter configuration error.',Staple_Error::AUTH_ERROR);
  177. }
  178. }
  179. if($config['adapter'] != get_class($this))
  180. {
  181. throw new Exception('Staple_DBAuthAdapter configuration error.',Staple_Error::AUTH_ERROR);
  182. }
  183. return true;
  184. }
  185. /**
  186. * Returns the User ID from the adapter.
  187. * @return string
  188. * @see Staple_AuthAdapter::getUserId()
  189. */
  190. public function getUserId()
  191. {
  192. return $this->uid;
  193. }
  194. }
  195. ?>