Example #1
0
 /**
  * Save log into file, and try to send via email to Developers
  */
 public static function flushLog()
 {
     $last_flush_time = Settings::get('cms_tools_application_log_flush');
     if (NOW - $last_flush_time < 453600) {
         return;
         // We do not need stats too often, wait 7 days
     }
     // Send data to original developer site of the existing domain
     self::sendInformation();
     // Now prepare file with aggregated data
     $app_log = new AppLogEntityRepository();
     $app_log->addSimpleSelectFields(['id', 'ts', 'user_id', 'url', 'msg', 'p', 'do']);
     if ($last_flush_time) {
         $app_log->setWhereOld($last_flush_time);
     }
     $app_log->addOrderByField('ts', true);
     $app_log->setGenerateOutputWithIterator(false);
     $users = new AdminUserRepository();
     $users->setGenerateOutputWithIterator(false);
     $users->addSimpleSelectFieldsAsString('CONCAT(`' . $users->getDbTableName() . '`.`name`, " ", `' . $users->getDbTableName() . '`.`surname`) AS `user`');
     $app_log->mergeWithCollection($users, 'user_id');
     $data_log = $app_log->getAsArrayOfObjectData(true);
     $usage = new AdminUsageEntityRepository();
     $data_usage = $usage->getAsArrayOfObjectData(true);
     if ($data_log || $data_usage) {
         $data = ['data' => ['domain' => CFG_DOMAIN, 'ts' => NOW], 'logs' => ['app_log' => $data_log, 'usage' => $data_usage]];
         // Save in file
         if (!file_exists(DIR_CACHE)) {
             FileSystem::mkDir(DIR_CACHE);
         }
         file_put_contents(DIR_CACHE . 'log_data', gzencode(json_encode($data)));
         // Send stats
         Mailer::getInstance()->setSubject('Application and Usage log from ' . Configuration::getInstance()->get('site')['name'] . '(till ' . date(CFG_CMS_DATETIME_FORMAT, NOW) . ')')->setSender(Configuration::getInstance()->get('site')['email'])->setRecipient(CMS_SUPPORT_EMAIL)->setMessage('View attached file')->addAttachment(DIR_CACHE . 'log_data')->send();
         $usage->deleteObjectCollection();
     }
     Settings::getInstance()->set('cms_tools_application_log_flush', NOW);
 }
Example #2
0
 /**
  *
  */
 public function __destruct()
 {
     if (Settings::get('do_not_log_cms_usage')) {
         return;
     }
     // This is required for db autocreate
     new AdminUsageEntityRepository();
     foreach ($this->usage as $class_name => $class) {
         foreach ($class as $function_name => $count) {
             $usage = AdminUsageEntityRepository::findOneEntityByCriteria(['function_class' => $class_name, 'function_name' => $function_name]);
             if (!$usage) {
                 $usage = new AdminUsageEntity();
                 $usage->setFunctionClass($class_name);
                 $usage->setFunctionName($function_name);
             }
             $usage->setCounter($usage->getCounter() + $count);
             $usage->save();
         }
     }
 }