public static function test_fs() { //-- if (SMART_FRAMEWORK_TESTUNIT_ALLOW_FS_TESTS !== true) { return SmartComponents::operation_notice('Test Unit File System Tests are DISABLED ...'); } //end if //-- //-- $time = microtime(true); //-- //-- $err = ''; $tests = array(); //-- //-- if ((string) DIRECTORY_SEPARATOR != '\\') { // broken links do not work on Windows ! $tests[] = '##### FileSystem OPERATIONS / TESTS - ALL: #####'; } else { $tests[] = '##### FileSystem OPERATIONS / TESTS *** PARTIAL SUPPORT ONLY (BY PLATFORM) ***: #####'; } //end if else //-- //-- $test_string = '#START#' . "\n" . 'グッド' . "\n" . 'SmartFramework/Test/FileSystem' . "\n" . time() . "\n" . SMART_FRAMEWORK_HTACCESS_NOINDEXING . SMART_FRAMEWORK_HTACCESS_FORBIDDEN . SMART_FRAMEWORK_HTACCESS_NOEXECUTION . "\n" . '#END#'; $test_str_cksum = SmartHashCrypto::sha512($test_string); $long_prefixed = SmartFileSysUtils::prefixed_sha1_path(sha1(time())); $short_prefixed = SmartFileSysUtils::prefixed_uuid10_dir(Smart::uuid_10_seq()); //-- $the_base_folder = 'tmp/tests/'; $the_sufx_folder = 'Folder1'; $the_base_file = 'NORMAL-Write_123_@#.txt'; //-- $the_folder = $the_base_folder . $the_sufx_folder . '/'; $the_copy_folder = $the_base_folder . 'folder2'; $the_move_folder = $the_base_folder . 'FOLDER3'; $the_extra_folder = $the_folder . 'extra/'; $the_file = $the_folder . $the_base_file; //-- $get_folder = SmartFileSysUtils::add_dir_last_slash(SmartFileSysUtils::get_dir_from_path($the_folder)); $get_file = SmartFileSysUtils::get_file_name_from_path($the_file); $get_xfile = SmartFileSysUtils::get_noext_file_name_from_path($the_file); $get_ext = SmartFileSysUtils::get_file_extension_from_path($the_file); //-- $the_copy_file = $the_file . '.copy.txt'; $the_move_file = $the_extra_folder . $the_base_file . '.copy.moved.txt'; $the_broken_link = $the_extra_folder . 'a-broken-link'; $the_broken_dir_link = $the_extra_folder . 'a-broken-dir-link'; $the_good_link = $the_extra_folder . 'a-good-link'; $the_good_dir_link = $the_extra_folder . 'a-good-dir-link'; //-- //-- $tests[] = 'INITIAL-FOLDER: ' . $get_folder; $tests[] = 'NEW-FOLDER: ' . $the_folder; $tests[] = 'NEW-FILE: ' . $the_file; //-- //-- if ((string) $err == '') { $the_test = 'CHECK TEST SAFE PATH NAME: DIR / FILE ...'; $tests[] = $the_test; if ((string) Smart::safe_pathname((string) $get_folder) !== (string) $get_folder or (string) Smart::safe_pathname((string) $the_copy_file) !== (string) $the_copy_file) { $err = 'ERROR: SAFE PATH NAME TEST ... FAILED !!!'; } //end if } //end if //-- if ((string) $err == '') { $the_test = 'CHECK TEST ABSOLUTE / BACKWARD PATHS ...'; $tests[] = $the_test; if (!SmartFileSysUtils::check_file_or_dir_name('/this/is/absolute', 'no') or SmartFileSysUtils::check_file_or_dir_name('/this/is/absolute') or SmartFileSysUtils::check_file_or_dir_name('/this/is/../backward/path')) { $err = 'ERROR: CHECK TEST ABSOLUTE / BACKWARD PATHS ... FAILED !!!'; } //end if } //end if //-- if ((string) $err == '') { $the_test = 'CHECK EXTRACT FOLDER FROM PATH ...'; $tests[] = $the_test; if ((string) $get_folder != SmartFileSysUtils::add_dir_last_slash(Smart::dir_name($the_folder))) { $err = 'ERROR: Path Extraction FAILED: Dir=' . $get_folder . ' ; DirName=' . SmartFileSysUtils::add_dir_last_slash(Smart::dir_name($the_folder)); } //end if } //end if if ((string) $err == '') { $the_test = 'CHECK EXTRACT FILE AND EXTENSION FROM PATH (1) ...'; $tests[] = $the_test; if ((string) $get_folder . SmartFileSysUtils::add_dir_last_slash($the_sufx_folder) . $get_file != $the_file) { $err = 'ERROR :: Path Extraction FAILED: Re-Composed-File=' . $get_folder . SmartFileSysUtils::add_dir_last_slash($the_sufx_folder) . $get_file . ' ; File=' . $the_file; } //end if } //end if if ((string) $err == '') { $the_test = 'CHECK EXTRACT FILE AND EXTENSION FROM PATH (2) ...'; $tests[] = $the_test; if ((string) $get_file != $get_xfile . '.' . $get_ext) { $err = 'ERROR :: Path Extraction FAILED: File=' . $get_file . ' ; XFile=' . $get_xfile . ' ; Ext=' . $get_ext; } //end if } //end if //-- SmartFileSysUtils::raise_error_if_unsafe_path($the_folder); if ((string) $err == '') { $the_test = 'CHECK PATH NAME DIR: check_file_or_dir_name() : ' . $the_folder; $tests[] = $the_test; $result = 0; $result = SmartFileSysUtils::check_file_or_dir_name($the_folder); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if SmartFileSysUtils::raise_error_if_unsafe_path($the_file); if ((string) $err == '') { $the_test = 'CHECK PATH NAME FILE: check_file_or_dir_name() : ' . $the_file; $tests[] = $the_test; $result = 0; $result = SmartFileSysUtils::check_file_or_dir_name($the_file); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if //-- //-- if ((string) $err == '') { $parent_folder = SmartFileSysUtils::add_dir_last_slash(''); $the_test = 'Check Add Dir Last (trailing) Slash: Empty Folder Name'; $tests[] = $the_test; if ((string) $parent_folder != './') { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $parent_folder = SmartFileSysUtils::add_dir_last_slash('.'); $the_test = 'Check Add Dir Last (trailing) Slash: Dot Folder Name: ' . $parent_folder; $tests[] = $the_test; if ((string) $parent_folder != './') { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $parent_folder = SmartFileSysUtils::add_dir_last_slash('./'); $the_test = 'Check Add Dir Last (trailing) Slash: DotSlash Folder Name: ' . $parent_folder; $tests[] = $the_test; if ((string) $parent_folder != './') { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $parent_folder = SmartFileSysUtils::add_dir_last_slash(Smart::dir_name($the_base_folder)); $the_test = 'Check Parent Dir Name with Add Dir Last (trailing) Slash: ' . $parent_folder . ' # from: ' . $the_base_folder; $tests[] = $the_test; if ((string) $parent_folder != 'tmp/') { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if //-- //-- if ((string) $err == '') { if (is_dir($get_folder)) { $the_test = 'DIR DELETE - INIT CLEANUP: dir_delete() + recursive: ' . $get_folder; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::dir_delete($the_base_folder, true); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } else { $tests[] = 'DIR DELETE - INIT CLEANUP: Test Not Run (folder does not exists): ' . $get_folder; } //end if else } //end if //-- if ((string) $err == '') { $the_test = 'DIR CREATE RECURSIVE: dir_recursive_create() : ' . $the_folder . $long_prefixed . $short_prefixed; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::dir_recursive_create($the_folder . $long_prefixed . $short_prefixed); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'DIR CREATE NON-RECURSIVE: dir_create() : extra/ in : ' . $the_extra_folder; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::dir_recursive_create($the_extra_folder); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if //-- if ((string) DIRECTORY_SEPARATOR != '\\') { // broken links do not work on Windows ! if ((string) $err == '') { $the_test = 'CREATE BROKEN FILE LINK FOR DELETION (1): link_create() : as : ' . $the_broken_link; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::link_create('tmp/cache', $the_broken_link); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'DELETE BROKEN FILE LINK (1): delete() : as : ' . $the_broken_link; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::delete($the_broken_link); if ($result !== 1 || is_link($the_broken_link)) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'CREATE BROKEN FILE LINK FOR DELETION (2): link_create() : as : ' . $the_broken_link; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::link_create('tmp/index.html', $the_broken_link); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'DELETE BROKEN FILE LINK (2): dir_delete() : as : ' . $the_broken_link; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::dir_delete($the_broken_link); if ($result !== 1 || is_link($the_broken_link)) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'CREATE BROKEN FILE LINK: link_create() : as : ' . $the_broken_link; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::link_create('tmp/index.html', $the_broken_link); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'CREATE BROKEN DIR LINK: link_create() : as : ' . $the_broken_dir_link; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::link_create('tmp/', $the_broken_dir_link); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'CREATE A FILE LINK: link_create() : as : ' . $the_good_link; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::link_create(Smart::real_path('tmp/index.html'), $the_good_link); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'CREATE A DIR LINK: link_create() : as : ' . $the_good_dir_link; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::link_create(Smart::real_path('tmp/'), $the_good_dir_link); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if } //end if //-- if ((string) $err == '') { $the_test = 'FILE WRITE with empty content: write() : ' . $the_file; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::write($the_file, ''); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'FILE WRITE: write() / before append : ' . $the_file; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::write($the_file, $test_string); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'FILE WRITE: write() +append : ' . $the_file; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::write($the_file, $test_string, 'a'); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'FILE READ / Append: read() Full Size: ' . $the_file; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::read($the_file); if ((string) SmartHashCrypto::sha512($result) != (string) SmartHashCrypto::sha512($test_string . $test_string)) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'FILE WRITE: re-write() : ' . $the_file; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::write($the_file, $test_string); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if //-- if ((string) DIRECTORY_SEPARATOR != '\\') { // broken links do not work on Windows ! if ((string) $err == '') { $the_test = 'FILE WRITE TO A BROKEN LINK: write() : ' . $the_broken_link; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::write($the_broken_link, $test_string); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'DELETE THE BROKEN LINK AFTER write() and RE-CREATE IT : ' . $the_broken_link; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::delete($the_broken_link); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'RE-CREATE BROKEN FILE LINK [AFTER WRITE]: link_create() : as : ' . $the_broken_link; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::link_create('tmp/index.html', $the_broken_link); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'FILE WRITE: write_if_not_exists() with Content Compare to a broken link : ' . $the_broken_link; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::write_if_not_exists($the_broken_link, $test_string, 'yes'); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'DELETE THE BROKEN LINK AFTER write_if_not_exists() and RE-CREATE IT : ' . $the_broken_link; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::delete($the_broken_link); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'RE-CREATE BROKEN FILE LINK [AFTER WRITE-IF-NOT-EXISTS]: link_create() : as : ' . $the_broken_link; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::link_create('tmp/index.html', $the_broken_link); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if } //end if //-- if ((string) $err == '') { $the_test = 'FILE WRITE: write_if_not_exists() without Content Compare : ' . $the_file; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::write_if_not_exists($the_file, $test_string, 'no'); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if //-- if ((string) $err == '') { $the_test = 'FILE READ: read() Full Size: ' . $the_file; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::read($the_file); if ((string) SmartHashCrypto::sha512($result) != (string) $test_str_cksum) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'FILE READ: read() Partial Size, First 10 bytes: ' . $the_file; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::read($the_file, 10); if ((string) sha1($result) != (string) sha1(substr($test_string, 0, 10))) { // here we read bytes so substr() not SmartUnicode::sub_str() should be used $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if //-- if ((string) $err == '') { $the_test = 'FILE STATIC-READ: staticread() Full Size: ' . $the_file; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::staticread($the_file); if ((string) SmartHashCrypto::sha512($result) != (string) $test_str_cksum) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'FILE STATIC-READ: staticread() Partial Size, First 10 bytes: ' . $the_file; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::staticread($the_file, 10); if ((string) sha1($result) != (string) sha1(substr($test_string, 0, 10))) { // here we read bytes so substr() not SmartUnicode::sub_str() should be used $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if //-- if ((string) $err == '') { $the_test = 'FILE COPY: copy() : ' . $the_file . ' to: ' . $the_copy_file; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::copy($the_file, $the_copy_file); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'FILE COPY with OVERWRITE: copy() : ' . $the_file . ' to: ' . $the_copy_file; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::copy($the_file, $the_copy_file, true); // overwrite destination file(s) if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'FILE RE-COPY (test should re-write the destination): copy() : ' . $the_file . ' to: ' . $the_move_file; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::copy($the_file, $the_move_file); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } else { $the_test = 'FILE DELETE: delete() : ' . $the_move_file; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::delete($the_move_file); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if } //end if if ((string) $err == '') { $the_test = 'FILE RENAME/MOVE: rename() : ' . $the_copy_file . ' to: ' . $the_move_file; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::rename($the_copy_file, $the_move_file); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if //-- if ((string) $err == '') { if (is_dir('__development/')) { //-- $the_test = 'RECURSIVE COPY (CLONE) DIR [DEVELOPMENT]: dir_copy() : ' . '__development/' . ' to: ' . $the_folder . '__development'; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::dir_copy('__development/', $the_folder . '__development'); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if //-- if ((string) $err == '') { $the_test = 'DIR COMPARE THE [DEVELOPMENT] SOURCE WITH [DEVELOPMENT] DESTINATION AFTER DIR COPY AND DIR MOVE:' . "\n" . 'compare_folders() : ' . '__development/' . ' with: ' . $the_folder . '__development/'; $tests[] = $the_test; $arr_diff = array(); $arr_diff = SmartFileSystem::compare_folders('__development', $the_folder . '__development', true, true); if (Smart::array_size($arr_diff) > 0) { $err = 'ERROR :: ' . $the_test . ' #DIFFERENCES=' . print_r($arr_diff, 1); } //end if } //end if //-- } else { $tests[] = 'RECURSIVE COPY (CLONE) DIR [DEVELOPMENT]: Test Not Run (Development environment not detected) ...'; } //end if else } //end if //-- if ((string) $err == '') { $the_test = 'RECURSIVE COPY (CLONE) DIR: dir_copy() : ' . $the_folder . ' to: ' . $the_copy_folder; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::dir_copy($the_folder, $the_copy_folder); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if //-- if ((string) $err == '') { $the_test = 'MOVE/RENAME DIR: dir_rename() : ' . $the_copy_folder . ' to: ' . $the_move_folder; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::dir_rename($the_copy_folder, $the_move_folder); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if //-- if ((string) $err == '') { $the_test = 'DIR COMPARE THE SOURCE WITH DESTINATION AFTER DIR COPY AND DIR MOVE: ' . $the_folder . ' with: ' . $the_move_folder; $tests[] = $the_test; $arr_diff = array(); $arr_diff = SmartFileSystem::compare_folders($the_folder, $the_move_folder, true, true); if (Smart::array_size($arr_diff) > 0) { $err = 'ERROR :: ' . $the_test . ' #DIFFERENCES=' . print_r($arr_diff, 1); } //end if } //end if //-- if ((string) $err == '') { $the_test = 'DIR DELETE - SIMPLE: dir_delete() non-recursive: ' . $the_extra_folder; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::dir_delete($the_extra_folder, false); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if if ((string) $err == '') { $the_test = 'DIR DELETE - LAST CLEANUP: dir_delete() + recursive: ' . $get_folder; $tests[] = $the_test; $result = 0; $result = SmartFileSystem::dir_delete($the_base_folder, true); if ($result !== 1) { $err = 'ERROR :: ' . $the_test . ' #RESULT=' . $result; } //end if } //end if //-- //-- $time = 'TOTAL TIME was: ' . (microtime(true) - $time); //-- //-- $end_tests = '##### END TESTS ... ' . $time . ' sec. #####'; //-- //-- if ((string) $err == '') { $img_sign = 'lib/core/img/sign_info.png'; $img_check = 'lib/core/img/q_completed.png'; $text_main = Smart::escape_js('<span style="color:#83B953;">Good ... Perfect :: グッド ... パーフェクト</span>'); $text_info = Smart::escape_js('<h2><span style="color:#83B953;">All</span> the SmartFramework FS Operations <span style="color:#83B953;">Tests PASSED on PHP</span><hr></h2><span style="font-size:14px;">' . Smart::nl_2_br(Smart::escape_html(implode("\n" . '* ', $tests) . "\n" . $end_tests)) . '</span>'); } else { $img_sign = 'lib/core/img/sign_error.png'; $img_check = 'lib/core/img/q_warning.png'; $text_main = Smart::escape_js('<span style="color:#FF5500;">An ERROR occured ... :: エラーが発生しました ...</span>'); $text_info = Smart::escape_js('<h2><span style="color:#FF5500;">A test FAILED</span> when testing FS Operations.<span style="color:#FF5500;"><hr>FAILED Test Details</span>:</h2><br><h3>' . Smart::escape_html($tests[Smart::array_size($tests) - 1]) . '</h3><br><span style="font-size:14px;"><pre>' . Smart::escape_html($err) . '</pre></span>'); } //end if else //-- //-- $html = <<<HTML <h1>SmartFramework LibFileSystem Tests: DONE ... [ Time: {$time} sec. ]</h1> <script type="text/javascript"> \tSmartJS_BrowserUtils.alert_Dialog( \t\t'<img src="{$img_sign}" align="right"><h1>{$text_main}</h1><hr><span style="color:#333333;"><img src="{$img_check}" align="right">{$text_info}<br>', \t\t'', \t\t'FileSystem Operations Test Suite for SmartFramework: PHP', \t\t'920', \t\t'480' \t); </script> HTML; //-- //-- return $html; //-- }
public static final function DownloadsHandler($encrypted_download_pack, $controller_key) { //-- $encrypted_download_pack = (string) $encrypted_download_pack; $controller_key = (string) $controller_key; //-- $client_signature = SmartUtils::get_visitor_signature(); //-- if ((string) SMART_APP_VISITOR_COOKIE == '') { Smart::log_info('File Download', 'Failed: 400 / Invalid Visitor Cookie' . ' on Client: ' . $client_signature); self::Raise400Error('ERROR: Invalid Visitor UUID. Cookies must be enabled to enable this feature !'); return ''; } //end if //-- $downloaded_file = ''; // init //-- $decoded_download_packet = (string) trim((string) SmartUtils::crypto_decrypt((string) $encrypted_download_pack, 'SmartFramework//DownloadLink' . SMART_FRAMEWORK_SECURITY_KEY)); //-- if ((string) $decoded_download_packet != '') { // if data is corrupted, decrypt checksum does not match, will return an empty string //-- if (SMART_FRAMEWORK_ADMIN_AREA === true) { // {{{SYNC-DWN-CTRL-PREFIX}}} $controller_key = (string) 'AdminArea/' . $controller_key; } else { $controller_key = (string) 'IndexArea/' . $controller_key; } //end if //-- {{{SYNC-DOWNLOAD-ENCRYPT-ARR}}} $arr_metadata = explode("\n", (string) $decoded_download_packet, 6); // only need first 5 parts //print_r($arr_metadata); // #PACKET-STRUCTURE# [we will have an array like below, according with the: SmartUtils::create_download_link()] // [TimedAccess]\n // [FilePath]\n // [AccessKey]\n // [UniqueKey]\n // [SFR.UA]\n // #END# //-- $crrtime = (string) trim((string) $arr_metadata[0]); $filepath = (string) trim((string) $arr_metadata[1]); $access_key = (string) trim((string) $arr_metadata[2]); $unique_key = (string) trim((string) $arr_metadata[3]); //-- unset($arr_metadata); //-- $timed_hours = 1; // default expire in 1 hour if (defined('SMART_FRAMEWORK_DOWNLOAD_EXPIRE')) { if ((int) SMART_FRAMEWORK_DOWNLOAD_EXPIRE > 0) { if ((int) SMART_FRAMEWORK_DOWNLOAD_EXPIRE <= 24) { // max is 24 hours (since download link is bind to unique browser signature + unique cookie ... make non-sense to keep more) $timed_hours = (int) SMART_FRAMEWORK_DOWNLOAD_EXPIRE; } //end if } //end if } //end if //-- if ((int) $timed_hours > 0) { if ((int) $crrtime < (int) (time() - 60 * 60 * $timed_hours)) { Smart::log_info('File Download', 'Failed: 403 / Download expired at: ' . date('Y-m-d H:i:s O', (int) $crrtime) . ' for: ' . $filepath . ' on Client: ' . $client_signature); self::Raise403Error('ERROR: The Access Key for this Download is Expired !'); return ''; } //end if } //end if //-- if ((string) $access_key != (string) sha1('DownloadLink:' . SMART_SOFTWARE_NAMESPACE . '-' . SMART_FRAMEWORK_SECURITY_KEY . '-' . SMART_APP_VISITOR_COOKIE . ':' . $filepath . '^' . $controller_key)) { Smart::log_info('File Download', 'Failed: 403 / Invalid Access Key for: ' . $filepath . ' on Client: ' . $client_signature); self::Raise403Error('ERROR: Invalid Access Key for this Download !'); return ''; } //end if //-- if ((string) $unique_key != (string) SmartHashCrypto::sha1('Time=' . $crrtime . '#' . SMART_SOFTWARE_NAMESPACE . '-' . SMART_FRAMEWORK_SECURITY_KEY . '-' . $access_key . '-' . SmartUtils::unique_auth_client_private_key() . ':' . $filepath . '+' . $controller_key)) { Smart::log_info('File Download', 'Failed: 403 / Invalid Client (Unique) Key for: ' . $filepath . ' on Client: ' . $client_signature); self::Raise403Error('ERROR: Invalid Client Key to Access this Download !'); return ''; } //end if //-- if (SmartFileSysUtils::check_file_or_dir_name($filepath)) { //-- $skip_log = 'no'; // default log if (defined('SMART_FRAMEWORK_DOWNLOAD_SKIP_LOG')) { $skip_log = 'yes'; // do not log if accessed via admin area and user is authenticated } //end if //-- $tmp_file_ext = (string) strtolower(SmartFileSysUtils::get_file_extension_from_path($filepath)); // [OK] $tmp_file_name = (string) strtolower(SmartFileSysUtils::get_file_name_from_path($filepath)); //-- $tmp_eval = SmartFileSysUtils::mime_eval($tmp_file_name); $mime_type = (string) $tmp_eval[0]; $mime_disp = (string) $tmp_eval[1]; //-- the path must not start with / but this is tested below $tmp_arr_paths = (array) explode('/', $filepath, 2); // only need 1st part for testing //-- allow file downloads just from specific folders like wpub/ or wsys/ (this is a very important security fix to dissalow any downloads that are not in the specific folders) if (substr((string) $filepath, 0, 1) != '/' and strpos((string) SMART_FRAMEWORK_DOWNLOAD_FOLDERS, '<' . trim((string) $tmp_arr_paths[0]) . '>') !== false and stripos((string) SMART_FRAMEWORK_DENY_UPLOAD_EXTENSIONS, '<' . $tmp_file_ext . '>') === false) { //-- SmartFileSysUtils::raise_error_if_unsafe_path($filepath); // re-test finally //-- @clearstatcache(); //-- if (is_file($filepath)) { //-- if (!headers_sent()) { //-- $fp = @fopen($filepath, 'rb'); $fsize = @filesize($filepath); //-- if (!$fp || $fsize <= 0) { //-- Smart::log_info('File Download', 'Failed: 404 / The requested File is Empty or Not Readable: ' . $filepath . ' on Client: ' . $client_signature); self::Raise404Error('WARNING: The requested File is Empty or Not Readable !'); return ''; //-- } //end if //-- set max execution time to zero ini_set('max_execution_time', 0); // we can expect a long time if file is big, but this will be anyway overriden by the WebServer Timeout Directive //-- // cache headers are presumed to be sent by runtime before of this step //-- header('Content-Type: ' . $mime_type); header('Content-Disposition: ' . $mime_disp); header('Content-Length: ' . $fsize); //-- @fpassthru($fp); // output without reading all in memory //-- @fclose($fp); //-- } else { //-- Smart::log_info('File Download', 'Failed: 500 / Headers Already Sent: ' . $filepath . ' on Client: ' . $client_signature); self::Raise500Error('ERROR: Download Failed, Headers Already Sent !'); return ''; //-- } //end if else //-- if ((string) $skip_log != 'yes') { //-- $downloaded_file = (string) $filepath; // return the file name to be logged //-- } //end if //-- } else { //-- Smart::log_info('File Download', 'Failed: 404 / The requested File does not Exists: ' . $filepath . ' on Client: ' . $client_signature); self::Raise404Error('WARNING: The requested File for Download does not Exists !'); return ''; //-- } //end if else } else { //-- Smart::log_info('File Download', 'Failed: 403 / Access to this File is Denied: ' . $filepath . ' on Client: ' . $client_signature); self::Raise403Error('ERROR: Download Access to this File is Denied !'); return ''; //-- } //end if else //-- } else { //-- Smart::log_info('File Download', 'Failed: 400 / Unsafe File Path: ' . $filepath . ' on Client: ' . $client_signature); self::Raise400Error('ERROR: Unsafe Download File Path !'); return ''; //-- } //end if else //-- } else { //-- Smart::log_info('File Download', 'Failed: 400 / Invalid Data Packet' . ' on Client: ' . $client_signature); self::Raise400Error('ERROR: Invalid Download Data Packet !'); return ''; //-- } //end if else //-- return (string) $downloaded_file; //-- }