Exemplo n.º 1
0
/**
 * Register with facebook
 */
function registerWithFacebook()
{
    // flush if already logged in
    Zend_Auth::getInstance()->clearIdentity();
    $session = new Zend_Session_Namespace('Default');
    $email = $session->fb_user_email;
    $avatar = $session->fb_avatar;
    // do not allow direct access - without fb_user_email inside session
    if (!$session->fb_user_email) {
        Zend_Controller_Action_HelperBroker::getStaticHelper('Redirector')->gotoUrl('');
    }
    require_once 'Form.php';
    $registerwithfacebook_form = new Addon_FacebookRegisterForm();
    $Profiles = new Application_Model_Profiles();
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        if ($registerwithfacebook_form->isValid($_POST)) {
            $name = $registerwithfacebook_form->getValue('name');
            $user = $Profiles->createRow();
            $user->name = $name;
            $user->email = $email;
            $user->password = '';
            $user->activationkey = 'activated';
            $user->language = Zend_Registry::get('config')->get('default_language');
            $user = $Profiles->createNewUser($user, 'facebook');
            // update last login date
            $ProfilesMeta = new Application_Model_ProfilesMeta();
            $ProfilesMeta->metaUpdate('last_login', Application_Plugin_Common::now(), $user->id);
            $Storage = new Application_Model_Storage();
            $StorageAdapter = $Storage->getAdapter();
            $defaultres = 64;
            $bigres = Zend_Registry::get('config')->get('avatar_size') ? Zend_Registry::get('config')->get('avatar_size') : $defaultres;
            // get the image
            $c = new Zend_Http_Client();
            $c->setUri($avatar);
            $result = $c->request('GET');
            $img = imagecreatefromstring($result->getBody());
            // create regular avatar image, resample and store
            $imgname = 'profileimage_' . $name . '.jpg';
            imagejpeg($img, TMP_PATH . '/' . $imgname);
            Application_Plugin_ImageLib::resample(TMP_PATH . '/' . $imgname, TMP_PATH . '/' . $imgname, $defaultres, $defaultres, false);
            $new_filename = $StorageAdapter->moveFileToStorage($imgname, 'avatar');
            $Profiles->updateField($name, 'avatar', $new_filename);
            // create big avatar image, resample and store
            $imgname = 'bigprofileimage_' . $name . '.jpg';
            imagejpeg($img, TMP_PATH . '/' . $imgname);
            Application_Plugin_ImageLib::resample(TMP_PATH . '/' . $imgname, TMP_PATH . '/' . $imgname, $bigres, $bigres, false);
            $big_avatar = $StorageAdapter->moveFileToStorage($imgname, 'avatar');
            $ProfilesMeta->metaUpdate('big_avatar', $big_avatar, $user->id);
            // free img resource
            imagedestroy($img);
            // login user
            $emailAuthAdapter = Application_Plugin_Common::getEmailAuthAdapter($email);
            $auth = Zend_Auth::getInstance();
            $auth->authenticate($emailAuthAdapter);
            $identity = $emailAuthAdapter->getResultRowObject();
            $authStorage = $auth->getStorage();
            $authStorage->write($identity);
            // clear session data
            $session->fb_user_email = '';
            $session->fb_user_display_name = '';
            $session->fb_avatar = '';
            $user_id = $user->id;
            // trigger hooks
            Zend_Registry::get('hooks')->trigger('hook_firsttimelogin', $user_id);
            // show welcome message
            Application_Plugin_Alerts::success(Zend_Registry::get('Zend_Translate')->translate('Welcome to the network.'), 'on');
            Zend_Controller_Action_HelperBroker::getStaticHelper('Redirector')->gotoUrl('');
        }
    }
    echo $registerwithfacebook_form;
}
 /**
  * move image to album (via ajax)
  */
 public function moveimageAction()
 {
     $Images = new Application_Model_Images();
     $Albums = new Application_Model_Albums();
     $current_user = Zend_Auth::getInstance()->getIdentity();
     $request = $this->getRequest();
     $image_id = $request->getParam('resource_id');
     $album_id = $request->getParam('album_id');
     // do some basic checks
     if (!$image_id || !$album_id) {
         $this->getHelper('json')->sendJson(false);
     }
     // see if this is a delete
     if ($album_id == 'trash') {
         $ret = $Images->deleteImage($image_id, 'posts');
         $this->getHelper('json')->sendJson($ret);
         return;
     }
     // see if this is "set as profile picture"
     if ($album_id == 'avatar' || $album_id == 'cover') {
         $image = $Images->getImage($image_id);
         $file_name = $image['data']['file_name'];
         $tmp_file_name = 'setas_' . $file_name;
         $Storage = new Application_Model_Storage();
         $StorageAdapter = $Storage->getAdapter();
         $StorageAdapter->getFileFromStorage($file_name, $tmp_file_name, 'posts');
         // save params to session and redirect to edit page
         $session = new Zend_Session_Namespace('Default');
         $pass_params = array('tmp_image' => $tmp_file_name, 'image_type' => $album_id, 'callback' => '', 'profile_name' => $current_user->name);
         $session->pass_params = $pass_params;
         $this->getHelper('json')->sendJson(true);
         return;
     }
     $album = $Albums->getAlbum($album_id);
     // see if this album belongs to the current user
     if (!isset($album['user_id']) || $album['user_id'] != $current_user->id) {
         $this->getHelper('json')->sendJson(false);
     }
     $ret = $Images->updateField($image_id, 'album_id', $album_id);
     if ($album['name']) {
         $ret = $album['name'];
     }
     $this->getHelper('json')->sendJson($ret);
 }
 /**
  */
 public function setImage()
 {
     // Form Submitted...
     if ($this->request->isPost() && $this->form->isValid($_POST)) {
         // file uploaded?
         if ($this->form->{$this->file_element}->isUploaded()) {
             $this->form->{$this->file_element}->receive();
             // must have
             $receive_path = $this->form->{$this->file_element}->getFileName();
             $filename = $this->form->{$this->file_element}->getValue();
             $extension = strtolower(pathinfo($receive_path, PATHINFO_EXTENSION));
             if ($this->profile_name) {
                 // delete old tmp image files
                 $Storage = new Application_Model_Storage();
                 $StorageAdapter = $Storage->getAdapter();
                 $StorageAdapter->deleteOldTmpFiles(0, 'profileimage_' . $this->profile_name);
                 $tmp_filename = 'profileimage_' . $this->profile_name . '.' . $extension;
                 // move new file to tmp folder
                 rename($receive_path, TMP_PATH . '/' . $tmp_filename);
                 // check if valid image
                 if (!Application_Plugin_ImageLib::isValidImage(TMP_PATH . '/' . $tmp_filename)) {
                     unlink(TMP_PATH . '/' . $tmp_filename);
                     Application_Plugin_Alerts::error($this->translator->translate('Server-side error'), 'off');
                     $this->redirector->gotoUrl();
                     return;
                 }
                 Application_Plugin_Alerts::success($this->translator->translate('You can adjust the picture here'), 'off');
                 // go back to current page after editing
                 $base_url = Application_Plugin_Common::getFullBaseUrl(false);
                 $callback_url = $base_url . $this->request->getRequestUri() . '/edit_done/1';
                 // save params to session and redirect to edit page
                 $session = new Zend_Session_Namespace('Default');
                 $pass_params = array('tmp_image' => $tmp_filename, 'image_type' => $this->image_type, 'callback' => $callback_url, 'profile_name' => $this->profile_name);
                 $session->pass_params = $pass_params;
                 $this->redirector->gotoUrl('images/edit');
             } else {
                 // here we store site settings images
                 // i.e. network background image
                 $this->form->{$this->file_element}->receive();
                 // must have
                 $receive_path = $this->form->{$this->file_element}->getFileName();
                 $filename = $this->form->{$this->file_element}->getValue();
                 $extension = strtolower(pathinfo($receive_path, PATHINFO_EXTENSION));
                 $file_name = $this->image_type . '.' . $extension;
                 // move new file to public image folder
                 rename($receive_path, PUBLIC_PATH . '/images/' . $file_name);
                 // store to app settings & refresh
                 $app_option_key = $this->image_type;
                 $AppOptions = new Application_Model_AppOptions();
                 $AppOptions->updateOption($app_option_key, $file_name);
                 $current_config = Zend_Registry::get('config');
                 $current_config->{$app_option_key} = $file_name;
                 Zend_Registry::set('config', $current_config);
                 Application_Plugin_Alerts::success($this->translator->translate('Image uploaded'), 'off');
                 $base_url = Application_Plugin_Common::getFullBaseUrl(false);
                 $callback_url = $base_url . $this->request->getRequestUri();
                 // flush url
                 $this->redirector->gotoUrl($callback_url);
             }
         } else {
             if ($this->is_requiered) {
                 // nothing to upload
                 Application_Plugin_Alerts::error($this->translator->translate('Please choose a picture'), 'off');
             }
         }
     }
     // somethig went wrong, image too big?
     if ($this->request->isPost() && !$this->form->isValid($_POST)) {
         Application_Plugin_Alerts::error($this->translator->translate('File not allowed or too big'), 'off');
     }
 }
