public static function ftpSearch(Net_FTP $ftp, $pattern, $path = '/', $stopOnMatch = false) { self::ftpInclude(); $results = array(); // Check pattern type if (!is_array($pattern) && !is_string($pattern)) { throw new Engine_Package_Exception('invalid pattern'); } $type = is_array($pattern) ? 'list' : ($pattern[0] == '/' ? 'regex' : 'name'); // Check path if (empty($path)) { throw new Engine_Package_Exception('invalid path'); } // Change dir $ret = $ftp->cd($path); if ($ftp->isError($ret)) { throw new Engine_Exception($ret->getMessage(), $ret->getCode()); } // List files $ret = $ftp->ls(); if ($ftp->isError($ret)) { throw new Engine_Exception($ret->getMessage(), $ret->getCode()); } // Check files $dirs = array(); foreach ($ret as $info) { $fullPath = rtrim($path, '/') . '/' . $info['name']; // DEBUG if ($info['is_dir'] == 'd') { $dirs[] = $fullPath; } switch ($type) { case 'list': if (in_array($info['name'], $pattern)) { $results[] = $fullPath; if ($stopOnMatch) { return $results; } } break; case 'regex': if (preg_match($pattern, $info['name'])) { // We could use the full path here (to give access to subdirectories) $results[] = $fullPath; if ($stopOnMatch) { return $results; } } break; case 'name': if ($pattern == $info['name']) { $results[] = $fullPath; if ($stopOnMatch) { return $results; } } break; default: throw new Engine_Package_Exception('invalid pattern'); break; } } // Recurse into directories foreach ($dirs as $dir) { $safeDir = rtrim($dir, '/') . '/'; // Check to make sure this is correct try { $childResults = self::ftpSearch($ftp, $pattern, $safeDir); $results = array_merge($results, $childResults); } catch (Exception $e) { continue; // @todo should we throw or ignore? } } return $results; }