Add some comments
[rockboxthemes.git] / public / upload.php
blob0c191e7301620da4598026ddec0c7855c0abdc89
1 <?php
2 /***************************************************************************
3 * __________ __ ___.
4 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
5 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
6 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
7 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
8 * \/ \/ \/ \/ \/
9 * $Id$
11 * Copyright (C) 2009 Jonas Häggqvist
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
23 require_once('preconfig.inc.php');
25 function checkuploadfields(&$site, &$err) {
26 $lcd = $site->target2lcd($_REQUEST['target']);
28 foreach($_REQUEST as $field => $value) {
29 switch($field) {
30 case 'author':
31 if (strpos($value, ' ') === false) {
32 $err[$field] = sprintf("This doesn't look like a proper full name (should contain at least one whitespace character): %s", $value);
34 break;
35 case 'email':
36 if (!preg_match("/.*@.*\..*/", $value)) {
37 $err[$field] = sprintf("This doesn't look like an email I can reach: %s", $value);
39 break;
40 case 'themename':
41 if (trim($value) == '') {
42 $err[$field] = "You need to provide a theme name";
44 elseif ($site->themenameexists($value, $lcd['mainlcd'])) {
45 $err[$field] = sprintf("A theme with the name '%s' already exists for this target", $value);
47 break;
48 case 'ccbysa':
49 if ($value !== "on") {
50 $err[$field] = "You need to accept to license your work under the Creative Commons Attribution Share Alike license to share your work here.";
52 break;
53 case 'target':
54 case 'description':
55 /* These can't really be wrong */
56 break;
59 if (!isset($_REQUEST['ccbysa'])) {
60 $err['ccbysa'] = "You need to accept to license your work under the Creative Commons Attribution Share Alike license to share your work here.";
64 function checkuploadfiles(&$site, &$err) {
65 $lcd = $site->target2lcd($_REQUEST['target']);
66 $requiredfiles = array('themefile', 'sshot_wps');
67 foreach($requiredfiles as $field) {
68 if (!isset($_FILES[$field]) || empty($_FILES[$field]['tmp_name'])) {
69 $err[$field] = array("You must provide this file");
72 foreach($_FILES as $name => $values) {
73 if (isset($err[$name]) || empty($_FILES[$field]['tmp_name'])) {
74 if (!empty($values['name'])) {
75 $err[$name][] = "Looks like upload failed. Was the file too large?";
77 continue;
79 $result = array();
80 switch($name) {
81 case 'themefile':
82 $result = $site->validatezip($values);
83 $test = $site->checkwps($values['tmp_name'], $lcd['mainlcd'], $lcd['remotelcd']);
84 $pass = false;
85 /* See if the wps passed at least one target/version combination */
86 foreach($test as $version => $targets) {
87 foreach($targets as $target => $results) {
88 if ($results['pass']) {
89 $pass = true;
91 elseif (!empty($results['output'])) {
92 $output = $results['output'];
96 /* If not, reject with an error */
97 if ($pass == false) {
98 $result[] = sprintf("Your wps didn't pass checkwps. Here's some output: %s", $output);
100 break;
101 case 'sshot_wps':
102 case 'sshot_menu':
103 if (isset($values['tmp_name']) && trim($values['tmp_name']) != "") {
104 $result = $site->validatesshot($values, $lcd['mainlcd']);
106 break;
108 if (count($result) > 0) {
109 $err[$name] = $result;
114 if (isset($_REQUEST['author'])) {
115 $err = array();
116 /* First we do some checking of the uploaded data */
117 checkuploadfields($site, $err);
118 checkuploadfiles($site, $err);
119 if (count($err) > 0) {
120 $t->assign('errors', $err);
122 /* If that went wrong, go on and include the theme */
123 else {
125 * At this stage, the theme has been validated, any possible errors
126 * are now our fault.
128 $lcd = $site->target2lcd($_REQUEST['target']);
131 * Figure out a decent shortname. Use the zipfile name and add some
132 * numbers if that exists.
134 $i = 0;
135 do {
136 $shortname = sprintf("%s%s",
137 basename(str_replace(' ', '_', strtolower($_FILES['themefile']['name'])), '.zip'),
138 $i == 0 ? '' : "-$i"
140 $destdir = sprintf("%s/%s/%s", config::datadir, $lcd['mainlcd'], $shortname);
141 $i++;
142 } while (file_exists($destdir));
144 $result = $site->addtheme(
145 $_REQUEST['themename'],
146 $shortname,
147 $_REQUEST['author'],
148 $_REQUEST['email'],
149 $lcd['mainlcd'],
150 $lcd['remotelcd'] == '' ? false : $lcd['remotelcd'],
151 $_REQUEST['description'],
152 $_FILES['themefile'],
153 $_FILES['sshot_wps'],
154 isset($_FILES['sshot_menu']) ? $_FILES['sshot_menu'] : false
156 if (is_array($result)) {
157 $t->assign('general_errors', $result);
159 else {
160 $insertid = $result;
161 $site->prepareverification($insertid, $_REQUEST['email'], $_REQUEST['author']);
162 $template = "uploadcomplete.tpl";
167 * If no template is set, it either means this is the first load, or upload
168 * failed
170 if (!isset($template)) {
171 $template = "upload.tpl";
172 $targets = $site->listtargets();
173 foreach($targets as $target) {
174 $values['targets'][$target['shortname']] = $target['fullname'];
178 $t->render($template, $values);