| 42 | | // Upload failed |
| 43 | | if ($img['error']) { |
| 44 | | unlink_safe($img['tmp_name']); |
| 45 | | errorMsg('Image uplaod error.'); |
| | 45 | foreach ($_FILES['image'] as $img) { |
| | 46 | // Upload failed |
| | 47 | if ($img['error']) { |
| | 48 | unlink_safe($img['tmp_name']); |
| | 49 | continue; |
| | 50 | //errorMsg('Image uplaod error.'); |
| | 51 | } |
| | 52 | |
| | 53 | // The image is to big |
| | 54 | if ($img['size'] > $maxsize) { |
| | 55 | unlink_safe($img['tmp_name']); |
| | 56 | continue; |
| | 57 | //errorMsg('Image too big.'); |
| | 58 | } |
| | 59 | |
| | 60 | /* |
| | 61 | * [0] - width |
| | 62 | * [1] - geight |
| | 63 | * [2] - IMAGETYPE_XXX |
| | 64 | + [3] - Text string with width and height |
| | 65 | * ["bits"] |
| | 66 | * ["channels"] |
| | 67 | * ["mime"] - Mime type |
| | 68 | */ |
| | 69 | $info = getimagesize($img['tmp_name']); |
| | 70 | |
| | 71 | // Check if this type of immage is allowed |
| | 72 | if (!isset($mime[$info['mime']])) { |
| | 73 | unlink_safe($img['tmp_name']); |
| | 74 | continue; |
| | 75 | //errorMsg('Imagetype not allowed.'); |
| | 76 | } |
| | 77 | |
| | 78 | // Assign the correct extension for this image |
| | 79 | $name = (get_magic_quotes_gpc()) ? stripslashes($img['name']) : $img['name']; |
| | 80 | $name = str_replace('\'', '', $name); |
| | 81 | $name = explode('.', $name); |
| | 82 | |
| | 83 | if(count($name) < 2) { |
| | 84 | $name = $name[0] . '.' . $mime[$info['mime']]; |
| | 85 | } else { |
| | 86 | $name[count($name) - 1] = $mime[$info['mime']]; |
| | 87 | $name = implode('.', $name); |
| | 88 | } |
| | 89 | |
| | 90 | // Choose the location for the file |
| | 91 | $name = trim(str_replace('//', '/', checkExists($imgdir . '/' . $name))); |
| | 92 | |
| | 93 | // Generate a URL save string to send to the browser |
| | 94 | $location = explode('/', $name); |
| | 95 | for ($i = 0; $i < count($location); $i++) { |
| | 96 | $location[$i] = rawurlencode($location[$i]); |
| | 97 | } |
| | 98 | $location = implode('/', $location); |
| | 99 | |
| | 100 | // Move the file to it's new location |
| | 101 | if (!move_uploaded_file_save($img['tmp_name'], $name)) { |
| | 102 | unlink_safe($img['tmp_name']); |
| | 103 | errorMsg('Can\'t move uploaded file.'); |
| | 104 | } |
| | 105 | |
| | 106 | /* |
| | 107 | * Create preview |
| | 108 | * |
| | 109 | * We use imagemagick because it suports a broad range of file |
| | 110 | * types |
| | 111 | * |
| | 112 | * Also, we call it directly with exec |
| | 113 | * |
| | 114 | * See http://www.imagemagick.org/Usage/thumbnails/ for more |
| | 115 | * information about the commands used |
| | 116 | */ |
| | 117 | $preview = dirname($name) . '/preview/' . basename($name); |
| | 118 | if (!file_exists(dirname($preview))) mkdir(dirname($preview)); |
| | 119 | exec('convert -define jpeg:size=' . $preview_width * 2 . 'x' . $preview_height * 2 . ' \\ |
| | 120 | \'' . $name . '\'[0] -thumbnail ' . $preview_width . 'x' . $preview_height . ' \\ |
| | 121 | -unsharp 0x.5 \'' . $preview . '\''); |
| | 122 | |
| | 123 | $user = (isset($_SESSION['openid_identity'])) ? $_SESSION['openid_identity'] : ''; |
| | 124 | |
| | 125 | // Save image info |
| | 126 | $db->exec("INSERT INTO images ( |
| | 127 | location, |
| | 128 | path, |
| | 129 | ip, |
| | 130 | time, |
| | 131 | original_name, |
| | 132 | user |
| | 133 | ) VALUES ( |
| | 134 | '" . $db->escape($location) . "', |
| | 135 | '" . $db->escape($name) . "', |
| | 136 | '" . ip2long($_SERVER['REMOTE_ADDR']) . "', |
| | 137 | '" . $time . "', |
| | 138 | '" . $db->escape($img['name']) . "', |
| | 139 | '" . $db->escape($user) . "' |
| | 140 | );" ); |
| | 141 | $res = $db->query("SELECT last_insert_rowid() as id;"); |
| | 142 | $row = $db->fetch($res); |
| | 143 | $id = $row['id']; |
| | 144 | |
| | 145 | /* |
| | 146 | * Tags |
| | 147 | */ |
| | 148 | if (isset($_POST['tags'])) { |
| | 149 | $tags = explode(',', $_POST['tags']); |
| | 150 | for ($i = 0; $i < count($tags); $i++) { |
| | 151 | $tags[$i] = trim($tags[$i]); |
| | 152 | } |
| | 153 | $tags = array_unique($tags); |
| | 154 | $sql = "BEGIN;\n"; |
| | 155 | foreach ($tags as $tag) { |
| | 156 | if (empty($tag)) continue; |
| | 157 | // check if the taga already exists |
| | 158 | $row = $db->fetch($db->query("SELECT ROWID as id FROM tags WHERE tag = '" . $db->escape(strtolower($tag)) . "'")); |
| | 159 | if (!$row) { |
| | 160 | $db->exec("INSERT INTO tags (tag, text) VALUES ('" . $db->escape(strtolower($tag)) . "', '" . $db->escape($tag) . "');"); |
| | 161 | $row = $db->fetch($db->query("SELECT last_insert_rowid() as id;")); |
| | 162 | } |
| | 163 | // Save the tag for this image and update tag counter |
| | 164 | $sql .= "INSERT INTO imagetags (image, tag) VALUES('" . $id . "', '" . $row['id'] . "');\n"; |
| | 165 | $sql .= "UPDATE tags SET count = count + 1 WHERE ROWID = '" . $row['id'] . "';\n"; |
| | 166 | } |
| | 167 | $sql .= "COMMIT;"; |
| | 168 | // Commit all changes |
| | 169 | $db->exec($sql); |
| | 170 | } |
| | 171 | $uploadcount++; |
| 54 | | /* |
| 55 | | * [0] - width |
| 56 | | * [1] - geight |
| 57 | | * [2] - IMAGETYPE_XXX |
| 58 | | + [3] - Text string with width and height |
| 59 | | * ["bits"] |
| 60 | | * ["channels"] |
| 61 | | * ["mime"] - Mime type |
| 62 | | */ |
| 63 | | $info = getimagesize($img['tmp_name']); |
| 64 | | |
| 65 | | // Check if this type of immage is allowed |
| 66 | | if (!isset($mime[$info['mime']])) { |
| 67 | | unlink_safe($img['tmp_name']); |
| 68 | | errorMsg('Imagetype not allowed.'); |
| 69 | | } |
| 70 | | |
| 71 | | // Assign the correct extension for this image |
| 72 | | $name = (get_magic_quotes_gpc()) ? stripslashes($img['name']) : $img['name']; |
| 73 | | $name = str_replace('\'', '', $name); |
| 74 | | $name = explode('.', $name); |
| 75 | | |
| 76 | | if(count($name) < 2) { |
| 77 | | $name = $name[0] . '.' . $mime[$info['mime']]; |
| 78 | | } else { |
| 79 | | $name[count($name) - 1] = $mime[$info['mime']]; |
| 80 | | $name = implode('.', $name); |
| 81 | | } |
| 82 | | |
| 83 | | // Choose the location for the file |
| 84 | | $name = trim(str_replace('//', '/', checkExists($imgdir . '/' . $name))); |
| 85 | | |
| 86 | | // Generate a URL save string to send to the browser |
| 87 | | $location = explode('/', $name); |
| 88 | | for ($i = 0; $i < count($location); $i++) { |
| 89 | | $location[$i] = rawurlencode($location[$i]); |
| 90 | | } |
| 91 | | $location = implode('/', $location); |
| 92 | | |
| 93 | | // Move the file to it's new location |
| 94 | | if (!move_uploaded_file_save($img['tmp_name'], $name)) { |
| 95 | | unlink_safe($img['tmp_name']); |
| 96 | | errorMsg('Can\'t move uploaded file.'); |
| 97 | | } |
| 98 | | |
| 99 | | /* |
| 100 | | * Create preview |
| 101 | | * |
| 102 | | * We use imagemagick because it suports a broad range of file |
| 103 | | * types |
| 104 | | * |
| 105 | | * Also, we call it directly with exec |
| 106 | | * |
| 107 | | * See http://www.imagemagick.org/Usage/thumbnails/ for more |
| 108 | | * information about the commands used |
| 109 | | */ |
| 110 | | $preview = dirname($name) . '/preview/' . basename($name); |
| 111 | | if (!file_exists(dirname($preview))) mkdir(dirname($preview)); |
| 112 | | exec('convert -define jpeg:size=' . $preview_width * 2 . 'x' . $preview_height * 2 . ' \\ |
| 113 | | \'' . $name . '\'[0] -thumbnail ' . $preview_width . 'x' . $preview_height . ' \\ |
| 114 | | -unsharp 0x.5 \'' . $preview . '\''); |
| 115 | | |
| 116 | | // Open database |
| 117 | | $db = new sqlite('lib/db.sqlite'); |
| 118 | | |
| 119 | | $user = (isset($_SESSION['openid_identity'])) ? $_SESSION['openid_identity'] : ''; |
| 120 | | |
| 121 | | // Save image info |
| 122 | | $db->exec("INSERT INTO images ( |
| 123 | | location, |
| 124 | | path, |
| 125 | | ip, |
| 126 | | time, |
| 127 | | original_name, |
| 128 | | user |
| 129 | | ) VALUES ( |
| 130 | | '" . $db->escape($location) . "', |
| 131 | | '" . $db->escape($name) . "', |
| 132 | | '" . ip2long($_SERVER['REMOTE_ADDR']) . "', |
| 133 | | '" . time() . "', |
| 134 | | '" . $db->escape($img['name']) . "', |
| 135 | | '" . $db->escape($user) . "' |
| 136 | | );" ); |
| 137 | | $res = $db->query("SELECT last_insert_rowid() as id;"); |
| 138 | | $row = $db->fetch($res); |
| 139 | | $id = $row['id']; |
| 140 | | |
| 141 | | /* |
| 142 | | * Tags |
| 143 | | */ |
| 144 | | if (isset($_POST['tags'])) { |
| 145 | | $tags = explode(',', $_POST['tags']); |
| 146 | | for ($i = 0; $i < count($tags); $i++) { |
| 147 | | $tags[$i] = trim($tags[$i]); |
| 148 | | } |
| 149 | | $tags = array_unique($tags); |
| 150 | | $sql = "BEGIN;\n"; |
| 151 | | foreach ($tags as $tag) { |
| 152 | | if (empty($tag)) continue; |
| 153 | | // check if the taga already exists |
| 154 | | $row = $db->fetch($db->query("SELECT ROWID as id FROM tags WHERE tag = '" . $db->escape(strtolower($tag)) . "'")); |
| 155 | | if (!$row) { |
| 156 | | $db->exec("INSERT INTO tags (tag, text) VALUES ('" . $db->escape(strtolower($tag)) . "', '" . $db->escape($tag) . "');"); |
| 157 | | $row = $db->fetch($db->query("SELECT last_insert_rowid() as id;")); |
| 158 | | } |
| 159 | | // Save the tag for this image and update tag counter |
| 160 | | $sql .= "INSERT INTO imagetags (image, tag) VALUES('" . $id . "', '" . $row['id'] . "');\n"; |
| 161 | | $sql .= "UPDATE tags SET count = count + 1 WHERE ROWID = '" . $row['id'] . "';\n"; |
| 162 | | } |
| 163 | | $sql .= "COMMIT;"; |
| 164 | | // Commit all changes |
| 165 | | $db->exec($sql); |
| 166 | | } |
| 167 | | |
| 168 | | // Redirect to image |
| 169 | | header('Location: ' . url() . 'image.php?i=' . urlnumber_encode($id)); |
| 170 | | errorMsg('Image saved.<br /><br /><a href="' . url() . 'image.php?i=' . urlnumber_encode($id) . '">Continue</a>'); |
| 171 | | |