<?php
/**
* COPYRIGHT © 2022 JOHN PEARCEY
* All rights reserved
*/
const BASE64_DIGITS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz$_';
if (version_compare( PHP_VERSION, '8', '<') ){
include_once __DIR__ . "/common-pre-v8.php";
}
function getStackTrace(){
try{
throw new Exception;
}catch( Exception $e ){
return $e->getTraceAsString();
}
}
/**
* Format a 2D array into a 1D array of strings suitable for output in a
* readable style.
*
* $fxd_widths
* An array of widths to set which overrides the calculated width values
*
*/
function format_array_2d( $arr, $fxd_widths=array() ){
$arr_col_mxw = null;
$tot_field_cnt = count($arr[0]);
if(count($fxd_widths)>=$tot_field_cnt ){
$arr_col_mxw = $fxd_widths;
}else{
$st_col = count($fxd_widths);
$arr_col_mxw = array();
$i=0;
for($i=0; $i<$tot_field_cnt; $i++){
if($i<$st_col){
array_push( $arr_col_mxw, $fxd_widths[$i]);
}else{
array_push( $arr_col_mxw, 0 );
}
}
//get the remaining field maximum widths for all rows
foreach($arr as $row){
for($i=$st_col; $i<count($row); $i++){
$field = $row[$i];
if( strlen($field)>$arr_col_mxw[$i]){
$arr_col_mxw[$i] = strlen($field);
}
}
}
}
//print_r($arr_col_mxw);
$arr_rtn = array();
foreach($arr as $row){
$str_row = '|';
$i=-1;
foreach($row as $field){
$i++;
if($arr_col_mxw[$i]==0) continue;
if( strlen($field) > $arr_col_mxw[$i]){
$str_row .= substr($field, 0, $arr_col_mxw[$i] ) ."| ";
}else{
$str_row .= str_pad($field, $arr_col_mxw[$i]) ."| ";
}
}
array_push($arr_rtn, $str_row);
}
return $arr_rtn;
}
/**
* e.g. split
* ../db/stanhopetest/img/img_52548
* into
* [ ../db/stanhopetest/img/ , img_52548 ]
*
*/
function split_file_path( $ffn ){
$fn = basename( $ffn ); // img_52548
//$fp = substr( $ffn, 0, strlen($ffn) - strlen($fn) );
$fp = dirname( $ffn ) . '/';
return [ $fp, $fn ];
}
/**
* Returns true if it is possible to create this directory on the file system.
* Includes the possibility of multiple directories.
*/
function is_creatable_dir( $dirs ){
$dir = $dirs;
while(!file_exists($dir)){
$dir = dirname( $dir );
}
return is_writable($dir);
}
/**
* Function to make directories and actually SET the permissions
* https://www.php.net/manual/en/function.chmod.php
*
* $perms is an octal number
* e.g. mkdirs( $dir, 02770 ); //770 and g+s
*/
function mkdirs( $dirs, $perms ){
$dir = $dirs;
while(!file_exists($dir)){
$dir = dirname( $dir );
}
$rootDir = $dir;
if(!file_exists($dirs)) mkdir( $dirs, 0750, true ); //perms not always set
$dir = $dirs;
while( $dir!=$rootDir) {
chmod( $dir, $perms );
$dir = dirname( $dir );
}
}
/**
* Return the contents of a url as a string using the curl functions.
*/
function getContentsFromUrl( $url ){
$ch = curl_init( $url );
//$fp = fopen("example_homepage.txt", "w");
$fp = tmpfile();
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
$c_err = curl_error($ch);
curl_close($ch);
if($c_err) {
error_log( $c_err );
fclose($fp);
return;
}
$f_path = stream_get_meta_data($fp)['uri'];
$rtn = file_get_contents( $f_path );
fclose($fp);
return $rtn;
}
function params_get_date( $key ){
return strtotime( params_get($key) );
}
function generateNonce( $digitCount = 12 ){
// e.g. 422466a05f24b09c978fa1f6
return bin2hex( random_bytes($digitCount) );
}
/**
* Generate a randon username starting with a letter and containing only letters and numbers.
*/
function generateRndUserName( $digitCount = 10 ){
$l1 = generateRndStrUsing( 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', 1 );
return $l1 . generateRndStrUsing( BASE64_DIGITS, $digitCount-1 );
}
/**
* Generate a randon password with letters and numbers and some funny stuff.
*/
function generateRndPwd( $digitCount = 10 ){
return generateRndStrUsing( '0123456789_!"£$%^&*()_-|\?/<>#~ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', $digitCount );
}
/**
* Generate a randon string of asc char. Default consisting of only [0-9][a-z][A-Z]
*/
function generateRndStrUsing( $using=BASE64_DIGITS, $digitCount = 8 ){
$str = '';
$maxlen = strlen($using);
for($i=0; $i<$digitCount; $i++){
$iv = random_int(0, $maxlen-1);
$str .= mb_substr( $using, $iv, 1); //use mb_substr because £ is not and asc char!
}
return $str;
}
/**
* Remove all html from the given string returning just the text content.
*/
function removeHtml( $str ){
//txt = txt.replace(/<div>/gi, '\n');
return preg_replace( '/<\/?[^>]+>/i', '', $str );
}
function getHtmlOkAsString( $data ){
$data = json_encode( $data );
return "[\"OK\", $data ]";
}
function sendHtmlOk( $data="" ){
echo getHtmlOkAsString( $data );
}
/**
* An effort to rectify my previously ill-thought out return encoding. You do not
* need to encode the data before calling this method. Any arrays will be json encoded.
*/
function sendHtmlOk_WithData( $data ){
$data = json_encode( $data );
echo "[\"OK\", $data ]";
}
function getHtmlErrorAsString( $strErrDesc = "--blank--" ){
$data = json_encode( $strErrDesc );
return "[\"Error\", $data]";
}
function sendHtmlError( $strErrDesc = "--blank--" ){
$rtn = getHtmlErrorAsString( $strErrDesc );
// mylog( $rtn ); //not sure where this is located???
echo $rtn;
}
/**
*
*/
function getAttForPath( $name, $prefPath, $path){
return "$name = \"$prefPath/$path\"";
}
?>