Example #1
0
	private function conversations() {
		if (_button('cancel')) {
			redirect(s_link('my dc'));
		}

		global $config, $user, $cache, $comments;

		// TODO: New conversation system
		// /my/dc/(page)/(selected)/(username)/

		$this->conversations_delete();

		$submit = _button('post');
		$msg_id = request_var('p', 0);
		$mode = request_var('mode', '');
		$error = w();

		if ($submit || $mode == 'start' || $mode == 'reply') {
			$member = '';
			$dc_subject = '';
			$dc_message = '';

			if ($submit) {
				if ($mode == 'reply') {
					$parent_id = request_var('parent', 0);

					$sql = 'SELECT *
						FROM _dc
						WHERE msg_id = ?
							AND (privmsgs_to_userid = ? OR privmsgs_from_userid = ?)';
					if (!$to_userdata = sql_fieldrow(sql_filter($sql, $parent_id, $user->d('user_id'), $user->d('user_id')))) {
						fatal_error();
					}

					$privmsgs_to_userid = ($user->d('user_id') == $to_userdata['privmsgs_to_userid']) ? 'privmsgs_from_userid' : 'privmsgs_to_userid';
					$to_userdata['user_id'] = $to_userdata[$privmsgs_to_userid];
				} else {
					$member = request_var('member', '');
					if (!empty($member)) {
						$member = get_username_base($member, true);

						if ($member !== false) {
							$sql = 'SELECT user_id, username, username_base, user_email
								FROM _members
								WHERE username_base = ?
									AND user_type <> ?';
							if (!$to_userdata = sql_fieldrow(sql_filter($sql, $member, USER_INACTIVE))) {
								$error[] = 'NO_SUCH_USER';
							}

							if (!sizeof($error) && $to_userdata['user_id'] == $user->d('user_id')) {
								$error[] = 'NO_AUTO_DC';
							}
						} else {
							$error[] = 'NO_SUCH_USER';
							$member = '';
						}
					} else {
						$error[] = 'EMPTY_USER';
					}
				}

				if (isset($to_userdata) && isset($to_userdata['user_id'])) {
					// Check blocked member
					$sql = 'SELECT ban_id
						FROM _members_ban
						WHERE user_id = ?
							AND banned_user = ?';
					if ($ban_profile = sql_fieldrow(sql_filter($sql, $to_userdata['user_id'], $user->d('user_id')))) {
						$error[] = 'BLOCKED_MEMBER';
					}
				}

				$dc_message = request_var('message', '');
				if (empty($dc_message)) {
					$error[] = 'EMPTY_MESSAGE';
				}

				if (!sizeof($error)) {
					$dc_id = $comments->store_dc($mode, $to_userdata, $user->d(), $dc_subject, $dc_message, true, true);

					redirect(s_link('my dc read', $dc_id) . '#' . $dc_id);
				}
			}
		}

		//
		// Start error handling
		//
		if (sizeof($error)) {
			_style('error', array(
				'MESSAGE' => parse_error($error))
			);

			if ($mode == 'reply') {
				$mode = 'read';
			}
		}

		$s_hidden_fields = w();

		switch ($mode) {
			case 'start':
				//
				// Start new conversation
				//
				if (!$submit) {
					$member = request_var('member', '');
					if ($member != '') {
						$member = get_username_base($member);

						$sql = 'SELECT user_id, username, username_base
							FROM _members
							WHERE username_base = ?
								AND user_type <> ?';
						$row = sql_fieldrow(sql_filter($sql, $member, USER_INACTIVE));
					}
				}

				_style('dc_start', array(
					'MEMBER' => $member,
					'SUBJECT' => $dc_subject,
					'MESSAGE' => $dc_message)
				);

				$s_hidden_fields = array('mode' => 'start');
				break;
			case 'read':
				//
				// Show selected conversation
				//
				if (!$msg_id) {
					fatal_error();
				}

				$sql = 'SELECT *
					FROM _dc
					WHERE msg_id = ?
						AND (privmsgs_to_userid = ? OR privmsgs_from_userid = ?)
						AND msg_deleted <> ?';
				if (!$msg_data = sql_fieldrow(sql_filter($sql, $msg_id, $user->d('user_id'), $user->d('user_id'), $user->d('user_id')))) {
					fatal_error();
				}

				//
				// Get all messages for this conversation
				//
				$sql = 'SELECT c.*, m.user_id, m.username, m.username_base, m.user_avatar, m.user_sig, m.user_rank, m.user_gender, m.user_posts
					FROM _dc c, _members m
					WHERE c.parent_id = ?
						AND c.privmsgs_from_userid = m.user_id
					ORDER BY c.privmsgs_date';
				if (!$result = sql_rowset(sql_filter($sql, $msg_data['parent_id']))) {
					fatal_error();
				}

				$with_user = $msg_data['privmsgs_to_userid'];
				if ($with_user == $user->d('user_id')) {
					$with_user = $msg_data['privmsgs_from_userid'];
				}

				$sql = 'SELECT username
					FROM _members
					WHERE user_id = ?';
				$with_username = sql_field(sql_filter($sql, $with_user), 'username', '');

				_style('conv', array(
					'URL' => s_link('my dc'),
					'SUBJECT' => $with_username,
					'CAN_REPLY' => $result[0]['msg_can_reply'],)
				);

				foreach ($result as $row) {
					$user_profile = $comments->user_profile($row);

					_style('conv.row', array(
						'USERNAME' => $user_profile['username'],
						'AVATAR' => $user_profile['user_avatar'],
						'SIGNATURE' => ($row['user_sig'] != '') ? $comments->parse_message($row['user_sig']) : '',
						'PROFILE' => $user_profile['profile'],
						'MESSAGE' => $comments->parse_message($row['privmsgs_text']),
						'POST_ID' => $row['msg_id'],
						'POST_DATE' => $user->format_date($row['privmsgs_date']))
					);
				}

				$s_hidden_fields = array('mark[]' => $msg_data['parent_id'], 'p' => $msg_id, 'parent' => $msg_data['parent_id'], 'mode' => 'reply');
				break;
			default:
				//
				// Get all conversations for this member
				//
				$offset = request_var('offset', 0);

				$sql = 'SELECT COUNT(c.msg_id) AS total
					FROM _dc c, _dc c2, _members m, _members m2
					WHERE (c.privmsgs_to_userid = ? OR c.privmsgs_from_userid = ?)
						AND c.msg_id = c.parent_id
						AND c.msg_deleted <> ?
						AND c.privmsgs_from_userid = m.user_id
						AND c.privmsgs_to_userid = m2.user_id
						AND (IF(c.last_msg_id,c.last_msg_id,c.msg_id) = c2.msg_id)';
				$total_conv = sql_field(sql_filter($sql, $user->d('user_id'), $user->d('user_id'), $user->d('user_id')), 'total', 0);

				$sql = 'SELECT c.msg_id, c.parent_id, c.last_msg_id, c.root_conv, c.privmsgs_date, c.privmsgs_subject, c2.privmsgs_date as last_privmsgs_date, m.user_id, m.username, m.username_base, m2.user_id as user_id2, m2.username as username2, m2.username_base as username_base2
					FROM _dc c, _dc c2, _members m, _members m2
					WHERE (c.privmsgs_to_userid = ? OR c.privmsgs_from_userid = ?)
						AND c.msg_id = c.parent_id
						AND c.msg_deleted <> ?
						AND c.privmsgs_from_userid = m.user_id
						AND c.privmsgs_to_userid = m2.user_id
						AND (IF(c.last_msg_id,c.last_msg_id,c.msg_id) = c2.msg_id)
					ORDER BY c2.privmsgs_date DESC
					LIMIT ??, ??';
				if ($result = sql_rowset(sql_filter($sql, $user->d('user_id'), $user->d('user_id'), $user->d('user_id'), $offset, $config['posts_per_page']))) {
					_style('messages');

					foreach ($result as $row) {
						$dc_with = ($user->d('user_id') == $row['user_id']) ? '2' : '';
						if (!$row['last_msg_id']) {
							$row['last_msg_id'] = $row['msg_id'];
							$row['last_privmsgs_date'] = $row['privmsgs_date'];
						}

						$dc_subject = 'Conversaci&oacute;n con ' . $row['username'.$dc_with];

						_style('messages.item', array(
							'S_MARK_ID' => $row['parent_id'],
							'SUBJECT' => $dc_subject,
							'U_READ' => s_link('my dc read', $row['last_msg_id']) . '#' . $row['last_msg_id'],
							'POST_DATE' => $user->format_date($row['last_privmsgs_date'], 'j F Y \a \l\a\s H:i') . ' horas.',
							'ROOT_CONV' => $row['root_conv'],

							'DC_USERNAME' => $row['username' . $dc_with],
							'DC_PROFILE' => s_link('m', $row['username_base' . $dc_with]))
						);
					}

					build_num_pagination(s_link('my dc s%d'), $total_conv, $config['posts_per_page'], $offset);
				} else if ($total_conv) {
					redirect(s_link('my dc'));
				} else {
					_style('no_messages');
				}

				_style('dc_total', array(
					'TOTAL' => $total_conv)
				);
				break;
		}

		//
		// Get friends for this member
		//
		$sql = 'SELECT DISTINCT m.user_id, m.username, m.username_base
			FROM _members_friends f, _members m
			WHERE (f.user_id = ? AND f.buddy_id = m.user_id)
				OR (f.buddy_id = ? AND f.user_id = m.user_id)
			ORDER BY m.username';
		if ($result = sql_rowset(sql_filter($sql, $user->d('user_id'), $user->d('user_id')))) {
			_style('sdc_friends', array(
				'DC_START' => s_link('my dc start'))
			);

			foreach ($result as $row) {
				_style('sdc_friends.item', array(
					'USERNAME' => $row['username'],
					'URL' => s_link('my dc start', $row['username_base']))
				);
			}
		}

		//
		// Output template
		//
		$page_title = ($mode == 'read') ? lang('dconv_read') : lang('dconvs');

		$layout_vars = array(
			'L_CONV' => $page_title,
			'S_ACTION' => s_link('my dc'),
			'S_HIDDEN_FIELDS' => s_hidden($s_hidden_fields)
		);

		page_layout($page_title, 'conversations', $layout_vars);
	}
