• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

How to clean postvars in array

#1
I am writing an addon where user can upload stuff with descriptions, titles, and various options.
i got quite far, but i ran into the problem of cleaning postvars in an array. (when for example uploading a bunch of images.
In the controllerPublic Action domyupload i get following if I do a print_r($_POST)
PHP:
Array
(
    [cat_id] => 21
    [qg_upload_policy] => 1
    [image] => Array
        (
            [55] => Array
                (
                    [title] => Real Title 1
                    [desc] => Description for image 1
                    [title_temp] => My default title 1
                    [uploaded] => 1
                )

            [54] => Array
                (
                    [title] => Real Title 2
                    [desc] => Description for image 2
                    [title_temp] => My default title 1
                    [uploaded] => 1
                )

        )

    [attachment_hash] => 0d65289a23796a06fa665dcbecd965a8
    [_xfToken] => 1,1289944902,17544bc009aa2a89a1958ee03ebdd51dd18f26da
)
Now cleaning cat_id and qg_upload_policy is easy..
I do:
PHP:
$catId = $this->_input->filterSingle('cat_id', XenForo_Input::UINT);
$uploadPolicy = $this->_input->filterSingle('qg_upload_policy', XenForo_Input::UINT);
But then I have to clean [image] and that is a multiple array.
(55 and 54 are the image ids)
My first approach would be to do:
$images = $this->_input->filterSingle('image', XenForo_Input::ARRAY_SIMPLE);
then I would do a foreach loop
foreach($images as $image)
{

}
but then I'm stuck.. and I have the feeling that this is quite complicated eating up lots of ressources for a simple thing..
Help would be appreciated

Luc
 

Indigo

Active member
#2
If you still wanted to use XF's input filter, you could technically do
PHP:
$title = XenForo_Input::rawFilter($image['title'], XenForo_Input::STRING);
since filter() and filterSingle() depend on variables inside of the request.

Alternatively, it looks you could also make a new input instance with a different source, like:
PHP:
$input = new XenForo_Input($image);
$clean = $input->filter(array(
    'title' => XenForo_Input::STRING,
    'desc' => XenForo_Input::STRING,
    'title_temp' => XenForo_Input::STRING,
    'uploaded' => XenForo_Input::UINT,
));
In that case, the variable names you put in would be keys of $image.
 

Jaxel

Well-known member
#3
What format is the uploaded file in? XML? "upload_file" should be the name of your file input.

Code:
		$fileTransfer = new Zend_File_Transfer_Adapter_Http();

		if ($fileTransfer->isUploaded('upload_file'))
		{
			$fileInfo = $fileTransfer->getFileInfo('upload_file');
			$fileName = $fileInfo['upload_file']['tmp_name'];

			if (!file_exists($fileName) || !is_readable($fileName))
			{
				throw new XenForo_Exception(new XenForo_Phrase('please_enter_valid_file_name_requested_file_not_read'), true);
			}

			$file = new SimpleXMLElement($fileName, null, true);
		}
$file is now an XML object.
 
#4
thanx for the replies...
@indigo
i will test both see what is faster.. (if there are about 2 or 300 $images) but i guess it will workout pretty the same. The new instance method looks more elegant though..

@jaxel
NO the file is allready uploaded, in its folder and in the database.. (with flashuploader or via single form)
this only happens IF the user wants to change something (title or description) the temptitle would be the default title (Filename cleaned minus .jpg and spaces and ucfirst). He could also set a content filter. the catid is the category he uploads in.. Al the other info is allready in database via upload_file .

Thank You again for your help

Luc
 
#5
i came up with this:
PHP:
$this->_assertPostOnly();

$catId = $this->_input->filterSingle('cat_id', XenForo_Input::UINT);
$images = $this->_input->filterSingle('image', XenForo_Input::ARRAY_SIMPLE);
foreach($images AS $image)
{
    $save_to_db = isset($image['uploaded']) && $image['uploaded']==1 ? true : false;
    if( $save_to_db)
    {
        $image['filter'] = isset($image['filter']) && $image['filter']==1 ? 1 : 0;
        $image['title_temp'] = str_replace("|", '"', $image['title_temp']);
        $image['title'] = (!isset($image['title']) || empty($image['title']) || $image['title'] == '') ? $image['title_temp'] : $image['title'];
        $image['cat_id'] = $catId;
        $input = new XenForo_Input($image);
        $cleanImage = $input->filter(array(
        'cat_id' => XenForo_Input::UINT,
        'title' => XenForo_Input::STRING,
        'desc' => XenForo_Input::STRING,
        'filter' => XenForo_Input::UINT
        ));
        /*** save $cleanImage elements to db ***/
    }
}
unset($images,$image,$cleanImage);
Now my newbie question...
to put this in db, should i create a new $datawriter before the foreach loop and do the set and save inside, OR do I have to create a new $datawriter inside the loop at every iterance?

of course here, i could also just update with a "normal" db update call..

Thanks again.

Luc