-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.php
303 lines (267 loc) · 12.7 KB
/
index.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
//Define db connection settings
define('CACKLE_DB_LOCALHOST', "localhost");
define('CACKLE_DB_USER', "root");
define('CACKLE_DB_PASSWORD', "");
define('CACKLE_DB_NAME', "cackle-php" );
//Define timer
define('CACKLE_TIMER', 60);
//Define Cackle API
define('ACCOUNT_API_KEY', "");
define('SITE_API_KEY', "");
class Cackle{
function Cackle(){
if ($this->time_is_over(CACKLE_TIMER)){
$this->comment_sync(ACCOUNT_API_KEY,SITE_API_KEY);
}
$this->cackle_display_comments();
}
function time_is_over($cron_time){
$sql="select common_value from common where `common_name` = 'last_time'";
$get_last_time = $this->db_connect($sql, "common_value");
$now=time();
$establish_time_sql="insert into `common` (`common_name`,`common_value`) values ('last_time',$now)";
$delete_time_sql="delete from `common` where `common_name` = 'last_time' and `common_value` > 0;";
if ($get_last_time==null){
$this->db_connect($establish_time_sql);
return time();
}
else{
if($get_last_time + $cron_time > $now){
return false;
}
if($get_last_time + $cron_time < $now){
$this->db_connect($delete_time_sql);
$this->db_connect($establish_time_sql);
return $cron_time;
}
}
}
function db_connect($sql,$field_to_return=null){
$link = mysql_connect(CACKLE_DB_LOCALHOST, CACKLE_DB_USER, CACKLE_DB_PASSWORD) or die("Could not connect\n");
mysql_select_db(CACKLE_DB_NAME, $link) or die(mysql_error());
mysql_query('SET NAMES \'UTF8\'');
$r = mysql_query($sql, $link) or die(mysql_error());
if($field_to_return!=null){
$db_resp = mysql_fetch_array($r);
return $db_resp[$field_to_return];
}
$x=0;
if($r==1) return; // no select request
$row=array(); // to be safe with empty select results
while ($res=mysql_fetch_array($r)) {
$row[$x]=$res;
$x++;
}
return $row;
mysql_close($link);
}
function comment_sync($accountApiKey,$siteApiKey,$cackle_last_comment=0){
$get_last_comment = $this->db_connect("select common_value from common where `common_name` = 'last_comment'","common_value");
if ($get_last_comment!=null){
$cackle_last_comment = $get_last_comment;
}
$params1 = "accountApiKey=$accountApiKey&siteApiKey=$siteApiKey&id=$cackle_last_comment";
$host="cackle.me/api/comment/mutable_list?$params1";
function curl($url)
{
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL,$url);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec ($ch);
curl_close($ch);
return $result;
}
$response = curl($host);
$response = $this->cackle_json_decodes($response);
$this->push_comments($response);
}
function cackle_json_decodes($response){
$response_without_jquery = str_replace('jQuery(', '', $response);
$response = str_replace(');', '', $response_without_jquery);
$obj = json_decode($response,true);
return $obj;
}
function insCreate ($table,$arr){
$str="";
$key_str="";
$val_str="";
foreach($arr as $key=>$val){
$key_str .= "`" . $key . "`, ";
$val_str .= "'" . mysql_escape_string($val) . "', ";
}
$str .= "insert into " . "`" . $table . "` " ;
$sql_req=$str . "(" . $key_str . ") values (" . $val_str . "); ";
$sql_req = str_replace(", )",")",$sql_req);
return $sql_req;
}
/**
* Insert each comment to database with parents
*/
function insert_comm($comment,$status){
/*
* Here you can convert $url to your post ID
*/
$url = $comment['channel'];
if ($comment['author']!=null){
$author_name = $comment['author']['name'];
$author_email= $comment['author']['email'];
$author_www = $comment['author']['www'];
$author_avatar = $comment['author']['avatar'];
$author_provider = $comment['author']['provider'];
$author_anonym_name = null;
$anonym_email = null;
}
else{
$author_name = null;
$author_email= null;
$author_www = null;
$author_avatar = null;
$author_provider = null;
$author_anonym_name = $comment['anonym']['name'];
$anonym_email = $comment['anonym']['email'];
}
$get_parent_local_id = null;
$comment_id = $comment['id'];
$comment_modified = $comment['modified'];
if ($comment['parentId']) {
$comment_parent_id = $comment['parentId'];
$sql = "select comment_id from comment where user_agent='Cackle:$comment_parent_id';";
$get_parent_local_id = $this->db_connect($sql, "comment_id"); //get parent comment_id in local db
}
//You should define post_id in $commentdata according you cms engine(ex. maybe your cms have function to return post_id by page's url)
$commentdata = array(
'url' => $url,
'author_name' => $author_name,
'author_email' => $author_email,
'author_www' => $author_www,
'author_avatar' => $author_avatar,
'author_provider' => $author_provider,
'anonym_name' => $author_anonym_name,
'anonym_email' => $anonym_email,
'rating' => $comment['rating'],
'created' => strftime("%Y-%m-%d %H:%M:%S", $comment['created']/1000),
'ip' => $comment['ip'],
'message' =>$comment['message'],
'status' => $status,
'user_agent' => 'Cackle:' . $comment['id'],
'parent' => $get_parent_local_id
);
$this->db_connect($this->insCreate("comment",$commentdata));
$sql_last_comment_delete="delete from `common` where `common_name` = 'last_comment'";
$sql_last_comment_establish="insert into `common` set `common_name` = 'last_comment'";
$sql_last_comment="update `common` set `common_name` = 'last_comment', `common_value` = $comment_id where `common_name` = 'last_comment';";
$this->db_connect($sql_last_comment_delete);
$this->db_connect($sql_last_comment_establish);
$this->db_connect($sql_last_comment);
$get_last_modified = $this->db_connect("select common_value from common where `common_name` = 'last_modified'","common_value");
if ($comment['modified'] > $get_last_modified) {
$sql_last_modified_delete="delete from `common` where `common_name` = 'last_modified'";
$sql_last_modified_establish="insert into `common` set `common_name` = 'last_modified'";
$sql_last_modified="update `common` set `common_name` = 'last_modified', `common_value` = $comment_modified where `common_name` = 'last_modified';";
$this->db_connect($sql_last_modified_delete);
$this->db_connect($sql_last_modified_establish);
$this->db_connect($sql_last_modified);
}
}
function comment_status_decoder($comment) {
$status;
if (strtolower($comment['status']) == "approved") {
$status = 1;
}
elseif (strtolower($comment['status'] == "pending") || strtolower($comment['status']) == "rejected") {
$status = 0;
}
elseif (strtolower($comment['status']) == "spam") {
$status = 0;
}
elseif (strtolower($comment['status']) == "deleted") {
$status = 0;
}
return $status;
}
function update_comment_status($comment_id, $status, $modified, $comment_content) {
$sql_status="update `comment` set `status` = $status, `message` = '$comment_content' where `user_agent` = 'Cackle:$comment_id';";
$this->db_connect($sql_status);
$sql_last_modified_delete="delete from `common` where `common_name` = 'last_modified'";
$sql_last_modified_establish="insert into `common` set `common_name` = 'last_modified'";
$sql_last_modified="update `common` set `common_name` = 'last_modified', `common_value` = $modified where `common_name` = 'last_modified';";
$this->db_connect($sql_last_modified_delete);
$this->db_connect($sql_last_modified_establish);
$this->db_connect($sql_last_modified);
}
function push_comments ($response){
$obj = $response['comments'];
if ($obj) {
foreach ($obj as $comment) {
$get_last_comment = $this->db_connect("select common_value from common where `common_name` = 'last_comment'","common_value");
$get_last_modified = $this->db_connect("select common_value from common where `common_name` = 'last_modified'","common_value");
if ($comment['id'] > $get_last_comment) {
$this->insert_comm($comment, $this->comment_status_decoder($comment));
} else {
if (!$get_last_modified){
$get_last_modified == 0;
}
if ($comment['modified'] > $get_last_modified) {
$this->update_comment_status($comment['id'], $this->comment_status_decoder($comment), $comment['modified'], $comment['message'] );
}
}
}
}
}
function cackle_comment( $comment) {
?><li id="cackle-comment-<?php echo $comment['comment_id']; ?>">
<div id="cackle-comment-header-<?php echo $comment['comment_id']; ?>" class="cackle-comment-header">
<cite id="cackle-cite-<?php echo $comment['comment_id']; ?>">
<?php if($comment['author_name']) : ?>
<a id="cackle-author-user-<?php echo $comment['comment_id']; ?>" href="<?php echo $comment['author_www']; ?>" target="_blank" rel="nofollow"><?php echo $comment['author_name']; ?></a>
<?php else : ?>
<span id="cackle-author-user-<?php echo $comment['comment_id']; ?>"><?php echo $comment['anonym_name']; ?></span>
<?php endif; ?>
</cite>
</div>
<div id="cackle-comment-body-<?php echo $comment['comment_id']; ?>" class="cackle-comment-body">
<div id="cackle-comment-message-<?php echo $comment['comment_id']; ?>" class="cackle-comment-message">
<?php echo $comment['message']; ?>
</div>
</div>
</li><?php }
function cackle_display_comments(){ ?>
<div id="mc-container">
<div id="mc-content">
<ul id="cackle-comments">
<?php $this->list_comments(); ?>
</ul>
</div>
</div>
<script type="text/javascript">
var mcSite = '<?php echo $api_id //from cackle's admin panel?>';
var mcChannel = '<?php echo $post_id?>';
document.getElementById('mc-container').innerHTML = '';
(function() {
var mc = document.createElement('script');
mc.type = 'text/javascript';
mc.async = true;
mc.src = 'http://cackle.me/mc.widget-min.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(mc);
})();
</script>
<?php }
function get_local_comments(){
//getting all comments for special post_id from database.
//$post_id = 1; //ex.
$get_all_comments = $this->db_connect("select * from `comment` where `url` = $post_id and `status` = 1;");
return $get_all_comments;
}
function list_comments(){
$obj = $this->get_local_comments();
foreach ($obj as $comment) {
$this->cackle_comment($comment);
}
}
}
$a = new Cackle();
?>