[ create a new paste ] login | about

Link: http://codepad.org/kY1hW06z    [ raw code | output | fork ]

PHP, pasted on Aug 11:
<?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);

?>


Output:
1
2

Warning: array_map(): Argument #2 should be an array on line 41


Create a new paste based on this one


Comments: