Zachary Kotlarek 12 lat temu
rodzic
commit
96d2eef476
5 zmienionych plików z 178 dodań i 37 usunięć
  1. 65 0
      web/includes/auth.php
  2. 4 0
      web/includes/config.php
  3. 2 0
      web/includes/main.php
  4. 20 37
      web/index.php
  5. 87 0
      web/login.php

+ 65 - 0
web/includes/auth.php

@@ -0,0 +1,65 @@
+<?
+
+# Always start a session
+session_start();
+
+function login($username, $password) {
+	require 'config.php';
+	$username = preg_replace('/\W/', '_', $username);
+
+	# Set the PAM service name
+	ini_set('pam.servicename', $PAM_SERVICE);
+
+	if (pam_auth($username, $password)) {
+		$_SESSION['USER'] = $username;
+		session_regenerate_id(true);
+	} else {
+		logout();
+	}
+
+	# Redirect, if we have a target
+	if (isset($_GET['dest'])) {
+		header('Location: ' . $_GET['dest']);
+	}
+}
+
+function logout() {
+	require 'config.php';
+	unset($_SESSION['USER']);
+	session_regenerate_id(true);
+	header('Location: ./');
+	exit();
+}
+
+function username() {
+	return $_SESSION['USER'];
+}
+
+function authenticated() {
+	return isset($_SESSION['USER']);
+}
+
+function require_authentication() {
+	require 'config.php';
+	if (!authenticated()) {
+
+		# Provide the current URL for post-auth redirect, if possible
+		$dest = '';
+		if (!preg_match('/' . preg_quote($LOGIN_PAGE) . '/', $_SERVER['PHP_SELF'])) {
+			$dest = $_SERVER['PHP_SELF'];
+			if ($_GET['series']) {
+				$dest .= '?series=' . $_GET['series'];
+			}
+		}
+
+		$url = $LOGIN_PAGE;
+		if (strlen($dest)) {
+			$url .= '?dest=' . urlencode($dest);
+		}
+
+		header('Location: ' . $url);
+		exit();
+	}
+}
+
+?>

+ 4 - 0
web/includes/config.php

@@ -16,4 +16,8 @@ $TVDB_URL         = 'http://thetvdb.com/?tab=series';
 $EXISTS_FILES  = array('no_quality_checks', 'more_number_formats', 'skip');
 $CONTENT_FILES = array('must_match', 'search_name', 'excludes');
 
+### Auth Config
+$LOGIN_PAGE  = 'login';
+$PAM_SERVICE = 'tv_web';
+
 ?>

+ 2 - 0
web/includes/main.php

@@ -2,6 +2,8 @@
 
 require 'config.php';
 
+require_once 'auth.php';
+
 require_once 'util.php';
 require_once 'webloc.php';
 

+ 20 - 37
web/index.php

