diff --git a/code/bin/update_plugins.php b/code/bin/update_plugins.php index 0dfdee4..91239f1 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,18 +17,15 @@ 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` "; - $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 4b83609..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,6 +115,16 @@ function savePlugin($githubUrl, $skipDate = false) // debug($result, 'Raw GitHub API result'); + // 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'])); $submittedAt = time(); @@ -223,7 +233,7 @@ function savePlugin($githubUrl, $skipDate = false) $result['message'] .= $e; } - die(json_encode($result)); + return json_encode($result); } @@ -252,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); } @@ -278,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); } } @@ -292,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); }