root/geowiki/shared.php

Revision 9, 4.1 kB (checked in by mikel, 3 years ago)

geowiki initial checkin

Line 
1 <?php
2
3 //returns position of last occurrence of $needle in $haystack, left from $offset
4 //modified gordon's proposal on http://in2.php.net/manual/en/function.strrpos.php
5 //$offset <= 0 will search from the end of the string (0 from last character)
6 function searchBack($haystack, $needle, $offset=0) {
7     $strrpos = false;
8     if (is_string($haystack) && is_string($needle) && is_numeric($offset)) {
9         $strlen = strlen($haystack);
10         if ($offset <= 0)
11             $offset = $strlen + $offset;
12
13         $strpos = strpos(strrev(substr($haystack, 0, $offset)), strrev($needle));
14         if (is_numeric($strpos)) {
15             $strrpos = $offset - $strpos - strlen($needle);
16         }
17     }
18     return $strrpos;
19 }
20
21 //log an $action on $item into $logFile
22 function changeLog($logFile, $action, $item) {
23     $timeStamp = date('Y-m-d G:i:s T');
24     $logString = "<history:$action><date>$timeStamp</date>$item</history:$action>\n";
25     $fh = fopen($logFile, 'a');
26     fwrite($fh, $logString);
27     fclose($fh);
28 }
29
30 //makes an array out of $string and trims it
31 function getTrimmedArray($separator, $string) {
32     $tmp = explode($separator, $string);
33     for ($i = 0; $i < count($tmp); $i++)
34         $tmp[$i] = trim($tmp[$i]);
35     return $tmp;
36 }
37
38 //return the content of $xml between the strings $start and $end
39 function getContent($xml, $startTag, $endTag) {
40     $content = "";
41     $startPos = strpos($xml, $startTag);
42     if ($startPos === false)
43         return $content;
44         
45     $endPos = strpos($xml, $endTag, $startPos);
46     if ($endPos === false)
47         return $content;
48     
49     $startPos += strlen($startTag);
50     $content = substr($xml, $startPos, $endPos - $startPos);
51 //    echo $startPos, ":", $endPos, ":", ($endPos - $startPos), strlen($content);
52     return $content;
53 }
54
55 function getTagContent($xml, $tag) {
56     return getContent($xml, "<$tag>", "</$tag>");
57 }
58
59 //$item: xml string, containing the data for one item
60 //returns a list with the entries
61 function xml2fields($item) {
62     $url = getTagContent($item, "link");
63     $title = getTagContent($item, "title");
64     $desc = getTagContent($item, "description");
65     $desc = substr($desc, 9);
66     $desc = substr($desc, 0, strlen($desc) - 3);
67     $user = getTagContent($item, "author");
68     
69     $geoType = "point";
70     $coords = "";
71
72     $line = getTagContent($item, "geo:line");
73     $poly = getTagContent($item, "geo:polygon");
74     if ($line != "") {
75         $geoType = "line";
76         $coords = $line;
77 //        $coords = str_replace(" ", "\n", $line);       
78     }
79     else if ($poly != "") {
80         $geoType = "polygon";
81         $coords = $poly;
82 //        $coords = str_replace(" ", "\n", $poly);
83     }
84     else {
85         $lat = getTagContent($item, "geo:lat");
86         $lon = getTagContent($item, "geo:long");
87         if ($lat != "" && $lon != "")
88             $coords = $lat . "," . $lon;
89     }
90
91     $imgurl = getTagContent($item, "photo:thumbnail");
92
93     $tags = "";
94     $categories = str_replace("</category><category>", ", ", $item);
95     $tags = getTagContent($categories, "category");
96     
97     return array($title, $url, $desc, $imgurl, $tags, $coords, $geoType, $user);
98 }
99
100 //builds the xml item-tag from the given values
101 function fields2xml($uid,$title,$url,$description,$imgurl,$tags,$coords,$geoType,$user) {
102     $item = '';
103     if (empty($coords))
104         return $item;
105     if (empty($file))
106         $file = "rss.xml";
107     
108     //<category>
109     $catTag = "";
110     $tmp = explode(",", $tags);
111     foreach($tmp as $tag) {
112         $catTag .= "<category>" . trim($tag) . "</category>";
113     }
114
115     //<photo:thumbnail>
116     $imgTag = "";
117     if (!empty($imgurl)) {
118         $imgTag = "<photo:thumbnail>" . $imgurl . "</photo:thumbnail>";
119     }
120     
121     //<guid>
122     $uidTag = "";
123     if (!empty($uid)) $uidTag = "<guid>$uid</guid>";
124
125     //<geo:..> (coordinates)
126     $coordString = trim($coords);
127
128     if ($geoType == "point") {
129         $tmp = explode(",", $coordString);
130         $coordTag = "<geo:lat>$tmp[0]</geo:lat><geo:long>$tmp[1]</geo:long>";
131     }
132     else if ($geoType == "line")
133         $coordTag = "<geo:line>$coordString</geo:line>";
134     else if ($geoType == "polygon")
135         $coordTag = "<geo:polygon>$coordString</geo:polygon>";
136
137     //construct rss Tag
138     $item = "<item>" . $uidTag . "<title>$title</title><link>$url</link><description><![CDATA[" . $description . "]]></description><author>$user</author>" . $coordTag . $catTag . $imgTag . "</item>";
139
140     return $item;
141 }
142
143 //echo getTagContent("<ab>lab</ab>", "ab");
144 //echo fields2xml('$uid','$title','$url','$description','$imgurl','$tags','1,2 3,4 5,6','polygon','$user');
145 ?>
146
Note: See TracBrowser for help on using the browser.