cgi-bin/DW/Controller/RPC/CutExpander.pm
author fu
Fri Jan 27 13:51:52 2012 +0800
changeset 4239 555a213bdacc
parent 3553 bafea8052e02
permissions -rw-r--r--
http://bugs.dwscoalition.org/show_bug.cgi?id=4230

Fix all the bad my $var = ... if ... constructions.

Patch by sophie.
fu@2373
     1
#!/usr/bin/perl
fu@2373
     2
#
fu@3553
     3
# DW::Controller::RPC::CutExpander
fu@2373
     4
#
fu@2373
     5
# AJAX endpoint that returns the expanded text for a cut tag.
fu@2373
     6
#
fu@2373
     7
# Author:
fu@2373
     8
#      Allen Petersen
fu@2373
     9
#
fu@3553
    10
# Copyright (c) 2009-2011 by Dreamwidth Studios, LLC.
fu@2373
    11
#
fu@2373
    12
# This program is free software; you may redistribute it and/or modify it under
fu@2373
    13
# the same terms as Perl itself. For a copy of the license, please reference
fu@2373
    14
# 'perldoc perlartistic' or 'perldoc perlgpl'.
fu@2373
    15
#
fu@2373
    16
fu@3553
    17
package DW::Controller::RPC::CutExpander;
fu@2373
    18
fu@2373
    19
use strict;
fu@2373
    20
use warnings;
fu@2373
    21
use DW::Routing;
fu@2373
    22
use JSON;
fu@2373
    23
fu@2768
    24
DW::Routing->register_string( "/__rpc_cuttag", \&cutexpander_handler, app => 1, user => 1, format => 'json' );
fu@2373
    25
fu@2373
    26
sub cutexpander_handler {
fu@2373
    27
    my $opts = shift;
fu@2373
    28
fu@2373
    29
    # gets the request and args
fu@2373
    30
    my $r = DW::Request->get;
fu@2373
    31
    my $args = $r->get_args;
fu@2373
    32
fu@2373
    33
    my $remote = LJ::get_remote();
fu@2373
    34
fu@2373
    35
    # error handler
fu@2373
    36
    my $error_out = sub {
fu@2373
    37
       my ( $code, $message ) = @_;
fu@2373
    38
       $r->status( $code );
fu@3553
    39
       $r->print( objToJson( { error => $message } ) );
fu@2373
    40
fu@2373
    41
       return $r->OK;
fu@2373
    42
    };
fu@2373
    43
fu@2373
    44
    if ( $args->{ditemid} && $args->{journal} && $args->{cutid} ) {
fu@2373
    45
        # all parameters are included; get the entry.
fu@2373
    46
        my $ditemid = $args->{ditemid};
fu@2373
    47
        my $uid = LJ::get_userid( $args->{journal} );
fu@4239
    48
        my $entry = $uid ? LJ::Entry->new( $uid, ditemid => $ditemid ) : undef;
fu@2373
    49
fu@3489
    50
        # FIXME: This returns 200 due to old library, Make return proper when we are jQuery only.
fu@2373
    51
        return $error_out->( 200, BML::ml( "error.nopermission" ) ) unless $entry;
fu@2373
    52
        
fu@2373
    53
        # make sure the user can read the entry
fu@2373
    54
        if ( $entry->visible_to( $remote ) ) {
fu@2373
    55
            my $text = load_cuttext( $entry, $remote, $args->{cutid} );
fu@2415
    56
            # FIXME: temporary fix.
fu@2415
    57
            # remove some unicode characters that could cause the returned JSON to break
fu@2415
    58
            # like in LJ::ejs, but we don't need to escape quotes, etc (objToJson does that)
fu@2415
    59
            $text =~ s/\xE2\x80[\xA8\xA9]//gs;
fu@2373
    60
            $r->print( objToJson( { text => $text } ) );
fu@2373
    61
            return $r->OK;
fu@2373
    62
        }
fu@2373
    63
    }
fu@2373
    64
fu@3489
    65
    # FIXME: This returns 200 due to old library, Make return proper when we are jQuery only.
fu@2373
    66
    return $error_out->( 200, BML::ml( "error.nopermission" ) );
fu@2373
    67
}
fu@2373
    68
fu@2373
    69
# loads the cutttext for the given entry
fu@2373
    70
sub load_cuttext {
fu@2373
    71
    my ( $entry_obj, $remote, $cutid ) = @_;
fu@2373
    72
fu@2373
    73
    # most of this is taken from S2->Entry_from_entryobj, modified for this
fu@2373
    74
    # more limited purpose.
fu@2373
    75
    my $get = {};
fu@2373
    76
    my $subject = "";
fu@2373
    77
fu@2373
    78
    my $anum = $entry_obj->anum;
fu@2373
    79
    my $jitemid = $entry_obj->jitemid;
fu@2373
    80
    my $ditemid = $entry_obj->ditemid;
fu@2373
    81
fu@2373
    82
    # $journal: journal posted to
fu@2373
    83
    my $journalid = $entry_obj->journalid;
fu@2373
    84
    my $journal = LJ::load_userid( $journalid );
fu@2373
    85
fu@2373
    86
    # is style=mine used?  or if remote has it on and this entry is not part of
fu@2373
    87
    # their journal.  if either are yes, it needs to be added to comment links
fu@2373
    88
    my %opt_stylemine = $remote && $remote->prop( 'opt_stylemine' ) && $remote->id != $journalid ? ( style => 'mine' ) : ();
fu@2373
    89
    my $style_args = LJ::viewing_style_args( %$get, %opt_stylemine );
fu@2373
    90
    
fu@2373
    91
    #load and prepare text of entry
fu@2373
    92
    my $text = LJ::CleanHTML::quote_html( $entry_obj->event_raw, $get->{nohtml} );
fu@2373
    93
    LJ::item_toutf8( $journal, \$subject, \$text ) if $LJ::UNICODE && $entry_obj->props->{unknown8bit};
fu@2373
    94
fu@2373
    95
    my $suspend_msg = $entry_obj && $entry_obj->should_show_suspend_msg_to( $remote ) ? 1 : 0;
fu@2373
    96
    # cleaning the entry text: cuts and such
fu@2373
    97
    my $cleanhtml_opts = { cuturl => LJ::item_link( $journal, $jitemid, $anum, $style_args ),
fu@2373
    98
        journal => $journal->username,
fu@2373
    99
        ditemid => $ditemid,
fu@2373
   100
        suspend_msg => $suspend_msg,
fu@2373
   101
        unsuspend_supportid => $suspend_msg ? $entry_obj->prop( 'unsuspend_supportid' ) : 0,
fu@2373
   102
        preformatted => $entry_obj->prop( "opt_preformatted" ),
fu@2373
   103
        cut_retrieve => $cutid, 
fu@2373
   104
    };
fu@2373
   105
fu@2373
   106
    LJ::CleanHTML::clean_event( \$text, $cleanhtml_opts );
fu@2373
   107
fu@2373
   108
    LJ::expand_embedded( $journal, $jitemid, $remote, \$text );
fu@2373
   109
    
fu@2373
   110
    return $text;
fu@2373
   111
}
fu@2373
   112
fu@2373
   113
1;