public function actionCreate() { $user = $this->checkAccess("create"); $requestData = Yii::$app->getRequest()->getBodyParams(); $requestData['user_id'] = $user->id; $model = new Studio(); $model->load($requestData, ''); if (!$model->save()) { return ApiHelper::errorResponse($model->getErrors(), 422); } return $model; }
public function actionMessage($id) { $stream = Stream::findOne(['stream_id' => $id]); $this->checkAccess('message', $stream); $user = User::findIdentityByAccessToken(Yii::$app->request->get('access-token')); $text = Yii::$app->request->post('message'); $date = date("Y-m-d H:i:s"); $message = new Message(); $message->load(['user_id' => $user->user_id, 'stream_id' => $id, 'message_text' => $text, 'message_date' => $date], ''); if (!$message->save()) { return ApiHelper::errorResponse($message->getErrors(), 422); } return ApiHelper::successResponse($message); }
public function actionDispute() { $user = $this->checkAccess("create"); $class_id = (int) Yii::$app->request->post('class_id'); $dispute_type = (int) Yii::$app->request->post('dispute_type'); $dispute = \common\models\TransactionHistory::checkDispute($class_id, $user->user_id); $class = \common\models\Classes::findOne($class_id); if ($dispute && $class) { if ($class->class_etime + 60 * 60 * 24 < time()) { return ApiHelper::errorResponse('Time for dispute expired.'); } $dispute->transaction_dispute_status = 'open'; $dispute->transaction_dispute_time = time(); $dispute->transaction_dispute_type = $dispute_type; $dispute->save(); return ApiHelper::successResponse(true); } return ApiHelper::errorResponse('Incorrect Access.'); }
public function actionAdminAutologin() { $model = new $this->modelClass(); $post = \Yii::$app->request->post(); $data = User::findByPasswordResetToken($post['token']); if ($data && Yii::$app->user->loginByAccessToken($data->user_auth_key)) { $modelU = new $this->modelClass(); $modelU->setLoginTime(\Yii::$app->user->identity->getUserId()); $udata = $modelU->getUserInfo(\Yii::$app->user->identity->getUserId()); $model->generatePasswordResetToken(); return ApiHelper::successResponse(array(['auth' => \Yii::$app->user->identity->getAuthKey(), 'udata' => $udata, 'role' => \Yii::$app->user->identity->getUserRole()])); } return ApiHelper::errorResponse(["Incorrect Access."], 422); }
public function actionUpdate($id) { $models = Classes::findAll(['class_period_time' => $id]); $transaction = Yii::$app->getDb()->beginTransaction(); try { foreach ($models as &$model) { $this->checkAccess("update", $model); $model->load(Yii::$app->getRequest()->getBodyParams(), ''); if (!$model->save()) { $transaction->rollBack(); return ApiHelper::errorResponse($model->getErrors(), 422); } } $transaction->commit(); } catch (\Exception $ex) { $transaction->rollBack(); return ApiHelper::errorResponse($ex->getMessage()); } return ApiHelper::successResponse($models); }
public function actionUpload() { $this->checkAccess("upload"); if (empty($_FILES) || !isset($_FILES['video'])) { return ApiHelper::errorResponse("Video file missing", 422); } $client = new \Google_Client(); $client->setClientId(Yii::$app->params['OAUTH2_CLIENT_ID']); $client->setClientSecret(Yii::$app->params['OAUTH2_CLIENT_SECRET']); $client->setScopes('https://www.googleapis.com/auth/youtube'); $client->setRedirectUri(Yii::$app->params['redirectVideo']); // Define an object that will be used to make all API requests. $youtube = new \Google_Service_YouTube($client); if ($this->checkToken()) { $token = $this->getToken(); } else { $oldToken = $this->getToken(); $token = $this->updateToken($oldToken->refresh_token); if (is_array($token)) { return ApiHelper::errorResponse($token, 500); } } $client->setAccessToken($token->toJson()); // Check to ensure that the access token was successfully acquired. if ($client->getAccessToken()) { try { // REPLACE this value with the path to the file you are uploading. $videoPath = $this->saveToLocal($_FILES['video']); // Create a snippet with title, description, tags and category ID // Create an asset resource and set its snippet metadata and type. // This example sets the video's title, description, keyword tags, and // video category. $snippet = new \Google_Service_YouTube_VideoSnippet(); $snippet->setTitle(Yii::$app->request->post('title')); $snippet->setDescription(Yii::$app->request->post('description')); $snippet->setTags(explode(",", Yii::$app->request->post('tags'))); // Numeric video category. See // https://developers.google.com/youtube/v3/docs/videoCategories/list $snippet->setCategoryId("22"); // Set the video's status to "public". Valid statuses are "public", // "private" and "unlisted". $status = new \Google_Service_YouTube_VideoStatus(); $status->privacyStatus = "public"; // Associate the snippet and status objects with a new video resource. $video = new \Google_Service_YouTube_Video(); $video->setSnippet($snippet); $video->setStatus($status); // Specify the size of each chunk of data, in bytes. Set a higher value for // reliable connection as fewer chunks lead to faster uploads. Set a lower // value for better recovery on less reliable connections. $chunkSizeBytes = 1 * 1024 * 1024; // Setting the defer flag to true tells the client to return a request which can be called // with ->execute(); instead of making the API call immediately. $client->setDefer(true); // Create a request for the API's videos.insert method to create and upload the video. $insertRequest = $youtube->videos->insert("status,snippet", $video); // Create a MediaFileUpload object for resumable uploads. $media = new \Google_Http_MediaFileUpload($client, $insertRequest, 'video/*', null, true, $chunkSizeBytes); $media->setFileSize(filesize($videoPath)); // Read the media file and upload it chunk by chunk. $status = false; $handle = fopen($videoPath, "rb"); while (!$status && !feof($handle)) { $chunk = fread($handle, $chunkSizeBytes); $status = $media->nextChunk($chunk); } fclose($handle); // If you want to make other calls after the file upload, set setDefer back to false $client->setDefer(true); //$status['snippet']['title'] return ApiHelper::successResponse(["link" => "https://www.youtube.com/watch?v=" . $status['id']]); } catch (\Google_Service_Exception $e) { return ApiHelper::errorResponse(sprintf('<p>A service error occurred: <code>%s</code></p>', htmlspecialchars($e->getMessage())), 500); } catch (\Google_Exception $e) { return ApiHelper::errorResponse(sprintf('<p>An client error occurred: <code>%s</code></p>', htmlspecialchars($e->getMessage())), 500); } } return ApiHelper::errorResponse("No authorizied", 401); }
public function actionLike($id) { $this->checkAccess("like"); $user = User::findIdentityByAccessToken(Yii::$app->request->get('access-token')); if ($like = GalleryLike::findOne(['user_id' => $user->id, 'image_id' => $id])) { if (!$like->delete()) { return ApiHelper::errorResponse($like->getErrors(), 422); } } else { $like = new GalleryLike(); $like->user_id = $user->id; $like->image_id = $id; if (!$like->save()) { return ApiHelper::errorResponse($like->getErrors(), 422); } } return ApiHelper::successResponse(['message' => 'success']); }