Subversion Repositories locum

Rev

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

Rev Author Line No. Line
1 jblyberg 1
<?php
20 jblyberg 2
/**
22 jblyberg 3
 * Locum is a software library that abstracts ILS functionality into a
4
 * catalog discovery layer for use with such things as bolt-on OPACs like
5
 * SOPAC.
6
 * @package Locum
7
 * @author John Blyberg
8
 */
1 jblyberg 9
 
22 jblyberg 10
 
11
/**
12
 * This is the parent Locum class for all locum-related activity.
13
 * This is called as the parent by either the client or the server piece.
14
 */
1 jblyberg 15
class locum {
16
 
111 jblyberg 17
  public $locum_config;
18
  public $locum_cntl;
19
  public $db;
20
  public $dsn;
1 jblyberg 21
 
111 jblyberg 22
  /**
23
   * Locum constructor.
24
   * This function prepares Locum for activity.
25
   */
26
  public function __construct() {
27
    if (file_exists('locum-hooks.php')) {
28
      require_once('locum-hooks.php');
29
    }
30
    if (is_callable(array('locum_hook', 'constructor_override'))) {
31
      $hook = new locum_client_hook;
32
      $hook->constructor_override($this);
112 jblyberg 33
      if ($this->replace_constructor) {
34
        return;
35
      }
111 jblyberg 36
    }
37
 
38
    ini_set('memory_limit','128M');
39
    $this->locum_config = parse_ini_file('config/locum.ini', true);
120 jblyberg 40
    $script_dir = realpath(dirname(__FILE__));
1 jblyberg 41
 
111 jblyberg 42
    // Take care of requirements
43
    require_once('MDB2.php');
44
    require($this->locum_config['locum_config']['dsn_file']);
45
    $this->dsn = $dsn;
46
    $connector_type = 'locum_'
47
      . $this->locum_config['ils_config']['ils'] . '_'
48
      . $this->locum_config['ils_config']['ils_version'];
49
    require_once('connectors/' . $connector_type . '/' . $connector_type . '.php');
1 jblyberg 50
 
111 jblyberg 51
    // Fire up the Locum connector
52
    $locum_class_name = 'locum_' . $this->locum_config['ils_config']['ils'] . '_' . $this->locum_config['ils_config']['ils_version'];
53
    $this->locum_cntl =& new $locum_class_name;
120 jblyberg 54
    if (file_exists($script_dir . '/connectors/' . $connector_type . '/config/' . $connector_type . '.ini')) {
55
      $this->locum_config = array_merge($this->locum_config, parse_ini_file('connectors/' . $connector_type . '/config/' . $connector_type . '.ini', true));
115 jblyberg 56
    }
120 jblyberg 57
    $this->locum_cntl->locum_config = $this->locum_config;
111 jblyberg 58
  }
1 jblyberg 59
 
60
 
111 jblyberg 61
  /**
62
   * Instead of using stdout, Locum will handle output via this logging transaction.
63
   *
64
   * @param string $msg Log message
65
   * @param int $severity Loglevel/severity of the message
66
   * @param boolean $silent Output to stdout.  Default: yes
67
   */
68
  public function putlog($msg, $severity = 1, $silent = TRUE) {
112 jblyberg 69
 
111 jblyberg 70
    if ($severity > 5) { $severity = 5; }
71
    $logfile = $this->locum_config['locum_config']['log_file'];
72
    $quiet = $this->locum_config['locum_config']['run_quiet'];
2 jblyberg 73
 
111 jblyberg 74
    for ($i = 0; $i < $severity; $i++) { $indicator .= '*'; }
75
    $indicator = '[' . str_pad($indicator, 5) . ']';
76
    file_put_contents($logfile, $indicator . ' ' . $msg . "\n", FILE_APPEND);
77
    if (!$quiet && !$silent) { print $indicator . ' ' . $msg . "\n"; }
78
  }
2 jblyberg 79
 
111 jblyberg 80
  /**
81
   * Returns a specifically formatted array or string based on locum config values passed.  This is primarily used internally,
82
   * for instance, with custom search handlers.
83
   *
84
   * @param string $csv Comma (or otherwise) separated values
85
   * @param string $implode Optional implode character.  If not provided, the function will return an array of values
86
   * @param string $separator Optional separator string for the values.  Defaults to comma.
87
   * @return string|array Formatted values
88
   */
89
  public function csv_parser($csv, $implode = FALSE, $separator = ',') {
112 jblyberg 90
 
111 jblyberg 91
    $csv_array = explode($separator, trim($csv));
92
    $cleaned = array();
93
    foreach ($csv_array as $csv_value) {
94
      $cleaned[] = trim($csv_value);
95
    }
96
    if ($implode) {
97
      $cleaned = implode($implode, $cleaned);
98
    }
99
    return $cleaned;
100
  }
132 jblyberg 101
 
152 jblyberg 102
  public function db_query($query, $query_only = TRUE, $return_type = 'all', $assoc = TRUE) {
103
    $db =& MDB2::connect($this->dsn);
104
    $db_result =& $db->query($query);
105
    if ($query_only) {
106
      return TRUE;
107
    } else {
108
      switch (trim(strtolower($return_type))) {
109
        case 'row':
110
          if ($assoc) {
111
            return $db_result->fetchRow(MDB2_FETCHMODE_ASSOC);
112
          } else {
113
            return $db_result->fetchRow();
114
          }
115
          break;
116
        case 'col':
117
          return $db_result->fetchCol();
118
          break;
119
        case 'all':
120
        default:
121
          if ($assoc) {
122
            return $db_result->fetchAll(MDB2_FETCHMODE_ASSOC);
123
          } else {
124
            return $db_result->fetchAll();
125
          }
126
          break;
127
      }
128
    }
129
  }
130
 
132 jblyberg 131
  /**
141 jblyberg 132
   * Checks $query_value against $ini_value to see a) if its a regex or csv match.
133
   * It will then return TRUE if it is a match or FALSE if not.
134
   *
135
   * @param string $ini_value INI file value
136
   * @param string $query_value Value to be matched against $ini_value
137
   * @return boolean TRUE = match / FALSE = no match
138
   */
139
  public function match_ini_value($ini_value, $query_value) {
140
    if (preg_match('/^\//', $ini_value)) {
141
      if (preg_match($ini_value, $query_value)) { return TRUE; }
142
    } else {
143
      if (in_array($query_value, locum::csv_parser($match_crit))) { return TRUE; }
144
    }
145
    return FALSE;
146
  }
147
 
148
  /**
132 jblyberg 149
   * Returns a CRC32 value that is compatible with MySQL (>=4.1) CRC32() function
150
   *
151
   * @param string $str String to be converted
152
   * @return int a CRC32 polynomial of the string
153
   */
154
  public function string_poly($str) {
134 jblyberg 155
    if (crc32($str) < 0) {
156
      return crc32($str) + 4294967296;
157
    } else {
158
      return crc32($str);
159
    }
132 jblyberg 160
  }
161
 
1 jblyberg 162
}