From 64f5d80ff2497edc5d201e9124cb6d98f72efe00 Mon Sep 17 00:00:00 2001 From: redmatrix Date: Tue, 16 Feb 2016 18:15:19 -0800 Subject: some iconfig basics --- include/items.php | 137 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) (limited to 'include/items.php') diff --git a/include/items.php b/include/items.php index ab859bebd..3deb11640 100755 --- a/include/items.php +++ b/include/items.php @@ -4675,6 +4675,10 @@ function fetch_post_tags($items,$link = false) { dbesc($tag_finder_str), intval(TERM_OBJ_POST) ); + $imeta = q("select * from iconfig where iid in ( %s )", + dbesc($tag_finger_str) + ); + } for($x = 0; $x < count($items); $x ++) { @@ -4698,6 +4702,26 @@ function fetch_post_tags($items,$link = false) { } } } + if($imeta) { + foreach($imeta as $i) { + if(array_key_exists('item_id',$items[$x])) { + if($i['iid'] == $items[$x]['item_id']) { + if(! is_array($items[$x]['iconfig'])) + $items[$x]['iconfig'] = array(); + $i['v'] = ((preg_match('|^a:[0-9]+:{.*}$|s',$i['v'])) ? unserialize($i['v']) : $i['v']); + $items[$x]['iconfig'][] = $i; + } + } + else { + if($i['iid'] == $items[$x]['id']) { + if(! is_array($items[$x]['iconfig'])) + $items[$x]['iconfig'] = array(); + $i['v'] = ((preg_match('|^a:[0-9]+:{.*}$|s',$i['v'])) ? unserialize($i['v']) : $i['v']); + $items[$x]['iconfig'][] = $i; + } + } + } + } } return $items; @@ -5360,3 +5384,116 @@ function send_profile_photo_activity($channel,$photo,$profile) { } + + + + +function get_iconfig(&$item, $family, $key) { + + $is_item = false; + if(is_array($item)) { + $is_item = true; + if((! array_key_exists('iconfig',$item)) || (! is_array($item['iconfig']))) + $item['iconfig'] = array(); + + if(array_key_exists('item_id',$item)) + $iid = $item['item_id']; + else + $iid = $item['id']; + } + elseif(intval($item)) + $iid = $item; + + if(! $iid) + return false; + + if(is_array($item) && array_key_exists('iconfig',$item) && is_array($item['iconfig'])) { + foreach($item['iconfig'] as $c) { + if($c['iid'] == $iid && $c['cat'] == $family && $c['k'] == $key) + return $c['v']; + } + } + + $r = q("select * from iconfig where iid = %d and cat = '%s' and k = '%s' limit 1", + intval($iid), + dbesc($family), + dbesc($key) + ); + if($r) { + $v = ((preg_match('|^a:[0-9]+:{.*}$|s',$r[0]['v'])) ? unserialize($r[0]['v']) : $r[0]['v']); + if($is_item) + $item['iconfig'][] = $r[0]; + return $v; + } + return false; + +} + + +function set_iconfig(&$item, $family, $key, $value) { + + $dbvalue = ((is_array($value)) ? serialize($value) : $value); + $dbvalue = ((is_bool($dbvalue)) ? intval($dbvalue) : $dbvalue); + + $is_item = false; + $idx = null; + + if(is_array($item)) { + $is_item = true; + if((! array_key_exists('iconfig',$item)) || (! is_array($item['iconfig']))) + $item['iconfig'] = array(); + elseif($item['iconfig']) { + for($x = 0; $x < count($item['iconfig']); $x ++) { + if($item['iconfig'][$x]['cat'] == $family && $item['iconfig']['k'] == $key) { + $idx = $x; + } + } + } + if(array_key_exists('item_id',$item)) + $iid = $item['item_id']; + else + $iid = $item['id']; + } + elseif(intval($item)) + $iid = $item; + + if(! $iid) + return false; + + if(get_iconfig($item, $family, $key) === false) { + $r = q("insert into iconfig( iid, cat, k, v ) values ( %d, '%s', '%s', '%s' ) ", + intval($iid), + dbesc($family), + dbesc($key), + dbesc($dbvalue) + ); + } + else { + $r = q("update iconfig set v = '%s' where iid = %d and cat = '%s' and k = '%s' ", + dbesc($dbvalue), + intval($iid), + dbesc($family), + dbesc($key) + ); + } + + $y = q("select * from iconfig where iid = %d and cat = '%s' and k = '%s' limit 1", + intval($iid), + dbesc($family), + dbesc($key) + ); + if(! $y) + return false; + + $y[0]['v'] = $value; + + if($is_item) { + if(is_null($idx)) + $item['iconfig'][] = $y[0]; + else + $item['iconfig'][$idx] = $y[0]; + } + + return $value; +} + -- cgit v1.2.3 From 77eb6c57618a7ccfa64f200e7d1f18dcc1dd16f2 Mon Sep 17 00:00:00 2001 From: redmatrix Date: Tue, 16 Feb 2016 20:49:32 -0800 Subject: more work on iconfig --- include/items.php | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 75 insertions(+), 5 deletions(-) (limited to 'include/items.php') diff --git a/include/items.php b/include/items.php index 3deb11640..0d51af7bc 100755 --- a/include/items.php +++ b/include/items.php @@ -849,7 +849,7 @@ function get_item_elements($x,$allow_code = false) { if($allow_code) $arr['body'] = $x['body']; else - $arr['body'] = (($x['body']) ? htmlspecialchars($x['body'],ENT_COMPAT,'UTF-8',false) : ''); + $arr['body'] = (($x['body']) ? htmlspecialchars($x['body'],ENT_COMPAT,'UTF-8',false) : ''); $key = get_config('system','pubkey'); @@ -917,6 +917,7 @@ function get_item_elements($x,$allow_code = false) { $arr['attach'] = activity_sanitise($x['attach']); $arr['term'] = decode_tags($x['tags']); + $arr['iconfig'] = decode_item_meta($x['meta']); $arr['item_private'] = ((array_key_exists('flags',$x) && is_array($x['flags']) && in_array('private',$x['flags'])) ? 1 : 0); @@ -1324,6 +1325,9 @@ function encode_item($item,$mirror = false) { if($item['term']) $x['tags'] = encode_item_terms($item['term'],$mirror); + if($item['iconfig']) + $x['meta'] = encode_item_meta($item['iconfig'],$mirror); + if($item['diaspora_meta']) { $z = json_decode($item['diaspora_meta'],true); if($z) { @@ -1434,6 +1438,28 @@ function encode_item_terms($terms,$mirror = false) { return $ret; } +function encode_item_meta($meta,$mirror = false) { + $ret = array(); + + if($meta) { + foreach($meta as $m) { + $ret[] = array('family' => $m['cat'], 'key' => $m['k'], 'value' => $m['v']); + } + } + + return $ret; +} + +function decode_item_meta($meta) { + $ret = array(); + + if(is_array($meta) && $meta) { + foreach($meta as $m) { + $ret[] = array('cat' => escape_tags($m['family']),'k' => escape_tags($m['key']),'v' => $m['value']); + } + } +} + /** * @brief * @@ -2446,6 +2472,13 @@ function item_store($arr, $allow_exec = false, $deliver = true) { unset($arr['term']); } + $meta = null; + if(array_key_exists('iconfig',$arr)) { + $meta = $arr['iconfig']; + unset($arr['iconfig']); + } + + if(strlen($allow_cid) || strlen($allow_gid) || strlen($deny_cid) || strlen($deny_gid) || strlen($public_policy)) $private = 1; else @@ -2523,6 +2556,15 @@ function item_store($arr, $allow_exec = false, $deliver = true) { $arr['term'] = $terms; } + if($meta) { + foreach($meta as $m) { + set_iconfig($current_post,$m['cat'],$m['k'],$m['v']); + } + $arr['iconfig'] = $meta; + } + + + call_hooks('post_remote_end',$arr); // update the commented timestamp on the parent @@ -2744,6 +2786,13 @@ function item_store_update($arr,$allow_exec = false, $deliver = true) { unset($arr['term']); } + $meta = null; + if(array_key_exists('iconfig',$arr)) { + $meta = $arr['iconfig']; + unset($arr['iconfig']); + } + + dbesc_array($arr); logger('item_store_update: ' . print_r($arr,true), LOGGER_DATA); @@ -2785,6 +2834,17 @@ function item_store_update($arr,$allow_exec = false, $deliver = true) { $arr['term'] = $terms; } + $r = q("delete from iconfig where iid = %d", + intval($orig_post_id) + ); + + if($meta) { + foreach($meta as $m) { + set_iconfig($current_post,$m['cat'],$m['k'],$m['v']); + } + $arr['iconfig'] = $meta; + } + call_hooks('post_remote_update_end',$arr); if($deliver) { @@ -5450,15 +5510,25 @@ function set_iconfig(&$item, $family, $key, $value) { } } if(array_key_exists('item_id',$item)) - $iid = $item['item_id']; + $iid = intval($item['item_id']); else - $iid = $item['id']; + $iid = intval($item['id']); + } elseif(intval($item)) - $iid = $item; + $iid = intval($item); - if(! $iid) + if(! $iid) { + $entry = array('cat' => $family, 'k' => $key, 'v' => $value); + if($is_item) { + if(is_null($idx)) + $item['iconfig'][] = $entry; + else + $item['iconfig'][$idx] = $entry; + return $value; + } return false; + } if(get_iconfig($item, $family, $key) === false) { $r = q("insert into iconfig( iid, cat, k, v ) values ( %d, '%s', '%s', '%s' ) ", -- cgit v1.2.3 From 41ec5403e184fa8526c4d3fd0176e70a5b2b67f1 Mon Sep 17 00:00:00 2001 From: redmatrix Date: Tue, 16 Feb 2016 22:53:27 -0800 Subject: slight changes to set_iconfig - if passed an item structure only update the structure. Update the DB only if passed an item_id. This makes a clean separation of behaviour that's easy to document. One could get into some weird situations the original way which tried to update and sync both. --- include/items.php | 46 +++++++++++++--------------------------------- 1 file changed, 13 insertions(+), 33 deletions(-) (limited to 'include/items.php') diff --git a/include/items.php b/include/items.php index 0d51af7bc..2239510dc 100755 --- a/include/items.php +++ b/include/items.php @@ -5480,10 +5480,10 @@ function get_iconfig(&$item, $family, $key) { dbesc($key) ); if($r) { - $v = ((preg_match('|^a:[0-9]+:{.*}$|s',$r[0]['v'])) ? unserialize($r[0]['v']) : $r[0]['v']); + $r[0]['v'] = ((preg_match('|^a:[0-9]+:{.*}$|s',$r[0]['v'])) ? unserialize($r[0]['v']) : $r[0]['v']); if($is_item) $item['iconfig'][] = $r[0]; - return $v; + return $r[0]['v']; } return false; @@ -5504,31 +5504,25 @@ function set_iconfig(&$item, $family, $key, $value) { $item['iconfig'] = array(); elseif($item['iconfig']) { for($x = 0; $x < count($item['iconfig']); $x ++) { - if($item['iconfig'][$x]['cat'] == $family && $item['iconfig']['k'] == $key) { + if($item['iconfig'][$x]['cat'] == $family && $item['iconfig'][$x]['k'] == $key) { $idx = $x; } } } - if(array_key_exists('item_id',$item)) - $iid = intval($item['item_id']); - else - $iid = intval($item['id']); + $entry = array('cat' => $family, 'k' => $key, 'v' => $value); + if(is_null($idx)) + $item['iconfig'][] = $entry; + else + $item['iconfig'][$idx] = $entry; + return $value; } - elseif(intval($item)) + + if(intval($item)) $iid = intval($item); - if(! $iid) { - $entry = array('cat' => $family, 'k' => $key, 'v' => $value); - if($is_item) { - if(is_null($idx)) - $item['iconfig'][] = $entry; - else - $item['iconfig'][$idx] = $entry; - return $value; - } + if(! $iid) return false; - } if(get_iconfig($item, $family, $key) === false) { $r = q("insert into iconfig( iid, cat, k, v ) values ( %d, '%s', '%s', '%s' ) ", @@ -5547,23 +5541,9 @@ function set_iconfig(&$item, $family, $key, $value) { ); } - $y = q("select * from iconfig where iid = %d and cat = '%s' and k = '%s' limit 1", - intval($iid), - dbesc($family), - dbesc($key) - ); - if(! $y) + if(! $r) return false; - $y[0]['v'] = $value; - - if($is_item) { - if(is_null($idx)) - $item['iconfig'][] = $y[0]; - else - $item['iconfig'][$idx] = $y[0]; - } - return $value; } -- cgit v1.2.3 From a83eb14ff6f760e0dfd74c61dad128a83c4234bf Mon Sep 17 00:00:00 2001 From: redmatrix Date: Wed, 17 Feb 2016 00:42:51 -0800 Subject: iconfig debugging --- include/items.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'include/items.php') diff --git a/include/items.php b/include/items.php index 2239510dc..527d55b71 100755 --- a/include/items.php +++ b/include/items.php @@ -2840,7 +2840,7 @@ function item_store_update($arr,$allow_exec = false, $deliver = true) { if($meta) { foreach($meta as $m) { - set_iconfig($current_post,$m['cat'],$m['k'],$m['v']); + set_iconfig($orig_post_id,$m['cat'],$m['k'],$m['v']); } $arr['iconfig'] = $meta; } @@ -4736,7 +4736,7 @@ function fetch_post_tags($items,$link = false) { intval(TERM_OBJ_POST) ); $imeta = q("select * from iconfig where iid in ( %s )", - dbesc($tag_finger_str) + dbesc($tag_finder_str) ); } -- cgit v1.2.3 From 696c6e88ea6b1dbd06751cb5b87221b8377167a1 Mon Sep 17 00:00:00 2001 From: redmatrix Date: Wed, 17 Feb 2016 01:05:28 -0800 Subject: iconfig debugging - federation now working --- include/items.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'include/items.php') diff --git a/include/items.php b/include/items.php index 527d55b71..a2b736eea 100755 --- a/include/items.php +++ b/include/items.php @@ -1457,7 +1457,8 @@ function decode_item_meta($meta) { foreach($meta as $m) { $ret[] = array('cat' => escape_tags($m['family']),'k' => escape_tags($m['key']),'v' => $m['value']); } - } + } + return $ret; } /** -- cgit v1.2.3 From 62e141a89c1e2c5583718690ea83a13dd1d23dd1 Mon Sep 17 00:00:00 2001 From: redmatrix Date: Wed, 17 Feb 2016 14:14:42 -0800 Subject: provide del_iconfig for completeness --- include/items.php | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'include/items.php') diff --git a/include/items.php b/include/items.php index a2b736eea..5a1dadcb8 100755 --- a/include/items.php +++ b/include/items.php @@ -5548,3 +5548,37 @@ function set_iconfig(&$item, $family, $key, $value) { return $value; } + + +function del_iconfig(&$item, $family, $key) { + + + $is_item = false; + $idx = null; + + if(is_array($item)) { + $is_item = true; + if(is_array($item['iconfig'])) { + for($x = 0; $x < count($item['iconfig']); $x ++) { + if($item['iconfig'][$x]['cat'] == $family && $item['iconfig'][$x]['k'] == $key) { + unset($item['iconfig'][$x]); + } + } + } + return true; + } + + if(intval($item)) + $iid = intval($item); + + if(! $iid) + return false; + + return q("delete from iconfig where iid = %d and cat = '%s' and k = '%s' ", + intval($iid), + dbesc($family), + dbesc($key) + ); + +} + -- cgit v1.2.3