ExtendedDBAuthAdapter.class.php 8.0 KB

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