Open Journal Systems  3.3.0
OAIUtils.inc.php
1 <?php
2 
18 class OAIUtils {
19 
26  static function UTCDate($timestamp = 0, $includeTime = true) {
27  $format = "Y-m-d";
28  if($includeTime) {
29  $format .= "\TH:i:s\Z";
30  }
31 
32  if($timestamp == 0) {
33  return gmdate($format);
34 
35  } else {
36  return gmdate($format, $timestamp);
37  }
38  }
39 
48  static function UTCtoTimestamp($date, $requiredGranularity = null) {
49  // FIXME Has limited range (see http://php.net/strtotime)
50  if (preg_match("/^\d\d\d\d\-\d\d\-\d\d$/", $date)) {
51  // Match date
52  $time = strtotime("$date UTC");
53  return ($time != -1) ? $time : 'invalid';
54 
55  } else if (preg_match("/^(\d\d\d\d\-\d\d\-\d\d)T(\d\d:\d\d:\d\d)Z$/", $date, $matches)) {
56  // Match datetime
57  // FIXME
58  $date = "$matches[1] $matches[2]";
59  if ($requiredGranularity && $requiredGranularity != 'YYYY-MM-DDThh:mm:ssZ') {
60  return 'invalid_granularity';
61 
62  } else {
63  $time = strtotime("$date UTC");
64  return ($time != -1) ? $time : 'invalid';
65  }
66 
67  } else {
68  return 'invalid';
69  }
70  }
71 
72 
77  static function prepInput(&$data) { // REFERENCE REQUIRED
78  if (!is_array($data)) {
79  $data = urldecode($data);
80 
81  } else {
82  foreach ($data as $k => $v) {
83  if (is_array($data[$k])) {
84  self::prepInput($data[$k]);
85  } else {
86  $data[$k] = urldecode($v);
87  }
88  }
89  }
90  return $data;
91  }
92 
99  static function prepOutput(&$data) { // REFERENCE REQUIRED
100  if (!is_array($data)) {
101  $data = htmlspecialchars($data);
102 
103  } else {
104  foreach ($data as $k => $v) {
105  if (is_array($data[$k])) {
106  self::prepOutput($data[$k]);
107  } else {
108  // FIXME FIXME FIXME
109  $data[$k] = htmlspecialchars($v);
110  }
111  }
112  }
113  return $data;
114  }
115 
123  static function parseStr($string, &$array) {
124  $pairs = explode('&', $string);
125  foreach ($pairs as $p) {
126  $vars = explode('=', $p);
127  if (!empty($vars[0]) && isset($vars[1])) {
128  $key = $vars[0];
129  $value = join('=', array_splice($vars, 1));
130 
131  if (!isset($array[$key])) {
132  $array[$key] = $value;
133  } else if (is_array($array[$key])) {
134  array_push($array[$key], $value);
135  } else {
136  $array[$key] = array($array[$key], $value);
137  }
138  }
139  }
140  }
141 }
142 
143 
OAIUtils\parseStr
static parseStr($string, &$array)
Definition: OAIUtils.inc.php:123
OAIUtils\prepInput
static prepInput(&$data)
Definition: OAIUtils.inc.php:77
OAIUtils\UTCDate
static UTCDate($timestamp=0, $includeTime=true)
Definition: OAIUtils.inc.php:26
OAIUtils\prepOutput
static prepOutput(&$data)
Definition: OAIUtils.inc.php:99
OAIUtils\UTCtoTimestamp
static UTCtoTimestamp($date, $requiredGranularity=null)
Definition: OAIUtils.inc.php:48
OAIUtils
Definition: OAIUtils.inc.php:18