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 UBSite {
 19:     /**
 20:      * @const string Elgg entity SUBTYPE for this class
 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:      * Constructor
 32:      * @throws APIException
 33:      */
 34:     function __construct() {
 35:         $elgg_entity = elgg_get_site_entity();
 36:         $this->copy_from_elgg($elgg_entity);
 37:     }
 38: 
 39:     /**
 40:      * Loads object parameters stored in Elgg
 41:      *
 42:      * @param ElggEntity $elgg_entity Elgg Object to load parameters from.
 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:      * Saves Site parameters into Elgg
 55:      * @return int Site ID
 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:      * @param ElggEntity $elgg_entity
 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:      * Dummy method in case someone treats ClipIt Site as a typical ClipIt Object
 83:      *
 84:      * @param int $limit
 85:      * @param int $offset
 86:      * @param string $order_by
 87:      * @param bool $ascending
 88:      * @param bool $id_only
 89:      * @return array
 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:      * Get the REST API method list, including description and required parameters.
101:      * @return array List of all available REST API Methods.
102:      */
103:     static function api_list() {
104:         return list_all_apis();
105:     }
106: 
107:     /**
108:      * Get authentication token, required for all other REST API calls. The token must be set as the value for the
109:      * "auth_token" key in each REST API call.
110:      *
111:      * @param string $login User login
112:      * @param string $password User password
113:      * @param int $timeout Session timeout in minutes
114:      *
115:      * @return string Authentication Token.
116:      * @throws SecurityException
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; // convert mins to ms
130:             // if the token expires in less than $timeout, we extend the timeout
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:             //$object['class'] = get_class_from_subtype($object['subtype']);
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:     /* Static Functions */
186:     /**
187:      * Lists the properties contained in this object
188:      * @return array Array of properties with type and default value
189:      */
190:     static function list_properties() {
191:         return get_class_vars(get_called_class());
192:     }
193: 
194: }
API documentation generated by ApiGen 2.8.0