Przeglądaj źródła

Update ext.php to serve any file from extensions

Add an extension->getFileUrl() method to facilitate url generation
Marien Fressinaud 11 lat temu
rodzic
commit
f9b037742a
2 zmienionych plików z 42 dodań i 18 usunięć
  1. 23 0
      lib/Minz/Extension.php
  2. 19 18
      p/ext.php

+ 23 - 0
lib/Minz/Extension.php

@@ -75,6 +75,9 @@ class Minz_Extension {
 	public function getEntrypoint() {
 		return $this->entrypoint;
 	}
+	public function getPath() {
+		return $this->path;
+	}
 	public function getAuthor() {
 		return $this->author;
 	}
@@ -93,4 +96,24 @@ class Minz_Extension {
 		}
 		$this->type = $type;
 	}
+
+	/**
+	 * Return the url for a given file.
+	 *
+	 * @param $filename name of the file to serve.
+	 * @param $type the type (js or css) of the file to serve.
+	 * @return the url corresponding to the file.
+	 */
+	public function getFileUrl($filename, $type) {
+		$dir = end(explode('/', $this->path));
+		$file_name_url = urlencode($dir . '/' . $filename);
+
+		$absolute_path = $this->path . '/' . $filename;
+		$mtime = @filemtime($absolute_path);
+
+		$url = '/ext.php?f=' . $file_name_url .
+		       '&t=' . $type .
+		       '&' . $mtime;
+		return Minz_Url::display($url);
+	}
 }

+ 19 - 18
p/ext.php

@@ -1,32 +1,33 @@
 <?php
-if (!isset($_GET['e'])) {
-	header('HTTP/1.1 400 Bad Request');
-	die();
-}
-$extension = substr($_GET['e'], 0, 64);
-if (!ctype_alpha($extension)) {
+if (!isset($_GET['f']) ||
+		!isset($_GET['t'])) {
 	header('HTTP/1.1 400 Bad Request');
 	die();
 }
 
 require('../constants.php');
-$filename = FRESHRSS_PATH . '/extensions/' . $extension . '/';
 
-if (isset($_GET['j'])) {
-	header('Content-Type: application/javascript; charset=UTF-8');
-	header('Content-Disposition: inline; filename="script.js"');
-	$filename .= 'script.js';
-} elseif (isset($_GET['c'])) {
+$file_name = urldecode($_GET['f']);
+$file_type = $_GET['t'];
+
+$absolute_filename = EXTENSIONS_PATH . '/' . $file_name;
+
+switch ($file_type) {
+case 'css':
 	header('Content-Type: text/css; charset=UTF-8');
-	header('Content-Disposition: inline; filename="style.css"');
-	$filename .= 'style.css';
-} else {
+	header('Content-Disposition: inline; filename="' . $file_name . '"');
+	break;
+case 'js':
+	header('Content-Type: application/javascript; charset=UTF-8');
+	header('Content-Disposition: inline; filename="' . $file_name . '"');
+	break;
+default:
 	header('HTTP/1.1 400 Bad Request');
 	die();
 }
 
-$mtime = @filemtime($filename);
-if ($mtime == false) {
+$mtime = @filemtime($absolute_filename);
+if ($mtime === false) {
 	header('HTTP/1.1 404 Not Found');
 	die();
 }
@@ -34,5 +35,5 @@ if ($mtime == false) {
 require(LIB_PATH . '/http-conditional.php');
 
 if (!httpConditional($mtime, 604800, 2)) {
-	readfile($filename);
+	readfile($absolute_filename);
 }