2008-12-30 02:05:03 +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-30 02:05:03 +01:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "content.h"
|
2009-04-21 22:50:55 +02:00
|
|
|
#include "../core/request.h"
|
|
|
|
#include "../core/error.h"
|
|
|
|
#include "../core/db.h"
|
|
|
|
#include "../core/data.h"
|
2009-06-05 22:29:06 +02:00
|
|
|
#include "../core/misc.h"
|
2009-11-13 21:40:11 +01:00
|
|
|
#include "../core/plugin.h"
|
|
|
|
|
2008-12-30 02:05:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
bool Content::Init()
|
|
|
|
{
|
|
|
|
templates.Read();
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool Content::DirsHaveReadExecPerm()
|
|
|
|
{
|
|
|
|
std::vector<Item*>::iterator i;
|
|
|
|
|
|
|
|
for(i = request.dir_table.begin() ; i!=request.dir_table.end() ; ++i)
|
|
|
|
{
|
|
|
|
if( !request.HasReadExecAccess(**i) )
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-04-20 00:13:21 +02:00
|
|
|
void Content::SetDefaultFunctionForFile()
|
|
|
|
{
|
|
|
|
if( request.HasReadExecAccess(request.item) )
|
|
|
|
request.pfunction = data.functions.GetFunction(FUN_RUN);
|
|
|
|
else
|
|
|
|
request.pfunction = data.functions.GetFunction(FUN_CAT);
|
2008-12-30 02:05:03 +01:00
|
|
|
|
2009-04-20 00:13:21 +02:00
|
|
|
if( request.pfunction )
|
|
|
|
log << log3 << "Content: default function: " << request.pfunction->item.url << logend;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Content::SetDefaultFunctionForDir()
|
2008-12-30 02:05:03 +01:00
|
|
|
{
|
2009-06-07 04:40:27 +02:00
|
|
|
long default_item = request.dir_table.back()->default_item;
|
|
|
|
|
|
|
|
if( default_item != -1 )
|
|
|
|
{
|
|
|
|
request.session->IncrementTimersIfExist();
|
|
|
|
|
|
|
|
log << log3 << "Content: Default item: id: " << default_item << logend;
|
|
|
|
RedirectTo(default_item);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-05 22:29:06 +02:00
|
|
|
if( data.mounts.CurrentMountType() == Mount::thread )
|
2008-12-30 02:05:03 +01:00
|
|
|
{
|
2009-04-20 00:13:21 +02:00
|
|
|
request.pfunction = data.functions.GetFunction(FUN_THREAD);
|
2009-06-07 04:40:27 +02:00
|
|
|
|
2008-12-30 02:05:03 +01:00
|
|
|
if( request.pfunction )
|
|
|
|
log << log3 << "Content: default function: " << request.pfunction->item.url << logend;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-06-07 04:40:27 +02:00
|
|
|
// cms
|
|
|
|
request.pfunction = data.functions.GetFunction(FUN_LS);
|
|
|
|
|
|
|
|
if( request.pfunction )
|
|
|
|
log << log3 << "Content: default function: " << request.pfunction->item.url << logend;
|
|
|
|
}
|
2009-04-20 00:13:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Content::SetDefaultFunction()
|
|
|
|
{
|
|
|
|
if( request.is_item )
|
|
|
|
{
|
|
|
|
SetDefaultFunctionForFile();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetDefaultFunctionForDir();
|
2008-12-30 02:05:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Content::MakeStandardFunction()
|
|
|
|
{
|
|
|
|
if( !request.pfunction )
|
|
|
|
SetDefaultFunction();
|
|
|
|
|
|
|
|
if( request.result == Request::redirect )
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
if( !request.pfunction )
|
|
|
|
{
|
|
|
|
log << log1 << "Content: no function (neither cat nor ls)" << logend;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-26 21:49:28 +01:00
|
|
|
if( request.pfunction->code == FUN_LOGOUT )
|
2008-12-30 02:05:03 +01:00
|
|
|
FunLogout();
|
|
|
|
else
|
2009-01-26 21:49:28 +01:00
|
|
|
if( request.pfunction->code == FUN_CAT )
|
2008-12-30 02:05:03 +01:00
|
|
|
FunCat();
|
|
|
|
else
|
2009-01-26 21:49:28 +01:00
|
|
|
if( request.pfunction->code == FUN_LS )
|
2008-12-30 02:05:03 +01:00
|
|
|
FunLs();
|
|
|
|
else
|
2009-01-26 21:49:28 +01:00
|
|
|
if( request.pfunction->code == FUN_EMACS )
|
2008-12-30 02:05:03 +01:00
|
|
|
FunEmacs();
|
|
|
|
else
|
2009-01-26 21:49:28 +01:00
|
|
|
if( request.pfunction->code == FUN_MKDIR )
|
2008-12-31 14:36:46 +01:00
|
|
|
FunMkdir();
|
|
|
|
else
|
2009-01-26 21:49:28 +01:00
|
|
|
if( request.pfunction->code == FUN_DEFAULT )
|
|
|
|
FunDefault();
|
2008-12-31 19:28:12 +01:00
|
|
|
else
|
2009-01-26 21:49:28 +01:00
|
|
|
if( request.pfunction->code == FUN_PRIV )
|
|
|
|
FunPriv();
|
2008-12-30 02:05:03 +01:00
|
|
|
else
|
2009-01-26 21:49:28 +01:00
|
|
|
if( request.pfunction->code == FUN_RM )
|
2008-12-30 02:05:03 +01:00
|
|
|
FunRm();
|
|
|
|
else
|
2009-01-26 21:49:28 +01:00
|
|
|
if( request.pfunction->code == FUN_RUN )
|
|
|
|
FunRun();
|
|
|
|
else
|
|
|
|
if( request.pfunction->code == FUN_NODE )
|
2008-12-30 02:05:03 +01:00
|
|
|
FunNode();
|
2009-04-16 03:59:42 +02:00
|
|
|
else
|
|
|
|
if( request.pfunction->code == FUN_WHO )
|
|
|
|
FunWho();
|
|
|
|
else
|
|
|
|
if( request.pfunction->code == FUN_LAST )
|
|
|
|
FunLast();
|
2009-04-20 00:13:21 +02:00
|
|
|
else
|
|
|
|
if( request.pfunction->code == FUN_THREAD )
|
|
|
|
FunThread();
|
|
|
|
else
|
|
|
|
if( request.pfunction->code == FUN_CREATETHREAD )
|
|
|
|
FunCreateThread();
|
|
|
|
else
|
|
|
|
request.status = Error::permision_denied;
|
|
|
|
|
2008-12-30 02:05:03 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Content::MakePost()
|
|
|
|
{
|
2009-05-21 21:27:16 +02:00
|
|
|
if( !request.pfunction )
|
|
|
|
SetDefaultFunction();
|
|
|
|
|
2008-12-30 02:05:03 +01:00
|
|
|
if( !request.pfunction )
|
|
|
|
{
|
|
|
|
log << log1 << "Content: MakePost: no function" << logend;
|
|
|
|
request.status = Error::no_function;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
switch( request.pfunction->code )
|
|
|
|
{
|
2009-01-26 21:49:28 +01:00
|
|
|
case FUN_EMACS:
|
2008-12-30 02:05:03 +01:00
|
|
|
PostFunEmacs();
|
|
|
|
break;
|
|
|
|
|
2009-01-26 21:49:28 +01:00
|
|
|
case FUN_MKDIR:
|
2008-12-31 14:36:46 +01:00
|
|
|
PostFunMkdir();
|
|
|
|
break;
|
|
|
|
|
2009-01-26 21:49:28 +01:00
|
|
|
case FUN_DEFAULT:
|
|
|
|
PostFunDefault();
|
2008-12-31 19:28:12 +01:00
|
|
|
break;
|
|
|
|
|
2009-01-26 21:49:28 +01:00
|
|
|
case FUN_PRIV:
|
|
|
|
PostFunPriv();
|
2008-12-30 02:05:03 +01:00
|
|
|
break;
|
|
|
|
|
2009-01-26 21:49:28 +01:00
|
|
|
case FUN_LOGIN:
|
2008-12-30 02:05:03 +01:00
|
|
|
PostFunLogin();
|
|
|
|
break;
|
|
|
|
|
2009-04-20 00:13:21 +02:00
|
|
|
case FUN_CREATETHREAD:
|
|
|
|
PostFunCreateThread();
|
|
|
|
break;
|
|
|
|
|
2008-12-30 02:05:03 +01:00
|
|
|
default:
|
|
|
|
log << log1 << "Content: unknown post function" << logend;
|
2009-04-20 00:13:21 +02:00
|
|
|
// !! moze daj tutaj tez access denied?
|
2008-12-30 02:05:03 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Content::Make()
|
|
|
|
{
|
|
|
|
if( request.dir_table.empty() )
|
|
|
|
{
|
2009-02-06 14:12:03 +01:00
|
|
|
log << log1 << "Content: there is no a root dir (dir_table is empty)" << logend;
|
2008-12-30 02:05:03 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// request.status can be changed by function_parser
|
|
|
|
if( request.status == Error::ok )
|
|
|
|
{
|
|
|
|
if( DirsHaveReadExecPerm() )
|
|
|
|
{
|
2009-05-21 21:27:16 +02:00
|
|
|
|
2008-12-30 02:05:03 +01:00
|
|
|
if( request.method == Request::post )
|
|
|
|
MakePost();
|
|
|
|
|
|
|
|
if( request.result == Request::redirect )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if( request.status == Error::ok )
|
|
|
|
MakeStandardFunction();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
request.status = Error::permision_denied;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( request.result == Request::redirect )
|
|
|
|
return;
|
|
|
|
|
2009-02-06 14:12:03 +01:00
|
|
|
if( request.dir_table.empty() )
|
|
|
|
{
|
|
|
|
log << log1 << "Content: there is no a root dir (dir_table is empty -- after calling some standard functions)" << logend;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-11-13 21:40:11 +01:00
|
|
|
plugin.Call(CMSLU_CONTENT_MAKE);
|
2008-12-30 02:05:03 +01:00
|
|
|
templates.Generate();
|
|
|
|
|
|
|
|
//request.PrintGetTable();
|
|
|
|
//request.PrintEnv();
|
|
|
|
//request.PrintIn();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// !! mozna zrobic jakas obsluge kiedy nie mozemy sie redirectnac, np gdy wystapil blad
|
|
|
|
// !! moze zwracac jakas wartosc?
|
|
|
|
void Content::RedirectTo(const Item & item)
|
|
|
|
{
|
|
|
|
std::string path;
|
|
|
|
|
|
|
|
request.result = Request::redirect;
|
|
|
|
request.str = data.base_url;
|
|
|
|
|
|
|
|
|
|
|
|
if( item.type == Item::dir )
|
|
|
|
{
|
|
|
|
// item_id is pointing to a directory
|
|
|
|
data.dirs.MakePath(item.id, path);
|
|
|
|
request.str += path;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( !data.dirs.MakePath(item.parent_id, path) )
|
|
|
|
log << log1 << "Content: Can't redirect: no dirs for item id: " << item.id << logend;
|
|
|
|
|
|
|
|
request.str += path;
|
|
|
|
request.str += item.url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Content::RedirectTo(long item_id)
|
|
|
|
{
|
|
|
|
std::string path;
|
|
|
|
Item * pdir;
|
|
|
|
|
|
|
|
request.result = Request::redirect;
|
|
|
|
request.str = data.base_url;
|
|
|
|
pdir = data.dirs.GetDir(item_id);
|
|
|
|
|
|
|
|
|
|
|
|
if( pdir )
|
|
|
|
{
|
|
|
|
// item_id is pointing to a directory
|
|
|
|
data.dirs.MakePath(pdir->id, path);
|
|
|
|
request.str += path;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// !! zrobic nowy interfejs
|
|
|
|
// !! GetItem pozamieniac na GetFile
|
|
|
|
db.GetItem(request.item_table, item_id);
|
|
|
|
|
|
|
|
if( !request.item_table.empty() )
|
|
|
|
{
|
|
|
|
if( !data.dirs.MakePath(request.item_table[0].parent_id, path) )
|
|
|
|
log << log1 << "Content: Can't redirect: no dirs for item id: " << request.item_table[0].id << ", requested directory id: " << request.item_table[0].parent_id << logend;
|
|
|
|
|
|
|
|
request.str += path + request.item_table[0].url;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
log << log1 << "Content: Can't redirect: no such item: id: " << item_id << logend;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-21 01:49:28 +02:00
|
|
|
void Content::ReadAdditionalInfo()
|
|
|
|
{
|
|
|
|
if( request.dir_table.empty() )
|
|
|
|
return;
|
|
|
|
|
2009-06-05 22:29:06 +02:00
|
|
|
if( data.mounts.CurrentMountType() == Mount::thread )
|
2009-04-21 01:49:28 +02:00
|
|
|
{
|
2009-06-05 22:29:06 +02:00
|
|
|
if( db.GetThreadByDirId(request.dir_table.back()->id, request.thread) == Error::ok )
|
|
|
|
request.is_thread = true;
|
2009-04-21 01:49:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-30 02:05:03 +01:00
|
|
|
|
|
|
|
|
2009-06-05 22:29:06 +02:00
|
|
|
void Content::PrepareUrl(Item & item)
|
|
|
|
{
|
|
|
|
TrimWhite(item.url);
|
|
|
|
|
|
|
|
if( item.url.empty() )
|
|
|
|
item.url = item.subject; // if the subject is empty then the url will be corrected by CorrectUrl()
|
|
|
|
|
|
|
|
CorrectUrl(item);
|
|
|
|
|
|
|
|
if( data.functions.GetFunction(item.url) )
|
|
|
|
{
|
|
|
|
// the name provided by an user is the same as a name of a function
|
|
|
|
// we add one underscore character at the beginning
|
|
|
|
|
|
|
|
// names of functions should not begin with an underscore '_'
|
|
|
|
// and we can simply add one '_' at the beginning
|
|
|
|
// and the name will be unique
|
|
|
|
item.url.insert(item.url.begin(), '_');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-30 02:05:03 +01:00
|
|
|
|
|
|
|
|