static void setPassword(HttpConn *conn) { char str[16] = {0}; cchar *role = getSessionVar("role"); if(role == NULL){ rendersts(str, 9); render(str); return; } if((strcmp(role, "root") !=0) && (strcmp(role, "admin") !=0)){ rendersts(str, 5);//无权限 render(str); return; } MprJson *jsonparam = mprParseJson(espGetQueryString(conn)); cchar *oldpassword = mprGetJson(jsonparam, "oldpassword"); cchar *newpassword = mprGetJson(jsonparam, "newpassword"); cchar *username = getSessionVar("userName"); //Edi *db = ediOpen("db/muxnms.mdb", "mdb", EDI_AUTO_SAVE ); EdiRec *user = readRecWhere("user", "username", "==", username); MprJson *userjson = mprParseJson(ediRecAsJson(user, 0)); //printf("=======password====%s========%s\n", oldpassword, mprGetJson(userjson, "password")); if(strcmp(oldpassword, mprGetJson(userjson, "password")) == 0){ ediSetField(user, "password", newpassword); updateRec(user); rendersts(str, 1); }else{ rendersts(str, 0); } //ediClose(db); render(str); //add optlog Edi *db = ediOpen("db/muxnms.mdb", "mdb", EDI_AUTO_SAVE); EdiRec *optlog = ediCreateRec(db, "optlog"); if(optlog == NULL){ printf("================>>>optlog is NULL!!\n"); return; } time_t curTime; time(&curTime); memset(optstr, 0, 256); sprintf(optstr, "{'user': '******', 'desc': '用户修改了密码.', 'level': '2', 'logtime':'%d'}", getSessionVar("userName"), curTime); MprJson *row = mprParseJson(optstr); if(ediSetFields(optlog, row) == 0){ printf("================>>>ediSetFields Failed!!\n"); } ediUpdateRec(db, optlog); //ediClose(db); }
static void getoptlogs() { char str[16] = {0}; int len = 0; cchar *role = getSessionVar("role"); if(role == NULL){ rendersts(str, 9); render(str); return; } if((strcmp(role, "root") !=0) && (strcmp(role, "admin") !=0)){ rendersts(str, 5);//无权限 render(str); return; } //find optlog Edi *db = ediOpen("db/muxnms.mdb", "mdb", EDI_AUTO_SAVE); // //删除7天前的日志 time_t curTime; time(&curTime); //EdiGrid *oldlogs = ediReadWhere(db, "optlog", "logtime", "<", curTime - 7*24*3600); EdiGrid *oldlogs = ediReadTable(db, "optlog"); //printf("========logs========>>>%s\n", ediGridAsJson(oldlogs, MPR_JSON_PRETTY)); for(len=0; len < oldlogs->nrecords; len++){ EdiRec *log = oldlogs->records[len]; if(atoi(mprGetJson(mprParseJson(ediRecAsJson(log, MPR_JSON_PRETTY)), "logtime")) < (curTime - 7*24*3600)){ ediRemoveRec(db, "optlog", log->id); } } EdiGrid *logs = ediReadTable(db, "optlog"); render(ediGridAsJson(logs, MPR_JSON_PRETTY)); }
static void copyMappings(HttpRoute *route, MprJson *dest, MprJson *obj) { MprJson *child, *job, *jvalue; cchar *key, *value; int ji; for (ITERATE_CONFIG(route, obj, child, ji)) { if (child->type & MPR_JSON_OBJ) { job = mprCreateJson(MPR_JSON_OBJ); copyMappings(route, job, child); mprSetJsonObj(dest, child->name, job); } else { key = child->value; if (sends(key, "|time")) { key = ssplit(sclone(key), " \t|", NULL); if ((value = mprGetJson(route->config, key)) != 0) { mprSetJson(dest, child->name, itos(httpGetTicks(value)), MPR_JSON_NUMBER); } } else { if ((jvalue = mprGetJsonObj(route->config, key)) != 0) { mprSetJsonObj(dest, child->name, mprCloneJson(jvalue)); } } } } }
PUBLIC bool espTestConfig(HttpRoute *route, cchar *key, cchar *desired) { cchar *value; if ((value = mprGetJson(route->config, key)) != 0) { return smatch(value, desired); } return 0; }
PUBLIC cchar *getConfig(cchar *field) { HttpRoute *route; cchar *value; route = getStream()->rx->route; if ((value = mprGetJson(route->config, field)) == 0) { return ""; } return value; }
PUBLIC cchar *espGetConfig(HttpRoute *route, cchar *key, cchar *defaultValue) { cchar *value; if (sstarts(key, "app.")) { mprLog("warn esp", 0, "Using legacy \"app\" configuration property"); } if ((value = mprGetJson(route->config, key)) != 0) { return value; } return defaultValue; }
/* Expand ${token} references in a path or string. */ static char *stemplateInner(cchar *str, void *keys, int json) { MprBuf *buf; cchar *value; char *src, *result, *cp, *tok; if (str) { if (schr(str, '$') == 0) { return sclone(str); } buf = mprCreateBuf(0, 0); for (src = (char*) str; *src; ) { if (*src == '$') { if (*++src == '{') { for (cp = ++src; *cp && *cp != '}'; cp++) ; tok = snclone(src, cp - src); } else { for (cp = src; *cp && (isalnum((uchar) *cp) || *cp == '_'); cp++) ; tok = snclone(src, cp - src); } if (json) { value = mprGetJson(keys, tok); } else { value = mprLookupKey(keys, tok); } if (value != 0) { mprPutStringToBuf(buf, value); if (src > str && src[-1] == '{') { src = cp + 1; } else { src = cp; } } else { mprPutCharToBuf(buf, '$'); if (src > str && src[-1] == '{') { mprPutCharToBuf(buf, '{'); } mprPutCharToBuf(buf, *src++); } } else { mprPutCharToBuf(buf, *src++); } } mprAddNullToBuf(buf); result = sclone(mprGetBufStart(buf)); } else { result = MPR->emptyString; } return result; }
/* Load the config.json */ PUBLIC int espLoadConfig(HttpRoute *route) { EspRoute *eroute; MprJson *msettings; MprPath cinfo; cchar *cpath, *value; eroute = route->eroute; cpath = mprJoinPath(route->documents, "config.json"); if (mprGetPathInfo(cpath, &cinfo) == 0) { if (eroute->config && cinfo.mtime > eroute->configLoaded) { eroute->config = 0; } eroute->configLoaded = cinfo.mtime; } if (!eroute->config) { if ((eroute->config = mprLoadJson(cpath)) != 0) { /* Blend the mode properties into settings */ eroute->mode = mprGetJsonValue(eroute->config, "mode", 0); if ((msettings = mprGetJson(eroute->config, sfmt("modes.%s", eroute->mode), 0)) != 0) { mprBlendJson(mprLookupJson(eroute->config, "settings"), msettings, 0); } if ((value = espGetConfig(route, "settings.showErrors", 0)) != 0) { httpSetRouteShowErrors(route, smatch(value, "true")); } if ((value = espGetConfig(route, "settings.update", 0)) != 0) { eroute->update = smatch(value, "true"); } if ((value = espGetConfig(route, "settings.keepSource", 0)) != 0) { eroute->keepSource = smatch(value, "true"); } if ((eroute->serverPrefix = espGetConfig(route, "settings.serverPrefix", 0)) == 0) { eroute->serverPrefix = sclone(BIT_ESP_SERVER_PREFIX); } if ((value = espGetConfig(route, "settings.login.name", 0)) != 0) { /* Automatic login as this user. Password not required */ httpSetAuthUsername(route->auth, value); } if ((value = espGetConfig(route, "settings.xsrfToken", 0)) != 0) { httpSetRouteXsrf(route, smatch(value, "true")); } if ((value = espGetConfig(route, "settings.sendJson", 0)) != 0) { eroute->json = smatch(value, "true"); } if (espTestConfig(route, "settings.map", "compressed")) { httpAddRouteMapping(route, "js,css,less", "min.${1}.gz, min.${1}, ${1}.gz"); httpAddRouteMapping(route, "html,xml", "${1}.gz"); } if (!eroute->database) { if ((eroute->database = espGetConfig(route, "server.database", 0)) != 0) { if (espOpenDatabase(route, eroute->database) < 0) { mprError("Cannot open database %s", eroute->database); return MPR_ERR_CANT_OPEN; } } } eroute->json = espTestConfig(route, "settings.json", "1"); } else { eroute->config = mprCreateJson(MPR_JSON_OBJ); espAddComponent(route, "legacy-mvc"); } if (espHasComponent(route, "legacy-mvc")) { eroute->legacy = 1; } } return 0; }