示例#1
0
文件: Handler.php 项目: visapi/amun
 public function create(RecordInterface $record)
 {
     if ($record->hasFields('groupId', 'status', 'identity', 'name', 'pw')) {
         // check whether identity exists
         $con = new Condition();
         $con->add('identity', '=', $record->identity);
         if ($this->table->count($con) > 0) {
             throw new Exception('Identity already exists');
         }
         // check whether name and hostid exists
         $con = new Condition();
         $con->add('hostId', '=', !empty($record->hostId) ? $record->hostId : 0);
         $con->add('name', '=', $record->name);
         if ($this->table->count($con) > 0) {
             throw new Exception('Identity already exists');
         }
         // default values
         if (!isset($record->countryId)) {
             $record->setCountryId(1);
         }
         if (!isset($record->timezone)) {
             $record->setTimezone('UTC');
         }
         $date = new DateTime('NOW', $this->registry['core.default_timezone']);
         $record->token = Security::generateToken();
         $record->ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1';
         $record->lastSeen = $date->format(DateTime::SQL);
         $record->updated = $date->format(DateTime::SQL);
         $record->date = $date->format(DateTime::SQL);
         // set host id if we have an remote host discover the profile url
         if (empty($record->hostId)) {
             $record->hostId = 0;
             $record->profileUrl = $this->config['psx_url'] . '/' . $this->config['psx_dispatch'] . 'profile/' . $record->name;
         } else {
             $record->status = Record::REMOTE;
             $record->profileUrl = $this->discoverProfileUrl($record->hostId, $record->name);
         }
         // set global id
         if (!isset($record->globalId)) {
             $profileUrl = new Url($record->profileUrl);
             $record->globalId = $this->base->getUUID('user:account:' . $profileUrl->getHost() . ':' . $record->name . ':' . uniqid());
         }
         // set thumbnail if email available and thumbnail not set
         if (!isset($record->thumbnailUrl)) {
             $default = $this->config['psx_url'] . '/img/avatar/no_image.png';
             if (!empty($record->email)) {
                 $record->thumbnailUrl = 'http://www.gravatar.com/avatar/' . md5(strtolower(trim($record->email))) . '?d=' . urlencode($default) . '&s=48';
             } else {
                 $record->thumbnailUrl = $default;
             }
         }
         $this->table->insert($record->getData());
         $record->id = $this->sql->getLastInsertId();
         // insert relation to self
         $this->sql->insert($this->registry['table.user_friend'], array('status' => Friend\Record::NORMAL, 'userId' => $record->id, 'friendId' => $record->id, 'date' => $date->format(DateTime::SQL)));
         $this->notify(RecordAbstract::INSERT, $record);
         return $record;
     } else {
         throw new Exception('Missing field in record');
     }
 }