Пример #1
0
  private static void put(ArrayValue array, Value key, Value value, boolean addSlashes) {
    if (addSlashes && value.isString()) {
      value = StringModule.addslashes(value.toStringValue());
    }

    if (key == null) array.put(value);
    else array.put(key, value);
  }
Пример #2
0
  /** Converts to an array. */
  @Override
  public ArrayValue toArray() {
    ArrayValue array = new ArrayValueImpl();

    for (Map.Entry<Value, Value> entry : entrySet()) {
      array.put(entry.getKey(), entry.getValue());
    }

    return array;
  }
Пример #3
0
  public Value receive(Env env, @Optional("1") long timeout) throws JMSException {
    Message message = _consumer.receive(timeout);

    if (message == null) return BooleanValue.FALSE;

    if (message instanceof ObjectMessage) {
      Object object = ((ObjectMessage) message).getObject();

      return env.wrapJava(object);
    } else if (message instanceof TextMessage) {
      return env.createString(((TextMessage) message).getText());
    } else if (message instanceof StreamMessage) {
      Object object = ((StreamMessage) message).readObject();

      return env.wrapJava(object);
    } else if (message instanceof BytesMessage) {
      BytesMessage bytesMessage = (BytesMessage) message;
      int length = (int) bytesMessage.getBodyLength();

      StringValue bb = env.createBinaryBuilder(length);

      TempBuffer tempBuffer = TempBuffer.allocate();
      int sublen;

      while (true) {
        sublen = bytesMessage.readBytes(tempBuffer.getBuffer());

        if (sublen > 0) bb.append(tempBuffer.getBuffer(), 0, sublen);
        else break;
      }

      TempBuffer.free(tempBuffer);

      return bb;
    } else if (message instanceof MapMessage) {
      MapMessage mapMessage = (MapMessage) message;

      Enumeration mapNames = mapMessage.getMapNames();

      ArrayValue array = new ArrayValueImpl();

      while (mapNames.hasMoreElements()) {
        String name = mapNames.nextElement().toString();

        Object object = mapMessage.getObject(name);

        array.put(env.createString(name), env.wrapJava(object));
      }

      return array;
    } else {
      return BooleanValue.FALSE;
    }
  }
Пример #4
0
  public boolean send(@NotNull Value value, @Optional JMSQueue replyTo) throws JMSException {
    Message message = null;

    if (value.isArray()) {
      message = _session.createMapMessage();

      ArrayValue array = (ArrayValue) value;

      Set<Map.Entry<Value, Value>> entrySet = array.entrySet();

      for (Map.Entry<Value, Value> entry : entrySet) {
        if (entry.getValue() instanceof BinaryValue) {
          byte[] bytes = ((BinaryValue) entry.getValue()).toBytes();

          ((MapMessage) message).setBytes(entry.getKey().toString(), bytes);
        } else {
          // every primitive except for bytes can be translated from a string
          ((MapMessage) message).setString(entry.getKey().toString(), entry.getValue().toString());
        }
      }
    } else if (value instanceof BinaryValue) {
      message = _session.createBytesMessage();

      byte[] bytes = ((BinaryValue) value).toBytes();

      ((BytesMessage) message).writeBytes(bytes);
    } else if (value.isLongConvertible()) {
      message = _session.createStreamMessage();

      ((StreamMessage) message).writeLong(value.toLong());
    } else if (value.isDoubleConvertible()) {
      message = _session.createStreamMessage();

      ((StreamMessage) message).writeDouble(value.toDouble());
    } else if (value.toJavaObject() instanceof String) {
      message = _session.createTextMessage();

      ((TextMessage) message).setText(value.toString());
    } else if (value.toJavaObject() instanceof Serializable) {
      message = _session.createObjectMessage();

      ((ObjectMessage) message).setObject((Serializable) value.toJavaObject());
    } else {
      return false;
    }

    if (replyTo != null) message.setJMSReplyTo(replyTo._destination);

    _producer.send(message);

    return true;
  }
