$v) { if (!in_array($k, $supported_contact_data)) { return null; } } $contact_xml = ''."\n"; $contact_xml .= ''; foreach ($contact_data as $k => $v) { $contact_xml .= "<$k>".htmlentities($v, ENT_QUOTES).""; } $contact_xml .= ''."\n"; $response = ic_do_put("contact", $key, $secret, $token, $contact_xml); if (IC_DEBUG == true) { echo "add contact response: \n".var_export($response, true); } if (empty($response) || empty($response['_attributes']) || empty($response['_attributes']['status']) || $response['_attributes']['status'] != 'success') { return null; } if (empty($response['result']) || empty($response['result']['contact']) || empty($response['result']['contact']['_attributes']) || empty($response['result']['contact']['_attributes']['id'])) { return null; } return $response['result']['contact']['_attributes']['id']; } # ]]] function ic_subscribe($key, $secret, $login, $password, $token, $contact_id, $list_id) # [[[ { if (empty($token) || empty($contact_id) || empty($list_id)) { return null; } $subscription_xml = ''."\n"; $subscription_xml .= ''; $subscription_xml .= 'subscribed'; $subscription_xml .= ''."\n"; $response = ic_do_put("contact/$contact_id/subscription/$list_id", $key, $secret, $token, $subscription_xml); if (IC_DEBUG == true) { echo "add subscription response: \n".var_export($response, true); } if (empty($response) || empty($response['_attributes']) || empty($response['_attributes']['status']) || $response['_attributes']['status'] != 'success') { return null; } return true; } # ]]] ###################################################################################################### function ic_authorize($key, $secret, $login, $password) # [[[ { global $intellicontact_seq; if (empty($key) || empty($secret) || empty($login) || empty($password)) { return null; } $hash = md5($password); $path = "auth/login/{$login}/$hash"; $response = ic_do_get($path, $key, $secret); if (!is_array($response) || !array_key_exists("auth", $response)) { return null; } if (!empty($response['_attributes']['status']) && $response['_attributes']['status'] == 'failure') { return null; } $intellicontact_seq = $response['auth']['seq']; return $response['auth']['token']; } # ]]] function ic_compute_signature($path, $key, $secret, $token = null, $putdata = null) # [[[ { global $intellicontact_seq; $signature = $secret . $path; $signature .= "api_key" . $key; if (!empty($token)) { if (!empty($putdata)) { $signature .= "api_put" . $putdata; } $signature .= "api_seq".$intellicontact_seq; $signature .= "api_tok" . $token; } if (IC_DEBUG == true) { echo "
".__FUNCTION__."; signature: $signature
"; } $signature = md5($signature); if (IC_DEBUG == true) { echo "computed signature: " . $signature . "
"; } return $signature; } # ]]] function ic_construct_url($path, $key, $secret, $token, $putdata) # [[[ { global $intellicontact_seq; $path = ltrim($path,"/"); $api_seq = ""; if (!empty($token)) { $api_seq = "&api_seq=".$intellicontact_seq; } $url = IC_BASE_URL."/$path/?api_key={$key}{$api_seq}&api_sig=".ic_compute_signature($path, $key, $secret, $token, $putdata); if (!empty($token)) { $url .= "&api_tok=$token"; } return $url; } #]]] function ic_do_get($path, $key, $secret, $token = null, $putdata = null) # [[[ { global $intellicontact_seq; $url = ic_construct_url($path, $key, $secret, $token, $putdata); $xml = ic_get_url($url); if (empty($xml)) { return null; } $response_data = ic_xml2hash($xml); $intellicontact_seq++; return $response_data; } # ]]] function ic_do_put($path, $key, $secret, $token = null, $putdata = null) # [[[ { global $intellicontact_seq; $url = ic_construct_url($path, $key, $secret, $token, $putdata); $xml = ic_put_url($url, $putdata); if (empty($xml)) { return null; } $response_data = ic_xml2hash($xml); $intellicontact_seq++; return $response_data; } # ]]] function ic_get_url($url) # [[[ { $host_info = parse_url($url); list($a, $result) = func_http_get_request($host_info['host'], $host_info['path'], $host_info['query']); if (IC_DEBUG == true) { echo "
";
		echo "GET $url\n";
		echo "RESPONSE\n".htmlspecialchars($result)."\n";
		echo "
"; } return $result; } # ]]] function ic_put_url($url, $data) # [[[ { $host_info = parse_url($url); list($a, $result) = func_http_put_request($host_info['host'], $host_info['path'], $host_info['query'], $data); if (IC_DEBUG == true) { echo "
";
		echo "PUT $url\n";
		echo "DATA\n".htmlspecialchars($data)."\n";
		echo "RESPONSE\n".htmlspecialchars($result)."\n";
		echo "
"; } return $result; } #]]] function ic_xml2hash($data, $custom_attributes = null) # [[[ { $attributes = array("encoding" => "utf-8", "complexType" => "array", "parseAttributes" => true, "attributesArray" => "_attributes", "forceEnum" => array("response")); if (!empty($custom_attributes) && is_array($custom_attributes)) { $attributes = func_array_merge($attributes, $custom_attributes); } $xml_uns_handler = new XML_Unserializer($attributes); if (!is_object($xml_uns_handler)) { #die("[ERROR] Failed to create unserializer instance: " . __FILE__ ."; lineno: " . __LINE__); return null; } if (!$xml_uns_handler->unserialize($data, false)) { #die("[ERROR] Failed to unserialize data: " . __FILE__ ."; lineno: " . __LINE__); $result = null; } else { $result = $xml_uns_handler->getUnserializedData(); } if (IC_DEBUG == true) { echo "
".__FUNCTION__ . ":\n
".var_export($result, true)."
\n"; } return $result; } # ]]] function func_http_put_request($host, $post_url, $post_str, $post_data, $post_cookies=array()) # [[[ { $hp = explode(':',$host); $cookie = ""; $result = ""; $header_passed = false; if( !isset($hp[1]) || !is_numeric($hp[1]) ) $hp[1] = 80; $host = implode(':', $hp); $fp = fsockopen($hp[0], $hp[1], $errno, $errstr, IC_REQUEST_TIMEOUT); if (!$fp) { return array ("", ""); } else { fputs ($fp, "PUT $post_url?$post_str HTTP/1.0\r\n"); fputs ($fp, "Host: $host\r\n"); fputs ($fp, "User-Agent: Mozilla/4.5 [en]\r\n"); if (!empty($post_cookies)) fputs ($fp, "Cookie: ".join('; ',$post_cookies)."\r\n"); fputs($fp,"Content-Length: ".strlen($post_data)."\r\n"); fputs($fp,"\r\n"); fputs($fp, $post_data); $http_header = array (); $http_header["ERROR"] = chop(fgets($fp,4096)); $cookies = array (); while (!feof($fp)) { if (!$header_passed) $line = fgets($fp, 4096); else $result .= fread($fp, 65536); if ($header_passed == false && ($line == "\n" || $line == "\r\n")) { $header_passed = true; continue; } if ($header_passed == false) { $header_line = explode(": ", $line, 2); $header_line[0] = strtoupper($header_line[0]); $http_header[$header_line[0]] = chop($header_line[1]); if ($header_line[0] == 'SET-COOKIE') array_push($cookies, chop($header_line[1])); } } fclose($fp); } func_parse_cookie_array($http_header, $cookies); return array($http_header, $result); } # ]]] # vim: set fdm=marker fmr=[[[,]]] : ?>