Ejemplo n.º 1
0
    public function add_version()
    {
        if (empty($_POST['extension_id'])) {
            exit;
        }
        # TODO
        $extension = new Extension($_POST['extension_id']);
        if (!$extension->editable()) {
            show_403(__("Access Denied"), __("You do not have sufficient privileges to edit this extension.", "extend"));
        }
        if ($_FILES['extension']['error'] == 4) {
            Flash::warning(__("You forgot the extension file, silly!", "extend"), $_SESSION['redirect_to']);
        }
        if (empty($_POST['number'])) {
            Flash::warning(__("Please enter a version number.", "extend"), $_SESSION['redirect_to']);
        }
        if (empty($_POST['compatible'])) {
            Flash::warning(__("Please list the Chyrp versions you know to be compatible with this extension.", "extend"), $_SESSION['redirect_to']);
        }
        $visitor = Visitor::current();
        # Add the MIT license if no license is specified
        $zip = new ZipArchive();
        if ($zip->open($_FILES['extension']['tmp_name']) === true and $zip->locateName("LICENSE") === false) {
            $header = "Copyright (c) " . date("Y") . " " . oneof($visitor->full_name, $visitor->login);
            $mit = <<<EOF
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name(s) of the above
copyright holders shall not be used in advertising or otherwise
to promote the sale, use or other dealings in this Software
without prior written authorization.
EOF;
            $zip->addFromString("LICENSE", $header . "\n\n" . $mit . "\n");
            $zip->close();
        }
        $filename = upload($_FILES['extension'], "zip", "extension/" . pluralize($extension->type->url));
        $image = $_FILES['image']['error'] == 0 ? upload($_FILES['image'], null, "previews/" . pluralize($extension->type->url)) : "";
        $version = Version::add($_POST['number'], $_POST['description'], comma_sep($_POST['compatible']), comma_sep($_POST['tags']), $filename, $image, 0, 0, $extension);
        $files = array();
        foreach ($_FILES['attachment'] as $key => $val) {
            foreach ($val as $file => $attr) {
                $files[$file][$key] = $attr;
            }
        }
        foreach ($files as $attachment) {
            if ($attachment['error'] != 4) {
                $path = upload($attachment, null, "attachments");
                Attachment::add(basename($path), $path, "version", $version->id);
            }
        }
        Flash::notice(__("Version added.", "extend"), $version->url());
    }