/**
  * Loads pdoTools parser
  *
  * @return pdoParser
  */
 protected function getParser()
 {
     $this->parser = $this->modx->getParser();
     if (!$this->parser instanceof pdoParser) {
         if (!class_exists('pdoParser')) {
             require_once dirname(__FILE__) . '/pdoparser.class.php';
         }
         $this->parser = new pdoParser($this->modx);
     }
     return $this->parser;
 }
 /**
  * @param string $content
  * @param array $tags
  */
 public function findTags($content, &$tags)
 {
     $parser = $this->modx->getParser();
     $collectedTags = array();
     $parser->collectElementTags($content, $collectedTags);
     foreach ($collectedTags as $tag) {
         $tagName = $tag[1];
         if (substr($tagName, 0, 1) == '!') {
             $tagName = substr($tagName, 1);
         }
         $token = substr($tagName, 0, 1);
         $tagParts = xPDO::escSplit('?', $tagName, '`', 2);
         $tagName = trim($tagParts[0]);
         $tagPropString = null;
         $tagName = trim($this->modx->stripTags($tagName));
         if (in_array($token, array('$', '+', '~', '#', '%', '-', '*'))) {
             $tagName = substr($tagName, 1);
         }
         switch ($token) {
             case '$':
                 $class = 'modChunk';
                 break;
             case '+':
             case '~':
             case '#':
             case '%':
             case '-':
             case '*':
                 continue 2;
                 break;
             default:
                 $class = 'modSnippet';
                 break;
         }
         if (isset($tagParts[1])) {
             $tagPropString = trim($tagParts[1]);
             $this->findTags($tagPropString, $tags);
             $element = $parser->getElement($class, $tagName);
             if ($element) {
                 $properties = $element->getProperties($tagPropString);
             } else {
                 $properties = array();
             }
         } else {
             $properties = array();
         }
         $this->debug('Found ' . $class . ' ' . $tagName . ' with properties ' . print_r($properties, 1));
         $tagName = $parser->realname($tagName);
         if (empty($tagName)) {
             continue;
         }
         $tags[$tagName] = array('name' => $tagName, 'class' => $class);
         foreach ($properties as $property) {
             $prop = trim($property);
             if (!empty($prop) && !is_numeric($prop) && is_string($prop)) {
                 $tags[$prop] = array('name' => $prop, 'class' => 'modChunk', 'isProperty' => true);
             }
         }
     }
 }
 /**
  * Collects and processes any set of tags
  *
  * @param mixed $html Source code for parse
  * @param integer $maxIterations
  *
  * @return mixed $html Parsed html
  */
 public function processTags($html, $maxIterations = 10)
 {
     if (strpos($html, '[[') !== false) {
         $this->modx->getParser()->processElementTags('', $html, false, false, '[[', ']]', array(), $maxIterations);
         $this->modx->getParser()->processElementTags('', $html, true, true, '[[', ']]', array(), $maxIterations);
     }
     return $html;
 }
 /**
  * Grab a persistent instance of the xPDO class to share connection data
  * across multiple tests and test suites.
  * 
  * @param array $options An array of configuration parameters.
  * @return xPDO An xPDO object instance.
  */
 public static function _getConnection($options = array())
 {
     $modx = FiTestHarness::$modx;
     if (is_object($modx)) {
         if (!$modx->request) {
             $modx->getRequest();
         }
         if (!$modx->error) {
             $modx->request->loadErrorHandler();
         }
         $modx->error->reset();
         FiTestHarness::$modx = $modx;
         return FiTestHarness::$modx;
     }
     /* include config.core.php */
     $properties = array();
     $config = array();
     include strtr(realpath(dirname(__FILE__)) . '/config.inc.php', '\\', '/');
     require_once $config['modx_base_path'] . 'config.core.php';
     require_once MODX_CORE_PATH . 'config/' . MODX_CONFIG_KEY . '.inc.php';
     require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
     include_once strtr(realpath(dirname(__FILE__)) . '/properties.inc.php', '\\', '/');
     if (!defined('MODX_REQP')) {
         define('MODX_REQP', false);
     }
     $modx = new modX(null, $properties);
     $ctx = !empty($options['ctx']) ? $options['ctx'] : 'web';
     $modx->initialize($ctx);
     $debug = !empty($options['debug']);
     $modx->setDebug($debug);
     if (!empty($properties['logTarget'])) {
         $modx->setLogTarget($properties['logTarget']);
     }
     if (!empty($properties['logLevel'])) {
         $modx->setLogLevel($properties['logLevel']);
     }
     $modx->user = $modx->newObject('modUser');
     $modx->user->set('id', $modx->getOption('modx.test.user.id', null, 1));
     $modx->user->set('username', $modx->getOption('modx.test.user.username', null, 'test'));
     $modx->getRequest();
     $modx->getParser();
     $modx->request->loadErrorHandler();
     @error_reporting(E_ALL);
     @ini_set('display_errors', true);
     FiTestHarness::$modx = $modx;
     return $modx;
 }
