/** * Obtain the single instance of the elis_files_logger class using the * singleton pattern * * @return object Our single elis_files_logger object */ public static function instance() { if (self::$instance == NULL) { // Instantiate our instance self::$instance = new elis_files_logger(); } return self::$instance; }
/** * Validate that the logger "instance" method follows the singleton * pattern and only ever returns a single logger object */ public function test_logger_maintains_single_instance() { $this->resetAfterTest(true); $this->setup_test_data_xml(); // Validate that only once instance is maintained $logger1 = elis_files_logger::instance(); $logger2 = elis_files_logger::instance(); // Should be the exact same instance $this->assertTrue($logger1 === $logger2); // Should be a valid class instance $this->assertInstanceOf('elis_files_logger', $logger1); }
/** * Upload a file into the repository via FTP. * * @uses $USER * @param string $filename The name of the file being uploaded. * @param string $filepath The full path to the file being uploaded on the local filesystem. * @param string $filemime The MIME/type of the file being uploaded. * @param int $filesize The size in bytes of the file being uploaded. * @param string $uuid The UUID of the folder where the file is being uploaded to. * @param bool $useadmin Set to false to make sure that the administrative user configured in * the plug-in is not used for this operation (default: true). * @return object Node values for the uploaded file. */ function elis_files_upload_ftp($filename, $filepath, $filemime, $filesize, $uuid = '', $useadmin = true) { global $USER; $config = get_config('elisfiles'); // Obtain the logger object in a clean state, in case we need it $logger = elis_files_logger::instance(); $logger->flush(); // We need to get just the hostname out of the configured host URL $uri = parse_url($config->server_host); if (!($ftp = ftp_connect($uri['host'], $config->ftp_port, 5))) { // error_log('Could not connect to Alfresco FTP server '.$uri['host'].$config->ftp_port); // Signal an FTP failure $logger->signal_error(ELIS_FILES_ERROR_FTP); return false; } if (!ftp_login($ftp, $config->server_username, $config->server_password)) { // error_log('Could not authenticate to Alfresco FTP server'); ftp_close($ftp); // Signal an FTP failure $logger->signal_error(ELIS_FILES_ERROR_FTP); return false; } ftp_pasv($ftp, true); $repo_path = elis_files_node_path($uuid); // The FTP server represents "Company Home" as "Alfresco" so we need to change that now: $repo_path = str_replace("/Company Home", "Alfresco", $repo_path, $count); if ($count === 0) { // /Company Home not found, so prepend Alfresco onto the repo path $repo_path = 'Alfresco'.$repo_path; } $repo_path .= (substr($repo_path, -1) !== '/' ? '/' : '').$filename; $res = ftp_put($ftp, $repo_path, $filepath, FTP_BINARY); if ($res != FTP_FINISHED) { // error_log('Could not upload file '.$filepath.' to Alfresco: '.$repo_path.'/'.$filename); // mtrace('$res = '.$res); ftp_close($ftp); // Signal an FTP failure $logger->signal_error(ELIS_FILES_ERROR_FTP); return false; } ftp_close($ftp); $dir = elis_files_read_dir($uuid, $useadmin); if (!empty($dir->files) && ($username = elis_files_transform_username($USER->username))) { foreach ($dir->files as $file) { if ($file->title == $filename) { if (!empty($file->uuid)) { // We're not going to check the response for this right now. elis_files_request('/moodle/nodeowner/' . $file->uuid . '?username=' . $username); } return $file; } } } return false; }