Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
xml_user_bridge.inc
1 <?php
18 require_once SQ_INCLUDE_PATH.'/asset.inc';
19 require_once SQ_CORE_PACKAGE_PATH.'/interfaces/bridge/bridge.inc';
20 require_once SQ_FUDGE_PATH.'/general/file_system.inc';
21 
22 
34 class XML_User_Bridge extends Asset implements Bridge
35 {
36 
37  public $groups = NULL;
38  public $group_members = NULL;
39  public $user_memberships = NULL;
40 
41 
50  function __construct($assetid=0)
51  {
52  $res = parent::__construct($assetid);
53  $this->loadFromFile();
54  return $res;
55 
56  }//end constructor
57 
58 
69  protected function _getName($short_name=FALSE, $contextid=NULL)
70  {
71  // No context specified, using the current context
72  if ($contextid === NULL) {
73  $contextid = $GLOBALS['SQ_SYSTEM']->getContextId();
74  }//end if
75 
76  // Obtain the attribute value for Name from the specified Context
77  $values = $GLOBALS['SQ_SYSTEM']->am->getAttributeValuesByName('name', $this->type(), Array($this->id), $contextid);
78  if (empty($values) === TRUE) {
79  return parent::_getName($short_name, $contextid);
80  } else {
81  return $values[$this->id];
82  }
83 
84  }//end _getName()
85 
86 
93  function loadFromFile()
94  {
95  #require_once 'XML/Tree.php';
96  require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
97 
98  $filename = $this->data_path.'/contents.xml';
99 
100  if (!file_exists($filename) || (filemtime($filename) < strtotime('-'.$this->attr('refresh_time').' seconds'))) {
101  @unlink($filename);
102  if ($this->attr('data_url')) {
103  $xml = @file_get_contents($this->attr('data_url'));
104  if ($xml) {
105  if (!is_dir($this->data_path)) {
106  create_directory($this->data_path);
107  }
108  string_to_file($xml, $filename);
109  }
110  }
111  }
112  if (!file_exists($filename)) return;
113 
114  $root = @simplexml_load_file($filename);
115  if (empty($root)) {
116  return;
117  }
118 
119  $this->groups = Array();
120  $this->group_members = Array();
121  $this->user_memberships = Array();
122  foreach ($root->children() as $group_node) {
123  if ($group_node->getName() != 'group') continue;
124  $groupid = (int)$group_node->attributes()->id;
125  $this->groups[$groupid] = (string)$group_node->attributes()->name;
126  foreach ($group_node->children() as $member_node) {
127  $_attributes = Array();
128  foreach ($member_node->attributes() as $k => $v) {
129  $_attributes[$k] = (string)$v;
130  }
131  $this->group_members[$groupid][((int)$member_node->attributes()->id)] = $_attributes;
132  $this->user_memberships[(int)$member_node->attributes()->id][] = $groupid;
133  }
134  }
135 
136 
137  }//end loadFromFile()
138 
139 
154  function getAsset($assetid, $type_code='', $mute_errors=FALSE)
155  {
156  $bits = explode(':', $assetid);
157  switch (count($bits)) {
158  case 1:
159  return $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid, $type_code, $mute_errors);
160  case 2:
161  $GLOBALS['SQ_SYSTEM']->am->includeAsset('user_group');
162  $shadowid = $bits[1];
163  $ug = new User_Group();
164  $ug->id = $assetid;
165  $ug->name = $this->groups[$shadowid];
166  $ug->short_name = $this->groups[$shadowid];
167  $ug->status = $this->status;
168  $ug->setAttrValue('name', $this->groups[$shadowid]);
169  return $ug;
170  case 3:
171  $groupid = $bits[1];
172  $personid = $bits[2];
173  $user_type = $this->attr('user_type');
174  $GLOBALS['SQ_SYSTEM']->am->includeAsset($user_type);
175  $user = new $user_type();
176  $user->id = $assetid;
177  $user->status = $this->status;
178  if (isset($this->group_members[$groupid][$personid])) {
179  foreach ($this->group_members[$groupid][$personid] as $field => $val) {
180  if (isset($user->vars[$field])) {
181  $user->setAttrValue($field, $val);
182  }
183  }
184  }
185  $user->name = $user->_getName();
186  $user->short_name = $user->_getName(TRUE);
187  return $user;
188  }//end switch
189 
190  }//end getAsset()
191 
192 
218  function getLinks($assetid, $link_types, $type_code='', $strict_type_code=TRUE, $side_of_link='major', $sort_by=NULL, $dependant=NULL, $exclusive=NULL)
219  {
220  // We don't have any dependant or exclusive links
221  if ($dependant || $exclusive) {
222  return Array();
223  }
224 
225  // We only have type 1 links
226  if (! $link_types && SQ_LINK_TYPE_1) {
227  return Array();
228  }
229 
230  // We only can return links for descendants of User and User Group
231  $user_type_codes = array_diff($GLOBALS['SQ_SYSTEM']->am->getTypeDescendants(Array('user', 'user_group'), TRUE), Array('root_user'));
232  if (!empty($type_code) && !in_array($type_code, $user_type_codes)) {
233  return Array();
234  }
235 
236  $bits = explode(':', $assetid);
237  switch (count($bits)) {
238  case 1:
239  if (($assetid == $this->id) && ($side_of_link == 'major')) {
240  // Get the groups under this asset
241  $sample = Array(
242  'majorid' => $this->id,
243  'link_type' => SQ_LINK_TYPE_1,
244  'major_type_code' => 'xml_user_bridge',
245  'minor_type_code' => 'user_group',
246  'value' => '',
247  'linkid' => 0,
248  'sort_order' => -1,
249  'is_dependant' => 1,
250  );
251  $res = Array();
252  foreach ($this->groups as $id => $name) {
253  $sample['minorid'] = $this->id.':'.$id;
254  $res[] = $sample;
255  }
256  return $res;
257  } else {
258  return $GLOBALS['SQ_SYSTEM']->am->getLinks($assetid, $link_types, $type_code, $strict_type_code, $side_of_link, $sort_by, $dependant, $exclusive);
259  }
260  break;
261  case 2:
262  if ($side_of_link == 'major') {
263  // Get the users under this group
264  $sample = Array(
265  'majorid' => $assetid,
266  'link_type' => SQ_LINK_TYPE_1,
267  'major_type_code' => 'user_group',
268  'minor_type_code' => $this->attr('user_type'),
269  'value' => '',
270  'linkid' => 0,
271  'sort_order' => -1,
272  'is_dependant' => 1,
273  );
274  $res = Array();
275  foreach ($this->group_members[$bits[1]] as $userid => $details) {
276  $sample['minorid'] = $assetid.':'.$userid;
277  $res[] = $sample;
278  }
279  return $res;
280 
281  } else {
282  // Just this
283  $res = Array(
284  'minorid' => $assetid,
285  'majorid' => $this->id,
286  'link_type' => SQ_LINK_TYPE_1,
287  'major_type_code' => 'xml_user_bridge',
288  'minor_type_code' => 'user_group',
289  'value' => '',
290  'linkid' => 0,
291  'sort_order' => -1,
292  'is_dependant' => 1,
293  );
294  return Array($res);
295  }
296  break;
297  case 3:
298  if ($side_of_link == 'major') {
299  return Array();
300  } else {
301  // Get the groups this user belongs to
302  $sample = Array(
303  'minorid' => $assetid,
304  'link_type' => SQ_LINK_TYPE_1,
305  'major_type_code' => 'user_group',
306  'minor_type_code' => $this->attr('user_type'),
307  'value' => '',
308  'linkid' => 0,
309  'sort_order' => -1,
310  'is_dependant' => 1,
311  );
312  $res = Array();
313  foreach ($this->user_memberships[$bits[2]] as $groupid) {
314  $sample['majorid'] = $this->id.':'.$groupid;
315  $res[] = $sample;
316  }
317  return $res;
318  }
319  break;
320  }//end switch
321 
322 
323  }//end getLinks()
324 
325 
339  function getParents($assetid, $type_code='', $strict_type_code=TRUE)
340  {
341  $bits = explode(':', $assetid);
342  $res = Array();
343  switch (count($bits)) {
344  case 1:
345  $res = $GLOBALS['SQ_SYSTEM']->am->getParents($assetid, $type_code, $strict_type_code);
346  break;
347  case 2:
348  if (empty($type_code) || ($type_code == 'xml_user_bridge') || (!$strict_type_code && ($type_code == 'bridge'))) {
349  $res = Array($this->id => 'xml_user_bridge');
350  }
351  break;
352  case 3:
353  if (empty($type_code) || ($type_code == 'user_group') || (!$strict_type_code && ($type_code == 'folder'))) {
354  foreach ($this->user_memberships[$bits[2]] as $groupid) {
355  $res[$groupid] = 'user_group';
356  }
357  }
358  break;
359  }
360  return $res;
361 
362  }//end getParents()
363 
364 
385  function getChildren($assetid, $type_code='', $strict_type_code=TRUE, $dependant=NULL, $sort_by=NULL)
386  {
387  $bits = explode(':', $assetid);
388  $res = Array();
389  switch (count($bits)) {
390  case 1:
391  if ($assetid == $this->id) {
392  foreach ($this->group_members as $groupid => $members) {
393  $res[$this->id.':'.$groupid] = Array(Array('type_code' => 'user_group'));
394  }
395  return $res;
396  } else {
397  return $GLOBALS['SQ_SYSTEM']->am->getChildren($assetid, $type_code='', $strict_type_code=TRUE, $dependant=NULL, $sort_by=NULL);
398  }
399  break;
400  case 2:
401  $res = Array();
402  foreach (array_get_index($this->group_members, $bits[1], Array()) as $memberid => $details) {
403  $res[$assetid.':'.$memberid] = Array(Array('type_code' => 'bulkmail_user'));
404  }
405  return $res;
406  break;
407  case 3:
408  return Array();
409  break;
410  }
411 
412  }//end getChildren()
413 
414 
436  function getLink($assetid, $link_type=NULL, $type_code='', $strict_type_code=TRUE, $value=NULL, $side_of_link='major', $exclusive=NULL)
437  {
438  // There are no individual links of note in this sytem
439  return NULL;
440 
441  }//end getLink()
442 
443 
455  function getLinkById($linkid, $assetid=0, $side_of_link='major')
456  {
457  return Array();
458 
459  }//end getLinkById()
460 
461 
476  function canCreateLink(&$minor, $link_type, $exclusive)
477  {
478  return FALSE;
479 
480  }//end canCreateLink()
481 
482 
497  function getAssetInfo($assetids, $type_code=Array(), $strict_type_code=TRUE, $field='')
498  {
499  assert_type($assetids, 'array');
500  if (empty($assetids)) return Array();
501  if (!is_array($type_code)) {
502  $type_code = Array($type_code);
503  }
504  $template = reset($GLOBALS['SQ_SYSTEM']->am->getAssetInfo($this->id));
505 
506  $res = Array();
507  $available_types = $GLOBALS['SQ_SYSTEM']->am->getTypeAncestors($this->attr('user_type'));
508  $available_types[] = $this->attr('user_type');
509  if (empty($type_code) || array_intersect($available_types, $type_code)) {
510  // Look through the users
511  foreach ($assetids as $assetid) {
512  $asset =& $this->getAsset($assetid, $this->attr('user_type'));
513  if (empty($field)) {
514  $entry = $template;
515  foreach ($template as $i => $v) {
516  if ($i == 'type_code') continue;
517  $entry[$i] = $asset->$i;
518  }
519  } else {
520  $entry = $asset->$field;
521  }
522  $res[$assetid] = $entry;
523  }
524  }
525 
526  if (empty($type_code) || in_array('user_group', $type_code)) {
527  // Look through the groups
528  foreach ($assetids as $id) {
529  $bits = explode(':', $id);
530  if (count($bits) == 2) {
531  $name = array_get_index($this->groups, $bits[1]);
532  if ($name) {
533  $entry = $template;
534  $entry['name'] = $name;
535  $entry['short_name'] = $name;
536  $entry['type_code'] = 'user_group';
537  if (empty($field)) {
538  $res[$id] = $entry;
539  } else {
540  $res[$id] = array_get_index($entry, $field);
541  }
542  }
543  }
544  }
545  }
546 
547  return $res;
548 
549  }//end getAssetInfo()
550 
551 
561  function assetExists($assetids)
562  {
563  if (!is_array($assetids)) {
564  $my_assetids = Array($assetids);
565  } else {
566  $my_assetids = $assetids;
567  }
568  foreach ($my_assetids as $assetid) {
569  $bits = explode(':', $assetid);
570  switch (count($bits)) {
571  case 1:
572  $res[$assetid] = ($assetid == $this->id);
573  break;
574  case 2:
575  $res[$assetid] = isset($this->groups[$bits[1]]);
576  break;
577  case 3:
578  $res[$assetid] = isset($this->groups[$bits[1]]) && isset($this->group_members[$bits[1]][$bits[2]]);
579  break;
580  }
581  }
582  return (is_array($assetids)) ? $res : $res[$assetids];
583 
584  }//end assetExists()
585 
586 
607  function getPermission($assetid, $permission, $granted=null, $and_greater=true, $expand_groups=false, $all_info=false)
608  {
609  return $GLOBALS['SQ_SYSTEM']->am->getPermission($this->id, $permission, $granted, $and_greater, $expand_groups, $all_info);
610 
611  }//end getPermission()
612 
613 
624  public function getLineageFromURL($assetid, $protocol, $url)
625  {
626  return Array();
627 
628  }//end getLineageFromURL()
629 
630 
651  public function countLinks($assetid, $side_of_link='major', $link_types=0, $type_code='', $strict_type_code=TRUE, $ignore_linkid=0)
652  {
653  return 0;
654 
655  }//end countLinks()
656 
657 
667  public function deleteAssetLink($linkid, $moving=FALSE)
668  {
669  return FALSE;
670 
671  }//end deleteAssetLink()
672 
673 
682  public function getAssetMapAssetInfo($assetid)
683  {
684  return Array();
685 
686  }//end getAssetMapAssetInfo()
687 
688 
689 }//end class
690 ?>