예제 #1
0
 /**
  * To prevent a security hole, not all functions are permitted
  * @param $functionName string
  * @return bool
  */
 public function functionIsPermitted($functionName)
 {
     if ($this->permittedFilterFunctions) {
         return $this->permittedFilterFunctions->isFunctionPermitted($functionName);
     }
     return true;
 }
 public static function getInstance()
 {
     static $inst = null;
     if ($inst === null) {
         $inst = new CFDBPermittedFunctions();
         $inst->init();
     }
     return $inst;
 }
 /**
  * @param $functionName string name of sort function such as strcmp, strcasecmp, strnatcmp
  * @param $fieldName
  * @param $ascDesc string 'ASC' or 'DESC'
  */
 function __construct($functionName, $fieldName, $ascDesc = 'ASC')
 {
     $this->functionName = $functionName;
     $this->fieldName = $fieldName;
     $this->reverse = strtoupper($ascDesc) == 'DESC';
     $this->functionPermitted = CFDBPermittedFunctions::getInstance()->isFunctionPermitted($functionName);
 }
 public function setUp()
 {
     date_default_timezone_set('America/New_York');
     $str = file_get_contents('HtmlTemplateTransformTest.json');
     $data = json_decode($str, true);
     $mock = new MockQueryResultIterator($data);
     CFDBQueryResultIteratorFactory::getInstance()->setQueryResultsIteratorMock($mock);
     global $wpdb;
     $wpdb = new WPDB_Mock();
     $fields = array();
     foreach (array_keys($data[0]) as $key) {
         $fields[] = (object) array('field_name' => $key);
     }
     $wpdb->getResultReturnVal = $fields;
     CFDBPermittedFunctions::getInstance()->addPermittedFunction('cambiaFecha');
 }
