mobileupload.php 2.59 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
/**
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
 * Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
 *                         http://wiki.mahara.org/Contributors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @package    mahara
 * @subpackage artefact-file
 * @author     Catalyst IT Ltd
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
 *
 */

define('INTERNAL', 1);
define('PUBLIC', 1);

require(dirname(dirname(dirname(__FILE__))) . '/init.php');
safe_require('artefact', 'file');

$protocol = strtoupper($_SERVER['SERVER_PROTOCOL']);
if ($protocol != 'HTTP/1.1') {
    $protocol = 'HTTP/1.0';
}

$data = new StdClass;
$USER = new User();

42
43
44
45
46
try {
	$USER->find_by_mobileuploadtoken(param_variable('token'));
}
catch (AuthUnknownUserException $e) {
    	header($protocol.' 500 Invalid user token');
47
48
49
	exit;	
}

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
$data->owner = $USER->get('id'); // id of owner

$folder = param_variable('foldername');

if ( $folder == 'Home' ) {
	$data->parent = null;
} else {
	$artefact = ArtefactTypeFolder::get_folder_by_name($folder, null, $data->owner);  // id of folder you're putting the file into
	if ( ! $artefact ) {
    		header($protocol." 500 Upload folder '$folder' does not exit");
		exit;	
	}

	$data->parent = $artefact->id;
}
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
if ( $data->parent == 0 ) $data->parent = null;

$originalname = $_FILES['userfile']['name'];
$originalname = $originalname ? basename($originalname) : get_string('file', 'artefact.file');

$data->title = ArtefactTypeFileBase::get_new_file_title($originalname, $data->parent, $data->owner);

try {
    $newid = ArtefactTypeFile::save_uploaded_file('userfile', $data);
}
catch (QuotaExceededException $e) {
    	header($protocol.' 500 Quota exceeded');
	exit;	
}
catch (UploadException $e) {
    	header($protocol.' 500 Failed to save file');
	exit;	
}

84
85
// Here we need to create a new hash - update our own store of it and return it too the handset
echo $USER->refresh_mobileuploadtoken();
86