FileLib.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. App::uses('File', 'Utility');
  3. /**
  4. * Convenience class for reading, writing and appending to files.
  5. *
  6. */
  7. class FileLib extends File {
  8. /**
  9. * Allowed delimiters for csv
  10. */
  11. protected $allowedDelimiters = array(
  12. ',',
  13. ';',
  14. '|',
  15. ' ',
  16. '#');
  17. /**
  18. * Allowed enclosures for csv
  19. */
  20. protected $allowedEnclosures = array('"', '\'');
  21. /**
  22. * Allowed tags for pattern reading
  23. */
  24. protected $allowedTags = array(
  25. '<h1>',
  26. '<h2>',
  27. '<h3>',
  28. '<p>',
  29. '<b>',
  30. '<a>',
  31. '<img>');
  32. protected $defaultFormat = '%s'; // %s\t%s\t%s => some nice text
  33. /**
  34. * A better csv reader which handles encoding as well as removes completely empty lines
  35. *
  36. * @param integer $length (0 = no limit)
  37. * @param string $delimiter (null defaults to ,)
  38. * @param string $enclosure (null defaults to " - do not pass empty string)
  39. * @param string $mode
  40. * @param string $force Force open/read the file
  41. * @param boolean $removeEmpty Remove empty lines (simple newline characters without meaning)
  42. * @return array Content or false on failure
  43. */
  44. public function readCsv($length = 0, $delimiter = null, $enclosure = null, $mode = 'rb', $force = false, $removeEmpty = false) {
  45. $res = array();
  46. if ($this->open($mode, $force) === false) {
  47. return false;
  48. }
  49. if ($this->lock !== null && flock($this->handle, LOCK_SH) === false) {
  50. return false;
  51. }
  52. # php cannot handle delimiters with more than a single char
  53. if (mb_strlen($delimiter) > 1) {
  54. $count = 0;
  55. while (!feof($this->handle)) {
  56. if ($count > 100) {
  57. throw new RuntimeException('max recursion depth');
  58. }
  59. $count++;
  60. $tmp = fgets($this->handle, 8000);
  61. $tmp = explode($delimiter, $tmp);
  62. if (true || WINDOWS) {
  63. $tmp = $this->_encode($tmp);
  64. }
  65. $isEmpty = true;
  66. foreach ($tmp as $key => $val) {
  67. if (!empty($val)) {
  68. $isEmpty = false;
  69. break;
  70. }
  71. }
  72. if ($isEmpty) {
  73. continue;
  74. }
  75. $res[] = $tmp;
  76. }
  77. } else {
  78. while (true) {
  79. $data = fgetcsv($this->handle, $length, (isset($delimiter) ? $delimiter : ','), (isset($enclosure) ? $enclosure : '"'));
  80. if ($data === false) {
  81. break;
  82. }
  83. if (true || WINDOWS) {
  84. $data = $this->_encode($data);
  85. }
  86. $isEmpty = true;
  87. foreach ($data as $key => $val) {
  88. if (!empty($val)) {
  89. $isEmpty = false;
  90. break;
  91. }
  92. }
  93. if ($isEmpty && $removeEmpty) {
  94. continue;
  95. }
  96. $res[] = $data;
  97. }
  98. }
  99. if ($this->lock !== null) {
  100. flock($this->handle, LOCK_UN);
  101. }
  102. $this->close();
  103. return $res;
  104. }
  105. /**
  106. * Write an array to a csv file
  107. *
  108. * @param array $data
  109. * @param string $delimiter (null defaults to ,)
  110. * @param string $enclosure (null defaults to " - do not pass empty string)
  111. * @return boolean Success
  112. */
  113. public function writeCsv($data, $delimiter = null, $enclosure = null) {
  114. if ($this->open('w', true) !== true) {
  115. return false;
  116. }
  117. if ($this->lock !== null) {
  118. if (flock($this->handle, LOCK_EX) === false) {
  119. return false;
  120. }
  121. }
  122. $success = true;
  123. foreach ($data as $row) {
  124. if (fputcsv($this->handle, array_values($row), (isset($delimiter) ? $delimiter : ','), (isset($enclosure) ? $enclosure : '"')) === false) {
  125. $success = false;
  126. }
  127. }
  128. if ($this->lock !== null) {
  129. flock($this->handle, LOCK_UN);
  130. }
  131. $this->close();
  132. return $success;
  133. }
  134. /**
  135. * Read files with fscanf() and pattern
  136. *
  137. * @param string $format (e.g. "%s\t%s\t%s\n")
  138. * @param string $mode
  139. * @param string $force Force open/read the file
  140. * @return array Content or false on failure
  141. */
  142. public function readWithPattern($format = null, $mode = 'rb', $force = false) {
  143. $res = array();
  144. if ($this->open($mode, $force) === false) {
  145. return false;
  146. }
  147. if ($this->lock !== null && flock($this->handle, LOCK_SH) === false) {
  148. return false;
  149. }
  150. if (empty($format)) {
  151. $format = $this->defaultFormat;
  152. }
  153. while (true) {
  154. $data = fscanf($this->handle, $format);
  155. if ($data === false) {
  156. break;
  157. }
  158. $res[] = $data;
  159. }
  160. if ($this->lock !== null) {
  161. flock($this->handle, LOCK_UN);
  162. }
  163. return $res;
  164. }
  165. /**
  166. * Return the contents of this File as a string - but without tags
  167. *
  168. * @param string/array $tags: <tag><tag2><tag3> or array('<tag>',...) otherwise default tags are used
  169. * @param string $mode
  170. * @param boolean $force If true then the file will be re-opened even if its already opened, otherwise it won't
  171. * @return mixed string on success, false on failure
  172. */
  173. public function readWithTags($tags = null, $mode = 'rb', $force = false) {
  174. if ($this->open($mode, $force) === false) {
  175. return false;
  176. }
  177. if ($this->lock !== null && flock($this->handle, LOCK_SH) === false) {
  178. return false;
  179. }
  180. if (empty($tags)) {
  181. $tags = implode($this->allowedTags);
  182. } else {
  183. if (is_array($tags)) {
  184. $tags = implode($tags);
  185. }
  186. }
  187. $data = '';
  188. while (!feof($this->handle)) {
  189. $data .= fgetss($this->handle, 4096, $tags);
  190. }
  191. $data = trim($data);
  192. if ($this->lock !== null) {
  193. flock($this->handle, LOCK_UN);
  194. }
  195. return $data;
  196. }
  197. /**
  198. * Transfer array to cake structure
  199. *
  200. * @param data (usually with the first row as keys!)
  201. * @param options
  202. * - keys (defaults to first array content in data otherwise) (order is important!)
  203. * - preserve_keys (do not slug and lowercase)
  204. * @return array result or FALSE on failure
  205. */
  206. public function transfer($data, $options = array()) {
  207. $res = array();
  208. if (empty($options['keys'])) {
  209. $keys = array_shift($data);
  210. } else {
  211. $keys = $options['keys'];
  212. }
  213. foreach ($keys as $num => $key) {
  214. if (empty($options['preserve_keys'])) {
  215. $key = strtolower(Inflector::slug($key));
  216. }
  217. foreach ($data as $n => $val) {
  218. $res[$n][$key] = $val[$num];
  219. }
  220. }
  221. return $res;
  222. }
  223. /**
  224. * Assert proper encoding
  225. *
  226. * @param array Input
  227. * @return array Output
  228. */
  229. protected function _encode(array $array) {
  230. $convertedArray = array();
  231. foreach ($array as $key => $value) {
  232. if (!mb_check_encoding($key, 'UTF-8')) {
  233. $key = utf8_encode($key);
  234. }
  235. if (is_array($value)) {
  236. $value = $this->_encode($value);
  237. }
  238. if (!mb_check_encoding($value, 'UTF-8')) {
  239. $value = utf8_encode($value);
  240. }
  241. $convertedArray[$key] = trim($value);
  242. }
  243. return $convertedArray;
  244. }
  245. /**
  246. * Check if a blob string contains the BOM.
  247. * Useful for file_get_contents() + json_decode() that needs the BOM removed.
  248. *
  249. * @param string $content
  250. * @return boolean Success
  251. */
  252. public static function hasByteOrderMark($content) {
  253. return strpos($content, b"\xEF\xBB\xBF") === 0;
  254. }
  255. /**
  256. * Remove BOM from a blob string if detected.
  257. * Useful for file_get_contents() + json_decode() that needs the BOM removed.
  258. *
  259. * @param string $content
  260. * @return string Cleaned content
  261. */
  262. public static function removeByteOrderMark($content) {
  263. return trim($content, b"\xEF\xBB\xBF");
  264. }
  265. }