Exemple #5
0
 /**
  * @param $snippetName
  * @param array $params
  *
  * @return string
  */
 public function runSnippet($snippetName, array $params = array())
 {
     $output = '';
     $cacheable = true;
     if (strpos($snippetName, '!') === 0) {
         $snippetName = substr($snippetName, 1);
         $cacheable = false;
     }
     if ($this->modx->getParser()) {
         $snippet = $this->modx->parser->getElement('modSnippet', $snippetName);
         if ($snippet instanceof modSnippet) {
             $snippet->setCacheable($cacheable);
             $output = $snippet->process($params);
         }
     }
     return $output;
 }
 /**
  * Create or grab a reference to a static xPDO/modX instance.
  *
  * The instances can be reused by multiple tests and test suites.
  *
  * @param string $class A fixture class to get an instance of.
  * @param string $name A unique identifier for the fixture.
  * @param boolean $new
  * @param array $options An array of configuration options for the fixture.
  * @return object|null An instance of the specified fixture class or null on failure.
  */
 public static function &getFixture($class, $name, $new = false, array $options = array())
 {
     if (!$new && array_key_exists($name, self::$fixtures) && self::$fixtures[$name] instanceof $class) {
         $fixture =& self::$fixtures[$name];
     } else {
         $properties = array();
         include_once dirname(dirname(dirname(__FILE__))) . '/core/model/modx/modx.class.php';
         include dirname(__FILE__) . '/properties.inc.php';
         self::$properties = $properties;
         if (array_key_exists('debug', self::$properties)) {
             self::$debug = (bool) self::$properties['debug'];
         }
         $fixture = null;
         $driver = self::$properties['xpdo_driver'];
         switch ($class) {
             case 'modX':
                 if (!defined('MODX_REQP')) {
                     define('MODX_REQP', false);
                 }
                 if (!defined('MODX_CONFIG_KEY')) {
                     define('MODX_CONFIG_KEY', array_key_exists('config_key', self::$properties) ? self::$properties['config_key'] : 'test');
                 }
                 $fixture = new modX(null, self::$properties["{$driver}_array_options"]);
                 if ($fixture instanceof modX) {
                     $logLevel = array_key_exists('logLevel', self::$properties) ? self::$properties['logLevel'] : modX::LOG_LEVEL_WARN;
                     $logTarget = array_key_exists('logTarget', self::$properties) ? self::$properties['logTarget'] : (XPDO_CLI_MODE ? 'ECHO' : 'HTML');
                     $fixture->setLogLevel($logLevel);
                     $fixture->setLogTarget($logTarget);
                     if (!empty(self::$debug)) {
                         $fixture->setDebug(self::$properties['debug']);
                     }
                     $fixture->initialize(self::$properties['context']);
                     $fixture->user = $fixture->newObject('modUser');
                     $fixture->user->set('id', $fixture->getOption('modx.test.user.id', null, 1));
                     $fixture->user->set('username', $fixture->getOption('modx.test.user.username', null, 'test'));
                     $fixture->getRequest();
                     $fixture->getParser();
                     $fixture->request->loadErrorHandler();
                 }
                 break;
             case 'xPDO':
                 $fixture = new xPDO(self::$properties["{$driver}_string_dsn_test"], self::$properties["{$driver}_string_username"], self::$properties["{$driver}_string_password"], self::$properties["{$driver}_array_options"], self::$properties["{$driver}_array_driverOptions"]);
                 if ($fixture instanceof xPDO) {
                     $logLevel = array_key_exists('logLevel', self::$properties) ? self::$properties['logLevel'] : xPDO::LOG_LEVEL_WARN;
                     $logTarget = array_key_exists('logTarget', self::$properties) ? self::$properties['logTarget'] : (XPDO_CLI_MODE ? 'ECHO' : 'HTML');
                     $fixture->setLogLevel($logLevel);
                     $fixture->setLogTarget($logTarget);
                     if (!empty(self::$debug)) {
                         $fixture->setDebug(self::$properties['debug']);
                     }
                 }
                 break;
             default:
                 $fixture = new $class($options);
                 break;
         }
         if ($fixture !== null && $fixture instanceof $class) {
             self::$fixtures[$name] = $fixture;
         } else {
             die("Error setting fixture {$name} of expected class {$class}.");
         }
     }
     return $fixture;
 }
 /**
  * Parse any tags in the properties
  * @param array $properties
  * @return array
  */
 public function parseProperties(array $properties)
 {
     $properties = $this->getProperties();
     $this->xpdo->getParser();
     if ($this->xpdo->parser) {
         foreach ($properties as &$property) {
             $this->xpdo->parser->processElementTags('', $property['value'], true, true);
         }
     }
     return $properties;
 }
