tipsypastels

Untitled

Feb 19th, 2020
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.98 KB | None | 0 0
  1. <?php
  2.  
  3. require_once(CWD . '/library/php-jwt-master/src/JWTException.php');
  4. require_once(CWD . '/library/php-jwt-master/src/ValidatesJWT.php');
  5. require_once(CWD . '/library/php-jwt-master/src/JWT.php');
  6. require_once('./includes/functions.php');
  7.  
  8. use Ahc\Jwt\JWT;
  9.  
  10. /**
  11. FILES WHERE WE ADDED PC3_ACTIVITY
  12. - index.php
  13. */
  14.  
  15. class PC3_Session {
  16. /**
  17. * Constructor. Attempts to grab a session that matches parameters, but will create one if it can't.
  18. *
  19. * @param vB_Registry Reference to a registry object
  20. * @param string styleid calculated in init.php
  21. */
  22.  
  23. function __construct(&$registry, $styleid)
  24. {
  25. $this->registry =& $registry;
  26. $this->token = $registry->GPC['pc3_token'];
  27. $this->refreshtoken = $registry->GPC['pc3_refreshtoken'];
  28. $this->styleid = intval($styleid);
  29. $this->lastactivity = TIMENOW;
  30. $this->languageid = 4; // english
  31. $this->created = TRUE; // stub, doesn't matter with stateless auth
  32. $this->jwt = new JWT(
  33. $registry->config['PC3_Bridge']['jwt_secret'],
  34. 'HS256',
  35. $registry->config['PC3_Bridge']['jwt_expires_in'],
  36. );
  37.  
  38. $this->authenticate_by_access_token() || $this->authenticate_by_refresh_token();
  39.  
  40. if ($this->deviceid) {
  41. $this->device = $registry->db->query_first_slave("
  42. select * from user_authenticated_devices
  43. where id = '" . $registry->db->escape_string($this->deviceid) .
  44. "'");
  45. }
  46.  
  47. // not a stored device, don't authenticate
  48. if (!$this->device) {
  49. setcookie('pc3_token', '');
  50. $this->userid = null;
  51. }
  52.  
  53. $this->fetch_userinfo();
  54.  
  55. $this->set('styleid', $styleid);
  56. }
  57.  
  58. private function authenticate_by_access_token() {
  59. if ($this->token) {
  60. try {
  61. $this->set_payload($this->jwt->decode($this->token));
  62. return true;
  63. } catch(Exception $e) {}
  64. }
  65.  
  66. return false;
  67. }
  68.  
  69. private function authenticate_by_refresh_token() {
  70. if ($this->refreshtoken) {
  71. try {
  72. $payload = $this->jwt->decode($this->refreshtoken);
  73.  
  74. if ($payload['type'] !== 'refresh') {
  75. throw new Exception("tried to use non-refresh token as refresh token");
  76. }
  77.  
  78. $access_token = $this->jwt->encode(array(
  79. 'userId' => $payload['userId'],
  80. 'deviceId' => $payload['deviceId'],
  81. ));
  82.  
  83. setcookie(
  84. 'pc3_token',
  85. $access_token,
  86. time() + $this->registry->config['PC3_Bridge']['jwt_expires_in'],
  87. "/",
  88. "",
  89. TRUE
  90. );
  91. $this->token = $access_token;
  92. $this->set_payload($payload);
  93.  
  94. return true;
  95. } catch(Exception $e) {
  96. die($e);
  97. }
  98. }
  99.  
  100. return false;
  101. }
  102.  
  103. private function set_payload($payload) {
  104. if ($payload) {
  105. $this->userid = $payload['userId'];
  106. $this->deviceid = $payload['deviceId'];
  107. }
  108. }
  109.  
  110. function save() {
  111. // no-op
  112. }
  113.  
  114. function set_session_visibility() {
  115. // no-op
  116. }
  117.  
  118. function do_lastvisit_update() {
  119. $ip = '"' . $this->registry->db->escape_string(IPADDRESS) . '"';
  120. $ua = '"' . $this->registry->db->escape_string(USER_AGENT) . '"';
  121. $uid = $this->userid ? $this->userid : 'NULL';
  122. $activity = (defined('PC3_ACTIVITY') && PC3_ACTIVITY) ? ('"' . PC3_ACTIVITY . '"') : NULL;
  123.  
  124. if ($activity) {
  125. $this->registry->db->query_write("
  126. replace into online_visitor_activities (ipAddress, userAgent, userId, activity)
  127. values ($ip, $ua, $uid, $activity)
  128. ");
  129. }
  130. }
  131.  
  132. function set($key, $value) {
  133. $this->$key = $value;
  134. $this->changes[$key] = $value;
  135. }
  136.  
  137.  
  138. function fetch_sessionhash() {
  139. return md5(uniqid(microtime(), true));
  140. }
  141.  
  142.  
  143. function &fetch_userinfo() {
  144. if($this->userinfo) {
  145. // we have this already so don’t bother
  146. return $this->userinfo;
  147. }
  148. else if ($this->userid AND !defined('SKIP_USERINFO')) {
  149. // user is logged in
  150. $useroptions = (defined('IN_CONTROL_PANEL') ? FETCH_USERINFO_ADMIN : 0) + (defined('AVATAR_ON_NAVBAR') ? FETCH_USERINFO_AVATAR : 0);
  151. $this->userinfo = fetch_userinfo($this->userid, $useroptions, $this->languageid);
  152. return $this->userinfo;
  153. }
  154. else {
  155. // guest setup
  156. $this->userinfo = array(
  157. 'userid' => 0,
  158. 'usergroupid' => 1,
  159. 'username' => (!empty($_REQUEST['username']) ? htmlspecialchars_uni($_REQUEST['username']) : ''),
  160. 'password' => '',
  161. 'email' => '',
  162. 'styleid' => $this->styleid,
  163. 'languageid' => $this->languageid,
  164. 'lastactivity' => $this->lastactivity,
  165. 'daysprune' => 0,
  166. 'timezoneoffset' => $this->registry->options['timeoffset'],
  167. 'dstonoff' => $this->registry->options['dstonoff'],
  168. 'showsignatures' => 1,
  169. 'showavatars' => 1,
  170. 'showimages' => 1,
  171. 'showusercss' => 1,
  172. 'dstauto' => 0,
  173. 'maxposts' => -1,
  174. 'startofweek' => 1,
  175. 'threadedmode' => $this->registry->options['threadedmode'],
  176. 'securitytoken' => 'guest',
  177. 'securitytoken_raw' => 'guest'
  178. );
  179.  
  180. $this->userinfo['options'] =
  181. $this->registry->bf_misc_useroptions['showsignatures'] | $this->registry->bf_misc_useroptions['showavatars'] |
  182. $this->registry->bf_misc_useroptions['showimages'] | $this->registry->bf_misc_useroptions['dstauto'] |
  183. $this->registry->bf_misc_useroptions['showusercss'];
  184.  
  185. if (!defined('SKIP_USERINFO'))
  186. {
  187. // get default language
  188. $phraseinfo = $this->registry->db->query_first_slave("
  189. SELECT languageid" . fetch_language_fields_sql(0) . "
  190. FROM " . TABLE_PREFIX . "language
  191. WHERE languageid = " . (!empty($this->vars['languageid']) ? $this->vars['languageid'] : intval($this->registry->options['languageid'])) . "
  192. ");
  193. if (empty($phraseinfo))
  194. { // can't phrase this since we can't find the language
  195. trigger_error('The requested language does not exist, reset via tools.php.', E_USER_ERROR);
  196. }
  197. foreach($phraseinfo AS $_arrykey => $_arryval)
  198. {
  199. $this->userinfo["$_arrykey"] = $_arryval;
  200. }
  201. unset($phraseinfo);
  202. }
  203.  
  204. return $this->userinfo;
  205. }
  206. }
  207. }
  208.  
  209. // function human_readable_visitor_activity($activity) {
  210. // $matches = NULL;
  211.  
  212. // switch(true) {
  213. // case preg_match('/^index$/', $activity): {
  214. // return _link_to('Viewing Index', '/');
  215. // }
  216. // case preg_match('/^thread (\\d+)$/', $activity, $matches, PREG_UNMATCHED_AS_NULL): {
  217. // $thread = fetch_threadinfo($matches[1]);
  218. // return _use_thread($thread);
  219. // }
  220. // default: {
  221. // return NULL;
  222. // }
  223. // }
  224. // }
  225.  
  226. // function _use_thread($thread) {
  227. // if (_can_see_thread($thread)) {
  228. // return _link_to("Viewing Thread $thread[title]", "/showthread.php?t=$thread[threadid]");
  229. // } else {
  230. // return _link_to('Viewing Thread');
  231. // }
  232. // }
  233.  
  234. // // centralize this better for newcore
  235. // function _can_see_thread($thread) {
  236. // global $vbulletin;
  237.  
  238. // if (!can_see_unpublished_post($thread) OR ($thread['isdeleted'] AND !can_moderate($thread['forumid']))) {
  239. // return false;
  240. // }
  241.  
  242. // $forumperms = fetch_permissions($thread['forumid']);
  243.  
  244. // if (!($forumperms & $vbulletin->bf_ugp_forumpermissions['canview']) OR !($forumperms & $vbulletin->bf_ugp_forumpermissions['canviewthreads']))
  245. // {
  246. // return false;
  247. // }
  248.  
  249. // if (!($forumperms & $vbulletin->bf_ugp_forumpermissions['canviewothers']) AND ($thread['postuserid'] != $vbulletin->userinfo['userid'] OR $vbulletin->userinfo['userid'] == 0))
  250. // {
  251. // return false;
  252. // }
  253.  
  254. // return true;
  255. // }
  256.  
  257. // function _link_to($title, $path = NULL) {
  258. // return array(
  259. // 'title' => $title,
  260. // 'path' => $path,
  261. // );
  262. // }
Advertisement
Add Comment
Please, Sign In to add comment