sync.php 3.99 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
<?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, artefact-blog
 * @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');
safe_require('artefact', 'blog');

$json = array();

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

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

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

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

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

$USER = new User();

try {
    $USER->find_by_mobileuploadtoken($token, $username);
}
catch (AuthUnknownUserException $e) {
68
    jsonreply(array('fail' => 'Invalid user token'));
69
70
71
72
73
74
75
76
77
78
79
}

// error_log(var_dump($USER));

// Add in bits of sync data - let's start with notifications
$lastsync = time();
try {
    $lastsync = param_variable('lastsync') + 0;
}
catch (ParameterException $e) { }

80
$activity_arr = get_records_sql_array("select n.id, n.subject, n.message 
81
					from {notification_internal_activity} n, {activity_type} a
82
83
84
					where n.type=a.id and n.read=0 
                                          and FLOOR(EXTRACT(EPOCH FROM ctime AT TIME ZONE 'UTC')) >= ? 
					  and n.usr= ? ", 
85
					array($lastsync + 0, $USER->id));
86
if ( count($activity_arr) > 0 ) 
87
  $json['activity'] = $activity_arr;
88
89
90

// OK - let's add tags

91
$tags_arr = array();
92
93
94

$tagsort = param_alpha('ts', null) != 'freq' ? 'alpha' : 'freq';

95
96
97
98
99
100
foreach (get_my_tags(null, false, $tagsort) as $tag) {
    $tags_arr[] = array("id" => $tag->tag, "tag" => $tag->tag);
}

if ( count($tags_arr) > 0 ) 
  $json['tags'] = $tags_arr;
101
102

// OK - let's add journals
103
$blogs_arr = array();
104
105
106
107
108
109
110
111

$blogs = (object) array(
    'offset' => param_integer('offset', 0),
    'limit'  => param_integer('limit', 10),
);

list($blogs->count, $blogs->data) = ArtefactTypeBlog::get_blog_list($blogs->limit, $blogs->offset);

112
113
114
115
116
foreach ($blogs->data as $blog) {
    if ( ! $blog->locked ) {
        $blogs_arr[] = array("id" => $blog->id, "blog" => $blog->title);
    }
}
117

118
119
if ( count($blogs_arr) > 0 ) 
  $json['blogs'] = $blogs_arr;
120
121
122

// OK - let's add folders

123
$folders_arr = array();
124
125
$folders = ArtefactTypeFile::get_my_files_data(0, $USER->id, null, null, array("artefacttype" => array("folder")));

126
127
128
129
130
131
132
foreach ($folders as $folder) {
    if ( ! $folder->locked ) {
        $folders_arr[] = array("id" => $folder->id, "folder" => $folder->title);
    }
}
if ( count($folders_arr) > 0 ) 
  $json['folders'] = $folders_arr;
133

134
135
// 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) ));
136

137
function jsonreply( $arr ) {
138
139
140
141
142
143
144
145
  global $json;
  if ( $json ) 
    $arr['sync'] = $json;
  header('Content-Type: application/json');
  echo json_encode($arr);
  exit;
}