($filename); if ($LJ::FILEEDIT_VIA_DB || $LJ::FILEEDIT_VIA_DB{$filename}) { my $dbh = LJ::get_db_writer(); $dbh->do("REPLACE INTO includetext (incname, inctext, updatetime) ". "VALUES (?, ?, UNIX_TIMESTAMP())", undef, $filename, $content); return 0 if $dbh->err; LJ::MemCache::set("includefile:$filename", $content); return 1; } open (FILE, ">$INC_DIR/$filename") or return 0; print FILE $content; close FILE; return 1; }; my $load_file = sub { my ($filename) = @_; return undef unless $valid_filename->($filename); my $contents; if ($LJ::FILEEDIT_VIA_DB || $LJ::FILEEDIT_VIA_DB{$filename}) { my $dbh = LJ::get_db_writer(); $contents = $dbh->selectrow_array("SELECT inctext FROM includetext WHERE incname=?", undef, $filename); return $contents if defined $contents; } open (FILE, "$INC_DIR/$filename") or return undef; while () { $contents .= $_; } close FILE; return $contents; }; if ($files{'*'}) { # if user has access to edit all files, find what those files are! delete $files{'*'}; opendir (DIR, $INC_DIR); while (my $file = readdir(DIR)) { $files{$file} = 1; } closedir (DIR); } ## get rid of files that don't match our safe pattern { my @del; foreach my $k (keys %files) { push @del, $k unless $valid_filename->($k); } foreach my $k (@del) { delete $files{$k}; } } my $mode = $FORM{'mode'}; unless ($mode) { $mode = $FORM{'file'} ? "edit" : "pick"; } if ($mode eq "pick") { $ret .= "
\n"; $ret .= "Pick file to edit:
"; $ret .= "Wordwrap? "; $ret .= "Rows: "; $ret .= "Cols: "; $ret .= "
"; return $ret; } my $file = $FORM{'file'}; unless ($files{$file}) { return "ERROR! you don't have access to this document."; } if ($mode eq "edit") { $ret .= "Editing: $file

"; my $contents = $load_file->($file); return "Error: Couldn't open file" unless defined $contents; my $r = ($FORM{'r'}+0) || $DEF_ROW; my $c = ($FORM{'c'}+0) || $DEF_COL; my $wrap = $FORM{'w'} ? "SOFT" : "OFF"; $ret .= "

\n"; $ret .= ""; $ret .= ""; $ret .= "

(no undo.. are you sure?)"; $ret .= "

\n"; return $ret; } if ($mode eq "save") { unless (LJ::did_post()) { return "Error: requires post"; } $ret .= "Saving: $file

"; if ($save_file->($file, $FORM{'contents'})) { $ret .= "saved."; } else { $ret .= "Error saving"; } return $ret; } return "unknown mode"; _code?> lib: cgi-bin/ljlib.pl form: htdocs/admin/fileedit/index.bml post: htdocs/admin/fileedit/index.bml _c?>