Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
tool_export_assets_to_xml.inc
1 <?php
18 require_once SQ_SYSTEM_ROOT.'/core/assets/system/tool/tool.inc';
19 require_once SQ_FUDGE_PATH.'/general/file_system.inc';
20 require_once SQ_LIB_PATH.'/html_form/html_form.inc';
21 require_once SQ_DATA_PATH.'/private/conf/tools.inc';
22 
37 {
38 
39 
46  function Tool_Export_Assets_To_Xml($assetid=0)
47  {
48  $this->Tool($assetid);
49 
50  }//end constructor
51 
52 
62  public static function paintTool(&$o, $type_code)
63  {
64  // do a lock check, only allows one user at a time
65  $import_mgr = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('import_tools_manager');
66  $locks = $GLOBALS['SQ_SYSTEM']->am->getLockInfo($import_mgr->id, 'attributes', TRUE, TRUE);
67  $userid = $GLOBALS['SQ_SYSTEM']->currentUserId();
68  $userHasLockId = '';
69  $hasLock = TRUE;
70 
71  foreach ($locks as $lock) {
72  if(empty($lock)) continue;
73  if($lock['lock_type'] == 'attributes' && $lock['userid'] == $userid) {
74  $hasLock = TRUE;
75  }
76  else if ($lock['lock_type'] == 'attributes' ) {
77  $hasLock = FALSE;
78  $userHasLockId = $lock['userid'];
79  }
80  }
81 
82  if(!$hasLock) {
83  $o->openRaw();
84  echo (translate('tool_export_assets_to_xml_can_not_acquire_lock', $userHasLockId));
85  $o->closeRaw();
86  return FALSE;
87 
88  }
89 
90  $show_download = FALSE;
91  if(isset($_REQUEST['show_download'])) {
92  $show_download = TRUE;
93  }
94  else if (isset($_REQUEST['download'])) {
95  $export_dir = SQ_DATA_PATH.'/private/export_assets';
96  if (!create_directory($export_dir)) return FALSE;
97 
98  $command = "cd $export_dir ; tar -czf export.tgz * --remove-files";
99  exec($command, $output, $return);
100 
101  // exec populate $return with 0 if successful. If not successful, $return will be an exit status code and
102  // we don't have a usable compressed file for the user to download
103  if ($return !== 0) {
104  trigger_localised_error('EXP0001', E_USER_ERROR, $return);
105  return;
106  }
107 
108 
109  // if we don't have a valid filesize, there isn't much point going fruther as the user will end up downloading an unusable file
110  $filesize = filesize($export_dir.'/export.tgz');
111  if (!$filesize) {
112  trigger_localised_error('EXP0002', E_USER_ERROR);
113  return;
114  }
115 
116 
117  require_once SQ_FUDGE_PATH.'/standards_lists/mime_types.inc';
118  $ext = 'tgz';
119  $type = (empty($standards_lists_mime_types[$ext])) ? 'text/plain' : $standards_lists_mime_types[$ext];
120 
121  // Fix for IE caching
122  header("Pragma: public");
123  header("Expires: 0");
124  header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
125  header("Cache-Control: private",false);
126 
127  // We'll be outputting a file
128  header('Content-Type: '.$type);
129 
130  // Set the name of the file
131  header('Content-Disposition: attachment; filename="export.tgz";');
132  header('Content-Length: '.$filesize);
133 
134  // Get the source file
135  self::_readfile_chunked($export_dir.'/export.tgz');
136 
137  // release lock after user downloads the tarball, otherwise locks will be left to lock system to decide
138  $GLOBALS['SQ_SYSTEM']->am->releaseLock($import_mgr->id, 'attributes');
139  return;
140  @ob_flush();
141  return;
142  }
143 
144 
145  $tool_info = $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($type_code);
146  $o->openSection($tool_info['name']);
147 
148  $o->openField(translate('tool_export_assets_to_xml_root_node'));
149 
150  asset_finder($type_code.'_root_node[0]', 0, Array('asset' => 'D'), 'sq_sidenav', FALSE, 'null', Array('clear'));
151  echo '<span id ="tool_export_assets_to_xml_mapping" style="margin-left:10px;">';
152  echo translate('tool_export_assets_to_xml_target_parent');
153  text_box($type_code.'_target_parent[0]', '1', '1', '', '', 'style="margin-left:10px;"');
154  echo '</span>';
155  $map_frame_exp = trim('parent.frames["sq_sidenav"]', '.');
156  $name =$type_code.'_root_node';
157  $safe_name = 'sq_asset_finder_'.preg_replace('/[^A-Za-z0-9]/', '_', $name);
158  ?>
159  <script type="text/javascript">
160  // insert target parent text box when clicking More button
161  var target_parent_index = 1;
162  function addParentNode(node) {
163  var new_span = document.getElementById('tool_export_assets_to_xml_mapping').cloneNode(true);
164  new_span.lastChild.setAttribute('id', '<?php echo $type_code; ?>' + '_target_parent[' + target_parent_index + ']');
165  new_span.lastChild.setAttribute('name', '<?php echo $type_code; ?>' + '_target_parent[' + target_parent_index + ']');
166  node.parentNode.insertBefore(new_span, node);
167  target_parent_index = target_parent_index + 1;
168  }
169  </script>
170  <?php
171  echo '<input type="button" name="'.htmlspecialchars($safe_name.'_more_btn').'" id="'.htmlspecialchars($safe_name.'_more_btn').'" value="'.translate('more').'..." onclick="addNewAssetFinder(this, \''.$name.'\', \''.$safe_name.'\', \'\', \''.htmlentities($map_frame_exp, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET).'\', \'null\', true); addParentNode(this);" style="margin-left:10px;"/>';
172  $o->note(translate('tool_export_assets_to_xml_root_node_note'));
173  $o->closeField();
174 
175  if($show_download) {
176  $o->openField(translate('download'));
177  $backend_url = $GLOBALS['SQ_SYSTEM']->backend->getBackendUrl();
178  $download_url = $backend_url.'&backend_section=tools&tool_type_code='.$type_code.'&download=1';
179  echo '<a href="'.$download_url.'" >'.translate('download').'</a>';
180  $o->closeField();
181  }
182 
183  $o->closeSection();
184 
185 
186 
187 
188  }//end paintTool()
189 
190 
200  public static function processTool(&$o, $type_code)
201  {
202  $import_mgr = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('import_tools_manager');
203  if(!$GLOBALS['SQ_SYSTEM']->am->acquireLock($import_mgr->id, 'attributes')) {
204  trigger_localised_error('EXP0003', E_USER_ERROR);
205  return FALSE;
206  }
207 
208  // make export temp directory
209  $export_dir = SQ_DATA_PATH.'/private/export_assets';
210  if (!create_directory($export_dir)) return FALSE;
211 
212  // clean up before export
213  $command = "rm -rf $export_dir/*";
214  system($command);
215  $root_node_mapping = Array();
216  foreach ($_POST[$type_code.'_root_node'] as $index => $node) {
217  $node_id = $node['assetid'];
218  $target_parentid = $_POST[$type_code.'_target_parent'][$index];
219  if(empty($node_id) || empty($target_parentid)) continue;
220  $root_node_mapping[] = $node_id.':'.$target_parentid;
221 
222  }
223 
224  if (empty($root_node_mapping)) {
225  trigger_error ('There is no root node mapping specified');
226  return FALSE;
227  }
228  // set HIPO running_vars
229  $vars['export_path'] = $export_dir;
230  $vars['root_node_mapping'] = $root_node_mapping;
231 
232  // run HIPO job
233 
234  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
235  $hh->queueHipo('hipo_job_export_assets_to_xml', $vars, '', SQ_PACKAGES_PATH.'/import_tools/hipo_jobs');
236  $backend_url = $GLOBALS['SQ_SYSTEM']->backend->getBackendUrl();
237  // run the hipo job and redirect back to the tool and show download link
238  $url = $hh->runQueuedJobs($backend_url.'&backend_section=tools&tool_type_code='.$type_code.'&show_download=1');
239 
240  if (!empty($url)) $o->setRedirect($url);
241  return TRUE;
242 
243 
244  }//end processTool()
245 
246 
247 
258  private static function _readfile_chunked($filename, $retbytes = TRUE, $chunksize = 1048576) {
259  $buffer = '';
260  $cnt =0;
261  // $handle = fopen($filename, 'rb');
262  $handle = fopen($filename, 'rb');
263  if ($handle === false) {
264  return false;
265  }
266  while (!feof($handle)) {
267  $buffer = fread($handle, $chunksize);
268  echo $buffer;
269  ob_flush();
270  flush();
271  if ($retbytes) {
272  $cnt += strlen($buffer);
273  }
274  }
275  $status = fclose($handle);
276  if ($retbytes && $status) {
277  return $cnt; // return num. bytes delivered like readfile() does.
278  }
279  return $status;
280  } // end _readfile_chunked()
281 
282 
283 
284 }//end class
285 
286 
287 ?>