/** * intializes the object with a setup array * * @param array $config an array of optional parameters: * 'filename' => an image path, filename or URL * 'classname' => a classname for the image * 'wrap_tags' => array of open and close HTML * 'link' URI for a wrapping anchor tag * 'mode' => display mode: as an image or a filename or both * 'module' => calling module */ function __construct($config) { parent::__construct($config); $this->classname .= ' ' . Participants_Db::$prefix . 'image image-field-wrap'; if (empty($this->link) and $this->link !== false) { $this->link = $this->image_defined && Participants_Db::$plugin_options['image_link'] == 1 ? $this->image_uri : ''; } }
/** * intializes the object with a setup array * * @param array $config an array of optional parameters: * 'filename' => an image path, filename or URL * 'classname' => a classname for the image * 'wrap_tags' => array of open and close HTML * 'link' URI for a wrapping anchor tag * 'relstring' => string to show in the "rel" attribute of the image * 'mode' => display mode: as an image or a filename or both * 'module' => calling module */ function __construct($config) { parent::__construct($config); $this->classname .= ' ' . Participants_Db::$prefix . 'image image-field-wrap'; if (empty($this->link)) { $this->link = $this->image_defined && Participants_Db::plugin_setting_is_true('image_link') ? $this->image_uri : ''; } }
/** * returns an element value formatted for display or storage * * this supplants the function Participants_Db::prep_field_for_display * * @param object $field a Field_Item object * @param bool $html if true, retuns the value wrapped in HTML, false returns * the formatted value alone * @return string the object's current value, formatted */ public static function get_field_value_display($field, $html = true) { $return = ''; if (has_filter(Participants_Db::$prefix . 'before_display_field')) { $return = Participants_Db::set_filter('before_display_field', $return, $field->value, $field->form_element); } if (empty($return)) { switch ($field->form_element) { case 'image-upload': $image = new PDb_Image(array('filename' => $field->value, 'link' => isset($field->link) ? $field->link : '', 'mode' => 'both', 'module' => $field->module)); if ($html) { if (isset($field->module) and in_array($field->module, array('single', 'list'))) { $image->display_mode = 'image'; } elseif (isset($field->module) and in_array($field->module, array('signup', 'admin-edit'))) { $image->display_mode = $image->image_defined ? 'both' : 'none'; $image->link = false; } $image->set_image_wrap(); $return = $image->get_image_html(); } elseif ($image->file_exists) { $return = $image->get_image_file(); } else { $return = $field->value; } break; case 'file-upload': if ($html and !empty($field->value)) { if ($field->module == 'signup') { $field->link = false; $return = $field->value; } else { $field->link = xnau_Image_Handler::concatenate_directory_path(get_bloginfo('url'), Participants_Db::$plugin_options['image_upload_location']) . $field->value; $return = self::make_link($field); } break; } else { $return = $field->value; break; } case 'date': case 'timestamp': $return = ''; if (self::is_empty($field->value) === false) { $date = Participants_Db::parse_date($field->value, $field); $format = Participants_Db::$date_format; if (Participants_Db::$plugin_options['show_time'] == '1' and $field->form_element == 'timestamp') { $format .= ' ' . get_option('time_format'); } $return = date_i18n($format, $date); } else { $return = ''; } break; case 'multi-checkbox': case 'multi-select-other': /* * these elements are stored as serialized arrays of values, the data is displayed * a comma-separated string of the values, using the value titles if defined */ $multivalues = Participants_Db::unserialize_array($field->value); // remove empty elements and convert to string for display $multivalues = array_filter((array) $multivalues, array(__CLASS__, 'is_displayable')); $titles = array(); foreach ($multivalues as $value) { $titles[] = self::get_value_title($value, $field->name); } $return = implode(', ', $titles); break; case 'link': $linkdata = Participants_Db::unserialize_array($field->value); if (!is_array($linkdata)) { $return = ''; break; } if (empty($linkdata[1])) { $linkdata[1] = str_replace('http://', '', $linkdata[0]); } if ($html) { $return = vsprintf(empty($linkdata[0]) ? '%1$s%2$s' : '<a href="%1$s">%2$s</a>', $linkdata); } else { $return = $linkdata[0]; } break; case 'text-line': if ($html) { $field->value = self::get_value_title($field->value, $field->name); $return = self::make_link($field); break; } else { $return = $field->value; break; } case 'text-area': case 'textarea': $return = sprintf('<span class="textarea">%s</span>', $field->value); break; case 'rich-text': $return = sprintf('<span class="textarea richtext">%s</span>', Participants_Db::process_rich_text($field->value)); break; default: $field->value = self::get_value_title($field->value, $field->name); $return = self::make_link($field); } } return $return; }
/** * handles a file upload * * @param string $name the name of the current field * @param array $file the $_FILES array element corresponding to one file * * @return string the path to the uploaded file or false if error */ private static function _handle_file_upload($name, $file) { $field_atts = self::get_field_atts($name); $type = 'image-upload' == $field_atts->form_element ? 'image' : 'file'; $delete = (bool) (isset($_REQUEST[$name . '-deletefile']) and $_REQUEST[$name . '-deletefile'] == 'delete'); // attempt to create the target directory if it does not exist if (!is_dir(xnau_Image_Handler::concatenate_directory_path(ABSPATH, self::$plugin_options['image_upload_location']))) { if (false === self::_make_uploads_dir(self::$plugin_options['image_upload_location'])) { return false; } } if (!is_uploaded_file(realpath($file['tmp_name']))) { self::_show_validation_error(__('There is something wrong with the file you tried to upload. Try another.', 'participants-database'), $name); return false; } /* get the allowed file types and test the uploaded file for an allowed file * extension */ $extensions = empty($field_atts->values) ? self::$plugin_options['allowed_file_types'] : implode(',', self::unserialize_array($field_atts->values)); $test = preg_match('#^(.+)\\.(' . implode('|', array_map('trim', explode(',', strtolower($extensions)))) . ')$#', strtolower($file['name']), $matches); //error_log(__METHOD__.' ext:'.$extensions.' test:'. $test.' matches:'.print_r($matches,1)); if (0 === $test) { if ($type == 'image' && empty($field_atts->values)) { self::_show_validation_error(sprintf(__('For "%s", you may only upload image files like JPEGs, GIFs or PNGs.', 'participants-database'), $field_atts->title), $name); } else { self::_show_validation_error(sprintf(__('The file selected for "%s" must be one of these types: %s. ', 'participants-database'), $field_atts->title, preg_replace('#(,)(?=[^,])#U', ', ', $extensions)), $name); } return false; } else { // validate and construct the new filename using only the allowed file extension $new_filename = preg_replace(array('#\\.#', "/\\s+/", "/[^-\\.\\w]+/"), array("-", "_", ""), $matches[1]) . '.' . $matches[2]; // now make sure the name is unique by adding an index if needed $index = 1; while (file_exists(xnau_Image_Handler::concatenate_directory_path(ABSPATH, self::$plugin_options['image_upload_location']) . $new_filename)) { $filename_parts = pathinfo($new_filename); $new_filename = preg_replace(array('#_[0-9]+$#'), array(''), $filename_parts['filename']) . '_' . $index . '.' . $filename_parts['extension']; $index++; } } /* * we perform a validity check on the image files, this also makes sure only * images are uploaded in image upload fields and only non-images are uploaded * in file upload fields */ $fileinfo = getimagesize($file['tmp_name']); $valid_image = in_array($fileinfo[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_WBMP)); if ($type == 'image' and !$valid_image) { self::_show_validation_error(sprintf(__('For "%s", you may only upload image files like JPEGs, GIFs or PNGs.', 'participants-database'), $field_atts->title), $name); return false; } if ($file['size'] > self::$plugin_options['image_upload_limit'] * 1024) { self::_show_validation_error(sprintf(__('The file you tried to upload is too large. The file must be smaller than %sK.', 'participants-database'), self::$plugin_options['image_upload_limit']), $name); return false; } if (false === move_uploaded_file($file['tmp_name'], xnau_Image_Handler::concatenate_directory_path(ABSPATH, self::$plugin_options['image_upload_location']) . $new_filename)) { self::_show_validation_error(__('The file could not be saved.', 'participants-database')); return false; } else { return $new_filename; } }