/**
  * Get A UNIQUE (SAFE) ID for DB Tables
  *
  * @param ENUM $y_mode 							:: mode: uid10str | uid10num | uid36 | uid45
  * @param STRING $y_field_name 					:: the field name
  * @param STRING $y_table_name 					:: the table name
  * @param RESOURCE $y_connection 				:: the connection to mysql server
  * @return STRING 								:: the generated Unique ID
  *
  */
 public static function new_safe_id($y_mode, $y_id_field, $y_table_name, $y_connection = 'DEFAULT')
 {
     //==
     $y_connection = self::check_connection($y_connection, 'NEW-SAFE-ID');
     //==
     //--
     if (!self::validate_table_and_fields_names($y_table_name)) {
         self::error(self::get_connection_id($y_connection), 'NEW-SAFE-ID', 'Get New Safe ID', 'Invalid Table Name', $y_table_name);
         return '';
     }
     //end if
     if (!self::validate_table_and_fields_names($y_id_field)) {
         self::error(self::get_connection_id($y_connection), 'NEW-SAFE-ID', 'Get New Safe ID', 'Invalid Field Name', $y_id_field . ' / [Table=' . $y_table_name . ']');
         return '';
     }
     //end if
     //--
     //--
     $tmp_result = 'NO-ID-INIT';
     //init (must be not empty)
     $counter = 0;
     // default is zero
     //--
     while ((string) $tmp_result != '') {
         // while we cannot find an unused ID
         //--
         $counter += 1;
         //--
         if ($counter > 7500) {
             // loop to max 7500
             self::error(self::get_connection_id($y_connection), 'NEW-SAFE-ID', 'Get New Safe ID', 'Could Not Assign a Unique ID', '(timeout / 7500) ... try again !');
             return '';
         }
         //end if
         //--
         if ($counter % 500 == 0) {
             sleep(1);
         }
         //end if
         //--
         $new_id = 'NO-ID-ALGO';
         switch ((string) $y_mode) {
             case 'uid45':
                 $new_id = (string) Smart::uuid_45(SMART_FRAMEWORK_NETSERVER_ID . SmartUtils::get_server_current_url());
                 // will use the server ID.Host as Prefix to ensure it is true unique in a cluster
                 break;
             case 'uid36':
                 $new_id = (string) Smart::uuid_36(SMART_FRAMEWORK_NETSERVER_ID . SmartUtils::get_server_current_url());
                 // will use the server ID.Host as Prefix to ensure it is true unique in a cluster
                 break;
                 //			case 'uid10seq': // sequences are not safe without a second registry allocation table as the chance to generate the same ID in the same time moment is just 1 in 999
                 //				$new_id = (string) Smart::uuid_10_seq();
                 //				break;
             //			case 'uid10seq': // sequences are not safe without a second registry allocation table as the chance to generate the same ID in the same time moment is just 1 in 999
             //				$new_id = (string) Smart::uuid_10_seq();
             //				break;
             case 'uid10num':
                 $new_id = (string) Smart::uuid_10_num();
                 break;
             case 'uid10str':
             default:
                 $new_id = (string) Smart::uuid_10_str();
         }
         //end switch
         //--
         $result_arr = array();
         $result_arr = self::read_data('SELECT `' . $y_id_field . '` FROM `' . $y_table_name . '` WHERE (`' . $y_id_field . '` = \'' . $this->quote($new_id) . '\') LIMIT 1 OFFSET 0', 'Checking if NEW ID Exists ...', $y_connection);
         $tmp_result = (string) trim((string) $result_arr[0]);
         $result_arr = array();
         //--
     }
     //end while
     //--
     //--
     return (string) $new_id;
     //--
 }
 /**
  * Generate a PDF Document on the fly from a piece of HTML code.
  *
  * Notice: this is using a secured cache folder, unique per visitor ID
  *
  * @param STRING $y_html_content				:: The HTML Code
  * @param ENUM $y_orientation					:: Page Orientation: 'normal' | 'wide'
  * @param STRING $y_runtime_script 				:: The allowed Runtime Script to allow send credentials for sub-downloads. Ex: admin.php
  * @param STRING $y_runtime_url					:: The allowed Runtime URL ended by '/' to allow send credentials for sub-downloads. Ex: http(s)://some-server/some_path/ ; normally this should be set in config to enforce https:// and a single URL only
  * @param BOOLEAN $y_allow_send_credentials 	:: Set to TRUE to allow or set to FALSE to dissalow sending the auth credentials for sub-downloads: in the case there are embedded pictures generated by admin.php which may need authentication before to work, the credentials need to be set automatically in this case
  *
  * @returns STRING 							:: The PDF Document Contents
  *
  */
 public static function generate($y_html_content, $y_orientation = 'normal', $y_runtime_script = '', $y_runtime_url = '', $y_allow_send_credentials = false)
 {
     //--
     $pdfdata = '';
     //--
     $htmldoc = self::is_active();
     //--
     if ((string) $htmldoc != '') {
         //--
         if ((string) $y_orientation == 'wide') {
             $orientation = self::tag_page_wide();
         } else {
             $orientation = self::tag_page_normal();
         }
         //end if else
         //--
         $tmp_prefix_dir = 'tmp/cache/pdf/';
         $protect_file = $tmp_prefix_dir . '.htaccess';
         $dir = $tmp_prefix_dir . SMART_FRAMEWORK_SESSION_PREFIX . '/';
         // we use different for index / admin / @
         //--
         $uniquifier = SmartUtils::unique_auth_client_private_key() . SMART_APP_VISITOR_COOKIE;
         $the_dir = $dir . Smart::safe_varname(Smart::uuid_10_seq() . '_' . Smart::uuid_10_num() . '_' . SmartHashCrypto::sha1($uniquifier)) . '/';
         //--
         $tmp_uuid = Smart::uuid_45($uniquifier) . Smart::uuid_36($uniquifier);
         $file = $the_dir . '__document_' . SmartHashCrypto::sha256('@@PDF#File::Cache@@' . $tmp_uuid) . '.html';
         $logfile = $the_dir . '__headers_' . SmartHashCrypto::sha256('@@PDF#File::Cache@@' . $tmp_uuid) . '.log';
         //--
         if (is_dir($the_dir)) {
             SmartFileSystem::dir_delete($the_dir);
         }
         //end if
         //--
         if (!is_dir($the_dir)) {
             SmartFileSystem::dir_recursive_create($the_dir);
         }
         // end if
         //--
         SmartFileSystem::write_if_not_exists($protect_file, trim(SMART_FRAMEWORK_HTACCESS_FORBIDDEN) . "\n", 'yes');
         //-- process the code
         $y_html_content = (string) self::remove_between_tags((string) $y_html_content);
         $y_html_content = (string) self::safe_charset((string) $y_html_content);
         //-- extract images
         $htmlparser = new SmartHtmlParser((string) $y_html_content);
         $arr_imgs = $htmlparser->get_tags('img');
         $htmlparser = '';
         unset($htmlparser);
         //--
         $chk_duplicates_arr = array();
         //--
         for ($i = 0; $i < Smart::array_size($arr_imgs); $i++) {
             //--
             $tmp_img_src = trim((string) $arr_imgs[$i]['src']);
             //--
             if (strlen($chk_duplicates_arr[$tmp_img_src]) <= 0) {
                 //--
                 $tmp_url_img_src = '';
                 //--
                 if ((string) $y_runtime_script != '' and (string) $y_runtime_url != '') {
                     // replace relative paths
                     if (substr($tmp_img_src, 0, @strlen($y_runtime_script)) == (string) $y_runtime_script) {
                         $tmp_url_img_src = (string) $y_runtime_url . $tmp_img_src;
                         $y_html_content = (string) @str_replace('src="' . $tmp_img_src . '"', 'src="' . $tmp_url_img_src . '"', (string) $y_html_content);
                         $tmp_img_src = (string) $tmp_url_img_src;
                     }
                     //end if
                 }
                 //end if
                 //--
                 $tmp_img_ext = '.' . strtolower(SmartFileSysUtils::get_file_extension_from_path($tmp_img_src));
                 // [OK]
                 $tmp_img_cache = 'pdf_img_' . SmartHashCrypto::sha256('@@PDF#File::Cache::IMG@@' . '#' . $i . '@' . $tmp_img_src . '//' . $tmp_uuid);
                 //--
                 $tmp_arr = array();
                 //--
                 if (substr($tmp_img_src, 0, 7) == 'http://' or substr($tmp_img_src, 0, 8) == 'https://') {
                     //--
                     $tmp_img_ext = '';
                     // we clear the extension as we don't know yet (we will get it from headers)
                     $tmp_img_cache = 'pdf_url_img_' . SmartHashCrypto::sha256('@@PDF#File::Cache::URL::IMG@@' . '#' . $i . '@' . $tmp_img_src . '//' . $tmp_uuid);
                     //--
                 }
                 //end if
                 //--
                 if ($y_allow_send_credentials === true) {
                     $allow_set_credentials = 'yes';
                 } else {
                     $allow_set_credentials = 'no';
                 }
                 //end if else
                 //--
                 $tmp_arr = SmartUtils::load_url_or_file($tmp_img_src, SMART_FRAMEWORK_NETSOCKET_TIMEOUT, 'GET', '', '', '', $allow_set_credentials);
                 // [OK] :: allow set credentials
                 //--
                 $tmp_img_ext = '.noextension';
                 $tmp_where_we_guess = '';
                 //--
                 $guess_arr = array();
                 //--
                 $guess_arr = SmartUtils::guess_image_extension_by_url_head($tmp_arr['headers']);
                 $tmp_img_ext = (string) $guess_arr['extension'];
                 $tmp_where_we_guess = (string) $guess_arr['where-was-detected'];
                 $guess_arr = array();
                 if ((string) $tmp_img_ext == '') {
                     $tmp_img_ext = SmartUtils::guess_image_extension_by_first_bytes(substr($tmp_arr['content'], 0, 256));
                     if ((string) $tmp_img_ext != '') {
                         $tmp_where_we_guess = ' First Bytes ...';
                     }
                     //end if
                 }
                 //end if
                 //--
                 if ((string) SMART_FRAMEWORK_DEBUG_MODE == 'yes') {
                     // if debug, append information to log
                     SmartFileSystem::write($logfile, '####################' . "\n" . '#################### [FILE # ' . $i . ' = \'' . $tmp_img_src . '\']' . "\n\n" . '==== [MODE] :: ' . $tmp_arr['mode'] . "\n" . '==== [LOG] :: ' . "\n" . $tmp_arr['log'] . "\n" . '==== [HEADERS] ::' . "\n" . $tmp_arr['headers'] . "\n" . '########' . "\n" . '==== [GUESS EXTENSION] :: ' . $tmp_where_we_guess . "\n\n" . '###################' . "\n\n\n\n", 'a');
                 }
                 //end if
                 //--
                 if ((string) $tmp_arr['result'] == '1' and (string) $tmp_arr['code'] == '200') {
                     //--
                     SmartFileSystem::write($the_dir . $tmp_img_cache . $tmp_img_ext, $tmp_arr['content']);
                     //-- if empty, it may be a file
                     if ((string) $tmp_img_ext == '' or (string) $tmp_img_ext == '.png' or (string) $tmp_img_ext == '.gif' or (string) $tmp_img_ext == '.jpg') {
                         $y_html_content = (string) @str_replace('src="' . $tmp_img_src . '"', 'src="' . $tmp_img_cache . $tmp_img_ext . '"', (string) $y_html_content);
                     } else {
                         // we want to avoid html code to be loaded as image by mistakes of http browser class or servers
                         $y_html_content = (string) @str_replace('src="' . $tmp_img_src . '"', 'src="' . $y_runtime_url . 'lib/framework/img/sign_warn.png"', (string) $y_html_content);
                     }
                     //end if else
                     //--
                 } else {
                     //--
                     $y_html_content = (string) @str_replace('src="' . $tmp_img_src . '"', 'src="' . $y_runtime_url . 'lib/framework/img/sign_error.png"', (string) $y_html_content);
                     //--
                 }
                 //end if
                 //--
             }
             //end if
             //--
             $chk_duplicates_arr[$tmp_img_src] = 'processed';
             //--
         }
         //end for
         //--
         $chk_duplicates_arr = array();
         unset($chk_duplicates_arr);
         $arr_imgs = array();
         unset($arr_imgs);
         //--
         SmartFileSystem::write($file, $orientation . "\n" . $y_html_content);
         //--
         if (is_file($file)) {
             //--
             ob_start();
             //--
             @passthru($htmldoc . ' ' . self::pdf_options($file));
             //--
             $pdfdata = ob_get_clean();
             //--
         } else {
             //--
             Smart::log_warning('ERROR: PDF Generator Failed to find the PDF Document: ' . $file . "\n" . $y_html_content);
             //--
         }
         //end if else
         //-- cleanup
         if ((string) SMART_FRAMEWORK_DEBUG_MODE != 'yes') {
             // if not debug, cleanup the dir
             if (is_dir($the_dir)) {
                 SmartFileSystem::dir_delete($the_dir);
             }
             //end if
         }
         //end if
         //--
     } else {
         //--
         Smart::log_notice('NOTICE: PDF Generator is INACTIVE ...');
         //--
     }
     //end if
     //--
     return (string) $pdfdata;
     //--
 }
    public static function test_redisserver()
    {
        global $configs;
        //--
        if (SMART_FRAMEWORK_TESTUNIT_ALLOW_REDIS_TESTS !== true) {
            return SmartComponents::operation_notice('Test Unit for Redis Server is DISABLED ...');
        }
        //end if
        //--
        //--
        if (SmartPersistentCache::isActive()) {
            //--
            $redis_big_content = self::pack_test_archive();
            // CREATE THE Test Archive (time not counted)
            //--
            $redis_test_key = 'redis-test-key_' . Smart::uuid_10_num() . '-' . Smart::uuid_36() . '-' . Smart::uuid_45();
            $redis_test_value = array('unicode-test' => '"Unicode78źź:ăĂîÎâÂșȘțȚşŞţŢグッド', 'big-key-test' => (string) $redis_big_content, 'random-key' => Smart::uuid_10_str() . '.' . Smart::random_number(1000, 9999));
            $redis_test_checkum = sha1(implode("\n", (array) $redis_test_value));
            $redis_test_arch_content = SmartUtils::cache_variable_archive($redis_test_value);
            $redis_test_arch_checksum = sha1($redis_test_arch_content);
            //--
            $tests = array();
            $tests[] = '##### Redis / TESTS (Persistent Cache) with a Variable Key-Size of ' . SmartUtils::pretty_print_bytes(strlen($redis_test_arch_content), 2) . ' : #####';
            //--
            $err = '';
            //--
            if ((string) $err == '') {
                $tests[] = 'Building a Test Archive file for Redis Tests (time not counted)';
                // archive was previous created, only test here
                if ((string) $redis_big_content == '') {
                    $err = 'Failed to build the Test Archive file for the Redis Test (see the error log for more details) ...';
                }
                //end if
            }
            //end if
            //--
            $time = microtime(true);
            $tests[] = '++ START Counter ...';
            //--
            if ((string) $err == '') {
                $tests[] = 'Building the Cache Archive';
                if ((string) $redis_test_arch_content == '') {
                    $err = 'Failed to build the Cache Variable(s) Archive file for the Redis Test (see the error log for more details) ...';
                }
                //end if
            }
            //end if
            //--
            if ((string) $err == '') {
                $tests[] = 'Set a short Redis Key (auto-expire in 3 seconds)';
                $redis_set_key = SmartPersistentCache::setKey('redis-server-tests', $redis_test_key, (string) $redis_test_value['unicode-test'], 3);
                if ($redis_set_key !== true) {
                    $err = 'Redis SetKey (short) returned a non-true result: ' . "\n" . $redis_test_key;
                }
                //end if
                if ((string) $err == '') {
                    $tests[] = 'Wait 5 seconds for Redis Key to expire, then check again if exists (time not counted)';
                    sleep(5);
                    // wait the Redis Key to Expire
                    $time = (double) $time + 5;
                    // ignore those 5 seconds (waiting time) to fix counter
                    $tests[] = '-- FIX Counter (substract the 5 seconds, waiting time) ...';
                    if (SmartPersistentCache::keyExists('redis-server-tests', $redis_test_key)) {
                        $err = 'Redis (short) Key does still exists (but should be expired after 5 seconds) and is not: ' . "\n" . $redis_test_key;
                    }
                    //end if
                }
                //end if
            }
            //end if
            //--
            if ((string) $err == '') {
                $tests[] = 'Set a long Redis Key (will not expire)';
                $redis_set_key = SmartPersistentCache::setKey('redis-server-tests', $redis_test_key, $redis_test_arch_content);
                if ($redis_set_key !== true) {
                    $err = 'Redis SetKey (long) returned a non-true result: ' . "\n" . $redis_test_key;
                }
                //end if
            }
            //end if
            //--
            if ((string) $err == '') {
                $tests[] = 'Check if Redis Key exists (after set)';
                if (!SmartPersistentCache::keyExists('redis-server-tests', $redis_test_key)) {
                    $err = 'Redis Key does not exists: ' . "\n" . $redis_test_key;
                }
                //end if
            }
            //end if
            //--
            if ((string) $err == '') {
                $tests[] = 'Get Redis Key';
                $redis_cached_value = SmartUtils::cache_variable_unarchive(SmartPersistentCache::getKey('redis-server-tests', $redis_test_key));
                if (Smart::array_size($redis_cached_value) > 0) {
                    $tests[] = 'Check if Redis Key is valid (array-keys)';
                    if ((string) $redis_cached_value['unicode-test'] != '' and (string) $redis_cached_value['big-key-test'] != '') {
                        $tests[] = 'Check if Redis Key is valid (checksum)';
                        if ((string) sha1(implode("\n", (array) $redis_cached_value)) == (string) $redis_test_checkum) {
                            if ($redis_test_value === $redis_cached_value) {
                                $tests[] = 'Unset Redis Key';
                                $redis_unset_key = SmartPersistentCache::unsetKey('redis-server-tests', $redis_test_key);
                                if ($redis_unset_key === true) {
                                    $tests[] = 'Check if Redis Key exists (after unset)';
                                    if (SmartPersistentCache::keyExists('redis-server-tests', $redis_test_key)) {
                                        $err = 'Redis Key does exists (after unset) and should not: ' . "\n" . $redis_test_key;
                                    } else {
                                        // OK
                                    }
                                    //end if
                                } else {
                                    $err = 'Redis UnSetKey returned a non-true result: ' . "\n" . $redis_test_key;
                                }
                                //end if else
                            } else {
                                $err = 'Redis Cached Value is broken: comparing stored value with original value failed on key: ' . "\n" . $redis_test_key;
                            }
                            //end if else
                        } else {
                            $err = 'Redis Cached Value is broken: checksum failed on key: ' . "\n" . $redis_test_key;
                        }
                        //end if else
                    } else {
                        $err = 'Redis Cached Value is broken: array-key is missing after Cache-Variable-Unarchive on key: ' . "\n" . $redis_test_key;
                    }
                    //end if
                } else {
                    $err = 'Redis Cached Value is broken: non-array value was returned after Cache-Variable-Unarchive on key: ' . "\n" . $redis_test_key;
                }
                //end if
            }
            //end if
            //--
            $title = 'SmartFramework Redis Server Tests: DONE ...';
            //--
            $time = 'TOTAL TIME (Except building the test archive) was: ' . (microtime(true) - $time);
            // substract the 3 seconds waiting time for Redis Key to expire
            //--
            $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 &nbsp;&nbsp;&nbsp; :: &nbsp;&nbsp;&nbsp; グッド ... パーフェクト</span>');
                $text_info = Smart::escape_js('<h2><span style="color:#83B953;">All</span> the SmartFramework Redis Server 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 ... &nbsp;&nbsp;&nbsp; :: &nbsp;&nbsp;&nbsp; エラーが発生しました ...</span>');
                $text_info = Smart::escape_js('<h2><span style="color:#FF5500;">A test FAILED</span> when testing Redis Server Operations.<span style="color:#FF5500;"><hr>FAILED Test Details</span>:</h2><br><span style="font-size:14px;"><pre>' . Smart::escape_html($err) . '</pre></span>');
            }
            //end if else
            //--
        } else {
            //--
            $title = 'SmartFramework Redis Server Tests - Redis Server was NOT SET ...';
            //--
            $img_sign = 'lib/core/img/sign_info.png';
            $img_check = 'lib/core/img/q_warning.png';
            $text_main = Smart::escape_js('<span style="color:#778899;">No Redis Server Tests performed ...</span>');
            $text_info = '<h2>The current configuration have not set the Redis Server ...</h2>';
            //--
        }
        //end if
        //--
        //--
        $html = <<<HTML
