YumRepoConfigParser.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. /**
  20. * Parse repo file and get all enabled gpg keys
  21. * @param string $repoFile
  22. * @return mixed
  23. * array (
  24. * $currentRepoId = array ( "gpgkey" => $currentGpgLocation),
  25. * ....
  26. * )
  27. */
  28. function getEnabledGpgKeyLocations($repoFile) {
  29. $logger = new HMCLogger("YumRepoConfigParser");
  30. $logger->log_info("Parsing gpg key info from " . $repoFile);
  31. if (!file_exists($repoFile)) {
  32. $logger->log_error("Invalid repo file provided, file=" . $repoFile);
  33. return FALSE;
  34. }
  35. $fileContents = file($repoFile,
  36. FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
  37. $logger->log_debug("Repo file contents, file=" . $repoFile
  38. . ", contents=" . print_r($fileContents, true));
  39. $response = array();
  40. $currentRepoId = "";
  41. $currentRepoEnabled = 1;
  42. $currentGpgCheck = -1;
  43. $currentGpgLocation = "";
  44. $globalGpgCheck = 0;
  45. foreach ($fileContents as $fLine) {
  46. $line = trim($fLine);
  47. if (substr($line, 0, 1) == "#") {
  48. continue;
  49. }
  50. $matches = array();
  51. $logger->log_debug("Parsing repo file, file=" . $repoFile
  52. . ", line=" . $line);
  53. $logger->log_debug("Current: "
  54. . ", repoId=" . $currentRepoId
  55. . ", repoEnabled=" . $currentRepoEnabled
  56. . ", gpgCheckEnabled=" . $currentGpgCheck
  57. . ", gpgCheckLocation=" . $currentGpgLocation);
  58. if (preg_match("/\[(.*)\]/", $line, $matches) > 0) {
  59. $newRepoId = $matches[1];
  60. $logger->log_debug("Found new repo id in repo file, file=" . $repoFile
  61. . ", repoId=" . $newRepoId);
  62. if ($currentRepoEnabled == 1
  63. && (($currentGpgCheck == -1 && $globalGpgCheck == 1)
  64. || ($currentGpgCheck == 1))) {
  65. if ($currentGpgLocation != "") {
  66. $logger->log_debug("Adding gpgkey $currentGpgLocation for repo"
  67. .", id=" . $currentRepoId);
  68. $response[$currentRepoId] = array ( "gpgkey" => $currentGpgLocation);
  69. }
  70. } else if ($currentRepoId != ""
  71. && $currentRepoId != "main") {
  72. $logger->log_debug("Skipping repo as repo/check not enabled"
  73. .", id=" . $currentRepoId);
  74. }
  75. $currentRepoId = $newRepoId;
  76. $currentGpgLocation = "";
  77. $currentGpgCheck = -1;
  78. $currentRepoEnabled = -1;
  79. continue;
  80. }
  81. $eIdx = strpos($line, "=");
  82. if ($eIdx === FALSE) {
  83. $logger->log_warn("Invalid line when parsing repo file, file=" . $repoFile
  84. . ", line=" . $line);
  85. continue;
  86. }
  87. $key = trim(substr($line, 0, $eIdx));
  88. $val = trim(substr($line, $eIdx+1));
  89. $logger->log_debug("Parsed line, key=" . $key . ", val=" . $val);
  90. if ($key == "gpgcheck") {
  91. if ($currentRepoId == "main") {
  92. $globalGpgCheck = intval($val);
  93. } else {
  94. $currentGpgCheck = intval($val);
  95. }
  96. } else if ($key == "enabled") {
  97. $currentRepoEnabled = intval($val);
  98. } else if ($key == "gpgkey") {
  99. $currentGpgLocation = $val;
  100. }
  101. }
  102. if ($currentRepoEnabled == 1
  103. && (($currentGpgCheck == -1 && $globalGpgCheck == 1)
  104. || ($currentGpgCheck == 1))) {
  105. if ($currentGpgLocation != "") {
  106. $response[$currentRepoId] = array ( "gpgkey" => $currentGpgLocation);
  107. }
  108. } else if ($currentRepoId != ""
  109. && $currentRepoId != "main") {
  110. $logger->log_debug("Skipping repo as repo/check not enabled"
  111. .", id=" . $currentRepoId);
  112. }
  113. return $response;
  114. }
  115. ?>