Skip to content

Commit 551d061

Browse files
initial commit
0 parents  commit 551d061

15 files changed

+1427
-0
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
lib_emplacify
2+
--
3+
4+
HEY LOOK! THIS ISN'T QUITE DONE YET!! REALLY. THERE ARE STILL BUGS.
5+
6+
Start here: [http://www.aaronland.info/weblog/2010/12/06/urmum/#enplacify](http://www.aaronland.info/weblog/2010/12/06/urmum/#enplacify)
7+
8+
lib_emplacify is *designed* to be used in conjuction with [flamework](https://github.com/straup/flamework) (as of this writing, specifically my fork). Although it probably doesn't need to be part of core flamework I've already found uses for it in two other projects that [hold hands](https://github.com/Citytracking/dotspotting/blob/master/README.FLAMEWORK.md) with flamework so now it's a little bundle of libraries and functions and can check out and slot in where necessary.
9+
10+
If you don't want to bother using flamework though I've included just enough real and mock flamework code so that this code will run on its own. Take a look at the `test.php` file for details.
11+
12+
See also:
13+
--
14+
15+
* [https://github.com/mncaudill/flickr-machinetag-geo](https://github.com/mncaudill/flickr-machinetag-geo)

flamework/lib_cache.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
#
4+
# $Id$
5+
#
6+
7+
$GLOBALS['cache_local'] = array();
8+
$GLOBALS['cache_remote_conns'] = array();
9+
10+
#################################################################
11+
12+
function cache_get($cache_key){
13+
14+
if ($GLOBALS['cfg']['cache_force_refresh']){
15+
16+
return array(
17+
'ok' => 0,
18+
'error' => 'force refresh'
19+
);
20+
}
21+
22+
$cache_key = _cache_prepare_cache_key($cache_key);
23+
log_notice("cache", "fetch cache key {$cache_key}");
24+
25+
if (isset($GLOBALS['cache_local'][$cache_key])){
26+
27+
return array(
28+
'ok' => 1,
29+
'cache' => 'local',
30+
'cache_key' => $cache_key,
31+
'data' => $GLOBALS['cache_local'][$cache_key],
32+
);
33+
}
34+
35+
$remote_rsp = _cache_do_remote('get', $cache_key);
36+
37+
return $remote_rsp;
38+
}
39+
40+
#################################################################
41+
42+
function cache_set($cache_key, $data, $store_locally=0){
43+
44+
$cache_key = _cache_prepare_cache_key($cache_key);
45+
log_notice("cache", "set cache key {$cache_key}");
46+
47+
if ($store_locally){
48+
$GLOBALS['cache_local'][$cache_key] = $data;
49+
}
50+
51+
$remote_rsp = _cache_do_remote('set', $cache_key, $data);
52+
53+
return array(
54+
'ok' => 1
55+
);
56+
}
57+
58+
#################################################################
59+
60+
function cache_unset($cache_key){
61+
62+
$cache_key = _cache_prepare_cache_key($cache_key);
63+
log_notice("cache", "unset cache key {$cache_key}");
64+
65+
if (isset($GLOBALS['cache_local'][$cache_key])){
66+
unset($GLOBALS['cache_local'][$cache_key]);
67+
}
68+
69+
$remote_rsp = _cache_do_remote('unset', $cache_key);
70+
71+
return array(
72+
'ok' => 1
73+
);
74+
}
75+
76+
#################################################################
77+
78+
function _cache_prepare_cache_key($key){
79+
80+
if (! $GLOBALS['cfg']['cache_prefix']){
81+
return $key;
82+
}
83+
84+
return "{$GLOBALS['cfg']['cache_prefix']}_{$key}";
85+
}
86+
87+
#################################################################
88+
89+
function _cache_do_remote($method, $key, $data=null){
90+
91+
$engine = trim($GLOBALS['cfg']['cache_remote_engine']);
92+
93+
if (! $engine){
94+
return array( 'ok' => 0, 'error' => 'Remote caching is not enabled' );
95+
}
96+
97+
$remote_lib = "cache_{$engine}";
98+
$remote_func = "cache_{$engine}_{$method}";
99+
100+
$args = ($data) ? array($key, $data) : array($key);
101+
102+
loadlib($remote_lib);
103+
$rsp = call_user_func_array($remote_func, $args);
104+
105+
$rsp['cache_key'] = $key;
106+
$rsp['cache'] = $engine;
107+
108+
return $rsp;
109+
}
110+
111+
#################################################################
112+
?>

flamework/lib_geo_geocode.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
#
4+
# $Id$
5+
#
6+
7+
#################################################################
8+
9+
loadlib("http");
10+
11+
#################################################################
12+
13+
function geo_geocode_service_map($string_keys=0){
14+
15+
# 0 means 'not geocoded'
16+
17+
$map = array(
18+
1 => 'yahoo',
19+
);
20+
21+
if ($string_keys){
22+
$map = array_flip($map);
23+
}
24+
25+
return $map;
26+
}
27+
28+
#################################################################
29+
30+
function geo_geocode_string($string){
31+
32+
$service = $GLOBALS['cfg']['geo_geocoding_service'];
33+
$func = "geo_geocode_{$service}";
34+
35+
if ((! $service) || (! is_callable($func))){
36+
37+
return array(
38+
'ok' => 0,
39+
'error' => 'Unknown or undefined service',
40+
);
41+
}
42+
43+
$rsp = call_user_func($func, $string);
44+
45+
$map = geo_geocode_service_map('string keys');
46+
$rsp['service_id'] = $map[ $service ];
47+
48+
return $rsp;
49+
}
50+
51+
#################################################################
52+
53+
function geo_geocode_yahoo($string){
54+
55+
$api_key = $GLOBALS['cfg']['geo_geocoding_yahoo_apikey'];
56+
57+
$url = 'http://where.yahooapis.com/geocode?q='.urlencode($string).'&flags=j&appid='.urlencode($api_key);
58+
59+
$http_rsp = http_get($url);
60+
61+
$rsp = array(
62+
'ok' => 0,
63+
'error' => 'unknown error'
64+
);
65+
66+
if ($http_rsp['ok']){
67+
68+
# pass in a 1 to disable 'shit-mode'
69+
$geocode_response = json_decode($http_rsp['body'], 1);
70+
71+
if ($geocode_response['ResultSet']['Found'] == 1){
72+
73+
$results = $geocode_response['ResultSet']['Results'][0];
74+
75+
$rsp['ok'] = 1;
76+
$rsp['error'] = null;
77+
$rsp['latitude'] = (float)$results['latitude'];
78+
$rsp['longitude'] = (float)$results['longitude'];
79+
$rsp['extras']['woeid'] = (float)$results['woeid'];
80+
} else {
81+
$rsp['error'] = 'could not geocode';
82+
}
83+
84+
}
85+
86+
return $rsp;
87+
}
88+
89+
?>

0 commit comments

Comments
 (0)