/client/Client.php

https://gitlab.com/leduc1984/roBrowser · PHP · 209 lines · 110 code · 48 blank · 51 comment · 16 complexity · 746b320c8ff4d32e9077e5e2a005a739 MD5 · raw file

  1. <?php
  2. /**
  3. * @fileoverview Client - File Manager
  4. * @author Vincent Thibault (alias KeyWorld - Twitter: @robrowser)
  5. * @version 1.5.1
  6. */
  7. final class Client
  8. {
  9. /**
  10. * @var {string} client path
  11. */
  12. static public $path = '';
  13. /**
  14. * @var {string} data.ini file
  15. */
  16. static public $data_ini = '';
  17. /**
  18. * @var {Array} grf list
  19. */
  20. static private $grfs = array();
  21. /**
  22. * @var {boolean} auto extract mode
  23. */
  24. static public $AutoExtract = false;
  25. /**
  26. * Initialize client file
  27. */
  28. static public function init()
  29. {
  30. if (empty(self::$data_ini)) {
  31. Debug::write('No DATA.INI file defined in configs ?');
  32. return;
  33. }
  34. $path = self::$path . self::$data_ini;
  35. if (!file_exists($path)) {
  36. Debug::write('File not found: ' . $path, 'error');
  37. return;
  38. }
  39. if (!is_readable($path)) {
  40. Debug::write('Can\'t read file: ' . $path, 'error');
  41. return;
  42. }
  43. // Setup GRF context
  44. $data_ini = parse_ini_file($path, true );
  45. $grfs = array();
  46. $info = pathinfo($path);
  47. $keys = array_keys($data_ini);
  48. $index = array_search('data', array_map('strtolower', $keys));
  49. if ($index === false) {
  50. Debug::write('Can\'t find token "[Data]" in "' . $path . '".', 'error');
  51. return;
  52. }
  53. $grfs = $data_ini[ $keys[$index] ];
  54. ksort($grfs);
  55. Debug::write('File ' . $path . ' loaded.', 'success');
  56. Debug::write('GRFs to use :', 'info');
  57. // Open GRFs files
  58. foreach ($grfs as $index => $grf_filename) {
  59. Debug::write($index . ') ' . $info['dirname'] . '/' . $grf_filename);
  60. self::$grfs[$index] = new Grf($info['dirname'] . '/' . $grf_filename);
  61. self::$grfs[$index]->filename = $grf_filename;
  62. }
  63. }
  64. /**
  65. * Get a file from client, search it on data folder first and then on grf
  66. *
  67. * @param {string} file path
  68. * @return {boolean} success
  69. */
  70. static public function getFile($path)
  71. {
  72. $local_path = self::$path;
  73. $local_path .= str_replace('\\', '/', $path );
  74. $local_pathEncoded = utf8_encode($local_path);
  75. $grf_path = str_replace('/', '\\', $path );
  76. Debug::write('Searching file ' . $path . '...', 'title');
  77. // Read data first
  78. if (file_exists($local_pathEncoded) && !is_dir($local_pathEncoded) && is_readable($local_pathEncoded)) {
  79. Debug::write('File found at ' . $local_path, 'success');
  80. // Store file
  81. if(self::$AutoExtract) {
  82. return self::store( $path, file_get_contents($local_pathEncoded) );
  83. }
  84. return file_get_contents($local_pathEncoded);
  85. }
  86. else {
  87. Debug::write('File not found at ' . $local_path);
  88. }
  89. foreach (self::$grfs as $grf) {
  90. // Load GRF just if needed
  91. if (!$grf->loaded) {
  92. Debug::write('Loading GRF: ' . $grf->filename, 'info');
  93. $grf->load();
  94. }
  95. // If file is found
  96. if ($grf->getFile($grf_path, $content)) {
  97. if (self::$AutoExtract) {
  98. return self::store( $path, $content );
  99. }
  100. return $content;
  101. }
  102. }
  103. return false;
  104. }
  105. /**
  106. * Storing file in data folder (convert it if needed)
  107. *
  108. * @param {string} save to path
  109. * @param {string} file content
  110. * @return {string} content
  111. */
  112. static public function store( $path, $content )
  113. {
  114. $path = utf8_encode($path);
  115. $current_path = self::$path;
  116. $local_path = $current_path . str_replace('\\', '/', $path );
  117. $parent_path = preg_replace("/[^\/]+$/", '', $local_path);
  118. if (!file_exists($parent_path)) {
  119. if (!@mkdir( $parent_path, 0777, true)) {
  120. Debug::write("Can't build path '{$parent_path}', need write permission ?", 'error');
  121. return $content;
  122. }
  123. }
  124. if (!is_writable($parent_path)) {
  125. Debug::write("Can't write file to '{$parent_path}', need write permission.", 'error');
  126. return $content;
  127. }
  128. // storing bmp images as png
  129. if (strtolower(pathinfo($path, PATHINFO_EXTENSION)) === 'bmp') {
  130. $img = imagecreatefrombmpstring( $content );
  131. $path = str_ireplace('.bmp', '.png', $local_path);
  132. imagepng($img, $path );
  133. return file_get_contents( $path );
  134. }
  135. // Saving file
  136. file_put_contents( $local_path, $content);
  137. return $content;
  138. }
  139. /**
  140. * Search files (only work in GRF)
  141. *
  142. * @param {string} regex
  143. * @return {Array} file list
  144. */
  145. static public function search($filter) {
  146. $out = array();
  147. foreach (self::$grfs as $grf) {
  148. // Load GRF only if needed
  149. if (!$grf->loaded) {
  150. $grf->load();
  151. }
  152. // Search
  153. $list = $grf->search($filter);
  154. // Merge
  155. $out = array_unique( array_merge($out, $list) );
  156. }
  157. //sort($out);
  158. return $out;
  159. }
  160. }