Project

General

Profile

1
<?php
2
#############################################################################
3
# This file holds the utility function getAllConcepts which calls the 
4
# tapir service to find out all the concepts (data units) that are 
5
# supported.  This list is necessary so that we know what to ask for
6
# when making the rquest.
7
#############################################################################
8

    
9
require_once('HTTP/Request.php'); // pear package
10
require_once('tapirRequestTemplate.php'); 
11

    
12

    
13

    
14
function getAllConcepts() {
15
    global $url;
16

    
17
    $error_log_filename = "error.log";
18
    $http_request = new HTTP_Request();
19

    
20
    $body = buildCapabilitiesRequest();
21

    
22
    $http_request->setMethod( 'POST' );
23
    $http_request->addHeader('Content-Type', 'text/xml');
24
    $http_request->addRawPostData( $body );
25
    $http_request->setURL( $url );
26
    $http_request->_timeout = 300;
27
    $http_request->_readTimeout = 300;
28

    
29
    // This can be used to see the entire request
30
    #$raw_request = $http_request->_buildRequest();
31
    #echo "\n\n" . $raw_request;
32

    
33
    $http_request->sendRequest();
34

    
35
    $response = $http_request->getResponseBody();
36
    $code = $http_request->getResponseCode();
37

    
38
    if ( $code != 200 ) // 200 = OK
39
    {
40
        $label = 'Unknown Error';
41

    
42
        switch ( $code )
43
        {
44
            case 201: $label = 'Created'; break;
45
            case 202: $label = 'Accepted'; break;
46
            case 203: $label = 'Non-Authoritative Information'; break;
47
            case 204: $label = 'No Content'; break;
48
            case 205: $label = 'Reset Content'; break;
49
            case 206: $label = 'Partial Content'; break;
50
            case 300: $label = 'Multiple Choices'; break;
51
            case 301: $label = 'Moved Permanently'; break;
52
            case 302: $label = 'Found'; break;
53
            case 303: $label = 'See Other'; break;
54
            case 304: $label = 'Not Modified'; break;
55
            case 305: $label = 'Use Proxy'; break;
56
            case 307: $label = 'Temporary Redirect'; break;
57
            case 400: $label = 'Bad Request'; break;
58
            case 401: $label = 'Unauthorized'; break;
59
            case 402: $label = 'Payment Required'; break;
60
            case 403: $label = 'Forbidden'; break;
61
            case 404: $label = 'Not Found'; break;
62
            case 405: $label = 'Method Not Allowed'; break;
63
            case 406: $label = 'Not Acceptable'; break;
64
            case 407: $label = 'Proxy Authentication Required'; break;
65
            case 408: $label = 'Request Timeout'; break;
66
            case 409: $label = 'Conflict'; break;
67
            case 410: $label = 'Gone'; break;
68
            case 411: $label = 'Length Required'; break;
69
            case 412: $label = 'Precondition Failed'; break;
70
            case 413: $label = 'Request Entity Too Large'; break;
71
            case 414: $label = 'Request-URI Too Long'; break;
72
            case 415: $label = 'Unsupported Media Type'; break;
73
            case 416: $label = 'Requested Range Not Satisfiable'; break;
74
            case 417: $label = 'Expectation Failed'; break;
75
            case 500: $label = 'Internal Server Error'; break;
76
            case 501: $label = 'Not Implemented'; break;
77
            case 502: $label = 'Bad Gateway'; break;
78
            case 503: $label = 'Service Unavailable'; break;
79
            case 504: $label = 'Gateway Timeout'; break;
80
            case 505: $label = 'HTTP Version Not Supported'; break;
81
	}
82

    
83
       $error_log = fopen($error_log_filename,"a");
84
       fwrite($error_log, "Service responded with HTTP ".$code." code: ".$label."\n".
85
              "while attempting to request supported concepts\n");
86
       fclose($error_log);
87
       echo( "Service responded with HTTP ".$code." code: ".$label."\n".
88
              "while attempting to request supported concepts\n");
89
       return 0;
90
    }
91

    
92
    #Weird encoding bug - need to remove the ^F and ^L characters so
93
    #that the xml parser won't choke.
94
    $xmlDoc = new DOMDocument();
95
    $response = preg_replace("//","6",$response);
96
    $response = preg_replace("//","12",$response);
97
    $xmlDoc->loadXML($response);
98

    
99
    $errors = $xmlDoc->getElementsByTagName("error");
100
    if($errors->length > 0) {
101
       $error_log = fopen($error_log_filename,"a");
102
       foreach($errors as $error) {
103
         fwrite($error_log, $error->nodeValue . "\n");
104
       }
105
       fclose($error_log);
106
       return 0;
107
    }
108

    
109
    $concepts = $xmlDoc->getElementsByTagName("mappedConcept");
110
    $concepts_array = array();
111
    if($concepts->length == 0) {
112
       $error_log = fopen($error_log_filename,"a");
113
       fwrite($error_log, "No mappedConcept node, assuming there's a missed error.\n");
114
       fclose($error_log);
115
       return 0;
116
    } else {
117
      foreach($concepts as $concept) {
118
        $concept_id = $concept->getAttribute("id");
119
        $concept_key = preg_replace("/\//","_",$concept_id);
120
        $concept_key = preg_replace("/\./","_",$concept_key);
121
        $concept_key = preg_replace("/:/","",$concept_key);
122
        $concepts_array[$concept_key] = $concept_id;
123
      }
124
    }
125
    return $concepts_array;
126
}
127

    
128
?>
129

    
(3-3/5)