Commit 569616ff authored by ivan zlax's avatar ivan zlax

another try

parent cb2a3989
<?php <?php
/** /**
* Name: LJRossia Post feature * Name: Livejournal Post feature
* Description: Post to LJRossia * Description: Post to Livejournal
* Version: 1.0 * Version: 1.0
* Author: Tony Baldwin <https://red.free-haven.org/channel/tony> * Author: Tony Baldwin <https://red.free-haven.org/channel/tony>
* Author: Michael Johnston * Author: Michael Johnston
* Author: Cat Gray <https://free-haven.org/profile/catness> * Author: Cat Gray <https://free-haven.org/profile/catness>
* Maintainer: zlax * Maintainer: none
*/ */
require_once('include/permissions.php'); require_once('include/permissions.php');
...@@ -39,94 +39,94 @@ function ljrpost_jot_nets(&$a,&$b) { ...@@ -39,94 +39,94 @@ function ljrpost_jot_nets(&$a,&$b) {
$ljr_defpost = get_pconfig(local_channel(),'ljrpost','post_by_default'); $ljr_defpost = get_pconfig(local_channel(),'ljrpost','post_by_default');
$selected = ((intval($ljr_defpost) == 1) ? ' checked="checked" ' : ''); $selected = ((intval($ljr_defpost) == 1) ? ' checked="checked" ' : '');
$b .= '<div class="profile-jot-net"><input type="checkbox" name="ljrpost_enable" ' . $selected . ' value="1" /> ' $b .= '<div class="profile-jot-net"><input type="checkbox" name="ljrpost_enable" ' . $selected . ' value="1" /> '
. t('Post to LJRossia') . '</div>'; . t('Post to Livejournal') . '</div>';
} }
} }
function ljrpost_settings(&$a,&$s) { function ljrpost_settings(&$a,&$s) {
if(! local_channel()) if(! local_channel())
return; return;
/* Add our stylesheet to the page so we can make our settings look nice */ /* Add our stylesheet to the page so we can make our settings look nice */
//App::$page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . z_root() . '/addon/ljrpost/ljrpost.css' . '" media="all" />' . "\r\n"; //App::$page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . z_root() . '/addon/ljrpost/ljrpost.css' . '" media="all" />' . "\r\n";
/* Get the current state of our config variables */ /* Get the current state of our config variables */
$enabled = get_pconfig(local_channel(),'ljrpost','post'); $enabled = get_pconfig(local_channel(),'ljrpost','post');
$checked = (($enabled) ? 1 : false); $checked = (($enabled) ? 1 : false);
$def_enabled = get_pconfig(local_channel(),'ljrpost','post_by_default'); $def_enabled = get_pconfig(local_channel(),'ljrpost','post_by_default');
$def_checked = (($def_enabled) ? 1 : false); $def_checked = (($def_enabled) ? 1 : false);
$ljr_username = get_pconfig(local_channel(), 'ljrpost', 'ljr_username'); $ljr_username = get_pconfig(local_channel(), 'ljrpost', 'ljr_username');
$ljr_password = z_unobscure(get_pconfig(local_channel(), 'ljrpost', 'ljr_password')); $ljr_password = z_unobscure(get_pconfig(local_channel(), 'ljrpost', 'ljr_password'));
/* Add some HTML to the existing form */ /* Add some HTML to the existing form */
$sc .= replace_macros(get_markup_template('field_checkbox.tpl'), array( $sc .= replace_macros(get_markup_template('field_checkbox.tpl'), array(
'$field' => array('ljrpost', t('Enable LJRossia Post Plugin'), $checked, '', array(t('No'),t('Yes'))), '$field' => array('ljrpost', t('Enable Livejournal Post Plugin'), $checked, '', array(t('No'),t('Yes'))),
)); ));
$sc .= replace_macros(get_markup_template('field_input.tpl'), array( $sc .= replace_macros(get_markup_template('field_input.tpl'), array(
'$field' => array('ljr_username', t('LJRossia username'), $ljr_username, '') '$field' => array('ljr_username', t('Livejournal username'), $ljr_username, '')
)); ));
$sc .= replace_macros(get_markup_template('field_password.tpl'), array( $sc .= replace_macros(get_markup_template('field_password.tpl'), array(
'$field' => array('ljr_password', t('LJRossia password'), $ljr_password, '') '$field' => array('ljr_password', t('Livejournal password'), $ljr_password, '')
)); ));
$sc .= replace_macros(get_markup_template('field_checkbox.tpl'), array( $sc .= replace_macros(get_markup_template('field_checkbox.tpl'), array(
'$field' => array('ljr_bydefault', t('Post to LJRossia by default'), $def_checked, '', array(t('No'),t('Yes'))), '$field' => array('ljr_bydefault', t('Post to Livejournal by default'), $def_checked, '', array(t('No'),t('Yes'))),
)); ));
$s .= replace_macros(get_markup_template('generic_addon_settings.tpl'), array( $s .= replace_macros(get_markup_template('generic_addon_settings.tpl'), array(
'$addon' => array('ljrpost',t('LJRossia Post Settings'), '', t('Submit')), '$addon' => array('ljrpost',t('Livejournal Post Settings'), '', t('Submit')),
'$content' => $sc '$content' => $sc
)); ));
} }
function ljrpost_settings_post(&$a,&$b) { function ljrpost_settings_post(&$a,&$b) {
if(x($_POST,'ljrpost-submit')) { if(x($_POST,'ljrpost-submit')) {
set_pconfig(local_channel(),'ljrpost','post',intval($_POST['ljrpost'])); set_pconfig(local_channel(),'ljrpost','post',intval($_POST['ljrpost']));
set_pconfig(local_channel(),'ljrpost','post_by_default',intval($_POST['ljr_bydefault'])); set_pconfig(local_channel(),'ljrpost','post_by_default',intval($_POST['ljr_bydefault']));
set_pconfig(local_channel(),'ljrpost','ljr_username',trim($_POST['ljr_username'])); set_pconfig(local_channel(),'ljrpost','ljr_username',trim($_POST['ljr_username']));
set_pconfig(local_channel(),'ljrpost','ljr_password',z_obscure(trim($_POST['ljr_password']))); set_pconfig(local_channel(),'ljrpost','ljr_password',z_obscure(trim($_POST['ljr_password'])));
} }
} }
function ljrpost_post_local(&$a,&$b) { function ljrpost_post_local(&$a,&$b) {
// This can probably be changed to allow editing by pointing to a different API endpoint // This can probably be changed to allow editing by pointing to a different API endpoint
if($b['edit']) if($b['edit'])
return; return;
if((! local_channel()) || (local_channel() != $b['uid'])) if((! local_channel()) || (local_channel() != $b['uid']))
return; return;
if($b['item_private'] || $b['parent']) if($b['item_private'] || $b['parent'])
return; return;
logger('LJRossia xpost invoked'); logger('Livejournal xpost invoked');
$ljr_post = intval(get_pconfig(local_channel(),'ljrpost','post')); $ljr_post = intval(get_pconfig(local_channel(),'ljrpost','post'));
$ljr_enable = (($ljr_post && x($_REQUEST,'ljrpost_enable')) ? intval($_REQUEST['ljrpost_enable']) : 0); $ljr_enable = (($ljr_post && x($_REQUEST,'ljrpost_enable')) ? intval($_REQUEST['ljrpost_enable']) : 0);
if($_REQUEST['api_source'] && intval(get_pconfig(local_channel(),'ljrpost','post_by_default'))) if($_REQUEST['api_source'] && intval(get_pconfig(local_channel(),'ljrpost','post_by_default')))
$ljr_enable = 1; $ljr_enable = 1;
if(! $ljr_enable) if(! $ljr_enable)
return; return;
...@@ -144,8 +144,8 @@ function ljrpost_send(&$a,&$b) { ...@@ -144,8 +144,8 @@ function ljrpost_send(&$a,&$b) {
if((! is_item_normal($b)) || $b['item_private'] || ($b['created'] !== $b['edited'])) if((! is_item_normal($b)) || $b['item_private'] || ($b['created'] !== $b['edited']))
return; return;
if(! perm_is_allowed($b['uid'],'','view_stream',false)) if(! perm_is_allowed($b['uid'],'','view_stream',false))
return; return;
if(! strstr($b['postopts'],'ljrpost')) if(! strstr($b['postopts'],'ljrpost'))
return; return;
...@@ -153,40 +153,40 @@ function ljrpost_send(&$a,&$b) { ...@@ -153,40 +153,40 @@ function ljrpost_send(&$a,&$b) {
if($b['parent'] != $b['id']) if($b['parent'] != $b['id'])
return; return;
// LJRossia post in the LJ user's timezone. // Livejournal post in the LJ user's timezone.
// Hopefully the person's Friendica account // Hopefully the person's Friendica account
// will be set to the same thing. // will be set to the same thing.
$tz = 'UTC'; $tz = 'UTC';
$x = q("select channel_timezone from channel where channel_id = %d limit 1", $x = q("select channel_timezone from channel where channel_id = %d limit 1",
intval($b['uid']) intval($b['uid'])
); );
if($x && strlen($x[0]['channel_timezone'])) if($x && strlen($x[0]['channel_timezone']))
$tz = $x[0]['channel_timezone']; $tz = $x[0]['channel_timezone'];
$ljr_username = get_pconfig($b['uid'],'ljrpost','ljr_username'); $ljr_username = get_pconfig($b['uid'],'ljrpost','ljr_username');
$ljr_password = z_unobscure(get_pconfig($b['uid'],'ljrpost','ljr_password')); $ljr_password = z_unobscure(get_pconfig($b['uid'],'ljrpost','ljr_password'));
$ljr_blog = 'https://lj.rossia.org/interface/xmlrpc'; $ljr_blog = 'https://lj.rossia.org/interface/xmlrpc';
if($ljr_username && $ljr_password && $ljr_blog) { if($ljr_username && $ljr_password && $ljr_blog) {
require_once('include/bbcode.php'); require_once('include/bbcode.php');
require_once('include/datetime.php'); require_once('include/datetime.php');
$title = $b['title']; $title = $b['title'];
$post = bbcode($b['body']); $post = bbcode($b['body']);
$post = xmlify($post); $post = xmlify($post);
$tags = ljrpost_get_tags($b['tag']); $tags = ljrpost_get_tags($b['tag']);
$date = datetime_convert('UTC',$tz,$b['created'],'Y-m-d H:i:s'); $date = datetime_convert('UTC',$tz,$b['created'],'Y-m-d H:i:s');
$year = intval(substr($date,0,4)); $year = intval(substr($date,0,4));
$mon = intval(substr($date,5,2)); $mon = intval(substr($date,5,2));
$day = intval(substr($date,8,2)); $day = intval(substr($date,8,2));
$hour = intval(substr($date,11,2)); $hour = intval(substr($date,11,2));
$min = intval(substr($date,14,2)); $min = intval(substr($date,14,2));
$xml = <<< EOT $xml = <<< EOT
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<methodCall><methodName>LJ.XMLRPC.postevent</methodName> <methodCall><methodName>LJ.XMLRPC.postevent</methodName>
<params><param> <params><param>
...@@ -213,20 +213,20 @@ function ljrpost_send(&$a,&$b) { ...@@ -213,20 +213,20 @@ function ljrpost_send(&$a,&$b) {
EOT; EOT;
logger('ljrpost: data: ' . $xml, LOGGER_DATA); logger('ljrpost: data: ' . $xml, LOGGER_DATA);
if($ljr_blog !== 'test') { if($ljr_blog !== 'test') {
$recurse = 0; $recurse = 0;
$x = z_post_url($ljr_blog,$xml,$recurse,array('headers' => array("Content-Type: text/xml"))); $x = z_post_url($ljr_blog,$xml,$recurse,array('headers' => array("Content-Type: text/xml")));
} }
logger('posted to LJRossia: ' . print_r($x,true), LOGGER_DEBUG); logger('posted to Livejournal: ' . print_r($x,true), LOGGER_DEBUG);
} }
} }
function ljrpost_get_tags($post) function ljrpost_get_tags($post)
{ {
preg_match_all("/\]([^\[#]+)\[/",$post,$matches); preg_match_all("/\]([^\[#]+)\[/",$post,$matches);
$tags = implode(', ',$matches[1]); $tags = implode(', ',$matches[1]);
return $tags; return $tags;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment