Subversion Repositories computer_asset_manager_v1

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
23 rodolico 1
<?php
2
 
3
/*
4
 * these are some global functions which we need since their classes have not
5
 * been created yet. The code that calls these should be modified once CAMPv2
6
 * is complete. At that point, these should be calls to those classes.
7
 */
8
 
9
   function makeSafeSQLValue ( $value, $type='S' ) {
10
      if(get_magic_quotes_gpc()) {
11
           $value = stripslashes($value);
12
       }
13
      $value = mysql_real_escape_string( $value );
14
      if (($type == 'S') and strlen($value)  > 0) { // put quotes around strings
15
         $value = "'" . $value . "'";
16
      } elseif ($type == 'D') {
17
         if ( $result = strtotime( $value ) ) {
18
            $value = Date( 'Y-m-d', $result);
19
         } else {
20
            $value = '0000-00-00';
21
         }
22
         $value = "'" . $value . "'";
23
      } elseif ($type == 'DT') {
24
         if ( $result = strtotime( $value ) ) {
25
            $value = Date( 'Y-m-d H:i:s', $result);
26
         } else {
27
            $value = '0000-00-00';
28
         }
29
         $value = "'" . $value . "'";
30
      } elseif (strlen($value) == 0) { // and substitue null for empty values otherwise;
31
         $value = 'null';
32
      }
33
      return $value;
34
   }
35
 
36
 
37
   /*
38
      Taken from comments at http://www.php.net/manual/en/function.mysql-query.php
39
      function originally named 'q'
40
      $r = q('Select id,foo FROM blah');
41
      echo $r[0]['id']; // first row, field 'id'
42
 
43
      // for single field single row selects
44
      // only the value is returned
45
      $count = q('SELECT count(*) from blah');
46
      // $count is the number
47
 
48
      Returns affected_rows and/or insert_id for anything other than select's.
49
      If you dont want field name keys then pass 0 for second parameter.
50
 
51
      For a query returning multiple rows, will return an associative array
52
         return['data'] contains an two dimensional array of all data received from the query
53
         return['meta']
54
            array of associative arrays. Each row in the array corresponds to a column in the query return
55
            Each array row contains the following:
56
               'name'   name of the column
57
               'length' maximum width of the column FOR THIS QUERY
58
               'numeric'true if the column is numeric
59
               'type'   type of the column (database dependant)
60
 
61
   */
62
 
63
      function queryDatabaseExtended($query,$assoc=1,$showErrors=true) {
64
      // print "<pre>In Query database\n---------------------\n$query\n---------------------\n</pre>";
65
      $r = @mysql_query($query);
66
      if( mysql_errno() ) {
67
         $error = 'MYSQL ERROR #'.mysql_errno().' : <small>' . mysql_error(). "</small><br><VAR>$query</VAR>";
68
         if ( $showErrors ) echo($error);
69
         return FALSE;
70
      }
71
      if( ! preg_match ( '/^\s*select/i', $query ) ) {
72
         $f = array( 'affected_rows' => mysql_affected_rows(),'insert_id' => mysql_insert_id());
73
         // create audit trail
74
         return $f;
75
      }
76
      $count = @mysql_num_rows($r);
77
      $fieldMeta = array();
78
      $i = 0;
79
      while ($i++ < mysql_num_fields($r)) {
80
         $meta = mysql_fetch_field ( $r );
81
         //objectDebugScreen($meta);
82
         $fieldMeta[] = array('name' => $meta->name, 'length' => $meta->max_length, 'numeric' => $meta->numeric, 'type' => $meta->type );
83
      }
84
      if( ! $count ) return '';
85
      $all = array();
86
      for( $i = 0; $i < $count; $i++ ) {
87
        if( $assoc ) $f = mysql_fetch_assoc($r);
88
        else $f = mysql_fetch_row($r);
89
        $all[] = $f;
90
      }
91
      mysql_free_result($r);
92
      return array( 'meta' => $fieldMeta, 'data' => $all, 'count' => $count);
93
   } // function queryDatabaseExtended
94
 
95
 
96
   // function returns the first column of the first row of data returned from query
97
   // or null no value returned
98
   function getOneDBValue( $sql ) {
99
      $data = queryDatabaseExtended( $sql, false ); // get the query results into a standard array
100
      return isset( $data['count'] ) ? $data['data'][0][0] : null;
101
   }
102
 
103
  function countNumberOfRows ( $sql ) {
104
     $count = queryDatabaseExtended("select count(*) numRows from ($sql) test");
105
     return $count['data'][0]['numRows'];
106
  }
107
 
108
   function makeWhereClause ($conditions) {
109
      $whereClause = ' where ' . implode (' and ', $conditions );
110
      return $whereClause;
111
   }
112
 
113
   function insertValuesIntoQuery( $query, $values ) {
114
      foreach ( $values as $name => $value ) {
115
         $query = search_replace_string($query, "<$name>", $value );
116
      }
117
      return $query;
118
   }
119
 
120
 
121
function getClientID ( $name ) {
122
   $client = getOneDBValue( "select client_id from client where name = '$name' and removed_date is null
123
             union
124
             select client_id from client_alias where alias = '$name' and removed_date is null");
125
   return $client;
126
}
127
 
128
 
129
function addSerialNumber( $machineID, $serialNumber ) {
130
   queryDatabaseExtended( "update device set serial = '$serialNumber' where device_id = $machineID" );
131
}
132
 
133
function fuzzyFindMachineID ( $name, $clientID ) {
134
   return getOneDBValue ("select device_id
135
                from device join site using (site_id)
136
                where site.client_id = $clientID
137
                     and device.name = '$name'
138
                     and device.removed_date is null
139
               union
140
               select device_id 
141
               from device_alias join device using (device_id) join site using (site_id) 
142
               where device_alias.alias = '$name' and site.client_id = $clientID");
143
} // fuzzyFindMachineID
144
 
145
 
146
function getMachineID ( $name, $clientID, $serialNumber ) {
147
   $machineID = null;
148
   if ( isset( $serialNumber ) ) {
149
      $machineID = getOneDBValue( "select device_id from device where serial = '$serialNumber' and device.removed_date is null" );
150
      if ( $machineID ) { # check if it is unique
151
         $count = getOneDBValue( "select count(*) from device where serial = '$serialNumber' and device.removed_date is null" );
152
         if ( $count > 1 ) { # woops, we have a duplicate serial number
153
            return -1;
154
         }
155
      }
156
      if ( ! $machineID ) {
157
         if ( $machineID = fuzzyFindMachineID( $name, $clientID ) )
158
            addSerialNumber( $machineID, $serialNumber );
159
      }
160
   }
161
 
162
   if ( ! isset( $machineID ) )
163
      $machineID = getOneDBValue ("select device_id
164
                from device join site using (site_id)
165
                where site.client_id = $clientID
166
                     and device.name = '$name'
167
                     and device.removed_date is null
168
               union
169
               select device_id 
170
               from device_alias join device using (device_id) join site using (site_id) 
171
               where device_alias.alias = '$name' and site.client_id = $clientID");
172
   return $machineID;
173
}
174
 
175
?>