function disc_client_get_zip($soapclient, $session, $verbose = false, $attempts = 0, $force_md5_sync = false) { $max_attempts = 3; global $sugar_config, $timedate; // files might be big ini_set("memory_limit", "-1"); set_time_limit(3600); ini_set('default_socket_timeout', 3600); $return_str = ""; //1) rather than using md5, we will use the date_modified if (file_exists('modules/Sync/file_config.php') && $force_md5_sync != true) { require_once 'modules/Sync/file_config.php'; global $file_sync_info; if (!isset($file_sync_info['last_local_sync']) && !isset($file_sync_info['last_server_sync'])) { $last_server_sync = $last_local_sync = $timedate->nowDb(); $is_first_sync = true; } else { $last_local_sync = $file_sync_info['last_local_sync']; $last_server_sync = $file_sync_info['last_server_sync']; $is_first_sync = false; } } else { $last_server_sync = $last_local_sync = $timedate->nowDb(); $is_first_sync = true; } $tempdir = create_cache_directory("disc_client"); $temp_file = tempnam($tempdir, "sug"); $file_list = array(); if (!$is_first_sync) { $all_src_files = findAllTouchedFiles(".", array(), $last_local_sync); foreach ($all_src_files as $src_file) { $file_list[$src_file] = $src_file; } } else { $all_src_files = findAllFiles(".", array()); require "install/data/disc_client.php"; foreach ($all_src_files as $src_file) { foreach ($disc_client_ignore as $ignore_pattern) { if (!preg_match("#" . $ignore_pattern . "#", $src_file)) { $md5 = md5_file($src_file); $file_list[$src_file] = $md5; } } } } //2) save the list of md5 files to file system if (!write_array_to_file("client_file_list", $file_list, $temp_file)) { echo "Could not save file."; } // read file $contents = sugar_file_get_contents($temp_file); $md5 = md5($contents); // encode data $data = base64_encode($contents); $md5file = array('filename' => $temp_file, 'md5' => $md5, 'data' => $data, 'error' => null); $result = $soapclient->call('get_encoded_zip_file', array('session' => $session, 'md5file' => $md5file, 'last_sync' => $last_server_sync, 'is_md5_sync' => $is_first_sync)); //3) at this point we could have the zip file $zip_file = tempnam($tempdir, "zip") . '.zip'; if (isset($result['result']) && !empty($result['result'])) { $fh = sugar_fopen($zip_file, 'w'); fwrite($fh, base64_decode($result['result'])); fclose($fh); unzip($zip_file, ".", true); } if (file_exists($zip_file)) { unlink($zip_file); } $file_sync_info['last_local_sync'] = $timedate->nowDb(); $server_time = $soapclient->call('get_gmt_time', array()); $file_sync_info['last_server_sync'] = $server_time; $file_sync_info['is_first_sync'] = $is_first_sync; write_array_to_file('file_sync_info', $file_sync_info, 'modules/Sync/file_config.php'); echo "File sync complete."; }
function findAllTouchedFiles($the_dir, $the_array, $date_modified, $filter = '') { if (!is_dir($the_dir)) { return $the_array; } $d = dir($the_dir); while (false !== ($f = $d->read())) { if ($f == "." || $f == "..") { continue; } if (is_dir("{$the_dir}/{$f}")) { // i think depth first is ok, given our cvs repo structure -Bob. $the_array = findAllTouchedFiles("{$the_dir}/{$f}", $the_array, $date_modified, $filter); } else { $file_date = date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s', filemtime("{$the_dir}/{$f}"))) - date('Z')); if (strtotime($file_date) > strtotime($date_modified) && (empty($filter) || preg_match($filter, $f))) { array_push($the_array, "{$the_dir}/{$f}"); } } } return $the_array; }
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()); }