Further work on Moodle integration.
[moodle/mihaisucan.git] / lib / paintweb / ext / moodle / imagesave.php
blob4955b5659acc5541982f326dda99cfc78f929616
1 <?php
2 /*
3 * Copyright (C) 2009 Mihai Şucan
5 * This file is part of PaintWeb.
7 * PaintWeb is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * PaintWeb is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with PaintWeb. If not, see <http://www.gnu.org/licenses/>.
20 * $URL: http://code.google.com/p/paintweb $
21 * $Date: 2009-07-30 22:31:47 +0300 $
24 // This script performs asynchronous image save in PaintWeb. This is used by the
25 // Moodle extension of PaintWeb, to save image edits.
27 require_once('../../../../config.php');
29 function paintwebSaveDone ($successful, $errorMessage = null) {
30 global $imgUrl, $imgUrlNew;
32 $output = array(
33 'successful' => $successful,
34 'url' => $imgUrl,
35 'urlNew' => $imgUrlNew,
36 'errorMessage' => $errorMessage
39 echo json_encode($output);
40 exit;
43 function prepareImgUrl () {
44 global $CFG, $imgUrl, $imgUrlNew, $imgDest, $imgProxies, $myProxy, $imgDataURL, $imgAllowedTypes, $imgInfo;
46 // check if the image URL points to a proxy.
47 foreach ($imgProxies as $proxy => $type) {
48 if (strpos($imgUrl, $proxy) !== 0) {
49 continue;
52 $relpath = substr($imgUrl, strlen($proxy));
53 if ($relpath{0} === '?') {
54 $relpath = substr($relpath, 1);
55 $pathvars = array();
56 parse_str($relpath, $pathvars);
57 if (isset($pathvars['file'])) {
58 $relpath = $pathvars['file'];
59 } else {
60 $relpath = false;
62 unset($pathvars);
64 } else if (strpos($relpath, '?')) {
65 $relpath = substr($relpath, 0, strpos($relpath, '?'));
68 if (!$relpath) {
69 continue;
72 $relpath = trim($relpath, '/');
73 $tmpDest = $CFG->dataroot . '/' . $relpath;
75 if (!file_exists($tmpDest) || !is_writeable($tmpDest) || is_dir($tmpDest)) {
76 continue;
79 $fname = basename($tmpDest);
80 $ext = substr($fname, strrpos($fname, '.') + 1);
81 $ftype = array_search(strtolower($ext), $imgAllowedTypes);
83 if (!$ftype || $ftype !== $imgInfo[0]) {
84 continue;
87 // check permissions, if the image proxy is for a course file.
88 if ($type === 'course') {
89 $arrpath = explode('/', $relpath);
90 if (count($arrpath) < 2) {
91 continue;
94 $course = get_record('course', 'id', $arrpath[0]);
95 require_login($course);
96 require_capability('moodle/course:managefiles', get_context_instance(CONTEXT_COURSE, $course->id));
99 $imgDest = $tmpDest;
100 return;
103 $imgDest = $CFG->dataroot . '/' . $CFG->paintwebImagesFolder;
105 if (!is_dir($imgDest) || !make_upload_directory($imgDest, false)) {
106 paintwebSaveDone(false, 'failed to mkdir ' . $imgDest);
109 // simply create a new file in the PainWeb images folder.
110 $fname = sha1($imgDataURL) . '.' . $imgAllowedTypes[$imgInfo[0]];
111 $imgDest .= '/' . $fname;
112 $imgUrlNew = $CFG->wwwroot . '/' . $myProxy . '?img=' . $fname;
115 // The list of allowed image MIME types associated to file extensions.
116 $imgAllowedTypes = array('image/png' => 'png', 'image/jpeg' => 'jpg');
118 // The list of file serving proxies recognized from Moodle. For example, course
119 // images are served by /file.php. So, when you add an image in TinyMCE, the
120 // path will be like /file.php/course_id/dir/file.ext.
121 $imgProxies = array(
122 $CFG->wwwroot . '/file.php' => 'course', // course files
123 $CFG->httpswwwroot . '/file.php' => 'course' // course files
126 if ($CFG->paintwebDisallowImageUpdates) {
127 $imgProxies = array();
130 $myProxy = dirname(__FILE__) . '/imageview.php';
131 if (strpos($myProxy, $CFG->dirroot) === 0) {
132 $myProxy = trim(substr($myProxy, strlen($CFG->dirroot)), '/');
133 } else {
134 paintwebSaveDone(false, 'failed to find my image file proxy!');
137 $imgUrl = $_POST['url'];
138 $imgUrlNew = null;
140 $imgDataURL = &$_POST['dataURL'];
142 if (empty($imgUrl)) {
143 paintwebSaveDone(false, 'empty url');
146 if (empty($imgDataURL)) {
147 paintwebSaveDone(false, 'empty data URL');
150 // A data URL starts like this:
151 // data:[<MIME-type>][;charset="<encoding>"][;base64],<data>
153 // Here we find the comma delimiter.
154 $comma = strpos($imgDataURL, ',');
155 $imgInfo = substr($imgDataURL, 0, $comma);
156 if (empty($imgInfo) || !isset($imgDataURL{($comma+2)})) {
157 paintwebSaveDone(false, 'malformed data URL');
160 // Split by ':' to find the 'data' prefix and the rest of the info.
161 $imgInfo = explode(':', $imgInfo);
163 // The array must have exactly two elements and the second element must not be
164 // empty.
165 if (count($imgInfo) !== 2 || $imgInfo[0] !== 'data' || empty($imgInfo[1])) {
166 paintwebSaveDone(false, 'malformed data URL');
169 // The MIME type must be given and it must be base64-encoded.
170 $imgInfo = explode(';', $imgInfo[1]);
172 if (count($imgInfo) < 2 || !array_key_exists($imgInfo[0], $imgAllowedTypes) ||
173 ($imgInfo[1] !== 'base64' && $imgInfo[2] !== 'base64')) {
174 paintwebSaveDone(false, 'malformed data URL');
177 prepareImgUrl();
179 $imgDataURL = substr($imgDataURL, $comma + 1);
181 if (!file_put_contents($imgDest, base64_decode($imgDataURL))) {
182 paintwebSaveDone(false, 'failed to save file');
185 paintwebSaveDone(true);
187 // vim:set spell spl=en fo=anl1qrowcb tw=80 ts=2 sw=2 sts=2 sta et noai nocin fenc=utf-8 ff=unix: