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