You are here

function coder_upgrade_upgrade_call_drupal_set_header_alter in Coder 7.2

Same name and namespace in other branches
  1. 7 coder_upgrade/conversions/call.inc \coder_upgrade_upgrade_call_drupal_set_header_alter()

Implements hook_upgrade_call_drupal_set_header_alter().

File

coder_upgrade/conversions/call.inc, line 1183
Provides conversion routines applied to function calls.

Code

function coder_upgrade_upgrade_call_drupal_set_header_alter(&$node, &$reader) {

  // DONE
  // Create helper objects.
  $editor = PGPEditor::getInstance();

  // Get the function call object.
  $item =& $node->data;

  // Process function call.
  $name =& $item->name;

  // http://drupal.org/node/224333#drupal_set_header_renamed
  $name['value'] = 'drupal_add_http_header';
  if (!$item
    ->parameterCount()) {

    // Nothing else to do.
    return;
  }
  $temp = $item
    ->printParameter();
  $p0 = $item
    ->getParameter();
  if ($p0
    ->count() > 1) {

    // This is a complex expression.
    if ($p0
      ->isType(T_CONSTANT_ENCAPSED_STRING)) {
      $operand =& $p0
        ->getElement();
      if (($pos = strpos($operand['value'], ':')) !== FALSE) {
        $type = trim(substr($operand['value'], 0, $pos), "'\" ");

        /*
         * Retain the existing quote type in case of any embedded quotes and
         * other quoted items in the rest of the expression.
         * Ex: 'Content-Disposition: attachment; filename="'
         */
        $operand['value'] = str_replace(array(
          $type,
          ': ',
          ':',
        ), '', $operand['value']);

        // Append the charset onto the parameter string.
        //        if ($type == 'Content-Type' && strpos($value, 'charset') === FALSE) {
        //          $value .= '; charset=utf-8';
        //        }
        $type = "'{$type}'";

        // if value == '', then remove operator == '.' from expression
        if ($operand['value'] == "''" || $operand['value'] == '""') {
          $p0
            ->deleteElement();
          $p0
            ->deleteElement();

          // Delete the first operator.
        }
        $editor
          ->insertParameter($item, 0, $type);
      }
    }
    elseif ($p0
      ->isType(T_VARIABLE)) {
      $temp = $p0
        ->getElement()
        ->toString();
      if (strpos($temp, "\$_SERVER['SERVER_PROTOCOL']") !== FALSE || strpos($temp, '$_SERVER["SERVER_PROTOCOL"]') !== FALSE) {
        $p0
          ->deleteElement();
        $p0
          ->deleteElement();

        // Delete the first operator.
        if ($p0
          ->isType(T_CONSTANT_ENCAPSED_STRING)) {

          // Remove a leading space, e.g. ' 500 Internal server error'.
          $operand =& $p0
            ->getElement();
          $operand['value'] = str_replace(array(
            '" ',
            "' ",
          ), array(
            '"',
            "'",
          ), $operand['value']);
        }
        $editor
          ->insertParameter($item, 0, "'Status'");
      }
    }
  }
  else {
    if ($p0
      ->isType(T_CONSTANT_ENCAPSED_STRING)) {
      if (strpos($temp, 'HTTP/') !== FALSE) {
        $temp = trim(substr($temp, 9), "'\" ");

        // Assumes protocol = HTTP/n.n where n is a digit.
        $editor
          ->setParameters($item, array(
          "'Status'",
          "'{$temp}'",
        ));
      }
      elseif (strpos($temp, ':') !== FALSE) {
        list($type, $value) = explode(':', $temp);
        $type = trim($type, "'\" ");
        $value = trim($value, "'\" ");
        if ($type == 'Content-Type' && strpos($value, 'charset') === FALSE) {
          $value .= '; charset=utf-8';
        }
        $type = "'{$type}'";
        $value = "'{$value}'";
        $editor
          ->setParameters($item, array(
          $type,
          $value,
        ));
      }
    }
    else {

      // Could be a variable that we could search for and change?
    }
  }

  // TODO the hook_file_download() changes. Use parser on the entire function.
}