Parcourir la source

Add suppot for new messages, fix indexer errors

Billy Barrow il y a 1 an
Parent
commit
fc17d2eaa4
3 fichiers modifiés avec 330 ajouts et 4 suppressions
  1. 1 1
      config.php
  2. 2 2
      ppix-gen.php
  3. 327 1
      pprf.php

+ 1 - 1
config.php

@@ -9,7 +9,7 @@ define("PUBLICATION_DIR", "ppubs");
 define("PUBLICATION_NAME", "Post");
 define("DATE_FORMAT", "l d F Y, H:i");
 define("USE_PPIX", true);
-define("USE_PPCL", true);
+define("USE_PPCL", false);
 define("ENABLE_PPRF", true);
 define("ENABLE_PPRF_VCDIFF", true);
 define("XDELTA3_PATH", "/usr/bin/xdelta3");

+ 2 - 2
ppix-gen.php

@@ -19,7 +19,7 @@ class PpixGenerator {
         if($tags != null) {
             foreach(explode(" ", $tags) as $tag) {
                 if(array_key_exists($tag, $this->tags)) {
-                    array_push($collections[$this->tags[$tag]], $index);
+                    array_push($this->collections[$this->tags[$tag]], $index);
                 }
                 else {
                     $collection_index = count($this->collections);
@@ -37,7 +37,7 @@ class PpixGenerator {
 
         $default_asset = $ppub->asset_list[1];
         error_log("Default asset: " . $default_asset->mimetype);
-        if(str_starts_with($default_asset->mimetype, "text/")) {
+        if(substr($default_asset->mimetype, 0, 5) == "text/") {
             $words = $this->word_array_collect($words, $ppub->read_asset($default_asset));
         }
 

+ 327 - 1
pprf.php

@@ -3,12 +3,16 @@ include("config.php");
 include_once("ppcl.php");
 
 function send_message($type, $message) {
+    send_partial_message($type, $message);
+    exit();
+}
+
+function send_partial_message($type, $message) {
     header("Content-type: application/pprf");
     echo("PPRF\x00");
     $len = strlen($message) + 1;
     echo(pack("PC", $len, $type));
     echo($message);
-    exit();
 }
 
 function send_failure($code, $message) {
@@ -108,6 +112,328 @@ if(fread($handle, 5) != "PPRF\x00") {
 
 $message_info = unpack("Psize/Ctype", fread($handle, 9));
 
+// Get listing
+if($message_info["type"] == 1) {
+    error_log("Get listing!!!!");
+    $ppcl = get_ppcl();
+    verify_collection_message($handle, $ppcl);
+
+    $data = unpack("vflags/vcols/Vskip/Ctake", fread($handle, 9));
+
+    $flag_tag = ($data["flags"] & (1 << 0)) != 0;
+    $flag_search = ($data["flags"] & (1 << 1)) != 0;
+    $flag_since = ($data["flags"] & (1 << 2)) != 0;
+    $flag_hidden = ($data["flags"] & (1 << 3)) != 0;
+
+    $col_title = ($data["cols"] & (1 << 0)) != 0;
+    $col_author = ($data["cols"] & (1 << 1)) != 0;
+    $col_description = ($data["cols"] & (1 << 2)) != 0;
+    $col_timestamp = ($data["cols"] & (1 << 3)) != 0;
+    $col_tags = ($data["cols"] & (1 << 4)) != 0;
+    $col_poster = ($data["cols"] & (1 << 5)) != 0;
+    $col_copyright = ($data["cols"] & (1 << 6)) != 0;
+    $col_checksum = ($data["cols"] & (1 << 7)) != 0;
+
+    $tag = null;
+    $search = null;
+    $since = null;
+
+    if($flag_tag && $flag_search) {
+        send_failure(1, "Listing by tag and search at the same time is not supported");
+    }
+
+    if($flag_tag) {
+        $size = unpack("v", fread($handle, 2))[1];
+        $tag = fread($handle, $size);
+    }
+    if($flag_search) {
+        $size = unpack("v", fread($handle, 2))[1];
+        $search = fread($handle, $size);
+    }
+    if($flag_since) {
+        $size = unpack("C", fread($handle, 2))[1];
+        $since = new DateTime(fread($handle, $size));
+    }
+
+    $results = array();
+    if(USE_PPIX) {
+        include_once("ppix.php");
+        $ppix = new Ppix(fopen(PUBLICATION_DIR . "/lib.ppix", 'rb'));
+        if($flag_search) {
+            $ids = $ppix->do_search(strtolower($search));
+            $list = array();
+            for ($i=0; $i < count($ids); $i++) { 
+                $list[$i] = $ppix->get_publication_by_id($ids[$i]);
+            }
+            $results = $list;
+
+        } else if($flag_tag) {
+            $tags = $ppix->get_tags();
+            $col = $tags[$tag];
+            if($col != null) {
+                $ids = $ppix->get_collection_by_id($col);
+                $list = array();
+                for ($i=0; $i < count($ids); $i++) { 
+                    $list[$i] = $ppix->get_publication_by_id($ids[$i]);
+                }
+                $results = $list;
+            }
+
+        } else {
+            $count = $ppix->get_publication_count();
+            $list = array();
+            for ($i=0; $i < $count; $i++) { 
+                $list[$i] = $ppix->get_publication_by_id($i);
+            }
+            $results = $list;
+        }
+    }
+    else {
+        if($flag_tag || $flag_search) {
+            send_failure(1, "Indexed listings are not enabled on this server");
+        }
+        $count = count($ppcl->publications);
+        $list = array();
+        for ($i=0; $i < $count; $i++) { 
+            $list[$i] = $ppcl->publications[$i]->name;
+        }
+        $results = $list;
+    }
+
+    $publications = array();
+    foreach($results as $result) {
+        foreach($ppcl->publications as $pub) {
+            if($pub->name == $result && (!$flag_since || $pub->timestamp > $since)) {
+                array_push($publications, $pub);
+            }
+        }
+    }
+
+    $publications = array_slice($publications, $data["skip"], $data["take"]);
+    
+    $res_cols = 0;
+    if($col_title) {
+        $res_cols |= (1 << 0);
+    }
+    if($col_author) {
+        $res_cols |= (1 << 1);
+    }
+    if($col_description) {
+        $res_cols |= (1 << 2);
+    }
+    if($col_timestamp) {
+        $res_cols |= (1 << 3);
+    }
+    if($col_tags) {
+        $res_cols |= (1 << 4);
+    }
+    if($col_poster) {
+        $res_cols |= (1 << 5);
+    }
+    if($col_copyright) {
+        $res_cols |= (1 << 6);
+    }
+    if($col_checksum) {
+        $res_cols |= (1 << 7);
+    }
+
+    $message = pack("vC", $res_cols, count($publications));
+
+    include_once("ppub.php");
+    foreach($publications as $pub) {
+        $message .= pack("v", strlen($pub->name));
+        $message .= $pub->name;
+
+        $ppub = new Ppub();
+        $ppub->read_file(PUBLICATION_DIR . "/".$pub->name);
+        if($col_title) {
+            if(!isset($ppub->metadata["title"])) {
+                $message .= "\x00\x00";
+            }
+            else {
+                $message .= pack("v", strlen($ppub->metadata["title"]));
+                $message .= $ppub->metadata["title"];
+            }
+        }
+        if($col_author) {
+            if(!isset($ppub->metadata["author"])) {
+                $message .= "\x00\x00";
+            }
+            else {
+                $message .= pack("v", strlen($ppub->metadata["author"]));
+                $message .= $ppub->metadata["author"];
+            }
+        }
+        if($col_description) {
+            if(!isset($ppub->metadata["description"])) {
+                $message .= "\x00\x00";
+            }
+            else {
+                $message .= pack("v", strlen($ppub->metadata["description"]));
+                $message .= $ppub->metadata["description"];
+            }
+        }
+        if($col_timestamp) {
+            if(!isset($ppub->metadata["date"])) {
+                $message .= "\x00\x00";
+            }
+            else {
+                $message .= pack("C", strlen($ppub->metadata["date"]));
+                $message .= $ppub->metadata["date"];
+            }
+        }
+        if($col_tags) {
+            if(!isset($ppub->metadata["tags"])) {
+                $message .= "\x00\x00";
+            }
+            else {
+                $message .= pack("v", strlen($ppub->metadata["tags"]));
+                $message .= $ppub->metadata["tags"];
+            }
+        }
+        if($col_poster) {
+            if(!isset($ppub->metadata["poster"])) {
+                $message .= "\x00\x00";
+            }
+            else {
+                $message .= pack("v", strlen($ppub->metadata["poster"]));
+                $message .= $ppub->metadata["poster"];
+            }
+        }
+        if($col_copyright) {
+            if(!isset($ppub->metadata["copyright"])) {
+                $message .= "\x00\x00";
+            }
+            else {
+                $message .= pack("v", strlen($ppub->metadata["copyright"]));
+                $message .= $ppub->metadata["copyright"];
+            }
+        }
+        if($col_checksum) {
+            $message .= $pub->checksum;
+        }
+    }
+
+    send_message(130, $message);
+    
+}
+
+// Get asset
+if($message_info["type"] == 3) {
+    include_once("ppub.php");
+    $ppcl = get_ppcl();
+    verify_collection_message($handle, $ppcl);
+    $flags = unpack("v", fread($handle, 2))[1];
+    $name_len = unpack("C", fread($handle, 1))[1];
+    $name = fread($handle, $name_len);
+    $asset_len = unpack("v", fread($handle, 2))[1];
+    $asset_name = "";
+    if($asset_len > 0) {
+        $asset_name = fread($handle, $asset_len);
+    }
+    $skip = unpack("P", fread($handle, 8))[1];
+    $take = unpack("P", fread($handle, 8))[1];
+
+    error_log("Get asset " . $asset_name . " from publication " . $name);
+
+    $pub_entry = null;
+    foreach($ppcl->publications as $pub) {
+        if($pub->name == $name) {
+            $pub_entry = $pub;
+            break;
+        }
+    }
+
+    if($pub_entry == null) {
+        send_failure(16, "Publication \"" . $name . "\" not found");
+    }
+
+    $ppub = new Ppub();
+    $ppub->read_file(PUBLICATION_DIR . "/" . $name);
+    $asset = null;
+
+    if($asset_name == "") {
+        $asset = $ppub->asset_list[1];
+    }
+    else {
+        $asset = $ppub->asset_index[$asset_name];
+    }
+
+    if($asset == null) {
+        send_failure(17, "Asset \"" . $asset_name . "\" not found");
+    }
+
+    // Reserved flags
+    $message = "\x00\x00";
+    $message .= pack("C", strlen($asset->mimetype));
+    $message .= $asset->mimetype;
+
+    if($skip == 0 && $take == 0) {
+        $data = $ppub->read_asset($asset);
+        $message .= pack("P", strlen($data));
+        $message .= $data;
+        send_message(132, $message);
+    }
+    else if(!$ppub->can_stream_asset($asset)) {
+        send_failure(18, "Asset is not streamable");
+    }
+
+    $file_size = $ppub->get_asset_size($asset);
+
+    if($skip > $file_size) {
+        $message .= pack("P", 0);
+        send_message(132, $message);
+    }
+
+    if($take + $skip > $file_size) {
+        $take = $file_size - $skip;
+    }
+
+    $message .= pack("P", strlen($take));
+    send_partial_message(132, $message);
+    $ppub->stream_asset($asset, $skip, $take);
+    exit();   
+}
+
+// Get publication
+if($message_info["type"] == 4) {
+    $ppcl = get_ppcl();
+    verify_collection_message($handle, $ppcl);
+    $flags = unpack("v", fread($handle, 2))[1];
+    $name_len = unpack("C", fread($handle, 1))[1];
+    $name = fread($handle, $name_len);
+
+    $pub_entry = null;
+    foreach($ppcl->publications as $pub) {
+        if($pub->name == $name) {
+            $pub_entry = $pub;
+            break;
+        }
+    }
+
+    if($pub_entry == null) {
+        send_failure(16, "Publication \"" . $name . "\" not found");
+    }
+
+    $path = PUBLICATION_DIR . "/" . $name;
+    $size = filesize($path);
+
+    $ppub = fopen($path, 'rb');
+
+    $message = "\x00\x00";
+    $message .= pack("P", $size);
+
+    send_partial_message(133, $message);
+    $pos = 0;
+    while($pos < $size) {
+        $chunksize = min(1024 * 1024, $size - $pos);
+        echo(fread($ppub, $chunksize));
+        flush();
+        $pos += $chunksize;
+    }
+    exit();
+}
+
 // Get collection
 if($message_info["type"] == 6) {
     $ppcl = get_ppcl();