Exemplo n.º 1
0
Variant f_bzopen(CVarRef filename, CStrRef mode) {
  if (mode != "r" && mode != "w") {
    raise_warning(
      "'%s' is not a valid mode for bzopen(). "
      "Only 'w' and 'r' are supported.",
      mode.data()
    );
    return false;
  }

  BZ2File *bz;
  if (filename.isString()) {
    if (filename.asCStrRef().empty()) {
      raise_warning("filename cannot be empty");
      return false;
    }
    bz = NEWOBJ(BZ2File)();
    bool ret = bz->open(File::TranslatePath(filename.toString()), mode);
    if (!ret) {
      raise_warning("%s", Util::safe_strerror(errno).c_str());
      return false;
    }
  } else {
    if (!filename.isResource()) {
      raise_warning("first parameter has to be string or file-resource");
      return false;
    }
    PlainFile* f = filename.cast<PlainFile>();
    if (!f) {
      return false;
    }

    std::string stream_mode = f->getMode();
    int stream_mode_len = stream_mode.length();

    if (stream_mode_len != 1 &&
        !(stream_mode_len == 2 && stream_mode.find('b') != string::npos)) {
      raise_warning("cannot use stream opened in mode '%s'", stream_mode.c_str());
      return false;
    } else if (stream_mode_len == 1 &&
        stream_mode[0] != 'r' && stream_mode[0] != 'w' &&
        stream_mode[0] != 'a' && stream_mode[0] != 'x') {
      raise_warning("cannot use stream opened in mode '%s'", stream_mode.c_str());
      return false;
    }

    const char rw_mode = stream_mode[0];
    if (mode == "r" && rw_mode != 'r') {
      raise_warning("cannot write to a stream opened in read only mode");
      return false;
    } else if (mode == "w" && rw_mode != 'w' && rw_mode != 'a' && rw_mode != 'x') {
      raise_warning("cannot read from a stream opened in write only mode");
      return false;
    }

    bz = NEWOBJ(BZ2File)(f);
  }
  Object handle(bz);
  return handle;
}
string& SourceRootInfo::initPhpRoot() {
  GlobalVariables *g = get_global_variables();
  CVarRef server = g->get(s_SERVER);
  CVarRef v = server.rvalAt(s_PHP_ROOT);
  if (v.isString()) {
    *s_phproot.getCheck() = string(v.asCStrRef().data()) + string("/");
  } else {
    // Our best guess at the source root.
    *s_phproot.getCheck() = GetCurrentSourceRoot();
  }
  return *s_phproot.getCheck();
}
Exemplo n.º 3
0
bool f_stream_is_local(CVarRef stream_or_url) {
  if (stream_or_url.isString()) {
    auto wrapper = Stream::getWrapperFromURI(stream_or_url.asCStrRef());
    return wrapper->m_isLocal;

  } else if (stream_or_url.isResource()) {
    File* file = dynamic_cast<File*>(stream_or_url.asCResRef().get());
    if (!file) {
      raise_warning("supplied resource is not a valid stream resource");
      return false;
    }
    return file->m_isLocal;
  }
  // Zend returns true for random data types...
  return true;
}
Exemplo n.º 4
0
Variant Array::rvalAt(CVarRef key, bool error /* = false */,
                      bool isKey /* = false */) const {
  if (!m_px) return null_variant;
  switch (key.m_type) {
  case KindOfNull:
    return m_px->get(empty_string, error);
  case KindOfBoolean:
  case KindOfByte:
  case KindOfInt16:
  case KindOfInt32:
  case KindOfInt64:
    return m_px->get(key.m_data.num, error);
  case KindOfDouble:
    return m_px->get((int64)key.m_data.dbl, error);
  case KindOfStaticString:
  case KindOfString: {
    int64 n;
    if (!isKey && key.m_data.pstr->isStrictlyInteger(n)) {
      return m_px->get(n, error);
    } else {
      return m_px->get(key.asCStrRef(), error);
    }
  }
  case KindOfArray:
    throw_bad_type_exception("Invalid type used as key");
    break;
  case KindOfObject:
    if (key.isResource()) {
      return m_px->get(key.toInt64(), error);
    }
    throw_bad_type_exception("Invalid type used as key");
    break;
  case KindOfVariant:
    return rvalAt(*(key.m_data.pvar), error, isKey);
  default:
    ASSERT(false);
    break;
  }
  return null_variant;
}
Exemplo n.º 5
0
CVarRef Array::rvalAtRef(CVarRef key, ACCESSPARAMS_IMPL) const {
  if (!m_px) return null_variant;
  switch (key.m_type) {
  case KindOfUninit:
  case KindOfNull:
    return m_px->get(empty_string, flags & AccessFlags::Error);
  case KindOfBoolean:
  case KindOfInt32:
  case KindOfInt64:
    return m_px->get(key.m_data.num, flags & AccessFlags::Error);
  case KindOfDouble:
    return m_px->get((int64)key.m_data.dbl, flags & AccessFlags::Error);
  case KindOfStaticString:
  case KindOfString: {
    int64 n;
    if (!(flags & AccessFlags::Key) &&
        key.m_data.pstr->isStrictlyInteger(n)) {
      return m_px->get(n, flags & AccessFlags::Error);
    } else {
      return m_px->get(key.asCStrRef(), flags & AccessFlags::Error);
    }
  }
  case KindOfArray:
    throw_bad_type_exception("Invalid type used as key");
    break;
  case KindOfObject:
    if (key.isResource()) {
      return m_px->get(key.toInt64(), flags & AccessFlags::Error);
    }
    throw_bad_type_exception("Invalid type used as key");
    break;
  case KindOfVariant:
    return rvalAtRef(*(key.m_data.pvar), flags);
  default:
    ASSERT(false);
    break;
  }
  return null_variant;
}
Exemplo n.º 6
0
void Globals::initialize() {
  StaticInits *s = s_next_inits;
  while (s) {
    const ClassPropTable *cpt = s->table;
    const int *e = s->entries;
    int n = *e++;
    while (n--) {
      const ClassPropTableEntry *p = cpt->m_entries + *e++;
      char *addr = (char*)this + p->offset;
      if (LIKELY(p->isFastInit())) {
        CVarRef v = cpt->getInitV(p->init_offset);
        switch (p->type) {
          case KindOfBoolean:
            *(bool*)addr = v.asBooleanVal();
            break;
          case KindOfInt64:
            *(int64*)addr = v.asInt64Val();
            break;
          case KindOfDouble:
            *(double*)addr = v.asDoubleVal();
            break;
          case KindOfString:
            *(String*)addr = v.asCStrRef();
            break;
          case KindOfArray:
            *(Array*)addr = v.asCArrRef();
            break;
          case KindOfVariant:
            *(Variant*)addr = v;
            break;
          default:
            ASSERT(false);
        }
        continue;
      }
      CVarRef v = cpt->getInitVal(p);
      if (LIKELY(p->type == KindOfVariant)) {
        *(Variant*)addr = v;
      } else {
        switch (p->type) {
          case KindOfBoolean:
            *(bool*)addr = v;
            break;
          case KindOfInt64:
            *(int64*)addr = v;
            break;
          case KindOfDouble:
            *(double*)addr = v;
            break;
          case KindOfString:
            *(String*)addr = v;
            break;
          case KindOfArray:
            *(Array*)addr = v;
            break;
          default:
            ASSERT(false);
        }
      }
    }
    s = s->next;
  }
}