<commit_before><?php
/**
 * poche, a read it later open source system
 *
 * @category   poche
 * @author     <NAME> <<EMAIL>>
 * @copyright  2013
 * @license    http://www.wtfpl.net/ see COPYING file
 */

class Poche
{
    public static $canRenderTemplates = true;
    public static $configFileAvailable = true;

    public $user;
    public $store;
    public $tpl;
    public $messages;
    public $pagination;
    
    private $currentTheme = '';
    private $notInstalledMessage = '';
    
    # @todo make this dynamic (actually install themes and save them in the database including author information et cetera)
    private $installedThemes = array(
        'default' => array('requires' => array()),
        'dark' => array('requires' => array('default')),
        'dmagenta' => array('requires' => array('default')),
        'solarized' => array('requires' => array('default')),
        'solarized-dark' => array('requires' => array('default'))
    );

    public function __construct()
    {
        if (! $this->configFileIsAvailable()) {
            return;
        }
        
        $this->init();
        
        if (! $this->themeIsInstalled()) {
            return;
        }
        
        $this->initTpl();
        
        if (! $this->systemIsInstalled()) {
            return;
        }
           
        $this->store = new Database();
        $this->messages = new Messages();

        # installation
        if (! $this->store->isInstalled()) {
            $this->install();
        }
    }
    
    private function init() 
    {
        Tools::initPhp();
        Session::$sessionName = 'poche'; 
        Session::init();

        if (isset($_SESSION['poche_user']) && $_SESSION['poche_user'] != array()) {
            $this->user = $_SESSION['poche_user'];
        } else {
            # fake user, just for install & login screens
            $this->user = new User();
            $this->user->setConfig($this->getDefaultConfig());
        }

        # l10n
        $language = $this->user->getConfigValue('language');
        putenv('LC_ALL=' . $language);
        setlocale(LC_ALL, $language);
        bindtextdomain($language, LOCALE); 
        textdomain($language); 

        # Pagination
        $this->pagination = new Paginator($this->user->getConfigValue('pager'), 'p');
        
        # Set up theme
        $themeDirectory = $this->user->getConfigValue('theme');
        
        if ($themeDirectory === false) {
            $themeDirectory = DEFAULT_THEME;
        }
        
        $this->currentTheme = $themeDirectory;
    }

    public function configFileIsAvailable() {
        if (! self::$configFileAvailable) {
            $this->notInstalledMessage = 'You have to rename <strong>inc/poche/config.inc.php.new</strong> to <strong>inc/poche/config.inc.php</strong>.';

            return false;
        }

        return true;
    }
    
    public function themeIsInstalled() {
        # Twig is an absolute requirement for Poche to function. Abort immediately if the Composer installer hasn't been run yet
        if (! self::$canRenderTemplates) {
            $this->notInstalledMessage = 'Twig does not seem to be installed. Please initialize the Composer installation to automatically fetch dependencies. Have a look at <a href="http://inthepoche.com/?pages/Documentation">the documentation.</a>';
            
            return false;
        }
        
        # Check if the selected theme and its requirements are present
        if (! is_dir(THEME . '/' . $this->getTheme())) {
            $this->notInstalledMessage = 'The currently selected theme (' . $this->getTheme() . ') does not seem to be properly installed (Missing directory: ' . THEME . '/' . $this->getTheme() . ')';
            
            self::$canRenderTemplates = false;
            
            return false;
        }
        
        foreach ($this->installedThemes[$this->getTheme()]['requires'] as $requiredTheme) {
            if (! is_dir(THEME . '/' . $requiredTheme)) {
                $this->notInstalledMessage = 'The required "' . $requiredTheme . '" theme is missing for the current theme (' . $this->getTheme() . ')';
                
                self::$canRenderTemplates = false;
                
                return false;
            }
        }
        
        return true;
    }
    
