From 49a1b5c707394c01874217afeb6b390b3dbe66f9 Mon Sep 17 00:00:00 2001 From: Bruno Saad Date: Sun, 11 Sep 2022 16:34:08 +0000 Subject: [PATCH 1/2] added comments to help fixing the issue --- code/bin/update_plugins.php | 6 +++--- code/lib/db/submit.php | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/code/bin/update_plugins.php b/code/bin/update_plugins.php index 0dfdee4..0105e1b 100644 --- a/code/bin/update_plugins.php +++ b/code/bin/update_plugins.php @@ -8,7 +8,7 @@ exit; } -// changes directory to current script, so the rel patchs work, independently of the user's current directory +// changes directory to current script, so the relative paths work, independently of the user's current directory chdir(dirname(__FILE__)); require_once "../lib/functions.php"; @@ -17,12 +17,12 @@ require_once "../lib/db/submit.php"; -if ($argv[1]) { +if ($argv[1]) { // if the user provided a specific github URL to update $githubUrl = $argv[1]; savePlugin($githubUrl, true); -} else { +} else { // else we run for all plugins // build the query $query = "SELECT `plugins`.`id`,`plugins`.`name`,`plugins`.`owner`,`users`.`username` "; diff --git a/code/lib/db/submit.php b/code/lib/db/submit.php index 4b83609..c33d78b 100644 --- a/code/lib/db/submit.php +++ b/code/lib/db/submit.php @@ -115,6 +115,8 @@ function savePlugin($githubUrl, $skipDate = false) // debug($result, 'Raw GitHub API result'); + // ISSUE #23 ADD VALIDATION HERE + // converts the timestamp $updatedAt = date("U", strtotime($result['data']['repository']['updatedAt'])); $submittedAt = time(); From 19056f36456d368961132ba98bf143fbacdf7dd2 Mon Sep 17 00:00:00 2001 From: Bruno Saad Date: Mon, 12 Sep 2022 00:13:14 +0000 Subject: [PATCH 2/2] fix for issue 23 --- code/bin/update_plugins.php | 9 ++------- code/features/home/home.php | 3 +-- code/lib/db/submit.php | 21 +++++++++++++++------ 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/code/bin/update_plugins.php b/code/bin/update_plugins.php index 0105e1b..91239f1 100644 --- a/code/bin/update_plugins.php +++ b/code/bin/update_plugins.php @@ -25,10 +25,7 @@ } else { // else we run for all plugins // build the query - $query = "SELECT `plugins`.`id`,`plugins`.`name`,`plugins`.`owner`,`users`.`username` "; - $query .= "FROM `plugins` "; - $query .= "LEFT JOIN `users` "; - $query .= "ON `plugins`.`owner` = `users`.`id`;"; + $query = "SELECT `plugins`.`url` FROM `plugins`"; // get plugins $stmt_plugins = $pdo->prepare($query); @@ -36,8 +33,6 @@ $plugins_array = $stmt_plugins->fetchAll(); foreach ($plugins_array as $row_plugins) { - // builds the github URL - $githubUrl = "https://github.com/" . $row_plugins['username'] . "/" . $row_plugins['name']; - savePlugin($githubUrl, true); + savePlugin($row_plugins['url'], true); } } diff --git a/code/features/home/home.php b/code/features/home/home.php index 02a514f..07e02ec 100644 --- a/code/features/home/home.php +++ b/code/features/home/home.php @@ -3,8 +3,7 @@ // if a plugin was submitted if (isset($_POST['githubUrl'])) { $githubUrl = filter_input(INPUT_POST, 'githubUrl', FILTER_SANITIZE_URL); - savePlugin($githubUrl); - die(); + die(savePlugin($githubUrl)); } $nav['active'] = 'home'; diff --git a/code/lib/db/submit.php b/code/lib/db/submit.php index c33d78b..50dbf12 100644 --- a/code/lib/db/submit.php +++ b/code/lib/db/submit.php @@ -25,7 +25,7 @@ function savePlugin($githubUrl, $skipDate = false) $result = array(); $result['status'] = 'error'; $result['message'] = 'URL is not valid'; - die(json_encode($result)); + return json_encode($result); } // puts array into variables so we can use them below: @@ -115,7 +115,15 @@ function savePlugin($githubUrl, $skipDate = false) // debug($result, 'Raw GitHub API result'); - // ISSUE #23 ADD VALIDATION HERE + // If Git|Hub returns an error, we return before trying to mess with the database + if (isset($result['errors'])) { + header('Content-Type: application/json; charset=UTF-8'); + $result = array(); + $result['status'] = 'error'; + $result['message'] = 'GitHub link not found. Are you sure the URL is correct and the repo is public?'; + + return json_encode($result); + } // converts the timestamp $updatedAt = date("U", strtotime($result['data']['repository']['updatedAt'])); @@ -225,7 +233,7 @@ function savePlugin($githubUrl, $skipDate = false) $result['message'] .= $e; } - die(json_encode($result)); + return json_encode($result); } @@ -254,7 +262,7 @@ function savePlugin($githubUrl, $skipDate = false) $result = array(); $result['status'] = 'error'; $result['message'] = 'Error when adding user'; - die(json_encode($result)); + return json_encode($result); } @@ -280,7 +288,7 @@ function savePlugin($githubUrl, $skipDate = false) $result = array(); $result['status'] = 'error'; $result['message'] = 'Error when adding tags'; - die(json_encode($result)); + return json_encode($result); } } @@ -294,9 +302,10 @@ function savePlugin($githubUrl, $skipDate = false) $result['message'] = 'success'; $result['redirect'] = $redirect; header('Content-Type: application/json; charset=UTF-8'); - echo json_encode($result, true); // debugging code debug($url, "URL Debugging"); debug( json_encode($result), "RESULT"); + + return json_encode($result, true); }