/**
  * Performs a batch insert into a specific table using either LOAD DATA INFILE or plain INSERTs,
  * as a fallback. On MySQL, LOAD DATA INFILE is 20x faster than a series of plain INSERTs.
  *
  * @param string $tableName PREFIXED table name! you must call Common::prefixTable() before passing the table name
  * @param array $fields array of unquoted field names
  * @param array $values array of data to be inserted
  * @param bool $throwException Whether to throw an exception that was caught while trying
  *                                LOAD DATA INFILE, or not.
  * @throws Exception
  * @return bool  True if the bulk LOAD was used, false if we fallback to plain INSERTs
  */
 public static function tableInsertBatch($tableName, $fields, $values, $throwException = false)
 {
     $filePath = PIWIK_USER_PATH . '/tmp/assets/' . $tableName . '-' . Common::generateUniqId() . '.csv';
     $filePath = SettingsPiwik::rewriteTmpPathWithHostname($filePath);
     $loadDataInfileEnabled = Config::getInstance()->General['enable_load_data_infile'];
     if ($loadDataInfileEnabled && Db::get()->hasBulkLoader()) {
         try {
             $fileSpec = array('delim' => "\t", 'quote' => '"', 'escape' => '\\\\', 'escapespecial_cb' => function ($str) {
                 return str_replace(array(chr(92), chr(34)), array(chr(92) . chr(92), chr(92) . chr(34)), $str);
             }, 'eol' => "\r\n", 'null' => 'NULL');
             // hack for charset mismatch
             if (!DbHelper::isDatabaseConnectionUTF8() && !isset(Config::getInstance()->database['charset'])) {
                 $fileSpec['charset'] = 'latin1';
             }
             self::createCSVFile($filePath, $fileSpec, $values);
             if (!is_readable($filePath)) {
                 throw new Exception("File {$filePath} could not be read.");
             }
             $rc = self::createTableFromCSVFile($tableName, $fields, $filePath, $fileSpec);
             if ($rc) {
                 unlink($filePath);
                 return true;
             }
         } catch (Exception $e) {
             Log::info("LOAD DATA INFILE failed or not supported, falling back to normal INSERTs... Error was: %s", $e->getMessage());
             if ($throwException) {
                 throw $e;
             }
         }
     }
     // if all else fails, fallback to a series of INSERTs
     @unlink($filePath);
     self::tableInsertBatchIterate($tableName, $fields, $values);
     return false;
 }
Example #2
0
 public function __construct()
 {
     $loader = $this->getDefaultThemeLoader();
     $this->addPluginNamespaces($loader);
     // If theme != default we need to chain
     $chainLoader = new Twig_Loader_Chain(array($loader));
     // Create new Twig Environment and set cache dir
     $templatesCompiledPath = PIWIK_USER_PATH . '/tmp/templates_c';
     $templatesCompiledPath = SettingsPiwik::rewriteTmpPathWithHostname($templatesCompiledPath);
     $this->twig = new Twig_Environment($chainLoader, array('debug' => true, 'strict_variables' => true, 'cache' => $templatesCompiledPath));
     $this->twig->addExtension(new Twig_Extension_Debug());
     $this->twig->clearTemplateCache();
     $this->addFilter_translate();
     $this->addFilter_urlRewriteWithParameters();
     $this->addFilter_sumTime();
     $this->addFilter_money();
     $this->addFilter_truncate();
     $this->addFilter_notificiation();
     $this->addFilter_percentage();
     $this->twig->addFilter(new Twig_SimpleFilter('implode', 'implode'));
     $this->twig->addFilter(new Twig_SimpleFilter('ucwords', 'ucwords'));
     $this->addFunction_includeAssets();
     $this->addFunction_linkTo();
     $this->addFunction_sparkline();
     $this->addFunction_postEvent();
     $this->addFunction_isPluginLoaded();
     $this->addFunction_getJavascriptTranslations();
 }