Exemplo n.º 4
0
 public function GetStorageUrl($resource)
 {
     $Storage = new Application_Model_Storage();
     $StorageAdapter = $Storage->getAdapter();
     return $StorageAdapter->getStoragePath($resource);
 }
Exemplo n.º 5
0
 /**
  * Delete all user's images
  */
 public function removeUsersImages($user_id)
 {
     $Profiles = new Application_Model_Profiles();
     $ProfilesMeta = new Application_Model_ProfilesMeta();
     $profile = $Profiles->getProfileByField('id', $user_id);
     if (!$profile) {
         return false;
     }
     $Storage = new Application_Model_Storage();
     $StorageAdapter = $Storage->getAdapter();
     $user_id = (int) $user_id;
     $sql = "\r\n\t\tSELECT\r\n\t\t*\r\n\t\tFROM images\r\n\t\tWHERE uploaded_by = {$user_id}\r\n\t\t";
     $images = $this->getAdapter()->fetchAll($sql);
     if (!empty($images)) {
         foreach ($images as $image) {
             $StorageAdapter->deleteFileFromStorage($image['file_name'], 'posts');
             if ($image['original']) {
                 $StorageAdapter->deleteFileFromStorage($image['original'], 'posts');
             }
             $result = $this->delete(array('id = ?' => $image['id']));
         }
     }
     // remove user avatar, cover and background
     $background_file = $ProfilesMeta->getMetaValue('background_file', $user_id);
     if ($background_file) {
         $ret = $StorageAdapter->deleteFileFromStorage($background_file, 'cover');
     }
     $avatar_file = $profile->avatar;
     if (strpos($avatar_file, 'default') === false) {
         $ret = $StorageAdapter->deleteFileFromStorage($avatar_file, 'avatar');
     }
     $cover_file = $profile->cover;
     if (strpos($cover_file, 'default') === false) {
         $ret = $StorageAdapter->deleteFileFromStorage($cover_file, 'cover');
     }
     return;
 }
 /**
  * Custom background
  */
 public function setbackgroundpictureAction()
 {
     $Profiles = new Application_Model_Profiles();
     $ProfilesMeta = new Application_Model_ProfilesMeta();
     $request = $this->getRequest();
     $request_profile_id = $request->getParam('id', false);
     $profile = $Profiles->getProfileByField('id', $request_profile_id);
     if (Zend_Auth::getInstance()->getIdentity()->role == 'admin' && $request_profile_id) {
         // admin edit
         $profile_id = $request_profile_id;
         $this->view->sidebar_editprofile = $profile;
         // attach sidebar box
         Zend_Registry::get('hooks')->attach('hook_view_sidebar', 5, function () {
             echo Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->view->render('/_sidebar/editprofile.phtml');
         });
     } elseif ($request_profile_id && $Profiles->getProfile($profile->name, false, true)) {
         // users pages & groups
         $this->buildMenu(true);
         $profile_id = $request_profile_id;
     } else {
         // user profile
         $this->buildMenu();
         $profile_id = Zend_Auth::getInstance()->getIdentity()->id;
     }
     $profile_name = Zend_Auth::getInstance()->getIdentity()->name;
     $form = new Application_Form_CustomBackground();
     $current_background_file = $ProfilesMeta->getMetaValue('background_file', $profile_id);
     $Storage = new Application_Model_Storage();
     $StorageAdapter = $Storage->getAdapter();
     if ($request->isPost() && $form->isValid($_POST)) {
         // file uploaded?
         if ($form->background->isUploaded()) {
             $form->background->receive();
             // must have
             $receive_path = $form->background->getFileName();
             $filename = $form->background->getValue();
             $extension = strtolower(pathinfo($receive_path, PATHINFO_EXTENSION));
             $tmp_filename = 'profileimage_' . $profile_name . '.' . $extension;
             // delete old tmp image files
             $StorageAdapter->deleteOldTmpFiles(0, 'profileimage_' . $profile_name);
             // move new file to tmp folder
             rename($receive_path, TMP_PATH . '/' . $tmp_filename);
             // check if valid image
             if (!Application_Plugin_ImageLib::isValidImage(TMP_PATH . '/' . $tmp_filename)) {
                 unlink(TMP_PATH . '/' . $tmp_filename);
                 Application_Plugin_Alerts::error($this->view->translate('Server-side error'), 'off');
                 $this->redirect();
                 return;
             }
             // delete old file
             $StorageAdapter->deleteFileFromStorage($current_background_file, 'cover');
             // move uploaded file to permanent location
             $current_background_file = $StorageAdapter->moveFileToStorage($tmp_filename, 'cover');
             // update db
             $ProfilesMeta->metaUpdate('background_file', $current_background_file, $profile_id);
         }
         $ProfilesMeta->metaUpdate('background_repeat', $form->getValue('background_repeat'), $profile_id);
         $ProfilesMeta->metaUpdate('background_scroll', $form->getValue('background_scroll'), $profile_id);
         $ProfilesMeta->metaUpdate('background_stretch', $form->getValue('background_stretch'), $profile_id);
         $ProfilesMeta->metaUpdate('background_noimage', $form->getValue('background_noimage'), $profile_id);
         Application_Plugin_Alerts::success($this->view->translate('Settings updated, please clear your browser cache'), 'off');
     }
     $this->view->image = $current_background_file ? $StorageAdapter->getStoragePath('cover') . $current_background_file : false;
     $this->view->form = $form;
     $this->view->load_colorpicker = true;
 }
 /**
  * callback after next page load (aka Poor Man's Cron)
  *
  * prevents slowing down the page load
  */
 public function callbackAction()
 {
     $Notifications = new Application_Model_Notifications();
     $out = $Notifications->getNotifications(false, false, true);
     // delete old tmp image files in 1%
     if (rand(1, 100) == 1) {
         $Storage = new Application_Model_Storage();
         $StorageAdapter = $Storage->getAdapter();
         $StorageAdapter->deleteOldTmpFiles();
     }
     // TODO: delete old notifications, gc etc
     // trigger hooks
     Zend_Registry::get('hooks')->trigger('hook_app_callback');
     $this->_helper->json($out);
 }
