Advertisement
Guest User

Untitled

a guest
Feb 16th, 2020
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.59 KB | None | 0 0
  1. <?php
  2. //------------------------------------------------------------------------------
  3. if (!isset($BSAUTH_SERVICES)) $BSAUTH_SERVICES = array();
  4. //------------------------------------------------------------------------------
  5. if (!function_exists("bsauth_register_options")) {
  6. function bsauth_register_options(){
  7. register_setting( 'bs_auth_pages', 'login_page' );
  8. register_setting( 'bs_auth_pages', 'register_page' );
  9. register_setting( 'bs_auth_pages', 'link_page' );
  10. register_setting( 'bs_auth_pages', 'logout_frontpage' );
  11. register_setting( 'bs_auth_pages', 'bsauth_custom_button' );
  12.  
  13. register_setting( 'bs_auth_pages', 'bs_auth_hide_local' );
  14.  
  15. register_setting( 'bs_auth_pages', 'bs_auth_signup_user_url' );
  16. register_setting( 'bs_auth_pages', 'bs_auth_signup_user_email' );
  17. register_setting( 'bs_auth_pages', 'bs_auth_signup_display_name' );
  18. register_setting( 'bs_auth_pages', 'bs_auth_signup_nickname' );
  19. register_setting( 'bs_auth_pages', 'bs_auth_signup_first_name' );
  20. register_setting( 'bs_auth_pages', 'bs_auth_signup_last_name' );
  21. register_setting( 'bs_auth_pages', 'bs_auth_signup_description' );
  22. register_setting( 'bs_auth_pages', 'bs_auth_signup_jabber' );
  23. register_setting( 'bs_auth_pages', 'bs_auth_signup_aim' );
  24. register_setting( 'bs_auth_pages', 'bs_auth_signup_yim' );
  25.  
  26.  
  27. }
  28. }
  29. //------------------------------------------------------------------------------
  30. if (!function_exists("bsauth_buttons_sort")) {
  31. function bsauth_buttons_sort($a, $b) {
  32. if ($a["order"] == $b["order"]) return 0;
  33. return ($a["order"] < $b["order"]) ? -1 : 1;
  34. }
  35. }
  36. //------------------------------------------------------------------------------
  37. if (!function_exists("bsauth_login_display")) {
  38. function bsauth_login_display(){
  39. global $BSAUTH_SERVICES;
  40.  
  41.  
  42.  
  43. //if (isset($_SESSION['bsauth_link_id']) && is_user_logged_in()) {
  44. if (isset($_SESSION['bsauth_link']) && is_user_logged_in()) {
  45. header("Location: ".site_url("/".get_option("link_page")). '?' . $_SERVER['QUERY_STRING']);
  46. //header("Location: ".site_url("/".get_option("link_page")));//. '?' . $_SERVER['QUERY_STRING']);
  47. }
  48.  
  49. if ( !is_user_logged_in() ) {
  50. if ( isset($_POST['bsauth_login'])){
  51. $login = explode ("-", $_POST['bsauth_login']);
  52. $service = $login[0];
  53. $login_id = $login[1];
  54. $_SESSION['bsauth_plugin'] = $service;
  55. $_SESSION['bsauth_login_id'] = $login_id;
  56. } else {
  57. $service = $_SESSION['bsauth_plugin'];
  58. $login_id = $_SESSION['bsauth_login_id'];
  59. }
  60.  
  61. if (isset($service) && isset($login_id)) {
  62. $service = $BSAUTH_SERVICES[$service];
  63. if ($service!=null) {
  64. $service->Login($login_id);
  65. } else {
  66. _e("Invalid service type","blaat_auth");
  67. }
  68. }
  69. }
  70.  
  71. if ( is_user_logged_in() ) {
  72. if (isset($_SESSION['bsauth_registered'])) {
  73. _e("Registered","blaat_auth");
  74. unset ($_SESSION['bsauth_registered']);
  75. unset( $_SESSION['bsauth_fetch_data']);
  76. unset( $_SESSION['bsauth_register_auto']);
  77. unset( $_SESSION['bsauth_plugin']);
  78. unset( $_SESSION['bsauth_login_id']);
  79. } else {
  80. _e("Logged in","blaat_auth");
  81. }
  82. } else {
  83.  
  84. if (!(get_option("bs_auth_hide_local"))) {
  85. echo "<div id='bsauth_local'>";
  86. echo "<p>" . __("Log in with a local account","blaat_auth") . "</p>" ;
  87. wp_login_form();
  88. echo "</div>";
  89. }
  90.  
  91. echo "<div id='bsauth_buttons'>";
  92. echo "<p>" . __("Log in with","blaat_auth") . "</p>";
  93.  
  94. $ACTION=site_url("/".get_option("login_page"));
  95. echo "<form method='post'>";
  96.  
  97. $buttons = array();
  98. foreach ($BSAUTH_SERVICES as $service) {
  99. $buttons_new = array_merge ( $buttons ,
  100. $service->getButtons());
  101. $buttons=$buttons_new;
  102. }
  103.  
  104. usort($buttons, "bsauth_buttons_sort");
  105.  
  106. foreach ($buttons as $button) {
  107. echo bsauth_generate_button($button,"login");
  108. //echo $button['button'];
  109. //if (isset($button['css'])) echo $button['css'];
  110. }
  111.  
  112. echo "</form>";
  113. echo "</div>";
  114.  
  115. echo "<style>" . htmlspecialchars(get_option("bsauth_custom_button")) . "</style>";
  116. }
  117. }
  118. }
  119. //------------------------------------------------------------------------------
  120. if (!function_exists("bsauth_register_display")) {
  121. function bsauth_register_display() {
  122.  
  123. global $BSAUTH_SERVICES;
  124.  
  125. if (isset($_POST['cancel'])) {
  126. unset($_SESSION['bsauth_register']);
  127. }
  128.  
  129.  
  130. if (is_user_logged_in()) {
  131. _e("You cannot register a new account since you are already logged in.","blaat_auth");
  132. } else {
  133. session_start();
  134. if (isset($_SESSION['bsauth_register'])) {
  135.  
  136.  
  137. $register = explode ("-", $_SESSION['bsauth_register']);
  138.  
  139. $service = $_SESSION['bsauth_display'];
  140. printf( __("You are authenticated to %s","blaat_auth") , $service );
  141. echo "<br>";
  142.  
  143.  
  144. if ($_SESSION['bsauth_fetch_data']) {
  145. $service = $BSAUTH_SERVICES[$register[0]];
  146. if($service) {
  147. $new_user = $service->getRegisterData();
  148. }
  149. }
  150.  
  151. if (isset($_POST['username']) && isset($_POST['email'])) {
  152. if (!isset($new_user)) $new_user = array();
  153. $new_user['user_login']= $_POST['username'];
  154. $new_user['user_email']= $_POST['email'];
  155. }
  156.  
  157. //if (isset($_POST['username']) && isset($_POST['email'])) {
  158. if (isset($new_user) && (isset($new_user['user_login']) &&
  159. ( isset($new_user['user_email']) || (get_option("bs_auth_signup_user_email")!="Required") )
  160. )
  161. && ( $_POST['register'] || $_SESSION['bsauth_register_auto'] )) {
  162. $new_user['user_pass'] = wp_hash_password(wp_generate_password());
  163. $user_id = wp_insert_user($new_user);
  164. if (is_numeric($user_id)) {
  165. $reg_ok=true;
  166. $_SESSION['bsauth_registered']=1;
  167. wp_set_current_user ($user_id);
  168. wp_set_auth_cookie($user_id);
  169. global $BSAUTH_SERVICES;
  170. $serviceToLink = $BSAUTH_SERVICES[$register[0]];
  171. if ($serviceToLink) {
  172. $serviceToLink->Link($register[1]);
  173. header("Location: ".site_url("/".get_option("login_page")));
  174. } else {
  175. echo "DEBUG:::: Unable to link your account"; // TODO message
  176. }
  177. unset($_SESSION['bsauth_register']);
  178. } else {
  179. $reg_ok=false;
  180. $error = __($user_id->get_error_message());
  181. }
  182. } else {
  183. $reg_ok=false;
  184. // no username/password given
  185. }
  186. if ($reg_ok){
  187.  
  188. } else {
  189. if (isset($error)) {
  190. echo "<div class='error'>$error</div>";
  191. }
  192. _e("Please provide a username and e-mail address to complete your signup","blaat_auth");
  193. ?><form method='post'>
  194. <table>
  195. <tr><td><?php _e("Username"); ?></td><td><input name='username' value='<?php echo htmlspecialchars($new_user['user_login']);?>'</td></tr>
  196. <?php if (get_option("bs_auth_signup_user_email")!="Disabled") { ?>
  197. <tr><td><?php _e("E-mail Address"); ?></td><td><input name='email' value='<?php echo htmlspecialchars($new_user['user_email']);?>' ></td></tr>
  198. <?php } ?>
  199. <tr><td><button name='cancel' type=submit><?php _e("Cancel"); ?></button></td><td><button name='register' value='1' type=submit><?php _e("Register"); ?></button></td></tr>
  200. </table>
  201. </form>
  202. <?php
  203. printf( __("If you already have an account, please click <a href='%s'>here</a> to link it.","blaat_auth") , site_url("/".get_option("link_page")));
  204. }
  205. } else {
  206. if(isset($_POST['username']) && isset($_POST['email']) && isset($_POST['password'])){
  207. $user_id = wp_create_user( $_POST['username'], $_POST['password'] , $_POST['email'] ) ;
  208. if (is_numeric($user_id)) {
  209. $reg_ok=true;
  210. $_SESSION['bsauth_registered']=1;
  211. wp_set_current_user ($user_id);
  212. wp_set_auth_cookie($user_id);
  213. header("Location: ".site_url("/".get_option("login_page")));
  214. } else {
  215. $reg_ok=false;
  216. $error = __($user_id->get_error_message());
  217. }
  218. } else {
  219. $error= __("Some data is missing. You need to fill out all fields.","blaat_auth");
  220. }
  221. if($reg_ok){
  222. } else {
  223. if (!(get_option("bs_auth_hide_local"))) {
  224. echo "<div id='bsauth_local'>";
  225. echo "<p>" . __("Enter a username, password and e-mail address to sign up","blaat_auth") . "</p>" ;
  226. ?>
  227. <form method=post>
  228. <table>
  229. <tr><td><?php _e("Username"); ?></td><td><input name='username'></td></tr>
  230. <tr><td><?php _e("Password"); ?></td><td><input type='password' name='password'></td></tr>
  231. <tr><td><?php _e("E-mail Address"); ?></td><td><input name='email'></td></tr>
  232. <tr><td></td><td><button type=submit><?php _e("Register"); ?></button></td></tr>
  233. </table>
  234. </form>
  235. <?php
  236. echo "</div>";
  237. }
  238.  
  239. echo "<div id='bsauth_buttons'>";
  240. echo "<p>" . __("Sign up with","blaat_auth") . "</p>";
  241. $action=htmlspecialchars(site_url("/".get_option("login_page")));
  242. echo "<form action='$action' method='post'>";
  243. global $BSAUTH_SERVICES;
  244.  
  245. $buttons = array();
  246. foreach ($BSAUTH_SERVICES as $service) {
  247. $buttons_new = array_merge ( $buttons , $service->getButtons() );
  248. $buttons=$buttons_new;
  249. }
  250.  
  251. usort($buttons, "bsauth_buttons_sort");
  252.  
  253. foreach ($buttons as $button) {
  254. //echo bsauth_generate_button($button,"register"); // TODO
  255. echo bsauth_generate_button($button,"login");
  256. //echo $button['button'];
  257. //if (isset($button['css'])) echo $button['css'];
  258. }
  259.  
  260. echo "</form>";
  261. echo "</div>";
  262. echo "<style>" . htmlspecialchars(get_option("bsauth_custom_button")) . "</style>";
  263. }
  264. }
  265. }
  266. }
  267. }
  268. //------------------------------------------------------------------------------
  269. if (!function_exists("bsauth_generate_button")) {
  270. function bsauth_generate_button($button, $action){
  271.  
  272. if (isset($button['logo']))
  273. $style="style='background-image:url(\"" .$button['logo']. "\");'";
  274.  
  275. return "<button class='bs-auth-btn' name=bsauth_$action
  276. type=submit value='".$button['plugin']."-".$button['id']."'>
  277. <span $style class='bs-auth-btn-logo
  278. bs-auth-btn-logo-".$button['plugin']."-".$button['service']."'>
  279. </span><span class='bs-auth-btn-text'>".
  280. $button['display_name']."</span></button>";
  281. }
  282. }
  283.  
  284.  
  285. //------------------------------------------------------------------------------
  286. if (!function_exists("bsauth_link_display")) {
  287.  
  288. function bsauth_link_display(){
  289. session_start();
  290.  
  291.  
  292.  
  293. global $BSAUTH_SERVICES;
  294. global $wpdb;
  295. $user = wp_get_current_user();
  296. echo "<style>" . htmlspecialchars(get_option("bsauth_custom_button")) . "</style>";
  297. if (is_user_logged_in()) {
  298.  
  299.  
  300.  
  301. if (isset($_POST['bsauth_link'])) {
  302. $link = explode ("-", $_POST['bsauth_link']);
  303. $_SESSION['bsauth_link']=$_POST['bsauth_link'];
  304. }
  305. if (isset($_POST['bsauth_unlink']))
  306. $link = explode ("-", $_POST['bsauth_unlink']);
  307. if (isset($link)){
  308. $service = $link[0];
  309. $link_id = $link[1];
  310. $_SESSION['bsauth_plugin'] = $service;
  311. $_SESSION['bsauth_link_id'] = $link_id;
  312. }
  313.  
  314.  
  315.  
  316. if (isset($_SESSION['bsauth_plugin'])) $service = $_SESSION['bsauth_plugin'];
  317. if (isset($_SESSION['bsauth_link_id'])) $link_id = $_SESSION['bsauth_link_id'];
  318.  
  319.  
  320. if (isset($service) && isset($link_id)) {
  321. $service = $BSAUTH_SERVICES[$service];
  322. if ($service!=null) {
  323. // is SESSION required here?
  324. if (isset($_SESSION['bsauth_link'])) {
  325. //echo "link request<br>";
  326. $service->Link($link_id);
  327. // not yet...
  328. //unset($_SESSION['bsauth_link']);
  329. } else
  330. if (isset($_POST['bsauth_unlink'])) {
  331. //echo "link request<br>";
  332. $service->Unlink($link_id);
  333. unset($_POST['bsauth_unlink']);
  334. } //else echo "request not specified!";
  335. } else {
  336. // TODO error handling
  337. echo "service not registered!";
  338. }
  339. } // else echo "no service/link id<br>";
  340.  
  341.  
  342.  
  343. $buttonsLinked = array();
  344. $buttonsUnlinked = array();
  345.  
  346.  
  347.  
  348. foreach ($BSAUTH_SERVICES as $service) {
  349. $buttons = $service->getButtonsLinked($user->ID);
  350.  
  351. $buttonsLinked_new = array_merge ( $buttonsLinked , $buttons['linked'] );
  352. $buttonsUnlinked_new = array_merge ( $buttonsUnlinked , $buttons['unlinked'] );
  353. $buttonsLinked=$buttonsLinked_new;
  354. $buttonsUnlinked=$buttonsUnlinked_new;
  355. }
  356.  
  357. usort($buttonsLinked, "bsauth_buttons_sort");
  358. usort($buttonsUnlinked, "bsauth_buttons_sort");
  359.  
  360. foreach ($buttonsLinked as $linked) {
  361. $unlinkHTML .= bsauth_generate_button($linked,"unlink");
  362. }
  363.  
  364. foreach ($buttonsUnlinked as $unlinked) {
  365. $linkHTML .= bsauth_generate_button($unlinked,"link");
  366. }
  367.  
  368. unset($_SESSION['bsoauth_id']);
  369. unset($_SESSION['bsauth_link']);
  370.  
  371.  
  372. echo "<form method='post' action='". site_url("/".get_option("link_page")) ."'><div class='link authservices'><div class='blocktitle'>".
  373. __("Link your account to","blaat_auth") . "</div>".
  374. $linkHTML . "
  375. </div></form><form method=post>
  376. <div class='unlink authservices'><div class='blocktitle'>".
  377. __("Unlink your account from","blaat_auth") . "</div>".
  378. $unlinkHTML . "
  379. </div></form>";
  380.  
  381. } else {
  382. if (!(get_option("bs_auth_hide_local"))) {
  383. // oauth user, no wp-user
  384. if (isset($_SESSION['bsauth_register']) ) {
  385. echo "<div id='bsauth_local'>";
  386. printf( "<p>" . __("Please provide a local account to link to %s","blaat_auth") . "</p>" , $service);
  387. wp_login_form();
  388. echo "</div>";
  389. } else {
  390. printf( "<p>" . __("You need to be logged in to use this feature","blaat_auth") . "</p>");
  391. }
  392. } else {
  393. printf( "<p>" . __("This feature has been disabled","blaat_auth") . "</p>");
  394. }
  395. }
  396. }
  397. }
  398. //------------------------------------------------------------------------------
  399. if (!function_exists("bsauth_display")) {
  400. function bsauth_display($content) {
  401. $login_page = get_option('login_page');
  402. $link_page = get_option('link_page');
  403. $register_page = get_option('register_page');
  404.  
  405. switch ($GLOBALS['post']->post_name) {
  406. case $login_page :
  407. bsauth_login_display();
  408. break;
  409. case $link_page :
  410. bsauth_link_display();
  411. break;
  412. case $register_page :
  413. bsauth_register_display();
  414. break;
  415. default :
  416. return $content;
  417. }
  418. }
  419. }
  420. //------------------------------------------------------------------------------
  421. // When a WordPress user is deleted, remove any external linked accounts
  422. if (!function_exists("bsauth_delete_user")) {
  423. function bsauth_delete_user($user_id) {
  424. global $BSAUTH_SERVICES;
  425. // For each service, delete the linked service
  426. foreach ($BSAUTH_SERVICES as $service) {
  427. $service->Delete($user_id);
  428. }
  429. }
  430. // Call the delete user function when a WordPress user is deleted.
  431. add_action( 'deleted_user', 'bsauth_delete_user' );
  432. }
  433.  
  434. //------------------------------------------------------------------------------
  435. if (!function_exists("bsauth_generate_select_signup_requirement")) {
  436. function bsauth_generate_select_signup_requirement($option_field){
  437. $option_value = get_option($option_field);
  438. echo "<select name='" . htmlspecialchars($option_field) . "'>";
  439.  
  440. $selected = ($option_value=="Disabled") ? "selected='selected'" : "";
  441. echo "<option value='Disabled' $selected>";
  442. _e("Disabled" , "blaat_auth");
  443. echo "</option>";
  444.  
  445. $selected = ($option_value=="Optional") ? "selected='selected'" : "";
  446. echo "<option value='Optional' $selected>";
  447. _e("Optional" , "blaat_auth");
  448. echo "</option>";
  449.  
  450. $selected = ($option_value=="Required") ? "selected='selected'" : "";
  451. echo "<option value='Required' $selected>";
  452. _e("Required" , "blaat_auth");
  453. echo "</option>";
  454.  
  455. echo "</select>";
  456. }
  457. }
  458.  
  459. //------------------------------------------------------------------------------
  460. if (!function_exists("blaat_plugins_auth_page")) {
  461. function blaat_plugins_auth_page(){
  462. echo '<div class="wrap">';
  463. echo '<h2>';
  464. _e("BlaatSchaap WordPress Authentication Plugins","blaat_auth");
  465. echo '</h2>';
  466. echo '<form method="post" action="options.php">';
  467. settings_fields( 'bs_auth_pages' );
  468.  
  469. echo '<table class="form-table">';
  470.  
  471. echo '<tr><th>'. __("Login page","blaat_auth") .'</th><td>';
  472. echo blaat_page_select("login_page");
  473. echo '</td></tr>';
  474.  
  475. echo '<tr><th>'. __("Register page","blaat_auth") .'</th><td>';
  476. echo blaat_page_select("register_page");
  477. echo '</td></tr>';
  478.  
  479. echo '<tr><th>'. __("Link page","blaat_auth") .'</th><td>';
  480. echo blaat_page_select("link_page");
  481. echo '</td></tr>';
  482.  
  483. echo '<tr><th>';
  484. _e("Redirect to frontpage after logout", "blaat_auth") ;
  485. echo "</th><td>";
  486. $checked = get_option('logout_frontpage') ? "checked" : "";
  487. echo "<input type=checkbox name='logout_frontpage' value='1' $checked>";
  488. echo "</td></tr>";
  489.  
  490.  
  491. echo '<tr><th>';
  492. _e("Hide local accounts", "blaat_auth") ;
  493. echo "</th><td>";
  494. $checked = get_option('bs_auth_hide_local') ? "checked" : "";
  495. echo "<input type=checkbox name='bs_auth_hide_local' value='1' $checked>";
  496. echo "</td></tr>";
  497.  
  498. echo '<tr><th>';
  499. _e("Require e-mail address", "blaat_auth") ;
  500. echo "</th><td>";
  501. bsauth_generate_select_signup_requirement("bs_auth_signup_user_email");
  502. echo "</td></tr>";
  503.  
  504.  
  505.  
  506.  
  507.  
  508. echo '<tr><th>'. __("Custom Button CSS","blaat_auth") .'</th><td>';
  509. echo "<textarea cols=70 rows=15 id='bsauth_custom_button_textarea' name='bsauth_custom_button'>";
  510. echo htmlspecialchars(get_option("bsauth_custom_button"));
  511. echo "</textarea>";
  512. echo '</td></tr>';
  513.  
  514. echo '</table><input name="Submit" type="submit" value="';
  515. echo esc_attr_e('Save Changes') ;
  516. echo '" ></form></div>';
  517.  
  518. }
  519. }
  520. //------------------------------------------------------------------------------
  521. //------------------------------------------------------------------------------
  522. // go frontpage
  523. // -- general auth related support
  524.  
  525. if (get_option("logout_frontpage")) {
  526. add_action('wp_logout','go_frontpage');
  527. }
  528.  
  529. if (!function_exists("go_frontpage")) {
  530. function go_frontpage(){
  531. wp_redirect( home_url() );
  532. exit();
  533. }
  534. }
  535. //------------------------------------------------------------------------------
  536.  
  537.  
  538. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement