1 #include "otsdaq-utilities/Logbook/LogbookSupervisor.h"
20 const std::string LOGBOOK_PATH = __ENV__(
"LOGBOOK_DATA_PATH") + std::string(
"/");
21 #define LOGBOOK_CATEGORY_LIST_PATH LOGBOOK_PATH + "category_list.xml"
22 #define LOGBOOK_CATEGORY_DIR_PREFACE "log_"
23 #define LOGBOOK_UPLOADS_PATH "uploads/"
24 #define LOGBOOK_LOGBOOKS_PATH "logbooks/"
25 #define LOGBOOK_PREVIEWS_PATH "previews/"
26 #define LOGBOOK_FILE_PREFACE "entries_"
27 #define LOGBOOK_FILE_EXTENSION ".xml"
29 #define ACTIVE_CATEGORY_PATH LOGBOOK_PATH + "active_category.txt"
30 #define REMOVE_CATEGORY_LOG_PATH LOGBOOK_PATH + "removed_categories.log"
32 #define XML_ADMIN_STATUS "logbook_admin_status"
33 #define XML_STATUS "logbook_status"
34 #define XML_MOST_RECENT_DAY "most_recent_day"
35 #define XML_CATEGORY_ROOT "categories"
36 #define XML_CATEGORY "category"
37 #define XML_ACTIVE_CATEGORY "active_category"
38 #define XML_CATEGORY_CREATE "create_time"
39 #define XML_CATEGORY_CREATOR "creator"
41 #define XML_LOGBOOK_ENTRY "logbook_entry"
42 #define XML_LOGBOOK_ENTRY_SUBJECT "logbook_entry_subject"
43 #define XML_LOGBOOK_ENTRY_TEXT "logbook_entry_text"
44 #define XML_LOGBOOK_ENTRY_FILE "logbook_entry_file"
45 #define XML_LOGBOOK_ENTRY_TIME "logbook_entry_time"
46 #define XML_LOGBOOK_ENTRY_CREATOR "logbook_entry_creator"
47 #define XML_LOGBOOK_ENTRY_HIDDEN "logbook_entry_hidden"
48 #define XML_LOGBOOK_ENTRY_HIDER "logbook_entry_hider"
49 #define XML_LOGBOOK_ENTRY_HIDDEN_TIME "logbook_entry_hidden_time"
51 #define XML_PREVIEW_INDEX "preview_index"
52 #define LOGBOOK_PREVIEW_FILE "preview.xml"
53 #define LOGBOOK_PREVIEW_UPLOAD_PREFACE "upload_"
58 #define __MF_SUBJECT__ "Logbook"
63 int sendmail(
const char* to,
const char* from,
const char* subject,
const char* message)
66 FILE* mailpipe = popen(
"/usr/lib/sendmail -t",
"w");
69 fprintf(mailpipe,
"To: %s\n", to);
70 fprintf(mailpipe,
"From: %s\n", from);
71 fprintf(mailpipe,
"Subject: %s\n\n", subject);
72 fwrite(message, 1, strlen(message), mailpipe);
73 fwrite(
".\n", 1, 2, mailpipe);
79 perror(
"Failed to invoke sendmail");
85 LogbookSupervisor::LogbookSupervisor(xdaq::ApplicationStub* stub)
87 , allowedFileUploadTypes_({
"image/png",
94 , matchingFileUploadTypes_(
95 {
"png",
"jpeg",
"gif",
"bmp",
"pdf",
"zip",
"txt"})
106 this, &LogbookSupervisor::MakeSystemLogEntry,
"MakeSystemLogEntry", XDAQ_NS_URI);
115 LogbookSupervisor::~LogbookSupervisor(
void) { destroy(); }
118 void LogbookSupervisor::init(
void)
125 std::string path = LOGBOOK_PATH;
126 DIR* dir = opendir(path.c_str());
129 else if(-1 == mkdir(path.c_str(), 0755))
132 std::stringstream ss;
133 ss << __COUT_HDR_FL__ <<
"Service directory creation failed: " << path
138 path = LOGBOOK_PATH + LOGBOOK_UPLOADS_PATH;
139 dir = opendir(path.c_str());
142 else if(-1 == mkdir((path).c_str(), 0755))
145 __SS__ <<
"Service directory creation failed: " << path << std::endl;
149 path = LOGBOOK_PATH + LOGBOOK_LOGBOOKS_PATH;
150 dir = opendir(path.c_str());
153 else if(-1 == mkdir(path.c_str(), 0755))
156 __SS__ <<
"Service directory creation failed: " << path << std::endl;
162 __COUT__ <<
"Active Category is " << activeCategory_ << std::endl;
163 mostRecentDayIndex_ = 0;
167 void LogbookSupervisor::destroy(
void)
173 void LogbookSupervisor::defaultPage(xgi::Input* , xgi::Output* out)
175 __COUT__ <<
" active category " << activeCategory_ << std::endl;
176 *out <<
"<!DOCTYPE HTML><html lang='en'><frameset col='100%' row='100%'><frame "
177 "src='/WebPath/html/Logbook.html?urn="
178 << this->getApplicationDescriptor()->getLocalId()
179 <<
"&active_category=" << activeCategory_ <<
"'></frameset></html>";
188 CorePropertySupervisorBase::setSupervisorProperty(
189 CorePropertySupervisorBase::SUPERVISOR_PROPERTIES.UserPermissionsThreshold,
191 "*=1 | CreateCategory=-1 | RemoveCategory=-1 | GetCategoryListAdmin=-1 "
192 "| SetActiveCategory=-1" +
193 " | AdminRemoveRestoreEntry=-1");
201 CorePropertySupervisorBase::setSupervisorProperty(
202 CorePropertySupervisorBase::SUPERVISOR_PROPERTIES.AutomatedRequestTypes,
204 CorePropertySupervisorBase::setSupervisorProperty(
205 CorePropertySupervisorBase::SUPERVISOR_PROPERTIES.NonXMLRequestTypes,
206 "LogImage | LogReport");
207 CorePropertySupervisorBase::setSupervisorProperty(
208 CorePropertySupervisorBase::SUPERVISOR_PROPERTIES.RequireUserLockRequestTypes,
209 "CreateCategory | RemoveCategory | PreviewEntry | AdminRemoveRestoreEntry");
219 const WebUsers::RequestUserInfo& userInfo)
221 __COUTTV__(requestType);
246 if(requestType ==
"CreateCategory")
264 std::string creator = userInfo.username_;
268 __COUT__ <<
"Created" << std::endl;
270 else if(requestType ==
"RemoveCategory")
282 std::string remover = userInfo.username_;
285 else if(requestType ==
"GetCategoryList")
289 if(userInfo.permissionLevel_ >=
291 "GetCategoryListAdmin"))
293 xmlOut.addTextElementToData(
"is_admin",
"0");
298 xmlOut.addTextElementToData(
"is_admin",
"1");
299 getCategories(&xmlOut);
301 else if(requestType ==
"SetActiveCategory")
314 else if(requestType ==
"RefreshLogbook")
320 uint32_t Duration = CgiDataUtilities::postDataAsInt(cgiIn,
"Duration");
323 sscanf(Date.c_str(),
"%li", &date);
325 __COUT__ <<
"date " << date <<
" duration " << Duration << std::endl;
326 std::stringstream str;
327 refreshLogbook(date, Duration, &xmlOut, (std::ostringstream*)&str);
328 __COUT__ << str.str() << std::endl;
330 else if(requestType ==
"PreviewEntry")
339 std::string EntryText = cgiIn(
"EntryText");
340 __COUT__ <<
"EntryText " << EntryText << std::endl << std::endl;
341 std::string EntrySubject = cgiIn(
"EntrySubject");
342 __COUT__ <<
"EntrySubject " << EntrySubject << std::endl << std::endl;
345 std::string creator = userInfo.username_;
347 savePostPreview(EntrySubject, EntryText, cgiIn.getFiles(), creator, &xmlOut);
351 else if(requestType ==
"ApproveEntry")
359 movePreviewEntry(PreviewNumber, Approve ==
"1", &xmlOut);
361 else if(requestType ==
"AdminRemoveRestoreEntry")
373 std::string hider = userInfo.username_;
375 hideLogbookEntry(EntryId, Hide, hider);
377 xmlOut.addTextElementToData(XML_ADMIN_STATUS,
"1");
381 __SUP_SS__ <<
"requestType Request, " << requestType
382 <<
", not recognized by the Logbook Supervisor (was it intended for "
383 "another Supervisor?)."
396 const WebUsers::RequestUserInfo& )
402 if(requestType ==
"LogImage")
405 __COUT__ <<
" Get Log Image " << src << std::endl;
407 out <<
"<!DOCTYPE HTML><html lang='en'><frameset col='100%' row='100%'><frame "
408 "src='/WebPath/html/LogbookImage.html?urn="
409 << this->getApplicationDescriptor()->getLocalId() <<
"&src=" << src
410 <<
"'></frameset></html>";
412 else if(requestType ==
"LogReport")
415 __COUT__ <<
" Start Log Report for " << activeCategory << std::endl;
417 out <<
"<!DOCTYPE HTML><html lang='en'><header><title>ots Logbook "
418 "Reports</title></header><frameset col='100%' row='100%'><frame "
419 "src='/WebPath/html/LogbookReport.html?urn="
420 << this->getApplicationDescriptor()->getLocalId()
421 <<
"&activeCategory=" << activeCategory <<
"'></frameset></html>";
424 __COUT__ <<
"requestType request not recognized." << std::endl;
435 SOAPUtilities::receive(msg, parameters);
436 std::string EntryText = parameters.getValue(
"EntryText");
437 std::string SubjectText = parameters.getValue(
"SubjectText");
439 if(SubjectText ==
"")
440 SubjectText =
"System Log";
442 __COUT__ <<
"Received External Supervisor System Entry " << EntryText << std::endl;
443 __COUTV__(SubjectText);
444 __COUT__ <<
"Active Category is " << activeCategory_ << std::endl;
446 std::string retStr =
"Success";
449 logDirPath = (std::string)LOGBOOK_PATH + (std::string)LOGBOOK_LOGBOOKS_PATH +
450 (std::string)LOGBOOK_CATEGORY_DIR_PREFACE + activeCategory_;
452 char dayIndexStr[20];
455 xercesc::DOMElement* entryEl;
458 if(activeCategory_ ==
"")
461 "Warning - Currently, no Active Category. Turn off the Logbook XDAQ "
462 "Application to suppress this message.";
463 __COUT__ << retStr << std::endl;
468 dir = opendir(logDirPath.c_str());
471 retStr =
"Error - Active Category directory missing.";
472 __COUT__ << retStr << std::endl;
477 sprintf(dayIndexStr,
"%6.6lu", time(0) / (60 * 60 * 24));
479 logPath = logDirPath +
"/" + LOGBOOK_FILE_PREFACE + activeCategory_ +
"_" +
480 (std::string)dayIndexStr + LOGBOOK_FILE_EXTENSION;
481 __COUT__ <<
"logPath " << logPath << std::endl;
494 entryEl = logXml.addTextElementToData(XML_LOGBOOK_ENTRY);
511 return SOAPUtilities::makeSOAPMessageReference(
"SystemLogEntryStatusResponse",
556 std::string LogbookSupervisor::getActiveCategory()
558 FILE* fp = fopen(std::string((std::string)ACTIVE_CATEGORY_PATH).c_str(),
"r");
560 activeCategory_ =
"";
564 if(!fgets(line, 100, fp))
570 if(line[strlen(line) - 2] ==
'\r')
571 line[strlen(line) - 2] =
'\0';
572 else if(line[strlen(line) - 1] ==
'\n')
573 line[strlen(line) - 1] =
'\0';
575 activeCategory_ = line;
578 return activeCategory_;
584 void LogbookSupervisor::setActiveCategory(std::string category)
586 FILE* fp = fopen(std::string((std::string)ACTIVE_CATEGORY_PATH).c_str(),
"w");
589 __COUT__ <<
"FATAL ERROR!!! - file write" << std::endl;
593 fprintf(fp,
"%s", category.c_str());
596 if(activeCategory_ !=
"" &&
597 activeCategory_ != category)
598 theRemoteWebUsers_.makeSystemLogEntry(
599 "Category was made inactive.");
601 bool entryNeeded =
false;
603 activeCategory_ != category)
606 activeCategory_ = category;
607 __COUT__ <<
"Active Category set to " << activeCategory_ << std::endl;
610 theRemoteWebUsers_.makeSystemLogEntry(
611 "Category was made active.");
617 bool LogbookSupervisor::validateCategoryName(std::string& exp)
619 if(exp.length() < CATEGORY_NAME_MIN_LENTH || exp.length() > CATEGORY_NAME_MAX_LENTH)
621 for(
int i = 0; i < (int)exp.length(); ++i)
622 if(!((exp[i] >=
'a' && exp[i] <=
'z') || (exp[i] >=
'A' && exp[i] <=
'Z') ||
623 (exp[i] >=
'0' && exp[i] <=
'9') || (exp[i] ==
'-' || exp[i] ==
'_')))
625 exp = exp.substr(0, i) + exp.substr(i + 1);
636 void LogbookSupervisor::getCategories(
HttpXmlDocument* xmlOut, std::ostringstream* out)
642 __COUT__ <<
"Fatal Error - Category database." << std::endl;
643 __COUT__ <<
"Creating empty category database." << std::endl;
645 expXml.addTextElementToData((std::string)XML_CATEGORY_ROOT);
650 std::vector<std::string> exps;
654 xmlOut->addTextElementToData(XML_ACTIVE_CATEGORY, activeCategory_);
656 for(
unsigned int i = 0; i < exps.size(); ++i)
659 xmlOut->addTextElementToData(XML_CATEGORY, exps[i]);
661 *out << exps[i] << std::endl;
667 void LogbookSupervisor::createCategory(std::string category,
671 if(!validateCategoryName(category))
674 xmlOut->addTextElementToData(
675 XML_ADMIN_STATUS,
"Error - Category name must be 3-25 characters.");
679 __COUT__ <<
"category " << category << std::endl;
682 std::string dirPath = (std::string)LOGBOOK_PATH + (std::string)LOGBOOK_LOGBOOKS_PATH +
683 (std::string)LOGBOOK_CATEGORY_DIR_PREFACE + category;
685 __COUT__ <<
"dirPath " << dirPath << std::endl;
687 bool directoryExists =
false;
688 DIR* dir = opendir(dirPath.c_str());
692 directoryExists =
true;
700 xmlOut->addTextElementToData(XML_ADMIN_STATUS,
701 "Fatal Error - Category database.");
705 std::vector<std::string> exps;
708 for(
unsigned int i = 0; i < exps.size(); ++i)
709 if(category == exps[i])
712 xmlOut->addTextElementToData(
714 "Failed - Category, " + category +
", already exists.");
717 __COUT__ <<
"categories count: " << exps.size() << std::endl;
725 xercesc::DOMElement* expEl =
728 sprintf(createTime,
"%lu", time(0));
737 dirPath +=
"/" + (std::string)LOGBOOK_UPLOADS_PATH;
738 __COUT__ <<
"Checking uploads directory" << std::endl;
740 directoryExists =
false;
741 dir = opendir(dirPath.c_str());
744 __COUT__ <<
"Creating uploads directory" << std::endl;
745 if(-1 == mkdir(dirPath.c_str(), 0755))
748 xmlOut->addTextElementToData(XML_ADMIN_STATUS,
749 "Failed - uploads directory for " +
750 category +
" was not created.");
751 __COUT__ <<
"Uploads directory failure." << std::endl;
758 xmlOut->addTextElementToData(XML_ADMIN_STATUS,
759 "Directory already exists for " + category +
760 ", re-added to list of categories.");
763 __COUT__ <<
"Creating category and uploads directory at: " << dirPath << std::endl;
764 if(-1 == mkdir(dirPath.c_str(), 0755) ||
765 -1 == mkdir((dirPath +
"/" + (std::string)LOGBOOK_UPLOADS_PATH).c_str(), 0755))
768 xmlOut->addTextElementToData(
770 "Failed - directory, " + category +
", could not be created.");
775 xmlOut->addTextElementToData(XML_ADMIN_STATUS,
776 "Category, " + category +
", successfully created.");
783 void LogbookSupervisor::webUserSetActiveCategory(std::string category,
788 setActiveCategory(category);
790 xmlOut->addTextElementToData(XML_ADMIN_STATUS,
791 "Active category cleared successfully.");
799 xmlOut->addTextElementToData(XML_ADMIN_STATUS,
800 "Fatal Error - Category database.");
803 std::vector<std::string> exps;
807 for(i = 0; i < exps.size(); ++i)
808 if(category == exps[i])
814 xmlOut->addTextElementToData(
815 XML_ADMIN_STATUS,
"Failed - Category, " + category +
", not found.");
820 setActiveCategory(category);
822 xmlOut->addTextElementToData(
823 XML_ADMIN_STATUS,
"Active category set to " + category +
" successfully.");
830 void LogbookSupervisor::removeCategory(std::string category,
834 __COUT__ <<
"category " << category << std::endl;
841 xmlOut->addTextElementToData(XML_ADMIN_STATUS,
842 "Fatal Error - Category database.");
845 std::vector<std::string> exps;
849 for(i = 0; i < exps.size(); ++i)
850 if(category == exps[i])
856 xmlOut->addTextElementToData(
857 XML_ADMIN_STATUS,
"Failed - Category, " + category +
", not found.");
866 __COUT__ <<
"categories original count: " << expXml.
getChildrenCount(parent)
869 __COUT__ <<
"categories new count: " << expXml.
getChildrenCount(parent) << std::endl;
872 FILE* fp = fopen(((std::string)REMOVE_CATEGORY_LOG_PATH).c_str(),
"a");
876 xmlOut->addTextElementToData(XML_ADMIN_STATUS,
"Fatal Error - Remove log.");
880 "%s -- %s Category removed by %s.\n",
881 asctime(localtime(&((time_t
const&)(time(0))))),
889 if(activeCategory_ == category)
893 xmlOut->addTextElementToData(XML_ADMIN_STATUS,
894 "Category, " + category +
", successfully removed.");
903 void LogbookSupervisor::refreshLogbook(time_t date,
906 std::ostringstream* out,
907 std::string category)
910 category = activeCategory_;
912 xmlOut->addTextElementToData(XML_ACTIVE_CATEGORY, category);
915 std::string dirPath = (std::string)LOGBOOK_PATH + (std::string)LOGBOOK_LOGBOOKS_PATH +
916 (std::string)LOGBOOK_CATEGORY_DIR_PREFACE + category;
919 *out << __COUT_HDR_FL__ <<
"dirPath " << dirPath << std::endl;
921 DIR* dir = opendir(dirPath.c_str());
925 xmlOut->addTextElementToData(
926 XML_STATUS,
"Error - Directory for category, " + category +
", missing.");
928 *out << __COUT_HDR_FL__ <<
"Error - Directory missing" << std::endl;
932 unsigned int baseDay;
937 unsigned int extractedDay;
940 mostRecentDayIndex_ = 0;
941 while((drnt = readdir(dir)))
946 if(strcmp(&(drnt->d_name[strlen(drnt->d_name) - 4]),
".xml"))
949 for(finish = strlen(drnt->d_name) - 1; finish > 0; --finish)
950 if(drnt->d_name[finish] ==
'.')
955 *out << __COUT_HDR_FL__ <<
"failed to find day index finish "
959 for(start = finish - 1; start > 0; --start)
960 if(drnt->d_name[start - 1] ==
'_')
965 *out << __COUT_HDR_FL__ <<
"failed to find day index start "
969 drnt->d_name[finish] =
'\0';
970 extractedDay = atoi((
char*)(&(drnt->d_name[start])));
973 if(!mostRecentDayIndex_ || mostRecentDayIndex_ < extractedDay)
974 mostRecentDayIndex_ = extractedDay;
977 *out << __COUT_HDR_FL__
978 <<
"dirContents done, found most recent day: " << mostRecentDayIndex_
981 baseDay = mostRecentDayIndex_;
984 baseDay = (date / (60 * 60 * 24));
987 std::string entryPath;
988 char dayIndexStr[20];
994 for(uint32_t i = duration; i != 0; --i)
996 sprintf(dayIndexStr,
"%6.6u", baseDay - i + 1);
997 entryPath = dirPath +
"/" + LOGBOOK_FILE_PREFACE + category +
"_" +
998 (std::string)dayIndexStr + LOGBOOK_FILE_EXTENSION;
1001 *out << __COUT_HDR_FL__ <<
"Directory Entry " << entryPath << std::endl;
1003 fp = fopen(entryPath.c_str(),
"r");
1007 *out << __COUT_HDR_FL__ <<
"File not found" << std::endl;
1018 xmlOut->addTextElementToData(
1019 XML_STATUS,
"Critical Failure - log did not load. Notify admins.");
1021 *out << __COUT_HDR_FL__ <<
"Failure - log XML did not load" << std::endl;
1030 xmlOut->addTextElementToData(XML_STATUS,
"1");
1032 *out << __COUT_HDR_FL__ <<
"Today: " << time(0) / (60 * 60 * 24) << std::endl;
1034 sprintf(dayIndexStr,
"%lu", time(0) / (60 * 60 * 24) - mostRecentDayIndex_);
1036 xmlOut->addTextElementToData(XML_MOST_RECENT_DAY,
1044 void LogbookSupervisor::cleanUpPreviews()
1046 std::string previewPath =
1047 (std::string)LOGBOOK_PATH + (std::string)LOGBOOK_PREVIEWS_PATH;
1049 DIR* dir = opendir(previewPath.c_str());
1052 __COUT__ <<
"Error - Previews directory missing: " << previewPath << std::endl;
1056 struct dirent* entry;
1057 time_t dirCreateTime;
1064 if(strcmp(entry->d_name,
".") != 0 && strcmp(entry->d_name,
"..") != 0 &&
1065 strcmp(entry->d_name,
".svn") != 0)
1068 for(i = 0; i < strlen(entry->d_name); ++i)
1069 if(entry->d_name[i] ==
'_')
1071 entry->d_name[i] =
' ';
1074 sscanf(entry->d_name,
"%li", &dirCreateTime);
1076 if((time(0) - dirCreateTime) > LOGBOOK_PREVIEW_EXPIRATION_TIME)
1078 __COUT__ <<
"Expired" << std::endl;
1080 entry->d_name[i] =
'_';
1082 __COUT__ <<
"rm -rf " << previewPath + (std::string)entry->d_name
1086 ((std::string)(
"rm -rf " + previewPath + (std::string)entry->d_name))
1098 void LogbookSupervisor::savePostPreview(std::string& subject,
1100 const std::vector<cgicc::FormFile>& files,
1101 std::string creator,
1104 if(activeCategory_ ==
"")
1107 xmlOut->addTextElementToData(XML_STATUS,
1108 "Failed - no active category currently!");
1117 std::string previewPath = (std::string)LOGBOOK_PATH +
1118 (std::string)LOGBOOK_PREVIEWS_PATH + (std::string)fileIndex;
1120 __COUT__ <<
"previewPath " << previewPath << std::endl;
1121 if(-1 == mkdir(previewPath.c_str(), 0755))
1124 xmlOut->addTextElementToData(XML_STATUS,
1125 "Failed - preview could not be generated.");
1140 escapeLogbookEntry(text);
1141 escapeLogbookEntry(subject);
1142 __COUT__ <<
"~~subject " << subject << std::endl
1143 <<
"~~text " << text << std::endl
1148 previewXml.addTextElementToData(XML_LOGBOOK_ENTRY);
1150 XML_LOGBOOK_ENTRY_TIME, fileIndex, XML_LOGBOOK_ENTRY);
1152 xmlOut->addTextElementToData(XML_LOGBOOK_ENTRY_TIME, fileIndex);
1154 XML_LOGBOOK_ENTRY_CREATOR, creator, XML_LOGBOOK_ENTRY);
1156 xmlOut->addTextElementToData(XML_LOGBOOK_ENTRY_CREATOR,
1160 xmlOut->addTextElementToData(XML_LOGBOOK_ENTRY_TEXT, text);
1162 XML_LOGBOOK_ENTRY_SUBJECT, subject, XML_LOGBOOK_ENTRY);
1164 xmlOut->addTextElementToData(XML_LOGBOOK_ENTRY_SUBJECT,
1167 __COUT__ <<
"file size " << files.size() << std::endl;
1169 std::string filename;
1170 std::ofstream myfile;
1171 for(
unsigned int i = 0; i < files.size(); ++i)
1174 XML_LOGBOOK_ENTRY_FILE, files[i].getDataType(), XML_LOGBOOK_ENTRY);
1176 xmlOut->addTextElementToData(XML_LOGBOOK_ENTRY_FILE,
1177 files[i].getDataType());
1179 if((filename = validateUploadFileType(files[i].getDataType())) ==
1183 xmlOut->addTextElementToData(
1185 "Failed - invalid file type, " + files[i].getDataType() +
".");
1190 sprintf(fileIndex,
"%d", i);
1191 filename = previewPath +
"/" + (std::string)LOGBOOK_PREVIEW_UPLOAD_PREFACE +
1192 (std::string)fileIndex +
"." + filename;
1194 __COUT__ <<
"file " << i <<
" - " << filename << std::endl;
1195 myfile.open(filename.c_str());
1196 if(myfile.is_open())
1198 files[i].writeToStream(myfile);
1204 previewXml.
saveXmlDocument(previewPath +
"/" + (std::string)LOGBOOK_PREVIEW_FILE);
1207 xmlOut->addTextElementToData(XML_STATUS,
"1");
1209 xmlOut->addTextElementToData(XML_PREVIEW_INDEX,
1219 void LogbookSupervisor::movePreviewEntry(std::string previewNumber,
1223 __COUT__ <<
"previewNumber " << previewNumber
1224 << (approve ?
" Accepted" :
" Cancelled") << std::endl;
1226 std::string sysCmd, previewPath = (std::string)LOGBOOK_PATH +
1227 (std::string)LOGBOOK_PREVIEWS_PATH + previewNumber;
1234 previewXml.
loadXmlDocument(previewPath +
"/" + (std::string)LOGBOOK_PREVIEW_FILE);
1236 std::string logPath,
1237 logDirPath = (std::string)LOGBOOK_PATH + (std::string)LOGBOOK_LOGBOOKS_PATH +
1238 (std::string)LOGBOOK_CATEGORY_DIR_PREFACE + activeCategory_;
1241 DIR* dir = opendir(logDirPath.c_str());
1244 __COUT__ <<
"Error - Active Category directory missing: " << logPath
1250 char dayIndexStr[20];
1251 sprintf(dayIndexStr,
"%6.6lu", time(0) / (60 * 60 * 24));
1253 logPath = logDirPath +
"/" + LOGBOOK_FILE_PREFACE + activeCategory_ +
"_" +
1254 (std::string)dayIndexStr + LOGBOOK_FILE_EXTENSION;
1255 __COUT__ <<
"logPath " << logPath << std::endl;
1273 std::vector<std::string> fileTypes;
1275 std::string entryTimeLabel = previewXml.
getMatchingValue(XML_LOGBOOK_ENTRY_TIME);
1276 std::string fileExtension, previewFilename, logFilename;
1278 for(
unsigned int i = 0; i < fileTypes.size(); ++i)
1280 if((fileExtension = validateUploadFileType(fileTypes[i])) ==
1283 __COUT__ <<
"Failed - invalid file type: " << fileTypes[i] << std::endl;
1288 sprintf(fileIndex,
"%d", i);
1289 previewFilename = (std::string)LOGBOOK_PREVIEW_UPLOAD_PREFACE +
1290 (std::string)fileIndex +
"." + fileExtension;
1291 logFilename = (std::string)LOGBOOK_PREVIEW_UPLOAD_PREFACE + entryTimeLabel +
1292 "_" + (std::string)fileIndex +
"." + fileExtension;
1294 sysCmd =
"mv " + (previewPath +
"/" + previewFilename) +
" " +
1295 (logDirPath +
"/" + (std::string)LOGBOOK_UPLOADS_PATH + logFilename);
1296 __COUT__ << sysCmd << std::endl;
1297 system(sysCmd.c_str());
1302 sysCmd =
"rm -rf " + previewPath;
1303 __COUT__ << sysCmd << std::endl << std::endl;
1304 system(sysCmd.c_str());
1310 std::string LogbookSupervisor::validateUploadFileType(
const std::string fileType)
1312 for(
unsigned int i = 0; i < allowedFileUploadTypes_.size(); ++i)
1313 if(allowedFileUploadTypes_[i] == fileType)
1314 return matchingFileUploadTypes_[i];
1323 void LogbookSupervisor::escapeLogbookEntry(std::string& )
1337 void LogbookSupervisor::hideLogbookEntry(
const std::string& entryId,
1339 const std::string& hider)
1341 __COUT__ <<
"Hide=" << hide <<
" for entryid " << entryId << std::endl;
1344 char dayIndexStr[20];
1346 for(i = 0; i < entryId.length(); ++i)
1347 if(entryId[i] ==
'_')
1349 dayIndexStr[i] =
'\0';
1353 dayIndexStr[i] = entryId[i];
1355 sscanf(dayIndexStr,
"%li", &days);
1356 days /= 60 * 60 * 24;
1357 sprintf(dayIndexStr,
"%6.6lu", days);
1359 std::string logDirPath = (std::string)LOGBOOK_PATH +
1360 (std::string)LOGBOOK_LOGBOOKS_PATH +
1361 (std::string)LOGBOOK_CATEGORY_DIR_PREFACE + activeCategory_;
1362 std::string logPath = logDirPath +
"/" + LOGBOOK_FILE_PREFACE + activeCategory_ +
1363 "_" + (std::string)dayIndexStr + LOGBOOK_FILE_EXTENSION;
1365 __COUT__ <<
"logPath=" << logPath << std::endl;
1371 __COUT__ <<
"Failure - log XML did not load" << std::endl;
1375 std::vector<std::string> allEntryIds;
1377 for(i = 0; i < allEntryIds.size(); ++i)
1378 if(allEntryIds[i] == entryId)
1380 if(i == allEntryIds.size())
1382 __COUT__ <<
"Failure - entry not found" << std::endl;
1390 XML_LOGBOOK_ENTRY, i);
1400 __COUT__ <<
"Hidden tag already applied to entry." << std::endl;
1404 XML_LOGBOOK_ENTRY_HIDDEN,
1408 XML_LOGBOOK_ENTRY_HIDER, hider, hiddenParentEl);
1409 sprintf(dayIndexStr,
"%lu", time(0));
1411 XML_LOGBOOK_ENTRY_HIDDEN_TIME, dayIndexStr, hiddenParentEl);
1417 __COUT__ <<
"Entry already was not hidden." << std::endl;
1425 __COUT__ <<
"Success." << std::endl;
static std::string postData(cgicc::Cgicc &cgi, const std::string &needle)
static std::string getData(cgicc::Cgicc &cgi, const std::string &needle)
WebUsers::permissionLevel_t getSupervisorPropertyUserPermissionsThreshold(const std::string &requestType)
xercesc::DOMElement * getMatchingElement(const std::string &field, const unsigned int occurance=0)
void getAllMatchingValues(const std::string &field, std::vector< std::string > &retVec)
xercesc::DOMElement * getMatchingElementInSubtree(xercesc::DOMElement *currEl, const std::string &field, const unsigned int occurance=0)
void copyDataChildren(HttpXmlDocument &document)
bool loadXmlDocument(const std::string &filePath)
unsigned int getChildrenCount(xercesc::DOMElement *parent=0)
std::string getMatchingValue(const std::string &field, const unsigned int occurance=0)
xoap::MessageReference MakeSystemLogEntry(xoap::MessageReference msg)
External Supervisor XOAP handlers.
virtual void request(const std::string &requestType, cgicc::Cgicc &cgiIn, HttpXmlDocument &xmlOut, const WebUsers::RequestUserInfo &userInfo) override
virtual void forceSupervisorPropertyValues(void) override
override to force supervisor property values (and ignore user settings)
virtual void setSupervisorPropertyDefaults(void) override
override to control supervisor specific defaults
virtual void nonXmlRequest(const std::string &requestType, cgicc::Cgicc &cgiIn, std::ostream &out, const WebUsers::RequestUserInfo &userInfo) override
void addParameter(const std::string name, const std::string value)
xercesc::DOMElement * addTextElementToParent(const std::string &childName, const std::string &childText, xercesc::DOMElement *parent)
void recursiveRemoveChild(xercesc::DOMElement *childEl, xercesc::DOMElement *parentEl)
void saveXmlDocument(const std::string &filePath)
void INIT_MF(const char *name)