We propose and analyze a multifunctional setup consisting of high-finesse optical cavities, beam splitters, and phase shifters. The basic scheme projects arbitrary photonic two-mode input states onto the subspace spanned by the product of Fock states vertical bar n >vertical bar n > with n = 0,1,2,.... This protocol does not only provide the possibility to conditionally generate highly entangled photon number states as resource for quantum information protocols but also allows one to test and hence purify this type of quantum states in a communication scenario, which is of great practical importance. The scheme is especially attractive as a generalization to many modes allows for distribution and purification of entanglement in networks. In an alternative working mode, the setup allows for quantum nondemolition number resolved photodetection in the optical domain.