FileLib.php 5.5 KB

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