Пример #1
0
 /**
  * Query database
  * If array is passed as a parameter, queries from array will be commited in single transaction. If any query
  * fail during transaction, all transaction will be cancelled.
  * @throws Exception
  * @param string|array $query
  * @return void
  */
 public function query($query)
 {
     if (!is_array($query)) {
         $this->connect();
         Debugger::prepareDBLine();
         $this->queries++;
         Debugger::addDBLine('MySQL', $query);
     } else {
         try {
             if (Debugger::isEnabled()) {
                 $timer = microtime(true);
                 Debugger::addDBLine('MySQL', 'Transaction start');
             }
             $this->transactionStart();
             foreach ($query as $subQuery) {
                 $this->realQuery($subQuery);
                 $this->queries++;
                 Debugger::addDBLine('MySQL', $query);
             }
             $this->transactionCommit();
             if (Debugger::isEnabled()) {
                 /** @noinspection PhpUndefinedVariableInspection */
                 Debugger::addDBLine('MySQL', 'Transaction commited in ' . number_format((microtime(true) - $timer) * 1000, 1) . 'ms');
             }
         } catch (Exception $ex) {
             $this->transactionCancel();
             Debugger::addDBLine('MySQL', 'Transaction failed: ' . $ex->getMessage());
             throw new Exception('MySQL transaction failed because of ' . $ex->getMessage());
         }
     }
 }
Пример #2
0
 public function dispatch()
 {
     if (!\Difra\Debugger::isEnabled()) {
         throw new \Difra\View\HttpError(404);
     }
     \Difra\View::$instance = 'adm';
 }
Пример #3
0
 /**
  * Convert LESS to CSS
  * @param string $string
  * @return string
  */
 public static function compile($string)
 {
     self::init();
     $parser = new \Less_Parser();
     $parser->SetOptions(['compress' => !Debugger::isEnabled()]);
     $parser->parse($string);
     return $parser->getCss();
 }
Пример #4
0
 /**
  * Choose most suitable file
  * @param $file
  * @return mixed|string
  */
 private function getFile($file)
 {
     $debuggerEnabled = Debugger::isEnabled();
     if (!$debuggerEnabled and !empty($file['min'])) {
         return file_get_contents($file['min']);
     } elseif (!$debuggerEnabled and !empty($file['raw'])) {
         return Minify::getInstance($this->type)->minify(file_get_contents($file['raw']));
     } elseif ($debuggerEnabled and !empty($file['raw'])) {
         return file_get_contents($file['raw']);
     } elseif ($debuggerEnabled and !empty($file['min'])) {
         return file_get_contents($file['min']);
     }
     return '';
 }
Пример #5
0
 /**
  * @param \DOMDocument $xml
  * @param bool|string $specificInstance
  * @param bool $dontEcho
  * @param bool $dontFillXML
  * @param bool $normalize
  * @return bool|string
  * @throws Exception
  */
 public static function render(&$xml, $specificInstance = false, $dontEcho = false, $dontFillXML = false, $normalize = true)
 {
     if ($specificInstance) {
         $instance = $specificInstance;
     } elseif (self::$instance) {
         $instance = self::$instance;
     } else {
         $instance = 'main';
     }
     Debugger::addLine("Render start (instance '{$instance}')");
     if (!($resource = Resourcer::getInstance('xslt')->compile($instance))) {
         throw new Exception("XSLT resource not found");
     }
     $xslDom = new \DomDocument();
     $xslDom->resolveExternals = true;
     $xslDom->substituteEntities = true;
     if (!$xslDom->loadXML($resource)) {
         throw new Exception("XSLT load problem for instance '{$instance}'");
     }
     $xslProcessor = new \XSLTProcessor();
     $xslProcessor->importStylesheet($xslDom);
     if (!$dontFillXML and !HttpError::$error and !Debugger::$shutdown) {
         View\XML::fillXML($xml, $instance);
     }
     // transform template
     if ($html = $xslProcessor->transformToDoc($xml)) {
         if ($normalize) {
             $html = self::normalize($html);
         } else {
             $html->formatOutput = true;
             $html = $html->saveXML();
         }
         if ($dontEcho) {
             return $html;
         }
         echo $html;
         self::$rendered = true;
         if (Debugger::isEnabled()) {
             echo '<!-- Page rendered in ' . Debugger::getTimer() . ' seconds -->';
         }
         if (function_exists('fastcgi_finish_request')) {
             fastcgi_finish_request();
         }
     } else {
         $errormsg = libxml_get_errors();
         //error_get_last();
         throw new Exception($errormsg ? $errormsg['message'] : "Can't render templates");
     }
     return true;
 }
