210 lines
6.7 KiB
MQL5
210 lines
6.7 KiB
MQL5
//+------------------------------------------------------------------+
|
|
//| FolderEncrypt.mqh |
|
|
//| Copyright 2026, Niquel Mendoza. |
|
|
//| https://www.mql5.com/es/users/nique_372 |
|
|
//+------------------------------------------------------------------+
|
|
#property copyright "Copyright 2026, Niquel Mendoza."
|
|
#property link "https://www.mql5.com/es/users/nique_372"
|
|
#property strict
|
|
|
|
#ifndef MQLARTICLES_UTILS_FILE_FOLDEROPS_FOLDERENCRYPT_MQH
|
|
#define MQLARTICLES_UTILS_FILE_FOLDEROPS_FOLDERENCRYPT_MQH
|
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
#include "Base.mqh"
|
|
#include "..\\..\\Encrypt\\Encrypt.mqh"
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
class CFolderOpsEncrypt : public CFolderOpsBase
|
|
{
|
|
private:
|
|
bool m_solo_first_pasada;
|
|
void* m_random;
|
|
CEncryptor* m_encyptor;
|
|
bool m_clean_all;
|
|
|
|
|
|
public:
|
|
CFolderOpsEncrypt(void) : m_solo_first_pasada(false), m_random(NULL), m_encyptor(NULL), m_clean_all(false)
|
|
{}
|
|
~CFolderOpsEncrypt(void) {}
|
|
|
|
//---
|
|
void Encryptor(CEncryptor* encryptor) { m_encyptor = encryptor; }
|
|
CEncryptor* Encryptor() { return m_encyptor; }
|
|
|
|
//---
|
|
void RandomForEncryptor(void* random) { m_random = random; }
|
|
void* RandomForEncryptor() { return m_random; }
|
|
|
|
//---
|
|
void SetInitialPass() { m_solo_first_pasada = true; m_clean_all = false; }
|
|
bool EncryptFolder(const string& init_dir, bool common_flag, string& key, ENUM_CRYPT_METHOD method, bool delete_prev_file, string ext_encrypt = NULL);
|
|
bool DecryptFolder(const string& init_dir, bool common_flag, const string& key, ENUM_CRYPT_METHOD method, bool delete_prev_file, string ext_decrypt = NULL);
|
|
};
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
bool CFolderOpsEncrypt::DecryptFolder(const string& init_dir, bool common_flag, const string& key, ENUM_CRYPT_METHOD method, bool delete_prev_file, string ext_decrypt = NULL)
|
|
{
|
|
//---
|
|
if(m_solo_first_pasada)
|
|
{
|
|
//---
|
|
m_encyptor.Key(key);
|
|
}
|
|
|
|
//---
|
|
string filename = "";
|
|
::ResetLastError();
|
|
long handle = FileFindFirst(init_dir + "*", filename, (common_flag ? FILE_COMMON : 0));
|
|
if(handle == INVALID_HANDLE)
|
|
{
|
|
LogWarning(StringFormat("Error al comenzar la busqueda, ultimo error = %d, puede ser nomal si no hay archivos", ::GetLastError()), FUNCION_ACTUAL);
|
|
return true;
|
|
}
|
|
|
|
//---
|
|
do
|
|
{
|
|
filename = init_dir + filename;
|
|
|
|
//---
|
|
if(filename[StringLen(filename) - 1] == '\\')
|
|
{
|
|
m_solo_first_pasada = false;
|
|
// a false dado qeu volveremos a llamar
|
|
//--- Buscamos si este folder esta en los arc hivos incluidos si lo esta entonces limpiamos todo sin exepcion
|
|
if(IsFileIncluyed(filename))
|
|
m_clean_all = true;
|
|
else
|
|
m_clean_all = false;
|
|
|
|
//---
|
|
if(!DecryptFolder(filename, common_flag, key, method, delete_prev_file, ext_decrypt))
|
|
{
|
|
LogError(StringFormat("Error al limpiar el folder = %s", filename), FUNCION_ACTUAL);
|
|
FileFindClose(handle);
|
|
return false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
//---
|
|
if(!m_clean_all)
|
|
{
|
|
if(IsFileExcluyed(filename))
|
|
continue;
|
|
if(!IsFileIncluyed(filename))
|
|
continue;
|
|
}
|
|
|
|
//---
|
|
if(!m_encyptor.DecryptFile(filename, common_flag, method, delete_prev_file, ext_decrypt))
|
|
{
|
|
LogWarning(StringFormat("Fallo al eliminar el desencriptar = '%s'", filename), FUNCION_ACTUAL);
|
|
}
|
|
}
|
|
}
|
|
while(FileFindNext(handle, filename));
|
|
|
|
//---
|
|
FileFindClose(handle);
|
|
|
|
//--- Si se limpio
|
|
return true;
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
bool CFolderOpsEncrypt::EncryptFolder(const string& init_dir, bool common_flag, string& key, ENUM_CRYPT_METHOD method, bool delete_prev_file, string ext_encrypt = NULL)
|
|
{
|
|
//---
|
|
if(m_solo_first_pasada)
|
|
{
|
|
//---
|
|
if(key.Length() < 1)
|
|
{
|
|
m_encyptor.SetAleatoryKey(method, RandomSimpleGenerateRandomUchar, m_random);
|
|
key = m_encyptor.Key();
|
|
}
|
|
else
|
|
if(key == "u") // usar la que ya existe
|
|
{
|
|
key = m_encyptor.Key();
|
|
}
|
|
else
|
|
{
|
|
m_encyptor.Key(key);
|
|
}
|
|
}
|
|
|
|
//---
|
|
string filename = "";
|
|
::ResetLastError();
|
|
//Print(init_dir);
|
|
long handle = FileFindFirst(init_dir + "*", filename, (common_flag ? FILE_COMMON : 0));
|
|
if(handle == INVALID_HANDLE)
|
|
{
|
|
LogWarning(StringFormat("Error al comenzar la busqueda, ultimo error = %d, puede ser nomal si no hay archivos", ::GetLastError()), FUNCION_ACTUAL);
|
|
return true;
|
|
}
|
|
|
|
//---
|
|
do
|
|
{
|
|
const string full_filename = init_dir + filename;
|
|
|
|
//---
|
|
if(filename[StringLen(filename) - 1] == '\\')
|
|
{
|
|
m_solo_first_pasada = false;
|
|
// a false dado qeu volveremos a llamar
|
|
//--- Buscamos si este folder esta en los arc hivos incluidos si lo esta entonces limpiamos todo sin exepcion
|
|
if(IsFileIncluyed(filename))
|
|
m_clean_all = true;
|
|
|
|
//---
|
|
if(!EncryptFolder(full_filename, common_flag, key, method, delete_prev_file, ext_encrypt))
|
|
{
|
|
LogError(StringFormat("Error al encryptar el folder = %s", full_filename), FUNCION_ACTUAL);
|
|
FileFindClose(handle);
|
|
return false;
|
|
}
|
|
m_clean_all = false;
|
|
}
|
|
else
|
|
{
|
|
//---
|
|
if(!m_clean_all)
|
|
{
|
|
if(IsFileExcluyed(filename))
|
|
continue;
|
|
if(!IsFileIncluyed(filename))
|
|
continue;
|
|
}
|
|
|
|
//---
|
|
//Print(filename);
|
|
if(!m_encyptor.EncryptFile(full_filename, common_flag, method, delete_prev_file, ext_encrypt))
|
|
{
|
|
LogWarning(StringFormat("Fallo al encryptar el archivo = '%s'", filename), FUNCION_ACTUAL);
|
|
}
|
|
|
|
}
|
|
}
|
|
while(FileFindNext(handle, filename));
|
|
|
|
//---
|
|
FileFindClose(handle);
|
|
|
|
//--- Si se limpio
|
|
return true;
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
#endif // MQLARTICLES_UTILS_FILE_FOLDEROPS_FOLDERENCRYPT_MQH
|