Overview

Packages

  • ClipIt
    • clipit
      • api
    • urjc
      • backend
  • Elgg
    • Core
      • Access
      • Authentication
      • Cache
      • Caches
      • Core
      • DataMode
        • Site
      • DataModel
        • Annotations
        • Entities
        • Extender
        • File
        • Importable
        • Loggable
        • Notable
        • Object
        • User
      • DataStorage
      • Exception
      • Exceptions
        • Stub
      • FileStore
        • Disk
      • Groups
      • Helpers
      • HMAC
      • Memcache
      • Metadata
      • Navigation
      • ODD
      • Output
      • Plugins
        • Settings
      • Sessions
      • SocialModel
        • Friendable
        • Locatable
      • WebServicesAPI
      • Widgets
      • XML
      • XMLRPC
    • Exceptions
      • Stub
  • None
  • PHP

Classes

  • UBCollection
  • UBEvent
  • UBFile
  • UBItem
  • UBMessage
  • UBSite
  • UBUser
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * ClipIt - JuxtaLearn Web Space
  4:  * PHP version:     >= 5.2
  5:  * Creation date:   2013-10-10
  6:  * Last update:     $Date$
  7:  * @author          Pablo LlinĂ¡s Arnaiz <pebs74@gmail.com>, URJC JuxtaLearn Team
  8:  * @version         $Version$
  9:  * @link            http://www.juxtalearn.eu
 10:  * @license         GNU Affero General Public License v3
 11:  * @package         ClipIt
 12:  * @subpackage      urjc_backend
 13:  */
 14: 
 15: /**
 16:  * <Class Description>
 17:  */
 18: class UBUser extends UBItem {
 19:     /**
 20:      * @const string Elgg entity TYPE for this class
 21:      */
 22:     const TYPE = "user";
 23:     /**
 24:      * @const string Elgg entity SUBTYPE for this class
 25:      */
 26:     const SUBTYPE = "";
 27: 
 28:     // @var string $login Login name used to authenticate, must be unique
 29:     public $login = "";
 30:     // @var string $password Login password (md5 of password + password_hash)
 31:     public $password = "";
 32:     // @var string $email User email
 33:     public $email = "";
 34:     // @var string $role User role (default: "user")
 35:     public $role = "user";
 36:     // @var string $language User interface language
 37:     public $language = "";
 38:     // @var int $last_login Timestamp from last login
 39:     public $last_login = 0;
 40:     // @var int $avatar_file Id of file containing Avatar
 41:     public $avatar_file = 0;
 42:     // @var string $hash Random string to encode password
 43:     private $hash = "";
 44: 
 45:     /**
 46:      * Constructor
 47:      *
 48:      * @param int $id If !null, load instance.
 49:      * @param ElggUser $elgg_user Object to load instance from (optional)
 50:      *
 51:      * @throws APIException
 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:      * Loads a User instance from the system.
 66:      *
 67:      * @param ElggUser $elgg_user User to load from the system.
 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:      * Saves this instance to the system.
 83:      * @return bool|int Returns id of saved instance, or false if error.
 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:      * Copy $this user parameters into an Elgg User entity.
 99:      *
100:      * @param ElggUser $elgg_user Elgg User object instance to save $this to
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:      * Creates an encoded user password using a random hash for encoding.
121:      *
122:      * @param string $clear_password The new user password in clear text.
123:      *
124:      * @return bool 'true' if success, 'false' if error.
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:      * Sets values to specified properties of a User
138:      *
139:      * @param int $id Id of User to set property values
140:      * @param array $prop_value_array Array of property=>value pairs to set into the User
141:      *
142:      * @return int|bool Returns Id of User if correct, or false if error
143:      * @throws InvalidParameterException
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:             // remove unwanted spaces from beginning and end of value
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:      * Authenticate a user and log him into the system.
181:      *
182:      * @param string $login User login
183:      * @param string $password User password
184:      * @param bool $persistent Determines whether to make the session persistent
185:      *
186:      * @return bool Returns true if ok, or false if error
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:      * Logs out a user from the system.
198:      * @return bool Returns true if ok, or false if error.
199:      */
200:     static function logout() {
201:         return logout();
202:     }
203: 
204:     /**
205:      * Get users with login contained in a given list of logins.
206:      *
207:      * @param array $login_array Array of user logins
208:      *
209:      * @return static[] Returns an array of User objects
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:      * Get users with email contained in a given list of emails. Each email can be associated
226:      * with multiple users. The output will be an array of Users per login, nested inside a main
227:      * array.
228:      *
229:      * @param array $email_array Array of user emails
230:      *
231:      * @return static[] Returns an array of arrays of User objects
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:      * Get users with role contained in a given list of roles.
252:      *
253:      * @param array $role_array Array of user roles
254:      *
255:      * @return static[] Returns an array of [role] => array(Users)
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:      * Get a User's last login timestamp.
276:      *
277:      * @param int $id User ID
278:      *
279:      * @return int|bool Last login timestamp, or false in case of error
280:      */
281:     static function get_last_login($id) {
282:         $user = new static($id);
283:         return $user->last_login;
284:     }
285: 
286:     /**
287:      * Sets the avatar for a User
288:      *
289:      * @param int $id User ID
290:      * @param int $file_id Id of the image file containing the User's avatar
291:      *
292:      * @return int|false Returns the ID of the User, or false if error.
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:      * Returns the avatar for a User
302:      *
303:      * @param int $id User ID
304:      * @param string $size Desired size of avatar image: small, medium or large.
305:      *
306:      * @return array|null Returns an array with 2 elements: "url" => <avatar_url> and "path" => <avatar_path>, or null
307:      *     if none.
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: //    static function export_data($id_array = null, $format = "excel") {
331: //        // New Excel object
332: //        $php_excel = new PHPExcel();
333: //        // Set document properties
334: //        $php_excel->getProperties()
335: //            ->setCreator("ClipIt")
336: //            ->setTitle("ClipIt User Accounts")
337: //            ->setKeywords("clipit user account");
338: //        // Add table title and columns
339: //        $active_sheet = $php_excel->setActiveSheetIndex(0);
340: //        $active_sheet->getDefaultColumnDimension()->setWidth(30);
341: //        $active_sheet->getStyle(1)->getFont()->setBold(true);
342: //        $row = 1;
343: //        $col = 0;
344: //        $values = array("NAME", "LOGIN", "PASSWORD", "EMAIL", "ROLE");
345: //        foreach ($values as $value) {
346: //            $active_sheet->setCellValueByColumnAndRow($col++, $row, $value);
347: //        }
348: //        // Load ClipIt Users
349: //        if (!empty($id_array)) {
350: //            $user_array = static::get_by_id($id_array);
351: //        } else {
352: //            $user_array = static::get_all();
353: //        }
354: //        // Write Users to spreadsheet
355: //        $row = 2;
356: //        $col = 0;
357: //        foreach ($user_array as $user) {
358: //            $values = array($user->name, $user->login, "<password>", $user->email, $user->role);
359: //            foreach ($values as $value) {
360: //                $active_sheet->setCellValueByColumnAndRow($col++, $row, $value);
361: //            }
362: //            $row++;
363: //            $col = 0;
364: //        }
365: //        switch ($format) {
366: //            case "excel":
367: //                $objWriter = PHPExcel_IOFactory::createWriter($php_excel, 'Excel2007');
368: //                $objWriter->save('/tmp/clipit_users.xlsx');
369: //        }
370: //        return true;
371: //    }
372: 
373:     /**
374:      * Add Users from an Excel file, and return an array of User Ids from those created or selected from the file.
375:      *
376:      * @param string $file_path Local file path
377:      *
378:      * @return array|null Array of User IDs, or null if error.
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:      * Parse a single role from an Excel file, containing one user, and add it to ClipIt if new
400:      *
401:      * @param PHPExcel_Worksheet_Row $row_iterator
402:      *
403:      * @return array|false ID of User contained in row, or false in case of error.
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:         // Check for non-user row
410:         $value = $cell_iterator->current()->getValue();
411:         if (empty($value) || strtolower($value) == "users" || strtolower($value) == "name") {
412:             return $row_result;
413:         }
414:         // name
415:         $name = $value;
416:         $prop_value_array["name"] = trim($name, " \t\n\r\0\x0B".chr(194).chr(160));
417:         $cell_iterator->next();
418:         // login
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:         // password
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:         // email
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:         // create user object
441:         $row_result["user_id"] = (int)static::create($prop_value_array);
442:         // group
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: }
API documentation generated by ApiGen 2.8.0