Skip to content

Commit

Permalink
Merge pull request #12 from EncryptEx/code-cleanup
Browse files Browse the repository at this point in the history
Cleaned up the code
  • Loading branch information
EncryptEx authored Aug 22, 2023
2 parents 2786056 + 4e42a85 commit 18b7039
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 72 deletions.
10 changes: 5 additions & 5 deletions db/database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ CREATE TABLE `action-types` (
-- Volcado de datos para la tabla `action-types`
--
INSERT INTO `action-types` (`id`, `name`)
VALUES (1, 'email');
VALUES (1, 'email (useful to send alerts)');

-- --------------------------------------------------------
--
Expand Down Expand Up @@ -114,22 +114,22 @@ ALTER TABLE `users`
--
-- AUTO_INCREMENT de la tabla `action-logs`
--
ALTER TABLE `action-logs` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT = 67;
ALTER TABLE `action-logs` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `action-types`
--
ALTER TABLE `action-types` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT = 2;
ALTER TABLE `action-types` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `triggers`
--
ALTER TABLE `triggers` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT = 10;
ALTER TABLE `triggers` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT de la tabla `users`
--
ALTER TABLE `users` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT = 12;
ALTER TABLE `users` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

COMMIT;

Expand Down
23 changes: 19 additions & 4 deletions src/private/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,20 @@ public function triggerLog(int $triggerId, string $data, string $filename = null
* Returns the name of that actionID
* @return string Action's name
*/
public function getActionName(int $actionId)
public function getActionId(int $actionId)
{
# Connect to DB
$pdo = $this->databaseConnect();

# Check if does exist, either return error msg
$SQL_SELECT = "SELECT name FROM `action-types` WHERE id=:id LIMIT 1";
$SQL_SELECT = "SELECT id FROM `action-types` WHERE id=:id LIMIT 1";
$selectStmt = $pdo->prepare($SQL_SELECT);
$input = ['id' => $actionId];
$selectStmt->execute($input);

if ($selectStmt->rowCount() > 0) {
foreach ($selectStmt as $row) {
return $row['name'];
return $row['id'];
}
}
return false;
Expand Down Expand Up @@ -840,4 +840,19 @@ public function getAllLogfilesNames(): array

return $filenames;
}
}

/**
* Function to stop software from running when something is not on the right track.
*/
public function showError(string $errorMsg){
http_response_code(500);
print(json_encode(
[
'success' => false,
'message' => $errorMsg,
'timestamp' => time()
]
));
die();
}
}
90 changes: 27 additions & 63 deletions src/public/trigger.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,99 +43,63 @@
if (isset($_FILES) && count($_FILES) == 1) {

$uploaddir = "/../private/uploads/";
$newName = "log" . time() . "-". $triggerId . ".txt";
$newName = "log" . time() . "-" . $triggerId . ".txt";
$uploadfile = __DIR__ . $uploaddir . $newName;


$allowed = array('txt', 'log');
$ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
if (!in_array($ext, $allowed)) {
http_response_code(500);
print(json_encode(
[
'success' => false,
'message' => 'Only .txt and .log extensions are supported',
'timestamp' => time()
]
));
die();
$utils->showError('Only .txt and .log extensions are supported');
}

if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
$filePath = $uploadfile;
} else {
http_response_code(500);
print(json_encode(
[
'success' => false,
'message' => 'There was an error while uploading the file',
'timestamp' => time()
]
));
die();
$utils->showError('There was an error while uploading the file');
}
}


if ($utils->getActionName($actionToDo) == "email") {
if ($utils->getActionId($actionToDo) == 1) { // is email (alert)
# send email to ownerId

$emailResult = $utils->sendEmailTo($ownerId, $triggerName, $stringUrl, $data, $status, $filePath);

if (!$emailResult['success']) {
http_response_code(500);
print(json_encode(
[
'success' => false,
'message' => $emailResult['message'],
'timestamp' => time()
]
));
die();
$utils->showError($emailResult['message']);
}
} else {
# id does not match, throw error
http_response_code(500);
print(json_encode(
[
'success' => false,
'message' => 'There was an error while trying to fetch the action type',
'timestamp' => time()
]
));
die();
$utils->showError('There was an error while trying to fetch the action type');
}

# save log
$result = $utils->triggerLog($triggerId, $data, $newName);

if (!$result) { # if couldn't be saved, throw error
http_response_code(500);
print(json_encode(
[
'success' => false,
'message' => 'There was an error while trying to save the log',
'timestamp' => time()
]
));
die();
if (!$result) { # if couldn't be saved, throw error
$utils->showError('There was an error while trying to save the log');
}

http_response_code(200);
print(json_encode(
[
'success' => true,
'message' => 'Action performed successfully!',
'timestamp' => time()
]
));
print(
json_encode(
[
'success' => true,
'message' => 'Action performed successfully!',
'timestamp' => time()
]
)
);
} else {
http_response_code(404);
print(json_encode(
[
'success' => false,
'message' => 'The requested TriggerID does not exist',
'timestamp' => time()
]
));
}
print(
json_encode(
[
'success' => false,
'message' => 'The requested TriggerID does not exist',
'timestamp' => time()
]
)
);
}

0 comments on commit 18b7039

Please sign in to comment.