Example #3
0
 /**
  * @param string $directory directory to use
  * @param int $timeToLiveInSeconds TTL
  */
 public function __construct($directory, $timeToLiveInSeconds = 300)
 {
     $cachePath = PIWIK_USER_PATH . '/tmp/cache/' . $directory . '/';
     $this->cachePath = SettingsPiwik::rewriteTmpPathWithHostname($cachePath);
     if ($timeToLiveInSeconds < self::MINIMUM_TTL) {
         $timeToLiveInSeconds = self::MINIMUM_TTL;
     }
     $this->ttl = $timeToLiveInSeconds;
 }
 public function installOrUpdatePluginFromFile($pathToZip)
 {
     $tmpPluginFolder = PIWIK_USER_PATH . self::PATH_TO_DOWNLOAD . $this->pluginName;
     $tmpPluginFolder = SettingsPiwik::rewriteTmpPathWithHostname($tmpPluginFolder);
     try {
         $this->makeSureFoldersAreWritable();
         $this->extractPluginFiles($pathToZip, $tmpPluginFolder);
         $this->makeSurePluginJsonExists($tmpPluginFolder);
         $metadata = $this->getPluginMetadataIfValid($tmpPluginFolder);
         $this->makeSureThereAreNoMissingRequirements($metadata);
         $this->pluginName = $metadata->name;
         $this->fixPluginFolderIfNeeded($tmpPluginFolder);
         $this->copyPluginToDestination($tmpPluginFolder);
     } catch (\Exception $e) {
         $this->removeFileIfExists($pathToZip);
         $this->removeFolderIfExists($tmpPluginFolder);
         throw $e;
     }
     $this->removeFileIfExists($pathToZip);
     $this->removeFolderIfExists($tmpPluginFolder);
     return $metadata;
 }
 private function oneClick_Unpack()
 {
     $pathExtracted = PIWIK_USER_PATH . self::PATH_TO_EXTRACT_LATEST_VERSION;
     $pathExtracted = SettingsPiwik::rewriteTmpPathWithHostname($pathExtracted);
     $this->pathRootExtractedPiwik = $pathExtracted . 'piwik';
     if (file_exists($this->pathRootExtractedPiwik)) {
         Filesystem::unlinkRecursive($this->pathRootExtractedPiwik, true);
     }
     $archive = Unzip::factory('PclZip', $this->pathPiwikZip);
     if (0 == ($archive_files = $archive->extract($pathExtracted))) {
         throw new Exception(Piwik::translate('CoreUpdater_ExceptionArchiveIncompatible', $archive->errorInfo()));
     }
     if (0 == count($archive_files)) {
         throw new Exception(Piwik::translate('CoreUpdater_ExceptionArchiveEmpty'));
     }
     unlink($this->pathPiwikZip);
 }
Example #6
0
 private function setLogFilePathFromConfig($logConfig)
 {
     $logPath = $logConfig[self::LOGGER_FILE_PATH_CONFIG_OPTION];
     if (!SettingsServer::isWindows() && $logPath[0] != '/') {
         $logPath = PIWIK_USER_PATH . DIRECTORY_SEPARATOR . $logPath;
     }
     $logPath = SettingsPiwik::rewriteTmpPathWithHostname($logPath);
     if (is_dir($logPath)) {
         $logPath .= '/piwik.log';
     }
     $this->logToFilePath = $logPath;
 }
 /**
  * Return $filename with temp directory and delete file
  *
  * @static
  * @param  $filename
  * @return string path of file in temp directory
  */
 protected static function getOutputPath($filename)
 {
     $outputFilename = PIWIK_USER_PATH . '/tmp/assets/' . $filename;
     $outputFilename = SettingsPiwik::rewriteTmpPathWithHostname($outputFilename);
     @chmod($outputFilename, 0600);
     @unlink($outputFilename);
     return $outputFilename;
 }
Example #8
0
 /**
  * Returns the directory session files are stored in.
  *
  * @return string
  */
 public static function getSessionsDirectory()
 {
     $path = PIWIK_USER_PATH . '/tmp/sessions';
     return SettingsPiwik::rewriteTmpPathWithHostname($path);
 }
 * Piwik - Open source web analytics
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 * @category Piwik_Plugins
 * @package ScheduledReports
 */
/**
 * Override settings in libs/tcpdf_config.php
 *
 * @package ScheduledReports
 */
define('K_PATH_MAIN', PIWIK_INCLUDE_PATH . '/libs/tcpdf/');
$pathTmpTCPDF = PIWIK_USER_PATH . '/tmp/tcpdf/';
$pathTmpTCPDF = \Piwik\SettingsPiwik::rewriteTmpPathWithHostname($pathTmpTCPDF);
define('K_PATH_CACHE', $pathTmpTCPDF);
define('K_PATH_IMAGES', $pathTmpTCPDF);
if (!defined('K_TCPDF_EXTERNAL_CONFIG')) {
    // DOCUMENT_ROOT fix for IIS Webserver
    if (!isset($_SERVER['DOCUMENT_ROOT']) or empty($_SERVER['DOCUMENT_ROOT'])) {
        if (isset($_SERVER['SCRIPT_FILENAME'])) {
            $_SERVER['DOCUMENT_ROOT'] = str_replace('\\', '/', substr($_SERVER['SCRIPT_FILENAME'], 0, 0 - strlen($_SERVER['PHP_SELF'])));
        } elseif (isset($_SERVER['PATH_TRANSLATED'])) {
            $_SERVER['DOCUMENT_ROOT'] = str_replace('\\', '/', substr(str_replace('\\\\', '\\', $_SERVER['PATH_TRANSLATED']), 0, 0 - strlen($_SERVER['PHP_SELF'])));
        } else {
            // define here your DOCUMENT_ROOT path if the previous fails
            $_SERVER['DOCUMENT_ROOT'] = '/var/www';
        }
    }
    if (!defined('K_PATH_MAIN')) {
 /**
  * Check if the merged file directory exists and is writable.
  *
  * @return string The directory location
  * @throws Exception if directory is not writable.
  */
 public function getAssetDirectory()
 {
     $mergedFileDirectory = PIWIK_USER_PATH . "/tmp/assets";
     $mergedFileDirectory = SettingsPiwik::rewriteTmpPathWithHostname($mergedFileDirectory);
     if (!is_dir($mergedFileDirectory)) {
         Filesystem::mkdir($mergedFileDirectory);
     }
     if (!is_writable($mergedFileDirectory)) {
         throw new Exception("Directory " . $mergedFileDirectory . " has to be writable.");
     }
     return $mergedFileDirectory;
 }