File indexing completed on 2024-12-15 03:45:06
0001 <?php 0002 /* 0003 SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org> 0004 0005 SPDX-License-Identifier: MIT 0006 */ 0007 0008 require_once('restexception.php'); 0009 0010 class Utils { 0011 0012 public static function isValidIdentifier($str) 0013 { 0014 if (!is_string($str) || strlen($str) <= 0) 0015 return false; 0016 if (!ctype_alpha($str[0]) && $str[0] != '_') // must start with a letter or underscore 0017 return false; 0018 for ($i = 0; $i < strlen($str); $i++) { 0019 if (ctype_space($str[$i]) || ctype_cntrl($str[$i])) 0020 return false; 0021 } 0022 return true; 0023 } 0024 0025 public static function normalizeString($input) 0026 { 0027 $result = ''; 0028 for ($i = 0; $i < strlen($input); $i++) { 0029 $c = $input[$i]; 0030 if (!ctype_alnum($c)) 0031 $c = '_'; 0032 $result .= $c; 0033 } 0034 return $result; 0035 } 0036 0037 /** Driver-dependen default SQL string type. */ 0038 public static function sqlStringType($driver) 0039 { 0040 switch ($driver) { 0041 case 'sqlite': 0042 case 'pgsql': 0043 return 'VARCHAR'; 0044 case 'mysql': 0045 return 'VARCHAR(255)'; 0046 } 0047 throw new RESTException('Unsupported database driver.', 500); 0048 } 0049 0050 /** Driver-dependent column declaration for a numeric auto-increment primary key. */ 0051 public static function primaryKeyColumnDeclaration($driver, $columnName) 0052 { 0053 switch ($driver) { 0054 case 'sqlite': 0055 return $columnName . ' INTEGER PRIMARY KEY AUTOINCREMENT'; 0056 case 'mysql': 0057 return $columnName . ' INTEGER PRIMARY KEY AUTO_INCREMENT'; 0058 case 'pgsql': 0059 return $columnName . ' SERIAL PRIMARY KEY'; 0060 } 0061 throw new RESTException('Unsupported database driver.', 500); 0062 } 0063 0064 public static function httpError($responseCode, $message) 0065 { 0066 error_log('UserFeedback ERROR: ' . $message . ' (' . $responseCode . ')'); 0067 http_response_code($responseCode); 0068 header('Content-Type: text/plain'); 0069 print($message); 0070 exit(1); 0071 } 0072 0073 } 0074 0075 ?>