X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=lib%2Ffacebookaction.php;h=043a078cd5bbd2fa8b0849fa7dc3c5238927cc3f;hb=a13bfa5848705d23353783f195b67c088a72a8cb;hp=43464b19b49168fc3ac947f00bbdcdece176d520;hpb=04ef1ba8eee7a9e2a565d7b4b747ef607665d562;p=quix0rs-gnu-social.git diff --git a/lib/facebookaction.php b/lib/facebookaction.php index 43464b19b4..043a078cd5 100644 --- a/lib/facebookaction.php +++ b/lib/facebookaction.php @@ -1,293 +1,750 @@ . + * along with this program. If not, see . + * + * @category Faceboook + * @package Laconica + * @author Zach Copley + * @copyright 2008 Control Yourself, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://laconi.ca/ */ -if (!defined('LACONICA')) { exit(1); } +if (!defined('LACONICA')) +{ + exit(1); +} -require_once(INSTALLDIR.'/extlib/facebook/facebook.php'); +require_once INSTALLDIR.'/lib/facebookutil.php'; +require_once INSTALLDIR.'/lib/noticeform.php'; + + +class FacebookAction extends Action +{ + + var $facebook = null; + var $fbuid = null; + var $flink = null; + var $action = null; + var $app_uri = null; + var $app_name = null; + + /** + * Constructor + * + * Just wraps the HTMLOutputter constructor. + * + * @param string $output URI to output to, default = stdout + * @param boolean $indent Whether to indent output, default true + * + * @see XMLOutputter::__construct + * @see HTMLOutputter::__construct + */ + function __construct($output='php://output', $indent=true, $facebook=null, $flink=null) + { + parent::__construct($output, $indent); + + $this->facebook = $facebook; + $this->flink = $flink; + + if ($this->flink) { + $this->fbuid = $flink->foreign_id; + $this->user = $flink->getUser(); + } + + $this->args = array(); + } + + function prepare($argarray) + { + parent::prepare($argarray); + + $this->facebook = getFacebook(); + $this->fbuid = $this->facebook->require_login(); + + $this->action = $this->trimmed('action'); + + $app_props = $this->facebook->api_client->Admin_getAppProperties( + array('canvas_name', 'application_name')); + + $this->app_uri = 'http://apps.facebook.com/' . $app_props['canvas_name']; + $this->app_name = $app_props['application_name']; + + $this->flink = Foreign_link::getByForeignID($this->fbuid, FACEBOOK_SERVICE); + + return true; + + } + + function showStylesheets() + { + // Add a timestamp to the file so Facebook cache wont ignore our changes + $ts = filemtime(INSTALLDIR.'/theme/base/css/display.css'); + + $this->element('link', array('rel' => 'stylesheet', + 'type' => 'text/css', + 'href' => theme_path('css/display.css', 'base') . '?ts=' . $ts)); + + $theme = common_config('site', 'theme'); + + $ts = filemtime(INSTALLDIR. '/theme/' . $theme .'/css/display.css'); + + $this->element('link', array('rel' => 'stylesheet', + 'type' => 'text/css', + 'href' => theme_path('css/display.css', null) . '?ts=' . $ts)); + + $ts = filemtime(INSTALLDIR.'/theme/base/css/facebookapp.css'); + + $this->element('link', array('rel' => 'stylesheet', + 'type' => 'text/css', + 'href' => theme_path('css/facebookapp.css', 'base') . '?ts=' . $ts)); + } + + function showScripts() + { + // Add a timestamp to the file so Facebook cache wont ignore our changes + $ts = filemtime(INSTALLDIR.'/js/facebookapp.js'); + + $this->element('script', array('src' => common_path('js/facebookapp.js') . '?ts=' . $ts)); + } + + /** + * Start an Facebook ready HTML document + * + * For Facebook we don't want to actually output any headers, + * DTD info, etc. Just Stylesheet and JavaScript links. + * + * If $type isn't specified, will attempt to do content negotiation. + * + * @param string $type MIME type to use; default is to do negotation. + * + * @return void + */ + + function startHTML($type=null) + { + $this->showStylesheets(); + $this->showScripts(); + + $this->elementStart('div', array('class' => 'facebook-page')); + } -class FacebookAction extends Action { + /** + * Ends a Facebook ready HTML document + * + * @return void + */ + function endHTML() + { + $this->elementEnd('div'); + $this->endXML(); + } - function handle($args) + /** + * Show notice form. + * + * MAY overload if no notice form needed... or direct message box???? + * + * @return nothing + */ + function showNoticeForm() { - parent::handle($args); + // don't do it for most of the Facebook pages } - function get_facebook() + function showBody() + { + $this->elementStart('div', array('id' => 'wrap')); + $this->showHeader(); + $this->showCore(); + $this->showFooter(); + $this->elementEnd('div'); + } + + function showAside() { - $apikey = common_config('facebook', 'apikey'); - $secret = common_config('facebook', 'secret'); - return new Facebook($apikey, $secret); } - function update_profile_box($facebook, $fbuid, $user) + function showHead($error, $success) { + + if ($error) { + $this->element("h1", null, $error); + } + + if ($success) { + $this->element("h1", null, $success); + } - $notice = $user->getCurrentNotice(); + $this->elementStart('fb:if-section-not-added', array('section' => 'profile')); + $this->elementStart('span', array('id' => 'add_to_profile')); + $this->element('fb:add-section-button', array('section' => 'profile')); + $this->elementEnd('span'); + $this->elementEnd('fb:if-section-not-added'); + + } - # Need to include inline CSS for styling the Profile box + + // Make this into a widget later + function showLocalNav() + { + $this->elementStart('ul', array('class' => 'nav')); + + $this->elementStart('li', array('class' => + ($this->action == 'facebookhome') ? 'current' : 'facebook_home')); + $this->element('a', + array('href' => 'index.php', 'title' => _('Home')), _('Home')); + $this->elementEnd('li'); + + $this->elementStart('li', + array('class' => + ($this->action == 'facebookinvite') ? 'current' : 'facebook_invite')); + $this->element('a', + array('href' => 'invite.php', 'title' => _('Invite')), _('Invite')); + $this->elementEnd('li'); + + $this->elementStart('li', + array('class' => + ($this->action == 'facebooksettings') ? 'current' : 'facebook_settings')); + $this->element('a', + array('href' => 'settings.php', + 'title' => _('Settings')), _('Settings')); + $this->elementEnd('li'); + + $this->elementEnd('ul'); + } + + /** + * Show header of the page. + * + * Calls template methods + * + * @return nothing + */ + function showHeader() + { + $this->elementStart('div', array('id' => 'header')); + $this->showLogo(); + $this->showNoticeForm(); + $this->elementEnd('div'); + } + + /** + * Show page, a template method. + * + * @return nothing + */ + function showPage($error = null, $success = null) + { + $this->startHTML(); + $this->showHead($error, $success); + $this->showBody(); + $this->endHTML(); + } + - $style = ''; + function showInstructions() + { - $html = $this->render_notice($notice); + $this->elementStart('div', array('class' => 'facebook_guide')); - $fbml = "$content $html"; - $fbml .= "$content $html"; + $this->elementStart('dl', array('class' => 'system_notice')); + $this->element('dt', null, 'Page Notice'); - $fbml_main = "$content $html"; + $loginmsg_part1 = _('To use the %s Facebook Application you need to login ' . + 'with your username and password. Don\'t have a username yet? '); + $loginmsg_part2 = _(' a new account.'); - $facebook->api_client->profile_setFBML(null, $fbuid, $fbml, null, null, $fbml_main); + $this->elementStart('dd'); + $this->elementStart('p'); + $this->text(sprintf($loginmsg_part1, common_config('site', 'name'))); + $this->element('a', + array('href' => common_local_url('register')), _('Register')); + $this->text($loginmsg_part2); + $this->elementEnd('p'); + $this->elementEnd('dd'); + + $this->elementEnd('dl'); + $this->elementEnd('div'); } - # Display methods - function show_header($selected ='Home') + function showLoginForm($msg = null) { - # Add a timestamp to the CSS file so Facebook cache wont ignore our changes - $ts = filemtime(theme_file('facebookapp.css')); - $cssurl = theme_path('facebookapp.css') . "?ts=$ts"; + $this->elementStart('div', array('id' => 'content')); + $this->element('h1', null, _('Login')); - $header = ''; - # $header .=''; - $header .= ''; + if ($msg) { + $this->element('fb:error', array('message' => $msg)); + } - $header .= - '' - .'' - .'' - .'' - .''; - $header .= '
'; + $this->showInstructions(); - echo $header; + $this->elementStart('div', array('id' => 'content_inner')); - } + $this->elementStart('form', array('method' => 'post', + 'class' => 'form_settings', + 'id' => 'login', + 'action' => 'index.php')); - function show_footer() - { - $footer = '
'; - echo $footer; - } + $this->elementStart('fieldset'); - function show_login_form() - { + $this->elementStart('ul', array('class' => 'form_datas')); + $this->elementStart('li'); + $this->input('nickname', _('Nickname')); + $this->elementEnd('li'); + $this->elementStart('li'); + $this->password('password', _('Password')); + $this->elementEnd('li'); + $this->elementEnd('ul'); - $loginform = - '

