1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: 16: 17:
18: class UBUser extends UBItem {
19: 20: 21:
22: const TYPE = "user";
23: 24: 25:
26: const SUBTYPE = "";
27:
28:
29: public $login = "";
30:
31: public $password = "";
32:
33: public $email = "";
34:
35: public $role = "user";
36:
37: public $language = "";
38:
39: public $last_login = 0;
40:
41: public $avatar_file = 0;
42:
43: private $hash = "";
44:
45: 46: 47: 48: 49: 50: 51: 52:
53: function __construct($id = null, $elgg_user = null) {
54: if (!empty($id)) {
55: if (empty($elgg_user)) {
56: if (!($elgg_user = new ElggUser($id))) {
57: throw new APIException("ERROR: Failed to load " . get_called_class() . " object with ID '" . $id . "'.");
58: }
59: }
60: $this->copy_from_elgg($elgg_user);
61: }
62: }
63:
64: 65: 66: 67: 68:
69: protected function copy_from_elgg($elgg_user) {
70: parent::copy_from_elgg($elgg_user);
71: $this->email = (string)$elgg_user->email;
72: $this->login = (string)$elgg_user->username;
73: $this->password = (string)$elgg_user->password;
74: $this->hash = (string)$elgg_user->salt;
75: $this->role = (string)$elgg_user->get("role");
76: $this->language = (string)$elgg_user->language;
77: $this->last_login = (int)$elgg_user->get("last_login");
78: $this->avatar_file = (int)$elgg_user->get("avatar_file");
79: }
80:
81: 82: 83: 84:
85: protected function save() {
86: if (empty($this->id)) {
87: $elgg_user = new ElggUser();
88: $elgg_user->subtype = (string)static::SUBTYPE;
89: } elseif (!$elgg_user = new ElggUser($this->id)) {
90: return false;
91: }
92: $this->copy_to_elgg($elgg_user);
93: $elgg_user->save();
94: return $this->id = $elgg_user->guid;
95: }
96:
97: 98: 99: 100: 101:
102: protected function copy_to_elgg($elgg_user) {
103: parent::copy_to_elgg($elgg_user);
104: $elgg_user->email = (string)$this->email;
105: $elgg_user->username = (string)$this->login;
106: $elgg_user->password = (string)$this->password;
107: $elgg_user->salt = (string)$this->hash;
108: $elgg_user->set("role", (string)$this->role);
109: if ($this->language == "") {
110: $elgg_user->language = get_language();
111: } else {
112: $elgg_user->language = $this->language;
113: }
114: $elgg_user->owner_guid = 0;
115: $elgg_user->container_guid = 0;
116: $elgg_user->set("avatar_file", (int)$this->avatar_file);
117: }
118:
119: 120: 121: 122: 123: 124: 125:
126: protected static function create_password($clear_password) {
127: if (!$clear_password) {
128: return false;
129: }
130: $new_password = array();
131: $new_password["hash"] = generate_random_cleartext_password();
132: $new_password["password"] = md5($clear_password . $new_password["hash"]);
133: return $new_password;
134: }
135:
136: 137: 138: 139: 140: 141: 142: 143: 144:
145: static function set_properties($id, $prop_value_array) {
146: if (!$item = new static($id)) {
147: return false;
148: }
149: $property_list = (array)static::list_properties();
150: foreach ($prop_value_array as $prop => $value) {
151: if (!array_key_exists($prop, $property_list)) {
152: throw new InvalidParameterException("ERROR: One or more property names do not exist.");
153: }
154: if ($prop == "id") {
155: throw new InvalidParameterException("ERROR: Cannot modify 'id' of instance.");
156: }
157:
158: $value = trim($value, " \t\n\r\0\x0B".chr(194).chr(160));
159: if ($prop == "password") {
160: $new_password = static::create_password($value);
161: $item->password = $new_password["password"];
162: $item->hash = $new_password["hash"];
163: continue;
164: }
165: if ($prop == "login" && empty($id)) {
166: $user_array = static::get_by_login(array($value));
167: if (!empty($user_array[$value])) {
168: return (int)$user_array[$value]->id;
169: }
170: }
171: $item->$prop = $value;
172: }
173: if(empty($item->name)){
174: $item->name = $item->login;
175: }
176: return $item->save();
177: }
178:
179: 180: 181: 182: 183: 184: 185: 186: 187:
188: static function login($login, $password, $persistent = false) {
189: if (!elgg_authenticate($login, $password)) {
190: return false;
191: }
192: $elgg_user = get_user_by_username($login);
193: return login($elgg_user, $persistent);
194: }
195:
196: 197: 198: 199:
200: static function logout() {
201: return logout();
202: }
203:
204: 205: 206: 207: 208: 209: 210:
211: static function get_by_login($login_array) {
212: $user_array = array();
213: foreach ($login_array as $login) {
214: $elgg_user = get_user_by_username($login);
215: if (!$elgg_user) {
216: $user_array[$login] = null;
217: } else {
218: $user_array[$login] = new static((int)$elgg_user->guid, $elgg_user);
219: }
220: }
221: return $user_array;
222: }
223:
224: 225: 226: 227: 228: 229: 230: 231: 232:
233: static function get_by_email($email_array) {
234: $user_array = array();
235: foreach ($email_array as $email) {
236: $elgg_user_array = get_user_by_email($email);
237: if (!$elgg_user_array) {
238: $user_array[UBSite::normalize_xml_key($email)] = null;
239: } else {
240: $temp_array = array();
241: foreach ($elgg_user_array as $elgg_user) {
242: $temp_array[] = new static((int)$elgg_user->guid, $elgg_user);
243: }
244: $user_array[UBSite::normalize_xml_key($email)] = $temp_array;
245: }
246: }
247: return $user_array;
248: }
249:
250: 251: 252: 253: 254: 255: 256:
257: static function get_by_role($role_array) {
258: $user_array = array();
259: foreach ($role_array as $role) {
260: $elgg_user_array = elgg_get_entities_from_metadata(array('type' => static::TYPE, 'subtype' => static::SUBTYPE, 'metadata_names' => array("role"), 'metadata_values' => array($role), 'limit' => 0));
261: if (!$elgg_user_array) {
262: $user_array[$role] = null;
263: } else {
264: $temp_array = array();
265: foreach ($elgg_user_array as $elgg_user) {
266: $temp_array[] = new static($elgg_user->guid, $elgg_user);
267: }
268: $user_array[$role] = $temp_array;
269: }
270: }
271: return $user_array;
272: }
273:
274: 275: 276: 277: 278: 279: 280:
281: static function get_last_login($id) {
282: $user = new static($id);
283: return $user->last_login;
284: }
285:
286: 287: 288: 289: 290: 291: 292: 293:
294: static function set_avatar($id, $file_id) {
295: $prop_value_array = array();
296: $prop_value_array["avatar_file"] = (int)$file_id;
297: return static::set_properties((int)$id, $prop_value_array);
298: }
299:
300: 301: 302: 303: 304: 305: 306: 307: 308:
309: static function get_avatar($id, $size = "medium") {
310: $prop_value_array = static::get_properties($id, array("avatar_file"));
311: $avatar_file = new ClipitFile((int)$prop_value_array["avatar_file"]);
312: if (empty($avatar_file)) {
313: return null;
314: }
315: $avatar = null;
316: switch ($size) {
317: case "small":
318: $avatar = (array)$avatar_file->thumb_small;
319: break;
320: case "medium":
321: $avatar = (array)$avatar_file->thumb_medium;
322: break;
323: case "large":
324: $avatar = (array)$avatar_file->thumb_large;
325: break;
326: }
327: return $avatar;
328: }
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:
341:
342:
343:
344:
345:
346:
347:
348:
349:
350:
351:
352:
353:
354:
355:
356:
357:
358:
359:
360:
361:
362:
363:
364:
365:
366:
367:
368:
369:
370:
371:
372:
373: 374: 375: 376: 377: 378: 379:
380: static function import_data($file_path) {
381: $php_excel = PHPExcel_IOFactory::load($file_path);
382: $user_array = array();
383: $row_iterator = $php_excel->getSheet()->getRowIterator();
384: while ($row_iterator->valid()) {
385: $row_result = (array)static::parse_excel_row($row_iterator->current());
386: if (!empty($row_result)) {
387: if(!empty($row_result["group"])){
388: $user_array[$row_result["group"]][] = (int)$row_result["user_id"];
389: } else{
390: $user_array[0][] = (int)$row_result["user_id"];
391: }
392: }
393: $row_iterator->next();
394: }
395: return $user_array;
396: }
397:
398: 399: 400: 401: 402: 403: 404:
405: private function parse_excel_row($row_iterator) {
406: $row_result = array();
407: $prop_value_array = array();
408: $cell_iterator = $row_iterator->getCellIterator();
409:
410: $value = $cell_iterator->current()->getValue();
411: if (empty($value) || strtolower($value) == "users" || strtolower($value) == "name") {
412: return $row_result;
413: }
414:
415: $name = $value;
416: $prop_value_array["name"] = trim($name, " \t\n\r\0\x0B".chr(194).chr(160));
417: $cell_iterator->next();
418:
419: $login = (string)$cell_iterator->current()->getValue();
420: if (!empty($login)) {
421: $prop_value_array["login"] = trim($login, " \t\n\r\0\x0B".chr(194).chr(160));
422: } else {
423: return $row_result;
424: }
425: $cell_iterator->next();
426:
427: $password = (string)$cell_iterator->current()->getValue();
428: if (!empty($password)) {
429: $prop_value_array["password"] = trim($password, " \t\n\r\0\x0B".chr(194).chr(160));
430: } else {
431: return $row_result;
432: }
433: $cell_iterator->next();
434:
435: $email = (string)$cell_iterator->current()->getValue();
436: if (!empty($email)) {
437: $prop_value_array["email"] = trim($email, " \t\n\r\0\x0B".chr(194).chr(160));
438: }
439: $cell_iterator->next();
440:
441: $row_result["user_id"] = (int)static::create($prop_value_array);
442:
443: $group = (string)$cell_iterator->current()->getValue();
444: if(!empty($group)){
445: $row_result["group"] = trim($group, " \t\n\r\0\x0B".chr(194).chr(160));
446: } else{
447: $row_result["group"] = null;
448: }
449: return $row_result;
450: }
451: }