phpdig_functions.php 9.4 KB

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