Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1198 lines
31 KiB

12 years ago
<?php
set_time_limit(0);
define ( 'BUILD_ID', 1011 );
12 years ago
define ( 'DFRN_PROTOCOL_VERSION', '2.0' );
12 years ago
12 years ago
define ( 'EOL', "<br />\r\n" );
define ( 'ATOM_TIME', 'Y-m-d\TH:i:s\Z' );
12 years ago
// registration policy
define ( 'REGISTER_CLOSED', 0 );
define ( 'REGISTER_APPROVE', 1 );
define ( 'REGISTER_OPEN', 2 );
12 years ago
// relationship types
12 years ago
12 years ago
define ( 'REL_VIP', 1);
define ( 'REL_FAN', 2);
define ( 'REL_BUD', 3);
// page/profile types
// PAGE_NORMAL is a typical personal profile account
// PAGE_SOAPBOX automatically approves all friend requests as REL_FAN, (readonly)
// PAGE_COMMUNITY automatically approves all friend requests as REL_FAN, but with
// write access to wall and comments (no email and not included in page owner's ACL lists)
// PAGE_FREELOVE automatically approves all friend requests as full friends (REL_BUD).
define ( 'PAGE_NORMAL', 0 );
define ( 'PAGE_SOAPBOX', 1 );
define ( 'PAGE_COMMUNITY', 2 );
define ( 'PAGE_FREELOVE', 3 );
12 years ago
// Maximum number of "people who like (or don't like) this"
// that we will list by name
12 years ago
define ( 'MAX_LIKERS', 75);
12 years ago
// email notification options
12 years ago
define ( 'NOTIFY_INTRO', 0x0001 );
define ( 'NOTIFY_CONFIRM', 0x0002 );
define ( 'NOTIFY_WALL', 0x0004 );
define ( 'NOTIFY_COMMENT', 0x0008 );
define ( 'NOTIFY_MAIL', 0x0010 );
12 years ago
// various namespaces we may need to parse
12 years ago
define ( 'NAMESPACE_DFRN' , 'http://purl.org/macgirvin/dfrn/1.0' );
define ( 'NAMESPACE_THREAD' , 'http://purl.org/syndication/thread/1.0' );
define ( 'NAMESPACE_TOMB' , 'http://purl.org/atompub/tombstones/1.0' );
define ( 'NAMESPACE_ACTIVITY', 'http://activitystrea.ms/spec/1.0/' );
define ( 'NAMESPACE_ACTIVITY_SCHEMA', 'http://activitystrea.ms/schema/1.0/');
define ( 'NAMESPACE_SALMON_ME', 'http://salmon-protocol.org/ns/magic-env');
define ( 'NAMESPACE_OSTATUSSUB', 'http://ostatus.org/schema/1.0/subscribe');
define ( 'NAMESPACE_GEORSS', 'http://www.georss.org/georss');
12 years ago
12 years ago
// activity stream defines
define ( 'ACTIVITY_LIKE', NAMESPACE_ACTIVITY_SCHEMA . 'like' );
12 years ago
define ( 'ACTIVITY_DISLIKE', NAMESPACE_DFRN . '/dislike' );
12 years ago
define ( 'ACTIVITY_OBJ_HEART', NAMESPACE_DFRN . '/heart' );
define ( 'ACTIVITY_FRIEND', NAMESPACE_ACTIVITY_SCHEMA . 'make-friend' );
define ( 'ACTIVITY_POST', NAMESPACE_ACTIVITY_SCHEMA . 'post' );
define ( 'ACTIVITY_UPDATE', NAMESPACE_ACTIVITY_SCHEMA . 'update' );
define ( 'ACTIVITY_OBJ_COMMENT', NAMESPACE_ACTIVITY_SCHEMA . 'comment' );
define ( 'ACTIVITY_OBJ_NOTE', NAMESPACE_ACTIVITY_SCHEMA . 'note' );
define ( 'ACTIVITY_OBJ_PERSON', NAMESPACE_ACTIVITY_SCHEMA . 'person' );
define ( 'ACTIVITY_OBJ_PHOTO', NAMESPACE_ACTIVITY_SCHEMA . 'photo' );
define ( 'ACTIVITY_OBJ_P_PHOTO', NAMESPACE_ACTIVITY_SCHEMA . 'profile-photo' );
define ( 'ACTIVITY_OBJ_ALBUM', NAMESPACE_ACTIVITY_SCHEMA . 'photo-album' );
12 years ago
// item weight for query ordering
define ( 'GRAVITY_PARENT', 0);
define ( 'GRAVITY_LIKE', 3);
define ( 'GRAVITY_COMMENT', 6);
12 years ago
// Our main application structure for the life of this page
// Primarily deals with the URL that got us here
// and tries to make some sense of it, and
// stores our page contents and config storage
// and anything else that might need to be passed around
// before we spit the page out.
12 years ago
if(! class_exists('App')) {
class App {
public $module_loaded = false;
public $config;
public $page;
public $profile;
public $user;
public $cid;
public $contact;
12 years ago
public $content;
public $data;
12 years ago
public $error = false;
public $cmd;
public $argv;
public $argc;
public $module;
12 years ago
public $pager;
12 years ago
public $strings;
public $path;
public $interactive = true;
12 years ago
private $scheme;
private $hostname;
private $baseurl;
12 years ago
private $db;
private $curl_code;
private $curl_headers;
12 years ago
function __construct() {
$this->config = array();
$this->page = array();
12 years ago
$this->pager= array();
12 years ago
$this->scheme = ((isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'])) ? 'https' : 'http' );
$this->hostname = $_SERVER['SERVER_NAME'];
set_include_path("include/$this->hostname" . PATH_SEPARATOR . 'include' . PATH_SEPARATOR . '.' );
if(substr($_SERVER['QUERY_STRING'],0,2) == "q=")
12 years ago
$_SERVER['QUERY_STRING'] = substr($_SERVER['QUERY_STRING'],2);
$this->cmd = trim($_GET['q'],'/');
12 years ago
12 years ago
$this->argv = explode('/',$this->cmd);
$this->argc = count($this->argv);
if((array_key_exists('0',$this->argv)) && strlen($this->argv[0])) {
$this->module = $this->argv[0];
}
else {
$this->module = 'home';
}
if($this->cmd === '.well-known/host-meta')
require_once('include/hostxrd.php');
12 years ago
$this->pager['page'] = ((x($_GET,'page')) ? $_GET['page'] : 1);
$this->pager['itemspage'] = 50;
$this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
$this->pager['total'] = 0;
12 years ago
}
function get_baseurl($ssl = false) {
if(strlen($this->baseurl))
return $this->baseurl;
$this->baseurl = (($ssl) ? 'https' : $this->scheme) . "://" . $this->hostname . ((isset($this->path) && strlen($this->path)) ? '/' . $this->path : '' );
return $this->baseurl;
}
function set_baseurl($url) {
$this->baseurl = $url;
$this->hostname = basename($url);
12 years ago
}
function get_hostname() {
return $this->hostname;
}
function set_hostname($h) {
$this->hostname = $h;
}
12 years ago
function set_path($p) {
$this->path = trim(trim($p),'/');
12 years ago
}
function get_path() {
return $this->path;
}
12 years ago
function set_pager_total($n) {
$this->pager['total'] = intval($n);
}
12 years ago
function set_pager_itemspage($n) {
$this->pager['itemspage'] = intval($n);
$this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
}
12 years ago
function init_pagehead() {
$tpl = load_view_file("view/head.tpl");
$this->page['htmlhead'] = replace_macros($tpl,array(
'$baseurl' => $this->get_baseurl()
));
12 years ago
}
function set_curl_code($code) {
$this->curl_code = $code;
}
function get_curl_code() {
return $this->curl_code;
}
function set_curl_headers($headers) {
$this->curl_headers = $headers;
}
function get_curl_headers() {
return $this->curl_headers;
}
12 years ago
}}
// retrieve the App structure
// useful in functions which require it but don't get it passed to them
if(! function_exists('get_app')) {
function get_app() {
global $a;
return $a;
}};
12 years ago
12 years ago
// Multi-purpose function to check variable state.
// Usage: x($var) or $x($array,'key')
// returns false if variable/key is not set
// if variable is set, returns 1 if has 'non-zero' value, otherwise returns 0.
// e.g. x('') or x(0) returns 0;
12 years ago
if(! function_exists('x')) {
function x($s,$k = NULL) {
if($k != NULL) {
if((is_array($s)) && (array_key_exists($k,$s))) {
if($s[$k])
return (int) 1;
return (int) 0;
}
return false;
}
else {
if(isset($s)) {
if($s) {
return (int) 1;
}
return (int) 0;
}
return false;
}
}}
12 years ago
// called from db initialisation if db is dead.
12 years ago
if(! function_exists('system_unavailable')) {
function system_unavailable() {
include('system_unavailable.php');
killme();
}}
12 years ago
// Primarily involved with database upgrade, but also sets the
// base url for use in cmdline programs which don't have
// $_SERVER variables.
if(! function_exists('check_config')) {
function check_config(&$a) {
load_config('system');
$build = get_config('system','build');
if(! x($build))
$build = set_config('system','build',BUILD_ID);
$url = get_config('system','url');
if(! x($url))
$url = set_config('system','url',$a->get_baseurl());
if($build != BUILD_ID) {
$stored = intval($build);
$current = intval(BUILD_ID);
if(($stored < $current) && file_exists('update.php')) {
// We're reporting a different version than what is currently installed.
// Run any existing update scripts to bring the database up to current.
require_once('update.php');
12 years ago
for($x = $stored; $x < $current; $x ++) {
if(function_exists('update_' . $x)) {
$func = 'update_' . $x;
$func($a);
}
}
set_config('system','build', BUILD_ID);
}
}
return;
}}
12 years ago
// This is our template processor.
// $s is the string requiring macro substitution.
// $r is an array of key value pairs (search => replace)
// returns substituted string.
// WARNING: this is pretty basic, and doesn't properly handle search strings that are substrings of each other.
// For instance if 'test' => "foo" and 'testing' => "bar", testing could become either bar or fooing,
// depending on the order in which they were declared in the array.
12 years ago
if(! function_exists('replace_macros')) {
function replace_macros($s,$r) {
$search = array();
$replace = array();
if(is_array($r) && count($r)) {
foreach ($r as $k => $v ) {
$search[] = $k;
$replace[] = $v;
}
}
return str_replace($search,$replace,$s);
}}
12 years ago
// load string translation table for alternate language
12 years ago
if(! function_exists('load_translation_table')) {
12 years ago
function load_translation_table($lang) {
global $a;
if(file_exists("view/$lang/strings.php"))
include("view/$lang/strings.php");
}}
12 years ago
12 years ago
// translate string if translation exists
if(! function_exists('t')) {
12 years ago
function t($s) {
$a = get_app();
12 years ago
if($a->strings[$s])
return $a->strings[$s];
return $s;
}}
12 years ago
12 years ago
// curl wrapper. If binary flag is true, return binary
// results.
12 years ago
if(! function_exists('fetch_url')) {
function fetch_url($url,$binary = false, &$redirects = 0) {
12 years ago
$ch = curl_init($url);
if(($redirects > 8) || (! $ch))
return false;
12 years ago
curl_setopt($ch, CURLOPT_HEADER, false);
12 years ago
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
// by default we will allow self-signed certs
// but you can override this
$check_cert = get_config('system','verifyssl');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (($check_cert) ? true : false));
$prx = get_config('system','proxy');
if(strlen($prx)) {
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_PROXY, $prx);
$prxusr = get_config('system','proxyuser');
if(strlen($prxusr))
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
}
12 years ago
if($binary)
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$s = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$header = substr($s,0,strpos($s,"\r\n\r\n"));
if($http_code == 301 || $http_code == 302 || $http_code == 303) {
$matches = array();
preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
$url = trim(array_pop($matches));
$url_parsed = parse_url($url);
if (isset($url_parsed)) {
$redirects++;
return fetch_url($url,$binary,$redirects);
}
}
$a = get_app();
$a->set_curl_code($http_code);
$body = substr($s,strlen($header)+4);
$a->set_curl_headers($header);
12 years ago
curl_close($ch);
return($s);
12 years ago
}}
12 years ago
// post request to $url. $params is an array of post variables.
12 years ago
if(! function_exists('post_url')) {
function post_url($url,$params, &$redirects = 0) {
12 years ago
$ch = curl_init($url);
if(($redirects > 8) || (! $ch))
return false;
12 years ago
curl_setopt($ch, CURLOPT_HEADER, false);
12 years ago
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
$check_cert = get_config('system','verifyssl');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (($check_cert) ? true : false));
$prx = get_config('system','proxy');
if(strlen($prx)) {
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_PROXY, $prx);
$prxusr = get_config('system','proxyuser');
if(strlen($prxusr))
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
}
12 years ago
$s = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$header = substr($s,0,strpos($s,"\r\n\r\n"));
if($http_code == 301 || $http_code == 302 || $http_code == 303) {
$matches = array();
preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
$url = trim(array_pop($matches));
$url_parsed = parse_url($url);
if (isset($url_parsed)) {
$redirects++;
return post_url($url,$binary,$redirects);
}
}
$a = get_app();
$a->set_curl_code($http_code);
$body = substr($s,strlen($header)+4);
$a->set_curl_headers($header);
12 years ago
curl_close($ch);
return($s);
12 years ago
}}
12 years ago
// random hash, 64 chars
12 years ago
if(! function_exists('random_string')) {
function random_string() {
return(hash('sha256',uniqid(rand(),true)));
}}
12 years ago
// This is our primary input filter. The high bit hack only involved some old
// IE browser, forget which.
// Use this on any text input where angle chars are not valid or permitted
// They will be replaced with safer brackets. This may be filtered further
// if these are not allowed either.
12 years ago
if(! function_exists('notags')) {
function notags($string) {
// protect against :<> with high-bit set
return(str_replace(array("<",">","\xBA","\xBC","\xBE"), array('[',']','','',''), $string));
}}
12 years ago
// use this on "body" or "content" input where angle chars shouldn't be removed,
// and allow them to be safely displayed.
12 years ago
if(! function_exists('escape_tags')) {
function escape_tags($string) {
12 years ago
return(htmlspecialchars($string));
12 years ago
}}
12 years ago
// wrapper for adding a login box. If $register == true provide a registration
// link. This will most always depend on the value of $a->config['register_policy'].
12 years ago
// returns the complete html for inserting into the page
12 years ago
12 years ago
if(! function_exists('login')) {
function login($register = false) {
$o = "";
$register_html = (($register) ? load_view_file("view/register-link.tpl") : "");
12 years ago
if(x($_SESSION,'authenticated')) {
$o = load_view_file("view/logout.tpl");
12 years ago
}
else {
$o = load_view_file("view/login.tpl");
12 years ago
$o = replace_macros($o,array('$register_html' => $register_html ));
}
return $o;
}}
12 years ago
// generate a string that's random, but usually pronounceable.
// used to generate initial passwords
12 years ago
if(! function_exists('autoname')) {
function autoname($len) {
$vowels = array('a','a','ai','au','e','e','e','ee','ea','i','ie','o','ou','u');
if(mt_rand(0,5) == 4)
$vowels[] = 'y';
$cons = array(
'b','bl','br',
'c','ch','cl','cr',
'd','dr',
'f','fl','fr',
'g','gh','gl','gr',
'h',
'j',
'k','kh','kl','kr',
'l',
'm',
'n',
'p','ph','pl','pr',
'qu',
'r','rh',
's','sc','sh','sm','sp','st',
't','th','tr',
'v',
'w','wh',
'x',
'z','zh'
);
$midcons = array('ck','ct','gn','ld','lf','lm','lt','mb','mm', 'mn','mp',
'nd','ng','nk','nt','rn','rp','rt');
$noend = array('bl', 'br', 'cl','cr','dr','fl','fr','gl','gr',
'kh', 'kl','kr','mn','pl','pr','rh','tr','qu','wh');
$start = mt_rand(0,2);
if($start == 0)
$table = $vowels;
else
$table = $cons;
$word = '';
for ($x = 0; $x < $len; $x ++) {
$r = mt_rand(0,count($table) - 1);
$word .= $table[$r];
if($table == $vowels)
$table = array_merge($cons,$midcons);
else
$table = $vowels;
}
$word = substr($word,0,$len);
foreach($noend as $noe) {
if((strlen($word) > 2) && (substr($word,-2) == $noe)) {
$word = substr($word,0,-1);
break;
}
}
if(substr($word,-1) == 'q')
$word = substr($word,0,-1);
return $word;
}}
12 years ago
// Used to end the current process, after saving session state.
12 years ago
if(! function_exists('killme')) {
function killme() {
session_write_close();
exit;
}}
12 years ago
// redirect to another URL and terminate this process.
12 years ago
if(! function_exists('goaway')) {
function goaway($s) {
header("Location: $s");
killme();
}}
12 years ago
// Generic XML return
// Outputs a basic dfrn XML status structure to STDOUT, with a <status> variable
// of $st and an optional text <message> of $message and terminates the current process.
12 years ago
if(! function_exists('xml_status')) {
function xml_status($st, $message = '') {
if(strlen($message))
$xml_message = "\t<message>" . xmlify($message) . "</message>\r\n";
header( "Content-type: text/xml" );
12 years ago
echo '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
echo "<result>\r\n\t<status>$st</status>\r\n$xml_message</result>\r\n";
12 years ago
killme();
}}
12 years ago
// Returns the uid of locally logged in user or false.
12 years ago
12 years ago
if(! function_exists('local_user')) {
function local_user() {
if((x($_SESSION,'authenticated')) && (x($_SESSION,'uid')))
return intval($_SESSION['uid']);
12 years ago
return false;
}}
12 years ago
// Returns contact id of authenticated site visitor or false
12 years ago
if(! function_exists('remote_user')) {
function remote_user() {
if((x($_SESSION,'authenticated')) && (x($_SESSION,'visitor_id')))
return intval($_SESSION['visitor_id']);
12 years ago
return false;
}}
12 years ago
// contents of $s are displayed prominently on the page the next time
// a page is loaded. Usually used for errors or alerts.
if(! function_exists('notice')) {
function notice($s) {
$a = get_app();
if($a->interactive)
$_SESSION['sysmsg'] .= $s;
}}
12 years ago
// wrapper around config to limit the text length of an incoming message
if(! function_exists('get_max_import_size')) {
function get_max_import_size() {
global $a;
return ((x($a->config,'max_import_size')) ? $a->config['max_import_size'] : 0 );
}}
12 years ago
// escape text ($str) for XML transport
// returns escaped text.
if(! function_exists('xmlify')) {
function xmlify($str) {
$buffer = '';
for($x = 0; $x < strlen($str); $x ++) {
$char = $str[$x];
switch( $char ) {
case "\r" :
break;
case "&" :
$buffer .= '&amp;';
break;
case "'" :
$buffer .= '&apos;';
break;
case "\"" :
$buffer .= '&quot;';
break;
case '<' :
$buffer .= '&lt;';
break;
case '>' :
$buffer .= '&gt;';
break;
case "\n" :
$buffer .= ' ';
break;
default :
$buffer .= $char;
break;
}
}
$buffer = trim($buffer);
return($buffer);
}}
12 years ago
// undo an xmlify
// pass xml escaped text ($s), returns unescaped text
if(! function_exists('unxmlify')) {
function unxmlify($s) {
$ret = str_replace('&amp;','&', $s);
$ret = str_replace(array('&lt;','&gt;','&quot;','&apos;'),array('<','>','"',"'"),$ret);
return $ret;
}}
12 years ago
// convenience wrapper, reverse the operation "bin2hex"
if(! function_exists('hex2bin')) {
function hex2bin($s) {
return(pack("H*",$s));
}}
12 years ago
// Automatic pagination.
// To use, get the count of total items.
// Then call $a->set_pager_total($number_items);
// Optionally call $a->set_pager_itemspage($n) to the number of items to display on each page
// Then call paginate($a) after the end of the display loop to insert the pager block on the page
// (assuming there are enough items to paginate).
// When using with SQL, the setting LIMIT %d, %d => $a->pager['start'],$a->pager['itemspage']
// will limit the results to the correct items for the current page.
// The actual page handling is then accomplished at the application layer.
12 years ago
if(! function_exists('paginate')) {
12 years ago
function paginate(&$a) {
$o = '';
$stripped = ereg_replace("(&page=[0-9]*)","",$_SERVER['QUERY_STRING']);
$stripped = str_replace('q=','',$stripped);
$stripped = trim($stripped,'/');
$url = $a->get_baseurl() . '/' . $stripped;
if($a->pager['total'] > $a->pager['itemspage']) {
$o .= '<div class="pager">';
if($a->pager['page'] != 1)
12 years ago
$o .= '<span class="pager_prev">'."<a href=\"$url".'&page='.($a->pager['page'] - 1).'">' . t('prev') . '</a></span> ';
12 years ago
12 years ago
$o .= "<span class=\"pager_first\"><a href=\"$url"."&page=1\">" . t('first') . "</a></span> ";
12 years ago
$numpages = $a->pager['total'] / $a->pager['itemspage'];
$numstart = 1;
$numstop = $numpages;
if($numpages > 14) {
$numstart = (($pagenum > 7) ? ($pagenum - 7) : 1);
$numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 14));
}
for($i = $numstart; $i <= $numstop; $i++){
12 years ago
if($i == $a->pager['page'])
12 years ago
$o .= '<span class="pager_current">'.(($i < 10) ? '&nbsp;'.$i : $i);
else
$o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? '&nbsp;'.$i : $i)."</a>";
$o .= '</span> ';
}
if(($a->pager['total'] % $a->pager['itemspage']) != 0) {
if($i == $a->pager['page'])
$o .= '<span class="pager_current">'.(($i < 10) ? '&nbsp;'.$i : $i);
else
$o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? '&nbsp;'.$i : $i)."</a>";
$o .= '</span> ';
}
$lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
12 years ago
$o .= "<span class=\"pager_last\"><a href=\"$url"."&page=$lastpage\">" . t('last') . "</a></span> ";
12 years ago
if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0)
12 years ago
$o .= '<span class="pager_next">'."<a href=\"$url"."&page=".($a->pager['page'] + 1).'">' . t('next') . '</a></span>';
12 years ago
$o .= '</div>'."\r\n";
}
return $o;
}}
12 years ago
12 years ago
// Turn user/group ACLs stored as angle bracketed text into arrays
if(! function_exists('expand_acl')) {
12 years ago
function expand_acl($s) {
// turn string array of angle-bracketed elements into numeric array
// e.g. "<1><2><3>" => array(1,2,3);
$ret = array();
12 years ago
if(strlen($s)) {
$t = str_replace('<','',$s);
$a = explode('>',$t);
foreach($a as $aa) {
if(intval($aa))
$ret[] = intval($aa);
12 years ago
}
}
return $ret;
}}
12 years ago
12 years ago
// Used to wrap ACL elements in angle brackets for storage
if(! function_exists('sanitise_acl')) {
12 years ago
function sanitise_acl(&$item) {
12 years ago
if(intval($item))
$item = '<' . intval(notags(trim($item))) . '>';
else
unset($item);
}}
12 years ago
// retrieve a "family" of config variables from database to cached storage
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'];
}
}
}}
12 years ago
// get a particular config variable given the family name
// and key. Returns false if not set.
// $instore is only used by the set_config function
// to determine if the key already exists in the DB
// If a key is found in the DB but doesn't exist in
// local config cache, pull it into the cache so we don't have
// to hit the DB again for this item.
if(! function_exists('get_config')) {
function get_config($family, $key, $instore = false) {
12 years ago
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;
}}
12 years ago
// Store a config value ($value) in the category ($family)
// under the key ($key)
// Return the value, or false if the database update failed
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;