summaryrefslogtreecommitdiff
path: root/plugins/OStatus/extlib/hkit/hcard.profile.php
blob: 6ec0dc89066de1d9b8ec8df59041d28159571f68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
	// hcard profile for hkit
	
	$this->root_class = 'vcard';
	
	$this->classes = array( 
		'fn', array('honorific-prefix', 'given-name', 'additional-name', 'family-name', 'honorific-suffix'),
		'n', array('honorific-prefix', 'given-name', 'additional-name', 'family-name', 'honorific-suffix'),
		'adr', array('post-office-box', 'extended-address', 'street-address', 'postal-code', 'country-name', 'type', 'region', 'locality'),
		'label', 'bday', 'agent', 'nickname', 'photo', 'class', 
		'email', array('type', 'value'), 
		'category', 'key', 'logo', 'mailer', 'note',
		'org', array('organization-name', 'organization-unit'),
		'tel', array('type', 'value'),
		'geo', array('latitude', 'longitude'),
		'tz', 'uid', 'url', 'rev', 'role', 'sort-string', 'sound', 'title'		
	);
	
	// classes that must only appear once per card
	$this->singles = array(
		'fn'
	);
	
	// classes that are required (not strictly enforced - give at least one!)
	$this->required = array(
		'fn'
	);

	$this->att_map = array(
		'fn'	=> array('IMG|alt'),
		'url'	=> array('A|href', 'IMG|src', 'AREA|href'),
		'photo'	=> array('IMG|src'),
		'bday'	=> array('ABBR|title'),
		'logo'	=> array('IMG|src'),
		'email' => array('A|href'),
		'geo'	=> array('ABBR|title')
	);

	
	$this->callbacks = array(
		'url'	=> array($this, 'resolvePath'),
		'photo'	=> array($this, 'resolvePath'),
		'logo'	=> array($this, 'resolvePath'),
		'email'	=> array($this, 'resolveEmail')
	);



	function hKit_hcard_post($a)
	{
		
		foreach ($a as &$vcard){
			
			hKit_implied_n_optimization($vcard);
			hKit_implied_n_from_fn($vcard);
			
		}
		
		return $a;
	
	}
	
	
	function hKit_implied_n_optimization(&$vcard)
	{
		if (array_key_exists('fn', $vcard) && !is_array($vcard['fn']) && 
			!array_key_exists('n', $vcard) && (!array_key_exists('org', $vcard) || $vcard['fn'] != $vcard['org'])){
			
			if (sizeof(explode(' ', $vcard['fn'])) == 2){
				$patterns	= array();
				$patterns[] = array('/^(\S+),\s*(\S{1})$/', 2, 1); 		// Lastname, Initial
				$patterns[] = array('/^(\S+)\s*(\S{1})\.*$/', 2, 1); 	// Lastname Initial(.)
				$patterns[] = array('/^(\S+),\s*(\S+)$/', 2, 1); 		// Lastname, Firstname
				$patterns[] = array('/^(\S+)\s*(\S+)$/', 1, 2); 		// Firstname Lastname
			
				foreach ($patterns as $pattern){
					if (preg_match($pattern[0], $vcard['fn'], $matches) === 1){
						$n					= array();
						$n['given-name']	= $matches[$pattern[1]];
						$n['family-name']	= $matches[$pattern[2]];
						$vcard['n']			= $n;
						
						
						break;
					}
				}
			}
		}
	}
	
	
	function hKit_implied_n_from_fn(&$vcard)
	{
		if (array_key_exists('fn', $vcard) && is_array($vcard['fn']) 
			&& !array_key_exists('n', $vcard) && (!array_key_exists('org', $vcard) || $vcard['fn'] != $vcard['org'])){
				
			$vcard['n']		= $vcard['fn'];
		}

		if (array_key_exists('fn', $vcard) && is_array($vcard['fn'])){
			$vcard['fn']	= $vcard['fn']['text'];
		}
	}

?>