ExtendedDBAuthAdapter.class.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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_ExtendedDBAuthAdapter 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. /**
  81. * public function getAuth($cred)
  82. {
  83. if($this->checkConfig($this->_settings))
  84. {
  85. if(array_key_exists('username', $cred) AND array_key_exists('password', $cred))
  86. {
  87. $db = Staple_DB::get();
  88. $this->uid = $cred['username'];
  89. switch($this->_settings['pwenctype'])
  90. {
  91. case 'MD5':
  92. $pass = md5($cred['password']);
  93. break;
  94. case 'SHA1':
  95. $pass =sha1($cred['password']);
  96. break;
  97. //case 'AES':
  98. // $pass = Staple_Encrypt::AES_encrypt(($cred['password']),'');
  99. // break;
  100. default:
  101. $pass = $cred['password'];
  102. }
  103. $sql = 'SELECT '.$db->real_escape_string($this->_settings['uidfield']).','.$db->real_escape_string($this->_settings['pwfield']).'
  104. FROM '.$db->real_escape_string($this->_settings['authtable']).'
  105. WHERE '.$db->real_escape_string($this->_settings['uidfield']).' = '.
  106. '\''.$db->real_escape_string($cred['username']).'\'
  107. AND '.$db->real_escape_string($this->_settings['pwfield']).' = '.
  108. '\''.$db->real_escape_string($pass).'\';';
  109. if(($result = $db->query($sql)) !== false)
  110. {
  111. $myrow = $result->fetch_array();
  112. //Secondary check to make sure the results did not differ from MySQL's response.
  113. if($myrow[$this->_settings['uidfield']] == $this->uid && $myrow[$this->_settings['pwfield']] == $pass)
  114. {
  115. return true;
  116. }
  117. }
  118. }
  119. }
  120. return false;
  121. }
  122. *
  123. */
  124. public function getAuth($cred)
  125. {
  126. if ($this->checkConfig($this->_settings))
  127. {
  128. if (array_key_exists('pin', $cred))
  129. {
  130. $db = Staple_DB::get();
  131. switch ($this->_settings['pwenctype'])
  132. {
  133. case 'MD5':
  134. $pass = md5($cred['pin']);
  135. break;
  136. case 'SHA1':
  137. $pass = sha1($cred['pin']);
  138. break;
  139. default:
  140. $pass = $cred['pin'];
  141. }
  142. $sql = 'SELECT ' . $db->real_escape_string($this->_settings['pinfield']) . ',' . $db->real_escape_string($this->_settings['uidfield']) . '
  143. FROM ' . $db->real_escape_string($this->_settings['authtable']) . '
  144. WHERE ' . $db->real_escape_string($this->_settings['pinfield']) . ' = ' .
  145. '\'' . $db->real_escape_string($pass) . '\';';
  146. if(($result = $db->query($sql)) !== false)
  147. {
  148. $myrow = $result->fetch_array();
  149. //Secondary check to make sure the results did not differ from MySQL's response.
  150. if($myrow[$this->_settings['pinfield']] == $pass)
  151. {
  152. $this->uid = $myrow[$this->_settings['uidfield']];
  153. return true;
  154. }
  155. }
  156. }
  157. if (array_key_exists('username', $cred) && array_key_exists('password', $cred))
  158. {
  159. $db = Staple_DB::get();
  160. $this->uid = $cred['username'];
  161. switch ($this->_settings['pwenctype'])
  162. {
  163. case 'MD5':
  164. $pass = md5($cred['password']);
  165. break;
  166. case 'SHA1':
  167. $pass = sha1($cred['password']);
  168. break;
  169. default:
  170. $pass = $cred['password'];
  171. }
  172. $sql = 'SELECT ' . $db->real_escape_string($this->_settings['uidfield']) . ',' . $db->real_escape_string($this->_settings['pwfield']) . '
  173. FROM ' . $db->real_escape_string($this->_settings['authtable']) . '
  174. WHERE ' . $db->real_escape_string($this->_settings['uidfield']) . ' = ' .
  175. '\'' . $db->real_escape_string($cred['username']) . '\'
  176. AND ' . $db->real_escape_string($this->_settings['pwfield']) . ' = ' .
  177. '\'' . $db->real_escape_string($pass) . '\';';
  178. if (($result = $db->query($sql)) !== false)
  179. {
  180. $myrow = $result->fetch_array();
  181. //Secondary check to make sure the results did not differ from MySQL's response.
  182. if ($myrow[$this->_settings['uidfield']] == $this->uid && $myrow[$this->_settings['pwfield']] == $pass)
  183. {
  184. return true;
  185. }
  186. }
  187. }
  188. }
  189. }
  190. /**
  191. * Gets the access level for the supplied $uid.
  192. * @param string $uid
  193. * @return int
  194. * @see Staple_AuthAdapter::getLevel()
  195. */
  196. public function getLevel($uid)
  197. {
  198. if($this->checkConfig($this->_settings))
  199. {
  200. if(array_key_exists('rolefield', $this->_settings))
  201. {
  202. $db = Staple_DB::get();
  203. $sql = 'SELECT '.$db->real_escape_string($this->_settings['rolefield']).'
  204. FROM '.$db->real_escape_string($this->_settings['authtable']).'
  205. WHERE '.$db->real_escape_string($this->_settings['uidfield']).' = '.
  206. '\''.$db->real_escape_string($uid).'\';';
  207. $result = $db->query($sql);
  208. if($result !== false)
  209. {
  210. $myrow = $result->fetch_array();
  211. $level = (int)$myrow[$this->_settings['rolefield']];
  212. if($level < 0)
  213. {
  214. return 0;
  215. }
  216. else
  217. {
  218. return $level;
  219. }
  220. }
  221. else
  222. {
  223. return 0;
  224. }
  225. }
  226. else
  227. {
  228. return 1;
  229. }
  230. }
  231. }
  232. /**
  233. *
  234. * Checks the configuration fields for validity
  235. * @param array $config
  236. * @throws Exception
  237. */
  238. protected function checkConfig(array $config)
  239. {
  240. $keys = array('enabled','adapter','authtable','uidfield','pwfield','pwenctype');
  241. foreach($keys as $value)
  242. {
  243. if(!array_key_exists($value, $config))
  244. {
  245. throw new Exception('Staple_DBAuthAdapter configuration error.',Staple_Error::AUTH_ERROR);
  246. }
  247. }
  248. if($config['adapter'] != get_class($this))
  249. {
  250. throw new Exception('Staple_DBAuthAdapter configuration error.',Staple_Error::AUTH_ERROR);
  251. }
  252. return true;
  253. }
  254. /**
  255. * Returns the User ID from the adapter.
  256. * @return string
  257. * @see Staple_AuthAdapter::getUserId()
  258. */
  259. public function getUserId()
  260. {
  261. return $this->uid;
  262. }
  263. }
  264. ?>