Elgg  Version master
CropIcon.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Entity;
4 
5 use Symfony\Component\HttpFoundation\File\UploadedFile;
6 
12 class CropIcon {
13 
21  public function __invoke(\Elgg\Event $event) {
22 
23  $crop_input = (array) get_input('_entity_edit_icon_crop');
24  if (empty($crop_input)) {
25  // BC fallback
26  $entity_guid = (int) get_input('_entity_edit_icon_crop_guid');
27  $input_name = (string) get_input('_entity_edit_icon_crop_input');
28  $icon_type = (string) get_input('_entity_edit_icon_crop_type');
29 
31  // not enough information
32  return;
33  }
34 
35  $crop_input[$input_name] = [
36  'guid' => $entity_guid,
37  'type' => $icon_type,
38  ];
39  }
40 
41  foreach ($crop_input as $input_name => $info) {
42  $entity_guid = (int) elgg_extract('guid', $info);
43  $icon_type = (string) elgg_extract('type', $info);
44 
46  continue;
47  }
48 
50  }
51  }
52 
62  protected function prepareUpload(int $entity_guid, string $input_name, string $icon_type): void {
63  if (get_input("{$input_name}_remove") || elgg_get_uploaded_file($input_name)) {
64  // user wanted to remove icon, or provided own icon
65  return;
66  }
67 
69  if (!$entity instanceof \ElggEntity || !$entity->hasIcon('master', $icon_type)) {
70  // entity doesn't have an icon
71  return;
72  }
73 
74  $current = $entity->getIconCoordinates($icon_type);
75 
76  $input_cropping_coords = [
77  'x1' => (int) get_input("{$input_name}_x1", get_input('x1')),
78  'y1' => (int) get_input("{$input_name}_y1", get_input('y1')),
79  'x2' => (int) get_input("{$input_name}_x2", get_input('x2')),
80  'y2' => (int) get_input("{$input_name}_y2", get_input('y2')),
81  ];
82 
83  $diff = array_diff_assoc($input_cropping_coords, $current);
84  if (empty($diff)) {
85  // no new cropping data
86  return;
87  }
88 
89  // get master image to fake an image upload with
90  $master = $entity->getIcon('master', $icon_type);
91 
92  // copy master to temp location
93  $tmp_file = new \ElggTempFile();
94  $tmp_file->open('write');
95  $tmp_file->write($master->grabFile());
96  $tmp_file->close();
97 
98  $file = [
99  'name' => basename($master->getFilenameOnFilestore()),
100  'type' => $master->getMimeType(),
101  'size' => $master->getSize(),
102  'tmp_name' => $tmp_file->getFilenameOnFilestore(),
103  'error' => UPLOAD_ERR_OK,
104  ];
105 
106  // store the 'new' file in PHP global
107  $_FILES[$input_name] = $file;
108 
109  // store the 'new' file in Elgg request filebag
110  $uploaded_file = $this->arrayToUploadedFile($file);
111 
112  $filebag = _elgg_services()->request->files;
113  $filebag->set($input_name, $uploaded_file);
114  }
115 
123  protected function arrayToUploadedFile($file_data) {
124 
125  if (!is_array($file_data)) {
126  return false;
127  }
128 
129  $req_fields = ['error', 'name', 'size', 'tmp_name', 'type'];
130  $keys = array_keys($file_data);
131  sort($keys);
132 
133  if ($keys !== $req_fields) {
134  return false;
135  }
136 
137  return new UploadedFile(
138  $file_data['tmp_name'],
139  $file_data['name'],
140  $file_data['type'],
141  $file_data['error'],
142  true
143  );
144  }
145 }
$entity
Definition: reset.php:8
$entity_guid
Action for adding and editing comments.
Definition: save.php:6
Generic action listener to support cropping an existing icon.
Definition: CropIcon.php:12
prepareUpload(int $entity_guid, string $input_name, string $icon_type)
Prepare a single existing icon for cropping.
Definition: CropIcon.php:62
arrayToUploadedFile($file_data)
convert $_FILES array to UploadedFile
Definition: CropIcon.php:123
__invoke(\Elgg\Event $event)
Set inputs required to support cropping an existing icon.
Definition: CropIcon.php:21
Models an event passed to event handlers.
Definition: Event.php:11
$input_name
Definition: crop.php:24
$icon_type
Definition: crop.php:23
_elgg_services()
Get the global service provider.
Definition: elgglib.php:353
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:256
get_input(string $variable, $default=null, bool $filter_result=true)
Parameter input functions.
Definition: input.php:20
elgg_is_empty($value)
Check if a value isn't empty, but allow 0 and '0'.
Definition: input.php:176
get_entity(int $guid)
Loads and returns an entity object from a guid.
Definition: entities.php:70
elgg_get_uploaded_file(string $input_name, bool $check_for_validity=true)
Returns a single valid uploaded file object.
Definition: filestore.php:156
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.
Definition: robots.php:10
$keys
Definition: access.php:31