Example #1
0
 protected function _testStreams()
 {
     // You have to do this before working with the session
     $this->prepare('Db', $this->config->get('db'));
     Factory::load('Streams\\Db:streamdb_sessions', 'dbs', $this->db, 'sessions');
     //unlink('db://images/test.jpg');
     //$this->session->set('debug', 'test');
     echo $this->session->get('debug');
     //print_r(scandir('sessions://'));
     Factory::load('Streams\\File:streamfile_public', 'public', PUBLIC_PATH);
     #file_put_contents('public://test.jpg', 'ulfggdfgfdgdfgdf2');
     #unlink('public://test.jpg');
     //Factory::load('Streams\Db:streamdb_files', 'db', $this->db, 'files');
     //file_put_contents('db://images/test.jpg', 'ulfggdfgfdgdfgdf2');
     //file_get_contents('db://images/test.jpg');
     //unlink('db://images/test.jpg');
     //file_put_contents('db://images/test2.jpg', '123');
     //$bla = fopen('db://images/test.jpg', 'w');
     //$bla = fopen('sessions://images/test2.jpg', 'w');
     // var_dump(filetype('db://images/test.jpg'));
     // var_dump(file_exists('db://images/test.jpg'));
     // var_dump(is_readable('db://images/test.jpg'));
     // var_dump(is_writable('db://images/test.jpg'));
     // var_dump(is_executable('db://images/test.jpg'));
     // var_dump(filemtime('db://images/test.jpg'));
     // var_dump(touch('db://images/test.jpg'));
     //print_r( file_get_contents('db://images/test.jpg') );
 }
Example #2
0
 public static function markdown($content)
 {
     $content = \Michelf\MarkdownExtra::defaultTransform($content);
     // change table formatting
     $content = str_replace('<table>', '<table class="table table-striped table-condensed">', $content);
     // add syntax highlighter
     $content = preg_replace('|<pre><code class="([a-z]+?)">|s', '<pre><code class="language-$1">', $content);
     // auto link classes
     $content = preg_replace_callback('|(\\\\[A-Z][\\\\A-Za-z0-9]+)|s', function ($match) {
         $url = Factory::load('Url')->create('class/');
         $url .= str_replace('\\', '/', $match[0]);
         return '<a href="' . $url . '">' . $match[0] . '</a>';
     }, $content);
     return $content;
 }
Example #3
0
 public function run()
 {
     $class = Factory::load('Docblock', '\\Morrow\\View');
     $this->view->setContent('class', $class->get());
 }
Example #4
0
 /**
  * Used for the Serpent mapping `:thumb`. Outputs the path to a thumb of a passed image path.
  * @param   string $filepath The path to the image.
  * @param   array $params THe parameters to edit the image. Explained in \Morrow\Image.
  * @return  string Returns the path to the thumbnail in the temp folder.
  */
 public static function thumb($filepath, $params = array())
 {
     try {
         $path = Factory::load('Image')->get($filepath, $params);
         $path = str_replace(PUBLIC_PATH, '', $path);
     } catch (\Exception $e) {
         if (isset($params['fallback'])) {
             $path = Factory::load('Image')->get($params['fallback'], $params);
             $path = str_replace(PUBLIC_PATH, '', $path);
         } else {
             throw new \Exception(__CLASS__ . ': image "' . $filepath . '" does not exist.');
         }
     }
     return $path;
 }