Пример #5
0
  @SuppressWarnings("unchecked")
  public <T> Dataset withValue(String name, T value) {
    Object valueHolder = getAttribute(name);
    if (valueHolder == null) {
      with(name, value);

    } else if (valueHolder instanceof ArrayValue) {
      ((ArrayValue<T>) valueHolder).with(value);

    } else {
      ArrayValue<T> av = add(name, new ArrayValue<T>((T) valueHolder));
      av.with(value);
    }
    return this;
  }
Пример #6
0
    void addValue(Value value) {
      ArrayValue array = null;

      if (!_isArray) {
        _isArray = true;
        Value oldValue = _value;
        _value = new ArrayValueImpl();
        array = (ArrayValue) _value;
        array.append(oldValue);
      } else {
        array = (ArrayValue) _value;
      }

      array.append(value);
    }
Пример #7
0
  private static void addFormFile(
      Env env,
      ArrayValue files,
      String fileName,
      String tmpName,
      String mimeType,
      long fileLength,
      boolean addSlashesToValues,
      long maxFileSize) {
    ArrayValue entry = new ArrayValueImpl();
    int error;

    // php/1667
    long uploadMaxFilesize = env.getIniBytes("upload_max_filesize", 2 * 1024 * 1024);

    if (fileName.length() == 0)
      // php/0864
      error = FileModule.UPLOAD_ERR_NO_FILE;
    else if (fileLength > uploadMaxFilesize) error = FileModule.UPLOAD_ERR_INI_SIZE;
    else if (fileLength > maxFileSize) error = FileModule.UPLOAD_ERR_FORM_SIZE;
    else error = FileModule.UPLOAD_ERR_OK;

    addFormValue(env, entry, "name", env.createString(fileName), null, addSlashesToValues);

    long size;

    if (error != FileModule.UPLOAD_ERR_INI_SIZE) {
      size = fileLength;
    } else {
      mimeType = "";
      tmpName = "";
      size = 0;
    }

    if (mimeType != null) {
      addFormValue(env, entry, "type", env.createString(mimeType), null, addSlashesToValues);

      entry.put("type", mimeType);
    }

    addFormValue(env, entry, "tmp_name", env.createString(tmpName), null, addSlashesToValues);

    addFormValue(env, entry, "error", LongValue.create(error), null, addSlashesToValues);

    addFormValue(env, entry, "size", LongValue.create(size), null, addSlashesToValues);

    addFormValue(env, files, null, entry, null, addSlashesToValues);
  }
Пример #8
0
  private static void putRequestMap(
      Env env,
      ArrayValue post,
      ArrayValue files,
      HttpServletRequest request,
      boolean addSlashesToValues,
      boolean isAllowUploads) {
    // this call consumes the inputstream
    Map<String, String[]> map = request.getParameterMap();

    if (map == null) return;

    long maxFileSize = Long.MAX_VALUE;

    Value maxFileSizeV = post.get(MAX_FILE_SIZE);
    if (maxFileSizeV.isNull()) maxFileSize = maxFileSizeV.toLong();

    if (isAllowUploads) {
      for (Map.Entry<String, String[]> entry : map.entrySet()) {
        String key = entry.getKey();

        int len = key.length();

        if (len < 10 || !key.endsWith(".filename")) continue;

        String name = key.substring(0, len - 9);

        String[] fileNames = request.getParameterValues(name + ".filename");
        String[] tmpNames = request.getParameterValues(name + ".file");
        String[] mimeTypes = request.getParameterValues(name + ".content-type");

        for (int i = 0; i < fileNames.length; i++) {
          long fileLength = new FilePath(tmpNames[i]).getLength();

          addFormFile(
              env,
              files,
              name,
              fileNames[i],
              tmpNames[i],
              mimeTypes[i],
              fileLength,
              addSlashesToValues,
              maxFileSize);
        }
      }
    }

    ArrayList<String> keys = new ArrayList<String>();

    keys.addAll(request.getParameterMap().keySet());

    Collections.sort(keys);

    for (String key : keys) {
      String[] value = request.getParameterValues(key);

      Post.addFormValue(env, post, key, value, addSlashesToValues);
    }
  }
