mourningdove/htdocs/tools/endpoints/draft.bml

129 lines
4.6 KiB
Text
Raw Normal View History

2026-05-24 01:03:05 +00:00
<?_c
# This code was forked from the LiveJournal project owned and operated
# by Live Journal, Inc. The code has been modified and expanded by
# Dreamwidth Studios, LLC. These files were originally licensed under
# the terms of the license supplied by Live Journal, Inc, which can
# currently be found at:
#
# http://code.livejournal.org/trac/livejournal/browser/trunk/LICENSE-LiveJournal.txt
#
# In accordance with the original license, this code and all its
# modifications are provided under the GNU General Public License.
# A copy of that license can be found in the LICENSE file included as
# part of this distribution.
_c?>
<?_code # -*-bml-*-
{
use strict;
use vars qw(%POST);
use Storable;
use LJ::JSON;
my $err = sub {
my $msg = shift;
return to_json({
'alert' => $msg,
});
};
# get user
my $u = LJ::get_remote()
or return $err->("User logged in");
# check referers. should only be accessed from update.bml at the moment
LJ::check_referer("/update.bml")
or return $err->("Invalid referer");
my $ret = {};
# This property thaws the contents of the userprop 'draft_properties' and
# sends them back as a JS object.
if ( defined $GET{getProperties} ) {
my $rv = $u->prop( 'draft_properties' ) ? Storable::thaw( $u->prop(
'draft_properties' ) ) : {};
BML::set_content_type('text/javascript; charset=utf-8');
BML::finish();
return to_json( $rv );
}
# This property clears out all the fields of the user's draft, except the
# draft body itself.
if ( defined $POST{clearProperties} ) {
$u->clear_prop( 'draft_properties' );
}
# If even one property of the draft was changed, this property saves them
# all into a new draft (in order to avoid multiple HTTP posts which would
# decrease performance considerably).
# This is set up as a long if statement to avoid tying draft property saving to
# the draft body save logic, so that users won't have to change their
# draft body every time they want to get their properties saved.
if ( ( defined $POST{saveSubject} ) ||
( defined $POST{saveUserpic} ) ||
( defined $POST{saveTaglist} ) ||
( defined $POST{saveMoodID} ) ||
( defined $POST{saveMood} ) ||
( defined $POST{saveLocation} ) ||
( defined $POST{saveMusic} ) ||
( defined $POST{saveAdultReason} ) ||
( defined $POST{saveCommentSet} ) ||
( defined $POST{saveCommentScr} ) ||
( defined $POST{saveAdultCnt} ) )
{
my %properties = ( subject => $POST{saveSubject},
userpic => $POST{saveUserpic},
taglist => $POST{saveTaglist},
moodid => $POST{saveMoodID},
mood => $POST{saveMood},
location1 => $POST{saveLocation},
music => $POST{saveMusic},
adultreason => $POST{saveAdultReason},
commentset => $POST{saveCommentSet},
commentscr => $POST{saveCommentScr},
adultcnt => $POST{saveAdultCnt} );
# If the property is null, a default menu selection or a JS undefined
# value, we don't want to save it.
foreach my $key ( keys ( %properties ) ) {
if ( ( $properties{$key} =~ /^$/ ) ||
( $properties{$key} =~ /^0$/ ) ||
( $properties{$key} =~ /^undefined$/ ) ) {
delete $properties{$key};
};
};
# Freeze the hash into a frozen storable string. If the hash is not empty
# save it to the userprop. If it is, delete it.
my $frozen_properties = Storable::nfreeze( \%properties );
if ( $frozen_properties =~ /\w/ ) {
$u->set_prop('draft_properties', $frozen_properties);
} else {
$u->clear_prop('draft_properties');
};
}
# This property saves the main body of the draft.
if (defined $POST{'saveDraft'}) {
$u->set_draft_text($POST{'saveDraft'});
# This property clears out the main body of the draft.
} elsif ($POST{'clearDraft'}) {
$u->set_draft_text('');
} else {
$ret->{draft} = $u->draft_text;
}
sleep 1 if $LJ::IS_DEV_SERVER;
BML::set_content_type('text/javascript; charset=utf-8');
BML::finish();
return to_json( $ret );
}
_code?>