+}}
+
+if(! function_exists('sanitise_acl')) {
+function sanitise_acl(&$item) {
+ if(intval($item))
+ $item = '<' . intval(notags(trim($item))) . '>';
+ else
+ unset($item);
+}}
+
+if(! function_exists('load_config')) {
+function load_config($family) {
+ global $a;
+ $r = q("SELECT * FROM `config` WHERE `cat` = '%s'",
+ dbesc($family)
+ );
+ if(count($r)) {
+ foreach($r as $rr) {
+ $k = $rr['k'];
+ $a->config[$family][$k] = $rr['v'];
+ }
+ }
+}}
+
+
+if(! function_exists('get_config')) {
+function get_config($family, $key, $instore = false) {
+
+ global $a;
+ if(! $instore) {
+ if(isset($a->config[$family][$key])) {
+ if($a->config[$family][$key] == '!<unset>!')
+ return false;
+ return $a->config[$family][$key];
+ }
+ }
+ $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
+ dbesc($family),
+ dbesc($key)
+ );
+ if(count($ret)) {
+ $a->config[$family][$key] = $ret[0]['v'];
+ return $ret[0]['v'];
+ }
+ else {
+ $a->config[$family][$key] = '!<unset>!';
+ }
+ return false;
+}}
+
+if(! function_exists('set_config')) {
+function set_config($family,$key,$value) {
+
+ global $a;
+ $a->config[$family][$key] = $value;
+
+ if(get_config($family,$key,true) === false) {
+ $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
+ dbesc($family),
+ dbesc($key),
+ dbesc($value)
+ );
+ if($ret)
+ return $value;
+ return $ret;
+ }
+ $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
+ dbesc($value),
+ dbesc($family),
+ dbesc($key)
+ );
+ if($ret)
+ return $value;
+ return $ret;
+}}
+
+if(! function_exists('convert_xml_element_to_array')) {
+function convert_xml_element_to_array($xml_element, &$recursion_depth=0) {
+
+ // If we're getting too deep, bail out
+ if ($recursion_depth > 512) {
+ return(null);
+ }
+
+ if (!is_string($xml_element) &&
+ !is_array($xml_element) &&
+ (get_class($xml_element) == 'SimpleXMLElement')) {
+ $xml_element_copy = $xml_element;
+ $xml_element = get_object_vars($xml_element);
+ }
+
+ if (is_array($xml_element)) {
+ $result_array = array();
+ if (count($xml_element) <= 0) {
+ return (trim(strval($xml_element_copy)));
+ }
+
+ foreach($xml_element as $key=>$value) {
+
+ $recursion_depth++;
+ $result_array[strtolower($key)] =
+ convert_xml_element_to_array($value, $recursion_depth);
+ $recursion_depth--;
+ }
+ if ($recursion_depth == 0) {
+ $temp_array = $result_array;
+ $result_array = array(
+ strtolower($xml_element_copy->getName()) => $temp_array,
+ );
+ }
+
+ return ($result_array);
+
+ } else {
+ return (trim(strval($xml_element)));
+ }
+}}
+
+
+if(! function_exists('webfinger')) {
+function webfinger($s) {
+ if(! strstr($s,'@')) {
+ return $s;
+ }
+ $host = substr($s,strpos($s,'@') + 1);
+ $url = 'http://' . $host . '/.well-known/host-meta' ;
+ $xml = fetch_url($url);
+ if (! $xml)
+ return '';
+ $h = simplexml_load_string($xml);
+ $arr = convert_xml_element_to_array($h);
+
+ if(! isset($arr['xrd']['link']))
+ return '';
+
+ $link = $arr['xrd']['link'];
+ if(! isset($link[0]))
+ $links = array($link);
+ else
+ $links = $link;
+
+ foreach($links as $link)
+ if($link['@attributes']['rel'] && $link['@attributes']['rel'] == 'lrdd')
+ $tpl = $link['@attributes']['template'];
+ if((empty($tpl)) || (! strpos($tpl, '{uri}')))
+ return '';
+
+ $pxrd = str_replace('{uri}', urlencode('acct://'.$s), $tpl);
+
+ $xml = fetch_url($pxrd);
+ if (! $xml)
+ return '';
+ $h = simplexml_load_string($xml);
+ $arr = convert_xml_element_to_array($h);
+
+ if(! isset($arr['xrd']['link']))
+ return '';
+
+ $link = $arr['xrd']['link'];
+ if(! isset($link[0]))
+ $links = array($link);
+ else
+ $links = $link;
+
+ foreach($links as $link)
+ if($link['@attributes']['rel'] == NAMESPACE_DFRN)
+ return $link['@attributes']['href'];
+ return '';
+}}