private boolean setLobParameter(Env env, int index, Value value) {
    if (_preparedStmt == null) {
      return false;
    }

    try {
      if (value == null || value.isNull()) {
        _preparedStmt.setObject(index, null);
      } else if (value.isString()) {
        _preparedStmt.setBinaryStream(index, value.toInputStream(), value.length());
      } else {
        InputStream inputStream = value.toInputStream();

        if (inputStream == null) {
          env.warning(
              L.l(
                  "type {0} ({1}) for parameter index {2} cannot be used for lob",
                  value.getType(), value.getClass(), index));
          return false;
        }

        int length = -1;

        if (value instanceof FileReadValue) {
          length = (int) ((FileReadValue) value).getLength();

          if (length <= 0) length = -1;
        }

        if (length < 0) {
          TempBuffer tempBuffer = TempBuffer.allocate();

          try {
            byte[] bytes = new byte[1024];

            int len;

            while ((len = inputStream.read(bytes, 0, 1024)) != -1) tempBuffer.write(bytes, 0, len);
          } catch (IOException e) {
            env.warning(e);
            return false;
          }

          TempReadStream tempReadStream = new TempReadStream(tempBuffer);
          tempReadStream.setFreeWhenDone(true);

          _preparedStmt.setBinaryStream(
              index, new ReadStream(tempReadStream), tempBuffer.getLength());
        } else {
          _preparedStmt.setBinaryStream(index, inputStream, length);
        }
      }
    } catch (SQLException e) {
      setError(env, e);
      return false;
    }

    return true;
  }
Example #2
0
  /** Connects to the underlying database. */
  @Override
  protected ConnectionEntry connectImpl(
      Env env,
      String host,
      String userName,
      String password,
      String dbname,
      int port,
      String socket,
      int flags,
      String driver,
      String url,
      boolean isNewLink) {
    if (isConnected()) {
      env.warning(L.l("Connection is already opened to '{0}'", this));
      return null;
    }

    try {

      if (host == null || host.equals("")) {
        host = "localhost";
      }

      if (driver == null || driver.equals("")) {
        driver = "org.postgresql.Driver";
      }

      if (url == null || url.equals("")) {
        url = "jdbc:postgresql://" + host + ":" + port + "/" + dbname;
      }

      ConnectionEntry jConn;

      jConn = env.getConnection(driver, url, userName, password, !isNewLink);

      return jConn;
    } catch (SQLException e) {
      env.warning("A link to the server could not be established. " + e.toString());
      env.setSpecialValue("postgres.connectErrno", LongValue.create(e.getErrorCode()));
      env.setSpecialValue("postgres.connectError", env.createString(e.getMessage()));

      log.log(Level.FINE, e.toString(), e);

      return null;
    } catch (Exception e) {
      env.warning("A link to the server could not be established. " + e.toString());
      env.setSpecialValue("postgres.connectError", env.createString(e.getMessage()));

      log.log(Level.FINE, e.toString(), e);
      return null;
    }
  }
  /**
   * Converts node tree to a valid xml string.
   *
   * @return xml string
   */
  public final Value asXML(Env env, @Optional Value filename) {
    Value value = toXML(env);

    if (!value.isString()) {
      return value;
    }

    StringValue str = value.toStringValue(env);

    if (filename.isDefault()) {
      return str;
    } else {
      Path path = env.lookupPwd(filename);

      OutputStream os = null;

      try {
        os = path.openWrite();

        str.writeTo(os);

        return BooleanValue.TRUE;
      } catch (IOException e) {
        env.warning(e);

        return BooleanValue.FALSE;
      } finally {
        if (os != null) {
          IoUtil.close(os);
        }
      }
    }
  }