    /**
     * all checks before installation.
     * @todo move HTML to template
     * @return boolean 
     */
    public function systemIsInstalled()
    {
        $msg = '';
        
        $configSalt = defined('SALT') ? constant('SALT') : '';
        
        if (empty($configSalt)) {
            $msg = '<h1>error</h1><p>You have not yet filled in the SALT value in the config.inc.php file.</p>';
        } else if (! is_writable(CACHE)) {
            Tools::logm('you don\'t have write access on cache directory');
            $msg = '<h1>error</h1><p>You don\'t have write access on cache directory.</p>';
        } else if (STORAGE == 'sqlite' && ! file_exists(STORAGE_SQLITE)) {
            Tools::logm('sqlite file doesn\'t exist');
            $msg = '<h1>error</h1><p>sqlite file doesn\'t exist, you can find it in install folder.</p>';
        } else if (file_exists(ROOT . '/install/update.php') && ! DEBUG_POCHE) {
            $msg = '<h1>setup</h1><p><strong>It\'s your first time here?</strong> Please copy /install/poche.sqlite in db folder. Then, delete install folder.<br /><strong>If you have already installed poche</strong>, an update is needed <a href="install/update.php">by clicking here</a>.</p>';
        } else if (is_dir(ROOT . '/install') && ! DEBUG_POCHE) {
            $msg = '<h1>setup</h1><p><strong>If you want to update your poche</strong>, you just have to delete /install folder. <br /><strong>To install your poche with sqlite</strong>, copy /install/poche.sqlite in /db and delete the folder /install. you have to delete the /install folder before using poche.</p>';
        } else if (STORAGE == 'sqlite' && ! is_writable(STORAGE_SQLITE)) {
            Tools::logm('you don\'t have write access on sqlite file');
            $msg = '<h1>error</h1><p>You don\'t have write access on sqlite file.</p>';
        }

        if (! empty($msg)) {
            $this->notInstalledMessage = $msg;

            return false;
        }

        return true;
    }
    
    public function getNotInstalledMessage() {
        return $this->notInstalledMessage;
    }

