replace xml_tag with HTMLElement
This commit is contained in:
		
							parent
							
								
									c29fe0583f
								
							
						
					
					
						commit
						9b878d98d6
					
				@ -190,8 +190,8 @@ function stream_file(string $file, int $start, int $end): void
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
if (!function_exists('http_parse_headers')) { #http://www.php.net/manual/en/function.http-parse-headers.php#112917
 | 
			
		||||
 | 
			
		||||
# http://www.php.net/manual/en/function.http-parse-headers.php#112917
 | 
			
		||||
if (!function_exists('http_parse_headers')) {
 | 
			
		||||
    /**
 | 
			
		||||
     * #return string[]
 | 
			
		||||
     */
 | 
			
		||||
@ -478,25 +478,6 @@ function clamp(?int $val, ?int $min=null, ?int $max=null): int
 | 
			
		||||
    return $val;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function xml_tag(string $name, array $attrs=[], array $children=[]): string
 | 
			
		||||
{
 | 
			
		||||
    $xml = "<$name ";
 | 
			
		||||
    foreach ($attrs as $k => $v) {
 | 
			
		||||
        $xv = str_replace(''', ''', htmlspecialchars((string)$v, ENT_QUOTES));
 | 
			
		||||
        $xml .= "$k=\"$xv\" ";
 | 
			
		||||
    }
 | 
			
		||||
    if (count($children) > 0) {
 | 
			
		||||
        $xml .= ">\n";
 | 
			
		||||
        foreach ($children as $child) {
 | 
			
		||||
            $xml .= xml_tag($child);
 | 
			
		||||
        }
 | 
			
		||||
        $xml .= "</$name>\n";
 | 
			
		||||
    } else {
 | 
			
		||||
        $xml .= "/>\n";
 | 
			
		||||
    }
 | 
			
		||||
    return $xml;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Original PHP code by Chirp Internet: www.chirp.com.au
 | 
			
		||||
 * Please acknowledge use of this code by including this header.
 | 
			
		||||
 | 
			
		||||
@ -76,14 +76,6 @@ class PolyfillsTest extends TestCase
 | 
			
		||||
        $this->assertEquals(clamp(15, 5, 10), 10);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function test_xml_tag()
 | 
			
		||||
    {
 | 
			
		||||
        $this->assertEquals(
 | 
			
		||||
            "<test foo=\"bar\" >\n<cake />\n</test>\n",
 | 
			
		||||
            xml_tag("test", ["foo"=>"bar"], ["cake"])
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function test_truncate()
 | 
			
		||||
    {
 | 
			
		||||
        $this->assertEquals(truncate("test words", 10), "test words");
 | 
			
		||||
 | 
			
		||||
@ -1,5 +1,12 @@
 | 
			
		||||
<?php declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
use \MicroHTML\HTMLElement;
 | 
			
		||||
function TAGS(...$args) {return new HTMLElement("tags", $args);}
 | 
			
		||||
function TAG(...$args) {return new HTMLElement("tag", $args);}
 | 
			
		||||
function POSTS(...$args) {return new HTMLElement("posts", $args);}
 | 
			
		||||
function POST(...$args) {return new HTMLElement("post", $args);}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class DanbooruApi extends Extension
 | 
			
		||||
{
 | 
			
		||||
    public function onPageRequest(PageRequestEvent $event)
 | 
			
		||||
@ -14,10 +21,10 @@ class DanbooruApi extends Extension
 | 
			
		||||
                $this->api_add_post();
 | 
			
		||||
            } elseif ($event->page_matches("api/danbooru/find_posts") || $event->page_matches("api/danbooru/post/index.xml")) {
 | 
			
		||||
                $page->set_mime(MimeType::XML_APPLICATION);
 | 
			
		||||
                $page->set_data($this->api_find_posts());
 | 
			
		||||
                $page->set_data((string)$this->api_find_posts());
 | 
			
		||||
            } elseif ($event->page_matches("api/danbooru/find_tags")) {
 | 
			
		||||
                $page->set_mime(MimeType::XML_APPLICATION);
 | 
			
		||||
                $page->set_data($this->api_find_tags());
 | 
			
		||||
                $page->set_data((string)$this->api_find_tags());
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // Hackery for danbooruup 0.3.2 providing the wrong view url. This simply redirects to the proper
 | 
			
		||||
@ -37,7 +44,7 @@ class DanbooruApi extends Extension
 | 
			
		||||
     * Authenticates a user based on the contents of the login and password parameters
 | 
			
		||||
     * or makes them anonymous. Does not set any cookies or anything permanent.
 | 
			
		||||
     */
 | 
			
		||||
    private function authenticate_user()
 | 
			
		||||
    private function authenticate_user(): void
 | 
			
		||||
    {
 | 
			
		||||
        global $config, $user;
 | 
			
		||||
 | 
			
		||||
@ -66,7 +73,7 @@ class DanbooruApi extends Extension
 | 
			
		||||
     * - tags: any typical tag query. See Tag#parse_query for details.
 | 
			
		||||
     * - after_id: limit results to tags with an id number after after_id. Useful if you only want to refresh
 | 
			
		||||
     */
 | 
			
		||||
    private function api_find_tags(): string
 | 
			
		||||
    private function api_find_tags(): HTMLElement
 | 
			
		||||
    {
 | 
			
		||||
        global $database;
 | 
			
		||||
        $results = [];
 | 
			
		||||
@ -110,16 +117,15 @@ class DanbooruApi extends Extension
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Tag results collected, build XML output
 | 
			
		||||
        $xml = "<tags>\n";
 | 
			
		||||
        $xml = TAGS();
 | 
			
		||||
        foreach ($results as $tag) {
 | 
			
		||||
            $xml .= xml_tag("tag", [
 | 
			
		||||
            $xml->appendChild(TAG([
 | 
			
		||||
                "type" => "0",
 | 
			
		||||
                "counts" => $tag[0],
 | 
			
		||||
                "name" => $tag[1],
 | 
			
		||||
                "id" => $tag[2],
 | 
			
		||||
            ]);
 | 
			
		||||
            ]));
 | 
			
		||||
        }
 | 
			
		||||
        $xml .= "</tags>";
 | 
			
		||||
        return $xml;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -137,7 +143,7 @@ class DanbooruApi extends Extension
 | 
			
		||||
     *
 | 
			
		||||
     * #return string
 | 
			
		||||
     */
 | 
			
		||||
    private function api_find_posts()
 | 
			
		||||
    private function api_find_posts(): HTMLElement
 | 
			
		||||
    {
 | 
			
		||||
        $results = [];
 | 
			
		||||
 | 
			
		||||
@ -175,7 +181,7 @@ class DanbooruApi extends Extension
 | 
			
		||||
 | 
			
		||||
        // Now we have the array $results filled with Image objects
 | 
			
		||||
        // Let's display them
 | 
			
		||||
        $xml = "<posts count=\"{$count}\" offset=\"{$start}\">\n";
 | 
			
		||||
        $xml = POSTS(["count"=>$count, "offset"=>$start]);
 | 
			
		||||
        foreach ($results as $img) {
 | 
			
		||||
            // Sanity check to see if $img is really an image object
 | 
			
		||||
            // If it isn't (e.g. someone requested an invalid md5 or id), break out of the this
 | 
			
		||||
@ -185,7 +191,7 @@ class DanbooruApi extends Extension
 | 
			
		||||
            $taglist = $img->get_tag_list();
 | 
			
		||||
            $owner = $img->get_owner();
 | 
			
		||||
            $previewsize = get_thumbnail_size($img->width, $img->height);
 | 
			
		||||
            $xml .= xml_tag("post", [
 | 
			
		||||
            $xml->appendChild(TAG([
 | 
			
		||||
                "id" => $img->id,
 | 
			
		||||
                "md5" => $img->hash,
 | 
			
		||||
                "file_name" => $img->filename,
 | 
			
		||||
@ -202,9 +208,8 @@ class DanbooruApi extends Extension
 | 
			
		||||
                "source" => $img->source,
 | 
			
		||||
                "score" => 0,
 | 
			
		||||
                "author" => $owner->name
 | 
			
		||||
            ]);
 | 
			
		||||
            ]));
 | 
			
		||||
        }
 | 
			
		||||
        $xml .= "</posts>";
 | 
			
		||||
        return $xml;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -235,7 +240,7 @@ class DanbooruApi extends Extension
 | 
			
		||||
     * Get:
 | 
			
		||||
     * - Redirected to the newly uploaded post.
 | 
			
		||||
     */
 | 
			
		||||
    private function api_add_post()
 | 
			
		||||
    private function api_add_post(): void
 | 
			
		||||
    {
 | 
			
		||||
        global $user, $page;
 | 
			
		||||
        $danboorup_kludge = 1;            // danboorup for firefox makes broken links out of location: /path
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user