예제 #5
0
 public function setCommonOptions($htmlOptions = false)
 {
     if ($this->options && is_array($this->options)) {
         foreach (array('debug', 'permissionmsg', 'unbuffered', 'show', 'hide', 'class', 'style', 'id', 'orderby', 'limit', 'tlimit', 'header', 'headers', 'content', 'filter', 'tfilter', 'search', 'tsearch', 'trans') as $optionName) {
             $this->dereferenceOption($optionName);
         }
         if (isset($this->options['debug']) && $this->options['debug'] != 'false') {
             $this->debug = true;
         }
         $this->isFromShortCode = isset($this->options['fromshortcode']) && $this->options['fromshortcode'] === true;
         if (!isset($this->options['unbuffered'])) {
             //$this->options['unbuffered'] = $this->isFromShortCode ? 'false' : 'true'; // todo
             $this->options['unbuffered'] = 'false';
         } else {
             if ($this->options['unbuffered'] == 'checked') {
                 $this->options['unbuffered'] = 'true';
             }
         }
         if (isset($this->options['showColumns'])) {
             $this->showColumns = $this->options['showColumns'];
         } else {
             if (isset($this->options['show'])) {
                 $this->showColumns = preg_split('/,/', $this->options['show'], -1, PREG_SPLIT_NO_EMPTY);
             }
         }
         if (isset($this->options['hideColumns'])) {
             $this->hideColumns = $this->options['hideColumns'];
         } else {
             if (isset($this->options['hide'])) {
                 $this->hideColumns = preg_split('/,/', $this->options['hide'], -1, PREG_SPLIT_NO_EMPTY);
             }
         }
         if ($htmlOptions) {
             if (isset($this->options['class'])) {
                 $this->htmlTableClass = $this->options['class'];
             } else {
                 $this->htmlTableClass = $this->defaultTableClass;
             }
             if (isset($this->options['id'])) {
                 $this->htmlTableId = $this->options['id'];
             } else {
                 $this->htmlTableId = 'cftble_' . rand();
             }
             if (isset($this->options['style'])) {
                 $this->style = $this->options['style'];
             }
         }
         $permittedFunctions = null;
         if (isset($this->options['filter']) || isset($this->options['trans'])) {
             require_once 'CFDBPermittedFunctions.php';
             $permittedFunctions = CFDBPermittedFunctions::getInstance();
             $permitAll = $this->queryPermitAllFunctions();
             $permittedFunctions->setPermitAllFunctions($permitAll);
         }
         $filters = array();
         if (isset($this->options['filter'])) {
             require_once 'CFDBFilterParser.php';
             $aFilter = new CFDBFilterParser();
             $aFilter->setComparisonValuePreprocessor(new DereferenceShortcodeVars());
             $aFilter->setPermittedFilterFunctions($permittedFunctions);
             $aFilter->parse($this->options['filter']);
             if ($this->debug) {
                 echo '<pre>\'' . $this->options['filter'] . "'\n";
                 print_r($aFilter->tree);
                 echo '</pre>';
             }
             $filters[] = $aFilter;
         }
         $transformFilters = array();
         if (isset($this->options['tfilter'])) {
             require_once 'CFDBFilterParser.php';
             $aFilter = new CFDBFilterParser();
             $aFilter->setComparisonValuePreprocessor(new DereferenceShortcodeVars());
             $aFilter->setPermittedFilterFunctions($permittedFunctions);
             $aFilter->parse($this->options['tfilter']);
             if ($this->debug) {
                 echo '<pre>\'' . $this->options['tfilter'] . "'\n";
                 print_r($aFilter->tree);
                 echo '</pre>';
             }
             $transformFilters[] = $aFilter;
         }
         if (isset($this->options['search'])) {
             require_once 'CFDBSearchEvaluator.php';
             $aFilter = new CFDBSearchEvaluator();
             $aFilter->setSearch($this->options['search']);
             $filters[] = $aFilter;
         }
         if (isset($this->options['tsearch'])) {
             require_once 'CFDBSearchEvaluator.php';
             $aFilter = new CFDBSearchEvaluator();
             $aFilter->setSearch($this->options['tsearch']);
             $transformFilters[] = $aFilter;
         }
         $numFilters = count($filters);
         if ($numFilters == 1) {
             $this->rowFilter = $filters[0];
         } else {
             if ($numFilters > 1) {
                 require_once 'CFDBCompositeEvaluator.php';
                 $this->rowFilter = new CFDBCompositeEvaluator();
                 $this->rowFilter->setEvaluators($filters);
             }
         }
         $numTransformFilters = count($transformFilters);
         if ($numTransformFilters == 1) {
             $this->rowTransformFilter = $transformFilters[0];
         } else {
             if ($numTransformFilters > 1) {
                 require_once 'CFDBCompositeEvaluator.php';
                 $this->rowTransformFilter = new CFDBCompositeEvaluator();
                 $this->rowTransformFilter->setEvaluators($transformFilters);
             }
         }
         if (isset($this->options['trans'])) {
             require_once 'CFDBTransformParser.php';
             $this->transform = new CFDBTransformParser();
             $this->transform->setComparisonValuePreprocessor(new DereferenceShortcodeVars());
             $this->transform->setPermittedFilterFunctions($permittedFunctions);
             $transformOption = $this->options['trans'];
             // Set up "orderby" post-processing
             if (isset($this->options['orderby'])) {
                 $orderByStrings = explode(',', $this->options['orderby']);
                 foreach ($orderByStrings as $anOrderBy) {
                     $anOrderBy = trim($anOrderBy);
                     $ascOrDesc = null;
                     list($ascOrDesc, $anOrderBy) = $this->parseOrderBy($anOrderBy);
                     $ascOrDesc = trim($ascOrDesc);
                     if (empty($ascOrDesc)) {
                         $ascOrDesc = 'ASC';
                     }
                     // Append a Sort transform
                     $transformOption .= '&&NaturalSortByField(' . $anOrderBy . ',' . $ascOrDesc . ')';
                 }
             }
             $this->transform->parse($transformOption);
             if ($this->debug) {
                 echo '<pre>\'' . $transformOption . "'\n";
                 print_r($this->transform->tree);
                 echo '</pre>';
             }
             $this->transform->setupTransforms();
         }
         if (isset($this->options['headers'])) {
             // e.g. "col1=Column 1 Display Name,col2=Column2 Display Name"
             $headersList = preg_split('/,/', $this->options['headers'], -1, PREG_SPLIT_NO_EMPTY);
             if (is_array($headersList)) {
                 $this->headers = array();
                 foreach ($headersList as $nameEqualValue) {
                     $nameEqualsValueArray = explode('=', $nameEqualValue, 2);
                     // col1=Column 1 Display Name
                     if (count($nameEqualsValueArray) >= 2) {
                         $this->headers[$nameEqualsValueArray[0]] = $nameEqualsValueArray[1];
                     }
                 }
             }
         }
     }
 }
 public function testRegisterFunction()
 {
     $this->assertFalse(CFDBPermittedFunctions::getInstance()->isFunctionPermitted('blahblah'));
     cfdb_register_function('blahblah');
     $this->assertTrue(CFDBPermittedFunctions::getInstance()->isFunctionPermitted('blahblah'));
 }