Example #1
0
 public function tearDown()
 {
     parent::tearDown();
     $app = \FelixOnline\Core\App::getInstance();
     $app['db']->dbh->close();
     \FelixOnline\Core\App::setInstance(null);
 }
Example #2
0
 public function getSQL()
 {
     $app = \FelixOnline\Core\App::getInstance();
     if (is_null($this->value) && $this->config['null'] == true) {
         return 'NULL';
     }
     return $app['safesql']->query($this->placeholder, array($this->value));
 }
Example #3
0
/**
 * Test utilities
 */
function loginUser($user)
{
    $app = \FelixOnline\Core\App::getInstance();
    // Log in user
    $app['env']['session']['loggedin'] = true;
    $app['env']['session']['uname'] = $user;
    $app['db']->query("INSERT INTO `login` \n\t\t(`session_id`, `ip`, `browser`, `user`, `timestamp`, `valid`, `logged_in`)\n\t\tVALUES \n\t\t('1', '" . $app['env']['REMOTE_ADDR'] . "', '" . $app['env']['HTTP_USER_AGENT'] . "', '" . $user . "', NOW(), 1, 1)");
    $app['currentuser']->setUser($user);
}
Example #4
0
 public function getSQL()
 {
     $app = \FelixOnline\Core\App::getInstance();
     if (is_null($this->value) && $this->config['null'] == true) {
         return 'NULL';
     }
     $datetime = (new \DateTime("@{$this->value}"))->format('Y-m-d H:i:s');
     return $app['safesql']->query($this->placeholder, array($datetime));
 }
Example #5
0
 public function __construct($message, $code = self::EXCEPTION_UNIVERSAL, \Exception $previous = null)
 {
     try {
         $app = \FelixOnline\Core\App::getInstance();
         if (isset($app['currentuser'])) {
             $this->user = $app['currentuser'];
         } else {
             $this->user = null;
         }
     } catch (\Exception $e) {
         // no app
         $this->user = null;
     }
     parent::__construct($message, $code, $previous);
 }
Example #6
0
 /**
  * Get cache item
  *
  * pk - primary key column
  */
 protected function getCache($pk)
 {
     $app = \FelixOnline\Core\App::getInstance();
     return $app['cache']->getItem($this->dbtable . '/' . $pk->getValue());
 }
Example #7
0
 private function log($action, $fields, $pk = null)
 {
     if ($this->dontlog) {
         return;
     }
     if (is_null($pk)) {
         $pk = $this->fields[$this->pk]->getValue();
     }
     $app = App::getInstance();
     if (isset($app['currentuser']) && $app['currentuser']->isLoggedIn()) {
         $user = $app['currentuser']->getUser();
     } else {
         $user = '******';
     }
     $sql = $app['safesql']->query("INSERT INTO audit_log (`id`, `timestamp`, `table`, `key`, `user`, `action`, `fields`) VALUES (NULL, NOW(), '%s', '%s', '%s', '%s', '%s')", array($this->dbtable, $pk, $user, $action, json_encode($fields)));
     $app['db']->query($sql);
 }
Example #8
0
 function query($query_string, $query_vars)
 {
     $app = \FelixOnline\Core\App::getInstance();
     $link_id = $app['db']->dbh;
     if (is_array($query_vars)) {
         $_var_count = count($query_vars);
         if ($_var_count != preg_match_all('!%[sSiIfFcClLqQnN]!', $query_string, $_match)) {
             $this->_error_msg('unmatched number of vars and % placeholders: ' . $query_string);
         }
         // get string position for each element
         $_var_pos = array();
         $_curr_pos = 0;
         for ($_x = 0; $_x < $_var_count; $_x++) {
             $_var_pos[$_x] = strpos($query_string, $_match[0][$_x], $_curr_pos);
             $_curr_pos = $_var_pos[$_x] + 1;
         }
         // build query from passed in variables, escape them
         // start from end of query and work backwards so string
         // positions are not altered during replacement
         $_last_removed_pos = null;
         $_last_var_pos = null;
         for ($_x = $_var_count - 1; $_x >= 0; $_x--) {
             if (isset($_last_removed_pos) && $_last_removed_pos < $_var_pos[$_x]) {
                 // already removed, skip
                 continue;
             }
             // escape string
             $query_vars[$_x] = $this->_sql_escape($link_id, $query_vars[$_x]);
             if (in_array($_match[0][$_x], array('%S', '%I', '%F', '%C', '%L', '%Q', '%N'))) {
                 // get positions of [ and ]
                 if (isset($_last_var_pos)) {
                     $_right_pos = strpos($query_string, ']', $_last_var_pos);
                 } else {
                     $_right_pos = strpos($query_string, ']', $_var_pos[$_x]);
                 }
                 // no way to get strpos from the right side starting in the middle
                 // of the string, so slice the first part out then find it
                 $_str_slice = substr($query_string, 0, $_var_pos[$_x]);
                 $_left_pos = strrpos($_str_slice, '[');
                 if ($_right_pos === false || $_left_pos === false) {
                     $this->_error_msg('missing or unmatched brackets: ' . $query_string);
                 }
                 if (in_array($query_vars[$_x], $this->_drop_values, true)) {
                     $_last_removed_pos = $_left_pos;
                     // remove entire part of string
                     $query_string = substr_replace($query_string, '', $_left_pos, $_right_pos - $_left_pos + 1);
                     $_last_var_pos = null;
                 } else {
                     if ($_x > 0 && $_var_pos[$_x - 1] > $_left_pos) {
                         // still variables left in brackets, leave them and just replace var
                         $_convert_var = $this->_convert_var($query_vars[$_x], $_match[0][$_x]);
                         $query_string = substr_replace($query_string, $_convert_var, $_var_pos[$_x], 2);
                         $_last_var_pos = $_var_pos[$_x] + strlen($_convert_var);
                     } else {
                         // remove the brackets only, and replace %S
                         $query_string = substr_replace($query_string, '', $_right_pos, 1);
                         $query_string = substr_replace($query_string, $this->_convert_var($query_vars[$_x], $_match[0][$_x]), $_var_pos[$_x], 2);
                         $query_string = substr_replace($query_string, '', $_left_pos, 1);
                         $_last_var_pos = null;
                     }
                 }
             } else {
                 $query_string = substr_replace($query_string, $this->_convert_var($query_vars[$_x], $_match[0][$_x]), $_var_pos[$_x], 2);
             }
         }
     }
     return $query_string;
 }
