예제 #1
0
 /**
  * Retrieves the singleton instance
  * @return DiamondMVC
  */
 public static function instance()
 {
     if (!self::$_instance) {
         self::$_instance = new DiamondMVC();
     }
     return self::$_instance;
 }
예제 #2
0
파일: login.php 프로젝트: Zyr93/DiamondMVC
 protected function action_login()
 {
     $user = DiamondMVC::instance()->getCurrentUser();
     if ($user->isLoggedIn()) {
         if (isset($_REQUEST['returnto'])) {
             redirect(urldecode($_REQUEST['returnto']));
         } else {
             redirect(DIAMONDMVC_URL . Config::main()->get('DEFAULT_LOGIN_REDIRECT'));
         }
         return;
     }
     // Wurden Daten übermittelt?
     if (isset($_REQUEST['login'])) {
         if ($user->login($_REQUEST['username'], $_REQUEST['password'])) {
             if (isset($_REQUEST['returnto'])) {
                 redirect(urldecode($_REQUEST['returnto']));
             } else {
                 redirect(DIAMONDMVC_URL . Config::main()->get('DEFAULT_LOGIN_REDIRECT'));
             }
             return;
         } else {
             $this->addMessage('Error', 'The given email-password combination was not found.', 'error');
         }
     }
 }
예제 #3
0
 /**
  * Registers standard fields and triggers the formbuilder::register-fields
  * {@link Event} for plugins to register additional fields.
  */
 public static function registerFields()
 {
     self::registerField(0, 'FieldTextBox');
     self::registerField(1, 'FieldFulltext');
     self::registerField(2, 'FieldSearchBox');
     DiamondMVC::instance()->trigger('formbuilder::register-fields');
     logMsg("FormBuilder: registered fields:\n" . print_r(self::$fieldmap, true), 1, 0);
 }
예제 #4
0
파일: fields.php 프로젝트: Zyr93/DiamondMVC
 public function __construct($db = null)
 {
     parent::__construct('fields', $db);
     DiamondMVC::instance()->on('formbuilder::register-fields', $this);
 }
예제 #5
0
 /**
  * Outputs the contents of the snippet (from memory).
  * @return Snippet This instance to enable method chaining.
  */
 public function render()
 {
     $evt = new RenderEvent($this);
     DiamondMVC::instance()->trigger($evt);
     if (!$evt->isDefaultPrevented()) {
         echo $this->getContents();
     }
     return $this;
 }
예제 #6
0
파일: fns.php 프로젝트: Zyr93/DiamondMVC
/**
 * Schreibt einen MySQL-Query in die entsprechende Log-Datei.
 * Queries werden nur im Debug-Mode geloggt, um überflüssige Dateisystemzugriffe zu vermeiden.
 * @param string $query  Zu loggender MySQL-Query.
 * @param string $bind   Typendefinition der Marker
 * @param array  $params Zu bindende Parameter
 */
function logQuery($query, $bind = '', $params = null)
{
    if (!Config::main()->isDebugMode()) {
        return;
    }
    $user = DiamondMVC::instance()->getCurrentUser();
    if (!is_array($params)) {
        $params = array();
    }
    if ($user->isLoggedIn()) {
        $file = date('Y-m-d-') . $user->getName();
    } else {
        $ip = $_SERVER['REMOTE_ADDR'];
        if ($ip === '127.0.0.1' or $ip === '::1') {
            $ip = 'localhost';
        }
        $file = date('Y-m-d-') . $ip;
    }
    $file = DIAMONDMVC_ROOT . '/logs/' . $file . '.qlg.txt';
    if (!is_dir(DIAMONDMVC_ROOT . '/logs')) {
        mkdir(DIAMONDMVC_ROOT . '/logs');
    }
    $res = fopen($file, 'a');
    if (!is_resource($res)) {
        trigger_error('Log-Datei ' . $file . ' konnte nicht geöffnet werden.', E_USER_WARNING);
    } else {
        fwrite($res, date('[H:i:s]') . $query . PHP_EOL);
        if (!empty($bind)) {
            fwrite($res, "\tBind ({$bind}): " . implode(', ', array_map('mapArgs', $params)) . PHP_EOL);
        }
        fclose($res);
    }
}
예제 #7
0
 /**
  * Gibt das Template aus.
  * @param  string   $view Name der darzustellenden View.
  * @return Template       Diese Instanz zur Methodenverkettung.
  */
 public function render($view = '')
 {
     $this->view = $view;
     $evt = new RenderEvent($this);
     DiamondMVC::instance()->trigger($evt);
     if ($evt->isDefaultPrevented()) {
         return $this;
     }
     $view = $this->controller->getView($view)->read();
     ob_start();
     require_once DIAMONDMVC_ROOT . '/templates/' . $this->name . '/index.php';
     $content = ob_get_contents();
     ob_end_clean();
     // Platzhalter ersetzen.
     foreach ($this->bind as $bind => $to) {
         $content = str_replace('${' . $bind . '}', $to, $content);
     }
     $content = preg_replace('/\\$\\{[^\\}]*\\}/', '', $content);
     // Ressourcen ab <head> anhängen.
     $pos = strpos($content, '</head>');
     $preInject = substr($content, 0, $pos);
     $postInject = substr($content, $pos);
     $inject = $this->meta . $view->getMeta();
     // Inject the AMD modules and their dependencies.
     $scripts = array_merge($this->scripts, $view->getScripts());
     $sheets = array_merge($this->stylesheets, $view->getStylesheets());
     // Assemble list of stylesheets to include.
     foreach ($this->controller->getAllModules() as $modules) {
         foreach ($modules as $module) {
             foreach ($module->getScripts() as $script) {
                 $scripts[] = $script;
             }
             foreach ($module->getStylesheets() as $sheet) {
                 $sheets[] = $sheet;
             }
         }
     }
     // No duplicates
     array_unique($sheets);
     array_unique($scripts);
     $inject .= '<script type="applicaton/json" id="amd-modules">' . json_encode($scripts) . '</script>';
     foreach ($sheets as $sheet) {
         $mime = 'stylesheet';
         if (($index = strpos($sheet, ';')) !== false) {
             $mime = substr($sheet, $index + 1);
             $sheet = substr($sheet, 0, $index);
         }
         $inject .= '<link rel="' . $mime . '" href="' . $sheet . '">';
     }
     $content = $preInject . $inject . $postInject;
     $evt = new Event('render');
     $evt->source = 'template';
     $evt->content = $content;
     DiamondMVC::instance()->trigger($evt);
     if (!$evt->isDefaultPrevented()) {
         echo $evt->content;
     }
     return $this;
 }
예제 #8
0
파일: upload.php 프로젝트: Zyr93/DiamondMVC
 /**
  * Triggers the {@link UploadEvent} which allows handlers to decide whether to keep or dump the
  * uploaded file. If kept, the file is moved to the /uploads directory using the name returned
  * by {@link UploadEvent#getName()}.
  * @param  string  $prop Name of the $_FILES superglobal property the uploaded file is associated with
  * @param  array   $data Entry in the $_FILES superglobal array
  * @return boolean       Whether the uploaded file was kept
  */
 protected function handleUpload($prop, $data)
 {
     // Upload accepted by all handlers?
     $evt = new UploadEvent($prop, $data);
     DiamondMVC::instance()->trigger($evt);
     if ($evt->isDefaultPrevented()) {
         return false;
     }
     // Error during the upload process itself?
     if ($evt->hasError()) {
         return false;
     }
     $name = $evt->getName();
     $dest = DIAMONDMVC_ROOT . '/uploads/' . $name;
     if (isset($this->paths[$prop])) {
         $tmp = $this->paths[$prop];
         $index = strpos($tmp, ';');
         $path = substr($tmp, 0, $index);
         $append = intval(substr($tmp, $index + 1));
         $dest = $path;
         if ($append) {
             $dest .= "/{$name}";
         }
     }
     logMsg('ControllerUpload: uploading to ' . $dest, 9, false);
     if (!move_uploaded_file($data['tmp_name'], $dest)) {
         $this->addMessage('Sorry!', 'Upload failed on server side!', 'error');
         logMsg("Failed to move uploaded file {$data['tmp_name']} to " . DIAMONDMVC_ROOT . "/uploads/{$name}", 9, 5);
         return false;
     }
     return true;
 }
예제 #9
0
 public function __construct($db = null)
 {
     parent::__construct('errortpl', $db);
     DiamondMVC::instance()->on('system::action', $this);
 }
예제 #10
0
 /**
  * Performs the given action, if it exists. The action saves its result in {@link #result},
  * retrievable through {@link #getResult()}.
  * 
  * This method is supposed to act as a sort of factory method for all actions a controller
  * provides. As such the actions themselves ought to be protected from external direct access.
  * However, if you wish to directly expose your actions, be sure to clean the controller's
  * state using {@link #cleanup()}.
  * @param  string $action  Name of the action to perform
  * @param  mixed  $args... Arguments to pass to the action. Usually none
  * @return mixed           Result of the action, usually stored in {@link #result}.
  */
 public function action()
 {
     $args = func_get_args();
     $action = array_shift($args);
     $this->cleanup();
     if (method_exists($this, "action_{$action}")) {
         $this->action = $action;
         DiamondMVC::instance()->trigger('controller::action', $this);
         return call_user_func_array(array($this, "action_{$action}"), $args);
     }
     return null;
 }
예제 #11
0
<?php

