Page MenuHomePhorge

No OneTemporary

diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..94a9ed0
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,674 @@
+ GNU GENERAL PUBLIC LICENSE
+ Version 3, 29 June 2007
+
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+ Preamble
+
+ The GNU General Public License is a free, copyleft license for
+software and other kinds of works.
+
+ The licenses for most software and other practical works are designed
+to take away your freedom to share and change the works. By contrast,
+the GNU General Public License is intended to guarantee your freedom to
+share and change all versions of a program--to make sure it remains free
+software for all its users. We, the Free Software Foundation, use the
+GNU General Public License for most of our software; it applies also to
+any other work released this way by its authors. You can apply it to
+your programs, too.
+
+ When we speak of free software, we are referring to freedom, not
+price. Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+them if you wish), that you receive source code or can get it if you
+want it, that you can change the software or use pieces of it in new
+free programs, and that you know you can do these things.
+
+ To protect your rights, we need to prevent others from denying you
+these rights or asking you to surrender the rights. Therefore, you have
+certain responsibilities if you distribute copies of the software, or if
+you modify it: responsibilities to respect the freedom of others.
+
+ For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must pass on to the recipients the same
+freedoms that you received. You must make sure that they, too, receive
+or can get the source code. And you must show them these terms so they
+know their rights.
+
+ Developers that use the GNU GPL protect your rights with two steps:
+(1) assert copyright on the software, and (2) offer you this License
+giving you legal permission to copy, distribute and/or modify it.
+
+ For the developers' and authors' protection, the GPL clearly explains
+that there is no warranty for this free software. For both users' and
+authors' sake, the GPL requires that modified versions be marked as
+changed, so that their problems will not be attributed erroneously to
+authors of previous versions.
+
+ Some devices are designed to deny users access to install or run
+modified versions of the software inside them, although the manufacturer
+can do so. This is fundamentally incompatible with the aim of
+protecting users' freedom to change the software. The systematic
+pattern of such abuse occurs in the area of products for individuals to
+use, which is precisely where it is most unacceptable. Therefore, we
+have designed this version of the GPL to prohibit the practice for those
+products. If such problems arise substantially in other domains, we
+stand ready to extend this provision to those domains in future versions
+of the GPL, as needed to protect the freedom of users.
+
+ Finally, every program is threatened constantly by software patents.
+States should not allow patents to restrict development and use of
+software on general-purpose computers, but in those that do, we wish to
+avoid the special danger that patents applied to a free program could
+make it effectively proprietary. To prevent this, the GPL assures that
+patents cannot be used to render the program non-free.
+
+ The precise terms and conditions for copying, distribution and
+modification follow.
+
+ TERMS AND CONDITIONS
+
+ 0. Definitions.
+
+ "This License" refers to version 3 of the GNU General Public License.
+
+ "Copyright" also means copyright-like laws that apply to other kinds of
+works, such as semiconductor masks.
+
+ "The Program" refers to any copyrightable work licensed under this
+License. Each licensee is addressed as "you". "Licensees" and
+"recipients" may be individuals or organizations.
+
+ To "modify" a work means to copy from or adapt all or part of the work
+in a fashion requiring copyright permission, other than the making of an
+exact copy. The resulting work is called a "modified version" of the
+earlier work or a work "based on" the earlier work.
+
+ A "covered work" means either the unmodified Program or a work based
+on the Program.
+
+ To "propagate" a work means to do anything with it that, without
+permission, would make you directly or secondarily liable for
+infringement under applicable copyright law, except executing it on a
+computer or modifying a private copy. Propagation includes copying,
+distribution (with or without modification), making available to the
+public, and in some countries other activities as well.
+
+ To "convey" a work means any kind of propagation that enables other
+parties to make or receive copies. Mere interaction with a user through
+a computer network, with no transfer of a copy, is not conveying.
+
+ An interactive user interface displays "Appropriate Legal Notices"
+to the extent that it includes a convenient and prominently visible
+feature that (1) displays an appropriate copyright notice, and (2)
+tells the user that there is no warranty for the work (except to the
+extent that warranties are provided), that licensees may convey the
+work under this License, and how to view a copy of this License. If
+the interface presents a list of user commands or options, such as a
+menu, a prominent item in the list meets this criterion.
+
+ 1. Source Code.
+
+ The "source code" for a work means the preferred form of the work
+for making modifications to it. "Object code" means any non-source
+form of a work.
+
+ A "Standard Interface" means an interface that either is an official
+standard defined by a recognized standards body, or, in the case of
+interfaces specified for a particular programming language, one that
+is widely used among developers working in that language.
+
+ The "System Libraries" of an executable work include anything, other
+than the work as a whole, that (a) is included in the normal form of
+packaging a Major Component, but which is not part of that Major
+Component, and (b) serves only to enable use of the work with that
+Major Component, or to implement a Standard Interface for which an
+implementation is available to the public in source code form. A
+"Major Component", in this context, means a major essential component
+(kernel, window system, and so on) of the specific operating system
+(if any) on which the executable work runs, or a compiler used to
+produce the work, or an object code interpreter used to run it.
+
+ The "Corresponding Source" for a work in object code form means all
+the source code needed to generate, install, and (for an executable
+work) run the object code and to modify the work, including scripts to
+control those activities. However, it does not include the work's
+System Libraries, or general-purpose tools or generally available free
+programs which are used unmodified in performing those activities but
+which are not part of the work. For example, Corresponding Source
+includes interface definition files associated with source files for
+the work, and the source code for shared libraries and dynamically
+linked subprograms that the work is specifically designed to require,
+such as by intimate data communication or control flow between those
+subprograms and other parts of the work.
+
+ The Corresponding Source need not include anything that users
+can regenerate automatically from other parts of the Corresponding
+Source.
+
+ The Corresponding Source for a work in source code form is that
+same work.
+
+ 2. Basic Permissions.
+
+ All rights granted under this License are granted for the term of
+copyright on the Program, and are irrevocable provided the stated
+conditions are met. This License explicitly affirms your unlimited
+permission to run the unmodified Program. The output from running a
+covered work is covered by this License only if the output, given its
+content, constitutes a covered work. This License acknowledges your
+rights of fair use or other equivalent, as provided by copyright law.
+
+ You may make, run and propagate covered works that you do not
+convey, without conditions so long as your license otherwise remains
+in force. You may convey covered works to others for the sole purpose
+of having them make modifications exclusively for you, or provide you
+with facilities for running those works, provided that you comply with
+the terms of this License in conveying all material for which you do
+not control copyright. Those thus making or running the covered works
+for you must do so exclusively on your behalf, under your direction
+and control, on terms that prohibit them from making any copies of
+your copyrighted material outside their relationship with you.
+
+ Conveying under any other circumstances is permitted solely under
+the conditions stated below. Sublicensing is not allowed; section 10
+makes it unnecessary.
+
+ 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
+
+ No covered work shall be deemed part of an effective technological
+measure under any applicable law fulfilling obligations under article
+11 of the WIPO copyright treaty adopted on 20 December 1996, or
+similar laws prohibiting or restricting circumvention of such
+measures.
+
+ When you convey a covered work, you waive any legal power to forbid
+circumvention of technological measures to the extent such circumvention
+is effected by exercising rights under this License with respect to
+the covered work, and you disclaim any intention to limit operation or
+modification of the work as a means of enforcing, against the work's
+users, your or third parties' legal rights to forbid circumvention of
+technological measures.
+
+ 4. Conveying Verbatim Copies.
+
+ You may convey verbatim copies of the Program's source code as you
+receive it, in any medium, provided that you conspicuously and
+appropriately publish on each copy an appropriate copyright notice;
+keep intact all notices stating that this License and any
+non-permissive terms added in accord with section 7 apply to the code;
+keep intact all notices of the absence of any warranty; and give all
+recipients a copy of this License along with the Program.
+
+ You may charge any price or no price for each copy that you convey,
+and you may offer support or warranty protection for a fee.
+
+ 5. Conveying Modified Source Versions.
+
+ You may convey a work based on the Program, or the modifications to
+produce it from the Program, in the form of source code under the
+terms of section 4, provided that you also meet all of these conditions:
+
+ a) The work must carry prominent notices stating that you modified
+ it, and giving a relevant date.
+
+ b) The work must carry prominent notices stating that it is
+ released under this License and any conditions added under section
+ 7. This requirement modifies the requirement in section 4 to
+ "keep intact all notices".
+
+ c) You must license the entire work, as a whole, under this
+ License to anyone who comes into possession of a copy. This
+ License will therefore apply, along with any applicable section 7
+ additional terms, to the whole of the work, and all its parts,
+ regardless of how they are packaged. This License gives no
+ permission to license the work in any other way, but it does not
+ invalidate such permission if you have separately received it.
+
+ d) If the work has interactive user interfaces, each must display
+ Appropriate Legal Notices; however, if the Program has interactive
+ interfaces that do not display Appropriate Legal Notices, your
+ work need not make them do so.
+
+ A compilation of a covered work with other separate and independent
+works, which are not by their nature extensions of the covered work,
+and which are not combined with it such as to form a larger program,
+in or on a volume of a storage or distribution medium, is called an
+"aggregate" if the compilation and its resulting copyright are not
+used to limit the access or legal rights of the compilation's users
+beyond what the individual works permit. Inclusion of a covered work
+in an aggregate does not cause this License to apply to the other
+parts of the aggregate.
+
+ 6. Conveying Non-Source Forms.
+
+ You may convey a covered work in object code form under the terms
+of sections 4 and 5, provided that you also convey the
+machine-readable Corresponding Source under the terms of this License,
+in one of these ways:
+
+ a) Convey the object code in, or embodied in, a physical product
+ (including a physical distribution medium), accompanied by the
+ Corresponding Source fixed on a durable physical medium
+ customarily used for software interchange.
+
+ b) Convey the object code in, or embodied in, a physical product
+ (including a physical distribution medium), accompanied by a
+ written offer, valid for at least three years and valid for as
+ long as you offer spare parts or customer support for that product
+ model, to give anyone who possesses the object code either (1) a
+ copy of the Corresponding Source for all the software in the
+ product that is covered by this License, on a durable physical
+ medium customarily used for software interchange, for a price no
+ more than your reasonable cost of physically performing this
+ conveying of source, or (2) access to copy the
+ Corresponding Source from a network server at no charge.
+
+ c) Convey individual copies of the object code with a copy of the
+ written offer to provide the Corresponding Source. This
+ alternative is allowed only occasionally and noncommercially, and
+ only if you received the object code with such an offer, in accord
+ with subsection 6b.
+
+ d) Convey the object code by offering access from a designated
+ place (gratis or for a charge), and offer equivalent access to the
+ Corresponding Source in the same way through the same place at no
+ further charge. You need not require recipients to copy the
+ Corresponding Source along with the object code. If the place to
+ copy the object code is a network server, the Corresponding Source
+ may be on a different server (operated by you or a third party)
+ that supports equivalent copying facilities, provided you maintain
+ clear directions next to the object code saying where to find the
+ Corresponding Source. Regardless of what server hosts the
+ Corresponding Source, you remain obligated to ensure that it is
+ available for as long as needed to satisfy these requirements.
+
+ e) Convey the object code using peer-to-peer transmission, provided
+ you inform other peers where the object code and Corresponding
+ Source of the work are being offered to the general public at no
+ charge under subsection 6d.
+
+ A separable portion of the object code, whose source code is excluded
+from the Corresponding Source as a System Library, need not be
+included in conveying the object code work.
+
+ A "User Product" is either (1) a "consumer product", which means any
+tangible personal property which is normally used for personal, family,
+or household purposes, or (2) anything designed or sold for incorporation
+into a dwelling. In determining whether a product is a consumer product,
+doubtful cases shall be resolved in favor of coverage. For a particular
+product received by a particular user, "normally used" refers to a
+typical or common use of that class of product, regardless of the status
+of the particular user or of the way in which the particular user
+actually uses, or expects or is expected to use, the product. A product
+is a consumer product regardless of whether the product has substantial
+commercial, industrial or non-consumer uses, unless such uses represent
+the only significant mode of use of the product.
+
+ "Installation Information" for a User Product means any methods,
+procedures, authorization keys, or other information required to install
+and execute modified versions of a covered work in that User Product from
+a modified version of its Corresponding Source. The information must
+suffice to ensure that the continued functioning of the modified object
+code is in no case prevented or interfered with solely because
+modification has been made.
+
+ If you convey an object code work under this section in, or with, or
+specifically for use in, a User Product, and the conveying occurs as
+part of a transaction in which the right of possession and use of the
+User Product is transferred to the recipient in perpetuity or for a
+fixed term (regardless of how the transaction is characterized), the
+Corresponding Source conveyed under this section must be accompanied
+by the Installation Information. But this requirement does not apply
+if neither you nor any third party retains the ability to install
+modified object code on the User Product (for example, the work has
+been installed in ROM).
+
+ The requirement to provide Installation Information does not include a
+requirement to continue to provide support service, warranty, or updates
+for a work that has been modified or installed by the recipient, or for
+the User Product in which it has been modified or installed. Access to a
+network may be denied when the modification itself materially and
+adversely affects the operation of the network or violates the rules and
+protocols for communication across the network.
+
+ Corresponding Source conveyed, and Installation Information provided,
+in accord with this section must be in a format that is publicly
+documented (and with an implementation available to the public in
+source code form), and must require no special password or key for
+unpacking, reading or copying.
+
+ 7. Additional Terms.
+
+ "Additional permissions" are terms that supplement the terms of this
+License by making exceptions from one or more of its conditions.
+Additional permissions that are applicable to the entire Program shall
+be treated as though they were included in this License, to the extent
+that they are valid under applicable law. If additional permissions
+apply only to part of the Program, that part may be used separately
+under those permissions, but the entire Program remains governed by
+this License without regard to the additional permissions.
+
+ When you convey a copy of a covered work, you may at your option
+remove any additional permissions from that copy, or from any part of
+it. (Additional permissions may be written to require their own
+removal in certain cases when you modify the work.) You may place
+additional permissions on material, added by you to a covered work,
+for which you have or can give appropriate copyright permission.
+
+ Notwithstanding any other provision of this License, for material you
+add to a covered work, you may (if authorized by the copyright holders of
+that material) supplement the terms of this License with terms:
+
+ a) Disclaiming warranty or limiting liability differently from the
+ terms of sections 15 and 16 of this License; or
+
+ b) Requiring preservation of specified reasonable legal notices or
+ author attributions in that material or in the Appropriate Legal
+ Notices displayed by works containing it; or
+
+ c) Prohibiting misrepresentation of the origin of that material, or
+ requiring that modified versions of such material be marked in
+ reasonable ways as different from the original version; or
+
+ d) Limiting the use for publicity purposes of names of licensors or
+ authors of the material; or
+
+ e) Declining to grant rights under trademark law for use of some
+ trade names, trademarks, or service marks; or
+
+ f) Requiring indemnification of licensors and authors of that
+ material by anyone who conveys the material (or modified versions of
+ it) with contractual assumptions of liability to the recipient, for
+ any liability that these contractual assumptions directly impose on
+ those licensors and authors.
+
+ All other non-permissive additional terms are considered "further
+restrictions" within the meaning of section 10. If the Program as you
+received it, or any part of it, contains a notice stating that it is
+governed by this License along with a term that is a further
+restriction, you may remove that term. If a license document contains
+a further restriction but permits relicensing or conveying under this
+License, you may add to a covered work material governed by the terms
+of that license document, provided that the further restriction does
+not survive such relicensing or conveying.
+
+ If you add terms to a covered work in accord with this section, you
+must place, in the relevant source files, a statement of the
+additional terms that apply to those files, or a notice indicating
+where to find the applicable terms.
+
+ Additional terms, permissive or non-permissive, may be stated in the
+form of a separately written license, or stated as exceptions;
+the above requirements apply either way.
+
+ 8. Termination.
+
+ You may not propagate or modify a covered work except as expressly
+provided under this License. Any attempt otherwise to propagate or
+modify it is void, and will automatically terminate your rights under
+this License (including any patent licenses granted under the third
+paragraph of section 11).
+
+ However, if you cease all violation of this License, then your
+license from a particular copyright holder is reinstated (a)
+provisionally, unless and until the copyright holder explicitly and
+finally terminates your license, and (b) permanently, if the copyright
+holder fails to notify you of the violation by some reasonable means
+prior to 60 days after the cessation.
+
+ Moreover, your license from a particular copyright holder is
+reinstated permanently if the copyright holder notifies you of the
+violation by some reasonable means, this is the first time you have
+received notice of violation of this License (for any work) from that
+copyright holder, and you cure the violation prior to 30 days after
+your receipt of the notice.
+
+ Termination of your rights under this section does not terminate the
+licenses of parties who have received copies or rights from you under
+this License. If your rights have been terminated and not permanently
+reinstated, you do not qualify to receive new licenses for the same
+material under section 10.
+
+ 9. Acceptance Not Required for Having Copies.
+
+ You are not required to accept this License in order to receive or
+run a copy of the Program. Ancillary propagation of a covered work
+occurring solely as a consequence of using peer-to-peer transmission
+to receive a copy likewise does not require acceptance. However,
+nothing other than this License grants you permission to propagate or
+modify any covered work. These actions infringe copyright if you do
+not accept this License. Therefore, by modifying or propagating a
+covered work, you indicate your acceptance of this License to do so.
+
+ 10. Automatic Licensing of Downstream Recipients.
+
+ Each time you convey a covered work, the recipient automatically
+receives a license from the original licensors, to run, modify and
+propagate that work, subject to this License. You are not responsible
+for enforcing compliance by third parties with this License.
+
+ An "entity transaction" is a transaction transferring control of an
+organization, or substantially all assets of one, or subdividing an
+organization, or merging organizations. If propagation of a covered
+work results from an entity transaction, each party to that
+transaction who receives a copy of the work also receives whatever
+licenses to the work the party's predecessor in interest had or could
+give under the previous paragraph, plus a right to possession of the
+Corresponding Source of the work from the predecessor in interest, if
+the predecessor has it or can get it with reasonable efforts.
+
+ You may not impose any further restrictions on the exercise of the
+rights granted or affirmed under this License. For example, you may
+not impose a license fee, royalty, or other charge for exercise of
+rights granted under this License, and you may not initiate litigation
+(including a cross-claim or counterclaim in a lawsuit) alleging that
+any patent claim is infringed by making, using, selling, offering for
+sale, or importing the Program or any portion of it.
+
+ 11. Patents.
+
+ A "contributor" is a copyright holder who authorizes use under this
+License of the Program or a work on which the Program is based. The
+work thus licensed is called the contributor's "contributor version".
+
+ A contributor's "essential patent claims" are all patent claims
+owned or controlled by the contributor, whether already acquired or
+hereafter acquired, that would be infringed by some manner, permitted
+by this License, of making, using, or selling its contributor version,
+but do not include claims that would be infringed only as a
+consequence of further modification of the contributor version. For
+purposes of this definition, "control" includes the right to grant
+patent sublicenses in a manner consistent with the requirements of
+this License.
+
+ Each contributor grants you a non-exclusive, worldwide, royalty-free
+patent license under the contributor's essential patent claims, to
+make, use, sell, offer for sale, import and otherwise run, modify and
+propagate the contents of its contributor version.
+
+ In the following three paragraphs, a "patent license" is any express
+agreement or commitment, however denominated, not to enforce a patent
+(such as an express permission to practice a patent or covenant not to
+sue for patent infringement). To "grant" such a patent license to a
+party means to make such an agreement or commitment not to enforce a
+patent against the party.
+
+ If you convey a covered work, knowingly relying on a patent license,
+and the Corresponding Source of the work is not available for anyone
+to copy, free of charge and under the terms of this License, through a
+publicly available network server or other readily accessible means,
+then you must either (1) cause the Corresponding Source to be so
+available, or (2) arrange to deprive yourself of the benefit of the
+patent license for this particular work, or (3) arrange, in a manner
+consistent with the requirements of this License, to extend the patent
+license to downstream recipients. "Knowingly relying" means you have
+actual knowledge that, but for the patent license, your conveying the
+covered work in a country, or your recipient's use of the covered work
+in a country, would infringe one or more identifiable patents in that
+country that you have reason to believe are valid.
+
+ If, pursuant to or in connection with a single transaction or
+arrangement, you convey, or propagate by procuring conveyance of, a
+covered work, and grant a patent license to some of the parties
+receiving the covered work authorizing them to use, propagate, modify
+or convey a specific copy of the covered work, then the patent license
+you grant is automatically extended to all recipients of the covered
+work and works based on it.
+
+ A patent license is "discriminatory" if it does not include within
+the scope of its coverage, prohibits the exercise of, or is
+conditioned on the non-exercise of one or more of the rights that are
+specifically granted under this License. You may not convey a covered
+work if you are a party to an arrangement with a third party that is
+in the business of distributing software, under which you make payment
+to the third party based on the extent of your activity of conveying
+the work, and under which the third party grants, to any of the
+parties who would receive the covered work from you, a discriminatory
+patent license (a) in connection with copies of the covered work
+conveyed by you (or copies made from those copies), or (b) primarily
+for and in connection with specific products or compilations that
+contain the covered work, unless you entered into that arrangement,
+or that patent license was granted, prior to 28 March 2007.
+
+ Nothing in this License shall be construed as excluding or limiting
+any implied license or other defenses to infringement that may
+otherwise be available to you under applicable patent law.
+
+ 12. No Surrender of Others' Freedom.
+
+ If conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License. If you cannot convey a
+covered work so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you may
+not convey it at all. For example, if you agree to terms that obligate you
+to collect a royalty for further conveying from those to whom you convey
+the Program, the only way you could satisfy both those terms and this
+License would be to refrain entirely from conveying the Program.
+
+ 13. Use with the GNU Affero General Public License.
+
+ Notwithstanding any other provision of this License, you have
+permission to link or combine any covered work with a work licensed
+under version 3 of the GNU Affero General Public License into a single
+combined work, and to convey the resulting work. The terms of this
+License will continue to apply to the part which is the covered work,
+but the special requirements of the GNU Affero General Public License,
+section 13, concerning interaction through a network will apply to the
+combination as such.
+
+ 14. Revised Versions of this License.
+
+ The Free Software Foundation may publish revised and/or new versions of
+the GNU General Public License from time to time. Such new versions will
+be similar in spirit to the present version, but may differ in detail to
+address new problems or concerns.
+
+ Each version is given a distinguishing version number. If the
+Program specifies that a certain numbered version of the GNU General
+Public License "or any later version" applies to it, you have the
+option of following the terms and conditions either of that numbered
+version or of any later version published by the Free Software
+Foundation. If the Program does not specify a version number of the
+GNU General Public License, you may choose any version ever published
+by the Free Software Foundation.
+
+ If the Program specifies that a proxy can decide which future
+versions of the GNU General Public License can be used, that proxy's
+public statement of acceptance of a version permanently authorizes you
+to choose that version for the Program.
+
+ Later license versions may give you additional or different
+permissions. However, no additional obligations are imposed on any
+author or copyright holder as a result of your choosing to follow a
+later version.
+
+ 15. Disclaimer of Warranty.
+
+ THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
+APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
+HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
+OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
+THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
+IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
+ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
+
+ 16. Limitation of Liability.
+
+ IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
+THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
+GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
+USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
+DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
+PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
+EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGES.
+
+ 17. Interpretation of Sections 15 and 16.
+
+ If the disclaimer of warranty and limitation of liability provided
+above cannot be given local legal effect according to their terms,
+reviewing courts shall apply local law that most closely approximates
+an absolute waiver of all civil liability in connection with the
+Program, unless a warranty or assumption of liability accompanies a
+copy of the Program in return for a fee.
+
+ END OF TERMS AND CONDITIONS
+
+ How to Apply These Terms to Your New Programs
+
+ If you develop a new program, and you want it to be of the greatest
+possible use to the public, the best way to achieve this is to make it
+free software which everyone can redistribute and change under these terms.
+
+ To do so, attach the following notices to the program. It is safest
+to attach them to the start of each source file to most effectively
+state the exclusion of warranty; and each file should have at least
+the "copyright" line and a pointer to where the full notice is found.
+
+ <one line to give the program's name and a brief idea of what it does.>
+ Copyright (C) <year> <name of author>
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+Also add information on how to contact you by electronic and paper mail.
+
+ If the program does terminal interaction, make it output a short
+notice like this when it starts in an interactive mode:
+
+ <program> Copyright (C) <year> <name of author>
+ This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
+ This is free software, and you are welcome to redistribute it
+ under certain conditions; type `show c' for details.
+
+The hypothetical commands `show w' and `show c' should show the appropriate
+parts of the General Public License. Of course, your program's commands
+might be different; for a GUI interface, you would use an "about box".
+
+ You should also get your employer (if you work as a programmer) or school,
+if any, to sign a "copyright disclaimer" for the program, if necessary.
+For more information on this, and how to apply and follow the GNU GPL, see
+<http://www.gnu.org/licenses/>.
+
+ The GNU General Public License does not permit incorporating your program
+into proprietary programs. If your program is a subroutine library, you
+may consider it more useful to permit linking proprietary applications with
+the library. If this is what you want to do, use the GNU Lesser General
+Public License instead of this License. But first, please read
+<http://www.gnu.org/philosophy/why-not-lgpl.html>.
diff --git a/composer.json b/composer.json
index f631e70..7bd5430 100644
--- a/composer.json
+++ b/composer.json
@@ -1,39 +1,39 @@
{
"name": "kolab/Net_LDAP3",
"description": "A successor of the PEAR:Net_LDAP2 module providing advanced functionality for accessing LDAP directories",
"type": "library",
"version": "1.0.2",
"keywords": ["pear", "ldap", "vlv"],
"homepage": "http://git.kolab.org/pear/Net_LDAP3/",
- "license": "GPL-3.0",
+ "license": "GPL-3.0+",
"authors": [
{
"name": "Jeroen van Meeuwen",
"email": "vanmeeuwen@kolabsys.com",
"role": "Lead"
},
{
"name": "Aleksander Machniak",
"email": "machniak@kolabsys.com",
"role": "Developer"
},
{
"name": "Thomas Bruederli",
"email": "roundcube@gmail.com",
"role": "Developer"
}
],
"repositories": [
{
"type": "pear",
"url": "http://pear.php.net/"
}
],
"require": {
"php": ">=5.3.3",
"pear-pear/Net_LDAP2": ">=2.0.12"
},
"autoload": {
"classmap": ["lib/"]
}
}
diff --git a/lib/Net/LDAP3.php b/lib/Net/LDAP3.php
index 78d497d..37a0ea3 100644
--- a/lib/Net/LDAP3.php
+++ b/lib/Net/LDAP3.php
@@ -1,2866 +1,2875 @@
<?php
/*
+-----------------------------------------------------------------------+
| Net/LDAP3.php |
| |
| Based on code created by the Roundcube Webmail team. |
| |
| Copyright (C) 2006-2014, The Roundcube Dev Team |
| Copyright (C) 2012-2014, Kolab Systems AG |
| |
- | Licensed under the GNU General Public License version 3 or |
- | any later version with exceptions for plugins. |
- | See the README file for a full license statement. |
+ | This program is free software: you can redistribute it and/or modify |
+ | it under the terms of the GNU General Public License as published by |
+ | the Free Software Foundation, either version 3 of the License, or |
+ | (at your option) any later version. |
+ | |
+ | This program is distributed in the hope that it will be useful, |
+ | but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ | GNU General Public License for more details. |
+ | |
+ | You should have received a copy of the GNU General Public License |
+ | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| |
| PURPOSE: |
| Provide advanced functionality for accessing LDAP directories |
| |
+-----------------------------------------------------------------------+
| Authors: Thomas Bruederli <roundcube@gmail.com> |
| Aleksander Machniak <machniak@kolabsys.com> |
| Jeroen van Meeuwen <vanmeeuwen@kolabsys.com> |
+-----------------------------------------------------------------------+
*/
require_once __DIR__ . '/LDAP3/Result.php';
/**
* Model class to access a LDAP directories
*
* @package Net_LDAP3
*/
class Net_LDAP3
{
public $conn;
public $vlv_active = false;
private $attribute_level_rights_map = array(
"r" => "read",
"s" => "search",
"w" => "write",
"o" => "delete",
"c" => "compare",
"W" => "write",
"O" => "delete"
);
private $entry_level_rights_map = array(
"a" => "add",
"d" => "delete",
"n" => "modrdn",
"v" => "read"
);
/*
* Manipulate configuration through the config_set and config_get methods.
* Available options:
* 'debug' => false,
* 'hosts' => array(),
* 'port' => 389,
* 'use_tls' => false,
* 'ldap_version' => 3, // using LDAPv3
* 'auth_method' => '', // SASL authentication method (for proxy auth), e.g. DIGEST-MD5
* 'numsub_filter' => '(objectClass=organizationalUnit)', // with VLV, we also use numSubOrdinates to query the total number of records. Set this filter to get all numSubOrdinates attributes for counting
* 'referrals' => false, // Sets the LDAP_OPT_REFERRALS option. Mostly used in multi-domain Active Directory setups
* 'network_timeout' => 10, // The timeout (in seconds) for connect + bind arrempts. This is only supported in PHP >= 5.3.0 with OpenLDAP 2.x
* 'sizelimit' => 0, // Enables you to limit the count of entries fetched. Setting this to 0 means no limit.
* 'timelimit' => 0, // Sets the number of seconds how long is spend on the search. Setting this to 0 means no limit.
* 'vlv' => false, // force VLV off
* 'config_root_dn' => 'cn=config', // Root DN to read config (e.g. vlv indexes) from
* 'service_bind_dn' => 'uid=kolab-service,ou=Special Users,dc=example,dc=org',
* 'service_bind_pw' => 'Welcome2KolabSystems',
* 'root_dn' => 'dc=example,dc=org',
*/
protected $config = array(
'sizelimit' => 0,
'timelimit' => 0,
);
protected $debug_level = false;
protected $list_page = 1;
protected $page_size = 10;
protected $cache;
// Use public method config_set('log_hook', $callback) to have $callback be
// call_user_func'ed instead of the local log functions.
protected $_log_hook;
// Use public method config_set('config_get_hook', $callback) to have
// $callback be call_user_func'ed instead of the local config_get function.
protected $_config_get_hook;
// Use public method config_set('config_set_hook', $callback) to have
// $callback be call_user_func'ed instead of the local config_set function.
protected $_config_set_hook;
// Not Yet Implemented
// Intended to allow hooking in for the purpose of caching.
protected $_result_hook;
// Runtime. These are not the variables you're looking for.
protected $_current_bind_dn;
protected $_current_bind_pw;
protected $_current_host;
protected $_supported_control = array();
protected $_vlv_indexes_and_searches;
/**
* Constructor
*
* @param array $config Configuration parameters that have not already
* been initialized. For configuration parameters
* that have in fact been set, use the config_set()
* method after initialization.
*/
public function __construct($config = array())
{
if (!empty($config) && is_array($config)) {
foreach ($config as $key => $value) {
if (empty($this->config[$key])) {
$setter = 'config_set_' . $key;
if (method_exists($this, $setter)) {
$this->$setter($value);
}
else if (isset($this->$key)) {
$this->$key = $value;
}
else {
$this->config[$key] = $value;
}
}
}
}
}
/**
* Add multiple entries to the directory information tree in one go.
*/
public function add_entries($entries, $attributes = array())
{
// If $entries is an associative array, it's keys are DNs and its
// values are the attributes for that DN.
//
// If $entries is a non-associative array, the attributes are expected
// to be positional in $attributes.
$result_set = array();
if (array_keys($entries) == range(0, count($entries) - 1)) {
// $entries is sequential
if (count($entries) !== count($attributes)) {
$this->_error("Wrong entry/attribute count in " . __FUNCTION__);
return false;
}
for ($i = 0; $i < count($entries); $i++) {
$result_set[$i] = $this->add_entry($entries[$i], $attributes[$i]);
}
}
else {
// $entries is associative
foreach ($entries as $entry_dn => $entry_attributes) {
if (array_keys($attributes) !== range(0, count($attributes)-1)) {
// $attributes is associative as well, let's merge these
//
// $entry_attributes takes precedence, so is in the second
// position in array_merge()
$entry_attributes = array_merge($attributes, $entry_attributes);
}
$result_set[$entry_dn] = $this->add_entry($entry_dn, $entry_attributes);
}
}
return $result_set;
}
/**
* Add an entry to the directory information tree.
*/
public function add_entry($entry_dn, $attributes)
{
// TODO:
// - Get entry rdn attribute value from entry_dn and see if it exists in
// attributes -> issue warning if so (but not block the operation).
$this->_debug("Entry DN", $entry_dn);
$this->_debug("Attributes", $attributes);
foreach ($attributes as $attr_name => $attr_value) {
if (empty($attr_value)) {
unset($attributes[$attr_name]);
}
}
$this->_debug("C: Add $entry_dn: " . json_encode($attributes));
if (($add_result = ldap_add($this->conn, $entry_dn, $attributes)) == false) {
$this->_debug("S: " . ldap_error($this->conn));
$this->_debug("S: Adding entry $entry_dn failed. " . ldap_error($this->conn));
return false;
}
$this->_debug("LDAP: S: OK");
return true;
}
/**
* Add replication agreements and initialize the consumer(s) for
* $domain_root_dn.
*
* Searches the configured replicas for any of the current domain/config
* databases, and uses this information to configure the additional
* replication for the (new) domain database (at $domain_root_dn).
*
* Very specific to Netscape-based directory servers, and currently also
* very specific to multi-master replication.
*/
public function add_replication_agreements($domain_root_dn)
{
$replica_hosts = $this->list_replicas();
if (empty($replica_hosts)) {
return;
}
$result = $this->search($this->config_get('config_root_dn'), "(&(objectclass=nsDS5Replica)(nsDS5ReplicaType=3))", "sub");
if (!$result) {
$this->_debug("No replication configuration found.");
return;
}
// Preserve the number of replicated databases we have, because the replication ID
// can be calculated from the number of databases replicated, and the number of
// servers.
$num_replica_dbs = $result->count();
$replicas = $result->entries(true);
$max_replica_agreements = 0;
foreach ($replicas as $replica_dn => $replica_attrs) {
$result = $this->search($replica_dn, "(objectclass=nsDS5ReplicationAgreement)", "sub");
if ($result) {
if ($max_replica_agreements < $result->count()) {
$max_replica_agreements = $result->count();
$max_replica_agreements_dn = $replica_dn;
}
}
}
$max_repl_id = $num_replica_dbs * count($replica_hosts);
$this->_debug("The current maximum replication ID is $max_repl_id");
$this->_debug("The current maximum number of replication agreements for any database is $max_replica_agreements (for $max_replica_agreements_dn)");
$this->_debug("With " . count($replica_hosts) . " replicas, the next is " . ($max_repl_id + 1) . " and the last one is " . ($max_repl_id + count($replica_hosts)));
// Then add the replication agreements
foreach ($replica_hosts as $num => $replica_host) {
$ldap = new Net_LDAP3($this->config);
$ldap->config_set('hosts', array($replica_host));
$ldap->connect();
$ldap->bind($this->_current_bind_dn, $this->_current_bind_pw);
$replica_attrs = array(
'cn' => 'replica',
'objectclass' => array(
'top',
'nsds5replica',
'extensibleobject',
),
'nsDS5ReplicaBindDN' => $ldap->get_entry_attribute($replica_dn, "nsDS5ReplicaBindDN"),
'nsDS5ReplicaId' => ($max_repl_id + $num + 1),
'nsDS5ReplicaRoot' => $domain_root_dn,
'nsDS5ReplicaType' => $ldap->get_entry_attribute($replica_dn, "nsDS5ReplicaType"),
'nsds5ReplicaPurgeDelay' => $ldap->get_entry_attribute($replica_dn, "nsds5ReplicaPurgeDelay"),
'nsDS5Flags' => $ldap->get_entry_attribute($replica_dn, "nsDS5Flags")
);
$new_replica_dn = 'cn=replica,cn="' . $domain_root_dn . '",cn=mapping tree,cn=config';
$this->_debug("Adding $new_replica_dn to $replica_host with attributes: " . var_export($replica_attrs, true));
$result = $ldap->add_entry($new_replica_dn, $replica_attrs);
if (!$result) {
$this->_error("Could not add replication configuration to database for $domain_root_dn on $replica_host");
continue;
}
$result = $ldap->search($replica_dn, "(objectclass=nsDS5ReplicationAgreement)", "sub");
if (!$result) {
$this->_error("Host $replica_host does not have any replication agreements");
continue;
}
$entries = $result->entries(true);
$replica_agreement_tpl_dn = key($entries);
$this->_debug("Using " . var_export($replica_agreement_tpl_dn, true) . " as the template for new replication agreements");
foreach ($replica_hosts as $replicate_to_host) {
// Skip the current server
if ($replicate_to_host == $replica_host) {
continue;
}
$this->_debug("Adding a replication agreement for $domain_root_dn to $replicate_to_host on " . $replica_host);
$attrs = array(
'objectclass',
'nsDS5ReplicaBindDN',
'nsDS5ReplicaCredentials',
'nsDS5ReplicaTransportInfo',
'nsDS5ReplicaBindMethod',
'nsDS5ReplicaHost',
'nsDS5ReplicaPort'
);
$replica_agreement_attrs = $ldap->get_entry_attributes($replica_agreement_tpl_dn, $attrs);
$replica_agreement_attrs['cn'] = array_shift(explode('.', $replicate_to_host)) . str_replace(array('dc=',','), array('_',''), $domain_root_dn);
$replica_agreement_attrs['nsDS5ReplicaRoot'] = $domain_root_dn;
$replica_agreement_dn = "cn=" . $replica_agreement_attrs['cn'] . "," . $new_replica_dn;
$this->_debug("Adding $replica_agreement_dn to $replica_host with attributes: " . var_export($replica_agreement_attrs, true));
$result = $ldap->add_entry($replica_agreement_dn, $replica_agreement_attrs);
if (!$result) {
$this->_error("Failed adding $replica_agreement_dn");
}
}
}
$server_id = implode('', array_diff($replica_hosts, $this->_server_id_not));
$this->_debug("About to trigger consumer initialization for replicas on current 'parent': $server_id");
$result = $this->search($this->config_get('config_root_dn'), "(&(objectclass=nsDS5ReplicationAgreement)(nsds5replicaroot=$domain_root_dn))", "sub");
if ($result) {
foreach ($result->entries(true) as $agreement_dn => $agreement_attrs) {
$this->modify_entry_attributes(
$agreement_dn,
array(
'replace' => array(
'nsds5BeginReplicaRefresh' => 'start',
),
)
);
}
}
}
public function attribute_details($attributes = array())
{
$schema = $this->init_schema();
if (!$schema) {
return array();
}
$attribs = $schema->getAll('attributes');
$attributes_details = array();
foreach ($attributes as $attribute) {
if (array_key_exists($attribute, $attribs)) {
$attrib_details = $attribs[$attribute];
if (!empty($attrib_details['sup'])) {
foreach ($attrib_details['sup'] as $super_attrib) {
$_attrib_details = $attribs[$super_attrib];
if (is_array($_attrib_details)) {
$attrib_details = array_merge($_attrib_details, $attrib_details);
}
}
}
}
else if (array_key_exists(strtolower($attribute), $attribs)) {
$attrib_details = $attribs[strtolower($attribute)];
if (!empty($attrib_details['sup'])) {
foreach ($attrib_details['sup'] as $super_attrib) {
$_attrib_details = $attribs[$super_attrib];
if (is_array($_attrib_details)) {
$attrib_details = array_merge($_attrib_details, $attrib_details);
}
}
}
}
else {
$this->_warning("LDAP: No schema details exist for attribute $attribute (which is strange)");
}
// The relevant parts only, please
$attributes_details[$attribute] = array(
'type' => !empty($attrib_details['single-value']) ? 'text' : 'list',
'description' => $attrib_details['desc'],
'syntax' => $attrib_details['syntax'],
'max-length' => $attrib_details['max-length'] ?: false,
);
}
return $attributes_details;
}
public function attributes_allowed($objectclasses = array())
{
$this->_debug("Listing allowed_attributes for objectclasses", $objectclasses);
if (!is_array($objectclasses) || empty($objectclasses)) {
return false;
}
$schema = $this->init_schema();
if (!$schema) {
return false;
}
$may = array();
$must = array();
$superclasses = array();
foreach ($objectclasses as $objectclass) {
$superclass = $schema->superclass($objectclass);
if (!empty($superclass)) {
$superclasses = array_merge($superclass, $superclasses);
}
$_may = $schema->may($objectclass);
$_must = $schema->must($objectclass);
if (is_array($_may)) {
$may = array_merge($may, $_may);
}
if (is_array($_must)) {
$must = array_merge($must, $_must);
}
}
$may = array_unique($may);
$must = array_unique($must);
$superclasses = array_unique($superclasses);
return array('may' => $may, 'must' => $must, 'super' => $superclasses);
}
public function classes_allowed()
{
$schema = $this->init_schema();
if (!$schema) {
return false;
}
$list = $schema->getAll('objectclasses');
$classes = array();
foreach ($list as $class) {
$classes[] = $class['name'];
}
return $classes;
}
/**
* Bind connection with DN and password
*
* @param string $dn Bind DN
* @param string $pass Bind password
*
* @return boolean True on success, False on error
*/
public function bind($bind_dn, $bind_pw)
{
if (!$this->conn) {
return false;
}
if ($bind_dn == $this->_current_bind_dn) {
return true;
}
$this->_debug("C: Bind [dn: $bind_dn]");
if (@ldap_bind($this->conn, $bind_dn, $bind_pw)) {
$this->_debug("S: OK");
$this->_current_bind_dn = $bind_dn;
$this->_current_bind_pw = $bind_pw;
return true;
}
$this->_debug("S: ".ldap_error($this->conn));
$this->_error("Bind failed for dn=$bind_dn: ".ldap_error($this->conn));
return false;
}
/**
* Close connection to LDAP server
*/
public function close()
{
if ($this->conn) {
$this->_debug("C: Close");
ldap_unbind($this->conn);
$this->_current_bind_dn = null;
$this->_current_bind_pw = null;
$this->conn = null;
}
}
/**
* Get the value of a configuration item.
*
* @param string $key Configuration key
* @param mixed $default Default value to return
*/
public function config_get($key, $default = null)
{
if (!empty($this->_config_get_hook)) {
return call_user_func_array($this->_config_get_hook, array($key, $value));
}
else if (method_exists($this, "config_get_{$key}")) {
return call_user_func(array($this, "config_get_$key"), $value);
}
else if (!isset($this->config[$key])) {
return $default;
}
else {
return $this->config[$key];
}
}
/**
* Set a configuration item to value.
*
* @param string $key Configuration key
* @param mixed $value Configuration value
*/
public function config_set($key, $value = null)
{
if (is_array($key)) {
foreach ($key as $k => $v) {
$this->config_set($k, $v);
}
return;
}
if (!empty($this->_config_set_hook)) {
call_user_func($this->_config_set_hook, array($key, $value));
}
else if (method_exists($this, "config_set_{$key}")) {
call_user_func_array(array($this, "config_set_$key"), array($value));
}
else if (isset($this->$key)) {
$this->$key = $value;
}
else {
// 'host' option is deprecated
if ($key == 'host') {
$this->config['hosts'] = (array) $value;
}
else {
$this->config[$key] = $value;
}
}
}
/**
* Establish a connection to the LDAP server
*/
public function connect($host = null)
{
if (!function_exists('ldap_connect')) {
$this->_error("No ldap support in this PHP installation");
return false;
}
if (is_resource($this->conn)) {
$this->_debug("Connection already exists");
return true;
}
$hosts = !empty($host) ? $host : $this->config_get('hosts', array());
$port = $this->config_get('port', 389);
foreach ((array) $hosts as $host) {
$this->_debug("C: Connect [$host:$port]");
if ($lc = @ldap_connect($host, $port)) {
if ($this->config_get('use_tls', false) === true) {
if (!ldap_start_tls($lc)) {
$this->_debug("S: Could not start TLS. " . ldap_error($lc));
continue;
}
}
$this->_debug("S: OK");
$ldap_version = $this->config_get('ldap_version', 3);
$timeout = $this->config_get('network_timeout');
$referrals = $this->config_get('referrals');
ldap_set_option($lc, LDAP_OPT_PROTOCOL_VERSION, $ldap_version);
if ($timeout) {
ldap_set_option($lc, LDAP_OPT_NETWORK_TIMEOUT, $timeout);
}
if ($referrals !== null) {
ldap_set_option($lc, LDAP_OPT_REFERRALS, (bool) $referrals);
}
$this->_current_host = $host;
$this->conn = $lc;
break;
}
$this->_debug("S: NOT OK");
}
if (!is_resource($this->conn)) {
$this->_error("Could not connect to LDAP");
return false;
}
return true;
}
/**
* Shortcut to ldap_delete()
*/
public function delete_entry($entry_dn)
{
$this->_debug("LDAP: C: Delete $entry_dn");
if (ldap_delete($this->conn, $entry_dn) === false) {
$this->_debug("LDAP: S: " . ldap_error($this->conn));
return false;
}
$this->_debug("LDAP: S: OK");
return true;
}
/**
* Deletes specified entry and all entries in the tree
*/
public function delete_entry_recursive($entry_dn)
{
// searching for sub entries, but not scope sub, just one level
$result = $this->search($entry_dn, '(objectclass=*)', 'one');
if ($result) {
$entries = $result->entries(true);
foreach (array_keys($entries) as $sub_dn) {
if (!$this->delete_entry_recursive($sub_dn)) {
return false;
}
}
}
if (!$this->delete_entry($entry_dn)) {
return false;
}
return true;
}
public function effective_rights($subject)
{
$effective_rights_control_oid = "1.3.6.1.4.1.42.2.27.9.5.2";
$supported_controls = $this->supported_controls();
if (!in_array($effective_rights_control_oid, $supported_controls)) {
$this->_debug("LDAP: No getEffectiveRights control in supportedControls");
return false;
}
$attributes = array(
'attributeLevelRights' => array(),
'entryLevelRights' => array(),
);
$entry_dn = $this->entry_dn($subject);
if (!$entry_dn) {
$entry_dn = $this->config_get($subject . "_base_dn");
}
if (!$entry_dn) {
$entry_dn = $this->config_get("base_dn");
}
if (!$entry_dn) {
$entry_dn = $this->config_get("root_dn");
}
$this->_debug("effective_rights for subject $subject resolves to entry dn $entry_dn");
$moz_ldapsearch = "/usr/lib64/mozldap/ldapsearch";
if (!is_file($moz_ldapsearch)) {
$moz_ldapsearch = "/usr/lib/mozldap/ldapsearch";
}
if (!is_file($moz_ldapsearch)) {
$moz_ldapsearch = null;
}
if (empty($moz_ldapsearch)) {
$this->_error("Mozilla LDAP C SDK binary ldapsearch not found, cannot get effective rights on subject $subject");
return null;
}
$output = array();
$command = Array(
$moz_ldapsearch,
'-x',
'-h',
$this->_current_host,
'-p',
$this->config_get('port', 389),
'-b',
escapeshellarg($entry_dn),
'-s',
'base',
'-D',
escapeshellarg($this->_current_bind_dn),
'-w',
escapeshellarg($this->_current_bind_pw)
);
if ($this->vendor_name() == "Oracle Corporation") {
// For Oracle DSEE
$command[] = "-J";
$command[] = escapeshellarg(
implode(
':',
Array(
$effective_rights_control_oid, // OID
'true' // Criticality
)
)
);
$command[] = "-c";
$command[] = escapeshellarg(
'dn:' . $this->_current_bind_dn
);
} else {
// For 389 DS:
$command[] = "-J";
$command[] = escapeshellarg(
implode(
':',
Array(
$effective_rights_control_oid, // OID
'true', // Criticality
'dn:' . $this->_current_bind_dn // User DN
)
)
);
}
// For both
$command[] = '"(objectclass=*)"';
$command[] = '"*"';
if ($this->vendor_name() == "Oracle Corporation") {
// Oracle DSEE
$command[] = 'aclRights';
}
// remove password from debug log
$command_debug = $command;
$command_debug[13] = '*';
$command = implode(' ', $command);
$command_debug = implode(' ', $command_debug);
$this->_debug("LDAP: Executing command: $command_debug");
exec($command, $output, $return_code);
$this->_debug("LDAP: Command output:" . var_export($output, true));
$this->_debug("Return code: " . $return_code);
if ($return_code) {
$this->_error("Command $moz_ldapsearch returned error code: $return_code");
return null;
}
$lines = array();
foreach ($output as $line_num => $line) {
if (substr($line, 0, 1) == " ") {
$lines[count($lines)-1] .= trim($line);
}
else {
$lines[] = trim($line);
}
}
if ($this->vendor_name() == "Oracle Corporation") {
// Example for attribute level rights:
// aclRights;attributeLevel;$attr:$right:$bool,$right:$bool
// Example for entry level rights:
// aclRights;entryLevel: add:1,delete:1,read:1,write:1,proxy:1
foreach ($lines as $line) {
$line_components = explode(':', $line);
$attribute_name = explode(';', array_shift($line_components));
switch ($attribute_name[0]) {
case "aclRights":
$this->parse_aclrights($attributes, $line);
break;
case "dn":
$attributes[$attribute_name[0]] = trim(implode(';', $line_components));
break;
default:
break;
}
}
} else {
foreach ($lines as $line) {
$line_components = explode(':', $line);
$attribute_name = array_shift($line_components);
$attribute_value = trim(implode(':', $line_components));
switch ($attribute_name) {
case "attributeLevelRights":
$attributes[$attribute_name] = $this->parse_attribute_level_rights($attribute_value);
break;
case "dn":
$attributes[$attribute_name] = $attribute_value;
break;
case "entryLevelRights":
$attributes[$attribute_name] = $this->parse_entry_level_rights($attribute_value);
break;
default:
break;
}
}
}
return $attributes;
}
/**
* Resolve entry data to entry DN
*
* @param string $subject Entry string (e.g. entry DN or unique attribute value)
* @param array $attributes Additional attributes
* @param string $base_dn Optional base DN
*
* @return string Entry DN string
*/
public function entry_dn($subject, $attributes = array(), $base_dn = null)
{
$this->_debug("entry_dn on subject $subject");
$is_dn = ldap_explode_dn($subject, 1);
if (is_array($is_dn) && array_key_exists("count", $is_dn) && $is_dn["count"] > 0) {
$this->_debug("$subject is a dn");
return $subject;
}
$this->_debug("$subject is not a dn");
if (strlen($subject) < 32 || preg_match('/[^a-fA-F0-9-]/', $subject)) {
$this->_debug("$subject is not a unique identifier");
return;
}
$unique_attr = $this->config_get('unique_attribute', 'nsuniqueid');
$this->_debug("Using unique_attribute " . var_export($unique_attr, true) . " at " . __FILE__ . ":" . __LINE__);
$attributes = array_merge(array($unique_attr => $subject), (array)$attributes);
$subject = $this->entry_find_by_attribute($attributes, $base_dn);
if (!empty($subject)) {
return key($subject);
}
}
public function entry_find_by_attribute($attributes, $base_dn = null)
{
$this->_debug("Net_LDAP3::entry_find_by_attribute(\$attributes, \$base_dn) called with base_dn", $base_dn, "and attributes", $attributes);
if (empty($attributes) || !is_array($attributes)) {
return false;
}
if (empty($attributes[key($attributes)])) {
return false;
}
$filter = count($attributes) ? "(&" : "";
foreach ($attributes as $key => $value) {
$filter .= "(" . $key . "=" . $value . ")";
}
$filter .= count($attributes) ? ")" : "";
if (empty($base_dn)) {
$base_dn = $this->config_get('root_dn');
$this->_debug("Using base_dn from domain " . $this->domain . ": " . $base_dn);
}
$result = $this->search($base_dn, $filter, 'sub', array_keys($attributes));
if ($result && $result->count() > 0) {
$this->_debug("Results found: " . implode(', ', array_keys($result->entries(true))));
return $result->entries(true);
}
else {
$this->_debug("No result");
return false;
}
}
public function find_user_groups($member_dn)
{
$groups = array();
$root_dn = $this->config_get('root_dn');
// TODO: Do not query for both, it's either one or the other
$entries = $this->search($root_dn, "(|" .
"(&(objectclass=groupofnames)(member=$member_dn))" .
"(&(objectclass=groupofuniquenames)(uniquemember=$member_dn))" .
")"
);
if ($entries) {
$groups = array_keys($entries->entries(true));
}
return $groups;
}
public function get_entry_attribute($subject_dn, $attribute)
{
$entry = $this->get_entry_attributes($subject_dn, (array)$attribute);
return $entry[strtolower($attribute)];
}
public function get_entry_attributes($subject_dn, $attributes)
{
// @TODO: use get_entry?
$result = $this->search($subject_dn, '(objectclass=*)', 'base', $attributes);
if (!$result) {
return array();
}
$entries = $result->entries(true);
$entry_dn = key($entries);
$entry = $entries[$entry_dn];
return $entry;
}
/**
* Get a specific LDAP entry, identified by its DN
*
* @param string $dn Record identifier
* @param array $attributes Attributes to return
*
* @return array Hash array
*/
public function get_entry($dn, $attributes = array())
{
$rec = null;
if ($this->conn && $dn) {
$this->_debug("C: Read [dn: $dn] [(objectclass=*)]");
if ($ldap_result = @ldap_read($this->conn, $dn, '(objectclass=*)', $attributes)) {
$this->_debug("S: OK");
if ($entry = ldap_first_entry($this->conn, $ldap_result)) {
$rec = ldap_get_attributes($this->conn, $entry);
}
}
else {
$this->_debug("S: ".ldap_error($this->conn));
}
if (!empty($rec)) {
$rec['dn'] = $dn; // Add in the dn for the entry.
}
}
return $rec;
}
public function list_replicas()
{
$this->_debug("Finding replicas for this server.");
// Search any host that is a replica for the current host
$replica_hosts = $this->config_get('replica_hosts', array());
$root_dn = $this->config_get('config_root_dn');
if (!empty($replica_hosts)) {
return $replica_hosts;
}
$ldap = new Net_LDAP3($this->config);
$ldap->connect();
$ldap->bind($this->_current_bind_dn, $this->_current_bind_pw);
$result = $ldap->search($root_dn, '(objectclass=nsds5replicationagreement)', 'sub', array('nsds5replicahost'));
if (!$result) {
$this->_debug("No replicas configured");
return $replica_hosts;
}
$this->_debug("Replication agreements found: " . var_export($result->entries(true), true));
foreach ($result->entries(true) as $dn => $attrs) {
if (!in_array($attrs['nsds5replicahost'], $replica_hosts)) {
$replica_hosts[] = $attrs['nsds5replicahost'];
}
}
// $replica_hosts now holds the IDs of servers we are currently NOT
// connected to. We might need this later in order to set
$this->_server_id_not = $replica_hosts;
$this->_debug("So far, we have the following replicas: " . var_export($replica_hosts, true));
$ldap->close();
foreach ($replica_hosts as $replica_host) {
$ldap->config_set('hosts', array($replica_host));
$ldap->connect();
$ldap->bind($this->_current_bind_dn, $this->_current_bind_pw);
$result = $ldap->search($root_dn, '(objectclass=nsds5replicationagreement)', 'sub', array('nsds5replicahost'));
if (!$result) {
$this->_debug("No replicas configured on $replica_host");
$ldap->close();
continue;
}
foreach ($result->entries(true) as $dn => $attrs) {
if (!in_array($attrs['nsds5replicahost'], $replica_hosts)) {
$replica_hosts[] = $attrs['nsds5replicahost'];
}
}
$ldap->close();
}
$this->config_set('replica_hosts', $replica_hosts);
return $replica_hosts;
}
public function login($username, $password, $domain = null, &$attributes = null)
{
$this->_debug("Net_LDAP3::login(\$username = '" . $username . "', \$password = '****', \$domain = '" . $domain . "')");
$_bind_dn = $this->config_get('service_bind_dn');
$_bind_pw = $this->config_get('service_bind_pw');
if (empty($_bind_dn)) {
$this->_debug("No valid service bind dn found.");
return null;
}
if (empty($_bind_pw)) {
$this->_debug("No valid service bind password found.");
return null;
}
$bound = $this->bind($_bind_dn, $_bind_pw);
if (!$bound) {
$this->_debug("Could not bind with service bind credentials.");
return null;
}
$entry_dn = $this->entry_dn($username);
if (!empty($entry_dn)) {
$bound = $this->bind($entry_dn, $password);
if (!$bound) {
$this->_error("Could not bind with " . $entry_dn);
return null;
}
// fetch user attributes if requested
if (!empty($attributes)) {
$attributes = $this->get_entry($entry_dn, $attributes);
$attributes = self::normalize_entry($attributes, true);
}
return $entry_dn;
}
$base_dn = $this->config_get('root_dn');
if (empty($base_dn)) {
$this->_debug("Could not get a valid base dn to search.");
return null;
}
$localpart = $username;
if (empty($domain) ) {
if (count(explode('@', $username)) > 1) {
$_parts = explode('@', $username);
$localpart = $_parts[0];
$domain = $_parts[1];
}
else {
$localpart = $username;
$domain = '';
}
}
$realm = $domain;
$filter = $this->config_get("login_filter", null);
if (empty($filter)) {
$filter = $this->config_get("filter", null);
}
if (empty($filter)) {
$filter = "(&(|(mail=%s)(mail=%U@%d)(alias=%s)(alias=%U@%d)(uid=%s))(objectclass=inetorgperson))";
}
$this->_debug("Net::LDAP3::login() original filter: " . $filter);
$replace_patterns = array(
'/%s/' => $username,
'/%d/' => $domain,
'/%U/' => $localpart,
'/%r/' => $realm
);
$filter = preg_replace(array_keys($replace_patterns), array_values($replace_patterns), $filter);
$this->_debug("Net::LDAP3::login() actual filter: " . $filter);
$result = $this->search($base_dn, $filter, 'sub', $attributes);
if (!$result) {
$this->_debug("Could not search $base_dn with $filter");
return null;
}
if ($result->count() > 1) {
$this->_debug("Multiple entries found.");
return null;
}
else if ($result->count() < 1) {
$this->_debug("No entries found.");
return null;
}
$entries = $result->entries(true);
$entry_dn = key($entries);
$bound = $this->bind($entry_dn, $password);
if (!$bound) {
$this->_debug("Could not bind with " . $entry_dn);
return null;
}
// replace attributes list with key-value data
if (!empty($attributes)) {
$attributes = $entries[$entry_dn];
}
return $entry_dn;
}
public function list_group_members($dn, $entry = null, $recurse = true)
{
$this->_debug("Called list_group_members(" . $dn . ")");
if (is_array($entry) && in_array('objectclass', $entry)) {
if (!in_array(array('groupofnames', 'groupofuniquenames', 'groupofurls'), $entry['objectclass'])) {
$this->_debug("Called list_group_members on a non-group!");
return array();
}
}
else {
$entry = $this->get_entry($dn, array('member', 'uniquemember', 'memberurl', 'objectclass'));
if (!$entry) {
return array();
}
}
$group_members = array();
foreach ((array)$entry['objectclass'] as $objectclass) {
switch (strtolower($objectclass)) {
case "groupofnames":
case "kolabgroupofnames":
$group_members = array_merge($group_members, $this->list_group_member($dn, $entry['member'], $recurse));
break;
case "groupofuniquenames":
case "kolabgroupofuniquenames":
$group_members = array_merge($group_members, $this->list_group_uniquemember($dn, $entry['uniquemember'], $recurse));
break;
case "groupofurls":
$group_members = array_merge($group_members, $this->list_group_memberurl($dn, $entry['memberurl'], $recurse));
break;
}
}
return array_values(array_filter($group_members));
}
public function modify_entry($subject_dn, $old_attrs, $new_attrs)
{
$this->_debug("OLD ATTRIBUTES", $old_attrs);
$this->_debug("NEW ATTRIBUTES", $new_attrs);
// TODO: Get $rdn_attr - we have type_id in $new_attrs
$dn_components = ldap_explode_dn($subject_dn, 0);
$rdn_components = explode('=', $dn_components[0]);
$rdn_attr = $rdn_components[0];
$this->_debug("Net_LDAP3::modify_entry() using rdn attribute: " . $rdn_attr);
$mod_array = array(
'add' => array(), // For use with ldap_mod_add()
'del' => array(), // For use with ldap_mod_del()
'replace' => array(), // For use with ldap_mod_replace()
'rename' => array(), // For use with ldap_rename()
);
// This is me cheating. Remove this special attribute.
if (array_key_exists('ou', $old_attrs) || array_key_exists('ou', $new_attrs)) {
$old_ou = is_array($old_attrs['ou']) ? array_shift($old_attrs['ou']) : $old_attrs['ou'];
$new_ou = is_array($new_attrs['ou']) ? array_shift($new_attrs['ou']) : $new_attrs['ou'];
unset($old_attrs['ou']);
unset($new_attrs['ou']);
}
else {
$old_ou = null;
$new_ou = null;
}
// Compare each attribute value of the old attrs with the corresponding value
// in the new attrs, if any.
foreach ($old_attrs as $attr => $old_attr_value) {
if (is_array($old_attr_value)) {
if (count($old_attr_value) == 1) {
$old_attrs[$attr] = $old_attr_value[0];
$old_attr_value = $old_attrs[$attr];
}
}
if (array_key_exists($attr, $new_attrs)) {
if (is_array($new_attrs[$attr])) {
if (count($new_attrs[$attr]) == 1) {
$new_attrs[$attr] = $new_attrs[$attr][0];
}
}
if (is_array($old_attrs[$attr]) && is_array($new_attrs[$attr])) {
$_sort1 = $new_attrs[$attr];
sort($_sort1);
$_sort2 = $old_attr_value;
sort($_sort2);
}
else {
$_sort1 = true;
$_sort2 = false;
}
if ($new_attrs[$attr] !== $old_attr_value && $_sort1 !== $_sort2) {
$this->_debug("Attribute $attr changed from " . var_export($old_attr_value, true) . " to " . var_export($new_attrs[$attr], true));
if ($attr === $rdn_attr) {
$this->_debug("This attribute is the RDN attribute. Let's see if it is multi-valued, and if the original still exists in the new value.");
if (is_array($old_attrs[$attr])) {
if (!is_array($new_attrs[$attr])) {
if (in_array($new_attrs[$attr], $old_attrs[$attr])) {
// TODO: Need to remove all $old_attrs[$attr] values not equal to $new_attrs[$attr], and not equal to the current $rdn_attr value [0]
$this->_debug("old attrs. is array, new attrs. is not array. new attr. exists in old attrs.");
$rdn_attr_value = array_shift($old_attrs[$attr]);
$_attr_to_remove = array();
foreach ($old_attrs[$attr] as $value) {
if (strtolower($value) != strtolower($new_attrs[$attr])) {
$_attr_to_remove[] = $value;
}
}
$this->_debug("Adding to delete attribute $attr values:" . implode(', ', $_attr_to_remove));
$mod_array['del'][$attr] = $_attr_to_remove;
if (strtolower($new_attrs[$attr]) !== strtolower($rdn_attr_value)) {
$this->_debug("new attrs is not the same as the old rdn value, issuing a rename");
$mod_array['rename']['dn'] = $subject_dn;
$mod_array['rename']['new_rdn'] = $rdn_attr . '=' . self::quote_string($new_attrs[$attr], true);
}
}
else {
$this->_debug("new attrs is not the same as any of the old rdn value, issuing a full rename");
$mod_array['rename']['dn'] = $subject_dn;
$mod_array['rename']['new_rdn'] = $rdn_attr . '=' . self::quote_string($new_attrs[$attr], true);
}
}
else {
// TODO: See if the rdn attr. value is still in $new_attrs[$attr]
if (in_array($old_attrs[$attr][0], $new_attrs[$attr])) {
$this->_debug("Simply replacing attr $attr as rdn attr value is preserved.");
$mod_array['replace'][$attr] = $new_attrs[$attr];
}
else {
// TODO: This fails.
$mod_array['rename']['dn'] = $subject_dn;
$mod_array['rename']['new_rdn'] = $rdn_attr . '=' . self::quote_string($new_attrs[$attr][0], true);
$mod_array['del'][$attr] = $old_attrs[$attr][0];
}
}
}
else {
if (!is_array($new_attrs[$attr])) {
$this->_debug("Renaming " . $old_attrs[$attr] . " to " . $new_attrs[$attr]);
$mod_array['rename']['dn'] = $subject_dn;
$mod_array['rename']['new_rdn'] = $rdn_attr . '=' . self::quote_string($new_attrs[$attr], true);
}
else {
$this->_debug("Adding to replace");
// An additional attribute value is being supplied. Just replace and continue.
$mod_array['replace'][$attr] = $new_attrs[$attr];
continue;
}
}
}
else {
if (!isset($new_attrs[$attr]) || $new_attrs[$attr] === '' || (is_array($new_attrs[$attr]) && empty($new_attrs[$attr]))) {
switch ($attr) {
case "userpassword":
break;
default:
$this->_debug("Adding to del: $attr");
$mod_array['del'][$attr] = (array)($old_attr_value);
break;
}
}
else {
$this->_debug("Adding to replace: $attr");
$mod_array['replace'][$attr] = (array)($new_attrs[$attr]);
}
}
}
else {
$this->_debug("Attribute $attr unchanged");
}
}
else {
// TODO: Since we're not shipping the entire object back and forth, and only post
// part of the data... we don't know what is actually removed (think modifiedtimestamp, etc.)
$this->_debug("Group attribute $attr not mentioned in \$new_attrs..., but not explicitly removed... by assumption");
}
}
foreach ($new_attrs as $attr => $value) {
// OU's parent base dn
if ($attr == 'base_dn') {
continue;
}
if (is_array($value)) {
if (count($value) == 1) {
$new_attrs[$attr] = $value[0];
$value = $new_attrs[$attr];
}
}
if (array_key_exists($attr, $old_attrs)) {
if (is_array($old_attrs[$attr])) {
if (count($old_attrs[$attr]) == 1) {
$old_attrs[$attr] = $old_attrs[$attr][0];
}
}
if (is_array($new_attrs[$attr]) && is_array($old_attrs[$attr])) {
$_sort1 = $old_attrs[$attr];
sort($_sort1);
$_sort2 = $value;
sort($_sort2);
}
else {
$_sort1 = true;
$_sort2 = false;
}
if ($value === null || $value === '' || (is_array($value) && empty($value))) {
if (!array_key_exists($attr, $mod_array['del'])) {
switch ($attr) {
case 'userpassword':
break;
default:
$this->_debug("Adding to del(2): $attr");
$mod_array['del'][$attr] = (array)($old_attrs[$attr]);
break;
}
}
}
else {
if (!($old_attrs[$attr] === $value) && !($attr === $rdn_attr) && !($_sort1 === $_sort2)) {
if (!array_key_exists($attr, $mod_array['replace'])) {
$this->_debug("Adding to replace(2): $attr");
$mod_array['replace'][$attr] = $value;
}
}
}
}
else {
if (!empty($value)) {
$mod_array['add'][$attr] = $value;
}
}
}
if (empty($old_ou)) {
$subject_dn_components = ldap_explode_dn($subject_dn, 0);
unset($subject_dn_components["count"]);
$subject_rdn = array_shift($subject_dn_components);
$old_ou = implode(',', $subject_dn_components);
}
$subject_dn = self::unified_dn($subject_dn);
$prefix = self::unified_dn('ou=' . $old_ou) . ',';
// object is an organizational unit
if (strpos($subject_dn, $prefix) === 0) {
$root = substr($subject_dn, strlen($prefix)); // remove ou=*,
if ((!empty($new_attrs['base_dn']) && strtolower($new_attrs['base_dn']) !== strtolower($root))
|| (strtolower($old_ou) !== strtolower($new_ou))
) {
if (!empty($new_attrs['base_dn'])) {
$root = $new_attrs['base_dn'];
}
$mod_array['rename']['new_parent'] = $root;
$mod_array['rename']['dn'] = $subject_dn;
$mod_array['rename']['new_rdn'] = 'ou=' . self::quote_string($new_ou, true);
}
}
// not OU object, but changed ou attribute
else if (!empty($old_ou) && !empty($new_ou)) {
// unify DN strings for comparison
$old_ou = self::unified_dn($old_ou);
$new_ou = self::unified_dn($new_ou);
if (strtolower($old_ou) !== strtolower($new_ou)) {
$mod_array['rename']['new_parent'] = $new_ou;
if (empty($mod_array['rename']['dn']) || empty($mod_array['rename']['new_rdn'])) {
$rdn_attr_value = self::quote_string($new_attrs[$rdn_attr], true);
$mod_array['rename']['dn'] = $subject_dn;
$mod_array['rename']['new_rdn'] = $rdn_attr . '=' . $rdn_attr_value;
}
}
}
$this->_debug($mod_array);
$result = $this->modify_entry_attributes($subject_dn, $mod_array);
if ($result) {
return $mod_array;
}
}
/**
* Bind connection with (SASL-) user and password
*
* @param string $authc Authentication user
* @param string $pass Bind password
* @param string $authz Autorization user
*
* @return boolean True on success, False on error
*/
public function sasl_bind($authc, $pass, $authz=null)
{
if (!$this->conn) {
return false;
}
if (!function_exists('ldap_sasl_bind')) {
$this->_error("Unable to bind: ldap_sasl_bind() not exists");
return false;
}
if (!empty($authz)) {
$authz = 'u:' . $authz;
}
$method = $this->config_get('auth_method');
if (empty($method)) {
$method = 'DIGEST-MD5';
}
$this->_debug("C: Bind [mech: $method, authc: $authc, authz: $authz]");
if (ldap_sasl_bind($this->conn, null, $pass, $method, null, $authc, $authz)) {
$this->_debug("S: OK");
return true;
}
$this->_debug("S: ".ldap_error($this->conn));
$this->_error("Bind failed for authcid=$authc ".ldap_error($this->conn));
return false;
}
/**
* Execute LDAP search
*
* @param string $base_dn Base DN to use for searching
* @param string $filter Filter string to query
* @param string $scope The LDAP scope (list|sub|base)
* @param array $attrs List of entry attributes to read
* @param array $prop Hash array with query configuration properties:
* - sort: array of sort attributes (has to be in sync with the VLV index)
* - search: search string used for VLV controls
* @param bool $count_only Set to true if only entry count is requested
*
* @return mixed Net_LDAP3_Result object or number of entries (if $count_only=true) or False on failure
*/
public function search($base_dn, $filter = '(objectclass=*)', $scope = 'sub', $attrs = array('dn'), $props = array(), $count_only = false)
{
if (!$this->conn) {
$this->_debug("No active connection for " . __CLASS__ . "::" . __FUNCTION__);
return false;
}
// make sure attributes list is not empty
if (empty($attrs)) {
$attrs = array('dn');
}
// make sure filter is not empty
if (empty($filter)) {
$filter = '(objectclass=*)';
}
$this->_debug("C: Search base dn: [$base_dn] scope [$scope] with filter [$filter]");
$function = self::scope_to_function($scope, $ns_function);
if (!$count_only && ($sort = $this->find_vlv($base_dn, $filter, $scope, $props['sort']))) {
// when using VLV, we get the total count by...
// ...either reading numSubOrdinates attribute
if (($sub_filter = $this->config_get('numsub_filter')) &&
($result_count = @$ns_function($this->conn, $base_dn, $sub_filter, array('numSubOrdinates'), 0, 0, 0))
) {
$counts = ldap_get_entries($this->conn, $result_count);
for ($vlv_count = $j = 0; $j < $counts['count']; $j++) {
$vlv_count += $counts[$j]['numsubordinates'][0];
}
$this->_debug("D: total numsubordinates = " . $vlv_count);
}
// ...or by fetching all records dn and count them
else if (!function_exists('ldap_parse_virtuallist_control')) {
// @FIXME: this search will ignore $props['search']
$vlv_count = $this->search($base_dn, $filter, $scope, array('dn'), $props, true);
}
$this->vlv_active = $this->_vlv_set_controls($sort, $this->list_page, $this->page_size,
$this->_vlv_search($sort, $props['search']));
}
else {
$this->vlv_active = false;
}
$sizelimit = (int) $this->config['sizelimit'];
$timelimit = (int) $this->config['timelimit'];
$phplimit = (int) @ini_get('max_execution_time');
// set LDAP time limit to be (one second) less than PHP time limit
// otherwise we have no chance to log the error below
if ($phplimit && $timelimit >= $phplimit) {
$timelimit = $phplimit - 1;
}
$this->_debug("Using function $function on scope $scope (\$ns_function is $ns_function)");
if ($this->vlv_active) {
if (!empty($this->additional_filter)) {
$filter = "(&" . $filter . $this->additional_filter . ")";
$this->_debug("C: (With VLV) Setting a filter (with additional filter) of " . $filter);
}
else {
$this->_debug("C: (With VLV) Setting a filter (without additional filter) of " . $filter);
}
}
else {
if (!empty($this->additional_filter)) {
$filter = "(&" . $filter . $this->additional_filter . ")";
}
$this->_debug("C: (Without VLV) Setting a filter of " . $filter);
}
$this->_debug("Executing search with return attributes: " . var_export($attrs, true));
$ldap_result = @$function($this->conn, $base_dn, $filter, $attrs, 0, $sizelimit, $timelimit);
if (!$ldap_result) {
$this->_warning("$function failed for dn=$base_dn: ".ldap_error($this->conn));
return false;
}
// when running on a patched PHP we can use the extended functions
// to retrieve the total count from the LDAP search result
if ($this->vlv_active && function_exists('ldap_parse_virtuallist_control')) {
if (ldap_parse_result($this->conn, $ldap_result, $errcode, $matcheddn, $errmsg, $referrals, $serverctrls)) {
ldap_parse_virtuallist_control($this->conn, $serverctrls, $last_offset, $vlv_count, $vresult);
$this->_debug("S: VLV result: last_offset=$last_offset; content_count=$vlv_count");
}
else {
$this->_debug("S: ".($errmsg ? $errmsg : ldap_error($this->conn)));
}
}
else {
$this->_debug("S: ".ldap_count_entries($this->conn, $ldap_result)." record(s) found");
}
$result = new Net_LDAP3_Result($this->conn, $base_dn, $filter, $scope, $ldap_result);
if (isset($last_offset)) {
$result->set('offset', $last_offset);
}
if (isset($vlv_count)) {
$result->set('count', $vlv_count);
}
$result->set('vlv', $this->vlv_active);
return $count_only ? $result->count() : $result;
}
/**
* Similar to Net_LDAP3::search() but using a search array with multiple
* keys and values that to continue to use the VLV but with an original
* filter adding the search stuff to an additional filter.
*
* @see Net_LDAP3::search()
*/
public function search_entries($base_dn, $filter = '(objectclass=*)', $scope = 'sub', $attrs = array('dn'), $props = array())
{
$this->_debug("Net_LDAP3::search_entries with search " . var_export($props, true));
if (is_array($props['search']) && array_key_exists('params', $props['search'])) {
$_search = $this->search_filter($props['search']);
$this->_debug("C: Search filter: $_search");
if (!empty($_search)) {
$this->additional_filter = $_search;
}
else {
$this->additional_filter = "(|";
foreach ($props['search'] as $attr => $value) {
$this->additional_filter .= "(" . $attr . "=" . $this->_fuzzy_search_prefix() . $value . $this->_fuzzy_search_suffix() . ")";
}
$this->additional_filter .= ")";
}
$this->_debug("C: Setting an additional filter " . $this->additional_filter);
}
$search = $this->search($base_dn, $filter, $scope, $attrs, $props);
$this->additional_filter = null;
if (!$search) {
$this->_debug("Net_LDAP3: Search did not succeed!");
return false;
}
return $search;
}
/**
* Create LDAP search filter string according to defined parameters.
*/
public function search_filter($search)
{
if (empty($search) || !is_array($search) || empty($search['params'])) {
return null;
}
$operators = array('=', '~=', '>=', '<=');
$filter = '';
foreach ((array) $search['params'] as $field => $param) {
$value = (array) $param['value'];
switch ((string)$param['type']) {
case 'prefix':
$prefix = '';
$suffix = '*';
break;
case 'suffix':
$prefix = '*';
$suffix = '';
break;
case 'exact':
case '=':
case '~=':
case '>=':
case '<=':
$prefix = '';
$suffix = '';
// this is a common query to find entry by DN, make sure
// it is a unified DN so special characters are handled correctly
if ($field == 'entrydn') {
$value = array_map(array('Net_LDAP3', 'unified_dn'), $value);
}
break;
case 'exists':
$prefix = '*';
$suffix = '';
$param['value'] = '';
break;
case 'both':
default:
$prefix = '*';
$suffix = '*';
break;
}
$operator = $param['type'] && in_array($param['type'], $operators) ? $param['type'] : '=';
if (count($value) < 2) {
$value = array_pop($value);
}
if (is_array($value)) {
$val_filter = array();
foreach ($value as $val) {
$val = self::quote_string($val);
$val_filter[] = "(" . $field . $operator . $prefix . $val . $suffix . ")";
}
$filter .= "(|" . implode($val_filter, '') . ")";
}
else {
$value = self::quote_string($value);
$filter .= "(" . $field . $operator . $prefix . $value . $suffix . ")";
}
}
// join search parameters with specified operator ('OR' or 'AND')
if (count($search['params']) > 1) {
$filter = '(' . ($search['operator'] == 'AND' ? '&' : '|') . $filter . ')';
}
return $filter;
}
/**
* Set properties for VLV-based paging
*
* @param number $page Page number to list (starting at 1)
* @param number $size Number of entries to display on one page
*/
public function set_vlv_page($page, $size = 10)
{
$this->list_page = $page;
$this->page_size = $size;
}
/**
* Turn an LDAP entry into a regular PHP array with attributes as keys.
*
* @param array $entry Attributes array as retrieved from ldap_get_attributes() or ldap_get_entries()
* @param bool $flat Convert one-element-array values into strings
*
* @return array Hash array with attributes as keys
*/
public static function normalize_entry($entry, $flat = false)
{
$rec = array();
for ($i=0; $i < $entry['count']; $i++) {
$attr = $entry[$i];
for ($j=0; $j < $entry[$attr]['count']; $j++) {
$rec[$attr][$j] = $entry[$attr][$j];
}
if ($flat && count($rec[$attr]) == 1) {
$rec[$attr] = $rec[$attr][0];
}
}
return $rec;
}
/**
* Normalize a ldap result by converting entry attribute arrays into single values
*/
public static function normalize_result($_result)
{
if (!is_array($_result)) {
return array();
}
$result = array();
for ($x = 0; $x < $_result['count']; $x++) {
$dn = $_result[$x]['dn'];
$entry = self::normalize_entry($_result[$x], true);
if (!empty($entry['objectclass'])) {
if (is_array($entry['objectclass'])) {
$entry['objectclass'] = array_map('strtolower', $entry['objectclass']);
}
else {
$entry['objectclass'] = strtolower($entry['objectclass']);
}
}
$result[$dn] = $entry;
}
return $result;
}
public static function scopeint2str($scope)
{
switch ($scope) {
case 2:
return 'sub';
case 1:
return 'one';
case 0:
return 'base';
default:
$this->_debug("Scope $scope is not a valid scope integer");
}
}
/**
* Choose the right PHP function according to scope property
*
* @param string $scope The LDAP scope (sub|base|list)
* @param string $ns_function Function to be used for numSubOrdinates queries
* @return string PHP function to be used to query directory
*/
public static function scope_to_function($scope, &$ns_function = null)
{
switch ($scope) {
case 'sub':
$function = $ns_function = 'ldap_search';
break;
case 'base':
$function = $ns_function = 'ldap_read';
break;
case 'one':
case 'list':
default:
$function = 'ldap_list';
$ns_function = 'ldap_read';
break;
}
return $function;
}
private function config_set_config_get_hook($callback)
{
$this->_config_get_hook = $callback;
}
private function config_set_config_set_hook($callback)
{
$this->_config_set_hook = $callback;
}
/**
* Sets the debug level both for this class and the ldap connection.
*/
private function config_set_debug($value)
{
$this->config['debug'] = (bool) $value;
if ((int)($value) > 0) {
ldap_set_option(null, LDAP_OPT_DEBUG_LEVEL, (int)($value));
}
}
/**
* Sets a log hook that is called with every log message in this module.
*/
private function config_set_log_hook($callback)
{
$this->_log_hook = $callback;
}
/**
* Find a matching VLV
*/
protected function find_vlv($base_dn, $filter, $scope, $sort_attrs = null)
{
if ($scope == 'base') {
return false;
}
$vlv_indexes = $this->find_vlv_indexes_and_searches();
if (empty($vlv_indexes)) {
return false;
}
$this->_debug("Existing vlv index and search information", $vlv_indexes);
$filter = strtolower($filter);
foreach ($vlv_indexes as $vlv_index) {
if (!empty($vlv_index[$base_dn])) {
$this->_debug("Found a VLV for base_dn: " . $base_dn);
if ($vlv_index[$base_dn]['filter'] == $filter) {
if ($vlv_index[$base_dn]['scope'] == $scope) {
$this->_debug("Scope and filter matches");
// Not passing any sort attributes means you don't care
if (!empty($sort_attrs)) {
$sort_attrs = array_map('strtolower', (array) $sort_attrs);
foreach ($vlv_index[$base_dn]['sort'] as $sss_config) {
$sss_config = array_map('strtolower', $sss_config);
if (count(array_intersect($sort_attrs, $sss_config)) == count($sort_attrs)) {
$this->_debug("Sorting matches");
return $sort_attrs;
}
}
$this->_debug("Sorting does not match");
}
else {
$sort = array_filter((array) $vlv_index[$base_dn]['sort'][0]);
$this->_debug("Sorting unimportant");
return $sort;
}
}
else {
$this->_debug("Scope does not match");
}
}
else {
$this->_debug("Filter does not match");
}
}
}
return false;
}
/**
* Return VLV indexes and searches including necessary configuration
* details.
*/
protected function find_vlv_indexes_and_searches()
{
// Use of Virtual List View control has been specifically disabled.
if ($this->config['vlv'] === false) {
return false;
}
// Virtual List View control has been configured in kolab.conf, for example;
//
// [ldap]
// vlv = [
// {
// 'ou=People,dc=example,dc=org': {
// 'scope': 'sub',
// 'filter': '(objectclass=inetorgperson)',
// 'sort' : [
// [
// 'displayname',
// 'sn',
// 'givenname',
// 'cn'
// ]
// ]
// }
// },
// {
// 'ou=Groups,dc=example,dc=org': {
// 'scope': 'sub',
// 'filter': '(objectclass=groupofuniquenames)',
// 'sort' : [
// [
// 'cn'
// ]
// ]
// }
// },
// ]
//
if (is_array($this->config['vlv'])) {
return $this->config['vlv'];
}
// We have done this dance before.
if ($this->_vlv_indexes_and_searches !== null) {
return $this->_vlv_indexes_and_searches;
}
$this->_vlv_indexes_and_searches = array();
$config_root_dn = $this->config_get('config_root_dn');
if (empty($config_root_dn)) {
return array();
}
if ($this->cache && ($cached_config = $this->cache->get('vlvconfig'))) {
$this->_vlv_indexes_and_searches = $cached_config;
return $this->_vlv_indexes_and_searches;
}
$this->_debug("No VLV information available yet, refreshing");
$search_filter = '(objectclass=vlvsearch)';
$search_result = ldap_search($this->conn, $config_root_dn, $search_filter, array('*'), 0, 0, 0);
if ($search_result === false) {
$this->_debug("Search for '$search_filter' on '$config_root_dn' failed:".ldap_error($this->conn));
return;
}
$vlv_searches = new Net_LDAP3_Result($this->conn, $config_root_dn, $search_filter, 'sub', $search_result);
if ($vlv_searches->count() < 1) {
$this->_debug("Empty result from search for '(objectclass=vlvsearch)' on '$config_root_dn'");
return;
}
$index_filter = '(objectclass=vlvindex)';
foreach ($vlv_searches->entries(true) as $vlv_search_dn => $vlv_search_attrs) {
// The attributes we are interested in are as follows:
$_vlv_base_dn = $vlv_search_attrs['vlvbase'];
$_vlv_scope = $vlv_search_attrs['vlvscope'];
$_vlv_filter = $vlv_search_attrs['vlvfilter'];
// Multiple indexes may exist
$index_result = ldap_search($this->conn, $vlv_search_dn, $index_filter, array('*'), 0, 0, 0);
if ($index_result === false) {
$this->_debug("Search for '$index_filter' on '$vlv_search_dn' failed:".ldap_error($this->conn));
continue;
}
$vlv_indexes = new Net_LDAP3_Result($this->conn, $vlv_search_dn, $index_filter, 'sub', $index_result);
$vlv_indexes = $vlv_indexes->entries(true);
// Reset this one for each VLV search.
$_vlv_sort = array();
foreach ($vlv_indexes as $vlv_index_dn => $vlv_index_attrs) {
$_vlv_sort[] = explode(' ', trim($vlv_index_attrs['vlvsort']));
}
$this->_vlv_indexes_and_searches[] = array(
$_vlv_base_dn => array(
'scope' => self::scopeint2str($_vlv_scope),
'filter' => strtolower($_vlv_filter),
'sort' => $_vlv_sort,
),
);
}
// cache this
if ($this->cache) {
$this->cache->set('vlvconfig', $this->_vlv_indexes_and_searches);
}
return $this->_vlv_indexes_and_searches;
}
private function init_schema()
{
// use PEAR include if autoloading failed
if (!class_exists('Net_LDAP2')) {
require_once('Net/LDAP2.php');
}
$port = $this->config_get('port', 389);
$tls = $this->config_get('use_tls', false);
foreach ((array) $this->config_get('hosts') as $host) {
$this->_debug("C: Connect [$host:$port]");
$_ldap_cfg = array(
'host' => $host,
'port' => $port,
'tls' => $tls,
'version' => 3,
'binddn' => $this->config_get('service_bind_dn'),
'bindpw' => $this->config_get('service_bind_pw')
);
$_ldap_schema_cache_cfg = array(
'path' => "/tmp/" . $host . ":" . ($port ? $port : '389') . "-Net_LDAP2_Schema.cache",
'max_age' => 86400,
);
$_ldap = Net_LDAP2::connect($_ldap_cfg);
if (!is_a($_ldap, 'Net_LDAP2_Error')) {
$this->_debug("S: OK");
break;
}
$this->_debug("S: NOT OK");
$this->_debug($_ldap->getMessage());
}
if (is_a($_ldap, 'Net_LDAP2_Error')) {
return null;
}
$_ldap_schema_cache = new Net_LDAP2_SimpleFileSchemaCache($_ldap_schema_cache_cfg);
$_ldap->registerSchemaCache($_ldap_schema_cache);
// TODO: We should learn what LDAP tech. we're running against.
// Perhaps with a scope base objectclass recognize rootdse entry
$schema_root_dn = $this->config_get('schema_root_dn');
if (!$schema_root_dn) {
$_schema = $_ldap->schema();
}
return $_schema;
}
private function list_group_member($dn, $members, $recurse = true)
{
$this->_debug("Called list_group_member(" . $dn . ")");
$members = (array) $members;
$group_members = array();
// remove possible 'count' item
unset($members['count']);
// Use the member attributes to return an array of member ldap objects
// NOTE that the member attribute is supposed to contain a DN
foreach ($members as $member) {
$member_entry = $this->get_entry($member, array('member', 'uniquemember', 'memberurl', 'objectclass'));
if (empty($member_entry)) {
continue;
}
$group_members[$member] = $member;
if ($recurse) {
// Nested groups
$group_group_members = $this->list_group_members($member, $member_entry);
if ($group_group_members) {
$group_members = array_merge($group_group_members, $group_members);
}
}
}
return array_filter($group_members);
}
private function list_group_uniquemember($dn, $uniquemembers, $recurse = true)
{
$this->_debug("Called list_group_uniquemember(" . $dn . ")", $entry);
$uniquemembers = (array)($uniquemembers);
$group_members = array();
// remove possible 'count' item
unset($uniquemembers['count']);
foreach ($uniquemembers as $member) {
$member_entry = $this->get_entry($member, array('member', 'uniquemember', 'memberurl', 'objectclass'));
if (empty($member_entry)) {
continue;
}
$group_members[$member] = $member;
if ($recurse) {
// Nested groups
$group_group_members = $this->list_group_members($member, $member_entry);
if ($group_group_members) {
$group_members = array_merge($group_group_members, $group_members);
}
}
}
return array_filter($group_members);
}
private function list_group_memberurl($dn, $memberurls, $recurse = true)
{
$this->_debug("Called list_group_memberurl(" . $dn . ")");
$group_members = array();
$memberurls = (array) $memberurls;
$attributes = array('member', 'uniquemember', 'memberurl', 'objectclass');
// remove possible 'count' item
unset($memberurls['count']);
foreach ($memberurls as $url) {
$ldap_uri = $this->parse_memberurl($url);
$result = $this->search($ldap_uri[3], $ldap_uri[6], 'sub', $attributes);
if (!$result) {
continue;
}
foreach ($result->entries(true) as $entry_dn => $_entry) {
$group_members[$entry_dn] = $entry_dn;
$this->_debug("Found " . $entry_dn);
if ($recurse) {
// Nested group
$group_group_members = $this->list_group_members($entry_dn, $_entry);
if ($group_group_members) {
$group_members = array_merge($group_members, $group_group_members);
}
}
}
}
return array_filter($group_members);
}
/**
* memberUrl attribute parser
*
* @param string $url URL string
*
* @return array URL elements
*/
private function parse_memberurl($url)
{
preg_match('/(.*):\/\/(.*)\/(.*)\?(.*)\?(.*)\?(.*)/', $url, $matches);
return $matches;
}
private function modify_entry_attributes($subject_dn, $attributes)
{
if (is_array($attributes['rename']) && !empty($attributes['rename'])) {
$olddn = $attributes['rename']['dn'];
$newrdn = $attributes['rename']['new_rdn'];
$new_parent = $attributes['rename']['new_parent'];
$this->_debug("LDAP: C: Rename $olddn to $newrdn,$new_parent");
// Note: for some reason the operation fails if RDN contains special characters
// and last argument of ldap_rename() is set to TRUE. That's why we use FALSE.
// However, we need to modify RDN attribute value later, otherwise it
// will contain an array of previous and current values
for ($i = 1; $i >= 0; $i--) {
$result = ldap_rename($this->conn, $olddn, $newrdn, $new_parent, $i == 1);
if ($result) {
break;
}
}
if ($result) {
$this->_debug("LDAP: S: OK");
if ($new_parent) {
$subject_dn = $newrdn . ',' . $new_parent;
}
else {
$old_parent_dn_components = ldap_explode_dn($olddn, 0);
unset($old_parent_dn_components["count"]);
$old_rdn = array_shift($old_parent_dn_components);
$old_parent_dn = implode(",", $old_parent_dn_components);
$subject_dn = $newrdn . ',' . $old_parent_dn;
}
// modify RDN attribute value, see note above
if (!$i && empty($attributes['replace'][$attr])) {
list($attr, $val) = explode('=', $newrdn, 2);
$attributes['replace'][$attr] = self::quote_string($val, true, true);
}
}
else {
$this->_debug("LDAP: S: " . ldap_error($this->conn));
$this->_warning("LDAP: Failed to rename $olddn to $newrdn,$new_parent");
return false;
}
}
if (is_array($attributes['replace']) && !empty($attributes['replace'])) {
$this->_debug("LDAP: C: Mod-Replace $subject_dn: " . json_encode($attributes['replace']));
$result = ldap_mod_replace($this->conn, $subject_dn, $attributes['replace']);
if ($result) {
$this->_debug("LDAP: S: OK");
}
else {
$this->_debug("LDAP: S: " . ldap_error($this->conn));
$this->_warning("LDAP: Failed to replace attributes on $subject_dn: " . json_encode($attributes['replace']));
return false;
}
}
if (is_array($attributes['del']) && !empty($attributes['del'])) {
$this->_debug("LDAP: C: Mod-Delete $subject_dn: " . json_encode($attributes['del']));
$result = ldap_mod_del($this->conn, $subject_dn, $attributes['del']);
if ($result) {
$this->_debug("LDAP: S: OK");
}
else {
$this->_debug("LDAP: S: " . ldap_error($this->conn));
$this->_warning("LDAP: Failed to delete attributes on $subject_dn: " . json_encode($attributes['del']));
return false;
}
}
if (is_array($attributes['add']) && !empty($attributes['add'])) {
$this->_debug("LDAP: C: Mod-Add $subject_dn: " . json_encode($attributes['add']));
$result = ldap_mod_add($this->conn, $subject_dn, $attributes['add']);
if ($result) {
$this->_debug("LDAP: S: OK");
}
else {
$this->_debug("LDAP: S: " . ldap_error($this->conn));
$this->_warning("LDAP: Failed to add attributes on $subject_dn: " . json_encode($attributes['add']));
return false;
}
}
return true;
}
private function parse_aclrights(&$attributes, $attribute_value) {
$components = explode(':', $attribute_value);
$_acl_target = array_shift($components);
$_acl_value = trim(implode(':', $components));
$_acl_components = explode(';', $_acl_target);
switch ($_acl_components[1]) {
case "entryLevel":
$attributes['entryLevelRights'] = Array();
$_acl_value = explode(',', $_acl_value);
foreach ($_acl_value as $right) {
list($method, $bool) = explode(':', $right);
if ($bool == "1" && !in_array($method, $attributes['entryLevelRights'])) {
$attributes['entryLevelRights'][] = $method;
}
}
break;
case "attributeLevel":
$attributes['attributeLevelRights'][$_acl_components[2]] = Array();
$_acl_value = explode(',', $_acl_value);
foreach ($_acl_value as $right) {
list($method, $bool) = explode(':', $right);
if ($bool == "1" && !in_array($method, $attributes['attributeLevelRights'][$_acl_components[2]])) {
$attributes['attributeLevelRights'][$_acl_components[2]][] = $method;
}
}
break;
default:
break;
}
}
private function parse_attribute_level_rights($attribute_value)
{
$attribute_value = str_replace(", ", ",", $attribute_value);
$attribute_values = explode(",", $attribute_value);
$attribute_value = array();
foreach ($attribute_values as $access_right) {
$access_right_components = explode(":", $access_right);
$access_attribute = strtolower(array_shift($access_right_components));
$access_value = array_shift($access_right_components);
$attribute_value[$access_attribute] = array();
for ($i = 0; $i < strlen($access_value); $i++) {
$method = $this->attribute_level_rights_map[substr($access_value, $i, 1)];
if (!in_array($method, $attribute_value[$access_attribute])) {
$attribute_value[$access_attribute][] = $method;
}
}
}
return $attribute_value;
}
private function parse_entry_level_rights($attribute_value)
{
$_attribute_value = array();
for ($i = 0; $i < strlen($attribute_value); $i++) {
$method = $this->entry_level_rights_map[substr($attribute_value, $i, 1)];
if (!in_array($method, $_attribute_value)) {
$_attribute_value[] = $method;
}
}
return $_attribute_value;
}
private function supported_controls()
{
if (!empty($this->supported_controls)) {
return $this->supported_controls;
}
$this->_info("Obtaining supported controls");
if ($result = $this->search('', '(objectclass=*)', 'base', array('supportedcontrol'))) {
$result = $result->entries(true);
$control = $result['']['supportedcontrol'];
}
else {
$control = array();
}
$this->_info("Obtained " . count($control) . " supported controls");
$this->supported_controls = $control;
return $control;
}
private function vendor_name()
{
if (!empty($this->vendor_name)) {
return $this->vendor_name;
}
$this->_info("Obtaining LDAP server vendor name");
if ($result = $this->search('', '(objectclass=*)', 'base', array('vendorname'))) {
$result = $result->entries(true);
$name = $result['']['vendorname'];
}
else {
$name = false;
}
if ($name !== false) {
$this->_info("Vendor name is $name");
} else {
$this->_info("No vendor name!");
}
$this->vendor = $name;
return $name;
}
protected function _alert()
{
$this->__log(LOG_ALERT, func_get_args());
}
protected function _critical()
{
$this->__log(LOG_CRIT, func_get_args());
}
protected function _debug()
{
$this->__log(LOG_DEBUG, func_get_args());
}
protected function _emergency()
{
$this->__log(LOG_EMERG, func_get_args());
}
protected function _error()
{
$this->__log(LOG_ERR, func_get_args());
}
protected function _info()
{
$this->__log(LOG_INFO, func_get_args());
}
protected function _notice()
{
$this->__log(LOG_NOTICE, func_get_args());
}
protected function _warning()
{
$this->__log(LOG_WARNING, func_get_args());
}
/**
* Log a message.
*/
private function __log($level, $args)
{
$msg = array();
foreach ($args as $arg) {
$msg[] = !is_string($arg) ? var_export($arg, true) : $arg;
}
if (!empty($this->_log_hook)) {
call_user_func_array($this->_log_hook, array($level, $msg));
return;
}
if ($this->debug_level > 0) {
syslog($level, implode("\n", $msg));
}
}
/**
* Add BER sequence with correct length and the given identifier
*/
private static function _ber_addseq($str, $identifier)
{
$len = dechex(strlen($str)/2);
if (strlen($len) % 2 != 0) {
$len = '0'.$len;
}
return $identifier . $len . $str;
}
/**
* Returns BER encoded integer value in hex format
*/
private static function _ber_encode_int($offset)
{
$val = dechex($offset);
$prefix = '';
// check if bit 8 of high byte is 1
if (preg_match('/^[89abcdef]/', $val)) {
$prefix = '00';
}
if (strlen($val)%2 != 0) {
$prefix .= '0';
}
return $prefix . $val;
}
/**
* Quotes attribute value string
*
* @param string $str Attribute value
* @param bool $dn True if the attribute is a DN
* @param bool $reverse Do reverse replacement
*
* @return string Quoted string
*/
public static function quote_string($str, $is_dn = false, $reverse = false)
{
// take first entry if array given
if (is_array($str)) {
$str = reset($str);
}
if ($is_dn) {
$replace = array(
',' => '\2c',
'=' => '\3d',
'+' => '\2b',
'<' => '\3c',
'>' => '\3e',
';' => '\3b',
"\\"=> '\5c',
'"' => '\22',
'#' => '\23'
);
}
else {
$replace = array(
'*' => '\2a',
'(' => '\28',
')' => '\29',
"\\" => '\5c',
'/' => '\2f'
);
}
if ($reverse) {
return str_replace(array_values($replace), array_keys($replace), $str);
}
return strtr($str, $replace);
}
/**
* Unify DN string for comparison
*
* @para string $str DN string
*
* @return string Unified DN string
*/
public static function unified_dn($str)
{
$result = array();
foreach (explode(',', $str) as $token) {
list($attr, $value) = explode('=', $token, 2);
$pos = 0;
while (preg_match('/\\\\[0-9a-fA-F]{2}/', $value, $matches, PREG_OFFSET_CAPTURE, $pos)) {
$char = chr(hexdec(substr($matches[0][0], 1)));
$pos = $matches[0][1];
$value = substr_replace($value, $char, $pos, 3);
$pos += 1;
}
$result[] = $attr . '=' . self::quote_string($value, true);
}
return implode(',', $result);
}
/**
* create ber encoding for sort control
*
* @param array List of cols to sort by
* @return string BER encoded option value
*/
private static function _sort_ber_encode($sortcols)
{
$str = '';
foreach (array_reverse((array)$sortcols) as $col) {
$ber_val = self::_string2hex($col);
// 30 = ber sequence with a length of octet value
// 04 = octet string with a length of the ascii value
$oct = self::_ber_addseq($ber_val, '04');
$str = self::_ber_addseq($oct, '30') . $str;
}
// now tack on sequence identifier and length
$str = self::_ber_addseq($str, '30');
return pack('H'.strlen($str), $str);
}
/**
* Returns ascii string encoded in hex
*/
private static function _string2hex($str)
{
$hex = '';
for ($i=0; $i < strlen($str); $i++)
$hex .= dechex(ord($str[$i]));
return $hex;
}
/**
* Generate BER encoded string for Virtual List View option
*
* @param integer List offset (first record)
* @param integer Records per page
* @return string BER encoded option value
*/
private static function _vlv_ber_encode($offset, $rpp, $search = '')
{
// This string is ber-encoded, php will prefix this value with:
// 04 (octet string) and 10 (length of 16 bytes)
// the code behind this string is broken down as follows:
// 30 = ber sequence with a length of 0e (14) bytes following
// 02 = type integer (in two's complement form) with 2 bytes following (beforeCount): 01 00 (ie 0)
// 02 = type integer (in two's complement form) with 2 bytes following (afterCount): 01 18 (ie 25-1=24)
// a0 = type context-specific/constructed with a length of 06 (6) bytes following
// 02 = type integer with 2 bytes following (offset): 01 01 (ie 1)
// 02 = type integer with 2 bytes following (contentCount): 01 00
// whith a search string present:
// 81 = type context-specific/constructed with a length of 04 (4) bytes following (the length will change here)
// 81 indicates a user string is present where as a a0 indicates just a offset search
// 81 = type context-specific/constructed with a length of 06 (6) bytes following
// the following info was taken from the ISO/IEC 8825-1:2003 x.690 standard re: the
// encoding of integer values (note: these values are in
// two-complement form so since offset will never be negative bit 8 of the
// leftmost octet should never by set to 1):
// 8.3.2: If the contents octets of an integer value encoding consist
// of more than one octet, then the bits of the first octet (rightmost) and bit 8
// of the second (to the left of first octet) octet:
// a) shall not all be ones; and
// b) shall not all be zero
if ($search) {
$search = preg_replace('/[^-[:alpha:] ,.()0-9]+/', '', $search);
$ber_val = self::_string2hex($search);
$str = self::_ber_addseq($ber_val, '81');
}
else {
// construct the string from right to left
$str = "020100"; # contentCount
// returns encoded integer value in hex format
$ber_val = self::_ber_encode_int($offset);
// calculate octet length of $ber_val
$str = self::_ber_addseq($ber_val, '02') . $str;
// now compute length over $str
$str = self::_ber_addseq($str, 'a0');
}
// now tack on records per page
$str = "020100" . self::_ber_addseq(self::_ber_encode_int($rpp-1), '02') . $str;
// now tack on sequence identifier and length
$str = self::_ber_addseq($str, '30');
return pack('H'.strlen($str), $str);
}
private function _fuzzy_search_prefix()
{
switch ($this->config_get("fuzzy_search", 2)) {
case 2:
return "*";
break;
case 1:
case 0:
default:
return "";
break;
}
}
private function _fuzzy_search_suffix()
{
switch ($this->config_get("fuzzy_search", 2)) {
case 2:
return "*";
break;
case 1:
return "*";
case 0:
default:
return "";
break;
}
}
/**
* Return the search string value to be used in VLV controls
*
* @param array $sort List of attributes in vlv index
* @param array|string $search Search string or attribute => value hash
*
* @return string Search string
*/
private function _vlv_search($sort, $search)
{
if (!empty($this->additional_filter)) {
$this->_debug("Not setting a VLV search filter because we already have a filter");
return;
}
if (empty($search)) {
return;
}
foreach ((array) $search as $attr => $value) {
if ($attr && !in_array(strtolower($attr), $sort)) {
$this->_debug("Cannot use VLV search using attribute not indexed: $attr (not in " . var_export($sort, true) . ")");
return;
}
else {
return $value . $this->_fuzzy_search_suffix();
}
}
}
/**
* Set server controls for Virtual List View (paginated listing)
*/
private function _vlv_set_controls($sort, $list_page, $page_size, $search = null)
{
$sort_ctrl = array(
'oid' => "1.2.840.113556.1.4.473",
'value' => self::_sort_ber_encode($sort)
);
if (!empty($search)) {
$this->_debug("_vlv_set_controls to include search: " . var_export($search, true));
}
$vlv_ctrl = array(
'oid' => "2.16.840.1.113730.3.4.9",
'value' => self::_vlv_ber_encode(
$offset = ($list_page-1) * $page_size + 1,
$page_size,
$search
),
'iscritical' => true
);
$this->_debug("C: set controls sort=" . join(' ', unpack('H'.(strlen($sort_ctrl['value'])*2), $sort_ctrl['value']))
. " (" . implode(',', (array) $sort) . ");"
. " vlv=" . join(' ', (unpack('H'.(strlen($vlv_ctrl['value'])*2), $vlv_ctrl['value']))) . " ($offset/$page_size)");
if (!ldap_set_option($this->conn, LDAP_OPT_SERVER_CONTROLS, array($sort_ctrl, $vlv_ctrl))) {
$this->_debug("S: ".ldap_error($this->conn));
$this->set_error(self::ERROR_SEARCH, 'vlvnotsupported');
return false;
}
return true;
}
}
diff --git a/lib/Net/LDAP3/Result.php b/lib/Net/LDAP3/Result.php
index 0759df0..b89c9fd 100644
--- a/lib/Net/LDAP3/Result.php
+++ b/lib/Net/LDAP3/Result.php
@@ -1,152 +1,161 @@
<?php
/*
+-----------------------------------------------------------------------+
| Net/LDAP3/Result.php |
| |
| Based on code created by the Roundcube Webmail team. |
| |
| Copyright (C) 2006-2014, The Roundcube Dev Team |
| Copyright (C) 2012-2014, Kolab Systems AG |
| |
- | Licensed under the GNU General Public License version 3 or |
- | any later version with exceptions for plugins. |
- | See the README file for a full license statement. |
+ | This program is free software: you can redistribute it and/or modify |
+ | it under the terms of the GNU General Public License as published by |
+ | the Free Software Foundation, either version 3 of the License, or |
+ | (at your option) any later version. |
+ | |
+ | This program is distributed in the hope that it will be useful, |
+ | but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ | GNU General Public License for more details. |
+ | |
+ | You should have received a copy of the GNU General Public License |
+ | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| |
| PURPOSE: |
| Provide advanced functionality for accessing LDAP directories |
| |
+-----------------------------------------------------------------------+
| Authors: Thomas Bruederli <roundcube@gmail.com> |
| Jeroen van Meeuwen <vanmeeuwen@kolabsys.com> |
+-----------------------------------------------------------------------+
*/
/**
* Model class representing an LDAP search result
*
* @package LDAP
*/
class Net_LDAP3_Result implements Iterator
{
protected $conn;
protected $base_dn;
protected $filter;
protected $scope;
private $count;
private $current;
private $iteratorkey = 0;
/**
* Default constructor
*
* @param resource $conn LDAP link identifier
* @param string $base_dn Base DN used to get this result
* @param string $filter Filter query used to get this result
* @param string $scope Scope of the result
* @param resource $result LDAP result entry identifier
*/
function __construct($conn, $base_dn, $filter, $scope, $result)
{
$this->conn = $conn;
$this->base_dn = $base_dn;
$this->filter = $filter;
$this->scope = $scope;
$this->result = $result;
}
public function get($property, $default = null)
{
if (isset($this->$property)) {
return $this->$property;
} else {
return $default;
}
}
public function set($property, $value)
{
$this->$property = $value;
}
/**
* Wrapper for ldap_sort()
*/
public function sort($attr)
{
return ldap_sort($this->conn, $this->result, $attr);
}
/**
* Get entries count
*/
public function count()
{
if (!isset($this->count)) {
$this->count = ldap_count_entries($this->conn, $this->result);
}
return $this->count;
}
/**
* Wrapper for ldap_get_entries()
*
* @param bool $normalize Optionally normalize the entries to a list of hash arrays
*
* @return array List of LDAP entries
*/
public function entries($normalize = false)
{
$entries = ldap_get_entries($this->conn, $this->result);
if ($normalize) {
return Net_LDAP3::normalize_result($entries);
}
return $entries;
}
/**
* Wrapper for ldap_get_dn() using the current entry pointer
*/
public function get_dn()
{
return $this->current ? ldap_get_dn($this->conn, $this->current) : null;
}
/*** Implement PHP 5 Iterator interface to make foreach work ***/
function current()
{
$attrib = ldap_get_attributes($this->conn, $this->current);
$attrib['dn'] = ldap_get_dn($this->conn, $this->current);
return $attrib;
}
function key()
{
return $this->iteratorkey;
}
function rewind()
{
$this->iteratorkey = 0;
$this->current = ldap_first_entry($this->conn, $this->result);
}
function next()
{
$this->iteratorkey++;
$this->current = ldap_next_entry($this->conn, $this->current);
}
function valid()
{
return (bool)$this->current;
}
}

File Metadata

Mime Type
text/x-diff
Expires
Mon, Jun 9, 6:34 PM (1 d, 6 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
196830
Default Alt Text
(144 KB)

Event Timeline