/*************************************************************************
* UrBackup - Client/Server backup system
* Copyright (C) 2011 Martin Raiber
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
**************************************************************************/
#ifndef CLIENT_ONLY
#include "action_header.h"
#include "../server_settings.h"
#include "../os_functions.h"
#include "../server_status.h"
extern std::string server_identity;
ACTION_IMPL(status)
{
Helper helper(tid, &GET, &PARAMS);
JSON::Object ret;
std::string rights=helper.getRights("status");
std::vector clientids;
IDatabase *db=helper.getDatabase();
if(rights!="all" && rights!="none" )
{
std::vector s_clientid;
Tokenize(rights, s_clientid, ",");
for(size_t i=0;iid==-1) return;
if(session!=NULL && (rights=="all" || !clientids.empty()) )
{
{
ServerSettings settings(db);
if(!os_directory_exists(os_file_prefix()+settings.getSettings()->backupfolder) || !os_directory_exists(os_file_prefix()+settings.getSettings()->backupfolder_uncompr) || settings.getSettings()->backupfolder.empty())
{
ret.set("dir_error", true);
}
else if(!os_directory_exists(os_file_prefix()+settings.getSettings()->backupfolder+os_file_sep()+L"clients") && !os_create_dir(os_file_prefix()+settings.getSettings()->backupfolder+os_file_sep()+L"clients") )
{
ret.set("dir_error" ,true);
}
IFile *tmp=Server->openTemporaryFile();
if(tmp==NULL)
{
ret.set("tmpdir_error", true);
}
else
{
Server->destroy(tmp);
}
}
bool details=false;
if(GET.find(L"details")!=GET.end())
{
details=true;
ret.set("details", true);
}
std::wstring hostname=GET[L"hostname"];
if(!hostname.empty() && rights=="all")
{
if(GET[L"remove"]==L"true")
{
IQuery *q=db->Prepare("DELETE FROM extra_clients WHERE id=?");
q->Bind(hostname);
q->Write();
q->Reset();
}
else
{
IQuery *q=db->Prepare("INSERT INTO extra_clients (hostname) SELECT ? AS hostname WHERE NOT EXISTS (SELECT hostname FROM extra_clients WHERE hostname=?)");
q->Bind(hostname);
q->Bind(hostname);
q->Write();
q->Reset();
}
}
std::wstring s_remove_client=GET[L"remove_client"];
if(!s_remove_client.empty() && helper.getRights("remove_client")=="all")
{
int remove_client=watoi(s_remove_client);
if(GET.find(L"stop_remove_client")!=GET.end())
{
IQuery *q=db->Prepare("UPDATE clients SET delete_pending=0 WHERE id=?");
q->Bind(remove_client);
q->Write();
q->Reset();
}
else
{
IQuery *q=db->Prepare("UPDATE clients SET delete_pending=1 WHERE id=?");
q->Bind(remove_client);
q->Write();
q->Reset();
}
}
JSON::Array status;
IDatabase *db=helper.getDatabase();
std::string filter;
if(!clientids.empty())
{
filter=" WHERE ";
for(size_t i=0;iRead("SELECT id, delete_pending, name, strftime('"+helper.getTimeFormatString()+"', lastbackup, 'localtime') AS lastbackup, strftime('"+helper.getTimeFormatString()+"', lastseen, 'localtime') AS lastseen,"
"strftime('"+helper.getTimeFormatString()+"', lastbackup_image, 'localtime') AS lastbackup_image FROM clients"+filter);
int backup_ok_mod=3;
db_results res_t=db->Read("SELECT value FROM settings WHERE key='backup_ok_mod' AND clientid=0");
if(res_t.size()>0)
{
backup_ok_mod=watoi(res_t[0][L"value"]);
}
std::vector client_status=ServerStatus::getStatus();
for(size_t i=0;iPrepare("SELECT id FROM clients WHERE lastbackup IS NOT NULL AND datetime('now','-"+nconvert(settings.getSettings()->update_freq_incr*backup_ok_mod)+" seconds')Bind(clientid);
db_results res_file_ok=q->Read();
q->Reset();
stat.set("file_ok", !res_file_ok.empty());
q=db->Prepare("SELECT id FROM clients WHERE lastbackup_image IS NOT NULL AND datetime('now','-"+nconvert(settings.getSettings()->update_freq_image_incr*backup_ok_mod)+" seconds')Bind(clientid);
res_file_ok=q->Read();
q->Reset();
stat.set("image_ok", !res_file_ok.empty());
status.add(stat);
}
if(rights=="all")
{
for(size_t i=0;iRead("SELECT id, hostname, lastip FROM extra_clients");
for(size_t i=0;i