FileLib.php 5.6 KB

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