1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: 16: 17:
18: class UBSite {
19: 20: 21:
22: const SUBTYPE = "UBSite";
23: public $id = 0;
24: public $name = "";
25: public $description = "";
26: public $url = "";
27: public $owner_id = 0;
28: public $time_created = 0;
29:
30: 31: 32: 33:
34: function __construct() {
35: $elgg_entity = elgg_get_site_entity();
36: $this->copy_from_elgg($elgg_entity);
37: }
38:
39: 40: 41: 42: 43:
44: protected function copy_from_elgg($elgg_entity) {
45: $this->id = (int)$elgg_entity->get("guid");
46: $this->name = (string)$elgg_entity->get("name");
47: $this->description = (string)$elgg_entity->get("description");
48: $this->url = (string)$elgg_entity->get("url");
49: $this->owner_id = (int)$elgg_entity->getOwnerGUID();
50: $this->time_created = (int)$elgg_entity->getTimeCreated();
51: }
52:
53: 54: 55: 56:
57: protected function save() {
58: $elgg_entity = elgg_get_site_entity();
59: $this->copy_to_elgg($elgg_entity);
60: $elgg_entity->save();
61: return $this->id = $elgg_entity->get("guid");
62: }
63:
64: 65: 66:
67: protected function copy_to_elgg($elgg_entity) {
68: $elgg_entity->set("name", (string)$this->name);
69: $elgg_entity->set("description", (string)$this->description);
70: $elgg_entity->set("url", (string)$this->url);
71: }
72:
73: static function get_site() {
74: return new static();
75: }
76:
77: static function get_site_id() {
78: return (int)datalist_get("default_site");
79: }
80:
81: 82: 83: 84: 85: 86: 87: 88: 89: 90:
91: static function get_all($limit = 0, $offset = 0, $order_by = "", $ascending = true, $id_only = false) {
92: if($id_only) {
93: return static::get_site_id();
94: } else{
95: return array(static::get_site_id() => static::get_site());
96: }
97: }
98:
99: 100: 101: 102:
103: static function api_list() {
104: return list_all_apis();
105: }
106:
107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117:
118: static function get_token($login, $password = "", $timeout = 60) {
119: global $CONFIG;
120: if (!elgg_get_logged_in_user_guid()) {
121: if (elgg_authenticate($login, $password) !== true) {
122: throw new SecurityException(elgg_echo('SecurityException:authenticationfailed'));
123: }
124: }
125: $user = get_user_by_username($login);
126: $query = "select * from {$CONFIG->dbprefix}users_apisessions where user_guid = {$user->guid};";
127: $row = get_data_row($query);
128: if (isset($row->token)) {
129: $timeout = $timeout * 60 * 1000;
130:
131: if (((int)$row->expires - time()) < $timeout) {
132: $new_expires = time() + $timeout;
133: $update_timeout_query = "update {$CONFIG->dbprefix}users_apisessions set expires = {$new_expires} where user_guid = {$user->guid};";
134: update_data($update_timeout_query);
135: }
136: return $row->token;
137: } else {
138: return create_user_token($login, $timeout);
139: }
140: }
141:
142: static function remove_token($token) {
143: return remove_user_token($token, null);
144: }
145:
146: static function lookup($id) {
147: if(empty($id)){
148: return null;
149: }
150: try {
151: $elgg_object = new ElggObject((int)$id);
152: $object['type'] = (string)$elgg_object->type;
153: $object['subtype'] = (string)get_subtype_from_id($elgg_object->subtype);
154: $object['name'] = (string)$elgg_object->get("name");
155: $object['description'] = (string)$elgg_object->description;
156:
157: return $object;
158: } catch (Exception $e) {
159: try {
160: $elgg_user = new ElggUser((int)$id);
161: $object['type'] = (string)$elgg_user->type;
162: $object['subtype'] = (string)get_subtype_from_id($elgg_user->subtype);
163: return $object;
164: } catch (Exception $e) {
165: return null;
166: }
167: }
168: }
169:
170: static function get_domain() {
171: $site = elgg_get_site_entity();
172: $urlData = parse_url($site->url);
173: $hostData = explode('.', $urlData['host']);
174: $hostData = array_reverse($hostData);
175: return $hostData[1] . '.' . $hostData[0];
176: }
177:
178:
179: static function normalize_xml_key($key) {
180: return str_replace(array('!', '"', '#', '$', '%', '&', '(', ')', '*', '+', ',', '/', ';', '<', '=', '>', '?', '@', '\\', '[', ']', '^', '`', '{', '}', '|', '~'),
181: '_',
182: $key);
183: }
184:
185:
186: 187: 188: 189:
190: static function list_properties() {
191: return get_class_vars(get_called_class());
192: }
193:
194: }