Example #4
0
  /**
   * Runs an XPath expression on this node.
   *
   * @param env
   * @param expression
   * @return array of results
   * @throws XPathExpressionException
   */
  public Value xpath(Env env, String expression) {
    try {
      XPath xpath = XPathFactory.newInstance().newXPath();

      InputSource is = new InputSource(asXML(env).toInputStream());
      NodeList nodes = (NodeList) xpath.evaluate(expression, is, XPathConstants.NODESET);

      int nodeLength = nodes.getLength();

      if (nodeLength == 0) return NullValue.NULL;

      // There are matching nodes
      ArrayValue result = new ArrayValueImpl();
      for (int i = 0; i < nodeLength; i++) {
        Node node = nodes.item(i);

        boolean isPrefix = node.getPrefix() != null;

        SimpleXMLElement xml =
            buildNode(env, _cls, null, nodes.item(i), node.getNamespaceURI(), isPrefix);

        result.put(wrapJava(env, _cls, xml));
      }

      return result;
    } catch (XPathExpressionException e) {
      env.warning(e);
      log.log(Level.FINE, e.getMessage());

      return NullValue.NULL;
    }
  }
Example #5
0
  public static Value __construct(Env env, @Optional String queueName) {
    JMSQueue queue = JMSModule.message_get_queue(env, queueName, null);

    if (queue == null) {
      env.warning(L.l("'{0}' is an unknown JMSQueue", queueName));
      return NullValue.NULL;
    }

    return env.wrapJava(queue);
  }
Example #6
0
  protected static Value create(
      Env env,
      QuercusClass cls,
      Value data,
      int options,
      boolean dataIsUrl,
      Value namespaceV,
      boolean isPrefix) {
    if (data.length() == 0) {
      env.warning(L.l("xml data must have length greater than 0"));
      return BooleanValue.FALSE;
    }

    try {
      String namespace = null;

      if (!namespaceV.isNull()) namespace = namespaceV.toString();

      Node node = parse(env, data, options, dataIsUrl, namespace, isPrefix);

      if (node == null) {
        return BooleanValue.FALSE;
      }

      SimpleXMLElement elt = buildNode(env, cls, null, node, namespace, isPrefix);

      return wrapJava(env, cls, elt);

    } catch (IOException e) {
      env.warning(e);

      return BooleanValue.FALSE;
    } catch (ParserConfigurationException e) {
      env.warning(e);

      return BooleanValue.FALSE;
    } catch (SAXException e) {
      env.warning(e);

      return BooleanValue.FALSE;
    }
  }
Example #7
0
  /**
   * Fetch a result row as an associative, a numeric array, or both.
   *
   * @param type one of MYSQLI_ASSOC, MYSQLI_NUM, or MYSQLI_BOTH (default) By using the MYSQLI_ASSOC
   *     constant this function will behave identically to the mysqli_fetch_assoc(), while
   *     MYSQLI_NUM will behave identically to the mysqli_fetch_row() function. The final option
   *     MYSQLI_BOTH will create a single array with the attributes of both.
   * @return a result row as an associative, a numeric array, or both or null if there are no more
   *     rows in the result set
   */
  @ReturnNullAsFalse
  public ArrayValue fetch_array(Env env, @Optional("MYSQLI_BOTH") int type) {
    if (type != MysqliModule.MYSQLI_ASSOC
        && type != MysqliModule.MYSQLI_BOTH
        && type != MysqliModule.MYSQLI_NUM) {
      env.warning(L.l("invalid result_type"));
      return null;
    }

    return fetchArray(env, type);
  }
Example #8
0
  /** Adds an attribute to this node. */
  public void addAttribute(Env env, String name, StringValue value, @Optional String namespace) {
    if (namespace != null && namespace.length() > 0) {
      int colonIndex = name.indexOf(":");

      // php/1x42
      if (colonIndex <= 0 || colonIndex >= name.length()) {
        env.warning(
            L.l("Adding attributes with namespaces requires " + "attribute name with a prefix"));
        return;
      }
    }

    if (_attributes == null) _attributes = new ArrayList<SimpleXMLElement>();

    SimpleXMLAttribute attr = new SimpleXMLAttribute(env, _cls, this, name, namespace, value);

    _attributes.add(attr);
  }
Example #9
0
  private static Node parse(
      Env env, Value data, int options, boolean dataIsUrl, String namespace, boolean isPrefix)
      throws IOException, ParserConfigurationException, SAXException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();

    Document document = null;

    if (dataIsUrl) {
      Path path = env.lookup(data.toStringValue());

      // PHP throws an Exception instead
      if (path == null) {
        log.log(Level.FINE, L.l("Cannot read file/URL '{0}'", data));
        env.warning(L.l("Cannot read file/URL '{0}'", data));

        return null;
      }

      ReadStream is = path.openRead();

      try {
        document = builder.parse(is);
      } finally {
        is.close();
      }
    } else {
      StringReader reader = new java.io.StringReader(data.toString());

      document = builder.parse(new InputSource(reader));
    }

    NodeList childList = document.getChildNodes();

    // php/1x70
    for (int i = 0; i < childList.getLength(); i++) {
      if (childList.item(i).getNodeType() == Node.ELEMENT_NODE) return childList.item(i);
    }

    return childList.item(0);
  }
