FileLib.php 6.9 KB

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