AddProjectAction.class.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. class AddProjectAction implements IAction {
  3. public function execute() {
  4. if(!Authorize::isLoggedIn() || !Authorize::hasPermission()) {
  5. return array("error" => "Access not authorized");
  6. }
  7. $bucket = $_POST['bucket'] ?? $_GET['bucket'] ?? "";
  8. $project = $_POST['project'] ?? $_GET['project'] ?? "";
  9. $region = $_POST['region'] ?? $_GET['region'] ?? "us-east-1";
  10. $displayName = $_POST['display_name'] ?? $_GET['display_name'] ?? "";
  11. //TODO: scrub inputs
  12. if(empty($bucket) || empty($displayName) || empty($project)) {
  13. return array("error" => "One or more required fields missing: bucket, project, display_name");
  14. }
  15. //TODO: instead of bucket, need to pick CDN url
  16. //https://d2we20985k2v2g.cloudfront.net/test/hand.png
  17. $sql = "INSERT INTO projects (bucket, folder, display_name)
  18. VALUES
  19. (:bucket, :folder, :display_name);";
  20. $db = SqliteDatabase::getSingleton();
  21. $preparedQuery = $db->prepare($sql);
  22. $preparedQuery->bindValue(':bucket', $bucket);
  23. $preparedQuery->bindValue(':folder', $project);
  24. $preparedQuery->bindValue(':display_name', $displayName);
  25. //$preparedQuery->bindValue(':region', $region);
  26. try {
  27. $result = $preparedQuery->execute();
  28. $loggerData = array();
  29. $loggerData['display_name'] = $_SESSION['display_name'];
  30. $loggerData['user_id'] = $_SESSION['user_id'];
  31. $loggerData['project_display_name'] = $displayName;
  32. $loggerData['project_folder'] = $project;
  33. $loggerData['project_bucket'] = $bucket;
  34. SecurityLogger::action("create-project", $loggerData, time());
  35. return array("status" => "success", "message" => "Successfully created project '" . $displayName . "'.");
  36. }
  37. catch(Exception $e) {
  38. return array("error" => "Error when creating project '" . $displayName. "': possibly duplicate?" , "exception" => $e->getMessage());
  39. }
  40. }
  41. }