escape_string($str); } else if ($db_link) { return mysql_real_escape_string($str, $db_link); } else { return mysql_real_escape_string($str); } } function mysqli_legacy_connect($server, $user, $password, $persistent=false) { $persistent = false; // detect if we're connecting to a socket if (stripos($server, ".sock") !== false) { return mysqli_connect(($persistent?"p:":"")."localhost", $user, $password, null, null, preg_replace("/^:/", "", $server)); } else { return mysqli_connect(($persistent?"p:":"").$server, $user, $password); } } /** Converted to MySQLi 2016-06-29 **/ function GetTableDescription($table, $db_link=null) { $table = GenericEscape($table, $db_link); // get table description $Description = null; if ($db_link && IsMySQLi($db_link)) { $DescribeResponse = $db_link->query("DESCRIBE ".$table); if ($DescribeResponse && $DescribeResponse->num_rows>0) { while ($DescribeObj = $DescribeResponse->fetch_object()) { $Description[$DescribeObj->Field] = $DescribeObj; } return $Description; } else { echo $db_link->error; return false; } } else { if ($db_link) { $DescribeResponse = mysql_query("DESCRIBE $table", $db_link); } else { $DescribeResponse = mysql_query("DESCRIBE $table"); } if ($DescribeResponse && mysql_num_rows($DescribeResponse)>0) { while ($DescribeObj = mysql_fetch_object($DescribeResponse)) { $Description[$DescribeObj->Field] = $DescribeObj; } return $Description; } else { echo mysql_error(); return false; } } } /** Converted to MySQLi 2016-06-29 (untested) **/ if (!function_exists("GenericUpdate")) { function GenericUpdate($data, $table, $Description=null, $db_link=null, $where=NULL) { if (!$Description) $Description = GetTableDescription($table, $db_link); $Updates = Array(); foreach ($data as $key => $val) { if (isset($Description[$key])) { if (($val===null) && ($Description[$key]->Null=="YES")) { $Updates[]= $key."=NULL"; } else if ($val!==null) { $tmp = ""; if (strpos($Description[$key]->Type, "enum")!==FALSE) { $tmp = $key."='".GenericEscape($val, $db_link)."'"; } else if (strpos($Description[$key]->Type, "int")!==FALSE) { $tmp = $key."=".intval($val); } else if (strpos($Description[$key]->Type, "varchar")!==FALSE) { $tmp = $key."='".GenericEscape($val, $db_link)."'"; } else if (strpos($Description[$key]->Type, "text")!==FALSE) { $tmp = $key."='".GenericEscape($val, $db_link)."'"; } else if (strpos($Description[$key]->Type, "datetime")!==FALSE) { $ts = strtotime($val); if (! ($ts===false || $ts===-1)) { $tmp = $key."='".date("Y-m-d H:i:s", $ts)."'"; } } else if (strpos($Description[$key]->Type, "date")!==FALSE) { $ts = strtotime($val); if (! ($ts===false || $ts===-1)) { $tmp = $key."='".date("Y-m-d", $ts)."'"; } } else if (strpos($Description[$key]->Type, "time")!==FALSE) { $ts = strtotime($val); if (! ($ts===false || $ts===-1)) { $fields[] = $key; $tmp = $key."='".date("H:i:s", $ts)."'"; } } if ($where==null && $Description[$key]->Key=="PRI") { $where = $tmp; } else { $Updates[] = $tmp; } } } } if (count($Updates)>0) { $q = "UPDATE ".$table." SET ".implode(", ",$Updates)." WHERE ".$where; // $q = mb_convert_encoding($q, 'UTF-8', 'UTF-8'); if (IsMySQLi($db_link)) return $db_link->query($q); else if ($db_link) return mysql_query($q, $db_link); else return mysql_query($q); } else return false; } } /** Converted to MySQLi 2016-06-29 (untested) **/ if (!function_exists("GenericInsert")) { function GenericInsert($data, $table, $Description=null, $db_link=null) { // data should be an associative array of $data["field"] = value; $table = GenericEscape($table, $db_link); // get table description (if not supplied) if (!$Description) $Description = GetTableDescription($table, $db_link); // get our fields & values $fields = null; $values = null; foreach ($data as $key => $val) { if ($val === NULL) { $fields[] = $key; $values[] = "NULL"; } else if (isset($Description[$key])) { if (strpos($Description[$key]->Type, "enum")!==FALSE) { $fields[] = $key; $values[] = "'".GenericEscape($val, $db_link)."'"; } else if (strpos($Description[$key]->Type, "int")!==FALSE) { $fields[] = $key; $values[] = intval($val); } else if (strpos($Description[$key]->Type, "varchar")!==FALSE) { $fields[] = $key; $values[] = "'".GenericEscape($val, $db_link)."'"; } else if (strpos($Description[$key]->Type, "text")!==FALSE) { $fields[] = $key; $values[] = "'".GenericEscape($val, $db_link)."'"; } else if (strpos($Description[$key]->Type, "datetime")!==FALSE) { $ts = strtotime($val); if (! ($ts===false || $ts===-1)) { $fields[] = $key; $values[] = "'".date("Y-m-d H:i:s", $ts)."'"; } } else if (strpos($Description[$key]->Type, "date")!==FALSE) { $ts = strtotime($val); if (! ($ts===false || $ts===-1)) { $fields[] = $key; $values[] = "'".date("Y-m-d", $ts)."'"; } } else if (strpos($Description[$key]->Type, "time")!==FALSE) { $ts = strtotime($val); if (! ($ts===false || $ts===-1)) { $fields[] = $key; $values[] = "'".date("H:i:s", $ts)."'"; } } } } // attempt to insert $fields = implode(", ", $fields); $values = implode(", ", $values); $q = "INSERT INTO $table ($fields) VALUES ($values)"; if (IsMySQLi($db_link)) { return $db_link->query($q); } else if ($db_link) { return mysql_query($q, $db_link); } else { return mysql_query($q); } } } function MassInsert($data, $table, $db_link=null) { $description = GetTableDescription($table, $db_link); if ($description) { foreach ($data as $d) { GenericInsert($d, $table, $description, $db_link); } } else { return false; } } function ObjectToArray($data) { if(is_array($data) || is_object($data)) { $result = array(); foreach($data as $key => $value) { $result[$key] = ObjectToArray($value); } return $result; } return $data; } /** * Indents a flat JSON string to make it more human-readable. * * @param string $json The original JSON string to process. * * @return string Indented version of the original JSON string. */ function json_indent($json) { $result = ''; $pos = 0; $strLen = strlen($json); $indentStr = ' '; $newLine = "\n"; $prevChar = ''; $outOfQuotes = true; for ($i=0; $i<=$strLen; $i++) { // Grab the next character in the string. $char = substr($json, $i, 1); // Are we inside a quoted string? if ($char == '"' && $prevChar != '\\') { $outOfQuotes = !$outOfQuotes; // If this character is the end of an element, // output a new line and indent the next line. } else if(($char == '}' || $char == ']') && $outOfQuotes) { $result .= $newLine; $pos --; for ($j=0; $j<$pos; $j++) { $result .= $indentStr; } } // Add the character to the result string. $result .= $char; // If the last character was the beginning of an element, // output a new line and indent the next line. if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) { $result .= $newLine; if ($char == '{' || $char == '[') { $pos ++; } for ($j = 0; $j < $pos; $j++) { $result .= $indentStr; } } $prevChar = $char; } return $result; } ?>