/**
 * @package  DiamondMVC
 * @author   Zyr <*****@*****.**>
 * @version  1.0
 * @license  CC-SA 4.0 (https://creativecommons.org/licenses/by-sa/4.0/)
 * 
 * An event can be created and triggered via DiamondMVC by any component. Plugins can then
 * manipulate or react to the information stored in this object to customize the behavior
 * of algorithms.
 */
defined('DIAMONDMVC') or die;
DiamondMVC::instance()->loadLibrary('events');
class Event
{
    private $namespace = '';
    private $name = '';
    private $isDefaultPrevented = false;
    private $stopPropagation = false;
    public function __construct($name)
    {
        if (($index = strrpos($name, '::')) !== false) {
            $this->namespace = substr($name, 0, $index);
            $this->name = substr($name, $index + 2);
        } else {
            $this->name = $name;
        }
    }
    public function getNamespace()
    {
예제 #12
0
 /**
  * Sets the permission level for the associated user.
  * @param  integer $userid Unique ID of the user to set the permission level for. If omitted defaults to the current user.
  * @param  string  $name   of the permission to check for
  * @param  integer $level  Permission level to set
  * @return boolean         True if the permission was successfully set, otherwise false. False is also returned if a database error occurred or the requested permission does not exist.
  */
 public static function set($userid, $name = '', $level = 0)
 {
     $db =& self::$db;
     if (is_string($userid)) {
         $level = $name;
         $name = $userid;
         $userid = DiamondMVC::instance()->getCurrentUser()->getUID();
     }
     // Get the permission ID for easier access
     $db->pushState()->select('sys_perms')->fields('uid')->filter('name=?')->bind('s', $name)->ignoreDeleted()->ignoreHidden()->seek();
     if (!$db->found()) {
         $db->popState();
         logMsg("Permissions: Permission {$name} not found", 9, 5);
         return false;
     }
     $model = (new ModelRow($db, '', '', ''))->read($db->getData());
     $permid = intval($model->get('uid'));
     $db->popState();
     // Check if the user already has this permission granted. If so, we'll simply update it.
     $db->pushState()->select('sys_user_perms')->fields('level')->fielddef('i')->filter('userid=? and permid=?')->bind('is', $userid, $permid)->ignoreDeleted()->ignoreHidden()->seek();
     if ($db->found()) {
         $db->replace($level);
         if (!$db->found()) {
             logMsg('Permissions: Failed to update user permission ' . $name . ' for user with ID ' . $userid, 9, 5);
             $db->popState();
             return false;
         }
         $db->popState();
         return true;
     }
     $db->popState();
     // Otherwise we'll create a new data set for it.
     $db->pushState()->select('sys_user_perms')->fields('permid, userid, level')->fielddef('iii')->ignoreDeleted()->ignoreHidden()->append($permid, $userid, $level);
     if (!$db->found()) {
         logMsg('Permissions: Failed to grant user permission ' . $name . ' to user with ID ' . $userid, 9, 5);
         $db->popState();
         return false;
     }
     $db->popState();
     return true;
 }
예제 #13
0
 public function __construct($db = null)
 {
     parent::__construct('uploadextension', $db);
     DiamondMVC::instance()->on('upload', $this);
 }
예제 #14
0
파일: menus.php 프로젝트: Zyr93/DiamondMVC
 public function __construct($db = null)
 {
     parent::__construct('menus', $db);
     DiamondMVC::instance()->on('controller::action', $this);
 }
예제 #15
0
파일: index.php 프로젝트: Zyr93/DiamondMVC
 * @package  DiamondMVC
 * @author   Zyr <*****@*****.**>
 * @version  1.0
 * @license  CC-SA 4.0 (https://creativecommons.org/licenses/by-sa/4.0/)
 * 
 * The main engine of the DiamondMVC which instructs the controllers and renders the views.
 */
define('DIAMONDMVC', '1.1.3');
define('DIAMONDMVC_ROOT', dirname(__FILE__));
$https = $_SERVER['SERVER_PROTOCOL'] === 'https' or $_SERVER['SERVER_PORT'] == 443;
$tmp = ($https ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
$tmp = str_replace('\\', '/', $tmp);
if (substr($tmp, -1) === '/') {
    $tmp = substr($tmp, 0, strlen($tmp) - 1);
}
define('DIAMONDMVC_URL', $tmp, true);
unset($tmp);
defined('DS') or define('DS', DIRECTORY_SEPARATOR);
require_once DIAMONDMVC_ROOT . '/config.php';
require_once DIAMONDMVC_ROOT . '/lib/fns.php';
require_once DIAMONDMVC_ROOT . '/lib/autoload.php';
// So far not needed... thus only an unnecessary safety risk.
// if( isset($_REQUEST['sid']) ) {
// 	session_id($_REQUEST['sid']);
// }
session_start();
if (!DiamondMVC::instance()->isInstalled()) {
    redirect(DIAMONDMVC_URL . '/firstinstallation');
} else {
    DiamondMVC::instance()->run();
}