<?php 
 
/* 
 
0.3a - 25.04.2006 
    - fixed utf-8 posts bug 
 
0.2a - 31.03.2006 
    - fixed autodelete feature 
    - fixed empty lj-cut removing 
    + 'no conversion' option added 
 
0.1a - 29.03.2006 
    * Initial version 
 
*/ 
 
require_once "ConvertCharset.class.php"; 
 
class NP_LiveJournal extends NucleusPlugin { 
 
// name of plugin 
function getName() { 
    return 'LiveJournal Plugin'; 
} 
 
// author of plugin 
function getAuthor()  { 
    return 'Evgeny Lykhin'; 
} 
 
// an URL to the plugin website 
// can also be of the form mailto:foo@bar.com 
function getURL() 
{ 
    return 'http://www.lykhin.com/nucleus-livejournal'; 
} 
 
// version of the plugin 
function getVersion() { 
    return '0.1a'; 
} 
 
function getMinNucleusVersion() { 
    return 220; 
} 
 
// a description to be shown on the installed plugins listing 
function getDescription() { 
    return 'This plugin allows to copy posts from Nucleus directly to LiveJournal.'; 
} 
 
function doSkinVar($skinType) { 
    echo 'LiveJournal Plugin!'; 
} 
 
function getPostedBy()   { 
    return '<p align="right" style="font-size: 7pt; line-height: 8pt; margin-top: 0pt; margin-bottom: 0pt"><a href="' . $this->getURL() .'">Nucleus LiveJournal Plugin &copy; Evgeny Lykhin</a></p>'; 
} 
 
function install() 
{ 
    global $CONF; 
    $this->createOption('livejournal_username', 'LiveJournal username', 'text'); 
    $this->createOption('livejournal_password', 'LiveJournal password (it\'s not safe, use MD5-coded instead!)', 'password'); 
    $this->createOption('livejournal_md5pswrd', 'LiveJournal password, MD5-coded', 'text'); 
    $indexurl = $CONF['IndexURL']; 
    $this->createOption('livejournal_template', 'LiveJournal post template', 'textarea', "%body%\r\n<lj-cut>%more%</lj-cut>\r\n\r\n<font size=-2><a href=\"" . $indexurl . "index.php?itemid=%itemid%\">Read more</a></font>"); 
    $this->createOption('livejournal_encoding', 'Nucleus posts encoding table', 'select', 'windows-1251', $this->GetEncodingOptionList()); 
    $this->createOption('livejournal_autoupd', 'Auto update LiveJournal post after edit in Nucleus', 'yesno', 'yes'); 
    $this->createOption('livejournal_autodel', 'Auto delete from LiveJournal after delete in Nucleus', 'yesno', 'yes'); 
    $this->createOption('livejournal_nocomm', 'Disable comments in LiveJournal posts', 'yesno', 'no'); 
    sql_query(sprintf("CREATE TABLE %s (item_id int(11), ljitem_id int(11), ljitem_url varchar(255)) TYPE=MYISAM",sql_table('plug_livejournal_items'))); 
} 
 
function unInstall() 
{ 
    $this->deleteOption('livejournal_username'); 
    $this->deleteOption('livejournal_password'); 
    $this->deleteOption('livejournal_md5pswrd'); 
    $this->deleteOption('livejournal_template'); 
    $this->deleteOption('livejournal_encoding'); 
    $this->deleteOption('livejournal_autoupd'); 
    $this->deleteOption('livejournal_autodel'); 
    $this->deleteItemOption('livejournal_nocomm'); 
} 
 
function getTableList() 
{ 
    return array(sql_table('plug_livejournal_items')); 
} 
 
function getEventList() 
{ 
    return array('PostAddItem', 'PreUpdateItem', 'PostDeleteItem'); 
} 
 
var $errormsg; 
var $lasturl; 
 
function ReadItemData($itemid) 
{ 
    $query = sprintf("SELECT ititle, ibody, imore, itime, iclosed, idraft, icat FROM %s WHERE inumber=%d", sql_table('item'), intval($itemid)); 
        $res = sql_query($query); 
    if ($res) 
        $data = mysql_fetch_array($res); 
    else 
        $data = 0; 
        mysql_free_result($res); 
    return $data; 
} 
 
function event_PostAddItem(&$params) 
{ 
    $itemid = $params['itemid']; 
    $data = $this->ReadItemData($itemid); 
    if ($data) 
    { 
        $username = $this->getOption("livejournal_username"); 
        $password = $this->getOption("livejournal_password"); 
        $hpassword = $this->getOption("livejournal_md5pswrd"); 
        $encoding = $this->getOption("livejournal_encoding"); 
        $title = $data[0]; 
        $body = $data[1]; 
        $more = $data[2]; 
        $time = $data[3]; 
        $closed = $data[4]; 
        $draft = $data[5]; 
 
        $text = $this->GetText($itemid, $title, $body, $more); 
 
        if ($encoding != 'no conversion') 
        { 
            $ConvertCharset = new ConvertCharset; 
            $title = $ConvertCharset->Convert($title, $encoding, "utf-8"); 
            $text = $ConvertCharset->Convert($text, $encoding, "utf-8"); 
        } 
 
        if ($username && ($password || $hpassword) && !$draft) 
        { 
            $nocomm = $closed || ($this->getOption('livejournal_nocomm') == 'yes'); 
            $lj_itemid = $this->LiveJournalPost($username, $password, $hpassword, $title, $text, $time, $nocomm); 
            if ($this->errormsg) 
                doError($this->errormsg); 
            if ($lj_itemid >= 0) 
            { 
                $query = sprintf("INSERT INTO %s (item_id, ljitem_id, ljitem_url) VALUES (%d, %d, '%s')",sql_table('plug_livejournal_items'), $itemid, $lj_itemid, $this->lasturl); 
                sql_query($query); 
            } 
        } 
    } 
} 
 
function GetText($itemid, $title, $body, $more) 
{ 
    $template = $this->getOption("livejournal_template"); 
 
    $search = array("%body%", "%more%", "%itemid%", "%title%"); 
    $replace = array($body, $more, $itemid, $title); 
    $text = str_replace($search, $replace, $template); 
 
    // Remove empty lj-cuts 
    $text = preg_replace("#<lj-cut\s*(text=\".*\")?>\s*</lj-cut>#", "", $text); 
 
    $text .= $this->getPostedBy(); 
 
    return $text; 
} 
 
function GetLJItemId($itemid) 
{ 
    $res = sql_query(sprintf("SELECT * FROM %s WHERE item_id = %d", sql_table('plug_livejournal_items'), $itemid)); 
    if ($res && $data = mysql_fetch_array($res)) 
        $ljitem_id = $data['ljitem_id']; 
    else 
        $ljitem_id = 0; 
    return $ljitem_id; 
} 
 
function event_PostDeleteItem(&$params) 
{ 
    if ($this->getOption('livejournal_autodel') == 'yes') 
    { 
 
    $itemid = $params['itemid']; 
    $ljitem_id = $this->GetLJItemId($itemid); 
 
    if ($ljitem_id) 
    { 
        $query = sprintf("DELETE FROM %s WHERE item_id = %d", sql_table('plug_livejournal_items'), $itemid); 
        sql_query($query); 
 
        $username = $this->getOption("livejournal_username"); 
        $password = $this->getOption("livejournal_password"); 
        $hpassword = $this->getOption("livejournal_md5pswrd"); 
 
        $this->LiveJournalPost($username, $password, $hpassword, "", "", 0, 0, $ljitem_id); 
    } 
 
    } 
} 
 
function event_PreUpdateItem(&$params) 
{ 
    if ($this->getOption('livejournal_autoupd') == 'yes') 
    { 
 
    $itemid = $params['itemid']; 
    $ljitem_id = $this->GetLJItemId($itemid); 
    if ($ljitem_id) 
    { 
        $username = $this->getOption("livejournal_username"); 
        $password = $this->getOption("livejournal_password"); 
        $hpassword = $this->getOption("livejournal_md5pswrd"); 
        $encoding = $this->getOption("livejournal_encoding"); 
 
        $title = $params['title']; 
        $body = $params['body']; 
        $more = $params['more']; 
 
        $text = $this->GetText($itemid, $title, $body, $more); 
 
        if ($encoding != 'no conversion') 
        { 
            $ConvertCharset = new ConvertCharset; 
            $title = $ConvertCharset->Convert($title, $encoding, "utf-8"); 
            $text = $ConvertCharset->Convert($text, $encoding, "utf-8"); 
        } 
 
        $data = $this->ReadItemData($itemid); 
        if ($data && $username && ($password || $hpassword) && !$draft) 
        { 
            $time = $data[3]; 
            $closed = $data[4]; 
 
            $nocomm = $closed || ($this->getOption('livejournal_nocomm') == 'yes'); 
            $this->LiveJournalPost($username, $password, $hpassword, $title, $text, $time, $nocomm, $ljitem_id); 
            if ($this->errormsg) 
                doError($this->errormsg); 
        } 
    } 
 
    } 
} 
 
function LiveJournalPost($username, $password, $hpassword, $title, $body, $time, $nocomm, $itemid = -1) 
{ 
    if ($itemid >= 0) 
        $methodname = 'LJ.XMLRPC.editevent'; 
    else 
        $methodname = 'LJ.XMLRPC.postevent'; 
 
    $this->lasturl = ""; 
    $this->errormsg = ""; 
 
    if (!class_exists('xmlrpcmsg')) { 
        global $DIR_LIBS; 
        include($DIR_LIBS . 'xmlrpc.inc.php'); 
    } 
 
    if ($hpassword) 
    { 
        $psw_name = "hpassword"; 
        $psw = $hpassword; 
    } 
    else 
    { 
        $psw_name = "password"; 
        $psw = $password; 
    } 
    $time_arr = getdate(strtotime($time)); 
 
    $param_arr = array( 
        'username' => new xmlrpcval($username), 
        $psw_name => new xmlrpcval($psw), 
        'event' => new xmlrpcval($body), 
        'subject' => new xmlrpcval($title), 
        'lineendings' => new xmlrpcval('pc'), 
        'year' => new xmlrpcval($time_arr["year"]), 
        'mon' => new xmlrpcval($time_arr["mon"]), 
        'day' => new xmlrpcval($time_arr["mday"]), 
        'hour' => new xmlrpcval($time_arr["hours"]), 
        'min' => new xmlrpcval($time_arr["minutes"]), 
        'ver' => new xmlrpcval(1) 
    ); 
    if ($itemid >= 0) 
        $param_arr['itemid'] = new xmlrpcval($itemid); 
 
    $param_arr['props'] = new xmlrpcval(array('opt_nocomments' => new xmlrpcval($nocomm, 'boolean')), 'struct'); 
 
    $struct = new xmlrpcval($param_arr, 'struct'); 
    $message = new xmlrpcmsg($methodname, array($struct)); 
 
    $client = new xmlrpc_client('/interface/xmlrpc', 'livejournal.com', 80); 
    if ($client) 
    { 
 
        $response = $client->send($message, 15); 
        if ($response) 
        { 
            $value = $response->value(); 
            if ($value) 
            { 
                $itemidval = $value->structmem("itemid"); 
                if ($itemidval) 
                    $itemid = $itemidval->scalarval(); 
                $urlval = $value->structmem("url"); 
                if ($urlval) 
                    $this->lasturl = $urlval->scalarval(); 
            } 
            else 
                $this->errormsg = "LiveJournal plugin error: " . $response->faultString(); 
        } 
    } 
    else 
        $this->errormsg = "LiveJournal plugin error: can't connect to livejournal.com"; 
 
    return $itemid; 
} 
 
function GetEncodingOptionList() 
{ 
    global $DIR_PLUGINS; 
    $dir = $DIR_PLUGINS . "ConvertTables"; 
 
    $dh  = opendir($dir); 
    while (false !== ($filename = readdir($dh))) { 
        $files[] = $filename; 
    } 
 
    sort($files); 
 
    $result = "no conversion|no conversion"; 
 
    foreach ($files as $file) 
    if ($file[0] != '.') 
        $result .= '|' . $file . '|' . $file; 
 
    return $result; 
} 
 
} 
?> 
np_livejournal_code.txt · Last modified: 2006/07/05 13:03 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki