ExtendedDBAuthAdapter.class.php 7.9 KB

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