<h1>{$title}</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'Redis Server Test Suite for SmartFramework: PHP',
\t\t'725',
\t\t'480'
\t);
</script>
HTML;
        //--
        //--
        return $html;
        //--
    }
Пример #4
0
 /**
  * Get A UNIQUE (SAFE) ID for DB Tables / Schema
  *
  * @param ENUM 		$y_mode 					:: mode: uid10str | uid10num | uid10seq | uid36 | uid45
  * @param STRING 	$y_field_name 				:: the field name
  * @param STRING 	$y_table_name 				:: the table name
  * @param STRING 	$y_schema 					:: the schema
  * @param RESOURCE 	$y_connection 				:: pgsql connection
  * @return STRING 								:: the generated Unique ID
  *
  */
 public static function new_safe_id($y_mode, $y_id_field, $y_table_name, $y_schema = 'public', $y_connection = 'DEFAULT')
 {
     //==
     $y_connection = self::check_connection($y_connection, 'NEW-SAFE-ID');
     //==
     //--
     if (!self::validate_table_and_fields_names($y_id_field)) {
         self::error($y_connection, 'NEW-SAFE-ID', 'Get New Safe ID', 'Invalid Field Name', $y_id_field . ' / [Schema=' . $y_schema . ';Table=' . $y_table_name . ']');
         return '';
     }
     //end if
     if (!self::validate_table_and_fields_names($y_table_name)) {
         self::error($y_connection, 'NEW-SAFE-ID', 'Get New Safe ID', 'Invalid Table Name', $y_table_name);
         return '';
     }
     //end if
     if (!self::validate_table_and_fields_names($y_schema)) {
         self::error($y_connection, 'NEW-SAFE-ID', 'Get New Safe ID', 'Invalid Schema Name', $y_schema);
         return '';
     }
     //end if
     //--
     //--
     $use_safe_id_record = true;
     if (defined('SMART_SOFTWARE_DB_DISABLE_SAFE_IDS')) {
         if (SMART_SOFTWARE_DB_DISABLE_SAFE_IDS === true) {
             $use_safe_id_record = false;
         }
         //end if
     }
     //end if
     //--
     if ($use_safe_id_record === true) {
         //--
         if (self::check_if_table_exists('_safe_id_records', 'smart_runtime', $y_connection) !== 1) {
             if (self::check_if_schema_exists('smart_runtime', $y_connection) !== 1) {
                 self::write_data('CREATE SCHEMA "smart_runtime"', 'Initialize SafeID Schema', $y_connection);
             }
             //end if
             self::write_data((string) self::schema_safe_id_records_table(), 'Initialize SafeID Table', $y_connection);
         }
         //end if
         //--
         if ((int) Smart::random_number(0, 99) == 1) {
             // 1% chance to run it for cleanup records older than 24 hours
             self::write_data('DELETE FROM "smart_runtime"."_safe_id_records" WHERE ("date_time" < \'' . self::escape_str(date('Y-m-d H:i:s', @strtotime('-1 day')), 'no', $y_connection) . '\')', 'Safe ID Records Cleanup (OLDs)', $y_connection);
             // cleanup olds
         }
         //end if
         //--
     }
     //end if
     //--
     $tmp_result = 'NO-ID-INIT';
     //init (must be not empty)
     $counter = 0;
     $id_is_ok = false;
     //--
     while ($id_is_ok !== true) {
         // while we cannot find an unused ID
         //--
         $counter += 1;
         //--
         if ($counter > 7500) {
             // loop to max 7500
             self::error($y_connection, 'NEW-SAFE-ID', 'Get New Safe ID', 'Could Not Assign a Unique ID', '(timeout / 7500) ... try again !');
             return '';
         }
         //end if
         //--
         if ($counter % 500 == 0) {
             sleep(1);
         }
         //end if
         //--
         $new_id = 'NO-ID-ALGO';
         switch ((string) $y_mode) {
             case 'uid45':
                 $new_id = (string) Smart::uuid_45(SMART_FRAMEWORK_NETSERVER_ID . SmartUtils::get_server_current_url());
                 // will use the server ID.Host as Prefix to ensure it is true unique in a cluster
                 break;
             case 'uid36':
                 $new_id = (string) Smart::uuid_36(SMART_FRAMEWORK_NETSERVER_ID . SmartUtils::get_server_current_url());
                 // will use the server ID.Host as Prefix to ensure it is true unique in a cluster
                 break;
             case 'uid10seq':
                 if ($use_safe_id_record === true) {
                     // sequences are not safe without a second registry allocation table as the chance to generate the same ID in the same time moment is just 1 in 999
                     $new_id = (string) Smart::uuid_10_seq();
                 } else {
                     $new_id = (string) Smart::uuid_10_str();
                 }
                 //end if else
                 break;
             case 'uid10num':
                 $new_id = (string) Smart::uuid_10_num();
                 break;
             case 'uid10str':
             default:
                 $new_id = (string) Smart::uuid_10_str();
         }
         //end switch
         //--
         $result_arr = array();
         $chk_uniqueness = 'SELECT ' . self::escape_identifier($y_id_field, $y_connection) . ' FROM ' . self::escape_identifier($y_schema, $y_connection) . '.' . self::escape_identifier($y_table_name, $y_connection) . ' WHERE (' . self::escape_identifier($y_id_field, $y_connection) . ' = ' . self::escape_literal($new_id, 'no', $y_connection) . ') LIMIT 1 OFFSET 0';
         $result_arr = self::read_data($chk_uniqueness, 'Safe Check if NEW ID Exists into Table', $y_connection);
         $tmp_result = (string) trim((string) $result_arr[0]);
         $result_arr = array();
         //--
         if ((string) $tmp_result == '') {
             //--
             if ($use_safe_id_record === true) {
                 // with safety check against safe ID records table
                 //-- reserve this ID to bse sure will not be assigned to another instance
                 $uniqueness_mark = (string) $y_schema . '.' . $y_table_name . ':' . $y_id_field;
                 $write_res = self::write_igdata('INSERT INTO "smart_runtime"."_safe_id_records" ("id", "table_space", "date_time") ( SELECT \'' . self::escape_str($new_id, 'no', $y_connection) . '\', \'' . self::escape_str($uniqueness_mark, 'no', $y_connection) . '\', \'' . self::escape_str(date('Y-m-d H:i:s'), 'no', $y_connection) . '\' WHERE (NOT EXISTS ( SELECT 1 FROM "smart_runtime"."_safe_id_records" WHERE (("id" = \'' . self::escape_str($new_id, 'no', $y_connection) . '\') AND ("table_space" = \'' . self::escape_str($uniqueness_mark, 'no', $y_connection) . '\')) LIMIT 1 OFFSET 0 ) AND NOT EXISTS (' . $chk_uniqueness . ') ) )', 'Safe Record of NEW ID of Table into Zone Control', $y_connection);
                 //--
                 if ($write_res[1] === 1) {
                     $id_is_ok = true;
                 }
                 //end if
                 //--
             } else {
                 // default (not safe in very high load environments ...
                 //--
                 $id_is_ok = true;
                 //--
             }
             //end if else
             //--
         }
         //end if
         //--
     }
     //end while
     //--
     //--
     return (string) $new_id;
     //--
 }