Browse Source

Remove the widget AdminDB

Fix the syntax to fit with the PSR
Use the argument unpacking syntax for some functions
Simplify RPC
Update the PHP minimal version to PHP 5.6+
pull/551/head
Timothée Jaussoin 8 years ago
parent
commit
7b69a15bc6
  1. 1
      app/views/admin.tpl
  2. 48
      app/widgets/AdminDB/AdminDB.php
  3. 72
      app/widgets/AdminDB/admindb.tpl
  4. 12
      app/widgets/AdminDB/locales.ini
  5. 2
      app/widgets/AdminTest/AdminTest.php
  6. 6
      app/widgets/AdminTest/locales.ini
  7. 2
      composer.json
  8. 13
      linker.php
  9. 6
      src/Movim/Console/ConfigCommand.php
  10. 9
      src/Movim/Console/DaemonCommand.php
  11. 6
      src/Movim/Console/DatabaseCommand.php
  12. 38
      src/Movim/Daemon/Core.php
  13. 33
      src/Movim/Daemon/Session.php
  14. 51
      src/Movim/RPC.php
  15. 16
      src/Movim/Widget/Base.php
  16. 2
      src/Movim/Widget/Wrapper.php

1
app/views/admin.tpl

@ -17,7 +17,6 @@
<?php $this->widget('Tabs');?>
<?php $this->widget('AdminTest');?>
<?php $this->widget('AdminMain');?>
<?php $this->widget('AdminDB');?>
<?php $this->widget('Statistics');?>
<?php $this->widget('Api');?>
</div>

48
app/widgets/AdminDB/AdminDB.php

@ -1,48 +0,0 @@
<?php
class AdminDB extends \Movim\Widget\Base
{
function load() {
}
public function ajaxUpdateDatabase()
{
$md = \modl\Modl::getInstance();
$md->check(true);
$this->rpc('MovimUtils.reloadThis');
}
public function hidePassword($pass)
{
return str_repeat('*', strlen($pass));
}
function display()
{
$md = \Modl\Modl::getInstance();
$infos = $md->check();
$errors = '';
$this->view->assign('infos', $infos);
$this->view->assign('db_update', $this->call('ajaxUpdateDatabase')
."this.className='button color loading';");
try {
$md->connect();
} catch(Exception $e) {
$errors = $e->getMessage();
}
if(file_exists(DOCUMENT_ROOT.'/config/db.inc.php')) {
require DOCUMENT_ROOT.'/config/db.inc.php';
}
$supported = $md->getSupportedDatabases();
$this->view->assign('connected', $md->_connected);
$this->view->assign('conf', $conf);
$this->view->assign('dbtype', $supported[$conf['type']]);
$this->view->assign('errors', $errors);
}
}

72
app/widgets/AdminDB/admindb.tpl

@ -1,72 +0,0 @@
<div id="admindb" class="tabelem" title="{$c->__('db.legend')}">
<ul class="list flex large">
<li class="subheader">
<p>{$c->__('db.legend')}</p>
</li>
{if="!$connected"}
<li class="block large">
<span class="primary icon bubble color red">
<i class="zmdi zmdi-code-setting"></i>
</span>
<p>{$c->__('db.connect_error')}</p>
<p>{$errors}</p>
</li>
{else}
<li class="block large">
<span class="primary icon bubble color green">
<i class="zmdi zmdi-code-setting"></i>
</span>
<p class="normal">{$c->__('db.connect_success')}</p>
</li>
{if="null !== $infos"}
<li class="block large">
<span class="primary icon bubble color orange">
<i class="zmdi zmdi-refresh"></i>
</span>
<span class="control">
<a class="button" onclick="{$db_update}"> {$c->__('button.update')}</a>
</span>
<p>{$c->__('db.update')}</p>
{loop="$infos"}
<p>{$value}</p>
{/loop}
</li>
{else}
<li class="block large">
<span class="primary icon bubble color green">
<i class="zmdi zmdi-refresh"></i>
</span>
<p class="normal">{$c->__('db.up_to_date')}</p>
</li>
{/if}
{/if}
</ul>
<form class="flex padded_top_bottom">
<div class="block">
<input value="{$dbtype}" disabled/>
<label for="logLevel">{$c->__('db.type')}</label>
</div>
<div class="block">
<input value="{$conf.username}" disabled/>
<label for="dbUsername">{$c->__('db.username')}</label>
</div>
<div class="block">
<input value="{$c->hidePassword($conf.password)}" disabled/>
<label for="dbPassword">{$c->__('db.password')}</label>
</div>
<div class="block">
<input value="{$conf.host}" disabled/>
<label for="dbHost">{$c->__('db.host')}</label>
</div>
<div class="block">
<input value="{$conf.port}" disabled/>
<label for="dbPort">{$c->__('db.port')}</label>
</div>
<div class="block">
<input value="{$conf.database}" disabled/>
<label for="dbName">{$c->__('db.name')}</label>
</div>
</form>
</div>

