upload.php 5.7 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
<?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);
30
31
32
33
define('SECTION_PLUGINTYPE', 'artefact');
define('SECTION_PLUGINNAME', 'blog');

$json = array();
34
35
36

require(dirname(dirname(dirname(__FILE__))) . '/init.php');
safe_require('artefact', 'file');
37
safe_require('artefact', 'blog');
38
39

if (!get_config('allowmobileuploads')) {
40
    jsonreply( array('fail' => 'Mobile uploads disabled') );
41
42
43
44
45
46
47
48
49
50
}

$token = '';
try {
    $token = param_variable('token');
    $token = trim($token);
}
catch (ParameterException $e) { }

if ($token == '') {
51
    jsonreply( array('fail' => 'Auth token cannot be blank') );
52
53
54
55
56
57
58
59
60
}

$username = '';
try {
    $username = trim(param_variable('username'));
}
catch (ParameterException $e) { }

if ($username == '') {
61
    jsonreply( array('fail' => 'Username cannot be blank') );
62
63
64
65
66
67
68
69
70
}

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

try {
    $USER->find_by_mobileuploadtoken($token, $username);
}
catch (AuthUnknownUserException $e) {
71
    jsonreply( array('fail' => 'Invalid user token') );
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
}

$data->owner = $USER->get('id'); // id of owner

$folder = '';
try {
    $folder = param_variable('foldername');
    $folder = trim($folder);

    if ($folder) {
        // TODO: create if doesn't exist - note assumes it is a base folder (hence null parent)
        $artefact = ArtefactTypeFolder::get_folder_by_name($folder, null, $data->owner);  // id of folder you're putting the file into
        if ($artefact) {
            $data->parent = $artefact->id;
            if ($data->parent == 0) {
                $data->parent = null;
            }
        }
        else {
            $fd = (object) array(
                'owner' => $data->owner,
                'title' => $folder,
                'parent' => null,
            );
            $f = new ArtefactTypeFolder(0, $fd);
            $f->commit();
            $data->parent = $f->get('id');
        }
    }
    else {
        $data->parent = null;
    }
}
catch (ParameterException $e) {
    $data->parent = null;
}

// Set title
$title = '';
try {
    $title = param_variable('title');
}
114
catch (ParameterException $e) { }
115
116

// Set description
117
$description = '';
118
try {
119
    $description = param_variable('description');
120
121
122
123
}
catch (ParameterException $e) { }

// Set tags
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
$tags = '';
try {
    $tags = explode(" ", param_variable('tags'));
}
catch (ParameterException $e) { }

$artefact_id = '';
if ( $_FILES ) {

    if ( ! $title ) { 
        basename($_FILES['userfile']['name']);
    }

    try {
        $data->title = ArtefactTypeFileBase::get_new_file_title($title, $data->parent, $data->owner);
        $data->description = $description;
        $data->tags = $tags;
        $artefact_id = ArtefactTypeFile::save_uploaded_file('userfile', $data);
        if ( $artefact_id ) {
            $json['id'] = $artefact_id;
        }
    }
    catch (QuotaExceededException $e) {
        jsonreply( array('fail' => 'Quota exceeded' ) );
    }
    catch (UploadException $e) {
        jsonreply( array('fail' => 'Failed to save file') );
    }
}


// Check for Journal ID to add a post to
$blog = ''; $blogpost = ''; $draft = 0; $allowcomments = 1;
157
try {
158
    $blog = param_integer('blog');
159
160
161
162
}
catch (ParameterException $e) { }

try {
163
    $blogpost = param_integer('blogpost');
164
}
165
166
167
168
catch (ParameterException $e) { }

try {
    $draft = param_variable('draft');
169
}
170
171
172
173
catch (ParameterException $e) { }

try {
    $allowcomments = param_variable('allowcomments');
174
175
}

176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
catch (ParameterException $e) { }
// Check to see if we're creating a journal entry
if ( $blog && $title && $description ) {
    if (!get_record('artefact', 'id', $blog, 'owner', $USER->get('id'))) {
        // Blog security is also checked closer to when blogs are added, this 
        // check ensures that malicious users do not even see the screen for 
        // adding a post to a blog that is not theirs
        throw new AccessDeniedException(get_string('youarenottheownerofthisblog', 'artefact.blog'));
    }
    $postobj = new ArtefactTypeBlogPost($blogpost, null);
    $postobj->set('title', $title);
    $postobj->set('description', $description);
    $postobj->set('tags', $tags);
    $postobj->set('published', !$draft);
    $postobj->set('allowcomments', (int) $allowcomments);
    $postobj->set('parent', $blog);
    $postobj->set('owner', $USER->id);
    $postobj->commit();

    // If we created an artefact - attach it.
    if ( $artefact_id ) {
        $postobj->attach($artefact_id);
    }
}

// Here we need to create a new hash - update our own store of it and return it to the handset
jsonreply( array('success' => $USER->refresh_mobileuploadtoken($token) ) );
203

204
205
206
207
function jsonreply( $arr ) {
  global $json;
  if ( $json )
    $arr['sync'] = $json;
208
  header('Content-Type: application/json');
209
  echo json_encode($arr);
210
211
  exit;
}
212