Пример #9
0
  /**
   * @param env the PHP executing environment
   * @return array of fieldDirect objects
   */
  public Value getFieldDirectArray(Env env) {
    ArrayValue array = new ArrayValueImpl();

    try {
      int numColumns = getMetaData().getColumnCount();

      for (int i = 0; i < numColumns; i++) {
        array.put(fetchFieldDirect(env, i));
      }

      return array;
    } catch (SQLException e) {
      log.log(Level.FINE, e.toString(), e);
      return BooleanValue.FALSE;
    }
  }
Пример #10
0
  static void fillPost(
      Env env,
      ArrayValue postArray,
      ArrayValue files,
      HttpServletRequest request,
      boolean addSlashesToValues,
      boolean isAllowUploads) {
    InputStream is = null;

    try {
      String encoding = request.getCharacterEncoding();
      String contentType = request.getHeader("Content-Type");

      is = request.getInputStream();

      fillPost(
          env,
          postArray,
          files,
          is,
          contentType,
          encoding,
          Integer.MAX_VALUE,
          addSlashesToValues,
          isAllowUploads);

      if (postArray.getSize() == 0) {
        // needs to be last or else this function will consume the inputstream
        putRequestMap(env, postArray, files, request, addSlashesToValues, isAllowUploads);
      }

    } catch (IOException e) {
      env.warning(e);
    } finally {
      try {
        if (is != null) is.close();
      } catch (IOException e) {
      }
    }
  }
Пример #11
0
  static void fillPost(
      Env env,
      ArrayValue postArray,
      ArrayValue files,
      InputStream is,
      String contentType,
      String encoding,
      int contentLength,
      boolean addSlashesToValues,
      boolean isAllowUploads) {
    long maxPostSize = env.getIniBytes("post_max_size", 0);

    try {
      if (encoding == null) encoding = env.getHttpInputEncoding();

      if (contentType != null && contentType.startsWith("multipart/form-data")) {

        String boundary = getBoundary(contentType);

        ReadStream rs = new ReadStream(new VfsStream(is, null));

        if (boundary == null) {
          env.warning(L.l("multipart/form-data POST is missing boundary"));

          return;
        }

        MultipartStream ms = new MultipartStream(rs, boundary);

        if (encoding != null) ms.setEncoding(encoding);

        readMultipartStream(env, ms, postArray, files, addSlashesToValues, isAllowUploads);

        rs.close();

        if (rs.getLength() > maxPostSize) {
          env.warning(
              L.l("POST length of {0} exceeds max size of {1}", rs.getLength(), maxPostSize));

          postArray.clear();
          files.clear();

          return;
        }
      } else {
        StringValue bb = env.createBinaryBuilder();

        bb.appendReadAll(is, Integer.MAX_VALUE);

        if (bb.length() > maxPostSize) {
          env.warning(L.l("POST length of {0} exceeds max size of {1}", bb.length(), maxPostSize));
          return;
        }

        env.setInputData(bb);

        if (contentType != null && contentType.startsWith("application/x-www-form-urlencoded"))
          StringUtility.parseStr(env, bb, postArray, false, encoding);
      }

    } catch (IOException e) {
      env.warning(e);
    } finally {
    }
  }
Пример #12
0
 public void storeInMemory(Value v) {
   if (offset < 0 || offset >= array.getLength() * 8)
     throw new ICodeException("Array offset " + offset + " is out of bounds");
   array.value[offset >> 3] = v;
 }
