aboutsummaryrefslogtreecommitdiffstats
path: root/mod/rate.php
blob: cc12dcac944a746ae239a6e4ce63c7f8ee53b7d6 (plain) (blame)
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
<?php


function rate_init(&$a) {

	if(! local_channel())
		return;

	$channel = $a->get_channel();

	$target = $_REQUEST['target'];
	if(! $target)
		return;

	$a->data['target'] = $target;

	if($target) {
		$r = q("SELECT * FROM xchan where xchan_hash like '%s' LIMIT 1",
			dbesc($target)
		);
		if($r) {
			$a->poi = $r[0];
		}
	}


	return;

}


function rate_post(&$a) {

	if(! local_channel())
		return;

	if(! $a->data['target'])
		return;

	if(! $_REQUEST['execute'])
		return;

	$channel = $a->get_channel();

	$rating = intval($_POST['rating']);
	if($rating < (-10))
		$rating = (-10);
	if($rating > 10)
		$rating = 10;

	$rating_text = trim(escape_tags($_REQUEST['rating_text']));

	$signed = $a->data['target'] . '.' . $rating . '.' . $rating_text;

	$sig = base64url_encode(rsa_sign($signed,$channel['channel_prvkey']));

	$z = q("select * from xlink where xlink_xchan = '%s' and xlink_link = '%s' and xlink_static = 1 limit 1",
		dbesc($channel['channel_hash']),
		dbesc($a->data['target'])
	);

	if($z) {
		$record = $z[0]['xlink_id'];
		$w = q("update xlink set xlink_rating = '%d', xlink_rating_text = '%s', xlink_sig = '%s', xlink_updated = '%s'
			where xlink_id = %d",
			intval($rating),
			dbesc($rating_text),
			dbesc($sig),
			dbesc(datetime_convert()),
			intval($record)
		);
	}
	else {
		$w = q("insert into xlink ( xlink_xchan, xlink_link, xlink_rating, xlink_rating_text, xlink_sig, xlink_updated, xlink_static ) values ( '%s', '%s', %d, '%s', '%s', '%s', 1 ) ",
			dbesc($channel['channel_hash']),
			dbesc($a->data['target']),
			intval($rating),
			dbesc($rating_text),
			dbesc($sig),
			dbesc(datetime_convert())
		);
		$z = q("select * from xlink where xlink_xchan = '%s' and xlink_link = '%s' and xlink_static = 1 limit 1",
			dbesc($channel['channel_hash']),
			dbesc($a->data['target'])
		);
		if($z)
			$record = $z[0]['xlink_id'];
	}

	if($record) {
		proc_run('php','include/ratenotif.php','rating',$record);
	}

}



function rate_content(&$a) {

	if(! local_channel()) {
		notice( t('Permission denied.') . EOL);
		return;
	}

//	if(! $a->data['target']) {
//		notice( t('No recipients.') . EOL);
//		return;
//	}

	$poco_rating = get_config('system','poco_rating_enable');
	if((! $poco_rating) && ($poco_rating !== false)) {
		notice('Ratings are disabled on this site.');
		return;
	}

	$channel = $a->get_channel();

	$r = q("select * from xlink where xlink_xchan = '%s' and xlink_link = '%s' and xlink_static = 1",
		dbesc($channel['channel_hash']),
		dbesc($a->data['target'])
	);
	if($r) {
		$a->data['xlink'] = $r[0];				
		$rating_val = $r[0]['xlink_rating'];
		$rating_text = $r[0]['xlink_rating_text'];
	}
	else {
		$rating_val = 0;
		$rating_text = '';
	}

	// if unset default to enabled
	if($poco_rating === false)
		$poco_rating = true;

	if($poco_rating) {
		$rating = replace_macros(get_markup_template('rating_slider.tpl'),array(
			'$min' => -10,
			'$val' => $rating_val
		));
	}
	else {
		$rating = false;
	}

	$o = replace_macros(get_markup_template('rating_form.tpl'),array(
		'$header' => t('Rating'),
		'target' => $a->data['target'],
		'$tgt_name' => (($a->poi && $a->poi['xchan_name']) ? $a->poi['xchan_name'] : sprintf( t('Remote Channel [%s] (not yet known on this site)'), substr($a->data['target'],0,16))),
		'$lbl_rating'     => t('Rating (this information is public)'),
		'$lbl_rating_txt' => t('Optionally explain your rating (this information is public)'),
		'$rating_txt'     => $rating_text,
		'$rating'         => $rating,
		'$rating_val'     => $rating_val,
		'$slide'          => $slide,
		'$submit' => t('Submit')
	));

	return $o;

}