@@ -14,32 +14,24 @@ if (isset($_REQUEST['series'])) {
 
 # Did the user request an update?
 if ($series !== false && isset($_POST['Save'])) {
-	# Require auth (or for the time being, specific IP addresses)
-	if (preg_match('/^(?:172\.19\.[17]\.|2602:3f:e50d:76|74\.93\.97\.65)/', $_SERVER['REMOTE_ADDR'])) {
-
-		# Grab the current settings for comparison
-		$series_last = readFlags($series);
-		$seasons_last = findSeasons($series);
-
-		# Save series and season data
-		saveFlags($series, $_POST, $series_last, $seasons_last);
-		saveSeasons($series, $_POST, $series_last, $seasons_last);
-	} else {
-		echo '<h4 style="color: red;">Cannot save: User not authenticated</h4>';
-	}
+	require_authentication();
+
+	# Grab the current settings for comparison
+	$series_last = readFlags($series);
+	$seasons_last = findSeasons($series);
+
+	# Save series and season data
+	saveFlags($series, $_POST, $series_last, $seasons_last);
+	saveSeasons($series, $_POST, $series_last, $seasons_last);
 }
 
 # Did the user add a season?
 if ($series !== false && isset($_POST['AddSeason'])) {
-	# Require auth (or for the time being, specific IP addresses)
-	if (preg_match('/^(?:172\.19\.[17]\.|2602:3f:e50d:76|74\.93\.97\.65)/', $_SERVER['REMOTE_ADDR'])) {
-
-		# Add a season folder
-		$season = intval($_POST['season_add']);
-		addSeason($series, $season);
-	} else {
-		echo '<h4 style="color: red;">Cannot save: User not authenticated</h4>';
-	}
+	require_authentication();
+
+	# Add a season folder
+	$season = intval($_POST['season_add']);
+	addSeason($series, $season);
 }
 
 # Did the user delete a season?
@@ -52,28 +44,19 @@ if ($series !== false) {
 		}
 	}
 	if ($season !== false) {
+		require_authentication();
 
-		# Require auth (or for the time being, specific IP addresses)
-		if (preg_match('/^(?:172\.19\.[17]\.|2602:3f:e50d:76|74\.93\.97\.65)/', $_SERVER['REMOTE_ADDR'])) {
-
-			# Remove a season folder (if empty)
-			delSeason($series, $season);
-		} else {
-			echo '<h4 style="color: red;">Cannot save: User not authenticated</h4>';
-		}
+		# Remove a season folder (if empty)
+		delSeason($series, $season);
 	}
 }
 
 # Did the user add a series?
 if ($series === false && isset($_POST['series_add'])) {
-	# Require auth (or for the time being, specific IP addresses)
-	if (preg_match('/^(?:172\.19\.[17]\.|2602:3f:e50d:76|74\.93\.97\.65)/', $_SERVER['REMOTE_ADDR'])) {
+	require_authentication();
 
-		# Add a series folder
-		$series = addSeries($_POST['series_add']);
-	} else {
-		echo '<h4 style="color: red;">Cannot save: User not authenticated</h4>';
-	}
+	# Add a series folder
+	$series = addSeries($_POST['series_add']);
 }
 
 #=========================================================================================

+ 87 - 0
web/login.php

@@ -0,0 +1,87 @@
+<?
+
+# Include the TV functions
+require_once 'includes/main.php';
+
+# Send our headers early
+header('Content-type: text/html; charset=utf-8');
+
+# Process login
+if (isset($_POST['username']) && isset($_POST['password'])) {
+	login($_POST['username'], $_POST['password']);
+}
+
+# Process logout
+if (isset($_REQUEST['logout'])) {
+	logout();
+}
+
+#=========================================================================================
+
+# Generic XHTML 1.1 header
+print <<<ENDOLA
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
+   "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head>
+	<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
+	<title>UberZach TV - Login</title>
+
+	<!-- Default JQuery -->
+	<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
+
+	<!-- Provide # support in JQuery-mobile autodividers -->
+	<!-- Must be loaded before JQuery-mobile  -->
+	<script>
+	$( document ).on( "mobileinit", function() {
+		$.mobile.listview.prototype.options.autodividersSelector = function( elt ) {
+			var text = $.trim( elt.text() ) || null;
+			if ( !text ) {
+				return null;
+			}
+			if ( !isNaN(parseFloat(text)) ) {
+				return "#";
+			} else {
+				text = text.slice( 0, 1 ).toUpperCase();
+				return text;
+			}
+		};
+	});
+	</script>
+
+	<!-- Default JQuery-mobile -->
+	<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css" />
+	<script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
+
+	<!-- Custom code for the linkbar -->
+	<script src="autodividers-linkbar.js"></script>
+	<link rel="stylesheet" href="autodividers-linkbar.css">
+</head>
+<body>
+
+ENDOLA;
+
+$url = $_SERVER['PHP_SELF'];
+if (isset($_GET['dest'])) {
+	$url .= '?dest=' . urlencode($_GET['dest']);
+}
+
+print <<<ENDOLA
+<div style="width: 50%; margin-left: auto; margin-right: auto;">
+<form action="${url}" method="post" data-ajax="false">
+<p>
+<label>Username: <input type="text" name="username" /></label><br/>
+<label>Password: <input type="password" name="password" /></label><br/>
+</p>
+<p><input type="submit" name="login" value="Login" /></p>
+</form>
+</div>
+
+ENDOLA;
+
+# Generic XHTML 1.1 footer
+print <<<ENDOLA
+</body>
+</html>
+ENDOLA;
+?>