/phplib/session.php

https://github.com/clyfe/DEXonline · PHP · 198 lines · 159 code · 27 blank · 12 comment · 30 complexity · bf504ad5a6856098fa61019cc2e70759 MD5 · raw file

  1. <?php
  2. function session_init() {
  3. if (isset($_COOKIE[session_name()])) {
  4. session_start();
  5. }
  6. if (util_isWebBasedScript()) {
  7. if (!session_userExists()) {
  8. session_loadUserFromCookie();
  9. }
  10. }
  11. // Otherwise we're being called by a local script, not a web-based one.
  12. }
  13. function session_login($user) {
  14. session_setVariable('user', $user);
  15. $cookie = new Cookie();
  16. $cookie->userId = $user->id;
  17. $cookie->cookieString = util_randomCapitalLetterString(12);
  18. $cookie->save();
  19. setcookie("prefs[lll]", $cookie->cookieString,
  20. time() + ONE_MONTH_IN_SECONDS);
  21. log_userLog('Logged in, IP=' . $_SERVER['REMOTE_ADDR']);
  22. util_redirect(util_getWwwRoot());
  23. }
  24. function session_logout() {
  25. log_userLog('Logging out, IP=' . $_SERVER['REMOTE_ADDR']);
  26. $cookieString = session_getCookieSetting('lll');
  27. $cookie = Cookie::get("cookieString = '$cookieString'");
  28. if ($cookie) {
  29. $cookie->delete();
  30. }
  31. setcookie("prefs[lll]", NULL, time() - 3600);
  32. unset($_COOKIE['prefs']['lll']);
  33. session_kill();
  34. util_redirect(util_getWwwRoot());
  35. }
  36. // Try to load loing information from the long-lived cookie
  37. function session_loadUserFromCookie() {
  38. if (!isset($_COOKIE['prefs']) || !isset($_COOKIE['prefs']['lll'])) {
  39. return;
  40. }
  41. $cookie = Cookie::get(sprintf('cookieString = "%s"', $_COOKIE['prefs']['lll']));
  42. $user = $cookie ? User::get("id={$cookie->userId}") : null;
  43. if ($user) {
  44. session_setVariable('user', $user);
  45. } else {
  46. // There is a cookie, but it is invalid
  47. setcookie("prefs[lll]", NULL, time() - 3600);
  48. unset($_COOKIE['prefs']['lll']);
  49. }
  50. }
  51. function session_getCookieSetting($name) {
  52. if (array_key_exists('prefs', $_COOKIE)) {
  53. $prefsCookie = $_COOKIE['prefs'];
  54. if (array_key_exists($name, $prefsCookie)) {
  55. return $prefsCookie[$name];
  56. }
  57. }
  58. return FALSE;
  59. }
  60. function session_userExists() {
  61. return session_variableExists('user') && isset($_SESSION['user']->id);
  62. }
  63. function session_getUser() {
  64. if (!session_userExists()) {
  65. return FALSE;
  66. }
  67. return $_SESSION['user'];
  68. }
  69. function session_getUserNick() {
  70. return session_variableExists('user') && isset($_SESSION['user']->nick)
  71. ? $_SESSION['user']->nick : "Anonim";
  72. }
  73. function session_getUserId() {
  74. return session_variableExists('user') && isset($_SESSION['user']->id)
  75. ? $_SESSION['user']->id : 0;
  76. }
  77. function session_user_prefers($pref) {
  78. if (isset($_SESSION['user'])) {
  79. return isset($_SESSION['user']->preferences) && in_array($pref, preg_split('/,/', $_SESSION['user']->preferences));
  80. } else {
  81. $prefs = session_getCookieSetting('anonymousPrefs');
  82. return in_array($pref, preg_split('/,/', $prefs));
  83. }
  84. }
  85. function session_setAnonymousPrefs($pref) {
  86. $_COOKIE['prefs']['anonymousPrefs'] = $pref;
  87. session_sendAnonymousPrefs();
  88. }
  89. function session_sendAnonymousPrefs() {
  90. setcookie('prefs[anonymousPrefs]', session_getAnonymousPrefs(), time() + 3600 * 24 * 365, "/");
  91. }
  92. function session_getAnonymousPrefs() {
  93. $cookiePrefs = session_getCookieSetting('anonymousPrefs');
  94. return $cookiePrefs ? $cookiePrefs : '';
  95. }
  96. function session_getSkin() {
  97. $user = session_getUser();
  98. $skin = ($user && $user->skin) ? $user->skin : session_getCookieSetting('skin');
  99. if ($skin && session_isValidSkin($skin)) {
  100. return $skin;
  101. } else {
  102. $skins = pref_getServerPreference('skins');
  103. return $skins[0];
  104. }
  105. }
  106. function session_setSkin($skin) {
  107. $_COOKIE['prefs']['skin'] = $skin;
  108. setcookie('prefs[skin]', session_getSkin(), time() + 3600 * 24 * 365, "/");
  109. }
  110. function session_isValidSkin($skin) {
  111. return in_array($skin, pref_getServerPreference('skins'));
  112. }
  113. /**
  114. * Returns an array of the skin-specific preferences defined in the section skin-{$skin}.
  115. * Returns an empty array if the section is not defined. Never returns false/null.
  116. **/
  117. function session_getSkinPreferences($skin) {
  118. $prefs = pref_getServerPreference("skin-{$skin}");
  119. return $prefs ? $prefs : array();
  120. }
  121. function session_setSourceCookie($source) {
  122. setcookie('prefs[source]', $source, time() + 3600 * 24 * 365, "/");
  123. }
  124. function session_getDefaultContribSourceId() {
  125. $value = session_getCookieSetting('source');
  126. // Previously we stored some short name, not the source id -- just return
  127. // FALSE in that case
  128. return is_numeric($value) ? $value : FALSE;
  129. }
  130. function session_isDebug() {
  131. return session_getUserNick() == pref_getDebugUser();
  132. }
  133. function session_getWithDefault($name, $default) {
  134. if (isset($_SESSION)){
  135. if (array_key_exists($name, $_SESSION)) {
  136. return $_SESSION[$name];
  137. }
  138. }
  139. return $default;
  140. }
  141. function session_setVariable($var, $value) {
  142. // Lazy start of the session so we don't send a PHPSESSID cookie unless we have to
  143. if (!isset($_SESSION)) {
  144. session_start();
  145. }
  146. $_SESSION[$var] = $value;
  147. }
  148. function session_unsetVariable($var) {
  149. if (isset($_SESSION)) {
  150. unset($_SESSION[$var]);
  151. if (!count($_SESSION)) {
  152. // Note that this will prevent us from creating another session this same request.
  153. // This does not seem to cause a problem at the moment.
  154. session_kill();
  155. }
  156. }
  157. }
  158. function session_variableExists($var) {
  159. return isset($_SESSION) && isset($_SESSION[$var]);
  160. }
  161. function session_kill() {
  162. if (!isset($_SESSION)) {
  163. session_start(); // It has to have been started in order to be destroyed.
  164. }
  165. session_unset();
  166. session_destroy();
  167. if (ini_get("session.use_cookies")) {
  168. setcookie(session_name(), '', time() - 3600, '/'); // expire it
  169. }
  170. }
  171. ?>