Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
upload_image.php
1 <?php
25 require_once dirname(__FILE__).'/../../../../core/include/init.inc';
26 require_once dirname(__FILE__).'/../../../../core/assets/files/image/image.inc';
27 
28 if (empty($GLOBALS['SQ_SYSTEM']->user) || !($GLOBALS['SQ_SYSTEM']->user->canAccessBackend() || $GLOBALS['SQ_SYSTEM']->user->type() == 'simple_edit_user')) {
29  echo return_javascript_error('You cannot upload file as a non-backend user');
30  exit;
31 }
32 
33 // verify nonce secuirty token to make sure the user submitting the request is using Matrix's backend interface
34  if(!isset($_POST['token'])) {
35  trigger_error('Security token not found');
36  exit;
37  }
38  $token = get_unique_token();
39  if($_POST['token'] !== $token) {
40  trigger_error('Invalid secuirty token');
41  exit;
42  }
43 
44 // Check if something was submitted
45 if (!isset($_FILES['create_image_upload']['name']) || !isset($_FILES['create_image_upload']['tmp_name']) || empty($_FILES['create_image_upload']['tmp_name']) || !isset($_FILES['create_image_upload']['error']) || !empty($_FILES['create_image_upload']['error'])) {
46  // No file submitted
47  echo return_javascript_error('No file submitted');
48  exit;
49 } else if (!isset($_POST['f_create_root_node']['assetid']) || empty($_POST['f_create_root_node']['assetid'])) {
50  // No root node specified
51  echo return_javascript_error('No root node selected');
52  exit;
53 }//end if
54 
55 // OK, hopefully I have the right information, let's continue and try and create the asset
56 $am = $GLOBALS['SQ_SYSTEM']->am;
57 $root_node = $am->getAsset($_POST['f_create_root_node']['assetid']);
58 $successful = FALSE;
59 $success_return = '';
60 if (!is_null($root_node)) {
61  $new_image = new Image();
62 
63  // Prepare the image for uploading
64  $new_image->_tmp['uploading_file'] = TRUE;
65  $_FILES['create_image_upload']['filename'] = $_FILES['create_image_upload']['name'];
66  $_FILES['create_image_upload']['path'] = $_FILES['create_image_upload']['tmp_name'];
67 
68  // Check for valid file types
69  $invalid_file_type = $new_image->validFile($_FILES['create_image_upload']);
70  if (!$invalid_file_type) {
71  echo return_upload_error('File extension not allowed. [CORE0106]');
72  exit();
73  }//end if
74 
75  // Create the image
76  $new_image->setAttrValue('name', $_FILES['create_image_upload']['name']);
77  $new_image->saveAttributes();
78  $link = Array(
79  'asset' => $root_node,
80  'link_type' => SQ_LINK_TYPE_1,
81  'value' => '',
82  'sort_order' => -1,
83  );
84  $successful = $new_image->create($link, $_FILES['create_image_upload']);
85  if ($successful) {
86  // If image creation successful, update the form and close this dialog
87  ob_start();
88  ?>
89  <html><head>
90  <script type="text/javascript">
91  top.frames['sq_wysiwyg_popup_main'].toggleCreateImage();
92  top.frames['sq_wysiwyg_popup_main'].document.getElementById('sq_asset_finder_f_imageid_assetid').value = "<?php echo $new_image->id; ?>";
93  top.frames['sq_wysiwyg_popup_main'].document.getElementById('sq_asset_finder_f_imageid_label').value = "<?php echo $new_image->short_name; ?>";
94  top.frames['sq_wysiwyg_popup_main'].document.getElementById('f_imageid[assetid]').value = "<?php echo $new_image->id; ?>";
95  top.frames['sq_wysiwyg_popup_main'].setImageInfo();
96  </script>
97  </head>
98  <body></body>
99  </html>
100  <?php
101  $success_return = ob_get_contents();
102  ob_end_clean();
103 
104  echo $success_return;
105  exit();
106  } else {
107  echo return_upload_error('Unable to create file, web path already exists[CORE0086] or file is infected[CORE0300]');
108  exit();
109  }//end if
110 } else {
111  echo return_upload_error('Invalid root node');
112  exit();
113 }//end if
114 
115 // We get to here than an ERROR occurred, so respond with a generic error
116 echo return_upload_error('Could not create image asset');
117 
118 
125 function return_javascript_error($error='') {
126  $return_code = '';
127  if (!empty($error)) {
128  ob_start();
129  ?>
130  <html><head>
131  <script type="text/javascript">
132  alert('<?php echo $error; ?>');
133  </script>
134  </head>
135  <body></body>
136  </html>
137  <?php
138  $return_code = ob_get_contents();
139  ob_end_clean();
140  }//end if
141 
142  return $return_code;
143 
144 }//end return_javascript_error()
145 
146 
153 function return_upload_error($error='') {
154  $return_code = '';
155  if (empty($error)) {
156  $error = 'Service unavailable, could not upload image';
157  }//end if
158  ob_start();
159  ?>
160  <html><head>
161  <script type="text/javascript">
162  top.frames['sq_wysiwyg_popup_main'].document.getElementById('show_upload_error').style.display = "block";
163  top.frames['sq_wysiwyg_popup_main'].document.getElementById('show_upload_error').style.visibility = "visible";
164  top.frames['sq_wysiwyg_popup_main'].document.getElementById('show_upload_error').innerHTML = "<?php echo $error ?>";
165  </script>
166  </head>
167  <body></body>
168  </html>
169  <?php
170  $return_code = ob_get_contents();
171  ob_end_clean();
172 
173  return $return_code;
174 
175 }//end return_upload_error()
176 
177 
178 ?>