This file is indexed.

/usr/share/mediawiki-extensions/openid/SpecialOpenID.body.php is in mediawiki-extensions-openid 3.7.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<?php
/**
 * SpecialOpenID.body.php -- Superclass for all
 * Copyright 2006,2007 Internet Brands (http://www.internetbrands.com/)
 * Copyright 2008 by Evan Prodromou (http://evan.prodromou.name/)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * @file
 * @author Evan Prodromou <evan@prodromou.name>
 * @ingroup Extensions
 */

# FIXME: for login(); figure out better way to share this code
# between Login and Convert

require_once( "Auth/OpenID/Consumer.php" );
require_once( "Auth/OpenID/SReg.php" );
require_once( "Auth/OpenID/AX.php" );
require_once( "Auth/OpenID/FileStore.php" );

class SpecialOpenID extends SpecialPage {

	function getOpenIDStore( $storeType, $prefix, $options ) {
		global $wgOut, $wgMemc, $wgDBtype;

		switch ( $storeType ) {

		case 'file':
			# Auto-create path if it doesn't exist
			if ( !is_dir( $options['path'] ) ) {
				if ( !mkdir( $options['path'], 0770, true ) ) {
					$wgOut->showErrorPage( 'openidconfigerror', 'openidconfigerrortext' );
					return null;
				}
			}
			return new Auth_OpenID_FileStore( $options['path'] );

		case 'db':
			if ( $wgDBtype == 'sqlite' ) {
				$db = new MediaWikiOpenIDDatabaseConnection( wfGetDB( DB_MASTER ) );
				require_once( 'Auth/OpenID/SQLiteStore.php' );
				return new Auth_OpenID_SQLiteStore( $db );
			} else {
				$lb = wfGetLBFactory()->newMainLB();
				$db = new MediaWikiOpenIDDatabaseConnection( $lb->getConnection( DB_MASTER ) );
				switch( $wgDBtype ) {
				case 'mysql':
					require_once( 'Auth/OpenID/MySQLStore.php' );
					return new Auth_OpenID_MySQLStore( $db );
				case 'postgres':
					require_once( 'Auth/OpenID/PostgreSQLStore.php' );
					return new Auth_OpenID_PostgreSQLStore( $db );
				default:
					$wgOut->showErrorPage( 'openidconfigerror', 'openidconfigerrortext' );
					return null;
				}
			}

		case 'memcached':
			return new MediaWikiOpenIDMemcachedStore( $wgMemc );

		 default:
			$wgOut->showErrorPage( 'openidconfigerror', 'openidconfigerrortext' );
		}
	}

	function xriBase( $xri ) {
		if ( substr( $xri, 0, 6 ) == 'xri://' ) {
			return substr( $xri, 6 );
		} else {
			return $xri;
		}
	}

	function xriToUrl( $xri ) {
		return 'http://xri.net/' . SpecialOpenID::xriBase( $xri );
	}

	static function OpenIDToUrl( $openid ) {
		/* ID is either an URL already or an i-name */
		if ( Auth_Yadis_identifierScheme( $openid ) == 'XRI' ) {
			return SpecialOpenID::xriToUrl( $openid );
		} else {
			return $openid;
		}
	}

	function interwikiExpand( $openid_url ) {
		# try to make it into a title object
		$nt = Title::newFromText( $openid_url );
		# If it's got an iw, return that
		if ( !is_null( $nt ) && !is_null( $nt->getInterwiki() )
			&& strlen( $nt->getInterwiki() ) > 0 ) {
			return $nt->getFullUrl();
		} else {
			return $openid_url;
		}
	}

	# Login, Finish

	function getConsumer() {
		global $wgOpenIDConsumerStoreType, $wgOpenIDConsumerStorePath;

		$store = $this->getOpenIDStore(
			$wgOpenIDConsumerStoreType,
			'consumer',
			array( 'path' => $wgOpenIDConsumerStorePath )
		);

		return new Auth_OpenID_Consumer( $store );
	}