Example #5
0
 /**
  * This function indexes the passed content.
  * @param   string  $content  The content the view class has created.
  * @return  string  Returns the modified content.
  */
 public function get($content)
 {
     // get output from standard handler
     $original_output = $content;
     // deactivate index building
     if (!$this->_config['buildindex']) {
         return $original_output;
     }
     // check only sometimes
     if (rand(1, $this->_config['check_divisor']) !== 1) {
         return $original_output;
     }
     $output = $original_output;
     // connect to DB and do maintenance
     $this->_searchdb->connect();
     $this->_createTableIfNotExists($this->_config['db_tablename']);
     if (rand(1, $this->_config['gc_divisor']) === 1) {
         $this->_deleteOldEntries($this->_config['db_tablename']);
     }
     // create url for current page
     $url = \Morrow\Factory::load('Url')->create('');
     // \Morrow\Debug::dump($this->checkUrl($url));
     // ##### check checksum #####
     // now check if we have to refresh the database entry
     $md5 = md5($output);
     $result = $this->_searchdb->result("\n\t\t\tSELECT checksum\n\t\t\tFROM " . $this->_config['db_tablename'] . "\n\t\t\tWHERE url = ?\n\t\t", $url);
     if (isset($result['RESULT'][0]['checksum'])) {
         $checksum = $result['RESULT'][0]['checksum'];
     } else {
         $checksum = 1;
     }
     if ($checksum === $md5) {
         // touch the current entry
         $this->_touchEntry($this->_config['db_tablename'], $url);
         return $original_output;
     }
     // ##### create Index #####
     // strip the excludes
     $regex = preg_quote($this->_config['tag_exclude_start']) . '(.+?)' . preg_quote($this->_config['tag_exclude_end']);
     $output = preg_replace('=' . $regex . '=s', '', $output);
     // now we take all content between the include tags
     $regex = preg_quote($this->_config['tag_include_start']) . '(.+?)' . preg_quote($this->_config['tag_include_end']);
     $found = preg_match_all('=' . $regex . '=s', $output, $matches, PREG_PATTERN_ORDER);
     if ($found === 0) {
         return $original_output;
     }
     // put all results in a string
     $output = implode(' ', $matches[1]);
     // create text version of content
     $output = strip_tags($output);
     // Replace useless space
     $output = preg_replace('=([ \\t\\r\\n]|&nbsp;)+=i', ' ', $output);
     // replace entities for better search results
     $output = html_entity_decode($output);
     // first we take the title tag
     $found = preg_match('=<title>(.+?)</title>=is', $original_output, $match);
     if ($found === 0) {
         $title = '';
     } else {
         $title = html_entity_decode($match[1]);
     }
     // save to database
     $replace['url'] = $url;
     $replace['checksum'] = $md5;
     $replace['title'] = $title;
     $replace['searchdata'] = $output;
     $replace['bytes'] = strlen($original_output);
     $replace['changed'] = array('FUNC' => "datetime('now')");
     // execute exclude patterns
     $save_to_db = true;
     foreach ($this->exclude_patterns as $patterns) {
         foreach ($patterns as $field => $pattern) {
             if (preg_match($pattern, $replace[$field])) {
                 $save_to_db = false;
                 break;
             }
         }
     }
     // now save
     if ($save_to_db) {
         $this->_searchdb->replace($this->_config['db_tablename'], $replace);
         $this->_searchdb->query("VACUUM");
     }
     // output the original content
     return $original_output;
 }