    private function initTpl()
    {
        $loaderChain = new Twig_Loader_Chain();
       
        # add the current theme as first to the loader chain so Twig will look there first for overridden template files
        try {
            $loaderChain->addLoader(new Twig_Loader_Filesystem(THEME . '/' . $this->getTheme()));
        } catch (Twig_Error_Loader $e) {<commit_msg>help when sqlite file not found<commit_after><?php
/**
 * poche, a read it later open source system
 *
 * @category   poche
 * @author     <NAME> <<EMAIL>>
 * @copyright  2013
 * @license    http://www.wtfpl.net/ see COPYING file
 */

class Poche
{
    public static $canRenderTemplates = true;
    public static $configFileAvailable = true;

    public $user;
    public $store;
    public $tpl;
    public $messages;
    public $pagination;
    
    private $currentTheme = '';
    private $notInstalledMessage = '';
    
    # @todo make this dynamic (actually install themes and save them in the database including author information et cetera)
    private $installedThemes = array(
        'default' => array('requires' => array()),
        'dark' => array('requires' => array('default')),
        'dmagenta' => array('requires' => array('default')),
        'solarized' => array('requires' => array('default')),
        'solarized-dark' => array('requires' => array('default'))
    );

    public function __construct()
    {
        if (! $this->configFileIsAvailable()) {
            return;
        }
        
        $this->init();
        
        if (! $this->themeIsInstalled()) {
            return;
        }
        
        $this->initTpl();
        
        if (! $this->systemIsInstalled()) {
            return;
        }
           
        $this->store = new Database();
        $this->messages = new Messages();

        # installation
        if (! $this->store->isInstalled()) {
            $this->install();
        }
    }
    
    private function init() 
    {
        Tools::initPhp();
        Session::$sessionName = 'poche'; 
        Session::init();

        if (isset($_SESSION['poche_user']) && $_SESSION['poche_user'] != array()) {
            $this->user = $_SESSION['poche_user'];
        } else {
            # fake user, just for install & login screens
            $this->user = new User();
            $this->user->setConfig($this->getDefaultConfig());
        }

        # l10n
        $language = $this->user->getConfigValue('language');
        putenv('LC_ALL=' . $language);
        setlocale(LC_ALL, $language);
        bindtextdomain($language, LOCALE); 
        textdomain($language); 

        # Pagination
        $this->pagination = new Paginator($this->user->getConfigValue('pager'), 'p');
        
        # Set up theme
        $themeDirectory = $this->user->getConfigValue('theme');
        
        if ($themeDirectory === false) {
            $themeDirectory = DEFAULT_THEME;
        }
        
        $this->currentTheme = $themeDirectory;
    }

    public function configFileIsAvailable() {
        if (! self::$configFileAvailable) {
            $this->notInstalledMessage = 'You have to rename <strong>inc/poche/config.inc.php.new</strong> to <strong>inc/poche/config.inc.php</strong>.';

            return false;
        }

        return true;
    }
    
    public function themeIsInstalled() {
        # Twig is an absolute requirement for Poche to function. Abort immediately if the Composer installer hasn't been run yet
        if (! self::$canRenderTemplates) {
            $this->notInstalledMessage = 'Twig does not seem to be installed. Please initialize the Composer installation to automatically fetch dependencies. Have a look at <a href="http://inthepoche.com/?pages/Documentation">the documentation.</a>';
            
            return false;
        }
        
        # Check if the selected theme and its requirements are present
        if (! is_dir(THEME . '/' . $this->getTheme())) {
            $this->notInstalledMessage = 'The currently selected theme (' . $this->getTheme() . ') does not seem to be properly installed (Missing directory: ' . THEME . '/' . $this->getTheme() . ')';
            
            self::$canRenderTemplates = false;
            
            return false;
        }
        
        foreach ($this->installedThemes[$this->getTheme()]['requires'] as $requiredTheme) {
            if (! is_dir(THEME . '/' . $requiredTheme)) {
                $this->notInstalledMessage = 'The required "' . $requiredTheme . '" theme is missing for the current theme (' . $this->getTheme() . ')';
                
                self::$canRenderTemplates = false;
                
                return false;
            }
        }
        
        return true;
    }
    
    /**
     * all checks before installation.
     * @todo move HTML to template
     * @return boolean 
     */
    public function systemIsInstalled()
    {
        $msg = '';
        
        $configSalt = defined('SALT') ? constant('SALT') : '';
        
        if (empty($configSalt)) {
            $msg = '<h1>error</h1><p>You have not yet filled in the SALT value in the config.inc.php file.</p>';
        } else if (! is_writable(CACHE)) {
            Tools::logm('you don\'t have write access on cache directory');
            $msg = '<h1>error</h1><p>You don\'t have write access on cache directory.</p>';
        } else if (STORAGE == 'sqlite' && ! file_exists(STORAGE_SQLITE)) {
            Tools::logm('sqlite file doesn\'t exist');
            $msg = '<h1>error</h1><p>sqlite file doesn\'t exist, you can find it in install folder. Copy it in /db folder.</p>';
        } else if (file_exists(ROOT . '/install/update.php') && ! DEBUG_POCHE) {
            $msg = '<h1>setup</h1><p><strong>It\'s your first time here?</strong> Please copy /install/poche.sqlite in db folder. Then, delete install folder.<br /><strong>If you have already installed poche</strong>, an update is needed <a href="install/update.php">by clicking here</a>.</p>';
        } else if (is_dir(ROOT . '/install') && ! DEBUG_POCHE) {
            $msg = '<h1>setup</h1><p><strong>If you want to update your poche</strong>, you just have to delete /install folder. <br /><strong>To install your poche with sqlite</strong>, copy /install/poche.sqlite in /db and delete the folder /install. you have to delete the /install folder before using poche.</p>';
        } else if (STORAGE == 'sqlite' && ! is_writable(STORAGE_SQLITE)) {
            Tools::logm('you don\'t have write access on sqlite file');
            $msg = '<h1>error</h1><p>You don\'t have write access on sqlite file.</p>';
        }

        if (! empty($msg)) {
            $this->notInstalledMessage = $msg;

            return false;
        }

        return true;
    }
    
    public function getNotInstalledMessage() {
        return $this->notInstalledMessage;
    }

    private function initTpl()
    {
        $loaderChain = new Twig_Loader_Chain();
       
        # add the current theme as first to the loader chain so Twig will look there first for overridden template files
        try {
            $loaderChain->addLoader(new Twig_Loader_Filesystem(THEME . '/' . $this->getTheme()));
        } catch (Twig_Error_Loader $e) {
{'fraction_non_alphanumeric': 0.13760864801694545, 'fraction_numerical': 0.0026294646117887665, 'mean_word_length': 2.136769759450172, 'pattern_counts': {'":': 0, '<': 95, '<?xml version=': 0, '>': 187, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6241967', 'n_tokens_mistral': 3876, 'n_tokens_neox': 3784, 'n_words': 1024}