Example #10
0
 /**
  * Gets the name for this category.
  */
 private String getCategory(Env env, int category)
 {
   switch (category) {
     case StringModule.LC_MESSAGES:
       return "LC_MESSAGES";
     case StringModule.LC_ALL:
       return "LC_ALL";
     case StringModule.LC_CTYPE:
       return "LC_CTYPE";
     case StringModule.LC_NUMERIC:
       return "LC_NUMERIC";
     case StringModule.LC_TIME:
       return "LC_TIME";
     case StringModule.LC_COLLATE:
       return "LC_COLLATE";
     case StringModule.LC_MONETARY:
       return "LC_MONETARY";
     default:
       env.warning(L.l("Invalid category. Please use named constants"));
       return "LC_MESSAGES";
   }
 }
  public Value filter_input_array(
      Env env, int type, @Optional Value definition, @Optional("true") boolean isAddEmpty) {
    ArrayValue inputArray;

    switch (type) {
      case INPUT_POST:
        inputArray = env.getInputPostArray();
        break;
      case INPUT_GET:
        inputArray = env.getInputGetArray();
        break;
      case INPUT_COOKIE:
        inputArray = env.getInputCookieArray();
        break;
      case INPUT_ENV:
        inputArray = env.getInputEnvArray();
        break;
      default:
        return env.warning(L.l("filter input type is unknown: {0}", type));
    }

    Filter filter = getFilter(env, definition);

    ArrayValue array = new ArrayValueImpl();

    for (Map.Entry<Value, Value> entry : inputArray.entrySet()) {
      Value key = entry.getKey();
      Value value = entry.getValue();

      Value newKey = filter.filter(env, key, definition);
      Value newValue = filter.filter(env, value, definition);

      array.put(newKey, newValue);
    }

    return array;
  }
  public static Value filter_input(
      Env env, int type, StringValue name, @Optional Value filterIdV, @Optional Value flagV) {
    ArrayValue array;

    switch (type) {
      case INPUT_POST:
        array = env.getInputPostArray();
        break;
      case INPUT_GET:
        array = env.getInputGetArray();
        break;
      case INPUT_COOKIE:
        array = env.getInputCookieArray();
        break;
      case INPUT_ENV:
        array = env.getInputEnvArray();
        break;
      default:
        return env.warning(L.l("filter input type is unknown: {0}", type));
    }

    Filter filter = getFilter(env, filterIdV);

    Value value = array.get(name);

    if (value == UnsetValue.UNSET) {
      int flags = AbstractFilter.getFlags(env, flagV);

      if ((flags & FILTER_NULL_ON_FAILURE) > 0) {
        return BooleanValue.FALSE;
      } else {
        return NullValue.NULL;
      }
    }

    return filter.filter(env, value, flagV);
  }
  public Value url_stat(Env env, StringValue path, LongValue flags) {
    env.warning(L.l("stat not supported by protocol"));

    return BooleanValue.FALSE;
  }
  public boolean rmdir(Env env, StringValue path, LongValue options) {
    env.warning(L.l("rmdir not supported by protocol"));

    return false;
  }
  public boolean rename(Env env, StringValue path_from, StringValue path_to) {
    env.warning(L.l("rename not supported by protocol"));

    return false;
  }
  public boolean unlink(Env env, StringValue path) {
    env.warning(L.l("unlink not supported by protocol"));

    return false;
  }
  public Value opendir(Env env, StringValue path, LongValue flags) {
    env.warning(L.l("opendir not supported by protocol"));

    return BooleanValue.FALSE;
  }
Example #18
0
  /**
   * Evaluates the expression.
   *
   * @param env the calling environment.
   * @return the expression value.
   */
  public Value eval(Env env) {
    env.warning("required argument missing");

    return NullValue.NULL;
  }