function array_strip_offset($opts, $pipe, $cmd = __FUNCTION__) { # merge opts $opts = merge_opts($opts, $pipe, 'offset'); # get the list and offset if (is_array($opts)) { if (array_key_exists('offset', $opts)) { $offset = $opts['offset']; } else { return error($cmd, "no offset defined"); } if (array_key_exists('list', $opts)) { $list = $opts; } else { $list = $pipe; } } else { $list = $pipe; $offset = (int) $opts; if ($offset <= 0) { return $list; } } # check validity of list and offset if (!check_opt_set_type($cmd, $list, 'list', 'array_of_strings')) { return false; } if (!check_opt_set_type($cmd, $offset, 'offset', 'integer')) { return false; } # strip offset recursively $list = array_strip_offset_util($list, $offset); return $list; }
function ftp_scandir($opts, $pipe = false, $cmd = __FUNCTION__) { # set prefix $prefix = 'ftp'; # merge opts $opts = merge_opts($opts, $pipe, 'dir'); # get execute opt $execute = get_opt_config_value($prefix, $opts, 'execute', true); if (!check_opt_set_type($cmd, $execute, 'execute', 'boolean')) { return false; } # check if we should execute or not if (!$execute) { return true; } # get remote dir opt $dir = get_opt($prefix, $opts, 'dir'); if (!check_opt_set_type($cmd, $dir, 'dir', 'string')) { return false; } # get search opt $search = get_opt($prefix, $opts, 'search', ''); if (!check_opt_set_type($cmd, $search, 'search', 'string')) { return false; } # get chdir retries $chdir_retries = get_opt_config_value($prefix, $opts, 'chdir_retries', 3); if (!check_opt_set_type($cmd, $chdir_retries, 'chdir_retries', 'integer')) { return false; } # get scandir retries opt $scandir_retries = get_opt_config_value($prefix, $opts, 'scandir_retries', 3); if (!check_opt_set_type($cmd, $scandir_retries, 'scandir_retries', 'integer')) { return false; } # setup connection $conn = ftp_get_conn($opts, $cmd); if (!$conn) { return false; } # get connection values $ftp_handle = $conn['handle']; $svr = $conn['svr']; # change to dir (note: we chdir because some server do not allow rawlist except in current working directory) debug_echo($cmd, "scanning dir on FTP server : {$dir} ..."); $success = false; for ($i = 0; $i < $chdir_retries; $i++) { if (@ftp_chdir($ftp_handle, $dir)) { $success = true; break; } } if (!$success) { return error($cmd, "could not chdir to dir on FTP server : {$dir} ({$chdir_retries} attempts)"); } # build full search if ($dir == '/') { $full_search = "/{$search}"; $path_offset = 1; } else { $full_search = "{$dir}/{$search}"; $path_offset = strlen($dir) + 1; } # try to get list for ($i = 0; $i < $scandir_retries; $i++) { // Note: if rawlist is used then it's because a server does not accept nlist $list = @ftp_nlist($ftp_handle, $full_search); if (!is_array($list)) { break; } } if (!$list) { return error($cmd, "could not scan dir on FTP server : {$dir} ({$scandir_retries} attempts)"); } # convert rawlist //$list = ftp_rawlist_2_nlist ($list); # remove directory name $list = array_strip_offset_util($list, $path_offset); # display list of files $count = count($list); debug_echo($cmd, "{$count} files found in dir on FTP server : {$dir}"); debug_dump_list($list, true); # return if ($pipe === false) { return $list; } return array('files' => $list); }