Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
condition_user_ip_management.inc
1 <?php
17 require_once SQ_INCLUDE_PATH.'/asset_management.inc';
18 require_once dirname(__FILE__).'/condition_user_ip_edit_fns.inc';
19 
31 {
32 
33 
41  {
42  Asset_Management::__construct($pm);
43 
44  }//end constructor
45 
46 
55  function _upgrade($current_version)
56  {
57  if (!parent::_upgrade($current_version)) return FALSE;
58 
59  $asset_name = strtoupper($this->_pm->getTypeInfo($this->getAssetType(), 'name'));
60 
61  if (version_compare($current_version, '0.2', '<')) {
62  // Need to go through all users who have user group restrictions set
63  pre_echo('STARTING UPGRADE OF '.$asset_name.' FROM VERSION '.$current_version.' TO VERSION 0.2');
64 
65  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
66  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
67 
68  // Get a list of users...
69  $users = $GLOBALS['SQ_SYSTEM']->am->getTypeAssetids('user', FALSE, TRUE);
70  pre_echo('Updating User restrictions - '.count($users).' asset(s)...');
71  foreach ($users as $assetid => $type_code) {
72  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid, $type_code);
73  if (is_null($asset)) continue;
74 
75  $restrictions = $asset->attr('restrictions');
76  foreach ($restrictions as $group_assetid => $group_restrictions) {
77  foreach ($group_restrictions as $key => $restriction) {
78  if ($restriction['name'] == $this->getAssetType()) {
79  $restriction = $this->_convertConditionData($restriction);
80 
81  if (!is_null($restriction)) {
82  $restrictions[$group_assetid][$key] = $restriction;
83  } else {
84  unset($restrictions[$group_assetid][$key]);
85  }
86  }
87  }
88  }
89 
90  $asset->setAttrValue('restrictions', $restrictions);
91 
92  if (!$asset->saveAttributes()) {
93  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
94  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
95 
96  pre_echo($asset_name.' UPGRADE FAILED - Could not save attributes');
97  return FALSE;
98  }
99 
100  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
101  unset($asset);
102 
103  }//end foreach
104 
105  // Need to go through all paint layout bodycopies who have conditional keywords set up
106  $pl_bodycopies = $GLOBALS['SQ_SYSTEM']->am->getTypeAssetids('paint_layout_bodycopy', FALSE, TRUE);
107  pre_echo('Updating Paint Layout Bodycopy conditions - '.count($pl_bodycopies).' asset(s)...');
108  foreach ($pl_bodycopies as $assetid => $type_code) {
109  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid, $type_code);
110  if (is_null($asset)) continue;
111 
112  $conditional_keywords = $asset->attr('conditional_keywords');
113  foreach ($conditional_keywords as $condition_name => $parameters) {
114  if ($parameters['type_code'] == $this->getAssetType()) {
115  $parameters['restriction'] = $this->_convertConditionData($parameters['restriction']);
116  }
117 
118  $conditional_keywords[$condition_name] = $parameters;
119  }//end foreach
120 
121  $asset->setAttrValue('conditional_keywords', $conditional_keywords);
122 
123  if (!$asset->saveAttributes()) {
124  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
125  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
126 
127  pre_echo($asset_name.' UPGRADE FAILED - Could not save attributes');
128  return FALSE;
129  }
130 
131  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
132  unset($asset);
133 
134  }//end foreach
135 
136  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
137  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
138 
139  pre_echo($asset_name.' SUCCESSFULLY UPGRADED TO VERSION 0.2');
140 
141  }//end if
142 
143  return TRUE;
144 
145  }//end _upgrade()
146 
147 
156  function _convertConditionData($restriction)
157  {
158  $condition_data = $restriction['condition_data'];
159 
160  if (isset($condition_data['user_ip'])) {
161  $ip_octets = explode('.', $condition_data['user_ip']);
162  $subnet_octets = Array(255, 255, 255, 255);
163  $valid = TRUE;
164  $non_star_found = FALSE;
165 
166  // loop through each octet, backwards
167  for ($i = 3; $i >= 0; $i--) {
168  // if a '*' is found, make sure we haven't had an actual number yet,
169  // then switch the subnet and IP octets to zero
170  if ($ip_octets[$i] === '*') {
171  if ($non_star_found) {
172  trigger_error('Cannot upgrade IP condition "'.$condition_data['user_ip'].' as it cannot be represented in a IP/subnet format - ignoring this condition', E_USER_NOTICE);
173  $valid = FALSE;
174  break;
175  } else {
176  $subnet_octets[$i] = '0';
177  $ip_octets[$i] = '0';
178  }
179  } else {
180  $non_star_found = TRUE;
181  }
182  }
183 
184  // is it valid?
185  if ($valid) {
186  $match = $restriction['match'];
187  $condition_data['user_ip_ranges'] = Array(
188  Array(
189  'ip_address' => implode('.', $ip_octets),
190  'subnet' => implode('.', $subnet_octets),
191  'grant' => $match,
192  ),
193  );
194  $condition_data['default_grant'] = 1 - $match;
195  $restriction['match'] = TRUE;
196  unset($condition_data['user_ip']);
197  $restriction['condition_data'] = $condition_data;
198  } else {
199  $restriction = NULL;
200  }
201 
202  }//end if
203 
204  return $restriction;
205 
206  }//end _convertConditionData()
207 
208 
209 }//end class
210 
211 ?>