This commit is contained in:
marijus 2014-09-27 15:42:28 +02:00
commit c20c9e99b6
4 changed files with 338 additions and 106 deletions

View File

@ -1,20 +1,48 @@
<?php /** @file */ <?php
/** /**
* @file include/config.php
* @brief Arbitrary configuration storage.
* *
* Arbitrary configuration storage
* Note: * Note:
* Please do not store booleans - convert to 0/1 integer values * Please do not store booleans - convert to 0/1 integer values
* The get_?config() functions return boolean false for keys that are unset, * The get_?config() functions return boolean false for keys that are unset,
* and this could lead to subtle bugs. * and this could lead to subtle bugs.
* *
* There are a few places in the code (such as the admin panel) where boolean * Arrays get stored as serialize strings.
* configurations need to be fixed as of 10/08/2011. *
* @todo There are a few places in the code (such as the admin panel) where
* boolean configurations need to be fixed as of 10/08/2011.
*
* - <b>config</b> is used for hub specific configurations. It overrides the
* configurations from .htconfig file. The storage is of size TEXT.
* - <b>pconfig</b> is used for channel specific configurations and takes a
* <i>channel_id</i> as identifier. It stores for example which features are
* enabled per channel. The storage is of size MEDIUMTEXT.
* @code $var = get_pconfig(local_user(), 'category', 'key');@endcode
* - <b>xconfig</b> is the same as pconfig, except that it uses <i>xchan</i> as
* an identifier. This is for example for people who do not have a local account.
* The storage is of size MEDIUMTEXT.
* @code $observer = $a->get_observer_hash();
* if ($observer) {
* $var = get_xconfig($observer, 'category', 'key');
* }@endcode
*
* - get_config() and set_config() can also be done through the command line tool
* @ref util/config
* - get_pconfig() and set_pconfig() can also be done through the command line tool
* @ref util/pconfig and takes a channel_id as first argument.
*
*/ */
/**
// retrieve a "family" of config variables from database to cached storage * @brief Loads the hub's configuration from database to a cached storage.
*
* Retrieve a category ($family) of config variables from database to a cached
* storage in the global $a->config[$family].
*
* @param string $family
* The category of the configuration value
*/
function load_config($family) { function load_config($family) {
global $a; global $a;
@ -22,9 +50,7 @@ function load_config($family) {
$a->config[$family] = array(); $a->config[$family] = array();
if(! array_key_exists('config_loaded', $a->config[$family])) { if(! array_key_exists('config_loaded', $a->config[$family])) {
$r = q("SELECT * FROM config WHERE cat = '%s'", dbesc($family)); $r = q("SELECT * FROM config WHERE cat = '%s'", dbesc($family));
if($r !== false) { if($r !== false) {
if($r) { if($r) {
foreach($r as $rr) { foreach($r as $rr) {
@ -37,17 +63,24 @@ function load_config($family) {
} }
} }
// get a particular config variable given the family name /**
// and key. Returns false if not set. * @brief Get a particular config variable given the category name ($family)
// $instore is only used by the set_config function * and a key.
// to determine if the key already exists in the DB *
// If a key is found in the DB but doesn't exist in * Get a particular config variable from the given category ($family) and the
// local config cache, pull it into the cache so we don't have * $key from a cached storage in $a->config[$family]. If a key is found in the
// to hit the DB again for this item. * DB but does not exist in local config cache, pull it into the cache so we
* do not have to hit the DB again for this item.
*
* Returns false if not set.
*
* @param string $family
* The category of the configuration value
* @param string $key
* The configuration key to query
* @return mixed Return value or false on error or if not set
*/
function get_config($family, $key) { function get_config($family, $key) {
global $a; global $a;
if((! array_key_exists($family, $a->config)) || (! array_key_exists('config_loaded', $a->config[$family]))) if((! array_key_exists($family, $a->config)) || (! array_key_exists('config_loaded', $a->config[$family])))
@ -65,36 +98,59 @@ function get_config($family, $key) {
return false; return false;
} }
/**
* @brief Returns a value directly from the database configuration storage.
*
* This function queries directly the database and bypasses the chached storage
* from get_config($family, $key).
*
* @param string $family
* The category of the configuration value
* @param string $key
* The configuration key to query
* @return mixed
*/
function get_config_from_storage($family, $key) { function get_config_from_storage($family, $key) {
$ret = q("select * from config where cat = '%s' and k = '%s' limit 1", $ret = q("SELECT * FROM config WHERE cat = '%s' AND k = '%s' LIMIT 1",
dbesc($family), dbesc($family),
dbesc($key) dbesc($key)
); );
return $ret; return $ret;
} }
/**
* @brief Sets a configuration value for the hub.
// Store a config value ($value) in the category ($family) *
// under the key ($key) * Stores a config value ($value) in the category ($family) under the key ($key).
// Return the value, or false if the database update failed *
* Please do not store booleans - convert to 0/1 integer values!
*
* @param string $family
* The category of the configuration value
* @param string $key
* The configuration key to set
* @param mixed $value
* The value to store in the configuration
* @return mixed
* Return the set value, or false if the database update failed
*/
function set_config($family, $key, $value) { function set_config($family, $key, $value) {
global $a; global $a;
// manage array value // manage array value
$dbvalue = ((is_array($value)) ? serialize($value) : $value); $dbvalue = ((is_array($value)) ? serialize($value) : $value);
$dbvalue = ((is_bool($dbvalue)) ? intval($dbvalue) : $dbvalue); $dbvalue = ((is_bool($dbvalue)) ? intval($dbvalue) : $dbvalue);
if(get_config($family, $key) === false || (! get_config_from_storage($family, $key))) { if(get_config($family, $key) === false || (! get_config_from_storage($family, $key))) {
$a->config[$family][$key] = $value;
$ret = q("INSERT INTO config ( cat, k, v ) VALUES ( '%s', '%s', '%s' ) ", $ret = q("INSERT INTO config ( cat, k, v ) VALUES ( '%s', '%s', '%s' ) ",
dbesc($family), dbesc($family),
dbesc($key), dbesc($key),
dbesc($dbvalue) dbesc($dbvalue)
); );
if($ret) if($ret) {
return $value; $a->config[$family][$key] = $value;
$ret = $value;
}
return $ret; return $ret;
} }
@ -104,15 +160,29 @@ function set_config($family,$key,$value) {
dbesc($key) dbesc($key)
); );
if($ret) {
$a->config[$family][$key] = $value; $a->config[$family][$key] = $value;
$ret = $value;
if($ret) }
return $value;
return $ret; return $ret;
} }
/**
* @brief Deletes the given key from the hub's configuration database.
*
* Removes the configured value from the stored cache in $a->config[$family]
* and removes it from the database.
*
* @param string $family
* The category of the configuration value
* @param string $key
* The configuration key to delete
* @return mixed
*/
function del_config($family, $key) { function del_config($family, $key) {
global $a; global $a;
$ret = false;
if(array_key_exists($family, $a->config) && array_key_exists($key, $a->config[$family])) if(array_key_exists($family, $a->config) && array_key_exists($key, $a->config[$family]))
unset($a->config[$family][$key]); unset($a->config[$family][$key]);
$ret = q("DELETE FROM config WHERE cat = '%s' AND k = '%s' LIMIT 1", $ret = q("DELETE FROM config WHERE cat = '%s' AND k = '%s' LIMIT 1",
@ -123,7 +193,17 @@ function del_config($family,$key) {
} }
function load_pconfig($uid,$family = '') { /**
* @brief Loads all configuration values of a channel into a cached storage.
*
* All configuration values of the given channel are stored in global cache
* which is available under the global variable $a->config[$uid].
*
* @param string $uid
* The channel_id
* @return void|false Nothing or false if $uid is false
*/
function load_pconfig($uid) {
global $a; global $a;
if($uid === false) if($uid === false)
@ -132,8 +212,6 @@ function load_pconfig($uid,$family = '') {
if(! array_key_exists($uid, $a->config)) if(! array_key_exists($uid, $a->config))
$a->config[$uid] = array(); $a->config[$uid] = array();
// family is no longer used - load entire user config
$r = q("SELECT * FROM `pconfig` WHERE `uid` = %d", $r = q("SELECT * FROM `pconfig` WHERE `uid` = %d",
intval($uid) intval($uid)
); );
@ -151,11 +229,26 @@ function load_pconfig($uid,$family = '') {
} }
} }
/**
* @brief Get a particular channel's config variable given the category name
* ($family) and a key.
*
* Get a particular channel's config value from the given category ($family)
* and the $key from a cached storage in $a->config[$uid].
*
* Returns false if not set.
*
* @param string $uid
* The channel_id
* @param string $family
* The category of the configuration value
* @param string $key
* The configuration key to query
* @param boolean $instore (deprecated, without function)
* @return mixed Stored value or false if it does not exist
*/
function get_pconfig($uid, $family, $key, $instore = false) { function get_pconfig($uid, $family, $key, $instore = false) {
logger('include/config.php: get_pconfig() deprecated instore param used', LOGGER_DEBUG);
global $a; global $a;
if($uid === false) if($uid === false)
@ -173,11 +266,25 @@ function get_pconfig($uid,$family, $key, $instore = false) {
); );
} }
/**
* @brief Sets a configuration value for a channel.
*
* Stores a config value ($value) in the category ($family) under the key ($key)
* for the channel_id $uid.
*
* Please do not store booleans - convert to 0/1 integer values!
*
* @param string $uid
* The channel_id
* @param string $family
* The category of the configuration value
* @param string $key
* The configuration key to query
* @return mixed Stored $value or false
*/
function set_pconfig($uid, $family, $key, $value) { function set_pconfig($uid, $family, $key, $value) {
global $a; global $a;
// manage array value // manage array value
$dbvalue = ((is_array($value)) ? serialize($value) : $value); $dbvalue = ((is_array($value)) ? serialize($value) : $value);
$dbvalue = ((is_bool($dbvalue)) ? intval($dbvalue) : $dbvalue); $dbvalue = ((is_bool($dbvalue)) ? intval($dbvalue) : $dbvalue);
@ -235,10 +342,24 @@ function set_pconfig($uid,$family,$key,$value) {
return $ret; return $ret;
} }
/**
* @brief Deletes the given key from the channel's configuration.
*
* Removes the configured value from the stored cache in $a->config[$uid]
* and removes it from the database.
*
* @param string $uid
* The channel_id
* @param string $family
* The category of the configuration value
* @param string $key
* The configuration key to delete
* @return mixed
*/
function del_pconfig($uid, $family, $key) { function del_pconfig($uid, $family, $key) {
global $a; global $a;
$ret = false;
if(x($a->config[$uid][$family], $key)) if(x($a->config[$uid][$family], $key))
unset($a->config[$uid][$family][$key]); unset($a->config[$uid][$family][$key]);
$ret = q("DELETE FROM pconfig WHERE uid = %d AND cat = '%s' AND k = '%s' LIMIT 1", $ret = q("DELETE FROM pconfig WHERE uid = %d AND cat = '%s' AND k = '%s' LIMIT 1",
@ -250,8 +371,17 @@ function del_pconfig($uid,$family,$key) {
} }
/**
function load_xconfig($xchan,$family = '') { * @brief Loads a full xchan's configuration into a cached storage.
*
* All configuration values of the given observer hash are stored in global
* cache which is available under the global variable $a->config[$xchan].
*
* @param string $xchan
* The observer's hash
* @return void|false Returns false if xchan is not set
*/
function load_xconfig($xchan) {
global $a; global $a;
if(! $xchan) if(! $xchan)
@ -260,8 +390,6 @@ function load_xconfig($xchan,$family = '') {
if(! array_key_exists($xchan, $a->config)) if(! array_key_exists($xchan, $a->config))
$a->config[$xchan] = array(); $a->config[$xchan] = array();
// family is no longer used. Entire config is loaded
$r = q("SELECT * FROM `xconfig` WHERE `xchan` = '%s'", $r = q("SELECT * FROM `xconfig` WHERE `xchan` = '%s'",
dbesc($xchan) dbesc($xchan)
); );
@ -279,11 +407,24 @@ function load_xconfig($xchan,$family = '') {
} }
} }
/**
* @brief Get a particular observer's config variable given the category
* name ($family) and a key.
*
* Get a particular observer's config value from the given category ($family)
* and the $key from a cached storage in $a->config[$xchan].
*
* Returns false if not set.
*
* @param string $xchan
* The observer's hash
* @param string $family
* The category of the configuration value
* @param string $key
* The configuration key to query
* @return mixed Stored $value or false if it does not exist
*/
function get_xconfig($xchan, $family, $key) { function get_xconfig($xchan, $family, $key) {
global $a; global $a;
if(! $xchan) if(! $xchan)
@ -299,12 +440,25 @@ function get_xconfig($xchan,$family, $key) {
? unserialize($a->config[$xchan][$family][$key]) ? unserialize($a->config[$xchan][$family][$key])
: $a->config[$xchan][$family][$key] : $a->config[$xchan][$family][$key]
); );
} }
/**
* @brief Sets a configuration value for an observer.
*
* Stores a config value ($value) in the category ($family) under the key ($key)
* for the observer's $xchan hash.
*
* Please do not store booleans - convert to 0/1 integer values!
*
* @param string $xchan
* The observer's hash
* @param string $family
* The category of the configuration value
* @param string $key
* The configuration key to set
* @return mixed Stored $value or false
*/
function set_xconfig($xchan, $family, $key, $value) { function set_xconfig($xchan, $family, $key, $value) {
global $a; global $a;
// manage array value // manage array value
@ -341,13 +495,26 @@ function set_xconfig($xchan,$family,$key,$value) {
if($ret) if($ret)
return $value; return $value;
return $ret; return $ret;
} }
/**
* @brief Deletes the given key from the observer's config.
*
* Removes the configured value from the stored cache in $a->config[$xchan]
* and removes it from the database.
*
* @param string $xchan
* The observer's hash
* @param string $family
* The category of the configuration value
* @param string $key
* The configuration key to delete
* @return mixed
*/
function del_xconfig($xchan, $family, $key) { function del_xconfig($xchan, $family, $key) {
global $a; global $a;
$ret = false;
if(x($a->config[$xchan][$family], $key)) if(x($a->config[$xchan][$family], $key))
unset($a->config[$xchan][$family][$key]); unset($a->config[$xchan][$family][$key]);
$ret = q("DELETE FROM `xconfig` WHERE `xchan` = '%s' AND `cat` = '%s' AND `k` = '%s' LIMIT 1", $ret = q("DELETE FROM `xconfig` WHERE `xchan` = '%s' AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
@ -357,6 +524,3 @@ function del_xconfig($xchan,$family,$key) {
); );
return $ret; return $ret;
} }

View File

@ -518,10 +518,26 @@ function identity_basic_export($channel_id, $items = false) {
$ret['photo'] = array('type' => $r[0]['type'], 'data' => base64url_encode($r[0]['data'])); $ret['photo'] = array('type' => $r[0]['type'], 'data' => base64url_encode($r[0]['data']));
} }
$r = q("select * from obj where obj_channel = %d",
intval($channel_id)
);
if($r)
$ret['obj'] = $r;
if(! $items) if(! $items)
return $ret; return $ret;
$r = q("select likes.*, item.mid from likes left join item on likes.iid = item.id where likes.channel_id = %d",
intval($channel_id)
);
if($r)
$ret['likes'] = $r;
$r = q("select item_id.*, item.mid from item_id left join item on item_id.iid = item.id where item_id.uid = %d", $r = q("select item_id.*, item.mid from item_id left join item on item_id.iid = item.id where item_id.uid = %d",
intval($channel_id) intval($channel_id)
); );

52
mod/p.php Normal file
View File

@ -0,0 +1,52 @@
<?php /** @file */
require_once('include/bb2diaspora.php');
// used in Diaspora communications to provide a server copy of a sent post in XML format.
function p_init(&$a) {
if(argc() < 2)
http_status_exit(401);
$mid = str_replace('.xml','',argv(1));
$r = q("select * from item where mid = '%s' and (item_flags & %d) and item_private = 0 limit 1",
dbesc($mid),
intval(ITEM_WALL)
);
if((! $r) || (! perm_is_allowed($r[0]['uid'],'','view_stream')))
http_status_exit(404);
$c = q("select * from channel where channel_id = %d limit 1",
intval($r[0]['uid'])
);
if(! $c)
http_status_exit(404);
$myaddr = $c[0]['channel_address'] . '@' . $a->get_hostname();
$item = $r[0];
$title = $item['title'];
$body = bb2diaspora_itembody($item);
$created = datetime_convert('UTC','UTC',$item['created'],'Y-m-d H:i:s \U\T\C');
$tpl = get_markup_template('diaspora_post.tpl');
$msg = replace_macros($tpl, array(
'$body' => xmlify($body),
'$guid' => $item['mid'],
'$handle' => xmlify($myaddr),
'$public' => 'true',
'$created' => $created,
'$provider' => (($item['app']) ? $item['app'] : 'redmatrix')
));
header('Content-type: text/xml');
echo $msg;
killme();
}

View File

@ -1 +1 @@
2014-09-26.810 2014-09-27.811