PageRenderTime 27ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/mnw-themepage-handler.php

https://github.com/singpolyma/mnw
PHP | 242 lines | 207 code | 2 blank | 33 comment | 21 complexity | 9913af45017b7a01e2971f6858a76124 MD5 | raw file
Possible License(s): AGPL-3.0
  1. <?php
  2. /**
  3. * This file is part of mnw.
  4. *
  5. * mnw - an OpenMicroBlogging compatible Microblogging plugin for Wordpress
  6. * Copyright (C) 2009, Adrian Lang
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. class mnw_Themepage {
  22. protected $data;
  23. public $shouldDisplay;
  24. /*
  25. * Parse requests to the main microblog page.
  26. */
  27. public function __construct() {
  28. /* Assure that we have a valid themepage setting. */
  29. if (get_option('mnw_themepage_url') == '') {
  30. /* Since this method is only called from the themepage, we can
  31. just copy the current url if something‘s broken. */
  32. global $wp_query;
  33. update_option('mnw_themepage_url', $wp_query->post->guid);
  34. }
  35. $this->data = $this->parseRequest();
  36. $this->shouldDisplay = ($this->data[0] !== false);
  37. }
  38. protected static function parseRequest() {
  39. if (!isset($_REQUEST[MNW_ACTION])) {
  40. /* No action at all – display the standard page. */
  41. return array('', array());
  42. }
  43. /* Hash containing file and procedure name to handle the request. */
  44. $actions = array(
  45. 'subscribe' => array('subscribe.php', 'mnw_parse_subscribe'),
  46. 'get_notice' => array('get_notice.php', 'mnw_get_notice'),
  47. 'xrds' => array('mnw_provider.php', 'mnw_get_xrds'),
  48. 'oauth' => array('mnw_provider.php', 'mnw_handle_oauth'),
  49. 'omb' => array('mnw_provider.php', 'mnw_handle_omb'),
  50. 'notices' => array('mnw-notices.php', 'mnw_notices'));
  51. if (!isset($actions[$_REQUEST[MNW_ACTION]])) {
  52. /* Save a poor user who entered a wrong action. */
  53. wp_redirect(get_option('mnw_themepage_url'));
  54. return array(false, array());
  55. }
  56. /* Load file and call method for this action request. */
  57. $action = $actions[$_REQUEST[MNW_ACTION]];
  58. require_once $action[0];
  59. return $action[1]();
  60. }
  61. /* $continue is a string from the set ('', 'subscribe', 'get_notice', 'userauth', 'userauth_continue', 'notices').
  62. $data contains additional data generated by the plugin. */
  63. public function render() {
  64. $continue = $this->data[0];
  65. $data = $this->data[1];
  66. echo '<p>';
  67. printf(__('%s supports the <a href="//openmicroblogging.org"
  68. title="Official website of the OpenMicroBlogging standard">
  69. OpenMicroBlogging</a> standard. ', 'mnw'),
  70. get_bloginfo('title'));
  71. _e('It uses the free plugin <a href="//adrianlang.de/mnw"
  72. title="Official website of mnw">mnw</a> for OMB functionality.', 'mnw');
  73. ?>
  74. </p>
  75. <ul>
  76. <li><a href="<?php echo attribute_escape(get_option('mnw_themepage_url')); ?>" title="<?php _e('Go to the subscribe form', 'mnw'); ?>"><?php _e('Subscribe', 'mnw'); ?></a></li>
  77. <li><a href="<?php echo attribute_escape(mnw_set_action('notices') . '&type=sent'); ?>" title="<?php _e('Display sent notices', 'mnw'); ?>"><?php _e('Sent notices', 'mnw'); ?></a></li>
  78. <li><a href="<?php echo attribute_escape(mnw_set_action('notices') . '&type=received'); ?>" title="<?php _e('Display received notices', 'mnw'); ?>"><?php _e('Received notices', 'mnw'); ?></a></li>
  79. </ul>
  80. <?php
  81. switch ($continue) {
  82. case '':
  83. case 'subscribe':
  84. /* Gather data for subscribe form. */
  85. global $wp_query;
  86. $action = attribute_escape(mnw_set_action('subscribe') . '&' .
  87. MNW_SUBSCRIBE_ACTION . '=continue');
  88. /* Start displaying the form. */
  89. echo '<h3>' . __('Subscribe', 'mnw') . '</h3>';
  90. echo '<p>';
  91. printf(__('If you have an user account at another OMB service
  92. like <a href="//identi.ca" title="identi.ca, the largest
  93. open microblogging service">identi.ca</a>, you can
  94. easily subscribe to %s.', 'mnw'), get_bloginfo('name'));
  95. echo '</p>';
  96. echo '<p>';
  97. _e('To subscribe, just enter the URL of your profile at another OMB
  98. service. ', 'mnw');
  99. _e('You will be asked to log in there if you are not yet. ', 'mnw');
  100. printf(__('There will be a confirmation prompt showing details of
  101. %s.', 'mnw'), get_bloginfo('name'));
  102. echo '</p>';
  103. if (isset($data['error'])) {
  104. echo '<p>';
  105. printf(__('Error: %s', 'mnw'), $data['error']);
  106. echo '</p>';
  107. }
  108. ?>
  109. <form id='omb-subscribe' method='post' action='<?php echo $action; ?>'>
  110. <label for="profile_url"><?php _e('OMB Profile URL', 'mnw'); ?>
  111. </label>
  112. <input name="profile_url" type="text" class="input_text"
  113. id="profile_url" value='<?php
  114. if (isset($data['profile_url']))
  115. echo $data['profile_url']; ?>'/>
  116. <input type="submit" id="submit" name="submit" class="submit"
  117. value="<?php _e('Subscribe', 'mnw'); ?>"/>
  118. </form>
  119. <?php
  120. break;
  121. case 'get_notice':
  122. if (isset($data['notice']) && $data['notice'] !== false) {
  123. ?>
  124. <h3><?php printf(__('Notice from %s', 'mnw'), date(
  125. __('d F Y H:i:s', 'mnw'),
  126. strtotime($data['notice']->created)));?></h3>
  127. <p style="font-size: 2em; margin-left: 0.5em;">
  128. <?php
  129. if ($data['notice']->url !== null) {
  130. echo "<a href='" . $data['notice']->url . "'>";
  131. }
  132. echo $data['notice']->content;
  133. if ($data['notice']->url !== null) {
  134. echo "</a>";
  135. }
  136. ?>
  137. </p>
  138. <?php
  139. } else {
  140. ?>
  141. <h3><?php _e('Notice', 'mnw'); ?></h3>
  142. <p><?php _e('Notice not found.', 'mnw'); ?></p>
  143. <?php
  144. }
  145. break;
  146. case 'userauth':
  147. ?>
  148. <h3><?php _e('Authorize subscription', 'mnw'); ?></h3>
  149. <p>
  150. <?php
  151. if (isset($data['error'])) {
  152. echo $data['error'];
  153. } else {
  154. $uri = $data['remote_user']->getIdentifierURI();
  155. $action = attribute_escape(mnw_set_action('oauth') . '&' .
  156. MNW_OAUTH_ACTION . '=userauth_continue');
  157. ?>
  158. <form id="mnw_userauthorization" name="mnw_userauthorization"
  159. method="post" action="<?php echo $action; ?>">
  160. <p><?php printf(__('Do you really want to subscribe %s?',
  161. 'mnw'), '<a href="' .
  162. $data['remote_user']->getProfileURL() . '">' .
  163. $uri . '</a>');?></p>
  164. <input id="profile" type="hidden" value="<?php echo $uri; ?>"
  165. name="profile"/>
  166. <input id="nonce" type="hidden" name="nonce"
  167. value="<?php echo wp_create_nonce('mnw_userauth_nonce'); ?>"/>
  168. <input id="accept" class="submit" type="submit" title=""
  169. value="<?php _e('Yes', 'mnw'); ?>" name="accept"/>
  170. <input id="reject" class="submit" type="submit" title=""
  171. value="<?php _e('No', 'mnw'); ?>" name="reject"/>
  172. </form>
  173. <?php
  174. }
  175. ?>
  176. </p>
  177. <?php
  178. break;
  179. case 'userauth_continue':
  180. ?>
  181. <h3><?php _e('Authorization granted', 'mnw'); ?></h3>
  182. <p>
  183. <?php
  184. if ($data['token'] !== '') {
  185. printf(__('Confirm the subscribee‘s service that token %s is
  186. authorized.', 'mnw'), $data['token']);
  187. } else {
  188. _e('You rejected the subscription.', 'mnw');
  189. }
  190. echo '</p>';
  191. break;
  192. case 'notices':
  193. $show_sent = ($data[0] == 'sent');
  194. $paged = $data[1];
  195. $total = $data[2];
  196. ?>
  197. <h3><?php if ($show_sent) { _e('Sent notices', 'mnw'); } else { _e('Received notices', 'mnw');} ?></h3>
  198. <div>
  199. <ul>
  200. <?php
  201. if ($data[3] !== null) {
  202. foreach($data[3] as $notice) {
  203. echo '<li>';
  204. if ($show_sent) {
  205. printf('„%s“ @ <a href="%s">%s</a>', $notice['content'], $notice['noticeurl'], $notice['created']);
  206. } else {
  207. printf('„%s“<div><a href="%s" title="%s">%s</a> @ <a href="%s">%s</a></div>', $notice['content'], $notice['authorurl'], $notice['fullname'], $notice['nickname'], $notice['noticeurl'], $notice['created']);
  208. }
  209. echo '</li>';
  210. }
  211. }
  212. ?>
  213. </ul>
  214. <div style="float: right;">
  215. <span>
  216. <?php
  217. printf(__('Displaying %s–%s of %s', 'mnw'),
  218. number_format_i18n(($paged - 1) * 15 + 1),
  219. number_format_i18n(min($paged * 15, $total)),
  220. number_format_i18n($total));
  221. ?>
  222. </span>
  223. <?php
  224. echo paginate_links(array(
  225. 'base' => add_query_arg( 'paged', '%#%' ),
  226. 'format' => '',
  227. 'prev_text' => __('&laquo;'),
  228. 'next_text' => __('&raquo;'),
  229. 'total' => ceil($total / 15.0),
  230. 'current' => $paged));
  231. ?>
  232. </div>
  233. <a href="<?php echo attribute_escape(mnw_set_action('notices') . "&type=$data[0]&format=atom"); ?>" title="<?php _e('Display Atom feed of notices', 'mnw');?>"><?php _e('Atom feed', 'mnw'); ?></a>
  234. </div>
  235. <?php
  236. break;
  237. }
  238. }
  239. }
  240. ?>