Beispiel #1
0
 public function onView()
 {
     // Init facebook login link
     if (FALSE && 'none' != Engine_Api::_()->getApi('settings', 'core')->core_facebook_enable) {
         $facebook = User_Model_DbTable_Facebook::getFBInstance();
         if ($facebook->getSession()) {
             try {
                 $me = $facebook->api('/me');
                 $uid = Engine_Api::_()->getDbtable('Facebook', 'User')->fetchRow(array('facebook_uid = ?' => $facebook->getUser()));
                 if ($uid) {
                     $uid = $uid->user_id;
                 }
                 if ($uid) {
                     // prevent Facebook users with established accounts from signing up again
                     Engine_Api::_()->user()->getAuth()->getStorage()->write($uid);
                     $this->getForm()->getElement('facebook')->setContent('<script type="text/javascript">window.location.reload();</script>"');
                     return;
                 } else {
                     // pre-fill facebook data into signup process
                     $this->getForm()->removeElement('facebook');
                     if ($this->getForm()->getElement('email')->getValue() == '') {
                         $this->getForm()->getElement('email')->setValue($me['email']);
                     }
                     if ($this->getForm()->getElement('username')->getValue() == '') {
                         $this->getForm()->getElement('username')->setValue(preg_replace('/[^A-Za-z]/', '', $me['name']));
                     }
                     $maps = Engine_Api::_()->fields()->getFieldsMaps('user');
                     $fb_data = array();
                     foreach (array('gender', 'first_name', 'last_name', 'birthdate') as $field_alias) {
                         if (isset($me[$field_alias])) {
                             $field = Engine_Api::_()->fields()->getFieldsObjectsByAlias('user', $field_alias);
                             $field_id = $field[$field_alias]['field_id'];
                             foreach ($maps as $map) {
                                 if ($field_id == $map->child_id) {
                                     $fb_data[$map->getKey()] = $me[$field_alias];
                                 }
                             }
                         }
                     }
                     $this->getSession()->data = $fb_data;
                 }
             } catch (Exception $e) {
                 $this->getForm()->removeElement('facebook');
             }
         }
     }
 }
Beispiel #2
0
 public function indexAction()
 {
     // Do not show if logged in
     if (Engine_Api::_()->user()->getViewer()->getIdentity()) {
         $this->setNoRender();
         return;
     }
     // Display form
     $form = $this->view->form = new User_Form_Login();
     $form->setTitle(null)->setDescription(null);
     $form->removeElement('forgot');
     // Facebook login
     if ('none' == Engine_Api::_()->getApi('settings', 'core')->core_facebook_enable) {
         $form->removeElement('facebook');
     } else {
         if ($form->getElement('facebook')) {
             $content = $form->getElement('facebook')->getContent();
             $content = str_replace('FB.Event.subscribe', 'FB.Event.subscribe(\'fb.log\', function(response) {
                              window.location.reload();
                             });
                             FB.Event.subscribe', $content);
             $content = str_replace('window.location.reload();', sprintf('window.location.href = "%s";', Zend_Controller_Front::getInstance()->getRouter()->assemble(array(), 'user_login')), $content);
             $form->getElement('facebook')->setContent($content);
         }
         $facebook = User_Model_DbTable_Facebook::getFBInstance();
         if ($facebook->getSession()) {
             try {
                 $me = $facebook->api('/me');
                 $uid = Engine_Api::_()->getDbtable('Facebook', 'User')->fetchRow(array('facebook_uid = ?' => $facebook->getUser()));
                 if ($uid) {
                     $uid = $uid->user_id;
                 }
                 if ($uid) {
                     // already integrated user account; sign in
                     Engine_Api::_()->user()->getAuth()->getStorage()->write($uid);
                 } else {
                     $form->removeElement('facebook');
                     //$form->setAction($this->view->url(array('controller'=>'settings','action'=>'general'), 'user_extended'));
                     $form->addNotice($this->view->translate('USER_FORM_AUTH_FACEBOOK_NOACCOUNT', $this->view->url(array(), 'user_signup'), $this->view->url(array('controller' => 'settings', 'action' => 'general'), 'user_extended')));
                 }
             } catch (Facebook_Exception $e) {
             }
         }
     }
 }
