4 * User importing script for site admin
6 * Copyright (c) 2011 Olivier Berger & Institut Telecom
8 * This program was developped in the frame of the COCLICO project
9 * (http://www.coclico-project.org/) with financial support of the Paris
12 * This file is part of FusionForge. FusionForge is free software;
13 * you can redistribute it and/or modify it under the terms of the
14 * GNU General Public License as published by the Free Software
15 * Foundation; either version 2 of the Licence, or (at your option)
18 * FusionForge is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 // Import users from a JSON file (Site Admin tool)
29 // Users are created in the pending queue, and await forge admin moderation
31 // TODO : add confirmation pass instead of batch insertion
33 require_once('../../../www/env.inc.php');
34 require_once $gfwww.'include/pre.php';
35 require_once $gfwww.'admin/admin_utils.php';
37 require_once $gfplugins.'projectimport/common/ProjectImporter.class.php';
38 require_once $gfplugins.'projectimport/common/UploadedFiles.class.php';
40 include_once('arc/ARC2.php');
43 * Manages the display of the page : HTML + forms
45 * @author Olivier Berger
48 class UsersImportPage extends FileManagerPage {
52 protected $form_header_already_displayed;
54 function UsersImportPage($HTML) {
55 $this->form_header_already_displayed = false;
57 $this->importer = ProjectImporter::getInstance();
59 $storage = new SiteAdminFilesDirectory($HTML);
61 parent::FileManagerPage($HTML, $storage);
65 * Display initial contents of the page
66 * @param string $message
68 function display_headers($message) {
72 $params['title']=_('Users importer');
73 $params['toptab']='projectimport';
75 site_admin_header($params);
77 $this->message .= $message;
83 function display_main() {
87 // Do the work, first !
88 $html = $this->do_work();
91 echo $this->message . '<br />';
93 html_feedback_top($feedback);
97 // If invoked initially (not on callback) or if more details needed
98 // display the last part of the form for JSON file upload
99 if (! $this->form_header_already_displayed) {
100 echo '<form enctype="multipart/form-data" action="'.getStringFromServer('PHP_SELF').'" method="post">';
101 $this->form_header_already_displayed = True;
104 $preselected = False;
107 if ($this->posted_selecteddumpfile) {
108 $preselected = basename($this->posted_selecteddumpfile);
110 elseif ($this->posted_uploadedfile) {
111 $preselected = $this->posted_uploadedfile;
115 $selectiondialog = $this->storage->displayFileSelectionForm($preselected);
117 echo $selectiondialog;
119 // finally, display the file upload form
120 echo '<fieldset><legend>Please upload a file :</legend>
122 <input type="file" id="uploaded_file" name="uploaded_file" tabindex="2" size="30" />
125 <div style="text-align:center;">
126 <input type="submit" name="submit" value="Submit" />
130 site_footer(array());
134 * Initializes data structures from POSTed data coming from the form input
136 function initialize_from_submitted_data() {
139 $filechosen = $this->initialize_chosenfile_from_submitted();
141 //print_r($filechosen);
142 $json = fread(fopen($this->posted_selecteddumpfile, 'r'),filesize($this->posted_selecteddumpfile));
144 $feedback = "Error : missing data";
148 // print_r($imported_file);
149 $this->importer->parse_OSLCCoreRDFJSON($json);
153 // Debug the loaded triples
154 $triples = $this->importer->parse_OSLCCoreRDFJSON($json);
155 $ser = ARC2::getTurtleSerializer();
157 if(count($triples)) {
158 $this->message .= '<pre>'. nl2br(htmlspecialchars($ser->toTurtle($triples))) . '</pre>';
164 if ((! $this->posted_selecteddumpfile) && (! $this->posted_uploadedfile)) {
165 $this->feedback(_('Please select an existing file to process, or upload a new one'));
173 * @return html string
180 // If the posted JSON file indeed contains a project dump, an importer was created,
181 // and if it has data we can work
182 // If it indeed has valid data
183 if ($this->importer->has_project_dump()) {
185 $this->message .= "Here are the results from your upload :";
188 if (! $this->form_header_already_displayed) {
189 $this->form_header_already_displayed = true;
190 $html .= '<form enctype="multipart/form-data" action="'.getStringFromServer('PHP_SELF').'" method="post">';
193 $imported_users = $this->importer->get_user_objs();
195 if (count($imported_users)) {
196 $html .= $this->html_generator->boxTop(_("Users found in imported file"));
198 foreach($imported_users as $user => $user_obj) {
200 $unix_name = $user_obj->getUnixName();
201 $email = $user_obj->getEmail();
203 $firstname = $user_obj->getFirstname();
204 $lastname = $user_obj->getLastname();
206 $theme_id=$this->html_generator->getThemeIdFromName(forge_get_config('default_theme'));
207 $password1 = substr(md5($GLOBALS['session_ser'] . time() . util_randbytes()), 0, 8);
208 $password2 = $password1;
209 $language_id = language_name_to_lang_id (choose_language_from_context ());
211 $new_user = new GFUser();
212 $res = $new_user->create($unix_name,$firstname,$lastname,$password1,$password2,
213 $email,$mail_site,$mail_va,$language_id,$timezone,$jabber_address,$jabber_only,$theme_id,'',
214 $address,$address2,$phone,$fax,$title,$ccode,$send_mail);
217 $error_msg = $new_user->getErrorMessage();
218 if ($feedback) $feedback .= '<br />';
219 $feedback .= 'Import of "'. $unix_name . '": '. $error_msg;
221 $html .= _('Failed to create user'). ': <pre>'. $unix_name .'</pre>';
224 $html .= _('Created user'). ': <pre>'. $unix_name .'</pre>';
227 $html .= 'User :<br />';
228 $html .= ' account name : '. $unix_name .'<br />';
229 $html .= ' email : '. $email .'<br />';
230 $html .= ' firstname : '. $firstname .'<br />';
231 $html .= ' lastname : '. $lastname .'<br />';
235 $html .= $this->html_generator->boxBottom();
240 $feedback .= 'Found no users<br />';
249 // The user should be forge admin
250 session_require_global_perm ('forge_admin');
252 global $group_id, $feedback;
254 $this_page = new UsersImportPage($HTML);
260 // when called back by post form we can initialize some elements provided by the user
261 if (getStringFromRequest('submit')) {
263 $this_page->initialize_from_submitted_data();
267 $message .= "You can import a list of users from a JSON RDF document compatible with ForgePlucker's dump format.<br />";
270 $this_page->display_headers($message);
272 $this_page->display_main();
278 // c-file-style: "bsd"