1 | /*global $, dotclear */ |
---|
2 | 'use strict'; |
---|
3 | |
---|
4 | dotclear.getEntryContent = function(postId, callback, options) { |
---|
5 | var res = ''; |
---|
6 | var opt = $.extend({ |
---|
7 | // Entry type (default: post) |
---|
8 | type: '', |
---|
9 | // Alert on error |
---|
10 | alert: true, |
---|
11 | // Clean content (useful for potential XSS in spam) |
---|
12 | clean: false, |
---|
13 | // Cut content after length chars (-1 to not cut) |
---|
14 | length: -1, |
---|
15 | }, options); |
---|
16 | |
---|
17 | // Check callback fn() |
---|
18 | if (!$.isFunction(callback)) { |
---|
19 | return; |
---|
20 | } |
---|
21 | |
---|
22 | // Get entry content |
---|
23 | $.get('services.php', { |
---|
24 | f: 'getPostById', |
---|
25 | id: postId, |
---|
26 | post_type: opt.type, |
---|
27 | xd_check: dotclear.nonce |
---|
28 | }) |
---|
29 | .done(function(data) { |
---|
30 | // Response received |
---|
31 | var rsp = $(data).children('rsp')[0]; |
---|
32 | if (rsp.attributes[0].value == 'ok') { |
---|
33 | var excerpt = $(rsp).find('post_display_excerpt').text(); |
---|
34 | var content = $(rsp).find('post_display_content').text(); |
---|
35 | if (excerpt || content) { |
---|
36 | // Clean content if requested |
---|
37 | if (opt.clean) { |
---|
38 | var text = document.createElement('textarea'); |
---|
39 | if (excerpt) { |
---|
40 | text.textContent = excerpt; |
---|
41 | excerpt = text.innerHTML; |
---|
42 | } |
---|
43 | if (content) { |
---|
44 | text.textContent = content; |
---|
45 | content = text.innerHTML; |
---|
46 | } |
---|
47 | } |
---|
48 | // Compose full content |
---|
49 | if (!opt.clean) { |
---|
50 | content = (excerpt ? `${excerpt}<hr />` : '') + content; |
---|
51 | } |
---|
52 | // Cut content if requested |
---|
53 | if (opt.length > -1) { |
---|
54 | content = content.substr(0, opt.length); |
---|
55 | } |
---|
56 | if (opt.clean && content) { |
---|
57 | content = `<pre>${content}</pre>`; |
---|
58 | } |
---|
59 | res = content; |
---|
60 | } |
---|
61 | } else { |
---|
62 | if (opt.alert) { |
---|
63 | window.alert($(rsp).find('message').text()); |
---|
64 | } |
---|
65 | } |
---|
66 | }) |
---|
67 | .fail(function(jqXHR, textStatus, errorThrown) { |
---|
68 | // No response |
---|
69 | window.console.log(`AJAX ${textStatus} (status: ${jqXHR.status} ${errorThrown})`); |
---|
70 | if (opt.alert) { |
---|
71 | window.alert('Server error'); |
---|
72 | } |
---|
73 | }) |
---|
74 | .always(function() { |
---|
75 | // Finally |
---|
76 | callback(res); |
---|
77 | }); |
---|
78 | }; |
---|
79 | |
---|
80 | dotclear.getCommentContent = function(commentId, callback, options) { |
---|
81 | var res = ''; |
---|
82 | var opt = $.extend({ |
---|
83 | // Get comment metadata (email, site, …) |
---|
84 | metadata: true, |
---|
85 | // Show IP in metadata |
---|
86 | ip: true, |
---|
87 | // Alert on error |
---|
88 | alert: true, |
---|
89 | // Clean content (useful for potential XSS in spam) |
---|
90 | clean: false, |
---|
91 | // Cut content after length chars (-1 to not cut) |
---|
92 | length: -1, |
---|
93 | }, options); |
---|
94 | |
---|
95 | // Check callback fn() |
---|
96 | if (!$.isFunction(callback)) { |
---|
97 | return; |
---|
98 | } |
---|
99 | |
---|
100 | // Get comment content |
---|
101 | $.get('services.php', { |
---|
102 | f: 'getCommentById', |
---|
103 | id: commentId, |
---|
104 | xd_check: dotclear.nonce |
---|
105 | }) |
---|
106 | .done(function(data) { |
---|
107 | // Response received |
---|
108 | var rsp = $(data).children('rsp')[0]; |
---|
109 | if (rsp.attributes[0].value == 'ok') { |
---|
110 | var content = $(rsp).find('comment_display_content').text(); |
---|
111 | if (content) { |
---|
112 | // Clean content if requested |
---|
113 | if (opt.clean) { |
---|
114 | var text = document.createElement('textarea'); |
---|
115 | text.textContent = content; |
---|
116 | content = text.innerHTML; |
---|
117 | } |
---|
118 | // Cut content if requested |
---|
119 | if (opt.length > -1) { |
---|
120 | content = content.substr(0, opt.length); |
---|
121 | } |
---|
122 | if (opt.clean && content) { |
---|
123 | content = `<pre>${content}</pre>`; |
---|
124 | } |
---|
125 | // Get metadata (if requested) |
---|
126 | if (opt.metadata) { |
---|
127 | var comment_email = $(rsp).find('comment_email').text(); |
---|
128 | var comment_site = $(rsp).find('comment_site').text(); |
---|
129 | var comment_ip = $(rsp).find('comment_ip').text(); |
---|
130 | var comment_spam_disp = $(rsp).find('comment_spam_disp').text(); |
---|
131 | |
---|
132 | content += `<p> |
---|
133 | <strong>${dotclear.msg.website}</strong> ${comment_site}<br /> |
---|
134 | <strong>${dotclear.msg.email}</strong> ${comment_email}`; |
---|
135 | if (opt.ip) { |
---|
136 | content += `<br /> |
---|
137 | <strong>${dotclear.msg.ip_address}</strong> <a href="comments.php?ip=${comment_ip}">${comment_ip}</a>`; |
---|
138 | } |
---|
139 | content += `</p>${comment_spam_disp}`; |
---|
140 | } |
---|
141 | res = content; |
---|
142 | } |
---|
143 | } else { |
---|
144 | if (opt.alert) { |
---|
145 | window.alert($(rsp).find('message').text()); |
---|
146 | } |
---|
147 | } |
---|
148 | }) |
---|
149 | .fail(function(jqXHR, textStatus, errorThrown) { |
---|
150 | // No response |
---|
151 | window.console.log(`AJAX ${textStatus} (status: ${jqXHR.status} ${errorThrown})`); |
---|
152 | if (opt.alert) { |
---|
153 | window.alert('Server error'); |
---|
154 | } |
---|
155 | }) |
---|
156 | .always(function() { |
---|
157 | // Finally |
---|
158 | callback(res); |
---|
159 | }); |
---|
160 | }; |
---|