phpdig_functions.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. /*
  3. ----------------------------------------------------------------------------------
  4. PhpDig Version 1.8.x - See the config file for the full version number.
  5. This program is provided WITHOUT warranty under the GNU/GPL license.
  6. See the LICENSE file for more information about the GNU/GPL license.
  7. Contributors are listed in the CREDITS and CHANGELOG files in this package.
  8. Developer from inception to and including PhpDig v.1.6.2: Antoine Bajolet
  9. Developer from PhpDig v.1.6.3 to and including current version: Charter
  10. Copyright (C) 2001 - 2003, Antoine Bajolet, http://www.toiletoine.net/
  11. Copyright (C) 2003 - current, Charter, http://www.phpdig.net/
  12. Contributors hold Copyright (C) to their code submissions.
  13. Do NOT edit or remove this copyright or licence information upon redistribution.
  14. If you modify code and redistribute, you may ADD your copyright to this notice.
  15. ----------------------------------------------------------------------------------
  16. */
  17. define('CONFIG_CHECK','check'); // do not edit this line
  18. //-------------UTILS FUNCTIONS
  19. //=================================================
  20. // extract _POST or _GET variables from a list varname => vartype
  21. // Useful for error_reporting E_ALL too, init variables
  22. // usage in script : extract(phpdigHttpVars(array('foobar'=>'string')));
  23. function phpdigHttpVars($varray=array()) {
  24. $parse_orders = array('_POST','_GET','HTTP_POST_VARS','HTTP_GET_VARS');
  25. $httpvars = array();
  26. // extract the right array
  27. if (is_array($varray)) {
  28. foreach($parse_orders as $globname) {
  29. global $$globname;
  30. if (!count($httpvars) && isset($$globname) && is_array($$globname)) {
  31. $httpvars = $$globname;
  32. }
  33. }
  34. // extract or create requested vars
  35. foreach($varray as $varname => $vartype) {
  36. if (in_array($vartype,array('integer','bool','double','float','string','array')) ) {
  37. if (!isset($httpvars[$varname])) {
  38. if (!isset($GLOBALS[$varname])) {
  39. $httpvars[$varname] = false;
  40. }
  41. else {
  42. $httpvars[$varname] = $GLOBALS[$varname];
  43. }
  44. }
  45. settype($httpvars[$varname],$vartype);
  46. }
  47. }
  48. return $httpvars;
  49. }
  50. }
  51. //=================================================
  52. // timer for profiling
  53. class phpdigTimer {
  54. var $time = 0;
  55. var $mode = '';
  56. var $marks = array();
  57. var $template = '';
  58. function phpdigTimer($mode='html') {
  59. $this->time = $this->getTime();
  60. if ($mode == 'cli') {
  61. $this->template = "%s:\t%0.9f s. \n";
  62. }
  63. else {
  64. $this->template = "<tr><td class=\"greyForm\">%s</td><td class=\"greyForm\">%0.9f s. </td></tr>\n";
  65. }
  66. }
  67. function start($name) {
  68. if (!isset($this->marks[$name])) {
  69. $this->marks[$name]['time'] = $this->getTime();
  70. $this->marks[$name]['stat'] = 'r';
  71. }
  72. else if ($this->marks[$name]['stat'] == 's') {
  73. $this->marks[$name]['time'] = $this->getTime()-$this->marks[$name]['time'];
  74. $this->marks[$name]['stat'] = 'r';
  75. }
  76. }
  77. function stop($name) {
  78. if (isset($this->marks[$name]) && $this->marks[$name]['stat'] == 'r') {
  79. $this->marks[$name]['time'] = $this->getTime()-$this->marks[$name]['time'];
  80. }
  81. else {
  82. $this->marks[$name]['time'] = 0;
  83. }
  84. $this->marks[$name]['stat'] = 's';
  85. }
  86. function display() {
  87. if ($this->mode != 'cli') {
  88. print "<table class=\"borderCollapse\"><tr><td class=\"blueForm\">Mark</td><td class=\"blueForm\">Value</td></tr>\n";
  89. }
  90. foreach($this->marks as $name => $value) {
  91. printf($this->template,ucwords($name),$value['time']);
  92. }
  93. if ($this->mode != 'cli') {
  94. print "</table>\n";
  95. }
  96. }
  97. // increase precision with deltime
  98. function getTime() {
  99. return array_sum(explode(' ',microtime()))-$this->time;
  100. }
  101. }
  102. //-------------STRING FUNCTIONS
  103. //=================================================
  104. //returns a localized string
  105. function phpdigMsg($string='') {
  106. global $phpdig_mess;
  107. if (isset($phpdig_mess[$string])) {
  108. return nl2br($phpdig_mess[$string]);
  109. }
  110. else {
  111. return ucfirst($string);
  112. }
  113. }
  114. //=================================================
  115. //print a localized string
  116. function phpdigPrnMsg($string='') {
  117. global $phpdig_mess;
  118. if (isset($phpdig_mess[$string])) {
  119. print nl2br($phpdig_mess[$string]);
  120. }
  121. else {
  122. print ucfirst($string);
  123. }
  124. }
  125. //=================================================
  126. //load the common words in an array
  127. function phpdigComWords($file='')
  128. {
  129. $lines = @file($file);
  130. if (is_array($lines))
  131. {
  132. while (list($id,$word) = each($lines))
  133. $common[trim($word)] = 1;
  134. }
  135. else
  136. $common['aaaa'] = 1;
  137. return $common;
  138. }
  139. //=================================================
  140. //highlight a string part
  141. function phpdigHighlight($ereg='',$string='')
  142. {
  143. if ($ereg) {
  144. $string = @eregi_replace($ereg,"\\1<^#_>\\2</_#^>\\3",@eregi_replace($ereg,"\\1<^#_>\\2</_#^>\\3",$string));
  145. $string = str_replace("^#_","span class=\"phpdigHighlight\"",str_replace("_#^","span",$string));
  146. return $string;
  147. }
  148. else {
  149. return $result;
  150. }
  151. }
  152. //=================================================
  153. //replace all characters with an accent
  154. function phpdigStripAccents($chaine,$encoding=PHPDIG_ENCODING) {
  155. global $phpdigEncode;
  156. if (!isset($phpdigEncode[$encoding])) {
  157. $encoding = PHPDIG_ENCODING;
  158. }
  159. // exceptions
  160. if ($encoding == 'iso-8859-1') {
  161. $chaine = str_replace('Æ','ae',str_replace('æ','ae',$chaine));
  162. }
  163. return( strtr( $chaine,$phpdigEncode[$encoding]['str'],$phpdigEncode[$encoding]['tr']) );
  164. }
  165. //==========================================
  166. //Create a ereg for highlighting
  167. function phpdigPregQuotes($chaine,$encoding=PHPDIG_ENCODING) {
  168. global $phpdigEncode;
  169. if (!isset($phpdigEncode[$encoding])) {
  170. $encoding = PHPDIG_ENCODING;
  171. }
  172. $chaine = preg_quote(strtolower(phpdigStripAccents($chaine,$encoding)));
  173. return str_replace($phpdigEncode[$encoding]['char'],$phpdigEncode[$encoding]['ereg'],$chaine);
  174. }
  175. //=================================================
  176. // Create Useful arrays for different encodings
  177. function phpdigCreateSubstArrays($subststrings) {
  178. $phpdigEncode = array();
  179. global $phpdigEncode;
  180. foreach($subststrings as $encoding => $subststring) {
  181. $tempArray = explode(',',$subststring);
  182. if (!isset($phpdigEncode[$encoding])) {
  183. $phpdigEncode[$encoding] = array();
  184. }
  185. $phpdigEncode[$encoding]['str'] = '';
  186. $phpdigEncode[$encoding]['tr'] = '';
  187. $phpdigEncode[$encoding]['char'] = array();
  188. $phpdigEncode[$encoding]['ereg'] = array();
  189. foreach ($tempArray as $tempSubstitution) {
  190. $chrs = explode(':',$tempSubstitution);
  191. $phpdigEncode[$encoding]['char'][strtolower($chrs[0])] = strtolower($chrs[0]);
  192. settype($phpdigEncode[$encoding]['ereg'][strtolower($chrs[0])],'string');
  193. $phpdigEncode[$encoding]['ereg'][strtolower($chrs[0])] .= $chrs[0].$chrs[1];
  194. for($i=0; $i < strlen($chrs[1]); $i++) {
  195. $phpdigEncode[$encoding]['str'] .= $chrs[1][$i];
  196. $phpdigEncode[$encoding]['tr'] .= $chrs[0];
  197. }
  198. }
  199. foreach($phpdigEncode[$encoding]['ereg'] as $id => $ereg) {
  200. $phpdigEncode[$encoding]['ereg'][$id] = '['.$ereg.']';
  201. }
  202. }
  203. }
  204. //=================================================
  205. //epure a string from all non alnum words (words can contain &__&ßðþ character)
  206. function phpdigEpureText($text,$min_word_length=2,$encoding=PHPDIG_ENCODING) {
  207. global $phpdig_words_chars;
  208. $text = phpdigStripAccents(strtolower ($text));
  209. //no-latin upper to lowercase - now islandic
  210. switch (PHPDIG_ENCODING) {
  211. case 'iso-8859-1':
  212. $text = strtr( $text,'ÐÞ','ðþ');
  213. break;
  214. }
  215. // RH ereg_replace('[^'.$phpdig_words_chars[$encoding].' \'._~@#$:&%/;,=-]+',' ',$text);
  216. $text = ereg_replace('[^'.$phpdig_words_chars[$encoding].' \'._~@#$&%/=-]+',' ',$text);
  217. // RH ereg_replace('(['.$phpdig_words_chars[$encoding].'])[\'._~@#$:&%/;,=-]+($|[[:space:]]$|[[:space:]]['.$phpdig_words_chars[$encoding].'])','\1 \2',$text);
  218. $text = ereg_replace('(['.$phpdig_words_chars[$encoding].'])[\'._~@#$&%/=-]+($|[[:space:]]$|[[:space:]]['.$phpdig_words_chars[$encoding].'])','\1 \2',$text);
  219. // the next two repeated lines needed
  220. if ($min_word_length >= 1) {
  221. $text = ereg_replace('[[:space:]][^ ]{1,'.$min_word_length.'}[[:space:]]',' ',' '.$text.' ');
  222. $text = ereg_replace('[[:space:]][^ ]{1,'.$min_word_length.'}[[:space:]]',' ',' '.$text.' ');
  223. }
  224. $text = ereg_replace('\.{2,}',' ',$text);
  225. $text = ereg_replace('^[[:space:]]*\.+',' ',$text);
  226. return trim(ereg_replace("[[:space:]]+"," ",$text));
  227. }
  228. //-------------SQL FUNCTIONS
  229. //=================================================
  230. //insert an entry in logs
  231. function phpdigAddLog ($id_connect,$option='start',$includes=array(),$excludes=array(),$num_results=0,$time=0) {
  232. if (!is_array($excludes)) {
  233. $excludes = array();
  234. }
  235. sort($excludes);
  236. if (!is_array($includes)) {
  237. $includes = array();
  238. }
  239. sort($includes);
  240. $query = 'INSERT INTO '.PHPDIG_DB_PREFIX.'logs (l_num,l_mode,l_ts,l_includes,l_excludes,l_time) '
  241. .'VALUES ('.$num_results.',\''.substr($option,0,1).'\',NOW(),'
  242. .'\''.implode(' ',$includes).'\',\''.implode(' ',$excludes).'\','.(double)$time.')';
  243. mysql_query($query,$id_connect);
  244. return mysql_insert_id($id_connect);
  245. }
  246. ?>