Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
VersionCheck.inc.php
1 <?php
2 
18 define('VERSION_CODE_PATH', 'dbscripts/xml/version.xml');
19 
20 import('lib.pkp.classes.db.XMLDAO');
21 import('lib.pkp.classes.site.Version');
22 
23 class VersionCheck {
24 
29  function &getLatestVersion() {
30  $application = PKPApplication::getApplication();
31  $returner =& VersionCheck::parseVersionXML(
32  $application->getVersionDescriptorUrl()
33  );
34  return $returner;
35  }
36 
41  function &getCurrentDBVersion() {
42  $versionDao = DAORegistry::getDAO('VersionDAO');
43  $dbVersion =& $versionDao->getCurrentVersion();
44  return $dbVersion;
45  }
46 
51  function &getCurrentCodeVersion() {
52  $versionInfo = VersionCheck::parseVersionXML(VERSION_CODE_PATH);
53  if ($versionInfo) {
54  $version = $versionInfo['version'];
55  } else {
56  $version = false;
57  }
58  return $version;
59  }
60 
66  function &parseVersionXML($url) {
67  $xmlDao = new XMLDAO();
68  $data = $xmlDao->parseStruct($url, array());
69  if (!$data) {
70  $result = false;
71  return $result;
72  }
73 
74  // FIXME validate parsed data?
75  $versionInfo = array();
76 
77  if(isset($data['application'][0]['value']))
78  $versionInfo['application'] = $data['application'][0]['value'];
79  if(isset($data['type'][0]['value']))
80  $versionInfo['type'] = $data['type'][0]['value'];
81  if(isset($data['release'][0]['value']))
82  $versionInfo['release'] = $data['release'][0]['value'];
83  if(isset($data['tag'][0]['value']))
84  $versionInfo['tag'] = $data['tag'][0]['value'];
85  if(isset($data['date'][0]['value']))
86  $versionInfo['date'] = $data['date'][0]['value'];
87  if(isset($data['info'][0]['value']))
88  $versionInfo['info'] = $data['info'][0]['value'];
89  if(isset($data['package'][0]['value']))
90  $versionInfo['package'] = $data['package'][0]['value'];
91  if(isset($data['patch'][0]['value'])) {
92  $versionInfo['patch'] = array();
93  foreach ($data['patch'] as $patch) {
94  $versionInfo['patch'][$patch['attributes']['from']] = $patch['value'];
95  }
96  }
97  if(isset($data['class'][0]['value']))
98  $versionInfo['class'] = (string) $data['class'][0]['value'];
99  $versionInfo['lazy-load'] = (isset($data['lazy-load'][0]['value']) ? (int) $data['lazy-load'][0]['value'] : 0);
100  $versionInfo['sitewide'] = (isset($data['sitewide'][0]['value']) ? (int) $data['sitewide'][0]['value'] : 0);
101 
102  if(isset($data['release'][0]['value']) && isset($data['application'][0]['value'])) {
103  $version =& Version::fromString(
104  $data['release'][0]['value'],
105  isset($data['type'][0]['value']) ? $data['type'][0]['value'] : null,
106  $data['application'][0]['value'],
107  isset($data['class'][0]['value']) ? $data['class'][0]['value'] : '',
108  $versionInfo['lazy-load'],
109  $versionInfo['sitewide']
110  );
111  $versionInfo['version'] =& $version;
112  }
113 
114  return $versionInfo;
115  }
116 
123  function getPatch(&$versionInfo, $codeVersion = null) {
124  if (!isset($codeVersion)) {
125  $codeVersion =& VersionCheck::getCurrentCodeVersion();
126  }
127  if (isset($versionInfo['patch'][$codeVersion->getVersionString()])) {
128  return $versionInfo['patch'][$codeVersion->getVersionString()];
129  }
130  return null;
131  }
132 
143  function &getValidPluginVersionInfo($versionFile, $returnErrorMsg = false) {
144  $nullVar = null;
145  $errorMsg = null;
146  $fileManager = new FileManager();
147  if ($fileManager->fileExists($versionFile)) {
148  $versionInfo =& VersionCheck::parseVersionXML($versionFile);
149  } else {
150  $errorMsg = 'manager.plugins.versionFileNotFound';
151  }
152 
153  // Validate plugin name and type to avoid abuse
154  if (is_null($errorMsg)) {
155  $productType = explode(".", $versionInfo['type']);
156  if(count($productType) != 2 || $productType[0] != 'plugins') {
157  $errorMsg = 'manager.plugins.versionFileInvalid';
158  }
159  }
160 
161  if (is_null($errorMsg)) {
162  $pluginVersion =& $versionInfo['version'];
163  $namesToValidate = array($pluginVersion->getProduct(), $productType[1]);
164  foreach($namesToValidate as $nameToValidate) {
165  if (!String::regexp_match('/[a-z][a-zA-Z0-9]+/', $nameToValidate)) {
166  $errorMsg = 'manager.plugins.versionFileInvalid';
167  break;
168  }
169  }
170  }
171 
172  if ($errorMsg) {
173  if ($returnErrorMsg) {
174  return $errorMsg;
175  } else {
176  $templateMgr = TemplateManager::getManager();
177  $templateMgr->assign('message', $errorMsg);
178  return $nullVar;
179  }
180  } else {
181  return $pluginVersion;
182  }
183  }
184 
191  $versionInfo =& VersionCheck::getLatestVersion();
192  $latestVersion = $versionInfo['release'];
193 
194  $currentVersion =& VersionCheck::getCurrentDBVersion();
195  if($currentVersion->compare($latestVersion) < 0) return $latestVersion;
196  else return false;
197  }
198 }
199 
200 ?>
static & getDAO($name, $dbconn=null)
& getValidPluginVersionInfo($versionFile, $returnErrorMsg=false)
static regexp_match($pattern, $subject)
Definition: String.inc.php:339
Provides methods to check for the latest version of OJS.
Operations for retrieving and modifying objects from an XML data source.
Definition: XMLDAO.inc.php:19
Class defining basic operations for file management.
& fromString($versionString, $productType=null, $product=null, $productClass= '', $lazyLoad=0, $sitewide=1)
Definition: Version.inc.php:67
& parseVersionXML($url)
getPatch(&$versionInfo, $codeVersion=null)