Exemple #8
0
<?php

/* JUST FOR TESTING THE TEXTILE IMPLEMENTATION */
/* Instantiate constants and variables for later use.
 ***/
dirname(dirname(dirname(dirname(__FILE__)))) . 'core.config.php';
require_once 'C:/wamp/www/revolution/core/model/modx/modx.class.php';
$modx = new modX();
$modx->initialize('mgr');
$modx->getParser();
$modx->getService('lexicon', 'modLexicon');
define('HANDYMAN', true);
$hmo = '';
/* Include the main HandyMan class.
 * This class takes care of authentification and provides the extension
 * with functions to execute the requests.
 *
 * After inclusion, set up the $hm variable as the main object.
 **/
include_once dirname(dirname(dirname(dirname(__FILE__)))) . '/core/components/handyman/classes/handyman.class.php';
$hm = new HandyMan($modx);
$hm->initialize();
$hm->modx->getService('h2t', 'html2textile', $hm->config['corePath'] . 'classes/textile/');
$hm->modx->getService('t2h', 'Textile', $hm->config['corePath'] . 'classes/textile/');
$case = !empty($_REQUEST['case']) ? $_REQUEST['case'] : 1;
$original = file_get_contents('data/' . $case . '.html');
$textiled = $hm->modx->h2t->detextile($original);
$html = $hm->modx->t2h->TextileThis($textiled);
$amts = 25;
$i = 0;
$recursingTextile = $textiled;
 /**
  * Process the tag and return the result.
  *
  * @see modElement::process()
  * @param array|string $properties An array of properties or a formatted
  * property string.
  * @param string $content Optional content to use for the element
  * processing.
  * @return mixed The result of processing the tag.
  */
 public function process($properties = null, $content = null)
 {
     $this->modx->getParser();
     $this->getProperties($properties);
     $this->getTag();
     $this->filterInput();
     if ($this->modx->getDebug() === true) {
         $this->modx->log(xPDO::LOG_LEVEL_DEBUG, "Processing Element: " . $this->get('name') . ($this->_tag ? "\nTag: {$this->_tag}" : "\n") . "\nProperties: " . print_r($this->_properties, true));
     }
     if ($this->isCacheable() && isset($this->modx->elementCache[$this->_tag])) {
         $this->_output = $this->modx->elementCache[$this->_tag];
         $this->_processed = true;
     } else {
         $this->getContent(is_string($content) ? array('content' => $content) : array());
     }
     return $this->_result;
 }
 /**
  * Gets the value of a TV for the Resource.
  *
  * @access public
  * @param mixed $pk Either the ID of the TV, or the name of the TV.
  * @return null/mixed The value of the TV for the Resource, or null if the
  * TV is not found.
  */
 public function getTVValue($pk)
 {
     $byName = false;
     if (is_string($pk)) {
         $byName = true;
     }
     /** @var modTemplateVar $tv */
     if ($byName && $this->xpdo instanceof modX) {
         $tv = $this->xpdo->getParser()->getElement('modTemplateVar', $pk);
     } else {
         $tv = $this->xpdo->getObject('modTemplateVar', $byName ? array('name' => $pk) : $pk);
     }
     return $tv == null ? null : $tv->renderOutput($this->get('id'));
 }
 /**
  * Prepare the final response after the resource has been processed.
  *
  * @param array $options Various options that can be set.
  */
 public function outputContent(array $options = array())
 {
     if (!($this->contentType = $this->modx->resource->getOne('ContentType'))) {
         if ($this->modx->getDebug() === true) {
             $this->modx->log(modX::LOG_LEVEL_DEBUG, "No valid content type for RESOURCE: " . print_r($this->modx->resource->toArray(), true));
         }
         $this->modx->log(modX::LOG_LEVEL_FATAL, "The requested resource has no valid content type specified.");
     }
     if (!$this->contentType->get('binary')) {
         $this->modx->resource->_output = $this->modx->resource->process();
         $this->modx->resource->_jscripts = $this->modx->jscripts;
         $this->modx->resource->_sjscripts = $this->modx->sjscripts;
         $this->modx->resource->_loadedjscripts = $this->modx->loadedjscripts;
         /* collect any uncached element tags in the content and process them */
         $this->modx->getParser();
         $maxIterations = intval($this->modx->getOption('parser_max_iterations', $options, 10));
         $this->modx->parser->processElementTags('', $this->modx->resource->_output, true, false, '[[', ']]', array(), $maxIterations);
         $this->modx->parser->processElementTags('', $this->modx->resource->_output, true, true, '[[', ']]', array(), $maxIterations);
         /*FIXME: only do this for HTML content ?*/
         if (strpos($this->contentType->get('mime_type'), 'text/html') !== false) {
             /* Insert Startup jscripts & CSS scripts into template - template must have a </head> tag */
             if (($js = $this->modx->getRegisteredClientStartupScripts()) && strpos($this->modx->resource->_output, '</head>') !== false) {
                 /* change to just before closing </head> */
                 $this->modx->resource->_output = preg_replace("/(<\\/head>)/i", $js . "\n\\1", $this->modx->resource->_output, 1);
             }
             /* Insert jscripts & html block into template - template must have a </body> tag */
             if (strpos($this->modx->resource->_output, '</body>') !== false && ($js = $this->modx->getRegisteredClientScripts())) {
                 $this->modx->resource->_output = preg_replace("/(<\\/body>)/i", $js . "\n\\1", $this->modx->resource->_output, 1);
             }
         }
         $this->modx->beforeRender();
         /* invoke OnWebPagePrerender event */
         if (!isset($options['noEvent']) || empty($options['noEvent'])) {
             $this->modx->invokeEvent('OnWebPagePrerender');
         }
         $totalTime = $this->modx->getMicroTime() - $this->modx->startTime;
         $queryTime = $this->modx->queryTime;
         $queryTime = sprintf("%2.4f s", $queryTime);
         $queries = isset($this->modx->executedQueries) ? $this->modx->executedQueries : 0;
         $totalTime = sprintf("%2.4f s", $totalTime);
         $phpTime = $totalTime - $queryTime;
         $phpTime = sprintf("%2.4f s", $phpTime);
         $source = $this->modx->resourceGenerated ? "database" : "cache";
         $this->modx->resource->_output = str_replace("[^q^]", $queries, $this->modx->resource->_output);
         $this->modx->resource->_output = str_replace("[^qt^]", $queryTime, $this->modx->resource->_output);
         $this->modx->resource->_output = str_replace("[^p^]", $phpTime, $this->modx->resource->_output);
         $this->modx->resource->_output = str_replace("[^t^]", $totalTime, $this->modx->resource->_output);
         $this->modx->resource->_output = str_replace("[^s^]", $source, $this->modx->resource->_output);
     } else {
         $this->modx->beforeRender();
         /* invoke OnWebPagePrerender event */
         if (!isset($options['noEvent']) || empty($options['noEvent'])) {
             $this->modx->invokeEvent("OnWebPagePrerender");
         }
     }
     /* send out content-type, content-disposition, and custom headers from the content type */
     if ($this->modx->getOption('set_header')) {
         $type = $this->contentType->get('mime_type') ? $this->contentType->get('mime_type') : 'text/html';
         $header = 'Content-Type: ' . $type;
         if (!$this->contentType->get('binary')) {
             $charset = $this->modx->getOption('modx_charset', null, 'UTF-8');
             $header .= '; charset=' . $charset;
         }
         header($header);
         if (!$this->checkPreview()) {
             $dispositionSet = false;
             if ($customHeaders = $this->contentType->get('headers')) {
                 foreach ($customHeaders as $headerKey => $headerString) {
                     header($headerString);
                     if (strpos($headerString, 'Content-Disposition:') !== false) {
                         $dispositionSet = true;
                     }
                 }
             }
             if (!$dispositionSet && $this->modx->resource->get('content_dispo')) {
                 if ($alias = array_search($this->modx->resourceIdentifier, $this->modx->aliasMap)) {
                     $name = basename($alias);
                 } elseif ($this->modx->resource->get('alias')) {
                     $name = $this->modx->resource->get('alias');
                     if ($ext = $this->contentType->getExtension()) {
                         $name .= ".{$ext}";
                     }
                 } elseif ($name = $this->modx->resource->get('pagetitle')) {
                     $name = $this->modx->resource->cleanAlias($name);
                     if ($ext = $this->contentType->getExtension()) {
                         $name .= ".{$ext}";
                     }
                 } else {
                     $name = 'download';
                     if ($ext = $this->contentType->getExtension()) {
                         $name .= ".{$ext}";
                     }
                 }
                 $header = 'Cache-Control: public';
                 header($header);
                 $header = 'Content-Disposition: attachment; filename=' . $name;
                 header($header);
                 $header = 'Vary: User-Agent';
                 header($header);
             }
         }
     }
     /* tell PHP to call _postProcess after returning the response (for caching) */
     register_shutdown_function(array(&$this->modx, "_postProcess"));
     if ($this->modx->resource instanceof modStaticResource && $this->contentType->get('binary')) {
         $this->modx->resource->process();
     } else {
         if ($this->contentType->get('binary')) {
             $this->modx->resource->_output = $this->modx->resource->process();
         }
         @session_write_close();
         echo $this->modx->resource->_output;
         while (@ob_end_flush()) {
         }
         flush();
         exit;
     }
 }
 /**
  * Create a PDF with the options set in the class
  *
  * @param modResource $resource
  * @param string|boolean $aliasPath
  * @return string
  */
 public function createPDF($resource, $aliasPath)
 {
     // Create folders
     if (!@is_dir($this->getOption('pdfPath'))) {
         if (!@mkdir($this->getOption('pdfPath'), $this->modx->getOption('new_folder_permissions', null, 0775))) {
             $this->modx->log(modX::LOG_LEVEL_ERROR, 'Could not create the pdf output path: ' . $this->getOption('pdfPath'), '', 'PDFResource');
             return '';
         }
     }
     if ($aliasPath && !@is_dir($this->getOption('pdfPath') . $aliasPath)) {
         if (!@mkdir($this->getOption('pdfPath') . $aliasPath, $this->modx->getOption('new_folder_permissions', null, 0775))) {
             $this->modx->log(modX::LOG_LEVEL_ERROR, 'Could not create the pdf alias path: ' . $this->getOption('pdfPath') . $aliasPath, '', 'PDFResource');
             return '';
         }
     }
     // Get options
     $id = $resource->get('id');
     $pdfTpl = $this->getOption('pdfTpl', null, 'tplPDF');
     $cssTpl = $this->getOption('cssTpl', null, 'tplCSS');
     $optionsTV = $this->getOption('pdfTvOptions', null, 'pdf_options');
     $processTVs = $this->getOption('processTVs', null, false);
     $tvPrefix = $this->getOption('tvPrefix', null, 'tv.');
     $placeholder = $resource->toArray();
     // Prepare template variables and resource based options
     $pdfOptions = null;
     $tvs = $this->modx->getCollection('modTemplateVar');
     foreach ($tvs as $tv) {
         /** @var modTemplateVar $tv */
         $placeholder[$tvPrefix . $tv->get('name')] = $processTVs ? $tv->renderOutput($id) : $tv->getValue($id);
         if ($tv->get('name') == $optionsTV && $tv->getValue($id) != '') {
             $pdfOptions = json_decode($tv->getValue($id), true);
             if ($pdfOptions) {
                 $pdfTpl = $this->modx->getOption('pdfTpl', $pdfOptions, $pdfTpl);
                 $cssTpl = $this->modx->getOption('cssTpl', $pdfOptions, $cssTpl);
             }
         }
     }
     // Parse template chunks
     $placeholder['tplPath'] = $this->modx->getOption('assets_path');
     $html = $this->getChunk($pdfTpl, $placeholder);
     $this->modx->getParser()->processElementTags('', $html, false, false, '[[', ']]', array(), $this->modx->getOption('max_iterations'));
     $this->modx->getParser()->processElementTags('', $html, true, true, '[[', ']]', array(), $this->modx->getOption('max_iterations'));
     $css = $this->getChunk($cssTpl, $placeholder);
     unset($placeholder);
     // Generate PDF file
     $this->initPDF(array('mode' => $this->getOption('mode', $pdfOptions, 'utf-8'), 'format' => $this->getOption('format', $pdfOptions, 'A4'), 'defaultFontSize' => intval($this->getOption('defaultFontSize', $pdfOptions, 8)), 'defaultFont' => $this->getOption('defaultFont', $pdfOptions, ''), 'mgl' => intval($this->getOption('mgl', $pdfOptions, 10)), 'mgr' => intval($this->getOption('mgr', $pdfOptions, 10)), 'mgt' => intval($this->getOption('mgt', $pdfOptions, 7)), 'mgb' => intval($this->getOption('mgb', $pdfOptions, 7)), 'mgh' => intval($this->getOption('mgh', $pdfOptions, 10)), 'mgf' => intval($this->getOption('mgf', $pdfOptions, 10)), 'orientation' => $this->getOption('orientation', $pdfOptions, 'P'), 'customFonts' => $this->getOption('customFonts', $pdfOptions, '[]')));
     $this->pdf->SetTitle($resource->get('pagetitle'));
     $this->pdf->SetAuthor($this->getOption('author', $pdfOptions, $this->modx->getOption('site_name')));
     $this->pdf->SetCreator($this->getOption('creator', $pdfOptions, $this->modx->getOption('site_url') . ' powered by PDFResource/mPDF'));
     // Password protection
     $userPassword = $this->getOption('userPassword', $pdfOptions, '');
     $ownerPassword = $this->getOption('ownerPassword', $pdfOptions, '');
     $permissions = json_decode($this->getOption('permissions', $pdfOptions, ''), true);
     if ($userPassword || $ownerPassword) {
         // Set default permissions if needed
         $permissions = $permissions ? $permissions : array();
         // Random owner password if needed
         $ownerPassword = $ownerPassword ? $ownerPassword : null;
         $this->pdf->SetProtection($permissions, $userPassword, $ownerPassword, 128);
     }
     // Call additional mPDF methods
     $mpdfMethods = json_decode($this->getOption('mPDFMethods', $pdfOptions, ''), true);
     $mpdfMethods = is_array($mpdfMethods) ? $mpdfMethods : array();
     foreach ($mpdfMethods as $methodName) {
         $value = $this->getOption($methodName, $pdfOptions, '');
         $value = is_array($value) ? $value : json_decode($value, true);
         if ($value && method_exists($this->pdf, $methodName)) {
             call_user_func_array(array($this->pdf, $methodName), $value);
         }
     }
     $this->pdf->WriteHTML($css, 1);
     $this->pdf->WriteHTML($html, 2);
     if ($aliasPath) {
         return $this->pdf->Output($this->getOption('pdfPath') . $aliasPath . $resource->get('alias') . '.pdf', 'F');
     } else {
         return $this->pdf->Output('', 'S');
     }
 }