1){ $json .= '"params":[{'; foreach ($array[params] as $key=>$val){ $json .= $key.':"'.$val.'", '; } $json = substr($json,0,-2); $json .="}]}"; }else{ $json .= '"params":'.json_encode($array[params]); $json .= "}"; } return $json; } function m_submit_toapi($data, $referrer=""){ // TEST USER //{"id":"1","result":{"userID":627687,"error":{"continue":true,"info":[{"level":"Warning","class":"Diagnostics","name":"Warning","message":"Ronald Redding does not have any Sponsor's configured. Unable to send Sponsor Introduction."},{"level":"Info","class":"Diagnostics","name":"Info","message":"User successfully created!"}]}}} $host = "http://www.riogenesismarketing.com/"; // need to replace this with real url // Building referrer we dont use this. // if($referrer=="") // if not given use this script as referrer // $referrer=$_SERVER["REQUEST_URI"]; d(data,$data); d(req,$host.$data[url]); unset($data[loginid]); $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8', 'X-JSON-RPC: '.$data['method'])); curl_setopt($ch, CURLOPT_URL, $host.$data[url]); curl_setopt($ch, CURLINFO_HEADER_OUT, true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); // Set timeout to 10 sec unset($data[url]); $postcount = count($data); curl_setopt($ch, CURLOPT_HEADER, 0); // Don't return headers curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Return the result when curl_exec is called //curl_setopt($ch, CURLOPT_REFERER, $referrer ); // The referrer d(postcount,$postcount); unset($data[loginid]); //$data[id]="1"; //seq# if ($data['method'] == "login" || $data['method'] == "syncUser" || $data['method'] == 'deactivateUser'){ // cookie handler curl_setopt($ch, CURLOPT_COOKIEJAR, url_getcookiefile()); curl_setopt($ch, CURLOPT_COOKIEFILE, url_getcookiefile()); } curl_setopt($ch, CURLOPT_POST, $postcount); // We're doing a post call // FORMAT: '{"params":[{"displayName":"Ronald Redding","userName":"redrooster1_rio","password":"rggrpmrkew2","firstName":null,"lastName":null,"companyName":"Red Rooster Real Estate","industryID":"2","emailAddress":"","phoneNumber":"","mobileNumber":"","addressLine1":"1101 W Grove St.","addressLine2":null,"city":"Boise","state":"ID","zip":"83702","subscriptionID":"22"}],"method":"createUser","id":"1"}');// json_encode($data)); // Here's the post data //if ($data['method']=="syncUser") // curl_setopt($ch, CURLOPT_POSTFIELDS, '{"id":2,"method":"syncUser","params":{ "request" : null }}'); // else curl_setopt($ch, CURLOPT_POSTFIELDS, myjsonencode($data,1)); d(json,myjsonencode($data,1)); d(curl,$ch); $result = curl_exec ($ch); $result[httperror] = curl_getinfo($ch,CURLINFO_HTTP_CODE); $result[header] = curl_getinfo($ch); $result[curlerror] = curl_error($ch); $headerSent = curl_getinfo($ch, CURLINFO_HEADER_OUT ); curl_close ($ch); d(headers,$headerSent); d(result,$result); return "{".$result; } function url_getcookiefile() { //this function makes it so that all requests on the current page to this function, will return the same temporary filename global $url_cookiecache; if(!$url_cookiecache) $url_cookiecache = tempnam(sys_get_temp_dir(), 'url'); return $url_cookiecache ; } /** * @name makreting_request * @param [string] $request * @param [integer] $dta (loginid or array with [loginid]) * @return mixed depends on call either value, true or false. ********************************************************************** * calls: * adduser - add new user returns userid or false. * checkusername - check if username exists returns true or false. * syncuser - this call does login 1st then syncuser that is same as adduser * reactivateuser - returns true or false * deactivateuser - returns true or false * login - returns array *********************************************** */ function marketing_request($request,$dta){ if (!is_array($dta) && !empty($dta)) $data[loginid] = $dta; else{ $data = $dta; unset($dta); } d(data,$data); switch (strtolower($request)){ case "adduser": $fdata['method'] = "createUser"; $data[params] = m_verifty_data($request,$data); if (!$data) return false; break; case "checkusername": $fdata['method'] = "checkUserName"; $data[params] = m_verifty_data($request,$data); if (!$data) return false; break; case "syncuser": $fdata['method'] = "syncUser"; $data[params] = m_verifty_data($request,$data); if (!$data[params]) return false; break; case "reactivateuser": $fdata['method'] = 'reactivateUser'; if (!$data[loginid]) return FALSE; else{ $logininfo = db_query("select loginusername,loginpassword,logincreated from tbllogin where loginid=".db_number($data[loginid])); if (!$logininfo) return FALSE; $data[params][userName] = $logininfo[0][loginusername]."_rio"; } break; case "disableuser": $fdata['method'] = 'deactivateUser'; if (!$data[loginid]) return FALSE; else{ // for this we need to login 1st $token = marketing_request(login, $data[loginid]); if (!$token[token]){ unset($data); return FALSE; } // 2. deactivate User. $fdata['method'] = "deactivateUser"; $data[params][token]= $token[token]; $logininfo = db_query("select loginusername,loginpassword,logincreated from tbllogin where loginid=".db_number($data[loginid])); if (!$logininfo) return FALSE; $data[params][userName] = $logininfo[0][loginusername]."_rio"; } break; } if ($fdata['method']){ $fdata[url] = "app_services/account/service.ashx"; if ($data[params][token]){ $fdata[url] .= "?authtckt=".$data[params][token]; unset($data[params][token]); } $skip =1; } switch (strtolower($request)){ case "login": $fdata['method'] = "login"; d(login,$data); m_loginrequest($data); break; case "getguid": $fdata['method'] = "getWebGuid"; if (!$data[loginid]) return FALSE; else m_loginrequest($data); break; } if ($fdata['method'] && !$skip) $fdata[url] = "app_services/securityservice.ashx"; else if (!$fdata['method']) return "ERROR Unknown request!"; $data[url] = $fdata[url]; $data['method'] = $fdata['method']; $ret = m_submit_toapi($data); dlog("Marketing-API-",$fdata['method']."Call:" .myjsonencode($data)." date:".date("m/d/y G:i:s", time())."json: ".$ret); // Log $return = my_jsontoarray($ret); switch ($fdata['method']){ case "getWebGuid": $result = preg_replace('@"@',"",$return[1][2]); break; case "createUser": d(FN,$return); if (preg_replace('@"@',"",$return[1][1]) == "userID" && preg_replace('@"@',"",$return[3][1]) == "continue") $result = $return[1][2]; else $result = FALSE; break; case "checkUserName": $result = preg_replace('@"@',"",$return[1][2]); break; case "reactivateUser": $result = preg_replace('@"@',"",$return[1][3]); break; case "login": $ret = preg_replace("@{@","",$ret); $ret = preg_replace("@}@","",$ret); $ret = explode(",",$ret); foreach ($ret as $key=>$val) $result[reset(explode(":",preg_replace('@"@',"",$val)))] = end(explode(":",preg_replace('@"@',"",$val))); break; case "syncUser" : $result = preg_replace('@"@',"",$return[1][3]); break; case "deactivateUser": //d(ret,$return); $result = preg_replace('@"@',"",$return[1][3]); break; } return $result; } function m_loginrequest(&$data){ if (!$data[loginid]) return FALSE; else{ $logininfo = db_query("select loginusername,loginpassword,logincreated from tbllogin where loginid=".db_number($data[loginid])); if (!$logininfo) return FALSE; $data[params][userName] = $logininfo[0][loginusername]."_rio"; $data[params][password] = myencript($logininfo[0][loginusername],$logininfo[0][logincreated]); } } function m_verifty_data(&$request,&$data){ $request = strtolower($request); switch ($request){ case "adduser": m_user($data,$newarray); break; case "checkusername": $username = db_query("select loginusername from tbllogin where loginid=".db_number($data[loginid])); if (!$username) return FALSE; $newarray[userName] = $username[0][loginusername]."_rio"; break; case "syncuser": // For this we have to make 2 calls // 1. login $token = marketing_request(login, $data[loginid]); //d(token,$token); if (!$token[token]){ unset($data); return FALSE; } // 2. syncUser this part is still broken... $fdata['method'] = "syncUser"; m_user($data,$newarray); $newarray[token]= $token[token]; //d(data,$data); //d(newarr,$newarray); break; } return $newarray; } /** * @name myencript * @param [string] $text text to encript * @param [strting] $key key to use */ function myencript($text,$key){ $password = $text; $key = preg_replace("/ /", "", $key); for($i=0;$i$val){ $result[$key] = explode(":",$val); } return $result; } function m_user(&$data,&$newarray){ /* * displayName - string - user's full name * userName - string - a unique username, likely should match RIO name * password - string * firstName - string * lastName - string * companyName - string * industryID - int - indicates the type of professional * emailAddress - string * phoneNumber - string * mobileNumber - string * addressLine1 - string * addressLine2 - string * city - string * state - string * zip - string * subscriptionID - int - the identifier of their subscription type * as of 09/24/2012 we will use following * IndustryID: Constant we will use is 2, for future reference, where are the other options * 1 Mortgage * 2 Real Estate * 3 Title/Escrow * 4 Other * The subscriptionID will be 22. */ if (!$data[loginid]) return false; $usernamesql = "select loginid,loginusername, loginpassword, staffid, logincreated, tblcompany.*,tblstaff.*,tblofficelocation.* from tbllogin join (select companyid,companyname, companyaddress, companyaddress2, companycity, companystate, companyzip from tblcompany ) as tblcompany using (companyid) join (select staffid, stafffname, stafflname, staffemail,staffphone, staffcell, officelocationid from tblstaff ) as tblstaff using (staffid) left join tblofficelocation using (officelocationid) where loginid = ".db_number($data[loginid]); $userdata = db_query($usernamesql); d($usernamesql,$userdata); if (empty($userdata)){ $userdata = db_query("select * from tbllogin join (select companyname, companyfname as stafffname, companylname as stafflname, companyemail as staffemail, * from tblcompany) as tblcompany using (companyid) where loginid = ".db_number($data[loginid])); } if (is_array($userdata) && !empty($userdata)){ $newarray[displayName] = $userdata[0][stafffname]." ".$userdata[0][stafflname]; $newarray[userName] = $userdata[0][loginusername]."_rio"; $newarray[password] = myencript($userdata[0][loginusername],$userdata[0][logincreated]); $newarray[firstName] = $userdata[0][stafffname]; $newarray[lastName] = $userdata[0][stafflname]; $newarray[companyName] = $userdata[0][companyname]; $newarray[industryID] = 2; // hardcoded for now. $newarray[emailAddress] = $userdata[0][staffemail]; $newarray[phoneNumber] = $userdata[0][staffphone]; $newarray[mobileNumber] = $userdata[0][staffcell]; if ($userdata[0][officelocationid]>0 && $userdata[0][officelocationaddress]){ $newarray[addressLine1] = $userdata[0][officelocationaddress]; $newarray[addressLine2] = $userdata[0][officelocationaddress2]; $newarray[city] = $userdata[0][officelocationcity]; $newarray[state] = $userdata[0][officelocationstate]; $newarray[zip] = $userdata[0][officelocationzip]; }else{ $newarray[addressLine1] = $userdata[0][companyaddress]; $newarray[addressLine2] = $userdata[0][companyaddress2]; $newarray[city] = $userdata[0][companycity]; $newarray[state] = $userdata[0][companystate]; $newarray[zip] = $userdata[0][companyzip]; } $newarray[billingTypeID] = 5; // Per email from Brandon $newarray[subscriptionID] = 22; //Fixed for now. }else ddie("Marketing adduser SQL".__LINE__." developer error: loginid:".$data[loginid]); // this should not happen since we got loginid. }