issue #55: adding xhr functionality to controllers

This commit is contained in:
Pepijn Over 2014-04-24 23:31:48 +02:00
parent 54de2fcf9c
commit b4a7ab3ba7
15 changed files with 295 additions and 4165 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@
/config.php
/build
/docs/_build
/vendor/

View File

@ -12,6 +12,11 @@ export:
@echo 'Testing on syntax errors (thats all the automated testing your are going to get for now..) '
find ./build/phpservermon -name "*.php" | xargs -I file php -l file
find ./build/phpservermon -name "*.php" -exec sed -i "" "s/@package_version@/$(tag)/" {} \;
@echo 'Downloading dependencies'
cd ./build/phpservermon; php composer.phar install; cd ../../;
rm -f ./build/phpservermon/composer.phar
rm -f ./build/phpservermon/composer.json
rm -f ./build/phpservermon/composer.lock
@echo 'Building HTML documentation'
cd ./build/phpservermon/docs; make BUILDDIR=. html; cd ../../../;
@echo 'Cleaning up docs dir'

View File

@ -70,6 +70,12 @@ Install
Please see docs/install.rst.
In a nutshell: unzip, upload, run install.php, enjoy.
If you have downloaded the source from GitHub (and not a pre-built package), the dependencies are not included.
To be able to run an installation from the repo, you need to run the following command to install the dependencies::
php composer.phar install
Documentation
-------------

10
composer.json Normal file
View File

@ -0,0 +1,10 @@
{
"name": "phpservermon/phpservermon",
"description": "PHP Server Monitor",
"homepage": "http://www.phpservermonitor.org",
"require": {
"php": ">=5.3.7",
"phpmailer/phpmailer": "5.2.6",
"symfony/http-foundation": "2.4.*"
}
}

133
composer.lock generated Normal file
View File

@ -0,0 +1,133 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file"
],
"hash": "63c9618391ed04b38b098c0139b50570",
"packages": [
{
"name": "phpmailer/phpmailer",
"version": "v5.2.6",
"source": {
"type": "git",
"url": "https://github.com/PHPMailer/PHPMailer.git",
"reference": "4d9434e394496a5bb7acd9e73046587184b413df"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/PHPMailer/PHPMailer/zipball/4d9434e394496a5bb7acd9e73046587184b413df",
"reference": "4d9434e394496a5bb7acd9e73046587184b413df",
"shasum": ""
},
"require": {
"php": ">=5.0.0"
},
"require-dev": {
"phpdocumentor/phpdocumentor": "*",
"phpunit/phpunit": "*"
},
"type": "library",
"autoload": {
"classmap": [
"class.phpmailer.php",
"class.pop3.php",
"class.smtp.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"LGPL-2.1"
],
"authors": [
{
"name": "Jim Jagielski",
"email": "jimjag@gmail.com"
},
{
"name": "Marcus Bointon",
"email": "phpmailer@synchromedia.co.uk"
},
{
"name": "Andy Prevost",
"email": "codeworxtech@users.sourceforge.net"
},
{
"name": "Brent R. Matzelle"
}
],
"description": "PHPMailer is a full-featured email creation and transfer class for PHP",
"time": "2013-04-11 16:45:39"
},
{
"name": "symfony/http-foundation",
"version": "v2.4.3",
"target-dir": "Symfony/Component/HttpFoundation",
"source": {
"type": "git",
"url": "https://github.com/symfony/HttpFoundation.git",
"reference": "11ad7352c7bfa12145a0df8e1c4c303c585ce169"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/HttpFoundation/zipball/11ad7352c7bfa12145a0df8e1c4c303c585ce169",
"reference": "11ad7352c7bfa12145a0df8e1c4c303c585ce169",
"shasum": ""
},
"require": {
"php": ">=5.3.3"
},
"require-dev": {
"symfony/expression-language": "~2.4"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.4-dev"
}
},
"autoload": {
"psr-0": {
"Symfony\\Component\\HttpFoundation\\": ""
},
"classmap": [
"Symfony/Component/HttpFoundation/Resources/stubs"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com",
"homepage": "http://fabien.potencier.org",
"role": "Lead Developer"
},
{
"name": "Symfony Community",
"homepage": "http://symfony.com/contributors"
}
],
"description": "Symfony HttpFoundation Component",
"homepage": "http://symfony.com",
"time": "2014-03-26 11:35:33"
}
],
"packages-dev": [
],
"aliases": [
],
"minimum-stability": "stable",
"stability-flags": [
],
"platform": {
"php": ">=5.3.7"
},
"platform-dev": [
]
}

