You are here

function socialcalc_parse_parts in Sheetnode 7

Same name and namespace in other branches
  1. 5 socialcalc.inc \socialcalc_parse_parts()
  2. 6 socialcalc.inc \socialcalc_parse_parts()
  3. 7.2 socialcalc.inc \socialcalc_parse_parts()
2 calls to socialcalc_parse_parts()
socialcalc_parse in ./socialcalc.inc
_sheetnode_ajax_load in ./sheetnode.module
AJAX function to return a sheet value.

File

./socialcalc.inc, line 33
SocialCalc manipulation functions Translated from socialcalc-3.js and companion files

Code

function socialcalc_parse_parts($data) {

  // Parse the MIME header.
  $matches = array();
  if (!preg_match('/^MIME-Version:\\s1\\.0/mi', $data, $matches)) {
    return array();
  }
  $matches = array();
  if (!preg_match('/^Content-Type:\\s*multipart\\/mixed;\\s*boundary=(\\S+)/mi', $data, $matches)) {
    return array();
  }
  $boundary = $matches[1];
  $matches = array();
  $count = preg_match_all("/^(--" . $boundary . "(?:\r\n|\n|--))/m", $data, $matches, PREG_OFFSET_CAPTURE | PREG_PATTERN_ORDER);
  if ($count === FALSE || $count < 2) {
    return array();
  }

  // Parse the SocialCalc parts.
  for ($i = 0; $i < $count; $i++) {
    if ($i == $count - 1) {
      continue;
    }
    $start = $matches[0][$i][1] + strlen($matches[1][$i][0]);
    $end = $matches[0][$i + 1][1];
    $part = substr($data, $start, $end - $start);
    $skip = array();
    preg_match("/^Content-Type:(?:.*?)(?:\r\n|\n)(?:\r\n|\n)/mi", $part, $skip);
    $part = substr($part, strlen($skip[0]));
    if ($i == 0) {

      // Sheet header
      $j = 1;
      $line = strtok($part, "\n");
      while ($line !== FALSE) {
        $line = rtrim($line);
        $names = explode(':', $line);
        if (isset($names)) {
          switch ($names[0]) {
            case 'version':
              break;
            case 'part':
              $partnames[$j++] = $names[1];
              break;
          }
        }
        $line = strtok("\n");
      }
    }
    else {

      // Sheet parts
      $parts[$partnames[$i]] = $part;
    }
  }
  return $parts;
}