PageRenderTime 198ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/application/controllers/ApplicationController.php

https://code.google.com/p/ontowiki/
PHP | 859 lines | 622 code | 145 blank | 92 comment | 148 complexity | 43c2086dfc9896dcf4e7c0154856b298 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * OntoWiki application controller.
  4. *
  5. * @package application
  6. * @subpackage mvc
  7. * @author Norman Heino <norman.heino@gmail.com>
  8. * @author Philipp Frischmuth <pfrischmuth@googlemail.com>
  9. * @copyright Copyright (c) 2008, {@link http://aksw.org AKSW}
  10. * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
  11. * @version $Id: ApplicationController.php 4313 2009-10-14 21:37:47Z c.riess.dev $
  12. */
  13. class ApplicationController extends OntoWiki_Controller_Base
  14. {
  15. /**
  16. * Displays OntoWiki's about page
  17. */
  18. public function aboutAction()
  19. {
  20. OntoWiki_Navigation::disableNavigation();
  21. $this->view->placeholder('main.window.title')->set('About OntoWiki');
  22. $version = $this->_config->version->number;
  23. if (isset($this->_config->version->suffix)) {
  24. $version .= ' ' . $this->_config->version->suffix;
  25. }
  26. $cacheWritable = is_writable($this->_config->cache->path)
  27. ? ' <span style="color:#aea">(writable)</span>'
  28. : ' <span style="color:#eaa">(not writable!)</span>';
  29. $logWritable = is_writable($this->_config->log->path)
  30. ? ' <span style="color:#aea">(writable)</span>'
  31. : ' <span style="color:#eaa">(not writable!)</span>';
  32. $data = array(
  33. 'System' => array(
  34. 'OntoWiki Version' => $version,
  35. 'PHP Version' => phpversion(),
  36. 'Backend' => $this->_owApp->erfurt->getStore()->getBackendName(),
  37. 'Debug Mode' => defined('_OWDEBUG') ? 'enabled' : 'disabled'
  38. ),
  39. 'User Interface' => array(
  40. 'Theme' => rtrim($this->_config->themes->default, '/'),
  41. 'Language' => $this->_config->languages->locale,
  42. ),
  43. 'Paths' => array(
  44. 'Extensions Path' => _OWROOT . rtrim($this->_config->extensions->base, '/'),
  45. 'Translations Path' => _OWROOT . rtrim($this->_config->languages->path, '/'),
  46. 'Themes Path' => _OWROOT . rtrim($this->_config->themes->path, '/')
  47. ),
  48. 'Cache' => array(
  49. 'Path' => rtrim($this->_config->cache->path, '/') . $cacheWritable,
  50. 'Module Caching' => ((bool)$this->_config->cache->modules == true) ? 'enabled' : 'disabled',
  51. 'Translation Caching' => ((bool)$this->_config->cache->translation == true) ? 'enabled' : 'disabled'
  52. ),
  53. 'Logging' => array(
  54. 'Path' => rtrim($this->_config->log->path, '/') . $logWritable,
  55. 'Level' => (bool)$this->_config->loglevel ? $this->_config->loglevel : 'disabled'
  56. )
  57. );
  58. $this->view->data = $data;
  59. }
  60. /**
  61. * Authenticates with Erfurt using the provided credentials.
  62. */
  63. public function loginAction()
  64. {
  65. $erfurt = $this->_owApp->erfurt;
  66. $post = $this->_request->getPost();
  67. $this->_helper->layout()->disableLayout();
  68. $this->_helper->viewRenderer->setNoRender();
  69. // If remember option is on make session persistent
  70. if (!empty($post['login-save']) && $post['login-save'] == 'on') {
  71. // Make session persistent (for about 23 years)
  72. Zend_Session::rememberMe(726364800);
  73. }
  74. $loginType = $post['logintype'];
  75. // lokaler Login
  76. if ($loginType === 'locallogin') {
  77. $username = $post['username'];
  78. $password = $post['password'];
  79. $authResult = $erfurt->authenticate($username, $password);
  80. }
  81. // OpenID
  82. else if ($loginType === 'openidlogin') {
  83. $username = $post['openid_url'];
  84. $redirectUrl = $post['redirect-uri'];
  85. $verifyUrl = $this->_config->urlBase . 'application/verifyopenid';
  86. $authResult = $erfurt->authenticateWithOpenId($username, $verifyUrl, $redirectUrl);
  87. }
  88. // FOAF+SSL
  89. else if ($loginType === 'webidlogin') {
  90. $redirectUrl = $this->_config->urlBase . 'application/loginfoafssl';
  91. $authResult = $erfurt->authenticateWithFoafSsl(null, $redirectUrl);
  92. } else {
  93. // Not supported...
  94. return;
  95. }
  96. // reload selected model w/ new privileges
  97. if ($this->_owApp->selectedModel instanceof Erfurt_Rdf_Model) {
  98. $this->_owApp->selectedModel = $erfurt->getStore()->getModel((string) $this->_owApp->selectedModel);
  99. }
  100. $this->_owApp->authResult = $authResult->getMessages();
  101. }
  102. public function verifyopenidAction()
  103. {
  104. $erfurt = $this->_owApp->erfurt;
  105. $get = $this->_request->getQuery();
  106. $authResult = $erfurt->verifyOpenIdResult($get);
  107. $this->_owApp->authResult = $authResult->getMessages();
  108. if (isset($get['ow_redirect_url'])) {
  109. $this->_redirect(urldecode($get['ow_redirect_url']), array('prependBase' => false));
  110. } else {
  111. $this->_redirect($this->_config->urlBase, array('prependBase' => false));
  112. }
  113. }
  114. public function loginfoafsslAction()
  115. {
  116. $erfurt = $this->_owApp->erfurt;
  117. $get = $this->_request->getQuery();
  118. //$get['url'] = $this->_request->getHttpHost() . $this->_request->getRequestUri();
  119. $authResult = $erfurt->authenticateWithFoafSsl($get);
  120. $this->_owApp->authResult = $authResult->getMessages();
  121. $this->_redirect($this->_config->urlBase, array('prependBase' => false));
  122. }
  123. /**
  124. * Destroys auth credentials and logs the current agent out.
  125. */
  126. public function logoutAction()
  127. {
  128. // destroy auth
  129. Erfurt_Auth::getInstance()->clearIdentity();
  130. // destroy any selections user has made
  131. Zend_Session::destroy(true);
  132. $this->_redirect($this->_config->urlBase);
  133. }
  134. /**
  135. * Registers a new user
  136. */
  137. public function registerAction()
  138. {
  139. OntoWiki_Navigation::disableNavigation();
  140. $this->_helper->viewRenderer->setScriptAction('register');
  141. $this->view->placeholder('main.window.title')->set('Register User');
  142. $this->view->formActionUrl = $this->_config->urlBase . 'application/register';
  143. $this->view->formMethod = 'post';
  144. $this->view->formClass = 'simple-input input-justify-left';
  145. $this->view->formName = 'registeruser';
  146. $this->view->username = '';
  147. $this->view->readonly = '';
  148. $this->view->email = '';
  149. $toolbar = $this->_owApp->toolbar;
  150. $toolbar->appendButton(OntoWiki_Toolbar::SUBMIT, array('name' => 'Register User'))
  151. ->appendButton(OntoWiki_Toolbar::RESET, array('name' => 'Reset Form'));
  152. $this->view->placeholder('main.window.toolbar')->set($toolbar);
  153. $post = $this->_request->getPost();
  154. $this->_owApp->appendMessage(new OntoWiki_Message(
  155. 'Already own an <span class="openid">OpenID?</span> <a href="' . $this->_config->urlBase . 'application/openidreg">Register here</a>',
  156. OntoWiki_Message::INFO,
  157. array('escape' => false, 'translate' => false)
  158. ));
  159. if ($post) {
  160. $registeredUsernames = array();
  161. $registeredEmailAddresses = array();
  162. foreach ($this->_erfurt->getUsers() as $userUri => $userArray) {
  163. if (array_key_exists('userName', $userArray)) {
  164. $registeredUsernames[] = $userArray['userName'];
  165. }
  166. if (array_key_exists('userEmail', $userArray)) {
  167. $registeredEmailAddresses[] = str_replace('mailto:', '', $userArray['userEmail']);
  168. }
  169. }
  170. $email = $post['email'];
  171. $username = $post['username'];
  172. $password = $post['password'];
  173. $password2 = $post['password2'];
  174. $emailValidator = new Zend_Validate_EmailAddress();
  175. if (!$this->_erfurt->isActionAllowed('RegisterNewUser') or
  176. !($actionConfig = $this->_erfurt->getActionConfig('RegisterNewUser'))) {
  177. $message = 'Action not permitted for the current user.';
  178. $this->_owApp->appendMessage(new OntoWiki_Message($message, OntoWiki_Message::ERROR));
  179. } else if (trim($email) == '') {
  180. $message = 'Email address must not be empty.';
  181. $this->_owApp->appendMessage(new OntoWiki_Message($message, OntoWiki_Message::ERROR));
  182. } else if (in_array($email, $registeredEmailAddresses)) {
  183. $message = 'Email address is already registered.';
  184. $this->_owApp->appendMessage(new OntoWiki_Message($message, OntoWiki_Message::ERROR));
  185. } else if (isset($actionConfig['mailvalidation']) &&
  186. $actionConfig['mailvalidation'] == 'yes' &&
  187. !$emailValidator->isValid($email)) {
  188. $message = 'Email address validation failed.';
  189. $this->_owApp->appendMessage(new OntoWiki_Message($message, OntoWiki_Message::ERROR));
  190. } else if (in_array($username, $registeredUsernames) or ($username == $this->_owApp->erfurt->getStore()->getDbUser())) {
  191. $message = 'Username already registered.';
  192. $this->_owApp->appendMessage(new OntoWiki_Message($message, OntoWiki_Message::ERROR));
  193. } else if (isset($actionConfig['uidregexp']) &&
  194. !preg_match($actionConfig['uidregexp'], $username)) {
  195. $message = 'Username contains illegal characters.';
  196. $this->_owApp->appendMessage(new OntoWiki_Message($message, OntoWiki_Message::ERROR));
  197. } else if ($password !== $password2) {
  198. $message = 'Passwords do not match.';
  199. $this->_owApp->appendMessage(new OntoWiki_Message($message, OntoWiki_Message::ERROR));
  200. } else if (strlen($password) < 5) {
  201. $message = 'Password needs at least 5 characters.';
  202. $this->_owApp->appendMessage(new OntoWiki_Message($message, OntoWiki_Message::ERROR));
  203. } else if (isset($actionConfig['passregexp']) &&
  204. $actionConfig['passregexp'] != '' &&
  205. !@preg_match($actionConfig['passregexp'], $password)) {
  206. $message = 'Password does not match regular expression set in system configuration';
  207. $this->_owApp->appendMessage(new OntoWiki_Message($message, OntoWiki_Message::ERROR));
  208. } else {
  209. // give default group?
  210. if (isset($actionConfig['defaultGroup'])) {
  211. $group = $actionConfig['defaultGroup'];
  212. }
  213. // add new user
  214. if ($this->_erfurt->addUser($username, $password, $email, $group)) {
  215. $message = 'The user "' . $username . '" has been successfully registered.';
  216. $this->_owApp->appendMessage(new OntoWiki_Message($message, OntoWiki_Message::SUCCESS));
  217. } else {
  218. $message = 'A registration error occured. Please refer to the log entries.';
  219. $this->_owApp->appendMessage(new OntoWiki_Message($message, OntoWiki_Message::ERROR));
  220. }
  221. }
  222. }
  223. }
  224. /**
  225. * Registers a new user with a given OpenID.
  226. */
  227. public function openidregAction()
  228. {
  229. OntoWiki_Navigation::disableNavigation();
  230. // We render a template, that is also used for preferences.
  231. $this->_helper->viewRenderer->setScriptAction('openid');
  232. $this->view->placeholder('main.window.title')->set('Register User with OpenID');
  233. $this->view->formActionUrl = $this->_config->urlBase . 'application/openidreg';
  234. $this->view->formMethod = 'post';
  235. $this->view->formClass = 'simple-input input-justify-left';
  236. $this->view->formName = 'registeruser';
  237. // Fetch POST and GET of the request. One of them or both will be empty.
  238. $post = $this->_request->getPost();
  239. $get = $this->_request->getQuery();
  240. if (!empty($post)) {
  241. // Step 1: User entered data and clicked on 'Check OpenID'
  242. if ((int)$post['step'] === 1) {
  243. $openId = $post['openid_url'];
  244. $label = $post['label'];
  245. $email = $post['email'];
  246. $emailValidator = new Zend_Validate_EmailAddress();
  247. // Is register action allowed for current user?
  248. if (!$this->_erfurt->isActionAllowed('RegisterNewUser') ||
  249. !($actionConfig = $this->_erfurt->getActionConfig('RegisterNewUser'))) {
  250. $message = 'Action not permitted for the current user.';
  251. $this->_owApp->appendMessage(new OntoWiki_Message($message, OntoWiki_Message::ERROR));
  252. }
  253. // openid_url field must not be empty
  254. else if (empty($openId)) {
  255. $message = 'No OpenID was entered.';
  256. $this->_owApp->appendMessage(new OntoWiki_Message($message, OntoWiki_Message::ERROR));
  257. }
  258. // Does user already exist?
  259. else if (array_key_exists($openId, $this->_erfurt->getUsers())) {
  260. $message = 'A user with the given OpenID is already registered.';
  261. $this->_owApp->appendMessage(new OntoWiki_Message($message, OntoWiki_Message::ERROR));
  262. }
  263. // If an (optional) email address is given, check whether it is valid.
  264. else if (!empty($email) && isset($actionConfig['mailvalidation']) &&
  265. $actionConfig['mailvalidation'] === 'yes' && !$emailValidator->isValid($email)) {
  266. $message = 'Email address validation failed.';
  267. $this->_owApp->appendMessage(new OntoWiki_Message($message, OntoWiki_Message::ERROR));
  268. }
  269. // Everything seems to be OK... Check the OpenID (redirect to the provider).
  270. else {
  271. // We want to verify the OpenID auth response in this action.
  272. $verifyUrl = $this->_config->urlBase . 'application/openidreg';
  273. // If label and/or email are given, put them at the end of the request url, for
  274. // we need them later.
  275. if (!empty($label) && !empty($email)) {
  276. $verifyUrl .= '?label=' . urlencode($label) . '&email=' . urlencode($email);
  277. } else if (!empty($label)) {
  278. $verifyUrl .= '?label=' . urlencode($label);
  279. } else if (!empty($email)) {
  280. $verifyUrl .= '?email=' . urlencode($email);
  281. }
  282. $sReg = new Zend_OpenId_Extension_Sreg(array(
  283. 'nickname' => false,
  284. 'email' => false), null, 1.1);
  285. $adapter = new Erfurt_Auth_Adapter_OpenId($openId, $verifyUrl, null, null, $sReg);
  286. // We use the adapter directly, for we do not store the identity in session.
  287. $result = $adapter->authenticate();
  288. // If we reach this point, something went wrong
  289. $message = 'OpenID check failed.';
  290. $this->_owApp->appendMessage(new OntoWiki_Message($message, OntoWiki_Message::ERROR));
  291. }
  292. // If we reach this section, something went wrong, so we reset the form and show the message.
  293. $this->view->openid = '';
  294. $this->view->readonly = '';
  295. $this->view->email = '';
  296. $this->view->label = '';
  297. $this->view->step = 1;
  298. $toolbar = $this->_owApp->toolbar;
  299. $toolbar->appendButton(OntoWiki_Toolbar::SUBMIT, array('name' => 'Check OpenID'))
  300. ->appendButton(OntoWiki_Toolbar::RESET, array('name' => 'Reset Form'));
  301. $this->view->placeholder('main.window.toolbar')->set($toolbar);
  302. } else if ((int)$post['step'] === 2) {
  303. // Step 2: OpenID was verified and user clicked on register button.
  304. $openid = $post['openid_url'];
  305. $email = $post['email'];
  306. $label = $post['label'];
  307. // Give user default group?
  308. $actionConfig = $this->_erfurt->getActionConfig('RegisterNewUser');
  309. $group = null;
  310. if (isset($actionConfig['defaultGroup'])) {
  311. $group = $actionConfig['defaultGroup'];
  312. }
  313. // Add the new user.
  314. if ($this->_erfurt->addOpenIdUser($openid, $email, $label, $group)) {
  315. $message = 'The user with the OpenID "' . $openid . '" has been successfully registered.';
  316. $this->_owApp->appendMessage(new OntoWiki_Message($message, OntoWiki_Message::SUCCESS));
  317. } else {
  318. $message = 'A registration error occured. Please refer to the log entries.';
  319. $this->_owApp->appendMessage(new OntoWiki_Message($message, OntoWiki_Message::ERROR));
  320. }
  321. // Reset the form...
  322. $this->view->openid = '';
  323. $this->view->readonly = '';
  324. $this->view->email = '';
  325. $this->view->label = '';
  326. $this->view->step = 1;
  327. $toolbar = $this->_owApp->toolbar;
  328. $toolbar->appendButton(OntoWiki_Toolbar::SUBMIT, array('name' => 'Check OpenID'))
  329. ->appendButton(OntoWiki_Toolbar::RESET, array('name' => 'Reset Form'));
  330. $this->view->placeholder('main.window.toolbar')->set($toolbar);
  331. }
  332. } else if (!empty($get)) {
  333. // This is the verify request
  334. $sReg = new Zend_OpenId_Extension_Sreg(array(
  335. 'nickname' => false,
  336. 'email' => false), null, 1.1);
  337. $adapter = new Erfurt_Auth_Adapter_OpenId(null, null, null, $get, $sReg);
  338. // We use the adapter directly, for we do not store the identity in session.
  339. $result = $adapter->authenticate();
  340. if (!$result->isValid()) {
  341. // Something went wrong, show a message
  342. $message = 'OpenID verification failed.';
  343. $this->_owApp->appendMessage(new OntoWiki_Message($message, OntoWiki_Message::ERROR));
  344. }
  345. $data = $sReg->getProperties();
  346. // Use the prefilled data from the user (if given) or if not use the data from the provider (if
  347. // available).
  348. if (isset($get['email'])) {
  349. $email = $get['email'];
  350. } else if (isset($data['email'])) {
  351. $email = $data['email'];
  352. } else {
  353. $email = '';
  354. }
  355. if (isset($get['label'])) {
  356. $label = $get['label'];
  357. } else if (isset($data['nickname'])) {
  358. $label = $data['nickname'];
  359. } else {
  360. $label = '';
  361. }
  362. $this->view->openid = $get['openid_identity'];
  363. $this->view->readonly = 'readonly="readonly"'; // OpenID must not be changed now.
  364. $this->view->email = $email;
  365. $this->view->label = $label;
  366. $this->view->step = 2;
  367. $this->view->checked = true; // We use this to show a green icon for success
  368. $toolbar = $this->_owApp->toolbar;
  369. $toolbar->appendButton(OntoWiki_Toolbar::SUBMIT, array('name' => 'Register User'))
  370. ->appendButton(OntoWiki_Toolbar::CANCEL, array('name' => 'Cancel', 'class' => 'openidreg-cancel'));
  371. $this->view->placeholder('main.window.toolbar')->set($toolbar);
  372. } else {
  373. // No post and get data... This is the initial form...
  374. $this->view->openid = '';
  375. $this->view->readonly = '';
  376. $this->view->email = '';
  377. $this->view->label = '';
  378. $this->view->step = 1;
  379. $toolbar = $this->_owApp->toolbar;
  380. $toolbar->appendButton(OntoWiki_Toolbar::SUBMIT, array('name' => 'Check OpenID'))
  381. ->appendButton(OntoWiki_Toolbar::RESET, array('name' => 'Reset Form'));
  382. $this->view->placeholder('main.window.toolbar')->set($toolbar);
  383. }
  384. }
  385. public function webidregAction()
  386. {
  387. OntoWiki_Navigation::disableNavigation();
  388. // We render a template, that is also used for preferences.
  389. $this->_helper->viewRenderer->setScriptAction('webid');
  390. $this->view->placeholder('main.window.title')->set('Register User with FOAF+SSL');
  391. $this->view->formActionUrl = $this->_config->urlBase . 'application/webidreg';
  392. $this->view->formMethod = 'post';
  393. $this->view->formClass = 'simple-input input-justify-left';
  394. $this->view->formName = 'registeruser';
  395. // Fetch POST and GET of the request. One of them or both will be empty.
  396. $post = $this->_request->getPost();
  397. $get = $this->_request->getQuery();
  398. // Step 1: Fetch the WebID...
  399. if (empty($post) && empty($get)) {
  400. $redirectUrl = $this->_config->urlBase . 'application/webidreg';
  401. $adapter = new Erfurt_Auth_Adapter_FoafSsl(null, $redirectUrl);
  402. $webId = $adapter->fetchWebId();
  403. // We should not reach this point;
  404. return;
  405. } else if (!empty($get)) {
  406. // Step 2: Check the web id and fetch foaf data
  407. $get['url'] = $this->_request->getRequestUri();
  408. $adapter = new Erfurt_Auth_Adapter_FoafSsl();
  409. try {
  410. $valid = $adapter->verifyIdpResult($get);
  411. if ($valid) {
  412. $webId = $get['webid'];
  413. $foafData = Erfurt_Auth_Adapter_FoafSsl::getFoafData($webId);
  414. if ($foafData !== false) {
  415. // Try to get a mbox and label...
  416. if (isset($foafData[$webId]['http://xmlns.com/foaf/0.1/mbox'])) {
  417. $email = $foafData[$webId]['http://xmlns.com/foaf/0.1/mbox'][0]['value'];
  418. } else {
  419. $email = '';
  420. }
  421. if (isset($foafData[$webId][EF_RDFS_LABEL])) {
  422. $label = $foafData[$webId][EF_RDFS_LABEL][0]['value'];
  423. } else {
  424. $label = '';
  425. }
  426. } else {
  427. $email = '';
  428. $label = '';
  429. }
  430. $this->view->webid = $webId;
  431. if ($webId != '') {
  432. $this->view->checked = true;
  433. }
  434. if (null !== $email) {
  435. $this->view->email = $email;
  436. } else {
  437. $this->view->email = '';
  438. }
  439. if (null !== $label) {
  440. $this->view->label = $label;
  441. } else {
  442. $this->view->label = '';
  443. }
  444. $toolbar = $this->_owApp->toolbar;
  445. $toolbar->appendButton(OntoWiki_Toolbar::SUBMIT, array('name' => 'Register'));
  446. $this->view->placeholder('main.window.toolbar')->set($toolbar);
  447. return;
  448. } else {
  449. // TODO Error message
  450. $this->view->webid = '';
  451. $this->view->email = '';
  452. $this->view->label = '';
  453. $this->_owApp->appendMessage(
  454. new OntoWiki_Message('No valid certificate found.', OntoWiki_Message::ERROR)
  455. );
  456. return;
  457. }
  458. } catch (Exception $e) {
  459. $this->view->webid = '';
  460. $this->view->email = '';
  461. $this->view->label = '';
  462. $this->_owApp->appendMessage(
  463. new OntoWiki_Message('Something went wrong: ' . $e->getMessage(), OntoWiki_Message::ERROR)
  464. );
  465. return;
  466. }
  467. } else if (!empty($post)) {
  468. $webId = $post['webid_url'];
  469. $label = $post['label'];
  470. $email = $post['email'];
  471. $emailValidator = new Zend_Validate_EmailAddress();
  472. // Is register action allowed for current user?
  473. if (!$this->_erfurt->isActionAllowed('RegisterNewUser') ||
  474. !($actionConfig = $this->_erfurt->getActionConfig('RegisterNewUser'))) {
  475. $message = 'Action not permitted for the current user.';
  476. $this->_owApp->appendMessage(new OntoWiki_Message($message, OntoWiki_Message::ERROR));
  477. }
  478. // openid_url field must not be empty
  479. else if (empty($webId)) {
  480. $message = 'No WebID was entered.';
  481. $this->_owApp->appendMessage(new OntoWiki_Message($message, OntoWiki_Message::ERROR));
  482. }
  483. // Does user already exist?
  484. else if (array_key_exists($webId, $this->_erfurt->getUsers())) {
  485. $message = 'A user with the given WebID is already registered.';
  486. $this->_owApp->appendMessage(new OntoWiki_Message($message, OntoWiki_Message::ERROR));
  487. }
  488. // If an (optional) email address is given, check whether it is valid.
  489. else if (!empty($email) && isset($actionConfig['mailvalidation']) &&
  490. $actionConfig['mailvalidation'] === 'yes' && !$emailValidator->isValid($email)) {
  491. $message = 'Email address validation failed.';
  492. $this->_owApp->appendMessage(new OntoWiki_Message($message, OntoWiki_Message::ERROR));
  493. }
  494. // Everything seems to be OK...
  495. else {
  496. $actionConfig = $this->_erfurt->getActionConfig('RegisterNewUser');
  497. $group = null;
  498. if (isset($actionConfig['defaultGroup'])) {
  499. $group = $actionConfig['defaultGroup'];
  500. }
  501. // Add the new user.
  502. if ($this->_erfurt->addOpenIdUser($webId, $email, $label, $group)) {
  503. $message = 'The user with the WebID "' . $webId . '" has been successfully registered.';
  504. $this->_owApp->appendMessage(new OntoWiki_Message($message, OntoWiki_Message::SUCCESS));
  505. } else {
  506. $message = 'A registration error occured. Please refer to the log entries.';
  507. $this->_owApp->appendMessage(new OntoWiki_Message($message, OntoWiki_Message::ERROR));
  508. }
  509. }
  510. // If we reach this section, something went wrong, so we reset the form and show the message.
  511. $this->view->webid = '';
  512. $this->view->email = '';
  513. $this->view->label = '';
  514. }
  515. }
  516. /**
  517. * Edits user preferences
  518. */
  519. public function preferencesAction()
  520. {
  521. $this->view->placeholder('main.window.title')->set('User Preferences');
  522. $this->addModuleContext('main.window.preferences');
  523. $user = $this->_owApp->getUser();
  524. // Anonymous and Db-User have no prefs.
  525. if ($user->isAnonymousUser() || $user->isDbUser()) {
  526. $this->_redirect($this->_config->urlBase, array('prependBase' => false));
  527. }
  528. $post = $this->_request->getPost();
  529. if ($post) {
  530. // We catch all exceptions here, for we do not want the user to see ow crash if something unexpected
  531. // happens.
  532. try {
  533. if (isset($post['openid'])) {
  534. $this->_updateOpenIdUser($post);
  535. } else {
  536. $this->_updateUser($post);
  537. }
  538. } catch (Exception $e) {
  539. $this->_owApp->appendMessage(
  540. new OntoWiki_Message('Something went wrong: ' . $e->getMessage(), OntoWiki_Message::ERROR)
  541. );
  542. }
  543. if (!$this->_owApp->hasMessages()) {
  544. $message = 'Changes saved.';
  545. $this->_owApp->appendMessage(new OntoWiki_Message($message, OntoWiki_Message::SUCCESS));
  546. }
  547. }
  548. $this->view->isOpenIdUser = ($user->isOpenId() || $user->isWebId());
  549. if ($user->isOpenId() || $user->isWebId()) {
  550. $this->view->openid = $user->getUri();
  551. $usernameReadonly = '';
  552. } else {
  553. $usernameReadonly = 'readonly="readonly"';
  554. }
  555. $email = $user->getEmail();
  556. if (substr($email, 0, 7) === 'mailto:') {
  557. $email = substr($email, 7);
  558. }
  559. $username = $user->getUsername();
  560. $this->view->formActionUrl = $this->_config->urlBase . 'application/preferences';
  561. $this->view->formMethod = 'post';
  562. $this->view->formClass = 'simple-input input-justify-left';
  563. $this->view->formName = 'registeruser';
  564. $this->view->username = $username;
  565. $this->view->userReadonly = $usernameReadonly;
  566. $this->view->email = $email;
  567. $this->view->submitText = 'Save Changes';
  568. $toolbar = $this->_owApp->toolbar;
  569. $toolbar->appendButton(OntoWiki_Toolbar::SUBMIT, array('name' => 'Save Changes', 'id' => 'registeruser'))
  570. ->appendButton(OntoWiki_Toolbar::RESET, array('name' => 'Reset Form'));
  571. $this->view->placeholder('main.window.toolbar')->set($toolbar);
  572. OntoWiki_Navigation::disableNavigation();
  573. $this->_helper->viewRenderer->setScriptAction('userdetails');
  574. }
  575. protected function _updateEmailAddress($newEmail)
  576. {
  577. try {
  578. $this->_erfurt->getAuth()->setEmail($newEmail);
  579. } catch (Erfurt_Auth_Identity_Exception $e) {
  580. $this->_owApp->appendMessage(new OntoWiki_Message($e->getMessage(), OntoWiki_Message::ERROR));
  581. return false;
  582. }
  583. return true;
  584. }
  585. protected function _updateUsername($newUsername)
  586. {
  587. try {
  588. $this->_erfurt->getAuth()->setUsername($newUsername);
  589. } catch (Erfurt_Auth_Identity_Exception $e) {
  590. $this->_owApp->appendMessage(new OntoWiki_Message($e->getMessage(), OntoWiki_Message::ERROR));
  591. return false;
  592. }
  593. return true;
  594. }
  595. protected function _updatePassword($password1, $password2)
  596. {
  597. if ($password1 !== $password2) {
  598. $message = 'Passwords do not match.';
  599. $this->_owApp->appendMessage(new OntoWiki_Message($message, OntoWiki_Message::ERROR));
  600. return false;
  601. }
  602. try {
  603. $this->_erfurt->getAuth()->getIdentity()->setPassword($password1);
  604. } catch (Erfurt_Auth_Identity_Exception $e) {
  605. $this->_owApp->appendMessage(new OntoWiki_Message($e->getMessage(), OntoWiki_Message::ERROR));
  606. return false;
  607. }
  608. return true;
  609. }
  610. protected function _updateOpenIdUser($post)
  611. {
  612. if ($this->_updateUsername($post['username'])) {
  613. if ($this->_updateEmailAddress($post['email'])) {
  614. if (isset($post['changepassword']) && $post['changepassword'] === '1') {
  615. return $this->_updatePassword($post['password1'], $post['password2']);
  616. } else {
  617. return true;
  618. }
  619. }
  620. }
  621. return false;
  622. }
  623. protected function _updateUser($post)
  624. {
  625. if ($this->_updateEmailAddress($post['email'])) {
  626. if (isset($post['changepassword']) && $post['changepassword'] === '1') {
  627. return $this->_updatePassword($post['password1'], $post['password2']);
  628. } else {
  629. return true;
  630. }
  631. }
  632. return false;
  633. }
  634. /**
  635. * Handles search requests
  636. */
  637. public function searchAction()
  638. {
  639. $title = $this->_owApp->translate->_('Resource Search');
  640. $this->view->placeholder('main.window.title')->set($title);
  641. OntoWiki_Navigation::disableNavigation();
  642. $store = $this->_erfurt->getStore();
  643. if (isset($this->_owApp->selectedModel) and null !== $this->_owApp->selectedModel) {
  644. $modelUri = $this->_owApp->selectedModel->getModelIri();
  645. } else {
  646. $modelUri = null;
  647. }
  648. if ($this->_request->getParam('searchtext-input') !== null) {
  649. $searchText = trim($this->getParam('searchtext-input'));
  650. }
  651. $error = false;
  652. $errorMsg = '';
  653. // check for very short searches (that barely make sense)
  654. if (strlen($searchText ) < 3) {
  655. $error = true;
  656. $this->_owApp->appendMessage(new OntoWiki_Message(
  657. $this->_owApp->translate->_('Too Short or empty. (length < 3 )'),
  658. OntoWiki_Message::ERROR
  659. ));
  660. $errorMsg .= $this->_owApp->translate->_(
  661. 'The given search string is either empty or too short: ' .
  662. 'For searches to make sense they need a minimum of expressiveness.'
  663. );
  664. }
  665. // check if search is already errorenous
  666. if (!$error) {
  667. // try sparql query pre search check (with limit to 1)
  668. $elements = $store->getSearchPattern($searchText,$modelUri);
  669. $query = new Erfurt_Sparql_Query2();
  670. $query->addElements($elements);
  671. $query->setLimit(1);
  672. $query->addFrom($modelUri);
  673. try {
  674. $store->sparqlQuery($query);
  675. } catch (Exception $e) {
  676. // build error message
  677. $this->_owApp->appendMessage(new OntoWiki_Message(
  678. $this->_owApp->translate->_('search failed'),
  679. OntoWiki_Message::ERROR
  680. ));
  681. $error = true;
  682. $errorMsg .= 'Message details: ';
  683. $errorMsg .= str_replace('LIMIT 1', '', $e->getMessage());
  684. }
  685. }
  686. // if error occured set output for error page
  687. if ($error) {
  688. $this->view->errorMsg = $errorMsg;
  689. } else {
  690. // set redirect to effective search controller
  691. $url = new OntoWiki_Url( array('controller' => 'list'), array());
  692. $url->setParam('s', $searchText);
  693. $url->setParam('init', '1');
  694. $this->_redirect($url);
  695. }
  696. }
  697. public function testAction()
  698. {
  699. OntoWiki_Navigation::disableNavigation();
  700. $this->_helper->viewRenderer->setNoRender();
  701. $this->view->placeholder('main.window.title')->set('Test');
  702. $testModel = new OntoWiki_ModelTestResource($this->_owApp->erfurt->getStore(), $this->_owApp->selectedModel);
  703. // var_dump((string)$testModel->getQuery());
  704. if ($result = $testModel->getQueryResult()) {
  705. $had = array();
  706. foreach ((array)$result['results']['bindings'] as $resultRow) {
  707. if (!array_key_exists($resultRow['class']['value'], $had)) {
  708. $had[$resultRow['class']['value']] = $resultRow['class']['value'];
  709. var_dump($resultRow['class']['value'], $testModel->getTitle($resultRow['class']['value'], 'en'));
  710. }
  711. }
  712. }
  713. }
  714. }