static String normalize_variable_name(CStrRef name) { StringBuffer sb; for (int i = 0; i < name.size(); i++) { char ch = name.charAt(i); if ((i > 0 && ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch == '_')) { sb.append(ch); } else if (ch >= 'A' && ch <= 'Z') { sb.append((char)(ch + 'a' - 'A')); } else { sb.append('_'); } } return sb.detach(); }
String f_sql_regcase(CStrRef str) { unsigned char c; register int i, j; char *tmp = (char*)malloc(str.size() * 4 + 1); for (i = j = 0; i < str.size(); i++) { c = (unsigned char)str.charAt(i); if (isalpha(c)) { tmp[j++] = '['; tmp[j++] = toupper(c); tmp[j++] = tolower(c); tmp[j++] = ']'; } else { tmp[j++] = c; } } tmp[j] = 0; return String(tmp, j, AttachString); }
bool IniSetting::Set(CStrRef name, CStrRef value) { CallbackMap::iterator iter = s_callbacks->find(name.data()); if (iter != s_callbacks->end()) { return (*iter->second.callback)(value, iter->second.p); } if (name == "memory_limit") { if (!value.empty()) { int64 newInt = value.toInt64(); char lastChar = value.charAt(value.size() - 1); if (lastChar == 'K' || lastChar == 'k') { newInt <<= 10; } else if (lastChar == 'M' || lastChar == 'm') { newInt <<= 20; } else if (lastChar == 'G' || lastChar == 'g') { newInt <<= 30; } g_context->setRequestMemoryMaxBytes(newInt); return true; } } else if (name == "max_execution_time" || name == "maximum_execution_time"){ int64 limit = value.toInt64(); TimeoutThread::DeferTimeout(limit); // Just for ini_get g_context->setRequestTimeLimit(limit); return true; } else if (name == "arg_separator.output") { g_context->setArgSeparatorOutput(value); return true; } else if (name == "log_errors") { bool log; ini_on_update_bool(value, &log); g_context->setLogErrors(log); return true; } else if (name == "error_log") { g_context->setErrorLog(value); return true; } return false; }
bool RequestURI::virtualFileExists(const VirtualHost *vhost, const string &sourceRoot, const string &pathTranslation, CStrRef filename) { if (filename.empty() || filename.charAt(filename.length() - 1) == '/') { return false; } String canon(Util::canonicalize(filename.c_str(), filename.size()), AttachString); if (!vhost->getDocumentRoot().empty()) { string fullname = canon.data(); int i = 0; while (i < fullname.size() && fullname[i] == '/') ++i; if (i) { fullname = fullname.substr(i); } if (!i || !m_rewritten) { fullname = pathTranslation + fullname; } m_path = fullname; m_absolutePath = String(sourceRoot) + m_path; processExt(); if (StaticContentCache::TheFileCache && !fullname.empty() && StaticContentCache::TheFileCache->fileExists(fullname.c_str())) { return true; } struct stat st; return RuntimeOption::AllowedFiles.find(fullname.c_str()) != RuntimeOption::AllowedFiles.end() || (stat(m_absolutePath.c_str(), &st) == 0 && (st.st_mode & S_IFMT) == S_IFREG); } m_path = canon; m_absolutePath = String(sourceRoot) + canon; processExt(); return true; }
String DateInterval::format(CStrRef format_spec) { StringBuffer s; for(int i = 0; i < format_spec.length(); i++) { const int MAXLEN = 22; // 64bit signed int string length, plus terminating \0 char buf[MAXLEN]; int l; char c = format_spec.charAt(i); if (c != '%') { s.append(c); continue; } i++; if (i == format_spec.length()) { // End of format, use literal % and finish s.append(c); break; } c = format_spec.charAt(i); switch(c) { case 'Y': l = snprintf(buf, MAXLEN, "%02lld", getYears()); break; case 'y': l = snprintf(buf, MAXLEN, "%lld", getYears()); break; case 'M': l = snprintf(buf, MAXLEN, "%02lld", getMonths()); break; case 'm': l = snprintf(buf, MAXLEN, "%lld", getMonths()); break; case 'D': l = snprintf(buf, MAXLEN, "%02lld", getDays()); break; case 'd': l = snprintf(buf, MAXLEN, "%lld", getDays()); break; case 'H': l = snprintf(buf, MAXLEN, "%02lld", getHours()); break; case 'h': l = snprintf(buf, MAXLEN, "%lld", getHours()); break; case 'I': l = snprintf(buf, MAXLEN, "%02lld", getMinutes()); break; case 'i': l = snprintf(buf, MAXLEN, "%lld", getMinutes()); break; case 'S': l = snprintf(buf, MAXLEN, "%02lld", getSeconds()); break; case 's': l = snprintf(buf, MAXLEN, "%lld", getSeconds()); break; case 'a': if (haveTotalDays()) { l = snprintf(buf, MAXLEN, "%lld", getTotalDays()); } else { l = snprintf(buf, MAXLEN, "(unknown)"); } break; case 'R': l = snprintf(buf, MAXLEN, "%c", isInverted() ? '-' : '+'); break; case 'r': l = snprintf(buf, MAXLEN, "%s", isInverted() ? "-" : ""); break; case '%': default: l = 0; s.append('%'); break; } if (l > 0) { s.append(buf, l); } } return s.detach(); }
String File::readRecord(CStrRef delimiter, int64 maxlen /* = 0 */) { if (eof() && m_writepos == m_readpos) { return empty_string; } if (maxlen <= 0 || maxlen > CHUNK_SIZE) { maxlen = CHUNK_SIZE; } int64 avail = m_writepos - m_readpos; if (m_buffer == NULL) { m_buffer = (char *)malloc(CHUNK_SIZE * 3); } if (avail < maxlen && !eof()) { assert(m_writepos + maxlen - avail <= CHUNK_SIZE * 3); m_writepos += readImpl(m_buffer + m_writepos, maxlen - avail); maxlen = m_writepos - m_readpos; } if (m_readpos >= CHUNK_SIZE) { memcpy(m_buffer, m_buffer + m_readpos, m_writepos - m_readpos); m_writepos -= m_readpos; m_readpos = 0; } int64 toread; const char *e; bool skip = false; if (delimiter.empty()) { toread = maxlen; } else { if (delimiter.size() == 1) { e = (const char *)memchr(m_buffer + m_readpos, delimiter.charAt(0), m_writepos - m_readpos); } else { int64 pos = string_find(m_buffer + m_readpos, m_writepos - m_readpos, delimiter.data(), delimiter.size(), 0, true); if (pos >= 0) { e = m_buffer + m_readpos + pos; } else { e = NULL; } } if (!e) { toread = maxlen; } else { toread = e - m_buffer - m_readpos; skip = true; } } if (toread > maxlen && maxlen > 0) { toread = maxlen; } if (toread >= 0) { String s = String(toread, ReserveString); char *buf = s.mutableSlice().ptr; if (toread) { memcpy(buf, m_buffer + m_readpos, toread); } m_readpos += toread; if (skip) { m_readpos += delimiter.size(); m_position += delimiter.size(); } return s.setSize(toread); } return empty_string; }
bool f_hphp_log(CStrRef filename, CStrRef message) { if (!RuntimeOption::EnableApplicationLog) { return false; } FILE *f = NULL; { ReadLock lock(s_loggers_mutex); if (Logger::UseCronolog) { CronLoggerMap::iterator iter = s_cronLoggers.find(filename.data()); if (iter != s_cronLoggers.end()) { f = iter->second.getOutputFile(); } } else { LoggerMap::const_iterator iter = s_loggers.find(filename.data()); if (iter != s_loggers.end()) { f = iter->second; } } } if (f == NULL) { WriteLock lock(s_loggers_mutex); if (Logger::UseCronolog) { CronLoggerMap::iterator iter = s_cronLoggers.find(filename.data()); if (iter != s_cronLoggers.end()) { f = iter->second.getOutputFile(); } else { Cronolog cl; if (strchr(filename.c_str(), '%')) { cl.m_template = filename; } else { cl.m_file = fopen(filename.data(), "a"); } s_cronLoggers[filename.data()] = cl; f = cl.getOutputFile(); if (f == NULL) { return false; } } } else { LoggerMap::const_iterator iter = s_loggers.find(filename.data()); if (iter != s_loggers.end()) { f = iter->second; } else { if (filename.charAt(0) == '|') { f = popen(filename.data() + 1, "w"); } else { f = fopen(filename.data(), "a"); } if (f == NULL) { return false; } s_loggers[filename.data()] = f; } } } bool ret = (fwrite(message.data(), message.size(), 1, f) == 1); if (ret) { fflush(f); } return ret; }