We explain in a unified way the experimental data on ultrahigh
energy cosmic rays (UHECR) and neutrinos, using a single source class
and obeying limits on the extragalactic diffuse gamma-ray background (EGRB). If UHECRs only interact hadronically with gas around their sources, the resulting diffuse CR flux can be matched well to the observed one, providing at the same time large neutrino fluxes.
Since the required fraction of heavy nuclei is, however, rather large, air showers in the Earth's atmosphere induced by UHECRs with energies $E\gtrsim 3\times 10^{18}$\,eV would reach in such a case their maxima too high. Therefore additional photo-hadronic interactions of UHECRs close to the accelerator have to be present, in order to modify the nuclear composition of CRs in a relatively narrow energy interval. We include thus both photon and gas backgrounds, and combine the resulting CR spectra with the high-energy part of the Galactic CR fluxes predicted by the escape model. As result, we find a good description of experimental data on the total CR flux, the mean shower maximum depth \Xmax\ and its width \X2 in the whole energy range above $E\simeq 10^{17}$\,eV. The predicted high-energy neutrino flux matches IceCube measurements, while the contribution to the EGRB is of order 30\%.