forked from craigk5n/webcalendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
layers_ajax.php
172 lines (149 loc) · 5.23 KB
/
layers_ajax.php
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
114
115
116
117
118
119
120
121
122
123
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/**
* Description
* Handler for AJAX requests from layers.php.
* We use JSON for some of the data we send back to the AJAX request.
* Because JSON support was not built-in to PHP until 5.2, we have our
* own implmentation in includes/JSON.php.
*/
include_once 'includes/translate.php';
require_once 'includes/classes/WebCalendar.class';
$WebCalendar = new WebCalendar( __FILE__ );
include 'includes/config.php';
include 'includes/dbi4php.php';
include 'includes/formvars.php';
include 'includes/functions.php';
require_valid_referring_url ();
$WebCalendar->initializeFirstPhase();
include 'includes/' . $user_inc;
include 'includes/access.php';
include 'includes/validate.php';
include 'includes/JSON.php';
include 'includes/ajax.php';
$WebCalendar->initializeSecondPhase();
load_global_settings();
load_user_preferences();
$WebCalendar->setLanguage();
$action = getValue ( 'action' );
$public = getValue ( 'public' );
$sendPlainText = false;
$format = getValue ( 'format' );
if ( ! empty ( $format ) &&
( $format == 'text' || $format == 'plain' ) );
$sendPlainText = true;
$error = '';
if ( $is_admin && ! empty ( $public ) && $PUBLIC_ACCESS == 'Y' ) {
$updating_public = true;
$layer_user = '__public__';
} else {
$layer_user = $login;
}
if ( $action == 'enable' || $action == 'disable' ) {
// Toggle LAYER_STATUS in the user's preferences between N and Y.
dbi_execute( 'DELETE FROM webcal_user_pref WHERE cal_login = ?
AND cal_setting = "LAYERS_STATUS"', [$layer_user] );
if( ! dbi_execute( 'INSERT INTO webcal_user_pref ( cal_login, cal_setting,
cal_value ) VALUES ( ?, \'LAYERS_STATUS\', ? )',
[$layer_user, ( $action === 'enable' ? 'Y' : 'N' )] ) ) {
ajax_send_error ( translate ( 'Unable to update preference' ) . ': ' . dbi_error() );
} else {
// Success
ajax_send_success();
}
} else if ( $action == 'list' ) {
// Use JSON to encode our list of layers.
load_user_layers ( $layer_user, 1 );
$ret_layers = [];
foreach ( $layers as $layer ) {
user_load_variables ( $layer['cal_layeruser'], 'layer' );
$ret_layers[] = ['id' => $layer['cal_layerid'],
'source' => $layer['cal_layeruser'],
'color' => $layer['cal_color'],
'dups' => $layer['cal_dups'],
'fullname' => $layerfullname];
}
ajax_send_object ( 'layers', $ret_layers, $sendPlainText );
} else if ( $action == 'save' ) {
// TODO: we should do some additional checking here to make
// sure someone isn't asking for a layer they are not authorized to view.
if ( $ALLOW_VIEW_OTHER != 'Y' ) {
$error = print_not_auth (7);
} else {
save_layer ( getPostValue('layeruser'),
getPostValue('source'), getPostValue('color'),
getPostValue('dups') == 'Y' ? 'Y' : 'N',
getPostValue('id') );
}
if ( $error == '' )
ajax_send_success();
else
ajax_send_error ( $error );
} else if ( $action == 'delete' ) {
// TODO: we should so some additional checking here to make
// sure someone isn't asking for a layer they are not authorized to view.
if ( $ALLOW_VIEW_OTHER != 'Y' ) {
$error = print_not_auth (7);
} else {
$id = getPostValue ( 'id' );
if ( $id <= 0 ) {
$error = translate('Invalid entry id.');
} else {
delete_layer ( getPostValue('layeruser'), $id );
}
}
if ( $error == '' )
ajax_send_success();
else
ajax_send_error ( $error );
} else {
ajax_send_error ( translate('Unknown error.') );
}
exit;
function delete_layer ( $user, $id ) {
global $error, $layers;
if ( ! dbi_execute ( 'DELETE FROM webcal_user_layers ' .
' WHERE cal_layerid = ? AND cal_login = ?',
[$id, $user] ) ) {
$error = translate ( "Database error" ) . ": " . dbi_error();
}
}
function save_layer ( $user, $source, $layercolor, $dups, $id ) {
global $error, $layers;
if ( $user == $source )
$error = translate ( 'You cannot create a layer for yourself.' );
load_user_layers ( $user, 1 );
if ( ! empty ( $source ) && $error == '' ) {
// Existing layer entry.
if ( ! empty ( $layers[$id]['cal_layeruser'] ) ) {
// Update existing layer entry for this user.
$layerid = $layers[$id]['cal_layerid'];
dbi_execute ( 'UPDATE webcal_user_layers SET cal_layeruser = ?,
cal_color = ?, cal_dups = ? WHERE cal_layerid = ?',
[$source, $layercolor, $dups, $layerid] );
} else {
// New layer entry.
// Check for existing layer for user. Can only have one layer per user.
$res = dbi_execute ( 'SELECT COUNT( cal_layerid ) FROM webcal_user_layers
WHERE cal_login = ? AND cal_layeruser = ?',
[$user, $source] );
if ( $res ) {
$row = dbi_fetch_row ( $res );
if ( $row[0] > 0 )
$error = translate ( 'You can only create one layer for each user.' );
dbi_free_result ( $res );
}
if ( $error == '' ) {
$res = dbi_execute ( 'SELECT MAX( cal_layerid ) FROM webcal_user_layers' );
if ( $res ) {
$row = dbi_fetch_row ( $res );
$layerid = $row[0] + 1;
} else
$layerid = 1;
dbi_execute ( 'INSERT INTO webcal_user_layers ( cal_layerid, cal_login,
cal_layeruser, cal_color, cal_dups ) VALUES ( ?, ?, ?, ?, ? )',
[$layerid, $user, $source, $layercolor, $dups] );
}
}
}
}
?>