public function testwrite_encoded_file() { //execute the method and test if it created file exists $cache_dir = 'vfs://root'; //without filename $tempArray = array('filename' => 'soap_array.txt', 'md5' => '523ef67de860fc54794f27117dba4fac', 'data' => 'some soap data'); $actual = write_encoded_file($tempArray, $cache_dir, ''); $this->assertFileExists($actual); unlink($actual); //with filename $tempArray = array('md5' => '523ef67de860fc54794f27117dba4fac', 'data' => 'some soap data'); $actual = write_encoded_file($tempArray, $cache_dir, 'soap_array.txt'); $this->assertFileExists($actual); unlink($actual); }
function disc_client_file_sync($soapclient, $session, $verbose = false, $attempts = 0) { $max_attempts = 3; global $sugar_config; // files might be big ini_set("memory_limit", "-1"); $return_str = ""; $tempdir_parent = create_cache_directory("disc_client"); $temp_dir = tempnam($tempdir_parent, "sug"); sugar_mkdir($temp_dir, 0775); if (!is_dir($temp_dir)) { die("Could not create a temp dir."); } // get pattern file $result = $soapclient->call('get_encoded_file', array('session' => $session, 'filename' => "install/data/disc_client.php")); if (!empty($soapclient->error_str)) { if ($attempts < $max_attempts && substr_count($soapclient->error_str, 'HTTP Error: socket read of headers timed out') > 0) { echo "Could not retrieve file patterns list. Error was: " . $soapclient->error_str; $attempts++; echo "<BR> {$attempts} of {$max_attempts} attempts trying again<br>"; flush(); ob_flush(); return disc_client_file_sync($soapclient, $session, $verbose, $attempts); } die("Failed: Could not retrieve file patterns list. Error was: " . $soapclient->error_str); } if ($result['error']['number'] != 0) { die("Failed: Could not retrieve file patterns list. Error was: " . $result['error']['name'] . ' - ' . $result['description']); } $newfile = write_encoded_file($result, $temp_dir); if (!copy($newfile, $result['filename'])) { die("Could not copy {$newfile} to new location."); } // get array definitions: $disc_client_ignore require "install/data/disc_client.php"; // get file list/md5s, write as temp file, then require_once from tmp location $result = $soapclient->call('get_disc_client_file_list', array('session' => $session)); if (!empty($soapclient->error_str)) { if ($attempts < $max_attempts && substr_count($soapclient->error_str, 'HTTP Error: socket read of headers timed out') > 0) { echo "Could not retrieve file patterns list. Error was: " . $soapclient->error_str; $attempts++; echo "<BR> {$attempts} of {$max_attempts} attempts trying again<br>"; flush(); ob_flush(); return disc_client_file_sync($soapclient, $session, $verbose, $attempts); } die("Failed: Could not retrieve file list. Error was: " . $soapclient->error_str); } if ($result['error']['number'] != 0) { die("Failed: Could not retrieve file list. Error was: " . $result['error']['name'] . ' - ' . $result['description']); } $temp_file = tempnam($temp_dir, "sug"); write_encoded_file($result, $temp_dir, $temp_file); require_once $temp_file; // determine which files are needed locally $needed_file_list = array(); $server_files = array(); // used later for removing unneeded local files foreach ($server_file_list as $result) { $server_filename = $result['filename']; $server_md5 = $result['md5']; $server_files[] = $server_filename; $ignore = false; foreach ($disc_client_ignore as $ignore_pattern) { if (preg_match("#" . $ignore_pattern . "#", $server_filename)) { $ignore = true; } } if (file_exists($server_filename)) { if (!$ignore && md5_file($server_filename) != $server_md5) { // not on the ignore list and the client's md5sum does not match the server's $needed_file_list[] = $server_filename; } } else { if (!$ignore) { $return_str .= disc_client_utils_print("File missing from client : {$temp_dir}/{$server_filename}<br>", $verbose); $needed_file_list[] = $server_filename; } } } if (sizeof($server_files) < 100) { if ($attempts < $max_attempts && substr_count($soapclient->error_str, 'HTTP Error: socket read of headers timed out') > 0) { echo "Could not retrieve file patterns list. Error was: " . $soapclient->error_str; $attempts++; echo "<BR> {$attempts} of {$max_attempts} attempts trying again<br>"; flush(); ob_flush(); return disc_client_file_sync($soapclient, $session, $verbose, $attempts); } die("Failed: Empty file list returned from server. Please try again."); } // get needed files foreach ($needed_file_list as $needed_file) { $result = $soapclient->call('get_encoded_file', array('session' => $session, 'filename' => "{$needed_file}")); write_encoded_file($result, $temp_dir); } // all files recieved, copy them into place foreach ($needed_file_list as $needed_file) { if (file_exists("{$temp_dir}/{$needed_file}")) { mkdir_recursive(dirname($needed_file), true); copy("{$temp_dir}/{$needed_file}", $needed_file); $return_str .= disc_client_utils_print("Updated file: {$needed_file} <br>", $verbose); } else { $return_str .= disc_client_utils_print("File missing from client : {$temp_dir}/{$needed_file}<br>", $verbose); } } if (sizeof($needed_file_list) == 0) { $return_str .= disc_client_utils_print("No files needed to be synchronized.<br>", $verbose); } // scrub local files that are not part of client application $local_file_list = findAllFiles(".", array()); $removed_file_count = 0; foreach ($local_file_list as $local_file) { $ignore = false; foreach ($disc_client_ignore as $ignore_pattern) { if (preg_match("#" . $ignore_pattern . "#", $local_file)) { $ignore = true; } } if (!$ignore && !in_array($local_file, $server_files)) { // not on the ignore list and the server does not know about it unlink($local_file); $return_str .= disc_client_utils_print("Removed file: {$local_file} <br>", $verbose); $removed_file_count++; } } if ($removed_file_count == 0) { $return_str .= disc_client_utils_print("No files needed to be removed.<br>", $verbose); } return $return_str; }
function get_encoded_portal_zip_file($session, $md5file, $last_sync, $is_md5_sync = 1) { // files might be big global $sugar_config; ini_set("memory_limit", "-1"); $md5 = ""; $data = ""; $error = new SoapError(); $the_error = ""; if (!validate_authenticated($session)) { $the_error = "Invalid session"; } require "install/data/disc_client.php"; $tempdir_parent = create_cache_directory("disc_client"); $temp_dir = tempnam($tempdir_parent, "sug"); sugar_mkdir($temp_dir, 0775); $temp_file = tempnam($temp_dir, "sug"); write_encoded_file($md5file, $temp_dir, $temp_file); $ignore = false; //generate md5 files on server require_once $temp_file; $server_files = array(); // used later for removing unneeded local files $zip_file = tempnam(tempdir_parent, $session); $root_files = array(); $custom_files = array(); $file_list = array(); if (!$is_md5_sync) { if (is_dir("portal")) { $root_files = findAllTouchedFiles("portal", array(), $last_sync); } if (is_dir("custom/portal")) { $custom_files = findAllTouchedFiles("custom/portal", array(), $last_sync); } $all_src_files = array_merge($root_files, $custom_files); foreach ($all_src_files as $src_file) { $ignore = false; foreach ($disc_client_ignore as $ignore_pattern) { if (preg_match("#" . $ignore_pattern . "#", $src_file)) { $ignore = true; } } if (!$ignore) { //we have to strip off portal or custom/portal before the src file to look it up $key = str_replace('custom/portal/', '', $src_file); $key = str_replace('portal/', '', $key); if ($client_file_list != null && isset($client_file_list[$key])) { //we have found a file out of sync $file_list[] = $src_file; //since we have processed this element of the client //list of files, remove it from the list unset($client_file_list[$key]); } else { //this file does not exist on the client side $file_list[] = $src_file; } } } } else { if (is_dir("portal")) { $root_files = findAllFiles("portal", array()); } if (is_dir("custom/portal")) { $custom_files = findAllFiles("custom/portal", array()); } $all_src_files = array_merge($root_files, $custom_files); foreach ($all_src_files as $src_file) { $ignore = false; foreach ($disc_client_ignore as $ignore_pattern) { if (preg_match("#" . $ignore_pattern . "#", $src_file)) { $ignore = true; } } if (!$ignore) { $value = md5_file($src_file); //we have to strip off portal or custom/portal before the src file to look it up $key = str_replace('custom/portal/', '', $src_file); $key = str_replace('portal/', '', $key); if ($client_file_list != null && isset($client_file_list[$key])) { if ($value != $client_file_list[$key]) { //we have found a file out of sync $file_list[] = $src_file; //since we have processed this element of the client //list of files, remove it from the list } unset($client_file_list[$key]); } else { //this file does not exist on the client side $file_list[] = $src_file; } } } } zip_files_list($zip_file, $file_list, '|.*portal/|'); $contents = sugar_file_get_contents($zip_file); // encode data $data = base64_encode($contents); unlink($zip_file); return array('result' => $data, 'error' => $error->get_soap_array()); }