Example #2
0
	public function all() {
		global $config, $user;

		$timezone = $config['board_timezone'] * 3600;

		list($d, $m, $y) = explode(' ', gmdate('j n Y', time() + $user->timezone + $user->dst));
		$midnight = gmmktime(0, 0, 0, $m, $d, $y) - $user->timezone - $user->dst;

		$g = getdate($midnight);
		$week = mktime(0, 0, 0, $m, ($d + (7 - ($g['wday'] - 1)) - (!$g['wday'] ? 7 : 0)), $y) - $timezone;

		$per_page = 6;

		$sql = 'SELECT *
			FROM _events
			ORDER BY date ASC';
		$result = sql_rowset($sql);

		foreach ($result as $row) {
			if ($row['date'] >= $midnight && !$row['images']) {
				if ($row['date'] >= $midnight && $row['date'] < $midnight + 86400) {
					$this->data['is_today'][] = $row;
				} else if ($row['date'] >= $midnight + 86400 && $row['date'] < $midnight + (86400 * 2)) {
					$this->data['is_tomorrow'][] = $row;
				} else if ($row['date'] >= $midnight + (86400 * 2) && $row['date'] < $week) {
					$this->data['is_week'][] = $row;
				} else {
					$this->data['is_future'][] = $row;
				}
			} else {
				if ($row['images']) {
					$this->data['is_gallery'][] = $row;
				}
			}
		}

		$total_gallery = sizeof($this->data['is_gallery']);

		if ($total_gallery) {
			$gallery_offset = request_var('gallery_offset', 0);

			$gallery = $this->data['is_gallery'];
			@krsort($gallery);

			$gallery = array_slice($gallery, $gallery_offset, $per_page);

			$event_ids = w();
			foreach ($gallery as $item) {
				$event_ids[] = $item['id'];
			}

			$sql = 'SELECT *
				FROM _events_images
				WHERE event_id IN (??)
				ORDER BY RAND()';
			$result = sql_rowset(sql_filter($sql, implode(',', $event_ids)));

			$random_images = w();
			foreach ($result as $row) {
				$random_images[$row['event_id']] = $row['image'];
			}

			_style('gallery', array(
				'EVENTS' => $total_gallery)
			);

			foreach ($gallery as $item) {
				_style('gallery.item', array(
					'URL' => s_link('events', $item['event_alias']),
					'TITLE' => $item['title'],
					'IMAGE' => $config['events_url'] . 'gallery/' . $item['id'] . '/thumbnails/' . $random_images[$item['id']] . '.jpg',
					'DATETIME' => $user->format_date($item['date'], lang('date_format')))
				);
			}

			build_num_pagination(s_link('events', 'g%d'), $total_gallery, $per_page, $gallery_offset);

			unset($this->data['is_gallery']);
		}

		if (!sizeof($this->data)) {
			return;
		}

		_style('future');

		foreach ($this->data as $is_date => $data) {
			_style('future.set', array(
				'L_TITLE' => lang('ue_' . $is_date))
			);

			foreach ($data as $item) {
				$event_mini = $config['events_path'] . 'mini/' . $item['id'] . '.jpg';

				if (@file_exists($event_mini)) {
					$event_image = $config['events_url'] . 'mini/';
				} else {
					$event_image = $config['events_url'] . 'future/';
				}

				_style('future.set.item', array(
					'ITEM_ID' => $item['id'],
					'TITLE' => $item['title'],
					'DATE' => $user->format_date($item['date'], lang('date_format')),
					'THUMBNAIL' => $event_image . $item['id'] . '.jpg',
					'SRC' => $event_image . $item['id'] . '.jpg?u=' . $item['event_update'],
					'U_TOPIC' => s_link('events', $item['event_alias']))
				);
			}
		}

		return;
	}
