A plugin that syncs wordpress and MantisBT accounts via the UsersWP user management plugin
This ensures that your MantisBT installation and WordPress user accounts are in sync. If you add a new user, this user also gets added to MantisBT. If you update the password, this also happens in MantisBT. For those that know what they’re doing, just put this code into the plugins folder. Otherwise, download the below and install as a plugin.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
<?php /** * Plugin Name: UsersWp Mantis Sync * Plugin URI: http://www.mywebsite.com/my-first-plugin * Description: Syncs any user signups or password changes with Mantis Bug Tracker * Version: 1.2 * Author: stigzler * Author URI: http://www.magoarcade.org */ defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); // Settings ========================================================== $servername = "localhost"; //address of your mysql server $dbusername = "myAdminAccount"; //mysql user - must have all permissions $dbpassword = 'djijwerfoijrefn'; //password $mdbname = "MyDatabaseName"; //mantis db name $regpassword = "DummyPassword";// just leave this as is // Log File ========================================================== function write_log ($txt) { $myfile = fopen("WPMantisSync-log.txt", "a") or die("Unable to open file!"); fwrite($myfile, "\n". date('h:i:s ', time()) . $txt); fclose($myfile); } add_action('plugins_loaded','hooks_setup'); function hooks_Setup() { add_action('wp_login','wms_wp_login',10,2); add_action('user_register', 'wms_user_register', 10,1); add_filter('uwp_validate_result', 'wms_uwp_validate_result',10,3); } function wms_uwp_validate_result($result,$action,$data) { write_log('====================================================='); write_log('UWP Form Submitted. Type: ' . $action ); /* Possible actions: register,login,forgot,change,reset,account */ if (is_wp_error($result)) { write_log("Result is an Error: ". $result->get_error_message()); return $result; } if ($action == "register") { write_log('Password captured: ' . $result["password"]); global $regpassword; $regpassword = $result["password"]; } elseif ($action == "forgot") { write_log('Data: ' . var_dump($result)); } elseif ($action == "reset") { write_log('Password reset to: ' . $result["password"]); $user = wms_get_user_by( 'user_login', $result["uwp_reset_username"] ); write_log("Username retrieved: " . $user->user_login); upsert_user($data["uwp_reset_username"],$result["password"] ,$user->user_email,$data["uwp_reset_username"]); } elseif ($action == "change") { } return $result; } function wms_user_register($userID) { write_log('====================================================='); write_log('User Register fired: ' . $userID); $user = wms_get_user_by( 'ID', $userID ); write_log('Username: ' . $user->user_login); global $regpassword; wms_user_signup($userID ,$user->user_login,$regpassword,$user->user_email,$user->user_login); } function wms_get_user_by( $field, $value ) { $userdata = WP_User::get_data_by( $field, $value ); if ( ! $userdata ) { return false; } $user = new WP_User; $user->init( $userdata ); return $user; } // User Signup ====================================================== function wms_user_signup ( $userID, $username, $password, $email, $meta) { # meta[0] = name write_log(''); write_log('==================================================='); write_log('User Signup Method: ' . $userID . "|" . $username . "|" . $password . "|" . $email. "|" . $meta); write_log('MD5: ' . md5 ($password)); upsert_user($username,$password,$email,$meta); return $true; } # ================================================================ # UPSERT User # ================================================================ function upsert_user ($username, $password, $email, $name) { write_log('==================================================='); write_log('Upsert User Routine started'); global $servername ,$dbusername ,$dbpassword ,$mdbname ; write_log('Database:'); write_log('serv: ' . $servername); write_log('name: ' . $mdbname); write_log('un: ' . $dbusername); write_log('pw: ' . $dbpassword); write_log('Passed vars:'); write_log('username:'.$username); write_log('password:'.$password); write_log('email:'.$email); write_log('name:'.$name); write_log('Upsert Routine. Checking database connection'); if ($name == ''){ write_log('Using username of real name'); $name = $username; } // Create connection $conn = mysqli_connect($servername, $dbusername, $dbpassword, $mdbname); // Check connection if (!$conn) { write_log("ERROR: Connection failed: " . mysqli_connect_error()); die("<p>Connection failed: </p>" . mysqli_connect_error()); } write_log('Connected successfully'); $query = "SELECT * FROM mantis_user_table WHERE username = '" . $username ."'"; if ($result=mysqli_query($conn,$query)) { if (mysqli_num_rows($result) > 0) { write_log('User already exists in database. Updating.'); $sql = "UPDATE mantis_user_table SET password = '" . md5 ($password). "' , email = '" . $email . "' WHERE username = '" . $username . "'"; write_log("SQL: " . $sql); if ($conn->query($sql) === TRUE) { write_log("New record created successfully"); } else { write_log("ERROR: Couldn't create record: ". $sql . " || " . $conn->error); } } else { write_log('User doesnt exist in db. Inserting.'); // calculate unique cookie $seed = $email . $username; $cookie_string = auth_generate_unique_cookie_string( $seed ); $sql = "INSERT INTO mantis_user_table (username, email, password, realname, enabled, protected, access_level, login_count, cookie_string) VALUES ('".$username."', '".$email."', '".md5 ($password)."', '".$name."', 1, 0, 25, 0,'".$cookie_string."')"; write_log("SQL: " . $sql); if ($conn->query($sql) === TRUE) { write_log("New record created successfully"); } else { write_log("ERROR: Couldn't create record: ". $sql . " || " . $conn->error); } } } mysqli_close($conn); } /* Creating the Cookie_String */ # -------------------- # Generate a UNIQUE string to use as the identifier for the login cookie # The string returned should be 64 characters in length function auth_generate_unique_cookie_string() { do { $cookie_string = auth_generate_cookie_string(); } while ( !auth_is_cookie_string_unique( $cookie_string ) ); return $cookie_string; } # -------------------- # Generate a string to use as the identifier for the login cookie # It is not guaranteed to be unique and should be checked # The string returned should be 64 characters in length function auth_generate_cookie_string() { $t_val = mt_rand( 0, mt_getrandmax() ) + mt_rand( 0, mt_getrandmax() ); $t_val = md5( $t_val ) . md5( time() ); return substr( $t_val, 0, 64 ); } # -------------------- # Return true if the cookie login identifier is unique, false otherwise function auth_is_cookie_string_unique( $cookie_string ) { write_log('Testing if random cookie unique...'); global $servername ,$dbusername ,$dbpassword ,$mdbname ; $conn = mysqli_connect($servername, $dbusername, $dbpassword, $mdbname); $sql = "SELECT EXISTS(SELECT 1 FROM mantis_user_table WHERE cookie_string = '" . $cookie_string ."' LIMIT 1)"; if ($conn->query($sql) === TRUE) { write_log('Entry already exists for this cookie.'); mysqli_close($conn); return false; } else { write_log('Entry DOES NOT exist for this cookie.'); mysqli_close($conn); return true; } } ?> |
Leave a Reply