	function canLogin( $openid_url ) {
		global $wgOpenIDConsumerDenyByDefault, $wgOpenIDConsumerAllow, $wgOpenIDConsumerDeny;

		if ( $this->isLocalUrl( $openid_url ) ) {
			return false;
		}

		if ( $wgOpenIDConsumerDenyByDefault ) {
			$canLogin = false;
			foreach ( $wgOpenIDConsumerAllow as $allow ) {
				if ( preg_match( $allow, $openid_url ) ) {
					wfDebug( "OpenID: $openid_url matched allow pattern $allow.\n" );
					$canLogin = true;
					foreach ( $wgOpenIDConsumerDeny as $deny ) {
						if ( preg_match( $deny, $openid_url ) ) {
							wfDebug( "OpenID: $openid_url matched deny pattern $deny.\n" );
							$canLogin = false;
							break;
						}
					}
					break;
				}
			}
		} else {
			$canLogin = true;
			foreach ( $wgOpenIDConsumerDeny as $deny ) {
				if ( preg_match( $deny, $openid_url ) ) {
					wfDebug( "OpenID: $openid_url matched deny pattern $deny.\n" );
					$canLogin = false;
					foreach ( $wgOpenIDConsumerAllow as $allow ) {
						if ( preg_match( $allow, $openid_url ) ) {
							wfDebug( "OpenID: $openid_url matched allow pattern $allow.\n" );
							$canLogin = true;
							break;
						}
					}
					break;
				}
			}
		}
		return $canLogin;
	}

	function isLocalUrl( $url ) {
		global $wgServer, $wgArticlePath;

		$pattern = $wgServer . $wgArticlePath;
		$pattern = str_replace( '$1', '(.*)', $pattern );
		$pattern = str_replace( '?', '\?', $pattern );

		return preg_match( '|^' . $pattern . '$|', $url );
	}

	function login( $openid_url, $finish_page ) {
		global $wgTrustRoot, $wgOut;

		# If it's an interwiki link, expand it

		$openid_url = $this->interwikiExpand( $openid_url );

		# Check if the URL is allowed

		if ( !$this->canLogin( $openid_url ) ) {
			$wgOut->showErrorPage( 'openidpermission', 'openidpermissiontext' );
			return;
		}

		if ( !is_null( $wgTrustRoot ) ) {
			$trust_root = $wgTrustRoot;
		} else {
			global $wgScriptPath, $wgServer;
			$trust_root = $wgServer . $wgScriptPath;
		}

		wfSuppressWarnings();

		$consumer = $this->getConsumer();

		if ( !$consumer ) {
			wfDebug( "OpenID: no consumer\n" );
			$wgOut->showErrorPage( 'openiderror', 'openiderrortext' );
			return;
		}

		# Make sure the user has a session!
		$this->setupSession();

		$auth_request = $consumer->begin( $openid_url );

		// Handle failure status return values.
		if ( !$auth_request ) {
			wfDebug( "OpenID: no auth_request\n" );
			$wgOut->showErrorPage( 'openiderror', 'openiderrortext' );
			return;
		}

		# Check the processed URLs, too

		$endpoint = $auth_request->endpoint;

		if ( !is_null( $endpoint ) ) {
			# Check if the URL is allowed

			if ( isset( $endpoint->identity_url ) && !$this->canLogin( $endpoint->identity_url ) ) {
				$wgOut->showErrorPage( 'openidpermission', 'openidpermissiontext' );
				return;
			}

			if ( isset( $endpoint->delegate ) && !$this->canLogin( $endpoint->delegate ) ) {
				$wgOut->showErrorPage( 'openidpermission', 'openidpermissiontext' );
				return;
			}
		}

		$sreg_request = Auth_OpenID_SRegRequest::build(
			// Required
			array(),
			// Optional
			array( 'nickname', 'email', 'fullname', 'language', 'timezone' )
		);

		if ( $sreg_request ) {
			$auth_request->addExtension( $sreg_request );
		}

		// Create attribute request object. Depending on your endpoint, you can request many things:
		// see http://code.google.com/apis/accounts/docs/OpenID.html#Parameters for parameters.
		// Usage: make($type_uri, $count=1, $required=false, $alias=null)
		$attribute[] = Auth_OpenID_AX_AttrInfo::make( 'http://axschema.org/contact/email', 1, 1, 'email' );
		$attribute[] = Auth_OpenID_AX_AttrInfo::make( 'http://axschema.org/namePerson/first', 1, 1, 'firstname' );
		$attribute[] = Auth_OpenID_AX_AttrInfo::make( 'http://axschema.org/namePerson/last', 1, 1, 'lastname' );

		// Create AX fetch request and add attributes
		$ax_request = new Auth_OpenID_AX_FetchRequest;

		foreach ( $attribute as $attr ) {
			$ax_request->add( $attr );
		}

		if ( $ax_request ) {
			$auth_request->addExtension( $ax_request );
		}

		$process_url = $this->scriptUrl( $finish_page );

		if ( $auth_request->shouldSendRedirect() ) {
			$redirect_url = $auth_request->redirectURL( $trust_root,
													   $process_url );
			if ( Auth_OpenID::isFailure( $redirect_url ) ) {
				displayError( "Could not redirect to server: " . $redirect_url->message );
			} else {
				# OK, now go
				$wgOut->redirect( $redirect_url );
			}
		} else {
			// Generate form markup and render it.
			$form_id = 'openid_message';
			$form_html = $auth_request->formMarkup( $trust_root, $process_url,
												   false, array( 'id' => $form_id ) );

			// Display an error if the form markup couldn't be generated;
			// otherwise, render the HTML.
			if ( Auth_OpenID::isFailure( $form_html ) ) {
				displayError( 'Could not redirect to server: ' . $form_html->message );
			} else {
				$wgOut->addWikiMsg( 'openidautosubmit' );
				$wgOut->addHTML( $form_html );
				$wgOut->addInlineScript(
					"jQuery( document ).ready( function(){ jQuery( \"#" . $form_id . "\" ).submit() } );" 
				);
			}
		}

		wfRestoreWarnings();
	}

