PageRenderTime 53ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/class.xsystem.inc

https://github.com/jcplat/console-seolan
PHP | 235 lines | 225 code | 6 blank | 4 comment | 3 complexity | 73f7b6cd440e58ae87dddfd4daf40e18 MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-2.1, GPL-3.0, Apache-2.0, BSD-3-Clause
  1. <?php
  2. class XSystem {
  3. static $tables = NULL;
  4. static $cfields = array();
  5. /// Rend vrai si la table $table existe dans la base de données
  6. public static function tableExists($table, $refresh=false) {
  7. if(empty(self::$tables) || $refresh)
  8. self::$tables = getMetaTables();
  9. return in_array($table, self::$tables);
  10. }
  11. /// Rend vrai si le champ $field de la table $table existe dans la base de données
  12. public static function fieldExists($table, $field) {
  13. if(empty(self::$cfields[$table]))
  14. self::$cfields[$table] = selectQueryGetAll('SHOW COLUMNS FROM '.$table.'/*1*/');
  15. return in_array($field, self::$cfields[$table]);
  16. }
  17. public static function clearCache() {
  18. self::$tables = NULL;
  19. self::$cfields = array();
  20. }
  21. public static function getmicrotime() {
  22. list($usec, $sec) = explode(' ',microtime());
  23. return ((float)$usec + (float)$sec);
  24. }
  25. public static function getusertime() {
  26. $t = posix_times();
  27. return $t['utime'];
  28. }
  29. // separation d'une ligne en token separes par le $delim avec un caractere delimiteur
  30. //
  31. public static function csv2array ($str, $delim=';', $qual="\"") {
  32. if(is_array($str)) {
  33. for($i=0;$i<count($str);$i++) {
  34. $ret[]=csv2array($str[$i],$delim,$qual);
  35. }
  36. return $ret;
  37. }
  38. // Taille de la ligne
  39. $width=strlen($str);
  40. // Enclosed
  41. $enclosed=false;
  42. // Item
  43. $item="";
  44. // Bucle
  45. for ( $i=0; $i<$width; $i++) {
  46. if ( $str[$i] == $delim && !$enclosed ) {
  47. $retval[] = $item;
  48. $item="";
  49. } else if ( $str[$i] == $qual && ( $i<$width && $str[$i+1] == $qual ) ) {
  50. $item .= $qual;
  51. $i++;
  52. } else if ( $str[$i] == $qual ) {
  53. $enclosed = !$enclosed;
  54. } else {
  55. $item .= $str[$i];
  56. }
  57. }
  58. // We give back the matrix
  59. $retval[]=$item;
  60. return $retval;
  61. }
  62. public static function uptime() {
  63. if(!defined('TZR_UPTIME_PATH')) return array('procs.r'=>"0");
  64. $r = explode(" ",file_get_contents(TZR_UPTIME_PATH),2);
  65. return array('procs.r'=>$r[0]);
  66. }
  67. public static function def($const, $val) {
  68. if(!defined($const)) define($const, $val);
  69. }
  70. // creation d'une archive a partir d'une liste de fichiers
  71. // la liste de fichier est au format "téléchargé
  72. //
  73. public static function untarfiles($filename) {
  74. $dir=TZR_TMP_DIR.uniqid('tzr');
  75. @mkdir($dir);
  76. @copy($filename, "$dir/ar.tgz");
  77. @system("cd $dir ; tar xzf ar.tgz");
  78. @unlink("$dir/ar.tgz");
  79. $catalog=@file("$dir/tzr-catalog.txt");
  80. if(empty($catalog)) $catalog=array();
  81. $files=array();
  82. foreach($catalog as $i => $l) {
  83. list($v1,$v2,$v3)=explode(";",rtrim($l));
  84. $file['tmp_name']="$dir/$v1";
  85. $file['type']=$v2;
  86. $file['name']=$v3;
  87. $files[$v1]=$file;
  88. }
  89. $files['tzr-dir']=$dir;
  90. return $files;
  91. }
  92. public static function tarfiles(&$ar, $fmt="tgz") {
  93. // construction de la liste des fichiers
  94. $dir=TZR_TMP_DIR.uniqid('tzr');
  95. mkdir($dir, 0750, true);
  96. $catalog="";
  97. foreach($ar as $l => $b) {
  98. $cat.="$l;".$b['type'].";".$b['name']."\n";
  99. //move_uploaded_file($b['tmp_name'], $dir."/".$l);
  100. if(!copy($b['tmp_name'], $dir."/".$l)){
  101. XLogs::critical('XSystem::tarfiles',"moving file fail : ".$dir."/".$l);
  102. }
  103. }
  104. file_put_contents("$dir/tzr-catalog.txt",$cat);
  105. $files=scandir($dir);
  106. $un = TZR_TMP_DIR.uniqid('tzr');
  107. $un .= '.'.$fmt;
  108. unset($files[0]);
  109. unset($files[1]);
  110. $list=implode(' ',$files);
  111. if($fmt=="tgz") {
  112. system("cd $dir ; tar -czf $un $list");
  113. XDir::unlink($dir,false,true);
  114. }
  115. if($fmt=="zip") {
  116. system("cd $dir ; zip $un $list");
  117. XDir::unlink($dir,false,true);
  118. }
  119. return $un;
  120. }
  121. /****f* XSystem/getRemote
  122. * NAME
  123. * XSystem::getRemote - recupération des données depuis un repertoire temporaire
  124. * DESCRIPTION
  125. * Recupération des données depuis le répertoire passé en
  126. * paramètre, qui peut être un répertoire distant. Si le transfert
  127. * s'est bien passé, les fichiers sur le répertoire d'origine sont
  128. * supprimés au fur etaà mesure des transferts.
  129. * Le nom du répertoire peut être une url.
  130. * INPUTS
  131. * $dirpath - répertoire
  132. ****/
  133. function getRemote($dirpath) {
  134. $tmpdir = TZR_TMP_DIR."download".uniqid();
  135. @mkdir($tmpdir);
  136. $dir = @opendir($dirpath);
  137. if($dir) {
  138. while (($file = readdir($dir)) !== false) {
  139. $filename = "$dirpath/$file";
  140. $newfilename = $tmpdir."/".$file;
  141. $fo = file_get_contents($filename);
  142. file_put_contents($newfilename,$fo);
  143. if(file_exists($newfilename)) {
  144. unlink($filename);
  145. }
  146. }
  147. closedir($dir);
  148. }
  149. return $tmpdir;
  150. }
  151. function xml2html($xml){
  152. $xml=str_replace('<?xml version="1.0" encoding="utf-8" ?>','',$xml);
  153. $xml=str_replace('<tzrdata v="2">','',$xml);
  154. $xml=str_replace('</tzrdata>','',$xml);
  155. return $xml;
  156. }
  157. function array2xml($ar, &$xml) {
  158. $init=false;
  159. if(empty($xml)) {
  160. $init=true;
  161. $xml="<?xml version=\"1.0\" encoding=\"utf-8\" ?><tzrdata v=\"2\">";
  162. }
  163. if(is_array($ar)) {
  164. $xml.="<table>";
  165. foreach($ar as $idx => $val) {
  166. $xml.="<tr><th>".htmlspecialchars($idx,ENT_COMPAT,TZR_INTERNAL_CHARSET)."</th><td>";
  167. if(is_array($val)) XSystem::array2xml($val,$xml);
  168. else $xml.=htmlspecialchars($val,ENT_COMPAT,TZR_INTERNAL_CHARSET);
  169. $xml.="</td></tr>";
  170. }
  171. $xml.="</table>";
  172. }
  173. if($init) $xml.="</tzrdata>";
  174. }
  175. function xml2arrayitem($idx,$val) {
  176. $v1=$val->children();
  177. if(!empty($v1[0])) {
  178. $val = XSystem::xml2array($val);
  179. } else { $val=(string)$val;}
  180. return array((string)$idx, $val);
  181. }
  182. function xml2array($xml){
  183. if(!is_object($xml)) {
  184. $oxml=simplexml_load_string($xml);
  185. $xml=&$oxml;
  186. if($xml["v"]>2) XLogs::critical("XSystem::xml2array","bad xml array release ".var_export($xml,true));
  187. }
  188. $ar=array();
  189. if($xml->table) {
  190. $e=$xml->table->children();
  191. foreach($e as $idx=>$val) {
  192. list($i, $v)=XSystem::xml2arrayitem($val->th, $val->td);
  193. $ar[$i]=$v;
  194. }
  195. }
  196. return $ar;
  197. }
  198. // fonction de copie capable de prendre en compte de tres gros fichiers : permet
  199. // de contourner les limitations de php qui charge tout le fichier
  200. // en memoire lors d'une copie de fichier.
  201. //
  202. function copy($src, $dst) {
  203. if(!file_exists($src)) return;
  204. if(filesize($src)<10*1024*1024) {
  205. return copy($src,$dst);
  206. }
  207. $fh = fopen($src, "r");
  208. $fh22 = fopen($dst, 'w+');
  209. if(!$fh || !$fh22) return;
  210. $blocksize=1024*1024;// 10Ko
  211. while (!feof($fh)) {
  212. $contents = fread($fh, $blocksize);
  213. fwrite($fh22, $contents, $blocksize);
  214. }
  215. fclose($fh);
  216. fclose($fh22);
  217. }
  218. }
  219. ?>