2008-12-10 05:42:49 +01:00
|
|
|
/*
|
2010-02-28 01:08:10 +01:00
|
|
|
* This file is a part of Winix
|
2008-12-10 05:42:49 +01:00
|
|
|
* and is not publicly distributed
|
|
|
|
*
|
2010-02-28 22:33:06 +01:00
|
|
|
* Copyright (c) 2008-2010, Tomasz Sowa
|
2008-12-10 05:42:49 +01:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-01-28 16:39:01 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2008-12-10 05:42:49 +01:00
|
|
|
#include "misc.h"
|
2009-04-21 22:50:55 +02:00
|
|
|
#include "log.h"
|
2009-12-30 21:46:12 +01:00
|
|
|
#include "data.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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-13 00:16:13 +01:00
|
|
|
|
|
|
|
// this function checks how many dots there are in the url
|
|
|
|
// if there are more than one (last) dot then the first dots will be changed into '_'
|
2009-12-30 21:46:12 +01:00
|
|
|
void CorrectUrlDots(std::string & url)
|
|
|
|
{
|
|
|
|
size_t i = url.size();
|
|
|
|
bool was_dot = false;
|
|
|
|
|
|
|
|
while( i-- > 0 )
|
|
|
|
{
|
|
|
|
if( url[i] == '.' )
|
|
|
|
{
|
|
|
|
if( was_dot )
|
|
|
|
// only one dot is allowed
|
|
|
|
url[i] = '_';
|
|
|
|
|
|
|
|
was_dot = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CorrectUrlChars(std::string & url)
|
2008-12-10 05:42:49 +01:00
|
|
|
{
|
2008-12-30 02:05:03 +01:00
|
|
|
std::string::iterator i;
|
2009-12-30 21:46:12 +01:00
|
|
|
|
2010-02-13 00:16:13 +01:00
|
|
|
for(i=url.begin(); i != url.end() ; ++i)
|
2008-12-30 02:05:03 +01:00
|
|
|
{
|
|
|
|
if( !CorrectUrlChar(*i) )
|
|
|
|
{
|
|
|
|
int c = ChangeLocalChar(*i);
|
|
|
|
|
|
|
|
if( CorrectUrlChar(c) )
|
|
|
|
*i = c;
|
|
|
|
else
|
|
|
|
*i = '_';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-30 21:46:12 +01:00
|
|
|
void CorrectUrlOnlyAllowedChar(std::string & url)
|
2008-12-30 02:05:03 +01:00
|
|
|
{
|
2009-12-30 21:46:12 +01:00
|
|
|
CorrectUrlDots(url);
|
|
|
|
CorrectUrlChars(url);
|
|
|
|
ToSmall(url);
|
|
|
|
Trim(url, '_');
|
2008-12-30 02:05:03 +01:00
|
|
|
|
2009-12-30 21:46:12 +01:00
|
|
|
if( url.empty() || url == "." )
|
2008-12-10 05:42:49 +01:00
|
|
|
{
|
2009-12-30 21:46:12 +01:00
|
|
|
if( data.item_url_empty.empty() )
|
|
|
|
url = "unnamed";
|
|
|
|
else
|
|
|
|
{
|
|
|
|
url = data.item_url_empty;
|
|
|
|
|
|
|
|
CorrectUrlDots(url);
|
|
|
|
CorrectUrlChars(url);
|
|
|
|
ToSmall(url);
|
|
|
|
// we don't trim here and the string will not be empty
|
|
|
|
}
|
2008-12-10 05:42:49 +01:00
|
|
|
}
|
|
|
|
|
2009-12-30 21:46:12 +01:00
|
|
|
if( url[0] >= '0' && url[0] <= '9' )
|
2008-12-14 06:28:28 +01:00
|
|
|
// url must begin with a letter
|
2009-12-30 21:46:12 +01:00
|
|
|
url.insert(url.begin(), '_');
|
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
|
|
|
|
|
2010-02-13 00:16:13 +01:00
|
|
|
if( in.empty() )
|
|
|
|
return;
|
|
|
|
|
2009-06-05 22:29:06 +02:00
|
|
|
out << "<p>";
|
|
|
|
|
2010-01-11 15:47:52 +01:00
|
|
|
// skipping first new line characters
|
|
|
|
for(i = in.begin() ; i != in.end() && (*i==13 || *i==10) ; ++i);
|
|
|
|
|
|
|
|
for( ; i != in.end() ; ++i )
|
2009-06-05 22:29:06 +02:00
|
|
|
{
|
2010-01-11 15:47:52 +01:00
|
|
|
if( *i == 13 ) // skipping stupid characters (\r\n\ in dos mode)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if( *i == 10 )
|
|
|
|
{
|
|
|
|
++was_enter;
|
|
|
|
}
|
|
|
|
else
|
2009-06-05 22:29:06 +02:00
|
|
|
{
|
2010-01-11 15:47:52 +01:00
|
|
|
if( was_enter == 1 )
|
|
|
|
out << "<br>\n";
|
2009-06-05 22:29:06 +02:00
|
|
|
else
|
2010-01-11 15:47:52 +01:00
|
|
|
if( was_enter > 1 )
|
|
|
|
out << "</p>\n<p>";
|
|
|
|
|
|
|
|
was_enter = 0;
|
2009-06-05 22:29:06 +02:00
|
|
|
}
|
2010-01-11 15:47:52 +01:00
|
|
|
|
|
|
|
if( !HtmlTryChar(out, *i) )
|
|
|
|
out << *i;
|
2009-06-05 22:29:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2010-02-15 01:31:14 +01:00
|
|
|
const char * DateToStr(int year, int month, int day)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
if( year == 0 )
|
|
|
|
sprintf(buffer, "%s %02d", month_letter[month], day);
|
|
|
|
else
|
|
|
|
sprintf(buffer, "%02d %s %02d", year, month_letter[month], day);
|
|
|
|
|
|
|
|
// warning: not thread safe (we do not use threads)
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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 )
|
2009-11-21 00:09:52 +01:00
|
|
|
month = 0;
|
2009-01-31 07:53:36 +01:00
|
|
|
|
|
|
|
if( month > 11 )
|
|
|
|
month = 11;
|
|
|
|
|
2009-03-23 00:54:15 +01:00
|
|
|
if( year == 0 )
|
2009-11-21 00:09:52 +01:00
|
|
|
sprintf(buffer, "%s %02d %02d:%02d:%02d", month_letter[month], day, hour, min, sec);
|
2009-03-23 00:54:15 +01:00
|
|
|
else
|
2009-11-21 00:09:52 +01:00
|
|
|
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
|
|
|
|
2010-02-11 21:56:52 +01:00
|
|
|
|
2009-11-21 00:09:52 +01:00
|
|
|
// this format is used with cookies
|
|
|
|
const char * DateToStrCookie(int year, int month, int day, int hour, int min, int sec)
|
|
|
|
{
|
|
|
|
static const char * month_str[]={ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
|
|
|
|
static char buffer[100];
|
|
|
|
|
|
|
|
--month;
|
|
|
|
|
|
|
|
if( month < 0 )
|
|
|
|
month = 0;
|
|
|
|
|
|
|
|
if( month > 11 )
|
|
|
|
month = 11;
|
|
|
|
|
|
|
|
sprintf(buffer, "%02d-%s-%04d %02d:%02d:%02d", day, month_str[month], year, hour, min, sec);
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char * DateToStrCookie(tm * ptm)
|
|
|
|
{
|
|
|
|
return DateToStrCookie(ptm->tm_year + 1900, ptm->tm_mon+1, ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char * DateToStrCookie(time_t t)
|
|
|
|
{
|
|
|
|
tm * ptm = std::localtime(&t);
|
|
|
|
|
|
|
|
return DateToStrCookie(ptm);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-12-30 21:46:12 +01:00
|
|
|
void Trim(std::string & s, char c)
|
|
|
|
{
|
|
|
|
std::string::size_type i;
|
|
|
|
|
|
|
|
if( s.empty() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// looking for the 'c' characters at the end
|
|
|
|
for(i=s.size()-1 ; i>0 && s[i]==c ; --i);
|
|
|
|
|
|
|
|
if( i==0 && s[i]==c )
|
|
|
|
{
|
|
|
|
// the whole string has the 'c' characters
|
|
|
|
s.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// deleting 'c' characters at the end
|
|
|
|
if( i != s.size() - 1 )
|
|
|
|
s.erase(i+1, std::string::npos);
|
|
|
|
|
|
|
|
// looking for the 'c' characters at the beginning
|
|
|
|
for(i=0 ; i<s.size() && s[i]==c ; ++i);
|
|
|
|
|
|
|
|
// deleting the 'c' 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-12-30 21:46:12 +01:00
|
|
|
int ToSmall(int c)
|
|
|
|
{
|
|
|
|
if( c>='A' && c<='Z' )
|
|
|
|
c = c - 'A' + 'a';
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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)
|
2009-12-30 21:46:12 +01:00
|
|
|
s[i] = ToSmall(s[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool IsSubString(const char * short_str, const char * long_str)
|
|
|
|
{
|
|
|
|
while( *short_str && *long_str && *short_str == *long_str )
|
2009-06-05 22:29:06 +02:00
|
|
|
{
|
2009-12-30 21:46:12 +01:00
|
|
|
++short_str;
|
|
|
|
++long_str;
|
2009-06-05 22:29:06 +02:00
|
|
|
}
|
2009-12-30 21:46:12 +01:00
|
|
|
|
|
|
|
if( *short_str == 0 )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
2009-06-05 22:29:06 +02:00
|
|
|
}
|
2009-10-01 00:31:20 +02:00
|
|
|
|
|
|
|
|
2009-12-30 21:46:12 +01:00
|
|
|
bool IsSubStringNoCase(const char * short_str, const char * long_str)
|
|
|
|
{
|
|
|
|
while( *short_str && *long_str && ToSmall(*short_str) == ToSmall(*long_str) )
|
|
|
|
{
|
|
|
|
++short_str;
|
|
|
|
++long_str;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( *short_str == 0 )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-10-01 00:31:20 +02:00
|
|
|
|
2010-02-13 00:16:13 +01:00
|
|
|
bool EqualNoCase(const char * str1, const char * str2)
|
2010-01-07 15:45:00 +01:00
|
|
|
{
|
2010-02-13 00:16:13 +01:00
|
|
|
while( *str1 && *str2 && ToSmall(*str1) == ToSmall(*str2) )
|
2010-01-07 15:45:00 +01:00
|
|
|
{
|
2010-02-13 00:16:13 +01:00
|
|
|
++str1;
|
|
|
|
++str2;
|
2010-01-07 15:45:00 +01:00
|
|
|
}
|
|
|
|
|
2010-02-13 00:16:13 +01:00
|
|
|
if( *str1 == 0 && *str2 == 0 )
|
2010-01-07 15:45:00 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2010-01-28 16:39:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool IsFile(const char * file)
|
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
|
|
|
|
return (stat(file, &sb) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsFile(const std::string & file)
|
|
|
|
{
|
|
|
|
return IsFile(file.c_str());
|
|
|
|
}
|
2010-02-13 00:16:13 +01:00
|
|
|
|
|
|
|
|
2010-02-19 00:30:22 +01:00
|
|
|
bool CreateDir(const char * dir, int priv)
|
|
|
|
{
|
|
|
|
if( !IsFile(dir) )
|
|
|
|
{
|
|
|
|
if( mkdir(dir, priv) < 0 )
|
|
|
|
{
|
|
|
|
log << log1 << "Can't create a directory on fs: " << dir << logend;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool CreateDir(const std::string & dir, int priv)
|
|
|
|
{
|
|
|
|
return CreateDir(dir.c_str(), priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-03-15 02:47:26 +01:00
|
|
|
// creating directories (can be more than one)
|
|
|
|
// 'dirs' can begin with a slash (will be skipped)
|
|
|
|
bool CreateDirs(const char * base_dir, const char * dirs, int priv)
|
|
|
|
{
|
|
|
|
static std::string temp;
|
|
|
|
const char * p = dirs;
|
|
|
|
|
|
|
|
temp = base_dir; // we start creating from 'base_dir'
|
|
|
|
|
|
|
|
if( temp.empty() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( temp[temp.size()-1] != '/' )
|
|
|
|
temp += '/';
|
|
|
|
|
|
|
|
while( true )
|
|
|
|
{
|
|
|
|
// skipping slashes
|
|
|
|
for( ; *p=='/' ; ++p );
|
|
|
|
|
|
|
|
if( *p == 0 )
|
|
|
|
break;
|
|
|
|
|
|
|
|
// taking the name
|
|
|
|
for( ; *p && *p!='/' ; ++p )
|
|
|
|
temp += *p;
|
|
|
|
|
|
|
|
if( !CreateDir(temp.c_str(), priv) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
temp += '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool CreateDirs(const std::string & base_dir, const std::string & dirs, int priv)
|
|
|
|
{
|
|
|
|
return CreateDirs(base_dir.c_str(), dirs.c_str(), priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-06-03 19:38:18 +02:00
|
|
|
bool CopyFile(FILE * in, FILE * out)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
size_t buflen = sizeof(buf)/sizeof(char);
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
len = fread(buf, 1, buflen, in);
|
|
|
|
|
|
|
|
if( len > 0 )
|
|
|
|
fwrite(buf, 1, len, out);
|
|
|
|
|
|
|
|
if( ferror(in) || ferror(out) )
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
while( !feof(in) );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool CopyFile(const char * src, const char * dst)
|
|
|
|
{
|
|
|
|
FILE * in, * out;
|
|
|
|
|
|
|
|
in = fopen(src, "rb");
|
|
|
|
|
|
|
|
if( !in )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
out = fopen(dst, "wb");
|
|
|
|
|
|
|
|
if( !out )
|
|
|
|
{
|
|
|
|
fclose(in);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool res = CopyFile(in, out);
|
|
|
|
|
|
|
|
fclose(in);
|
|
|
|
fclose(out);
|
2010-03-15 02:47:26 +01:00
|
|
|
|
2010-06-03 19:38:18 +02:00
|
|
|
if( res && ferror(out) )
|
|
|
|
res = false;
|
|
|
|
|
|
|
|
if( !res )
|
|
|
|
remove(dst);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool CopyFile(const std::string & src, const std::string & dst)
|
|
|
|
{
|
|
|
|
return CopyFile(src.c_str(), dst.c_str());
|
|
|
|
}
|
2010-03-15 02:47:26 +01:00
|
|
|
|
|
|
|
|
2010-02-13 00:16:13 +01:00
|
|
|
// if there is not an extension it returns a pointer to the last '\0' character
|
|
|
|
const char * GetFileExt(const char * name)
|
|
|
|
{
|
|
|
|
size_t i, ilast;
|
|
|
|
|
|
|
|
// looking for the end of the name
|
|
|
|
for(i=0 ; name[i] != 0 ; ++i);
|
|
|
|
|
|
|
|
if( i == 0 )
|
|
|
|
return name; // ops, the name is empty
|
|
|
|
|
|
|
|
// remember the end of the string
|
|
|
|
ilast = i;
|
|
|
|
|
|
|
|
// looking for the last dot
|
|
|
|
for(--i ; i>0 && name[i] != '.' ; --i);
|
|
|
|
|
|
|
|
if( name[i] != '.' )
|
|
|
|
return name + ilast; // ops, there is not a dot
|
|
|
|
|
|
|
|
// the extensions starts from i+1
|
|
|
|
// and can be empty (if the last character is a dot)
|
|
|
|
|
|
|
|
return name + i + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-22 23:52:09 +01:00
|
|
|
Item::Auth SelectFileType(const char * file_name)
|
2010-02-13 00:16:13 +01:00
|
|
|
{
|
|
|
|
const char * ext = GetFileExt(file_name);
|
|
|
|
|
|
|
|
// as an image we're using only those types which can be rendered
|
|
|
|
// by a web browser
|
|
|
|
if( EqualNoCase(ext, "jpg") ||
|
2010-03-15 02:47:26 +01:00
|
|
|
EqualNoCase(ext, "jpeg") ||
|
|
|
|
EqualNoCase(ext, "jpe") ||
|
|
|
|
EqualNoCase(ext, "pic") ||
|
|
|
|
EqualNoCase(ext, "tga") ||
|
2010-02-13 00:16:13 +01:00
|
|
|
EqualNoCase(ext, "gif") ||
|
2010-03-15 02:47:26 +01:00
|
|
|
EqualNoCase(ext, "bmp") ||
|
2010-02-13 00:16:13 +01:00
|
|
|
EqualNoCase(ext, "png") )
|
2010-02-22 23:52:09 +01:00
|
|
|
return Item::auth_image;
|
2010-02-13 00:16:13 +01:00
|
|
|
|
|
|
|
if( EqualNoCase(ext, "pdf") ||
|
|
|
|
EqualNoCase(ext, "doc") ||
|
|
|
|
EqualNoCase(ext, "xls") ||
|
|
|
|
EqualNoCase(ext, "txt") ||
|
|
|
|
EqualNoCase(ext, "ods") ||
|
|
|
|
EqualNoCase(ext, "odt") )
|
2010-02-22 23:52:09 +01:00
|
|
|
return Item::auth_document;
|
2010-02-13 00:16:13 +01:00
|
|
|
|
2010-02-22 23:52:09 +01:00
|
|
|
return Item::auth_other;
|
2010-02-13 00:16:13 +01:00
|
|
|
}
|
|
|
|
|