Пример #13
0
  private static void addFormFile(
      Env env,
      ArrayValue files,
      String name,
      String fileName,
      String tmpName,
      String mimeType,
      long fileLength,
      boolean addSlashesToValues,
      long maxFileSize) {
    int p = name.indexOf('[');
    String index = "";
    if (p >= 0) {
      index = name.substring(p);
      name = name.substring(0, p);
    }

    StringValue nameValue = env.createString(name);
    Value v = files.get(nameValue).toValue();
    ArrayValue entry = null;
    if (v instanceof ArrayValue) entry = (ArrayValue) v;

    if (entry == null) {
      entry = new ArrayValueImpl();
      files.put(nameValue, entry);
    }

    int error;

    // php/1667
    long uploadMaxFilesize = env.getIniBytes("upload_max_filesize", 2 * 1024 * 1024);

    if (fileName.length() == 0)
      // php/0864
      error = FileModule.UPLOAD_ERR_NO_FILE;
    else if (fileLength > uploadMaxFilesize) error = FileModule.UPLOAD_ERR_INI_SIZE;
    else if (fileLength > maxFileSize) error = FileModule.UPLOAD_ERR_FORM_SIZE;
    else error = FileModule.UPLOAD_ERR_OK;

    addFormValue(env, entry, "name" + index, env.createString(fileName), null, addSlashesToValues);

    long size;

    if (error == FileModule.UPLOAD_ERR_OK) {
      size = fileLength;
    } else {
      mimeType = "";
      tmpName = "";
      size = 0;
    }

    if (mimeType != null) {
      addFormValue(
          env, entry, "type" + index, env.createString(mimeType), null, addSlashesToValues);
    }

    addFormValue(
        env, entry, "tmp_name" + index, env.createString(tmpName), null, addSlashesToValues);

    addFormValue(env, entry, "error" + index, LongValue.create(error), null, addSlashesToValues);

    addFormValue(env, entry, "size" + index, LongValue.create(size), null, addSlashesToValues);

    addFormValue(env, files, name, entry, null, addSlashesToValues);
  }
Пример #14
0
  private static void readMultipartStream(
      Env env,
      MultipartStream ms,
      ArrayValue postArray,
      ArrayValue files,
      boolean addSlashesToValues,
      boolean isAllowUploads)
      throws IOException {
    ReadStream is;

    while ((is = ms.openRead()) != null) {
      String attr = (String) ms.getAttribute("content-disposition");

      if (attr == null || !attr.startsWith("form-data")) {
        // XXX: is this an error?
        continue;
      }

      String name = getAttribute(attr, "name", addSlashesToValues);
      String filename = getAttribute(attr, "filename", addSlashesToValues);

      if (filename != null) {
        int slashIndex = filename.lastIndexOf('/');
        int slashIndex2 = filename.lastIndexOf('\\');

        slashIndex = Math.max(slashIndex, slashIndex2);

        if (slashIndex >= 0) filename = filename.substring(slashIndex + 1);
      }

      int bracketIndex = -1;

      if (name != null) bracketIndex = name.lastIndexOf(']');

      if (bracketIndex >= 0 && bracketIndex < name.length() - 1) {
        // php/085c
      } else if (filename == null) {
        StringValue value = env.createStringBuilder();

        value.appendReadAll(is, Integer.MAX_VALUE);

        if (name != null) {
          addFormValue(env, postArray, name, value, null, addSlashesToValues);
        } else {
          env.warning(L.l("file upload is missing name and filename"));
        }
      } else {
        if (!isAllowUploads) {
          continue;
        }

        String tmpName = "";
        long tmpLength = 0;

        // A POST file upload with an empty string as the filename does not
        // create a temp file in the upload directory.

        if (filename.length() > 0) {
          Path tmpPath = env.getUploadDirectory().createTempFile("php", ".tmp");

          env.addRemovePath(tmpPath);

          WriteStream os = tmpPath.openWrite();
          try {
            os.writeStream(is);
          } finally {
            os.close();
          }

          tmpName = tmpPath.getFullPath();
          tmpLength = tmpPath.getLength();
        }

        // php/0865
        //
        // A header like "Content-Type: image/gif" indicates the mime type
        // for an uploaded file.

        String mimeType = getAttribute(attr, "mime-type", addSlashesToValues);
        if (mimeType == null) {
          mimeType = (String) ms.getAttribute("content-type");

          // php/085f
          if (mimeType != null && mimeType.endsWith(";"))
            mimeType = mimeType.substring(0, mimeType.length() - 1);
        }

        // php/0864
        //
        // mime type is empty string when no file is uploaded.

        if (filename.length() == 0) {
          mimeType = "";
        }

        long maxFileSize = Long.MAX_VALUE;

        Value maxFileSizeV = postArray.get(MAX_FILE_SIZE);
        if (!maxFileSizeV.isNull()) maxFileSize = maxFileSizeV.toLong();

        if (name != null) {
          addFormFile(
              env,
              files,
              name,
              filename,
              tmpName,
              mimeType,
              tmpLength,
              addSlashesToValues,
              maxFileSize);
        } else {
          addFormFile(
              env, files, filename, tmpName, mimeType, tmpLength, addSlashesToValues, maxFileSize);
        }
      }
    }
  }
