Advertisement
Guest User

Untitled

a guest
May 7th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.13 KB | None | 0 0
  1. <?php
  2. ######################################################################
  3. # vis_user_create_login_cookie()
  4. #
  5. ######################################################################
  6. function vis_user_create_login_cookie( $sessionToken ) {
  7. if ( VIS_DEBUG_MODE ) {
  8. echo "$ Creating login cookie... <br>";
  9. echo "-> Session Token: $sessionToken <br>";
  10. }
  11.  
  12. # Validate 'session_token' from the parameters and store it in a cookie
  13. $userObject = msc_get_userId( $sessionToken );
  14.  
  15. if ( empty( $userObject ) ) {
  16. if ( VIS_DEBUG_MODE ) {
  17. echo "<p> $ ERROR: Invalid Session Token / Sever down? </p>";
  18. }
  19. }
  20.  
  21. $userId = $userObject["id"];
  22. $userRole = $userObject["role"];
  23.  
  24. # Reducing tuckshop name
  25. if ( $userRole == "tuckshop_user" ) {
  26. $userRole = "tuckshop";
  27. }
  28.  
  29. # Generating sessionId and saving it to a cookie
  30. $sessionId = vis_create_sessionid( $userId, $userRole, $sessionToken );
  31.  
  32. if ( VIS_DEBUG_MODE ) {
  33. echo "-> User Id: $userId <br>";
  34. echo "-> User Role: $userRole <br>";
  35. echo "-> Session Id: $sessionId <br>";
  36. echo "<br><p>$ Saving cookie... <br>";
  37. }
  38. $ret = setcookie( VIS_SESSIONID_COOKIE_NAME, $sessionId, time() + 3600, "/", false );
  39.  
  40. if ( ! $ret ) {
  41. if ( VIS_DEBUG_MODE ) {
  42. echo "-> $ ERROR: Cookies are blocked due to unexpected output.</p>";
  43. }
  44. exit();
  45. }
  46.  
  47. if ( VIS_DEBUG_MODE ) {
  48. echo "-> Cookie saved successfuly.</p>";
  49. }
  50.  
  51. $userObject = array(
  52. "userId" => $userId,
  53. "userRole" => $userRole,
  54. );
  55.  
  56. return $userObject;
  57. }
  58.  
  59. ########################################################################
  60. ## vis_user_create()
  61. ## Creates a user from MSC data.
  62. ########################################################################
  63. function vis_user_create( $userId, $userRole ) {
  64. $userObject;
  65. $userWpId;
  66.  
  67. if ( $userRole == "customer" ) {
  68. if ( VIS_DEBUG_MODE ) {
  69. echo "$ Requesting customer (parent)... <br>";
  70. }
  71.  
  72. $userObject = msc_get_customer($userId);
  73. $userWpId = VIS_GENERIC_CUSTOMER_ID;
  74. } elseif ( $userRole == "tuckshop" ) {
  75. if ( VIS_DEBUG_MODE ) {
  76. echo "$ Requesting user (tuckshop/school)... <br>";
  77. }
  78.  
  79. $userObject = msc_get_user($userId);
  80. $userWpId = VIS_GENERIC_TUCKSHOP_ID;
  81. } else {
  82. if ( VIS_DEBUG_MODE ) {
  83. echo "<p> Invalid user role. </p>";
  84. }
  85.  
  86. return;
  87. }
  88.  
  89. $userFName = $userObject["firstname"];
  90. $userLName = $userObject["lastname"];
  91. $userEmail = $userObject["email"];
  92. $userAccountUrl = $userObject["accountUrl"];
  93. $userEditUrl = $userObject["editUrl"];
  94.  
  95. if ( VIS_DEBUG_MODE ) {
  96. echo "<p> -> User firstname: $userFName <br>";
  97. echo " -> User lastname: $userLName <br>";
  98. echo " -> User Email: $userEmail <br>";
  99. echo " -> User Account URL: $userAccountUrl <br>";
  100. echo " -> User Edit URL: $userEditUrl </p>";
  101. }
  102.  
  103. $wpUserObject = get_user_by( "email", $userEmail );
  104.  
  105. if ( $wpUserObject ) {
  106. if ( VIS_DEBUG_MODE ) {
  107. echo "$ Old user. Found id: " . $wpUserObject->get('ID') . "<br>";
  108. }
  109.  
  110. $userWpId = $wpUserObject->get('ID');
  111. $userMscRoles = get_user_meta($userWpId, VIS_MSCROLES_METADATA_FIELD, true);
  112.  
  113. if ( ! in_array( $userRole, $userMscRoles ) ) {
  114. array_push( $userMscRoles, $userRole );
  115. }
  116. } else {
  117. if ( VIS_DEBUG_MODE ) {
  118. echo "$ User not found. Creating new account with MSC data...<br>";
  119. }
  120.  
  121. $userWpId = vis_user_register($userId, $userEmail, $userFName, $userLName, array($userRole));
  122. $userMscRoles = get_user_meta($userWpId, VIS_MSCROLES_METADATA_FIELD, true);
  123. }
  124.  
  125. // Save the MSC user ID on the WP User meta
  126. //add_user_meta( $userWpId, 'msc_user_id', $userId, true );
  127.  
  128. if ( VIS_DEBUG_MODE ) {
  129. echo "$ User current MSC roles: ";
  130. var_dump( $userMscRoles );
  131. echo "<br>";
  132. }
  133. }
  134.  
  135. ########################################################################
  136. ## vis_user_register ()
  137. ## Responsible to register a new user into WP database.
  138. ########################################################################
  139. function vis_user_register( $userId, $userEmail, $userFName, $userLName, $userMscRoles ) {
  140. // Generate the password and create the user
  141. $password = wp_generate_password( 12, false );
  142. $userWpId = wp_create_user( $userEmail, $password, $userEmail );
  143.  
  144. if( is_wp_error( $userWpId ) ) {
  145. echo $userWpId->get_error_message();
  146. return 0;
  147. }
  148.  
  149. // Set the nickname
  150. wp_update_user(
  151. array(
  152. 'ID' => $userWpId,
  153. 'nickname' => $userEmail,
  154. 'first_name' => $userFName,
  155. 'last_name' => $userLName
  156. ));
  157.  
  158. // Set the role
  159. $user = new WP_User( $userWpId );
  160. $user->set_role('msc_user');
  161.  
  162. return $userWpId;
  163. }
  164.  
  165. ########################################################################
  166. ## vis_user_login()
  167. ## Logs in a user by its Id in the WP site.
  168. ########################################################################
  169. function vis_user_login() {
  170. # Decrypt cookie and load saved data
  171. $cryptedSessionId = $_COOKIE[VIS_SESSIONID_COOKIE_NAME];
  172. $sessionId = vis_decrypt_key($cryptedSessionId);
  173. $userId = vis_get_userid_from_sessionid($sessionId);
  174. $userRole = vis_get_userrole_from_sessionid($sessionId);
  175. $sessionToken = vis_get_sessiontoken_from_sessionid($sessionId);
  176.  
  177. if ( VIS_DEBUG_MODE ) {
  178. echo "<p> -> Session Id: $sessionId <br>";
  179. echo " -> User Id: $userId <br>";
  180. echo " -> User Role: $userRole <br>";
  181. echo " -> Session Token: $sessionToken </p>";
  182. }
  183.  
  184. $userWpId = vis_user_sync( $userId, $userRole );
  185.  
  186. if ( $userWpId > 0 ) {
  187. if ( VIS_DEBUG_MODE ) {
  188. echo "$ Loading user with id: $userWpId ...<br>";
  189. }
  190.  
  191. wp_set_auth_cookie( $userWpId, false, is_ssl() );
  192.  
  193. // Setting msc_role on Session to WooCommerce payments
  194. if ( $userRole == "tuckshop" ) {
  195. $userRole = "tuckshop_user";
  196. } else {
  197. $api_key = get_option( 'msc-login_msc_general_msc_api_key' );
  198.  
  199. $data = array(
  200. 'api_key' => $api_key,
  201. 'session_token' => $sessionToken,
  202. 'id' => $userId
  203. );
  204.  
  205. $options = get_option( 'vis_settings' );
  206. $api_url = $options['vis_msc_url'] . "/index.php?route=api/customer/getSameSchoolTuckshopUsers";
  207.  
  208. $ch = curl_init();
  209. curl_setopt($ch, CURLOPT_URL, $api_url);
  210. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  211. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  212. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  213. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  214. curl_setopt($ch, CURLOPT_POST, true);
  215.  
  216. $result = curl_exec($ch);
  217.  
  218. curl_close($ch);
  219.  
  220. $result = json_decode( $result, true );
  221. $tk_users = $result["data"][0]["tuckshop_users"];
  222. $tuckshop_users = array();
  223.  
  224. if( ! empty( $tk_users ) ) {
  225. foreach( $tk_users as $value ) {
  226. $user = get_users( array(
  227. 'number' => 1,
  228. 'meta_key' => 'msc_tuckshop_id',
  229. 'meta_value' => $value
  230. ) );
  231.  
  232. if( ! empty($user[0] ) ) {
  233. $user_id = $user[0]->data->ID;
  234. $tuckshop_users[] = $user_id;
  235. }
  236. }
  237. }
  238.  
  239. if( empty( $tuckshop_users ) ) {
  240. $tuckshop_users = "empty";
  241. }
  242.  
  243. $_SESSION['tuckshop_users'] = $tuckshop_users;
  244. }
  245.  
  246. $_SESSION['msc_role'] = $userRole;
  247.  
  248. if ( VIS_DEBUG_MODE ) {
  249. echo "$ Setting MSC_ROLE on session: " . $_SESSION['msc_role'] . "...<br>";
  250. }
  251.  
  252. // Save the MSC user ID to the WP user meta
  253. //update_user_meta( $userWpId, 'msc_user_id', $userId );
  254. }
  255.  
  256. header("Refresh:0");
  257. exit();
  258. }
  259.  
  260. ########################################################################
  261. ## vis_user_sync()
  262. ##
  263. ########################################################################
  264. function vis_user_sync( $userId, $userRole ) {
  265. // Requesting email from MSC api
  266. $userObject;
  267. $userWpId;
  268.  
  269. if ( $userRole == "customer" ) {
  270. if ( VIS_DEBUG_MODE ) {
  271. echo "<p> $ Requesting customer (parent) #$userId... </p>";
  272. }
  273.  
  274. $userObject = msc_get_customer( $userId );
  275. } elseif ( $userRole == "tuckshop" ) {
  276. if ( VIS_DEBUG_MODE ) {
  277. echo "<p> $ Requesting user (tuckshop/school) #$userId... </p>";
  278. }
  279.  
  280. $userObject = msc_get_user( $userId );
  281. } else {
  282. if ( VIS_DEBUG_MODE ) {
  283. echo "<p> ERROR: Invalid user role. Unable to login user. </p>";
  284. }
  285.  
  286. return;
  287. }
  288.  
  289. $userFName = $userObject["firstname"];
  290. $userLName = $userObject["lastname"];
  291. $userEmail = $userObject["email"];
  292. $userAccountUrl = $userObject["accountUrl"];
  293. $userEditUrl = $userObject["editUrl"];
  294.  
  295. if ( VIS_DEBUG_MODE ) {
  296. echo "<p> $ User firstname: $userFName <br>";
  297. echo " $ User lastname: $userLName <br>";
  298. echo " $ User Email: $userEmail <br>";
  299. echo " $ User Account URL: $userAccountUrl <br>";
  300. echo " $ User Edit URL: $userEditUrl </p>";
  301. }
  302.  
  303. // Find user by given email
  304. $wpUserObject = get_user_by( "email", $userEmail );
  305.  
  306. // If a user to registered with that email, get the user ID
  307. if ( $wpUserObject ) {
  308. if (VIS_DEBUG_MODE) {
  309. echo "$ Found user with WP id: " . $wpUserObject->get('ID') . "<br>";
  310. }
  311.  
  312. $userWpId = $wpUserObject->get('ID');
  313.  
  314. } else {
  315. if ( VIS_DEBUG_MODE ) {
  316. echo "<p> $ User not found. Creating account from MSC...</p>";
  317. }
  318.  
  319. $userWpId = vis_user_register( $userId, $userEmail, $userFName, $userLName, array( $userRole ) );
  320. }
  321.  
  322. // Did we fail to register the account?
  323. if ( is_wp_error( $userWpId ) ) {
  324. // Tell us why
  325. echo $userWpId->get_error_message();
  326. }
  327.  
  328. vis_user_set_metadata($userWpId, $userId, $userRole, $userAccountUrl);
  329.  
  330. return $userWpId;
  331. }
  332.  
  333. function vis_user_set_metadata( $userWpId, $userId, $userRole, $userAccountUrl ) {
  334. $userMscRoles = get_user_meta( $userWpId, VIS_MSCROLES_METADATA_FIELD, true );
  335.  
  336. if ( false == $userMscRoles ) {
  337. $userMscRoles = array( $userRole );
  338. } else {
  339. if ( ! in_array( $userRole, $userMscRoles ) ) {
  340. array_push( $userMscRoles, $userRole );
  341. }
  342. }
  343.  
  344. # Add msc_roles metadata array field
  345. $ret = add_user_meta( $userWpId, VIS_MSCROLES_METADATA_FIELD, $userMscRoles, true );
  346.  
  347. if ( VIS_DEBUG_MODE ) {
  348. echo "<br>RET: $ret <br>";
  349. echo "$ MSC Roles: ";
  350. var_dump( $userMscRoles );
  351. echo "<br>";
  352. }
  353.  
  354. # Add msc Ids to the metadata
  355. if ( $userRole == "customer" ) {
  356. add_user_meta( $userWpId, "msc_customer_id", $userId, true );
  357. }
  358. if ( $userRole == "tuckshop" ) {
  359. add_user_meta( $userWpId, "msc_tuckshop_id", $userId, true );
  360. }
  361.  
  362. if ( VIS_DEBUG_MODE ) {
  363. $ret = get_user_meta( $userWpId, "msc_customer_id", true );
  364. echo "$ MSC Customer Id: $ret";
  365. echo "<br>";
  366.  
  367. $ret = get_user_meta( $userWpId, "msc_tuckshop_id", true );
  368. echo "$ MSC Tuckshop Id: $ret";
  369. echo "<br>";
  370. }
  371.  
  372. # Add user account url to the metadata
  373. update_user_meta( $userWpId, "msc_account_url", $userAccountUrl );
  374.  
  375. if ( VIS_DEBUG_MODE ) {
  376. $ret = get_user_meta( $userWpId, "msc_account_url", true );
  377. echo "<br>$ Variable MSC Account Url: $userAccountUrl";
  378. echo "<br>$ Saved MSC Account Url: $ret";
  379. echo "<br>";
  380. }
  381. }
  382.  
  383. ########################################################################
  384. ## vis_user_logout()
  385. ## Logs out the current WP user.
  386. ########################################################################
  387. function vis_user_logout()
  388. {
  389. if (VIS_DEBUG_MODE) {
  390. echo "$ Logging out... <br>";
  391. }
  392.  
  393. $ret = setcookie(VIS_SESSIONID_COOKIE_NAME, "", time() + 1, "/", false);
  394.  
  395. if ($ret) {
  396. if (VIS_DEBUG_MODE) {
  397. echo "$ Login cookie deleted successfuly.<br>";
  398. }
  399. }
  400.  
  401. wp_logout();
  402. header("Refresh:0");
  403. if (VIS_DEBUG_MODE) {
  404. echo "$ User logged out successfuly!<br>";
  405. }
  406. }
  407.  
  408. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement