Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
form_question_rule_type_file_virus_check.inc
1 <?php
18 require_once dirname(__FILE__).'/../../form_question_rule/form_question_rule.inc';
19 require_once SQ_FUDGE_PATH.'/antivirus/antivirus.inc';
20 
33 {
34 
35 
42  function __construct($assetid=0)
43  {
44  parent::__construct($assetid);
45 
46  }//end constructor
47 
48 
49 
65  function evaluate($answer, $rule_data, &$q_asset)
66  {
67  //get all data we need from either current submission or incomplete submission
68  if (!empty($_FILES['q'.$q_asset->id]['tmp_name'])) {
69  $temp_file = $_FILES['q'.$q_asset->id]['tmp_name'];
70  $answer_name = $_FILES['q'.$q_asset->id]['name'];
71  }
72  else {
73  $extra_data = $q_asset->getExtraData();
74  $temp_file = array_get_index($extra_data, 'temp_filesystem_path','');
75  $answer_name = basename($temp_file);
76  }
77 
78  // if no file uploaded, no need to scan virus
79  if ((empty($answer_name) || $answer_name == 'No file uploaded') && !isset($temp_file)) return TRUE;
80 
81  // if disabled, no need to proceed
82  if(isset($rule_data['check_virus']) && $rule_data['check_virus'] == 2) return TRUE;
83 
84  $report;
85  $status = Antivirus::scan_file($temp_file,$report);
86  // If infected, remove and warn the user
87  if (!$status) {
88  unlink($temp_file);
89  return FALSE;
90  }
91 
92  return TRUE;
93  }//end evaluate()
94 
95 
108  function generateJSCode(&$q_asset, $rule_data)
109  {
110  // Generate a customisable indicator DIV
111 
112  // if disabled
113  if(isset($rule_data['check_virus']) && $rule_data['check_virus'] == 2) return '';
114 
115  $content = isset($rule_data['indicator_content']) && !empty ($rule_data['indicator_content']) ? $rule_data['indicator_content'] : translate('core_form_rule_file_virus_check_indicator_content_default');
116  $class = isset($rule_data['indicator_class']) && !empty ($rule_data['indicator_class']) ? $rule_data['indicator_class'] : translate('core_form_rule_file_virus_check_indicator_class_default');
117  ob_start();
118  ?>
119  var newdiv = document.createElement('div');
120  var divIdName = 'file_upload_virus_check_indicator';
121  if(document.getElementById(divIdName) == null ){
122  var indicator_content = '<?php echo $content; ?>';
123  newdiv.setAttribute('id',divIdName);
124  newdiv.setAttribute('class','<?php echo $class; ?>');
125  newdiv.innerHTML = indicator_content;
126  form.appendChild(newdiv);
127  }
128  <?php
129  $contents = ob_get_contents();
130  ob_end_clean();
131  return $contents;
132 
133  }//end generateJSCode()
134 
135 
145  function defaultError(&$q_asset, $rule_data)
146  {
147  return translate('core_form_rule_file_virus_check_error_true', $q_asset->getValue(), $q_asset->attr('name'));
148 
149  }//end defaultError()
150 
151 
152 }//end class
153 ?>