/** * Constructor. * @param $args array */ function ScheduledTask($args = array()) { $this->_args = $args; $this->_processId = uniqid(); // Check the scheduled task execution log folder. import('lib.pkp.classes.file.PrivateFileManager'); $fileMgr = new PrivateFileManager(); $scheduledTaskFilesPath = realpath($fileMgr->getBasePath()) . DIRECTORY_SEPARATOR . SCHEDULED_TASK_EXECUTION_LOG_DIR; $this->_executionLogFile = $scheduledTaskFilesPath . DIRECTORY_SEPARATOR . str_replace(' ', '', $this->getName()) . '-' . $this->getProcessId() . '-' . date('Ymd') . '.log'; if (!$fileMgr->fileExists($scheduledTaskFilesPath, 'dir')) { $success = $fileMgr->mkdirtree($scheduledTaskFilesPath); if (!$success) { // files directory wrong configuration? assert(false); $this->_executionLogFile = null; } } AppLocale::requireComponents(LOCALE_COMPONENT_PKP_ADMIN, LOCALE_COMPONENT_PKP_COMMON); }
/** * @param $usageEvent array */ private function _writeUsageEventInLogFile($usageEvent) { $desiredParams = array($usageEvent['ip']); if (isset($usageEvent['classification'])) { $desiredParams[] = $usageEvent['classification']; } else { $desiredParams[] = '-'; } if (isset($usageEvent['user'])) { $desiredParams[] = $usageEvent['user']->getId(); } else { $desiredParams[] = '-'; } $desiredParams = array_merge($desiredParams, array('"' . $usageEvent['time'] . '"', $usageEvent['canonicalUrl'], '"' . $usageEvent['userAgent'] . '"')); $usageLogEntry = implode(' ', $desiredParams) . PHP_EOL; import('lib.pkp.classes.file.PrivateFileManager'); $fileMgr = new PrivateFileManager(); // Get the current day filename. $filename = 'usage_events_' . date("Ymd") . '.log'; // Check the plugin file directory. $usageEventFilesPath = realpath($fileMgr->getBasePath()) . DIRECTORY_SEPARATOR . 'usageStats' . DIRECTORY_SEPARATOR . 'usageEventLogs'; if (!$fileMgr->fileExists($usageEventFilesPath, 'dir')) { $success = $fileMgr->mkdirtree($usageEventFilesPath); if (!$success) { // Files directory wrong configuration? assert(false); return false; } } $filePath = $usageEventFilesPath . DIRECTORY_SEPARATOR . $filename; $fp = fopen($filePath, 'ab'); if (flock($fp, LOCK_EX)) { fwrite($fp, $usageLogEntry); flock($fp, LOCK_UN); } else { // Couldn't lock the file. assert(false); } fclose($fp); }
/** * @param $usageEvent array */ function _writeUsageEventInLogFile($usageEvent) { $salt = null; if ($this->getSetting(CONTEXT_ID_NONE, 'dataPrivacyOption')) { // Salt management. if (!Config::getVar('usageStats', 'salt_filepath')) { return false; } $saltFilename = Config::getVar('usageStats', 'salt_filepath'); $currentDate = date("Ymd"); $saltFilenameLastModified = date("Ymd", filemtime($saltFilename)); $file = fopen($saltFilename, 'r'); $salt = trim(fread($file, filesize($saltFilename))); fclose($file); if (empty($salt) || $currentDate != $saltFilenameLastModified) { if (function_exists('mcrypt_create_iv')) { $newSalt = bin2hex(mcrypt_create_iv(16, MCRYPT_DEV_URANDOM | MCRYPT_RAND)); } elseif (function_exists('openssl_random_pseudo_bytes')) { $newSalt = bin2hex(openssl_random_pseudo_bytes(16, $cstrong)); } elseif (file_exists('/dev/urandom')) { $newSalt = bin2hex(file_get_contents('/dev/urandom', false, null, 0, 16)); } else { $newSalt = mt_rand(); } $file = fopen($saltFilename, 'wb'); if (flock($file, LOCK_EX)) { fwrite($file, $newSalt); flock($file, LOCK_UN); } else { assert(false); } fclose($file); $salt = $newSalt; } } // Manage the IP address (evtually hash it) if ($this->getSetting(CONTEXT_ID_NONE, 'dataPrivacyOption')) { if (!isset($salt)) { return false; } // Hash the IP $hashedIp = $this->_hashIp($usageEvent['ip'], $salt); // Never store unhashed IPs! if ($hashedIp === false) { return false; } $desiredParams = array($hashedIp); } else { $desiredParams = array($usageEvent['ip']); } if (isset($usageEvent['classification'])) { $desiredParams[] = $usageEvent['classification']; } else { $desiredParams[] = '-'; } if (!$this->getSetting(CONTEXT_ID_NONE, 'dataPrivacyOption') && isset($usageEvent['user'])) { $desiredParams[] = $usageEvent['user']->getId(); } else { $desiredParams[] = '-'; } $desiredParams = array_merge($desiredParams, array('"' . $usageEvent['time'] . '"', $usageEvent['canonicalUrl'], '200', '"' . $usageEvent['userAgent'] . '"')); $usageLogEntry = implode(' ', $desiredParams) . PHP_EOL; import('lib.pkp.classes.file.PrivateFileManager'); $fileMgr = new PrivateFileManager(); // Get the current day filename. $filename = $this->getUsageEventCurrentDayLogName(); // Check the plugin file directory. $usageEventFilesPath = $this->getUsageEventLogsPath(); if (!$fileMgr->fileExists($usageEventFilesPath, 'dir')) { $success = $fileMgr->mkdirtree($usageEventFilesPath); if (!$success) { // Files directory wrong configuration? assert(false); return false; } } $filePath = $usageEventFilesPath . DIRECTORY_SEPARATOR . $filename; // Log the entry $fp = fopen($filePath, 'ab'); if (flock($fp, LOCK_EX)) { fwrite($fp, $usageLogEntry); flock($fp, LOCK_UN); } else { // Couldn't lock the file. assert(false); } fclose($fp); }