File: //proc/self/root/scripts.20110531.215904.25158/send_anonymous_usage_data
#!/usr/bin/perl
# cpanel - send_anonymous_usage_data Copyright(c) 2010 cPanel, Inc.
# All rights Reserved.
# copyright@cpanel.net http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited
BEGIN { unshift @INC, '/usr/local/cpanel'; }
use IO::Socket ();
use Cpanel::Encoder::URI ();
use Cpanel::Config::Users ();
use Cpanel::PwCache ();
use Cpanel::Config::LoadCpConf ();
eval {
local $SIG{__DIE__};
local $SIG{__WARN__};
require JSON::Syck;
};
if ( !exists $INC{'JSON/Syck.pm'} ) {
exit; #not yet installed
}
my $version = 1.00;
my $cpconf = Cpanel::Config::LoadCpConf::loadcpconf();
if ( $cpconf->{'anon_data_optout'} ) {
exit;
}
# Only collect the data by uid as we do not want to tie it to a user.
# This data will help direct development
my %USERS = map { $_ => undef } Cpanel::Config::Users::getcpusers();
Cpanel::PwCache::no_uid_cache(); #uid cache only needed if we are going to make lots of getpwuid calls
Cpanel::PwCache::init_passwdless_pwcache();
my $pwcache_ref = Cpanel::PwCache::fetch_pwcache();
my $usage_json = "{\"version\":\"" . sprintf( "%0.2f", $version ) . "\",\"cpanel_access_data\":[";
my $user_data;
foreach my $pw (@$pwcache_ref) {
next if ( !exists $USERS{ $pw->[0] } || !-e $pw->[7] . '/.cpanel/nvdata/icFAA' || ( stat(_) )[7] > 65535 || ( stat(_) )[7] < 5 );
eval {
if ( my $json_data = JSON::Syck::LoadFile( $pw->[7] . '/.cpanel/nvdata/icFAA' ) ) {
$user_data .= "\"$pw->[2]\":" . JSON::Syck::Dump($json_data) . ",";
}
};
}
%USERS = ();
if ( !$user_data ) {
print "No user data available.\n";
exit;
}
$usage_json .= $user_data . '{}]}';
my $sock = IO::Socket::INET->new(
'PeerAddr' => 'anonusage.cpanel.net',
'PeerPort' => 'http(80)',
'Proto' => 'tcp',
'Timeout' => 60,
);
if ( !$sock ) {
print "Unable to send usage data.\n";
exit;
}
my $usage_json = 'client_version=' . $version . '&usage_data=' . Cpanel::Encoder::URI::uri_encode_str($usage_json);
my $cl = length($usage_json);
print {$sock} "POST /usage_upload.cgi HTTP/1.0\r\nConnection: close\r\nContent-length: $cl\r\nContent-type: application/x-www-form-urlencoded\r\n\r\n" . $usage_json or die "Failed to write to socket: $!";
my $seen_body = 0;
while ( readline($sock) ) {
if ($seen_body) { print; }
elsif (/^[\n\s]*$/) { $seen_body = 1; }
}
close($sock);