}
-class Horde_Form_Type_image extends Horde_Form_Type {
-
- /**
- * Has a file been uploaded on this form submit?
- *
- * @var boolean
- */
- var $_uploaded = null;
-
- /**
- * Show the upload button?
- *
- * @var boolean
- */
- var $_show_upload = true;
-
- /**
- * Show the option to upload also original non-modified image?
- *
- * @var boolean
- */
- var $_show_keeporig = false;
-
- /**
- * Limit the file size?
- *
- * @var integer
- */
- var $_max_filesize = null;
-
- /**
- * Hash containing the previously uploaded image info.
- *
- * @var array
- */
- var $_img = array();
-
- function init($show_upload = true, $show_keeporig = false, $max_filesize = null)
- {
- $this->_show_upload = $show_upload;
- $this->_show_keeporig = $show_keeporig;
- $this->_max_filesize = $max_filesize;
- }
-
- function onSubmit($var, $vars)
- {
- /* Get the upload. */
- $this->_getUpload($vars, $var);
-
- /* If this was done through the upload button override the submitted
- * value of the form. */
- if ($vars->get('_do_' . $var->getVarName())) {
- $var->form->setSubmitted(false);
- }
- }
-
- function isValid($var, $vars, $value, &$message)
- {
- $field = $vars->get($var->getVarName());
-
- /* Get the upload. */
- $this->_getUpload($vars, $var);
-
- /* The upload generated a PEAR Error. */
- if (is_a($this->_uploaded, 'PEAR_Error')) {
- /* Not required and no image upload attempted. */
- if (!$var->isRequired() && empty($field['img']) &&
- $this->_uploaded->getCode() == UPLOAD_ERR_NO_FILE) {
- return true;
- }
-
- if (($this->_uploaded->getCode() == UPLOAD_ERR_NO_FILE) &&
- empty($field['img'])) {
- /* Nothing uploaded and no older upload. */
- $message = _("This field is required.");
- return false;
- } elseif (!empty($field['img'])) {
- /* Nothing uploaded but older upload present. */
- return true;
- } else {
- /* Some other error message. */
- $message = $this->_uploaded->getMessage();
- return false;
- }
- } elseif ($this->_max_filesize &&
- $this->_img['size'] > $this->_max_filesize) {
- $message = sprintf(_("The image file was larger than the maximum allowed size (%d bytes)."), $this->_max_filesize);
- return false;
- }
-
- return true;
- }
-
- function getInfo($vars, $var, &$info)
- {
- /* Get the upload. */
- $this->_getUpload($vars, $var);
-
- /* Get image params stored in the hidden field. */
- $value = $var->getValue($vars);
- $info = $this->_img;
- if (empty($info['file'])) {
- unset($info['file']);
- return;
- }
- if ($this->_show_keeporig) {
- $info['keep_orig'] = !empty($value['keep_orig']);
- }
-
- /* Set the uploaded value (either true or PEAR_Error). */
- $info['uploaded'] = &$this->_uploaded;
-
- /* If a modified file exists move it over the original. */
- if ($this->_show_keeporig && $info['keep_orig']) {
- /* Requested the saving of original file also. */
- $info['orig_file'] = Horde::getTempDir() . '/' . $info['file'];
- $info['file'] = Horde::getTempDir() . '/mod_' . $info['file'];
- /* Check if a modified file actually exists. */
- if (!file_exists($info['file'])) {
- $info['file'] = $info['orig_file'];
- unset($info['orig_file']);
- }
- } else {
- /* Saving of original not required. */
- $mod_file = Horde::getTempDir() . '/mod_' . $info['file'];
- $info['file'] = Horde::getTempDir() . '/' . $info['file'];
-
- if (file_exists($mod_file)) {
- /* Unlink first (has to be done on Windows machines?) */
- unlink($info['file']);
- rename($mod_file, $info['file']);
- }
- }
- }
-
- /**
- * Gets the upload and sets up the upload data array. Either
- * fetches an upload done with this submit or retries stored
- * upload info.
- */
- function _getUpload($vars, $var)
- {
- /* Don't bother with this function if already called and set
- * up vars. */
- if (!empty($this->_img)) {
- return true;
- }
-
- /* Check if file has been uploaded. */
- $varname = $var->getVarName();
- $this->_uploaded = Horde_Browser::wasFileUploaded($varname . '[new]');
-
- if ($this->_uploaded === true) {
- /* A file has been uploaded on this submit. Save to temp dir for
- * preview work. */
- $this->_img['type'] = $this->getUploadedFileType($varname . '[new]');
-
- /* Get the other parts of the upload. */
- Horde_Array::getArrayParts($varname . '[new]', $base, $keys);
-
- /* Get the temporary file name. */
- $keys_path = array_merge(array($base, 'tmp_name'), $keys);
- $this->_img['file'] = Horde_Array::getElement($_FILES, $keys_path);
-
- /* Get the actual file name. */
- $keys_path= array_merge(array($base, 'name'), $keys);
- $this->_img['name'] = Horde_Array::getElement($_FILES, $keys_path);
-
- /* Get the file size. */
- $keys_path= array_merge(array($base, 'size'), $keys);
- $this->_img['size'] = Horde_Array::getElement($_FILES, $keys_path);
-
- /* Get any existing values for the image upload field. */
- $upload = $vars->get($var->getVarName());
- $upload['img'] = @unserialize($upload['img']);
-
- /* Get the temp file if already one uploaded, otherwise create a
- * new temporary file. */
- if (!empty($upload['img']['file'])) {
- $tmp_file = Horde::getTempDir() . '/' . $upload['img']['file'];
- } else {
- $tmp_file = Horde::getTempFile('Horde', false);
- }
-
- /* Move the browser created temp file to the new temp file. */
- move_uploaded_file($this->_img['file'], $tmp_file);
- $this->_img['file'] = basename($tmp_file);
-
- /* Store the uploaded image file data to the hidden field. */
- $upload['img'] = serialize($this->_img);
- $vars->set($var->getVarName(), $upload);
- } elseif ($this->_uploaded) {
- /* File has not been uploaded. */
- $upload = $vars->get($var->getVarName());
- if ($this->_uploaded->getCode() == 4 && !empty($upload['img'])) {
- $this->_img = @unserialize($upload['img']);
- }
- }
- }
-
- function getUploadedFileType($field)
- {
- /* Get any index on the field name. */
- $index = Horde_Array::getArrayParts($field, $base, $keys);
-
- if ($index) {
- /* Index present, fetch the mime type var to check. */
- $keys_path = array_merge(array($base, 'type'), $keys);
- $type = Horde_Array::getElement($_FILES, $keys_path);
- $keys_path= array_merge(array($base, 'tmp_name'), $keys);
- $tmp_name = Horde_Array::getElement($_FILES, $keys_path);
- } else {
- /* No index, simple set up of vars to check. */
- $type = $_FILES[$field]['type'];
- $tmp_name = $_FILES[$field]['tmp_name'];
- }
-
- if (empty($type) || ($type == 'application/octet-stream')) {
- /* Type wasn't set on upload, try analising the upload. */
- global $conf;
- require_once 'Horde/MIME/Magic.php';
- if (!($type = MIME_Magic::analyzeFile($tmp_name, isset($conf['mime']['magic_db']) ? $conf['mime']['magic_db'] : null))) {
- if ($index) {
- /* Get the name value. */
- $keys_path = array_merge(array($base, 'name'), $keys);
- $name = Horde_Array::getElement($_FILES, $keys_path);
-
- /* Work out the type from the file name. */
- $type = MIME_Magic::filenameToMIME($name);
-
- /* Set the type. */
- $keys_path = array_merge(array($base, 'type'), $keys);
- Horde_Array::getElement($_FILES, $keys_path, $type);
- } else {
- /* Work out the type from the file name. */
- $type = MIME_Magic::filenameToMIME($_FILES[$field]['name']);
-
- /* Set the type. */
- $_FILES[$field]['type'] = MIME_Magic::filenameToMIME($_FILES[$field]['name']);
- }
- }
- }
-
- return $type;
- }
-
- /**
- * Loads any existing image data into the image field. Requires that the
- * array $image passed to it contains the structure:
- * $image['load']['file'] - the filename of the image;
- * $image['load']['data'] - the raw image data.
- *
- * @param array $image The image array.
- */
- function loadImageData(&$image)
- {
- /* No existing image data to load. */
- if (!isset($image['load'])) {
- return;
- }
-
- /* Save the data to the temp dir. */
- $tmp_file = Horde::getTempDir() . '/' . $image['load']['file'];
- if ($fd = fopen($tmp_file, 'w')) {
- fwrite($fd, $image['load']['data']);
- fclose($fd);
- }
-
- $image['img'] = serialize(array('file' => $image['load']['file']));
- unset($image['load']);
- }
-
- /**
- * Return info about field type.
- */
- function about()
- {
- return array(
- 'name' => _("Image upload"),
- 'params' => array(
- 'show_upload' => array('label' => _("Show upload?"),
- 'type' => 'boolean'),
- 'show_keeporig' => array('label' => _("Show option to keep original?"),
- 'type' => 'boolean'),
- 'max_filesize' => array('label' => _("Maximum file size in bytes"),
- 'type' => 'int')));
- }
-
-}
-
class Horde_Form_Type_boolean extends Horde_Form_Type {
function isValid($var, $vars, $value, &$message)
$this->_getActionScripts($form, $var));
}
- /**
- * @todo Show image dimensions in the width/height boxes.
- */
- function _renderVarInput_image($form, $var, $vars)
- {
- $varname = $var->getVarName();
- $image = $var->getValue($vars);
-
- /* Check if existing image data is being loaded. */
- $var->type->loadImageData($image);
-
- Horde::addScriptFile('image.js', 'horde', true);
- $graphics_dir = $GLOBALS['registry']->getImageDir('horde');
- $img_dir = $graphics_dir . '/image';
-
- $html = '';
-
- /* Check if there is existing img information stored. */
- if (isset($image['img'])) {
- /* Hidden tag to store the preview image filename. */
- $html = sprintf(' <input type="hidden" name="%1$s" id="%1$s" value="%2$s" />',
- $varname . '[img]',
- htmlspecialchars($image['img'], ENT_QUOTES, Horde_Nls::getCharset()));
-
- /* Unserialize the img information to get the full array. */
- $image['img'] = @unserialize($image['img']);
- }
-
- /* Output the input tag. */
- if (empty($image['img'])) {
- $js = "
-var p = document.getElementById('" . $varname . "[preview]');
-o = '\\\\'; a = '/';
-tmp = '' + document.getElementById('" . $varname . "[new]').value;
-if (tmp) {
- while (tmp.indexOf(o) > -1) {
- pos = tmp.indexOf(o);
- tmp = '' + (tmp.substring(0, pos) + a + tmp.substring((pos + o.length), tmp.length));
- }
- p.src = 'file:///' + tmp;
- p.alt = '" . addslashes(_("If you see this message but no image, the image you want to upload can't be displayed by your browser.")) . "';
-}";
- $browser = Horde_Browser::singleton();
- if ($browser->isBrowser('msie')) {
- $html .= sprintf(' <input type="file" class="form-input-file" name="%1$s" id="%1$s" onchange="%2$s" />',
- $varname . '[new]',
- $js);
- } else {
- $html .= sprintf(' <input type="file" class="form-input-file" name="%1$s" id="%1$s"
- onclick="window.setTimeout(\'document.getElementById(\\\'%1$s\\\').blur();\', 5);"
- onblur="%2$s" />',
- $varname . '[new]',
- $js);
- }
- } else {
- $html .= sprintf(' <input type="file" class="form-input-file" name="%1$s" id="%1$s" />',
- $varname . '[new]');
- }
-
- /* Output the button to upload/reset the image. */
- if ($var->type->show_upload) {
- $html .= ' ';
- $html .= sprintf(' <input class="form-button-upload" name="%1$s" id="%1$s" type="submit" value="%2$s" /> ',
- '_do_' . $varname,
- _("Upload"));
- }
-
- if (empty($image['img'])) {
- /* No image information stored yet, show a blank
- * preview. */
- $html .= Horde::img('tree/blank.png', _("Preview"),
- 'class="form-image-preview-blank" id="' . $varname . '[preview]"',
- $graphics_dir);
- } else {
- /* Image information stored, show preview, add buttons for
- * image manipulation. */
- $html .= '<br />';
- $img = Horde::url($GLOBALS['registry']->get('webroot', 'horde') . '/services/images/view.php');
- if (isset($image['img']['vfs_id'])) {
- /* Calling an image from VFS. */
- $img = Horde_Util::addParameter($img, array('f' => $image['img']['vfs_id'],
- 's' => 'vfs',
- 'p' => $image['img']['vfs_path']));
- } else {
- /* Calling an image from a tmp directory (uploads). */
- $img = Horde_Util::addParameter($img, 'f', $image['img']['file']);
- }
-
- // TODO: possible to change to unobtrusive JS?
- /* Rotate 270. */
- $html .= Horde::link('#', '', '', '', 'showImage(\'' . Horde_Util::addParameter($img, array('a' => 'rotate', 'v' => '270')) . '\', \'_p_' . $varname . '\', true);') . Horde::img('rotate-270.png', _("Rotate Left"), '', $img_dir) . '</a>';
-
- /* Rotate 180. */
- $html .= Horde::link('#', '', '', '', 'showImage(\'' . Horde_Util::addParameter($img, array('a' => 'rotate', 'v' => '180')) . '\', \'_p_' . $varname . '\', true);') . Horde::img('rotate-180.png', _("Rotate 180"), '', $img_dir) . '</a>';
-
- /* Rotate 90. */
- $html .= Horde::link('#', '', '', '', 'showImage(\'' . Horde_Util::addParameter($img, array('a' => 'rotate', 'v' => '90')) . '\', \'_p_' . $varname . '\', true);') . Horde::img('rotate-90.png', _("Rotate Right"), '', $img_dir) . '</a>';
-
- /* Flip image. */
- $html .= Horde::link('#', '', '', '', 'showImage(\'' . Horde_Util::addParameter($img, 'a', 'flip') . '\', \'_p_' . $varname . '\', true);') . Horde::img('flip.png', _("Flip"), '', $img_dir) . '</a>';
-
- /* Mirror image. */
- $html .= Horde::link('#', '', '', '', 'showImage(\'' . Horde_Util::addParameter($img, 'a', 'mirror') . '\', \'_p_' . $varname . '\', true);') . Horde::img('mirror.png', _("Mirror"), '', $img_dir) . '</a>';
-
- /* Apply grayscale. */
- $html .= Horde::link('#', '', '', '', 'showImage(\'' . Horde_Util::addParameter($img, 'a', 'grayscale') . '\', \'_p_' . $varname . '\', true);') . Horde::img('grayscale.png', _("Grayscale"), '', $img_dir) . '</a>';
-
- /* Resize width. */
- $html .= sprintf('%s <input type="text" class="form-input-resize" onchange="src=getResizeSrc(\'%s\', \'%s\');showImage(src, \'_p_%s\', true);" %s />',
- _("w:"),
- Horde_Util::addParameter($img, 'a', 'resize'),
- $varname,
- $varname,
- '_w_'. $varname);
-
- /* Resize height. */
- $html .= sprintf('%s <input type="text" class="form-input-resize" onchange="src=getResizeSrc(\'%s\', \'%s\');showImage(src, \'_p_%s\', true);" %s />',
- _("h:"),
- Horde_Util::addParameter($img, 'a', 'resize'),
- $varname,
- $varname,
- '_h_'. $varname);
-
- /* Apply fixed ratio resize. */
- $html .= Horde::link('#', '', '', '', 'src=getResizeSrc(\'' . Horde_Util::addParameter($img, 'a', 'resize') . '\', \'' . $varname . '\', \'1\');showImage(src, \'_p_' . $varname . '\', true);') . Horde::img('ratio.png', _("Fix ratio"), '', $img_dir) . '</a>';
-
- /* Keep also original if it has been requested. */
- if ($var->type->show_keeporig) {
- $html .= sprintf(' <input type="checkbox" class="form-input-checkbox" name="%s"%s />%s' . "\n",
- $varname . '[keep_orig]',
- !empty($image['keep_orig']) ? ' checked="checked"' : '',
- _("Keep original?"));
- }
-
- /* The preview image element. */
- $html .= '<br /><img src="' . $img . '" id="_p_' . $varname .'" />'."\n";
- }
-
- return $html;
- }
-
function _renderVarInput_longtext($form, $var, $vars)
{
global $browser;
}
}
- function _renderVarDisplay_image($form, $var, $vars)
- {
- $img_params = $var->getValue($vars);
- $img_url = Horde::url($GLOBALS['registry']->get('webroot', 'horde') . '/services/images/view.php');
- $img_url = Horde_Util::addParameter($img_url, $img_params);
-
- return Horde::img($img_url, isset($img_params['f']) ? $img_params['f'] : '', '', '');
- }
-
function _renderVarDisplay_phone($form, &$var, &$vars)
{
global $registry;