Exemplo n.º 1
0
bool disableWrapper(const String& scheme) {
  String lscheme = f_strtolower(scheme);

  if (lscheme.same(s_file)) {
    // Zend quietly succeeds, but does nothing
    return true;
  }

  bool ret = false;

  // Unregister request-specific wrappers entirely
  if (s_request_wrappers->m_wrappers.find(lscheme) !=
      s_request_wrappers->m_wrappers.end()) {
    s_request_wrappers->m_wrappers.erase(lscheme);
    ret = true;
  }

  // Disable builtin wrapper if it exists
  if (s_wrappers.find(lscheme.data()) == s_wrappers.end()) {
    // No builtin to disable
    return ret;
  }

  if (s_request_wrappers->m_disabled.find(lscheme) !=
      s_request_wrappers->m_disabled.end()) {
    // Already disabled
    return ret;
  }

  // Disable it
  s_request_wrappers->m_disabled.insert(lscheme);
  return true;
}
Exemplo n.º 2
0
AutoloadHandler::Result AutoloadHandler::loadFromMap(const String& name,
                                                     const String& kind,
                                                     bool toLower,
                                                     const T &checkExists) {
  assert(!m_map.isNull());
  while (true) {
    const Variant& type_map = m_map.get()->get(kind);
    auto const typeMapCell = type_map.asCell();
    if (typeMapCell->m_type != KindOfArray) return Failure;
    String canonicalName = toLower ? f_strtolower(name) : name;
    const Variant& file = typeMapCell->m_data.parr->get(canonicalName);
    bool ok = false;
    if (file.isString()) {
      String fName = file.toCStrRef().get();
      if (fName.get()->data()[0] != '/') {
        if (!m_map_root.empty()) {
          fName = m_map_root + fName;
        }
      }
      try {
        JIT::VMRegAnchor _;
        bool initial;
        auto const ec = g_context.getNoCheck();
        Unit* u = ec->evalInclude(fName.get(), nullptr, &initial);
        if (u) {
          if (initial) {
            TypedValue retval;
            ec->invokeFunc(&retval, u->getMain(), init_null_variant,
                           nullptr, nullptr, nullptr, nullptr,
                           ExecutionContext::InvokePseudoMain);
            tvRefcountedDecRef(&retval);
          }
          ok = true;
        }
      } catch (...) {}
    }
    if (ok && checkExists(name)) {
      return Success;
    }
    const Variant& func = m_map.get()->get(s_failure);
    if (func.isNull()) return Failure;
    // can throw, otherwise
    //  - true means the map was updated. try again
    //  - false means we should stop applying autoloaders (only affects classes)
    //  - anything else means keep going
    Variant action = vm_call_user_func(func, make_packed_array(kind, name));
    auto const actionCell = action.asCell();
    if (actionCell->m_type == KindOfBoolean) {
      if (actionCell->m_data.num) continue;
      return StopAutoloading;
    }
    return ContinueAutoloading;
  }
}
Exemplo n.º 3
0
Variant ArrayUtil::ChangeKeyCase(CArrRef input, bool lower) {
  Array ret = Array::Create();
  for (ArrayIter iter(input); iter; ++iter) {
    Variant key(iter.first());
    if (key.isString()) {
      if (lower) {
        ret.set(f_strtolower(key.toString()), iter.secondRef());
      } else {
        ret.set(f_strtoupper(key.toString()), iter.secondRef());
      }
    } else {
      ret.set(key, iter.secondRef());
    }
  }
  return ret;
}
Exemplo n.º 4
0
bool registerRequestWrapper(const String& scheme,
                            std::unique_ptr<Wrapper> wrapper) {
  String lscheme = f_strtolower(scheme);

  // Global, non-disabled wrapper
  if ((s_wrappers.find(lscheme.data()) != s_wrappers.end()) &&
      (s_request_wrappers->m_disabled.find(lscheme) ==
       s_request_wrappers->m_disabled.end())) {
    return false;
  }

  // A wrapper has already been registered for that scheme
  if (s_request_wrappers->m_wrappers.find(lscheme) !=
      s_request_wrappers->m_wrappers.end()) {
    return false;
  }

  s_request_wrappers->m_wrappers[lscheme] = std::move(wrapper);
  return true;
}
Exemplo n.º 5
0
void f_spl_autoload(const String& class_name,
                    const String& file_extensions /* = null_string */) {
    Array ext = file_extensions.isNull()
                ? s_extension_list->extensions
                : StringUtil::Explode(file_extensions, ",").toArray();
    String lClass = f_strtolower(class_name);
    bool found = false;
    for (ArrayIter iter(ext); iter; ++iter) {
        String fileName = lClass + iter.second().toString();
        include(fileName, true, "", false);
        if (f_class_exists(class_name, false)) {
            found = true;
            break;
        }
    }

    if (!found && !AutoloadHandler::s_instance->isRunning()) {
        throw_spl_exception("Class %s could not be loaded", class_name.c_str());
    }
}
Exemplo n.º 6
0
bool restoreWrapper(const String& scheme) {
  String lscheme = f_strtolower(scheme);
  bool ret = false;

  // Unregister request-specific wrapper
  if (s_request_wrappers->m_wrappers.find(lscheme) !=
    s_request_wrappers->m_wrappers.end()) {
    s_request_wrappers->m_wrappers.erase(lscheme);
    ret = true;
  }

  // Un-disable builtin wrapper
  if (s_request_wrappers->m_disabled.find(lscheme) ==
      s_request_wrappers->m_disabled.end()) {
    // Not disabled
    return ret;
  }

  // Perform action un-disable
  s_request_wrappers->m_disabled.erase(lscheme);
  return true;
}
Exemplo n.º 7
0
Wrapper* getWrapper(const String& scheme) {
  String lscheme = f_strtolower(scheme);

  // Request local wrapper?
  {
    auto it = s_request_wrappers->m_wrappers.find(lscheme);
    if (it != s_request_wrappers->m_wrappers.end()) {
      return it->second.get();
    }
  }

  // Global, non-disabled wrapper?
  {
    auto it = s_wrappers.find(lscheme.data());
    if ((it != s_wrappers.end()) &&
        (s_request_wrappers->m_disabled.find(lscheme) ==
         s_request_wrappers->m_disabled.end())) {
      return it->second;
    }
  }

  return nullptr;
}
Exemplo n.º 8
0
Variant invokeImpl(void *extra, CArrRef params) {
  const char *function = (const char*)extra;
  // for TestExtFunction
  if (strcasecmp(function, "test") == 0) {
    return params[0];
  }

  // for TestExtPreg::test_preg_replace_callback
  if (strcasecmp(function, "next_year") == 0) {
    Array matches = params[0].toArray();
    return matches[1].toString() + String(matches[2].toInt32() + 1);
  }

  // for TestExtArray::test_array_filter
  if (strcasecmp(function, "odd") == 0) {
    return params[0].toInt32() & 1;
  }
  if (strcasecmp(function, "even") == 0) {
    return !(params[0].toInt32() & 1);
  }

  // for TestExtArray::test_array_map
  if (strcasecmp(function, "cube") == 0) {
    int n = params[0].toInt32();
    return n * n * n;
  }

  // for TestExtArray::test_array_multisort
  if (strcasecmp(function, "strtolower") == 0) {
    return f_strtolower(params[0]);
  }

  // for TestExtArray::test_array_reduce
  if (strcasecmp(function, "rsum") == 0) {
    int v = params[0].toInt32();
    int w = params[1].toInt32();
    v += w;
    return v;
  }
  if (strcasecmp(function, "rmul") == 0) {
    int v = params[0].toInt32();
    int w = params[1].toInt32();
    v *= w;
    return v;
  }

  // for TestExtArray::test_array_walk_recursive
  if (strcasecmp(function, "test_print") == 0) {
    String item = params[0].toString();
    String key = params[1].toString();
    echo(key + ": " + item + "\n");
  }

  // for TestExtArray::test_array_walk
  if (strcasecmp(function, "test_alter") == 0) {
    Variant &item1 = lval(((Array&)params).lvalAt(0));
    String key = params[1];
    String prefix = params[2];
    item1 = prefix + ": " + item1;
  }

  // for TestExtArray::test_array_udiff
  if (strcasecmp(function, "comp_func") == 0) {
    int n1 = params[0].toInt32();
    int n2 = params[1].toInt32();
    if (n1 == n2) return 0;
    return n1 > n2 ? 1 : -1;
  }

  // for TestExtArray::test_usort
  if (strcasecmp(function, "reverse_comp_func") == 0) {
    int n1 = params[0].toInt32();
    int n2 = params[1].toInt32();
    if (n1 == n2) return 0;
    return n1 > n2 ? -1 : 1;
  }

  // for TestExtArray::test_array_uintersect
  if (strcasecmp(function, "strcasecmp") == 0) {
    String s1 = params[0].toString();
    String s2 = params[1].toString();
    return strcasecmp(s1.data(), s2.data());
  }

  // for TestExtArray::test_uasort
  if (strcasecmp(function, "reverse_strcasecmp") == 0) {
    String s1 = params[0].toString();
    String s2 = params[1].toString();
    return strcasecmp(s2.data(), s1.data());
  }

  // for TestExtFbml
  if (strcasecmp(function, "urltr") == 0) {
    String s1 = params[0].toString();
    String s2 = params[1].toString();
    return String("url:") + s1 + "=" + s2;
  }

  // for TestExtCurl::test_curl_exec
  if (strcasecmp(function, "curl_write_func") == 0) {
    print("curl_write_func called with ");
    print(params[1]);
    return params[1].toString().size();
  }

  // for TestExtPreg::test_preg_replace
  if (strcasecmp(function, "strtoupper") == 0) {
    return f_strtoupper(params[0].toString());
  }
  if (strcasecmp(function, "test_preg_rep") == 0) {
    return test_preg_rep(params[0].toString(), params[1].toString(),
                       params[2].toString());
  }
  if (strcasecmp(function, "sprintf") == 0) {
    return f_sprintf(params.size(), params[0],
                     params.slice(1, params.size() - 1, false));
  }

  // for TestExtSqlite3::test_sqlite3
  if (strcasecmp(function, "lower") == 0) {
    return f_strtolower(params[0]);
  }
  if (strcasecmp(function, "sumlen_step") == 0) {
    return params[0].toInt64() + f_strlen(params[2]);
  }
  if (strcasecmp(function, "sumlen_fini") == 0) {
    return params[0].toInt64();
  }

  // for TestExtSoap
  if (strcasecmp(function, "hello") == 0) {
    return "Hello World";
  }
  if (strcasecmp(function, "add") == 0) {
    return params[0].toInt32() + params[1].toInt32();
  }
  if (strcasecmp(function, "sub") == 0) {
    return params[0].toInt32() - params[1].toInt32();
  }
  if (strcasecmp(function, "sum") == 0) {
    int sum = 0;
    for (ArrayIter iter(params[0]); iter; ++iter) {
      sum += iter.second().toInt32();
    }
    return sum;
  }
  if (strcasecmp(function, "strlen") == 0) {
    return f_strlen(params[0]);
  }
  if (strcasecmp(function, "fault") == 0) {
    return Object((NEW(c_SoapFault)())->create("MyFault","My fault string"));
  }

  // for TestExtServer
  if (strcasecmp(function, "xbox_process_message") == 0) {
    return StringUtil::Reverse(params[0]);
  }

  return true;
}
Exemplo n.º 9
0
bool TestExtString::test_strtolower() {
  VS(f_strtolower("ABC"), "abc");
  return Count(true);
}