Beispiel #1
0
function add_product($dbh, &$product, &$errors)
{
    $product = array();
    $errors = empty_errors();
    // считываем строки из запроса
    read_string($_POST, 'title', $product, $errors, 2, 60, false);
    read_integer($_POST, 'category_id', $product, $errors, 1, null, true);
    read_decimal($_POST, 'price', $product, $errors, '0.0', null, true);
    read_integer($_POST, 'stock', $product, $errors, 1, null, true);
    read_string($_POST, 'description', $product, $errors, 1, 10000, false, null, false);
    read_img($_FILES, 'img', $product, $errors, 0, 204800, true);
    if (has_errors($errors)) {
        return false;
    }
    // форма передана правильно, сохраняем пользователя в базу данных
    $db_product = db_product_insert($dbh, $product);
    return true;
}
/**
 * Méthode permettant de créer une copie locale des images d'un texte
 * 
 * @param $text string le texte dont il faut transformer les images
 * @param $dir string le dossier dans lequel on enregistre les images
 * @return string
 * 
 * @author Cyril MAGUIRE
 */
function rec_img($text, $dir)
{
    global $plxAdmin;
    $array_ext = array('jpg', 'png', 'gif');
    preg_match_all('!src=[\\"\']([^\\"\']+)!', $text, $matches, PREG_SET_ORDER);
    if (!empty($matches)) {
        if ($plxAdmin->aConf['userfolders'] and $_SESSION['profil'] == PROFIL_WRITER) {
            if (!is_dir(PLX_ROOT . $plxAdmin->aConf['images'] . $_SESSION['user'] . '/' . $dir)) {
                @mkdir(PLX_ROOT . $plxAdmin->aConf['images'] . $_SESSION['user'] . '/' . $dir);
                $url = $plxAdmin->aConf['images'] . $_SESSION['user'] . '/' . $dir . '/';
                $dir = PLX_ROOT . $plxAdmin->aConf['images'] . $_SESSION['user'] . '/' . $dir . '/';
            }
        } else {
            if (!is_dir(PLX_ROOT . $plxAdmin->aConf['images'] . $dir)) {
                @mkdir(PLX_ROOT . $plxAdmin->aConf['images'] . $dir);
                $url = $plxAdmin->aConf['images'] . $dir . '/';
                $dir = PLX_ROOT . $plxAdmin->aConf['images'] . $dir . '/';
            }
        }
    }
    $i = 0;
    foreach ($matches as $matche) {
        if (substr($matche[1], 0, 10) == 'data:image') {
            $ext = substr($matche[1], 11, 3);
            $data = read_base64_img($matche[1]);
        } else {
            $ext = strtolower(substr($matche[1], strrpos($matche[1], '.') + 1));
            if (in_array($ext, $array_ext)) {
                $data = read_img($matche[1], 6);
            } else {
                $data = null;
            }
        }
        if ($data != null) {
            $img_name = time() . '_' . $i . '.' . $ext;
            file_put_contents($dir . $img_name, $data);
            $text = str_replace($matche[1], $url . $img_name, $text);
        }
        $i++;
    }
    return $text;
}