Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
system_integrity_incomplete_attachments.php
1 <?php
28 error_reporting(E_ALL);
29 if ((php_sapi_name() != 'cli')) trigger_error("You can only run this script from the command line\n", E_USER_ERROR);
30 
31 $SYSTEM_ROOT = (isset($_SERVER['argv'][1])) ? $_SERVER['argv'][1] : '';
32 if (empty($SYSTEM_ROOT)) {
33  echo "ERROR: You need to supply the path to the System Root as the first argument\n";
34  exit();
35 }
36 
37 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.'/core/include/init.inc')) {
38  echo "ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
39  exit();
40 }
41 
42 $ACTION = (isset($_SERVER['argv'][2])) ? $_SERVER['argv'][2] : '';
43 $ACTION = ltrim($ACTION, '-');
44 if (empty($ACTION) || ($ACTION != 'fix' && $ACTION != 'check')) {
45  echo "ERROR: No action specified\n";
46  exit();
47 }//end if
48 
49 // Define what the script will do later
50 $CORRECT = FALSE;
51 if ($ACTION == 'fix') {
52  $CORRECT = TRUE;
53 }//end if
54 
55 require_once $SYSTEM_ROOT.'/core/include/init.inc';
56 if (ini_get('memory_limit') != '-1') ini_set('memory_limit', '-1');
57 
58 $count = 0;
59 $count_rm = 0;
60 $count_2 = 0;
61 $count_rm_2 = 0;
62 
63 $form_assetids = $GLOBALS['SQ_SYSTEM']->am->getTypeAssetIds('form', FALSE);
64 foreach ($form_assetids as $assetid) {
65  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
66  $complete_link = $GLOBALS['SQ_SYSTEM']->am->getLink($assetid, SQ_LINK_TYPE_2, 'folder', TRUE, 'submissions_folder');
67  $path = $asset->data_path;
68  $path .= '/incomplete_attachments';
69  $files = list_dirs($path);
70  foreach ($files as $file) {
71  if ((strpos($file, 's') === 0) && (assert_valid_assetid(substr($file, 1), '', TRUE, FALSE))) {
72  // This is an incomplete submission, so check if the submission is still valid
73  $incomplete_submission_assetid = substr($file, 1);
74  $incomplete_submission = $GLOBALS['SQ_SYSTEM']->am->getAsset($incomplete_submission_assetid, '', TRUE);
75  if (is_null($incomplete_submission)) {
76  // Report only
77  echo 'Form #'.$assetid.' still has some incomplete attachment directories for non-existent Submission #'.$incomplete_submission_assetid.'.';
78  $count++;
79  if ($CORRECT) {
80  // Remove the dir, not needed
81  echo ' Removing.';
82  delete_directory($path.'/'.$file);
83  $count_rm++;
84  echo ' Done.';
85  }//end if
86  echo "\n";
87  } else {
88  $parents = $GLOBALS['SQ_SYSTEM']->am->getParents($incomplete_submission_assetid, 'folder', TRUE, NULL, NULL, TRUE, 1, 1);
89  if (!empty($complete_link) && !empty($parents) && array_key_exists($complete_link['minorid'], $parents)){
90  echo 'Form #'.$assetid.' still has some incomplete attachment directories for completed Submission #'.$incomplete_submission_assetid.'.';
91  $count_2++;
92  if ($CORRECT) {
93  // Remove the dir, not needed
94  echo ' Removing.';
95  delete_directory($path.'/'.$file);
96  $count_rm_2++;
97  echo ' Done.';
98  }//end if
99  echo "\n";
100  }
101  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($incomplete_submission, TRUE);
102  }//end if
103  unset($incomplete_submission);
104  }//end if
105  }//end foreach
106  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset, TRUE);
107  unset($asset);
108 }//end foreach
109 
110 echo "Incomplete attachment directories for non-existent submissions found: ".$count."\n";
111 echo "Incomplete attachment directories for non-existent submissions deleted: ".$count_rm."\n";
112 echo "Incomplete attachment directories for completed submissions found: ".$count_2."\n";
113 echo "Incomplete attachment directories for completed submissions deleted: ".$count_rm_2."\n";
114 echo "All done!\n";
115 ?>