00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef File_Pick_hpp
00012 #define File_Pick_hpp
00013
00014 #include "cgiTemplates.h"
00015
00016 #include "ocFileSys.h"
00017
00018
00019 #include "openLogin.h"
00020 #include "openLogger.h"
00021
00022 class datesort_ocDirEntry
00023 {
00024 public:
00025 bool operator ( ) ( const ocDirEntry & d1, const ocDirEntry & d2 )
00026 {
00027 return d1.last_mod > d2.last_mod;
00028 }
00029 };
00030 datesort_ocDirEntry datesort;
00031
00032 class filePicker
00033 {
00034 private:
00035 string m_fileBase;
00036
00037 string m_fileDir;
00038
00039 string m_domain;
00040 string m_urlDir;
00041 string m_exe_name;
00042
00043 string m_fileUploaded;
00044 ocString fLnk;
00045 ocString sLnk ;
00046 ocString delLink;
00047 ocFileSys fs;
00048
00049 public:
00050 filePicker(openLogin & login, string base="Templates", string exe_name="filePick.cgi")
00051 :m_fileBase(base),m_exe_name(exe_name)
00052 ,fLnk("<a href='javascript:pickfile(\"_img_\")'>Pick</a></div>")
00053 ,sLnk("<a href='javascript:showfile(\"_img_\")'>Show</a></div>")
00054 ,delLink("<a href='javascript:deletefile(\"_img_\")'>"
00055 "[delete]"
00056 "</a>")
00057 {
00058 init(login);
00059 }
00060 void FLink( string value )
00061 {
00062 fLnk = value;
00063 }
00064 void init(openLogin & login)
00065 {
00066
00067 m_fileDir = fs.getCWD();
00068
00069 m_domain = getenv("SERVER_NAME");
00070
00071 string scriptname= getenv("SCRIPT_NAME");
00072 m_urlDir = "";
00073
00074 string::size_type pos = scriptname.find_last_of("/");
00075 if( pos != string::npos )
00076 {
00077 m_urlDir = scriptname.substr(0,pos);
00078 }
00079
00080 m_fileDir += "/";
00081 m_urlDir += "/";
00082
00083 m_fileDir += m_fileBase;
00084 m_urlDir += m_fileBase;
00085
00086
00087 }
00088 virtual ~filePicker(){;}
00089
00090 void checkForfilesToDelete(cgiScript & script)
00091 {
00092 cgiInput & args = script.ClientArguments();
00093 if( args.count("delete") > 0 && args["delete"].length() > 0)
00094 {
00095 string delPath = m_fileDir;
00096 delPath +="/";
00097 delPath += args["delete"].c_str();
00098
00099 writelog2( "Deleting ", delPath );
00100
00101 if( !fs.remove(delPath) )
00102 {
00103 script << "Error Deleting " << delPath << " - " << fs.check();
00104 }
00105 }
00106 }
00107 bool isFile( ocString candidate )
00108 {
00109 bool bret;
00110 bret = candidate.regExMatch(".[a-z,A-Z]");
00111 return bret;
00112 }
00113 void fileList(cgiScript & script, string title="File In: " )
00114 {
00115 cgiInput & args = script.ClientArguments();
00116
00117 cgiTemplates htmlDoc;
00118 title += fs.getCWD();
00119 writelog("Loading Templates/filelist.htmp");
00120 htmlDoc.load("Templates/filelist.htmp");
00121 char * Rows[2] = { "oddcell", "evencell" };
00122 writelog("Writing top of page");
00123 script << ocString(htmlDoc.getParagraph("top"))
00124 .replace( "__REPORT_TITLE_GOES_HERE", title.c_str() )
00125 .replaceAll( "filePick.cgi", m_exe_name.c_str() )
00126 << endl;
00127 ocString headitem = htmlDoc.getParagraph("headitem");
00128 string rowsep = htmlDoc.getParagraph("rowsep");
00129
00130 writelog("Writing headers");
00131
00132 script << headitem.replace("__hcell_label","File");
00133 script << headitem.replace("__hcell_label","Action");
00134 script << headitem.replace("__hcell_label","Delete");
00135
00136
00137
00138 if( fs.openDir(m_fileDir) )
00139 {
00140 ocDirectory & entries = fs.getDirectoryEntries();
00141
00142 sort( entries.begin(),
00143 entries.end(),
00144 datesort );
00145
00146
00147 int iMax = entries.size()?1:0;
00148 bool fileFound = false;
00149 if( args.count("showall") ) iMax = entries.size();
00150
00151 for( int iRow = 0;
00152 iRow < iMax || (!fileFound && iRow < entries.size());
00153 ++iRow )
00154 {
00155
00156 if(isFile(entries[iRow].name))
00157 {
00158 fileFound = true;
00159 string imgPath = m_fileDir;
00160 imgPath +="/";
00161 imgPath += entries[iRow].name;
00162 string imgLink = entries[iRow].name;
00163 string relLink = m_urlDir;
00164 relLink +="/";
00165 relLink += entries[iRow].name;
00166 ocString row = htmlDoc.getParagraph(Rows[ iRow % 2 ] );
00167 string actions = fLnk.replace( "_img_", imgLink.c_str());
00168 actions += " / ";
00169 actions += sLnk.replace( "_img_", relLink.c_str() );
00170 script << rowsep;
00171
00172 script << row.replace( "__cell_data", entries[iRow].name.c_str() );
00173 script << row.replace( "__cell_data", actions.c_str() );
00174 script << row.replace( "__cell_data",
00175 delLink.replaceAll( "_img_",
00176 entries[iRow].name.c_str()
00177 ).c_str() );
00178 script << endl;
00179 }
00180 }
00181 }
00182 script << htmlDoc.getParagraph("bottom") << endl;
00183 }
00184 string fileDir( void )
00185 {
00186 return m_fileDir;
00187 }
00188 string fileBase( void )
00189 {
00190 return m_fileBase;
00191 }
00192 void fileBase( string in )
00193 {
00194 m_fileBase = in;
00195 }
00196 };
00197
00198 #endif