12
app/widgets/AdminDB/locales.ini

@ -1,12 +0,0 @@
[db]
legend = Database
connect_error = "Modl wasn't able to connect to the database"
connect_success = Movim is connected to the database
update = The database need to be updated
up_to_date = Movim database is up to date
type = Database Type
username = Username
password = Password
host = Host
port = Port
name = Database Name

2
app/widgets/AdminTest/AdminTest.php

@ -10,7 +10,7 @@ class AdminTest extends \Movim\Widget\Base
public function version()
{
return (version_compare(PHP_VERSION, '5.4.0') >= 0);
return (version_compare(PHP_VERSION, '5.6.0') >= 0);
}
public function testDir($dir)

6
app/widgets/AdminTest/locales.ini

@ -6,14 +6,14 @@ info = Movim has found some issues or things that need to be fixed
php1 = Update your PHP-Version: %s
php2 = Required: 5.3.0
rights = "Read and write rights for the webserver in Movim's %s directory"
db = The database need to be updated, go to the database panel to fix this
websocket = WebSocket connection error, check if the Movim Daemon is running and is reachable
db = The database need to be updated
websocket = WebSocket connection error, check if the Movim Daemon is running and is reachable
xmpp_websocket = XMPP Websocket connection error, please check the validity of the URL given in the General Configuration
[schema]
browser = Browser
movim = Movim Core
daemon = Movim Daemon
database = Database
database = Database
api = API
xmpp = XMPP

2
composer.json

