/inc/app/help/lib/Help.php

https://github.com/durand54/sitellite · PHP · 216 lines · 165 code · 37 blank · 14 comment · 37 complexity · f5d12af92acacaad9733ddefddc6e48d MD5 · raw file

  1. <?php
  2. function help_get_id ($file) {
  3. return str_replace ('.html', '', basename ($file));
  4. }
  5. function help_get_title ($body, $id) {
  6. if (preg_match ('/<h[1-6][^>]*>([^<]+)<\/h[1-6]>/i', $body, $regs)) {
  7. return $regs[1];
  8. }
  9. return $id;
  10. }
  11. function help_extract_info ($file, $query, $hits) {
  12. $data = @join ('', @file ($file));
  13. $res = new StdClass;
  14. // id is helpfile
  15. $res->id = help_get_id ($file);
  16. // title is first header
  17. $res->title = help_get_title ($data, $res->id);
  18. $data = strip_tags ($data);
  19. // hits
  20. $res->hits = $hits;
  21. // description is paragraph containing query
  22. if (preg_match ('/[\r\n]+(.*?)(' . preg_quote ($query[0], '/') . ')([^\r\n]*)[\r\n]+/i', $data, $regs)) {
  23. $res->description = help_highlight ($regs[1] . $regs[2] . $regs[3], $query);
  24. } else {
  25. $res->description = intl_get ('No summary available.');
  26. }
  27. return $res;
  28. }
  29. function help_search ($appname, $query, $lang) {
  30. loader_import ('saf.File');
  31. loader_import ('saf.File.Directory');
  32. $query = help_split_query ($query);
  33. //info ($query);
  34. $files = Dir::find ('*.html', 'inc/app/' . $appname . '/docs/' . $lang);
  35. if (count ($files) == 0) {
  36. return false; // no help files
  37. }
  38. $results = array ();
  39. foreach ($files as $file) {
  40. if (strstr ($file, '/.')) {
  41. continue;
  42. }
  43. if ($hits = File::contains ($query, false, $file, true, 'strip_tags')) {
  44. $results[] = help_extract_info ($file, $query, $hits);
  45. }
  46. }
  47. // sort by hits now
  48. for ($i = 0; $i < count ($results); $i++) {
  49. for ($j = $i + 1; $j < count ($results); $j++) {
  50. if ($results[$j]->hits > $results[$i]->hits) {
  51. $tmp = $results[$j];
  52. $results[$j] = $results[$i];
  53. $results[$i] = $tmp;
  54. }
  55. }
  56. }
  57. return $results;
  58. }
  59. function help_split_query ($query) {
  60. $pieces = array ();
  61. $res = preg_split ('/("|[ ]+)/', $query, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
  62. $open = false;
  63. foreach ($res as $piece) {
  64. //echo '--' . htmlentities ($piece) . '--' . BR;
  65. if ($open && $piece == '"') {
  66. $open = false;
  67. } elseif ($open) {
  68. $pieces[count ($pieces) - 1] .= $piece;
  69. } elseif ($piece == '"') {
  70. $open = true;
  71. $pieces[] = '';
  72. } elseif (preg_match ('/[a-zA-Z0-9_\'\.\/"-]/', $piece)) {
  73. $pieces[] = $piece;
  74. }
  75. }
  76. return $pieces;
  77. }
  78. function help_highlight ($string, $queries) {
  79. $string = strip_tags ($string);
  80. foreach ($queries as $query) {
  81. $string = preg_replace ('/(' . preg_quote ($query, '/') . ')/i', '<strong class=\'highlighted\'>\1</strong>', $string);
  82. }
  83. return $string;
  84. }
  85. function help_get_pages ($appname, $lang) {
  86. loader_import ('saf.File.Directory');
  87. $files = Dir::find ('*.html', 'inc/app/' . $appname . '/docs/' . $lang);
  88. if (count ($files) == 0) {
  89. return array (); // no help files
  90. }
  91. foreach ($files as $k => $v) {
  92. if (strstr ($v, '/.')) {
  93. unset ($files[$k]);
  94. }
  95. }
  96. sort ($files);
  97. return $files;
  98. }
  99. function help_get_previous ($appname, $lang, $current, $files) {
  100. // get rid of index.html
  101. $key = array_search ('inc/app/' . $appname . '/docs/' . $lang . '/index.html', $files);
  102. if ($key !== false && $key !== null) {
  103. unset ($files[$key]);
  104. }
  105. // find current file
  106. $fullname = 'inc/app/' . $appname . '/docs/' . $lang . '/' . $current . '.html';
  107. $key = array_search ($fullname, $files);
  108. if ($key !== false && $key !== null) {
  109. $key--;
  110. if (! isset ($files[$key])) {
  111. return false;
  112. }
  113. // get id and title of previous page
  114. $data = @join ('', @file ($files[$key]));
  115. $id = help_get_id ($files[$key]);
  116. return array ('id' => $id, 'title' => help_get_title ($data, $id));
  117. }
  118. return false;
  119. }
  120. function help_get_next ($appname, $lang, $current, $files) {
  121. // get rid of index.html
  122. $key = array_search ('inc/app/' . $appname . '/docs/' . $lang . '/index.html', $files);
  123. if ($key !== false && $key !== null) {
  124. unset ($files[$key]);
  125. }
  126. // find current file
  127. $fullname = 'inc/app/' . $appname . '/docs/' . $lang . '/' . $current . '.html';
  128. $key = array_search ($fullname, $files);
  129. if ($key !== false && $key !== null) {
  130. $key++;
  131. if (! isset ($files[$key])) {
  132. return false;
  133. }
  134. // get id and title of next page
  135. $data = @join ('', @file ($files[$key]));
  136. $id = help_get_id ($files[$key]);
  137. return array ('id' => $id, 'title' => help_get_title ($data, $id));
  138. }
  139. return false;
  140. }
  141. function help_get_apps () {
  142. $out = array ();
  143. $dh = opendir ('inc/app');
  144. if (! $dh) {
  145. return array ('cms' => intl_get ('Sitellite Content Manager'));
  146. }
  147. while (false !== ($file = readdir ($dh))) {
  148. if (strpos ($file, '.') === 0 || ! @is_dir ('inc/app/' . $file)) {
  149. continue;
  150. }
  151. // look for docs/en directory
  152. if (@is_dir ('inc/app/' . $file . '/docs/en')) {
  153. if (! @file_exists ('inc/app/' . $file . '/conf/config.ini.php')) {
  154. $name = ucfirst ($file);
  155. } else {
  156. $data = parse_ini_file ('inc/app/' . $file . '/conf/config.ini.php');
  157. $name = $data['app_name'];
  158. if (empty ($name)) {
  159. $name = ucfirst ($file);
  160. }
  161. }
  162. $out[$file] = $name;
  163. }
  164. }
  165. closedir ($dh);
  166. asort ($out);
  167. return $out;
  168. }
  169. function help_get_langs ($appname) {
  170. if (@file_exists ('inc/app/' . $appname . '/docs/languages.php')) {
  171. $res = parse_ini_file ('inc/app/' . $appname . '/docs/languages.php');
  172. if (! $res['en']) {
  173. return array_merge (array ('en' => 'English'), $res);
  174. }
  175. return $res;
  176. }
  177. return array ('en' => 'English');
  178. }
  179. ?>