Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
csv.inc
1 <?php
29 class CSV
30 {
31 
37  var $filepath='';
38 
45  var $_dir='';
46 
53  var $_filename='';
54 
61  var $deliminator = ',';
62 
68  var $field_headers=false;
69 
75  var $values='';
76 
82  var $line_count=0;
83 
84 
92  function CSV($filepath='')
93  {
94  if ($filepath) $this->setFilepath($filepath);
95 
96  }//end constructor
97 
98 
107  function setFieldHeaders(&$headers)
108  {
109  $this->field_headers = &$headers;
110 
111  }//end setFieldHeaders()
112 
113 
122  function setValues(&$values)
123  {
124  $this->values = &$values;
125 
126  }//end setValues()
127 
128 
137  function setDeliminator($deliminator)
138  {
139  $this->deliminator = $deliminator;
140 
141  }//end setDeliminator()
142 
143 
152  function setFilename($filename='')
153  {
154  $this->_filename = $filename;
155 
156  }//end setFilename()
157 
158 
167  function setFilepath($filepath='')
168  {
169  if ($filepath) {
170  // full path to this file
171  $this->filepath = $filepath;
172 
173  // directory of this file
174  $this->_dir = dirname($filepath);
175  if (!file_exists($this->_dir)) mkdir($this->_dir, 0775);
176 
177  // name of file without full path
178  $this->setFilename(basename($filepath));
179 
180  if (!file_exists($this->_dir)) {
181  trigger_error('CSV directory "'.$this->_dir.'" does not exist!', E_USER_WARNING);
182  return;
183  }
184  }
185 
186  }//end setFilepath()
187 
188 
199  function setHeader($header='')
200  {
201  if ($header) {
202  // set custom headers
203  header($header);
204  } else {
205  header('Content-Type: application/csv');
206  header('Content-Disposition: attachment; filename='.$this->_filename.';');
207 
208  //This is known that Internet Explorer has problems with SSL connection (https).
209  //http://support.microsoft.com/default.aspx?scid=kb;en-us;812935
210  //http://support.microsoft.com/default.aspx?scid=kb;en-us;316431
211  // do not send no cache header in https for IE
212 
213  // get protocol information
214  $url_info = parse_url(current_url());
215  $protocol = (isset($url_info['scheme'])) ? $url_info['scheme'] : NULL;
216  $using_ie6 = strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6.') !== FALSE;
217 
218  if (!is_null($protocol) && $protocol == 'https' && $using_ie6) {
219  header('Cache-Control: private, max-age=0, must-revalidate');
220  header('Pragma: private');
221  header('Expires: '.gmdate('D, d M Y H:i:s', time()-3600).' GMT');
222 
223  }
224 
225  }
226 
227  }//end setHeader()
228 
229 
243  function export($ordered=false)
244  {
245  if (!$this->filepath) {
246  // no file to write to.....lets send the header and then the file
247  $this->setHeader();
248  $fp = null;
249  } else {
250  if (!$fp = fopen($this->filepath,'w')) {
251  trigger_error('Unable to open file "'.$this->filepath.'" for writing', E_USER_WARNING);
252  return;
253  }
254  }
255 
256  if ($this->field_headers) {
257  $line = $this->formatLine($this->field_headers, $this->deliminator);
258  if (isset($fp)) {
259  fputs($fp, $line."\n");
260  } else {
261  echo $line."\n";
262  }
263  }
264 
265  $this->printValues($ordered, $fp);
266 
267  if (isset($fp)) fclose($fp);
268 
269  }//end export()
270 
284  private function printValues($ordered=false, $fp=null)
285  {
286  foreach ($this->values as $value) {
287  // if field headers are there, use the ordered version of the
288  // format line method to export
289  if ($this->field_headers && $ordered) {
290  $line = $this->formatOrderedLine($value, $this->deliminator);
291  } else {
292  $line = $this->formatLine($value, $this->deliminator);
293  }
294  if (isset($fp) && $fp !== null) {
295  fputs($fp, $line."\n");
296  } else {
297  echo $line."\n";
298  }
299  }
300  }
301 
316  function exportValues($ordered=false)
317  {
318  if ($this->filepath) {
319  if (!$fp = fopen($this->filepath,'a')) {
320  trigger_error('Unable to open file "'.$this->filepath.'" for writing', E_USER_WARNING);
321  return;
322  }
323  } else {
324  $fp = null;
325  }
326  $this->printValues($ordered, $fp);
327 
328  if (isset($fp)) fclose($fp);
329  }
330 
340  function import($start_at=1, $lines=0)
341  {
342  // can only import from a file
343  if (!$this->filepath) return;
344 
345  // check to see if the file exsts
346  if (!is_file($this->filepath)) return;
347 
348  //set auto_detect_line_endings to detect weird old mac line endings and stuff
349  $line_ending_cfg = ini_set('auto_detect_line_endings', 1);
350 
351  if (!$fp = fopen($this->filepath,'r')) {
352  trigger_error('Unable to open file "'.$this->filepath.'" for reading', E_USER_WARNING);
353  return;
354  }
355 
356  $start_at = (int)$start_at;
357  $lines = (int)$lines;
358 
359  $line_count = 0;
360  $lines_done = 0;
361 
362  while (($tmp = fgetcsv($fp, 65535, $this->deliminator)) != null) {
363  $line_count++;
364  if ($line_count >= $start_at) {
365  $this->values[] = $tmp;
366  $lines_done++;
367  if ($lines && ($lines_done >= $lines)) break;
368  }
369  }
370 
371  fclose($fp);
372 
373  if (!$line_ending_cfg) {
374  ini_set('auto_detect_line_endings', 0);
375  }
376 
377  }//end import()
378 
379 
389  function formatLine(&$array, $deliminator=',')
390  {
391  $line = '';
392  foreach ($array as $val) {
393  if (is_array($val)) {
394  $line .= '"'.$this->formatLine($val,$deliminator).'"'.$deliminator;
395  } else {
396  if (preg_match("/[".preg_quote($deliminator, '/') . "\"\n\r]/", $val)) {
397  $val = '"'.str_replace('"', '""', $val).'"';
398  }
399  $line .= $val.$deliminator;
400  }
401  }
402 
403  // strip the last deliminator
404  return $line = substr($line, 0, (strlen($deliminator) * -1));
405 
406  }//end formatLine()
407 
408 
418  function formatOrderedLine(&$array, $deliminator=',')
419  {
420  if (!$this->field_headers) return '';
421  $line_elements = Array();
422 
423  foreach($this->field_headers as $key => $val) {
424  $this_val = '';
425  if (isset($array[$key])) {
426  if (is_array($array[$key])) {
427  $this_val = '"'.$this->formatLine($array[$key],$deliminator).'"';
428  } else {
429  $this_val = $array[$key];
430  }
431 
432  if (preg_match("/[".preg_quote($deliminator, '/') . "\"\n\r]/", $this_val)) {
433  $this_val = '"'.str_replace('"', '""', $this_val).'"';
434  }
435  }
436 
437  $line_elements[] = $this_val;
438  }
439 
440  return implode($deliminator, $line_elements);
441 
442  }//end formatLine()
443 
444 
455  function countLines($start_at=0)
456  {
457  // can only count from a file
458  if (!$this->filepath) return;
459 
460  // check if the file even exists
461  if (!is_file($this->filepath)) {
462  $this->line_count = 0;
463  return;
464  }
465 
466  if (!$fp = fopen($this->filepath,'r')) {
467  trigger_error('Unable to open file "'.$this->filepath.'" for counting', E_USER_WARNING);
468  return;
469  }
470 
471  $line_count = 0;
472 
473  while (($tmp = fgetcsv($fp, 65535, $this->deliminator)) != null) {
474  $line_count++;
475  }
476 
477  $this->line_count = $line_count - $start_at;
478 
479  fclose($fp);
480 
481  }//end countLines()
482 
483 
490  function isUnique($string)
491  {
492  // can only import from a file
493  if (!$this->filepath) return true;
494 
495  // check if the file even exists
496  if (!is_file($this->filepath)) return true;
497 
498  if (!$fp = fopen($this->filepath,'r')) {
499  trigger_error('Unable to open file "'.$this->filepath.'" for reading', E_USER_WARNING);
500  return true;
501  }
502 
503  while (($tmp = fgetcsv($fp, 65535, $this->deliminator)) != null) {
504  if (in_array($string, $tmp)) return false;
505  }
506 
507  fclose($fp);
508 
509  return true;
510 
511  }//end isUnique()
512 
513 
514 }//end class
515 
516 ?>