Exemplo n.º 8
0
 /**
  * Add new post
  */
 public function addPost(array $content, $wall_id, $privacy, $attached_files)
 {
     if (!Zend_Auth::getInstance()->hasIdentity() || strlen($content['content']) < 1 && empty($attached_files)) {
         return false;
     }
     $content['content'] = Application_Plugin_Common::limitInput($content['content']);
     $Connections = new Application_Model_Connections();
     $Profiles = new Application_Model_Profiles();
     $Images = new Application_Model_Images();
     $PostsMeta = new Application_Model_PostsMeta();
     $wall_profile = $Profiles->getProfileByField('id', $wall_id);
     $author_id = Zend_Auth::getInstance()->getIdentity()->id;
     $insert_id = $this->insert(array('author_id' => $author_id, 'wall_id' => $wall_id, 'created_on' => Application_Plugin_Common::now(), 'content' => $content['content'], 'is_hidden' => 0, 'privacy' => $privacy));
     // write post's meta data
     if (isset($content['meta'])) {
         foreach ($content['meta'] as $metakey => $metavalue) {
             $ret = $PostsMeta->metaUpdate($insert_id, $metakey, $metavalue);
         }
     }
     // move tmp file to posts folder and add meta data to post
     if (!empty($attached_files)) {
         $i = 0;
         foreach ($attached_files as $file) {
             ++$i;
             $file_data = array('name' => basename($file), 'size' => filesize($file));
             // check max images per post
             if ($i > Zend_Registry::get('config')->get('max_images_per_post')) {
                 break;
             }
             $Storage = new Application_Model_Storage();
             $StorageAdapter = $Storage->getAdapter();
             $original_filename = '';
             if (Zend_Registry::get('config')->get('resample_images')) {
                 Application_Plugin_ImageLib::resample(TMP_PATH . '/' . $file_data['name'], TMP_PATH . '/thumb_' . $file_data['name']);
                 $filename = $StorageAdapter->moveFileToStorage('thumb_' . $file_data['name'], 'posts');
                 if (Zend_Registry::get('config')->get('keep_original')) {
                     $original_filename = $StorageAdapter->moveFileToStorage($file_data['name'], 'posts');
                 } else {
                     $original_filename = '';
                     unlink(TMP_PATH . '/' . $file_data['name']);
                     // clean up
                 }
             } else {
                 $filename = $StorageAdapter->moveFileToStorage($file_data['name'], 'posts');
             }
             // in case this is not a user's wall - image owner will become the network
             // (image owner could become the wall owner but that's a bad idea)
             if ($wall_profile['id'] != $author_id) {
                 $owner = 0;
             } else {
                 $owner = $author_id;
             }
             $Images->addImage($filename, $file_data['size'], $owner, $author_id, $insert_id, 0, $original_filename);
         }
     }
     // post on someone else's wall, notify wall owner
     if ($wall_profile['type'] === 'user' && $wall_id != $author_id) {
         $Notifications = new Application_Model_Notifications();
         $Notifications->pushNotification(array($wall_id), 7, 'post', $insert_id);
     }
     // trigger hooks
     $data = array('post_id' => $insert_id, 'content' => $content);
     Zend_Registry::get('hooks')->trigger('hook_data_aftersavepost', $data);
     return true;
 }
 /**
  * Receive uploaded files (ajax/blueimp)
  */
 public function receivefileAction()
 {
     $ret = Zend_Registry::get('Zend_Translate')->translate('Server-side error');
     if ($this->getRequest()->isPost()) {
         $Images = new Application_Model_Images();
         $adapter = new Zend_File_Transfer_Adapter_Http();
         $adapter->addValidator('Extension', false, 'jpg,jpeg,png,gif');
         $files = $adapter->getFileInfo();
         $receive_to = $this->getRequest()->getParam('to');
         $form_unique_key = (int) $this->getRequest()->getParam('form_unique_key');
         $current_user_id = Zend_Auth::getInstance()->getIdentity()->id;
         $current_user_role = Zend_Auth::getInstance()->getIdentity()->role;
         foreach ($files as $file => $info) {
             // file uploaded & is valid
             if (!$adapter->isUploaded($file)) {
                 continue;
             }
             if (!$adapter->isValid($file)) {
                 continue;
             }
             // check max file size
             if ($info['size'] > Zend_Registry::get('config')->get('max_file_upload_size')) {
                 continue;
             }
             $filename = $adapter->getFileName($file);
             $extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
             $fileinfo = $adapter->getFileInfo($file);
             $filesize = $fileinfo[$file]['size'];
             $profilename = Zend_Auth::getInstance()->getIdentity()->name;
             $randomstring = Application_Plugin_Common::getRandomString();
             // generate tmp filename
             $tmp_filename = 'post_' . $profilename . '_' . $form_unique_key . '_' . $randomstring . '.' . $extension;
             $tmp_filename_full = TMP_PATH . '/' . $tmp_filename;
             // set to rename uploaded file upon receiving to tmp folder
             $adapter->setDestination(TMP_PATH);
             $adapter->addFilter('rename', $tmp_filename_full);
             // receive the files into the tmp directory, must have
             $adapter->receive($file);
             // check if valid image
             if (!Application_Plugin_ImageLib::isValidImage($tmp_filename_full)) {
                 unlink($tmp_filename_full);
                 continue;
             }
             // check storage limits
             $max_files_per_user = 0 + Zend_Registry::get('config')->get('max_files_per_user');
             $max_storage_per_user = 0 + Zend_Registry::get('config')->get('max_storage_per_user');
             if ($current_user_role == 'user' && ($max_files_per_user || $max_storage_per_user)) {
                 $storage_usage = $Images->getStorageUsage($current_user_id);
                 if ($max_files_per_user && $storage_usage['image_count'] > $max_files_per_user || $max_storage_per_user && $storage_usage['image_size'] > $max_storage_per_user) {
                     $ret = Zend_Registry::get('Zend_Translate')->translate('Storage limits reached');
                     unlink($tmp_filename_full);
                     continue;
                 }
             }
             if ($receive_to !== 'tmp') {
                 // receive to album, check if user is an album owner
                 if ($receive_to > 0) {
                     $Albums = new Application_Model_Albums();
                     $album = $Albums->getAlbum($receive_to);
                     // exit on wrong album
                     if (!$album || $album['user_id'] != $current_user_id) {
                         $this->_helper->json(false);
                         return;
                     }
                 }
                 $Storage = new Application_Model_Storage();
                 $StorageAdapter = $Storage->getAdapter();
                 $original_filename = '';
                 if (Zend_Registry::get('config')->get('resample_images')) {
                     Application_Plugin_ImageLib::resample(TMP_PATH . '/' . $tmp_filename, TMP_PATH . '/thumb_' . $tmp_filename);
                     $image_filename = $StorageAdapter->moveFileToStorage('thumb_' . $tmp_filename, 'posts');
                     if (Zend_Registry::get('config')->get('keep_original')) {
                         $original_filename = $StorageAdapter->moveFileToStorage($tmp_filename, 'posts');
                     } else {
                         $original_filename = '';
                         unlink(TMP_PATH . '/' . $tmp_filename);
                         // clean up
                     }
                 } else {
                     $image_filename = $StorageAdapter->moveFileToStorage($tmp_filename, 'posts');
                 }
                 if ($image_filename) {
                     $ret = $Images->addImage($image_filename, $filesize, $current_user_id, $current_user_id, 0, $receive_to, $original_filename);
                 }
             }
             $ret = true;
         }
     }
     $this->_helper->json($ret);
 }
Exemplo n.º 10
0
 /**
  * download original image
  */
 public function downloadimageAction()
 {
     $request = $this->getRequest();
     $image_id = $request->getParam('resource_id');
     $Images = new Application_Model_Images();
     $image = $Images->getImage($image_id);
     if (!isset($image['data']['file_name']) || empty($image['data']['file_name'])) {
         $this->redirect('');
     }
     if ($image['data']['original']) {
         $filename = $image['data']['original'];
     } else {
         $filename = $image['data']['file_name'];
     }
     $Storage = new Application_Model_Storage();
     $StorageAdapter = $Storage->getAdapter();
     $StorageAdapter->downloadFile($filename);
     die;
 }