Пример #15
0
 public static <W, R extends W> Value<W, R> parse(final Element e) {
   final String name = e.ownText();
   final String typeStr = e.attr("type");
   final Type<W, R> type = Types.get(typeStr);
   return type != null ? new Value<>(name, type) : ArrayValue.parse(name, typeStr);
 }
Пример #16
0
  public static void addFormValue(
      Env env,
      ArrayValue array,
      String key,
      Value formValue,
      String[] formValueList,
      boolean addSlashesToValues) {
    int p = -1;
    int q = -1;

    if (key != null) {
      p = key.indexOf('[');
      q = key.indexOf(']', p);
    }

    if (p >= 0 && p < q) {
      String index = key;

      Value keyValue;
      Value existingValue;

      if (p > 0) {
        key = key.substring(0, p);

        key = key.replaceAll("\\.", "_");

        keyValue = env.createString(key);
        existingValue = array.get(keyValue);

        if (existingValue == null || !existingValue.isset()) {
          existingValue = new ArrayValueImpl();
          array.put(keyValue, existingValue);
        } else if (!existingValue.isArray()) {
          // existing is overwritten
          // php/115g

          existingValue = new ArrayValueImpl();
          array.put(keyValue, existingValue);
        }

        array = (ArrayValue) existingValue;
      }

      int p1;
      while ((p1 = index.indexOf('[', q)) > 0) {
        key = index.substring(p + 1, q);

        if (key.equals("")) {
          existingValue = new ArrayValueImpl();
          array.put(existingValue);
        } else {
          keyValue = env.createString(key);
          existingValue = array.get(keyValue);

          if (existingValue == null || !existingValue.isset()) {
            existingValue = new ArrayValueImpl();
            array.put(keyValue, existingValue);
          } else if (!existingValue.isArray()) {
            existingValue = new ArrayValueImpl().put(existingValue);
            array.put(keyValue, existingValue);
          }
        }

        array = (ArrayValue) existingValue;

        p = p1;
        q = index.indexOf(']', p);
      }

      if (q > 0) index = index.substring(p + 1, q);
      else index = index.substring(p + 1);

      if (index.equals("")) {
        if (formValueList != null) {
          for (int i = 0; i < formValueList.length; i++) {
            Value value;

            if (formValueList[i] != null) value = env.createString(formValueList[i]);
            else value = NullValue.NULL;

            put(array, null, value, addSlashesToValues);
          }
        } else array.put(formValue);
      } else if ('0' <= index.charAt(0) && index.charAt(0) <= '9')
        put(array, LongValue.create(StringValue.toLong(index)), formValue, addSlashesToValues);
      else put(array, env.createString(index), formValue, addSlashesToValues);
    } else {
      if (key != null) {
        key = key.replaceAll("\\.", "_");
        put(array, env.createString(key), formValue, addSlashesToValues);
      } else {
        put(array, null, formValue, addSlashesToValues);
      }
    }
  }
Пример #17
0
 public Value loadFromMemory() {
   if (offset == -8) return new IntValue(array.getLength());
   if (offset < 0 || offset >= array.getLength() * 8)
     throw new ICodeException("Array offset " + offset + " is out of bounds");
   return array.value[offset >> 3];
 }