public function signInWithSocial() { $reqData = $this->getDataFromRequestWithJsonFormat(); if (!isset($reqData['SocialId']) || empty($reqData['SocialId'])) { $this->sendInvalidFieldResult('Social', 'SocialId', 'missing_field'); } if (!isset($reqData['Type']) || intval($reqData['Type']) <= 0) { $this->sendInvalidFieldResult('Social', 'Type', 'missing_field'); } $socialData = array('ExternalId' => htmlspecialchars($reqData['SocialId']), 'Type' => intval($reqData['Type']), 'Email' => isset($reqData['Email']) ? htmlspecialchars($reqData['Email']) : '', 'DisplayName' => isset($reqData['DisplayName']) ? htmlspecialchars($reqData['DisplayName']) : '', 'Firstname' => isset($reqData['Firstname']) ? htmlspecialchars($reqData['Firstname']) : '', 'Lastname' => isset($reqData['Lastname']) ? htmlspecialchars($reqData['Lastname']) : '', 'ProfileUrl' => isset($reqData['ProfileURL']) ? htmlspecialchars($reqData['ProfileURL']) : '', 'PhotoUrl' => isset($reqData['PhotoURL']) ? htmlspecialchars($reqData['PhotoURL']) : '', 'WebsiteUrl' => isset($reqData['WebsiteURL']) ? htmlspecialchars($reqData['WebsiteURL']) : ''); $user = UserRepository::findUserBySocialExternalId($socialData['ExternalId'], $socialData['Type']); if (!$user) { $email = Util::genRandomEmail(); $emailArr = explode('@', empty($socialData['Email']) ? $email : $socialData['Email']); $username = $emailArr[0]; $password = Util::genRandomPassword(); $user = UserRepository::findUserByUsernameOrEmail($username); if ($user) { $email = Util::genRandomEmail($username); $emailArr = explode('@', $email); $username = $emailArr[0]; } $userData = array('Username' => $username, 'Email' => $email, 'Password' => $this->app->passHash->hashPassword($password), 'CreatedDate' => date('Y-m-d H:i:s')); $success = false; $this->app->conn->beginTransaction(); $newUserId = UserRepository::save($userData); if ($newUserId) { $socialData['UserId'] = $newUserId; $socialData['CreatedDate'] = date('Y-m-d H:i:s'); $newSocialId = SocialRepository::save($socialData); if ($newSocialId) { $success = true; $this->app->conn->commit(); } else { $this->app->conn->rollback(); $this->sendOperationFailedResult('Social'); } } else { $this->sendOperationFailedResult('User'); } if (!empty($socialData['PhotoUrl']) && Validation::isValidUrl($socialData['PhotoUrl'])) { $arrUrlSegment = parse_url($socialData['PhotoUrl']); $temp = explode('.', $arrUrlSegment['path']); $extension = end($temp); $hashFileName = base64_encode(date('Ymdhis') . uniqid()); $avatarFolder = $this->app->config['app']['avatar_path'] . DS . $newUserId; $fs = new Filesystem(); try { $fs->mkdir($avatarFolder); } catch (IOExceptionInterface $e) { $this->app->log->getWriter()->write('An error occurred while creating your directory at ' . $e->getPath(), \Slim\Log::ERROR); } if ($fs->exists($avatarFolder)) { $fileTargetPath = implode(DS, array($this->app->config['app']['avatar_path'], $newUserId, $hashFileName . '.' . $extension)); $rstCurlAvatar = ImageUtility::getImageFromUrl($socialData['PhotoUrl']); if ($rstCurlAvatar && ImageUtility::getImageFileType($rstCurlAvatar)) { file_put_contents($fileTargetPath, $rstCurlAvatar); $imagine = new Imagine(); $image = $imagine->open($fileTargetPath); $image->thumbnail(new Box(180, 180))->save($fileTargetPath); $data = array('AvatarUrl' => $this->app->config['app']['asset_base_url'] . implode('/', array('avatar', $newUserId, $hashFileName . '.' . $extension)), 'LastModifiedDate' => date('Y-m-d H:i:s')); UserRepository::update($newUserId, $data); } } } $user = UserRepository::findUserById($newUserId); } else { $socialData['LastModifiedDate'] = date('Y-m-d H:i:s'); $newSocialId = SocialRepository::update($socialData['ExternalId'], $socialData['Type'], $socialData); } $this->sendResponse(array('user' => $user)); }