Example #9
0
 public function testRunNoWrongSafesqlException()
 {
     \FelixOnline\Core\App::setInstance(null);
     $this->setExpectedException('Exception', 'No safesql setup');
     $app = new \FelixOnline\Core\App(array('base_url' => 'foo'));
     $db = new \ezSQL_mysqli();
     $app['db'] = $db;
     $app['safesql'] = 'foo';
     $app->run();
 }
Example #10
0
 /**
  * Public: Add authors to article
  */
 public function addAuthors($authors)
 {
     $app = App::getInstance();
     foreach ($authors as $author) {
         $authorRecord = new ArticleAuthor();
         $authorRecord->setArticle($this)->setAuthor($author)->save();
     }
     return $authors;
 }
Example #11
0
 /**
  * Query sql
  */
 protected function query($sql)
 {
     $GLOBALS['current_sql'] = $sql;
     $app = \FelixOnline\Core\App::getInstance();
     $item = null;
     if ($this->cache == true) {
         $item = $app['cache']->getItem($this->table . '/' . md5($sql));
         $results = $item->get(\Stash\Invalidation::PRECOMPUTE, 300);
     }
     if ($item && !$item->isMiss()) {
         return $results;
     }
     set_error_handler(function ($errno, $errstr) {
         $sql = $GLOBALS['current_sql'];
         // $sql in query function not in scope here - this is a nasty hack
         unset($GLOBALS['current_sql']);
         throw new SQLException($errstr, $sql);
     });
     $results = $app['db']->get_results($sql);
     restore_error_handler();
     // restore old error handler
     if ($app['db']->last_error) {
         unset($GLOBALS['current_sql']);
         throw new SQLException($app['db']->last_error, $app['db']->captured_errors);
     }
     if ($item) {
         if ($this->cacheExpiry) {
             $item->expiresAfter($this->cacheExpiry);
         }
         $app['cache']->save($item->set($results));
     }
     unset($GLOBALS['current_sql']);
     return $results;
 }
Example #12
0
 public function testLogVisitRepeat()
 {
     $app = \FelixOnline\Core\App::getInstance();
     $pdo = $this->getConnection()->getConnection();
     $article = new \FelixOnline\Core\Article(1);
     $article->logVisit();
     $stm = $pdo->prepare("SELECT COUNT(article) AS hits FROM article_visit WHERE repeat_visit = 0 AND article = :id");
     $stm->execute(array(':id' => 1));
     $row = $stm->fetch();
     $this->assertEquals((int) $row['hits'], 2);
     $this->assertEquals(3, $this->getConnection()->getRowCount('article_visit'));
     $stm2 = $pdo->prepare("SELECT COUNT(*) as count FROM article_visit WHERE repeat_visit = 1");
     $stm2->execute(array());
     $row = $stm2->fetch();
     $this->assertEquals((int) $row['count'], 1);
     $article->logVisit();
     $this->assertEquals(4, $this->getConnection()->getRowCount('article_visit'));
     $stm->execute(array(':id' => 1));
     $row = $stm->fetch();
     $this->assertEquals((int) $row['hits'], 2);
     $stm2 = $pdo->prepare("SELECT COUNT(*) as count FROM article_visit WHERE repeat_visit = 1");
     $stm2->execute(array());
     $row = $stm2->fetch();
     $this->assertEquals((int) $row['count'], 2);
 }
Example #13
0
 public function testCache()
 {
     $app = \FelixOnline\Core\App::getInstance();
     $manager = $this->getManager();
     $manager->cache(true);
     $selects = $app['db']->get_row("SHOW STATUS LIKE 'Com_select'")->Value;
     $this->assertEquals(0, (int) $selects);
     $all = $manager->all();
     $selects = $app['db']->get_row("SHOW STATUS LIKE 'Com_select'")->Value;
     // 7 because of the selects when instantiating the models
     $this->assertEquals(7, (int) $selects);
     $this->assertCount(3, $all);
     $this->assertInstanceOf('FelixOnline\\Core\\Article', $all[0]);
     $all = $manager->all();
     $selects = $app['db']->get_row("SHOW STATUS LIKE 'Com_select'")->Value;
     $this->assertEquals(7, (int) $selects);
     $this->assertCount(3, $all);
     $this->assertInstanceOf('FelixOnline\\Core\\Article', $all[0]);
 }