FileLib.php 6.8 KB

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