PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/core/Tiki/Profile/List.php

https://gitlab.com/ElvisAns/tiki
PHP | 194 lines | 145 code | 36 blank | 13 comment | 30 complexity | 981265482db8da8e2c6818057cd5c725 MD5 | raw file
  1. <?php
  2. // (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
  3. //
  4. // All Rights Reserved. See copyright.txt for details and a complete list of authors.
  5. // Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
  6. // $Id$
  7. /**
  8. * Tiki_Profile_List
  9. *
  10. */
  11. class Tiki_Profile_List
  12. {
  13. public function getSources()
  14. {
  15. global $prefs;
  16. $raw = explode("\n", $prefs['profile_sources']);
  17. $raw = array_map('trim', $raw);
  18. $sources = [];
  19. foreach ($raw as $source) {
  20. if (! empty($source)) {
  21. $file = $this->getCacheLocation($source);
  22. $last = $this->getCacheLastUpdate($source);
  23. $short = dirname($source);
  24. $sources[] = [
  25. 'url' => $source,
  26. 'domain' => (0 === strpos($short, 'http://')) ? substr($short, 7) : $short,
  27. 'short' => $short,
  28. 'status' => ($last && filesize($file)) ? 'open' : 'closed',
  29. 'lastupdate' => $last,
  30. 'formatted' => $last ? date('Y-m-d H:i:s', $last) : '' ];
  31. }
  32. }
  33. return $sources;
  34. }
  35. public function refreshCache($path)
  36. {
  37. global $tikilib;
  38. $file = $this->getCacheLocation($path);
  39. // Replace existing with blank file
  40. if (file_exists($file)) {
  41. unlink($file);
  42. }
  43. touch($file);
  44. $content = $tikilib->httprequest($path);
  45. $parts = explode("\n", $content);
  46. $parts = array_map('trim', $parts);
  47. $good = false;
  48. foreach ($parts as $line) {
  49. // All lines contain 3 entries
  50. if (empty($line)) {
  51. continue;
  52. }
  53. if (substr_count($line, "\t") != 2) {
  54. return false;
  55. }
  56. $good = true;
  57. }
  58. // A valid file has at least one profile
  59. if (! $good) {
  60. return false;
  61. }
  62. file_put_contents($file, $content . "\n");
  63. return true;
  64. }
  65. public function getCategoryList($source = '')
  66. {
  67. $category_list = [];
  68. $sources = $this->getSources();
  69. foreach ($sources as $s) {
  70. if ($source && $s['url'] != $source) {
  71. continue;
  72. }
  73. if (! $s['lastupdate']) {
  74. continue;
  75. }
  76. $fp = fopen($this->getCacheLocation($s['url']), 'r');
  77. while (false !== $row = fgetcsv($fp, 200, "\t")) {
  78. $c = $row[0];
  79. if ($c) {
  80. $category_list[] = $c;
  81. }
  82. }
  83. }
  84. natsort($category_list);
  85. return(array_unique($category_list));
  86. }
  87. public function getList($source = '', $categories = [], $profilename = '')
  88. {
  89. $installer = new Tiki_Profile_Installer();
  90. $list = [];
  91. $sources = $this->getSources();
  92. foreach ($sources as $s) {
  93. if ($source && $s['url'] != $source) {
  94. continue;
  95. }
  96. if (! $s['lastupdate']) {
  97. continue;
  98. }
  99. $fp = fopen($this->getCacheLocation($s['url']), 'r');
  100. while (false !== $row = fgetcsv($fp, 200, "\t")) {
  101. if (count($row) != 3) {
  102. continue;
  103. }
  104. list($c, $t, $i) = $row;
  105. $key = "{$s['url']}#{$i}";
  106. if ($profilename && stripos($i, $profilename) === false) {
  107. continue;
  108. }
  109. if (array_key_exists($key, $list)) {
  110. $list[$key]['categories'][] = $c;
  111. } else {
  112. $list[$key] = [
  113. 'domain' => $s['domain'],
  114. 'categories' => [$c],
  115. 'name' => $i,
  116. 'installed' => $installer->isKeyInstalled($s['domain'], $i),
  117. ];
  118. }
  119. }
  120. fclose($fp);
  121. // Apply category filter
  122. foreach ($list as $pkey => $profile) {
  123. $in = true; // If there are no required categories, don't filter anything.
  124. if (! empty($categories)) {
  125. foreach ($categories as $category) {
  126. $in = false; // Start assuming this required category isn't in this profile's categories
  127. foreach ($profile['categories'] as $pcategory) {
  128. if ($category == $pcategory) {
  129. $in = true;
  130. break;
  131. }
  132. }
  133. if (! $in) {
  134. break;
  135. }
  136. }
  137. }
  138. if (! $in) {
  139. unset($list[$pkey]);
  140. }
  141. }
  142. }
  143. return array_values($list);
  144. }
  145. private function getCacheLocation($path)
  146. {
  147. $hash = md5($path);
  148. return "temp/cache/profile$hash";
  149. }
  150. private function getCacheLastUpdate($path)
  151. {
  152. $file = $this->getCacheLocation($path);
  153. if (! file_exists($file)) {
  154. return 0;
  155. }
  156. return filemtime($file);
  157. }
  158. }