FileLib.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 int $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 bool 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. * @access public
  180. * 2009-06-15 ms
  181. */
  182. public function readWithTags($tags = null, $mode = 'rb', $force = false) {
  183. if ($this->open($mode, $force) === false) {
  184. return false;
  185. }
  186. if ($this->lock !== null && flock($this->handle, LOCK_SH) === false) {
  187. return false;
  188. }
  189. if (empty($tags)) {
  190. $tags = implode($this->allowedTags);
  191. } else {
  192. if (is_array($tags)) {
  193. $tags = implode($tags);
  194. }
  195. }
  196. $data = '';
  197. while (!feof($this->handle)) {
  198. $data .= fgetss($this->handle, 4096, $tags);
  199. }
  200. $data = trim($data);
  201. if ($this->lock !== null) {
  202. flock($this->handle, LOCK_UN);
  203. }
  204. return $data;
  205. }
  206. /**
  207. * Transfer array to cake structure
  208. *
  209. * @param data (usually with the first row as keys!)
  210. * @param options
  211. * - keys (defaults to first array content in data otherwise) (order is important!)
  212. * - preserve_keys (do not slug and lowercase)
  213. * @return array $result or FALSE on failure
  214. * 2010-10-15 ms
  215. */
  216. public function transfer($data, $options = array()) {
  217. $res = array();
  218. if (empty($options['keys'])) {
  219. $keys = array_shift($data);
  220. } else {
  221. $keys = $options['keys'];
  222. }
  223. foreach ($keys as $num => $key) {
  224. if (empty($options['preserve_keys'])) {
  225. $key = strtolower(Inflector::slug($key));
  226. }
  227. foreach ($data as $n => $val) {
  228. $res[$n][$key] = $val[$num];
  229. }
  230. }
  231. return $res;
  232. }
  233. /**
  234. * Assert proper encoding
  235. *
  236. * @param array Input
  237. * @return array Output
  238. */
  239. protected function _encode(array $array) {
  240. $convertedArray = array();
  241. foreach ($array as $key => $value) {
  242. if (!mb_check_encoding($key, 'UTF-8')) {
  243. $key = utf8_encode($key);
  244. }
  245. if (is_array($value)) {
  246. $value = $this->_encode($value);
  247. }
  248. if (!mb_check_encoding($value, 'UTF-8')) {
  249. $value = utf8_encode($value);
  250. }
  251. $convertedArray[$key] = trim($value);
  252. }
  253. return $convertedArray;
  254. }
  255. }