2008-12-10 05:42:49 +01:00
|
|
|
/*
|
|
|
|
* This file is a part of CMSLU -- Content Management System like Unix
|
|
|
|
* and is not publicly distributed
|
|
|
|
*
|
2009-04-21 22:50:55 +02:00
|
|
|
* Copyright (c) 2008-2009, Tomasz Sowa
|
2008-12-10 05:42:49 +01:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "misc.h"
|
2009-04-21 22:50:55 +02:00
|
|
|
#include "log.h"
|
2008-12-10 05:42:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ToString(std::string & s, int value)
|
|
|
|
{
|
|
|
|
static char buffer[50];
|
|
|
|
|
|
|
|
sprintf(buffer, "%d", value);
|
|
|
|
s = buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ToString(std::string & s, long value)
|
|
|
|
{
|
|
|
|
static char buffer[50];
|
|
|
|
|
|
|
|
sprintf(buffer, "%ld", value);
|
|
|
|
s = buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-12-30 02:05:03 +01:00
|
|
|
|
|
|
|
bool CorrectUrlChar(char c)
|
|
|
|
{
|
|
|
|
if( (c >= 'a' && c <='z') ||
|
|
|
|
(c >= 'A' && c <='Z') ||
|
|
|
|
(c >= '0' && c <='9') ||
|
|
|
|
(c == '(' || c == ')' || c == '.' || c == ',' || c == '_' )
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CorrectUrl(Item & item)
|
2008-12-10 05:42:49 +01:00
|
|
|
{
|
2008-12-30 02:05:03 +01:00
|
|
|
std::string::iterator i;
|
|
|
|
|
|
|
|
for(i = item.url.begin(); i!=item.url.end() ; ++i)
|
|
|
|
{
|
|
|
|
if( !CorrectUrlChar(*i) )
|
|
|
|
{
|
|
|
|
int c = ChangeLocalChar(*i);
|
|
|
|
|
|
|
|
if( CorrectUrlChar(c) )
|
|
|
|
*i = c;
|
|
|
|
else
|
|
|
|
*i = '_';
|
|
|
|
}
|
|
|
|
}
|
2009-06-05 22:29:06 +02:00
|
|
|
|
|
|
|
ToSmall(item.url);
|
2008-12-30 02:05:03 +01:00
|
|
|
|
|
|
|
if( item.url.empty() )
|
|
|
|
item.url = "bez_nazwy"; // !! wrzucic do pliku konfiguracyjnego
|
|
|
|
else
|
|
|
|
if( item.url[0] >= '0' && item.url[0] <= '9' )
|
|
|
|
// url must begin with a letter
|
|
|
|
item.url.insert(item.url.begin(), '_');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SetUrlFromSubject(Item & item)
|
|
|
|
{
|
|
|
|
item.url = item.subject;
|
|
|
|
CorrectUrl(item);
|
|
|
|
|
|
|
|
/*
|
2008-12-10 05:42:49 +01:00
|
|
|
std::string::iterator i;
|
|
|
|
|
2008-12-14 06:28:28 +01:00
|
|
|
item.url.clear();
|
2008-12-10 05:42:49 +01:00
|
|
|
|
|
|
|
for(i = item.subject.begin(); i!=item.subject.end() ; ++i)
|
|
|
|
{
|
|
|
|
int c = ChangeLocalChar(*i);
|
|
|
|
|
|
|
|
if( (c >= 'a' && c <='z') ||
|
|
|
|
(c >= 'A' && c <='Z') ||
|
|
|
|
(c >= '0' && c <='9') ||
|
|
|
|
(c == '(' || c == ')' || c == '.' || c == ',' || c == '_' )
|
|
|
|
)
|
|
|
|
{
|
2008-12-14 06:28:28 +01:00
|
|
|
item.url += c;
|
2008-12-10 05:42:49 +01:00
|
|
|
}
|
|
|
|
else
|
2008-12-14 06:28:28 +01:00
|
|
|
item.url += '_';
|
2008-12-10 05:42:49 +01:00
|
|
|
}
|
|
|
|
|
2008-12-14 06:28:28 +01:00
|
|
|
if( item.url.empty() )
|
|
|
|
item.url = "bez_nazwy"; // !! wrzucic do pliku konfiguracyjnego
|
2008-12-10 05:42:49 +01:00
|
|
|
else
|
2008-12-14 06:28:28 +01:00
|
|
|
if( item.url[0] >= '0' && item.url[0] <= '9' )
|
|
|
|
// url must begin with a letter
|
|
|
|
item.url.insert(item.url.begin(), '_');
|
2008-12-30 02:05:03 +01:00
|
|
|
*/
|
2008-12-10 05:42:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-12-30 02:05:03 +01:00
|
|
|
|
2008-12-10 05:42:49 +01:00
|
|
|
int polish_letters_simple[] =
|
|
|
|
{ 'a', 'A',
|
|
|
|
'c', 'C',
|
|
|
|
's', 'S',
|
|
|
|
'e', 'E',
|
|
|
|
'l', 'L',
|
|
|
|
'o', 'O',
|
|
|
|
'z', 'Z',
|
|
|
|
'z', 'Z',
|
|
|
|
'n', 'N', 0 }; // 0 - terminating
|
|
|
|
|
|
|
|
|
|
|
|
int polish_letters_iso88592[] =
|
|
|
|
{ 0xb1, 0xa1,
|
|
|
|
0xe6, 0xc6,
|
|
|
|
0xb6, 0xa6,
|
|
|
|
0xea, 0xca,
|
|
|
|
0xb3, 0xa3,
|
|
|
|
0xf3, 0xd3,
|
|
|
|
0xbf, 0xaf,
|
|
|
|
0xbc, 0xac,
|
|
|
|
0xf1, 0xd1, 0 };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int ChangeLocalChar(unsigned char c)
|
|
|
|
{
|
|
|
|
// if( language == polish_iso88592 )
|
|
|
|
|
|
|
|
for(int i = 0 ; polish_letters_iso88592[i] ; ++i)
|
|
|
|
{
|
|
|
|
if( polish_letters_simple[i] == 0 )
|
|
|
|
{
|
|
|
|
log << log1 << "localization tables don't have the same size" << logend;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( polish_letters_iso88592[i] == c )
|
|
|
|
return polish_letters_simple[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-05 22:29:06 +02:00
|
|
|
bool HtmlTryChar(std::ostringstream & out, int c)
|
|
|
|
{
|
|
|
|
if( c == '<' )
|
|
|
|
{
|
|
|
|
out << "<";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if( c == '>' )
|
|
|
|
{
|
|
|
|
out << ">";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if( c == '&' )
|
|
|
|
{
|
|
|
|
out << "&";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-12-30 02:05:03 +01:00
|
|
|
void HtmlEscape(std::ostringstream & out, const std::string & in)
|
2008-12-10 05:42:49 +01:00
|
|
|
{
|
|
|
|
std::string::const_iterator i;
|
|
|
|
|
|
|
|
for(i = in.begin() ; i != in.end() ; ++i)
|
|
|
|
{
|
2009-06-05 22:29:06 +02:00
|
|
|
if( !HtmlTryChar(out, *i) )
|
2008-12-10 05:42:49 +01:00
|
|
|
out << *i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-30 02:05:03 +01:00
|
|
|
std::string HtmlEscape(const std::string & in)
|
2008-12-10 05:42:49 +01:00
|
|
|
{
|
|
|
|
std::ostringstream out;
|
|
|
|
|
2008-12-30 02:05:03 +01:00
|
|
|
HtmlEscape(out, in);
|
2008-12-10 05:42:49 +01:00
|
|
|
|
|
|
|
return out.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-05 22:29:06 +02:00
|
|
|
void HtmlEscapeFormTxt(std::ostringstream & out, const std::string & in)
|
|
|
|
{
|
|
|
|
std::string::const_iterator i;
|
|
|
|
int was_enter = 0; // how many enteres there were before
|
|
|
|
|
|
|
|
out << "<p>";
|
|
|
|
|
|
|
|
for(i = in.begin() ; i != in.end() ; ++i)
|
|
|
|
{
|
|
|
|
if( !HtmlTryChar(out, *i) )
|
|
|
|
{
|
|
|
|
if( *i == 13 )
|
|
|
|
// skipping stupid characters (\r\n\ in dos mode)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if( *i == 10 )
|
|
|
|
{
|
|
|
|
++was_enter;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( was_enter == 1 )
|
|
|
|
out << "<br>\n";
|
|
|
|
else
|
|
|
|
if( was_enter > 1 )
|
|
|
|
out << "</p>\n<p>";
|
|
|
|
|
|
|
|
out << *i;
|
|
|
|
was_enter = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out << "</p>\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string HtmlEscapeFormTxt(const std::string & in)
|
|
|
|
{
|
|
|
|
std::ostringstream out;
|
|
|
|
|
|
|
|
HtmlEscapeFormTxt(out, in);
|
|
|
|
|
|
|
|
return out.str();
|
|
|
|
}
|
|
|
|
|
2008-12-10 05:42:49 +01:00
|
|
|
|
2009-03-23 00:54:15 +01:00
|
|
|
const char * DateToStr(int year, int month, int day, int hour, int min, int sec)
|
2009-01-31 07:53:36 +01:00
|
|
|
{
|
|
|
|
static const char * month_letter[] = { "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII" };
|
|
|
|
static char buffer[100];
|
|
|
|
|
|
|
|
--month;
|
|
|
|
|
|
|
|
if( month < 0 )
|
|
|
|
month = 0;
|
|
|
|
|
|
|
|
if( month > 11 )
|
|
|
|
month = 11;
|
|
|
|
|
2009-03-23 00:54:15 +01:00
|
|
|
if( year == 0 )
|
|
|
|
sprintf(buffer, "%s.%02d %02d:%02d:%02d", month_letter[month], day, hour, min, sec);
|
|
|
|
else
|
|
|
|
sprintf(buffer, "%02d.%s.%02d %02d:%02d:%02d", year, month_letter[month], day, hour, min, sec);
|
2009-01-31 07:53:36 +01:00
|
|
|
|
|
|
|
// warning: not thread safe (we do not use threads)
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-16 03:59:42 +02:00
|
|
|
const char * DateToStr(tm * ptm)
|
|
|
|
{
|
2009-06-05 22:29:06 +02:00
|
|
|
return DateToStr(ptm->tm_year + 1900, ptm->tm_mon+1, ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
|
2009-04-16 03:59:42 +02:00
|
|
|
}
|
|
|
|
|
2008-12-21 22:17:09 +01:00
|
|
|
|
2009-03-23 00:54:15 +01:00
|
|
|
const char * DateToStr(time_t t)
|
|
|
|
{
|
|
|
|
tm * ptm = std::localtime(&t);
|
|
|
|
|
2009-04-16 03:59:42 +02:00
|
|
|
return DateToStr(ptm);
|
2009-03-23 00:54:15 +01:00
|
|
|
}
|
|
|
|
|
2009-04-16 03:59:42 +02:00
|
|
|
|
|
|
|
const char * IpToStr(unsigned int ip_)
|
|
|
|
{
|
|
|
|
static char buffer[100];
|
|
|
|
|
|
|
|
union
|
|
|
|
{
|
|
|
|
unsigned int ip;
|
|
|
|
unsigned char c[4];
|
|
|
|
} ip;
|
|
|
|
|
|
|
|
ip.ip = ip_;
|
|
|
|
|
|
|
|
sprintf(buffer, "%u.%u.%u.%u", (int)ip.c[0], (int)ip.c[1], (int)ip.c[2], (int)ip.c[3]);
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-18 02:58:38 +01:00
|
|
|
const char * ToStr(int value)
|
|
|
|
{
|
|
|
|
static char buffer[100];
|
|
|
|
|
|
|
|
sprintf(buffer, "%d", value);
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-05 22:29:06 +02:00
|
|
|
bool IsWhite(int s)
|
|
|
|
{
|
|
|
|
if( s==' ' || s=='\t' || s==13 )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-16 03:59:42 +02:00
|
|
|
|
2009-06-05 22:29:06 +02:00
|
|
|
void TrimWhite(std::string & s)
|
|
|
|
{
|
|
|
|
std::string::size_type i;
|
|
|
|
|
|
|
|
if( s.empty() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// looking for white characters at the end
|
|
|
|
for(i=s.size()-1 ; i>0 && IsWhite(s[i]) ; --i);
|
|
|
|
|
|
|
|
if( i==0 && IsWhite(s[i]) )
|
|
|
|
{
|
|
|
|
// the whole string has white characters
|
|
|
|
s.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// deleting white characters at the end
|
|
|
|
if( i != s.size() - 1 )
|
|
|
|
s.erase(i+1, std::string::npos);
|
|
|
|
|
|
|
|
// looking for white characters at the beginning
|
|
|
|
for(i=0 ; i<s.size() && IsWhite(s[i]) ; ++i);
|
|
|
|
|
|
|
|
// deleting white characters at the beginning
|
|
|
|
if( i != 0 )
|
|
|
|
s.erase(0, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-17 00:36:38 +01:00
|
|
|
const char * SkipWhite(const char * s)
|
|
|
|
{
|
|
|
|
while( IsWhite(*s) )
|
|
|
|
++s;
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-05 22:29:06 +02:00
|
|
|
void ToSmall(std::string & s)
|
|
|
|
{
|
|
|
|
std::string::size_type i;
|
|
|
|
|
|
|
|
for(i=0 ; i<s.size() ; ++i)
|
|
|
|
{
|
|
|
|
if( s[i]>='A' && s[i]<='Z' )
|
|
|
|
s[i] = s[i] - 'A' + 'a';
|
|
|
|
}
|
|
|
|
}
|
2009-10-01 00:31:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool ValidateEmail(const std::string & email)
|
|
|
|
{
|
|
|
|
if( email.empty() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool correct = true;
|
|
|
|
size_t i;
|
|
|
|
char allowed_chars[] = "!#$%&'*+-/=?^_`{|}~.@";
|
|
|
|
int at = 0;
|
|
|
|
|
|
|
|
for(i=0 ; i<email.length() && correct ; ++i)
|
|
|
|
{
|
|
|
|
correct = false;
|
|
|
|
|
|
|
|
if( (email[i] >= 'A' && email[i]<='Z') ||
|
|
|
|
(email[i] >= 'a' && email[i]<='z') ||
|
|
|
|
(email[i] >= '0' && email[i]<='9') )
|
|
|
|
{
|
|
|
|
correct = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(size_t a=0 ; a < sizeof(allowed_chars)-1 ; ++a)
|
|
|
|
{
|
|
|
|
if( email[i] == allowed_chars[a] )
|
|
|
|
{
|
|
|
|
correct = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( email[i] == '@' )
|
|
|
|
++at;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( at != 1 )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return correct;
|
|
|
|
}
|