"$querytype", 'queryclass' => "$queryclass", 'query' => "$query", 'fieldselect' => "$fieldselect", 'imagehandler' => "$imagehandler", "imageidkey" => "$idkey", "imagelocation" => "$imagelocation"); $search = $rets->SearchQuery($querytype, $queryclass, $query, array( 'Format' => 'COMPACT-DECODED', 'Count' => 1, 'Select' => $fieldselect)); $local_images = array(); if($rets->Error()) { $error = $rets -> Error(); $queryinfo[error] = $error; //rets server had no records switch ($error[code]) { //rets server had no records case '20201': break; default: dlog(functions_rets, 'Image Query result: (failed): '.print_r($queryinfo)); return false; } } else { while ($record = $rets->FetchRow($search)) { d("Target Property", $record); if (!file_exists($tmp_imagedir)) mkdir_wrapper($tmp_imagedir); if (!file_exists($tmp_imagedir) || !is_writable($tmp_imagedir)) unset($tmp_imagedir); if ($tmp_imagedir) { extract($record); //only download the number of images allowed by the get_mlsvar('images_limit'); $imagenums = array(); $images_limit = get_mlsvar('images_limit'); if($images_limit) for($i=1; $i<$images_limit+1; $i++) $imagenums[]=$i; if(!$imagenums) $imagenums[]='*'; $imagenums = implode(',',$imagenums); $photos = $rets->GetObject($querytype, "Photo", $record[$idkey], $imagenums, $imagelocation); $image_counter=0; if (!$photos) continue; if (function_exists($imagehandler)) { foreach ($photos as $photodata) { //download the image and get the local path to the image if ($imagepath = $imagehandler($photodata)) { //arranging the data in a standard format foreach ($imagepath as $key => $keyset) foreach ($keyset as $key2 => $value) $local_images[$key][$key2] = $value; } } } else { foreach ($photos as $photo) { $listingid = $photo['Content-ID']; $number = $photo['Object-ID']; if (trim($listingid) != '' && trim($number) != '') { $tmpname = "image_{$listingid}_{$number}.jpg"; if ($photo['Success'] == true && ($fpcr = file_put_contents("{$tmp_imagedir}/{$tmpname}", $photo['Data'])) !== false) $images[$listingid][$number] = $tmpname; else d("Photo download failed: '{$fpcr}': '{$tmpname}': {$photo['ReplyCode']} = {$photo['ReplyText']}"); } else d("Photo download failed(2): '{$fpcr}': '{$tmpname}': {$photo['ReplyCode']} = {$photo['ReplyText']}"); } $local_images = $images; } } else developer_error("Failed to create temp directory for RETS image upload", "Directory provided for uploading to was '{$download_info[tmp_imagedir]}', either unable to find or unable to write to this directory on {$_SERVER[DOCUMENT_ROOT]}"); } } $queryinfo[images] = $local_images; d('Image Query result: (success):', $queryinfo); return $local_images; } $user = ''; $pass = ''; $myrets = ''; function rets_connect_mls($loginurl, $username, $password, $headerinfo='', $usepost=false) { require_once("class.rets.php"); //set a default header value if we don't have one already global $user, $pass, $myrets; //a lite check to prevent reconnection if we already have a connection for this user if ($user != $username || $pass != $password) { //disconnect and clear references if any rets_disconnect_mls(); if (is_developer()) { $connection[header] = $headerinfo; $connection[login] = array( 'url' => "$loginurl", 'username' => "$username", 'password' => "$password"); d("Connection Info:", $connection); } $tmp_rets = new phRETS; $tmp_rets->SetParam("use_post_method", $usepost); if (!is_array($headerinfo)) $headerinfo = array('User-Agent' => 'RIOGenesisRETS'); foreach ($headerinfo as $key => $value) $tmp_rets->AddHeader($key, $value); flush(); dlog("rets_connect_status", "Connecting to: URL: $loginurl $username $password"); $connect = $tmp_rets->Connect($loginurl, $username, $password, $headerinfo['User-Agent-Password']); if ($connect) { dlog("rets_connect_status", "Connected to: URL: $loginurl $username $password"); d(" + Connected", $tmp_rets); //only want to set the values //if we have a successful connection $user = $username; $pass = $password; $myrets = $tmp_rets; } else { dlog("rets_connect_status", "Not Connected to: URL: $loginurl $username $password"); d(" + Not connected", $tmp_rets); d(error, $tmp_rets->Error()); //if our connection failed reset rather than disconnect return false; } } return ($myrets)?$myrets:false; } function rets_disconnect_mls() { global $myrets, $user, $pass, $rets_lastquery; //disconnect from the mls if we haven't already if (isset($rets_lastquery)) unset($GLOBALS[rets_lastquery]); if ($myrets) { $myrets->Disconnect(); d(" - Disconnected"); } //make sure the global values are unset if (trim($user) > '' || trim($pass) > '') unset($GLOBALS[myrets], $GLOBALS[user], $GLOBALS[pass]); } function rets_getlastquery() { global $rets_lastquery; $querydat = ""; if (is_array($rets_lastquery)) foreach ($rets_lastquery as $qname => $query) { $querydat ." ,[$qname(qlen:".strlen($query).")=$query]"; } return $querydat; } function rets_query_mls($loginurl, $username, $password, $querytype, $queryclass, $query, $fieldselect, $notused_imagesdir = '', $idkey = '', $headerinfo = '') { require_once("class.rets.php"); global $rets_lastquery; timetrack_start(rets,$query); if (!($rets = rets_connect_mls($loginurl, $username, $password, $headerinfo))) { d(headerinfo,$headerinfo); developer_error(functions_rets, "Rets Connection failed: (".__line__.") ".print_r($rets->Error(), true)); return false; } //used for error reporting $rets_lastquery = array("querytype" => "$querytype", 'queryclass' => "$queryclass", 'query' => "$query", 'fieldselect' => "$fieldselect", "idkey" => "$idkey"); d("Query Info:", $rets_lastquery); d(" + {$querytype} Class: {$queryclass} Query: {$query} Field: ", $fieldselect); dlog("rets_connect_status", "Querying: URL: $loginurl $username $password"); $search = $rets->SearchQuery($querytype, $queryclass, $query, array( 'Format' => 'COMPACT-DECODED', 'Count' => 1, 'Select' => $fieldselect)); if($rets->Error()) { $error = $rets -> Error(); $error[query] = $query; switch ($error[code]) { case '20201': //didn't find any properties, but it's not a connection error return array(); default: developer_error(functions_rets, "Rets Query connect ERR-{$error[code]}: (".__line__.") ".print_r($error, true)); //may want to consider running a disconnect and reconnect at this point return false; } } $recordcount = $rets->NumRows(); dlog("rets_connect_status", "Queried: URL: $loginurl $username $password found ($recordcount) records"); d("Found $recordcount records"); if ($rets->NumRows() > 0) { //build the metadata table $fields = $rets->GetMetadataTable($querytype, $queryclass); d(fields, $fields); $cshortnames = array(); if ($fields) foreach($fields as $kf => $field) { $shortnames[$field[SystemName]]=$field[ShortName]; $cshortname = preg_replace('/[\'\/ ]+/', '', strtolower($field[ShortName]));; // in some circumstances we found that there were fields with duplicate or missing short names, so we had to use the less intuitive db nameb if (trim($cshortname) == '' || in_array($cshortname,$cshortnames)) { $cdbname = preg_replace('/[\'\/ ]+/', '', strtolower($field[DBName])); // in some circumstances we found that there were fields with no dbname, so we had to use the less intuitive system name if (trim($cdbname) == '') $cdbname = preg_replace('/[\'\/ ]+/', '', strtolower($field[SystemName])); $usename = $cdbname; } else { $usename = $cshortname; } $cshortnames[$field[SystemName]]=$usename; $longnames[$usename]=$field[LongName]; } d(shortnames, $shortnames); d(cshortnames, $cshortnames); d(longnames, $longnames); //build the records array $records = array(); while ($record = $rets->FetchRow($search)) { $recordnumber++; $this_record = array(); foreach ($cshortnames as $systemnameid => $shortname) $this_record[$shortname] = $record[$systemnameid]; ///image handling downloading coded moved to different file $records[] = $this_record; flush(); } } $rets->FreeResult($search); for ($i = 0; $i < 10; $i++) { $examples[] = $records[$i]; } $out[records] = $records; $out[metadata] = $longnames; //disconnect is handled elsewhere to reduce the number of connects and disconnect //d("Disconnecting"); //rets_disconnect_mls(); timetrack_stop(rets,$query); //d(out, $out); return $out; } function rets_striptolower() { $rval = ''; foreach (func_get_args() as $arg) $rval .= preg_replace('/[^a-z0-9]/', '', strtolower($arg)); return $rval; }