/**
  * 	webdirUpload
  *
  * 	takes a file from a temporary folder, registers it in the file
  * 	manager
  * 	places the file to a web directory for direct download and makes a
  * 	thumbnail
  * 	copy if it is necessary
  *
  * @param array $_uldmap
  * 	request data
  */
 function webdirUpload($_uldmap)
 {
     $filerequestid = $_uldmap['t'] . '_' . $_uldmap['fld'] . '_' . $_uldmap['oid'];
     if ($_FILES[$filerequestid]['error'] != UPLOAD_ERR_OK) {
         return FALSE;
     }
     $oowner = $_uldmap['easycontactusr']->id;
     $filename = $_FILES[$filerequestid]['name'];
     $tmpname = $_FILES[$filerequestid]['tmp_name'];
     $filesize = $_FILES[$filerequestid]['size'];
     $filetype = mysql_real_escape_string($_FILES[$filerequestid]['type']);
     $id = intval($_uldmap['oid']);
     $Type = mysql_real_escape_string($_uldmap['t']);
     $fieldname = mysql_real_escape_string($_uldmap['fld']);
     $filename = mysql_real_escape_string($filename);
     $ds = DIRECTORY_SEPARATOR;
     $targdir = EASYCONTACTFORMS__fileUploadDir . $ds . $Type . $ds . $id . $ds . $fieldname;
     $query = "SELECT Name FROM #wp__easycontactforms_files WHERE Doctype='{$Type}' AND Docid='{$id}' AND Docfield='{$fieldname}'";
     $name = EasyContactFormsDB::getValue($query);
     $filepath = $targdir . $ds . $name;
     if (is_file($filepath)) {
         unlink($filepath);
     }
     $filepath = $targdir . $ds . $filename;
     $query = "DELETE FROM #wp__easycontactforms_files WHERE Doctype='{$Type}' AND Docid='{$id}' AND Docfield='{$fieldname}'";
     EasyContactFormsDB::query($query);
     $valuemap = array();
     $valuemap['Count'] = '0';
     $valuemap['Docfield'] = $fieldname;
     $valuemap['Doctype'] = $Type;
     $valuemap['Docid'] = $id;
     $valuemap['Name'] = $filename;
     $valuemap['Size'] = $filesize;
     $valuemap['Type'] = $filetype;
     $valuemap['Protected'] = 0;
     $valuemap['Webdir'] = 1;
     $valuemap['Storagename'] = $filename;
     $valuemap['ObjectOwner'] = $oowner;
     $isid = EasyContactFormsDB::insert($valuemap, 'Files');
     if ($Type == 'Files') {
         $valuemap = array();
         $valuemap['Docid'] = $isid;
         EasyContactFormsDB::update($valuemap, 'Files', $isid);
     }
     if (!is_dir($targdir)) {
         EasyContactFormsUtils::createFolder($targdir);
     }
     move_uploaded_file($tmpname, $filepath);
     if (isset($_uldmap['thumbnailx']) && intval($_uldmap['thumbnailx']) != 0) {
         $newfieldname = 'thumb' . $fieldname;
         $newfilename = 'thumb' . $filename;
         $newtargdir = EASYCONTACTFORMS__fileUploadDir . $ds . $Type . $ds . $id . $ds . $newfieldname;
         $query = "SELECT Name FROM #wp__easycontactforms_files WHERE Doctype='{$Type}' AND Docid='{$id}' AND Docfield='thumb{$fieldname}'";
         $name = EasyContactFormsDB::getValue($query);
         if (is_file($newtargdir . $ds . $name)) {
             unlink($newtargdir . $ds . $name);
         }
         EasyContactFormsUtils::createFolder($newtargdir);
         EasyContactFormsFiles::imgResize($filepath, $newtargdir . $ds . $newfilename, $_uldmap['thumbnailx'], $_uldmap['thumbnaily'], 0xffffff, 80);
         $query = "DELETE FROM #wp__easycontactforms_files WHERE Doctype='{$Type}' AND Docid='{$id}' AND Docfield='{$newfieldname}'";
         EasyContactFormsDB::query($query);
         $valuemap = array();
         $valuemap['Count'] = '0';
         $valuemap['Docfield'] = $newfieldname;
         $valuemap['Doctype'] = $Type;
         $valuemap['Docid'] = $id;
         $valuemap['Name'] = $newfilename;
         $valuemap['Size'] = filesize($newtargdir . $ds . $newfilename);
         $valuemap['Type'] = $filetype;
         $valuemap['Protected'] = 0;
         $valuemap['Webdir'] = 1;
         $valuemap['Storagename'] = $newfilename;
         $valuemap['ObjectOwner'] = $oowner;
         EasyContactFormsDB::insert($valuemap, 'Files');
     }
     if (isset($_uldmap['resizex']) && intval($_uldmap['resizex']) != 0) {
         EasyContactFormsFiles::imgResize($filepath, $filepath, $_uldmap['resizex'], $_uldmap['resizey'], 0xffffff, 80);
         $valuemap = array();
         $valuemap['Size'] = filesize($filepath);
         EasyContactFormsDB::update($valuemap, 'Files', $isid);
     }
     echo json_encode(array('success' => 'TRUE'));
     return TRUE;
 }
 /**
  * 	getFileDownloadLink
  *
  * 	File download link component
  *
  * @param array $config
  * 	a file download link configuration object
  *
  * @return string
  * 	html text
  */
 function getFileDownloadLink($config)
 {
     $config = (object) $config;
     require_once 'easy-contact-forms-files.php';
     $url = EasyContactFormsFiles::getFileDownloadLink($config->doctype, $config->field, $config->docid);
     if (!$url) {
         echo '';
         return;
     }
     $tag = $config->tag;
     $href = $config->tag == 'img' ? 'src' : 'href';
     $style = isset($config->style) ? $config->style : '';
     $content = isset($config->content) ? $config->content : '';
     echo "<{$tag} {$style} {$href} = '{$url}'>{$content}</{$tag}>";
 }