source: branches/rsr.v5.1/web/app/data/modules/data_utils.php @ 1

Last change on this file since 1 was 1, checked in by dj3c1t, 12 years ago

import initial

File size: 5.7 KB
Line 
1<?php
2
3  class data_utils extends data
4  {
5
6    # ----------------------------------------------------------------------------------------
7    #                                              fonction de modification de champ generique
8    #
9
10    function get($table, $champ, $where_champ, $where_valeur)
11    { $value = "";
12      try
13      { $query =
14         "SELECT `".$champ."` as res"
15        ." FROM #--".$table
16        ." WHERE ".$where_champ."=".$this->eq($where_valeur)
17        ." LIMIT 1";
18        $rst = $this->sql->query($query);
19        if($v_rst = $this->sql->fetch_assoc($rst)) $value = $v_rst["res"];
20      }
21      catch(Exception $_e) { $value = false; }
22      return $value;
23    }
24
25    function set($table, $champ, $valeur, $where_champ, $where_valeur)
26    { try
27      { $query =
28         "UPDATE #--".$table
29        ." SET `".$champ."`=".$this->eq($valeur)
30        ." WHERE `".$where_champ."`=".$this->eq($where_valeur)
31        ." LIMIT 1";
32        $this->sql->query($query);
33      }
34      catch(Exception $_e) { return false; }
35      return true;
36    }
37
38    # ----------------------------------------------------------------------------------------
39    #
40    #   ci-dessous
41    #
42    #    - utils : des fonctions courantes dans le traitement des données
43    #      (dates, echapement des simples quotes...)
44    #
45    #    - fonctions de base : des squelettes de fonctions pour l'accÚs en base
46    #      (liste, ajout, modification...)
47    #
48    # ----------------------------------------------------------------------------------------
49
50    # ----------------------------------------------------------------------------------------
51    #                                                                                    utils
52    #
53
54    function html($content)
55    { return str_replace
56      ( array("&gt;", "&lt;", "&quot;"),
57        array(">", "<", "\""),
58        $content
59      );
60    }
61
62    function encode_email($email)
63    { return
64       "<a href=\"".$this->ascii_entities("mailto:".$email)."\">"
65      .$this->ascii_entities($email)
66      ."</a>";
67    }
68
69    function ascii_entities($content)
70    { $entities = "";
71      $n = 0;
72      while($n < strlen($content))
73      { $entities .= "&#".ord($content[$n]).";";
74        $n++;
75      }
76      return $entities;
77    }
78
79    function resume($content, $max)
80    { $resume = "";
81      $v_content = explode(" ", $content);
82      for($n = 0; $n < $max; $n++) $resume .= $v_content[$n]." ";
83      return $resume;
84    }
85
86    function eq($content) { return (isset($content) ? "'".str_replace("'", "\'", $content)."'" : "NULL"); }
87
88    function frenchdate($i)
89    { if(ereg("([0-9]{2,4})-([0-9]{1,2})-([0-9]{1,2})", $i, $regs)) $i = $regs[3]."-".$regs[2]."-".$regs[1];
90      return $i;
91    }
92
93    function englishdate($i)
94    { if(ereg("([0-9]{1,2})-([0-9]{1,2})-([0-9]{2,4})", $i, $regs)) $i = $regs[3]."-".$regs[2]."-".$regs[1];
95      return $i;
96    }
97
98    function unix_time($mysql_timestamp)
99    { if
100      (    preg_match('/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/', $mysql_timestamp, $pieces)
101        || preg_match('/(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/', $mysql_timestamp, $pieces)
102      )
103      { $unix_time = mktime($pieces[4], $pieces[5], $pieces[6], $pieces[2], $pieces[3], $pieces[1]);
104      }
105      elseif
106      (    preg_match('/\d{4}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}/', $mysql_timestamp)
107        || preg_match('/\d{2}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}/', $mysql_timestamp)
108        || preg_match('/\d{4}\-\d{2}\-\d{2}/', $mysql_timestamp)
109        || preg_match('/\d{2}\-\d{2}\-\d{2}/', $mysql_timestamp)
110      )
111      { $unix_time = strtotime($mysql_timestamp);
112      }
113      elseif
114      (    preg_match('/(\d{4})(\d{2})(\d{2})/', $mysql_timestamp, $pieces)
115        || preg_match('/(\d{2})(\d{2})(\d{2})/', $mysql_timestamp, $pieces)
116      )
117      { $unix_time = mktime(0, 0, 0, $pieces[2], $pieces[3], $pieces[1]);
118      }
119      return $unix_time;
120    }
121
122    function str_to_hex($str)
123    { $hex = "";
124      $n = 0;
125      while($n < strlen($str))
126      { $hex_char = dechex(ord($str[$n]));
127        if(strlen($hex_char) == 1) $hex_char = "0".$hex_char;
128        $hex .= $hex_char;
129        $n++;
130      }
131      return $hex;
132    }
133
134    function is_email($email)
135    { $atom   = '[-a-z0-9!#$%&\'*+\\/=?^_`{|}~]';
136      $domain = '([a-z0-9]([-a-z0-9]*[a-z0-9]+)?)';
137      return preg_match
138      ( '/^'.$atom.'+'.'(\.'.$atom.'+)*'.'@'.'('.$domain.'{1,63}\.)+'.$domain.'{2,63}$/i',
139        $email
140      );
141    }
142
143    # ----------------------------------------------------------------------------------------
144    #                                                                        fonctions de base
145    #
146
147    function list_()
148    { $list = array();
149      try
150      { $query = "SELECT * FROM #--table";
151        $rst = $this->sql->query($query);
152        while($v_rst = $this->sql->fetch_assoc($rst)) $list[$v_rst["id"]] = $v_rst;
153        $this->sql->free_result($rst);
154      }
155      catch(Exception $_e) { $list = false; }
156      return $list;
157    }
158
159    function add_($value)
160    { try
161      { $query =
162         "INSERT INTO #--table(value) VALUES"
163        ."( ".$this->eq($value)
164        .")";
165        $this->sql->query($query);
166      }
167      catch(Exception $_e) { return false; }
168      return true;
169    }
170
171    function set_($id, $value)
172    { try
173      { $query =
174         "UPDATE #--table SET"
175        ."  value=".$this->eq($value)
176        ." WHERE id=".$this->eq($id);
177        $this->sql->query($query);
178      }
179      catch(Exception $_e) { return false; }
180      return true;
181    }
182
183    function del_($id)
184    { try
185      { $query = "DELETE FROM #--table WHERE id=".$this->eq($id);
186        $this->sql->query($query);
187      }
188      catch(Exception $_e) { return false; }
189      return true;
190    }
191
192  }
193
194?>
Note: See TracBrowser for help on using the repository browser.