Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Add files via Upload
  • Loading branch information
j0nem authored Sep 7, 2018
1 parent 22a55c4 commit 5ea6ab5
Show file tree
Hide file tree
Showing 15 changed files with 1,214 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Untis Bundle

Provides a client for the WebUntis services.
45 changes: 45 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "wbgym/untis-bundle",
"type": "contao-bundle",
"description": "Provides a Contao-client for the WebUntis services.",
"license": "GPL-3.0+",
"authors": [
{
"name": "Webteam WBGym",
"homepage": "https://wbgym.de"
}
],
"require": {
"php": "^5.6|^7.0",
"symfony/framework-bundle": "^3.3",
"contao/core-bundle": "^4.4",
"wbgym/school-bundle": "dev-master"
},
"require-dev": {
"contao/manager-plugin": "^2.0"
},
"conflict": {
"contao/core": "*",
"contao/manager-plugin": "<2.0 || >=3.0"
},
"autoload": {
"psr-4": {
"Wbgym\\UntisBundle\\": "src/"
},
"classmap": [
"src/Resources/contao/"
],
"exclude-from-classmap": [
"src/Resources/contao/config/",
"src/Resources/contao/dca/",
"src/Resources/contao/languages/",
"src/Resources/contao/templates/"
]
},
"config": {
"preferred-install": "dist"
},
"extra": {
"contao-manager-plugin": "Wbgym\\UntisBundle\\ContaoManager\\Plugin"
}
}
43 changes: 43 additions & 0 deletions src/ContaoManager/Plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* Contao Open Source CMS
*
* Copyright (c) 2005-2016 Leo Feyer
*
* @license LGPL-3.0+
*/

/**
* Wbgym/UntisBundle
*
* @author Webteam WBGym <webteam@wbgym.de>
* @package Untis Bundle
* @license LGPL-3.0+
*/

namespace Wbgym\UntisBundle\ContaoManager;

use Contao\CoreBundle\ContaoCoreBundle;
use Wbgym\UntisBundle\WbgymUntisBundle;
use Contao\ManagerPlugin\Bundle\BundlePluginInterface;
use Contao\ManagerPlugin\Bundle\Config\BundleConfig;
use Contao\ManagerPlugin\Bundle\Parser\ParserInterface;

/**
* Plugin for the Contao Manager.
*/
class Plugin implements BundlePluginInterface
{
/**
* {@inheritdoc}
*/
public function getBundles(ParserInterface $parser)
{
return [
BundleConfig::create(WbgymUntisBundle::class)
->setLoadAfter([ContaoCoreBundle::class])
->setReplace(['untis'])
];
}
}
60 changes: 60 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/**
* Contao Open Source CMS
*
* Copyright (c) 2005-2016 Leo Feyer
*
* @license LGPL-3.0+
*/

/**
* Wbgym/UntisBundle
*
* @author Webteam WBGym <webteam@wbgym.de>
* @package Untis Bundle
* @license LGPL-3.0+
*/

namespace Wbgym\UntisBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
* Adds the WebUntis bundle configuration structure.
*/
class Configuration implements ConfigurationInterface
{
/**
* Generates the configuration tree builder.
*
* @return TreeBuilder
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('wbgym_untis');

$rootNode
->children()
->scalarNode('api_url')
->cannotBeEmpty()
->end()
->scalarNode('school_code')
->cannotBeEmpty()
->end()
->scalarNode('username')
->cannotBeEmpty()
->end()
->scalarNode('password')
->cannotBeEmpty()
->end()
->scalarNode('client_name')
->cannotBeEmpty()
->end()
->end()
;
return $treeBuilder;
}
}
40 changes: 40 additions & 0 deletions src/DependencyInjection/WbgymUntisExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Contao Open Source CMS
*
* Copyright (c) 2005-2016 Leo Feyer
*
* @license LGPL-3.0+
*/

/**
* Wbgym/UntisBundle
*
* @author Webteam WBGym <webteam@wbgym.de>
* @package Untis Bundle
* @license LGPL-3.0+
*/

namespace Wbgym\UntisBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;

class WbgymUntisExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $mergedConfig, ContainerBuilder $container)
{
$config = new Configuration();
$processedConfig = $this->processConfiguration($config, $mergedConfig);

//Add parameters to Configuration
$container->setParameter('wbgym_untis.api_url',$processedConfig['api_url']);
$container->setParameter('wbgym_untis.school_code',$processedConfig['school_code']);
$container->setParameter('wbgym_untis.username',$processedConfig['username']);
$container->setParameter('wbgym_untis.password',$processedConfig['password']);
$container->setParameter('wbgym_untis.client_name',$processedConfig['client_name']);
}
}
108 changes: 108 additions & 0 deletions src/Resources/contao/classes/WUClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
/**
* WBGym
*
* Copyright (C) 2016 Webteam Weinberg-Gymnasium Kleinmachnow
*
* @package WGBym
* @author Johannes Cram <craj.me@gmail.com>
* @license http://www.gnu.org/licenses/lgpl-3.0.html LGPL
*/

