2 require_once('../inc/always.php');
4 function logRequestHeaders() {
7 /** Log the request headers */
8 $lines = apache_request_headers();
9 dbg_error_log( "LOG ", "***************** Request Header ****************" );
10 dbg_error_log( "LOG ", "%s %s", $_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI'] );
11 foreach( $lines AS $k => $v ) {
12 if ( $k != 'Authorization' ||
(isset($c->dbg
['password']) && $c->dbg
['password'] ) )
13 dbg_error_log( "LOG headers", "-->%s: %s", $k, $v );
15 dbg_error_log( "LOG headers", "-->%s: %s", $k, 'Delicious tasty password eaten by debugging monster!' );
17 dbg_error_log( "LOG ", "******************** Request ********************" );
19 // Log the request in all it's gory detail.
20 $lines = preg_split( '#[\r\n]+#', $c->raw_post
);
21 foreach( $lines AS $v ) {
22 dbg_error_log( "LOG request", "-->%s", $v );
27 if ( !isset($c->raw_post
) ) $c->raw_post
= file_get_contents( 'php://input');
28 // if ( (isset($c->dbg['ALL']) && $c->dbg['ALL']) || (isset($c->dbg['request']) && $c->dbg['request']) )
31 function Respond( $message="", $content_type="application/json; charset=\"utf-8\"" ) {
33 @header
( "Content-type: ".$content_type );
35 $lines = headers_list();
36 dbg_error_log( "LOG ", "***************** Response Header ****************" );
37 foreach( $lines AS $v ) {
38 dbg_error_log( "LOG headers", "-->%s", $v );
40 dbg_error_log( "LOG ", "******************** Response ********************" );
41 // Log the request in all it's gory detail.
42 $lines = preg_split( '#[\r\n]+#', $message);
43 foreach( $lines AS $v ) {
44 dbg_error_log( "LOG response", "-->%s", $v );
47 header( "Content-Length: ".strlen($message) );
53 function getCreditor( $creditorcode ) {
54 $qry = new AwlQuery('SELECT * FROM creditor WHERE creditorcode=?',$creditorcode);
55 if ( $qry->Exec() && $qry->rows() > 0 ) {
56 $result = $qry->Fetch();
59 $result['name'] = sprintf('Creditor %d not found', $creditorcode);
64 function getDebtor( $code ) {
65 $qry = new AwlQuery('SELECT * FROM tenant WHERE tenantcode=?',$code);
66 if ( $qry->Exec() && $qry->rows() > 0 ) {
67 $result = $qry->Fetch();
70 $result['name'] = sprintf('Tenant %d not found', $code);
75 function getProperty( $code ) {
76 $qry = new AwlQuery('SELECT * FROM property WHERE propertycode=?',$code);
77 if ( $qry->Exec() && $qry->rows() > 0 ) {
78 $result = $qry->Fetch();
81 $result['name'] = sprintf('Property %d not found', $code);
86 function getCompany( $code ) {
87 $qry = new AwlQuery('SELECT * FROM company WHERE companycode=?',$code);
88 if ( $qry->Exec() && $qry->rows() > 0 ) {
89 $result = $qry->Fetch();
90 $result['name'] = $result['legalname'];
93 $result['name'] = sprintf('Company %d not found', $code);
98 function getProject( $code ) {
99 $qry = new AwlQuery('SELECT * FROM project WHERE projectcode=?',$code);
100 if ( $qry->Exec() && $qry->rows() > 0 ) {
101 $result = $qry->Fetch();
104 $result['name'] = sprintf('Project %d not found', $code);
109 function getAsset( $code ) {
110 $qry = new AwlQuery('SELECT * FROM fixedasset WHERE assetcode=?',$code);
111 if ( $qry->Exec() && $qry->rows() > 0 ) {
112 $result = $qry->Fetch();
113 $result['name'] = substr( $result['description'], 0, 50);
116 $result['name'] = sprintf('Asset %d not found', $code);
121 function getEntity( $code ) {
122 $entitytype = strtoupper(substr($code,0,1));
123 $entitycode = intval(substr($code,1));
124 switch( $entitytype ) {
125 case 'C': return getCreditor($entitycode);
126 case 'T': return getDebtor($entitycode);
127 case 'P': return getProperty($entitycode);
128 case 'J': return getProject($entitycode);
129 case 'L': return getCompany($entitycode);
130 case 'F': return getAsset($entitycode);
132 $result['name'] = sprintf('Entity %s not found', $code);
137 function getAccount( $code ) {
138 $qry = new AwlQuery('SELECT * FROM chartofaccount WHERE accountcode=?',$code);
139 if ( $qry->Exec() && $qry->rows() > 0 ) {
140 $result = $qry->Fetch();
143 $result['name'] = sprintf('Account %05.2lf not found', $code);
148 param_to_global('request','#([a-z0-9_-]*)#i');
149 param_to_global('code','#([a-z0-9_.-]*)#i');
151 if ( isset($request) ) {
153 case 'creditor': $result = getCreditor($code); break;
154 case 'tenant': $result = getDebtor($code); break;
155 case 'property': $result = getProperty($code); break;
156 case 'project': $result = getProject($code); break;
157 case 'company': $result = getCompany($code); break;
158 case 'asset': $result = getAsset($code); break;
159 case 'entity': $result = getEntity($code); break;
160 case 'account': $result = getAccount($code); break;
163 $result = sprintf("Request '%s' was not understood", $request);
167 $result = "Request was not understood";
169 Respond(json_encode($result));