<?php
function Sanitize($name) {
return preg_replace('/[^a-zA-Z0-9-]/', '', $name);
}
function GenFileName($name) {
$user_id = get_current_user_id();
$user_id = (string)$user_id;
$user = $user_id;
$directory = 'lists/' . $user;
mkdir($directory, 0777, true);
$name = Sanitize($name);
return $directory . '/' . $name . '.txt';
}
function SafeList($name, $ids) {
$content = '';
// [1,5,2] -> '1,5,2,'
foreach ($ids as $id)
$content .= $id . ',';
// remove trailing komma: '1,5,2,' -> '1,5,2'
$content = substr($content, 0, strlen($content) - 1);
if (empty($content))
return;
$filename = GenFileName($name);
file_put_contents($filename, $content);
}
function DigitsOnly($str) {
return preg_replace( "/\D/", "", $str );
}
function ProcessPosts($posts) {
$listname = $posts['listname'];
$idsToSave = $posts['idsToSave'];
$ids = array_map(DigitsOnly, $idsToSave);
if (empty($listname) || empty($ids))
return;
SafeList($listname, $ids);
}
ProcessPosts($_POST);
?>