link = $link; $this->caption = $caption; } /** * Create htmlCode for the action * * @return string with htmlCode of the action */ function htmlCode(){ return "link."\">".$this->caption.""; } /** * Create AppCardAction from assoziative array * * @param array $member array with the member values * @return AppCardAction */ static public function fromArray($member){ $caption = $member['caption'] ?? null; $link = $member['link'] ?? "."; return new AppCardAction($caption, $link); } } /** * MaterializeCss card for an App */ class AppCard{ private $title = ""; //< title of the card private $description = ""; //< description of the App private $link = null; //< link for the card-content private $imgUrl = null; //< url for an image right under the title private $actionList = []; //< list of actions for the bottom of the card /** * Constructor for the AppCard * * @param string $title title of the card * @param string $description description of the card * @param string $link link for the card-content * @param string $imgUrl url for an image right under the title * @param array $actionList list of actions at the bottom of the card */ function __construct($title, $description, $link=null, $imgUrl=null, $actionList=[]){ //! @todo input sanitation $this->title = $title; $this->description = $description; $this->link = $link; $this->imgUrl = $imgUrl; $this->actionList = $actionList; } /** * Create htmlCode for the AppCard * * @return string html code for the AppCard */ public function htmlCode($options=[]){ $extraClass = $options['extraClass'] ?? ""; $actionListCode = ""; foreach($this->actionList as $a){ $actionListCode .= $a->htmlCode(); } return "
". "
". (($this->link!=null)?("link."\">"):(""))."
". "".$this->title."". (($this->imgUrl!=null)?("\"".$this-title."\" style=\"display:block;margin-left:auto;margin-right:auto;max-height:10vh;\" class=\"responsive-img\" src=\"".$this->imgUrl."\" />"):("")). "

".$this->description."

". "
".(($this->link!=null)?("
"):("")). "
".$actionListCode."
". "
". "
"; } /** * Create AppCard from an associative array * * @param array $member array with member as keys and values as the member values * @return AppCard from array values */ static public function fromArray($member){ $title = $member['title'] ?? ""; $description = $member['description'] ?? ""; $link = $member['link'] ?? null; $imgUrl = $member['imgUrl'] ?? null; $actionList = $member['actions'] ?? []; return new AppCard($title, $description, $link, $imgUrl, $actionList); } } /** * Generate a html table of the last logins of the users * * @param string $jsonFileName path to the json file with the logged logins * @return string Html table of users last logins */ function lastLoginTable($jsonFileName="lastLogins.json"){ $lastLogins=json_decode( file_get_contents($jsonFileName), true); $lastLoginsTable = "". "". ""; foreach( $lastLogins as $userName => $lastLogins ){ $lastLoginsTable .= ""; } $lastLoginsTable .= "
userNamelastLogins
".$userName."".$lastLogins['lastLogins'][0]."
"; return $lastLoginsTable; } /// Eine Fehler/Warnung/Notiz/Erfolgsmeldung als divBox im String zurückgeben function htmlRetMessage($anRetMessage){ $retHtmlString = ""; if( !empty($anRetMessage) ){ $retHtmlString .= "
"; if( !empty($anRetMessage['error']) ){ $retHtmlString .= "
"; $retHtmlString .= "ERROR:
"; $retHtmlString .= $anRetMessage['error']; $retHtmlString .= "
"; } if( !empty($anRetMessage['warning']) ){ $retHtmlString .= "
"; $retHtmlString .= "WARNING:
"; $retHtmlString .= $anRetMessage['warning']; $retHtmlString .= "
"; } if( !empty($anRetMessage['notice']) ){ $retHtmlString .= "
"; $retHtmlString .= "Info:
"; $retHtmlString .= $anRetMessage['notice']; $retHtmlString .= "
"; } if( !empty($anRetMessage['success']) ){ $retHtmlString .= "
"; $retHtmlString .= "SUCCESS:
"; $retHtmlString .= $anRetMessage['success']; $retHtmlString .= "
"; } $retHtmlString .= "
"; } return $retHtmlString; } /** * load a MarkdownFile with yaml header * * @param string $fileName filename of the markdown file * @return array assocative array('yaml'=>array(..), 'mdText'=>string) containing the yamlHeader as associative array and the markdown text as string */ function loadMarkdownFile($fileName){ // load the whole file $fileText = file_get_contents($fileName); // split at '---' to get ((),yamls,array) $fileParts = preg_split('/[\n]*[-]{3}[\n]/', $fileText, 3); // not all mdfiles have a yamlHeader, so the mdText can be at different indices $yaml=[]; $mdText = ""; switch( count($fileParts) ){ case 1:{ $mdText = $fileParts[0]; break; } case 3:{ $yaml = Spyc::YAMLLoadString($fileParts[1]); $mdText = $fileParts[2]; break; } default:{ $mdText = $fileText; } } // get a title, if none is in the markdown if(!array_key_exists('title', $yaml)){ // find the first heading, set it as header and remove it from the markdown if( preg_match("/^#(.*)$/m", $mdText, $matches) ){ $yaml['title'] = $matches[1]; $mdText = preg_replace("/^#(.*)$/m", "", $mdText, 1); } else{ // fallback for the title, if not even one heading is found $yaml['title'] = ""; } } return array( 'yaml' => $yaml , 'mdText' => $mdText ); } function checkCredentials($username, $password, $db_server, $db_user, $db_password, $db_name){ sleep(1); $mysqli = @new mysqli($db_server, $db_user, $db_password, $db_name); if ($mysqli->connect_error) { $message['error'] = 'Datenbankverbindung fehlgeschlagen: ' . $mysqli->connect_error; } else { $query = sprintf( "SELECT id, loginName, pwHash, config FROM wkParticipo_Users WHERE loginName = '%s'", $mysqli->real_escape_string($_POST['f']['username']) ); $result = $mysqli->query($query); if ($row = $result->fetch_array(MYSQLI_ASSOC)) { if( password_verify( $_POST['f']['password'], $row['pwHash']) ){ session_start(); $_SESSION = array( 'login' => true, 'user' => array( 'username' => $row['loginName'], 'userId' => $row['id'], 'userConfig' => json_decode($row['config'], true) ), ); $message['success'] = 'Anmeldung erfolgreich, weiter zum Inhalt.'; // Logging Logins logLoginsToJsonFile($_SESSION['user']['username']); header('Location: http://' . $_SERVER['HTTP_HOST'] . '/participo?user=' . $_POST['f']['username']); } else { sleep(5); $message['error'] = 'Das Kennwort ist nicht korrekt.'; } } } return $message; } /** * Log the Login of an user into a logFile * * @param string $userName name of the user * @param string $fileName filename to log to * @return void */ function logLoginsToJsonFile($userName, $fileName="lastLogins.json"){ try{ $lastLogins = json_decode(file_get_contents($fileName), true); if(!array_key_exists($userName, $lastLogins)) $lastLogins[$userName] = []; if(!array_key_exists('lastLogins', $lastLogins[$userName])) $lastLogins[$userName]['lastLogins'] = []; $lastLogins[$userName]['lastLogins'] = array_merge( array( date('Y-m-d H:i:s') ), $lastLogins[$userName]['lastLogins'] ); file_put_contents($fileName, json_encode($lastLogins)); } catch (Exception $e){ // silently ignore errors } } ?>