#!/usr/bin/perl
use strict;
$|=1;
use Zorpia::DB();
use Zorpia::Form();
use Zorpia::Content();
use Zorpia::Layout();
use Zorpia::Conv();
use Zorpia::Misc();
use Zorpia::Auth();
use Zorpia::User;
use Date::Format;
use Data::Dumper;
use Zorpia::Group();
use Zorpia::MemCache;
use CGI qw(:standard);
use CGI::Carp qw( fatalsToBrowser );
use Time::HiRes qw(gettimeofday);
use Zorpia::Config qw($config);
my $EXT = $config->{extension};
use vars qw($authObj $dbh_read $query $current_user $current_user_id $type $zorpian);
my $globals = &Zorpia::prepare_globals( member_mandatory => 0 );
$query = $globals->{query};
$authObj = $globals->{auth_obj};
$dbh_read = $globals->{dbh_read};
#------------------------------------#
my ($start_time,$end_time,$exe_time);
#------------------------------------#
$current_user_id = $globals->{user}->{id};
$current_user = $globals->{user};
my $NUM = 24;
if ($query->{'country'} eq ""){
if($current_user_id eq ""){
my ($ip_country, $ip_region, $ip_city) = &Zorpia::Misc::convert_IP_to_location($dbh_read,$ENV{'REMOTE_ADDR'});
my $country_id = &Zorpia::DB::check_if_row_exist($dbh_read, "location_country", "country_id", "code LIKE '$ip_country'") if ($ip_country);
$query->{'country'} = $country_id if($country_id);
} else {
$query->{'country'} = $current_user->{'country_id'};
}
}
# $zorpian is the sign for get the ajax code
# $type is the type of zorpian in scroll area
$type = $query->{'type'};
$zorpian = $query->{'zorpian'};
#------------------------------main body------------------------------------#
unless ($zorpian){
my $template = "home/main.html";
&print_index_page_form($globals,$template);
} else {
print "Content-type:text/html\n\n";
my $string = &member_scroll_div_code($type,$query->{'country'});
print "$string";
}
#----------------------------------------------------------------------------#
sub print_index_page_form
{
my ($globals,$template) = @_;
my $count;
my $first_name;
my $online_string;
my $royal_string;
my $key_country_related = "popular_group,top_group,hot_topics,just_updated,$query->{'country'},$query->{'state'},$query->{'city'}";
my $hash_country_related = Zorpia::MemCache::get($key_country_related);
if ($hash_country_related eq "") {
my ($city_options,$index,$city_disabled,$state_options,$state_disabled);
my $just_updated_string = &member_scroll_div_code('just_updated',$query->{'country'});
#-get group & topics-------------#
my $popular_groups = &get_popular_groups($query->{'country'});
if ($popular_groups->[0] eq ""){
$popular_groups = &get_popular_groups();
}
my $top_group = $popular_groups->[0];
#--------------------------------#
my $country_options = &print_country_options($authObj, $dbh_read, $query->{'country'});
if ($query->{'country'}){
($state_options,$index) = &print_state_options($authObj, $dbh_read, $query->{'country'}, $query->{'state'});
if($index !=0){
$state_disabled = "";
} else {
$state_disabled = "disabled";
}
} else {
$state_options = "";
$state_disabled = "disabled";
}
if ($query->{'country'}){
($city_options,$index) = &print_city_options($authObj, $dbh_read, $query->{'country'}, $query->{'state'}, $query->{'city'});
if($index != 0){
$city_disabled = "";
} else {
$city_disabled = "disabled";
}
} else {
$city_options = "";
$city_disabled = "disabled";
}
$hash_country_related = {
popular_groups => $popular_groups,
top_group => $top_group,
just_updated => $just_updated_string,
country_options => $country_options,
state_options => $state_options,
state_disabled => $state_disabled,
city_options => $city_options,
city_disabled => $city_disabled,
};
Zorpia::MemCache::set($key_country_related,$hash_country_related,900);
}
#--add hot_topic----------------------------#
my $key_hot_topics = "##hot_topic_for_en,www_site##";
my $hot_topics = Zorpia::MemCache::get($key_hot_topics);
if ($hot_topics eq "") {
$hot_topics = &get_hot_topics();
Zorpia::MemCache::set($key_hot_topics,$hot_topics,900);
}
#---end -------------------------------------#
my $key_international = "population,ysd_join_member,online-international";
my $hash_international = Zorpia::MemCache::get($key_international);
if($hash_international eq ""){
my $population = &get_population_of_zorpia($dbh_read);
my $ysd_join_number = &get_yesterday_join_number($dbh_read);
my $online_number = &get_online_number($dbh_read);
$hash_international = {
population => $population,
ysd_join_number => $ysd_join_number,
online_number => $online_number,
};
Zorpia::MemCache::set($key_international,$hash_international,900);
}
my $vars = {
globals => $globals,
title => "Free Photo Sharing, Journal and Social Networking",
tab => "hom",
no_ad => "1",
width => "0",
};
&Zorpia::Layout::print_header($vars);
my $vars = {
globals => $globals,
popular_groups => $hash_country_related->{popular_groups},
top_group => $hash_country_related->{top_group},
hot_topics => $hot_topics,
country_options => $hash_country_related->{country_options},
state_options => $hash_country_related->{state_options},
state_disabled => $hash_country_related->{state_disabled},
city_options => $hash_country_related->{city_options},
city_disabled => $hash_country_related->{city_disabled},
just_updated => $hash_country_related->{just_updated},
population => $hash_international->{population},
ysd_join_number => $hash_international->{ysd_join_number},
online_number => $hash_international->{online_number},
current_user_id => $current_user_id,
};
$globals->{template}->process("$template",$vars);
&Zorpia::Layout::print_footer($globals);
}
sub member_scroll_div_code
{
my ($type,$country) = @_;
my ($member_hash,$member_string, $count);
my $first_name;
my $online_string;
my $royal_string;
#print "Content-type:text/html\n\n";
#$start_time = gettimeofday();
#--------------------------------------------------#
$member_hash = &get_popular_member($type,$country);
#---------------------------------------------------#
#$end_time = gettimeofday();
#$exe_time = $end_time - $start_time;
#print "
get data form db time: $exe_time
";
#$start_time = gettimeofday();
#------------------------------------------------------#
$member_hash = &fill_in_member_list($member_hash,$type);
#------------------------------------------------------#
#$end_time = gettimeofday();
#$exe_time = $end_time - $start_time;
#print "
if less ,fill the list time: $exe_time
";
$member_string = qq(
);
$member_string .= qq();
foreach(@{$member_hash}){
$first_name = &deal_with_name_length($_->{'first_name'});
$online_string = qq(
) if($_->{'online'}); $royal_string = qq(
) if ($_->{'premium'});
$_->{'avatar'}=~s/ppt\.zorpia/ppn\.zorpia/g;
$member_string .= qq( | ) unless ($count % 6);
$member_string .= qq( | ) if ($count < 20 && !($count % 6));
undef($online_string);
undef($royal_string);
}
$member_string .=qq(
);
return $member_string;
}
sub get_popular_member
{
my ($type,$country_id) = @_;
my @member_list;
my $sth;
my $country;
my $country_memc;
my $sql_statement;
my $key;
my $result;
my %hash;
$country = " AND country_id='$country_id'" if ($country_id);
$country_memc = ",country_id=>'$country_id'" if ($country_id);
#print "Content-type:text/html\n\n";
if ($type eq "just_updated"){
$key = &get_memcache_key("user_details","user_id","profile_photos=>'>0', photos=>'>0',last_update=>'DESC'$country_memc");
$result = Zorpia::MemCache::get($key);
unless($result){
$sql_statement = qq(SELECT user_id FROM user_details WHERE 1 $country AND profile_photos>0 AND photos>0 ORDER BY last_update DESC LIMIT 0,$NUM);
} else {
return $result;
}
} elsif ($type eq "hot_girls"){
$key = &get_memcache_key("photo_ranking","user_id","score=>'>7',vote_no=>'>10',gender=>'female'$country_memc");
$result = Zorpia::MemCache::get($key);
#print "
key:$key
result:$result
";
unless ($result){
$sql_statement = qq(SELECT DISTINCT user_id FROM photo_ranking WHERE 1 AND score >7 AND vote_no >10 AND gender='female' $country);
} else {
return $result;
}
} elsif ($type eq "hot_guys"){
$key = &get_memcache_key("photo_ranking","user_id","score=>'>5',vote_no=>'>10',gender=>'male'$country_memc");
$result = Zorpia::MemCache::get($key);
#print "
key:$key
result:$result
";
unless ($result){
$sql_statement = qq(SELECT DISTINCT user_id FROM photo_ranking WHERE 1 AND score >5 AND vote_no >10 AND gender='male' $country);
} else {
return $result;
}
} elsif ($type eq "newbies"){
$key = &get_memcache_key("user_details","user_id","profile_photo=>'>0',photos=>'>0',registration=>'DESC'$country_memc");
$result = Zorpia::MemCache::get($key);
#print "
key:$key
result:$result
";
unless ($result){
$sql_statement = qq(SELECT user_id FROM user_details WHERE 1 $country AND profile_photos>0 AND photos>0 ORDER BY registration_time DESC LIMIT 0,$NUM);
} else {
return $result;
}
} elsif ($type eq "oldies"){
$key = &get_memcache_key("user_details","user_id","profile_photos=>'>0',photos=>'>0',last_visit=>'<(60*60*24*90)'$country_memc");
$result = Zorpia::MemCache::get($key);
#print "
key:$key
result:$result
";
unless ($result){
$sql_statement = qq(SELECT user_id FROM user_details WHERE 1 $country AND profile_photos>0 AND photos>0 AND last_visit <(60*60*24*90) LIMIT 0,$NUM);
} else {
return $result;
}
}
#print "
$sql_statement
";
$sth = $dbh_read->prepare($sql_statement);
$sth->execute;
my $i = 0;
if ($type eq "hot_girls" || $type eq "hot_guys"){
while(my $temp = $sth->fetchrow_array){
$hash{$i} = $temp;
$i++;
}
@member_list = &get_random_member_from_hash(\%hash);
undef(%hash);
} else{
while(my $temp = $sth->fetchrow_array){
$member_list[$i] = &Zorpia::User::get($dbh_read,$temp,undef,'both');
$i++;
}
}
$sth->finish;
Zorpia::MemCache::set($key,\@member_list,900);
return \@member_list;
}
sub get_random_member_from_hash
{
my $hash = shift;
my @member_list;
my @keys = keys(%{$hash});
my $hash_number = @keys;
my $random;
my @memb;
my @temp;
my $sign;
my $i =0;
while($i<$hash_number){
$random = int(rand($hash_number));
@temp = $random;
$sign = &array_intersect(\@temp,\@memb);
next if($sign->[0] ne "");
push(@memb,$random);
$member_list[$i] = &Zorpia::User::get($dbh_read,$hash->{$random},undef,'both');
$i++;
last if($i == 24);
}
return @member_list;
}
sub fill_in_member_list {
my ($member_array, $member_type) = @_;
my $number = @{$member_array};
my $temp_array;
my $fill_number;
my $sign;
my @user_id;
if ($number < $NUM){
$fill_number = $NUM - $number;
$temp_array = &get_popular_member($member_type,"");
if ($number == 0){
$member_array = $temp_array;
} else {
foreach(@{$member_array}){
push(@user_id,$_->{'user_id'});
}
for (my $i=0;$i<$fill_number;$i++){
my @temp;
push(@temp,$temp_array->[$i]->{'user_id'});
$sign = &array_intersect(\@temp,\@user_id);
if($sign->[0] eq ""){
push(@{$member_array},$temp_array->[$i]);
}
}
}
}
return $member_array;
}
sub deal_with_name_length
{
my $name = shift;
my $name_length = length($name);
if ($name_length > 6){
$name = substr($name,0,4);
$name .= "...";
}
return $name;
}
sub array_intersect
{
my ($a,$b) = @_;
my (%union,%isect,@union,@isect);
foreach my $e (@{$a}, @{$b}) { $union{$e}++ && $isect{$e}++ }
@isect = keys %isect;
return \@isect;
}
sub get_popular_groups
{
my $country_id = shift;
my $i = 0;
my @popular_groups;
my $sth;
my $hash_point;
my $country;
$country = " AND country_id='$country_id'" if ($country_id);
my $sql_statement = qq(SELECT group_id FROM `group` WHERE rating='0' $country ORDER BY users DESC LIMIT 0 , 5);
$sth = $dbh_read->prepare($sql_statement);
#print "
group's sql :
$sql_statement
";
#$start_time = gettimeofday();
$sth->execute;
#$end_time = gettimeofday();
#$exe_time = $end_time - $start_time;
#print "
get_group_member time:$exe_time
";
while(my $temp = $sth->fetchrow_array){
$hash_point = &Zorpia::Group::get_group_info($globals,$temp);
$hash_point->{'name'} = &deal_with_title_length($hash_point->{'name'});
$popular_groups[$i] = $hash_point;
$i++;
}
return \@popular_groups;
}
sub get_hot_topics
{
my @hot_topics;
my $i = 0;
my $sth;
my $sql_statement = < 0, last_post_when, time) DESC
LIMIT 0,5
SQL_STATEMENT
$sth = $dbh_read->prepare($sql_statement);
#print "
get topics 's sql:
$sql_statement
";
#$start_time = gettimeofday();
$sth->execute;
#$start_time = gettimeofday();
#$exe_time = $end_time - $start_time;
#print "
get_hot_topics time:$exe_time
";
while(my ($forum_id, $forum_topic_id, $title) = $sth->fetchrow_array){
$hot_topics[$i] = &topics_info_into_hash($forum_id,$forum_topic_id,$title);
$i++;
}
return \@hot_topics;
}
sub topics_info_into_hash
{
my ($forum_id, $forum_topic_id, $title) = @_;
my %hash;
$hash{'topic_url'} = $authObj->processURL("/cgi/forum.cgi?fid=$forum_id&s=comments&tid=$forum_topic_id");
$hash{'title'} = &deal_with_title_length($title);
return \%hash;
}
sub deal_with_title_length
{
my $title = shift;
my $title_length = length($title);
if ($title_length > 30){
$title = substr($title,0,27);
$title .= "...";
}
return $title;
}
sub print_country_options
{
my %form;
my $authObj;
my $dbh;
($authObj, $dbh, $form{'country'}) = @_;
my %country_selected;
my $country = $form{'country'};
$country_selected{$country} = "selected";
my $sql_statement = <prepare($sql_statement);
$sth->execute;
my @country_arr;
my @country_id;
my $index = 0;
while ( my($country, $country_id)= $sth->fetchrow_array) {
$country_id[$index] = $country_id;
# $country_arr[$index] = &Zorpia::Conv::get_country_short_text($country_id);
$country_arr[$index] = $country;
$index++;
}
$sth->finish;
my $country_options = "";
for (my $i=0; $i<$index; $i++){
$country_options .= "";
}
return $country_options;
}
sub print_state_options
{
my %form;
my $authObj;
my $dbh;
($authObj, $dbh, $form{'country'}, $form{'state'}) = @_;
my %state_selected;
my $state = $form{'state'};
if (!defined($state)) {
$state = "";
}
$state_selected{$state} = "selected";
my $sql_statement = <state_sql:$sql_statement
";
my $sth = $dbh->prepare($sql_statement);
$sth->execute;
my @state_arr;
my @state_id;
my $index = 0;
while ( my($state_id, $state)= $sth->fetchrow_array) {
$state_id[$index] = $state_id;
$state_arr[$index] = $state;
$index++;
}
$sth->finish;
my $state_options = "";
for (my $i=0; $i<$index; $i++){
$state_options .= "";
}
#print "
$state_options
";
#exit;
return $state_options,$index;
}
sub print_city_options
{
my %form;
my $authObj;
my $dbh;
($authObj, $dbh, $form{'country'}, $form{'state'}, $form{'city'}) = @_;
my %city_selected;
my $city = $form{'city'};
if (!defined($city)) {
$city = "";
}
$city_selected{$city} = "selected";
my $state;
if ($form{'state'} ne ""){
$state = " AND state_id='$form{'state'}' ";
} else {
if ($form{'country'} eq '219' || $form{'country'} eq '13' || $form{'country'} eq '218'){
$state = " AND state_id='0' ";
} else {
$state = "";
}
}
my $sql_statement = "SELECT city_id, name FROM location_city WHERE country_id='$form{'country'}' $state ORDER BY name";
my $sth = $dbh->prepare($sql_statement);
#print "
$sql_statement
";
$sth->execute;
my @city_arr;
my @city_id;
my $index = 0;
while ( my($city_id, $city)= $sth->fetchrow_array) {
$city_id[$index] = $city_id;
$city_arr[$index] = $city;
$index++;
}
$sth->finish;
#print "
--signb$index
";
#print "
city_id @city_id
";
#print "
city_arr @city_arr
";
my $city_options = "";
#print "
sign a
";
for (my $i=0; $i<$index; $i++){
$city_options .= "";
}
return $city_options,$index;
}
sub get_memcache_key
{
my ($table, $column, $condition) = @_;
my $sql_hash = {
type => 'sql',
object => {
table => ["$table"],
column => ["$column"],
condition => {"$condition"},
},
};
my $key = &Zorpia::MemCache::get_key($sql_hash);
return $key;
}
sub get_population_of_zorpia
{
my $dbh = shift;
my $sql_statement;
my $sth;
my $population;
$sql_statement = qq(SELECT count(*) FROM user);
$sth = $dbh->prepare($sql_statement);
$sth->execute;
$population = $sth->fetchrow_array;
$sth->finish;
return $population;
}
sub get_yesterday_join_number
{
my $dbh = shift;
my $sql_statement;
my $sth;
my $ysd_join_number;
my $now = time();
my $yesterday = $now - 60*60*24;
$sql_statement = qq(SELECT count(*) FROM user WHERE registration_time>='$yesterday' AND registration_time<='$now');
$sth = $dbh->prepare($sql_statement);
$sth->execute;
$ysd_join_number = $sth->fetchrow_array;
$sth->finish;
return $ysd_join_number;
}
sub get_online_number
{
my $dbh = shift;
my $sql_statement;
my $sth;
my $online_number;
my $time = time()-(30*60);
$sql_statement = qq(SELECT count(*) FROM user WHERE last_visit>='$time');
$sth = $dbh->prepare($sql_statement);
$sth->execute;
$online_number = $sth->fetchrow_array;
$sth->finish;
return $online_number;
}
#-test_code--#
#$start_time = gettimeofday();
#$end_time = gettimeofday();
$exe_time = $end_time - $start_time;
#print "Content-type:text/html\n\n";
#print "
time: $exe_time
";
#------------#