|
@@ -3,12 +3,16 @@ include("config.php");
|
|
include_once("ppcl.php");
|
|
include_once("ppcl.php");
|
|
|
|
|
|
function send_message($type, $message) {
|
|
function send_message($type, $message) {
|
|
|
|
+ send_partial_message($type, $message);
|
|
|
|
+ exit();
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+function send_partial_message($type, $message) {
|
|
header("Content-type: application/pprf");
|
|
header("Content-type: application/pprf");
|
|
echo("PPRF\x00");
|
|
echo("PPRF\x00");
|
|
$len = strlen($message) + 1;
|
|
$len = strlen($message) + 1;
|
|
echo(pack("PC", $len, $type));
|
|
echo(pack("PC", $len, $type));
|
|
echo($message);
|
|
echo($message);
|
|
- exit();
|
|
|
|
}
|
|
}
|
|
|
|
|
|
function send_failure($code, $message) {
|
|
function send_failure($code, $message) {
|
|
@@ -108,6 +112,328 @@ if(fread($handle, 5) != "PPRF\x00") {
|
|
|
|
|
|
$message_info = unpack("Psize/Ctype", fread($handle, 9));
|
|
$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
|
|
// Get collection
|
|
if($message_info["type"] == 6) {
|
|
if($message_info["type"] == 6) {
|
|
$ppcl = get_ppcl();
|
|
$ppcl = get_ppcl();
|