2020-05-22 00:20:59 +00:00
< ? php
session_start ();
include ( " ../include/settings.php " );
if ( ! isset ( $_GET [ " q " ])){
echo ( " Question not specified. " );
die ();
}
else if ( ! isset ( $_SESSION [ " uid " ])){
echo ( " You need to log in to perform that task. " );
}
2020-08-16 05:38:24 +00:00
else {
$db = new sqlite3 ( '../ask.db' );
$question = $db -> query ( " SELECT * FROM questions WHERE id = ' " . $_GET [ " q " ] . " '; " ) -> fetchArray ( SQLITE3_ASSOC );
if ( ! $question || ! $question [ " id " ]){
echo ( " Question not found. " );
die ();
2020-05-22 00:20:59 +00:00
}
2020-08-16 05:38:24 +00:00
else if ( $question [ " user " ] != $_SESSION [ " uid " ]){
echo ( " You have no permission to answer this question. " );
die ();
}
if ( isset ( $_POST [ " answered " ])){
if ( $_POST [ " answer_body " ] == " " ){
echo ( " Answer cannot be blank. " );
2020-05-22 00:20:59 +00:00
}
else {
$db -> exec ( " UPDATE questions SET answer = ' " . htmlspecialchars ( $_POST [ " answer_body " ], ENT_QUOTES ) . " ', a_date = " . strtotime ( " now " ) . " WHERE id = " . $_GET [ " q " ] . " ; " );
2020-08-16 09:34:33 +00:00
$question = $db -> query ( " SELECT * FROM questions WHERE id = ' " . $_GET [ " q " ] . " '; " ) -> fetchArray ( SQLITE3_ASSOC );
$user = $db -> query ( " SELECT * FROM users WHERE id = ' " . $_SESSION [ " uid " ] . " '; " ) -> fetchArray ( SQLITE3_ASSOC );
// Writing the tweet text
$separator = " - " ;
$tw_url_length = 23 ;
$max_length = 280 - strlen ( $separator ) - strlen ( " " ) - $tw_url_length ;
$ellipsis = " ... " ;
if ( strlen ( $question [ " question " ]) >= $max_length / 2 - strlen ( $separator )){
if ( strlen ( $question [ " answer " ]) >= $max_length / 2 - strlen ( $separator )){
$tweet = substr ( $question [ " question " ], 0 , $max_length / 2 - strlen ( $ellipsis )) . $ellipsis ;
$tweet = $tweet . $separator . substr ( $question [ " answer " ], 0 , $max_length / 2 - strlen ( $ellipsis )) . $ellipsis ;
}
else {
$tweet = substr ( $question [ " question " ], 0 , $max_length - strlen ( $question [ " answer " ]) - strlen ( $ellipsis )) . $ellipsis ;
$tweet = $tweet . $separator . $question [ " answer " ];
}
}
else {
if ( strlen ( $question [ " answer " ]) >= $max_length / 2 - strlen ( $separator )){
$tweet = $question [ " question " ] . $separator ;
$tweet = $tweet . substr ( $question [ " answer " ], 0 , $max_length - strlen ( $question [ " question " ]) - strlen ( $ellipsis )) . $ellipsis ;
}
else {
$tweet = $question [ " question " ] . $separator . $question [ " answer " ];
}
}
2020-08-16 15:22:31 +00:00
$tweet = $tweet . " https:// " . $_SERVER [ 'HTTP_HOST' ] . " /user/ " . $user [ " username " ] . " ?p= " . $question [ " id " ];
2020-08-16 09:34:33 +00:00
// echo strlen($tweet) . " - " . strlen($question["answer"]) . "<br/><br/>\n";
// echo $tweet;
if ( $user [ " tw_oauth_token " ] && $user [ " tw_oauth_verify " ]){
include ( " ../include/tw-post.php " );
post_tweet ( htmlspecialchars_decode ( $tweet , ENT_QUOTES ));
}
2020-08-16 15:22:31 +00:00
if ( $fancy_urls ){
2020-05-22 00:20:59 +00:00
header ( " Location: /user/ " . $db -> querySingle ( " SELECT username FROM users WHERE id = " . $question [ " user " ] . " ; " ));
die ();
}
else {
header ( " Location: /user.php?q= " . $db -> querySingle ( " SELECT username FROM users WHERE id = " . $question [ " user " ] . " ; " ));
die ();
}
}
}
}
?>
2020-05-31 03:53:02 +00:00
<! DOCTYPE html >
2020-05-22 00:20:59 +00:00
< html >
< head >
2020-05-31 03:53:02 +00:00
< ? php include ( " ../themes/ $theme_name /reply.php " ); ?>
2020-05-22 00:20:59 +00:00
</ head >
< body >
2020-08-16 09:34:33 +00:00
< ? php
?>
2020-05-31 03:53:02 +00:00
< div class = " main-container " >
< h3 class = " question " >< ? = $question [ " question " ] ?> </h3>
2020-05-22 00:20:59 +00:00
< form action = " " method = " post " >
< textarea cols = 100 rows = 10 name = " answer_body " placeholder = " Write your answer. " ></ textarea >< br />
< input type = " submit " name = " answered " />
</ form >
2020-05-31 03:53:02 +00:00
</ div >
2020-05-22 00:20:59 +00:00
</ body >
</ html >