[
  {
    "path": ".gitignore",
    "content": "*~\n.dropbox\nnbproject\nlocal\n\n"
  },
  {
    "path": "COPYING",
    "content": "                    GNU GENERAL PUBLIC LICENSE\n                       Version 3, 29 June 2007\n\n Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>\n Everyone is permitted to copy and distribute verbatim copies\n of this license document, but changing it is not allowed.\n\n                            Preamble\n\n  The GNU General Public License is a free, copyleft license for\nsoftware and other kinds of works.\n\n  The licenses for most software and other practical works are designed\nto take away your freedom to share and change the works.  By contrast,\nthe GNU General Public License is intended to guarantee your freedom to\nshare and change all versions of a program--to make sure it remains free\nsoftware for all its users.  We, the Free Software Foundation, use the\nGNU General Public License for most of our software; it applies also to\nany other work released this way by its authors.  You can apply it to\nyour programs, too.\n\n  When we speak of free software, we are referring to freedom, not\nprice.  Our General Public Licenses are designed to make sure that you\nhave the freedom to distribute copies of free software (and charge for\nthem if you wish), that you receive source code or can get it if you\nwant it, that you can change the software or use pieces of it in new\nfree programs, and that you know you can do these things.\n\n  To protect your rights, we need to prevent others from denying you\nthese rights or asking you to surrender the rights.  Therefore, you have\ncertain responsibilities if you distribute copies of the software, or if\nyou modify it: responsibilities to respect the freedom of others.\n\n  For example, if you distribute copies of such a program, whether\ngratis or for a fee, you must pass on to the recipients the same\nfreedoms that you received.  You must make sure that they, too, receive\nor can get the source code.  And you must show them these terms so they\nknow their rights.\n\n  Developers that use the GNU GPL protect your rights with two steps:\n(1) assert copyright on the software, and (2) offer you this License\ngiving you legal permission to copy, distribute and/or modify it.\n\n  For the developers' and authors' protection, the GPL clearly explains\nthat there is no warranty for this free software.  For both users' and\nauthors' sake, the GPL requires that modified versions be marked as\nchanged, so that their problems will not be attributed erroneously to\nauthors of previous versions.\n\n  Some devices are designed to deny users access to install or run\nmodified versions of the software inside them, although the manufacturer\ncan do so.  This is fundamentally incompatible with the aim of\nprotecting users' freedom to change the software.  The systematic\npattern of such abuse occurs in the area of products for individuals to\nuse, which is precisely where it is most unacceptable.  Therefore, we\nhave designed this version of the GPL to prohibit the practice for those\nproducts.  If such problems arise substantially in other domains, we\nstand ready to extend this provision to those domains in future versions\nof the GPL, as needed to protect the freedom of users.\n\n  Finally, every program is threatened constantly by software patents.\nStates should not allow patents to restrict development and use of\nsoftware on general-purpose computers, but in those that do, we wish to\navoid the special danger that patents applied to a free program could\nmake it effectively proprietary.  To prevent this, the GPL assures that\npatents cannot be used to render the program non-free.\n\n  The precise terms and conditions for copying, distribution and\nmodification follow.\n\n                       TERMS AND CONDITIONS\n\n  0. Definitions.\n\n  \"This License\" refers to version 3 of the GNU General Public License.\n\n  \"Copyright\" also means copyright-like laws that apply to other kinds of\nworks, such as semiconductor masks.\n\n  \"The Program\" refers to any copyrightable work licensed under this\nLicense.  Each licensee is addressed as \"you\".  \"Licensees\" and\n\"recipients\" may be individuals or organizations.\n\n  To \"modify\" a work means to copy from or adapt all or part of the work\nin a fashion requiring copyright permission, other than the making of an\nexact copy.  The resulting work is called a \"modified version\" of the\nearlier work or a work \"based on\" the earlier work.\n\n  A \"covered work\" means either the unmodified Program or a work based\non the Program.\n\n  To \"propagate\" a work means to do anything with it that, without\npermission, would make you directly or secondarily liable for\ninfringement under applicable copyright law, except executing it on a\ncomputer or modifying a private copy.  Propagation includes copying,\ndistribution (with or without modification), making available to the\npublic, and in some countries other activities as well.\n\n  To \"convey\" a work means any kind of propagation that enables other\nparties to make or receive copies.  Mere interaction with a user through\na computer network, with no transfer of a copy, is not conveying.\n\n  An interactive user interface displays \"Appropriate Legal Notices\"\nto the extent that it includes a convenient and prominently visible\nfeature that (1) displays an appropriate copyright notice, and (2)\ntells the user that there is no warranty for the work (except to the\nextent that warranties are provided), that licensees may convey the\nwork under this License, and how to view a copy of this License.  If\nthe interface presents a list of user commands or options, such as a\nmenu, a prominent item in the list meets this criterion.\n\n  1. Source Code.\n\n  The \"source code\" for a work means the preferred form of the work\nfor making modifications to it.  \"Object code\" means any non-source\nform of a work.\n\n  A \"Standard Interface\" means an interface that either is an official\nstandard defined by a recognized standards body, or, in the case of\ninterfaces specified for a particular programming language, one that\nis widely used among developers working in that language.\n\n  The \"System Libraries\" of an executable work include anything, other\nthan the work as a whole, that (a) is included in the normal form of\npackaging a Major Component, but which is not part of that Major\nComponent, and (b) serves only to enable use of the work with that\nMajor Component, or to implement a Standard Interface for which an\nimplementation is available to the public in source code form.  A\n\"Major Component\", in this context, means a major essential component\n(kernel, window system, and so on) of the specific operating system\n(if any) on which the executable work runs, or a compiler used to\nproduce the work, or an object code interpreter used to run it.\n\n  The \"Corresponding Source\" for a work in object code form means all\nthe source code needed to generate, install, and (for an executable\nwork) run the object code and to modify the work, including scripts to\ncontrol those activities.  However, it does not include the work's\nSystem Libraries, or general-purpose tools or generally available free\nprograms which are used unmodified in performing those activities but\nwhich are not part of the work.  For example, Corresponding Source\nincludes interface definition files associated with source files for\nthe work, and the source code for shared libraries and dynamically\nlinked subprograms that the work is specifically designed to require,\nsuch as by intimate data communication or control flow between those\nsubprograms and other parts of the work.\n\n  The Corresponding Source need not include anything that users\ncan regenerate automatically from other parts of the Corresponding\nSource.\n\n  The Corresponding Source for a work in source code form is that\nsame work.\n\n  2. Basic Permissions.\n\n  All rights granted under this License are granted for the term of\ncopyright on the Program, and are irrevocable provided the stated\nconditions are met.  This License explicitly affirms your unlimited\npermission to run the unmodified Program.  The output from running a\ncovered work is covered by this License only if the output, given its\ncontent, constitutes a covered work.  This License acknowledges your\nrights of fair use or other equivalent, as provided by copyright law.\n\n  You may make, run and propagate covered works that you do not\nconvey, without conditions so long as your license otherwise remains\nin force.  You may convey covered works to others for the sole purpose\nof having them make modifications exclusively for you, or provide you\nwith facilities for running those works, provided that you comply with\nthe terms of this License in conveying all material for which you do\nnot control copyright.  Those thus making or running the covered works\nfor you must do so exclusively on your behalf, under your direction\nand control, on terms that prohibit them from making any copies of\nyour copyrighted material outside their relationship with you.\n\n  Conveying under any other circumstances is permitted solely under\nthe conditions stated below.  Sublicensing is not allowed; section 10\nmakes it unnecessary.\n\n  3. Protecting Users' Legal Rights From Anti-Circumvention Law.\n\n  No covered work shall be deemed part of an effective technological\nmeasure under any applicable law fulfilling obligations under article\n11 of the WIPO copyright treaty adopted on 20 December 1996, or\nsimilar laws prohibiting or restricting circumvention of such\nmeasures.\n\n  When you convey a covered work, you waive any legal power to forbid\ncircumvention of technological measures to the extent such circumvention\nis effected by exercising rights under this License with respect to\nthe covered work, and you disclaim any intention to limit operation or\nmodification of the work as a means of enforcing, against the work's\nusers, your or third parties' legal rights to forbid circumvention of\ntechnological measures.\n\n  4. Conveying Verbatim Copies.\n\n  You may convey verbatim copies of the Program's source code as you\nreceive it, in any medium, provided that you conspicuously and\nappropriately publish on each copy an appropriate copyright notice;\nkeep intact all notices stating that this License and any\nnon-permissive terms added in accord with section 7 apply to the code;\nkeep intact all notices of the absence of any warranty; and give all\nrecipients a copy of this License along with the Program.\n\n  You may charge any price or no price for each copy that you convey,\nand you may offer support or warranty protection for a fee.\n\n  5. Conveying Modified Source Versions.\n\n  You may convey a work based on the Program, or the modifications to\nproduce it from the Program, in the form of source code under the\nterms of section 4, provided that you also meet all of these conditions:\n\n    a) The work must carry prominent notices stating that you modified\n    it, and giving a relevant date.\n\n    b) The work must carry prominent notices stating that it is\n    released under this License and any conditions added under section\n    7.  This requirement modifies the requirement in section 4 to\n    \"keep intact all notices\".\n\n    c) You must license the entire work, as a whole, under this\n    License to anyone who comes into possession of a copy.  This\n    License will therefore apply, along with any applicable section 7\n    additional terms, to the whole of the work, and all its parts,\n    regardless of how they are packaged.  This License gives no\n    permission to license the work in any other way, but it does not\n    invalidate such permission if you have separately received it.\n\n    d) If the work has interactive user interfaces, each must display\n    Appropriate Legal Notices; however, if the Program has interactive\n    interfaces that do not display Appropriate Legal Notices, your\n    work need not make them do so.\n\n  A compilation of a covered work with other separate and independent\nworks, which are not by their nature extensions of the covered work,\nand which are not combined with it such as to form a larger program,\nin or on a volume of a storage or distribution medium, is called an\n\"aggregate\" if the compilation and its resulting copyright are not\nused to limit the access or legal rights of the compilation's users\nbeyond what the individual works permit.  Inclusion of a covered work\nin an aggregate does not cause this License to apply to the other\nparts of the aggregate.\n\n  6. Conveying Non-Source Forms.\n\n  You may convey a covered work in object code form under the terms\nof sections 4 and 5, provided that you also convey the\nmachine-readable Corresponding Source under the terms of this License,\nin one of these ways:\n\n    a) Convey the object code in, or embodied in, a physical product\n    (including a physical distribution medium), accompanied by the\n    Corresponding Source fixed on a durable physical medium\n    customarily used for software interchange.\n\n    b) Convey the object code in, or embodied in, a physical product\n    (including a physical distribution medium), accompanied by a\n    written offer, valid for at least three years and valid for as\n    long as you offer spare parts or customer support for that product\n    model, to give anyone who possesses the object code either (1) a\n    copy of the Corresponding Source for all the software in the\n    product that is covered by this License, on a durable physical\n    medium customarily used for software interchange, for a price no\n    more than your reasonable cost of physically performing this\n    conveying of source, or (2) access to copy the\n    Corresponding Source from a network server at no charge.\n\n    c) Convey individual copies of the object code with a copy of the\n    written offer to provide the Corresponding Source.  This\n    alternative is allowed only occasionally and noncommercially, and\n    only if you received the object code with such an offer, in accord\n    with subsection 6b.\n\n    d) Convey the object code by offering access from a designated\n    place (gratis or for a charge), and offer equivalent access to the\n    Corresponding Source in the same way through the same place at no\n    further charge.  You need not require recipients to copy the\n    Corresponding Source along with the object code.  If the place to\n    copy the object code is a network server, the Corresponding Source\n    may be on a different server (operated by you or a third party)\n    that supports equivalent copying facilities, provided you maintain\n    clear directions next to the object code saying where to find the\n    Corresponding Source.  Regardless of what server hosts the\n    Corresponding Source, you remain obligated to ensure that it is\n    available for as long as needed to satisfy these requirements.\n\n    e) Convey the object code using peer-to-peer transmission, provided\n    you inform other peers where the object code and Corresponding\n    Source of the work are being offered to the general public at no\n    charge under subsection 6d.\n\n  A separable portion of the object code, whose source code is excluded\nfrom the Corresponding Source as a System Library, need not be\nincluded in conveying the object code work.\n\n  A \"User Product\" is either (1) a \"consumer product\", which means any\ntangible personal property which is normally used for personal, family,\nor household purposes, or (2) anything designed or sold for incorporation\ninto a dwelling.  In determining whether a product is a consumer product,\ndoubtful cases shall be resolved in favor of coverage.  For a particular\nproduct received by a particular user, \"normally used\" refers to a\ntypical or common use of that class of product, regardless of the status\nof the particular user or of the way in which the particular user\nactually uses, or expects or is expected to use, the product.  A product\nis a consumer product regardless of whether the product has substantial\ncommercial, industrial or non-consumer uses, unless such uses represent\nthe only significant mode of use of the product.\n\n  \"Installation Information\" for a User Product means any methods,\nprocedures, authorization keys, or other information required to install\nand execute modified versions of a covered work in that User Product from\na modified version of its Corresponding Source.  The information must\nsuffice to ensure that the continued functioning of the modified object\ncode is in no case prevented or interfered with solely because\nmodification has been made.\n\n  If you convey an object code work under this section in, or with, or\nspecifically for use in, a User Product, and the conveying occurs as\npart of a transaction in which the right of possession and use of the\nUser Product is transferred to the recipient in perpetuity or for a\nfixed term (regardless of how the transaction is characterized), the\nCorresponding Source conveyed under this section must be accompanied\nby the Installation Information.  But this requirement does not apply\nif neither you nor any third party retains the ability to install\nmodified object code on the User Product (for example, the work has\nbeen installed in ROM).\n\n  The requirement to provide Installation Information does not include a\nrequirement to continue to provide support service, warranty, or updates\nfor a work that has been modified or installed by the recipient, or for\nthe User Product in which it has been modified or installed.  Access to a\nnetwork may be denied when the modification itself materially and\nadversely affects the operation of the network or violates the rules and\nprotocols for communication across the network.\n\n  Corresponding Source conveyed, and Installation Information provided,\nin accord with this section must be in a format that is publicly\ndocumented (and with an implementation available to the public in\nsource code form), and must require no special password or key for\nunpacking, reading or copying.\n\n  7. Additional Terms.\n\n  \"Additional permissions\" are terms that supplement the terms of this\nLicense by making exceptions from one or more of its conditions.\nAdditional permissions that are applicable to the entire Program shall\nbe treated as though they were included in this License, to the extent\nthat they are valid under applicable law.  If additional permissions\napply only to part of the Program, that part may be used separately\nunder those permissions, but the entire Program remains governed by\nthis License without regard to the additional permissions.\n\n  When you convey a copy of a covered work, you may at your option\nremove any additional permissions from that copy, or from any part of\nit.  (Additional permissions may be written to require their own\nremoval in certain cases when you modify the work.)  You may place\nadditional permissions on material, added by you to a covered work,\nfor which you have or can give appropriate copyright permission.\n\n  Notwithstanding any other provision of this License, for material you\nadd to a covered work, you may (if authorized by the copyright holders of\nthat material) supplement the terms of this License with terms:\n\n    a) Disclaiming warranty or limiting liability differently from the\n    terms of sections 15 and 16 of this License; or\n\n    b) Requiring preservation of specified reasonable legal notices or\n    author attributions in that material or in the Appropriate Legal\n    Notices displayed by works containing it; or\n\n    c) Prohibiting misrepresentation of the origin of that material, or\n    requiring that modified versions of such material be marked in\n    reasonable ways as different from the original version; or\n\n    d) Limiting the use for publicity purposes of names of licensors or\n    authors of the material; or\n\n    e) Declining to grant rights under trademark law for use of some\n    trade names, trademarks, or service marks; or\n\n    f) Requiring indemnification of licensors and authors of that\n    material by anyone who conveys the material (or modified versions of\n    it) with contractual assumptions of liability to the recipient, for\n    any liability that these contractual assumptions directly impose on\n    those licensors and authors.\n\n  All other non-permissive additional terms are considered \"further\nrestrictions\" within the meaning of section 10.  If the Program as you\nreceived it, or any part of it, contains a notice stating that it is\ngoverned by this License along with a term that is a further\nrestriction, you may remove that term.  If a license document contains\na further restriction but permits relicensing or conveying under this\nLicense, you may add to a covered work material governed by the terms\nof that license document, provided that the further restriction does\nnot survive such relicensing or conveying.\n\n  If you add terms to a covered work in accord with this section, you\nmust place, in the relevant source files, a statement of the\nadditional terms that apply to those files, or a notice indicating\nwhere to find the applicable terms.\n\n  Additional terms, permissive or non-permissive, may be stated in the\nform of a separately written license, or stated as exceptions;\nthe above requirements apply either way.\n\n  8. Termination.\n\n  You may not propagate or modify a covered work except as expressly\nprovided under this License.  Any attempt otherwise to propagate or\nmodify it is void, and will automatically terminate your rights under\nthis License (including any patent licenses granted under the third\nparagraph of section 11).\n\n  However, if you cease all violation of this License, then your\nlicense from a particular copyright holder is reinstated (a)\nprovisionally, unless and until the copyright holder explicitly and\nfinally terminates your license, and (b) permanently, if the copyright\nholder fails to notify you of the violation by some reasonable means\nprior to 60 days after the cessation.\n\n  Moreover, your license from a particular copyright holder is\nreinstated permanently if the copyright holder notifies you of the\nviolation by some reasonable means, this is the first time you have\nreceived notice of violation of this License (for any work) from that\ncopyright holder, and you cure the violation prior to 30 days after\nyour receipt of the notice.\n\n  Termination of your rights under this section does not terminate the\nlicenses of parties who have received copies or rights from you under\nthis License.  If your rights have been terminated and not permanently\nreinstated, you do not qualify to receive new licenses for the same\nmaterial under section 10.\n\n  9. Acceptance Not Required for Having Copies.\n\n  You are not required to accept this License in order to receive or\nrun a copy of the Program.  Ancillary propagation of a covered work\noccurring solely as a consequence of using peer-to-peer transmission\nto receive a copy likewise does not require acceptance.  However,\nnothing other than this License grants you permission to propagate or\nmodify any covered work.  These actions infringe copyright if you do\nnot accept this License.  Therefore, by modifying or propagating a\ncovered work, you indicate your acceptance of this License to do so.\n\n  10. Automatic Licensing of Downstream Recipients.\n\n  Each time you convey a covered work, the recipient automatically\nreceives a license from the original licensors, to run, modify and\npropagate that work, subject to this License.  You are not responsible\nfor enforcing compliance by third parties with this License.\n\n  An \"entity transaction\" is a transaction transferring control of an\norganization, or substantially all assets of one, or subdividing an\norganization, or merging organizations.  If propagation of a covered\nwork results from an entity transaction, each party to that\ntransaction who receives a copy of the work also receives whatever\nlicenses to the work the party's predecessor in interest had or could\ngive under the previous paragraph, plus a right to possession of the\nCorresponding Source of the work from the predecessor in interest, if\nthe predecessor has it or can get it with reasonable efforts.\n\n  You may not impose any further restrictions on the exercise of the\nrights granted or affirmed under this License.  For example, you may\nnot impose a license fee, royalty, or other charge for exercise of\nrights granted under this License, and you may not initiate litigation\n(including a cross-claim or counterclaim in a lawsuit) alleging that\nany patent claim is infringed by making, using, selling, offering for\nsale, or importing the Program or any portion of it.\n\n  11. Patents.\n\n  A \"contributor\" is a copyright holder who authorizes use under this\nLicense of the Program or a work on which the Program is based.  The\nwork thus licensed is called the contributor's \"contributor version\".\n\n  A contributor's \"essential patent claims\" are all patent claims\nowned or controlled by the contributor, whether already acquired or\nhereafter acquired, that would be infringed by some manner, permitted\nby this License, of making, using, or selling its contributor version,\nbut do not include claims that would be infringed only as a\nconsequence of further modification of the contributor version.  For\npurposes of this definition, \"control\" includes the right to grant\npatent sublicenses in a manner consistent with the requirements of\nthis License.\n\n  Each contributor grants you a non-exclusive, worldwide, royalty-free\npatent license under the contributor's essential patent claims, to\nmake, use, sell, offer for sale, import and otherwise run, modify and\npropagate the contents of its contributor version.\n\n  In the following three paragraphs, a \"patent license\" is any express\nagreement or commitment, however denominated, not to enforce a patent\n(such as an express permission to practice a patent or covenant not to\nsue for patent infringement).  To \"grant\" such a patent license to a\nparty means to make such an agreement or commitment not to enforce a\npatent against the party.\n\n  If you convey a covered work, knowingly relying on a patent license,\nand the Corresponding Source of the work is not available for anyone\nto copy, free of charge and under the terms of this License, through a\npublicly available network server or other readily accessible means,\nthen you must either (1) cause the Corresponding Source to be so\navailable, or (2) arrange to deprive yourself of the benefit of the\npatent license for this particular work, or (3) arrange, in a manner\nconsistent with the requirements of this License, to extend the patent\nlicense to downstream recipients.  \"Knowingly relying\" means you have\nactual knowledge that, but for the patent license, your conveying the\ncovered work in a country, or your recipient's use of the covered work\nin a country, would infringe one or more identifiable patents in that\ncountry that you have reason to believe are valid.\n\n  If, pursuant to or in connection with a single transaction or\narrangement, you convey, or propagate by procuring conveyance of, a\ncovered work, and grant a patent license to some of the parties\nreceiving the covered work authorizing them to use, propagate, modify\nor convey a specific copy of the covered work, then the patent license\nyou grant is automatically extended to all recipients of the covered\nwork and works based on it.\n\n  A patent license is \"discriminatory\" if it does not include within\nthe scope of its coverage, prohibits the exercise of, or is\nconditioned on the non-exercise of one or more of the rights that are\nspecifically granted under this License.  You may not convey a covered\nwork if you are a party to an arrangement with a third party that is\nin the business of distributing software, under which you make payment\nto the third party based on the extent of your activity of conveying\nthe work, and under which the third party grants, to any of the\nparties who would receive the covered work from you, a discriminatory\npatent license (a) in connection with copies of the covered work\nconveyed by you (or copies made from those copies), or (b) primarily\nfor and in connection with specific products or compilations that\ncontain the covered work, unless you entered into that arrangement,\nor that patent license was granted, prior to 28 March 2007.\n\n  Nothing in this License shall be construed as excluding or limiting\nany implied license or other defenses to infringement that may\notherwise be available to you under applicable patent law.\n\n  12. No Surrender of Others' Freedom.\n\n  If conditions are imposed on you (whether by court order, agreement or\notherwise) that contradict the conditions of this License, they do not\nexcuse you from the conditions of this License.  If you cannot convey a\ncovered work so as to satisfy simultaneously your obligations under this\nLicense and any other pertinent obligations, then as a consequence you may\nnot convey it at all.  For example, if you agree to terms that obligate you\nto collect a royalty for further conveying from those to whom you convey\nthe Program, the only way you could satisfy both those terms and this\nLicense would be to refrain entirely from conveying the Program.\n\n  13. Use with the GNU Affero General Public License.\n\n  Notwithstanding any other provision of this License, you have\npermission to link or combine any covered work with a work licensed\nunder version 3 of the GNU Affero General Public License into a single\ncombined work, and to convey the resulting work.  The terms of this\nLicense will continue to apply to the part which is the covered work,\nbut the special requirements of the GNU Affero General Public License,\nsection 13, concerning interaction through a network will apply to the\ncombination as such.\n\n  14. Revised Versions of this License.\n\n  The Free Software Foundation may publish revised and/or new versions of\nthe GNU General Public License from time to time.  Such new versions will\nbe similar in spirit to the present version, but may differ in detail to\naddress new problems or concerns.\n\n  Each version is given a distinguishing version number.  If the\nProgram specifies that a certain numbered version of the GNU General\nPublic License \"or any later version\" applies to it, you have the\noption of following the terms and conditions either of that numbered\nversion or of any later version published by the Free Software\nFoundation.  If the Program does not specify a version number of the\nGNU General Public License, you may choose any version ever published\nby the Free Software Foundation.\n\n  If the Program specifies that a proxy can decide which future\nversions of the GNU General Public License can be used, that proxy's\npublic statement of acceptance of a version permanently authorizes you\nto choose that version for the Program.\n\n  Later license versions may give you additional or different\npermissions.  However, no additional obligations are imposed on any\nauthor or copyright holder as a result of your choosing to follow a\nlater version.\n\n  15. Disclaimer of Warranty.\n\n  THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY\nAPPLICABLE LAW.  EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT\nHOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM \"AS IS\" WITHOUT WARRANTY\nOF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,\nTHE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\nPURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM\nIS WITH YOU.  SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF\nALL NECESSARY SERVICING, REPAIR OR CORRECTION.\n\n  16. Limitation of Liability.\n\n  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING\nWILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS\nTHE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY\nGENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE\nUSE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF\nDATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD\nPARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),\nEVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF\nSUCH DAMAGES.\n\n  17. Interpretation of Sections 15 and 16.\n\n  If the disclaimer of warranty and limitation of liability provided\nabove cannot be given local legal effect according to their terms,\nreviewing courts shall apply local law that most closely approximates\nan absolute waiver of all civil liability in connection with the\nProgram, unless a warranty or assumption of liability accompanies a\ncopy of the Program in return for a fee.\n\n                     END OF TERMS AND CONDITIONS\n\n            How to Apply These Terms to Your New Programs\n\n  If you develop a new program, and you want it to be of the greatest\npossible use to the public, the best way to achieve this is to make it\nfree software which everyone can redistribute and change under these terms.\n\n  To do so, attach the following notices to the program.  It is safest\nto attach them to the start of each source file to most effectively\nstate the exclusion of warranty; and each file should have at least\nthe \"copyright\" line and a pointer to where the full notice is found.\n\n    <one line to give the program's name and a brief idea of what it does.>\n    Copyright (C) <year>  <name of author>\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\nAlso add information on how to contact you by electronic and paper mail.\n\n  If the program does terminal interaction, make it output a short\nnotice like this when it starts in an interactive mode:\n\n    <program>  Copyright (C) <year>  <name of author>\n    This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.\n    This is free software, and you are welcome to redistribute it\n    under certain conditions; type `show c' for details.\n\nThe hypothetical commands `show w' and `show c' should show the appropriate\nparts of the General Public License.  Of course, your program's commands\nmight be different; for a GUI interface, you would use an \"about box\".\n\n  You should also get your employer (if you work as a programmer) or school,\nif any, to sign a \"copyright disclaimer\" for the program, if necessary.\nFor more information on this, and how to apply and follow the GNU GPL, see\n<http://www.gnu.org/licenses/>.\n\n  The GNU General Public License does not permit incorporating your program\ninto proprietary programs.  If your program is a subroutine library, you\nmay consider it more useful to permit linking proprietary applications with\nthe library.  If this is what you want to do, use the GNU Lesser General\nPublic License instead of this License.  But first, please read\n<http://www.gnu.org/philosophy/why-not-lgpl.html>.\n"
  },
  {
    "path": "COPYING.LESSER",
    "content": "\t\t   GNU LESSER GENERAL PUBLIC LICENSE\n                       Version 3, 29 June 2007\n\n Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>\n Everyone is permitted to copy and distribute verbatim copies\n of this license document, but changing it is not allowed.\n\n\n  This version of the GNU Lesser General Public License incorporates\nthe terms and conditions of version 3 of the GNU General Public\nLicense, supplemented by the additional permissions listed below.\n\n  0. Additional Definitions.\n\n  As used herein, \"this License\" refers to version 3 of the GNU Lesser\nGeneral Public License, and the \"GNU GPL\" refers to version 3 of the GNU\nGeneral Public License.\n\n  \"The Library\" refers to a covered work governed by this License,\nother than an Application or a Combined Work as defined below.\n\n  An \"Application\" is any work that makes use of an interface provided\nby the Library, but which is not otherwise based on the Library.\nDefining a subclass of a class defined by the Library is deemed a mode\nof using an interface provided by the Library.\n\n  A \"Combined Work\" is a work produced by combining or linking an\nApplication with the Library.  The particular version of the Library\nwith which the Combined Work was made is also called the \"Linked\nVersion\".\n\n  The \"Minimal Corresponding Source\" for a Combined Work means the\nCorresponding Source for the Combined Work, excluding any source code\nfor portions of the Combined Work that, considered in isolation, are\nbased on the Application, and not on the Linked Version.\n\n  The \"Corresponding Application Code\" for a Combined Work means the\nobject code and/or source code for the Application, including any data\nand utility programs needed for reproducing the Combined Work from the\nApplication, but excluding the System Libraries of the Combined Work.\n\n  1. Exception to Section 3 of the GNU GPL.\n\n  You may convey a covered work under sections 3 and 4 of this License\nwithout being bound by section 3 of the GNU GPL.\n\n  2. Conveying Modified Versions.\n\n  If you modify a copy of the Library, and, in your modifications, a\nfacility refers to a function or data to be supplied by an Application\nthat uses the facility (other than as an argument passed when the\nfacility is invoked), then you may convey a copy of the modified\nversion:\n\n   a) under this License, provided that you make a good faith effort to\n   ensure that, in the event an Application does not supply the\n   function or data, the facility still operates, and performs\n   whatever part of its purpose remains meaningful, or\n\n   b) under the GNU GPL, with none of the additional permissions of\n   this License applicable to that copy.\n\n  3. Object Code Incorporating Material from Library Header Files.\n\n  The object code form of an Application may incorporate material from\na header file that is part of the Library.  You may convey such object\ncode under terms of your choice, provided that, if the incorporated\nmaterial is not limited to numerical parameters, data structure\nlayouts and accessors, or small macros, inline functions and templates\n(ten or fewer lines in length), you do both of the following:\n\n   a) Give prominent notice with each copy of the object code that the\n   Library is used in it and that the Library and its use are\n   covered by this License.\n\n   b) Accompany the object code with a copy of the GNU GPL and this license\n   document.\n\n  4. Combined Works.\n\n  You may convey a Combined Work under terms of your choice that,\ntaken together, effectively do not restrict modification of the\nportions of the Library contained in the Combined Work and reverse\nengineering for debugging such modifications, if you also do each of\nthe following:\n\n   a) Give prominent notice with each copy of the Combined Work that\n   the Library is used in it and that the Library and its use are\n   covered by this License.\n\n   b) Accompany the Combined Work with a copy of the GNU GPL and this license\n   document.\n\n   c) For a Combined Work that displays copyright notices during\n   execution, include the copyright notice for the Library among\n   these notices, as well as a reference directing the user to the\n   copies of the GNU GPL and this license document.\n\n   d) Do one of the following:\n\n       0) Convey the Minimal Corresponding Source under the terms of this\n       License, and the Corresponding Application Code in a form\n       suitable for, and under terms that permit, the user to\n       recombine or relink the Application with a modified version of\n       the Linked Version to produce a modified Combined Work, in the\n       manner specified by section 6 of the GNU GPL for conveying\n       Corresponding Source.\n\n       1) Use a suitable shared library mechanism for linking with the\n       Library.  A suitable mechanism is one that (a) uses at run time\n       a copy of the Library already present on the user's computer\n       system, and (b) will operate properly with a modified version\n       of the Library that is interface-compatible with the Linked\n       Version.\n\n   e) Provide Installation Information, but only if you would otherwise\n   be required to provide such information under section 6 of the\n   GNU GPL, and only to the extent that such information is\n   necessary to install and execute a modified version of the\n   Combined Work produced by recombining or relinking the\n   Application with a modified version of the Linked Version. (If\n   you use option 4d0, the Installation Information must accompany\n   the Minimal Corresponding Source and Corresponding Application\n   Code. If you use option 4d1, you must provide the Installation\n   Information in the manner specified by section 6 of the GNU GPL\n   for conveying Corresponding Source.)\n\n  5. Combined Libraries.\n\n  You may place library facilities that are a work based on the\nLibrary side by side in a single library together with other library\nfacilities that are not Applications and are not covered by this\nLicense, and convey such a combined library under terms of your\nchoice, if you do both of the following:\n\n   a) Accompany the combined library with a copy of the same work based\n   on the Library, uncombined with any other library facilities,\n   conveyed under the terms of this License.\n\n   b) Give prominent notice with the combined library that part of it\n   is a work based on the Library, and explaining where to find the\n   accompanying uncombined form of the same work.\n\n  6. Revised Versions of the GNU Lesser General Public License.\n\n  The Free Software Foundation may publish revised and/or new versions\nof the GNU Lesser General Public License from time to time. Such new\nversions will be similar in spirit to the present version, but may\ndiffer in detail to address new problems or concerns.\n\n  Each version is given a distinguishing version number. If the\nLibrary as you received it specifies that a certain numbered version\nof the GNU Lesser General Public License \"or any later version\"\napplies to it, you have the option of following the terms and\nconditions either of that published version or of any later version\npublished by the Free Software Foundation. If the Library as you\nreceived it does not specify a version number of the GNU Lesser\nGeneral Public License, you may choose any version of the GNU Lesser\nGeneral Public License ever published by the Free Software Foundation.\n\n  If the Library as you received it specifies that a proxy can decide\nwhether future versions of the GNU Lesser General Public License shall\napply, that proxy's public statement of acceptance of any version is\npermanent authorization for you to choose that version for the\nLibrary.\n"
  },
  {
    "path": "Dockerfile",
    "content": "from php:7.4-apache\n\nRUN apt update\nRUN apt install --yes imagemagick\n\nCOPY . /var/www/html/\n"
  },
  {
    "path": "README.md",
    "content": "# VisualCube\nGenerate custom Rubik's cube visualisations from your browser address bar. \n\n### Installation Instructions\n\nThese instructions are for installing the script on your own web server. If you do not have access to your own server, or would just like to try out the software, please visit:\nhttp://cube.rider.biz/visualcube.php\n\n#### Using Docker\n\n```\ndocker build -t visualcube .\ndocker run -p 80:80 --rm visualcube\n```\n\n##### Prerequisites\n\n* Access to an Apache web server with PHP and ImageMagic installed.\n* A MySQL database. This is optional, but will improve performance if you have a high traffic website.\n\n##### Steps\n\n1. Download and extract the code into a web-accessible folder.\n2. Edit the configuration variables in visualcube_config.php\n3. Point your browser to: www.yourwebsite.com/visualcube.php\n4. (Optional) Edit DB_USER, DB_PASS and DB_NAME in visualcube_dbprune.sh and install the cron job.\n5. (Optional) Configure mod_rewrite to redirect image suffixes to corresponding fmt=xxx form. See below.\n\n##### Configuring mod_rewrite\nAdd a .htaccess file to the same folder as visualcube.php with something like the following:\n```\nRewriteEngine On\n\nRewriteCond %{HTTP_HOST} ^(www\\.example\\.com)$\nRewriteRule ^visualcube\\.(gif|png|jpg|jpeg|tiff|ico)$ http://www.example.com/visualcube.php?%{QUERY_STRING}&fmt=$1 [L]\n```\n\n\n### Features\n\n* Fully 3D cube visualisation\n* Cube dimensions from 1x1x1 to NxNxN. Currently capped at 9x9 for performance.\n* Algorithm support\n* Complete orientation control\n* Multiple image formats\n* Custom image size\n* Cube and facelet transparency\n* Custom colour schemes and background\n* Image caching for speedy access\n* Cookie configurable variables\n* Arrow overlays\n* Highly configurable URL-based API\n* Open Source\n"
  },
  {
    "path": "cube_lib.php",
    "content": "<?php\n/*\n\tFile: cube_lib.php\n\tDate: 02 Apr 2010\n\tAuthor(s): Conrad Rider (www.crider.co.uk)\n\tDescription: Php library for modelling a Rubik's cube\n\n\tThis file is part of VisualCube.\n\n\tVisualCube is free software: you can redistribute it and/or modify\n\tit under the terms of the GNU Lesser General Public License as published by\n\tthe Free Software Foundation, either version 3 of the License, or\n\t(at your option) any later version.\n\n\tVisualCube is distributed in the hope that it will be useful,\n\tbut WITHOUT ANY WARRANTY; without even the implied warranty of\n\tMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n\tGNU Lesser General Public License for more details.\n\n\tYou should have received a copy of the GNU Lesser General Public License\n\talong with VisualCube.  If not, see <http://www.gnu.org/licenses/>.\n\t\n\tCopyright (C) 2010 Conrad Rider\n*/\n\n\t// Face constants\n\tglobal $U, $R, $F, $D, $L, $B;\n\t$U = 0; $R = 1; $F = 2; $D = 3; $L = 4; $B = 5;\n\n\t// Corner Constants\n\tglobal $URF, $UFL, $ULB, $UBR, $DFR, $DLF, $DBL, $DRB;\n\t$URF = 0; $UFL = 1; $ULB = 2; $UBR = 3; $DFR = 4; $DLF = 5; $DBL = 6; $DRB = 7;\n\n\t// Edge constants\n\tglobal $UR, $UF, $UL, $UB, $DR, $DF, $DL, $DB, $FR, $FL, $BL, $BR;\n\t$UR = 0; $UF = 1; $UL = 2; $UB = 3; $DR = 4; $DF = 5; $DL = 6; $DB = 7; $FR = 8; $FL = 9; $BL = 10; $BR = 11;\n\n\t// Mapping from face constants to face letters\n\tglobal $FACE_NAMES;\n\t$FACE_NAMES = Array(\n\t\t$U => 'u',\n\t\t$R => 'r',\n\t\t$F => 'f',\n\t\t$D => 'd',\n\t\t$L => 'l',\n\t\t$B => 'b',\n\t);\n\t\n\t// A solved cube\n\tglobal $SOLVED_CUBE;\n\t$SOLVED_CUBE = Array(\n\t\tArray( $U, $R, $F, $D, $L, $B ),\n\t\tArray( $URF, $UFL, $ULB, $UBR, $DFR, $DLF, $DBL, $DRB ),\n\t\tArray( 0, 0, 0, 0, 0, 0, 0, 0 ),\n\t\tArray( $UR, $UF, $UL, $UB, $DR, $DF, $DL, $DB, $FR, $FL, $BL, $BR ),\n\t\tArray( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ));\n\t\t\n\t// Partial cubes used to verify the correct parts are solved\n\tglobal $VCUBE;\n\t$VCUBE = Array(\n\t\t// Verify a solved first layer 2x2\n\t\t'2FL' => Array(\n\t\t\tArray( -1, -1, -1, -1, -1, -1 ),\n\t\t\tArray( -1, -1, -1, -1, $DFR, $DLF, $DBL, $DRB ),\n\t\t\tArray( -1, -1, -1, -1, 0, 0, 0, 0 ),\n\t\t\tArray( -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 ),\n\t\t\tArray( -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 )),\n\t\t// Verify an oriented first layer 2x2\n\t\t'2OFL' => Array(\n\t\t\tArray( -1, -1, -1, -1, -1, -1 ),\n\t\t\tArray( -1, -1, -1, -1, -1, -1, -1, -1),\n\t\t\tArray( -1, -1, -1, -1, 0, 0, 0, 0 ),\n\t\t\tArray( -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 ),\n\t\t\tArray( -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 )),\n\t\t// Verify orientation is completely solved for 2x2 PBL\n\t\t'2O' => Array(\n\t\t\tArray( -1, -1, -1, -1, -1, -1 ),\n\t\t\tArray( -1, -1, -1, -1, -1, -1, -1, -1 ),\n\t\t\tArray(  0, 0, 0, 0, 0, 0, 0, 0 ),\n\t\t\tArray( -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 ),\n\t\t\tArray( -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 )),\n\t\t// Verify a solved F2L on 3x3\n\t\t'F2L' => Array(\n\t\t\tArray( $U, $R, $F, $D, $L, $B ),\n\t\t\tArray( -1, -1, -1, -1, $DFR, $DLF, $DBL, $DRB ),\n\t\t\tArray( -1, -1, -1, -1, 0, 0, 0, 0 ),\n\t\t\tArray( -1, -1, -1, -1, $DR, $DF, $DL, $DB, $FR, $FL, $BL, $BR ),\n\t\t\tArray( -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0 )),\n\t\t// Verify a solved F2L and OCLL (for OCLL, COLL, ZBLL)\n\t\t'F2L_OCLL' => Array(\n\t\t\tArray( $U, $R, $F, $D, $L, $B ),\n\t\t\tArray( -1, -1, -1, -1, $DFR, $DLF, $DBL, $DRB ),\n\t\t\tArray( -1, -1, -1, -1, 0, 0, 0, 0 ),\n\t\t\tArray( -1, -1, -1, -1, $DR, $DF, $DL, $DB, $FR, $FL, $BL, $BR ),\n\t\t\tArray( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 )),\n\t\t// Verify a solved F2L and T OCLL (for T ZBLL)\n\t\t'F2L_OCLL_T' => Array(\n\t\t\tArray( $U, $R, $F, $D, $L, $B ),\n\t\t\tArray( -1, -1, -1, -1, $DFR, $DLF, $DBL, $DRB ),\n\t\t\tArray(  0,  1,  2,  0, 0, 0, 0, 0 ),\n\t\t\tArray( -1, -1, -1, -1, $DR, $DF, $DL, $DB, $FR, $FL, $BL, $BR ),\n\t\t\tArray( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 )),\n\t\t// Verify a solved F2L and U OCLL (for Headlights ZBLL)\n\t\t'F2L_OCLL_U' => Array(\n\t\t\tArray( $U, $R, $F, $D, $L, $B ),\n\t\t\tArray( -1, -1, -1, -1, $DFR, $DLF, $DBL, $DRB ),\n\t\t\tArray(  0,  2,  1,  0, 0, 0, 0, 0 ),\n\t\t\tArray( -1, -1, -1, -1, $DR, $DF, $DL, $DB, $FR, $FL, $BL, $BR ),\n\t\t\tArray( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 )),\n\t\t// Verify a solved F2L and L OCLL (for L ZBLL)\n\t\t'F2L_OCLL_L' => Array(\n\t\t\tArray( $U, $R, $F, $D, $L, $B ),\n\t\t\tArray( -1, -1, -1, -1, $DFR, $DLF, $DBL, $DRB ),\n\t\t\tArray(  0,  2,  0,  1, 0, 0, 0, 0 ),\n\t\t\tArray( -1, -1, -1, -1, $DR, $DF, $DL, $DB, $FR, $FL, $BL, $BR ),\n\t\t\tArray( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 )),\n\t\t// Verify a solved F2L and H OCLL (for H ZBLL)\n\t\t'F2L_OCLL_H' => Array(\n\t\t\tArray( $U, $R, $F, $D, $L, $B ),\n\t\t\tArray( -1, -1, -1, -1, $DFR, $DLF, $DBL, $DRB ),\n\t\t\tArray(  2,  1,  2,  1, 0, 0, 0, 0 ),\n\t\t\tArray( -1, -1, -1, -1, $DR, $DF, $DL, $DB, $FR, $FL, $BL, $BR ),\n\t\t\tArray( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 )),\n\t\t// Verify a solved F2L and PI OCLL (for PI ZBLL)\n\t\t'F2L_OCLL_PI' => Array(\n\t\t\tArray( $U, $R, $F, $D, $L, $B ),\n\t\t\tArray( -1, -1, -1, -1, $DFR, $DLF, $DBL, $DRB ),\n\t\t\tArray(  1,  2,  2,  1, 0, 0, 0, 0 ),\n\t\t\tArray( -1, -1, -1, -1, $DR, $DF, $DL, $DB, $FR, $FL, $BL, $BR ),\n\t\t\tArray( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 )),\n\t\t// Verify a solved F2L and Sune OCLL (for Sune ZBLL)\n\t\t'F2L_OCLL_S' => Array(\n\t\t\tArray( $U, $R, $F, $D, $L, $B ),\n\t\t\tArray( -1, -1, -1, -1, $DFR, $DLF, $DBL, $DRB ),\n\t\t\tArray(  0,  2,  2,  2, 0, 0, 0, 0 ),\n\t\t\tArray( -1, -1, -1, -1, $DR, $DF, $DL, $DB, $FR, $FL, $BL, $BR ),\n\t\t\tArray( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 )),\n\t\t// Verify a solved F2L and Anti-sune OCLL (for Anti-sune ZBLL)\n\t\t'F2L_OCLL_AS' => Array(\n\t\t\tArray( $U, $R, $F, $D, $L, $B ),\n\t\t\tArray( -1, -1, -1, -1, $DFR, $DLF, $DBL, $DRB ),\n\t\t\tArray(  1,  0,  1,  1, 0, 0, 0, 0 ),\n\t\t\tArray( -1, -1, -1, -1, $DR, $DF, $DL, $DB, $FR, $FL, $BL, $BR ),\n\t\t\tArray( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 )),\n\t\t// Verify a solved F2L and OLL (for PLL)\n\t\t'F2L_OLL' => Array(\n\t\t\tArray( $U, $R, $F, $D, $L, $B ),\n\t\t\tArray( -1, -1, -1, -1, $DFR, $DLF, $DBL, $DRB ),\n\t\t\tArray(  0,  0,  0,  0, 0, 0, 0, 0 ),\n\t\t\tArray( -1, -1, -1, -1, $DR, $DF, $DL, $DB, $FR, $FL, $BL, $BR ),\n\t\t\tArray(  0,  0,  0,  0, 0, 0, 0, 0, 0, 0, 0, 0 )),\n\t\t// Verify a solved F2L and LL corners (for ELL)\n\t\t'F2L_CLL' => Array(\n\t\t\tArray( $U, $R, $F, $D, $L, $B ),\n\t\t\tArray( $URF, $UFL, $ULB, $UBR, $DFR, $DLF, $DBL, $DRB ),\n\t\t\tArray( 0, 0, 0, 0, 0, 0, 0, 0 ),\n\t\t\tArray( -1, -1, -1, -1, $DR, $DF, $DL, $DB, $FR, $FL, $BL, $BR ),\n\t\t\tArray( -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0 )),\n\t\t// Verify a solved F2B (for CMLL)\n\t\t'F2B' => Array(\n\t\t\tArray( -1, $R, -1, -1, $L, -1 ),\n\t\t\tArray( -1, -1, -1, -1, $DFR, $DLF, $DBL, $DRB ),\n\t\t\tArray( -1, -1, -1, -1, 0, 0, 0, 0 ),\n\t\t\tArray( -1, -1, -1, -1, $DR, -1, $DL, -1, $FR, $FL, $BL, $BR ),\n\t\t\tArray( -1, -1, -1, -1, 0, -1, 0, -1, 0, 0, 0, 0 )),\n\t\t// Verify a solved F2L minus last slot (for ELS)\n\t\t'F2LS' => Array(\n\t\t\tArray( $U, $R, $F, $D, $L, $B ),\n\t\t\tArray( -1, -1, -1, -1, -1, $DLF, $DBL, $DRB ),\n\t\t\tArray( -1, -1, -1, -1, -1, 0, 0, 0 ),\n\t\t\tArray( -1, -1, -1, -1, $DR, $DF, $DL, $DB, -1, $FL, $BL, $BR ),\n\t\t\tArray( -1, -1, -1, -1, 0, 0, 0, 0, -1, 0, 0, 0 )),\n\t\t// Verify a solved F2L, minus last slot, with EO and solved FR (for CLS)\n\t\t'F2LS_EO' => Array(\n\t\t\tArray( $U, $R, $F, $D, $L, $B ),\n\t\t\tArray( -1, -1, -1, -1, -1, $DLF, $DBL, $DRB ),\n\t\t\tArray( -1, -1, -1, -1, -1, 0, 0, 0 ),\n\t\t\tArray( -1, -1, -1, -1, $DR, $DF, $DL, $DB, $FR, $FL, $BL, $BR ),\n\t\t\tArray( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ))\n\t);\n\t\n\t// An array storing all cubie-level moves\n\tglobal $CUBIE_MOVES;\n\t$CUBIE_MOVES = Array(\n\t\tArray( // U\n\t\t\tArray( $U, $R, $F, $D, $L, $B ),\n\t\t\tArray( $UBR, $URF, $UFL, $ULB, $DFR, $DLF, $DBL, $DRB ),\n\t\t\tArray( 0, 0, 0, 0, 0, 0, 0, 0 ),\n\t\t\tArray( $UB, $UR, $UF, $UL, $DR, $DF, $DL, $DB, $FR, $FL, $BL, $BR ),\n\t\t\tArray( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 )),\n\t\tArray( // R\n\t\t\tArray( $U, $R, $F, $D, $L, $B ),\n\t\t\tArray( $DFR, $UFL, $ULB, $URF, $DRB, $DLF, $DBL, $UBR ),\n\t\t\tArray( 2, 0, 0, 1, 1, 0, 0, 2 ),\n\t\t\tArray( $FR, $UF, $UL, $UB, $BR, $DF, $DL, $DB, $DR, $FL, $BL, $UR ),\n\t\t\tArray( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 )),\n\t\tArray( // F\n\t\t\tArray( $U, $R, $F, $D, $L, $B ),\n\t\t\tArray( $UFL, $DLF, $ULB, $UBR, $URF, $DFR, $DBL, $DRB ),\n\t\t\tArray( 1, 2, 0, 0, 2, 1, 0, 0 ),\n\t\t\tArray( $UR, $FL, $UL, $UB, $DR, $FR, $DL, $DB, $UF, $DF, $BL, $BR ),\n\t\t\tArray( 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0 )),\n\t\tArray( // D\n\t\t\tArray( $U, $R, $F, $D, $L, $B ),\n\t\t\tArray( $URF, $UFL, $ULB, $UBR, $DLF, $DBL, $DRB, $DFR ),\n\t\t\tArray( 0, 0, 0, 0, 0, 0, 0, 0 ),\n\t\t\tArray( $UR, $UF, $UL, $UB, $DF, $DL, $DB, $DR, $FR, $FL, $BL, $BR ),\n\t\t\tArray( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 )),\n\t\tArray( // L\n\t\t\tArray( $U, $R, $F, $D, $L, $B ),\n\t\t\tArray( $URF, $ULB, $DBL, $UBR, $DFR, $UFL, $DLF, $DRB ),\n\t\t\tArray( 0, 1, 2, 0, 0, 2, 1, 0 ),\n\t\t\tArray( $UR, $UF, $BL, $UB, $DR, $DF, $FL, $DB, $FR, $UL, $DL, $BR ),\n\t\t\tArray( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 )),\n\t\tArray( // B\n\t\t\tArray( $U, $R, $F, $D, $L, $B ),\n\t\t\tArray( $URF, $UFL, $UBR, $DRB, $DFR, $DLF, $ULB, $DBL ),\n\t\t\tArray( 0, 0, 1, 2, 0, 0, 2, 1 ),\n\t\t\tArray( $UR, $UF, $UL, $BR, $DR, $DF, $DL, $BL, $FR, $FL, $UB, $DB ),\n\t\t\tArray( 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1 )),\n\t\tArray( // E\n\t\t\tArray( $U, $F, $L, $D, $B, $R ),\n\t\t\tArray( $URF, $UFL, $ULB, $UBR, $DFR, $DLF, $DBL, $DRB ),\n\t\t\tArray( 0, 0, 0, 0, 0, 0, 0, 0 ),\n\t\t\tArray( $UR, $UF, $UL, $UB, $DR, $DF, $DL, $DB, $FL, $BL, $BR, $FR ),\n\t\t\tArray( 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1 )),\n\t\tArray( // M\n\t\t\tArray( $B, $R, $U, $F, $L, $D ),\n\t\t\tArray( $URF, $UFL, $ULB, $UBR, $DFR, $DLF, $DBL, $DRB ),\n\t\t\tArray( 0, 0, 0, 0, 0, 0, 0, 0 ),\n\t\t\tArray( $UR, $UB, $UL, $DB, $DR, $UF, $DL, $DF, $FR, $FL, $BL, $BR ),\n\t\t\tArray( 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0 )),\n\t\tArray( // S\n\t\t\tArray( $L, $U, $F, $R, $D, $B ),\n\t\t\tArray( $URF, $UFL, $ULB, $UBR, $DFR, $DLF, $DBL, $DRB ),\n\t\t\tArray( 0, 0, 0, 0, 0, 0, 0, 0 ),\n\t\t\tArray( $UL, $UF, $DL, $UB, $UR, $DF, $DR, $DB, $FR, $FL, $BL, $BR ),\n\t\t\tArray( 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0 )));\n\n\t// Now that the elementry moves are defined, the rest of the moves can be built\n\t$CUBIE_MOVES = Array(\n\t\t$CUBIE_MOVES[0],\n\t\t$CUBIE_MOVES[1],\n\t\t$CUBIE_MOVES[2],\n\t\t$CUBIE_MOVES[3],\n\t\t$CUBIE_MOVES[4],\n\t\t$CUBIE_MOVES[5],\n\t\tprod(array_copy($CUBIE_MOVES[0]), $CUBIE_MOVES[6], 3),\n\t\tprod(array_copy($CUBIE_MOVES[1]), $CUBIE_MOVES[7], 3),\n\t\tprod(array_copy($CUBIE_MOVES[2]), $CUBIE_MOVES[8], 1),\n\t\tprod(array_copy($CUBIE_MOVES[3]), $CUBIE_MOVES[6], 1),\n\t\tprod(array_copy($CUBIE_MOVES[4]), $CUBIE_MOVES[7], 1),\n\t\tprod(array_copy($CUBIE_MOVES[5]), $CUBIE_MOVES[8], 3),\n\t\t$CUBIE_MOVES[6],\n\t\t$CUBIE_MOVES[7],\n\t\t$CUBIE_MOVES[8],\n\t\tprod(prod(array_copy($CUBIE_MOVES[0]), $CUBIE_MOVES[3], 3), $CUBIE_MOVES[6], 3),\n\t\tprod(prod(array_copy($CUBIE_MOVES[1]), $CUBIE_MOVES[4], 3), $CUBIE_MOVES[7], 3),\n\t\tprod(prod(array_copy($CUBIE_MOVES[2]), $CUBIE_MOVES[5], 3), $CUBIE_MOVES[8], 1));\n\t\t\n\t// Mapping from power to chr to represent it\n\tglobal $ALG_POW;\n\t$ALG_POW = Array ('', \"2\", \"'\");\n\t\n\t// Returns the case identified by this alg (or -1 if not belonging to group),\n\t// as well an amended alg with any rotations required to make it fit the group\n\tfunction gen_state($moves, $puzzle, $group_id, $is_ll){\n\t\tglobal $SOLVED_CUBE, $CUBIE_MOVES, $VCUBE;\n\n//println(\"\\nGENSTATE:\\ninput moves=$moves\");\n\t\t$moves = trim_rotations($moves, $is_ll);\n\t\n//println(\"trimmed moves=$moves\");\n\t\t\n\t\t// 1. Apply different combinations of initial\n\t\t// and final moves until a solved state is found\n\t\t$prtns = Array(\"\", \"x\", \"x'\", \"x2\", \"z\", \"z'\", \"y\", \"y'\");\n\t\t$frtns = Array(\"\", \"x\", \"x'\", \"x2\", \"z\", \"z'\", \"y\", \"y'\");\n\t\t$valid = false;\n\t\tfor($i = 0; $i < count($frtns) && !$valid; $i++){\n\t\t\tfor($j = 0; $j < count($prtns) && !$valid; $j++){\n\t\t\t\t$cube = case_cube($prtns[$j].$moves.$frtns[$i]);\n//println(\"testing cube: \".$prtns[$j].$moves.$frtns[$i]);\n//printcube($cube, 3);\n\t\t\t\tif(is_member($cube, $group_id)){\n\t\t\t\t\t$prtn = $prtns[$j];\n\t\t\t\t\t$frtn = $frtns[$i];\n\t\t\t\t\t$moves = \"$prtn$moves$frtn\";\n\t\t\t\t\t$valid = true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t// Stop here if alg not valid\n\t\tif(!$valid) return Array(-1, $moves);\n\n//println(\"corrected moves=$prtn$moves$frtn\");\t\t\n\t\t\n\t\t// 2. Find angle to apply alg which results in lowest state id\n\t\t// This is necessery to ensure all rotations of same alg are given same state\n\t\t// Generate 4 case cubes\n\t\t$cubes = case_cubes2($moves, Array(\"\", \"y\", \"y2\", \"y'\"));\n\t\t$state = PHP_INT_MAX;\n\t\tforeach($cubes as $cube){\n//printcube($cube, 3);\n\t\t\t// Check cube from all y rotation angles\n\t\t\tfor($i = 0; $i < 4; $i++){\n\t\t\t\t// Generate identifier for state (depends on alg purpose)\n\t\t\t\t$s = cube_state($cube, $group_id);\n//println($s);\n\t\t\t\t// Set it as main ID if lowest found so-far\n\t\t\t\tif($s < $state && $s != -1) $state = $s;\n\t\t\t\t// Rotate cube by y\n\t\t\t\t$cube = prod($cube, $CUBIE_MOVES[move_id('y')], 1);\n\t\t\t}\n\t\t}\n//println(\"detected state=$state\");\n\t\t// Return cube state and moves which make cube state valid\n\t\treturn Array($state, $prtn, $frtn);\n\t}\n\t// Rotate input alg to match reference alg's orientation and output corrected alg\n\tfunction orient_alg($alg, $ref, $puzzle, $group_id){\n\t\tglobal $SOLVED_CUBE, $CUBIE_MOVES, $ALG_POW;\n\t\t// Remove initial y rotations from alg\n\t\t$alg = preg_replace('/^y[2\\']?/', '', $alg);\n\t\t$cubes = case_cubes2($alg, Array(\"\", \"y\", \"y2\", \"y'\"));\n\t\t$ref_cube = case_cube($ref);\n//print_r2($ref_cube[1]);\n//print_r2($ref_cube[3]);\n\n\t\t$match = false;\n\t\tforeach($cubes as $cube){\n\t\t\t// Now apply up to 3 y twists until the case matches\n\t\t\t$r = 0;\n\t\t\twhile(!$match && $r < 4){\n\t\t\t\t// Detection of identical case (inc cube oriented correctly)\n\t\t\t\t// For OLL all LL orientations should match\n\t\t\t\tswitch($group_id){\n\t\t\t\t\tcase  1: // OLL\n\t\t\t\t\t\t$match = cube_match(array_slice($cube[2], 0, 4), array_slice($ref_cube[2], 0, 4)); // CO\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase  2: // PBL (permutation of all corners)\n\t\t\t\t\t\t$match = cube_match($cube[1], $ref_cube[1]); // CP\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase  3: // CLL (LL orientation + permutation)\n\t\t\t\t\t\t$match = cube_match(array_slice($cube[2], 0, 4), array_slice($ref_cube[2], 0, 4)) // CO\n\t\t\t\t\t\t\t&& cube_match(array_slice($cube[1], 0, 4), array_slice($ref_cube[1], 0, 4)); // CP\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase  4: // OLL\n\t\t\t\t\t\t$match = cube_match(array_slice($cube[2], 0, 4), array_slice($ref_cube[2], 0, 4)) // CO\n\t\t\t\t\t\t\t&& cube_match(array_slice($cube[4], 0, 4), array_slice($ref_cube[4], 0, 4)); // EO\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase  5: // PLL\n\t\t\t\t\t\t$match = cube_match(array_slice($cube[1], 0, 4), array_slice($ref_cube[1], 0, 4)) // CP\n\t\t\t\t\t\t\t&& cube_match(array_slice($cube[3], 0, 4), array_slice($ref_cube[3], 0, 4)); // EP\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase  6: // CLL\n\t\t\t\t\t\t$match = cube_match(array_slice($cube[1], 0, 4), array_slice($ref_cube[1], 0, 4)) // CP\n\t\t\t\t\t\t\t&& cube_match(array_slice($cube[2], 0, 4), array_slice($ref_cube[2], 0, 4)); // CO\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase  7: // ELL\n\t\t\t\t\t\t$match = cube_match(array_slice($cube[3], 0, 4), array_slice($ref_cube[3], 0, 4)) // EP\n\t\t\t\t\t\t\t&& cube_match(array_slice($cube[4], 0, 4), array_slice($ref_cube[4], 0, 4)); // EO\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase  8: // CMLL\n\t\t\t\t\t\t$match = cube_match(array_slice($cube[1], 0, 4), array_slice($ref_cube[1], 0, 4)) // CP\n\t\t\t\t\t\t\t&& cube_match(array_slice($cube[2], 0, 4), array_slice($ref_cube[2], 0, 4)); // CO\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase  9: // COLL\n\t\t\t\t\t\t$match = cube_match(array_slice($cube[1], 0, 4), array_slice($ref_cube[1], 0, 4)) // CP\n\t\t\t\t\t\t\t&& cube_match(array_slice($cube[2], 0, 4), array_slice($ref_cube[2], 0, 4)); // CO\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 10: // ZBLL\n\t\t\t\t\t\t$match = cube_match(array_slice($cube[1], 0, 4), array_slice($ref_cube[1], 0, 4)) // CP\n\t\t\t\t\t\t\t&& cube_match(array_slice($cube[2], 0, 4), array_slice($ref_cube[2], 0, 4)) // CO\n\t\t\t\t\t\t\t&& cube_match(array_slice($cube[3], 0, 4), array_slice($ref_cube[3], 0, 4)); // EP\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 11: // ELS\n\t\t\t\t\t\t$match = cube_match(array_slice($cube[4], 0, 4), array_slice($ref_cube[4], 0, 4)) // EO\n\t\t\t\t\t\t\t&& els_FR($cube) == els_FR($ref_cube);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 12: // CLS \n\t\t\t\t\t\tcube_match(array_slice($cube[2], 0, 4), array_slice($ref_cube[2], 0, 4)); // CO\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 13: // ZBLL-T\n\t\t\t\t\t\t$match = cube_match(array_slice($cube[1], 0, 4), array_slice($ref_cube[1], 0, 4)) // CP\n\t\t\t\t\t\t\t&& cube_match(array_slice($cube[2], 0, 4), array_slice($ref_cube[2], 0, 4)) // CO\n\t\t\t\t\t\t\t&& cube_match(array_slice($cube[3], 0, 4), array_slice($ref_cube[3], 0, 4)); // EP\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 14: // ZBLL-U\n\t\t\t\t\t\t$match = cube_match(array_slice($cube[1], 0, 4), array_slice($ref_cube[1], 0, 4)) // CP\n\t\t\t\t\t\t\t&& cube_match(array_slice($cube[2], 0, 4), array_slice($ref_cube[2], 0, 4)) // CO\n\t\t\t\t\t\t\t&& cube_match(array_slice($cube[3], 0, 4), array_slice($ref_cube[3], 0, 4)); // EP\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 15: // ZBLL-L\n\t\t\t\t\t\t$match = cube_match(array_slice($cube[1], 0, 4), array_slice($ref_cube[1], 0, 4)) // CP\n\t\t\t\t\t\t\t&& cube_match(array_slice($cube[2], 0, 4), array_slice($ref_cube[2], 0, 4)) // CO\n\t\t\t\t\t\t\t&& cube_match(array_slice($cube[3], 0, 4), array_slice($ref_cube[3], 0, 4)); // EP\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 16: // ZBLL-H\n\t\t\t\t\t\t$match = cube_match(array_slice($cube[1], 0, 4), array_slice($ref_cube[1], 0, 4)) // CP\n\t\t\t\t\t\t\t&& cube_match(array_slice($cube[2], 0, 4), array_slice($ref_cube[2], 0, 4)) // CO\n\t\t\t\t\t\t\t&& cube_match(array_slice($cube[3], 0, 4), array_slice($ref_cube[3], 0, 4)); // EP\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 17: // ZBLL-Pi\n\t\t\t\t\t\t$match = cube_match(array_slice($cube[1], 0, 4), array_slice($ref_cube[1], 0, 4)) // CP\n\t\t\t\t\t\t\t&& cube_match(array_slice($cube[2], 0, 4), array_slice($ref_cube[2], 0, 4)) // CO\n\t\t\t\t\t\t\t&& cube_match(array_slice($cube[3], 0, 4), array_slice($ref_cube[3], 0, 4)); // EP\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 18: // ZBLL-S\n\t\t\t\t\t\t$match = cube_match(array_slice($cube[1], 0, 4), array_slice($ref_cube[1], 0, 4)) // CP\n\t\t\t\t\t\t\t&& cube_match(array_slice($cube[2], 0, 4), array_slice($ref_cube[2], 0, 4)) // CO\n\t\t\t\t\t\t\t&& cube_match(array_slice($cube[3], 0, 4), array_slice($ref_cube[3], 0, 4)); // EP\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 19: // ZBLL-As\n\t\t\t\t\t\t$match = cube_match(array_slice($cube[1], 0, 4), array_slice($ref_cube[1], 0, 4)) // CP\n\t\t\t\t\t\t\t&& cube_match(array_slice($cube[2], 0, 4), array_slice($ref_cube[2], 0, 4)) // CO\n\t\t\t\t\t\t\t&& cube_match(array_slice($cube[3], 0, 4), array_slice($ref_cube[3], 0, 4)); // EP\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n//if($match) println(\"r:$r true\");\n//else println(\"r:$r false\");\n//print_r2($cube[1]);\n//print_r2($cube[3]);\n\t\t\t\t// If no match found rotate cube and incrament rotation counter\n\t\t\t\tif(!$match){\n\t\t\t\t\t$cube = prod($cube, $CUBIE_MOVES[move_id('y')], 1);\n\t\t\t\t\t$r++;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif($match) break;\n\t\t}\n\t\t// Rotation required is the inverse of rotations used to match case\n\t\tif($r > 0) $alg = \"y\".$ALG_POW[3 - $r].$alg;\n\t\treturn $alg;\n\t}\n\t// Returns the location of the FR edge for ELS\n\tfunction els_FR($cube){\n\t\tglobal $FR;\n\t\t$frp = 0; // assuming its in position\n\t\tfor($j = 0; $j < 4; $j++){ if($cube[3][$j] == $FR){ $frp = $i + 1; break; }}\n\t\treturn $frp;\n\t}\n\t\n// TODO: This function failes on alg... x' U' R U L' U2 R' U' L' U' L2 u r2 U' z ... part of ZBLL-PI, or H\n// U' l2 u R2 U' R' U' L' U2 R' U L U' ... from ZBLL-PI\n\t// Returns true if the cube state is a member of the given group\n\tfunction is_member($cube, $group_id){\n//echo \"is member of? $group_id :\";\n//printcube($cube, 3);\n\t\tglobal $CUBIE_MOVES;\n\t\t// Check cube from all y rotation angles\n\t\tfor($i = 0; $i < 4; $i++){\n//println(\"|\".$cube[2][0].\", \".$cube[2][1].\", \".$cube[2][2].\", \".$cube[2][3].\"|\");\n//printcube($cube, 3);\n\t\t\tfor($j = 0; $j < 4; $j++){\n\t\t\t\tif(is_member_strict($cube, $group_id)) return true;\n\t\t\t\t// Rotate cube\n\t\t\t\t$cube = prod($cube, $CUBIE_MOVES[move_id('y')], 1);\n\t\t\t}\n\t\t\t// Rotate U-Layer\n\t\t\t$cube = prod($cube, $CUBIE_MOVES[move_id('U')], 1);\n\t\t}\n\t\treturn false;\n\t}\n\t\n\t// Returns true if the cube state is a member of the given group when AUF is allowed\n\tfunction is_member_auf($cube, $group_id){\n//echo \"is member of? $group_id :\";\n//printcube($cube, 3);\n\t\tglobal $CUBIE_MOVES;\n\t\t// Check cube from all auf rotation angles\n\t\tfor($i = 0; $i < 4; $i++){\n//println(\"|\".$cube[2][0].\", \".$cube[2][1].\", \".$cube[2][2].\", \".$cube[2][3].\"|\");\n//printcube($cube, 3);\n\t\t\tif(is_member_strict($cube, $group_id)) return true;\n\t\t\t// Rotate U-Layer\n\t\t\t$cube = prod($cube, $CUBIE_MOVES[move_id('U')], 1);\n\t\t}\n\t\treturn false;\n\t}\n\t\n\tfunction is_member_strict($cube, $group_id){\n\t\tglobal $VCUBE;\n\t\tswitch($group_id){\n\t\t\tcase  1: if(cube_match($cube, $VCUBE['2OFL']    )) return true; break; // OLL\n\t\t\tcase  2: if(cube_match($cube, $VCUBE['2O']      )) return true; break; // PBL\n\t\t\tcase  3: if(cube_match($cube, $VCUBE['2FL']     )) return true; break; // CLL\n\t\t\tcase  4: if(cube_match($cube, $VCUBE['F2L']     )) return true; break; // OLL\n\t\t\tcase  5: if(cube_match($cube, $VCUBE['F2L_OLL'] )) return true; break; // PLL\n\t\t\tcase  6: if(cube_match($cube, $VCUBE['F2L']     )) return true; break; // CLL\n\t\t\tcase  7: if(cube_match($cube, $VCUBE['F2L_CLL'] )) return true; break; // ELL\n\t\t\tcase  8: if(cube_match($cube, $VCUBE['F2B']     )) return true; break; // CMLL\n\t\t\tcase  9: if(cube_match($cube, $VCUBE['F2L_OCLL'])) return true; break; // COLL\n\t\t\tcase 10: if(cube_match($cube, $VCUBE['F2L_OCLL'])) return true; break; // ZBLL\n\t\t\tcase 11: if(cube_match($cube, $VCUBE['F2LS']    )) return true; break; // ELS\n\t\t\tcase 12: if(cube_match($cube, $VCUBE['F2LS_EO'] )) return true; break; // CLS\n\t\t\tcase 13: if(cube_match($cube, $VCUBE['F2L_OCLL_T'])) return true; break; // ZBLL-T\n\t\t\tcase 14: if(cube_match($cube, $VCUBE['F2L_OCLL_U'])) return true; break; // ZBLL-U\n\t\t\tcase 15: if(cube_match($cube, $VCUBE['F2L_OCLL_L'])) return true; break; // ZBLL-L\n\t\t\tcase 16: if(cube_match($cube, $VCUBE['F2L_OCLL_H'])) return true; break; // ZBLL-H\n\t\t\tcase 17: if(cube_match($cube, $VCUBE['F2L_OCLL_PI'])) return true; break; // ZBLL-PI\n\t\t\tcase 18: if(cube_match($cube, $VCUBE['F2L_OCLL_S'])) return true; break; // ZBLL-S\n\t\t\tcase 19: if(cube_match($cube, $VCUBE['F2L_OCLL_AS'])) return true; break; // ZBLL-AS\n\t\t}\n\t\treturn false;\n\t}\n\t\n\t// Returns a value uniquely identifying this cube state in this group\n\tfunction cube_state($cube, $group_id){\n\t\tswitch($group_id){\n\t\t\tcase  1: return encode_o(array_slice($cube[2], 0, 3), 3); // CO   (2x2 OLL)\n\t\t\tcase  2: return encode_p($cube[1]); // CP (all corners)           (PBL)\n\t\t\tcase  3: return encode_o(array_slice($cube[2], 0, 3), 3) // CO    (2x2 CLL)\n\t\t\t\t+ encode_p(array_slice($cube[1], 0, 4)) * 27; // CP\n\t\t\tcase  4: return encode_o(array_slice($cube[2], 0, 3), 3) // CO    (OLL)\n\t\t\t\t+ encode_o(array_slice($cube[4], 0, 3), 2) * 27; // EO (* 3^3)\n\t\t\tcase  5: return encode_p(array_slice($cube[1], 0, 4), 3) // CP    (PLL)\n\t\t\t\t+ encode_p(array_slice($cube[3], 0, 4)) * 24; // EO (* 4!)\n\t\t\tcase  6: return encode_o(array_slice($cube[2], 0, 3), 3) // CO     (CLL)\n\t\t\t\t+ encode_p(array_slice($cube[1], 0, 4)) * 27; // CP\n\t\t\tcase  7: return encode_o(array_slice($cube[4], 0, 3), 2) // EO     (ELL)\n\t\t\t\t+ encode_p(array_slice($cube[3], 0, 4)) * 8; // EP\n\t\t\tcase  8: return encode_o(array_slice($cube[2], 0, 3), 3) // CO     (CMLL)\n\t\t\t\t+ encode_p(array_slice($cube[1], 0, 4)) * 27; // CP\n\t\t\tcase  9: return encode_o(array_slice($cube[2], 0, 3), 3) // CO     (COLL)\n\t\t\t\t+ encode_p(array_slice($cube[1], 0, 4)) * 27; // CP\n\t\t\tcase 10: return encode_o(array_slice($cube[2], 0, 3), 3) // CO    (ZBLL)\n\t\t\t\t+ encode_p(array_slice($cube[1], 0, 4)) * 27 // CP\n\t\t\t\t+ encode_p(array_slice($cube[3], 0, 4)) * 27 * 24; // EP\n\t\t\tcase 11: // ELS\n\t\t\t\t// This must encode orientation of the 4 top edges (fifth is determined by other 4)\n\t\t\t\t// along with the position of the FR edge\n\t\t\t\treturn encode_o(array_slice($cube[4], 0, 4), 2)\n\t\t\t\t+ els_FR($cube) * 16; // EO(5 edges) + position of FR\n\t\t\tcase 12: // CLS must track the orientation of the 5 corners (determined by o of 4 top ones)\n\t\t\t\treturn encode_o(array_slice($cube[2], 0, 4), 3); // CO\n\t\t\tcase 13: return encode_o(array_slice($cube[2], 0, 3), 3) // CO    (ZBLL-T)\n\t\t\t\t+ encode_p(array_slice($cube[1], 0, 4)) * 27 // CP\n\t\t\t\t+ encode_p(array_slice($cube[3], 0, 4)) * 27 * 24; // EP\n\t\t\tcase 14: return encode_o(array_slice($cube[2], 0, 3), 3) // CO    (ZBLL-U)\n\t\t\t\t+ encode_p(array_slice($cube[1], 0, 4)) * 27 // CP\n\t\t\t\t+ encode_p(array_slice($cube[3], 0, 4)) * 27 * 24; // EP\n\t\t\tcase 15: return encode_o(array_slice($cube[2], 0, 3), 3) // CO    (ZBLL-L)\n\t\t\t\t+ encode_p(array_slice($cube[1], 0, 4)) * 27 // CP\n\t\t\t\t+ encode_p(array_slice($cube[3], 0, 4)) * 27 * 24; // EP\n\t\t\tcase 16: return encode_o(array_slice($cube[2], 0, 3), 3) // CO    (ZBLL-Pi)\n\t\t\t\t+ encode_p(array_slice($cube[1], 0, 4)) * 27 // CP\n\t\t\t\t+ encode_p(array_slice($cube[3], 0, 4)) * 27 * 24; // EP\n\t\t\tcase 17: return encode_o(array_slice($cube[2], 0, 3), 3) // CO    (ZBLL-H)\n\t\t\t\t+ encode_p(array_slice($cube[1], 0, 4)) * 27 // CP\n\t\t\t\t+ encode_p(array_slice($cube[3], 0, 4)) * 27 * 24; // EP\n\t\t\tcase 18: return encode_o(array_slice($cube[2], 0, 3), 3) // CO    (ZBLL-S)\n\t\t\t\t+ encode_p(array_slice($cube[1], 0, 4)) * 27 // CP\n\t\t\t\t+ encode_p(array_slice($cube[3], 0, 4)) * 27 * 24; // EP\n\t\t\tcase 19: return encode_o(array_slice($cube[2], 0, 3), 3) // CO    (ZBLL-AS)\n\t\t\t\t+ encode_p(array_slice($cube[1], 0, 4)) * 27 // CP\n\t\t\t\t+ encode_p(array_slice($cube[3], 0, 4)) * 27 * 24; // EP\n\t\t}\n\t\treturn -1;\n\t}\n\t// Returs the cube-state the given alg solves\n\tfunction case_cube($alg){\n\t\tglobal $SOLVED_CUBE, $F2L_CUBE, $CUBIE_MOVES;\n\t\t// Apply inverse to get state which alg solves\n\t\t$alg = invert_alg($alg);\n\t\t$cube = array_copy($SOLVED_CUBE);\n\t\treturn apply_alg($alg, $cube);\n\t}\n\t// Returns a set of case cubes representing the state of the\n\t// cube the alg solves, given the rotation was applied first\n\tfunction case_cubes($alg, $prerot){\n\t\tglobal $SOLVED_CUBE;\n\t\t// Apply inverse to get state which alg solves\n\t\t$alg = invert_alg($alg);\n\t\tforeach($prerot as $i => $rtn){\n\t\t\t$cubes[$i] = apply_alg($alg.invert_alg($rtn), array_copy($SOLVED_CUBE));\n\t\t}\n\t\treturn $cubes;\n\t}\n\t// Returns a set of case cubes representing the state of the\n\t// cube the alg solves, given the rotation was applied last\n\tfunction case_cubes2($alg, $postrot){\n\t\tglobal $SOLVED_CUBE;\n\t\t// Apply inverse to get state which alg solves\n\t\t$alg = invert_alg($alg);\n\t\tforeach($postrot as $i => $rtn){\n\t\t\t$cubes[$i] = apply_alg(invert_alg($rtn).$alg, array_copy($SOLVED_CUBE));\n\t\t}\n\t\treturn $cubes;\n\t}\n\t// Returns the move required to rotate the cube to an upright position\n\tfunction upright($cube){\n\t\tglobal $U, $R, $F, $D, $L, $B, $CUBIE_MOVES;\n\t\t// search for U face centre\n\t\t$upos = 0;\n\t\tforeach($cube[0] as $i => $c){\n\t\t\tif($c == $U){\n\t\t\t\t$upos = $i;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\t$move = \"\";\n\t\tswitch($upos){\n\t\t\tcase $R : $move = \"z'\"; break;\n\t\t\tcase $L : $move = \"z\"; break;\n\t\t\tcase $F : $move = \"x\"; break;\n\t\t\tcase $B : $move = \"x'\"; break;\n\t\t\tcase $D : $move = \"x2\"; break;\n\t\t}\n\t\treturn $move;\n\t}\n\t\n\t// Convert cubie cube to face cube, using the default facelet identifiers\n\tfunction face_cube($cube, $dim){\n\t\t// Construct default facelet id scheme\n\t\tfor($f = 0; $f < 6; $f++){\n\t\t\tfor($i = 0; $i < $dim; $i++){\n\t\t\t\tfor($j = 0; $j < $dim; $j++) $fd .= $f;\n\t\t\t}\n\t\t}\n\t\treturn facelet_cube($cube, $dim, $fd);\n\t}\n\t\n\t\n\t// Convert cubie cube to facelet cube mapping each facelet\n\t// to the given facelet id sequence\n\tfunction facelet_cube($cube, $d, $fi){\n\t\tglobal $U, $R, $F, $D, $L, $B;\n\t\t// Facelet constants\n\t\t\n\t\t// Dimension/2\n\t\t$h = (int)($d/2);\n\t\t// Dimension squared\n\t\t$s = $d * $d;\n\t\t// Half of dimension squared\n\t\t$m = (int)($s/2);\n\t\t\n\t\t// Map centre positions to facelet ids\n\t\t$mfid = Array($fi[$U*$s+$m], $fi[$R*$s+$m], $fi[$F*$s+$m], $fi[$D*$s+$m], $fi[$L*$s+$m], $fi[$B*$s+$m]);\n\n\t\t// Map the corner positions to facelet ids\n\t\t$cfid = Array(\n\t\t\tArray($fi[ ($U+1)*$s-1], $fi[      $R*$s], $fi[  $F*$s+$d-1]),\n\t\t\tArray($fi[($U+1)*$s-$d], $fi[      $F*$s], $fi[  $L*$s+$d-1]),\n\t\t\tArray($fi[       $U*$s], $fi[      $L*$s], $fi[  $B*$s+$d-1]),\n\t\t\tArray($fi[  $U*$s+$d-1], $fi[      $B*$s], $fi[  $R*$s+$d-1]),\n\t\t\tArray($fi[  $D*$s+$d-1], $fi[($F+1)*$s-1], $fi[($R+1)*$s-$d]),\n\t\t\tArray($fi[       $D*$s], $fi[($L+1)*$s-1], $fi[($F+1)*$s-$d]),\n\t\t\tArray($fi[($D+1)*$s-$d], $fi[($B+1)*$s-1], $fi[($L+1)*$s-$d]),\n\t\t\tArray($fi[ ($D+1)*$s-1], $fi[($R+1)*$s-1], $fi[($B+1)*$s-$d]));\n\t\t\n\t\t// Map the edge positions to facelet ids\n\t\t$efid = Array(\n\t\t\tArray($fi[$U*$s+$m+$h], $fi[      $R*$s+$h]), Array($fi[($U+1)*$s-1-$h], $fi[      $F*$s+$h]),\n\t\t\tArray($fi[$U*$s+$m-$h], $fi[      $L*$s+$h]), Array($fi[      $U*$s+$h], $fi[      $B*$s+$h]),\n\t\t\tArray($fi[$D*$s+$m+$h], $fi[($R+1)*$s-1-$h]), Array($fi[      $D*$s+$h], $fi[($F+1)*$s-1-$h]),\n\t\t\tArray($fi[$D*$s+$m-$h], $fi[($L+1)*$s-1-$h]), Array($fi[($D+1)*$s-1-$h], $fi[($B+1)*$s-1-$h]),\n\t\t\tArray($fi[$F*$s+$m+$h], $fi[   $R*$s+$m-$h]), Array($fi[   $F*$s+$m-$h], $fi[   $L*$s+$m+$h]),\n\t\t\tArray($fi[$B*$s+$m+$h], $fi[   $L*$s+$m-$h]), Array($fi[   $B*$s+$m-$h], $fi[   $R*$s+$m+$h]));\n\t\t\t\n\n\n//print_r($efid);\n/*\n\t\t// Map the corner positions to facelets\n\t\t$ccol = Array(\n\t\t\tArray($U, $R, $F), Array($U, $F, $L), Array($U, $L, $B), Array($U, $B, $R),\n\t\t\tArray($D, $F, $R), Array($D, $L, $F), Array($D, $B, $L), Array($D, $R, $B));\n\t\t\n\t\t// Map the edge positions to facelets\n\t\t$ecol = Array(\n\t\t\tArray($U, $R), Array($U, $F), Array($U, $L),\n\t\t\tArray($U, $B), Array($D, $R), Array($D, $F),\n\t\t\tArray($D, $L), Array($D, $B), Array($F, $R),\n\t\t\tArray($F, $L), Array($B, $L), Array($B, $R));\n*/\n\t\t// Map of centre facelet positions\n\t\t$mpos = Array($U*$s+$m, $R*$s+$m, $F*$s+$m, $D*$s+$m, $L*$s+$m, $B*$s+$m);\n\t\t\n\t\t// Map of corner facelet positions (for any dimensoin of cube)\n\t\t$cpos = Array(\n\t\t\tArray( ($U+1)*$s-1,       $R*$s,   $F*$s+$d-1), Array(($U+1)*$s-$d,       $F*$s,   $L*$s+$d-1),\n\t\t\tArray(       $U*$s,       $L*$s,   $B*$s+$d-1), Array(  $U*$s+$d-1,       $B*$s,   $R*$s+$d-1),\n\t\t\tArray(  $D*$s+$d-1, ($F+1)*$s-1, ($R+1)*$s-$d), Array(       $D*$s, ($L+1)*$s-1, ($F+1)*$s-$d),\n\t\t\tArray(($D+1)*$s-$d, ($B+1)*$s-1, ($L+1)*$s-$d), Array( ($D+1)*$s-1, ($R+1)*$s-1, ($B+1)*$s-$d));\n\t\t\n\t\t// Map edge facelet positions (for any dimensoin)\n\t\t$epos = Array(\n\t\t\tArray($U*$s+$m+$h,       $R*$s+$h), Array(($U+1)*$s-1-$h,       $F*$s+$h), Array($U*$s+$m-$h,       $L*$s+$h),\n\t\t\tArray(   $U*$s+$h,       $B*$s+$h), Array(   $D*$s+$m+$h, ($R+1)*$s-1-$h), Array(   $D*$s+$h, ($F+1)*$s-1-$h),\n\t\t\tArray($D*$s+$m-$h, ($L+1)*$s-1-$h), Array(($D+1)*$s-1-$h, ($B+1)*$s-1-$h), Array($F*$s+$m+$h,    $R*$s+$m-$h),\n\t\t\tArray($F*$s+$m-$h,    $L*$s+$m+$h), Array(   $B*$s+$m+$h,    $L*$s+$m-$h), Array($B*$s+$m-$h,    $R*$s+$m+$h));\n\t\t\t\n\t\t// Corners\n\t\tfor($i = 0; $i < 8; $i++){\n\t\t\t$j = $cube[1][$i]; // cornercubie with index j is at\n\t\t\t// cornerposition with index i\n\t\t\t$o = $cube[2][$i]; // Orientation of this cubie\n\t\t\tfor($n = 0; $n < 3; $n++) $fo[$cpos[$i][($n + $o) % 3]] = $cfid[$j][$n];\n\t\t}\n//print_r($mfid);\n//echo \"\\n<br/>\";\n\t\t// Pieces only applicable to odd sized puzzles\n\t\tif($d % 2 == 1){ \n\t\t\t// Centers\n\t\t\tfor($i = 0; $i < 6; $i++){\n//echo \"\\n<br/>\".$cube[0][$i];\n\t\t\t\t$fo[$mpos[$i]] = $mfid[$cube[0][$i]];\n\t\t\t}\n\t\t\t// Centre edges \n\t\t\tfor($i = 0; $i < 12; $i++){\n\t\t\t\t$j = $cube[3][$i]; // edgecubie with index j is at edgeposition with index i\n\t\t\t\t$o = $cube[4][$i]; // Orientation of this cubie\n\t\t\t\tfor($n = 0; $n < 2; $n++) $fo[$epos[$i][($n + $o) % 2]] = $efid[$j][$n];\n\t\t\t}\n\t\t}\n//print_r( $fo);\n\t\treturn $fo;\n\t}\n\t\n\t// Convert cubie cube to letter cube (letters representing facelets)\n\tfunction letter_cube($cube, $dim){\n\t\tglobal $FACE_NAMES;\n\t\t$fc = face_cube($cube, $dim);\n\t\tfor($i = 0; $i < count($fc); $i++){\n\t\t\t$lc[$i] = $FACE_NAMES[$fc[$i]];\n\t\t}\n\t\treturn implode($lc);\n\t}\n\t\n\t// Convert cubie cube to colour cube\n\tfunction col_cube($cube, $dim){\n\t\t// Sheme mapping\n\t\t$FACE_COL = Array(\n\t\t\t'u' => 'y',\n\t\t\t'r' => 'r',\n\t\t\t'f' => 'b',\n\t\t\t'd' => 'w',\n\t\t\t'l' => 'o',\n\t\t\t'b' => 'g');\n\t\t$fc = face_cube($cube, $dim);\n\t\t// Translate face defs into colour defs\n\t\tfor($i = 0; $i < strlen($fc); $i++){\n\t\t\t$col .= $FACE_COL[$fc[$i]];\n\t\t}\n\t\treturn $col;\n\t}\n\t\n\t// Print a cube to screen for debugging\n\tfunction printcube($cube, $dim){\n\t\t$fc = letter_cube($cube, $dim);\n\t\tprintln(\"<img src=\\\"visualcube.php?fmt=gif&fd=$fc\\\">\");\n\t}\n\t\n\t// Applys an alg to the given cube\n\tfunction apply_alg($alg, $cube){\n\t\tglobal $CUBIE_MOVES;\n\t\t$i = 0;\n\t\t$len = strlen($alg);\n\t\twhile($i < $len){\n\t\t\t$move = move_id(substr($alg, $i, 1));\n\t\t\tif($move >= 0){\n\t\t\t\t$pow = move_pow(substr($alg, $i+1, 1));\n\t\t\t\tif($pow > 1) $i++;\n\t\t\t\t// Make the move\n\t\t\t\t$cube = prod($cube, $CUBIE_MOVES[$move], $pow);\n\t\t\t}\n\t\t\t$i++;\n\t\t}\n\t\treturn $cube;\n\t}\n\t// Formats an inputed alg to remove dissallowed characters and standardise notation\n\tfunction format_alg($moves){\n\t\t// Remove characters not allowed in an alg\n\t\t$r = preg_replace('/[^UDLRFBudlrfbMESxyzw\\'`23]/', '', $moves);\n\t\t$r = preg_replace('/[3`]/', \"'\", $r); // Replace 3 or ` with a '\n\t\t$r = preg_replace('/2\\'|\\'2/', \"2\", $r); // Replace 2' or '2 with a 2\n\t\t// Fix wide notation\n\t\tif(preg_match('/w/', $r)){\n\t\t\t$r = preg_replace('/Uw/', 'u', $r);\n\t\t\t$r = preg_replace('/Rw/', 'r', $r);\n\t\t\t$r = preg_replace('/Fw/', 'f', $r);\n\t\t\t$r = preg_replace('/Dw/', 'd', $r);\n\t\t\t$r = preg_replace('/Lw/', 'l', $r);\n\t\t\t$r = preg_replace('/Bw/', 'b', $r);\n\t\t\t// now remove any extra w's\n\t\t\t$r = preg_replace('/w/', '', $r);\n\t\t}\n\t\t// Merge multiple moves/rotations of same face\n//println( \"init moves=|$moves|\");\n\t\treturn compress_alg($r);\n//println(\"compressed moves=|$moves|\");\t\n\t}\n\t// Removes all initial and final rotations from a cube\n\tfunction trim_rotations($alg, $is_ll){\n//echo \"isll?$is_ll\";\n\t\t// Remove AUFs if its an LL alg\n\t\tif($is_ll) $alg = remove_auf($alg);\n\t\t\n\t\t// Strip all initial rotations\n\t\t$alg = preg_replace('/^([xyz][2\\']?)+/', '', $alg);\n\t\t// Strip all final rotations\n\t\t$alg = preg_replace('/([xyz][2\\']?)+$/', '', $alg);\n//echo \"moves=|$moves|\";\n\t\treturn $alg;\n\t}\n\t// Removes AUFs and replaces them with y rotations\n\tfunction remove_auf($alg){\n\t\t$n = strlen($alg);\n\t\tfor($i = 0; $i < $n; $i++){\n\t\t\t$c = substr($alg, $i, 1);\n\t\t\tif($c == 'U') $alg[$i] = 'y';\n\t\t\telse if(preg_match('/[^yU2\\'\\s]/', $c)) // anything other than y and U turns are no longer aufs\n\t\t\t\tbreak;\n\t\t}\n\t\tfor($i = $n - 1; $i > -1; $i--){\n\t\t\t$c = substr($alg, $i, 1);\n\t\t\tif($c == 'U') $alg[$i] = 'y';\n\t\t\telse if(preg_match('/[^yU2\\'\\s]/', $c)) // anything other than y and U turns are no longer aufs\n\t\t\t\tbreak;\n\t\t}\n\t\treturn compress_alg($alg);\n\t}\n\t// Inserts spaces in an alg for display\n\tfunction expand_alg($alg){\n\t\t$n = strlen($alg);\n\t\t$i = 1;\n\t\t$exp = substr($alg, 0, 1);\n\t\twhile($i < $n){\n\t\t\t$c = substr($alg, $i, 1);\n\t\t\tif(move_id($c) != -1) $exp .= \" \";\n\t\t\t$exp .= $c;\n\t\t\t$i++;\n\t\t}\n\t\treturn $exp;\n\t}\n\t// Merges unnecessery repeated moves of the same face\n\tfunction compress_alg($alg){\n\t\tglobal $ALG_POW;\n\t\t$merge_done = true;\n\t\twhile($merge_done && strlen($alg) > 1){\n\t\t\t$n = strlen($alg);\n\t\t\t$i = 0;\n\t\t\t$merge_done = false;\n\t\t\twhile($i < $n){\n\t\t\t\t$move = $alg[$i];\n\t\t\t\tif(move_id($move) != -1){\n\t\t\t\t\t$pow = 1;\n\t\t\t\t\tif($i < $n -1) $pow = move_pow($alg[$i + 1]);\n\t\t\t\t\tif($pow > 1) $i++;\n\t\t\t\t\t// If moves the same, then simply add up powers\n\t\t\t\t\tif($lmove == $move){\n\t\t\t\t\t\t$lpow += $pow;\n\t\t\t\t\t\t$merge_done = true;\n\t\t\t\t\t}\n\t\t\t\t\t// Otherwise, last move can be added to alg\n\t\t\t\t\telse{\n\t\t\t\t\t\t$lpow = $lpow % 4;\n\t\t\t\t\t\tif($lpow > 0) $malg .= $lmove . $ALG_POW[$lpow-1];\n\t\t\t\t\t\t$lpow = $pow;\n\t\t\t\t\t\t$lmove = $move;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t$i++;\t\t\t\n\t\t\t}\n\t\t\t// Add final move\n\t\t\t$lpow = $lpow % 4;\n\t\t\tif($lpow > 0) $malg .= $lmove . $ALG_POW[$lpow-1];\n\t\t\t$alg = $malg;\n\t\t\t$malg = null;\n\t\t\t$lmove = null;\n\t\t\t$lpow = null;\n\t\t}\n\t\treturn $alg;\n\t}\n\t\n\t// Inverts an NxN cube algorithm\n\tfunction invert_alg($alg){\n\t\tglobal $ALG_POW;\n\t\t$inv = \"\";\n\t\t$pow = 1;\n\t\t$pre = '';\n\t\t$i = strlen($alg) - 1;\n\t\twhile($i >= 0){\n\t\t\t$c = substr($alg, $i, 1);\n\t\t\t$mv = fcs_move_id($c);\n\t\t\tif($mv != -1){\n\t\t\t\t// Retrive layer depth\n\t\t\t\tif($i > 0){\n\t\t\t\t\t$pre = substr($alg, $i-1, 1);\n\t\t\t\t\tif(!is_numeric($pre) || ($i > 1\n\t\t\t\t\t&& fcs_move_id(substr($alg, $i-2, 1)) != -1))\n\t\t\t\t\t\t$pre = '';\n\t\t\t\t\telse\t$i--;\n\t\t\t\t}\n\t\t\t\t// Invert and add the move\n\t\t\t\t$inv .= $pre . $c . $ALG_POW[3 - $pow] . ' ';\n\t\t\t\t$pow = 1; $pre = '';\n\t\t\t}\n\t\t\telse $pow = move_pow(substr($alg, $i, 1));\n\t\t\t$i--;\n\t\t}\n\t\treturn $inv;\n\t}\n\t\t\n\t// Returns an array of algorithm statistics\n\t// including, STM, HTM, QTM and GEN\n\tfunction alg_stats($alg){\n\t\t$n = strlen($alg);\n\t\t$i = 0;\n\t\t$gen = Array(0, 0, 0, 0, 0, 0, 0, 0, 0);\n\t\twhile($i < $n){\n\t\t\t$move = move_id(substr($alg, $i, 1));\n\t\t\tif($move != -1){\n\t\t\t\t$pow = 1;\n\t\t\t\tif($i < $n -1) $pow = move_pow(substr($alg, $i + 1, 1));\n\t\t\t\t// If move not a rotation\n\t\t\t\tif($move <= 14){\n\t\t\t\t\t$stm++;\n\t\t\t\t\t$htm++;\n\t\t\t\t\t$qtm_ = 1;\n\t\t\t\t\t// If its a slice move\n\t\t\t\t\tif($move >= 12 && $move <=14){\n\t\t\t\t\t\t$htm++;\n\t\t\t\t\t\t$qtm_ = 2;\n\t\t\t\t\t}\n\t\t\t\t\tif($pow == 2) $qtm_ *= 2;\n\t\t\t\t\t$qtm += $qtm_;\n\t\t\t\t\t$gen[$move >= 6 ? $move - 6 : $move] = 1;\n\t\t\t\t}\n\t\t\t}\n\t\t\t$i++;\n\t\t}\n\t\t$gn = 0;\n\t\tforeach($gen as $g) $gn += $g;\n\t\treturn Array($stm, $htm, $qtm, $gn);\n\t}\n\t\n\t// Encode orientation\n\tfunction encode_o($data, $mod){\n\t\t$o = 0;\n\t\tfor($i = 0; $i < count($data); $i++){\n\t\t\t$o = $o * $mod + $data[$i];\n\t\t}\n\t\treturn $o;\n\t}\n\t\n\t// Encode permutation\n\tfunction encode_p($data){\n\t\t$p = 0;\n\t\t$n = count($data);\n\t\tfor($i = 0; $i <  $n - 1; $i++){\n\t\t\t$p = $p * ($n - $i + 1);\n\t\t\tfor($j = $i + 1; $j < $n; $j++){\n\t\t\t\tif($data[$i] > $data[$j])\n\t\t\t\t\t$p += 1;\n\t\t\t}\n\t\t}\n\t\treturn $p;\n\t}\n\t\n\t// Returns whether the cubes match\n\t// Entries of -1 are counted as matching\n\tfunction cube_match($cube1, $cube2){\n\t\tfor($i = 0; $i < count($cube1); $i++){\n\t\t\tif(is_array($cube1[$i])){\n\t\t\t\tif(!cube_match($cube1[$i], $cube2[$i])) return false;\n\t\t\t}else if(!(\n\t\t\t\t$cube1[$i] == $cube2[$i] \n\t\t\t\t|| $cube1[$i] == -1\n\t\t\t\t|| $cube2[$i] == -1))\n\t\t\t\treturn false;\n\n\t\t}\n\t\treturn true;\n\t}\n\n\t// Permutes and orients cube1 by cube2 n times\n\tfunction prod($cube1, $cube2, $n){\n\t\tfor($i = 0; $i < $n; $i++){\n\t\t\t// Centres\n\t\t\t$tmp = Array();\n\t\t\tfor($m = 0; $m < 6; $m++){\n\t\t\t\t// Permute center\n\t\t\t\t$tmp[0][$m] = $cube1[0][$cube2[0][$m]];\n\t\t\t}\n\t\t\t// Corners\n\t\t\tfor($c = 0; $c < 8; $c++){\n\t\t\t\t// Permute corner\n\t\t\t\t$tmp[1][$c] = $cube1[1][$cube2[1][$c]];\n\t\t\t\t// Orient corner\n\t\t\t\t$tmp[2][$c] = ($cube1[2][$cube2[1][$c]] + $cube2[2][$c]) % 3;\n\t\t\t}\n\t\t\t// Edges\n\t\t\tfor($e = 0; $e < 12; $e++){\n\t\t\t\t// Permute edge\n\t\t\t\t$tmp[3][$e] = $cube1[3][$cube2[3][$e]];\n\t\t\t\t// Orient edge\n\t\t\t\t$tmp[4][$e] = ($cube1[4][$cube2[3][$e]] + $cube2[4][$e]) % 2;\n\t\t\t}\n\t\t\t$cube1 = $tmp;\n\t\t}\n\t\treturn $cube1;\n\t}\n\n\t// Returns the power of a move with given suffix\n\tfunction move_pow($char){\n\t\tswitch($char){\n\t\t\tcase \"2\" : return 2;\n\t\t\tcase \"'\" : return 3;\n\t\t\tcase \"3\" : return 3;\n\t\t}\n\t\treturn 1;\n\t}\n\t\n\t// Maps move names to a move id\n\tfunction move_id($move){\n\t\tswitch($move){\n\t\t\tcase 'U': return 0;\n\t\t\tcase 'R': return 1;\n\t\t\tcase 'F': return 2;\n\t\t\tcase 'D': return 3;\n\t\t\tcase 'L': return 4;\n\t\t\tcase 'B': return 5;\n\t\t\tcase 'u': return 6;\n\t\t\tcase 'r': return 7;\n\t\t\tcase 'f': return 8;\n\t\t\tcase 'd': return 9;\n\t\t\tcase 'l': return 10;\n\t\t\tcase 'b': return 11;\n\t\t\tcase 'E': return 12;\n\t\t\tcase 'M': return 13;\n\t\t\tcase 'S': return 14;\n\t\t\tcase 'y': return 15;\n\t\t\tcase 'x': return 16;\n\t\t\tcase 'z': return 17;\n\t\t}\n\t\treturn -1;\n\t}\n\n\n\n\t// -----------------[ NxNxN Face Cube ]---------------\n\t\n\t// Permutes an NxNxN face definition according to the given alg\n\tfunction fcs_doperm($fcs, $alg, $dim){\n\t\t$nf = $dim * $dim;\n\t\t$mt = Array();\n\t\t// Generate basic move tables\n\t\t// Twist generic face\n\t\t$fc_twist = Array();\n\t\tfor($row = 0; $row < $dim; $row++){\n\t\t\tfor($col = 0; $col < $dim; $col++){\n\t\t\t\t$fc_twist[$row * $dim + $col] = ($dim - $col - 1) * $dim + $row;\n\t\t\t}\n\t\t}\n\t\t// Inverse twist\n\t\t$fc_twist2 = Array();\n\t\tfor($row = 0; $row < $dim; $row++){\n\t\t\tfor($col = 0; $col < $dim; $col++){\n\t\t\t\t$fc_twist2[$row * $dim + $col] = $col * $dim + $dim - $row - 1;\n\t\t\t}\n\t\t}\n\t\t\n\t\t// Face order for slice turns\n\t\t$sl_fo = Array(\n\t\t\tArray(6, 5, 1, 6, 2, 4), // x\n\t\t\tArray(2, 6, 3, 5, 6, 0), // y\n\t\t\tArray(4, 0, 6, 1, 3, 6), // z\n\t\t\tArray(6, 2, 4, 6, 5, 1), // x'\n\t\t\tArray(5, 6, 0, 2, 6, 3), // y'\n\t\t\tArray(1, 3, 6, 4, 0, 6), // z'\n\t\t);\n\t\t// Sticker order for slice turns\n\t\t$sl_so = Array( // 1=+col+row, 2=-col+row, 3=+col-row, 4=-col-row,\n\t\t\t\t// 5=+row+col, 6=-row+col, 7=+row-col, 8=-row-col, 0=null\n\t\t\tArray(0, 1, 1, 0, 1, 1), // x\n\t\t\tArray(8, 0, 8, 8, 0, 5), // y\n\t\t\tArray(4, 7, 0, 1, 6, 0), // z\n\t\t);\n\t\t// Twist individual slices\n\t\t$sl_twist = Array();\n\t\tfor($lr = 0; $lr < 6; $lr++){\n\t\t\t$sl_twist[$lr] = Array();\n\t\t\tfor($sl = 0; $sl < $dim; $sl++){\n\t\t\t\t$sl_twist[$lr][$sl] = Array();\n\t\t\t\tfor($fc = 0; $fc < 6; $fc++){\n\t\t\t\t\tif($sl_so[$lr%3][$fc] == 0) continue;\n\t\t\t\t\tfor($i = 0; $i < $dim; $i++){\n\t\t\t\t\t\t$sl_twist[$lr][$sl][$fc*$nf + fcs_pos($sl, $i, $sl_so[$lr%3][$fc], $dim)]\n\t\t\t\t\t\t\t= $sl_fo[$lr][$fc]*$nf + fcs_pos($sl, $i, $sl_so[$lr%3][$sl_fo[$lr][$fc]], $dim);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t// Initialise move tables\n\t\t$fc_moves = Array();\n\t\tfor($i = 0; $i < $dim * 6; $i++){\n\t\t\tfor($j = 0; $j < $nf * 6; $j++){\n\t\t\t\t$fc_moves[$i][$j] = $j;\n\t\t\t}\n\t\t}\n\t\t\n\t\t// Create move tables composed of simple units\n\t\t\n\t\t// Rotations\n\t\tfor($i = 0; $i < 3; $i++){\n\t\t\t// Add all slice moves for axis\n\t\t\tfor($j = 0; $j < $dim; $j++)\n\t\t\t\tfcs_union($fc_moves[$i], $sl_twist[$i][$j], 0);\n\t\t\t// Add twist moves for each end\n\t\t\tfcs_union($fc_moves[$i], $fc_twist, $i*$dim*$dim);\n\t\t\tfcs_union($fc_moves[$i], $fc_twist2, ($i+3)*$dim*$dim);\n\t\t}\n\t\t// Centre-slice moves\n\t\tif($dim % 2 > 0){\n\t\t\tfcs_union($fc_moves[3], $sl_twist[3][floor($dim/2)], 0); // E\n\t\t\tfcs_union($fc_moves[4], $sl_twist[4][floor($dim/2)], 0); // M\n\t\t\tfcs_union($fc_moves[5], $sl_twist[2][floor($dim/2)], 0); // S\n\t\t}\n\n\t\t// Normal Moves\n\t\tfor($ax = 0; $ax < 3; $ax++){\n\t\t\tfor($dp = 0; $dp < $dim - 1; $dp++){\n\t\t\t\t// Primary axis end\n\t\t\t\t// Add twist move\n\t\t\t\tfcs_union($fc_moves[6 + $ax*($dim-1) + $dp], $fc_twist, $ax*$dim*$dim);\n\t\t\t\tfor($sl = 0; $sl <= $dp; $sl++) // Add slice moves up to depth $dp\n\t\t\t\t\tfcs_union($fc_moves[6 + $ax*($dim-1) + $dp], $sl_twist[$ax][$sl], 0);\n\t\t\t\t// Secondary axis end\n\t\t\t\t// Add twist move\n\t\t\t\tfcs_union($fc_moves[6 + (3+$ax)*($dim-1) + $dp], $fc_twist, (3+$ax)*$dim*$dim);\n\t\t\t\tfor($sl = 0; $sl <= $dp; $sl++) // Add slice moves up to depth $dp\n\t\t\t\t\tfcs_union($fc_moves[6 + (3+$ax)*($dim-1) + $dp], $sl_twist[3+$ax][$dim - $sl - 1], 0);\n\t\t\t}\n\t\t}\n\t\t\n\t\t// Carry out moves\n\t\t$moves = fcs_parse_alg($alg, $dim);\n\t\t\n\t\t$fcs_ = Array();\n\t\tif(!is_array($fcs)) $fcs = str_split($fcs);\n\t\tforeach($moves as $mv){\n\t\t\tfcs_permute($fcs, $fcs_, $fc_moves[$mv]);\n\t\t\t$tmp = $fcs;\n\t\t\t$fcs = $fcs_;\n\t\t\t$fcs_ = $tmp;\n\t\t}\n\t\treturn $fcs;\n\t}\n\tfunction fcs_pos($r, $c, $o, $d){\n\t\t$n = $d*$d;\n\t\tswitch($o){\n\t\t\tcase 1: return $d * $r + $c;\n\t\t\tcase 2: return $d * ($r + 1) - 1 - $c;\n\t\t\tcase 3: return $n - $d * ($r + 1) + $c;\n\t\t\tcase 4: return $n - $d * $r - 1 - $c;\n\t\t\tcase 5: return $d * $c + $r;\n\t\t\tcase 6: return $d * ($c + 1) - 1 - $r;\n\t\t\tcase 7: return $n - $d * ($c + 1) + $r;\n\t\t\tcase 8: return $n - $d * $c - 1 - $r;\n\t\t}\n\t}\n\t// Translations defined in $t2 are applied to $t1\n\tfunction fcs_union(&$t1, &$t2, $offset){\n\t\tforeach($t2 as $k => $v){\n\t\t\t$t1[$k + $offset] = $v + $offset;\n\t\t}\n\t}\n\t// Permutes given array, storing output in second array\n\tfunction fcs_permute(&$in, &$out, $perm){\n\t\t//print_r($perm);\n\t\tfor($i = 0; $i < count($in); $i++)\n\t\t\t$out[$i] = $in[$perm[$i]];\n\t\t//print_r($out);\n\t\treturn $out;\n\t}\n\t// Parses an algorithm string into a move-id sequence\n\tfunction fcs_parse_alg($alg, $dim){\n\t\t$moves = Array();\n\t\t$i = 0;\n\t\t$j = 0;\n\t\t$pre = 0;\n\t\t$len = strlen($alg);\n\t\twhile($i < $len){\n\t\t\t$c = substr($alg, $i, 1);\n\t\t\t$mv = fcs_move_id($c);\n\t\t\tif(is_numeric($c)){\n\t\t\t\t$pre = $c;\n\t\t\t}\n\t\t\telse if($mv >= 0){\n\t\t\t\t$pow = $i < strlen($alg) - 1 ? move_pow(substr($alg, $i+1, 1)) : 1;\n\t\t\t\tif($pow > 1) $i++;\n\t\t\t\t// Add the move\n\t\t\t\t$pre = $pre < 1 ? 1 : ($pre > $dim - 1 ? $dim - 1 : $pre);\n\t\t\t\tif($mv >= 12){ $mv -= 6; $pre = $pre < 2 ? 2 : $pre; }\n\t\t\t\tfor($k = 0; $k < $pow; $k++)\n\t\t\t\t\t$moves[$j++] = $mv < 6 ? $mv : 6 + ($mv - 6) * ($dim - 1) + ($pre - 1); \n\t\t\t\t$pre = 1;\n\t\t\t}\n\t\t\telse $pre = 1;\n\n\t\t\t$i++;\n\t\t}\n//\t\techo \"|\" . $alg . \"|\";\n//\t\tprint_r($moves);\n\t\t\n\t\treturn $moves;\n\t}\n\t// Maps move names to a move id\n\tfunction fcs_move_id($move){\n\t\tswitch($move){\n\t\t\tcase 'y': return 0;\n\t\t\tcase 'x': return 1;\n\t\t\tcase 'z': return 2;\n\t\t\tcase 'E': return 3;\n\t\t\tcase 'M': return 4;\n\t\t\tcase 'S': return 5;\n\t\t\tcase 'U': return 6;\n\t\t\tcase 'R': return 7;\n\t\t\tcase 'F': return 8;\n\t\t\tcase 'D': return 9;\n\t\t\tcase 'L': return 10;\n\t\t\tcase 'B': return 11;\n\t\t\tcase 'u': return 12;\n\t\t\tcase 'r': return 13;\n\t\t\tcase 'f': return 14;\n\t\t\tcase 'd': return 15;\n\t\t\tcase 'l': return 16;\n\t\t\tcase 'b': return 17;\n\t\t}\n\t\treturn -1;\n\t}\n\t// Formats an alg for bigcubes\n\tfunction fcs_format_alg($alg){\n\t\t// Fix URL encoded characters (urldecode doesn't seem to remove them)\n\t\t$r = preg_replace('/%27/', \"'\", $alg);\n\t\t$r = preg_replace('/%20/', \" \", $r);\n\t\t// Remove characters not allowed in an alg\n\t\t$r = preg_replace('/[^UDLRFBudlrfbMESxyzw\\'`234567890 ]/', '', $r);\n\t\t$r = preg_replace('/[`]/', \"'\", $r); // Replace ` with a '\n\t\t$r = preg_replace('/2\\'|\\'2/', \"2\", $r); // Replace 2' or '2 with a 2\n\t\t// Fix wide notation\n\t\tif(preg_match('/[w]/', $r)){\n\t\t\t$r = preg_replace('/Uw/', 'u', $r);\n\t\t\t$r = preg_replace('/Rw/', 'r', $r);\n\t\t\t$r = preg_replace('/Fw/', 'f', $r);\n\t\t\t$r = preg_replace('/Dw/', 'd', $r);\n\t\t\t$r = preg_replace('/Lw/', 'l', $r);\n\t\t\t$r = preg_replace('/Bw/', 'b', $r);\n\t\t\t// now remove any extra w's\n\t\t\t$r = preg_replace('/w/', '', $r);\n\t\t}\n\t\treturn $r;\n\t}\n\t// ------------------[ Basic Functions ]--------------\n\n\t// Copy an array\n\tfunction array_copy($ary){\n\t\tif(!is_array($ary)) return $ary;\n\t\t$out = Array(count($ary));\n\t\tforeach($ary as $k => $v){\n\t\t\t$out[$k] = array_copy($v);\n\t\t}\n\t\treturn $out;\n\t}\n\n?>\n"
  },
  {
    "path": "db_schema.sql",
    "content": "/** Caches generated visual cubes.\nStores the parameters used to generate the image,\nthe latest referrer to request the image,\nand the request frequencey */\nCREATE TABLE vcache(\n\thash CHAR(32) NOT NULL,\n\tfmt CHAR(4) NOT NULL,\n\treq VARCHAR(255) NOT NULL,\n\trfr VARCHAR(255) NOT NULL,\n\trcount INT UNSIGNED NOT NULL,\n\timg MEDIUMBLOB,\n\tPRIMARY KEY (hash));\n\t\n/** View contents with: */\nSELECT hash, fmt, req, rfr, rcount, OCTET_LENGTH(img) FROM vcache;\n\n"
  },
  {
    "path": "screen.css",
    "content": "body{\n\tmargin:0;\n\ttext-align:center;\n\tfont-size:10pt;\n\tfont-family:Verdana, Geneva, Arial, Helvetica, sans-serif;\n}\n\nh1{\n\tcolor:#888;\n\ttext-decoration:none;\n\tfont-family:Arial, Helvetica, sans-serif;\n\tfont-size:1.8em;\n\tfont-weight:bold;\n}\n\nh2{\n\tmargin-top:6em;\n\tfont-size:1.2em;\n\tfont-weight:bold;\n}\nh3{\n\tmargin-top:2em;\n\tfont-size:1.0em;\n\tfont-weight:bold;\n}\nem{\n\tfont-style:normal;\n\tfont-weight:bold;\n}\nimg{\n\tborder:0;\n\tfloat:right;\n}\n.imgarea{\n\ttext-align:center;\n}\n.imgarea img{\n\tfloat:none;\n}\n.imglist img{\n\tfloat:right;\n}\n.biglist li{\n\tmargin-bottom:15px;\n}\n.it{\n\tfont-style:italic;\n}\n.capimg2{\n\tfloat:left;\n\twidth:220px;\n\ttext-align:center;\n\tfont-size:0.8em;\n}\n.capimg2 img{\n\tfloat:none;\n}\n.capimg3{\n\tfloat:left;\n\twidth:150px;\n\ttext-align:center;\n\tfont-size:0.8em;\n}\n.capimg3 img{\n\tfloat:none;\n}\n.imglist li{\n\tclear:both;\n}\n#container{\n\twidth:800px;\n\tmin-height:350px;\n\tmargin:10px auto 0 auto;\n\tbackground-image:url('bg.png');\n\tbackground-repeat:no-repeat;\n}\n#header{\n\twidth:450px;\n\tmargin:0 auto;\n\ttext-align:left;\n}\n\n#header a{\n}\n\n#header img{\n\tfloat:right;\n\tborder:none;\n}\n\n#nav_box{\n\ttext-align:left;\n\tfloat:right;\n\tmargin-right:20px;\n\tmargin-top:20px;\n\tborder:1px solid #888888;\n\tbackground:#888888;\n\tfont-weight:bold;\n\tcolor:#FFFFFF;\n}\n#nav_box ul{\n\tlist-style:none;\n\tpadding:5px;\n\tmargin:0px;\n\tbackground:#F8F8F8;\n}\n#nav_box li{\n\tcolor:#666666;\n\tfont-weight:normal;\n\tmargin:1px 0;\n}\n\n#content{\n\twidth:450px;\n\tmargin:20px auto;\n\ttext-align:left;\n\tfont-size:1em;\n}\n\n#content .backlink{\n\tmargin-top:10px;\n}\n\n#footer{\n\twidth:450px;\n\tmargin:0 auto;\n\tfont-size:0.8em;\n\tcolor:#AAA;\n}\n.spaced_box{\n\tmargin-top:50px;\n}\n.term_list{\n\tlist-style:none;\n\tpadding-left:20px;\n}\n.term_list li{\n\tclear:both;\n\tpadding-top:10px;\n}\n.term_list em{\n\tdisplay:block;\n\tfloat:left;\n}\n.term_list span{\n\tdisplay:block;\n\tfont-weight:normal;\n\twidth:300px;\n\tfloat:right;\n}\n.form{\n\tmargin-top:20px;\n\ttext-align:center;\n}\n.form_content{\n\twidth:350px;\n\tmargin:0 auto;\n\tpadding:5px 10px;\n\tbackground:transparent;\n\tborder:0px solid #CCC;\n}\n.form_record{\n\tclear:both;\n}\n.form_record .label{\n\ttext-align:left;\n\twidth:130px;\n\tfloat:left;\n\tmargin:7px 0;\n}\n.form_record .data{\n\ttext-align:left;\n\tfloat:right;\n\tmargin:7px 0;\n}\n.form_record .data input,select,textarea{\n\twidth:200px;\n}\n.form_record .data textarea{\n\theight:4em;\n}\n.form_textbox{\n\tclear:both;\n}\n.form_textbox textarea{\n\twidth:100%;\n\theight:8em;\n}\n.form_submit{\n\tclear:both;\n\ttext-align:right;\n}\n\n"
  },
  {
    "path": "visualcube.php",
    "content": "<?php\n/*\n\tFile: visualcube.php\n\tDate: 02 Apr 2010\n\tAuthor(s): Conrad Rider (www.crider.co.uk)\n\tContributors: Shotaro Makisumi <smakisumi@gmail.com>, Jaume Casado Ruiz <minterior@gmail.com>\n\tDescription: Main script to generate cube images\n\n\tThis file is part of VisualCube.\n\n\tVisualCube is free software: you can redistribute it and/or modify\n\tit under the terms of the GNU Lesser General Public License as published by\n\tthe Free Software Foundation, either version 3 of the License, or\n\t(at your option) any later version.\n\n\tVisualCube is distributed in the hope that it will be useful,\n\tbut WITHOUT ANY WARRANTY; without even the implied warranty of\n\tMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n\tGNU Lesser General Public License for more details.\n\n\tYou should have received a copy of the GNU Lesser General Public License\n\talong with Foobar.  If not, see <http://www.gnu.org/licenses/>.\n\n\tCopyright (C) 2010 Conrad Rider\n\n\tTODO:\n\t* Automatic Permutation Arrows\n\t* Other puzzles\n\n\tCHANGES:\n\t(Version 0.5.5 to 0.5.6)\n\t* Modified options to attempt to fix rendering issues in ImageMagick v6.9.x\n\t(Version 0.5.4 to 0.5.5)\n\t* Changed to using style attribute to fix transparrancy issues\n\t(Version 0.5.3 to 0.5.4)\n\t* Added configurable DB host\n\t* Added configurable path to ImageMagick convert binary\n\t(Version 0.5.2 to 0.5.3)\n\t* Fixed links on API page\n\t* Changed default format to SVG, avoiding ImageMagick failures\n\t* Addressed PHP warnings and fixed typos in the code\n\t(Version 0.5.1 to 0.5.2)\n\t* Separate configuration file\n\t* Separate API definition file - easier to provide custom API page\n\t(Version 0.3.0 to 0.4.0)\n\t* Algs applicable to NxNxN cubes\n\t* Wider range of stage masks, with the ability to rotate them\n\t* Style variables configurable via cookies\n\n\n*/\n\n\t// Import configuration values\n\trequire 'visualcube_config.php';\n\n\tglobal\n\t\t$DB_HOST,\n\t\t$DB_NAME,\n\t\t$DB_USERNAME,\n\t\t$DB_PASSWORD,\n\t\t$MAX_PZL_DIM,\n\t\t$ENABLE_COOKIES,\n\t\t$ENABLE_CACHE,\n\t\t$CACHE_IMG_SIZE_LIMIT,\n\t\t$DEFAULTS;\n\n\n\t// VisualCube version\n\t$VERSION = \"0.5.5\";\n\n\n\t// Causes cube svg to be outputted as XML for inspection\n\t$DEBUG = false;\n\t// Do not display errors\n//\tif (!$DEBUG) error_reporting(0);\n\n\n\t// ----------------------[ API Page ]-----------------------\n\n\t// If no format specified, display API page\n\tif(!array_key_exists('fmt', $_REQUEST)){\n\n\t\tinclude 'visualcube_api.php';\n\n\t// Otherwise render a cube\n\t}else{\n\t\t// Check cache for image and return if it exists in cache\n\t\tif($ENABLE_CACHE){\n\t\t\t// Connect to db\n\t\t\t$mysql_con = mysqli_connect($DB_HOST, $DB_USERNAME, $DB_PASSWORD, $DB_NAME) or die(\"Connect Error: \" . mysqli_connect_error());\n\n\t\t\t$hash = md5($_SERVER['QUERY_STRING']);\n\t\t\t$imgdata = get_arrays($mysql_con, \"SELECT fmt, req, rcount, img FROM vcache WHERE hash='$hash'\");\n\t\t\t// Verify query strings are equal (deals with unlikely, but possible hash collisions)\n\t\t\tif($imgdata && count($imgdata) > 0 && $imgdata[0]['req'] == $_SERVER['QUERY_STRING']){\n\t\t\t\tdisplay_img($imgdata[0]['img'], $imgdata[0]['fmt']);\n\t\t\t\t// Increment access count\n\t\t\t\tmysqli_query($mysql_con, \"UPDATE vcache SET rcount=rcount+1 WHERE hash='$hash'\");\n\t\t\t\t// Disconnect from db\n\t\t\t\tmysqli_close();\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\n\n\t\t// Otherwise generate image\n\n\n\t\t// -----------------[ Constants ]-----------------\n\n\t\t// Faces\n\t\t$U = 0; $R = 1; $F = 2; $D = 3; $L = 4; $B = 5; $N = 6; $O = 7; $T = 8;\n\n\n\t\t// Colour constants\n\t\t$BLACK  = '000000';\n\t\t$DGREY  = '404040';\n\t\t$GREY   = '808080';\n\t\t$SILVER = 'BFBFBF';\n\t\t$WHITE  = 'FFFFFF';\n\t\t$YELLOW = 'FEFE00';\n\t\t$RED    = 'EE0000';//'FE0000';\n\t\t$ORANGE = 'FFA100';//'FE8600';\n\t\t$BLUE   = '0000F2';\n\t\t$GREEN  = '00D800';//'00F300';\n\t\t$PURPLE = 'A83DD9';\n\t\t$PINK   = 'F33D7B';\n\n\t\t// Other colour schemes\n\t\t// Array('FFFF00', 'FF0000', '0000FF', 'FFFFFF', 'FF7F00', '00FF00'); // Basic\n\t\t// Array('EFEF00', 'C80000', '0000B6', 'F7F7F7', 'FFA100', '00B648'); // Cubestation\n\t\t// Array('EFFF01', 'FF0000', '1600FF', 'FEFFFC', 'FF8000', '047F01'); // cube.rider\n\t\t// Array('FEFE00', 'FE0000', '0000F2', 'FEFEFE', 'FE8600', '00F300'); // alg.garron\n\n\t\t// Name colour mapping\n\t\t$NAME_COL = Array(\n\t\t\t'black'  => $BLACK,\n\t\t\t'dgrey'  => $DGREY,\n\t\t\t'grey'   => $GREY,\n\t\t\t'silver' => $SILVER,\n\t\t\t'white'  => $WHITE,\n\t\t\t'yellow' => $YELLOW,\n\t\t\t'red'    => $RED,\n\t\t\t'orange' => $ORANGE,\n\t\t\t'blue'   => $BLUE,\n\t\t\t'green'  => $GREEN,\n\t\t\t'purple' => $PURPLE,\n\t\t\t'pink'   => $PINK);\n\n\t\t// Abbreviation colour mapping\n\t\t$ABBR_COL = Array(\n\t\t\t'n' => $BLACK,\n\t\t\t'd' => $DGREY,\n\t\t\t'l' => $GREY,\n\t\t\t's' => $SILVER,\n\t\t\t'w' => $WHITE,\n\t\t\t'y' => $YELLOW,\n\t\t\t'r' => $RED,\n\t\t\t'o' => $ORANGE,\n\t\t\t'b' => $BLUE,\n\t\t\t'g' => $GREEN,\n\t\t\t'm' => $PURPLE,\n\t\t\t'p' => $PINK,\n\t\t\t't' => 't'); // Transparent\n\n\t\t// Default colour scheme\n\t\t$DEF_SCHEME = Array ($YELLOW, $RED, $BLUE, $WHITE, $ORANGE, $GREEN, $DGREY, $GREY, 't');\n\t\t// $DEF_SCHEME = Array ($WHITE, $RED, $GREEN, $BLUE, $ORANGE, $YELLOW, $GREY, $SILVER, 't'); // Japanese scheme\n\n\t\t// Corresponding mappings from colour code to face id\n\t\t$DEF_SCHCODE = Array('y', 'r', 'b', 'w', 'o', 'g',);\n\t\t//$DEF_SCHCODE = Array('w', 'r', 'g', 'b', 'o', 'y',); // Japanese scheme\n\n\n\t\t// -----------------------[ User Parameters ]--------------------\n\n\t\t// Retrieve format from user, default to first in list otherwise\n\t\t$LEGAL_FMT = Array ('gif', 'png', 'svg', 'jpg', 'jpe', 'jpeg', 'tiff', 'ico');\n\t\t$fmt = $LEGAL_FMT[0];\n\t\tif(array_key_exists('fmt', $_REQUEST) || array_key_exists('fmt', $DEFAULTS)){\n\t\t\t$fmt = array_key_exists('fmt', $_REQUEST) ? $_REQUEST['fmt'] : $DEFAULTS['fmt'];\n\t\t\tif(!in_array($fmt, $LEGAL_FMT))\n\t\t\t\t$fmt = $LEGAL_FMT[0];\n\t\t\telse{\n\t\t\t\tif($fmt == 'jpeg' || $fmt == 'jpe') $fmt = 'jpg';\n\t\t\t}\n\t\t}\n\n\t\t// Default rotation sequence\n\t\t$rtn = Array(Array(1, 45), Array(0, -34));\n\t\t// Get rotation from request (or cookie)\n\t\tif(array_key_exists('r', $_REQUEST) || array_key_exists('r', $DEFAULTS)\n\t\t|| ($ENABLE_COOKIES && isset($_COOKIE['vc_r']) && $_COOKIE['vc_r'] != '')){\n\t\t\t$_r = array_key_exists('r', $_REQUEST) ? $_REQUEST['r'] :\n\t\t\t($ENABLE_COOKIES && isset($_COOKIE['vc_r']) && $_COOKIE['vc_r'] != '' ?\n\t\t\t\t$_COOKIE['vc_r'] : $DEFAULTS['r']);\n\t\t\tpreg_match_all('/([xyz])(\\-?[0-9][0-9]?[0-9]?)/', $_r, $matches);\n\t\t\tfor($i = 0; $i < count($matches[0]); $i++){\n\t\t\t\tswitch($matches[1][$i]){\n\t\t\t\t\tcase 'x' : $rtn_[$i][0] = 0; break;\n\t\t\t\t\tcase 'y' : $rtn_[$i][0] = 1; break;\n\t\t\t\t\tcase 'z' : $rtn_[$i][0] = 2; break;\n\t\t\t\t\tdefault : break;\n\t\t\t\t}\n\t\t\t\t$rtn_[$i][1] = $matches[2][$i];\n\t\t\t}\n\t\t\tif($rtn_) $rtn = $rtn_;\n\t\t}\n\n\t\t// Retrieve cube Dimension\n\t\t$dim = $DEFAULTS['pzl'];\n\t\tif(array_key_exists('pzl', $_REQUEST) && is_numeric($_REQUEST['pzl'])\n\t\t&& $_REQUEST['pzl'] > 0 && $_REQUEST['pzl'] <= $MAX_PZL_DIM)\n\t\t\t$dim = $_REQUEST['pzl'];\n\n\t\t// Default scheme\n\t\t$scheme = $DEF_SCHEME;\n\t\t// Default mapping from colour code to face id\n\t\t$schcode = $DEF_SCHCODE;\n\t\t// Retrieve colour scheme from request (or cookie)\n\t\tif(array_key_exists('sch', $_REQUEST) || array_key_exists('sch', $DEFAULTS)\n\t\t|| ($ENABLE_COOKIES && isset($_COOKIE['vc_sch']) && $_COOKIE['vc_sch'] != '')){\n\t\t\t// Retrieve from cookie or 'sch' variable\n\t\t\t$sd = array_key_exists('sch', $_REQUEST) ? $_REQUEST['sch'] :\n\t\t\t($ENABLE_COOKIES && isset($_COOKIE['vc_sch']) && $_COOKIE['vc_sch'] != '' ?\n\t\t\t\t$_COOKIE['vc_sch'] : $DEFAULTS['sch']);\n\t\t\tif(preg_match('/^[ndlswyrogbpmt]{6}$/', $sd)){\n\t\t\t\tfor($i = 0; $i < 6; $i++){\n\t\t\t\t\t$scheme[$i] = $ABBR_COL[$sd[$i]];\n\t\t\t\t\t$schcode[$i] = $sd[$i];\n\t\t\t\t}\n\t\t\t}\n\t\t\telse{\n\t\t\t\t$cols = preg_split('/,/', $sd);\n\t\t\t\tif(count($cols) == 6){\n\t\t\t\t\t$cok = true;\n\t\t\t\t\tfor($i = 0; $i < 6; $i++){\n\t\t\t\t\t\t$scheme[$i] = parse_col($cols[$i]);\n\t\t\t\t\t\tif(!$cols[$i]) $cok = false;\n\t\t\t\t\t}\n\t\t\t\t\tif(!$cok) $scheme = $DEF_SCHEME;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Retrieve size from user\n\t\t$size = $DEFAULTS['size']; // default\n\t\tif(array_key_exists('size', $_REQUEST) && is_numeric($_REQUEST['size'])){\n\t\t\t$size = $_REQUEST['size'];\n\t\t\tif($size < 0) $size = 0;\n\t\t\tif($size > 1024) $size = 1024;\n\t\t}\n\n\t\t// Retrieve dist variable - projection distance (how close the eye is to the cube)\n\t\t$dist = $DEFAULTS['dist']; // default dist parameter\n\t\tif(array_key_exists('dist', $_REQUEST) || ($ENABLE_COOKIES && isset($_COOKIE['vc_dist']))){\n\t\t\t$dist_ = array_key_exists('dist', $_REQUEST) ? $_REQUEST['dist'] : $_COOKIE['vc_dist'];\n\t\t\tif(is_numeric($dist_)) $dist = $dist_ < 1 ? 1 : ($dist_ > 100 ? 100 : $dist_);\n\t\t}\n\n\t\t// Retrieve view variable\n\t\t$view = $DEFAULTS['view'];\n\t\tif(array_key_exists('view', $_REQUEST)){\n\t\t\t$view = $_REQUEST['view'];\n\t\t}\n\n\t\t// Retrieve background colour from request (or cookies)\n\t\t$bg = parse_col($DEFAULTS['bg']);\n\t\tif(!$bg) $bg = 'FFFFFF';\n\t\tif(array_key_exists('bg', $_REQUEST) || ($ENABLE_COOKIES && isset($_COOKIE['vc_bg']))){\n\t\t\t$bg_ = array_key_exists('bg', $_REQUEST) ? $_REQUEST['bg'] : $_COOKIE['vc_bg'];\n\t\t\tif($bg_ == \"t\") $bg = null;\n\t\t\telse{\n\t\t\t\t$bg_ = parse_col($bg_);\n\t\t\t\tif($bg_) $bg = $bg_;\n\t\t\t}\n\t\t}\n\n\t\t// Retrieve cube colour from request (or cookies)\n\t\t$cc = $view == 'trans' ? $SILVER : parse_col($DEFAULTS['cc']);\n\t\tif(array_key_exists('cc', $_REQUEST) || ($ENABLE_COOKIES && isset($_COOKIE['vc_cc']))){\n\t\t\t$cc_ = array_key_exists('cc', $_REQUEST) ? $_REQUEST['cc'] : $_COOKIE['vc_cc'];\n\t\t\t$cc_ = parse_col($cc_);\n\t\t\tif($cc_) $cc = $cc_;\n\t\t}\n\t\t$cc = !$cc ? $BLACK : $cc;\n\n\t\t// Retrieve cube opacity from request (or cookies)\n\t\t$co = $view == 'trans' ? 50 : $DEFAULTS['co'];\n\t\tif(array_key_exists('co', $_REQUEST) || ($ENABLE_COOKIES && isset($_COOKIE['vc_co']))){\n\t\t\t$co_ = array_key_exists('co', $_REQUEST) ? $_REQUEST['co'] : $_COOKIE['vc_co'];\n\t\t\tif(preg_match('/^[0-9][0-9]?$/', $co_)) $co = $co_;\n\t\t}\n\t\t$co = !is_numeric($co) || $co < 0 || $co > 100 ? 100 : $co;\n\n\t\t// Retrieve face opacity from request (or cookies)\n\t\t$fo = $DEFAULTS['fo'];\n\t\tif(!is_numeric($fo) || $fo < 0 || $fo > 100)\n\t\t\t$fo = 100;\n\t\tif(array_key_exists('fo', $_REQUEST) || ($ENABLE_COOKIES && isset($_COOKIE['vc_fo']))){\n\t\t\t$fo_ = array_key_exists('fo', $_REQUEST) ? $_REQUEST['fo'] : $_COOKIE['vc_fo'];\n\t\t\tif(preg_match('/^[0-9][0-9]?$/', $fo_)) $fo = $fo_;\n\t\t}\n\n\n\t\t// Create default face defs\n\t\tfor($fc = 0; $fc < 6; $fc++){ for($i = 0; $i < $dim * $dim; $i++)\n\t\t\t$facelets[$fc * $dim * $dim + $i] = $fc;\n\t\t}\n\t\t// Retrieve colour def\n\t\t// This overrides face def and makes the $scheme variable redundant (ie, gets reset to default)\n\t\t$using_cols = false;\n\t\t$uf = array_key_exists('fc', $_REQUEST) ? $_REQUEST['fc'] : (!array_key_exists('fd', $_REQUEST) ? $DEFAULTS['fc'] : '');\n\t\tif(preg_match('/^[ndlswyrobgmpt]+$/', $uf)){\n\t\t\t$using_cols = true;\n\t\t\t$scheme = $DEF_SCHEME;\n\t\t\t$nf = strlen($uf);\n\t\t\tfor($fc = 0; $fc < 6; $fc++){ for($i = 0; $i < $dim * $dim; $i++){\n\t\t\t\t// Add user defined face\n\t\t\t\tif($fc * $dim *$dim + $i < $nf)\n\t\t\t\t\t$facelets[$fc * $dim * $dim + $i] = $uf[$fc * $dim *$dim + $i];\n\t\t\t\t// Otherwise use scheme code\n\t\t\t\telse\n\t\t\t\t\t$facelets[$fc * $dim * $dim + $i] = $schcode[$fc];\n\t\t\t}}\n\t\t}\n\t\t// Retrieve facelet def\n\t\tif(!$uf){ $uf = array_key_exists('fd', $_REQUEST) ? $_REQUEST['fd'] : $DEFAULTS['fd'];\n\t\tif(preg_match('/^[udlrfbnot]+$/', $uf)){\n\t\t\t// Map from face names to numeric face ID\n\t\t\t$fd_map = Array('u' => $U, 'r' => $R, 'f' => $F, 'd' => $D, 'l' => $L, 'b' => $B, 'n' => $N, 'o' => $O, 't' => $T);\n\t\t\t$nf = strlen($uf);\n\t\t\tfor($fc = 0; $fc < 6; $fc++){ for($i = 0; $i < $dim * $dim; $i++){\n\t\t\t\t// Add user defined face\n\t\t\t\tif($fc * $dim *$dim + $i < $nf)\n\t\t\t\t\t$facelets[$fc * $dim * $dim + $i] = $fd_map[$uf[$fc * $dim *$dim + $i]];\n\t\t\t\t// Otherwise default to a blank/transparent face\n\t\t\t\telse $facelets[$fc * $dim *$dim + $i] = $view == 'trans' ? $T : $N;\n\t\t\t}}\n\t\t}}\n\t\t// Retrieve stage variable\n\t\tif(array_key_exists('stage', $_REQUEST) || array_key_exists('stage', $DEFAULTS)){\n\t\t\t$stage = array_key_exists('stage', $_REQUEST) ? $_REQUEST['stage'] : $DEFAULTS['stage'];\n\t\t\t// Extract rotation sequence if present\n\t\t\t$p = strrpos($stage, '-');\n\t\t\t$st_rtn = '';\n\t\t\tif($p > 0){\n\t\t\t\t$st_rtn = urldecode(substr($stage, $p+1));\n\t\t\t\t$stage = substr($stage, 0, $p);\n\t\t\t}\n\t\t\t// Stage Definitions\n\t\t\t$mask = '';\n\t\t\tif($dim == 3){\n\t\t\t\tswitch($stage){\n\t\t\t\t\tcase 'fl' :\n\t\t\t\t$mask = \"000000000000000111000000111111111111000000111000000111\";\n\t\t\t\tbreak;\n\t\t\t\t\tcase 'f2l' :\n\t\t\t\t$mask = \"000000000000111111000111111111111111000111111000111111\";\n\t\t\t\tbreak;\n\t\t\t\t\tcase 'll' :\n\t\t\t\t$mask = \"111111111111000000111000000000000000111000000111000000\";\n\t\t\t\tbreak;\n\t\t\t\t\tcase 'cll' :\n\t\t\t\t$mask = \"101010101101000000101000000000000000101000000101000000\";\n\t\t\t\tbreak;\n\t\t\t\t\tcase 'ell' :\n\t\t\t\t$mask = \"010111010010000000010000000000000000010000000010000000\";\n\t\t\t\tbreak;\n\t\t\t\t\tcase 'oll' :\n\t\t\t\t$mask = \"111111111000000000000000000000000000000000000000000000\";\n\t\t\t\tbreak;\n\t\t\t\t\tcase 'ocll' :\n\t\t\t\t$mask = \"101010101000000000000000000000000000000000000000000000\";\n\t\t\t\tbreak;\n\t\t\t\t\tcase 'oell' :\n\t\t\t\t$mask = \"010111010000000000000000000000000000000000000000000000\";\n\t\t\t\tbreak;\n\t\t\t\t\tcase 'coll' :\n\t\t\t\t$mask = \"111111111101000000101000000000000000101000000101000000\";\n\t\t\t\tbreak;\n\t\t\t\t\tcase 'ocell' :\n\t\t\t\t$mask = \"111111111010000000010000000000000000010000000010000000\";\n\t\t\t\tbreak;\n\t\t\t\t\tcase 'wv' :\n\t\t\t\t$mask = \"111111111000111111000111111111111111000111111000111111\";\n\t\t\t\tbreak;\n\t\t\t\t\tcase 'vh' :\n\t\t\t\t$mask = \"010111010000111111000111111111111111000111111000111111\";\n\t\t\t\tbreak;\n\t\t\t\t\tcase 'els' :\n\t\t\t\t$mask = \"010111010000111011000111110110111111000111111000111111\";\n\t\t\t\tbreak;\n\t\t\t\t\tcase 'cls' :\n\t\t\t\t$mask = \"111111111000111111000111111111111111000111111000111111\";\n\t\t\t\tbreak;\n\t\t\t\t\tcase 'cmll' :\n\t\t\t\t$mask = \"101000101101111111101101101101101101101111111101101101\";\n\t\t\t\tbreak;\n\t\t\t\t\tcase 'cross' :\n\t\t\t\t$mask = \"000000000000010010000010010010111010000010010000010010\";\n\t\t\t\tbreak;\n\t\t\t\t\tcase 'f2l_3' :\n\t\t\t\t$mask = \"000000000000110110000011011011111010000010010000010010\";\n\t\t\t\tbreak;\n\t\t\t\t\tcase 'f2l_2' :\n\t\t\t\t$mask = \"000000000000011011000010010010111111000110110000111111\";\n\t\t\t\tbreak;\n\t\t\t\t\tcase 'f2l_sm' :\n\t\t\t\t$mask = \"000000000000110110000011011011111110000110110000011011\";\n\t\t\t\tbreak;\n\t\t\t\t\tcase 'f2l_1' :\n\t\t\t\t$mask = \"000000000000011011000110110110111111000111111000111111\";\n\t\t\t\tbreak;\n\t\t\t\t\tcase 'f2b' :\n\t\t\t\t$mask = \"000000000000111111000101101101101101000111111000101101\";\n\t\t\t\tbreak;\n\t\t\t\t\tcase 'line' :\n\t\t\t\t$mask = \"000000000000000000000010010010010010000000000000010010\";\n\t\t\t\tbreak;\n\t\t\t\t\tcase '2x2x2' :\n\t\t\t\t$mask = \"000000000000110110000011011011011000000000000000000000\";\n\t\t\t\tbreak;\n\t\t\t\t\tcase '2x2x3' :\n\t\t\t\t$mask = \"000000000000110110000111111111111000000011011000000000\";\n\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}else if($dim == 2){\n\t\t\t\tswitch($stage){\n\t\t\t\t\tcase 'fl' :\n\t\t\t\t$mask = \"000000110011111100110011\";\n\t\t\t\tbreak;\n\t\t\t\t\tcase 'll' :\n\t\t\t\t$mask = \"111111001100000011001100\";\n\t\t\t\tbreak;\n\t\t\t\t\tcase 'oll' :\n\t\t\t\t$mask = \"111100000000111100000000\";\n\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Apply alg to mask if defined\n\t\t\tif($mask && $st_rtn != ''){\n\t\t\t\trequire_once \"cube_lib.php\";\n\t\t\t\t$mask = fcs_doperm($mask, fcs_format_alg($st_rtn), $dim);\n\t\t\t}\n\n\t\t\t// Apply mask to face def\n\t\t\tif($mask){\n\t\t\t\tfor($i = 0; $i < $dim * $dim * 6; $i++){\n\t\t\t\t\t$facelets[$i] = $mask[$i] == 0 ?\n\t\t\t\t\t\t($view == 'trans' ? ($using_cols ? 't' : $T) :\n\t\t\t\t\t\t($using_cols ? 'l' : $N)) : $facelets[$i];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Retrieve alg def\n\t\tif(array_key_exists('alg', $_REQUEST) || array_key_exists('case', $_REQUEST)\n\t\t|| array_key_exists('alg', $DEFAULTS) || array_key_exists('case', $DEFAULTS)){\n\t\t\trequire_once \"cube_lib.php\";\n\t\t\tif(array_key_exists('alg', $_REQUEST)) $alg = $_REQUEST['alg']; else $alg = $DEFAULTS['alg'];\n\t\t\tif(array_key_exists('case', $_REQUEST)) $case = $_REQUEST['case']; else $case = $DEFAULTS['case'];\n\t\t\t$alg = fcs_format_alg(urldecode($alg));\n\t\t\t$case = invert_alg(fcs_format_alg(urldecode($case)));\n//\t\t\t$facelets = facelet_cube(case_cube($alg), $dim, $facelets); // old 3x3 alg system\n\t\t\t$facelets = fcs_doperm($facelets, $case . ' ' . $alg, $dim); // new NxN facelet permute\n\t\t}\n\n\t\t// Retrieve arrow defn's\n\t\tif(array_key_exists('arw', $_REQUEST)){\n\t\t\t$astr = preg_split('/,/', $_REQUEST['arw']);\n\t\t\t$i = 0;\n\t\t\tforeach($astr as $a){\n\t\t\t\t$a_ = parse_arrow($a, $dim);\n\t\t\t\tif($a_) $arrows[$i++] = $a_;\n\t\t\t}\n\t\t}\n\n\t\t// Retrieve default arrow colour\n\t\t$ac = $GREY;\n\t\tif(array_key_exists('ac', $_REQUEST)){\n\t\t\t$ac_ = parse_col($_REQUEST['ac']);\n\t\t\tif($ac_ && $ac_ != 't') $ac = $ac_;\n\t\t}\n\n\t\t// ---------------[ 3D Cube Generator properties ]---------------\n\n\t\t// Outline width\n\t\t$OUTLINE_WIDTH = 0.94;\n\n\t\t// Stroke width\n\t\t$sw = 0;\n\n\t\t// Viewport\n\t\t$ox = -0.9;\n\t\t$oy = -0.9;\n\t\t$vw = 1.8;\n\t\t$vh = 1.8;\n\n\t\t// ------------------[ 3D Cube Generator ]-----------------------\n\n\t\t// Set up cube for OLL view if specified\n\t\tif($view == 'plan'){\n\t\t\t$rtn = Array(Array(0, -90));\n\t\t}\n\n\t\t// All cube face points\n\t\t$p = Array();\n\t\t// Translation vector to centre the cube\n\t\t$t = Array(-$dim/2, -$dim/2, -$dim/2);\n\t\t// Translation vector to move the cube away from viewer\n\t\t$zpos = Array(0, 0, $dist);\n\t\t// Rotation vectors to track visibility of each face\n\t\t$rv = Array(Array(0, -1, 0), Array(1, 0, 0), Array(0, 0, -1), Array(0, 1, 0), Array(-1, 0, 0), Array(0, 0, 1));\n\t\tfor($fc = 0; $fc < 6; $fc++){\n\t\t\tfor($i = 0; $i <= $dim; $i++){\n\t\t\t\tfor($j = 0; $j <= $dim; $j++){\n\t\t\t\t\tswitch($fc){\n\t\t\t\t\t\tcase $U : $p[$fc][$i][$j] = Array(       $i,    0, $dim - $j); break;\n\t\t\t\t\t\tcase $R : $p[$fc][$i][$j] = Array(     $dim,   $j,        $i); break;\n\t\t\t\t\t\tcase $F : $p[$fc][$i][$j] = Array(       $i,   $j,         0); break;\n\t\t\t\t\t\tcase $D : $p[$fc][$i][$j] = Array(       $i, $dim,        $j); break;\n\t\t\t\t\t\tcase $L : $p[$fc][$i][$j] = Array(        0,   $j, $dim - $i); break;\n\t\t\t\t\t\tcase $B : $p[$fc][$i][$j] = Array($dim - $i,   $j,      $dim); break;\n\t\t\t\t\t}\n\t\t\t\t\t// Now scale and tranform point to ensure size/pos independent of dim\n\t\t\t\t\t$p[$fc][$i][$j] = translate($p[$fc][$i][$j], $t);\n\t\t\t\t\t$p[$fc][$i][$j] = scale($p[$fc][$i][$j], 1 / $dim);\n\t\t\t\t\t// Rotate cube as per perameter settings\n\t\t\t\t\tforeach($rtn as $rn){\n\t\t\t\t\t\t$p[$fc][$i][$j] = rotate($p[$fc][$i][$j], $rn[0], M_PI * $rn[1]/180);\n\t\t\t\t\t}\n\t\t\t\t\t// Move cube away from viewer\n\t\t\t\t\t$p[$fc][$i][$j] = translate($p[$fc][$i][$j], $zpos);\n\t\t\t\t\t// Finally project the 3D points onto 2D\n\t\t\t\t\t$p[$fc][$i][$j] = project($p[$fc][$i][$j], $zpos[2]);\n\t\t\t\t}\n\t\t\t}\n\t\t\t// Rotate rotation vectors\n\t\t\tforeach($rtn as $rn){\n\t\t\t\t$rv[$fc] = rotate($rv[$fc], $rn[0], M_PI * $rn[1]/180);\n\t\t\t}\n\t\t}\n\n\t\t// Sort render order (crappy bubble sort)\n\t\t$ro = Array(0, 1, 2, 3, 4, 5);\n\t\tfor($i = 0; $i < 5; $i++){ for($j = 0; $j < 5; $j++){\n\t\t\tif($rv[$ro[$j]][2] < $rv[$ro[$j+1]][2]){\n\t\t\t\t$t = $ro[$j]; $ro[$j] = $ro[$j+1]; $ro[$j+1] = $t; }\n\t\t}}\n\n\t\t// Cube diagram SVG XML\n\t\t$cube = \"<?xml version='1.0' standalone='no'?>\n<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN'\n'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'>\n\n<svg version='1.1' xmlns='http://www.w3.org/2000/svg'\n\twidth='$size' height='$size'\n\tviewBox='$ox $oy $vw $vh'>\\n\";\n\n\t// Draw background\n\tif($bg) $cube .= \"\\t<rect fill='#$bg' x='$ox' y='$oy' width='$vw' height='$vh'/>\\n\";\n\n\t\t// Transparancy background rendering\n\t\tif($co < 100){\n\t\t\t// Create polygon for each background facelet (transparency only)\n\t\t\t$cube .= \"\\t<g style='opacity:\".($fo/100).\";stroke-opacity:0.5;stroke-width:$sw;stroke-linejoin:round'>\\n\";\n\t\t\tfor($ri = 0; $ri < 3; $ri++){\n\t\t\t\t$cube .= facelet_svg($ro[$ri]);\n\t\t\t}\n\t\t\t$cube .= \"\\t</g>\\n\";\n\n\t\t\t// Create outline for each background face (transparency only)\n\t\t\t$cube .= \"\\t<g style='stroke-width:0.1;stroke-linejoin:round;opacity:\".($co/100).\"'>\\n\";\n\t\t\tfor($ri = 0; $ri < 3; $ri++)\n\t\t\t\t$cube .= outline_svg($ro[$ri]);\n\t\t\t$cube .= \"\\t</g>\\n\";\n\t\t}\n\n\t\t// Create outline for each visible face\n\t\t$cube .= \"\\t<g style='stroke-width:0.1;stroke-linejoin:round;opacity:\".($co/100).\"'>\\n\";\n\t\tfor($ri = 3; $ri < 6; $ri++){\n\t\t\tif(face_visible($ro[$ri], $rv) || $co < 100)\n\t\t\t\t$cube .= outline_svg($ro[$ri]);\n\t\t}\n\t\t$cube .= \"\\t</g>\\n\";\n\n\t\t// Create polygon for each visible facelet\n\t\t$cube .= \"\\t<g style='opacity:\".($fo/100).\";stroke-opacity:0.5;stroke-width:$sw;stroke-linejoin:round'>\\n\";\n\t\tfor($ri = 3; $ri < 6; $ri++){\n\t\t\tif(face_visible($ro[$ri], $rv) || $co < 100)\n\t\t\t\t$cube .= facelet_svg($ro[$ri]);\n\t\t}\n\t\t$cube .= \"\\t</g>\\n\";\n\n\t\t// Create OLL view guides\n\t\tif($view == 'plan'){\n\t\t\t$cube .= \"\\t<g style='opacity:\".($fo/100).\";stroke-opacity:1;stroke-width:0.02;stroke-linejoin:round'>\\n\";\n\t\t\t$toRender = Array($F, $L, $B, $R);\n\t\t\tforeach($toRender as $fc)\n\t\t\t\t$cube .= oll_svg($fc);\n\t\t\t$cube .= \"\\t</g>\\n\";\n\t\t}\n\n\t\t// Draw Arrows\n\t\tif(isset($arrows)){\n\t\t\t$awidth = 0.12 / $dim;\n\t\t\t$cube .= \"\\t<g style='opacity:1;stroke-opacity:1;stroke-width:$awidth;stroke-linecap:round'>\\n\";\n\t\t\tforeach($arrows as $i => $a){\n\t\t\t\t$cube .= gen_arrow($i, $a[0], $a[1], $a[2], $a[4], array_key_exists(3, $a)?$a[3]:$ac);\n\t\t\t}\n\t\t\t$cube .= \"\\t</g>\\n\";\n\t\t}\n\n\t\t$cube .= \"</svg>\\n\";\n\n\n\n\n\n\t\t// Display cube\n\t\tif($DEBUG) echo $cube;\n\t\telse{\n\t\t\t$img = $fmt != 'svg' ? convert($cube, $fmt, $size) : $cube;\n\t\t\tdisplay_img($img, $fmt);\n\n\t\t\t// Cache image if enabled\n\t\t\tif($ENABLE_CACHE && !array_key_exists(\"nocache\", $_REQUEST) && strlen($img) < $CACHE_IMG_SIZE_LIMIT){\n\t\t\t\t$req = mysqli_real_escape_string($mysql_con, $_SERVER['QUERY_STRING']);\n\t\t\t\t$rfr = mysqli_real_escape_string($mysql_con, $_SERVER['HTTP_REFERER']);\n\t\t\t\t$hash = md5($req);\n\t\t\t\t$img = mysqli_real_escape_string($mysql_con, $img);\n\t\t\t\tmysqli_query($mysql_con, \"INSERT INTO vcache(hash, fmt, req, rfr, rcount, img) \".\n\t\t\t\t\t\t\"VALUES ('$hash', '$fmt', '$req', '$rfr', 1, '$img')\");\n\t\t\t\t// Disconnect from db\n\t\t\t\tmysqli_close();\n\t\t\t}\n\t\t}\n\t}\n\n\n\t// -----------------[ User input functions ]----------------------\n\n\t// Parse colour value\n\tfunction parse_col($col){\n\t\tglobal $NAME_COL, $ABBR_COL;\n\t\t// As an abbriviation\n\t\tif(preg_match('/^[ndlswyrogbpmt]$/', $col))\n\t\t\treturn $ABBR_COL[$col];\n\t\t// As a name\n\t\tif(array_key_exists($col, $NAME_COL))\n\t\t\treturn $NAME_COL[$col];\n\t\t// As 12-bit colour\n\t\tif(preg_match('/^[0-9a-fA-F]{3}$/', $col))\n\t\t\treturn $col[0].$col[0].$col[1].$col[1].$col[2].$col[2];\n\t\t// As 24-bit colour\n\t\tif(preg_match('/^[0-9a-fA-F]{6}$/', $col))\n\t\t\treturn $col;\n\t\t// Otherwise fail\n\t\treturn null;\n\t}\n\n\t// Parse arrow definition\n\tfunction parse_arrow($str, $dim){\n\t\t$parts = preg_split('/-/', $str);\n\t\t$fcodes = array('U' => 0, 'R' => 1, 'F' => 2, 'D' => 3, 'L' => 4, 'B' => 5);\n\t\tif(count($parts) == 0) return null;\n\t\tif(!preg_match_all('/([URFDLB])([0-9]+)/', $parts[0], $split) || count($split) < 2) return null;\n\t\t$arrow = array();\n\t\t$arrow[4] = 1;\n\t\tfor($i = 0; $i < 3; $i++){\n\t\t\tif($i == 2 && count($split[1]) < 3){\n\t\t\t\t$arrow[2] = null;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\telse\t$arrow[2][3] = 2;\n\t\t\t$arrow[$i][0] = $fcodes[$split[1][$i]];\n\t\t\t$fn = $split[2][$i]; $fn = $fn >= $dim * $dim ? $dim * $dim - 1 : $fn;\n\t\t\t$arrow[$i][1] = $fn % $dim;\n\t\t\t$arrow[$i][2] = floor($fn / $dim);\n\t\t}\n\t\t// Parse remainder\n\t\tfor($i = 1; $i < count($parts); $i++){\n\t\t\tif(preg_match('/^i[0-9]+$/', $parts[$i]) && $arrow[2]){\n\t\t\t\t$arrow[2][3] = substr($parts[$i],1) / 5;\n\t\t\t\t$arrow[2][3] = $arrow[2][3] > 10 ? 10 : $arrow[2][3]; // Var range = 0 to 50, default 10\n\t\t\t}\n\t\t\telse if(preg_match('/^s[0-9]+$/', $parts[$i])){\n\t\t\t\t$arrow[4] = substr($parts[$i],1) / 10;\n\t\t\t\t$arrow[4] = $arrow[4] > 2 ? 2 : $arrow[4]; // Var range = 0 to 20, default 10\n\t\t\t}\n\t\t\telse{\n\t\t\t\t$ac_ = parse_col($parts[$i]);\n\t\t\t\tif($ac_) $arrow[3] = $ac_;\n\t\t\t}\n\t\t}\n\t\treturn $arrow;\n\t}\n\n\t// Insert space in default fd/fc variables\n\tfunction insert_space($in, $dim){\n\t\t$out = '';\n\t\t$dim *= $dim;\n\t\tfor($i = 0; $i < 6; $i++){\n\t\t\t$out .= substr($in, $dim * $i, $dim) . ' ';\n\t\t}\n\t\treturn $out;\n\t}\n\n\n\t// -------------------[ 3D Geometry Functions ]--------------------\n\n\t// Move point by translation vector\n\tfunction translate($p, $t){\n\t\t$p[0] += $t[0];\n\t\t$p[1] += $t[1];\n\t\t$p[2] += $t[2];\n\t\treturn $p;\n\t}\n\n\tfunction scale($p, $f){\n\t\t$p[0] *= $f;\n\t\t$p[1] *= $f;\n\t\t$p[2] *= $f;\n\t\treturn $p;\n\t}\n\n\t// Scale point relative to position vector\n\tfunction trans_scale($p, $v, $f){\n\t\t// Translate each facelet to cf\n\t\t$iv = Array(-$v[0], -$v[1], -$v[2]);\n\t\treturn translate(scale(translate($p, $iv), $f), $v);\n\t}\n\n\tfunction rotate($p, $ax, $an){\n\t\t$np = Array($p[0], $p[1], $p[2]);\n\t\tswitch($ax){\n\t\t\tcase 0 :\n\t\t\t\t$np[2] = $p[2] * cos($an) - $p[1] * sin($an);\n\t\t\t\t$np[1] = $p[2] * sin($an) + $p[1] * cos($an);\n\t\t\t\tbreak;\n\t\t\tcase 1 :\n\t\t\t\t$np[0] =   $p[0] * cos($an) + $p[2] * sin($an);\n\t\t\t\t$np[2] = - $p[0] * sin($an) + $p[2] * cos($an);\n\t\t\t\tbreak;\n\t\t\tcase 2 :\n\t\t\t\t$np[0] = $p[0] * cos($an) - $p[1] * sin($an);\n\t\t\t\t$np[1] = $p[0] * sin($an) + $p[1] * cos($an);\n\t\t\t\tbreak;\n\t\t}\n\t\treturn $np;\n\t}\n\n\t// Project 3D points onto a 2D plane\n\tfunction project($p, $d){\n\t\treturn Array(\n\t\t\t$p[0] * $d / $p[2],\n\t\t\t$p[1] * $d / $p[2],\n\t\t\t$p[2] // Maintain z coordinate to allow use of rendering tricks\n\t\t);\n\t}\n\n\t// Returns whether a face is visible\n\tfunction face_visible($face, $rv){\n\t\treturn $rv[$face][2] < -0.105;\n\t}\n\n\n\n\n\t// ---------------------------[ Rendering Functions ]----------------------------\n\n\t// Returns svg for a cube outline\n\tfunction outline_svg($fc){\n\t\tglobal $p, $dim, $cc, $OUTLINE_WIDTH;\n\t\treturn \"\\t\\t<polygon fill='#$cc' stroke='#$cc' points='\".\n\t\t\t$p[$fc][   0][   0][0]*$OUTLINE_WIDTH.','.$p[$fc][   0][   0][1]*$OUTLINE_WIDTH.' '.\n\t\t\t$p[$fc][$dim][   0][0]*$OUTLINE_WIDTH.','.$p[$fc][$dim][   0][1]*$OUTLINE_WIDTH.' '.\n\t\t\t$p[$fc][$dim][$dim][0]*$OUTLINE_WIDTH.','.$p[$fc][$dim][$dim][1]*$OUTLINE_WIDTH.' '.\n\t\t\t$p[$fc][   0][$dim][0]*$OUTLINE_WIDTH.','.$p[$fc][   0][$dim][1]*$OUTLINE_WIDTH.\"'/>\\n\";\n\t}\n\n\t// Returns svg for a faces facelets\n\tfunction facelet_svg($fc){\n\t\tglobal $p, $dim;\n\t\t$svg = '';\n\t\tfor($i = 0; $i < $dim; $i++){\n\t\t\tfor($j = 0; $j < $dim; $j++){\n\t\t\t\t// Find centre point of facelet\n\t\t\t\t$cf = Array(($p[$fc][$j  ][$i  ][0] + $p[$fc][$j+1][$i+1][0])/2,\n\t\t\t\t\t($p[$fc][$j  ][$i  ][1] + $p[$fc][$j+1][$i+1][1])/2, 0);\n\t\t\t\t// Scale points in towards centre\n\t\t\t\t$p1 = trans_scale($p[$fc][$j  ][$i  ], $cf, 0.85);\n\t\t\t\t$p2 = trans_scale($p[$fc][$j+1][$i  ], $cf, 0.85);\n\t\t\t\t$p3 = trans_scale($p[$fc][$j+1][$i+1], $cf, 0.85);\n\t\t\t\t$p4 = trans_scale($p[$fc][$j  ][$i+1], $cf, 0.85);\n\t\t\t\t// Generate facelet polygon\n\t\t\t\t$svg .= gen_facelet($p1, $p2, $p3, $p4, $fc * $dim * $dim + $i * $dim + $j);\n\t\t\t}\n\t\t}\n\t\treturn $svg;\n\t}\n\n\t// Renders the top rim of the R U L and B faces out from side of cube\n\tfunction oll_svg($fc){\n\t\tglobal $p, $dim, $rv;\n\t\t$svg = '';\n\t\t// Translation vector, to move faces out\n\t\t$tv1 = scale($rv[$fc], 0.00);\n\t\t$tv2 = scale($rv[$fc], 0.20);\n\t\t$i = 0;\n\t\tfor($j = 0; $j < $dim; $j++){\n\t\t\t\t// Find centre point of facelet\n\t\t\t\t$cf = Array(($p[$fc][$j  ][$i  ][0] + $p[$fc][$j+1][$i+1][0])/2,\n\t\t\t\t\t($p[$fc][$j  ][$i  ][1] + $p[$fc][$j+1][$i+1][1])/2, 0);\n\t\t\t\t// Scale points in towards centre and skew\n\t\t\t\t$p1 = translate(trans_scale($p[$fc][$j  ][$i  ], $cf, 0.94), $tv1);\n\t\t\t\t$p2 = translate(trans_scale($p[$fc][$j+1][$i  ], $cf, 0.94), $tv1);\n\t\t\t\t$p3 = translate(trans_scale($p[$fc][$j+1][$i+1], $cf, 0.94), $tv2);\n\t\t\t\t$p4 = translate(trans_scale($p[$fc][$j  ][$i+1], $cf, 0.94), $tv2);\n\t\t\t\t// Generate facelet polygon\n\t\t\t\t$svg .= gen_facelet($p1, $p2, $p3, $p4, $fc * $dim * $dim + $i * $dim + $j);\n\n\t\t}\n\t\treturn $svg;\n\t}\n\n\t/** Generates a polygon SVG tag for cube facelets */\n\tfunction gen_facelet($p1, $p2, $p3, $p4, $seq){\n\t\tglobal $ABBR_COL, $facelets, $scheme, $using_cols, $cc, $T;\n\t\t$fcol = $using_cols ? ($facelets[$seq] == 't' ? 't' : $ABBR_COL[$facelets[$seq]])\n\t\t                    : ($facelets[$seq] == $T ? 't' : $scheme[$facelets[$seq]]);\n\t\treturn \"\\t\\t<polygon fill='#\".\n\t\t\t($fcol == 't' ? '000000' : $fcol).\"' stroke='#$cc' \".\n\t\t\t($fcol == 't' ? \"opacity='0' \" : ' ' ).\"points='\".\n\t\t\t\t$p1[0].','.$p1[1].' '.\n\t\t\t\t$p2[0].','.$p2[1].' '.\n\t\t\t\t$p3[0].','.$p3[1].' '.\n\t\t\t\t$p4[0].','.$p4[1].\"'/>\\n\";\n\t}\n\n\t// Generates svg for an arrow pointing from sticker s1 to s2\n\tfunction gen_arrow($id, $s1, $s2, $sv, $sc, $col){\n\t\tglobal $p, $dim;\n\t\tif($col == 't') return;\n\t\t// Find centre point of each facelet\n\t\t$p1 = Array(($p[$s1[0]][$s1[1]][$s1[2]][0] + $p[$s1[0]][$s1[1]+1][$s1[2]+1][0])/2,\n\t\t\t($p[$s1[0]][$s1[1]][$s1[2]][1] + $p[$s1[0]][$s1[1]+1][$s1[2]+1][1])/2, 0);\n\t\t$p2 = Array(($p[$s2[0]][$s2[1]][$s2[2]][0] + $p[$s2[0]][$s2[1]+1][$s2[2]+1][0])/2,\n\t\t\t($p[$s2[0]][$s2[1]][$s2[2]][1] + $p[$s2[0]][$s2[1]+1][$s2[2]+1][1])/2, 0);\n\t\t// Find midpoint between p1 and p2\n\t\t$cp = Array(($p1[0] + $p2[0])/2, ($p1[1] + $p2[1])/2, 0);\n\t\t// Shorten arrows towards midpoint according to config\n\t\t$p1 = trans_scale($p1, $cp, $sc);\n\t\t$p2 = trans_scale($p2, $cp, $sc);\n\t\tif($sv){\n\t\t\t$pv = Array(($p[$sv[0]][$sv[1]][$sv[2]][0] + $p[$sv[0]][$sv[1]+1][$sv[2]+1][0])/2,\n\t\t\t\t($p[$sv[0]][$sv[1]][$sv[2]][1] + $p[$sv[0]][$sv[1]+1][$sv[2]+1][1])/2, 0);\n\t\t\t// Project via point double dist from centre point\n\t\t\t$pv = trans_scale($pv, $cp, $sv[3]);\n\t\t}\n\t\t// Calculate arrow rotation\n\t\t$p_ = $sv ? $pv : $p1;\n\t\t$rt = $p_[1] > $p2[1] ? 270 : 90;\n\t\tif($p2[0]-$p_[0] != 0){\n\t\t\t$rt = rad2deg(atan(($p2[1]-($p_[1]))/($p2[0]-$p_[0])));\n\t\t\t$rt = ($p_[0] > $p2[0]) ? $rt + 180 : $rt;\n\t\t}\n\t\treturn '\t\t<path d=\"M '.$p1[0].','.$p1[1].' '.(isset($pv)?'Q '.$pv[0].','.$pv[1]:'L').' '.$p2[0].','.$p2[1].'\"\n\t\t\tstyle=\"fill:none;stroke:#'.$col.';stroke-opacity:1\" />\n\t\t<path transform=\" translate('.$p2[0].','.$p2[1].') scale('.(0.033 / $dim).') rotate('.$rt.')\"\n\t\t\td=\"M 5.77,0.0 L -2.88,5.0 L -2.88,-5.0 L 5.77,0.0 z\"\n\t\t\tstyle=\"fill:#'.$col.';stroke-width:0;stroke-linejoin:round\"/>'.\"\\n\";\n\t}\n\n\t/** Converts svg into given format */\n\tfunction convert($svg, $fmt, $size) {\n\t\tglobal $CONVERT;\n\t\t$opts = gen_image_opts($fmt, $size, \"svg:-\", \"$fmt:-\");\n\t\t$descriptorspec = array(0 => array(\"pipe\", \"r\"), 1 => array(\"pipe\", \"w\"));\n\t\t$convert = proc_open(\"$CONVERT $opts\", $descriptorspec, $pipes);\n\t\tfwrite($pipes[0], $svg);\n\t\tfclose($pipes[0]);\n\t\t$img = null;\n\t\twhile(!feof($pipes[1])) {\n\t\t\t$img .= fread($pipes[1], 1024);\n\t\t}\n\t\tfclose($pipes[1]);\n\t\tproc_close($convert);\n\t\treturn $img;\n\t}\n\n\t/** Alternative version using files rather than pipes,\n\tnot desired because of collision possibilities.. */\n\tfunction convert_file($svg, $fmt, $size) {\n\t\tglobal $CONVERT;\n\t\t$svgfile = fopen(\"/tmp/visualcube.svg\", 'w');\n\t\tfwrite($svgfile, $svg);\n\t\tfclose($svgfile);\n\t\t$opts = gen_image_opts($fmt, $size, '/tmp/visualcube.svg', \"/tmp/visualcube.$fmt\");\n\t\t$rsvg = exec(\"$CONVERT $opts\");\n\t\t$imgfile = fopen(\"/tmp/visualcube.$fmt\", 'r');\n\t\t$img = null;\n\t\twhile($imgfile and !feof($imgfile)) {\n\t\t\t$img .= fread($imgfile, 1024);\n\t\t}\n\t\tfclose($imgfile);\n\t\treturn $img;\n\t}\n\n\t/** Generate ImageMagick options depending on format */\n\tfunction gen_image_opts($fmt, $size, $infile, $outfile){\n\t\t$inopts = ' -density 600 -resize '.$size.'x'.$size;\n\t\t$outopts = ' -channel RGBA -alpha set';\n//\t\t$opts .= '+label \"Generated by VisualCube\"';\n//\t\t$opts .= ' -comment \"Generated by VisualCube\"';\n//\t\t$opts .= ' -caption \"Generated by VisualCube\"';\n//\t\t$opts = \"-gaussian 1\";\n\t\tswitch($fmt){\n\t\t\tcase 'png' : $inopts .= \" -background none\"; $outopts .= \" -quality 100 -define png:format=png32\";\n\t\t\tbreak;\n\t\t\tcase 'gif' : $inopts .= \" -background none\";\n\t\t\tbreak;\n\t\t\tcase 'ico' : $inopts .= \" -background none\";\n\t\t\tbreak;\n\t\t\tcase 'jpg' : $outopts .= \" -quality 90\";\n\t\t\tbreak;\n\n\t\t}\n\t\treturn \"$inopts $infile $outopts $outfile\";\n\t}\n\n\t/** Sends image to browser */\n\tfunction display_img($img, $fmt){\n\t\t$mime = $fmt;\n\t\tswitch($fmt){\n\t\t\tcase 'jpe' :\n\t\t\tcase 'jpg' : $mime = 'jpeg'; break;\n\t\t\tcase 'svg' : $mime = 'svg+xml'; break;\n\t\t\tcase 'ico' : $mime = 'vnd.microsoft.icon'; break;\n\t\t}\n\t\theader(\"Content-type: image/$mime\");\n//\t\theader(\"Content-Length: \" . filesize($img) .\"; \");\n\t\techo $img;\n\t}\n\n\n\n\n\n\t// -----------------------------[ DB Access Functions ]--------------------------\n\n\t// Return result of sql query as array\n\tfunction get_arrays($mysql_con, $query){\n\t\t$result = mysqli_query($mysql_con, $query);\n\t\t$count = mysqli_num_rows($result);\n\t\tif($count <= 0) return null;\n\t\t$ary = Array($count);\n\t\t$i = 0;\n\t\twhile($record = mysqli_fetch_array($result, MYSQLI_ASSOC)){\n\t\t\t$ary[$i] = $record;\n\t\t\t$i++;\n\t\t}\n\t\treturn $ary;\n\t}\n?>\n"
  },
  {
    "path": "visualcube_api.php",
    "content": "<?php\n\n\t// Import globals\n\tglobal\n\t\t$VERSION,\n\t\t$MAX_PZL_DIM,\n\t\t$ENABLE_COOKIES,\n\t\t$DEFAULTS;\n\n\t// XML definition\n\t$HTML_DEF = '<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>'.\"\\n\".\n\t\t'<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"'.\"\\n\".\n\t\t'   \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">'.\"\\n\";\n\n\t$CUBE_CRIDER=\"http://cube.crider.co.uk/\";\n\t\n\t// Start page render\n\techo $HTML_DEF;\n?>\n<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\" xml:lang=\"en\">\n\t<head>\n\t\t<title>VisualCube (v<?php echo $VERSION; ?>)</title>\n\t\t<meta name=\"description\"        content=\"Rubiks cube visualiser\"/>\n\t\t<meta name=\"keywords\"           content=\"visualcube, visual cube, imagecube, image cube, cube vis, viscube, visual rubiks, imgcube, cube image, cube gif, cub png, cube jpeg\"/>\n\t\t<meta name=\"resource-type\"      content=\"document\"/>\n\t\t<meta name=\"language\"           content=\"English\"/>\n\t\t<meta name=\"rating\"             content=\"general\"/>\n\t\t<meta name=\"robots\"             content=\"all\"/>\n\t\t<meta name=\"expires\"            content=\"never\"/>\n\t\t<meta name=\"revisit-after\"      content=\"14 days\"/>\n\t\t<meta name=\"distribution\"       content=\"global\"/>\n\t\t<meta name=\"author\"             content=\"Conrad Rider\"/>\n\t\t<meta name=\"copyright\"          content=\"Copyright © 2009-2010 Conrad Rider\"/>\n\t\t<meta http-equiv=\"Content-Type\" content=\"text/html; iso-8859-1\"/>\n\t\t<script type=\"text/javascript\">\n\t\t\t//<![CDATA[\n\t\t\tfunction setCookie(cname, value){\n\t\t\t\tvar days = 365;\n\t\t\t\tvar date = new Date();\n\t\t\t\tdate.setTime(date.getTime()+(days*24*60*60*1000));\n\t\t\t\tvar expires = \"; expires=\"+date.toGMTString();\n\t\t\t\tdocument.cookie = cname + \"=\" + value + expires + \"; path=/\";\n\t\t\t}\n\t\t\t//]]>\n\t\t</script>\n\t\t<style media=\"screen\" type=\"text/css\">\n\t\t\t@import url(\"screen.css\");\n\t\t\ttable{\n\t\t\t\tborder-spacing:0px; \n\t\t\t\tborder-top:1px solid silver;\n\t\t\t\tborder-left:1px solid silver;\n\t\t\t\tborder-right:0;\n\t\t\t\tborder-bottom:1px solid silver;\n\t\t\t\tbackground-color:#FAFAFA; \n\t\t\t\t\n\t\t\t}\n\t\t\tth{\n\t\t\t\tbackground-color:#DDDDDD; \n\t\t\t\tborder-right:1px solid silver;\n\t\t\t\ttext-align:center;\n\t\t\t}\n\t\t\th2{\n\t\t\t\tmargin-top:10px;\n\t\t\t}\n\t\t\ttd{\n\t\t\t\tvertical-align:top;\n\t\t\t\tborder-top:1px solid grey; \n\t\t\t\tborder-right: 1px solid silver;\n\t\t\t\tpadding:10px;\n\t\t\t}\n\t\t\tem{\n\t\t\t\tfont-style:normal;\n\t\t\t\tfont-weight:bold;\n\t\t\t}\n\t\t\t#examples img{\n\t\t\t\tborder:0;\n\t\t\t\tfloat:none;\n\t\t\t}\n\t\t\t#content_v{\n\t\t\t\tmargin:10px 50px;\n\t\t\t\ttext-align:left;\n\t\t\t}\n\t\t\t#header_v{\n\t\t\t\tmargin:10px 50px;\n\t\t\t\ttext-align:left;\n\t\t\t\t\n\t\t\t}\n\t\t\t#header_v img{\n\t\t\t\tfloat:right;\n\t\t\t\tborder:none;\n\t\t\t}\n\t\t\t#cookie_list{\n\t\t\t\twidth:250px;\n\t\t\t}\n\t\t\t#cookie_list li{\n\t\t\t\tmargin-bottom:20px;\n\t\t\t}\n\t\t\t#cookie_list input{\n\t\t\t\tfloat:right;\n\t\t\t\twidth:60px;\n\t\t\t}\n\t\t</style>\n\t</head>\n\t<body>\n\t\t<div id=\"nav_box\">\n\t\t\tcube.crider.co.uk\n\t\t\t<ul>\n\t\t\t\t<li><a href=\"<?php echo $CUBE_CRIDER; ?>\">ZZ Tutorial</a></li>\n\t\t\t\t<li><a href=\"<?php echo $CUBE_CRIDER; ?>beginner.php\">Beginner Tutorial</a></li>\n\t\t\t\t<li><a href=\"http://www.ctimer.co.uk\">cTimer</a></li>\n\t\t\t\t<li><em>VisualCube</em></li>\n\t\t\t\t<li><a href=\"<?php echo $CUBE_CRIDER; ?>algtrans.html\">Algorithm Translator</a></li>\n\t\t\t\t<li><a href=\"<?php echo $CUBE_CRIDER; ?>coracle.html\">Coracle Lookahead Drill</a></li>\n\t\t\t\t<li><a href=\"<?php echo $CUBE_CRIDER; ?>scrambler.html\">Random State Scrambler</a></li>\n\t\t\t\t<li><a href=\"http://www.crider.co.uk\">Author's Homepage</a></li>\n\t\t\t</ul>\n\t\t</div>\n\t\t<div id=\"header_v\">\n\t\t\t<a href=\"http://validator.w3.org/check?uri=referer\" title=\"Valid XHTML 1.0 Strict\">\n\t\t\t\t<img src=\"http://www.w3.org/Icons/valid-xhtml10\" alt=\"Valid XHTML 1.0 Strict\" width=\"88\" height=\"31\" style=\"margin-right:200px\"/>\n\t\t\t</a>\n\t\t\t<h1>VisualCube</h1>\n\t\t\tGenerate custom Rubik's cube visualisations from your browser address bar.\n\t\t\t<br/><br/>\n\t\t</div>\n\t\t<div id=\"content_v\">\n\t\t\t<h2>Instructions</h2>\n\t\t\tSimply point your browser to visualcube.php to create a cube image.<br/>\n\t\t\tA range of parameters can be set to customise the cube visualisation.<br/>\n\t\t\tClick the link below for an example:<br/><br/>\n\t\t\t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n\t\t\t<a href=\"visualcube.php?fmt=<?php echo $DEFAULTS['fmt']; ?>&size=150&amp;pzl=2&amp;alg=R2F2R2\">cube.crider.co.uk/visualcube.php?fmt=<?php echo $DEFAULTS['fmt']; ?>&size=150&amp;pzl=2&amp;alg=R2F2R2</a>\n\t\t\t<br/><br/><br/>\n\t\t\t<h2>Examples</h2>\n\t\t\tThe following images were generated by VisualCube.\n\t\t\t<br/><br/>\n\t\t\t<div id=\"examples\">\n\t\t\t\t<a href=\"visualcube.php?fmt=<?php echo $DEFAULTS['fmt']; ?>&amp;size=200&amp;alg=S2M2E2\">\n\t\t\t\t\t<img src=\"visualcube_0.gif\" alt=\"Example 0\"/></a>\n\t\t\t\t<a href=\"visualcube.php?fmt=<?php echo $DEFAULTS['fmt']; ?>&amp;size=200&amp;view=plan&amp;case=RUR'U'R'U2RUR'UR2U2R'y2&amp;arw=U1U5,U5U1,U3U7,U7U3\">\n\t\t\t\t\t<img src=\"visualcube_1.gif\" alt=\"Example 1\"/></a>\n\t\t\t\t<a href=\"visualcube.php?fmt=<?php echo $DEFAULTS['fmt']; ?>&amp;size=200&amp;co=30&amp;co=12&amp;fo=50\">\n\t\t\t\t\t<img src=\"visualcube_2.gif\" alt=\"Example 2\"/></a>\n\t\t\t\t<a href=\"visualcube.php?fmt=<?php echo $DEFAULTS['fmt']; ?>&amp;size=200&amp;r=y45x34&amp;cc=n&amp;fo=100&amp;co=35&amp;fd=tototototttttttttttttofotfttdtodotdtttttttttttttobotbt\">\n\t\t\t\t\t<img src=\"visualcube_3.gif\" alt=\"Example3\"/></a>\n\t\t\t\t<a href=\"visualcube.php?fmt=<?php echo $DEFAULTS['fmt']; ?>&amp;size=200&amp;pzl=7&amp;cc=grey&amp;r=z-15x-105&amp;sch=yyyyyy&amp;fc=ynyyynynnnynnnyyyyyyyyynnnyynyynyynnnyyynnynnnnny\">\n\t\t\t\t\t<img src=\"visualcube_4.gif\" alt=\"Example4\"/></a>\n\t\t\t</div>\n\t\t\tClick each cube to see how it was generated..\n\t\t\t<br/><br/><br/>\n\t\t\t<h2>Features</h2>\n\t\t\t<ul>\n\t\t\t\t<li>Fully 3D cube visualisation</li>\n\t\t\t\t<li>Cube dimensions from 1x1x1 to NxNxN</li>\n\t\t\t\t<li>Algorithm support</li>\n\t\t\t\t<li>Complete orientation control</li>\n\t\t\t\t<li>Multiple image formats</li>\n\t\t\t\t<li>Custom image size</li>\n\t\t\t\t<li>Cube and facelet transparency</li>\n\t\t\t\t<li>Custom colour schemes</li>\n\t\t\t\t<li>Custom background colour</li>\n\t\t\t\t<li>Image caching for speedy access</li>\n\t\t\t\t<li>Cookie configurable variables</li>\n\t\t\t</ul>\n\t\t\t<br/>\n\t\t\t<h2>To Do</h2>\n\t\t\t<div>\n\t\t\t\tThe following features/bug fixes are planned for the future (ordered by priority).\n\t\t\t\t<ul>\n\t\t\t\t\t<li>More special views added to the 'view' variable (permutation arrows for example)</li>\n\t\t\t\t\t<li>Visualisation of other puzzles inc square-1, megaminx and pyraminx</li>\n\t\t\t\t</ul>\n\t\t\t\t<br/><br/>\n\t\t\t</div>\n\t\t\t<h2>Source Code</h2>\n\t\t\t<div> The source code for this script is available under the GNU Lesser General Public License at\n\t\t\t<a href=\"https://github.com/Cride5/visualcube\">github.com/Cride5/visualcube</a>.</div>\n\t\t\t<br/><br/>\n\t\t\t<h2>Parameters</h2>\n\t\t\t<table>\n\t\t\t\t<tr><th>Variable</th><th>Description</th><th>Value Range</th><th>Default</th><th>Comment</th></tr>\n\t\t\t\t<tr><td><em>.</em></td><td>script extension</td><td> .png | .gif | .jpg | .svg | .tiff | .ico </td><td>.<?php echo $DEFAULTS['fmt']; ?></td>\n\t\t\t\t\t<td>The extension used in the script url dictates the format of the image generated. For example:\n\t\t\t\t\tvisiting <a href=\"visualcube.jpg\">/visualcube.jpg</a>\n\t\t\t\t\twill create a jpeg image.</td></tr>\n\t\t\t\t<tr><td><em>fmt</em></td><td>Image Format</td><td> png | gif | jpg | svg | tiff | ico </td><td><?php echo $DEFAULTS['fmt']; ?></td>\n\t\t\t\t\t<td>Used as an alternative to using an image extension as above.</td></tr>\n\t\t\t\t<tr><td><em>pzl</em></td><td>Puzzle Type</td><td>1 to <?php echo $MAX_PZL_DIM; ?></td><td><?php echo $DEFAULTS['pzl']; ?></td>\n\t\t\t\t\t<td>Values from N=(1 to <?php echo $MAX_PZL_DIM; ?>) represent an NxNxN cube. Currently only regular cubes are modelled</td></tr>\n\t\t\t\t<tr><td><em>size</em></td><td>Size of generated image</td><td>0 to 1024</td><td><?php echo $DEFAULTS['size']; ?></td><td>\n\t\t\t\t\tImages produced are square, so size specifies both width and height of the final image in pixels.</td></tr>\n\t\t\t\t<tr><td><em>view</em></td><td>Special View</td><td>plan|trans</td><td><?php echo $DEFAULTS['view']; ?>&nbsp;</td>\n\t\t\t\t\t<td>The view parameter allows special views to facilitate interpretation of different case aspects.\n\t\t\t\t\t<br/><em>plan</em> rotates cube to expose U face, while showing the sides of the top layer<br/>\n\t\t\t\t\t<em>trans</em> sets the cube base colour to transparent, and hides any masked or undefined faces</td></tr>\n\t\t\t\t<tr><td><em>stage</em></td><td>Stage Mask</td><td> ( fl | f2l | ll | cll | ell | oll | ocll | oell | coll | ocell | wv |\n\t\t\t\t\t vh | els | cls | cmll | cross | f2l_3 | f2l_2 | f2l_sm | f2l_1 | f2b | line | 2x2x2 | 2x2x3 )-?[xyz2']*\n\t\t\t\t\t </td><td><?php echo $DEFAULTS['stage']; ?>&nbsp;</td>\n\t\t\t\t\t<td>Sets parts of the cube to be masked from being coloured. The stage identifier is optionally followed by a rotation algorithm. For example: <em>stage=cross-x2</em> would rotate the cross mask so that the cross appears on the U-layer rather than the D-layer</td></tr>\n\t\t\t\t<tr><td><em>r</em></td><td>Rotation Sequence</td><td>([xyz]-?[0-9][0-9]?[0-9]?)+</td><td><?php echo $DEFAULTS['r']; ?></td>\n\t\t\t\t\t<td>Each entry in the sequence is an axis (x, y or z), followed by the\n\t\t\t\t\tnumber of degrees to rotate in a clockwise direction. Negative values are permitted.\n\t\t\t\t\tAny number of rotations is possible.</td></tr>\n\t\t\t\t<tr><th colspan=\"5\">State Definitions</th></tr>\n\t\t\t\t<tr><td><em>alg</em></td><td>Algorithm to apply</td><td>[UDLRFBudlrfbMESxyz'2 ]*</td><td><?php echo $DEFAULTS['alg']; ?>&nbsp;</td>\n\t\t\t\t\t<td>The system applies the algorithm to the cube and displays the resulting state.<br/><br/>\n\t\t\t\t\t<em>NOTE:</em> Currently unavailable for 4x4 cubes or above</td></tr>\n\t\t\t\t<tr><td><em>case</em></td><td>Algorithm to solve case</td><td>[UDLRFBudlrfbMESxyz'2 ]*</td><td><?php echo $DEFAULTS['case']; ?>&nbsp;</td>\n\t\t\t\t\t<td>The system displays the cube state which is solved by applying the algorithm\n\t\t\t\t\t(algorithm inverse).<br/><br/>\n\t\t\t\t\t<em>NOTE:</em> Currently unavailable for 4x4 cubes or above</td></tr>\n\t\t\t\t<tr><td><em>fd</em></td><td>Facelet Definition</td><td>[udlrfbnot]*</td>\n\t\t\t\t\t<td><?php echo $DEFAULTS['fd'] != '' ? insert_space($DEFAULTS['fd'], $DEFAULTS['pzl']) :\n\t\t\t\t\t'uuuuuuuuu rrrrrrrrr fffffffff ddddddddd lllllllll bbbbbbbbb'; ?></td>\n\t\t\t\t\t<td>Defines the cube state in terms of facelet positions.\n\t\t\t\t\tu stands for a 'U' facelet (and likewise with rfdlb).\n\t\t\t\t\tDefining a cube state using this method means the cube will be coloured\n\t\t\t\t\taccording to the scheme defined by the <em>sch</em> variable.\n\t\t\t\t\tThree special characters are used to indicate the following:<br/>\n\t\t\t\t\t<em>n</em>: This is a blank face (coloured grey)<br/>\n\t\t\t\t\t<em>o</em>: This is an 'oriented' face (coloured silver)<br/>\n\t\t\t\t\t<em>t</em>: This is a transparent face, and will not appear on the cube\n\t\t\t\t\t</td></tr>\n\t\t\t\t<tr><td><em>fc</em></td><td>Facelet Colours</td><td>[ndlswyrobgmpt]*</td>\n\t\t\t\t\t<td><?php echo $DEFAULTS['fc'] != '' ? insert_space($DEFAULTS['fc'], $DEFAULTS['pzl']) :\n\t\t\t\t\t'yyyyyyyyy rrrrrrrrr bbbbbbbbb wwwwwwwww ooooooooo ggggggggg'; ?></td>\n\t\t\t\t\t<td>Defines the cube state in terms of facelet colours,\n\t\t\t\t\tas an alternative to using fd. fc\n\t\t\t\t\ttakes precedence over fd if both are defined.<br/><br/>\n\t\t\t\t\tSee the 'sch' variable below for an explanation of the colour codes.\n\t\t\t\t\t</td></tr>\n\t\t\t\t<tr><th colspan=\"5\">Cube Style</th></tr>\n\t\t\t\t<tr><td><em>sch</em></td><td>Colour Scheme</td>\n\t\t\t\t\t<td>[ndlswyrobgmp]*6 <br/><br/>OR<br/><br/>\n\t\t\t\t\tComma separated list containing 6x one of:<br/>\n\t\t\t\t\tblack | dgrey | grey | silver | white | yellow | red | orange | blue\n\t\t\t\t\t| green | purple | pink | [0-9a-fA-F]*3 | [0-9a-fA-F]*6\n\t\t\t\t\t</td><td><?php echo $DEFAULTS['sch']; ?></td>\n\t\t\t\t\t<td>The order of the colours specified represent the faces in this order:<br/><br/>\n\t\t\t\t\tU R F D L B\n\t\t\t\t\t<br/><br/>The letters used in shorthand notation map to the generic\n\t\t\t\t\tface colours as follows: n=black (noir), d=dark grey, l=light grey,\n\t\t\t\t\ts=silver (lighter grey), w=white, y=yellow, r=red, o=orange, b=blue, g=green,\n\t\t\t\t\tm=purple (magenta), p=pink, t=transparent<br/><br/>\n\t\t\t\t\tWhere longhand notation is used, 3 or 6-digit hex codes can be used to specify\n\t\t\t\t\tthe exact colour. for example C80000 would be a dark red colour.\n\t\t\t\t\t</td></tr>\n\t\t\t\t<tr><td><em>bg</em></td><td>Background Colour of image</td><td>[ndlswyrobgmpt] <br/><br/>OR<br/><br/>\n\t\t\t\t\tblack | dgrey | grey | silver | white | yellow | red | orange | blue\n\t\t\t\t\t| green | purple | pink | [0-9a-fA-F]*3 | [0-9a-fA-F]*6\n\t\t\t\t\t</td>\n\t\t\t\t\t<td><?php echo $DEFAULTS['bg']; ?></td><td>The value <em>t</em> represents transparent, and is only valid for png and gif images.</td></tr>\n\t\t\t\t<tr><td><em>cc</em></td><td>Cube Colour</td><td>see above</td><td><?php echo $DEFAULTS['cc']; ?></td><td>This is the cube's base colour.</td></tr>\n\t\t\t\t<tr><td><em>co</em></td><td>Cube Opacity</td><td>0 to 99</td><td><?php echo $DEFAULTS['co']; ?></td>\n\t\t\t\t\t<td>Setting this value causes the base cube to be transparent.\n\t\t\t\t\tIt means facelets at the back of the cube will also be rendered.\n\t\t\t\t\tA value of 0 gives complete transparency.</td></tr>\n\t\t\t\t<tr><td><em>fo</em></td><td>Facelet Opacity</td><td>0 to 99</td><td><?php echo $DEFAULTS['fo']; ?></td>\n\t\t\t\t\t<td>Setting this value causes the facelets to be rendered with transparency.</td></tr>\n\t\t\t\t<tr><td><em>dist</em></td><td>Projection Distance</td><td>1 to 100</td><td><?php echo $DEFAULTS['dist']; ?></td>\n\t\t\t\t\t<td>Controls the distance of the cube from the perspective of the viewer.</td></tr>\n\t\t\t\t<tr><th colspan=\"5\">Added Features</th></tr>\n\t\t\t\t<tr><td><em>arw</em></td><td>Arrow Definition</td>\n\t\t\t\t\t<td>Comma separated list in the form:<br/><br/>\n\t\t\t\t\t\t&lt;a_from&gt;&lt;a_to&gt;(&lt;a_via&gt;)?(-i[0-9+])?(-s[0-9+])?(-&lt;colour&gt;)?<br/><br/>\n\t\t\t\t\t\tWhere &lt;a_x&gt; is: [URFDLB][0-9]+<br/><br/>\n\t\t\t\t\t\tAnd: &lt;colour&gt; is as defined in the 'sch' variable.</td>\n\t\t\t\t\t<td>-i10-s10</td>\n\t\t\t\t\t<td>Defines a list of arrows to be drawn on the cube.<br/><br/>\n\t\t\t\t\tEach arrow is defined with a minimum of two sticker identifiers to indicate\n\t\t\t\t\twhere it should be drawn from and to. The optional third sticker identifier\n\t\t\t\t\tindicates which sticker it should pass through if a curved arrow is to be drawn.\n\t\t\t\t\tArrows may be scaled so that they fall short, or past the centre of each facelet\n\t\t\t\t\tby specifying the <em>s</em> (scale) parameter after a dash.\n\t\t\t\t\tWhere curved arrows are drawn the degree to which the arrow deviates from a straight\n\t\t\t\t\tpath can be specified via the <em>i</em> (influence) parameter.\n\t\t\t\t\tArrows may also optionally be assigned individual colours, by using a -\n\t\t\t\t\tfollowed by a colour code.\n\t\t\t\t\t<br/><br/>Example:&nbsp;&nbsp;&nbsp;arw=U0U2,U2U8,U8U0,R6R2R0-s8-i5-yellow\n\t\t\t\t\t<br/><br/>\n\t\t\t\t\tThe above example draws 4 arrows. The first three use default colour and scale\n\t\t\t\t\tand point to stickers on the U-face. The fourth arrow \n\t\t\t\t\tpoints from sticker #6 to sticker #2 on the R-face, and is curved\n\t\t\t\t\tso that it passes through sticker #0. The arrow is slightly shortened to 8/10\n\t\t\t\t\tof its full length, and the influence of the pass through sticker in generating\n\t\t\t\t\tthe curve is set to 5/10.\n\t\t\t\t\t</td></tr>\n\t\t\t\t<tr><td><em>ac</em></td><td>Default Arrow Colour</td><td>[ndlswyrobgmp] <br/><br/>OR<br/><br/>\n\t\t\t\t\tblack | dgrey | grey | silver | white | yellow | red | orange | blue\n\t\t\t\t\t| green | purple | pink | [0-9a-fA-F]*3 | [0-9a-fA-F]*6\n\t\t\t\t\t</td>\n\t\t\t\t\t<td>grey</td><td>Arrows will be this colour by default, unless overridden by a\n\t\t\t\t\tcolour specified for each arrow in the arw variable.</td></tr>\n\t\t\t</table>\n\t\t\t<br/><br/><?php if($ENABLE_COOKIES){ ?>\n\t\t\t<h2>Cookie Configuration</h2>\n\t\t\tThe desired values for cookie configurable variables can be set below. <br/>\n\t\t\tRefer to the table above for acceptable values.\n\t\t\t<ul id=\"cookie_list\">\n\t\t\t\t<li><input type=\"text\" value=\"<?php if(array_key_exists('vc_r', $_COOKIE)) echo $_COOKIE['vc_r']?>\"\n\t\t\t\t\tonkeyup=\"setCookie('vc_r', this.value)\"/>Rotation Sequence (r):</li>\n\t\t\t\t<li><input type=\"text\" value=\"<?php if(array_key_exists('vc_sch', $_COOKIE)) echo $_COOKIE['vc_sch']?>\"\n\t\t\t\t\tonkeyup=\"setCookie('vc_sch', this.value)\"/>Colour Scheme (sch):</li>\n\t\t\t\t<li><input type=\"text\" value=\"<?php if(array_key_exists('vc_bg', $_COOKIE)) echo $_COOKIE['vc_bg']?>\"\n\t\t\t\t\tonkeyup=\"setCookie('vc_bg', this.value)\"/>Background Colour (bg):</li>\n\t\t\t\t<li><input type=\"text\" value=\"<?php if(array_key_exists('vc_cc', $_COOKIE)) echo $_COOKIE['vc_cc']?>\"\n\t\t\t\t\tonkeyup=\"setCookie('vc_cc', this.value)\"/>Cube Colour (cc):</li>\n\t\t\t\t<li><input type=\"text\" value=\"<?php if(array_key_exists('vc_co', $_COOKIE)) echo $_COOKIE['vc_co']?>\"\n\t\t\t\t\tonkeyup=\"setCookie('vc_co', this.value)\"/>Cube Opacity (co):</li>\n\t\t\t\t<li><input type=\"text\" value=\"<?php if(array_key_exists('vc_fo', $_COOKIE)) echo $_COOKIE['vc_fo']?>\"\n\t\t\t\t\tonkeyup=\"setCookie('vc_fo', this.value)\"/>Facelet Opacity (fo):</li>\n\t\t\t\t<li><input type=\"text\" value=\"<?php if(array_key_exists('vc_dist', $_COOKIE)) echo $_COOKIE['vc_dist']?>\"\n\t\t\t\t\tonkeyup=\"setCookie('vc_dist', this.value)\"/>Projection Distance (dist):</li>\n\t\t\t</ul><?php } ?>\n\t\t</div>\n\t\t<div id=\"footer\">\n\t\t\tCopyright &copy; 2010 <a href=\"http://www.crider.co.uk\">Conrad Rider</a>.\n\t\t\t<br/>Cube images generated by VisualCube are free to copy and distribute\n\t\t</div>\n\t</body>\n</html>\n"
  },
  {
    "path": "visualcube_config.php",
    "content": "<?php\n\n\n\t// Maximum cube dimension to render (pzl)\n\t$MAX_PZL_DIM = 10;\n\n\t// Whether to allow cookie config of style variables (r, sch, bg, cc, co, fo, dist)\n\t$ENABLE_COOKIES = true;\n\n\t// Default rendering values\n\t$DEFAULTS = Array(\n\t\t'fmt'   => 'svg',\n\t\t'pzl'   => '3',\n\t\t'size'  => '128',\n\t\t'view'  => '',\n\t\t'stage' => '',\n\t\t'r'     => 'y45x-34',\n\t\t'alg'   => '',\n\t\t'case'  => '',\n\t\t'fd'    => '',\n\t\t'fc'    => '',\n\t\t'sch'   => 'yrbwog',\n\t\t'bg'    => 'white',\n\t\t'cc'    => 'black',\n\t\t'co'    => '100',\n\t\t'fo'    => '100',\n\t\t'dist'  => '5',\n\t);\n\n\t// Location of ImageMagick's 'convert' binary.\n\t// NOTE: if the $PATH variable is set up correctly using just 'convert' is sufficient\n\t$CONVERT=\"convert\";\n\n\n\t// -------------[ Cache related configuration ]---------------\n\n\n\t// Whether image caching is enabled. NOTE: if enabled a cron\n\t// job will need to be set up to prune the database\n\t$ENABLE_CACHE = false;\n\t// Database Configuration (for image caching)\n\t$DB_HOST=\"localhost\";\n\t$DB_NAME=\"DATABASE_NAME\";\n\t$DB_USERNAME=\"DATABASE_USERNAME\";\n\t$DB_PASSWORD=\"DATABASE_PASSWORD\";\n\t// Maximum size of image to be cached\n\t$CACHE_IMG_SIZE_LIMIT = 10000; // 10Kb\n\n"
  },
  {
    "path": "visualcube_dbprune.sh",
    "content": "#!/bin/sh\n\n# Script to prune virtualcube image cache, to be run weekly\n\n# Prune database by removing all entries which have been accessed less than 1 times\nmysql -u DB_USER --safe-updates=0 --password=\"DB_PASS\" DB_NAME -e \"DELETE FROM vcache WHERE rcount < 1\"\n\n# Reset all existing entrys to 0\nmysql -u DB_USER --safe-updates=0 --password=\"DB_PASS\" DB_NAME -e \"UPDATE vcache SET rcount=0\"\n\n# Query to display db contents\n#mysql -u DB_USER --password=\"DB_PASS\" DB_NAME -e \"SELECT hash, fmt, req, rfr, rcount, OCTET_LENGTH(img) FROM vcache\"\n\n# This crontab line will run it at 4 am every Monday\n#00 4 * * Mon /path/to/visualcube_dbprune.sh \"Prune visualcube image cache\"\n# To install crontab on multi-user system place the line in a file and then run:\n# crontab <filename>\n\n\n"
  }
]