working version
[Anonymous-Twitter-Board.git] / class / additional-functions.php
blob390de578918d7effec31a406a7c34389038382de
1 <?php
2 class OAuthRandom{
3 public static function randomAlphaNumet($len){
4 $rand_string = "";
5 $opt_str = "1234567890qwertyuiopasdfghjklzxcvbnmZXCVBNMASDFGHJKLQWERTYUIOP";
6 $options = str_split($opt_str);
7 $max = mb_strlen($opt_str) - 1;
8 for($char = 0 ; $char < $len ; $char++){
9 $rand_string .= $options[rand(0, $max)];
12 return str_replace("/", "5", str_replace("=", "2", $rand_string));
16 class StandardFunctions{
17 public static function getIniFile($path){
18 $path_file = fopen($path,"r");
19 $return_array = array();
20 while(!feof($path_file)){
21 $line = fgets($path_file);
22 $key = trim(substr($line,0,strpos($line, "=")));
23 //eat last character
24 $value = trim(substr($line, strpos($line, "=")+1));
26 $return_array[$key] = $value;
28 fclose($path_file);
29 return $return_array;
32 public static function testBlock($val){
33 echo "<pre>";
34 var_dump($val);
35 echo "</pre>";
39 public static function recursiveEchoJson($json, $indents){
40 echo "<pre>";
41 foreach($json as $key => $attribute){
42 if(is_array ($attribute)){
43 StandardFunctions::makeIndents($indents);
44 echo "$key {\n";
46 StandardFunctions::recursiveEchoJson($attribute, ++$indents);
48 StandardFunctions::makeIndents(--$indents);
49 echo "}\n";
51 else{
52 StandardFunctions::makeIndents($indents);
53 echo "$key = $attribute \n";
56 echo "</pre>";
57 if($indents == 1) echo "<hr/>";
61 public static function makeIndents($indent_count){
62 for ($i = 0; $i < $indent_count ; $i++){ echo "\t"; }
66 //https://stackoverflow.com/questions/5695145/how-to-read-and-write-to-an-ini-file-with-php
67 public static function write_php_ini($array, $file)
69 $res = array();
70 foreach($array as $key => $val)
72 if(is_array($val))
74 $res[] = "[$key]";
75 foreach($val as $skey => $sval) $res[] = "$skey = ".(is_numeric($sval) ? $sval : '"'.$sval.'"');
77 else $res[] = "$key = ".(is_numeric($val) ? $val : '"'.$val.'"');
79 StandardFunctions::safefilerewrite($file, implode("\r\n", $res));
82 public static function safefilerewrite($fileName, $dataToSave)
83 { if ($fp = fopen($fileName, 'w'))
85 $startTime = microtime(TRUE);
87 { $canWrite = flock($fp, LOCK_EX);
88 // If lock not obtained sleep for 0 - 100 milliseconds, to avoid collision and CPU load
89 if(!$canWrite) usleep(round(rand(0, 100)*1000));
90 } while ((!$canWrite)and((microtime(TRUE)-$startTime) < 5));
92 //file was locked so now we can store information
93 if ($canWrite)
94 { fwrite($fp, $dataToSave);
95 flock($fp, LOCK_UN);
97 fclose($fp);