source: subversion/applications/mobile/healthwhere/inc_edit.php @ 34887

Last change on this file since 34887 was 21241, checked in by avantman42, 10 years ago

Removed source column in OS-created postcode DB. No need for it, and should make DB file smaller
Now using OS postcode data (OS OpenData? Code-Point® Open)
Re-licensed (GPL v2) because PHPCoord is licensed under GPL v2

File size: 3.9 KB
Line 
1<?php
2/*
3Healthwhere, a web service to find local pharmacies and hospitals
4Copyright (C) 2009-2010 Russell Phillips (russ@phillipsuk.org)
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License along
17with this program; if not, write to the Free Software Foundation, Inc.,
1851 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19*/
20
21//Functions used when editing OSM data
22$osm_auth = array ("httpauth"=>"$osm_user:$osm_password");
23$waynode = $_GET ['waynode'];
24
25/*
26 * Create a changeset
27 * Parameter: changeset comment (optional)
28 * Returns changeset ID
29*/
30function osm_create_changeset ($comment = "") {
31        global $osm_auth, $osm_api_base, $debug_log;
32        //Default comment
33        if ($comment == "")
34                $comment = "Updates from " . SERVICE_NAME . " " . VERSION;
35
36        $xml = "<osm>\n<changeset>\n<tag k='created_by' " .
37                "v='" . SERVICE_NAME . " " . VERSION. "'/>\n" .
38                "<tag k='comment' v='$comment'/>\n</changeset>\n</osm>";
39
40        $url = $osm_api_base . "/changeset/create";
41        $info = "";
42
43        $cs = http_put_data ($url, $xml, $osm_auth, $info);
44        $out = print_r ($info, True);
45        file_put_contents ($debug_log, "\n------------------\n" .
46                date ("Y-m-d H:i:s") . "\tCreate changeset URL: $url\n", FILE_APPEND);
47
48        //Get final line as an integer
49        $aLines = explode ("\n", $cs);
50        $iLine = count ($aLines) -1;
51        file_put_contents ($debug_log, "Changeset ID: {$aLines [$iLine]}\n", FILE_APPEND);
52        //Save changeset ID to a cookie and return it
53        if (setcookie ('csID', $aLines [$iLine]) === False)
54                file_put_contents ($debug_log, "Could not set cookie csID. Value = {$aLines [$iLine]}\n", FILE_APPEND);
55        return (int) $aLines [$iLine];
56}
57
58/*
59 * Update a node
60 * Parameters:
61 * $id: ID of node to be updated
62 * $xml: XML string
63 * $csID: changeset ID
64 * Returns True if successful, False if not
65*/
66function osm_update_node ($id, $xml, $csID) {
67        global $osm_auth, $osm_api_base, $debug_log, $db_file;
68
69        file_put_contents ($debug_log, "\n------------------\n", FILE_APPEND);
70        file_put_contents ($debug_log, "Updating node $id\n", FILE_APPEND);
71
72        $cs = "changeset=\"$csID\"";
73        //Set changeset ID in XML string
74        $xml = ereg_replace ('changeset="[0-9]*"', $cs, $xml);
75        $url = "$osm_api_base/node/$id";
76        http_put_data ($url, $xml, $osm_auth, $info);
77        //Check for 409 error
78        if ($info ['response_code'] == 409) {
79                file_put_contents ($debug_log, "409 Error. Creating new changeset\n", FILE_APPEND);
80                //409 response code means changeset is closed. Create a new one and use that
81                $csid = osm_create_changeset ();
82                $_COOKIE ['csID'] = $csid;
83                $cs = "changeset=\"$csid\"";
84                //Set changeset ID in XML string
85                $xml = ereg_replace ('changeset="[0-9]*"', $cs, $xml);
86                http_put_data ($url, $xml, $osm_auth, $info);
87        }
88        //Log debugging info
89        $out = print_r ($info, True);
90        file_put_contents ($debug_log, "Update node URL: $url\n", FILE_APPEND);
91        file_put_contents ($debug_log, "XML:\n$xml\n", FILE_APPEND);
92        file_put_contents ($debug_log, "Result:\n$out\n", FILE_APPEND);
93        if ($info ['response_code'] >= 400 && $info ['response_code'] <= 499)
94                return False;
95        else
96                return True;
97}
98
99/*
100 * Close a changeset
101 * Parameter: Changeset ID
102 * Returns nothing
103*/
104function osm_close_changeset ($id) {
105        global $osm_auth, $osm_api_base, $debug_log;
106
107        $url = "$osm_api_base/changeset/$id/close";
108        http_put_data ($url, "", $osm_auth, $info);
109        $out = print_r ($info, True);
110        file_put_contents ($debug_log, "Close changeset URL: $url\n", FILE_APPEND);
111        file_put_contents ($debug_log, "Result:\n$out\n", FILE_APPEND);
112}
113?>
Note: See TracBrowser for help on using the repository browser.