shalva kvaracxelia
shalva kvaracxelia

Reputation: 13

PHP callback contains

I have created a class in PHP:

<?php

class _new_model extends model {

/**
 * NEW contruct
 */
public function __construct() {
    parent::__construct();
}

function newuser(){
    $password = trim($_POST['psw']);
    $password2= trim($_POST['psw1']);
    if($password === $password2){
        $hashpsw = hash('sha512', $password);
        $salt = $this->createSalt();
        $psw = hash('sha512', $salt . $hashpsw);
        $query = $this->db->prepare('INSERT INTO users (username, password, salt) VALUES (?,?,?)');
        $query->bindValue(1, $_POST['user'], PDO::PARAM_STR);
        $query->bindValue(2, $psw, PDO::PARAM_STR);
        $query->bindValue(3, $salt, PDO::PARAM_STR);
        try{
            if($query->execute()){
                echo 'ჩაიწერა!';
            }else{
                echo 'მოხდა შეცდომა!';
            }
        }catch(PDOException $e){
            die($e->getMessage());
        }
    }else{
        echo 'პაროლები ერთმანეთს არ ემთხვევა!';
    }
}

/**
 * @return string
 */
function createSalt(){
    $text = md5(uniqid(rand(), TRUE));
    $salt = substr($text, 0, 32);
    return $salt;
}
function newlanguage(){
    $lang = trim($_POST('language'));
    $checklang = $this->checklang($lang);
    if($checklang == true){
        echo 'ეს ენა უკვე არსებობს';
    }else{
        if(is_uploaded_file($_FILES['flag']['tmp_name'])){
            $maxsize=100000;
            $size=$_FILES['flag']['size'];
            $imgdetails = getimagesize($_FILES['flag']['tmp_name']);
            $mime_type = $imgdetails['mime'];
            // checking for valid image type
            if(($mime_type=='image/jpeg')||($mime_type=='image/gif')||($mime_type=='image/bmp')||($mime_type=='image/png')){
                // checking for size again
                if($size<$maxsize){
                    $filename=$_FILES['flag']['name'];
                    $imgData =addslashes (file_get_contents($_FILES['flag']['tmp_name']));
                    $query = $this->db->prepare('INSERT INTO lang (name,mokled,flag,flagname,flagtype,flagsize) VALUES (?,?,?,?,?,?)');
                    $query->bindValue(1, $lang, PDO::PARAM_STR);
                    $query->bindValue(2, trim($_POST('shortlang')), PDO::PARAM_STR);
                    $query->bindValue(3, $imgData, PDO::PARAM_LOB);
                    $query->bindValue(4, $filename, PDO::PARAM_STR);
                    $query->bindValue(5, $mime_type, PDO::PARAM_STR);
                    $query->bindValue(6, addslashes($imgdetails[3]), PDO::PARAM_STR);
                    try{
                        if($query->execute()){
                            echo 'yes';
                        }else{
                            echo 'no query-ის გაშვებისას';
                        }
                    }catch (PDOException $e){
                        die($e->getMessage());
                    }
                }else{
                    echo "<span class='error'>Image to be uploaded is too large..Error uploading the image!!</span>";
                }
            }else{
                echo "<span class='error'>Not a valid image file! Please upload jpeg or gif image.</span>";
            }
        }else{
            switch($_FILES['flag']['error']){
                case 0: //no error; possible file attack!
                    echo "<span class='error'>There was a problem with your upload.</span>";
                    break;
                case 1: //uploaded file exceeds the upload_max_filesize directive in php.ini
                    echo "<span class='error'>The file you are trying to upload is too big.</span>";
                    break;
                case 2: //uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the html form
                    echo "<span class='error'>The file you are trying to upload is too big.</span>";
                    break;
                case 3: //uploaded file was only partially uploaded
                    echo "<span class='error'>The file you are trying upload was only partially uploaded.</span>";
                    break;
                case 4: //no file was uploaded
                    echo "<span class='error'>You must select an image for upload.</span>";
                    break;
                default: //a default error, just in case!
                    echo "<span class='error'>There was a problem with your upload.</span>";
                    break;
            }
        }
    }
}

/**
 * @param $name
 * @return bool
 */
function checklang($name){
    $query = $this->db->prepare('SELECT id FROM lang WHERE name = ?');
    $query->bindValue(1, $name, PDO::PARAM_STR);
    try{
        $query->execute();
        if($query->rowCount() > 0){
            return true;
        }else{
            return false;
        }
    }catch (PDOException $e){
        die($e->getMessage());
    }
}}

And have this HTML form:

<form enctype="multipart/form-data" action="<?php echo Domain; ?>_new/newlanguage/" name="newlanguage" method="post" id="newlanguage" >
<input type="text" name="language" id="language" placeholder="New Language">
<input type="text" name="shortlang" id="shortlang" placeholder="short name"><br>
<input type="file" name="flag" id="flag" placeholder="Flag"><br>
<input type="submit" name="submit" value="submit"></form>

When submitting the form, I get this error:

array callback has to contain indices 0 and 1 in _new_model.php on line 49 and on this line is written return $salt;

This is the function with name create salt. I don't understand when I'm calling newlanguage function, why it scanning the othere function?

PLEASE HELP

Upvotes: 0

Views: 2874

Answers (1)

Barmar
Barmar

Reputation: 781721

$_POST('language') should be $_POST['language'].

Upvotes: 3

Related Questions