Пример #1
0
 /**
  * @param int $age
  */
 public static function deleteOlder($age)
 {
     $age = (int) $age;
     $result = CM_Db_Db::select('cm_tmp_userfile', 'uniqid', '`createStamp` < ' . (time() - $age));
     foreach ($result->fetchAllColumn() as $uniqid) {
         $tmpFile = new CM_File_UserContent_Temp($uniqid);
         $tmpFile->delete();
     }
 }
Пример #2
0
 protected function _process()
 {
     $return = array();
     try {
         $fileInfo = reset($_FILES);
         if (empty($fileInfo)) {
             throw new CM_Exception('Invalid file upload');
         }
         if (isset($fileInfo['error']) && $fileInfo['error'] !== UPLOAD_ERR_OK) {
             throw new CM_Exception('File upload error: ' . self::$_uploadErrors[$fileInfo['error']]);
         }
         $fileTmp = new CM_File($fileInfo['tmp_name']);
         if ($fileTmp->getSize() > self::MAX_FILE_SIZE) {
             throw new CM_Exception_FormFieldValidation(new CM_I18n_Phrase('File too big'));
         }
         $file = CM_File_UserContent_Temp::create($fileInfo['name'], $fileTmp->read());
         $fileTmp->delete();
         $query = $this->_request->getQuery();
         $preview = null;
         if (isset($query['field'])) {
             $field = CM_FormField_File::factory($query['field'], ['name' => 'file']);
             $field->validateFile($file);
             $preview = $this->getRender()->fetchViewTemplate($field, 'preview', array('file' => $file));
         }
         $return['success'] = array('id' => $file->getUniqid(), 'preview' => $preview);
     } catch (CM_Exception_FormFieldValidation $ex) {
         $return['error'] = array('type' => get_class($ex), 'msg' => $ex->getMessagePublic($this->getRender()));
     }
     $this->_setContent(json_encode($return, JSON_HEX_TAG));
     // JSON decoding in IE-iframe needs JSON_HEX_TAG
 }
Пример #3
0
 public function testUpload()
 {
     $filename = 'test.jpg';
     $content = file_get_contents(DIR_TEST_DATA . 'img/' . $filename);
     $fileTmp = CM_File::create($this->_dir . 'test1', $content);
     $_FILES = array('file' => array('name' => $filename, 'tmp_name' => $fileTmp->getPath()));
     $request = new CM_Http_Request_Post('/upload/null');
     $upload = new CM_Http_Response_Upload($request, $this->getServiceManager());
     $upload->process();
     $data = json_decode($upload->getContent(), true);
     $this->assertNotEmpty($data['success']);
     $this->assertEquals(32, strlen($data['success']['id']));
     $file = new CM_File_UserContent_Temp($data['success']['id']);
     $this->assertEquals($content, $file->read());
     $this->assertFalse($fileTmp->exists());
 }
Пример #4
0
 public function testDeleteOlder()
 {
     $file = CM_File_UserContent_Temp::create('foo.txt', 'bar');
     $this->assertTrue($file->exists());
     CM_File_UserContent_Temp::deleteOlder(100);
     $this->assertTrue($file->exists());
     CMTest_TH::timeForward(1000);
     CM_File_UserContent_Temp::deleteOlder(100);
     $this->assertFalse($file->exists());
 }
Пример #5
0
 protected function _registerCallbacks()
 {
     $this->_registerClockworkCallbacks('10 seconds', array('CM_Model_User::offlineDelayed' => function () {
         CM_Model_User::offlineDelayed();
     }));
     $this->_registerClockworkCallbacks('1 minute', array('CM_Model_User::offlineOld' => function () {
         CM_Model_User::offlineOld();
     }, 'CM_ModelAsset_User_Roles::deleteOld' => function () {
         CM_ModelAsset_User_Roles::deleteOld();
     }, 'CM_Paging_Useragent_Abstract::deleteOlder' => function () {
         CM_Paging_Useragent_Abstract::deleteOlder(100 * 86400);
     }, 'CM_File_UserContent_Temp::deleteOlder' => function () {
         CM_File_UserContent_Temp::deleteOlder(86400);
     }, 'CM_SVM_Model::deleteOldTrainings' => function () {
         CM_SVM_Model::deleteOldTrainings(3000);
     }, 'CM_Paging_Ip_Blocked::deleteOlder' => function () {
         CM_Paging_Ip_Blocked::deleteOld();
     }, 'CM_Captcha::deleteOlder' => function () {
         CM_Captcha::deleteOlder(3600);
     }, 'CM_Session::deleteExpired' => function () {
         CM_Session::deleteExpired();
     }, 'CM_Stream_Video::synchronize' => function () {
         CM_Stream_Video::getInstance()->synchronize();
     }, 'CM_Stream_Video::checkStreams' => function () {
         CM_Stream_Video::getInstance()->checkStreams();
     }, 'CM_MessageStream_Service::synchronize' => function () {
         CM_Service_Manager::getInstance()->getStreamMessage()->synchronize();
     }));
     $this->_registerClockworkCallbacks('15 minutes', array('CM_Mail::processQueue' => function () {
         CM_Mail::processQueue(500);
     }, 'CM_Action_Abstract::aggregate' => function () {
         CM_Action_Abstract::aggregate();
     }, 'CM_Action_Abstract::deleteTransgressionsOlder' => function () {
         CM_Action_Abstract::deleteTransgressionsOlder(3 * 31 * 86400);
     }, 'CM_Paging_Log_Abstract::cleanup' => function () {
         foreach (CM_Paging_Log_Abstract::getClassChildren() as $logClass) {
             /** @var CM_Paging_Log_Abstract $log */
             $log = new $logClass();
             $log->cleanUp();
         }
     }));
     $this->_registerClockworkCallbacks('8 days', array('CMService_MaxMind::upgrade' => function () {
         try {
             $maxMind = new CMService_MaxMind();
             $maxMind->upgrade();
         } catch (Exception $exception) {
             if (!is_a($exception, 'CM_Exception')) {
                 $exception = new CM_Exception($exception->getMessage(), ['file' => $exception->getFile(), 'line' => $exception->getLine(), 'trace' => $exception->getTraceAsString()]);
             }
             $exception->setSeverity(CM_Exception::FATAL);
             throw $exception;
         }
     }));
 }