@ -5,7 +5,7 @@
}
},
"require": {
"php": ">=5.5.0",
"php": ">=5.6.0",
"ext-curl": "*",
"ext-mbstring": "*",
"ext-imagick": "*",

13
linker.php

@ -50,10 +50,8 @@ $loop->addPeriodicTimer(5, function() use(&$conn, &$timestamp) {
}
});
function writeOut()
function writeOut($msg = null)
{
$msg = RPC::commit();
if(!empty($msg)) {
echo base64_encode(gzcompress(json_encode($msg), 9))."";
}
@ -84,7 +82,7 @@ $stdin_behaviour = function ($data) use (&$conn, $loop, &$buffer, &$connector, &
if(isset($msg)) {
switch ($msg->func) {
case 'message':
$msg = $msg->body;
(new RPC)->handleJSON($msg->body);
break;
case 'ping':
@ -140,11 +138,6 @@ $stdin_behaviour = function ($data) use (&$conn, $loop, &$buffer, &$connector, &
$connector->connect($ip.':'.$port)->then($xmpp_behaviour);
break;
}
$rpc = new RPC;
$rpc->handleJSON($msg);
writeOut();
} else {
return;
}
@ -225,8 +218,6 @@ $xmpp_behaviour = function (React\Socket\Connection $stream) use (&$conn, $loop,
if(!$parser->parse($message)) {
fwrite(STDERR, colorize(getenv('sid'), 'yellow')." ".$parser->getError()."\n");
}
writeOut();
}
});

6
src/Movim/Console/ConfigCommand.php

@ -79,12 +79,12 @@ class ConfigCommand extends Command
protected function execute(InputInterface $input, OutputInterface $output)
{
$cd = new \Modl\ConfigDAO();
$cd = new \Modl\ConfigDAO;
$config = $cd->get();
foreach($input->getOptions() as $key => $value) {
if(property_exists($config, $key) && isset($value)) {
if($key == 'password') $value = sha1($value);
if (property_exists($config, $key) && isset($value)) {
if ($key == 'password') $value = sha1($value);
$old = $config->$key;
$config->$key = $value;

9
src/Movim/Console/DaemonCommand.php

@ -84,10 +84,9 @@ class DaemonCommand extends Command
$core = new Core($loop, $baseuri, $input);
$app = new HttpServer(new WsServer($core));
$cd = new \Modl\ConfigDAO;
$config = $cd->get();
$config = (new \Modl\ConfigDAO)->get();
if(empty($config->username) || empty($config->password)) {
if (empty($config->username) || empty($config->password)) {
$output->writeln('<comment>Please set a username and password for the admin panel ('.$baseuri.'?admin)</comment>');
$output->writeln('<info>To set those credentials run</info>');
@ -102,8 +101,6 @@ class DaemonCommand extends Command
$socketApi = new Reactor(1560, $loop);
new Api($socketApi, $core);
$server = new IoServer($app, $socket, $loop);
$server->run();
(new IoServer($app, $socket, $loop))->run();
}
}

6
src/Movim/Console/DatabaseCommand.php

@ -27,14 +27,14 @@ class DatabaseCommand extends Command
{
$md = \Modl\Modl::getInstance();
if($input->getOption('set')) {
if ($input->getOption('set')) {
$md->check(true);
$output->writeln('<info>Database updated</info>');
} else {
$toDo = $md->check();
if($toDo != null) {
if ($toDo != null) {
$output->writeln('<comment>The database needs to be updated</comment>');
foreach($toDo as $do) {
foreach ($toDo as $do) {
$output->writeln('<question>'.$do.'</question>');
}
} else {

38
src/Movim/Daemon/Core.php

@ -28,8 +28,7 @@ class Core implements MessageComponentInterface
$this->loop = $loop;
$this->baseuri = $baseuri;
$sd = new \Modl\SessionxDAO;
$sd->clear();
(new \Modl\SessionxDAO)->clear();
$this->registerCleaner();
}
@ -66,7 +65,7 @@ class Core implements MessageComponentInterface
$path = $explode['host'].$explode['path'];
if($explode['scheme'] == 'https') {
if ($explode['scheme'] == 'https') {
$ws = 'wss://'.$path.'ws/';
$secured = 'true';
echo colorize("Encrypted ", 'green')."\n";
@ -84,11 +83,11 @@ class Core implements MessageComponentInterface
public function onOpen(ConnectionInterface $conn)
{
$sid = $this->getSid($conn);
if($sid != null) {
if ($sid != null) {
$path = $this->getPath($conn);
if(in_array($path, $this->single)) {
if(array_key_exists($sid, $this->singlelocks)
if (in_array($path, $this->single)) {
if (array_key_exists($sid, $this->singlelocks)
&& array_key_exists($path, $this->singlelocks[$sid])) {
$this->singlelocks[$sid][$path]++;
$conn->close(1008);
@ -97,7 +96,7 @@ class Core implements MessageComponentInterface
}
}
if(!array_key_exists($sid, $this->sessions)) {
if (!array_key_exists($sid, $this->sessions)) {
$language = $this->getLanguage($conn);
$offset = $this->getOffset($conn);
$this->sessions[$sid] = new Session(
@ -118,7 +117,7 @@ class Core implements MessageComponentInterface
public function onMessage(ConnectionInterface $from, $msg)
{
$sid = $this->getSid($from);
if($sid != null && isset($this->sessions[$sid])) {
if ($sid != null && isset($this->sessions[$sid])) {
$this->sessions[$sid]->messageIn($msg);
}
}
@ -127,10 +126,10 @@ class Core implements MessageComponentInterface
{
$sid = $this->getSid($conn);
if($sid != null && isset($this->sessions[$sid])) {
if ($sid != null && isset($this->sessions[$sid])) {
$path = $this->getPath($conn);
if(in_array($path, $this->single)) {
if (in_array($path, $this->single)) {
if(array_key_exists($sid, $this->singlelocks)
&& array_key_exists($path, $this->singlelocks[$sid])) {
$this->singlelocks[$sid][$path]--;
@ -141,7 +140,7 @@ class Core implements MessageComponentInterface
}
$this->sessions[$sid]->detach($this->loop, $conn);
if($this->sessions[$sid]->process == null) {
if ($this->sessions[$sid]->process == null) {
unset($this->sessions[$sid]);
}
}
@ -149,7 +148,7 @@ class Core implements MessageComponentInterface
public function forceClose($sid)
{
if(array_key_exists($sid, $this->sessions)) {
if (array_key_exists($sid, $this->sessions)) {
$this->sessions[$sid]->killLinker();
unset($this->sessions[$sid]);
}
@ -159,12 +158,12 @@ class Core implements MessageComponentInterface
{
$this->loop->addPeriodicTimer(5, function() {
foreach($this->sessions as $sid => $session) {
if($session->countClients() == 0
if ($session->countClients() == 0
&& $session->registered == null) {
$session->killLinker();
}
if($session->process == null) {
if ($session->process == null) {
unset($this->sessions[$sid]);
}
}
@ -175,11 +174,8 @@ class Core implements MessageComponentInterface
private function cleanupDBSessions()
{
$sd = new \Modl\SessionxDAO;
$sd->deleteEmpty();
$pd = new \Modl\PresenceDAO;
$pd->cleanPresences();
(new \Modl\SessionxDAO)->deleteEmpty();
(new \Modl\PresenceDAO)->cleanPresences();
}
public function onError(ConnectionInterface $conn, \Exception $e)
@ -216,14 +212,14 @@ class Core implements MessageComponentInterface
private function getPath(ConnectionInterface $conn)
{
parse_str($conn->httpRequest->getUri()->getQuery(), $arr);
return (isset($arr['path'])) ? $arr['path'] : false;
return $arr['path'] ?? false;
}
private function getSid(ConnectionInterface $conn)
{
$cookies = Cookies::fromRequest($conn->httpRequest);
if($cookies->get('MOVIM_SESSION_ID')) {
if ($cookies->get('MOVIM_SESSION_ID')) {
return $cookies->get('MOVIM_SESSION_ID')->getValue();
} else {
return null;

33
src/Movim/Daemon/Session.php

@ -45,11 +45,11 @@ class Session
{
$this->clients->attach($conn);
if($this->verbose) {
if ($this->verbose) {
echo colorize($this->sid, 'yellow'). " : ".colorize($conn->resourceId." connected\n", 'green');
}
if($this->countClients() > 0) {
if ($this->countClients() > 0) {
$this->stateOut('up');
}
}
@ -58,13 +58,13 @@ class Session
{
$this->clients->detach($conn);
if($this->verbose) {
if ($this->verbose) {
echo colorize($this->sid, 'yellow'). " : ".colorize($conn->resourceId." deconnected\n", 'red');
}
if($this->countClients() == 0) {
if ($this->countClients() == 0) {
$loop->addPeriodicTimer(20, function($timer) {
if($this->countClients() == 0) {
if ($this->countClients() == 0) {
$this->stateOut('down');
}
$timer->cancel();
@ -99,7 +99,7 @@ class Session
// Buffering the incoming data and fire it once its complete
$this->process->stdout->on('data', function($output) use ($me, &$buffer) {
if(substr($output, -1) == "") {
if (substr($output, -1) == "") {
$out = $buffer . substr($output, 0, -1);
$buffer = '';
$me->messageOut($out);
@ -110,26 +110,23 @@ class Session
// The linker died, we close properly the session
$this->process->on('exit', function($output) use ($me) {
if($me->verbose) {
if ($me->verbose) {
echo colorize($this->sid, 'yellow'). " : ".colorize("linker killed \n", 'red');
}
$me->process = null;
$me->closeAll();
$pd = new \Modl\PresenceDAO;
$pd->clearPresence();
$sd = new \Modl\SessionxDAO;
$sd->delete($this->sid);
(new \Modl\PresenceDAO)->clearPresence();
(new \Modl\SessionxDAO)->delete($this->sid);
});
$self = $this;
$this->process->stderr->on('data', function($output) use ($me, $self) {
if(strpos($output, 'registered') !== false) {
if (strpos($output, 'registered') !== false) {
$self->registered = true;
} elseif(strpos($output, 'started') !== false) {
} elseif (strpos($output, 'started') !== false) {
$self->started = true;
} else {
echo $output;
@ -139,7 +136,7 @@ class Session
public function killLinker()
{
if(isset($this->process)) {
if (isset($this->process)) {
$this->process->terminate();
$this->process = null;
}
@ -154,9 +151,9 @@ class Session
public function stateOut($state)
{
if($this->state == $state) return;
if ($this->state == $state) return;
if(isset($this->process)) {
if (isset($this->process)) {
$this->state = $state;
$msg = new \stdClass;
$msg->func = $this->state;
@ -168,7 +165,7 @@ class Session
public function messageIn($msg)
{
$this->timestamp = time();
if(isset($this->process)) {
if (isset($this->process)) {
$this->process->stdin->write($msg."");
}
unset($msg);

51
src/Movim/RPC.php

@ -6,27 +6,12 @@ use Movim\Widget\Wrapper;
class RPC
{
protected static $funcall;
public static function call($funcname)
public static function call($funcname, ...$args)
{
$args = func_get_args();
array_shift($args);
self::$funcall = [
writeOut([
'func' => $funcname,
'params' => $args,
];
writeOut();
}
/**
* Sends outgoing requests.
*/
public static function commit()
{
return self::$funcall;
]);
}
/**
@ -34,31 +19,13 @@ class RPC
*/
public function handleJSON($request)
{
// Loading the widget.
if(isset($request->widget)) {
$widget_name = (string)$request->widget;
} else {
return;
}
$result = [];
// Preparing the parameters and calling the function.
if(isset($request->params)) {
$params = (array)$request->params;
foreach($params as $p) {
if(is_object($p) && isset($p->container)) {
array_push($result, (array)$p->container);
} else {
array_push($result, $p);
}
}
}
if(!isset($request->widget)) return;
$widgets = new Wrapper;
$widgets->runWidget($widget_name, (string)$request->func, $result);
(new Wrapper)->runWidget(
(string)$request->widget,
(string)$request->func,
(array)$request->params
);
}
}
?>

16
src/Movim/Widget/Base.php

@ -88,14 +88,14 @@ class Base
unset($this->_view);
}
function __()
function __(...$args)
{
return call_user_func_array('__', func_get_args());
return __(...$args);
}
function ___()
function ___(...$args)
{
echo call_user_func_array([&$this, '__'], func_get_args());
echo call_user_func_array([&$this, '__'], $args);
}
function supported($key)
@ -103,14 +103,14 @@ class Base
return $this->user->isSupported($key);
}
function route()
function route(...$args)
{
return call_user_func_array('\Movim\Route::urlize', func_get_args());
return \Movim\Route::urlize(...$args);
}
function rpc()
function rpc(...$args)
{
return call_user_func_array('\Movim\RPC::call', func_get_args());
return \Movim\RPC::call(...$args);
}
function load() {}

2
src/Movim/Widget/Wrapper.php

@ -142,7 +142,7 @@ class Wrapper
$params = [];
}
return call_user_func_array([$widget, $method], $params);
return $widget->$method(...$params);
}
/**

Loading…
Cancel
Save