Completed
Pull Request — newinternal (#542)
by Simon
11:55 queued 02:00
created

XmlApiPageBase::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca\Tasks;
10
11
use DOMDocument;
12
use DOMElement;
13
use Waca\API\ApiException;
14
use Waca\API\IXmlApiAction;
15
use Waca\WebRequest;
16
17
18
abstract class XmlApiPageBase extends ApiPageBase implements IXmlApiAction
19
{
20
    /**
21
     * API result document
22
     * @var DOMDocument
23
     */
24
    protected $document;
25
26
    public function __construct()
27
    {
28
        $this->document = new DOMDocument('1.0');
29
    }
30
31
    /**
32
     * Main function for this page, when no specific actions are called.
33
     *
34
     * @throws ApiException
35
     * @return void
36
     */
37
    final protected function main()
38
    {
39
        if (headers_sent()) {
40
            throw new ApiException('Headers have already been sent - this indicates a bug in the application!');
41
        }
42
43
        header("Content-Type: text/xml");
44
45
        // javascript access control
46
        $httpOrigin = WebRequest::origin();
47
48
        if ($httpOrigin !== null) {
49
            $CORSallowed = $this->getSiteConfiguration()->getCrossOriginResourceSharingHosts();
50
51
            if (in_array($httpOrigin, $CORSallowed)) {
52
                header("Access-Control-Allow-Origin: " . $httpOrigin);
53
            }
54
        }
55
56
        $responseData = $this->runApiPage();
57
58
        ob_end_clean();
59
        print($responseData);
60
        ob_start();
61
    }
62
63
    /**
64
     * Method that runs API action
65
     *
66
     * @param DOMElement $apiDocument
67
     *
68
     * @return DOMElement
69
     */
70
    abstract public function executeApiAction(DOMElement $apiDocument);
71
72
    /**
73
     * @return string
74
     */
75
    final public function runApiPage()
76
    {
77
        $apiDocument = $this->document->createElement("api");
78
79
        try {
80
            $apiDocument = $this->executeApiAction($apiDocument);
81
        }
82
        catch (ApiException $ex) {
83
            $exception = $this->document->createElement("error");
84
            $exception->setAttribute("message", $ex->getMessage());
85
            $apiDocument->appendChild($exception);
86
        }
87
88
        $this->document->appendChild($apiDocument);
89
90
        return $this->document->saveXML();
91
    }
92
}
93