	function scriptUrl( $par = false ) {
		global $wgServer, $wgScript;

		if ( !is_object( $par ) ) {
			$nt = $this->getTitle( $par );
		} else {
			$nt = $par;
		}

		if ( !is_null( $nt ) ) {
			$dbkey = wfUrlencode( $nt->getPrefixedDBkey() );
			return "{$wgServer}{$wgScript}?title={$dbkey}";
		} else {
			return '';
		}
	}

	protected function setupSession() {
		if ( session_id() == '' ) {
			wfSetupSession();
		}
	}

	function loginSetCookie( $openid ) {
		global $wgRequest, $wgOpenIDCookieExpiration;
		$wgRequest->response()->setcookie( 'OpenID', $openid, time() +  $wgOpenIDCookieExpiration );
	}

	# Find the user with the given openid
	# return the registered OpenID urls and registration timestamps (if available)
	public static function getUserOpenIDInformation( $user ) {
		$openid_urls_registration = array();

		if ( $user instanceof User && $user->getId() != 0 ) {
			$dbr = wfGetDB( DB_SLAVE );
			$res = $dbr->select(
				array( 'user_openid' ),
				array( 'uoi_openid', 'uoi_user_registration' ),
				array( 'uoi_user' => $user->getId() ),
				__METHOD__
			);

			foreach ( $res as $row ) {
				$openid_urls_registration[] = $row;
			}
			$res->free();
		}
		return $openid_urls_registration;
	}

	public static function getUserFromUrl( $openid ) {
		$dbr = wfGetDB( DB_SLAVE );

		$id = $dbr->selectField(
			'user_openid',
			'uoi_user',
			array( 'uoi_openid' => $openid ),
			__METHOD__
		);

		if ( $id ) {
			return User::newFromId( $id );
		} else {
			return null;
		}
	}

	public static function addUserUrl( $user, $url ) {
		$dbw = wfGetDB( DB_MASTER );

		$dbw->insert(
			'user_openid',
			array(
				'uoi_user' => $user->getId(),
				'uoi_openid' => $url,
				'uoi_user_registration' => wfTimestamp( TS_MW )
			),
			__METHOD__
		);

	}

	public static function removeUserUrl( $user, $url ) {
		$dbw = wfGetDB( DB_MASTER );

		$dbw->delete(
			'user_openid',
			array(
				'uoi_user' => $user->getId(),
				'uoi_openid' => $url
			),
			__METHOD__
		);

		return (bool)$dbw->affectedRows();
	}
}