summaryrefslogtreecommitdiff
path: root/backend/php/src/setup/setup_library/upgrade.php
blob: 122da589fdcd5cd4374c8af1750cd8f9b99c1cac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/**
* @author  Joel Wan & Mark Slemko.  Designs by Jonathan Easton
* @link  http://www.phpobjectgenerator.com
* @copyright  Offered under the  BSD license
*
* This upgrade file does the following:
* 1. Checks if there is a new version of POG
* 2. If there is, it reads generates newer versions of all objects in the object directory,
* zip then and present them to the user to 'download'
*/
ini_set("max_execution_time", 0);
include_once "../../configuration.php";
include_once "class.zipfile.php";
include_once "setup_misc.php";

	/**
	 * Connects to POG SOAP server defined in configuration.php and
	 * generates new versions of all objects detected in /objects/ dir.
	 * All upgraded objects are then zipped and presented to user.
	 *
	 * @param string $path
	 */
	function UpdateAllObjects($path)
	{
		$dir = opendir($path);
		$objects = array();
		while(($file = readdir($dir)) !== false)
		{
			if(strlen($file) > 4 && substr(strtolower($file), strlen($file) - 4) === '.php' && !is_dir($file) && $file != "class.database.php" && $file != "configuration.php" && $file != "setup.php" && $file != "class.pog_base.php")
			{
				$objects[] = $file;
			}
		}
		closedir($dir);
		$i = 0;
		foreach($objects as $object)
		{
			$content = file_get_contents($path."/".$object);
			$contentParts = split("<b>",$content);
			if (isset($contentParts[1]))
			{
				$contentParts2 = split("</b>",$contentParts[1]);
			}
			if (isset($contentParts2[0]))
			{
				$className = trim($contentParts2[0]);
			}
			if (isset($className))
			{
				eval ('include_once("../../objects/class.'.strtolower($className).'.php");');
				$instance = new $className();
				if (!TestIsMapping($instance))
				{
					$objectNameList[] = $className;

					$linkParts1 = split("\*\/", $contentParts[1]);
					$linkParts2 = split("\@link", $linkParts1[0]);
					$link = $linkParts2[1];
					$options = false;
					if ($GLOBALS['configuration']['proxy_host'] != false &&
						$GLOBALS['configuration']['proxy_port'] != false &&
						$GLOBALS['configuration']['proxy_username'] != false &&
						$GLOBALS['configuration']['proxy_password'] != false)
					{
						$options = array(
											'proxy_host' => $GLOBALS['configuration']['proxy_host'],
											'proxy_port' => $GLOBALS['configuration']['proxy_port'],
											'proxy_login' => $GLOBALS['configuration']['proxy_username'],
											'proxy_password' => $GLOBALS['configuration']['proxy_password']
											);
					}
					$client = new SoapClient(
											$GLOBALS['configuration']['soap'],
											$options) ;
					if ($i == 0)
					{
						$package = unserialize($client->GeneratePackageFromLink($link));
					}
					else
					{
						$objectString = $client->GenerateObjectFromLink($link);
						$package["objects"]["class.".strtolower($className).".php"] = $objectString;
					}
				}
			}
			$i++;
		}


		//upgrade mapping classes if any
		foreach ($objectNameList as $objectName)
		{
			$instance = new $objectName();
			foreach ($instance->pog_attribute_type as $key => $attribute_type)
			{
				if ($attribute_type['db_attributes'][1] == "JOIN")
				{
					$mappingString = $client->GenerateMapping($objectName, $key, (isset($GLOBALS['configuration']['pdoDriver']) ? 'php5.1' :'php5'), (isset($GLOBALS['configuration']['pdoDriver']) ? 'pdo' :'pog'), (isset($GLOBALS['configuration']['pdoDriver']) ? 'mysql' :''));
					$package["objects"]['class.'.strtolower(MappingName($objectName, $key)).'.php'] = $mappingString;
				}
			}
		}

		$zipfile = new createZip();
		$zipfile -> addPOGPackage($package);
		$zipfile -> forceDownload("pog.".time().".zip");
	}

	/**
	 * Checks if POG generator has been updated
	 *
	 * @return unknown
	 */
	function UpdateAvailable()
	{
		$client = new SoapClient($GLOBALS['configuration']['soap']);
		$generatorVersion = base64_decode($client -> GetGeneratorVersion());
		if ($generatorVersion != $GLOBALS['configuration']['versionNumber'].$GLOBALS['configuration']['revisionNumber'])
		{
			return true;
		}
		else
		{
			return  false;
		}
	}

	if (UpdateAvailable())
	{
		UpdateAllObjects("../../objects/");
	}
	else
	{
		echo "<script>
			alert('All POG objects are already up to date');
			window.close();
		</script>";
	}
?>