Zachary Kotlarek 13 лет назад
Родитель
Сommit
58cfda26f2
5 измененных файлов с 82 добавлено и 21 удалено
  1. 43 2
      web/includes/series.php
  2. 18 0
      web/includes/tvdb.php
  3. 19 0
      web/includes/util.php
  4. 1 16
      web/includes/webloc.php
  5. 1 3
      web/index.php

+ 43 - 2
web/includes/series.php

@@ -135,8 +135,49 @@ function saveFlags($series, $data, $series_last, $seasons_last) {
 	}
 	}
 }
 }
 
 
-# Add a series as identified by TVDB ID
-function addSeries($id) {
+# Add a series as identified by TVDB URL or ID
+function addSeries($str) {
+	global $TVDB_LANG_ID;
+	$id     = false;
+	$lid    = false;
+	$series = false;
+
+	# Accept URLs or raw IDs
+	if (preg_match('/^\d+$/', $str)) {
+		$id = intval($str);
+	} else {
+		list($id, $lid) = parseTVDBURL($str);
+	}
+
+	# Ensure we got something useful
+	if (!$id) {
+		die('Invalid ID or URL: ' . htmlspecialchars($str) . "\n");
+	}
+	if (!$lid) {
+		$lid = $TVDB_LANG_ID;
+	}
+
+	# Find the show name
+	$series = getTVDBTitle($id, $lid);
+
+	# Ensure the title is reasonable
+	if (!$series) {
+		die('No such TVDB series: ' . htmlspecialchars($id) . "\n");
+	}
+
+	# Clean the title for filesystem use
+	$series = cleanSeries($series);
+
+	if (seriesExists($series)) {
+		die('Series already exists: ' . htmlspecialchars($series) . "\n");
+	}
+
+	# If all is well, create the folder and webloc file
+	$series_path = seriesPath($series);
+	if(@mkdir($series_path)) {
+		writeWebloc(TVDBURL($id, $lid), $series_path . '/' . $series . '.webloc');
+		addSeason($series, 1);
+	}
 }
 }
 
 
 ?>
 ?>

+ 18 - 0
web/includes/tvdb.php

@@ -2,6 +2,24 @@
 
 
 require 'config.php';
 require 'config.php';
 
 
+# Parse and input URL for TVDB ID and LID
+function parseTVDBURL($url) {
+	$retval = array(
+		'tvdb-id'  => false,
+		'tvdb-lid' => false,
+	);
+
+        # Accept both the raw and encoded verisons of the URL
+	if (preg_match('/(?:\?|\&(?:amp;)?)id=(\d+)/', $url, $matches)) {
+		$retval['tvdb-id'] = $matches[1];
+	}
+	if (preg_match('/(?:\?|\&(?:amp;)?)lid=(\d+)/', $url, $matches)) {
+		$retval['tvdb-lid'] = $matches[1];
+	}
+
+	return $retval;
+}
+
 # Build a series URL from the ID and optional LID
 # Build a series URL from the ID and optional LID
 function TVDBURL($id, $lid) {
 function TVDBURL($id, $lid) {
 	global $TVDB_URL;
 	global $TVDB_URL;

+ 19 - 0
web/includes/util.php

@@ -2,6 +2,25 @@
 
 
 require 'config.php';
 require 'config.php';
 
 
+# Cleanup the provided series name
+# This does NOT provide comprehensively safe output,
+# it merely attempts to make names compatible with the filesystem and local convetions
+function cleanSeries($series) {
+	# Clearly unreasonable characters
+	$series = preg_replace('/[\0\n\r]/', ' ', $series);
+
+	# Not allowed on our filesystem
+	$series = preg_replace('/\s+[\/\:]\s+/', ' - ', $series);
+
+	# General string cleanup
+	$series = preg_replace('/\s+/', ' ', $series);
+
+	# Ensure we don't go out-of-scope
+	$series = basename($series);
+
+	return $series;
+}
+
 # True if the input path is junk -- self-links, OS X noise, etc.
 # True if the input path is junk -- self-links, OS X noise, etc.
 function isJunk($path) {
 function isJunk($path) {
 	$path = basename($path);
 	$path = basename($path);

+ 1 - 16
web/includes/webloc.php

@@ -51,23 +51,8 @@ function findWebloc($path) {
 
 
 # Read and parse the provided *.webloc file
 # Read and parse the provided *.webloc file
 function readWebloc($file) {
 function readWebloc($file) {
-	global $TVDB_URL;
-	$retval = array(
-		'tvdb-id'  => false,
-		'tvdb-lid' => false,
-	);
-
-	# Read and parse the file
-	# Accept both the resource-fork and data-fork (i.e. plist) verisons of the file
 	$str = trim(file_get_contents($file));
 	$str = trim(file_get_contents($file));
-	if (preg_match('/(?:\?|\&(?:amp;)?)id=(\d+)/', $str, $matches)) {
-		$retval['tvdb-id'] = $matches[1];
-	}
-	if (preg_match('/(?:\?|\&(?:amp;)?)lid=(\d+)/', $str, $matches)) {
-		$retval['tvdb-lid'] = $matches[1];
-	}
-
-	return $retval;
+	return parseTVDBURL($str);	
 }
 }
 
 
 ?>
 ?>

+ 1 - 3
web/index.php

@@ -9,9 +9,7 @@ header('Content-type: text/html; charset=utf-8');
 # Did the user request a specific series?
 # Did the user request a specific series?
 $series = false;
 $series = false;
 if (isset($_REQUEST['series'])) {
 if (isset($_REQUEST['series'])) {
-	# This is not a great filter, but it should make the string safe for use as a quoted path
-	$series = preg_replace('/[\0\n\r]/', '', $_REQUEST['series']);
-	$series = basename($series);
+	$series = cleanSeries($_REQUEST['series']);
 }
 }
 
 
 # Did the user request an update?
 # Did the user request an update?