PageRenderTime 29ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/ijpost/ijpost.php

https://gitlab.com/sim6/friendica-addons
PHP | 232 lines | 152 code | 64 blank | 16 comment | 29 complexity | bf519b4fd15d3030d3da13a60c1bcadf MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * Name: Insanejournal Post Connector
  4. * Description: Post to Insanejournal
  5. * Version: 1.0
  6. * Author: Tony Baldwin <https://free-haven.org/profile/tony>
  7. * Author: Michael Johnston
  8. * Author: Cat Gray <https://free-haven.org/profile/catness>
  9. */
  10. function ijpost_install() {
  11. register_hook('post_local', 'addon/ijpost/ijpost.php', 'ijpost_post_local');
  12. register_hook('notifier_normal', 'addon/ijpost/ijpost.php', 'ijpost_send');
  13. register_hook('jot_networks', 'addon/ijpost/ijpost.php', 'ijpost_jot_nets');
  14. register_hook('connector_settings', 'addon/ijpost/ijpost.php', 'ijpost_settings');
  15. register_hook('connector_settings_post', 'addon/ijpost/ijpost.php', 'ijpost_settings_post');
  16. }
  17. function ijpost_uninstall() {
  18. unregister_hook('post_local', 'addon/ijpost/ijpost.php', 'ijpost_post_local');
  19. unregister_hook('notifier_normal', 'addon/ijpost/ijpost.php', 'ijpost_send');
  20. unregister_hook('jot_networks', 'addon/ijpost/ijpost.php', 'ijpost_jot_nets');
  21. unregister_hook('connector_settings', 'addon/ijpost/ijpost.php', 'ijpost_settings');
  22. unregister_hook('connector_settings_post', 'addon/ijpost/ijpost.php', 'ijpost_settings_post');
  23. }
  24. function ijpost_jot_nets(&$a,&$b) {
  25. if(! local_user())
  26. return;
  27. $ij_post = get_pconfig(local_user(),'ijpost','post');
  28. if(intval($ij_post) == 1) {
  29. $ij_defpost = get_pconfig(local_user(),'ijpost','post_by_default');
  30. $selected = ((intval($ij_defpost) == 1) ? ' checked="checked" ' : '');
  31. $b .= '<div class="profile-jot-net"><input type="checkbox" name="ijpost_enable" ' . $selected . ' value="1" /> '
  32. . t('Post to Insanejournal') . '</div>';
  33. }
  34. }
  35. function ijpost_settings(&$a,&$s) {
  36. if(! local_user())
  37. return;
  38. /* Add our stylesheet to the page so we can make our settings look nice */
  39. $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/ijpost/ijpost.css' . '" media="all" />' . "\r\n";
  40. /* Get the current state of our config variables */
  41. $enabled = get_pconfig(local_user(),'ijpost','post');
  42. $checked = (($enabled) ? ' checked="checked" ' : '');
  43. $def_enabled = get_pconfig(local_user(),'ijpost','post_by_default');
  44. $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
  45. $ij_username = get_pconfig(local_user(), 'ijpost', 'ij_username');
  46. $ij_password = get_pconfig(local_user(), 'ijpost', 'ij_password');
  47. /* Add some HTML to the existing form */
  48. $s .= '<span id="settings_ijpost_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_ijpost_expanded\'); openClose(\'settings_ijpost_inflated\');">';
  49. $s .= '<img class="connector" src="images/insanejournal.gif" /><h3 class="connector">'. t("InsaneJournal Export").'</h3>';
  50. $s .= '</span>';
  51. $s .= '<div id="settings_ijpost_expanded" class="settings-block" style="display: none;">';
  52. $s .= '<span class="fakelink" onclick="openClose(\'settings_ijpost_expanded\'); openClose(\'settings_ijpost_inflated\');">';
  53. $s .= '<img class="connector" src="images/insanejournal.gif" /><h3 class="connector">'. t("InsaneJournal Export").'</h3>';
  54. $s .= '</span>';
  55. $s .= '<div id="ijpost-enable-wrapper">';
  56. $s .= '<label id="ijpost-enable-label" for="ijpost-checkbox">' . t('Enable InsaneJournal Post Plugin') . '</label>';
  57. $s .= '<input id="ijpost-checkbox" type="checkbox" name="ijpost" value="1" ' . $checked . '/>';
  58. $s .= '</div><div class="clear"></div>';
  59. $s .= '<div id="ijpost-username-wrapper">';
  60. $s .= '<label id="ijpost-username-label" for="ijpost-username">' . t('InsaneJournal username') . '</label>';
  61. $s .= '<input id="ijpost-username" type="text" name="ij_username" value="' . $ij_username . '" />';
  62. $s .= '</div><div class="clear"></div>';
  63. $s .= '<div id="ijpost-password-wrapper">';
  64. $s .= '<label id="ijpost-password-label" for="ijpost-password">' . t('InsaneJournal password') . '</label>';
  65. $s .= '<input id="ijpost-password" type="password" name="ij_password" value="' . $ij_password . '" />';
  66. $s .= '</div><div class="clear"></div>';
  67. $s .= '<div id="ijpost-bydefault-wrapper">';
  68. $s .= '<label id="ijpost-bydefault-label" for="ijpost-bydefault">' . t('Post to InsaneJournal by default') . '</label>';
  69. $s .= '<input id="ijpost-bydefault" type="checkbox" name="ij_bydefault" value="1" ' . $def_checked . '/>';
  70. $s .= '</div><div class="clear"></div>';
  71. /* provide a submit button */
  72. $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="ijpost-submit" name="ijpost-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div></div>';
  73. }
  74. function ijpost_settings_post(&$a,&$b) {
  75. if(x($_POST,'ijpost-submit')) {
  76. set_pconfig(local_user(),'ijpost','post',intval($_POST['ijpost']));
  77. set_pconfig(local_user(),'ijpost','post_by_default',intval($_POST['ij_bydefault']));
  78. set_pconfig(local_user(),'ijpost','ij_username',trim($_POST['ij_username']));
  79. set_pconfig(local_user(),'ijpost','ij_password',trim($_POST['ij_password']));
  80. }
  81. }
  82. function ijpost_post_local(&$a,&$b) {
  83. // This can probably be changed to allow editing by pointing to a different API endpoint
  84. if($b['edit'])
  85. return;
  86. if((! local_user()) || (local_user() != $b['uid']))
  87. return;
  88. if($b['private'] || $b['parent'])
  89. return;
  90. $ij_post = intval(get_pconfig(local_user(),'ijpost','post'));
  91. $ij_enable = (($ij_post && x($_REQUEST,'ijpost_enable')) ? intval($_REQUEST['ijpost_enable']) : 0);
  92. if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'ijpost','post_by_default')))
  93. $ij_enable = 1;
  94. if(! $ij_enable)
  95. return;
  96. if(strlen($b['postopts']))
  97. $b['postopts'] .= ',';
  98. $b['postopts'] .= 'ijpost';
  99. }
  100. function ijpost_send(&$a,&$b) {
  101. if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
  102. return;
  103. if(! strstr($b['postopts'],'ijpost'))
  104. return;
  105. if($b['parent'] != $b['id'])
  106. return;
  107. // insanejournal post in the LJ user's timezone.
  108. // Hopefully the person's Friendica account
  109. // will be set to the same thing.
  110. $tz = 'UTC';
  111. $x = q("select timezone from user where uid = %d limit 1",
  112. intval($b['uid'])
  113. );
  114. if($x && strlen($x[0]['timezone']))
  115. $tz = $x[0]['timezone'];
  116. $ij_username = get_pconfig($b['uid'],'ijpost','ij_username');
  117. $ij_password = get_pconfig($b['uid'],'ijpost','ij_password');
  118. $ij_blog = 'http://www.insanejournal.com/interface/xmlrpc';
  119. if($ij_username && $ij_password && $ij_blog) {
  120. require_once('include/bbcode.php');
  121. require_once('include/datetime.php');
  122. $title = $b['title'];
  123. $post = bbcode($b['body']);
  124. $post = xmlify($post);
  125. $tags = ijpost_get_tags($b['tag']);
  126. $date = datetime_convert('UTC',$tz,$b['created'],'Y-m-d H:i:s');
  127. $year = intval(substr($date,0,4));
  128. $mon = intval(substr($date,5,2));
  129. $day = intval(substr($date,8,2));
  130. $hour = intval(substr($date,11,2));
  131. $min = intval(substr($date,14,2));
  132. $xml = <<< EOT
  133. <?xml version="1.0" encoding="utf-8"?>
  134. <methodCall><methodName>LJ.XMLRPC.postevent</methodName>
  135. <params><param>
  136. <value><struct>
  137. <member><name>year</name><value><int>$year</int></value></member>
  138. <member><name>mon</name><value><int>$mon</int></value></member>
  139. <member><name>day</name><value><int>$day</int></value></member>
  140. <member><name>hour</name><value><int>$hour</int></value></member>
  141. <member><name>min</name><value><int>$min</int></value></member>
  142. <member><name>event</name><value><string>$post</string></value></member>
  143. <member><name>username</name><value><string>$ij_username</string></value></member>
  144. <member><name>password</name><value><string>$ij_password</string></value></member>
  145. <member><name>subject</name><value><string>$title</string></value></member>
  146. <member><name>lineendings</name><value><string>unix</string></value></member>
  147. <member><name>ver</name><value><int>1</int></value></member>
  148. <member><name>props</name>
  149. <value><struct>
  150. <member><name>useragent</name><value><string>Friendica</string></value></member>
  151. <member><name>taglist</name><value><string>$tags</string></value></member>
  152. </struct></value></member>
  153. </struct></value>
  154. </param></params>
  155. </methodCall>
  156. EOT;
  157. logger('ijpost: data: ' . $xml, LOGGER_DATA);
  158. if($ij_blog !== 'test')
  159. $x = post_url($ij_blog,$xml,array("Content-Type: text/xml"));
  160. logger('posted to insanejournal: ' . ($x) ? $x : '', LOGGER_DEBUG);
  161. }
  162. }
  163. function ijpost_get_tags($post)
  164. {
  165. preg_match_all("/\]([^\[#]+)\[/",$post,$matches);
  166. $tags = implode(', ',$matches[1]);
  167. return $tags;
  168. }