/
utils.php
204 lines (177 loc) · 5.05 KB
/
utils.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
/**
* @file utils.php
*
*/
//------------------------------------------------------------------------------
// Convert codes such as % to corresponding character (e.g., '%')
function convert_html_hash_codes($text)
{
$a = preg_split('/(&#[0-9]+;)/', $text, 0, PREG_SPLIT_DELIM_CAPTURE);
$str = '';
foreach ($a as $part)
{
if (preg_match('/&#(?<c>[0-9]+);/', $part, $match))
{
$part = chr($match['c']);
}
$str .= $part;
}
return $str;
}
//------------------------------------------------------------------------------
/**
* @brief Convert a decimal latitude or longitude to deg min sec format
*
* @param decimal Latitude or longitude as a decimal number
*
* @return Degree format
*/
function decimal_to_degrees($decimal)
{
$decimal = abs($decimal);
$degrees = floor($decimal);
$minutes = floor(60 * ($decimal - $degrees));
$seconds = round(60 * (60 * ($decimal - $degrees) - $minutes));
// °
$result = $degrees . '°' . $minutes . '’' . $seconds . '”';
return $result;
}
//------------------------------------------------------------------------------
function format_decimal_latlon($latitude, $longitude)
{
$html = decimal_to_degrees($latitude);
$html .= ($latitude < 0.0 ? 'S' : 'N');
$html .= ' ';
$html .= decimal_to_degrees($longitude);
$html .= ($latitude < 0.0 ? 'W' : 'E');
return $html;
}
//------------------------------------------------------------------------------
// from http://phpsense.com/php/php-word-splitter.html
function word_split($str,$words=15) {
$arr = preg_split("/[\s]+/", $str,$words+1);
$arr = array_slice($arr,0,$words);
return join(' ',$arr);
}
//------------------------------------------------------------------------------
/**
* @brief Format an arbitrary date as YYYY-MM-DD
*
* @param date A string representation of a date
*
* @return Date in YYYY-MM-DD format
*/
function format_date($date)
{
$formatted_date = '';
// Dates like 2006-8-7T15:47:36.000Z break PHP strtotime, so
// replace the T with a space.
$date = preg_replace('/-([0-9]{1,2})T([0-9]{1,2}):/', '-$1 $2:', $date);
if (PHP_VERSION < 5.0)
{
if (-1 != strtotime($date))
{
$formatted_date = date("Y-m-d", strtotime($date));
}
}
else
{
if (false != strtotime($date))
{
$formatted_date = date("Y-m-d", strtotime($date));
}
}
return $formatted_date;
}
//------------------------------------------------------------------------------
/**
* @brief Extract the year from a date
*
* @param date A string representation of a date in YYYY-MM-DD format
* @return Year in YYYY format
*/
function year_from_date($date)
{
$year = 'YYYY';
$matches = array();
if (preg_match("/([0-9]{4})(\-[0-9]{1,2})?(\-[0-9]{1,2})?/", $date, $matches))
{
$year = $matches[1];
}
return $year;
}
//------------------------------------------------------------------------------
/**
* @brief Clone function for PHP 4
*
* Clone function for PHP4. because PHP5 handles objects differently from
* PHP 4 it is possible to badly mangle objects. PHP4 passes a copy, PHP 5
* passes a reference. php4_clone makes it possibel to ensure a copy is being
* passed in both versions.
*
* Borrowed from http://www.hat.net/geeky/php_tricks_-_php_5_clone_in_php4
*
*/
function php4_clone($object)
{
if (version_compare(phpversion(), '5.0') < 0)
{
return $object;
}
else
{
return @clone($object);
}
}
//------------------------------------------------------------------------------
/**
* @brief Log service calls to disk
*
* @param filename Log file name
* @param msg Message to write to log file
*/
function logToFile($filename, $msg)
{
$fd = fopen($filename, "a");
$str = '[' . date("Y/m/d h:i:s", mktime()) . '] ' . $msg;
fwrite($fd, $str . "\n");
fclose($fd);
}
//------------------------------------------------------------------------------
/**
* @brief Store metadata in disk cache
*
* Store original metadata in disk cache, mainly for debugging. Typically the metadata is
* in XML format, such as the response from a DiGIR provider, OAI interface, or CrossRef OpenURL
* service. The metadata is stored in a file <b>namespace/id.extension</b>. Characters such
* as '/' and ':' in the identifier are replaced by '-' to make the identifier safe to
* use as a file name.
*
* @param namespace Namespace part of GUID
* @param id Identifier part GUID
* @param data Metadata
* @param extension Extension of file (default 'xml')
*
*/
function storeInCache ($namespace, $id, $data, $extension = 'xml')
{
global $config;
$cache_namespace = $config['cache_dir']. "/" . $namespace;
// make id safe as a filename
$id = preg_replace('/\//', '-', $id);
$id = preg_replace('/:/', '-', $id);
$cache_filename = $cache_namespace . "/" . $id . '.' . $extension;
// Ensure cache subfolder exists for this authority
if (!file_exists($cache_namespace))
{
$oldumask = umask(0);
mkdir($cache_namespace, 0777);
umask($oldumask);
}
// Store data in cache
$cache_file = @fopen($cache_filename, "w+") or die("could't open file --\"$cache_filename\"");
@fwrite($cache_file, $data);
fclose($cache_file);
}
?>