File: foafprofilesPlugin.class.php

package info (click to toggle)
fusionforge 5.3.2%2B20141104-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 60,472 kB
  • sloc: php: 271,846; sql: 36,817; python: 14,575; perl: 6,406; sh: 5,980; xml: 4,294; pascal: 1,411; makefile: 911; cpp: 52; awk: 27
file content (197 lines) | stat: -rw-r--r-- 6,975 bytes parent folder | download | duplicates (3)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php

/**
 * foafprofilesPlugin Class
 *
 * Copyright 2011, Olivier Berger & Institut Telecom
 *
 * This program was developped in the frame of the COCLICO project
 * (http://www.coclico-project.org/) with financial support of the Paris
 * Region council.
 *
 * This file is part of FusionForge. FusionForge is free software;
 * you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the Licence, or (at your option)
 * any later version.
 *
 * FusionForge is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

require_once 'common/include/rdfutils.php';

class foafprofilesPlugin extends Plugin {
	public function __construct($id=0) {
		$this->Plugin($id) ;
		$this->name = "foafprofiles";
		$this->text = "User FOAF Profiles"; // To show in the tabs, use...
		$this->_addHook("script_accepted_types");
		$this->_addHook("content_negociated_user_home");

	}

	/**
	 * Declares itself as accepting RDF XML on /users
	 * @param unknown_type $params
	 */
	function script_accepted_types (&$params) {
		$script = $params['script'];
		if ($script == 'user_home') {
			$params['accepted_types'][] = 'application/rdf+xml';
		}
	}

	/**
	 * Outputs user's FOAF profile
	 * @param unknown_type $params
	 */
	function content_negociated_user_home (&$params) {
		$username = $params['username'];
		$accept = $params['accept'];

		if($accept == 'application/rdf+xml') {
				$params['content_type'] = 'application/rdf+xml';

				$user_obj = user_get_object_by_name($username);

				$user_real_name = $user_obj->getRealName();
				$user_email = $user_obj->getEmail();
				$mbox = 'mailto:'.$user_email;
				$mbox_sha1sum = sha1($mbox);

				$projects = $user_obj->getGroups() ;
				sortProjectList($projects) ;
				$roles = RBACEngine::getInstance()->getAvailableRolesForUser($user_obj) ;
				sortRoleList($roles) ;

				// Construct an ARC2_Resource containing the project's RDF (DOAP) description
				$ns = array(
						'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
						'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
						'foaf' => 'http://xmlns.com/foaf/0.1/',
						'sioc' => 'http://rdfs.org/sioc/ns#',
						'doap' => 'http://usefulinc.com/ns/doap#',
						'dcterms' => 'http://purl.org/dc/terms/',
						'planetforge' => 'http://coclico-project.org/ontology/planetforge#'
				);

				$conf = array(
						'ns' => $ns
				);

				// First, let's deal with the account
				$account_res = ARC2::getResource($conf);
				$account_uri = util_make_url_u($username, $user_obj->getID());
				$account_uri = rtrim($account_uri,'/');
				$person_uri = $account_uri . '#person';

				$account_res->setURI( $account_uri );
				// $account_res->setRel('rdf:type', 'foaf:OnlineAccount');
				rdfutils_setPropToUri($account_res, 'rdf:type', 'foaf:OnlineAccount');
				rdfutils_setPropToUri($account_res, 'foaf:accountServiceHomepage', $account_uri . '/');
				$account_res->setProp('foaf:accountName', $username);
				rdfutils_setPropToUri($account_res, 'sioc:account_of', $person_uri);
				rdfutils_setPropToUri($account_res, 'foaf:accountProfilePage', $account_uri);

				$groups_index = array();
				$projects_index = array();
				$roles_index = array();

				$usergroups_uris = array();
				// see if there were any groups
				if (count($projects) >= 1) {
					foreach ($projects as $p) {
						// TODO : report also private projects if authenticated, for instance through OAuth
						if($p->isPublic()) {
							$project_link = util_make_link_g ($p->getUnixName(),$p->getID(),$p->getPublicName());
							$project_uri = util_make_url_g ($p->getUnixName(),$p->getID());
							// sioc:UserGroups for all members of a project are named after /projects/A_PROJECT/members/
							$usergroup_uri = $project_uri .'members/';

							$role_names = array();

							$usergroups_uris[] = $usergroup_uri;

							$usergroup_res = ARC2::getResource($conf);
							$usergroup_res->setURI( $usergroup_uri );
							rdfutils_setPropToUri($usergroup_res, 'rdf:type', 'sioc:UserGroup');
							rdfutils_setPropToUri($usergroup_res, 'sioc:usergroup_of', $project_uri);

							$roles_uris = array();
							foreach ($roles as $r) {
								if ($r instanceof RoleExplicit
										&& $r->getHomeProject() != NULL
										&& $r->getHomeProject()->getID() == $p->getID()) {
									$role_names[$r->getID()] = $r->getName() ;
									$role_uri = $project_uri .'roles/'.$r->getID();

									$roles_uris[] = $role_uri;
								}
							}
							rdfutils_setPropToUri($usergroup_res, 'planetforge:group_has_function', $roles_uris);

							$project_res = ARC2::getResource($conf);
							$project_res->setURI( $project_uri );
							rdfutils_setPropToUri($project_res, 'rdf:type', 'planetforge:ForgeProject');
							$project_res->setProp('doap:name', $p->getUnixName());

							$projects_index = ARC2::getMergedIndex($projects_index, $project_res->index);


							foreach ($role_names as $id => $name) {
								$role_res = ARC2::getResource($conf);
								$role_res->setURI( $project_uri .'roles/'.$id );
								rdfutils_setPropToUri($role_res, 'rdf:type', 'sioc:Role');
								$role_res->setProp('sioc:name', $name);

								$roles_index = ARC2::getMergedIndex($roles_index, $role_res->index);
							}

							$groups_index = ARC2::getMergedIndex($groups_index, $usergroup_res->index);

						}
					}
				} // end if groups
				rdfutils_setPropToUri($account_res, 'sioc:member_of', $usergroups_uris);

				// next, deal with the person
				$person_res = ARC2::getResource($conf);

				$person_res->setURI( $person_uri );
				rdfutils_setPropToUri($person_res, 'rdf:type', 'foaf:Person');
				$person_res->setProp('foaf:name', $user_real_name);
				rdfutils_setPropToUri($person_res, 'foaf:holdsAccount', $account_uri);
				$person_res->setProp('foaf:mbox_sha1sum', $mbox_sha1sum);

				// merge the two sets of triples
				$merged_index = ARC2::getMergedIndex($account_res->index, $person_res->index);
				$merged_index = ARC2::getMergedIndex($merged_index, $groups_index);
				$merged_index = ARC2::getMergedIndex($merged_index, $projects_index);
				$merged_index = ARC2::getMergedIndex($merged_index, $roles_index);

    			$conf = array(
    					'ns' => $ns,
    					'serializer_type_nodes' => true
    			);

    			$ser = ARC2::getRDFXMLSerializer($conf);

    			/* Serialize a resource index */
    			$doc = $ser->getSerializedIndex($merged_index);

    			$params['content'] = $doc . "\n";
		}
	}
}

// Local Variables:
// mode: php
// c-file-style: "bsd"
// End: