/** * add image * * albums: 1 - posts */ public function addImage($file_name, $file_size, $owner_id, $uploaded_by, $post_id, $album_id, $original = '') { $random = Application_Plugin_Common::getRandomString(); $data = array('uid' => $random, 'file_name' => $file_name, 'original' => $original, 'owner_id' => $owner_id, 'uploaded_by' => $uploaded_by, 'post_id' => $post_id, 'album_id' => $album_id, 'size' => $file_size, 'created_on' => Application_Plugin_Common::now(), 'is_hidden' => 0); $ret = $this->insert($data); return $ret; }
/** * Create an album */ public function createAlbum($album_name, $description) { // protected names if ($album_name == 'cover' || $album_name == 'avatar') { return false; } $user_id = Zend_Auth::getInstance()->getIdentity()->id; $data = array('user_id' => $user_id, 'name' => $album_name, 'description' => $description, 'cover_image' => '', 'created_on' => Application_Plugin_Common::now()); return $this->insert($data); }
/** * Send message */ public function sendMessage($to_user_id, $content, $message_type = 'pm') { if (!Zend_Auth::getInstance()->hasIdentity() || strlen($content) < 1) { return false; } $from_user_id = Zend_Auth::getInstance()->getIdentity()->id; if (!$to_user_id || $from_user_id == $to_user_id) { return false; } $ret = $this->insert(array('type' => $message_type, 'from_user_id' => $from_user_id, 'to_user_id' => $to_user_id, 'content' => $content, 'is_new' => 1, 'is_hidden' => 0, 'sent_on' => Application_Plugin_Common::now())); $Notifications = new Application_Model_Notifications(); $Notifications->pushNotification(array($to_user_id), 8, 'profile', $from_user_id, false); return $ret; }
/** * Add comment */ public function addComment($content, $resource_id, $resource_type) { if (!Zend_Auth::getInstance()->hasIdentity()) { return false; } if (!is_string($content) || !is_string($resource_type) || strlen($content) < 1) { return false; } $content = Application_Plugin_Common::limitInput($content); $author_id = Zend_Auth::getInstance()->getIdentity()->id; // find resource author switch ($resource_type) { case 'post': $Posts = new Application_Model_Posts(); $resource_author = $Posts->getPostAuthorId($resource_id); $resource_wall = $Posts->getPostsWallProfileData($resource_id); // for page comments written by page admin switch owner to be a page itself if ($resource_wall['type'] == 'page' && $resource_wall['owner'] == $author_id) { $author_id = $resource_wall['id']; $resource_author = $author_id; } break; case 'image': $Images = new Application_Model_Images(); $image = $Images->getImage($resource_id); $resource_author = $image['data']['uploaded_by']; break; default: $resource_author = 0; break; } $ret = $this->insert(array('author_id' => $author_id, 'resource_type' => $resource_type, 'resource_id' => $resource_id, 'created_on' => Application_Plugin_Common::now(), 'content' => $content, 'is_hidden' => 0)); $this->markOldAsHidden($resource_type, $resource_id); $Notifications = new Application_Model_Notifications(); // notify all users involved in comment discussion $notify_users = $this->getUsersCommented($resource_type, $resource_id, true); // notify resource author if not already on the list if (array_search($resource_author, $notify_users) === false) { $notify_users[] = $resource_author; } $Notifications->pushNotification($notify_users, 1, 'comment', $ret); // trigger hooks $data = array('comment_id' => $ret, 'content' => $content); Zend_Registry::get('hooks')->trigger('hook_data_aftersavecomment', $data); return $ret; }
/** * Like toggle */ public function toggleLike($resource_id, $resource_type) { if (!Zend_Auth::getInstance()->hasIdentity() || !$resource_id || !$resource_type) { return null; } $user_id = Zend_Auth::getInstance()->getIdentity()->id; if ($this->isLiked($resource_id, $resource_type)) { $result = $this->delete(array('resource_id = ?' => (int) $resource_id, 'resource_type = ?' => $resource_type, 'user_id = ?' => (int) $user_id)); $state = 0; } else { $data = array('user_id' => (int) $user_id, 'resource_type' => $resource_type, 'resource_id' => (int) $resource_id, 'created_on' => Application_Plugin_Common::now()); $ret = $this->insert($data); $state = 1; } $likes_count = $this->getLikesCount($resource_id, $resource_type); // notify author $Notifications = new Application_Model_Notifications(); if ($state == 1) { // find resource author switch ($resource_type) { case 'post': $Posts = new Application_Model_Posts(); $resource_author = array($Posts->getPostAuthorId($resource_id)); break; case 'comment': $Comments = new Application_Model_Comments(); $resource_author = array($Comments->getCommentAuthorId($resource_id)); break; case 'image': $Images = new Application_Model_Images(); $resource_author = array($Images->getImageOwnerId($resource_id)); break; default: $resource_author = false; break; } if ($resource_author) { // notify resource owner $Notifications->pushNotification($resource_author, 2, 'like', $ret); } } return array('count' => $likes_count, 'state' => $state); }
/** * Report resource */ public function report($resource_id, $resource_type, $reason) { if (!Zend_Auth::getInstance()->hasIdentity()) { return null; } $user_id = Zend_Auth::getInstance()->getIdentity()->id; if ($this->isReported($resource_id, $resource_type)) { return false; } $data = array('user_id' => $user_id, 'resource_type' => $resource_type, 'resource_id' => $resource_id, 'reason' => $reason, 'created_on' => Application_Plugin_Common::now(), 'reviewed_by' => 0, 'is_accepted' => 0); // new report email notification if (Zend_Registry::get('config')->get('report_notify_email')) { $to = Zend_Registry::get('config')->get('report_notify_email'); $subject = 'New report'; // prepare phtml email template $mail_template_path = APPLICATION_PATH . '/views/emails/'; $view = new Zend_View(); $view->setScriptPath($mail_template_path); $body = $view->render('newreport.phtml'); $ret = Application_Plugin_Common::sendEmail($to, $subject, $body, true); } return $this->insert($data); }
/** * Follow User */ public function followUser($user_id, $follow_id) { if ($this->areFriends($user_id, $follow_id) || $this->isFollowing($follow_id, $user_id)) { return false; } $data = array('user_id' => $user_id, 'follow_id' => $follow_id, 'created_on' => Application_Plugin_Common::now()); try { $ret = $this->insert($data); } catch (Zend_Db_Exception $e) { Application_Plugin_Common::log($e->getMessage()); } if ($ret === null) { return false; } return true; }
/** * 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; }
/** * Register submit */ public function submitRegisterForm($form) { if ($form->isValid($_POST)) { $Profiles = new Application_Model_Profiles(); $name = $form->getValue('regname'); $email = $form->getValue('regemail'); $hash = new Application_Plugin_Phpass(); $password = $hash->HashPassword($form->getValue('regpassword')); $user = $Profiles->createRow(); $user->name = $name; $user->email = $email; $user->password = $password; if (Zend_Registry::get('config')->get('user_activation_disabled')) { // create new user withot activation & login $user->activationkey = 'activated'; $new_profile = $Profiles->createNewUser($user); // auto-login user and store identity $authAdapter = Application_Plugin_Common::getAuthAdapter(); $authAdapter->setIdentity($new_profile->email)->setCredential('whatever')->setCredentialTreatment('autologin'); $auth = Zend_Auth::getInstance(); $auth->authenticate($authAdapter); $identity = $authAdapter->getResultRowObject(); $authStorage = $auth->getStorage(); $authStorage->write($identity); // update last login date $ProfilesMeta = new Application_Model_ProfilesMeta(); $ProfilesMeta->metaUpdate('last_login', Application_Plugin_Common::now(), $identity->id); // show welcome message Application_Plugin_Alerts::success($this->view->translate('Welcome to the network.'), 'on'); } else { // create activation key and sent it to user email $key = $Profiles->generateActivationKey($email); $user->activationkey = $key; $ret = Application_Plugin_Common::sendActivationEmail($email, $name, $key); // email has been sent, proceed if ($ret) { // show success message Application_Plugin_Alerts::info(Zend_Registry::get('Zend_Translate')->translate('Please Check your Inbox and come back after you activate your account.'), 'off'); // build url $base_url = Application_Plugin_Common::getFullBaseUrl(); $resendactivation_link = $base_url . '/index/activate/resend/' . $user->name; Application_Plugin_Alerts::info('<a href="' . $resendactivation_link . '">' . Zend_Registry::get('Zend_Translate')->translate('Click here to resend the activation email') . '</a>', 'off', false); // create new user $new_profile = $Profiles->createNewUser($user); } else { // show error message Application_Plugin_Alerts::error(Zend_Registry::get('Zend_Translate')->translate('Something went wrong, email was not sent.'), 'off'); Zend_Controller_Action_HelperBroker::getStaticHelper('Redirector')->gotoUrl(''); return; } } // flush url Zend_Controller_Action_HelperBroker::getStaticHelper('Redirector')->gotoUrl(''); } return $form; }
/** * Activation link lands here to activate user account */ public function activateAction() { $this->_helper->_layout->setLayout('layout_wide'); // flush if already logged in Zend_Auth::getInstance()->clearIdentity(); $activateaccount_form = new Application_Form_ActivateAccount(); $this->view->activateaccount_form = $activateaccount_form; $key = $this->getRequest()->getParam('key', false); $resend_username = $this->getRequest()->getParam('resend', false); $Profiles = new Application_Model_Profiles(); $ProfilesMeta = new Application_Model_ProfilesMeta(); $userData = $Profiles->getProfileByField('activationkey', $key); if (!$userData || $key == 'activated') { // try if this is a resend $userData = $Profiles->getProfile($resend_username); if (!$userData || $userData->activationkey == 'activated') { $this->redirect(''); } else { $resend_lock = $ProfilesMeta->getMetaValue('resend_activation_lock', $userData->id); $hour_lock = date('H'); // prevent too many attempts if ($resend_lock && $resend_lock == $hour_lock) { Application_Plugin_Alerts::info(Zend_Registry::get('Zend_Translate')->translate('Please Check your Inbox and come back after you activate your account.'), 'off'); $this->redirect(''); } $ret = Application_Plugin_Common::sendActivationEmail($userData->email, $userData->name, $userData->activationkey); // email has been sent, show success message if ($ret) { Application_Plugin_Alerts::info(Zend_Registry::get('Zend_Translate')->translate('Please Check your Inbox and come back after you activate your account.'), 'off'); // once per day $ProfilesMeta->metaUpdate('resend_activation_lock', $hour_lock, $userData->id); } else { // show error message Application_Plugin_Alerts::error(Zend_Registry::get('Zend_Translate')->translate('Something went wrong, email was not sent.'), 'off'); } $this->redirect(''); } } $request = $this->getRequest(); if ($request->isPost() && isset($_POST['identifier']) && $_POST['identifier'] == 'ActivateAccount') { if ($activateaccount_form->isValid($_POST)) { if ($Profiles->activateAccount($key)) { // auto-login user and store identity $authAdapter = Application_Plugin_Common::getAuthAdapter(); $authAdapter->setIdentity($userData->email)->setCredential('whatever')->setCredentialTreatment('autologin'); $auth = Zend_Auth::getInstance(); $auth->authenticate($authAdapter); $identity = $authAdapter->getResultRowObject(); $authStorage = $auth->getStorage(); $authStorage->write($identity); // update last login date $ProfilesMeta = new Application_Model_ProfilesMeta(); $ProfilesMeta->metaUpdate('last_login', Application_Plugin_Common::now(), $identity->id); // show welcome message Application_Plugin_Alerts::success($this->view->translate('Welcome to the network.'), 'on'); $this->redirect(''); } } } }
/** * Retrive all unreaded notifications */ public function getNotifications($only_new = false, $fixed_limit = false, $send_emails = false) { if (!Zend_Auth::getInstance()->hasIdentity()) { return; } $current_user_id = (int) Zend_Auth::getInstance()->getIdentity()->id; // application now time $now = Application_Plugin_Common::now(); if ($this->page_number < 1) { $this->page_number = 1; } $limit_from = ((int) $this->page_number - 1) * (int) Zend_Registry::get('config')->get('pagination_limit'); $limit_to = (int) Zend_Registry::get('config')->get('pagination_limit'); $sql = "\n\t\tSELECT\n\n\t\tn.id AS notification_id,\n\t\tn.type AS notification_type,\n\t\tn.resource_type AS notification_resource_type,\n\t\tn.resource_id AS notification_resource_id,\n\t\tn.created_on AS notification_date,\n\t\t\t\t\n\t\tt.email AS to_email,\n\t\tt.name AS to_name,\n\t\tt.screen_name AS to_screen_name,\n\n\t\tc.resource_type AS comment_resource_type,\n\t\tc_authors.name AS comment_author_name,\n\t\tc_authors.screen_name AS comment_author_screen_name,\n\t\tc_authors.avatar AS comment_author_avatar,\n\t\tc_posts.id AS commented_post_id,\n\t\tc.content AS comment_content,\n\t\tc_images.uid AS commented_image_uid,\n\t\tc_profile_wall.name AS commented_post_on_wall,\n\n\t\tl.id AS like_id,\n\t\tl.resource_type AS like_resource_type,\n\t\tl_users.name AS like_user_name,\n\t\tl_users.screen_name AS like_user_screen_name,\n\t\tl_users.avatar AS like_user_avatar,\n\n\t\tp.id AS profile_id,\n\t\tp.name AS profile_name,\n\t\tp.screen_name AS profile_screen_name,\n\t\tp.avatar AS profile_avatar,\n\n\t\tpo.id AS post_id,\n\t\tpo.content AS post_content,\n\t\tpo_authors.name AS post_author_name,\n\t\tpo_authors.screen_name AS post_author_screen_name,\n\t\tpo_authors.avatar AS post_author_avatar,\n\n\t\tp_meta.meta_value AS bulk_notifications\n\n\t\tFROM notifications n\n\n\t\tLEFT JOIN profiles t ON t.id = n.to_user\n\t\t\t\t\n\t\tLEFT JOIN comments c ON c.id = n.resource_id AND n.resource_type = 'comment'\n\t\tLEFT JOIN profiles c_authors ON c_authors.id = c.author_id\n\t\tLEFT JOIN posts c_posts ON c_posts.id = c.resource_id AND c.resource_type = 'post'\n\t\tLEFT JOIN profiles c_profile_wall ON c_profile_wall.id = c_posts.wall_id\n\t\tLEFT JOIN images c_images ON c_images.id = c.resource_id AND c.resource_type = 'image'\n\n\t\tLEFT JOIN likes l ON l.id = n.resource_id AND n.resource_type = 'like'\n\t\tLEFT JOIN profiles l_users ON l_users.id = l.user_id\n\n\t\tLEFT JOIN profiles p ON p.id = n.resource_id AND n.resource_type = 'profile'\n\n\t\tLEFT JOIN posts po ON po.id = n.resource_id AND n.resource_type = 'post'\n\t\tLEFT JOIN profiles po_authors ON po_authors.id = po.author_id\n\n\t\tLEFT JOIN profile_meta p_meta ON p_meta.profile_id = n.to_user AND p_meta.meta_key = 'bulk_notifications'\n\t\t\t\t\n\t\tWHERE 1\n\n\t\tAND (n.type <> 2 OR l.id IS NOT NULL)\n\t\t\t\n\t\t"; // if ($send_emails) { $sql .= "\n\t\t\t\t\tAND n.email_sent = 0\n\t\t\t\t\tAND n.created_on < '{$now}' - INTERVAL 5 MINUTE\n\t\t\t\t\tAND n.created_on > DATE(DATE_SUB('{$now}', INTERVAL 1 DAY))\n\t\t\t\t\t"; } else { $sql .= " AND n.to_user = {$current_user_id} "; } if ($only_new == true) { $sql .= " AND n.is_new = 1 "; } $sql .= " ORDER BY n.created_on DESC "; if ($fixed_limit) { // fixed limit, for notification box $sql .= " LIMIT " . (int) $fixed_limit; } elseif (!$send_emails) { // go with pagination $sql .= " LIMIT {$limit_from}, {$limit_to} "; } $result = $this->getAdapter()->fetchAll($sql); $transl = Zend_Registry::get('Zend_Translate'); // save locale since we might change it below $locale_saved = $transl->getLocale(); $result_rows = $this->fixData($result, $send_emails); // send emails if ($send_emails) { // set default language to network default $transl->setLocale(Zend_Registry::get('config')->get('default_language')); foreach ($result_rows as $row) { // update this notification to email_sent = 1, never mind if it wan't be really sent later on $data = array('email_sent' => '1'); $where = array('id = ?' => $row['notification_id']); $result = $this->update($data, $where); $notification_key = 'notification_email_' . $row['notification_type']; // if row is not updated then email was probably already sent // also, check if this user has enabled this notification if ($result == 1 && $row['do_send_email'] && $row['bulk_notifications'][$notification_key]) { $to = $row['to_email']; $subject = $row['subject_email']; // prepare phtml email template $mail_template_path = APPLICATION_PATH . '/views/emails/'; $view = new Zend_View(); $view->setScriptPath($mail_template_path); $view->assign('top', sprintf($transl->translate('Hello %s'), $row['to_screen_name'])); $view->assign('message', '<p>' . $row['html_link'] . '</p>'); $view->assign('footer', $transl->translate('Thank you')); $body = $view->render('notifications.phtml'); Application_Plugin_Common::sendEmail($to, $subject, $body); $row['view_from_name'] = $row['profile_name']; $row['view_from_screen_name'] = $row['profile_screen_name']; $row['view_from_avatar'] = $row['profile_avatar']; } } return 1; } // restore locale $transl->setLocale($locale_saved); return $result_rows; }
/** * Create new page - add defaults & save */ public function createNewPage(Application_Model_Profiles_Row $profile) { $profile->type = 'page'; $profile->avatar = 'default/pages.jpg'; $profile->cover = 'default/' . rand(1, 3) . '.jpg'; $profile->is_hidden = 0; try { $created_id = $profile->save(); } catch (Zend_Db_Exception $e) { Application_Plugin_Common::log($e->getMessage()); } $ProfilesMeta = new Application_Model_ProfilesMeta(); $ProfilesMeta->metaUpdate('date_created', Application_Plugin_Common::now(), $created_id); $user_id = Zend_Auth::getInstance()->getIdentity()->id; return $profile; }
/** * log error messages to file */ public static function log($messages) { $writer = new Zend_Log_Writer_Stream(APPLICATION_LOG); $log = new Zend_Log($writer); $backTrace = debug_backtrace(); if (isset($backTrace[2]['class'])) { $class_method = $backTrace[2]['class'] . "::" . $backTrace[2]['function'] . "()"; } else { $class_method = ""; } if (is_array($messages)) { foreach ($messages as $message) { $log->log($message, Zend_Log::ERR, array('timestamp' => Application_Plugin_Common::now(), 'class_method' => $class_method)); } } else { $log->log($messages, Zend_Log::ERR, array('timestamp' => Application_Plugin_Common::now(), 'class_method' => $class_method)); } }
/** * Share post to users wall */ public function sharePostToWall($post_id) { if (!Zend_Auth::getInstance()->hasIdentity()) { return false; } $post_id = (int) $post_id; $post = $this->getPost($post_id); $author_id = Zend_Auth::getInstance()->getIdentity()->id; $insert_id = $this->insert(array('author_id' => $author_id, 'wall_id' => $author_id, 'created_on' => Application_Plugin_Common::now(), 'content' => '', 'is_hidden' => 0, 'privacy' => $post['privacy'])); // write post's meta data $PostsMeta = new Application_Model_PostsMeta(); $json = json_encode(array('type' => 'share', 'data' => array('post' => $post_id))); $PostsMeta->metaUpdate($insert_id, 'rich_content', $json); return; }