Example #6
0
 /**
  * This function contains the main application flow.
  * @hidden
  */
 public function __construct()
 {
     /* global settings
      ********************************************************************************************/
     // compress the output
     if (!ob_start("ob_gzhandler")) {
         ob_start();
     }
     // include E_STRICT in error_reporting
     error_reporting(E_ALL | E_STRICT);
     /* declare errorhandler (needs config class)
      ********************************************************************************************/
     set_error_handler(array($this, 'errorHandler'));
     set_exception_handler(array($this, 'exceptionHandler'));
     /* load all config files
      ********************************************************************************************/
     $this->config = Factory::load('Config');
     $config = $this->config->load(APP_PATH . 'configs/');
     /* extract important variables
      ********************************************************************************************/
     // map cli parameters to $_GET
     if (php_sapi_name() === 'cli') {
         global $argc, $argv;
         if (isset($argv[2])) {
             parse_str($argv[2], $_GET);
         }
         $_GET['morrow_path_info'] = isset($argv[1]) ? $argv[1] : '';
     }
     $morrow_path_info = $_GET['morrow_path_info'];
     $basehref_depth = isset($_GET['morrow_basehref_depth']) ? $_GET['morrow_basehref_depth'] : 0;
     unset($_GET['morrow_path_info']);
     unset($_GET['morrow_basehref_depth']);
     /* load some necessary classes
      ********************************************************************************************/
     $this->input = Factory::load('Input');
     $this->page = Factory::load('Page');
     /* set timezone 
      ********************************************************************************************/
     if (!date_default_timezone_set($config['locale']['timezone'])) {
         throw new \Exception(__METHOD__ . '<br>date_default_timezone_set() failed.');
     }
     /* load page class and set nodes
      ********************************************************************************************/
     $url = $morrow_path_info;
     $url = preg_match('~[a-z0-9\\-/]~i', $url) ? trim($url, '/') : '';
     $nodes = explode('/', $url);
     $this->page->set('nodes', $nodes);
     /* load languageClass and define alias
      ********************************************************************************************/
     $lang['possible'] = $config['languages'];
     $lang['language_path'] = APP_PATH . 'languages/';
     $lang['search_paths'] = array(APP_PATH . 'templates/*', APP_PATH . '*.php');
     $this->language = Factory::load('Language', $lang);
     // language via path
     if (isset($nodes[0]) && $this->language->isValid($nodes[0])) {
         $input_lang_nodes = array_shift($nodes);
         $this->page->set('nodes', $nodes);
     }
     // language via input
     $lang['actual'] = $this->input->get('language');
     if ($lang['actual'] === null && isset($input_lang_nodes)) {
         $lang['actual'] = $input_lang_nodes;
     }
     if ($lang['actual'] !== null) {
         $this->language->set($lang['actual']);
     }
     /* url routing
      ********************************************************************************************/
     $routes = $config['routing'];
     $url = implode('/', $this->page->get('nodes'));
     // iterate all rules
     foreach ($routes as $rule => $new_url) {
         $rule = trim($rule, '/');
         $new_url = trim($new_url, '/');
         $regex = '=^' . $rule . '$=';
         // rebuild route to a preg pattern
         if (preg_match($regex, $url, $matches)) {
             $url = preg_replace($regex, $new_url, $url);
             unset($matches[0]);
             foreach ($matches as $key => $value) {
                 $this->input->set('routed.' . $key, $value);
             }
         }
     }
     // set nodes in page class
     $nodes = explode('/', $url);
     $nodes = array_map('strtolower', $nodes);
     /* prepare some internal variables
      ********************************************************************************************/
     $alias = implode('_', $nodes);
     $controller_file = APP_PATH . '_default.php';
     $page_controller_file = APP_PATH . $alias . '.php';
     $path = implode('/', $this->page->get('nodes'));
     $query = $this->input->getGet();
     $fullpath = $path . (count($query) > 0 ? '?' . http_build_query($query, '', '&') : '');
     /* load classes we need anyway
      ********************************************************************************************/
     $this->view = Factory::load('View');
     $this->url = Factory::load('Url', $this->language->get(), $lang['possible'], $fullpath, $basehref_depth);
     /* prepare classes so the user has less to pass
      ********************************************************************************************/
     Factory::prepare('Cache', STORAGE_PATH . 'codecache/');
     Factory::prepare('Db', $config['db']);
     Factory::prepare('Debug', $config['debug']);
     Factory::prepare('Image', PUBLIC_STORAGE_PATH . 'thumbs/');
     Factory::prepare('Log', $config['log']);
     Factory::prepare('MessageQueue', $config['messagequeue'], $this->input);
     Factory::prepare('Navigation', Factory::load('Language')->getTree(), $alias);
     Factory::prepare('Pagesession', 'page.' . $alias);
     Factory::prepare('Session', $config['session']);
     Factory::prepare('Security', new Factory('Session'), $this->view, $this->input, $this->url);
     /* define page params
      ********************************************************************************************/
     $base_href = $this->url->getBaseHref();
     $this->page->set('base_href', $base_href);
     $this->page->set('alias', $alias);
     $this->page->set('path.relative', $path);
     $this->page->set('path.relative_with_query', $fullpath);
     $this->page->set('path.absolute', $base_href . $path);
     $this->page->set('path.absolute_with_query', $base_href . $fullpath);
     /* load controller and render page
      ********************************************************************************************/
     // include global controller class
     include $controller_file;
     // include page controller class
     if (is_file($page_controller_file)) {
         include $page_controller_file;
         $controller = new \App\PageController();
         if (method_exists($controller, 'setup')) {
             $controller->setup();
         }
         $controller->run();
         if (method_exists($controller, 'teardown')) {
             $controller->teardown();
         }
     } else {
         $controller = new \App\DefaultController();
         if (method_exists($controller, 'setup')) {
             $controller->setup();
         }
         if (method_exists($controller, 'teardown')) {
             $controller->teardown();
         }
     }
     // assign the content to the view
     $this->view->setContent('page', $this->page->get());
     $view = $this->view->get();
     $headers = $view['headers'];
     $handle = $view['content'];
     // output headers
     foreach ($headers as $h) {
         header($h);
     }
     rewind($handle);
     fpassthru($handle);
     fclose($handle);
     ob_end_flush();
 }
 /**
  * Updates a row in the database with `$data` filtered by the member `$_allowed_update_fields`.
  * 
  * @param  array $data The fields to update.
  * @param  mixed $conditions  An integer (as `id`) or an associative array with conditions that must be fulfilled by the rows to be processed.
  * @return array An result array with the keys `SUCCESS` (boolean Was the query successful) and `AFFECTED_ROWS` (int The count of the affected rows).
  */
 public function update($data, $conditions)
 {
     $data = $this->filterFields($data, $this->_allowed_update_fields);
     $data['updated_at'] = Factory::load('\\datetime')->format('Y-m-d H:i:s');
     if (is_scalar($conditions)) {
         $conditions = array('id' => $conditions);
     }
     $where = $this->_createWhere($conditions);
     return $this->_db->updateSafe($this->_table, $data, "WHERE {$where}", array_values($conditions), true);
 }