added: parameter 'l' to 'ls' function

added: Db::ItemQuery struct for querying items
changed: some refactoring (renamed some config variables)




git-svn-id: svn://ttmath.org/publicrep/winix/trunk@589 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
2010-02-22 22:52:09 +00:00
parent 16e51cd4e5
commit 3702efc5be
29 changed files with 396 additions and 195 deletions

View File

@@ -109,9 +109,9 @@ void Config::AssignValues()
data.log_stdout = false;
data.post_file_max = Int("post_file_max", 8388608); // 8 MB
data.static_simplefs_dir = Text("static_simplefs_dir");
data.static_hashfs_dir = Text("static_hashfs_dir");
data.static_tmp_dir = Text("static_tmp_dir");
data.auth_simplefs_dir = Text("auth_simplefs_dir");
data.auth_hashfs_dir = Text("auth_hashfs_dir");
data.auth_tmp_dir = Text("auth_tmp_dir");
data.templates_dir = Text("templates_dir");
data.templates_dir_default = Text("templates_dir_default");
@@ -121,19 +121,17 @@ void Config::AssignValues()
data.db_pass = Text("db_pass");
data.item_url_empty = Text("item_url_empty");
data.base_server = Text("base_server");
data.base_url_prefix = Text("base_url_prefix");
data.base_url_static_prefix = Text("base_url_static_prefix");
data.base_url_static_ext_prefix = Text("base_url_static_ext_prefix");
data.base_url_static_auth_prefix = Text("base_url_static_auth_prefix");
data.base_server = Text("base_server");
data.base_url = Text("base_url");
data.base_url_auth = Text("base_url_auth");
data.base_url_static = Text("base_url_static");
data.base_url_common = Text("base_url_common");
NoLastSlash(data.base_server);
NoFirstHttp(data.base_server);
data.base_url = data.base_url_prefix + data.base_server;
data.base_url_static = data.base_url_static_prefix + data.base_server;
data.base_url_static_ext = data.base_url_static_ext_prefix + data.base_server;
data.base_url_static_auth = data.base_url_static_auth_prefix + data.base_server;
NoLastSlash(data.base_url);
NoLastSlash(data.base_url_auth);
NoLastSlash(data.base_url_static);
NoLastSlash(data.base_url_common);
data.priv_no_user = Text("priv_no_user", "-- no user --");
data.priv_no_group = Text("priv_no_group", "-- no group --");
@@ -247,12 +245,18 @@ void Config::NoLastSlash(std::string & s)
{
if( s.empty() )
return;
if( *(--s.end()) == '/' )
s.erase(--s.end());
size_t i = s.size();
for( ; i>0 && s[i-1]=='/' ; --i);
if( i < s.size() )
s.erase(i);
}
void Config::NoFirstHttp(std::string & s)
{
if( s.empty() )

View File

@@ -27,7 +27,7 @@ Data::Data()
void Data::SetAdditionalVariables()
{
SetHttpHost(base_url, base_url_http_host);
SetHttpHost(base_url_static_auth, base_url_static_auth_http_host);
SetHttpHost(base_url_auth, base_url_auth_http_host);
}

View File

@@ -2,7 +2,7 @@
* This file is a part of CMSLU -- Content Management System like Unix
* and is not publicly distributed
*
* Copyright (c) 2008-2009, Tomasz Sowa
* Copyright (c) 2008-2010, Tomasz Sowa
* All rights reserved.
*
*/
@@ -69,12 +69,6 @@ public:
std::string db_user;
std::string db_pass;
std::string base_server;
std::string base_url_prefix;
std::string base_url_static_prefix;
std::string base_url_static_ext_prefix;
std::string base_url_static_auth_prefix;
std::string http_session_id_name;
// when the HOST_HTTP environment variable doesn't point into 'base_url' (the part 'http://' and the last slash is removed)
@@ -113,12 +107,12 @@ public:
int post_file_max;
// directories for static files
std::string static_simplefs_dir;
std::string static_hashfs_dir;
std::string auth_simplefs_dir;
std::string auth_hashfs_dir;
// temporary directory for static content used by the upload function
// should be on the same partition as static_simplefs_dir and static_hashfs_dir
std::string static_tmp_dir;
// should be on the same partition as auth_simplefs_dir and auth_hashfs_dir
std::string auth_tmp_dir;
// default locale: en pl
std::string locale_str;
@@ -129,19 +123,20 @@ public:
// directory with default locale files (those from winix)
std::string locale_dir_default;
// below variables are based on the other config variables
// the main address of the server (e.g. someserver.com) (without the 'www' part etc)
std::string base_server;
// base_url_prefix + base_server
// the main address of the site (e.g. http://www.someserver.com)
std::string base_url;
// base_url_static_prefix + base_server
// static content authorized by winix
std::string base_url_auth;
// static content not authorized by winix
std::string base_url_static;
// additional static server for common content such as ckeditor
std::string base_url_static_ext;
// base_url_static_auth_prefix + base_server
std::string base_url_static_auth;
// additional static server for common content (not authorized)
std::string base_url_common;
// separator used in <title> html tag
std::string title_separator;
@@ -166,7 +161,7 @@ public:
// set by SetAdditionalVariables()
// without the first part http:// (or https://) or the whole string is empty
std::string base_url_http_host;
std::string base_url_static_auth_http_host;
std::string base_url_auth_http_host;

View File

@@ -421,7 +421,7 @@ Error Db::AddItemIntoItem(Item & item)
AssertConnection();
std::ostringstream query;
query << "insert into core.item (user_id, group_id, privileges, date_creation, date_modification, type, "
"parent_id, content_id, static_auth, default_item, subject, guest_name, url) values (";
"parent_id, content_id, auth, default_item, subject, guest_name, url) values (";
query << '\'' << item.user_id << "', ";
query << '\'' << item.group_id << "', ";
query << '\'' << item.privileges << "', ";
@@ -430,7 +430,7 @@ Error Db::AddItemIntoItem(Item & item)
query << '\'' << static_cast<int>(item.type) << "', ";
query << '\'' << item.parent_id << "', ";
query << '\'' << item.content_id << "', ";
query << '\'' << static_cast<int>(item.static_auth) << "', ";
query << '\'' << static_cast<int>(item.auth) << "', ";
query << '\'' << item.default_item << "', ";
query << '\'' << Escape(item.subject) << "', ";
query << '\'' << Escape(item.guest_name) << "', ";
@@ -527,7 +527,7 @@ Error Db::EditItemInItem(Item & item, bool with_url)
AssertConnection();
std::ostringstream query;
query << "update core.item set (user_id, group_id, privileges, date_creation, date_modification, type, "
"default_item, parent_id, subject, guest_name, static_auth";
"default_item, parent_id, subject, guest_name, auth";
if( with_url )
query << ", url";
@@ -543,7 +543,7 @@ Error Db::EditItemInItem(Item & item, bool with_url)
query << '\'' << item.parent_id << "', ";
query << '\'' << Escape(item.subject) << "', ";
query << '\'' << Escape(item.guest_name) << "', ";
query << '\'' << static_cast<int>(item.static_auth) << "' ";
query << '\'' << static_cast<int>(item.auth) << "' ";
if( with_url )
{
@@ -752,49 +752,56 @@ return result;
PGresult * Db::GetItemsQuery(long parent_id, Item::Type type, bool with_subject, bool with_content, bool sort_asc)
PGresult * Db::GetItemsQuery(const ItemQuery & iq)
{
std::ostringstream query;
query << "select item.id, user_id, group_id, privileges, date_creation, date_modification, url, type, parent_id, "
"content_id, default_item, guest_name, static_auth";
query << "select item.id";
if( type != Item::dir )
{
if( with_subject )
query << ", subject";
if( with_content )
query << ", content, content_type";
}
if( iq.sel_parent_id ) query << " ,parent_id";
if( iq.sel_user_id ) query << " ,user_id";
if( iq.sel_group_id ) query << " ,group_id";
if( iq.sel_guest_name) query << " ,guest_name";
if( iq.sel_privileges ) query << " ,privileges";
if( iq.sel_date ) query << " ,date_creation, date_modification";
if( iq.sel_subject ) query << " ,subject";
if( iq.sel_content ) query << " ,content, content_type, content_id";
if( iq.sel_url ) query << " ,url";
if( iq.sel_type ) query << " ,type";
if( iq.sel_default_item ) query << " ,default_item";
if( iq.sel_auth ) query << " ,auth";
query << " from core.item";
if( type != Item::dir && with_content )
query << " left join core.content on item.content_id = content.id";
query << " where parent_id='" << parent_id << "'";
if( iq.sel_content ) query << " left join core.content on item.content_id = content.id";
if( iq.where_id || iq.where_parent_id || iq.where_type || iq.where_auth )
{
query << " where ";
const char * add_and = " and ";
const char * if_and = "";
if( iq.where_id ) { query << if_and << "id='" << iq.id << "'" ; if_and = add_and; }
if( iq.where_parent_id ) { query << if_and << "parent_id='" << iq.parent_id << "'" ; if_and = add_and; }
if( iq.where_type ) { query << if_and << "type='" << static_cast<int>(iq.type) << "'" ; if_and = add_and; }
if( iq.where_auth ) { query << if_and << "auth='" << static_cast<int>(iq.auth) << "'" ; if_and = add_and; }
}
if( type == Item::dir )
query << " and type='0'";
if( type == Item::file )
query << " and type='1'";
query << " order by item.date_creation";
if( sort_asc )
query << " asc";
if( iq.sort_asc )
query << " asc;";
else
query << " desc";
query << " desc;";
query << ';';
return AssertQuery(query.str());
}
void Db::GetItems(std::vector<Item> & item_table, long parent_id, Item::Type type, bool with_subject, bool with_content, bool sort_asc)
void Db::GetItems(std::vector<Item> & item_table, const ItemQuery & item_query)
{
item_table.clear();
PGresult * r = 0;
@@ -805,7 +812,7 @@ void Db::GetItems(std::vector<Item> & item_table, long parent_id, Item::Type typ
{
AssertConnection();
r = GetItemsQuery(parent_id, type, with_subject, with_content, sort_asc);
r = GetItemsQuery(item_query);
AssertResultStatus(r, PGRES_TUPLES_OK);
Item item;

View File

@@ -49,7 +49,79 @@ public:
Error EditItemByUrl(Item & item, bool with_url = true);
void CheckAllUrlSubject();
void GetItems(std::vector<Item> & item_table, long parent_id, Item::Type type, bool with_subject, bool with_content, bool sort_asc);
struct ItemQuery
{
// id is selected always
bool sel_parent_id; // parent_id
bool sel_user_id; // user_id
bool sel_group_id; // group_id
bool sel_guest_name; // guest_name
bool sel_privileges; // privileges
bool sel_date; // date_creation, date_modification
bool sel_subject; // subject
bool sel_content; // content, content_type, (content_id)
bool sel_url; // url
bool sel_type; // type (dir, file, none)
bool sel_default_item; // default_item
bool sel_auth; // auth
bool where_id; //
bool where_parent_id; //
bool where_type;
bool where_auth;
long id; // if where_id is true
long parent_id; // if where_parent_id is true
Item::Type type;
Item::Auth auth;
bool sort_asc;
void SetAll(bool sel, bool where_)
{
sel_parent_id = sel;
sel_user_id = sel;
sel_group_id = sel;
sel_guest_name = sel;
sel_privileges = sel;
sel_date = sel;
sel_subject = sel;
sel_content = sel;
sel_url = sel;
sel_type = sel;
sel_default_item= sel;
sel_auth = sel;
where_id = where_;
where_parent_id = where_;
where_type = where_;
where_auth = where_;
}
void WhereId(long id_) { where_id = true; id = id_; }
void WhereParentId(long parent_id_) { where_parent_id = true; parent_id = parent_id_; }
void WhereType(Item::Type type_) { where_type = true; type = type_; }
void WhereAuth(Item::Auth st) { where_auth = true; auth = st; }
ItemQuery()
{
sort_asc = true;
SetAll(true, false);
id = -1;
parent_id = -1;
type = Item::none;
auth = Item::auth_none;
}
};
void GetItems(std::vector<Item> & item_table, const ItemQuery & item_query);
// !! pobiera tylko jeden item (cos wymyslec innego z nazwa albo argumentem)
void GetItem(std::vector<Item> & item_table, long id);
@@ -131,8 +203,8 @@ protected:
void CheckAllUrlSubjectModifyItem(Item & item);
PGresult * GetItemsQuery(long parent_id, Item::Type type, bool with_subject, bool with_content, bool sort_asc);
PGresult * GetItemsQuery(const ItemQuery & iq);
bool DelItemDelItem(const Item & item);
void DelItemDelContent(const Item & item);
Error DelItemCountContents(const Item & item, long & contents);
@@ -141,7 +213,7 @@ protected:
struct ItemColumns
{
int id, user_id, group_id, privileges, date_creation, date_modification, url, type, parent_id,
content_id, default_item, subject, content, content_type, guest_name, static_auth;
content_id, default_item, subject, content, content_type, guest_name, auth;
void SetColumns(PGresult * r);
void SetItem(PGresult * r, long row, Item & item);

View File

@@ -29,7 +29,7 @@ void Db::ItemColumns::SetColumns(PGresult * r)
content = PQfnumber(r, "content");
content_type = PQfnumber(r, "content_type");
guest_name = PQfnumber(r, "guest_name");
static_auth = PQfnumber(r, "static_auth");
auth = PQfnumber(r, "auth");
}
@@ -51,7 +51,7 @@ void Db::ItemColumns::SetItem(PGresult * r, long row, Item & item)
if( content != -1 ) item.content = Db::AssertValue(r, row, content);
if( content_type != -1 ) item.content_type = static_cast<Item::ContentType>( atoi(Db::AssertValue(r, row, content_type)) );
if( guest_name != -1 ) item.guest_name = Db::AssertValue(r, row, guest_name);
if( static_auth != -1 ) item.static_auth = static_cast<Item::StaticAuth>( atoi(Db::AssertValue(r, row, static_auth)) );
if( auth != -1 ) item.auth = static_cast<Item::Auth>( atoi(Db::AssertValue(r, row, auth)) );
}

View File

@@ -80,7 +80,7 @@ void FunctionParser::ParseItem()
if( request.status == Error::ok )
{
if( request.role == Request::authorizer && request.item.static_auth == Item::static_none )
if( request.role == Request::authorizer && request.item.auth == Item::auth_none )
{
log << log1 << "FP: item.url: " << url << " exists but has not a static content (authorizer role)" << logend;
request.status = Error::no_item;

View File

@@ -18,20 +18,21 @@
struct Item
{
long id;
long parent_id;
long user_id;
long group_id;
int privileges;
std::string guest_name; // used as a user name when user_id is equal -1
// used as a user name when user_id is equal -1
std::string guest_name;
int privileges;
tm date_creation;
tm date_modification;
std::string subject;
std::string content;
long content_id; // used by the database
std::string url;
enum ContentType
@@ -52,36 +53,34 @@ enum Type
dir = 0,
file = 1,
none = 1000
none = 1000 // !! pozbyc sie tego
};
Type type;
//item_type;
long parent_id;
long default_item;
// used by the database
long content_id;
// external static file (saved in file system)
// the direct url is the same but the prefix is: base_url_static_auth
enum StaticAuth
// external static file authorized by winix
enum Auth
{
static_none = 0,
static_image = 1, /* png, gif, jpg - only types available to render by a web browser*/
static_document = 2, /* pdf doc xls txt */
static_other = 3
auth_none = 0, /* there is not an external file */
auth_image = 1, /* png, gif, jpg - only types available to render by a web browser*/
auth_document = 2, /* pdf doc xls txt */
auth_other = 3 /* other file */
};
StaticAuth static_auth;
Auth auth;
// methods
Item()
{
Clear();
@@ -125,7 +124,7 @@ void Clear()
content_id = -1;
static_auth = static_none;
auth = auth_none;
SetDateToNow();
}

View File

@@ -635,7 +635,7 @@ return name + i + 1;
}
Item::StaticAuth SelectFileType(const char * file_name)
Item::Auth SelectFileType(const char * file_name)
{
const char * ext = GetFileExt(file_name);
@@ -644,7 +644,7 @@ Item::StaticAuth SelectFileType(const char * file_name)
if( EqualNoCase(ext, "jpg") ||
EqualNoCase(ext, "gif") ||
EqualNoCase(ext, "png") )
return Item::static_image;
return Item::auth_image;
if( EqualNoCase(ext, "pdf") ||
EqualNoCase(ext, "doc") ||
@@ -652,8 +652,8 @@ Item::StaticAuth SelectFileType(const char * file_name)
EqualNoCase(ext, "txt") ||
EqualNoCase(ext, "ods") ||
EqualNoCase(ext, "odt") )
return Item::static_document;
return Item::auth_document;
return Item::static_other;
return Item::auth_other;
}

View File

@@ -68,6 +68,6 @@ bool CreateDir(const char * dir, int priv);
bool CreateDir(const std::string & dir, int priv);
const char * GetFileExt(const char * name);
Item::StaticAuth SelectFileType(const char * file_name);
Item::Auth SelectFileType(const char * file_name);
#endif

View File

@@ -433,14 +433,14 @@ void PostMultiParser::CreateTmpFile()
{
char buf[100];
if( data.static_tmp_dir.empty() )
if( data.auth_tmp_dir.empty() )
{
log << log1 << "PMP: static_tmp_dir is not set in the config" << logend;
log << log1 << "PMP: auth_tmp_dir is not set in the config" << logend;
err = Error::cant_create_file;
return;
}
sprintf(buf, "%s/winix_%u_%d_%u", data.static_tmp_dir.c_str(), (unsigned)getpid(), tmp_filename_postfix, rand());
sprintf(buf, "%s/winix_%u_%d_%u", data.auth_tmp_dir.c_str(), (unsigned)getpid(), tmp_filename_postfix, rand());
tmp_filename_postfix += 1;
tmp_file.open(buf, std::ios_base::binary | std::ios_base::out);

View File

@@ -519,7 +519,7 @@ bool compressing = data.compression && role == responder && redirect_to.empty()
// if there is a redirect or a file to send then we do not send a content
return;
if( header == h_200 && role == authorizer && is_item && item.static_auth != Item::static_none )
if( header == h_200 && role == authorizer && is_item && item.auth != Item::auth_none )
// if there is an item and the item has 'file' storage we do not send a content
return;
@@ -867,21 +867,21 @@ bool Request::CanUseUpload(const Item & item, bool check_root)
if( !data.mounts.pmount )
return false;
if( data.mounts.pmount->fs == Mount::simplefs && data.static_simplefs_dir.empty() )
if( data.mounts.pmount->fs == Mount::simplefs && data.auth_simplefs_dir.empty() )
{
log << log1 << "Request: can't use upload function, static_simplefs_dir must be set in the config file" << logend;
log << log1 << "Request: can't use upload function, auth_simplefs_dir must be set in the config file" << logend;
return false;
}
if( data.mounts.pmount->fs == Mount::hashfs && data.static_hashfs_dir.empty() )
if( data.mounts.pmount->fs == Mount::hashfs && data.auth_hashfs_dir.empty() )
{
log << log1 << "Request: can't use upload function, static_hashfs_dir must be set in the config file" << logend;
log << log1 << "Request: can't use upload function, auth_hashfs_dir must be set in the config file" << logend;
return false;
}
if( data.static_tmp_dir.empty() )
if( data.auth_tmp_dir.empty() )
{
log << log1 << "Request: can't use upload function, static_tmp_dir must be set in the config file" << logend;
log << log1 << "Request: can't use upload function, auth_tmp_dir must be set in the config file" << logend;
return false;
}
@@ -943,11 +943,11 @@ bool Request::MakePathSimpleFs(std::string & path, bool create_dir)
{
size_t i;
path = data.static_simplefs_dir;
path = data.auth_simplefs_dir;
if( path.empty() )
{
log << log1 << "Request: static_simplefs_dir is not set in the config file" << logend;
log << log1 << "Request: auth_simplefs_dir is not set in the config file" << logend;
return false;
}
@@ -979,10 +979,10 @@ char * hash = buffer;
buffer[0] = '0';
sprintf(buffer+1, "%lx", (unsigned long)id);
path = data.static_hashfs_dir;
path = data.auth_hashfs_dir;
if( path.empty() )
{
log << log1 << "Request: static_hashfs_dir is not set in the config file" << logend;
log << log1 << "Request: auth_hashfs_dir is not set in the config file" << logend;
return false;
}

View File

@@ -161,13 +161,13 @@ bool RequestController::BaseUrlRedirect()
{
// authorizer
if( data.base_url_static_auth_http_host.empty() )
if( data.base_url_auth_http_host.empty() )
return false;
if( data.base_url_static_auth_http_host == request.env_http_host )
if( data.base_url_auth_http_host == request.env_http_host )
return false;
request.redirect_to = data.base_url_static_auth + request.env_request_uri;
request.redirect_to = data.base_url_auth + request.env_request_uri;
}
log << log3 << "RC: BaseUrlRedirect from: " << request.env_http_host << logend;