PageRenderTime 57ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/add-md5-api-key.php

https://bitbucket.org/bayrock/gw2spidy
PHP | 62 lines | 45 code | 13 blank | 4 comment | 8 complexity | 7a128c92a92bab0c0fc5e59ae49f2ffd MD5 | raw file
Possible License(s): BSD-3-Clause, BSD-2-Clause
  1. <?php
  2. use GW2Spidy\Util\CLIColors;
  3. use GW2Spidy\Util\Functions;
  4. use Igorw\Silex\Config;
  5. require dirname(__FILE__) . '/../autoload.php';
  6. $name = isset($argv[1]) ? $argv[1] : null;
  7. $check = isset($argv[2]) ? $argv[2] : null;
  8. $env = isset($argv[3]) ? $argv[3] : null;
  9. // '-' is null too ;) just so you can skip the arg if you want
  10. $check = $check == '-' ? null : $check;
  11. // grab default env
  12. $env = $env ?: getAppEnv()->getEnv();
  13. // generate md5
  14. $md5 = md5($name);
  15. // cnf file based on env
  16. $cnffile = __DIR__ . "/../config/cnf/{$env}.json";
  17. echo CLIColors::getColoredString("adding md5 [[ {$md5} ]] for [[ {$name} ]] to [[ {$env} ]] ... \n", "light_blue");
  18. if (in_array($env, array('dev', 'prod', 'default'))) {
  19. die(CLIColors::getColoredString("Can't add API keys to [[ {$env} ]] cnf file, protected.\n", "red"));
  20. }
  21. if (!file_exists($cnffile)) {
  22. die(CLIColors::getColoredString("Config file [[ {$cnffile} ]] doesn't exist.\n", "red"));
  23. }
  24. $cnf = Config::readConfig($cnffile);
  25. if (!$cnf) {
  26. die(CLIColors::getColoredString("Parsing cnf failed.\n", "red"));
  27. }
  28. if (!isset($cnf['gw2spidy']['api_secrets'])) {
  29. die(CLIColors::getColoredString("Please create the initial 'api_secrets' entry yourself.\n", "red"));
  30. }
  31. $md5 = md5($name);
  32. if ($check && $check != $md5) {
  33. die(CLIColors::getColoredString("md5 [[ {$md5} ]] didn't match the check [[ {$check} ]].\n", "red"));
  34. }
  35. $cnf['gw2spidy']['api_secrets'][$name] = $md5;
  36. $ok = file_put_contents($cnffile, "cnf = ".Functions::indent(json_encode($cnf)).";");
  37. if (!$ok) {
  38. die(CLIColors::getColoredString("Failed writing to file [[ {$cnffile} ]].\n", "red"));
  39. }
  40. echo CLIColors::getColoredString("Added md5 [[ {$md5} ]] for [[ {$name} ]]\n", "green");