示例#1
0
 public function shareAction()
 {
     if (!$this->_helper->requireUser()->isValid()) {
         return;
     }
     $type = $this->_getParam('type');
     $id = $this->_getParam('id');
     $viewer = Engine_Api::_()->user()->getViewer();
     $this->view->attachment = $attachment = Engine_Api::_()->getItem($type, $id);
     $this->view->form = $form = new Activity_Form_Share();
     if (!$attachment) {
         // tell smoothbox to close
         $this->view->status = true;
         $this->view->message = Zend_Registry::get('Zend_Translate')->_('You cannot share this item because it has been removed.');
         $this->view->smoothboxClose = true;
         return $this->render('deletedItem');
     }
     // hide facebook option if not logged in, or logged into wrong FB account
     if (true) {
         $facebook = User_Model_DbTable_Facebook::getFBInstance();
         if (!$facebook->getSession()) {
             $form->removeElement('post_to_facebook');
         } else {
             try {
                 $facebook->api('/me');
                 $fb_uid = Engine_Api::_()->getDbtable('Facebook', 'User')->fetchRow(array('user_id = ?' => Engine_Api::_()->user()->getViewer()->getIdentity()));
                 if ($fb_uid && $fb_uid->facebook_uid) {
                     $fb_uid = $fb_uid->facebook_uid;
                 } else {
                     $fb_uid = null;
                 }
                 if (!$fb_uid || $fb_uid != $facebook->getUser()) {
                     throw new Exception('User logged into a Facebook account other than the attached account.');
                 }
             } catch (Exception $e) {
                 $form->removeElement('post_to_facebook');
             }
         }
     }
     if (!$this->getRequest()->isPost()) {
         return;
     }
     if (!$form->isValid($this->getRequest()->getPost())) {
         return;
     }
     // Process
     $db = Engine_Api::_()->getDbtable('actions', 'activity')->getAdapter();
     $db->beginTransaction();
     try {
         // Get body
         $body = $form->getValue('body');
         // Add activity
         $api = $this->_helper->api()->getDbtable('actions', 'activity');
         $action = $api->addActivity($viewer, $viewer, 'post_self', $body);
         $api->attachActivity($action, $attachment);
         $db->commit();
         // Publish to facebook, if checked & enabled
         if ($this->_getParam('post_to_facebook', false) && 'publish' == Engine_Api::_()->getApi('settings', 'core')->core_facebook_enable) {
             $fb_uid = Engine_Api::_()->getDbtable('facebook', 'user')->fetchRow(array('user_id = ?' => Engine_Api::_()->user()->getViewer()->getIdentity()));
             if ($fb_uid && $fb_uid->facebook_uid) {
                 $fb_uid = $fb_uid->facebook_uid;
                 $facebook = User_Model_DbTable_Facebook::getFBInstance();
                 if ($facebook->getSession()) {
                     try {
                         $facebook->api('/me');
                         if ($fb_uid != $facebook->getUser()) {
                             throw new Exception('Unable to post to Facebook account; a different account is assigned to the user.');
                         }
                         $url = 'http://' . $_SERVER['HTTP_HOST'] . $this->getFrontController()->getBaseUrl();
                         $name = 'Activity Feed';
                         $desc = '';
                         $picUrl = null;
                         if ($attachment) {
                             $url = 'http://' . $_SERVER['HTTP_HOST'] . $attachment->getHref();
                             $desc = $attachment->getDescription();
                             $name = $attachment->getTitle();
                             if (empty($name)) {
                                 $name = ucwords($attachment->getShortType());
                             }
                             $picUrl = $attachment->getPhotoUrl();
                             if ($picUrl) {
                                 $picUrl = 'http://' . $_SERVER['HTTP_HOST'] . $picUrl;
                             }
                         }
                         // include the site name with the post:
                         $name = Engine_Api::_()->getApi('settings', 'core')->core_general_site_title . ": {$name}";
                         $fb_data = array('message' => $form->getValue('body'), 'link' => $url, 'name' => $name, 'description' => $desc);
                         if ($picUrl) {
                             $fb_data = array_merge($fb_data, array('picture' => $picUrl));
                         }
                         $res = $facebook->api('/me/feed', 'POST', $fb_data);
                     } catch (Exception $e) {
                         /* do nothing */
                     }
                 }
             }
         }
         // end Facebook
     } catch (Exception $e) {
         $db->rollBack();
         throw $e;
         // This should be caught by error handler
     }
     // If we're here, we're done
     $this->view->status = true;
     $this->view->message = Zend_Registry::get('Zend_Translate')->_('Success!');
     // Redirect if in normal context
     if (null === $this->_helper->contextSwitch->getCurrentContext()) {
         $return_url = $form->getValue('return_url', false);
         if (!$return_url) {
             $return_url = $this->view->url(array(), 'default', true);
         }
         return $this->_helper->redirector->gotoUrl($return_url, array('prependBase' => false));
     } else {
         if ('smoothbox' === $this->_helper->contextSwitch->getCurrentContext()) {
             $this->_forward('success', 'utility', 'core', array('smoothboxClose' => 10, 'parentRefresh' => 10, 'messages' => array('')));
         }
     }
 }
