public function subscribe($userId, $topic) { // Setting the session id so that if the session times out and the record // is removed from // the session_data table the deletion can cascade to event descriptions try { $sub = array("userId" => $userId, "topic" => $topic, "session_id" => AppUtils::getSessionId()); $this->db->event_subscriptions()->insert($sub); } catch (PDOException $e) { if ((int) $e->getCode() != 23000) { AppUtils::logError($e, __METHOD__); throw $e; } } }
/** * * @see ForumPostServicePDO::purgeForumPost() */ public static function purgeForumPost($forumId) { $app = \Slim\Slim::getInstance(); try { $pdo = new ForumPostServicePDO(); $pdo->purgeForumPost($forumId); $app->response()->status(204); // NO DOCUMENT STATUS CODE FOR SUCCESS } catch (Exception $e) { AppUtils::logError($e, __METHOD__); AppUtils::sendError($e->getCode(), "Error purging post for forum {$forumId}", $e->getMessage()); } }
/** * Login the user with credentials past in POST */ public static function login() { $app = \Slim\Slim::getInstance(); AppUtils::logout(); try { // get and decode JSON request body $request = $app->request(); $response = $app->response(); $body = $request->getBody(); $login = (array) json_decode($body); $loginOK = false; // AppUtils::logDebug("attempting login // ".$login['userId'].'/'.$login['password']); if (!isset($login['userId'])) { AppUtils::sendError(0, "Login Error", "User ID was not specified.", 401); return; } if (!isset($login['password'])) { AppUtils::sendError(0, "Login Error", "Password was not specified.", 401); return; } $userService = new UserServicePDO(); if ($userService->validateUser($login['userId'], $login['password'])) { // AppUtils::logDebug($login['userId'].' Successfully logged in.'); $access = $userService->getAccess($login['userId']); AppUtils::setLoginValid($login['userId'], $access); $rsp = array('userId' => $login['userId'], 'accessLevel' => $access); AppUtils::sendResponse($rsp); } else { // AppUtils::logDebug($login['userId'].' Failed login!'); AppUtils::sendError(0, "Login Error", "User ID/Password combination is invalid.", 401); } } catch (Exception $e) { AppUtils::logError($e, __METHOD__); AppUtils::sendError($e->getCode(), "Error Authenticating User", $e->getMessage()); } }
/** * * @see ForumServicePDO::deleteForum() */ public static function deleteForum($id) { $app = \Slim\Slim::getInstance(); $params = array('forumId' => $id, 'changeType' => ForumEvent::DELETE); try { $pdo = new ForumServicePDO(); $pdo->deleteForum($id); AppUtils::sendEvent(ForumEvent::DOMAIN, $id, ForumEvent::CHANGE, "Forum deleted: " . $id, $params); $app->response()->status(204); // NO DOCUMENT STATUS CODE FOR SUCCESS } catch (Exception $e) { AppUtils::logError($e, __METHOD__); AppUtils::sendError($e->getCode(), "Error deleting forum with ID: " . $id, $e->getMessage()); } }
/** * * @see EventServicePDO::unsubscribe() */ public static function unsubscribe($userId, $topic) { try { $pdo = new EventServicePDO(); $pdo->unsubscribe($userId, $topic); AppUtils::sendResponse(array("success" => true, "message" => "User {$userId} unsubscribed from topic {$topic}")); } catch (PDOException $e) { AppUtils::logError($e, __METHOD__); AppUtils::sendError($e->getCode(), "Error unsubscribing to events user {$userId} topic {$topic}", $e->getMessage()); } }
/** * * @see ForumServicePDO::deleteFileNode() */ public static function deleteFileNode($forumId, $id) { $app = \Slim\Slim::getInstance(); try { $pdo = new ForumServicePDO(); $pdo->deleteFileNode($id); $eventParams = array(); $eventParams['id'] = $id; $eventParams['changeType'] = ForumEvent::DELETE; AppUtils::sendEvent(ForumEvent::DOMAIN, $forumId, ForumEvent::NODE_CHANGE, "Node deleted id: " . $id, $eventParams); $app->response()->setStatus(204); // NO DOCUMENT STATUS CODE FOR // SUCCESS } catch (Exception $e) { AppUtils::logError($e, __METHOD__); AppUtils::sendError($e->getCode(), "Error deleting file node with ID {$id}", $e->getMessage()); } }
/** * * @see SettingsServicePDO::getDomains() */ public static function getDomains() { try { $pdo = new SettingsServicePDO(); $result = $pdo->getDomains(); AppUtils::sendResponse($result); } catch (PDOException $e) { AppUtils::logError($e, __METHOD__); AppUtils::sendError($e->getCode(), "Error getting all setting domains", $e->getMessage()); } }
/** * * @see UserServicePDO::setUserSetting() */ public static function setUserSetting($id, $domain, $settingKey) { $app = \Slim\Slim::getInstance(); try { $pdo = new UserServicePDO(); // get and decode JSON request body $request = $app->request(); // $body = $request->getBody(); // $settingData = (array) json_decode($body); $settingValue = $request->params('settingValue'); $pdo->setUserSetting($id, $domain, $settingKey, $settingValue); AppUtils::sendResponse($settingValue); } catch (Exception $e) { AppUtils::logError($e, __METHOD__); AppUtils::sendError($e->getCode(), "Error setting value for user {$id} setting {$domain}/{$settingKey}", $e->getMessage()); } }
/** * * @see ForumServicePDO::getForumsForUser() */ public static function getForumsForUser() { try { $pdo = new ForumServicePDO(); $userId = AppUtils::getUserId(); $forums = $pdo->getForumsForUser($userId); AppUtils::sendResponse($forums); } catch (PDOException $e) { AppUtils::logError($e, __METHOD__); AppUtils::sendError($e->getCode(), "Error getting forums for user {$userId}", $e->getMessage()); } }
/** * Uploads the file specified via HTTP POST * This code is based on the PHP examples */ public static function upload() { $app = \Slim\Slim::getInstance(); $forumId = $_POST['forumId']; $folderId = $_POST['id']; $fileName = $_FILES["file"]["name"]; $tempFileName = $_FILES["file"]["tmp_name"]; $contentType = $_FILES["file"]["type"]; // Check for errors if ($_FILES['file']['error'] > 0) { $errorMsg = 'Upload Error: '; // Print a message based upon the error. switch ($_FILES['file']['error']) { case 1: $errorMsg = $errorMsg . 'The file exceeds the upload_max_filesize setting in php.ini.'; break; case 2: $errorMsg = $errorMsg . 'The file exceeds the MAX_FILE_SIZE setting in the HTML form.'; break; case 3: $errorMsg = $errorMsg . 'The file was only partially uploaded.'; break; case 4: $errorMsg = $errorMsg . 'No file was uploaded.'; break; case 6: $errorMsg = $errorMsg . 'No temporary folder was available.'; break; case 7: $errorMsg = $errorMsg . 'Unable to write to the disk.'; break; case 8: $errorMsg = $errorMsg . 'File upload stopped.'; break; default: $errorMsg = $errorMsg . 'A system error occurred.'; break; } // End of switch. AppUtils::sendError(500, "File Upload Error", $errorMsg); } else { try { $fileNode = array('id' => '', 'forumId' => $forumId, 'parentId' => $folderId, 'name' => $fileName, 'contentType' => $contentType); $pdo = new ForumServicePDO(); $fileNode = $pdo->createFileNode((array) $fileNode); $fileId = $fileNode['id']; move_uploaded_file($tempFileName, FORUM_UPLOAD_DIR . $fileId); $fileNode['changeType'] = ForumEvent::CREATE; AppUtils::sendEvent(ForumEvent::DOMAIN, $forumId, ForumEvent::NODE_CHANGE, "Node created: " . $fileNode['name'], $fileNode); AppUtils::sendResponse($fileNode); } catch (Exception $e) { AppUtils::logError($e, __METHOD__); AppUtils::sendError($e->getCode(), "Error creating file node for {$fileName}", $e->getMessage()); } } // Delete the file if it still exists: if (file_exists($_FILES['file']['tmp_name']) && is_file($_FILES['file']['tmp_name'])) { unlink($_FILES['file']['tmp_name']); } }