Example #3
0
	public function run() {
		global $config, $auth, $user, $comments;

		$topic_id = request_var('t', 0);
		$post_id = request_var('p', 0);

		if (!$topic_id && !$post_id) {
			fatal_error();
		}

		//
		// Get topic data
		//
		if ($post_id) {
			$sql_from = ', _forum_posts p, _forum_posts p2, _members m ';
			$sql_where = sql_filter('p.post_id = ? AND p.poster_id = m.user_id AND t.topic_id = p.topic_id AND p2.topic_id = p.topic_id AND p2.post_id <= ?', $post_id, $post_id);
			$sql_count = ', p.post_text, m.username AS reply_username, COUNT(p2.post_id) AS prev_posts, p.post_deleted';
			$sql_order = ' GROUP BY p.post_id, t.topic_id, t.topic_title, t.topic_locked, t.topic_replies, t.topic_time, t.topic_important, t.topic_vote, t.topic_last_post_id, f.forum_name, f.forum_locked, f.forum_id, f.auth_view, f.auth_read, f.auth_post, f.auth_reply, f.auth_announce, f.auth_pollcreate, f.auth_vote ORDER BY p.post_id ASC';
		} else {
			$sql_from = $sql_count = $sql_order = '';
			$sql_where = sql_filter('t.topic_id = ?', $topic_id);
		}

		$sql = 'SELECT t.*, f.*' . $sql_count . '
			FROM _forum_topics t, _forums f' . $sql_from . '
			WHERE ' . $sql_where . ' AND f.forum_id = t.forum_id' . $sql_order;
		if (!$topic_data = sql_fieldrow($sql)) {
			fatal_error();
		}

		switch ($topic_data['forum_alias']) {
			case 'events':
				$sql = 'SELECT event_alias
					FROM _events
					WHERE event_topic = ?';
				if ($event_alias = sql_field(sql_filter($sql, $topic_data['topic_id']), 'event_alias', '')) {
					redirect(s_link('events', $event_alias));
				}
				break;
		}

		//
		// Hide deleted posts
		if (isset($topic_data['post_deleted']) && $topic_data['post_deleted']) {
			fatal_error();
		}

		//
		// Check mod auth
		$mod_auth = $user->is('mod');

		//
		// Init vars
		//
		$forum_id = (int) $topic_data['forum_id'];
		$topic_id = (int) $topic_data['topic_id'];
		$topic_url = s_link('topic', $topic_id);

		$reply = request_var('reply', 0);
		$start = request_var('offset', 0);
		$submit_reply = _button('post');
		$submit_vote = _button('vote');

		$post_message = '';
		$post_reply_message = '';
		$post_np = '';
		$current_time = time();

		$error = $is_auth = w();

		if (!$post_id && $reply) {
			$reply = 0;
		}

		//
		// Start member auth
		//
		$is_auth = $auth->forum(AUTH_ALL, $forum_id, $topic_data);

		if ($submit_reply || $submit_vote) {
			$auth_key = ($submit_reply) ? 'auth_reply' : 'auth_vote';

			if (((!$is_auth['auth_view'] || !$is_auth['auth_read'])) || !$is_auth[$auth_key]) {
				if (!$user->is('member')) {
					do_login();
				}

				$can_reply_closed = $auth->option(w('forum topics delete'));

				if (!$can_reply_closed && ($topic_data['forum_locked'] || $topic_data['topic_locked'])) {
					$error[] = 'TOPIC_LOCKED';

					if ($submit_vote && !$topic_data['topic_vote']) {
						$error[] = 'POST_HAS_NO_POLL';
					}
				}

				if (!sizeof($error)) {
					redirect($topic_url);
				}
			}

			if (!sizeof($error)) {
				if ($submit_vote) {
					$vote_option = request_var('vote_id', 0);

					if ($vote_option) {
						$sql = 'SELECT vd.vote_id
							FROM _poll_options vd, _poll_results vr
							WHERE vd.topic_id = ?
								AND vr.vote_id = vd.vote_id
								AND vr.vote_option_id = ?
							GROUP BY vd.vote_id';
						if ($vote_id = sql_field(sql_filter($sql, $topic_id, $vote_option), 'vote_id', 0)) {
							$sql = 'SELECT *
								FROM _poll_voters
								WHERE vote_id = ?
									AND vote_user_id = ?';
							if (!sql_fieldrow(sql_filter($sql, $vote_id, $user->d('user_id')))) {
								$sql = 'UPDATE _poll_results SET vote_result = vote_result + 1
									WHERE vote_id = ?
										AND vote_option_id = ?';
								sql_query(sql_filter($sql, $vote_id, $vote_option));

								$insert_vote = array(
									'vote_id' => (int) $vote_id,
									'vote_user_id' => (int) $user->d('user_id'),
									'vote_user_ip' => $user->ip,
									'vote_cast' => (int) $vote_option
								);
								sql_insert('poll_voters', $insert_vote);
							}
						}
					}

					redirect(s_link('topic', $topic_id));
				} else {
					$post_message = request_var('message', '', true);
					$post_np = request_var('np', '');

					if ($reply) {
						$post_reply_message = request_var('reply_message', '', true);
					}

					// Check message
					if (empty($post_message)) {
						$error[] = 'EMPTY_MESSAGE';
					}

					if (!sizeof($error) && !$mod_auth)
					{
						$sql = 'SELECT MAX(post_time) AS last_post_time
							FROM _forum_posts
							WHERE poster_id = ?';
						if ($last_post_time = sql_field(sql_filter($sql, $user->d('user_id')))) {
							if (intval($last_post_time) > 0 && ($current_time - intval($last_post_time)) < intval($config['flood_interval'])) {
								$error[] = 'FLOOD_ERROR';
							}
						}
					}

					if (!sizeof($error)) {
						$update_topic = w();

						if (strstr($post_message, '-Anuncio-') && $user->is('mod')) {
							$topic_announce = 1;
							$post_message = str_replace('-Anuncio-', '', $post_message);
							$update_topic['topic_announce'] = $topic_announce;
						}

						if (strstr($post_message, '-Cerrado-') && $user->is('mod')) {
							$topic_locked = 1;
							$post_message = str_replace('-Cerrado-', '', $post_message);
							$update_topic['topic_locked'] = $topic_locked;
						}

						$post_message = $comments->prepare($post_message);

						if ($reply && $post_reply_message != '') {
							$post_reply_message = preg_replace('#(^|[\n ]|\()(http|https|ftp)://([a-z0-9\-\.,\?!%\*_:;~\\&$@/=\+]+)(gif|jpg|jpeg|png)#ie', '', $post_reply_message);
						}

						if ($reply && empty($post_reply_message)) {
							$post_reply_message = '...';
						}

						if ($reply && $post_reply_message != '') {
							$post_message = '<blockquote><strong>' . $topic_data['reply_username'] . "</strong>" . nr(false, 2) . $post_reply_message . '</blockquote><br /> ' . $post_message;
						} else {
							$reply = 0;
						}

						$insert_data = array(
							'topic_id' => (int) $topic_id,
							'forum_id' => (int) $forum_id,
							'poster_id' => (int) $user->d('user_id'),
							'post_time' => (int) $current_time,
							'poster_ip' => $user->ip,
							'post_text' => $post_message,
							'post_np' => $post_np
						);
						if ($reply) {
							$insert_data['post_reply'] = $post_id;
						}

						$post_id = sql_insert('forum_posts', $insert_data);

						$user->delete_unread(UH_T, $topic_id);
						$user->save_unread(UH_T, $topic_id);

						if (!in_array($forum_id, forum_for_team_array()) && $topic_data['topic_points']) {
							//$user->points_add(1);
						}

						//
						$a_list = forum_for_team_list($forum_id);
						if (count($a_list)) {
							$sql_delete_unread = 'DELETE FROM _members_unread
								WHERE element = ?
									AND item = ?
									AND user_id NOT IN (??)';
							sql_query(sql_filter($sql_delete_unread, 8, $topic_id, implode(', ', $a_list)));
						}

						$update_topic['topic_last_post_id'] = $post_id;

						if ($topic_locked) {
							topic_feature($topic_id, 0);
						}

						$sql = 'UPDATE _forums SET forum_posts = forum_posts + 1, forum_last_topic_id = ?
							WHERE forum_id = ?';
						sql_query(sql_filter($sql, $topic_id, $forum_id));

						$sql = 'UPDATE _forum_topics SET topic_replies = topic_replies + 1, ' . sql_build('UPDATE', $update_topic) . sql_filter('
							WHERE topic_id = ?', $topic_id);
						sql_query($sql);

						$sql = 'UPDATE _members SET user_posts = user_posts + 1
							WHERE user_id = ?';
						sql_query(sql_filter($sql, $user->d('user_id')));

						redirect(s_link('post', $post_id) . '#' . $post_id);
					}
				}
			}
		}

		if (!$is_auth['auth_view'] || !$is_auth['auth_read']) {
			if (!$user->is('member')) {
				do_login();
			}

			fatal_error();
		}

		if ($post_id) {
			$start = floor(($topic_data['prev_posts'] - 1) / (int) $config['posts_per_page']) * (int) $config['posts_per_page'];
			$user->d('user_topic_order', 0);
		}

		if ($user->is('member')) {
			//
			// Is user watching this topic?
			//
			$sql = 'SELECT notify_status
				FROM _forum_topics_fav
				WHERE topic_id = ?
					AND user_id = ?';
			if (!sql_field(sql_filter($sql, $topic_id, $user->d('user_id')), 'notify_status')) {
				if (_button('watch')) {
					$sql_insert = array(
						'user_id' => $user->d('user_id'),
						'topic_id' => $topic_id,
						'notify_status' => 0
					);
					sql_insert('forum_topics_fav', $sql_insert);

					redirect($topic_url . (($start) ? 's' . $start . '/' : ''));
				}

				_style('watch_topic');
			}
		}

		//
		// Get all data for the topic
		//
		$get_post_id = ($reply) ? 'post_id' : 'topic_id';
		$get_post_data['p.' . $get_post_id] = ${$get_post_id};

		if (!$user->is('founder')) {
			$get_post_data['p.post_deleted'] = 0;
		}

		$sql = 'SELECT p.*, u.user_id, u.username, u.username_base, u.user_avatar, u.user_posts, u.user_gender, u.user_rank, u.user_sig
			FROM _forum_posts p, _members u
			WHERE u.user_id = p.poster_id
				AND p.post_deleted = 0
				AND ' . sql_build('SELECT', $get_post_data) . '
			ORDER BY p.post_time ' . (($user->d('user_topic_order')) ? 'DESC' : 'ASC') .
			((!$reply) ? ' LIMIT ' . (int) $start . ', ' . (int) $config['posts_per_page'] : '');
		if (!$messages = sql_rowset($sql)) {
			if ($topic_data['topic_replies'] + 1) {
				fatal_error();
			}

			redirect(s_link('topic', $topic_id));
		}

		//
		// Re-count topic replies
		//
		if ($user->is('founder')) {
			$sql = 'SELECT COUNT(p.post_id) AS total
				FROM _forum_posts p, _members u
				WHERE p.topic_id = ?
					AND u.user_id = p.poster_id';
			if ($total = sql_field(sql_filter($sql, $topic_id), 'total')) {
				$topic_data['topic_replies2'] = $total - 1;
			}
		}

		//
		// Update the topic views
		//
		if (!$start && !$user->is('founder')) {
			$sql = 'UPDATE _forum_topics
				SET topic_views = topic_views + 1
				WHERE topic_id = ?';
			sql_query(sql_filter($sql, $topic_id));
		}

		//
		// If the topic contains a poll, then process it
		//
		if ($topic_data['topic_vote']) {
			$sql = 'SELECT vd.vote_id, vd.vote_text, vd.vote_start, vd.vote_length, vr.vote_option_id, vr.vote_option_text, vr.vote_result
				FROM _poll_options vd, _poll_results vr
				WHERE vd.topic_id = ?
					AND vr.vote_id = vd.vote_id
				ORDER BY vr.vote_option_order, vr.vote_option_id ASC';
			if ($vote_info = sql_rowset(sql_filter($sql, $topic_id))) {
				$sql = 'SELECT vote_id
					FROM _poll_voters
					WHERE vote_id = ?
						AND vote_user_id = ?';
				$user_voted = sql_field(sql_filter($sql, $vote_info[0]['vote_id'], $user->d('user_id')), 'vote_id', 0);

				$poll_expired = ($vote_info[0]['vote_length']) ? (($vote_info[0]['vote_start'] + $vote_info[0]['vote_length'] < $current_time) ? true : 0) : 0;

				_style('poll', array(
					'POLL_TITLE' => $vote_info[0]['vote_text'])
				);

				if ($user_voted || $poll_expired || !$is_auth['auth_vote'] || $topic_data['topic_locked']) {
					$vote_results_sum = 0;
					foreach ($vote_info as $row) {
						$vote_results_sum += $row['vote_result'];
					}

					_style('poll.results');

					foreach ($vote_info as $row) {
						$vote_percent = ($vote_results_sum > 0) ? $row['vote_result'] / $vote_results_sum : 0;

						_style('poll.results.item', array(
							'CAPTION' => $row['vote_option_text'],
							'RESULT' => $row['vote_result'],
							'PERCENT' => sprintf("%.1d", ($vote_percent * 100)))
						);
					}
				} else {
					_style('poll.options', array(
						'S_VOTE_ACTION' => $topic_url)
					);

					foreach ($vote_info as $row) {
						_style('poll.options.item', array(
							'POLL_OPTION_ID' => $row['vote_option_id'],
							'POLL_OPTION_CAPTION' => $row['vote_option_text'])
						);
					}
				}
			}
		}

		//
		// Advanced auth
		//

		$controls = $user_profile = w();
		$unset_user_profile = w('user_id user_posts user_gender');

		_style('posts');

		foreach ($messages as $row) {
			if ($user->is('member')) {
				$poster = ($row['user_id'] != GUEST) ? $row['username'] : (($row['post_username'] != '') ? $row['post_username'] : lang('guest'));

				$controls[$row['post_id']]['reply'] = s_link('post', $row['post_id'], 'reply');

				if ($mod_auth) {
					$controls[$row['post_id']]['edit'] = s_link('acp', array('forums_post_modify', 'msg_id' => $row['post_id']));
					$controls[$row['post_id']]['delete'] = s_link('acp', array('forums_post_delete', 'msg_id' => $row['post_id']));
				}
			}

			$user_profile[$row['user_id']] = $comments->user_profile($row, '', $unset_user_profile);

			$data = array(
				'POST_ID' => $row['post_id'],
				'POST_DATE' => $user->format_date($row['post_time']),
				'MESSAGE' => $comments->parse_message($row['post_text']),
				'PLAYING' => $row['post_np'],
				'DELETED' => $row['post_deleted'],
				'UNREAD' => 0
			);

			foreach ($user_profile[$row['user_id']] as $key => $value) {
				$data[strtoupper($key)] = $value;
			}

			_style('posts.item', $data);
			_style('posts.item.' . (($row['user_id'] != GUEST) ? 'username' : 'guestuser'));

			if (isset($controls[$row['post_id']])) {
				_style('posts.item.controls');

				foreach ($controls[$row['post_id']] as $item => $url) {
					_style('posts.item.controls.'.$item, array('URL' => $url));
				}
			}
		}

		//
		// Display Member topic auth
		//
		/*
		if ($mod_auth) {
			$mod = array((($topic_data['topic_important']) ? 'important' : 'normal'), 'delete', 'move', ((!$topic_data['topic_locked']) ? 'lock' : 'unlock'), 'split', 'merge');

			$mod_topic = w();
			foreach ($mod as $item) {
				if ($auth->option(array('forum', 'topics', $item))) {
					$mod_topic[strtoupper($item)] = s_link('acp', array('topic', topic' => $topic_id, 'mode' => $item));
				}
			}

			if (sizeof($mod_topic)) {
				_style('auth');

				foreach ($mod_topic as $k => $v) {
					_style('auth.item', array(
						'URL' => $v,
						'LANG' => lang($k . '_topic'))
					);
				}
			}
		}
		*/
		build_num_pagination($topic_url . 's%d/', ($topic_data['topic_replies'] + 1), $config['posts_per_page'], $start, '', 'TOPIC_');

		//
		// Posting box
		if (sizeof($error)) {
			_style('post_error', array(
				'MESSAGE' => parse_error($error))
			);
		}

		$can_reply_closed = $auth->option(array('forum', 'topics', 'delete'));

		if ((!$topic_data['forum_locked'] && !$topic_data['topic_locked']) || $can_reply_closed) {
			if ($user->is('member')) {
				if ($is_auth['auth_reply']) {
					$s_post_action = (($reply) ? s_link('post', $post_id, 'reply') : $topic_url) . '#e';

					_style('post_box', array(
						'MESSAGE' => $post_message,
						'NP' => $post_np,
						'S_POST_ACTION' => $s_post_action)
					);

					if ($reply) {
						if (empty($post_reply_message)) {
							$post_reply_message = $comments->remove_quotes($topic_data['post_text']);
						}

						if (!empty($post_reply_message)) {
							$rx = array('#(^|[\n ]|\()(http|https|ftp)://([a-z0-9\-\.,\?!%\*_:;~\\&$@/=\+]+)(gif|jpg|jpeg|png)#is', '#\[yt:[0-9a-zA-Z\-\=\_]+\]#is', '#\[sb\]#is', '#\[\/sb\]#is');
							$post_reply_message = preg_replace($rx, '', $post_reply_message);
						}

						if (empty($post_reply_message)) {
							$post_reply_message = '...';
						}

						_style('post_box.reply', array(
							'MESSAGE' => $post_reply_message)
						);
					}
				}
			}
		}

		// MOD: Featured topic
		if ($user->is('mod')) {
			$v_lang = ($topic_data['topic_featured']) ? 'REM' : 'ADD';

			_style('feature', array(
				'U_FEAT' => s_link('acp', array('forums_topic_feature', 'msg_id', $topic_data['topic_id'])),
				'V_LANG' => lang('topic_featured_' . $v_lang))
			);
		}

		//
		// Send vars to template
		//
		v_style(array(
			'FORUM_NAME' => $topic_data['forum_name'],
			'TOPIC_TITLE' => $topic_data['topic_title'],
			'TOPIC_REPLIES' => $topic_data['topic_replies'],

			'S_TOPIC_ACTION' => $topic_url . (($start) ? 's' . $start . '/' : ''),
			'U_VIEW_FORUM' => s_link('forum', $topic_data['forum_alias']))
		);

		$layout_file = 'topic';
		if (@file_exists('./template/custom/topics_' . $forum_id . '.htm')) {
			$layout_file = 'custom/topics_' . $forum_id;
		}

		if (@file_exists('./template/custom/topic_' . $topic_id . '.htm')) {
			$layout_file = 'custom/topic_' . $topic_id;
		}

		$this->_title = $topic_data['topic_title'];
		$this->_template = $layout_file;

		return;
	}
Example #4
-2
	public function run() {
		global $config, $auth, $user, $comments, $cache;

		$forum_id = request_var('f', '');
		$start = request_var('offset', 0);
		$submit_topic = _button('post');

		if (empty($forum_id)) {
			fatal_error();
		}

		$is_int_forumid = false;
		if (preg_match('#^(\d+)$#is', $forum_id)) {
			$is_int_forumid = true;
			$forum_id = intval($forum_id);

			$sql = 'SELECT *
				FROM _forums
				WHERE forum_id = ?';
			$sql = sql_filter($sql, $forum_id);
		} else {
			$sql = 'SELECT *
				FROM _forums
				WHERE forum_alias = ?';
			$sql = sql_filter($sql, $forum_id);
		}

		if (!$forum_row = sql_fieldrow($sql)) {
			fatal_error();
		}

		if ($is_int_forumid) {
			redirect(s_link('forum', $forum_row['forum_alias']), true);
		}

		$forum_id = $forum_row['forum_id'];

		//
		// Start auth check
		//
		$is_auth = w();
		$is_auth = $auth->forum(AUTH_ALL, $forum_id, $forum_row);

		if (!$is_auth['auth_view'] || !$is_auth['auth_read']) {
			if (!$user->is('member')) {
				do_login();
			}

			fatal_error();
		}

		$error_msg = '';
		$post_title = '';
		$post_message = '';
		$post_np = '';
		$poll_title = '';
		$poll_options = '';
		$poll_length = '';
		$current_time = time();

		if ($submit_topic) {
			$topic_important = _button('topictype');
			$auth_key = ($topic_important) ? 'auth_announce' : 'auth_post';

			if ($forum_row['forum_locked'] && !$is_auth['auth_mod']) {
				$error_msg .= (($error_msg != '') ? '<br />' : '') . lang('forum_locked');
			}

			if (!$is_auth[$auth_key]) {
				if (!$user->is('member')) {
					do_login();
				}

				if (empty($error_msg)) {
					redirect($topic_url);
				}
			}

			if (empty($error_msg)) {
				$post_title = request_var('topic_title', '');
				$post_message = request_var('message', '', true);
				$post_np = request_var('np', '', true);
				$poll_title = '';
				$poll_options = '';
				$poll_length = 0;

				if ($is_auth['auth_pollcreate']) {
					$poll_title = request_var('poll_title', '');
					$poll_options = request_var('poll_options', '');
					$poll_length = request_var('poll_length', 0);
				}

				// Check subject
				if (empty($post_title)) {
					$error_msg .= (($error_msg != '') ? '<br />' : '') . lang('empty_subject');
				}

				// Check message
				if (empty($post_message)) {
					$error_msg .= (($error_msg != '') ? '<br />' : '') . lang('empty_message');
				}

				if (!empty($poll_options)) {
					$real_poll_options = w();
					$poll_options = explode(nr(), $poll_options);

					foreach ($poll_options as $option) {
						if ($option != '') {
							$real_poll_options[] = $option;
						}
					}

					$sizeof_poll_options = sizeof($real_poll_options);

					if ($sizeof_poll_options < 2) {
						$error_msg .= (($error_msg != '') ? '<br />' : '') . lang('few_poll_options');
					} else if ($sizeof_poll_options > $config['max_poll_options']) {
						$error_msg .= (($error_msg != '') ? '<br />' : '') . lang('many_poll_options');
					} else if ($poll_title == '') {
						$error_msg .= (($error_msg != '') ? '<br />' : '') . lang('empty_poll_title');
					}
				}

				if (empty($error_msg) && !$is_auth['auth_mod']) {
					$sql = 'SELECT MAX(post_time) AS last_post_time
						FROM _forum_posts
						WHERE poster_id = ?';
					if ($last_post_time = sql_field(sql_filter($sql, $user->d('user_id')))) {
						if (intval($last_post_time) > 0 && ($current_time - intval($last_post_time)) < intval($config['flood_interval'])) {
							$error_msg .= (($error_msg != '') ? '<br />' : '') . lang('flood_error');
						}
					}
				}

				if (empty($error_msg)) {
					$topic_announce = 0;
					$topic_locked = 0;

					if ((strstr($post_message, '-Anuncio-') && $user->is('all')) || in_array($forum_id, array(15, 16, 17))) {
						$topic_announce = 1;
						$post_message = str_replace('-Anuncio-', '', $post_message);
					}

					if (strstr($post_message, '-Cerrado-') && $user->is('mod')) {
						$topic_locked = 1;
						$post_message = str_replace('-Cerrado-', '', $post_message);
					}

					$post_message = $comments->prepare($post_message);
					$topic_vote = (!empty($poll_title) && $sizeof_poll_options >= 2) ? 1 : 0;

					if (!$user->is('founder')) {
						$post_title = strnoupper($post_title);
					}

					$insert_data['TOPIC'] = array(
						'topic_title' => $post_title,
						'topic_poster' => (int) $user->d('user_id'),
						'topic_time' => (int) $current_time,
						'forum_id' => (int) $forum_id,
						'topic_locked' => $topic_locked,
						'topic_announce' => $topic_announce,
						'topic_important' => (int) $topic_important,
						'topic_vote' => (int) $topic_vote,
						'topic_featured' => 1,
						'topic_points' => 1
					);
					$topic_id = sql_insert('forum_topics', $insert_data['TOPIC']);

					$insert_data['POST'] = array(
						'topic_id' => (int) $topic_id,
						'forum_id' => (int) $forum_id,
						'poster_id' => (int) $user->d('user_id'),
						'post_time' => (int) $current_time,
						'poster_ip' => $user->ip,
						'post_text' => $post_message,
						'post_np' => $post_np
					);
					$post_id = sql_insert('forum_posts', $insert_data['POST']);

					if ($topic_vote) {
						$insert_data['POLL'] = array(
							'topic_id' => (int) $topic_id,
							'vote_text' => $poll_title,
							'vote_start' => (int) $current_time,
							'vote_length' => (int) ($poll_length * 86400)
						);
						$poll_id = sql_insert('poll_options', $insert_data['POLL']);

						$poll_option_id = 1;
						foreach ($real_poll_options as $option) {
							$insert_data['POLLRESULTS'] = array(
								'vote_id' => (int) $poll_id,
								'vote_option_id' => (int) $poll_option_id,
								'vote_option_text' => $option,
								'vote_result' => 0
							);
							sql_insert('poll_results', $insert_data['POLLRESULTS']);

							$poll_option_id++;
						}

						if ($forum_id == $config['main_poll_f']) {
							$cache->delete('last_poll_id');
						}
					}

					$user->save_unread(UH_T, $topic_id);

					if (!in_array($forum_id, forum_for_team_array())) {
						//$user->points_add(2);
					}

					$a_list = forum_for_team_list($forum_id);
					if (count($a_list)) {
						$sql_delete_unread = 'DELETE FROM _members_unread
							WHERE element = ?
								AND item = ?
								AND user_id NOT IN (??)';
						sql_query(sql_filter($sql_delete_unread, 8, $topic_id, implode(', ', $a_list)));
					}

					if (count($a_list) || in_array($forum_id, array(20, 39))) {
						topic_feature($topic_id, 0);
						topic_arkane($topic_id, 0);
					}

					$sql = 'UPDATE _forums SET forum_posts = forum_posts + 1, forum_last_topic_id = ?, forum_topics = forum_topics + 1
						WHERE forum_id = ?';
					sql_query(sql_filter($sql, $topic_id, $forum_id));

					$sql = 'UPDATE _forum_topics SET topic_first_post_id = ?, topic_last_post_id = ?
						WHERE topic_id = ?';
					sql_query(sql_filter($sql, $post_id, $post_id, $topic_id));

					$sql = 'UPDATE _members SET user_posts = user_posts + 1
						WHERE user_id = ?';
					sql_query(sql_filter($sql, $user->d('user_id')));

					redirect(s_link('topic', $topic_id));
				}
			}
		}
		//
		// End Submit
		//

		$topics_count = ($forum_row['forum_topics']) ? $forum_row['forum_topics'] : 1;

		$topics = new stdClass();
		$total = new stdClass();

		//
		// All announcement data
		//
		$sql = 'SELECT t.*, u.user_id, u.username, u.username_base, u2.user_id as user_id2, u2.username as username2, u2.username_base as username_base2, p.post_time, p.post_username as post_username2
			FROM _forum_topics t, _members u, _forum_posts p, _members u2
			WHERE t.forum_id = ?
				AND t.topic_poster = u.user_id
				AND p.post_id = t.topic_last_post_id
				AND p.poster_id = u2.user_id
				AND t.topic_announce = 1
			ORDER BY t.topic_last_post_id DESC';
		$topics->important = sql_rowset(sql_filter($sql, $forum_id));
		$total->important = (is_array($topics->important)) ? count($topics->important) : 0;

		//
		// Grab all the topics data for this forum
		//
		$sql = 'SELECT t.*, u.user_id, u.username, u.username_base, u2.user_id as user_id2, u2.username as username2, u2.username_base as username_base2, p.post_username, p2.post_username AS post_username2, p2.post_time
			FROM _forum_topics t, _members u, _forum_posts p, _forum_posts p2, _members u2
			WHERE t.forum_id = ?
				AND t.topic_poster = u.user_id
				AND p.post_id = t.topic_first_post_id
				AND p2.post_id = t.topic_last_post_id
				AND u2.user_id = p2.poster_id
				AND t.topic_announce = 0
			ORDER BY t.topic_important DESC, /*t.topic_last_post_id*/p2.post_time DESC
			LIMIT ??, ??';
		$topics->normal = sql_rowset(sql_filter($sql, $forum_id, $start, $config['topics_per_page']));
		$total->normal = (is_array($topics->normal)) ? count($topics->normal) : 0;

		//
		// Total topics ...
		//
		//$total_topics += $total_announcements;
		//$total_topics = $total->important + $total->normal;

		//
		// Post URL generation for templating vars
		//
		if ($is_auth['auth_post'] || $is_auth['auth_mod']) {
			_style('topic_create', array(
				'L_POST_NEW_TOPIC' => ($forum_row['forum_locked']) ? lang('forum_locked') : lang('post_newtopic'))
			);
		}

		//
		// Dump out the page header and load viewforum template
		//
		v_style(array(
			'FORUM_ID' => $forum_id,
			'FORUM_NAME' => $forum_row['forum_name'],
			'U_VIEW_FORUM' => s_link('forum', $forum_row['forum_alias']))
		);
		//
		// End header
		//

		//
		// Let's build the topics
		//
		$i = 0;
		foreach ($topics as $alias => $list) {
			foreach ($list as $j => $row) {
				if (!$i) {
					_style('topics');

					$topics_count -= $total->important;

					build_num_pagination(s_link('forum', $forum_row['forum_alias'], 's%d'), $topics_count, $config['topics_per_page'], $start, '', 'TOPICS_');
				}

				if (!$j) {
					_style('topics.alias', array(
						'NAME' => lang('topic_' . $alias),
						'SHOW' => ($total->important && $total->normal > 1))
					);
				}

				$row = (object) $row;

				if ($row->user_id != GUEST) {
					$row->author = '<a  href="' . s_link('m', $row->username_base2) . '">' . $row->username2 . '</a>';
				} else {
					$row->author = '<span>*' . (($row->post_username2 != '') ? $row->post_username2 : lang('guest')) . '</span>';
				}

				if ($row->user_id2 != GUEST) {
					$row->poster = '<a href="' . s_link('m', $row->username_base2) . '">' . $row->username2 . '</a>';
				} else {
					$row->poster = '<span>*' . (($row->post_username2 != '') ? $row->post_username2 : lang('guest')) . '</span>';
				}

				_style('topics.alias.row', array(
					'FORUM_ID' => $forum_id,
					'TOPIC_ID' => $row->topic_id,
					'TOPIC_AUTHOR' => $row->author,
					'REPLIES' => $row->topic_replies,
					'VIEWS' => ($user->is('founder')) ? $row->topic_views : '',

					'TOPIC_TITLE' => $row->topic_title,
					'TOPIC_CREATION_TIME' => $user->format_date($row->topic_time),
					'LAST_POST_TIME' => $user->format_date($row->post_time),
					'LAST_POST_AUTHOR' => $row->poster,
					'U_TOPIC' => s_link('topic', $row->topic_id))
				);

				$i++;
			}
		}

		if (!$topics_count) {
			if ($start) {
				redirect(s_link('forum', $forum_row['forum_alias']), true);
			}
			_style('no_topics');
		}

		//
		// Posting box
		//
		if (!empty($error_msg) || (!$is_auth['auth_mod'] && $forum_row['forum_locked']) || (!$is_auth['auth_post'] && $forum_row['auth_post'] == AUTH_REG) || $is_auth['auth_post']) {
			if ($is_auth['auth_post']) {
				if (!empty($poll_options)) {
					$poll_options = implode(nr(), $poll_options);
				}

				_style('publish', array(
					'S_POST_ACTION' => s_link('forum', $forum_row['forum_alias']),

					'TOPIC_TITLE' => $post_title,
					'MESSAGE' => $post_message,
					'NP' => $post_np,

					'POLL_TITLE' => $poll_title,
					'POLL_OPTIONS' => $poll_options,
					'POLL_LENGTH' => $poll_length)
				);

				if ($is_auth['auth_pollcreate']) {
					_style('publish.poll');

					if (empty($poll_options)) {
						_style('publish.poll.hide');
					}
				}
			}

			if (!empty($error_msg)) {
				_style('publish.alert', array(
					'MESSAGE' => $error_msg)
				);
			}
		}

		$layout_file = 'topics';

		$use_m_template = 'custom/forum_' . $forum_id;
		if (@file_exists(ROOT . 'template/' . $use_m_template . '.htm')) {
			$layout_file = $use_m_template;
		}

		$this->_title = $forum_row['forum_name'];
		$this->_template = $layout_file;

		return;
	}