Пример #6
0
 /**
  * Choose view depending on request type
  */
 public static final function start()
 {
     $controller = Controller::getInstance();
     if (Controller::hasUnusedParameters()) {
         $controller->putExpires(true);
         throw new HttpError(404);
     } elseif (!is_null(self::$output)) {
         $controller->putExpires();
         header('Content-Type: ' . self::$outputType . '; charset="utf-8"');
         echo self::$output;
         View::$rendered = true;
     } elseif (Debugger::isEnabled() and isset($_GET['xml']) and $_GET['xml']) {
         if ($_GET['xml'] == '2') {
             View\XML::fillXML();
         }
         header('Content-Type: text/xml; charset="utf-8"');
         $controller->xml->formatOutput = true;
         $controller->xml->encoding = 'utf-8';
         echo rawurldecode($controller->xml->saveXML());
         View::$rendered = true;
     } elseif (!View::$rendered and Request::isAjax()) {
         $controller->putExpires();
         // should be application/json, but opera doesn't understand it and offers to save file to disk
         header('Content-type: text/plain');
         echo Ajaxer::getResponse();
         View::$rendered = true;
     } elseif (!View::$rendered) {
         $controller->putExpires();
         try {
             View::render($controller->xml);
         } catch (HttpError $ex) {
             if (!Debugger::isConsoleEnabled()) {
                 throw new HttpError(500);
             } else {
                 echo Debugger::debugHTML(true);
                 die;
             }
         }
     }
 }
Пример #7
0
 /**
  * @param \SimpleXMLElement $node
  * @param string $href
  * @param string $prefix
  * @param string $instance
  * @internal param string $url
  */
 private function recursiveProcessor($node, $href, $prefix, $instance)
 {
     /** @var \SimpleXMLElement $subNode */
     foreach ($node as $subname => $subNode) {
         /** @noinspection PhpUndefinedFieldInspection */
         if ($subNode->attributes()->sup and $subNode->attributes()->sup == '1') {
             if (!Debugger::isEnabled()) {
                 $subNode->addAttribute('hidden', 1);
             }
         }
         $newHref = "{$href}/{$subname}";
         $newPrefix = "{$prefix}_{$subname}";
         $subNode->addAttribute('id', $newPrefix);
         /** @noinspection PhpUndefinedFieldInspection */
         if (!isset($subNode->attributes()->href)) {
             $subNode->addAttribute('href', $newHref);
         }
         $subNode->addAttribute('pseudoHref', $newHref);
         $subNode->addAttribute('xpath', 'locale/menu/' . $instance . '/' . $newPrefix);
         $this->recursiveProcessor($subNode, $newHref, $newPrefix, $instance);
     }
 }
Пример #8
0
 /**
  * Set X-Accel-Expires header for web server-side caching
  * @param bool|int $ttl
  */
 public function putExpires($ttl = null)
 {
     if (Debugger::isEnabled()) {
         return;
     }
     if (is_null($ttl)) {
         $ttl = $this->cache;
     }
     if ($ttl === true) {
         $ttl = self::DEFAULT_CACHE;
     }
     if (!$ttl or !is_numeric($ttl) or $ttl < 0) {
         return;
     }
     View::addExpires($ttl);
 }
