Subversion Repositories computer_asset_manager_v1

Rev

Rev 3 | Details | Compare with Previous | Last modification | View Log | RSS feed

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