To add the Identi.ca application, you need to log into your Identi.ca account.

' - .'' - .' ' - .'' - .'

Login

' - .'
' - .'

Login with your username and password. Don\'t have a username yet?' - .' Register a new account.' - .'

' - .'
' - .'
' - .'
' - .'

' - .' ' - .' ' - .'

' - .'

' - .' ' - .' ' - .'

' - .'

' - .' ' - .'

' - .'
' - .'

' - .' Lost or forgotten password?' - .'

' - .'submit('submit', _('Login')); + $this->elementEnd('fieldset'); + $this->elementEnd('form'); - function render_notice($notice) - { + $this->elementStart('p'); + $this->element('a', array('href' => common_local_url('recoverpassword')), + _('Lost or forgotten password?')); + $this->elementEnd('p'); - global $config; + $this->elementEnd('div'); + $this->elementEnd('div'); - $profile = $notice->getProfile(); - $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE); + } + + + function updateProfileBox($notice) + { - $noticeurl = common_local_url('shownotice', array('notice' => $notice->id)); + // Need to include inline CSS for styling the Profile box - # XXX: we need to figure this out better. Is this right? - if (strcmp($notice->uri, $noticeurl) != 0 && preg_match('/^http/', $notice->uri)) { - $noticeurl = $notice->uri; + $app_props = $this->facebook->api_client->Admin_getAppProperties(array('icon_url')); + $icon_url = $app_props['icon_url']; + + $style = ''; + + $this->xw->openMemory(); + + $item = new FacebookProfileBoxNotice($notice, $this); + $item->show(); + + $fbml = "$style " . $this->xw->outputMemory(false) . ""; + $fbml .= "$style " . $this->xw->outputMemory(false) . ""; + + $fbml_main = "$style " . $this->xw->outputMemory(false) . ""; + + $this->facebook->api_client->profile_setFBML(null, $this->fbuid, $fbml, null, null, $fbml_main); + + $this->xw->openURI('php://output'); + } + + + /** + * Generate pagination links + * + * @param boolean $have_before is there something before? + * @param boolean $have_after is there something after? + * @param integer $page current page + * @param string $action current action + * @param array $args rest of query arguments + * + * @return nothing + */ + function pagination($have_before, $have_after, $page, $action, $args=null) + { + // Does a little before-after block for next/prev page + if ($have_before || $have_after) { + $this->elementStart('div', array('class' => 'pagination')); + $this->elementStart('dl', null); + $this->element('dt', null, _('Pagination')); + $this->elementStart('dd', null); + $this->elementStart('ul', array('class' => 'nav')); + } + if ($have_before) { + $pargs = array('page' => $page-1); + $newargs = $args ? array_merge($args, $pargs) : $pargs; + $this->elementStart('li', array('class' => 'nav_prev')); + $this->element('a', array('href' => "$this->app_uri/$action?page=$newargs[page]", 'rel' => 'prev'), + _('After')); + $this->elementEnd('li'); + } + if ($have_after) { + $pargs = array('page' => $page+1); + $newargs = $args ? array_merge($args, $pargs) : $pargs; + $this->elementStart('li', array('class' => 'nav_next')); + $this->element('a', array('href' => "$this->app_uri/$action?page=$newargs[page]", 'rel' => 'next'), + _('Before')); + $this->elementEnd('li'); + } + if ($have_before || $have_after) { + $this->elementEnd('ul'); + $this->elementEnd('dd'); + $this->elementEnd('dl'); + $this->elementEnd('div'); + } + } + + function updateFacebookStatus($notice) + { + $prefix = $this->facebook->api_client->data_getUserPreference(FACEBOOK_NOTICE_PREFIX, $this->fbuid); + $content = "$prefix $notice->content"; + + if ($this->facebook->api_client->users_hasAppPermission('status_update', $this->fbuid)) { + $this->facebook->api_client->users_setStatus($content, $this->fbuid, false, true); } + } + + function saveNewNotice() + { - $html = - '
  • ' - .'' - .'';
+        $inter = new CommandInterpreter();
 
-        if ($profile->fullname) {
-            $html .= $profile->fullname;
-        } else {
-            $html .= $profile->nickname;
+        $cmd = $inter->handle_command($user, $content_shortened);
+
+        if ($cmd) {
+            
+            // XXX fix this
+            
+            $cmd->execute(new WebChannel());
+            return;
         }
 
-        $html .=
-        '' - .'' . $profile->nickname . '' - .'

    ' . $notice->rendered . '

    ' - .'

    ' - .''; + $replyto = $this->trimmed('inreplyto'); - if ($notice->source) { - $html .= _(' from '); - $html .= $this->source_link($notice->source); - } + $notice = Notice::saveNew($user->id, $content, + 'Facebook', 1, ($replyto == 'false') ? null : $replyto); - if ($notice->reply_to) { - $replyurl = common_local_url('shownotice', array('notice' => $notice->reply_to)); - $html .= - ' (' . _('in reply to...') . ')'; + if (is_string($notice)) { + $this->showPage($notice); + return; } - $html .= '

  • '; + common_broadcast_notice($notice); + + // Also update the user's Facebook status + $this->updateFacebookStatus($notice); + $this->updateProfileBox($notice); + + } + +} - return $html; +class FacebookNoticeForm extends NoticeForm +{ + + var $post_action = null; + + /** + * Constructor + * + * @param HTMLOutputter $out output channel + * @param string $action action to return to, if any + * @param string $content content to pre-fill + */ + + function __construct($out=null, $action=null, $content=null, + $post_action=null, $user=null) + { + parent::__construct($out, $action, $content, $user); + $this->post_action = $post_action; + } + + /** + * Action of the form + * + * @return string URL of the action + */ + + function action() + { + return $this->post_action; } - function source_link($source) +} + +class FacebookNoticeList extends NoticeList +{ + + /** + * constructor + * + * @param Notice $notice stream of notices from DB_DataObject + */ + + function __construct($notice, $out=null) { - $source_name = _($source); - - $html = ''; - - switch ($source) { - case 'web': - case 'xmpp': - case 'mail': - case 'omb': - case 'api': - $html .= $source_name; - break; - default: - $ns = Notice_source::staticGet($source); - if ($ns) { - $html .= '' . $ns->name . ''; - } else { - $html .= $source_name; + parent::__construct($notice, $out); + } + + /** + * show the list of notices + * + * "Uses up" the stream by looping through it. So, probably can't + * be called twice on the same list. + * + * @return int count of notices listed. + */ + + function show() + { + $this->out->elementStart('div', array('id' =>'notices_primary')); + $this->out->element('h2', null, _('Notices')); + $this->out->elementStart('ul', array('class' => 'notices')); + + $cnt = 0; + + while ($this->notice->fetch() && $cnt <= NOTICES_PER_PAGE) { + $cnt++; + + if ($cnt > NOTICES_PER_PAGE) { + break; } - break; + + $item = $this->newListItem($this->notice); + $item->show(); } - $html .= ''; + $this->out->elementEnd('ul'); + $this->out->elementEnd('div'); - return $html; + return $cnt; } - function pagination($have_before, $have_after, $page, $fbaction, $args=null) + /** + * returns a new list item for the current notice + * + * Overridden to return a Facebook specific list item. + * + * @param Notice $notice the current notice + * + * @return FacebookNoticeListItem a list item for displaying the notice + * formatted for display in the Facebook App. + */ + + function newListItem($notice) { + return new FacebookNoticeListItem($notice, $this); + } - $html = ''; +} - if ($have_before || $have_after) { - $html = '