317653ccf1c494b245d5007ea62fe90a2fb6c31e
[atutor.git] / mods / mediawiki / MySQL_Auth / Auth_viaMySQL / Auth_viaMySQL.php
1 <?php
2 /**
3  * Auth_viaMySQL MediaWiki extension MediaWiki version 1.14.0rc1
4  *
5  * @file
6  * @ingroup Extensions
7  * @version 1.0
8  * @author John Derby Russell
9  * @link http://www.mediawiki.org/w/index.php/Extension:Auth_viaMySQL
10  */
11  
12 # Not a valid entry point, skip unless MEDIAWIKI is defined
13 if( !defined( 'MEDIAWIKI' ) )
14 {
15         echo "Auth_viaMySQL extension";
16         die();
17 }
18  
19 // Extension credits that will show up on Special:Version
20 $wgExtensionCredits['other'][] = array(
21       'name' => 'MySQL Auto Authentication -> Auth_viaMySQL',
22       'version' => '1.0',
23       'author' => 'John Derby Russell',
24       'url' => 'http://www.mediawiki.org/w/index.php/Extension:Auth_viaMySQL',
25       'description' => 'Auto-authenticates users using MySQL database',
26 );
27  
28 /**
29  *
30  * MySQL Login Database Integration
31  *
32  */
33  
34 require_once(MW_INSTALL_PATH.'/MySQLActiveUser.php') ;
35  
36 $wgHooks['UserLoadFromSession'][] = 'Auth_viaMySQL';
37  
38 // Kill logout url
39 $wgHooks['PersonalUrls'][] = 'PersonalUrls_killLogout'; /* Disallow logout link */
40  
41 function Auth_viaMySQL( $user, $result ) {
42     global $MySQLActiveUserData;
43     $MySQLActiveUserData->distribute_cookie_data() ;
44  
45     wfSetupSession();
46  
47     /**
48      * A lot of this is from User::newFromName
49      */
50     // Force usernames to capital
51     global $wgContLang;
52  
53     $name = $wgContLang->ucfirst( $MySQLActiveUserData->active_user_name );
54  
55     // Clean up name according to title rules
56     $t = Title::newFromText( $name );
57     if( is_null( $t ) ) {
58         return(true) ;
59     }
60  
61     $canonicalName = $t->getText();
62  
63     if( !User::isValidUserName( $canonicalName ) ) {
64         return(true) ;
65     }
66  
67     $user->setName( $canonicalName );
68  
69     $user_id_fromMW_DB = $user->idFromName( $MySQLActiveUserData->active_user_name ) ;
70  
71     $user->setId( $user_id_fromMW_DB );
72     if ( $user->getID() == 0 ) {
73         /**
74         * A lot of this is from LoginForm::initUser
75         * LoginForm in in the file called SpecialUserLogin.php line 342 (version 1.14.0rc1)
76         */
77         $canonicalName = $t->getText();
78         $user->setName( $canonicalName );
79         $user->addToDatabase();
80  
81         $user->setEmail( $MySQLActiveUserData->active_user_email );
82         $user->setRealName( '' );
83         $user->setToken();
84  
85         $user->saveSettings();
86     } else {
87         if ( !$user->loadFromDatabase() ) {
88             // Can't load from ID, user is anonymous
89             return(true) ;
90         }
91         $user->saveToCache();
92     }
93  
94     $result = 1; // This causes the rest of the authentication process to be skipped.
95     return(false);   // As should this, according to the internal error report:
96 }
97  
98 // Kill logout url
99 function PersonalUrls_killLogout($personal_urls, $title) {
100     $personal_urls['logout'] = null ;
101     $personal_urls['login'] = null ;
102     $personal_urls['anonlogin'] = null ;
103     return true ;
104 }
105 ?>