Пример #6
0
 protected function _registerCallbacks()
 {
     $this->_registerClockworkCallbacks('1 second', ['CM_Jobdistribution_DelayedQueue::queueOutstanding' => function () {
         $delayedQueue = $this->getServiceManager()->getDelayedJobQueue();
         $delayedQueue->queueOutstanding();
     }]);
     $this->_registerClockworkCallbacks('1 minute', array('CM_Model_User::offlineOld' => function () {
         CM_Model_User::offlineOld();
     }, 'CM_ModelAsset_User_Roles::deleteOld' => function () {
         CM_ModelAsset_User_Roles::deleteOld();
     }, 'CM_Paging_Useragent_Abstract::deleteOlder' => function () {
         CM_Paging_Useragent_Abstract::deleteOlder(100 * 86400);
     }, 'CM_File_UserContent_Temp::deleteOlder' => function () {
         CM_File_UserContent_Temp::deleteOlder(86400);
     }, 'CM_SVM_Model::deleteOldTrainings' => function () {
         CM_SVM_Model::deleteOldTrainings(3000);
     }, 'CM_Paging_Ip_Blocked::deleteOlder' => function () {
         CM_Paging_Ip_Blocked::deleteOld();
     }, 'CM_Captcha::deleteOlder' => function () {
         CM_Captcha::deleteOlder(3600);
     }, 'CM_Session::deleteExpired' => function () {
         CM_Session::deleteExpired();
     }, 'CM_MessageStream_Service::synchronize' => function () {
         CM_Service_Manager::getInstance()->getStreamMessage()->synchronize();
     }));
     if ($this->getServiceManager()->has('janus')) {
         $this->_registerClockworkCallbacks('1 minute', array('CM_Janus_Service::synchronize' => function () {
             $this->getServiceManager()->getJanus('janus')->synchronize();
         }, 'CM_Janus_Service::checkStreams' => function () {
             $this->getServiceManager()->getJanus('janus')->checkStreams();
         }));
     }
     $this->_registerClockworkCallbacks('15 minutes', array('CM_Action_Abstract::aggregate' => function () {
         CM_Action_Abstract::aggregate();
     }, 'CM_Action_Abstract::deleteTransgressionsOlder' => function () {
         CM_Action_Abstract::deleteTransgressionsOlder(3 * 31 * 86400);
     }, 'CM_Paging_Log::cleanup' => function () {
         $allLevelsList = array_values(CM_Log_Logger::getLevels());
         foreach (CM_Paging_Log::getClassChildren() as $pagingLogClass) {
             /** @type CM_Paging_Log $log */
             $log = new $pagingLogClass($allLevelsList);
             $log->cleanUp();
         }
         (new CM_Paging_Log($allLevelsList, false))->cleanUp();
         //deletes all untyped records
     }));
     if ($this->getServiceManager()->has('maxmind')) {
         $this->_registerClockworkCallbacks('8 days', array('CMService_MaxMind::upgrade' => function () {
             try {
                 /** @var CMService_MaxMind $maxMind */
                 $maxMind = $this->getServiceManager()->get('maxmind', 'CMService_MaxMind');
                 $maxMind->upgrade();
             } catch (Exception $exception) {
                 if (!is_a($exception, 'CM_Exception')) {
                     $exception = new CM_Exception($exception->getMessage(), null, ['file' => $exception->getFile(), 'line' => $exception->getLine(), 'trace' => $exception->getTraceAsString()]);
                 }
                 $exception->setSeverity(CM_Exception::FATAL);
                 throw $exception;
             }
         }));
     }
 }