/**
* Namespace
*/
namespace WBGym;

class WUClient extends \System {

protected static $jsessionid = null;
protected static $METHOD = 'POST';
protected static $CONTENT_TYPE = 'application/json';
protected static $CONNECTION_TYPE = 'close';
protected static $currentId;

/*
* Authenticate at WebUntis-API
*/
public function __construct() {
$container = \System::getContainer();

if(!static::$jsessionid) {
$params = array(
'user' => $container->getParameter('wbgym_untis.username'),
'password' => $container->getParameter('wbgym_untis.password'),
'client' => $container->getParameter('wbgym_untis.client_name')
);
$res = $this->request('authenticate',$params);

if($res->error) throw new \Exception ('WebUntis Authentication error ' . $res->error->code . ': ' . $res->error->message);
elseif(!$res->result) throw new \Exception ('WebUntis Service not available');

static::$jsessionid = $res->result->sessionId;
}
}

/*
* Send a JSON-RPC-request to API
*
* @param $strMethod Method
* @param $strParams Paramters
* @return array response of the request
*/
public function request($strMethod,$arrParams = null) {
$container = \System::getContainer();

static::$currentId = rand();

$strRequest = json_encode(array(
'id' => static::$currentId,
'method' => $strMethod,
'params' => $arrParams,
'jsonrpc' => '2.0'
));
$opts = array(
'http' => array(
'method' => static::$METHOD,
'header' =>
"Content-Type: " . static::$CONTENT_TYPE."\r\n" .
"Connection: " . static::$CONNECTION_TYPE."\r\n" .
"Content-Length: " . strlen($strRequest)."\r\n",
'content' => $strRequest
)
);
if(static::$jsessionid !== null) {
$opts['http']['header'] .= "Cookie: JSESSIONID=".static::$jsessionid."\r\n";
}

$context = stream_context_create($opts);
if(static::$jsessionid !== null) {
$uri = $container->getParameter('wbgym_untis.api_url');
}
else {
$uri = $container->getParameter('wbgym_untis.api_url') . '?school=' . $container->getParameter('wbgym_untis.school_code');
}

$res = json_decode(file_get_contents($uri, false, $context));

if($res->error) {
throw new \Exception('WebUntis Error ' . $res->error->code . ': ' . $res->error->message);
}
else if ($res->id != static::$currentId){
throw new \Exception('WebUntis Error: Request IDs do not match (Requested: '.static::$currentId.', Recieved: '.$res->id);
}
else {
return $res;
}
}

/*
* Logout from the API
*/
public function __destruct() {
$this->request('logout');
static::$jsessionid = null;
}

}
?>
77 changes: 77 additions & 0 deletions src/Resources/contao/classes/WUHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/**
* WBGym
*
* Copyright (C) 2016 Webteam Weinberg-Gymnasium Kleinmachnow
*
* @package WGBym
* @version 0.3.0
* @author Johannes Cram <craj.me@gmail.com>
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL
*/

/**
* Namespace
*/
namespace WBGym;

class WUHelper extends \System
{
/*
* Converts WebUntis dates (e.g. 20160910) to timestamp
*
* @param int $intDate
* @return int
*/
public function dateToTime($intDate) {
if(strlen($intDate) != 8) return false;
$arrDate = array(
'year' => substr($intDate,0,4),
'month' => substr($intDate,4,2),
'day' => substr($intDate,6,2),
);
return strtotime($arrDate['year'] . '-' . $arrDate['month'] . '-' . $arrDate['day']);
}

/*
* Returns the School Hour from beginning and ending (e.g. 730 - 815)
*
* @param int $intStart
* @param int $intEnd
* @return int/str School Hour
*/
public function getSchoolHour($intStart,$intEnd) {
$arrStdBegin = $GLOBALS['TL_LANG']['wbuntis']['school_hours']['begin'];
$arrStdEnd = $GLOBALS['TL_LANG']['wbuntis']['school_hours']['end'];

//Get Beginning
if($arrStdBegin[$intStart])
$strTime = $arrStdBegin[$intStart];

//Get Ending
if($arrStdEnd[$intEnd] && $arrStdBegin[$intStart] != $arrStdEnd[$intEnd])
$strTime .= ' - ' . $arrStdEnd[$intEnd];

if($intStart < 730 && $intEnd > 1725)
$strTime = 'Ganztägig';

return $strTime;
}

/*
* Returns string for substitution type by substitution array
*
* @param array $arrSub
* @return str
*/
public function subType($arrSub) {
if($arrSub['type'] == 'add') {
if($arrSub['su']) $category = 'class';
else $category = 'no_class';
return $GLOBALS['TL_LANG']['wbuntis']['sub_types'][$arrSub['type']][$category];
}
return $GLOBALS['TL_LANG']['wbuntis']['sub_types'][$arrSub['type']];
}

}
Loading

0 comments on commit 5ea6ab5

Please sign in to comment.