00001 <?php
00016 global $ENTITY_CACHE;
00017 $ENTITY_CACHE = array();
00018
00025 global $SUBTYPE_CACHE;
00026 $SUBTYPE_CACHE = null;
00027
00036 function _elgg_invalidate_cache_for_entity($guid) {
00037 global $ENTITY_CACHE;
00038
00039 $guid = (int)$guid;
00040
00041 unset($ENTITY_CACHE[$guid]);
00042
00043 elgg_get_metadata_cache()->clear($guid);
00044 }
00045
00059 function _elgg_cache_entity(ElggEntity $entity) {
00060 global $ENTITY_CACHE;
00061
00062
00063
00064 if (!($entity instanceof ElggPlugin) && elgg_get_ignore_access()) {
00065 return;
00066 }
00067
00068
00069
00070 if (count($ENTITY_CACHE) > 256) {
00071 $random_guid = array_rand($ENTITY_CACHE);
00072
00073 unset($ENTITY_CACHE[$random_guid]);
00074
00075
00076
00077
00078
00079 elgg_get_metadata_cache()->clear($random_guid);
00080 }
00081
00082 $ENTITY_CACHE[$entity->guid] = $entity;
00083 }
00084
00095 function _elgg_retrieve_cached_entity($guid) {
00096 global $ENTITY_CACHE;
00097
00098 if (isset($ENTITY_CACHE[$guid])) {
00099 if ($ENTITY_CACHE[$guid]->isFullyLoaded()) {
00100 return $ENTITY_CACHE[$guid];
00101 }
00102 }
00103
00104 return false;
00105 }
00106
00131 function get_subtype_id($type, $subtype) {
00132 global $SUBTYPE_CACHE;
00133
00134 if (!$subtype) {
00135 return false;
00136 }
00137
00138 if ($SUBTYPE_CACHE === null) {
00139 _elgg_populate_subtype_cache();
00140 }
00141
00142
00143 $result = _elgg_retrieve_cached_subtype($type, $subtype);
00144 if ($result !== null) {
00145 return $result->id;
00146 }
00147
00148 return false;
00149 }
00150
00161 function get_subtype_from_id($subtype_id) {
00162 global $SUBTYPE_CACHE;
00163
00164 if (!$subtype_id) {
00165 return false;
00166 }
00167
00168 if ($SUBTYPE_CACHE === null) {
00169 _elgg_populate_subtype_cache();
00170 }
00171
00172 if (isset($SUBTYPE_CACHE[$subtype_id])) {
00173 return $SUBTYPE_CACHE[$subtype_id]->subtype;
00174 }
00175
00176 return false;
00177 }
00178
00188 function _elgg_retrieve_cached_subtype($type, $subtype) {
00189 global $SUBTYPE_CACHE;
00190
00191 if ($SUBTYPE_CACHE === null) {
00192 _elgg_populate_subtype_cache();
00193 }
00194
00195 foreach ($SUBTYPE_CACHE as $obj) {
00196 if ($obj->type === $type && $obj->subtype === $subtype) {
00197 return $obj;
00198 }
00199 }
00200 return null;
00201 }
00202
00208 function _elgg_populate_subtype_cache() {
00209 global $CONFIG, $SUBTYPE_CACHE;
00210
00211 $results = get_data("SELECT * FROM {$CONFIG->dbprefix}entity_subtypes");
00212
00213 $SUBTYPE_CACHE = array();
00214 foreach ($results as $row) {
00215 $SUBTYPE_CACHE[$row->id] = $row;
00216 }
00217 }
00218
00234 function get_subtype_class($type, $subtype) {
00235 global $SUBTYPE_CACHE;
00236
00237 if ($SUBTYPE_CACHE === null) {
00238 _elgg_populate_subtype_cache();
00239 }
00240
00241
00242 $obj = _elgg_retrieve_cached_subtype($type, $subtype);
00243 if ($obj) {
00244 return $obj->class;
00245 }
00246
00247 return null;
00248 }
00249
00260 function get_subtype_class_from_id($subtype_id) {
00261 global $SUBTYPE_CACHE;
00262
00263 if (!$subtype_id) {
00264 return null;
00265 }
00266
00267 if ($SUBTYPE_CACHE === null) {
00268 _elgg_populate_subtype_cache();
00269 }
00270
00271 if (isset($SUBTYPE_CACHE[$subtype_id])) {
00272 return $SUBTYPE_CACHE[$subtype_id]->class;
00273 }
00274
00275 return null;
00276 }
00277
00300 function add_subtype($type, $subtype, $class = "") {
00301 global $CONFIG, $SUBTYPE_CACHE;
00302
00303 if (!$subtype) {
00304 return 0;
00305 }
00306
00307 $id = get_subtype_id($type, $subtype);
00308
00309 if (!$id) {
00310
00311 $cache_obj = (object) array(
00312 'type' => $type,
00313 'subtype' => $subtype,
00314 'class' => $class,
00315 );
00316
00317 $type = sanitise_string($type);
00318 $subtype = sanitise_string($subtype);
00319 $class = sanitise_string($class);
00320
00321 $id = insert_data("INSERT INTO {$CONFIG->dbprefix}entity_subtypes"
00322 . " (type, subtype, class) VALUES ('$type', '$subtype', '$class')");
00323
00324
00325 $cache_obj->id = $id;
00326 $SUBTYPE_CACHE[$id] = $cache_obj;
00327 }
00328
00329 return $id;
00330 }
00331
00346 function remove_subtype($type, $subtype) {
00347 global $CONFIG;
00348
00349 $type = sanitise_string($type);
00350 $subtype = sanitise_string($subtype);
00351
00352 return delete_data("DELETE FROM {$CONFIG->dbprefix}entity_subtypes"
00353 . " WHERE type = '$type' AND subtype = '$subtype'");
00354 }
00355
00365 function update_subtype($type, $subtype, $class = '') {
00366 global $CONFIG, $SUBTYPE_CACHE;
00367
00368 $id = get_subtype_id($type, $subtype);
00369 if (!$id) {
00370 return false;
00371 }
00372
00373 if ($SUBTYPE_CACHE === null) {
00374 _elgg_populate_subtype_cache();
00375 }
00376
00377 $unescaped_class = $class;
00378
00379 $type = sanitise_string($type);
00380 $subtype = sanitise_string($subtype);
00381 $class = sanitise_string($class);
00382
00383 $success = update_data("UPDATE {$CONFIG->dbprefix}entity_subtypes
00384 SET type = '$type', subtype = '$subtype', class = '$class'
00385 WHERE id = $id
00386 ");
00387
00388 if ($success && isset($SUBTYPE_CACHE[$id])) {
00389 $SUBTYPE_CACHE[$id]->class = $unescaped_class;
00390 }
00391
00392 return $success;
00393 }
00394
00413 function update_entity($guid, $owner_guid, $access_id, $container_guid = null, $time_created = null) {
00414 global $CONFIG, $ENTITY_CACHE;
00415
00416 $guid = (int)$guid;
00417 $owner_guid = (int)$owner_guid;
00418 $access_id = (int)$access_id;
00419 $container_guid = (int) $container_guid;
00420 if (is_null($container_guid)) {
00421 $container_guid = $owner_guid;
00422 }
00423 $time = time();
00424
00425 $entity = get_entity($guid);
00426
00427 if ($time_created == null) {
00428 $time_created = $entity->time_created;
00429 } else {
00430 $time_created = (int) $time_created;
00431 }
00432
00433 if ($entity && $entity->canEdit()) {
00434 if (elgg_trigger_event('update', $entity->type, $entity)) {
00435 $ret = update_data("UPDATE {$CONFIG->dbprefix}entities
00436 set owner_guid='$owner_guid', access_id='$access_id',
00437 container_guid='$container_guid', time_created='$time_created',
00438 time_updated='$time' WHERE guid=$guid");
00439
00440 if ($entity instanceof ElggObject) {
00441 update_river_access_by_object($guid, $access_id);
00442 }
00443
00444
00445 static $newentity_cache;
00446 if ((!$newentity_cache) && (is_memcache_available())) {
00447 $newentity_cache = new ElggMemcache('new_entity_cache');
00448 }
00449 if ($newentity_cache) {
00450 $newentity_cache->delete($guid);
00451 }
00452
00453
00454 if ($ret === false) {
00455 return false;
00456 }
00457
00458 return true;
00459 }
00460 }
00461 }
00462
00480 function can_write_to_container($user_guid = 0, $container_guid = 0, $type = 'all', $subtype = 'all') {
00481 $user_guid = (int)$user_guid;
00482 $user = get_entity($user_guid);
00483 if (!$user) {
00484 $user = elgg_get_logged_in_user_entity();
00485 }
00486
00487 $container_guid = (int)$container_guid;
00488 if (!$container_guid) {
00489 $container_guid = elgg_get_page_owner_guid();
00490 }
00491
00492 $return = false;
00493
00494 if (!$container_guid) {
00495 $return = true;
00496 }
00497
00498 $container = get_entity($container_guid);
00499
00500 if ($container) {
00501
00502 if ($container->canEdit($user_guid)) {
00503 $return = true;
00504 }
00505
00506
00507
00508 if (!$return && $user && $container instanceof ElggGroup) {
00509
00510 if ($container->isMember($user)) {
00511 $return = true;
00512 }
00513 }
00514 }
00515
00516
00517 return elgg_trigger_plugin_hook(
00518 'container_permissions_check',
00519 $type,
00520 array(
00521 'container' => $container,
00522 'user' => $user,
00523 'subtype' => $subtype
00524 ),
00525 $return);
00526 }
00527
00551 function create_entity($type, $subtype, $owner_guid, $access_id, $site_guid = 0,
00552 $container_guid = 0) {
00553
00554 global $CONFIG;
00555
00556 $type = sanitise_string($type);
00557 $subtype_id = add_subtype($type, $subtype);
00558 $owner_guid = (int)$owner_guid;
00559 $access_id = (int)$access_id;
00560 $time = time();
00561 if ($site_guid == 0) {
00562 $site_guid = $CONFIG->site_guid;
00563 }
00564 $site_guid = (int) $site_guid;
00565 if ($container_guid == 0) {
00566 $container_guid = $owner_guid;
00567 }
00568
00569 $user_guid = elgg_get_logged_in_user_guid();
00570 if (!can_write_to_container($user_guid, $owner_guid, $type, $subtype)) {
00571 return false;
00572 }
00573 if ($owner_guid != $container_guid) {
00574 if (!can_write_to_container($user_guid, $container_guid, $type, $subtype)) {
00575 return false;
00576 }
00577 }
00578 if ($type == "") {
00579 throw new InvalidParameterException(elgg_echo('InvalidParameterException:EntityTypeNotSet'));
00580 }
00581
00582 return insert_data("INSERT into {$CONFIG->dbprefix}entities
00583 (type, subtype, owner_guid, site_guid, container_guid,
00584 access_id, time_created, time_updated, last_action)
00585 values
00586 ('$type',$subtype_id, $owner_guid, $site_guid, $container_guid,
00587 $access_id, $time, $time, $time)");
00588 }
00589
00605 function get_entity_as_row($guid) {
00606 global $CONFIG;
00607
00608 if (!$guid) {
00609 return false;
00610 }
00611
00612 $guid = (int) $guid;
00613 $access = get_access_sql_suffix();
00614
00615 return get_data_row("SELECT * from {$CONFIG->dbprefix}entities where guid=$guid and $access");
00616 }
00617
00634 function entity_row_to_elggstar($row) {
00635 if (!($row instanceof stdClass)) {
00636 return $row;
00637 }
00638
00639 if ((!isset($row->guid)) || (!isset($row->subtype))) {
00640 return $row;
00641 }
00642
00643 $new_entity = false;
00644
00645
00646 static $newentity_cache;
00647 if ((!$newentity_cache) && (is_memcache_available())) {
00648 $newentity_cache = new ElggMemcache('new_entity_cache');
00649 }
00650 if ($newentity_cache) {
00651 $new_entity = $newentity_cache->load($row->guid);
00652 }
00653 if ($new_entity) {
00654 return $new_entity;
00655 }
00656
00657
00658 $classname = get_subtype_class_from_id($row->subtype);
00659 if ($classname != "") {
00660 if (class_exists($classname)) {
00661 $new_entity = new $classname($row);
00662
00663 if (!($new_entity instanceof ElggEntity)) {
00664 $msg = elgg_echo('ClassException:ClassnameNotClass', array($classname, 'ElggEntity'));
00665 throw new ClassException($msg);
00666 }
00667 } else {
00668 error_log(elgg_echo('ClassNotFoundException:MissingClass', array($classname)));
00669 }
00670 }
00671
00672 if (!$new_entity) {
00673
00674 switch ($row->type) {
00675 case 'object' :
00676 $new_entity = new ElggObject($row);
00677 break;
00678 case 'user' :
00679 $new_entity = new ElggUser($row);
00680 break;
00681 case 'group' :
00682 $new_entity = new ElggGroup($row);
00683 break;
00684 case 'site' :
00685 $new_entity = new ElggSite($row);
00686 break;
00687 default:
00688 $msg = elgg_echo('InstallationException:TypeNotSupported', array($row->type));
00689 throw new InstallationException($msg);
00690 }
00691 }
00692
00693
00694 if (($newentity_cache) && ($new_entity)) {
00695 $newentity_cache->save($new_entity->guid, $new_entity);
00696 }
00697
00698 return $new_entity;
00699 }
00700
00709 function get_entity($guid) {
00710
00711
00712
00713 static $shared_cache;
00714
00715
00716
00717
00718 if (!is_numeric($guid) || $guid === 0 || $guid === '0') {
00719 return false;
00720 }
00721
00722
00723 $new_entity = _elgg_retrieve_cached_entity($guid);
00724 if ($new_entity) {
00725 return $new_entity;
00726 }
00727
00728
00729 if (null === $shared_cache) {
00730 if (is_memcache_available()) {
00731 $shared_cache = new ElggMemcache('new_entity_cache');
00732 } else {
00733 $shared_cache = false;
00734 }
00735 }
00736
00737
00738 $entity_row = get_entity_as_row($guid);
00739 if (!$entity_row) {
00740 return false;
00741 }
00742
00743 if ($shared_cache) {
00744 $cached_entity = $shared_cache->load($guid);
00745
00746 if ($cached_entity) {
00747
00748 return $cached_entity;
00749 }
00750 }
00751
00752
00753 try {
00754 $new_entity = entity_row_to_elggstar($entity_row);
00755 } catch (IncompleteEntityException $e) {
00756 return false;
00757 }
00758
00759 if ($new_entity) {
00760 _elgg_cache_entity($new_entity);
00761 }
00762 return $new_entity;
00763 }
00764
00778 function elgg_entity_exists($guid) {
00779 global $CONFIG;
00780
00781 $guid = sanitize_int($guid);
00782
00783 $query = "SELECT count(*) as total FROM {$CONFIG->dbprefix}entities WHERE guid = $guid";
00784 $result = get_data_row($query);
00785 if ($result->total == 0) {
00786 return false;
00787 } else {
00788 return true;
00789 }
00790 }
00791
00858 function elgg_get_entities(array $options = array()) {
00859 global $CONFIG;
00860
00861 $defaults = array(
00862 'types' => ELGG_ENTITIES_ANY_VALUE,
00863 'subtypes' => ELGG_ENTITIES_ANY_VALUE,
00864 'type_subtype_pairs' => ELGG_ENTITIES_ANY_VALUE,
00865
00866 'guids' => ELGG_ENTITIES_ANY_VALUE,
00867 'owner_guids' => ELGG_ENTITIES_ANY_VALUE,
00868 'container_guids' => ELGG_ENTITIES_ANY_VALUE,
00869 'site_guids' => $CONFIG->site_guid,
00870
00871 'modified_time_lower' => ELGG_ENTITIES_ANY_VALUE,
00872 'modified_time_upper' => ELGG_ENTITIES_ANY_VALUE,
00873 'created_time_lower' => ELGG_ENTITIES_ANY_VALUE,
00874 'created_time_upper' => ELGG_ENTITIES_ANY_VALUE,
00875
00876 'reverse_order_by' => false,
00877 'order_by' => 'e.time_created desc',
00878 'group_by' => ELGG_ENTITIES_ANY_VALUE,
00879 'limit' => 10,
00880 'offset' => 0,
00881 'count' => FALSE,
00882 'selects' => array(),
00883 'wheres' => array(),
00884 'joins' => array(),
00885
00886 'callback' => 'entity_row_to_elggstar',
00887 );
00888
00889 $options = array_merge($defaults, $options);
00890
00891
00892
00893 if (isset($options['type_subtype_pair'])) {
00894 if (isset($options['type_subtype_pairs'])) {
00895 $options['type_subtype_pairs'] = array_merge($options['type_subtype_pairs'],
00896 $options['type_subtype_pair']);
00897 } else {
00898 $options['type_subtype_pairs'] = $options['type_subtype_pair'];
00899 }
00900 }
00901
00902 $singulars = array('type', 'subtype', 'guid', 'owner_guid', 'container_guid', 'site_guid');
00903 $options = elgg_normalise_plural_options_array($options, $singulars);
00904
00905
00906 if (!is_array($options['wheres'])) {
00907 $options['wheres'] = array($options['wheres']);
00908 }
00909
00910 $wheres = $options['wheres'];
00911
00912 $wheres[] = elgg_get_entity_type_subtype_where_sql('e', $options['types'],
00913 $options['subtypes'], $options['type_subtype_pairs']);
00914
00915 $wheres[] = elgg_get_guid_based_where_sql('e.guid', $options['guids']);
00916 $wheres[] = elgg_get_guid_based_where_sql('e.owner_guid', $options['owner_guids']);
00917 $wheres[] = elgg_get_guid_based_where_sql('e.container_guid', $options['container_guids']);
00918 $wheres[] = elgg_get_guid_based_where_sql('e.site_guid', $options['site_guids']);
00919
00920 $wheres[] = elgg_get_entity_time_where_sql('e', $options['created_time_upper'],
00921 $options['created_time_lower'], $options['modified_time_upper'], $options['modified_time_lower']);
00922
00923
00924
00925 foreach ($wheres as $i => $where) {
00926 if ($where === FALSE) {
00927 return FALSE;
00928 } elseif (empty($where)) {
00929 unset($wheres[$i]);
00930 }
00931 }
00932
00933
00934 $wheres = array_unique($wheres);
00935
00936
00937 if (!is_array($options['joins'])) {
00938 $options['joins'] = array($options['joins']);
00939 }
00940
00941
00942 $joins = array_unique($options['joins']);
00943
00944 foreach ($joins as $i => $join) {
00945 if ($join === FALSE) {
00946 return FALSE;
00947 } elseif (empty($join)) {
00948 unset($joins[$i]);
00949 }
00950 }
00951
00952
00953 if ($options['selects']) {
00954 $selects = '';
00955 foreach ($options['selects'] as $select) {
00956 $selects .= ", $select";
00957 }
00958 } else {
00959 $selects = '';
00960 }
00961
00962 if (!$options['count']) {
00963 $query = "SELECT DISTINCT e.*{$selects} FROM {$CONFIG->dbprefix}entities e ";
00964 } else {
00965 $query = "SELECT count(DISTINCT e.guid) as total FROM {$CONFIG->dbprefix}entities e ";
00966 }
00967
00968
00969 foreach ($joins as $j) {
00970 $query .= " $j ";
00971 }
00972
00973
00974 $query .= ' WHERE ';
00975
00976 foreach ($wheres as $w) {
00977 $query .= " $w AND ";
00978 }
00979
00980
00981 $query .= get_access_sql_suffix('e');
00982
00983
00984 if ($options['reverse_order_by']) {
00985 $options['order_by'] = elgg_sql_reverse_order_by_clause($options['order_by']);
00986 }
00987
00988 if (!$options['count']) {
00989 if ($options['group_by']) {
00990 $query .= " GROUP BY {$options['group_by']}";
00991 }
00992
00993 if ($options['order_by']) {
00994 $query .= " ORDER BY {$options['order_by']}";
00995 }
00996
00997 if ($options['limit']) {
00998 $limit = sanitise_int($options['limit'], false);
00999 $offset = sanitise_int($options['offset'], false);
01000 $query .= " LIMIT $offset, $limit";
01001 }
01002
01003 if ($options['callback'] === 'entity_row_to_elggstar') {
01004 $dt = _elgg_fetch_entities_from_sql($query);
01005 } else {
01006 $dt = get_data($query, $options['callback']);
01007 }
01008
01009 if ($dt) {
01010
01011 $guids = array();
01012 foreach ($dt as $item) {
01013
01014 if ($item instanceof ElggEntity) {
01015 _elgg_cache_entity($item);
01016
01017 if (!$item instanceof ElggPlugin) {
01018 $guids[] = $item->guid;
01019 }
01020 }
01021 }
01022
01023 reset($dt);
01024
01025 if ($guids) {
01026 elgg_get_metadata_cache()->populateFromEntities($guids);
01027 }
01028 }
01029 return $dt;
01030 } else {
01031 $total = get_data_row($query);
01032 return (int)$total->total;
01033 }
01034 }
01035
01045 function _elgg_fetch_entities_from_sql($sql) {
01046 static $plugin_subtype;
01047 if (null === $plugin_subtype) {
01048 $plugin_subtype = get_subtype_id('object', 'plugin');
01049 }
01050
01051
01052
01053 $types_to_optimize = array(
01054 'object' => 'title',
01055 'user' => 'password',
01056 'group' => 'name',
01057 );
01058
01059 $rows = get_data($sql);
01060
01061
01062 $lookup_types = array();
01063
01064 $guid_to_key = array();
01065
01066 if (isset($rows[0]->type, $rows[0]->subtype)
01067 && $rows[0]->type === 'object'
01068 && $rows[0]->subtype == $plugin_subtype) {
01069
01070
01071
01072 $types_to_optimize = array();
01073 }
01074
01075
01076 foreach ($rows as $i => $row) {
01077 if (empty($row->guid) || empty($row->type)) {
01078 throw new LogicException('Entity row missing guid or type');
01079 }
01080 if ($entity = _elgg_retrieve_cached_entity($row->guid)) {
01081 $rows[$i] = $entity;
01082 continue;
01083 }
01084 if (isset($types_to_optimize[$row->type])) {
01085
01086 if (isset($row->{$types_to_optimize[$row->type]})) {
01087
01088
01089 continue;
01090 }
01091 $lookup_types[$row->type][] = $row->guid;
01092 $guid_to_key[$row->guid] = $i;
01093 }
01094 }
01095
01096 if ($lookup_types) {
01097 $dbprefix = elgg_get_config('dbprefix');
01098
01099 foreach ($lookup_types as $type => $guids) {
01100 $set = "(" . implode(',', $guids) . ")";
01101 $sql = "SELECT * FROM {$dbprefix}{$type}s_entity WHERE guid IN $set";
01102 $secondary_rows = get_data($sql);
01103 if ($secondary_rows) {
01104 foreach ($secondary_rows as $secondary_row) {
01105 $key = $guid_to_key[$secondary_row->guid];
01106
01107 $rows[$key] = (object)array_merge((array)$rows[$key], (array)$secondary_row);
01108 }
01109 }
01110 }
01111 }
01112
01113 foreach ($rows as $i => $row) {
01114 if ($row instanceof ElggEntity) {
01115 continue;
01116 } else {
01117 try {
01118 $rows[$i] = entity_row_to_elggstar($row);
01119 } catch (IncompleteEntityException $e) {
01120
01121 unset($rows[$i]);
01122 }
01123 }
01124 }
01125 return $rows;
01126 }
01127
01140 function elgg_get_entity_type_subtype_where_sql($table, $types, $subtypes, $pairs) {
01141
01142 if ($subtypes && !$types) {
01143 elgg_log("Cannot set subtypes without type.", 'WARNING');
01144 return FALSE;
01145 }
01146
01147
01148 if (!$types && !$subtypes && !$pairs) {
01149 return '';
01150 }
01151
01152
01153 $valid_types = elgg_get_config('entity_types');
01154
01155
01156 $wheres = array();
01157 if (!is_array($pairs)) {
01158 if (!is_array($types)) {
01159 $types = array($types);
01160 }
01161
01162 if ($subtypes && !is_array($subtypes)) {
01163 $subtypes = array($subtypes);
01164 }
01165
01166
01167 $valid_types_count = count($types);
01168 $valid_subtypes_count = 0;
01169
01170
01171
01172
01173
01174
01175
01176 foreach ($types as $type) {
01177 if (!in_array($type, $valid_types)) {
01178 $valid_types_count--;
01179 unset($types[array_search($type, $types)]);
01180 } else {
01181
01182 $valid_subtypes_count += count($subtypes);
01183 }
01184 }
01185
01186
01187 if (!$valid_types_count) {
01188 return FALSE;
01189 }
01190
01191
01192
01193 foreach ($types as $type) {
01194 $subtype_ids = array();
01195 if ($subtypes) {
01196 foreach ($subtypes as $subtype) {
01197
01198 if (!$subtype && ELGG_ENTITIES_NO_VALUE === $subtype) {
01199
01200 $subtype_ids[] = ELGG_ENTITIES_NO_VALUE;
01201 } elseif (!$subtype) {
01202
01203
01204 continue;
01205 } else {
01206 $subtype_id = get_subtype_id($type, $subtype);
01207
01208 if ($subtype_id) {
01209 $subtype_ids[] = $subtype_id;
01210 } else {
01211 $valid_subtypes_count--;
01212 elgg_log("Type-subtype '$type:$subtype' does not exist!", 'NOTICE');
01213 continue;
01214 }
01215 }
01216 }
01217
01218
01219 if ($valid_subtypes_count <= 0) {
01220 return FALSE;
01221 }
01222 }
01223
01224 if (is_array($subtype_ids) && count($subtype_ids)) {
01225 $subtype_ids_str = implode(',', $subtype_ids);
01226 $wheres[] = "({$table}.type = '$type' AND {$table}.subtype IN ($subtype_ids_str))";
01227 } else {
01228 $wheres[] = "({$table}.type = '$type')";
01229 }
01230 }
01231 } else {
01232
01233 $valid_pairs_count = count($pairs);
01234 $valid_pairs_subtypes_count = 0;
01235
01236
01237
01238
01239 foreach ($pairs as $paired_type => $paired_subtypes) {
01240 if (!in_array($paired_type, $valid_types)) {
01241 $valid_pairs_count--;
01242 unset($pairs[array_search($paired_type, $pairs)]);
01243 } else {
01244 if ($paired_subtypes && !is_array($paired_subtypes)) {
01245 $pairs[$paired_type] = array($paired_subtypes);
01246 }
01247 $valid_pairs_subtypes_count += count($paired_subtypes);
01248 }
01249 }
01250
01251 if ($valid_pairs_count <= 0) {
01252 return FALSE;
01253 }
01254 foreach ($pairs as $paired_type => $paired_subtypes) {
01255
01256
01257 if (is_array($paired_subtypes)) {
01258 $paired_subtype_ids = array();
01259 foreach ($paired_subtypes as $paired_subtype) {
01260 if (ELGG_ENTITIES_NO_VALUE === $paired_subtype
01261 || ($paired_subtype_id = get_subtype_id($paired_type, $paired_subtype))) {
01262
01263 $paired_subtype_ids[] = (ELGG_ENTITIES_NO_VALUE === $paired_subtype) ?
01264 ELGG_ENTITIES_NO_VALUE : $paired_subtype_id;
01265 } else {
01266 $valid_pairs_subtypes_count--;
01267 elgg_log("Type-subtype '$paired_type:$paired_subtype' does not exist!", 'NOTICE');
01268
01269 continue;
01270 }
01271 }
01272
01273
01274 if ($valid_pairs_subtypes_count <= 0) {
01275 return FALSE;
01276 }
01277
01278
01279 if ($paired_subtype_ids_str = implode(',', $paired_subtype_ids)) {
01280 $wheres[] = "({$table}.type = '$paired_type'"
01281 . " AND {$table}.subtype IN ($paired_subtype_ids_str))";
01282 }
01283 } else {
01284 $wheres[] = "({$table}.type = '$paired_type')";
01285 }
01286 }
01287 }
01288
01289
01290 if (is_array($wheres) && count($wheres)) {
01291 $where = implode(' OR ', $wheres);
01292 return "($where)";
01293 }
01294
01295 return '';
01296 }
01297
01309 function elgg_get_guid_based_where_sql($column, $guids) {
01310
01311
01312 if (!$guids && $guids !== 0) {
01313 return '';
01314 }
01315
01316
01317 if (!is_array($guids)) {
01318 $guids = array($guids);
01319 }
01320
01321 $guids_sanitized = array();
01322 foreach ($guids as $guid) {
01323 if ($guid !== ELGG_ENTITIES_NO_VALUE) {
01324 $guid = sanitise_int($guid);
01325
01326 if (!$guid) {
01327 return false;
01328 }
01329 }
01330 $guids_sanitized[] = $guid;
01331 }
01332
01333 $where = '';
01334 $guid_str = implode(',', $guids_sanitized);
01335
01336
01337 if ($guid_str !== FALSE && $guid_str !== '') {
01338 $where = "($column IN ($guid_str))";
01339 }
01340
01341 return $where;
01342 }
01343
01358 function elgg_get_entity_time_where_sql($table, $time_created_upper = NULL,
01359 $time_created_lower = NULL, $time_updated_upper = NULL, $time_updated_lower = NULL) {
01360
01361 $wheres = array();
01362
01363
01364 if ($time_created_upper && $time_created_upper == sanitise_int($time_created_upper)) {
01365 $wheres[] = "{$table}.time_created <= $time_created_upper";
01366 }
01367
01368 if ($time_created_lower && $time_created_lower == sanitise_int($time_created_lower)) {
01369 $wheres[] = "{$table}.time_created >= $time_created_lower";
01370 }
01371
01372 if ($time_updated_upper && $time_updated_upper == sanitise_int($time_updated_upper)) {
01373 $wheres[] = "{$table}.time_updated <= $time_updated_upper";
01374 }
01375
01376 if ($time_updated_lower && $time_updated_lower == sanitise_int($time_updated_lower)) {
01377 $wheres[] = "{$table}.time_updated >= $time_updated_lower";
01378 }
01379
01380 if (is_array($wheres) && count($wheres) > 0) {
01381 $where_str = implode(' AND ', $wheres);
01382 return "($where_str)";
01383 }
01384
01385 return '';
01386 }
01387
01413 function elgg_list_entities(array $options = array(), $getter = 'elgg_get_entities',
01414 $viewer = 'elgg_view_entity_list') {
01415
01416 global $autofeed;
01417 $autofeed = true;
01418
01419 $defaults = array(
01420 'offset' => (int) max(get_input('offset', 0), 0),
01421 'limit' => (int) max(get_input('limit', 10), 0),
01422 'full_view' => TRUE,
01423 'list_type_toggle' => FALSE,
01424 'pagination' => TRUE,
01425 );
01426
01427 $options = array_merge($defaults, $options);
01428
01429
01430 if (isset($options['view_type_toggle'])) {
01431 $options['list_type_toggle'] = $options['view_type_toggle'];
01432 }
01433
01434 $options['count'] = TRUE;
01435 $count = $getter($options);
01436
01437 $options['count'] = FALSE;
01438 $entities = $getter($options);
01439
01440 $options['count'] = $count;
01441
01442 return $viewer($entities, $options);
01443 }
01444
01462 function get_entity_dates($type = '', $subtype = '', $container_guid = 0, $site_guid = 0,
01463 $order_by = 'time_created') {
01464
01465 global $CONFIG;
01466
01467 $site_guid = (int) $site_guid;
01468 if ($site_guid == 0) {
01469 $site_guid = $CONFIG->site_guid;
01470 }
01471 $where = array();
01472
01473 if ($type != "") {
01474 $type = sanitise_string($type);
01475 $where[] = "type='$type'";
01476 }
01477
01478 if (is_array($subtype)) {
01479 $tempwhere = "";
01480 if (sizeof($subtype)) {
01481 foreach ($subtype as $typekey => $subtypearray) {
01482 foreach ($subtypearray as $subtypeval) {
01483 $typekey = sanitise_string($typekey);
01484 if (!empty($subtypeval)) {
01485 if (!$subtypeval = (int) get_subtype_id($typekey, $subtypeval)) {
01486 return false;
01487 }
01488 } else {
01489 $subtypeval = 0;
01490 }
01491 if (!empty($tempwhere)) {
01492 $tempwhere .= " or ";
01493 }
01494 $tempwhere .= "(type = '{$typekey}' and subtype = {$subtypeval})";
01495 }
01496 }
01497 }
01498 if (!empty($tempwhere)) {
01499 $where[] = "({$tempwhere})";
01500 }
01501 } else {
01502 if ($subtype) {
01503 if (!$subtype_id = get_subtype_id($type, $subtype)) {
01504 return FALSE;
01505 } else {
01506 $where[] = "subtype=$subtype_id";
01507 }
01508 }
01509 }
01510
01511 if ($container_guid !== 0) {
01512 if (is_array($container_guid)) {
01513 foreach ($container_guid as $key => $val) {
01514 $container_guid[$key] = (int) $val;
01515 }
01516 $where[] = "container_guid in (" . implode(",", $container_guid) . ")";
01517 } else {
01518 $container_guid = (int) $container_guid;
01519 $where[] = "container_guid = {$container_guid}";
01520 }
01521 }
01522
01523 if ($site_guid > 0) {
01524 $where[] = "site_guid = {$site_guid}";
01525 }
01526
01527 $where[] = get_access_sql_suffix();
01528
01529 $sql = "SELECT DISTINCT EXTRACT(YEAR_MONTH FROM FROM_UNIXTIME(time_created)) AS yearmonth
01530 FROM {$CONFIG->dbprefix}entities where ";
01531
01532 foreach ($where as $w) {
01533 $sql .= " $w and ";
01534 }
01535
01536 $sql .= "1=1 ORDER BY $order_by";
01537 if ($result = get_data($sql)) {
01538 $endresult = array();
01539 foreach ($result as $res) {
01540 $endresult[] = $res->yearmonth;
01541 }
01542 return $endresult;
01543 }
01544 return false;
01545 }
01546
01569 function disable_entity($guid, $reason = "", $recursive = true) {
01570 global $CONFIG;
01571
01572 $guid = (int)$guid;
01573 $reason = sanitise_string($reason);
01574
01575 if ($entity = get_entity($guid)) {
01576 if (elgg_trigger_event('disable', $entity->type, $entity)) {
01577 if ($entity->canEdit()) {
01578 if ($reason) {
01579 create_metadata($guid, 'disable_reason', $reason, '', 0, ACCESS_PUBLIC);
01580 }
01581
01582 if ($recursive) {
01583 $hidden = access_get_show_hidden_status();
01584 access_show_hidden_entities(true);
01585 $ia = elgg_set_ignore_access(true);
01586
01587 $sub_entities = get_data("SELECT * FROM {$CONFIG->dbprefix}entities
01588 WHERE (
01589 container_guid = $guid
01590 OR owner_guid = $guid
01591 OR site_guid = $guid
01592 ) AND enabled='yes'", 'entity_row_to_elggstar');
01593
01594 if ($sub_entities) {
01595 foreach ($sub_entities as $e) {
01596 add_entity_relationship($e->guid, 'disabled_with', $entity->guid);
01597 $e->disable($reason);
01598 }
01599 }
01600 access_show_hidden_entities($hidden);
01601 elgg_set_ignore_access($ia);
01602 }
01603
01604 $entity->disableMetadata();
01605 $entity->disableAnnotations();
01606 _elgg_invalidate_cache_for_entity($guid);
01607
01608 $res = update_data("UPDATE {$CONFIG->dbprefix}entities
01609 SET enabled = 'no'
01610 WHERE guid = $guid");
01611
01612 return $res;
01613 }
01614 }
01615 }
01616 return false;
01617 }
01618
01630 function enable_entity($guid, $recursive = true) {
01631 global $CONFIG;
01632
01633 $guid = (int)$guid;
01634
01635
01636 $old_access_status = access_get_show_hidden_status();
01637 access_show_hidden_entities(true);
01638
01639 $result = false;
01640 if ($entity = get_entity($guid)) {
01641 if (elgg_trigger_event('enable', $entity->type, $entity)) {
01642 if ($entity->canEdit()) {
01643
01644 $result = update_data("UPDATE {$CONFIG->dbprefix}entities
01645 SET enabled = 'yes'
01646 WHERE guid = $guid");
01647
01648 $entity->deleteMetadata('disable_reason');
01649 $entity->enableMetadata();
01650 $entity->enableAnnotations();
01651
01652 if ($recursive) {
01653 $disabled_with_it = elgg_get_entities_from_relationship(array(
01654 'relationship' => 'disabled_with',
01655 'relationship_guid' => $entity->guid,
01656 'inverse_relationship' => true,
01657 'limit' => 0,
01658 ));
01659
01660 foreach ($disabled_with_it as $e) {
01661 $e->enable();
01662 remove_entity_relationship($e->guid, 'disabled_with', $entity->guid);
01663 }
01664 }
01665 }
01666 }
01667 }
01668
01669 access_show_hidden_entities($old_access_status);
01670 return $result;
01671 }
01672
01694 function delete_entity($guid, $recursive = true) {
01695 global $CONFIG, $ENTITY_CACHE;
01696
01697 $guid = (int)$guid;
01698 if ($entity = get_entity($guid)) {
01699 if (elgg_trigger_event('delete', $entity->type, $entity)) {
01700 if ($entity->canEdit()) {
01701
01702
01703 if (isset($ENTITY_CACHE[$guid])) {
01704 _elgg_invalidate_cache_for_entity($guid);
01705 }
01706
01707
01708 static $newentity_cache;
01709 if ((!$newentity_cache) && (is_memcache_available())) {
01710 $newentity_cache = new ElggMemcache('new_entity_cache');
01711 }
01712 if ($newentity_cache) {
01713 $newentity_cache->delete($guid);
01714 }
01715
01716
01717 if ($recursive) {
01718
01719
01720 static $__RECURSIVE_DELETE_TOKEN;
01721
01722 $__RECURSIVE_DELETE_TOKEN = md5(elgg_get_logged_in_user_guid());
01723
01724 $entity_disable_override = access_get_show_hidden_status();
01725 access_show_hidden_entities(true);
01726 $ia = elgg_set_ignore_access(true);
01727
01728
01729
01730
01731 $options = array(
01732 'wheres' => array(
01733 "((container_guid = $guid OR owner_guid = $guid OR site_guid = $guid)"
01734 . " AND guid != $guid)"
01735 ),
01736 'limit' => 0
01737 );
01738
01739 $batch = new ElggBatch('elgg_get_entities', $options);
01740 $batch->setIncrementOffset(false);
01741
01742 foreach ($batch as $e) {
01743 $e->delete(true);
01744 }
01745
01746 access_show_hidden_entities($entity_disable_override);
01747 $__RECURSIVE_DELETE_TOKEN = null;
01748 elgg_set_ignore_access($ia);
01749 }
01750
01751 $entity_disable_override = access_get_show_hidden_status();
01752 access_show_hidden_entities(true);
01753 $ia = elgg_set_ignore_access(true);
01754
01755
01756 $entity->deleteMetadata();
01757 $entity->deleteOwnedMetadata();
01758 $entity->deleteAnnotations();
01759 $entity->deleteOwnedAnnotations();
01760 $entity->deleteRelationships();
01761
01762 access_show_hidden_entities($entity_disable_override);
01763 elgg_set_ignore_access($ia);
01764
01765 elgg_delete_river(array('subject_guid' => $guid));
01766 elgg_delete_river(array('object_guid' => $guid));
01767 remove_all_private_settings($guid);
01768
01769 $res = delete_data("DELETE from {$CONFIG->dbprefix}entities where guid={$guid}");
01770 if ($res) {
01771 $sub_table = "";
01772
01773
01774 switch ($entity->type) {
01775 case 'object' :
01776 $sub_table = $CONFIG->dbprefix . 'objects_entity';
01777 break;
01778 case 'user' :
01779 $sub_table = $CONFIG->dbprefix . 'users_entity';
01780 break;
01781 case 'group' :
01782 $sub_table = $CONFIG->dbprefix . 'groups_entity';
01783 break;
01784 case 'site' :
01785 $sub_table = $CONFIG->dbprefix . 'sites_entity';
01786 break;
01787 }
01788
01789 if ($sub_table) {
01790 delete_data("DELETE from $sub_table where guid={$guid}");
01791 }
01792 }
01793
01794 return (bool)$res;
01795 }
01796 }
01797 }
01798 return false;
01799
01800 }
01801
01816 function volatile_data_export_plugin_hook($hook, $entity_type, $returnvalue, $params) {
01817 $guid = (int)$params['guid'];
01818 $variable_name = sanitise_string($params['varname']);
01819
01820 if (($hook == 'volatile') && ($entity_type == 'metadata')) {
01821 if (($guid) && ($variable_name)) {
01822 switch ($variable_name) {
01823 case 'renderedentity' :
01824 elgg_set_viewtype('default');
01825 $view = elgg_view_entity(get_entity($guid));
01826 elgg_set_viewtype();
01827
01828 $tmp = new ElggMetadata();
01829 $tmp->type = 'volatile';
01830 $tmp->name = 'renderedentity';
01831 $tmp->value = $view;
01832 $tmp->entity_guid = $guid;
01833
01834 return $tmp;
01835
01836 break;
01837 }
01838 }
01839 }
01840 }
01841
01858 function export_entity_plugin_hook($hook, $entity_type, $returnvalue, $params) {
01859
01860 if ((!is_array($params)) && (!isset($params['guid']))) {
01861 throw new InvalidParameterException(elgg_echo('InvalidParameterException:GUIDNotForExport'));
01862 }
01863
01864 if (!is_array($returnvalue)) {
01865 throw new InvalidParameterException(elgg_echo('InvalidParameterException:NonArrayReturnValue'));
01866 }
01867
01868 $guid = (int)$params['guid'];
01869
01870
01871 $entity = get_entity($guid);
01872 if (!($entity instanceof ElggEntity)) {
01873 $msg = elgg_echo('InvalidClassException:NotValidElggStar', array($guid, get_class()));
01874 throw new InvalidClassException($msg);
01875 }
01876
01877 $export = $entity->export();
01878
01879 if (is_array($export)) {
01880 foreach ($export as $e) {
01881 $returnvalue[] = $e;
01882 }
01883 } else {
01884 $returnvalue[] = $export;
01885 }
01886
01887 return $returnvalue;
01888 }
01889
01902 function oddentity_to_elggentity(ODDEntity $element) {
01903 $class = $element->getAttribute('class');
01904 $subclass = $element->getAttribute('subclass');
01905
01906
01907 $tmp = get_entity_from_uuid($element->getAttribute('uuid'));
01908
01909 if (!$tmp) {
01910
01911 $classname = get_subtype_class($class, $subclass);
01912 if ($classname) {
01913 if (class_exists($classname)) {
01914 $tmp = new $classname();
01915
01916 if (!($tmp instanceof ElggEntity)) {
01917 $msg = elgg_echo('ClassException:ClassnameNotClass', array($classname, get_class()));
01918 throw new ClassException($msg);
01919 }
01920 } else {
01921 error_log(elgg_echo('ClassNotFoundException:MissingClass', array($classname)));
01922 }
01923 } else {
01924 switch ($class) {
01925 case 'object' :
01926 $tmp = new ElggObject($row);
01927 break;
01928 case 'user' :
01929 $tmp = new ElggUser($row);
01930 break;
01931 case 'group' :
01932 $tmp = new ElggGroup($row);
01933 break;
01934 case 'site' :
01935 $tmp = new ElggSite($row);
01936 break;
01937 default:
01938 $msg = elgg_echo('InstallationException:TypeNotSupported', array($class));
01939 throw new InstallationException($msg);
01940 }
01941 }
01942 }
01943
01944 if ($tmp) {
01945 if (!$tmp->import($element)) {
01946 $msg = elgg_echo('ImportException:ImportFailed', array($element->getAttribute('uuid')));
01947 throw new ImportException($msg);
01948 }
01949
01950 return $tmp;
01951 }
01952
01953 return NULL;
01954 }
01955
01975 function import_entity_plugin_hook($hook, $entity_type, $returnvalue, $params) {
01976 $element = $params['element'];
01977
01978 $tmp = null;
01979
01980 if ($element instanceof ODDEntity) {
01981 $tmp = oddentity_to_elggentity($element);
01982
01983 if ($tmp) {
01984
01985 if (!$tmp->save()) {
01986 $msg = elgg_echo('ImportException:ProblemSaving', array($element->getAttribute('uuid')));
01987 throw new ImportException($msg);
01988 }
01989
01990
01991 if (!$tmp->guid) {
01992 throw new ImportException(elgg_echo('ImportException:NoGUID'));
01993 }
01994
01995
01996 add_uuid_to_guid($tmp->guid, $element->getAttribute('uuid'));
01997
01998 return $tmp;
01999 }
02000 }
02001 }
02002
02019 function can_edit_entity($entity_guid, $user_guid = 0) {
02020 $user_guid = (int)$user_guid;
02021 $user = get_entity($user_guid);
02022 if (!$user) {
02023 $user = elgg_get_logged_in_user_entity();
02024 }
02025
02026 $return = false;
02027 if ($entity = get_entity($entity_guid)) {
02028
02029
02030 if ($user) {
02031 if ($entity->getOwnerGUID() == $user->getGUID()) {
02032 $return = true;
02033 }
02034 if ($entity->container_guid == $user->getGUID()) {
02035 $return = true;
02036 }
02037 if ($entity->type == "user" && $entity->getGUID() == $user->getGUID()) {
02038 $return = true;
02039 }
02040 if ($container_entity = get_entity($entity->container_guid)) {
02041 if ($container_entity->canEdit($user->getGUID())) {
02042 $return = true;
02043 }
02044 }
02045 }
02046 }
02047
02048 return elgg_trigger_plugin_hook('permissions_check', $entity->type,
02049 array('entity' => $entity, 'user' => $user), $return);
02050 }
02051
02067 function can_edit_entity_metadata($entity_guid, $user_guid = 0, $metadata = null) {
02068 if ($entity = get_entity($entity_guid)) {
02069
02070 $return = null;
02071
02072 if ($metadata->owner_guid == 0) {
02073 $return = true;
02074 }
02075 if (is_null($return)) {
02076 $return = can_edit_entity($entity_guid, $user_guid);
02077 }
02078
02079 if ($user_guid) {
02080 $user = get_entity($user_guid);
02081 } else {
02082 $user = elgg_get_logged_in_user_entity();
02083 }
02084
02085 $params = array('entity' => $entity, 'user' => $user, 'metadata' => $metadata);
02086 $return = elgg_trigger_plugin_hook('permissions_check:metadata', $entity->type, $params, $return);
02087 return $return;
02088 } else {
02089 return false;
02090 }
02091 }
02092
02103 function get_entity_url($entity_guid) {
02104 global $CONFIG;
02105
02106 if ($entity = get_entity($entity_guid)) {
02107 $url = "";
02108
02109 if (isset($CONFIG->entity_url_handler[$entity->getType()][$entity->getSubType()])) {
02110 $function = $CONFIG->entity_url_handler[$entity->getType()][$entity->getSubType()];
02111 if (is_callable($function)) {
02112 $url = call_user_func($function, $entity);
02113 }
02114 } elseif (isset($CONFIG->entity_url_handler[$entity->getType()]['all'])) {
02115 $function = $CONFIG->entity_url_handler[$entity->getType()]['all'];
02116 if (is_callable($function)) {
02117 $url = call_user_func($function, $entity);
02118 }
02119 } elseif (isset($CONFIG->entity_url_handler['all']['all'])) {
02120 $function = $CONFIG->entity_url_handler['all']['all'];
02121 if (is_callable($function)) {
02122 $url = call_user_func($function, $entity);
02123 }
02124 }
02125
02126 if ($url == "") {
02127 $url = "view/" . $entity_guid;
02128 }
02129
02130 return elgg_normalize_url($url);
02131 }
02132
02133 return false;
02134 }
02135
02148 function elgg_register_entity_url_handler($entity_type, $entity_subtype, $function_name) {
02149 global $CONFIG;
02150
02151 if (!is_callable($function_name, true)) {
02152 return false;
02153 }
02154
02155 if (!isset($CONFIG->entity_url_handler)) {
02156 $CONFIG->entity_url_handler = array();
02157 }
02158
02159 if (!isset($CONFIG->entity_url_handler[$entity_type])) {
02160 $CONFIG->entity_url_handler[$entity_type] = array();
02161 }
02162
02163 $CONFIG->entity_url_handler[$entity_type][$entity_subtype] = $function_name;
02164
02165 return true;
02166 }
02167
02184 function elgg_register_entity_type($type, $subtype = null) {
02185 global $CONFIG;
02186
02187 $type = strtolower($type);
02188 if (!in_array($type, $CONFIG->entity_types)) {
02189 return FALSE;
02190 }
02191
02192 if (!isset($CONFIG->registered_entities)) {
02193 $CONFIG->registered_entities = array();
02194 }
02195
02196 if (!isset($CONFIG->registered_entities[$type])) {
02197 $CONFIG->registered_entities[$type] = array();
02198 }
02199
02200 if ($subtype) {
02201 $CONFIG->registered_entities[$type][] = $subtype;
02202 }
02203
02204 return TRUE;
02205 }
02206
02219 function unregister_entity_type($type, $subtype) {
02220 global $CONFIG;
02221
02222 $type = strtolower($type);
02223 if (!in_array($type, $CONFIG->entity_types)) {
02224 return FALSE;
02225 }
02226
02227 if (!isset($CONFIG->registered_entities)) {
02228 return FALSE;
02229 }
02230
02231 if (!isset($CONFIG->registered_entities[$type])) {
02232 return FALSE;
02233 }
02234
02235 if ($subtype) {
02236 if (in_array($subtype, $CONFIG->registered_entities[$type])) {
02237 $key = array_search($subtype, $CONFIG->registered_entities[$type]);
02238 unset($CONFIG->registered_entities[$type][$key]);
02239 } else {
02240 return FALSE;
02241 }
02242 } else {
02243 unset($CONFIG->registered_entities[$type]);
02244 }
02245
02246 return TRUE;
02247 }
02248
02257 function get_registered_entity_types($type = null) {
02258 global $CONFIG;
02259
02260 if (!isset($CONFIG->registered_entities)) {
02261 return false;
02262 }
02263 if ($type) {
02264 $type = strtolower($type);
02265 }
02266 if (!empty($type) && empty($CONFIG->registered_entities[$type])) {
02267 return false;
02268 }
02269
02270 if (empty($type)) {
02271 return $CONFIG->registered_entities;
02272 }
02273
02274 return $CONFIG->registered_entities[$type];
02275 }
02276
02285 function is_registered_entity_type($type, $subtype = null) {
02286 global $CONFIG;
02287
02288 if (!isset($CONFIG->registered_entities)) {
02289 return false;
02290 }
02291
02292 $type = strtolower($type);
02293
02294
02295
02296 if (!isset($CONFIG->registered_entities[$type])) {
02297 return false;
02298 }
02299
02300 if ($subtype && !in_array($subtype, $CONFIG->registered_entities[$type])) {
02301 return false;
02302 }
02303 return true;
02304 }
02305
02315 function entities_page_handler($page) {
02316 if (isset($page[0])) {
02317 global $CONFIG;
02318 set_input('guid', $page[0]);
02319 include($CONFIG->path . "pages/entities/index.php");
02320 return true;
02321 }
02322 return false;
02323 }
02324
02343 function elgg_list_registered_entities(array $options = array()) {
02344 global $autofeed;
02345 $autofeed = true;
02346
02347 $defaults = array(
02348 'full_view' => TRUE,
02349 'allowed_types' => TRUE,
02350 'list_type_toggle' => FALSE,
02351 'pagination' => TRUE,
02352 'offset' => 0,
02353 'types' => array(),
02354 'type_subtype_pairs' => array()
02355 );
02356
02357 $options = array_merge($defaults, $options);
02358
02359
02360 if (isset($options['view_type_toggle'])) {
02361 $options['list_type_toggle'] = $options['view_type_toggle'];
02362 }
02363
02364 $types = get_registered_entity_types();
02365
02366 foreach ($types as $type => $subtype_array) {
02367 if (in_array($type, $options['allowed_types']) || $options['allowed_types'] === TRUE) {
02368
02369 if ($type == 'object') {
02370 if (is_array($subtype_array) && count($subtype_array)) {
02371 $options['type_subtype_pairs'][$type] = $subtype_array;
02372 }
02373 } else {
02374 if (is_array($subtype_array) && count($subtype_array)) {
02375 $options['type_subtype_pairs'][$type] = $subtype_array;
02376 } else {
02377 $options['type_subtype_pairs'][$type] = ELGG_ENTITIES_ANY_VALUE;
02378 }
02379 }
02380 }
02381 }
02382
02383 if (!empty($options['type_subtype_pairs'])) {
02384 $count = elgg_get_entities(array_merge(array('count' => TRUE), $options));
02385 $entities = elgg_get_entities($options);
02386 } else {
02387 $count = 0;
02388 $entities = array();
02389 }
02390
02391 $options['count'] = $count;
02392 return elgg_view_entity_list($entities, $options);
02393 }
02394
02409 function elgg_instanceof($entity, $type = NULL, $subtype = NULL, $class = NULL) {
02410 $return = ($entity instanceof ElggEntity);
02411
02412 if ($type) {
02413
02414 $return = $return && ($entity->getType() == $type);
02415 }
02416
02417 if ($subtype) {
02418 $return = $return && ($entity->getSubtype() == $subtype);
02419 }
02420
02421 if ($class) {
02422 $return = $return && ($entity instanceof $class);
02423 }
02424
02425 return $return;
02426 }
02427
02440 function update_entity_last_action($guid, $posted = NULL) {
02441 global $CONFIG;
02442 $guid = (int)$guid;
02443 $posted = (int)$posted;
02444
02445 if (!$posted) {
02446 $posted = time();
02447 }
02448
02449 if ($guid) {
02450
02451 $query = "UPDATE {$CONFIG->dbprefix}entities SET last_action = {$posted} WHERE guid = {$guid}";
02452 $result = update_data($query);
02453 if ($result) {
02454 return TRUE;
02455 } else {
02456 return FALSE;
02457 }
02458 } else {
02459 return FALSE;
02460 }
02461 }
02462
02470 function entities_gc() {
02471 global $CONFIG;
02472
02473 $tables = array ('sites_entity', 'objects_entity', 'groups_entity', 'users_entity');
02474
02475 foreach ($tables as $table) {
02476 delete_data("DELETE from {$CONFIG->dbprefix}{$table}
02477 where guid NOT IN (SELECT guid from {$CONFIG->dbprefix}entities)");
02478 }
02479 }
02480
02492 function entities_test($hook, $type, $value, $params) {
02493 global $CONFIG;
02494 $value[] = $CONFIG->path . 'engine/tests/objects/entities.php';
02495 return $value;
02496 }
02497
02505 function entities_init() {
02506 elgg_register_page_handler('view', 'entities_page_handler');
02507
02508 elgg_register_plugin_hook_handler('unit_test', 'system', 'entities_test');
02509
02510 elgg_register_plugin_hook_handler('gc', 'system', 'entities_gc');
02511 }
02512
02514 elgg_register_plugin_hook_handler("import", "all", "import_entity_plugin_hook", 0);
02515
02517 elgg_register_plugin_hook_handler("export", "all", "export_entity_plugin_hook", 0);
02518
02520 elgg_register_plugin_hook_handler('volatile', 'metadata', 'volatile_data_export_plugin_hook');
02521
02523 elgg_register_event_handler('init', 'system', 'entities_init');
02524