private function uploadFile($type, $section, $subsection, $key, $value) { $sectionData = $this->getAdminData($type, $section, $subsection); if (isset($value['error']) && $value['error'] != UPLOAD_ERR_OK) { throw new KurogoDataException(Kurogo::file_upload_error_message($value['error'])); } if (!isset($value['tmp_name']) || !is_uploaded_file($value['tmp_name'])) { throw new KurogoDataException("Error locating uploaded file"); } switch ($sectionData['sectiontype']) { case 'fields': if (!isset($sectionData['fields'][$key])) { throw new KurogoConfigurationException("Invalid key {$key} for {$type} section {$section}"); } $fieldData = $sectionData['fields'][$key]; break; case 'section': $fieldData = $sectionData; throw new KurogoConfigurationException("Code not written for this type of field"); break; default: throw new KurogoConfigurationException("Unable to handle {$type} {$section}. Invalid section type " . $sectionData['sectiontype']); } if (!isset($fieldData['destinationType'])) { throw new KurogoConfigurationException("Unable to determine destination type"); } switch ($fieldData['destinationType']) { case 'file': if (!isset($fieldData['destinationFile'])) { throw new KurogoConfigurationException("Unable to determine destination location"); } $destination = $fieldData['destinationFile']; break; case 'folder': if (!isset($fieldData['destinationFile'])) { throw new KurogoConfigurationException("Unable to determine destination location"); } if (!isset($fieldData['destinationFolder'])) { throw new KurogoConfigurationException("Unable to determine destination location"); } $destination = rtrim($fieldData['destinationFolder'], '/') . '/' . ltrim($fieldData['destinationFile'], '/'); break; } $prefix = isset($fieldData['destinationPrefix']) ? $fieldData['destinationPrefix'] : ''; if ($prefix && defined($prefix)) { $destination = constant($prefix) . '/' . $destination; } if (isset($fieldData['fileType'])) { switch ($fieldData['fileType']) { case 'image': $this->setResponseVersion(1); try { $imageData = new ImageProcessor($value['tmp_name']); $transformer = new ImageTransformer($fieldData); $imageType = isset($fieldData['imageType']) ? $fieldData['imageType'] : null; $result = $imageData->transform($transformer, $imageType, $destination); if (KurogoError::isError($result)) { $this->throwError($result); } } catch (KurogoException $e) { throw new KurogoException("Uploaded file must be a valid image (" . $e->getMessage() . ")"); } break; default: throw new KurogoConfigurationException("Unknown fileType " . $fieldData['fileType']); } } else { if (!move_uploaded_file($value['tmp_name'], $destination)) { $this->throwError(new KurogoError(1, "Cannot save file", "Unable to save uploaded file")); } } }
public function transform(ImageTransformer $transformer, $imageType, $file) { $boundingBox = $transformer->getBoundingBox($this->width, $this->height); if (KurogoError::isError($boundingBox)) { return $boundingBox; } if (is_null($imageType)) { $imageType = $this->imagetype; } else { switch ($imageType) { case IMAGETYPE_GIF: case IMAGETYPE_JPEG: case IMAGETYPE_PNG: break; case 'gif': $imageType = IMAGETYPE_GIF; break; case 'jpg': $imageType = IMAGETYPE_JPEG; break; case 'png': $imageType = IMAGETYPE_PNG; break; } } $width = $boundingBox[0]; $height = $boundingBox[1]; if ($this->width == $width && $this->height == $height && $imageType == $this->imagetype) { return copy($this->fileName, $file) ? true : new KurogoError(1, "Error copying", "Error saving file"); } if (!function_exists('gd_info')) { throw new KurogoDataException("Resizing images requires the GD image library"); } switch ($this->imagetype) { case IMAGETYPE_JPEG: $src = imagecreatefromjpeg($this->fileName); break; case IMAGETYPE_PNG: $src = imagecreatefrompng($this->fileName); break; case IMAGETYPE_GIF: $src = imagecreatefromgif($this->fileName); break; default: throw new KurogoDataException("Unable to read files of this type ({$this->imagetype})"); } switch ($imageType) { case IMAGETYPE_JPEG: $dest = imagecreatetruecolor($width, $height); $saveFunc = 'ImageJPEG'; break; case IMAGETYPE_PNG: $dest = imagecreatetruecolor($width, $height); imagealphablending($dest, false); imagesavealpha($dest, true); $saveFunc = 'ImagePNG'; break; case IMAGETYPE_GIF: $dest = imagecreate($width, $height); $saveFunc = 'ImageGIF'; break; default: throw new KurogoDataException("Unable to save files of this type"); } if ($this->width != $width || $this->height != $height) { imagecopyresampled($dest, $src, 0, 0, 0, 0, $width, $height, $this->width, $this->height); } else { $dest &= $src; } return call_user_func($saveFunc, $dest, $file); }
public function transform(ImageTransformer $transformer, $imageType, $file) { $boundingBox = $transformer->getBoundingBox($this->width, $this->height); if (KurogoError::isError($boundingBox)) { return $boundingBox; } if (is_null($imageType)) { $imageType = $this->imagetype; } else { switch ($imageType) { case IMAGETYPE_GIF: case IMAGETYPE_JPEG: case IMAGETYPE_PNG: break; case 'gif': $imageType = IMAGETYPE_GIF; break; case 'jpg': $imageType = IMAGETYPE_JPEG; break; case 'png': $imageType = IMAGETYPE_PNG; break; } } $width = $boundingBox[0]; $height = $boundingBox[1]; if ($this->width == $width && $this->height == $height && $imageType == $this->imagetype) { return copy($this->fileName, $file) ? true : new KurogoError(1, "Error copying", "Error saving file"); } if (!function_exists('gd_info')) { throw new KurogoDataException("Resizing images requires the GD image library"); } switch ($this->imagetype) { case IMAGETYPE_JPEG: $src = imagecreatefromjpeg($this->fileName); break; case IMAGETYPE_PNG: $src = imagecreatefrompng($this->fileName); break; case IMAGETYPE_GIF: $src = imagecreatefromgif($this->fileName); break; default: throw new KurogoDataException("Unable to read files of this type ({$this->imagetype})"); } switch ($imageType) { case IMAGETYPE_JPEG: $dest = imagecreatetruecolor($width, $height); $saveFunc = 'ImageJPEG'; break; case IMAGETYPE_PNG: $dest = imagecreatetruecolor($width, $height); imagealphablending($dest, false); imagesavealpha($dest, true); $saveFunc = 'ImagePNG'; break; case IMAGETYPE_GIF: $dest = imagecreate($width, $height); $saveFunc = 'ImageGIF'; break; default: throw new KurogoDataException("Unable to save files of this type"); } $crop = false; if (isset($boundingBox[2]) && isset($boundingBox[3]) && !isset($boundingBox[4])) { //do crop $crop = true; $srcWidth = $boundingBox[2]; $srcHeight = $boundingBox[3]; if ($this->width == $srcWidth) { $y = ($this->height - $srcHeight) / 2; $x = 0; } if ($this->height == $srcHeight) { $x = ($this->width - $srcWidth) / 2; $y = 0; } imagecopyresampled($dest, $src, 0, 0, $x, $y, $width, $height, $srcWidth, $srcHeight); } elseif (isset($boundingBox[2]) && isset($boundingBox[3]) && isset($boundingBox[4])) { $crop = true; // do fill $rgb = $boundingBox[5] ? $boundingBox[5] : "ffffff"; $bg = imagecolorallocate($dest, hexdec(substr($rgb, 0, 2)), hexdec(substr($rgb, 2, 2)), hexdec(substr($rgb, 4, 2))); // fill white color on bg imagefill($dest, 0, 0, $bg); $y = ($height - $this->height) / 2; $x = ($width - $this->width) / 2; imagecopy($dest, $src, $x, $y, 0, 0, $this->width, $this->height); } if (!$crop) { if ($this->width != $width || $this->height != $height) { imagecopyresampled($dest, $src, 0, 0, 0, 0, $width, $height, $this->width, $this->height); } else { $dest &= $src; } } return call_user_func($saveFunc, $dest, $file); }
private function setConfigVar($type, $section, $subsection, $key, $value) { $sectionData = $this->getAdminData($type, $section, $subsection); $changed = false; switch ($sectionData['sectiontype']) { case 'fields': if (!isset($sectionData['fields'][$key])) { throw new Exception("Invalid key $key for $type section $section"); } $fieldData = $sectionData['fields'][$key]; break; case 'section': $fieldData = $sectionData; break; default: throw new Exception("Unable to handle $type $section. Invalid section type " . $sectionData['sectiontype']); } $config = $this->getAdminConfig($type, $fieldData['config'], ConfigFile::OPTION_CREATE_EMPTY); //remove blank values before validation if (is_array($value)) { foreach ($value as $k=>$v) { $prefix = isset($value[$k . '_prefix']) ? $value[$k . '_prefix'] : ''; if ($prefix && defined($prefix)) { $value[$k] = constant($prefix) . '/' . $v; } if (isset($value[$k . '_prefix'])) { unset($value[$k . '_prefix']); } if (isset($fieldData['fields'][$k]['omitBlankValue']) && $fieldData['fields'][$k]['omitBlankValue'] && strlen($v)==0) { $changed = $changed || $config->clearVar($key, $k); unset($value[$k]); } if ($fieldData['fields'][$k]['type']=='paragraph') { $value[$k] = explode("\n\n", str_replace(array("\r\n","\r"), array("\n","\n"), $v)); } } } if (isset($sectionData['sectionvalidatemethod'])) { $result = call_user_func($sectionData['sectionvalidatemethod'], $key, $value); if (KurogoError::isError($result)) { throw new Exception($result->getMessage()); } } if (is_array($value)) { $result = true; foreach ($value as $k=>$v) { if (preg_match("/^(.*?)_prefix$/", $k,$bits)) { continue; } if (!isset($fieldData['fields'][$k])) { throw new Exception("Invalid key $k for $type:" . $fieldData['config'] . " section $key"); } $prefix = isset($value[$k . '_prefix']) ? $value[$k . '_prefix'] : ''; if ($prefix && defined($prefix)) { $v = constant($prefix) . '/' . $v; } if (!$config->setVar($key, $k, $v, $c)) { $result = false; } $changed = $changed || $c; } } else { if (isset($fieldData['omitBlankValue']) && $fieldData['omitBlankValue'] && strlen($value)==0) { $changed = $config->clearVar($fieldData['section'], $key); } else { if ($fieldData['type']=='paragraph') { $value = explode("\n\n", str_replace(array("\r\n","\r"), array("\n","\n"), $value)); } $result = $config->setVar($fieldData['section'], $key, $value, $changed); if (!$result) { throw new Exception("Error setting $config $section $key $value"); } } } if ($changed) { if (!in_array($config, $this->changedConfigs)) { $this->changedConfigs[] = $config; } } }