/** User related functions **/
function get_unique_user($id = "", $email = "", $source, $tw_handle = ""){
$email = trim($email);
if(($id == null || $id == "" || $id == 0) && ($email == null || $email == "")){
return -1;
}
$email2 = str_replace("googlemail","gmail",$email);
switch($source){
case "facebook":
$condition = " (fb_id='$id' || fb_email='$email' || gp_email='$email' || gp_email = '$email2') && fb_id != ''";
$row = select_db(0,"user","uid",1,$condition);
break;
case "twitter":
$condition = " (tw_id='$id' || tw_handle='$tw_handle') && tw_id != ''";
$row = select_db(0,"user","uid",1,$condition);
break;
case "googleplus":
$condition = " gp_id='$id' && (gp_email='$email' || gp_email = '$email2' || fb_email='$email') && gp_id != ''";
$row = select_db(0,"user","uid",1,$condition);
break;
case "mpyr":
$condition = " mpyr_email IN ('$email','$email2') && mpyr_email != '' ";
$row = select_db(0,"user","uid",1,$condition);
break;
}
if($row == null){
return -1;
}else{
return $row['uid'];
}
}
function update_user($uid, $access_token, $json, $source){ //new information applied via json
if($uid < 1){
return false;
}
if($source == "facebook"){
$fvs = save_fb_user($access_token, $json, "", true, false);
$fvs['updated_dt'] = "now()";
$condition = "uid='$uid'";
update_db(0,"user",$fvs,$condition);
}else if($source == "twitter"){
$fvs = save_tw_user($json, "", true, false);
$fvs['updated_dt'] = "now()";
$condition = "uid='$uid'";
update_db(0,"user",$fvs,$condition);
}else if($source == "googleplus"){
$fvs = save_gp_user($access_token, $json, "", true, false);
$fvs['updated_dt'] = "now()";
$condition = "uid='$uid'";
update_db(0,"user",$fvs,$condition);
}
}
function get_source($uid){
$condition = " uid='$uid' ";
$row = select_db(0,"user","source",1,$condition);
if($row == null){
return "";
}else{
return $row['source'];
}
}
function auth_user($access_token, $source){
if($source == "facebook"){ //logging in using facebook
$url = "https://graph.facebook.com/me?access_token=".$access_token;
$content = get_api_results($url);
$json = json_decode($content, true);
if($json && isset($json['error']['message'])){ // access_token is not valid
header('Cache-Control: no-cache, must-revalidate');
header("content-type:application/json");
echo json_encode(array(
"success" => false,
"url" => $url,
"message" => $json['error']['message'],
"type" => $json['error']['type'],
"code" => $json['error']['code'],
"debug" => "Access token is not valid or missing",
));
//die('');
return false;
}else{ // access_token is valid
return $json;
}
}else if($source == "twitter"){
require 'Application/Views/Landings/content/twitter/tmhOAuth.php';
require 'Application/Views/Landings/content/twitter/tmhUtilities.php';
$tmhOAuth = new tmhOAuth(array(
'consumer_key' => 'dNWaFpHEVZTVYtzpvaRADw',
'consumer_secret' => 'wdJzjJv4yFtSPYv9nmZhc3D9sDzzQs7rFzxPtk',
));
if($access_token == ""){
$tmhOAuth->config['user_token'] = $_SESSION['access_token']['oauth_token'];
$tmhOAuth->config['user_secret'] = $_SESSION['access_token']['oauth_token_secret'];
}else{
$tmhOAuth->config['user_token'] = $access_token['oauth_token'];
$tmhOAuth->config['user_secret'] = $access_token['oauth_token_secret'];
}
//print_array($_SESSION['access_token']);
//echo "
";
$code = $tmhOAuth->request('GET', $tmhOAuth->url('1.1/account/verify_credentials'));
if($code == 200){
$json = json_decode($tmhOAuth->response['response'],true); // false by default (obj), if true (array)
return $json;
}else{
if(isset($_REQUEST['oauth_verifier'])){
$tmhOAuth->config['user_token'] = $_SESSION['oauth']['oauth_token'];
$tmhOAuth->config['user_secret'] = $_SESSION['oauth']['oauth_token_secret'];
$code = $tmhOAuth->request('POST', $tmhOAuth->url('oauth/access_token', ''), array(
'oauth_verifier' => $_REQUEST['oauth_verifier']
));
if ($code == 200) {
$_SESSION['access_token'] = $tmhOAuth->extract_params($tmhOAuth->response['response']);
$tmhOAuth->config['user_token'] = $_SESSION['access_token']['oauth_token'];
$tmhOAuth->config['user_secret'] = $_SESSION['access_token']['oauth_token_secret'];
$code = $tmhOAuth->request('GET', $tmhOAuth->url('1.1/account/verify_credentials'));
if($code == 200){
$json = json_decode($tmhOAuth->response['response'],true); // false by default (obj), if true (array)
return $json;
}else{
outputError($tmhOAuth);
return null;
}
} else {
outputError($tmhOAuth);
}
}
}
}else if($source == "googleplus"){
//do something with the token, first check is real
$data = @file_get_contents("https://www.googleapis.com/plus/v1/people/me?access_token={$access_token}");
$json = json_decode($data, true);
if($json){
return $json;
}else{
//use this since not all people have google plus accounts
$data = @file_get_contents("https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token={$access_token}");
$json = json_decode($data, true);
if($json){
return $json;
}else{
echo json_encode(array( // show as successful
"success" => false,
"message" => "Token not valid using oauth2 or google plus",
"access_token"=>$access_token,
));
return false;
}
}
}
return false;
}
function handle_login($source, $access_token, $json, $page, $createAccount = false){
$_SESSION['jid'] = $jid = $json['id'];
if($source == "facebook"){
$name = $json['first_name']." ". $json['last_name'];
$email = $json['email'];
}else if($source == "twitter"){
$name = $json['name'];
$tw_handle = $email = $json['screen_name'];
}else if($source == "googleplus"){
$name = $json['given_name']." ". $json['family_name'];
if(trim($name) == ""){
$name = $json['name']['givenName']." ".$json['name']['familyName'];
}
if(trim($name) == ""){
$name = $json['displayName'];
}
$email = $json['email'];
}
$uid = get_unique_user($jid, $email, $source);
//echo("first:".$uid);
if($uid > 0){
if(get_source($uid) == $source){ //login method is same as source
}else{
update_user($uid, $access_token, $json, $source); // //add new FB info to existing GP
}
$msg = "Welcome back, $name!";
}else{
$uid = get_unique_user("", $email, "mpyr");
//echo("second:".$uid);
//die('');
if($uid > 0){
update_user($uid, $access_token, $json, $source); // //add new FB info to existing GP
$msg = "Thank you for signing up, $name. Now you are ready to claim a property.";
}else{
$json['mpyr_email'] = $email;
/* Has never created an account before */
if($createAccount){
if($source == "facebook"){ //logging in using facebook
save_fb_user($access_token, $json, "", false, false);
}else if($source == "googleplus"){ //logging in using google
save_gp_user($access_token, $json, "", false, false);
}else if($source == "twitter"){ //logging in using twitter
save_tw_user($json, "", false, false);
}
$msg = "Thank you for signing up, $name. Now you are ready to claim a property.";
//echo("third:".$_SESSION['uid']);
}else{
$msg = "This account has not been linked with MPYR. Perhaps you used another account?";
return $msg;
}
}
}
//echo "uid:".$uid."
";
if($uid > 0){
$_SESSION['uid'] = $uid;
}
$_SESSION['source'] = $source;
$_SESSION['name'] = $name;
$_SESSION['uid'] = $uid;
$_SESSION['social_url'] = "https://mpyr.com/?ref=$uid";
$_SESSION['submitted_claim'] = get_number_submitted_claims($uid);
$_SESSION['submitted_login'] = $source;
return $msg;
}
function get_user_email($uid, $json = null, $source = ""){
if($json){
if($source == "facebook"){
$email = $json['email'];
}else if($source == "twitter"){
$tw_handle = $email = $json['screen_name'];
}else if($source == "googleplus"){
$email = $json['email'];
}
return $email;
}
$condition = " uid='$uid' ";
$row = select_db(0,"user","source,gp_email,fb_email,mpyr_email",1,$condition);
if($row['source'] == "facebook"){
return $row['fb_email'];
}else if($row['source'] == "googleplus"){
return $row['gp_email'];
}else{
if($row['mpyr_email'] != ""){
return $row['mpyr_email'];
}else{
return null;
}
}
}
function get_user_name($uid, $json = null, $source = ""){
if($json){
if($source == "facebook"){
$name = $json['first_name']." ". $json['last_name'];
}else if($source == "twitter"){
$name = $json['name'];
}else if($source == "googleplus"){
$name = $json['given_name']." ". $json['family_name'];
}
return $name;
}
$condition = " uid='$uid'";
$row = select_db(0,"user","source,first_name,last_name,tw_handle",1,$condition);
if($row == null){
return "Anonymous";
}else{
if($row['source'] == "twitter"){
return $row['tw_handle'];
}else{
return $row['first_name']." ".$row['last_name'];
}
}
}
function get_user_handle($uid){
$condition = " uid='$uid'";
$row = select_db(0,"user","tw_handle",1,$condition);
if($row == null){
return null;
}else{
return $row['tw_handle'];
}
}
function get_user_link($uid){
$condition = " uid='$uid' ";
$row = select_db(0,"user","source,gp_link,fb_link,tw_handle",1,$condition);
if($row == null){
//$link = "http://www.google.com";
}else{
if($row['source'] == "twitter"){
$link = "https://www.twitter.com/".$row['tw_handle'];
}else if($row['source'] == "facebook"){
$link = $row['fb_link'];
}else{
$link = $row['gp_link'];
}
return $link;
}
}
function get_pic($uid){
$condition = "uid='$uid'";
$pic = select_db(0,"user","fb_profile_image,tw_profile_image,gp_profile_image",1,$condition);
if($pic == null){
//return "http://faculty.sites.uci.edu/ltemplate/files/2011/04/generic_profile.jpg";
return null;
}else{
return $pic['local_image'];
/*
if($pic['fb_profile_image'] != ""){
return $pic['fb_profile_image'];
}else if($pic['tw_profile_image'] != ""){
return $pic['tw_profile_image'];
}else if($pic['gp_profile_image'] != ""){
return $pic['gp_profile_image'];
}else{
//return "http://faculty.sites.uci.edu/ltemplate/files/2011/04/generic_profile.jpg";
return null;
}
*/
}
}
function get_rank($uid,$db_rank="",$approved=0,$property = null){
if($uid > 0){
$condition = "uid='$uid'";
$user = select_db(0,"user","rank,approved",1,$condition);
$db_rank = $user['db_rank'];
$approved = $user['approved'];
}
if($approved == 1){
if($db_rank == ""){
if($property == null){ // no approved or pending properties
$rank = "Newbie? (no properties)";
}else if($property['ostatus'] == "on hold"){
//$rank = "Newbie? (pending)";
$rank = "Newbie";
}else if($property['ostatus'] == "approved"){
//$rank = "Newbie? (approved)";
$rank = "Newbie";
}
}else{
$rank = $db_rank;
}
}else if($approved == -1){
$rank = "Newbie? (rejected claim)";
}else{
if($uid > 0){
//$rank = "N/A (pending)";
$rank = "Newbie";
}else{
$rank = "N/A (unregistered)";
}
}
return $rank;
}
function get_award_status($uid, $type){
switch($type){
case "tweet":
$condition = " uid='$uid' && type='$type'";
$row = select_db(0,"awards","status",1,$condition);
if($row == null){
//return false;
return "Not Awarded Yet";
}else{
return $row['status'];
}
break;
}
}
function save_user_pic($uid,$url){
//save picture to local folder with a hash
$filename = $uid."-".uniqid();
$path = "/home/mpyr/public_html/Media/userpic/$filename.jpg";
$image = @file_get_contents($url);
@file_put_contents($path,$image);
$condition = "uid='$uid'";
$fvs = array();
$fvs['local_image'] = "https://www.mpyr.com/Media/userpic/$filename.jpg";
update_db(0,"user",$fvs,$condition);
}
function subscribe($email, $source, $ret_value = "", $uid = ""){
$email = str_replace("googlemail","gmail",$email);
/*
$url = "http://api.kickofflabs.com/v1/14738/subscribe?email=".$email."&skip_ar=1&ip=".$_SERVER["REMOTE_ADDR"];
if(isset($_SESSION['kid'])){
$url .= "&social_id=".$_SESSION['kid'];
}
$data = @file_get_contents($url);
$json = json_decode($data, true);
*/
/*
$json = subscribe_kol_alternative($email);
if($json){
$fvs = array();
$fvs['kol_social_id'] = $json['social_id'];
$fvs['kol_parent_id'] = $json['parent_id'];
$fvs['kol_social_id'] = $json['ref'];
$fvs['kol_parent_id'] = $_SESSION['kid'];
if($source == "facebook"){
$condition = "fb_email='$email'";
}else if($source == "googleplus"){
$condition = "gp_email='$email'";
}else if($source == "twitter"){
$condition = "mpyr_email='$email'";
}
$fvs['updated_dt'] = "now()";
update_db(0,"user",$fvs,$condition);
if($ret_value == ""){
return $json['social_url'];
}else{
return $json['social_id'];
}
}else{
return null;
}
*/
$uid = get_unique_user('uid',$email,$source);
if($ret_value == ""){
return "https://mpyr.com/ref=".$uid;
}else{
return $uid;
}
}
function subscribe_kol_alternative($email){
$url = "https://admin.bishopnetwork.com/ajax/save_email.php";
$referrer = $_SERVER['HTTP_REFERRER'];
$data = array('domain' => "mpyr.com", 'email' => $email, 'referrer' => $referrer, 'title'=>'title', 'ref' => $_GET['ref']);
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = @file_get_contents($url, false, $context);
$json = json_decode($result, true);
return $json;
}
function get_share_buttons($social_url){
if($social_url == null || $social_url == ""){
$social_url = "https://www.mpyr.com/";
}
$url = rawurlencode($social_url);
$share_buttons = "
";
return $share_buttons;
}
function get_share_text($social_url, $uid = 0){
$referrals = get_number_referrals($uid);
$share_text = "
Influenced so far:$referrals
";
//print_r($json);
//print_r($fvs);
//echo "";
if($uid < 1){ //user doesn't exist or it's rom
$fvs['signup_dt'] = "now()";
$fvs['signup_ip'] = $_SERVER["REMOTE_ADDR"];
$fvs['acct_type'] = "beta tester";
$fvs['notes'] = "pre-launch signup";
if($return_fvs){
return $fvs;
}else{
$fvs['source'] = $source; // don't overwrite source
$uid = insert_db(0,"user",$fvs); // save user to database
save_user_pic($uid,$fvs['fb_profile_image']);
$social_url = "https://mpyr.com/?ref=".$uid;
}
}
if($uid > 0){
$_SESSION['uid'] = $uid;
}
if($uid > 0 && has_approved_or_pending_properties($uid) == false){ //user is registered, but doesn't have approved or on hold properties so choose
$oid = save_ownership($uid, false); // take care of outputting
if($oid){
$name = "";
if(isset($json['first_name'])){
$name .= rawurlencode($json['first_name']);
}
if(isset($json['last_name'])){
$name .= rawurlencode(" ".$json['last_name']);
}
$link = rawurlencode($json['link']);
$pid = $_SESSION['property_id'];
$property = urlencode(get_property_name($_SESSION['property_id']));
$location = urlencode(get_location($_SESSION['property_id']));
//send email to Shawn & Rom
$url = "https://www.mpyr.com/email?action=signup-alert&property=$property&location=$location&source=$source&traffic=$traffic&browser=$browser&platform=$platform&oid=$oid&uid=$uid&pid=$pid&name={$name}&link=$link";
get_api_results($url);
//send email to user
$email_encoded = rtrim(strtr(base64_encode($json['email']), '+/', '-_'), '=');
$url = "https://www.mpyr.com/email?action=welcome-user&name=$name&to_email=$email_encoded&property=$property&location=$location&source=$source";
get_api_results($url);
if($is_echo){
echo json_encode(array( // show as successful
"success" => true,
"social_url" => $social_url,
"email" => $json['email'],
"uid" => $uid,
"name" => urldecode($name),
"num_linked" => get_number_linked_accounts($uid),
"share_buttons" => get_share_buttons($social_url),
"share_text" => get_share_text($social_url,$uid),
"message" => ($success_msg == "") ? "Thank you for signing up. Your claim has been received." : $success_msg
));
}
}else{
//can't save property
//error will display
if($is_echo){
$social_url = "https://mpyr.com/?ref=$uid";
echo json_encode(array(
"success" => false,
"social_url" => $social_url,
"uid" => $uid,
"email" => $json['email'],
"name" => get_user_name($uid),
"share_buttons" => get_share_buttons($social_url),
"share_text" => get_share_text($social_url,$uid),
"message" => "It looks like you have already placed a claim (FB)."
));
}
}
}else{
$condition = "uid='$uid'";
$fvs['updated_dt'] = "now()";
update_db(0,"user",$fvs,$condition);
if($is_echo){
$social_url = "https://mpyr.com/?ref=$uid";
echo json_encode(array(
"success" => false,
"social_url" => $social_url,
"uid" => $uid,
"email" => $json['email'],
"name" => get_user_name($uid),
"share_buttons" => get_share_buttons($social_url),
"share_text" => get_share_text($social_url,$uid),
"message" => "It looks like you are already registered (FB)."
));
}
}
}
function get_fb_image($fb_id){
$url = "http://graph.facebook.com/$fb_id/?fields=picture&type=large";
$data = @file_get_contents($url);
$json = json_decode($data, true);
if($json){
return $json["picture"]["data"]["url"];
}else{
return "";
}
}
function save_gp_user($access_token, $json, $success_msg = "", $return_fvs = false, $is_echo=true){
$source = "googleplus";
$fvs = array();
$fvs['gp_token'] = $access_token;
$fvs['gp_id'] = $json['id'];
//$fvs['gp_email'] = $json['email'];
$fvs['gp_email'] = $json['emails'][0]['value'];
$fvs['gp_verified_email'] = $json['gp_verified_email'];
$fvs['etag'] = $json['etag'];
$fvs['gender']= $json['gender'];
$fvs['objectType'] = $json['objectType'];
//$fvs['gp_name'] = $json['name']['givenName']." ".$json['name']['familyName'];
$fvs['gp_name'] = $json['displayName'];
$fvs['first_name'] = isset($json['given_name']) ? $json['given_name'] : $json['name']['givenName'];
$fvs['last_name'] = isset($json['family_name']) ? $json['family_name'] : $json['name']['familyName'];
$fvs['tagline'] = $json['tagline'];
$fvs['braggingRights'] = $json['braggingRights'];
$fvs['aboutMe'] = $json['aboutMe'];
$fvs['gp_link']= $json['url'];
$fvs['gp_profile_image'] = isset($json['image']['url']) ? $json['image']['url'] : $json['picture'];
$fvs['plus_user'] = $json['isPlusUser'] * 1;
$fvs['locale'] = $json['locale'];
//echo "";print_r($json);die('aaaaaa');
if($_SESSION['traffic'] == null || $_SESSION['traffic'] == ""){
$fvs['traffic'] = $traffic = "direct";
}else{
$fvs['traffic'] = $traffic = $_SESSION['traffic'];
}
$fvs['browser'] = $browser = getBrowser();
$fvs['platform'] = $platform = getPlatform();
$fvs['ref'] = isset($_GET['ref']) ? $_GET['ref'] : $_SESSION['ref'];
$email = $json['emails'][0]['value']; //may change!
$uid = get_unique_user($json['id'],$email,$source);
//die("uid : $uid");
if($uid < 1) {
$fvs['signup_dt'] = "now()";
$fvs['signup_ip'] = $_SERVER["REMOTE_ADDR"];
$fvs['acct_type'] = "beta tester";
$fvs['notes'] = "pre-launch signup";
if($return_fvs){
return $fvs;
}else{
$fvs['source'] = $source; // don't overwrite source
$uid = insert_db(0,"user",$fvs); // save user to database
save_user_pic($uid,$fvs['gp_profile_image']);
$social_url = "https://mpyr.com/?ref=".$uid;
}
}
if($uid > 0){
$_SESSION['uid'] = $uid;
}
if($uid > 0 && has_approved_or_pending_properties($uid) == false){ //user is registered, but doesn't have approved or on hold properties so choose
$oid = save_ownership($uid, false); // take care of outputting
if($oid){
$name = "";
if(isset($json['first_name'])){
$name .= rawurlencode($json['first_name']);
}
if(isset($json['last_name'])){
$name .= rawurlencode(" ".$json['last_name']);
}
if(trim($name) == ""){
$name = $json['displayName'];
}
$link = urlencode($fvs['gp_link']);
$pid = $_SESSION['property_id'];
$property = urlencode(get_property_name($_SESSION['property_id']));
$location = urlencode(get_location($_SESSION['property_id']));
//send email to Shawn & Rom
$url = "https://www.mpyr.com/email?action=signup-alert&property=$property&location=$location&source=$source&traffic=$traffic&browser=$browser&platform=$platform&oid=$oid&uid=$uid&pid=$pid&name={$name}&link=$link";
get_api_results($url);
//send email to user
$url = "https://www.mpyr.com/email?action=welcome-user&name=$name&to_email=$email&property=$property&location=$location&source=$source";
get_api_results($url);
//die($url);
if($is_echo){
echo json_encode(array( // show as successful
"success" => true,
"social_url" => $social_url,
"email" => $email,
"uid" => $uid,
"name" => urldecode($name),
"num_linked" => get_number_linked_accounts($uid),
"share_buttons" => get_share_buttons($social_url),
"share_text" => get_share_text($social_url,$uid),
"message" => ($success_msg == "") ? "Thank you for signing up. Your claim has been received." : $success_msg
));
}
}else{
//can't save property
//error will display
if($is_echo){
$social_url = "https://mpyr.com/?ref=$uid";
echo json_encode(array(
"success" => false,
"social_url" => $social_url,
"uid" => $uid,
"email" => $json['email'],
"name" => get_user_name($uid),
"share_buttons" => get_share_buttons($social_url),
"share_text" => get_share_text($social_url,$uid),
"message" => "It looks like you have already placed a claim (G)."
));
}
}
}else {
$condition = "uid='$uid'";
$fvs['updated_dt'] = "now()";
update_db(0,"user",$fvs,$condition);
if($is_echo){
$social_url = "https://mpyr.com/?ref=$uid";
echo json_encode(array(
"success" => false,
"social_url" => $social_url,
"uid" => $uid,
"email" => $json['email'],
"name" => get_user_name($uid),
"share_buttons" => get_share_buttons($social_url),
"share_text" => get_share_text($social_url,$uid),
"message" => "It looks like you are already registered (G)."
));
}
}
}
function save_tw_user($json, $success_msg = "", $return_fvs = false, $is_echo=false){ // do not echo by default
$source = "twitter";
$fvs = array();
if($_SESSION['traffic'] == null || $_SESSION['traffic'] == ""){
$fvs['traffic'] = $traffic = "direct";
}else{
$fvs['traffic'] = $traffic = $_SESSION['traffic'];
}
$fvs['browser'] = $browser = getBrowser();
$fvs['platform'] = $platform = getPlatform();
$fvs['ref'] = isset($_GET['ref']) ? $_GET['ref'] : $_SESSION['ref'];
$uid = get_unique_user($json['id'],'',$source);
//echo("first uid.$uid");
//die('');
if($uid < 1){ //user doesn't exist and has no approved property claim
$fvs['acct_type'] = "beta tester";
$fvs['notes'] = "pre-launch signup";
$fvs['signup_dt'] = "now()";
$fvs['signup_ip'] = $_SERVER["REMOTE_ADDR"];
$fvs['source'] = $source;
$fvs['tw_id'] = $json['id'];
$fvs['tw_handle'] = $json['screen_name'];
$fvs['tw_name'] = $json['name'];
$fvs['tw_profile_image'] = $json['profile_image_url'];
$fvs['followers_count'] = $json['followers_count'];
$fvs['statuses_count'] = $json['statuses_count'];
$fvs['time_zone'] = $json['time_zone'];
$unix = strtotime($json['created_at']);
$fvs['tw_created_dt'] = date("Y-m-d",$unix);
$fvs['tw_link'] = $json['url'];
$fvs['tw_friends'] = $json['friends_count'];
$fvs['tw_location'] = $json['location'];
$fvs['tw_description'] = $json['description'];
if($return_fvs){
return $fvs;
}else{
$uid = insert_db(0,"user",$fvs); // save user to database
save_user_pic($uid,$fvs['tw_profile_image']);
$social_url = "https://mpyr.com/?ref=".$uid;
}
/** Can't subscribe to KOL since there's no email **/
}
if($uid > 0){
$_SESSION['uid'] = $uid;
$email = get_user_email($uid);
if($email != null && $email != ""){
$_SESSION['email'] = get_user_email($uid);
}
}
if($uid > 0 && has_approved_or_pending_properties($uid) === false){ //user is registered, but doesn't have approved or on hold properties so can choose
$oid = save_ownership($uid, false);//take care of outputting
if($oid){
$name = "";
if(isset($json['screen_name'])){
$name .= rawurlencode($json['screen_name']);
}
$link = rawurlencode($json['url']);
$property = urlencode(get_property_name($_SESSION['property_id']));
$location = urlencode(get_location($_SESSION['property_id']));
//send email to Shawn & Rom
$url = "https://www.mpyr.com/email?action=signup-alert&property={$property}&location={$location}&source=$source&traffic=$traffic&browser=$browser&platform=$platform&oid=$oid&uid=$uid&pid=$pid&name={$name}&link=$link";
get_api_results($url);
//tweet to user
$_SESSION['tw_handle'] = $json['screen_name'];
if(!is_banned($json['screen_name'])){ // check if name of property is offensive, don't tweet
if(stripos($json['screen_name'], "froghog") === false){ //not a test account
if($email == null || $email == "" ){
$tweet = urlencode("@".$json['screen_name']." We received your claim request and will be reviewing it shortly ... #MPYR");
}else{
$social_url = subscribe($email, $source);
$tweet = urlencode("@".$json['screen_name']." We received your claim request and will be reviewing it shortly ... Share MPYR for earlier access: ".$social_url);
}
$url = "https://www.mpyr.com/tweet?name={$_SESSION['tw_handle']}&property=$property&location=$location&tweet=$tweet";
get_api_results($url);
}
}
if($is_echo){
echo json_encode(array( // show as successful
"success" => true,
"social_url" => $social_url,
"uid" => $uid,
"email" => $_SESSION['mpyr_email'],
"name" => urldecode($name),
"num_linked" => get_number_linked_accounts($uid),
"share_buttons" => get_share_buttons($social_url),
"share_text" => get_share_text($social_url,$uid),
"message" => ($success_msg == "") ? "Thank you for signing up. Your claim has been received." : $success_msg
));
}else{
header("location: https://www.mpyr.com/index.php?source=twitter");
}
}else{
if($is_echo){
echo json_encode(array(
"success" => false,
"social_url" => "https://mpyr.com/?ref=$uid",
"uid" => $uid,
"email" => $_SESSION['mpyr_email'],
"name" => get_user_name($uid),
"share_buttons" => get_share_buttons($social_url),
"share_text" => get_share_text($social_url,$uid),
"message" => "It looks like you have already placed a claim (TW)."
));
}else{
header("location: https://www.mpyr.com/index.php?source=twitter&error=hasClaimed&uid=$uid");
}
}
}else{
if($is_echo){
$social_url = "https://mpyr.com/?ref=$uid";
echo json_encode(array(
"success" => false,
"social_url" => $social_url,
"uid" => $uid,
"email" => $_SESSION['mpyr_email'],
"name" => get_user_name($uid),
"share_buttons" => get_share_buttons($social_url),
"share_text" => get_share_text($social_url,$uid),
"message" => "It looks like you are already registered (TW)."
));
}else{
//TO-DO: fix this: property and location not passed to this function
$tweet = "@".$json['screen_name'].", you are already registered and have approved/pending claims.";
$url = "https://www.mpyr.com/tweet?name={$_SESSION['tw_handle']}&property=$property&location=$location&tweet=$tweet";
get_api_results($url);
header("location: https://www.mpyr.com/index.php?source=twitter&error=isRegistered");
}
}
}
?>
/** Property-related functions */
function get_pid($property,$lat='',$lng=''){
$condition = "name='$property'";
if($lat != null || $lat != '' || $lat != 0){
$condition .= " && lat='$lat' ";
}
if($lng != null || $lng != '' || $lng != 0){
$condition .= " && lng='$lng' ";
}
$row = select_db(0,"property","pid",1,$condition);
if($row == null){
return -1;
}else{
return $row['pid'];
}
}
function get_property_id($oid){
$condition = "oid='$oid'";
$row = select_db(0,"ownership","pid",1,$condition);
if($row == null){
return -1;
}else{
return $row['pid'];
}
}
function get_property_owner($pid){
$condition = "pid='$pid'";
$group_by = "uid";
$order_by = " FIELD(status,'approved','on hold','rejected') ";
$row = select_db(0,"ownership","uid,status",1,$condition,"1=1",$group_by,$order_by);
if($row == null){
return -1;
}else{
if($row['status'] == "approved"){
return $row['uid'];
}else{
return 0;
}
}
}
function get_rejected_users($pid){
$list = array();
$condition = "pid='$pid' && status='rejected'";
$group_by = "uid";
$rows = select_db(0,"ownership","uid",999,$condition,"1=1",$group_by);
if($rows == null){
return null;
}else{
foreach($rows as $row){
$list[] = $row['uid'];
}
return $list;
}
}
function get_pending_users($pid){
$list = array();
$condition = "pid='$pid' && (status='on hold' || status='pending')";
$group_by = "uid";
$rows = select_db(0,"ownership","uid",999,$condition,"1=1",$group_by);
if($rows == null){
return null;
}else{
foreach($rows as $row){
$list[] = $row['uid'];
}
return $list;
}
}
function get_property_name($pid){
$condition = "pid='$pid'";
$row = select_db(0,"property","name",1,$condition);
return $row['name'];
}
function get_location($pid){
$condition = "pid='$pid'";
$row = select_db(0,"property","city,state",1,$condition);
return $row['city'].", ".$row['state'];
}
function get_property_data($reference){
$key = "AIzaSyDFSUxqmmKZ6xWgJJ2UrevIHFpZIIiDSlA";
$url = "https://maps.googleapis.com/maps/api/place/details/json?reference=".$reference."&key=".$key;
//echo $url."\n";
$data = @file_get_contents($url);
$json = json_decode($data, true);
return $json;
}
function parse_property($json, $reference, $save = true){
$fvs = array();
$fvs['name'] = $json['result']['name'];
$fvs['address'] = $json['result']['formatted_address'];
// fix for address components which don't have street numbers
$stop = false;
for($i=0; $i<6 && !$stop; $i++){
if($json['result']['address_components'][$i]['types'][0] == "locality" ||
$json['result']['address_components'][$i]['types'][0] == "sublocality"){
$city = $json['result']['address_components'][$i]['long_name'];
if($json['result']['address_components'][$i+1]['types'][0] == "locality"){
$city = $json['result']['address_components'][$i+1]['long_name'];
}
if($city == null || $city == ""){
break;
}
$fvs['city'] = $city;
for($j=$i+1; $j<7; $j++){
if($json['result']['address_components'][$j]['types'][0] == "administrative_area_level_1"){
$fvs['state'] = $json['result']['address_components'][$j]['long_name'];
$stop = true;
break;
}
}
}
}
if($fvs['city'] == null || $fvs['city'] == "" || $fvs['state'] == null || $fvs['state'] == ""){
$geocode = reverse_geocode($fvs['address']);
$fvs['city'] = $geocode['city'];
$fvs['state'] = $geocode['state'];
}
$fvs['lat'] = $json['result']['geometry']['location']['lat'];
$fvs['lng'] = $json['result']['geometry']['location']['lng'];
$fvs['zip'] = get_property_zip($fvs['lat'],$fvs['lng']);
$fvs['website'] = $json['result']['website'];
$fvs['gp_url'] = $json['result']['url'];
$fvs['reference'] = $reference;
$fvs['notes'] = "on hold";
$fvs['added_by_ip'] = $_SERVER["REMOTE_ADDR"];
$fvs['added_dt'] = "now()";
$fvs['added_dt'] = "now()";
$fvs['hold_sessionid'] = session_id();
$expires = date('Y-m-d H:i:s', time()+900);
$fvs['hold_expires'] = "$expires"; //lock property 15 minutes so no one else can add it
if($save){
$sql = insert_db(0,"property",$fvs); // save property to database
if($sql < 1){
echo "ERROR INSERTING PROPERTY ";
die('');
}
$_SESSION['property_id'] = $sql;
save_view($sql,"","promo site","autocomplete hold");
foreach($json['result']['types'] as $type){
$fvs = array();
$fvs['property_id'] = $sql;
$fvs['type_name'] = $type;
$sql2 = insert_db(0,"property_types",$fvs); // save property type to database
}
echo "SAVED property to db . $sql";
//print_r($fvs);
}
return $fvs;
}
// Get STATE from Google GeoData
function reverse_geocode($address) {
$address = str_replace(" ", "+", "$address");
$url = "https://maps.google.com/maps/api/geocode/json?address=$address&sensor=false";
$result = file_get_contents("$url");
$json = json_decode($result);
foreach ($json->results as $result)
{
foreach($result->address_components as $addressPart) {
if((in_array('locality', $addressPart->types)) && (in_array('political', $addressPart->types))){
$city = $addressPart->long_name;
}else if((in_array('administrative_area_level_1', $addressPart->types)) && (in_array('political', $addressPart->types))){
//$state = $addressPart->long_name;
$state = $addressPart->short_name;
}else if((in_array('country', $addressPart->types)) && (in_array('political', $addressPart->types))){
$country = $addressPart->long_name;
}
}
}
/*
if(($city != '') && ($state != '') && ($country != ''))
$address = $city.', '.$state.', '.$country;
else if(($city != '') && ($state != ''))
$address = $city.', '.$state;
else if(($state != '') && ($country != ''))
$address = $state.', '.$country;
else if($country != '')
$address = $country;
// return $address;
*/
$geocode = array();
$geocode['city'] = $city;
$geocode['state'] = $state;
return $geocode;
}
function get_property_zip($lat,$lng){
$url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=".$lat.",".$lng;
//echo $url."\n";
$data = @file_get_contents($url);
$json = json_decode($data, true);
//print_array($json);
if(isset($json['results'])){
for($i=0; $i<12; $i++){
if($json['results'][0]['address_components'][$i]['types'][0] == "postal_code"){
$zip = $json['results'][0]['address_components'][$i]['long_name'];
break;
}
}
return $zip;
}else{
return "";
}
}
function get_property_views($pid){
$condition = "pid='$pid'";
$count = select_db(0,"property_views","COUNT(vid)as ct",1,$condition);
return $count['ct'];
}
/** Check if property is on hold for another user */
function is_on_hold($pid){
$condition = "pid='$pid' && hold_expires!='0000-00-00'";
$row = select_db(0,"property","added_by_ip, hold_sessionid, hold_expires",1,$condition);
if($row == null){
return false;
}else{
if($_SERVER["REMOTE_ADDR"] == $row['added_by_ip']){ // user who placed this on hold is same user claiming it
extend_hold($pid);
return false;
}else{
//hold found but see if it has expired
$now = date('Y-m-d H:i:s');
$expires = $row['hold_expires'];
if($expires < $now){ // expiry before current time
remove_hold($pid);
return false;
}else{
return true;
}
}
}
}
function fill_gp_url(){
$condition = "gp_url = ''";
$join = "1=1";
$group_by = "";
$order_by = "pid DESC";
$rows = select_db(0,"property","pid,reference",999,$condition,$join,$group_by,$order_by);
foreach($rows as $row){
$reference = $row['reference'];
$json = get_property_data($reference);
if($json == null){
continue;
}
$fvs = array();
$fvs['gp_url'] = $json['result']['url'];
$condition = "pid=".$row['pid'];
update_db(1,"property",$fvs,$condition);
}
}
function save_view($pid = null,$uid = "",$page="",$action=""){
$fvs = array();
if(!isset($pid)){
$pid = $_SESSION['property_id'];
}
$fvs['pid'] = $pid;
$fvs['uid'] = $uid; // user id
$fvs['dt'] = "now()";
$fvs['ip'] = $_SERVER["REMOTE_ADDR"];
$fvs['browser'] = getBrowser();
$fvs['platform'] = getPlatform();
$fvs['page'] = $page;
$fvs['action'] = $action;
$vid = insert_db(0,"property_views",$fvs); // update property to show user
return $vid;
}
function extend_hold($pid){
$fvs = array();
$expires = date('Y-m-d H:i:s', time()+900);
$fvs['hold_expires'] = "$expires"; //lock property 15 minutes so no one else can add it
$condition = " pid='$pid' ";
update_db(0,"property",$fvs,$condition);
}
function remove_hold($pid){
$fvs = array();
$fvs['hold_sessionid'] = "";
$fvs['hold_expires'] = "0000-00-00";
$condition = " pid='$pid' ";
update_db(0,"property",$fvs,$condition);
}
?>
/** Website */
function get_api_results($url, $post = null, $headers = null){
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 4);
if(isset($post)){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
if($headers){
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
}
$data = curl_exec($ch);
if(!$data) {
//echo curl_error($ch);
}
curl_close($ch);
return $data;
}
function is_404($url) {
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($handle);
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
curl_close($handle);
if ($httpCode >= 200 && $httpCode < 300) {
return false;
} else {
return true;
}
}
function get_full_url(){
$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
$sp = strtolower($_SERVER["SERVER_PROTOCOL"]);
$protocol = substr($sp, 0, strpos($sp, "/")) . $s;
$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
return $protocol . "://" . $_SERVER['SERVER_NAME'] . $port . $_SERVER['REQUEST_URI'];
}
/** Auxiliary Functions */
function isMobile($className = ""){
global $detect;
$iPod = stripos($_SERVER['HTTP_USER_AGENT'],"iPod");
$iPhone = stripos($_SERVER['HTTP_USER_AGENT'],"iPhone");
$iPad = stripos($_SERVER['HTTP_USER_AGENT'],"iPad");
//$WebKit = stripos($_SERVER['HTTP_USER_AGENT'],"WebKit");
$Android = stripos($_SERVER['HTTP_USER_AGENT'],"Android");
$webOS = stripos($_SERVER['HTTP_USER_AGENT'],"webOS");
$isMobile = ($detect->isMobile() || $iPod || $iPhone || $iPad || $WebKit);
//echo $_SERVER['HTTP_USER_AGENT'];
if($className == ""){
return $isMobile;
}else{
if($isMobile){
echo "xxx";
}else{
echo $className;
}
}
}
function is_banned($check_word){
$check_word = strtolower($check_word);
include "_banned_words.php";
foreach($banned_words as $word){
$pos = stripos($check_word,$word);
if($pos !== false){
//echo "xx $pos, $check_word - $word
\n";
return true;
}
}
return false;
}
function get_page_rank($url){
$rootDomain = getRootDomain($url);
$url = "https://www.mpyr.com/non-mvc/_getPR.php?url=http://".$rootDomain;
$api = get_api_results($url);
if(strpos($api,"Failed") ) {
return "N/A";
}else{
return $api;
}
}
function getRootDomain($domain){
$pieces = parse_url($domain);
//print_r($pieces);
$domain = isset($pieces['host']) ? $pieces['host'] : '';
if (preg_match('/(?P[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
return $regs['domain'];
}else{
return false;
}
}
function getPlatform(){
$u_agent = $_SERVER['HTTP_USER_AGENT'];
$platform = 'Unknown';
if( strpos($_SERVER['HTTP_USER_AGENT'],"iPhone") ) {
$platform = 'iphone';
}elseif( strpos($_SERVER['HTTP_USER_AGENT'],"iPad") ) {
$platform = 'ipad';
}elseif( strpos($_SERVER['HTTP_USER_AGENT'],"Android") ) {
$platform = 'android';
}elseif (preg_match('/linux/i', $u_agent)) {
$platform = 'linux';
}elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
$platform = 'mac';
}elseif (preg_match('/windows|win32/i', $u_agent)) {
$platform = 'windows';
}
return $platform;
}
function getBrowser(){
$u_agent = $_SERVER['HTTP_USER_AGENT'];
$ub = "Unknown";
if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent)) {
$ub = "MSIE";
}
elseif(preg_match('/Firefox/i',$u_agent)) {
$ub = "Firefox";
}
elseif(preg_match('/Chrome/i',$u_agent)) {
$ub = "Chrome";
}
elseif(preg_match('/Safari/i',$u_agent)) {
$ub = "Safari";
}
elseif(preg_match('/Opera/i',$u_agent)) {
$ub = "Opera";
}
elseif(preg_match('/Netscape/i',$u_agent)) {
$ub = "Netscape";
}
return $ub;
}
?>
session_start();
if ( !isset( $_SESSION["origURL"] ) ){
$_SESSION["origURL"] = $_SERVER["HTTP_REFERER"];
}
if(isset($_REQUEST['logout'])){
session_destroy();
session_start();
}
$uri = $_SERVER['REQUEST_URI']; // or taken from another source //
$uid = $_SESSION['uid'];
$_SESSION['mpyr_email'] = str_replace("googlemail","gmail",$_SESSION['mpyr_email']);
if( strpos($uri, '/cr') !== false || isset($_REQUEST['cr']) ){
$_SESSION['cr'] = 1;
$_SESSION['traffic'] = "craigslist";
}else if( strpos($uri, '/ig') !== false || isset($_REQUEST['ig']) ){
$_SESSION['ig'] = 1;
$_SESSION['traffic'] = "instagram";
}else if( strpos($uri, '/tm') !== false || isset($_REQUEST['tm']) ){
$_SESSION['tm'] = 1;
$_SESSION['traffic'] = "tumblr";
}else if( strpos($uri, '/tw') !== false || isset($_REQUEST['tw']) ){
$_SESSION['tw'] = 1;
$_SESSION['traffic'] = "twitter";
}else if( strpos($uri, '/fb') !== false || isset($_REQUEST['fb']) ){
$_SESSION['fb'] = 1;
$_SESSION['traffic'] = "facebook";
}else if( strpos($uri, '?ref=ad') !== false || isset($_REQUEST['ad']) ){
$_SESSION['ad'] = 1;
$_SESSION['traffic'] = "adwords";
}else if( strpos($uri, '/kid') !== false || isset($_REQUEST['kid']) ){
$_SESSION['kid'] = $_REQUEST['kid'];
$_SESSION['traffic'] = "kickofflabs";
}else if( strpos($uri, '/t') !== false || isset($_REQUEST['t']) ){
$_SESSION['t'] = 1;
$_SESSION['traffic'] = "@shawnbishop";
}else{
if(isset($_SESSION['traffic'])){
// don't override traffic
}else{
$_SESSION['traffic'] = "direct";
}
}
//echo $_SESSION['cr']."=".$_SESSION['ig'];
?>
MPYR - Take Over the World
global $detect;
if($detect->isTablet()){ ?>
}else if($detect->isMobile()){ ?>
}else{ ?>
} ?>