Beispiel #3
0
 public function onUserCreateAfter($event)
 {
     $payload = $event->getPayload();
     if ($payload instanceof User_Model_User && 'none' != Engine_Api::_()->getApi('settings', 'core')->core_facebook_enable) {
         $facebook = User_Model_DbTable_Facebook::getFBInstance();
         if ($facebook->getSession()) {
             try {
                 $facebook->api('/me');
                 $table = Engine_Api::_()->getDbtable('facebook', 'user');
                 $row = $table->fetchRow(array('user_id = ?' => $payload->getIdentity()));
                 if (!$row) {
                     $row = Engine_Api::_()->getDbtable('facebook', 'user')->createRow();
                     $row->user_id = $payload->getIdentity();
                 }
                 $row->facebook_uid = $facebook->getUser();
                 $row->save();
             } catch (Exception $e) {
             }
         }
     }
 }
Beispiel #4
0
 public function init()
 {
     $this->setTitle('Share')->setDescription('Share this by re-posting it with your own message.')->setMethod('POST')->setAction(Zend_Controller_Front::getInstance()->getRouter()->assemble(array()));
     $this->addElement('Textarea', 'body', array('filters' => array(new Engine_Filter_HtmlSpecialChars(), new Engine_Filter_EnableLinks(), new Engine_Filter_Censor())));
     // Buttons
     $buttons = array();
     if ('publish' == Engine_Api::_()->getApi('settings', 'core')->core_facebook_enable && User_Model_DbTable_Facebook::getFBInstance()->getSession()) {
         $this->addElement('Dummy', 'post_to_facebook', array('content' => '
       <span href="javascript:void(0);" class="composer_facebook_toggle" onclick="toggleFacebookShareCheckbox();">
         <span class="composer_facebook_tooltip">
           Publish this on Facebook
         </span>
         <input type="checkbox" name="post_to_facebook" value="1" style="display:none;">
       </span>'));
         $this->getElement('post_to_facebook')->clearDecorators();
         $buttons[] = 'post_to_facebook';
     }
     $this->addElement('Button', 'submit', array('label' => 'Share', 'type' => 'submit', 'ignore' => true, 'decorators' => array('ViewHelper')));
     $buttons[] = 'submit';
     $this->addElement('Cancel', 'cancel', array('label' => 'cancel', 'link' => true, 'prependText' => ' or ', 'href' => '', 'onclick' => 'parent.Smoothbox.close();', 'decorators' => array('ViewHelper')));
     $buttons[] = 'cancel';
     $this->addDisplayGroup($buttons, 'buttons');
     $button_group = $this->getDisplayGroup('buttons');
 }
Beispiel #5
0
 public function generalAction()
 {
     // Config vars
     $user = $this->_helper->api()->core()->getSubject();
     $this->view->form = $form = new User_Form_Settings_General(array('item' => $user));
     // Set up profile type options
     /*
         $aliasedFields = $user->fields()->getFieldsObjectsByAlias();
         if( isset($aliasedFields['profile_type']) )
         {
           $options = $aliasedFields['profile_type']->getElementParams($user);
           unset($options['options']['order']);
           $form->accountType->setOptions($options['options']);
         }
         else
         { */
     $form->removeElement('accountType');
     /* } */
     // Removed disabled features
     if (!Engine_Api::_()->authorization()->isAllowed('user', $user, 'username')) {
         $form->removeElement('username');
     }
     // Facebook
     if ('none' != Engine_Api::_()->getApi('settings', 'core')->getSetting('core.facebook.enable', 'none')) {
         $facebook = User_Model_DbTable_Facebook::getFBInstance();
         if ($facebook->getSession()) {
             $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;
             }
             try {
                 $facebook->api('/me');
                 if ($fb_uid && $facebook->getUser() != $fb_uid) {
                     $form->removeElement('facebook_id');
                     $form->getElement('facebook')->addError('You appear to be logged into a different Facebook account than what was registered with this account.  Please log out of Facebook using the button below to log into your correct Facebook account.');
                     $form->getElement('facebook')->setContent($this->view->translate('<button onclick="window.location.href=this.value;return false;" value="%s">Logout of Facebook</button>', $facebook->getLogoutUrl()));
                 } else {
                     $form->removeElement('facebook');
                     $form->getElement('facebook_id')->setAttrib('checked', (bool) $fb_uid);
                 }
             } catch (Exception $e) {
                 $form->removeElement('facebook');
                 $form->removeElement('facebook_id');
             }
         } else {
             @$form->removeElement('facebook_id');
         }
     } else {
         // these should already be removed inside the form, but lets do it again.
         @$form->removeElement('facebook');
         @$form->removeElement('facebook_id');
     }
     // Check if post and populate
     if (!$this->getRequest()->isPost()) {
         $form->populate($user->toArray());
         $this->view->status = false;
         $this->view->error = Zend_Registry::get('Zend_Translate')->_('Invalid method');
         return;
     }
     // Check if valid
     if (!$form->isValid($this->getRequest()->getPost())) {
         $this->view->status = false;
         $this->view->error = Zend_Registry::get('Zend_Translate')->_('Invalid data');
         return;
     }
     // -- Process --
     // Set values for user object
     $user->setFromArray($form->getValues());
     $user->save();
     // Update account type
     /*
         $accountType = $form->getValue('accountType');
         if( isset($aliasedFields['profile_type']) )
         {
      $valueRow = $aliasedFields['profile_type']->getValue($user);
      if( null === $valueRow ) {
        $valueRow = Engine_Api::_()->fields()->getTable('user', 'values')->createRow();
        $valueRow->field_id = $aliasedFields['profile_type']->field_id;
        $valueRow->item_id = $user->getIdentity();
      }
      $valueRow->value = $accountType;
      $valueRow->save();
         }
     *
     */
     // Update facebook settings
     if (isset($facebook) && $form->getElement('facebook_id')) {
         if ($facebook->getSession()) {
             try {
                 $facebook->api('/me');
                 $uid = Engine_Api::_()->user()->getViewer()->getIdentity();
                 $table = Engine_Api::_()->getDbtable('facebook', 'user');
                 $row = $table->find($uid)->current();
                 if (!$row) {
                     $row = $table->createRow();
                     $row->user_id = $uid;
                 }
                 $row->facebook_uid = $this->getRequest()->getPost('facebook_id') ? $facebook->getUser() : 0;
                 $row->save();
                 $form->removeElement('facebook');
             } catch (Exception $e) {
             }
         }
     }
     // Send success message
     $this->view->status = true;
     $this->view->message = Zend_Registry::get('Zend_Translate')->_('Settings saved.');
     $form->addNotice(Zend_Registry::get('Zend_Translate')->_('Settings were successfully saved.'));
 }
Beispiel #6
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('')));
         }
     }
 }
Beispiel #7
0
 public function facebookSuccessAction()
 {
     $code = $this->_getParam('code');
     if ('none' == Engine_Api::_()->getApi('settings', 'core')->core_facebook_enable) {
         $form->removeElement('facebook');
     } else {
         $facebook = User_Model_DbTable_Facebook::getFBInstance();
         if ($facebook->getSession()) {
             die("hi facebooker");
         }
     }
     if (!$code) {
         $this->_forward('login');
         return;
     }
     $access_token = User_Model_DbTable_Facebook::getAccessToken($code);
 }