示例#2
0
 /**
  * @url : http://localhost/pcus907/ynmember/member/share/type/user/id/1/format/smoothbox
  * Sharing member
  * @throws Exception
  */
 public function shareAction()
 {
     if (!$this->_helper->requireUser()->isValid()) {
         return;
     }
     $type = $this->_getParam('type');
     $id = $this->_getParam('id');
     $viewer = Engine_Api::_()->user()->getViewer();
     $this->view->attachment = $attachment = Engine_Api::_()->getItem($type, $id);
     $this->view->form = $form = new Activity_Form_Share();
     if (!$attachment) {
         // tell smoothbox to close
         $this->view->status = true;
         $this->view->message = Zend_Registry::get('Zend_Translate')->_('You cannot share this item because it has been removed.');
         $this->view->smoothboxClose = true;
         return $this->render('deletedItem');
     }
     // hide facebook and twitter option if not logged in
     $facebookTable = Engine_Api::_()->getDbtable('facebook', 'user');
     if (!$facebookTable->isConnected()) {
         $form->removeElement('post_to_facebook');
     }
     $twitterTable = Engine_Api::_()->getDbtable('twitter', 'user');
     if (!$twitterTable->isConnected()) {
         $form->removeElement('post_to_twitter');
     }
     if (!$this->getRequest()->isPost()) {
         return;
     }
     if (!$form->isValid($this->getRequest()->getPost())) {
         return;
     }
     // Process
     $db = Engine_Api::_()->getDbtable('actions', 'activity')->getAdapter();
     $db->beginTransaction();
     try {
         // Get body
         $body = $form->getValue('body');
         // Set Params for Attachment
         $params = array();
         // Add activity
         $api = Engine_Api::_()->getDbtable('actions', 'activity');
         //$action = $api->addActivity($viewer, $viewer, 'post_self', $body);
         $action = $api->addActivity($viewer, $attachment->getOwner(), 'ynmember_share', $body, $params);
         if ($action) {
             $api->attachActivity($action, $attachment);
         }
         $db->commit();
         // Notifications
         $notifyApi = Engine_Api::_()->getDbtable('notifications', 'activity');
         // Add notification for owner of activity (if user and not viewer)
         if ($action->subject_type == 'user' && $attachment->getOwner()->getIdentity() != $viewer->getIdentity()) {
             $notifyApi->addNotification($attachment->getOwner(), $viewer, $action, 'ynmember_shared', array('label' => $this->view->translate('you')));
         }
         // Preprocess attachment parameters
         $publishMessage = html_entity_decode($form->getValue('body'));
         $publishUrl = null;
         $publishName = null;
         $publishDesc = null;
         $publishPicUrl = null;
         // Add attachment
         if ($attachment) {
             $publishUrl = $attachment->getHref();
             $publishName = $attachment->getTitle();
             $publishDesc = $attachment->getDescription();
             if (empty($publishName)) {
                 $publishName = ucwords($attachment->getShortType());
             }
             if ($tmpPicUrl = $attachment->getPhotoUrl()) {
                 $publishPicUrl = $tmpPicUrl;
             }
             // prevents OAuthException: (#100) FBCDN image is not allowed in stream
             if ($publishPicUrl && preg_match('/fbcdn.net$/i', parse_url($publishPicUrl, PHP_URL_HOST))) {
                 $publishPicUrl = null;
             }
         } else {
             $publishUrl = $action->getHref();
         }
         // Check to ensure proto/host
         if ($publishUrl && false === stripos($publishUrl, 'http://') && false === stripos($publishUrl, 'https://')) {
             $publishUrl = 'http://' . $_SERVER['HTTP_HOST'] . $publishUrl;
         }
         if ($publishPicUrl && false === stripos($publishPicUrl, 'http://') && false === stripos($publishPicUrl, 'https://')) {
             $publishPicUrl = 'http://' . $_SERVER['HTTP_HOST'] . $publishPicUrl;
         }
         // Add site title
         if ($publishName) {
             $publishName = Engine_Api::_()->getApi('settings', 'core')->core_general_site_title . ": " . $publishName;
         } else {
             $publishName = Engine_Api::_()->getApi('settings', 'core')->core_general_site_title;
         }
         // Publish to facebook, if checked & enabled
         if ($this->_getParam('post_to_facebook', false) && 'publish' == Engine_Api::_()->getApi('settings', 'core')->core_facebook_enable) {
             try {
                 $facebookTable = Engine_Api::_()->getDbtable('facebook', 'user');
                 $facebookApi = $facebook = $facebookTable->getApi();
                 $fb_uid = $facebookTable->find($viewer->getIdentity())->current();
                 if ($fb_uid && $fb_uid->facebook_uid && $facebookApi && $facebookApi->getUser() && $facebookApi->getUser() == $fb_uid->facebook_uid) {
                     $fb_data = array('message' => $publishMessage);
                     if ($publishUrl) {
                         $fb_data['link'] = $publishUrl;
                     }
                     if ($publishName) {
                         $fb_data['name'] = $publishName;
                     }
                     if ($publishDesc) {
                         $fb_data['description'] = $publishDesc;
                     }
                     if ($publishPicUrl) {
                         $fb_data['picture'] = $publishPicUrl;
                     }
                     $res = $facebookApi->api('/me/feed', 'POST', $fb_data);
                 }
             } catch (Exception $e) {
                 // Silence
             }
         }
         // end Facebook
         // Publish to twitter, if checked & enabled
         if ($this->_getParam('post_to_twitter', false) && 'publish' == Engine_Api::_()->getApi('settings', 'core')->core_twitter_enable) {
             try {
                 $twitterTable = Engine_Api::_()->getDbtable('twitter', 'user');
                 if ($twitterTable->isConnected()) {
                     // Get attachment info
                     $title = $attachment->getTitle();
                     $url = $attachment->getHref();
                     $picUrl = $attachment->getPhotoUrl();
                     // Check stuff
                     if ($url && false === stripos($url, 'http://')) {
                         $url = 'http://' . $_SERVER['HTTP_HOST'] . $url;
                     }
                     if ($picUrl && false === stripos($picUrl, 'http://')) {
                         $picUrl = 'http://' . $_SERVER['HTTP_HOST'] . $picUrl;
                     }
                     // Try to keep full message
                     // @todo url shortener?
                     $message = html_entity_decode($form->getValue('body'));
                     if (strlen($message) + strlen($title) + strlen($url) + strlen($picUrl) + 9 <= 140) {
                         if ($title) {
                             $message .= ' - ' . $title;
                         }
                         if ($url) {
                             $message .= ' - ' . $url;
                         }
                         if ($picUrl) {
                             $message .= ' - ' . $picUrl;
                         }
                     } else {
                         if (strlen($message) + strlen($title) + strlen($url) + 6 <= 140) {
                             if ($title) {
                                 $message .= ' - ' . $title;
                             }
                             if ($url) {
                                 $message .= ' - ' . $url;
                             }
                         } else {
                             if (strlen($title) > 24) {
                                 $title = Engine_String::substr($title, 0, 21) . '...';
                             }
                             // Sigh truncate I guess
                             if (strlen($message) + strlen($title) + strlen($url) + 9 > 140) {
                                 $message = Engine_String::substr($message, 0, 140 - (strlen($title) + strlen($url) + 9)) - 3 . '...';
                             }
                             if ($title) {
                                 $message .= ' - ' . $title;
                             }
                             if ($url) {
                                 $message .= ' - ' . $url;
                             }
                         }
                     }
                     $twitter = $twitterTable->getApi();
                     $twitter->statuses->update($message);
                 }
             } catch (Exception $e) {
                 // Silence
             }
         }
         // Publish to janrain
         if ('publish' == Engine_Api::_()->getApi('settings', 'core')->core_janrain_enable) {
             try {
                 $session = new Zend_Session_Namespace('JanrainActivity');
                 $session->unsetAll();
                 $session->message = $publishMessage;
                 $session->url = $publishUrl ? $publishUrl : 'http://' . $_SERVER['HTTP_HOST'] . _ENGINE_R_BASE;
                 $session->name = $publishName;
                 $session->desc = $publishDesc;
                 $session->picture = $publishPicUrl;
             } catch (Exception $e) {
                 // Silence
             }
         }
     } catch (Exception $e) {
         $db->rollBack();
         throw $e;
         // This should be caught by error handler
     }
     // Disable layout and viewrenderer
     //$this -> _helper -> layout -> disableLayout();
     return $this->_forward('success', 'utility', 'core', array('smoothboxClose' => true, 'parentRefresh' => false, 'layout' => 'default-simple', 'messages' => array(Zend_Registry::get('Zend_Translate')->_('Shared This Member Successfully!'))));
     exit;
 }