Пример #9
0
 /**
  * Fill output XML with some common data
  * @param \DOMDocument|null $xml
  * @param null $instance
  */
 public static function fillXML(&$xml = null, $instance = null)
 {
     $controller = Controller::getInstance();
     if (is_null($xml)) {
         $xml = $controller->xml;
         $node = $controller->realRoot;
     } else {
         $node = $xml->documentElement;
     }
     Debugger::addLine('Filling XML data for render: Started');
     // TODO: sync this with Envi::getState()
     $node->setAttribute('lang', Envi\Setup::getLocale());
     $node->setAttribute('site', Envi::getSubsite());
     $node->setAttribute('host', $host = Envi::getHost());
     $node->setAttribute('mainhost', $mainhost = Envi::getHost(true));
     $node->setAttribute('protocol', Envi::getProtocol());
     $node->setAttribute('fullhost', Envi::getURLPrefix());
     $node->setAttribute('instance', $instance ? $instance : View::$instance);
     $node->setAttribute('uri', Envi::getUri());
     $node->setAttribute('controllerUri', Action::getControllerUri());
     if ($host != $mainhost) {
         $node->setAttribute('urlprefix', Envi::getURLPrefix(true));
     }
     // get user agent
     Envi\UserAgent::getUserAgentXML($node);
     // ajax flag
     $node->setAttribute('ajax', (Request::isAjax() or isset($_SERVER['HTTP_X_REQUESTED_WITH']) and $_SERVER['HTTP_X_REQUESTED_WITH'] == 'SwitchPage') ? '1' : '0');
     $node->setAttribute('switcher', (!$controller->cache and isset($_SERVER['HTTP_X_REQUESTED_WITH']) and $_SERVER['HTTP_X_REQUESTED_WITH'] == 'SwitchPage') ? '1' : '0');
     // build and version number
     $node->setAttribute('build', Version::getBuild());
     $node->setAttribute('framework', Version::getFrameworkVersion(false));
     $node->setAttribute('frameworkLong', Version::getFrameworkVersion(true));
     // date
     /** @var $dateNode \DOMElement */
     $dateNode = $node->appendChild($xml->createElement('date'));
     $dateKeys = ['d', 'e', 'A', 'a', 'm', 'B', 'b', 'Y', 'y', 'c', 'x', 'H', 'M', 'S'];
     $dateValues = explode('|', strftime('%' . implode('|%', $dateKeys)));
     $dateCombined = array_combine($dateKeys, $dateValues);
     $dateNode->setAttribute('ts', time());
     foreach ($dateCombined as $k => $v) {
         $dateNode->setAttribute($k, $v);
     }
     // debug flag
     $node->setAttribute('debug', Debugger::isEnabled() ? '1' : '0');
     // config values (for js variable)
     $configNode = $node->appendChild($xml->createElement('config'));
     Envi::getStateXML($configNode);
     // menu
     if ($menuResource = Resourcer::getInstance('menu')->compile(View::$instance)) {
         $menuXML = new \DOMDocument();
         $menuXML->loadXML($menuResource);
         $node->appendChild($xml->importNode($menuXML->documentElement, true));
     }
     // auth
     Auth::getInstance()->getAuthXML($node);
     // locale
     Locales::getInstance()->getLocaleXML($node);
     // Add config js object
     $config = Envi::getState();
     $confJS = '';
     foreach ($config as $k => $v) {
         $confJS .= "config.{$k}='" . addslashes($v) . "';";
     }
     $node->setAttribute('jsConfig', $confJS);
     Debugger::addLine('Filling XML data for render: Done');
     Debugger::debugXML($node);
 }
Пример #10
0
 /**
  * Commit transaction
  * @return bool
  */
 public function commit()
 {
     $this->transaction = false;
     $result = $this->pdo->commit();
     if (Debugger::isEnabled()) {
         Debugger::addDBLine('DB', 'Transaction commited in ' . number_format((microtime(true) - $this->transaction) * 1000, 1) . ' ms');
     }
     return $result;
 }
Пример #11
0
 /**
  * Is debugging enabled?
  * @return bool
  */
 protected static function isDebug()
 {
     return \Difra\Debugger::isEnabled();
 }