Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
system_move_update.php
1 <?php
24 error_reporting(E_ALL);
25 $SYSTEM_ROOT = '';
26 // from cmd line
27 if ((php_sapi_name() == 'cli')) {
28  if (isset($_SERVER['argv'][1])) $SYSTEM_ROOT = $_SERVER['argv'][1];
29  $err_msg = "You need to supply the path to the System Root as the first argument\n";
30 
31 } else {
32  if (isset($_GET['SYSTEM_ROOT'])) $SYSTEM_ROOT = $_GET['SYSTEM_ROOT'];
33  $err_msg = '
34  <div style="background-color: red; color: white; font-weight: bold;">
35  You need to supply the path to the System Root as a query string variable called SYSTEM_ROOT
36  </div>
37  ';
38 }
39 
40 if (empty($SYSTEM_ROOT)) {
41  echo $err_msg;
42  exit();
43 }
44 
45 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.'/core/include/init.inc')) {
46  echo "ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
47  exit();
48 }
49 
50 // simple fn to print a prompt and return what the user enters
51 function get_line($prompt='')
52 {
53  echo $prompt;
54  // now get their entry and remove the trailing new line
55  return rtrim(fgets(STDIN, 4094));
56 
57 }//end get_line()
58 
59 
60 require_once $SYSTEM_ROOT.'/core/include/init.inc';
61 
62 if ((php_sapi_name() == 'cli')) {
63  if (isset($_SERVER['argv'][2])) {
64  $old_system_root = rtrim(trim($_SERVER['argv'][2]), '/');
65  if (strtolower(get_line('Confirm "'.$old_system_root.'" (Y/N) : ')) != 'y') {
66  unset($old_system_root);
67  }
68  }
69 }
70 
71 if (!isset($old_system_root)) {
72  do {
73  $old_system_root = get_line('Enter the old System Root : ');
74  } while (strtolower(get_line('Confirm "'.$old_system_root.'" (Y/N) : ')) != 'y');
75 }
76 
77 //$new_system_root = SQ_SYSTEM_ROOT;
78 // use user entered path (symbolic link friendly)
79 $new_system_root = $SYSTEM_ROOT;
80 
81 require_once SQ_FUDGE_PATH.'/general/file_system.inc';
82 function recurse_find_ffv_files($dir, $old_rep_root, $new_rep_root)
83 {
84  $d = dir($dir);
85  while (false !== ($entry = $d->read())) {
86  if ($entry == '.' || $entry == '..') continue;
87 
88  // if this is a directory
89  if (is_dir($dir.'/'.$entry)) {
90  // we have found a .FFV dir
91  if ($entry == '.FFV') {
92 
93  $ffv_dir = $dir.'/'.$entry;
94  $ffv_d = dir($ffv_dir);
95  while (false !== ($ffv_entry = $ffv_d->read())) {
96  if ($ffv_entry == '.' || $ffv_entry == '..') continue;
97 
98  // if this is a directory
99  if (is_file($ffv_dir.'/'.$ffv_entry)) {
100  $ffv_file = $ffv_dir.'/'.$ffv_entry;
101  $str = file_to_string($ffv_file);
102  if ($str) {
103  $str = str_replace('dir="'.$old_rep_root.'"', 'dir="'.$new_rep_root.'"', $str);
104  echo "File : $ffv_file\n";
105  #pre_echo("FILE : $ffv_file\n CONTENTS : \n$str");
106  string_to_file($str, $ffv_file);
107  }
108  }
109  }//end while
110  $ffv_d->close();
111 
112  // just a normal dir, recurse
113  } else {
114  recurse_find_ffv_files($dir.'/'.$entry, $old_rep_root, $new_rep_root);
115 
116  }//end if
117  }//end if
118  }//end while
119  $d->close();
120 
121 }//end recurse_find_ffv_files()
122 
123 
124 $old_rep_path = preg_replace('|/+$|', '', $old_system_root).'/data/file_repository';
125 $new_rep_path = preg_replace('|/+$|', '', $new_system_root).'/data/file_repository';
126 
127 pre_echo("OLD : $old_rep_path\nNEW : $new_rep_path");
128 
129 recurse_find_ffv_files(SQ_DATA_PATH.'/private', $old_rep_path, $new_rep_path);
130 recurse_find_ffv_files(SQ_DATA_PATH.'/public', $old_rep_path, $new_rep_path);
131 
132 $old_data_private_path = preg_replace('|/+$|', '', $old_system_root).'/data/';
133 $new_data_private_path = preg_replace('|/+$|', '', $new_system_root).'/data/';
134 
135 recurse_data_dir_for_safe_edit_files(SQ_DATA_PATH.'/private', $old_data_private_path, $new_data_private_path);
136 recurse_data_dir_for_safe_edit_files(SQ_DATA_PATH.'/public', $old_data_private_path, $new_data_private_path);
137 
138 //Bug #4560 Fix to take care of form_submission file paths stored in its attributes.
139 echo "Updating Form Submission file paths\n";
140 $new_root = preg_replace('|/+$|', '', $new_system_root);
141 $old_root = preg_replace('|/+$|', '', $old_system_root);
142 pre_echo("OLD : $old_root\nNEW : $new_root");
143 update_form_submission_filepaths($old_root, $new_root);
144 
145 function recurse_data_dir_for_safe_edit_files($dir, $old_rep_root, $new_rep_root)
146 {
147  $d = dir($dir);
148  $index_to_look = Array ('data_path', 'data_path_public');
149  while (false !== ($entry = $d->read())) {
150  if ($entry == '.' || $entry == '..') continue;
151  // if this is a directory
152  if (is_dir($dir.'/'.$entry)) {
153  // we have found a .sq_system dir
154  if ($entry == '.sq_system') {
155  $sq_system_dir = $dir.'/'.$entry;
156  $sq_system_d = dir($sq_system_dir);
157  while (false !== ($sq_system_entry = $sq_system_d->read())) {
158  if ($sq_system_entry == '.' || $sq_system_entry == '..' || $sq_system_entry != ".object_data") continue;
159  // if this is a directory
160  if (is_file($sq_system_dir.'/'.$sq_system_entry)) {
161  $sq_system_file = $sq_system_dir.'/'.$sq_system_entry;
162  $str = file_to_string($sq_system_file);
163  if ($str) {
164  echo "File : $sq_system_file\n";
165  preg_match ("/\"[A-Za-z_0-9]+\"/" ,$str , $asset_type);
166  $GLOBALS['SQ_SYSTEM']->am->includeAsset(str_replace('"', '', $asset_type[0]));
167  $content_array = unserialize($str);
168  foreach ($index_to_look as $value) {
169  $content_array->$value = str_replace($old_rep_root, $new_rep_root, $content_array->$value);
170  }
171  $str = serialize($content_array);
172  string_to_file($str, $sq_system_file);
173  }
174  }
175  }//end while
176  $sq_system_d->close();
177  // just a normal dir, recurse
178  } else {
179  recurse_data_dir_for_safe_edit_files($dir.'/'.$entry, $old_rep_root, $new_rep_root);
180 
181  }//end if
182  }//end if
183  }//end while
184  $d->close();
185 
186 }// end recurse_data_dir_for_safe_edit_files()
187 
188 function update_form_submission_filepaths($old_root, $new_root){
189 
190  $root_user =& $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_user');
191  $GLOBALS['SQ_SYSTEM']->setCurrentUser($root_user);
192  $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
193 
194  $children = $GLOBALS['SQ_SYSTEM']->am->getChildren(1, 'form_submission');
195  foreach (array_keys($children) as $child_id) {
196  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($child_id);
197  $data = $asset->attr('attributes');
198  if (isset($data['answers'])) {
199  foreach (array_keys($data['answers']) as $question_id) {
200  $extra_data = $asset->getExtraData($question_id);
201  if (!empty($extra_data['temp_filesystem_path'])) {
202  $path = $extra_data['temp_filesystem_path'];
203  $new_path = preg_replace("%$old_root%", $new_root, $path, 1);
204  if ($new_path != NULL){
205  if (strcmp($new_path, $path) != 0) {
206  $extra_data['temp_filesystem_path'] = $new_path;
207  if ($asset->setExtraData($question_id, $extra_data)){
208  $asset->saveAttributes();
209  echo "Updated Form Submission ID: $asset->id\n";
210  }
211  }
212  } else {
213  echo "Failed to update Form Submission ID: $asset->id\n";
214  }
215  }
216  if (!empty($extra_data['filesystem_path'])) {
217  $path = $extra_data['filesystem_path'];
218  $new_path = preg_replace("%$old_root%", $new_root, $path, 1);
219  if ($new_path != NULL){
220  if (strcmp($new_path, $path) != 0) {
221  $extra_data['filesystem_path'] = $new_path;
222  if ($asset->setExtraData($question_id, $extra_data)){
223  $asset->saveAttributes();
224  echo "Updated Form Submission ID: $asset->id\n";
225  }
226  }
227  } else {
228  echo "Failed to update Form Submission ID: $asset->id\n";
229  }
230  }
231  }
232  }
233  }
234 
235  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
236  $GLOBALS['SQ_SYSTEM']->restoreCurrentUser();
237 }
238 
239 
240 ?>