>= 1;
}
}
// process all the accumulators to create an average filter
function encode(&$accumulators, $threshold) {
$code = 0;
for ($i = 0; $i < count($accumulators); ++$i) {
// beware of case when threshold = 0: lets make it default to not classified
if ($accumulators[$i] > $threshold) $code |= (1 << $i);
}
return $code;
}
// average all the filters for this post
function recompute_filter($post_id) {
global $AP_db;
$sql = "SELECT (filter) FROM filter WHERE post_id=?";
$stmt = $AP_db->prepare($sql);
$stmt->execute([$post_id]);
// beware, rowCount doesn't work on some implementations
$total = 0; // $stmt->rowCount();
// if (empty($total)) return 0; // save time on posts that mostly have no entries
$accumulators = [
0,0,0,0
,0,0,0,0
,0,0,0,0
,0,0,0,0
];
while ( accumulate($accumulators, $stmt->fetch(PDO::FETCH_ASSOC)) ){ ++$total; };
//echo("
post $post_id has $total filter rows
");
return encode($accumulators, $total/2);
}
// compute the default filter for this user by averaging all their comment ratings
function average_filter($user_id) {
global $AP_db;
$sql = "SELECT (filter) FROM filter INNER JOIN posts ON filter.post_id=posts.post_ix WHERE posts.user_id=?";
$stmt = $AP_db->prepare($sql);
$stmt->execute([$user_id]);
$rows=$stmt->fetchall(PDO::FETCH_ASSOC); // one at a time just saw one of them IDK why
$total = count($rows);
//echo("
user $user_id has $total filter rows
");
$accumulators = [
0,0,0,0
,0,0,0,0
,0,0,0,0
,0,0,0,0
];
foreach ($rows as &$row) accumulate($accumulators, $row);
return encode($accumulators, $total/4);
}
/* -------------------------- processing ----------------------
*/
// submitting a reply also means this is a new post and the post_id supplied is interpreted as reply_id
// whenever I ask for a save_submit I am editing a post of my own
if (array_key_exists('reply_submit',$_POST)) {
$reply_id = $post_id;
$post_id = NULL;
$user_id = $_SESSION['user_id'];
$nickname = $_SESSION['nickname'];
$md5_email = $_SESSION['md5_email'];
}
elseif (array_key_exists('new_submit',$_POST)) {
$user_id = $_SESSION['user_id'];
$nickname = $_SESSION['nickname'];
$md5_email = $_SESSION['md5_email'];
}
//echo "
post data = user=$user_id, post_id=$post_id, reply_id=$reply_id
";
// if an existing post is identified then fetch any filter I may have given it
// this happens BEFORE potentially updating it so I can skip all the recalculating average rating when it didn't change
// and if it did change then I simply set the new value there
$filter = 0; // average_filter($_SESSION['user_id']); // default to my average filter value
// this however excludes all the unflagged ones and we might want it at user's discretion
if (!empty($post_id) ) {
$sql = "SELECT filter FROM filter WHERE post_id=? AND user_id=?"; // my id (not the owner of the post)
$stmt = $AP_db->prepare($sql);
$stmt->execute([$post_id, $_SESSION['user_id']]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if (!empty($row)) $filter = $row['filter']; // the database gives a number
//echo sprintf("
filter from database=%o (octal) for post=$post_id
", $filter);
}
/* -------------------------------------------------------------
only for moderators
*/
if ($_SESSION['user_id'] <= modlevel) {
//echo "
doing modlevel
";
if (array_key_exists('approve_submit', $_POST)) {
//echo "
approve_submit, post_id=$post_id
";
$sql = "INSERT INTO ratings (post_ix, moderator) VALUES (?, ?) ON DUPLICATE KEY UPDATE moderator=VALUES(moderator)";
$stmt = $AP_db->prepare($sql);
$stmt->execute([$post_id, 'approved']);
// reenable the file
$path = sprintf("$CFG_http_root/my/h%05d/%08d.htm",$user_id, $post_id);
$result = chmod($path, 0744);
//echo "
chmod($path,0744)=$result
";
}
include_once optional('/delete.i'); // include delete processing
}
/* ------------------------- skip rest of processing if this isn't a save_submit -----------------------
*/
if (array_key_exists('save_submit', $_POST)) {
function create_page_entry() {
global $AP_db;
// reverse look up which page it's on from host and path
$sql = "SELECT page_ix FROM url WHERE host=? AND path=?";
$stmt = $AP_db->prepare($sql);
$stmt->execute([$_SESSION['host'], $_SESSION['path']]);
$row = $stmt->fetch();
if (!empty($row)) return $row['page_ix']; // got it
// create a new page entry when it doesn't exist yet
$sql = "INSERT INTO url (host, path) VALUES (?, ?)";
$stmt = $AP_db->prepare($sql);
$stmt->execute([$_SESSION['host'], $_SESSION['path']]);
//echo "
Created new page entry
";
return $AP_db->lastInsertId(); // the new page id
}
// submitting a post
if (!empty($_POST['mytextarea'])) {
//echo "
saving text
";
if (empty($post_id)) { // it's a new post
// make sure there is an entry for this url in the url table
$page_id = create_page_entry();
// add an entry for the new post in the posts table
$sql ="INSERT INTO posts (user_id, page_id, reply_to) VALUES (?,?,?)";
$stmt = $AP_db->prepare($sql);
// belongs to the logged in user (not whoever we reply to)
$stmt->execute([$_SESSION['user_id'], $page_id, $reply_id]);
// now fetch the new post id for the next operation
$post_id = $AP_db->lastInsertId(); // grab the new post index (may need this for filter rating too)
//echo "
new post on page=$page_id, reply_to=$reply_id, has post_id $post_id
";
}
// clear approved status
else {
//echo "
cleared approved status
";
$sql = "UPDATE ratings SET moderator='normal' WHERE post_ix=? AND moderator='approved'";
$stmt = $AP_db->prepare($sql);
$stmt->execute([$post_id]);
}
// now make sure the user has a folder to save his/her posts
$path = sprintf("$CFG_http_root/my/h%05d", $_SESSION['user_id']);
mkdir($path, 0777, true); // recursive create the folder and don't care if it already exists
$path = sprintf("$path/%010d.htm", $post_id); // append the file name
// you can edit your own post even if it's being held for review, but not change it's status
$perms = fileperms($path); // === false when not exist
if (($perms & 0220) == 0) chmod($path, 0220); // demand write access (and keep that atleast!)
// new and old posts get written here to a file for this user
$f = fopen($path, "w"); // TODO: 10 decimal digits for 32 bit
//echo "
writing $path
";
if (!$f) {
var_dump(error_get_last());
}
else fwrite($f, $_POST['mytextarea']); // write out the file
} // end text_area
} // end of save_submit
/* --------------------- processing filter ratings is done on save_submit AND on filter_submit ----------
but only of a new value is asserted
*/
if (!empty($post_id) && array_key_exists('filter_submit', $_POST)) {
// increment counts for each separatae filter flag in an array of counters
$filter = intval($_POST['filter'],8); // copy the new filter value for the rest of the script
// this requires database to define a unique key on two values post_id and user_id
// also note I only have to specify the one to update!
$sql = "INSERT INTO filter (post_id, user_id, filter) VALUES (?,?,?) ON DUPLICATE KEY UPDATE filter=VALUES(filter)";
$stmt = $AP_db->prepare($sql);
// Note: the post_id may come from the $_POST but could be from inserting a new post above
$stmt->execute([$post_id, $_SESSION['user_id'], $filter]);
ap_log(sprintf("
have recorded new filter for $post_id by me (logged in user)=%o (octal)
", $filter));
// now recompute and store the ratings cache for this post
// NOTE: this is NOT to be confused with my own current rating hence $average and not $filter.
$average = recompute_filter($post_id);
$sql = "INSERT INTO ratings (post_ix, filter) VALUES (?,?) ON DUPLICATE KEY UPDATE filter=VALUES(filter)";
$stmt = $AP_db->prepare($sql);
$stmt->execute([$post_id, $average]);
//echo sprintf("
recompute average ratings of $post_id to %o
", $filter);
} // end of asserting filter rating
} catch (exception $e) {
var_dump($e);
ap_log("exception $e");
}
?>