function edit() { authenticate(1); global $path; global $template; $answerid = sanitize($path[2], "int"); $basePath = basePath(); $basePathNS = basePathNS(); $js = <<<EOD <script src="{$basePathNS}/js/showdown.js"></script> <script src="{$basePathNS}/js/wmd.js"></script> <link href="{$basePathNS}/css/wmd.css" type="text/css" rel="stylesheet" /> EOD; $template->set('js', $js); $sql = "select * from answers where id = '" . escape($answerid) . "'"; $query = mysql_query($sql); $result = mysql_fetch_array($query); $template->set('description', $result['description']); $template->set('answerid', $result['id']); }
echo basePathNS(); ?> /js/tagscomplete.js"></script> <script src="<?php echo basePathNS(); ?> /js/fancyalert.js"></script> <script type="text/javascript" src="<?php echo basePathNS(); ?> /js/prettify/prettify.js"></script> <link href="<?php echo basePathNS(); ?> /css/prettify.css" type="text/css" rel="stylesheet" /> <?php if (!empty($js)) { echo $js; } ?> </head> <body onload="prettyPrint()"> <div id="navigation"><div class="navcenter"> <form action="<?php echo basePath();
function view() { global $path; global $template; $questionid = sanitize($path[2], "int"); $sql = "select * from questions where id = '" . escape($questionid) . "'"; $query = mysql_query($sql); $result = mysql_fetch_array($query); $template->set('id', $result['id']); $template->set('userid', $result['userid']); $template->set('title', $result['title']); $template->set('created', $result['created']); $template->set('description', Markdown($result['description'])); $template->set('kb', $result['kb']); $template->set('link', $result['link']); $cache = 0; if (!empty($result['linkcache'])) { $cache = 1; } $template->set('cache', $cache); $sql = "select tag from tags_questions, tags where questionid = '" . escape($questionid) . "' and tags.id = tags_questions.tagid order by tag"; $query = mysql_query($sql); $tags = array(); while ($result = mysql_fetch_array($query)) { $tags[] = $result['tag']; } $template->set('tags', $tags); $sql = "select * from favorites where questionid = '" . escape($questionid) . "' and userid = '" . escape($_SESSION['userid']) . "'"; $query = mysql_query($sql); $result = mysql_fetch_array($query); $fave = 0; if ($result['id'] > 0) { $fave = 1; } $template->set('fave', $fave); $sql = "select sum(vote) count from questions_votes where questionid = '" . escape($questionid) . "'"; $query = mysql_query($sql); $result = mysql_fetch_array($query); $votes = $result['count']; if ($votes == '') { $votes = 0; } $template->set('votes', $votes); $sql = "select vote from questions_votes where questionid = '" . escape($questionid) . "' and userid = '" . escape($_SESSION['userid']) . "'"; $query = mysql_query($sql); $result = mysql_fetch_array($query); $nvote = 0; $pvote = 0; if ($result['vote'] == -1) { $nvote = 1; } if ($result['vote'] == 1) { $pvote = 1; } $template->set('nvote', $nvote); $template->set('pvote', $pvote); $sql = "select comments.id,comment,comments.userid,users.name username, comments_votes.id voted, comments.votes from comments left join users on comments.userid = users.id left join comments_votes on (comments_votes.commentid = comments.id and comments_votes.userid = '" . escape($_SESSION['userid']) . "') where type = '0' and typeid = '" . escape($questionid) . "' order by comments.created asc"; $query = mysql_query($sql); $comments = array(); while ($result = mysql_fetch_array($query)) { $pos = strpos($result['username'], ' '); if ($pos > 0) { $result['username'] = substr($result['username'], 0, $pos); } $comments[] = array("id" => $result['id'], "comment" => $result['comment'], "userid" => $result['userid'], "username" => $result['username'], "voted" => $result['voted'], "votes" => $result['votes']); } $template->set('comments', $comments); $sql = "select count(id) count from answers where questionid = '" . escape($questionid) . "'"; $query = mysql_query($sql); $result = mysql_fetch_array($query); $template->set('answerscount', $result['count']); $order = "votes desc"; $orderby = "votes"; $page = 1; if (!empty($_GET['order'])) { if ($_GET['order'] == "newest") { $order = "created desc"; $orderby = "newest"; } else { if ($_GET['order'] == "oldest") { $order = "created asc"; $orderby = "oldest"; } } } if (!empty($_GET['page'])) { $page = $_GET['page']; } $offset = ($page - 1) * ANSWERS_PER_PAGE; $paging = new Pagination(); $paging->set('urlscheme', '?order=' . $orderby . '&page=%page%'); $paging->set('perpage', ANSWERS_PER_PAGE); $paging->set('page', $page); $paging->set('total', $result['count']); $paging->set('order', $orderby); $template->set('pagination', $paging->display()); $paging->set('urlscheme', '?order=%label%&page=1'); $template->set('orderOptions', $paging->displayOptions()); $sqlanswer = ''; if ($page == 1) { $sqlanswer = "(select answers.*,users.name username from answers,users where questionid = '" . escape($questionid) . "' and answers.userid = users.id and answers.accepted = '1') UNION "; } $sql = "{$sqlanswer} (select answers.*,users.name username from answers,users where questionid = '" . escape($questionid) . "' and answers.userid = users.id and answers.accepted = '0' order by {$order}, created desc LIMIT " . ANSWERS_PER_PAGE . " OFFSET {$offset})"; $query = mysql_query($sql); $answers = array(); while ($result = mysql_fetch_array($query)) { $sql_nest = "select sum(vote) count from answers_votes where answerid = '" . escape($result['id']) . "'"; $query_nest = mysql_query($sql_nest); $result_nest = mysql_fetch_array($query_nest); $votes = $result_nest['count']; if ($votes == '') { $votes = 0; } $sql_nest = "select vote from answers_votes where answerid = '" . escape($result['id']) . "' and userid = '" . escape($_SESSION['userid']) . "'"; $query_nest = mysql_query($sql_nest); $result_nest = mysql_fetch_array($query_nest); $nvote = 0; $pvote = 0; if ($result_nest['vote'] == -1) { $nvote = 1; } if ($result_nest['vote'] == 1) { $pvote = 1; } $sql_nest = "select comments.id,comment,comments.userid,users.name username, comments_votes.id voted, comments.votes from comments left join users on comments.userid = users.id left join comments_votes on (comments_votes.commentid = comments.id and comments_votes.userid = '" . escape($_SESSION['userid']) . "') where type = '1' and typeid = '" . escape($result['id']) . "' order by comments.created asc"; $query_nest = mysql_query($sql_nest); $comments = array(); while ($result_nest = mysql_fetch_array($query_nest)) { $pos = strpos($result['username'], ' '); if ($pos > 0) { $result['username'] = substr($result['username'], 0, $pos); } $comments[] = array("id" => $result_nest['id'], "comment" => $result_nest['comment'], "userid" => $result_nest['userid'], "username" => $result['username'], "voted" => $result_nest['voted'], "votes" => $result_nest['votes']); } $answers[] = array("description" => Markdown($result['description']), "created" => $result['created'], "updated" => $result['updated'], "userid" => $result['userid'], "username" => $result['username'], "pvote" => $pvote, "nvote" => $nvote, "votes" => $votes, "id" => $result['id'], "accepted" => $result['accepted'], "comments" => $comments); } $template->set('answers', $answers); $basePathNS = basePathNS(); $js = ''; if ($_SESSION['userid'] != '') { $js = <<<EOD \t\t<script src="{$basePathNS}/js/showdown.js"></script> \t\t<script src="{$basePathNS}/js/wmd.js"></script> \t\t<link href="{$basePathNS}/css/wmd.css" type="text/css" rel="stylesheet" /> EOD; } $js .= <<<EOD <script> \tvar basePath = "index.php"; \tfunction vote(elem,type,voted) { \t\t\$this = \$(elem); \t\tvar id = \$this.parent().parent().parent().attr('id'); \t\tvar add = 1; \t\tvar minus = -1; \t\tvar up = 'up'; \t\tvar down = 'down'; \t\tif (voted == 'minus') { add = -1; minus = 1; up = 'down'; down = 'up'; } \t\t\$.post(basePath+"/"+type+"/vote", { id: id, vote: voted }, \t\t\tfunction(data) { \t\t\t\tvar result = data.substr(0,1); \t\t\t\tvar message = data.substr(1); \t\t\t\tif (result == 1) { \t\t\t\t\tif (!\$("#"+id+" .questionsview_"+up+"").hasClass("voteselected")) { \t\t\t\t \t\t\t\t\t\t\$("#"+id+" .questionsview_vote").html(parseInt(\$("#"+id+" .questionsview_vote").html())+add); \t\t\t\t\t\t \t\t\t\t\t\tif (\$("#"+id+" .questionsview_"+down+"").hasClass("voteselected")) { \t\t\t\t\t\t\t\$("#"+id+" .questionsview_vote").html(parseInt(\$("#"+id+" .questionsview_vote").html())+add); \t\t\t\t\t\t\t\$("#"+id+" .questionsview_"+down+"").removeClass("voteselected"); \t\t\t\t\t\t} \t\t\t\t\t} else { \t\t\t\t\t\t\$("#"+id+" .questionsview_vote").html(parseInt(\$("#"+id+" .questionsview_vote").html())+minus); \t\t\t\t\t} \t\t\t\t\t\$this.toggleClass("voteselected"); \t\t\t\t} \t\t\t\t \t\t\t\tif (message != '') { \t\t\t\t\t\$.fancyalert(message); \t\t\t\t} \t\t}); \t} \t\$(document).ready(function() { \t\t\$(".questionsview_answer .questionsview_up").click(function() { \t\t\tvote(this,'answers','plus'); \t\t}); \t\t\$(".questionsview_answer .questionsview_down").click(function() { \t\t\tvote(this,'answers','minus'); \t\t}); \t\t\$(".questionsview_question .questionsview_up").click(function() { \t\t\tvote(this,'questions','plus'); \t\t}); \t\t\$(".questionsview_question .questionsview_down").click(function() { \t\t\tvote(this,'questions','minus'); \t\t}); \t\t\$(".questionsview_question .questionsview_fave").click(function() { \t\t\t\$this = \$(this); \t\t\tvar id = \$this.parent().parent().parent().attr('id'); \t\t\t\$.post(basePath+"/questions/fave", { id: id }, \t\t\t\tfunction(data) { \t\t\t\t\tvar result = data.substr(0,1); \t\t\t\t\tvar message = data.substr(1); \t\t\t\t\tif (result == 1) { \t\t\t\t\t\t\$this.toggleClass("voteselected");\t\t\t\t \t\t\t\t\t} \t\t\t\t\t\$.fancyalert(message); \t\t\t\t\t \t\t\t}); \t\t}); \t\$(".commentfave").click(function() { \t\tvar id = \$(this).attr('id'); \t\t\$this = \$(this); \t\t\$.post(basePath+"/comments/vote", { id: id }, \t\t\tfunction(data) { \t\t\t\tvar result = data.substr(0,1); \t\t\t\tvar message = data.substr(1); \t\t\t\tif (result == "1") { \t\t\t\t\tif (\$this.next('div').html() == '') { \t\t\t\t\t\t\$this.next('div').html('0'); \t\t\t\t\t} \t\t\t\t\tif (!\$this.hasClass("voteselected")) { \t\t\t\t\t\t\$this.next('div').html(parseInt(\$this.next('div').html())+1); \t\t\t\t\t} else { \t\t\t\t\t\t\$this.next('div').html(parseInt(\$this.next('div').html())-1); \t\t\t\t\t} \t\t\t\t\t\$this.toggleClass("voteselected"); \t\t\t\t} \t\t\t\tif (message != '') { \t\t\t\t\t\$.fancyalert(message); \t\t\t\t} \t\t\t \t\t}); \t}); \t\t\$(".commentdel").click(function() { \t\tvar answer = confirm(" EOD; $js .= _("Delete this comment?"); $js .= <<<EOD ") \t\tif (answer){ \t\t\tvar id = \$(this).attr('id'); \t\t\t\$this = \$(this); \t\t\t\$.post(basePath+"/comments/del", { id: id }, \t\t\t\tfunction(data) { \t\t\t\tvar result = data.substr(0,1); \t\t\t\tvar message = data.substr(1); \t\t\t\tif (result == "1") { \t\t\t\t\t\$this.parent().fadeOut(2000); \t\t\t\t} \t\t\t\tif (message != '') { \t\t\t\t\t\$.fancyalert(message); \t\t\t\t} \t\t\t\t\t\t \t\t\t}); \t\t} \t\t \t}); \tvar comments = \$(".comments"); \t\$.each(comments, function() { \t var elements = ( \$('.comment:gt(4)',\$(this)).size()); \t\tif (elements > 0) {\t\t \t\t\t\$('.viewallcomments',\$(this)).css('display','block'); \t\t\t\$('.viewallcomments a',\$(this)).html(' EOD; $js .= _("View all comments"); $js .= <<<EOD ('+elements+' EOD; $js .= _("more"); $js .= <<<EOD )'); \t\t} \t\tallComments = \$(".comment",\$(this)).get(); \t\tallComments.sort(function(a,b) { \t\t\ta = \$(".commentfavevotes",a).html(); \t\t\tb = \$(".commentfavevotes",b).html();; \t\t\t \t\t\tif (a == '') a = 0; \t\t\tif (b == '') b = 0; \t\t\tif (a > b) { \t\t\t\treturn -1; \t\t\t} else if (a < b) { \t\t\t\treturn 1; \t\t\t} else { \t\t\t\treturn 0; \t\t\t} \t\t}); \t\t\$(allComments.slice(5)).hide(); \t}); }); function comment(id) { \t\$("#comment_"+id).html("<textarea class=\\"commenttextarea\\" id='commenttext_"+id+"'></textarea><input class=\\"smallbutton\\" type=\\"submit\\" value=\\" EOD; $js .= _("Add Comment"); $js .= <<<EOD \\" onclick=\\"addcomment('"+id+"')\\"/>"); } function addcomment(id) { \t\tvar comment = \$("#commenttext_"+id).val(); \t\tif (comment.length < 10) { \t\t\t\$.fancyalert(' EOD; $js .= _("Your comment must be atleast 10 characters in length"); $js .= <<<EOD '); \t\t\treturn; \t\t} \t\tif (comment.length > 600) { \t\t\t\$.fancyalert(' EOD; $js .= _("Your comment is too long, please reduce it to 600 characters"); $js .= <<<EOD '); \t\t\treturn; \t\t} \t\t\$("#commenttext_"+id).val(''); \t\t \t\t\$.post(basePath+"/comments/post", { id: id, comment: comment }, \t\t\tfunction(data) { \t\t\t\tif (data == 0) { \t\t\t\t\t\$("#commenttext_"+id).val(comment); \t\t\t\t\t\$.fancyalert(' EOD; $js .= _("Please login to post a comment"); $js .= <<<EOD ');\t\t\t\t\t \t\t\t\t} else { \t\t\t\t\t\$("#comments_"+id).append(data); \t\t\t\t} \t\t}); } function viewallcomments(id){ \t\$('#comments_'+id+' .comment').fadeIn(1000); \t\$('#comment_'+id+' .viewallcomments').css('display','none'); } </script> EOD; $template->set('js', $js); }