BIN
composer.phar Normal file

Binary file not shown.

View File

@ -64,6 +64,15 @@ One of the new features introduced in 3.0 is a user authentication system. Becau
For that reason the upgrade script will ask you to create a new account during the upgrade, which you can then use to change the password for the existing accounts.
If, for whatever reason this does not work, the upgrade script automatically changes the username of all existing users to their email addresses, which you could use for the forgot password screen.
Installing from GitHub
++++++++++++++++++++++
If you have downloaded the source from GitHub (and not a pre-built package), the dependencies are not included.
To be able to run an installation from the repo, you need to run the following command to install the dependencies::
php composer.phar install
Setting up a cronjob
++++++++++++++++++++

View File

@ -28,7 +28,6 @@
// Include paths
define('PSM_PATH_SRC', dirname(__FILE__) . DIRECTORY_SEPARATOR);
define('PSM_PATH_VENDOR', PSM_PATH_SRC . '..' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR);
define('PSM_PATH_INC', PSM_PATH_SRC . 'includes' . DIRECTORY_SEPARATOR);
define('PSM_PATH_TPL', PSM_PATH_SRC . 'templates' . DIRECTORY_SEPARATOR);
define('PSM_PATH_LANG', PSM_PATH_SRC . 'lang' . DIRECTORY_SEPARATOR);
@ -56,29 +55,28 @@ if(defined('PSM_DEBUG') && PSM_DEBUG) {
ini_set('display_errors', 0);
}
$vendor_autoload = PSM_PATH_SRC . '..' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
if(!file_exists($vendor_autoload)) {
die('No dependencies found in vendor dir. Did you install the dependencies? Please run "php composer.phar install".');
}
require_once $vendor_autoload;
// set autoloader, make sure to set $prepend = true so that our autoloader is called first
function __autoload($class) {
spl_autoload_register(function($class) {
// remove leading \
$class = ltrim($class, '\\');
$path_parts = explode('\\', $class);
$filename = array_pop($path_parts);
$path = implode(DIRECTORY_SEPARATOR, $path_parts) .
$path = PSM_PATH_SRC . implode(DIRECTORY_SEPARATOR, $path_parts) .
DIRECTORY_SEPARATOR .
$filename . '.class.php'
;
// search in these dirs:
$basedirs = array(
PSM_PATH_SRC,
PSM_PATH_VENDOR
);
foreach($basedirs as $dir) {
if(file_exists($dir . $path)) {
require_once $dir . $path;
return;
}
if(file_exists($path)) {
require_once $path;
return;
}
}
});
// auto-find all include files
$includes = glob(PSM_PATH_INC . '*.inc.php');

View File

@ -373,15 +373,15 @@ function psm_update_available() {
}
/**
* Prepare a new Mailer util.
* Prepare a new phpmailer instance.
*
* If the from name and email are left blank they will be prefilled from the config.
* @param string $from_name
* @param string $from_email
* @return \psm\Util\Mailer
* @return \PHPMailer
*/
function psm_build_mail($from_name = null, $from_email = null) {
$phpmailer = new \psm\Util\Mailer();
$phpmailer = new \PHPMailer();
$phpmailer->Encoding = "base64";
$phpmailer->SMTPDebug = false;

View File

@ -28,6 +28,7 @@
namespace psm\Module;
use psm\Service\Database;
use psm\Service\Template;
use Symfony\Component\HttpFoundation\Response;
abstract class AbstractController implements ControllerInterface {
@ -124,22 +125,37 @@ abstract class AbstractController implements ControllerInterface {
*/
protected $user_level_required_actions = array();
/**
* XHR mode?
* @var boolean $xhr
* @see isXHR()
*/
protected $xhr = false;
function __construct(Database $db, Template $tpl) {
$this->db = $db;
$this->tpl = $tpl;
}
/**
* Initialize the module
* Initialize the controller.
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function initialize() {
$action = psm_GET('action', psm_POST('action', $this->action_default));
$this->xhr = (bool) psm_GET('xhr', psm_POST('xhr', false));
if(!in_array($action, $this->actions) || !$this->initializeAction($action)) {
$this->initializeAction($this->action_default);
if(!in_array($action, $this->actions) || !($result = $this->initializeAction($action))) {
$result = $this->initializeAction($this->action_default);
}
$this->createHTML();
if($result instanceof Response) {
return $result;
}
// no response returned from execute, create regular HTML
return $this->createHTML();
}
/**
@ -147,7 +163,7 @@ abstract class AbstractController implements ControllerInterface {
*
* For it to run, the "execute$action" method must exist.
* @param string $action
* @return boolean whether action has been initialized successfully
* @return mixed FALSE when action couldnt be initialized, response otherwise
*/
protected function initializeAction($action) {
if(isset($this->user_level_required_actions[$action])) {
@ -161,63 +177,80 @@ abstract class AbstractController implements ControllerInterface {
$method = 'execute' . ucfirst($action);
if(method_exists($this, $method)) {
$this->action = $action;
$this->$method();
return true;
$result = $this->$method();
// if result from execute is null, no return value given so return true to indicate a successful execute
return ($result === null) ? true : $result;
}
return false;
}
/**
* Create the HTML code for the module.
*
* First the createHTMLLabels() will be called to add all labels to the template,
* Then the tpl_id set in $this->getTemplateId() will be added to the main template automatically
* Then the tpl_id set in $this->getTemplateId() will be added to the main template automatically.
* If XHR is on, no main template will be added.
*
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function createHTML() {
$tpl_data = array();
if(!empty($this->messages)) {
$this->tpl->addTemplateDataRepeat('main', 'messages', $this->messages);
}
// add menu to page?
if($this->add_menu) {
$tpl_data['html_menu'] = $this->createHTMLMenu();
}
// add modal dialog to page ?
if(sizeof($this->modal)) {
$html_modal = '';
foreach ($this->modal as $modal)
{
$html_modal .= $modal->createHTML();
}
$tpl_data['html_modal'] = $html_modal;
}
// add sidebar to page?
if($this->sidebar !== null) {
$tpl_data['html_sidebar'] = $this->sidebar->createHTML();
}
// add footer to page?
if($this->add_footer) {
$this->tpl->newTemplate('main_footer', 'main.tpl.html');
$tpl_data['html_footer'] = $this->tpl->getTemplate('main_footer');
$tpl_data['version'] = 'v' . PSM_VERSION;
}
$tpl_id_content = $this->getTemplateId();
if($tpl_id_content) {
$tpl_data['content'] = $this->tpl->getTemplate($tpl_id_content);
$html = '';
if($this->xhr) {
// in XHR mode, we will not add the main template
if($tpl_id_content) {
$this->createHTMLLabels();
$html = $this->tpl->display($tpl_id_content);
}
} else {
// regular request, add main frame
$tpl_data = array();
if(!empty($this->messages)) {
$this->tpl->addTemplateDataRepeat('main', 'messages', $this->messages);
}
// add menu to page?
if($this->add_menu) {
$tpl_data['html_menu'] = $this->createHTMLMenu();
}
// add modal dialog to page ?
if(sizeof($this->modal)) {
$html_modal = '';
foreach($this->modal as $modal) {
$html_modal .= $modal->createHTML();
}
$tpl_data['html_modal'] = $html_modal;
}
// add sidebar to page?
if($this->sidebar !== null) {
$tpl_data['html_sidebar'] = $this->sidebar->createHTML();
}
// add footer to page?
if($this->add_footer) {
$this->tpl->newTemplate('main_footer', 'main.tpl.html');
$tpl_data['html_footer'] = $this->tpl->getTemplate('main_footer');
$tpl_data['version'] = 'v' . PSM_VERSION;
}
if($tpl_id_content) {
$tpl_data['content'] = $this->tpl->getTemplate($tpl_id_content);
}
if(psm_update_available()) {
$tpl_data['update_available'] = str_replace('{version}', 'v'.psm_get_conf('version_update_check'), psm_get_lang('system', 'update_available'));
}
// add the module's custom template to the main template to get some content
$this->setTemplateId('main');
$this->tpl->addTemplatedata($this->getTemplateId(), $tpl_data);
$this->createHTMLLabels();
$html = $this->tpl->display($this->getTemplateId());
}
if(psm_update_available()) {
$tpl_data['update_available'] = str_replace('{version}', 'v'.psm_get_conf('version_update_check'), psm_get_lang('system', 'update_available'));
}
$response = new Response($html);
// add the module's custom template to the main template to get some content
$this->setTemplateId('main');
$this->tpl->addTemplatedata($this->getTemplateId(), $tpl_data);
$this->createHTMLLabels();
// display main template
echo $this->tpl->display($this->getTemplateId());
return $response;
}
/**
@ -280,8 +313,6 @@ abstract class AbstractController implements ControllerInterface {
* @see createHTML()
*/
protected function createHTMLLabels() {
global $type;
$this->tpl->addTemplateData(
'main',
array(
@ -465,4 +496,12 @@ abstract class AbstractController implements ControllerInterface {
$this->modal[$modal->getModalID()] = $modal;
return $this;
}
/**
* Check if XHR is on
* @return boolean
*/
public function isXHR() {
return $this->xhr;
}
}

View File

@ -27,6 +27,7 @@
**/
namespace psm;
use Symfony\Component\HttpFoundation\Response;
/**
* The router class opens the controller and initializes the module.
@ -97,6 +98,7 @@ class Router {
* If no mod is given it will attempt to load the default module.
* @param string $mod if empty, the mod getvar will be used, or fallback to default
* @throws \InvalidArgumentException
* @throws \LogicException
*/
public function run($mod = null) {
if($mod === null) {
@ -127,7 +129,12 @@ class Router {
$controller->setUser($this->services['user']);
// let the module prepare it's HTML code
$controller->initialize();
$response = $controller->initialize();
if(!($response instanceof Response)) {
throw new \LogicException('Controller did not return a Response object.');
}
$response->send();
}
/**

View File

@ -1,55 +0,0 @@
<?php
/**
* PHP Server Monitor
* Monitor your servers and websites.
*
* This file is part of PHP Server Monitor.
* PHP Server Monitor is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PHP Server Monitor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PHP Server Monitor. If not, see <http://www.gnu.org/licenses/>.
*
* @package phpservermon
* @author Pepijn Over <pep@neanderthal-technology.com>
* @copyright Copyright (c) 2008-2014 Pepijn Over <pep@neanderthal-technology.com>
* @license http://www.gnu.org/licenses/gpl.txt GNU GPL v3
* @version Release: @package_version@
* @link http://www.phpservermonitor.org/
* @since phpservermon 3.0.0
**/
namespace psm\Util;
/**
* PHPMailer is not using namespaces so unable to load files in autoloader.
*/
require_once(PSM_PATH_VENDOR . '/PHPMailer/class.phpmailer.php');
require_once(PSM_PATH_VENDOR . '/PHPMailer/class.smtp.php');
/**
* PSM Mailer utility
*
* The PHPMailer is an open source lib that can be found in vendor/PHPMailer.
*
* @see \PHPMailer
*/
class Mailer extends \PHPMailer {
/**
* Open new PHPMailer
*
* @param boolean $exceptions
*/
function __construct($exceptions = false) {
parent::__construct($exceptions);
}
}

View File

@ -33,6 +33,24 @@ $().ready(function() {
psm_tooltips();
});
function psm_xhr(mod, params, method, on_complete, options) {
method = (typeof method == 'undefined') ? 'GET' : method;
var xhr_options = {
data: params,
type: method,
success: on_complete,
error: function(jqjqXHR, textStatus, errorThrown) {
psm_flash_message(errorThrown);
}
};
$.extend(xhr_options, options);
var result = $.ajax('index.php?xhr=1&mod=' + mod, xhr_options);
return result;
}
function psm_tooltips() {
$('input[data-toggle="tooltip"]').tooltip({
'trigger':'hover',

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff