PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/webapp/lib/OpenPNE/Auth.php

https://github.com/4260/OpenPNE2
PHP | 331 lines | 214 code | 27 blank | 90 comment | 43 complexity | 59df0bdfb4b368736004e9aec0761e4b MD5 | raw file
  1. <?php
  2. /**
  3. * @copyright 2005-2008 OpenPNE Project
  4. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  5. */
  6. /**
  7. * PEAR::Auth class
  8. */
  9. require_once 'Auth.php';
  10. /**
  11. * 認証処理をおこなうクラス
  12. *
  13. * requires PEAR::Auth
  14. *
  15. * @package OpenPNE
  16. * @author OGAWA Rinpei <ogawa@tejimaya.com>
  17. */
  18. class OpenPNE_Auth
  19. {
  20. /**
  21. * @var Auth
  22. */
  23. var $auth;
  24. var $storage = 'DB';
  25. var $options = '';
  26. var $is_ktai = false;
  27. var $is_admin = false;
  28. var $is_encrypt_username = false;
  29. var $is_lowercase_username = false;
  30. var $is_check_user_agent = true;
  31. var $expire = 0;
  32. var $idle = 0;
  33. var $uid = 0;
  34. var $sess_id = '';
  35. var $cookie_path = '/';
  36. /**
  37. * 設定値の初期化をおこなう
  38. *
  39. * $config に渡せる値
  40. * + storage: PEAR::Authのストレージコンテナ名
  41. * + options: PEAR::Auth用オプション
  42. * + is_ktai: 携帯用認証かどうか
  43. * + is_admin: 管理画面用認証かどうか
  44. * + is_encrypt_username: usernameをt_encryptで暗号化するかどうか
  45. * + is_lowercase_username: usernameを強制的に小文字にするかどうか
  46. *
  47. * @param array $config
  48. */
  49. function OpenPNE_Auth($config = array())
  50. {
  51. ini_set('session.use_cookies', 0);
  52. if (isset($config['is_ktai'])) {
  53. $this->is_ktai = (bool)$config['is_ktai'];
  54. }
  55. if (isset($config['is_admin'])) {
  56. $this->is_admin = (bool)$config['is_admin'];
  57. }
  58. if (isset($config['storage'])) {
  59. $this->storage = $config['storage'];
  60. }
  61. if (isset($config['options'])) {
  62. $this->options = $config['options'];
  63. }
  64. if (isset($config['is_encrypt_username'])) {
  65. $this->is_encrypt_username = $config['is_encrypt_username'];
  66. }
  67. if (isset($config['is_lowercase_username'])) {
  68. $this->is_lowercase_username = $config['is_lowercase_username'];
  69. }
  70. if (isset($config['is_check_user_agent'])) {
  71. $this->is_check_user_agent = (bool)$config['is_check_user_agent'];
  72. }
  73. if ($this->is_ktai) {
  74. if (!empty($_REQUEST['ksid'])) {
  75. $this->sess_id = $_REQUEST['ksid'];
  76. session_id($this->sess_id);
  77. }
  78. $this->options['advancedsecurity'] = false;
  79. } else {
  80. if (!empty($_COOKIE[session_name()])) {
  81. $this->sess_id = $_COOKIE[session_name()];
  82. session_id($this->sess_id);
  83. }
  84. // cookie_path を OPENPNE_URLから抜き出す
  85. $url = parse_url(OPENPNE_URL);
  86. if (substr($url['path'], -1) != '/') {
  87. $url['path'] .= '/';
  88. }
  89. $this->cookie_path = $url['path'];
  90. }
  91. }
  92. /**
  93. * PEAR::Authのインスタンスを生成する
  94. *
  95. * @param bool $login ログイン処理に使用するかどうか
  96. * @return Auth
  97. */
  98. function &factory($login = false)
  99. {
  100. @session_start();
  101. if ($login) {
  102. $auth = new Auth($this->storage, $this->options, '', false);
  103. $auth->setAllowLogin(true);
  104. } else {
  105. $auth = new Auth('null');
  106. $auth->setAllowLogin(false);
  107. }
  108. $auth->setExpire($this->expire);
  109. $auth->setIdle($this->idle);
  110. return $auth;
  111. }
  112. /**
  113. * リクエストからログイン処理をおこなう
  114. *
  115. * @param bool $is_save_cookie クッキーの保存期限を設定するかどうか
  116. * @return bool
  117. */
  118. function login($is_save_cookie = false)
  119. {
  120. $this->auth =& $this->factory(true);
  121. if ($this->is_lowercase_username) {
  122. $this->auth->post[$this->auth->_postUsername] =
  123. strtolower($this->auth->post[$this->auth->_postUsername]);
  124. }
  125. if ($this->is_encrypt_username) {
  126. $this->auth->post[$this->auth->_postUsername] =
  127. t_encrypt($this->auth->post[$this->auth->_postUsername]);
  128. }
  129. $this->auth->start();
  130. if ($this->auth->getAuth()) {
  131. if (OPENPNE_SESSION_CHECK_URL) {
  132. $this->auth->setAuthData('OPENPNE_URL', OPENPNE_URL);
  133. }
  134. if ($this->is_check_user_agent) {
  135. $this->auth->setAuthData('USER_AGENT', $_SERVER['HTTP_USER_AGENT']);
  136. }
  137. $this->sess_id = session_id();
  138. if (!$this->is_ktai) {
  139. if ($is_save_cookie) {
  140. $expire = time() + 2592000; // 30 days
  141. } else {
  142. $expire = 0;
  143. }
  144. setcookie(session_name(), session_id(), $expire, $this->cookie_path);
  145. }
  146. return true;
  147. } else {
  148. return false;
  149. }
  150. }
  151. /**
  152. * 認証処理をおこなう
  153. *
  154. * @return bool 認証が成功したかどうか
  155. */
  156. function auth()
  157. {
  158. if (!$this->sess_id) {
  159. return false;
  160. }
  161. $this->auth =& $this->factory();
  162. return $this->checkAuth();
  163. }
  164. /**
  165. * ログアウト処理をおこなう
  166. *
  167. * @return bool
  168. */
  169. function logout()
  170. {
  171. if (!$this->auth) {
  172. if (!$this->sess_id) {
  173. return true;
  174. }
  175. $this->auth =& $this->factory();
  176. }
  177. if ($this->is_admin) {
  178. db_admin_update_c_admin_user_delete_sess_id(session_id());
  179. } else {
  180. db_member_update_c_member_secure_delete_sess_id(session_id());
  181. }
  182. if (isset($_COOKIE[session_name()])) {
  183. if (!$this->is_ktai) {
  184. setcookie(session_name(), '', time() - 3600, $this->cookie_path);
  185. }
  186. }
  187. $_SESSION = array();
  188. session_destroy();
  189. unset($this->auth);
  190. $this->set_session_save_handler();
  191. return true;
  192. }
  193. /**
  194. * セッションの生成時からの有効期限を設定する
  195. *
  196. * @param int $expiretime
  197. */
  198. function setExpire($expiretime)
  199. {
  200. $this->expire = $expiretime;
  201. }
  202. /**
  203. * セッションの最終アクセス時からの有効期限を設定する
  204. *
  205. * @param int $idletime
  206. */
  207. function setIdle($idletime)
  208. {
  209. $this->idle = $idletime;
  210. }
  211. /**
  212. * uid getter/setter
  213. *
  214. * @param string $uid
  215. * @return string
  216. */
  217. function uid($uid = '')
  218. {
  219. if ($uid) {
  220. $this->uid = $uid;
  221. }
  222. return $this->uid;
  223. }
  224. /**
  225. * PEAR::Auth側のusernameを返す
  226. *
  227. * @return string
  228. */
  229. function getUsername()
  230. {
  231. return $this->auth->getUsername();
  232. }
  233. /**
  234. * セッションハンドラを設定する
  235. *
  236. * @static
  237. */
  238. function set_session_save_handler()
  239. {
  240. static $sess_storage;
  241. if (is_null($sess_storage)) {
  242. switch (SESSION_STORAGE) {
  243. case 1:
  244. include_once 'OpenPNE/DBSession.php';
  245. $sess_storage = new OpenPNE_DBSession(db_get_dsn('session'));
  246. break;
  247. case 2:
  248. include_once 'OpenPNE/MemcacheSession.php';
  249. $sess_storage = new OpenPNE_MemcacheSession($GLOBALS['_OPENPNE_MEMCACHE_LIST']['session']['dsn']);
  250. break;
  251. default:
  252. return;
  253. }
  254. }
  255. if (!is_null($sess_storage)) {
  256. session_set_save_handler(array(&$sess_storage, 'open'),
  257. array(&$sess_storage, 'close'),
  258. array(&$sess_storage, 'read'),
  259. array(&$sess_storage, 'write'),
  260. array(&$sess_storage, 'destroy'),
  261. array(&$sess_storage, 'gc'));
  262. }
  263. }
  264. /**
  265. * checkAuth
  266. *
  267. * PEAR::Auth標準の認証処理に加えて、OPENPNE_URL及び
  268. * USER_AGENTのチェックもおこなう
  269. *
  270. * @return bool
  271. * @see PEAR::Auth::checkAuth
  272. */
  273. function checkAuth()
  274. {
  275. if (!$this->auth->checkAuth()) {
  276. return false;
  277. }
  278. if (OPENPNE_SESSION_CHECK_URL) {
  279. $openpne_url = $this->auth->getAuthData('OPENPNE_URL');
  280. if ($openpne_url !== OPENPNE_URL) {
  281. return false;
  282. }
  283. }
  284. if ($this->is_check_user_agent) {
  285. $login_user_agent = $this->auth->getAuthData('USER_AGENT');
  286. $now_user_agent = $_SERVER['HTTP_USER_AGENT'];
  287. // ドコモ端末からのアクセスの場合、ユーザエージェント情報から個体識別情報を取り除く
  288. if ($GLOBALS['__Framework']['carrier'] == 'i') {
  289. if (strncmp($now_user_agent, 'DoCoMo/1.0', 10) === 0) {
  290. $login_user_agent = substr($login_user_agent, 0, strpos($login_user_agent, '/ser'));
  291. $now_user_agent = substr($now_user_agent, 0, strpos($now_user_agent, '/ser'));
  292. } elseif (strncmp($now_user_agent, 'DoCoMo/2.0', 10) === 0) {
  293. $login_user_agent = substr($login_user_agent, 0, strpos($login_user_agent, '('));
  294. $now_user_agent = substr($now_user_agent, 0, strpos($now_user_agent, '('));
  295. }
  296. }
  297. if ($now_user_agent !== $login_user_agent) {
  298. return false;
  299. }
  300. }
  301. return true;
  302. }
  303. }
  304. ?>