April 2009


There seems to be a problem with my blog. This is the second time my latest entry content gets erased. Only the content is erased leaving the title intact. I am still investigating the issue. Meanwhile, I decided to upgrade to the latest Wordpress release to see if the issue gets resolved

After googling around for a plug-in to help me authenticate PHPBB3 forums against an already existing web application and finding none, I decided to write my own. I modelled it to auth_db and auth_apache and named it auth_dbext as short for authentication using DB from external source (external to PHPBB Database).

I am sharing it with others who might have a similar need and of course any improvements are welcome.

I have not implement some optional parts (see http://wiki.phpbb.com/Authentication_plugins for more info)

The login code is in the function (The full source code is at auth_dbext.phps)

  1. /**
  2. * Login function
  3. */
  4. function login_dbext(&$username, &$password)
  5. {
  6. global $db;</code>
  7.  
  8. // do not allow empty password
  9. if (!$password)
  10. {
  11. return array(
  12. ’status’    =&gt; LOGIN_ERROR_PASSWORD,
  13. ‘error_msg’    =&gt; ‘NO_PASSWORD_SUPPLIED’,
  14. ‘user_row’    =&gt; array(‘user_id’ =&gt; ANONYMOUS),
  15. );
  16. }
  17.  
  18. if (!$username)
  19. {
  20. return array(
  21. ’status’    =&gt; LOGIN_ERROR_USERNAME,
  22. ‘error_msg’    =&gt; ‘LOGIN_ERROR_USERNAME’,
  23. ‘user_row’    =&gt; array(‘user_id’ =&gt; ANONYMOUS),
  24. );
  25. }
  26.  
  27. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  28. // Note: on my systems, I include these following lines from an external file that is not web-accessible
  29. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  30. $db_host      = "localhost"; // Here goes the MySQL server address, hostname or IP
  31. $db_user      = "username";  // Here goes the MySQL user allowed to read the table below (GRANT SELECT ON ….)
  32. $db_password  = "passwd";    // Here should go the password associated with the above user
  33. $db_database  = "dbName";    // Here goes the Database containing the table below
  34. $db_table     = "tblUsers";  // Here will goes the table list users allowed to login into PHPBB
  35. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  36. $col_username = "username";
  37. $col_password = "password";
  38. $hashMethod   = "sha1"; // Can be one of:  md5, sha1, plain
  39. // In case you choose to use a non-standard hashing function, be
  40. // sure to change below where the $hashedPassword variable is created
  41.  
  42. $objMySqli = new mysqli($db_host, $db_user, $db_password, $db_database);
  43.  
  44. /* check connection */
  45. {
  46. return array(
  47. ’status’    =&gt; LOGIN_ERROR_EXTERNAL_AUTH ,
  48. ‘error_msg’    =&gt; ‘LOGIN_ERROR_EXTERNAL_AUTH ‘,
  49. ‘user_row’    =&gt; array(‘user_id’ =&gt; ANONYMOUS),
  50. );
  51. }
  52.  
  53. // Check the User/Password
  54. if($hashMethod == ’sha1′)
  55. {
  56. $hashedPassword = sha1($password);
  57. } elseif($hashMethod == ‘md5′) {
  58. $hashedPassword = md5($password);
  59. } else {
  60. $hashedPassword = $password;
  61. }
  62. $sql =
  63. "SELECT 11 as ID
  64. FROM " . $db_table . "
  65. WHERE
  66. " . $col_username . " = ‘" . mysqli_real_escape_string($username)          . "’ AND
  67. " . $col_password . " = ‘" . mysqli_real_escape_string($hashedPassword) . "’
  68. ";
  69.  
  70. if ( $result = $objMySqli-&gt;query($sql) )
  71. {
  72. if ( $result-&gt;num_rows &lt;= 0 )
  73. {
  74. return array(
  75. ’status’    =&gt; LOGIN_ERROR_USERNAME,
  76. ‘error_msg’    =&gt; ‘LOGIN_ERROR_USERNAME’,
  77. ‘user_row’    =&gt; array(‘user_id’ =&gt; ANONYMOUS),
  78. );
  79. }
  80.  
  81. $sql = ‘SELECT user_id, username, user_password, user_passchg, user_email, user_type
  82. FROM ‘ . USERS_TABLE . "
  83. WHERE username = ‘" . $db-&gt;sql_escape($username) . "’";
  84. $result = $db-&gt;sql_query($sql);
  85. $row = $db-&gt;sql_fetchrow($result);
  86. $db-&gt;sql_freeresult($result);
  87.  
  88. if ($row)
  89. {
  90. // User inactive…
  91. if ($row[‘user_type’] == USER_INACTIVE || $row[‘user_type’] == USER_IGNORE)
  92. {
  93. return array(
  94. ’status’        =&gt; LOGIN_ERROR_ACTIVE,
  95. ‘error_msg’        =&gt; ‘ACTIVE_ERROR’,
  96. ‘user_row’        =&gt; $row,
  97. );
  98. }
  99.  
  100. // Successful login…
  101. return array(
  102. ’status’        =&gt; LOGIN_SUCCESS,
  103. ‘error_msg’        =&gt; false,
  104. ‘user_row’        =&gt; $row,
  105. );
  106. }
  107.  
  108. // this is the user’s first login so create an empty profile
  109. return array(
  110. ’status’        =&gt; LOGIN_SUCCESS_CREATE_PROFILE,
  111. ‘error_msg’        =&gt; false,
  112. ‘user_row’        =&gt; user_row_dbext($username, sha1($password)),
  113. );
  114. } else {
  115. // TODO: Handle this situation
  116. }
  117.  
  118. // Not logged in using the external DB
  119. return array(
  120. ’status’        =&gt; LOGIN_ERROR_EXTERNAL_AUTH,
  121. ‘error_msg’        =&gt; ‘LOGIN_ERROR_EXTERNAL_AUTH’,
  122. ‘user_row’        =&gt; array(‘user_id’ =&gt; ANONYMOUS),
  123. );
  124. }

To use this plugin, copy it to the directory /includes/auth/ (the file should be /includes/auth/auth_dbext.php ) in your PHPBB3 install location.  This file can be downloaded at auth_dbext.php (ZIP) or view a highlighted source file at auth_dbext.phps