Project

General

Profile

1 1916 Luisehahne
<?php
2
/**
3
 *
4
 * @category        module
5
 * @package         droplet
6
 * @author          Ruud Eisinga (Ruud) John (PCWacht)
7
 * @author          WebsiteBaker Project
8
 * @copyright       2009-2013, WebsiteBaker Org. e.V.
9
 * @link            http://www.websitebaker.org/
10
 * @license         http://www.gnu.org/licenses/gpl.html
11
 * @platform        WebsiteBaker 2.8.x
12
 * @requirements    PHP 5.2.2 and higher
13
 * @version         $Id: upgrade.php 1717 2012-08-29 14:09:09Z Luisehahne $
14
 * @filesource      $HeadURL: svn://isteam.dynxs.de/wb_svn/wb280/branches/2.8.x/wb/modules/droplets/upgrade.php $
15
 * @lastmodified    $Date: 2012-08-29 16:09:09 +0200 (Mi, 29. Aug 2012) $
16
 *
17
 */
18
/* -------------------------------------------------------- */
19
// Must include code to stop this file being accessed directly
20
if(!defined('WB_PATH')) {
21
22
	require_once(dirname(dirname(dirname(__FILE__))).'/framework/globalExceptionHandler.php');
23
	throw new IllegalFileException();
24
}
25
26
/* -------------------------------------------------------- */
27
28
function prepareDropletToFile($aDroplet) {
29
	$retVal = '';
30
	$sDescription = '//:'.($aDroplet['description']!='') ? $aDroplet['description']: 'Desription';
31
	$sComments = '';
32
	$aComments = explode("\n",$aDroplet['comments']);
33
	$sCode = '';
34
	$aCode = explode("\n",$aDroplet['code']);
35
	if( (sizeof($aComments)) ){
36
		foreach($aComments AS $isComment) {
37
			$sComments .= '//:'.$isComment;
38
		}
39
	} else {
40
		$sComments .= '//:use [['.$aDroplet['name'].']]';
41
	}
42
	if( (sizeof($aCode)) ){
43
		foreach($aCode AS $isCode) {
44
			$sCode .= $isCode;
45
		}
46
	}
47
48
	$retVal = $sDescription."\n".$sComments."\n".$sCode;
49
	return $retVal;
50
}
51
//
52
function backupDropletFromDatabase($sTmpDir) {
53
	$retVal = '';
54
	$database=WbDatabase::getInstance();
55
	$sql = 'SELECT `name`,`description`,`comments`,`code`  FROM `'.$database->TablePrefix.'mod_droplets` '
56
	     . 'ORDER BY `modified_when` DESC';
57
	if( $oRes = $database->query($sql) ) {
58
		while($aDroplet = $oRes->fetchRow(MYSQL_ASSOC)) {
59
			$sData = prepareDropletToFile($aDroplet);
60
			$sFileName = $sTmpDir.$aDroplet['name'].'.php';
61
			if(file_put_contents($sFileName,$sData)) {
62
				$retVal .= $sFileName.',';
63
			}
64
		}
65
66
	}
67
	return $retVal;
68
}
69
70
71
function insertDropletFile($aDropletFiles,&$msg,$bOverwriteDroplets,$admin)
72
{
73
	$OK  = ' <span style="color:#006400; font-weight:bold;">OK</span> ';
74
	$FAIL = ' <span style="color:#ff0000; font-weight:bold;">FAILED</span> ';
75 1948 darkviper
	$oDb = WbDatabase::getInstance();
76 1916 Luisehahne
	foreach ($aDropletFiles as $sDropletFile) {
77
		$msgSql = '';
78
		$extraSql = '';
79
		$sDropletName = pathinfo ($sDropletFile, PATHINFO_FILENAME);
80 1951 darkviper
		$sql = 'SELECT `name` FROM `'.$oDb->TablePrefix.'mod_droplets` '
81
		     . 'WHERE `name` LIKE \''.addcslashes($oDb->escapeString($sDropletName), '%_').'\' ';
82
		if( !( $sTmpName = $oDb->get_one($sql)) )
83
		{
84 1948 darkviper
			$sql = 'INSERT INTO `'.$oDb->TablePrefix.'mod_droplets`';
85 1951 darkviper
			$msgSql = 'INSERT Droplet `'.$oDb->escapeString($sDropletName).'` INTO`'.$oDb->TablePrefix.'mod_droplets`'." $OK";
86
		} elseif ($bOverwriteDroplets)
87
		{
88
			$sDropletName = $sTmpName;
89 1948 darkviper
			$sql = 'UPDATE `'.$oDb->TablePrefix.'mod_droplets` ';
90 1951 darkviper
			$extraSql = 'WHERE `name` = \''.addcslashes($oDb->escapeString($sDropletName), '%_').'\' ';
91 1948 darkviper
			$msgSql = 'UPDATE Droplet `'.$sDropletName.'` INTO`'.$oDb->TablePrefix.'mod_droplets`'." $OK";
92 1916 Luisehahne
		}
93
// get description, comments and oode
94
		$sDropletFile = preg_replace('/^\xEF\xBB\xBF/', '', $sDropletFile);
95
		if( ($msgSql!='') && ($aFileData = file($sDropletFile)) ) {
96 1951 darkviper
				$bDescription = false;
97
				$bComments = false;
98
				$bCode = false;
99
				$sDescription = '';
100
				$sComments = '';
101
				$sCode = '';
102
				$sPattern = "#//:#im";
103
				while ( sizeof($aFileData) > 0 ) {
104
					$sSqlLine = trim(array_shift($aFileData));
105
					$isNotCode = (bool)preg_match($sPattern, $sSqlLine);
106
					if( $isNotCode==true ) {
107 1916 Luisehahne
// first step line is description
108 1951 darkviper
						if($bDescription==false) {
109
							$sDescription .= str_replace('//:','',$sSqlLine);
110
							$bDescription = true;
111
						} else {
112
// second step fill comments
113
							$sComments .= str_replace('//:','',$sSqlLine).PHP_EOL;
114
						}
115 1916 Luisehahne
					} else {
116 1951 darkviper
// third step fill code
117
						$sCode .= str_replace('//:','',$sSqlLine).PHP_EOL;
118 1916 Luisehahne
					}
119
				}
120 1951 darkviper
			$iModifiedWhen = time();
121
			$iModifiedBy = (method_exists($admin, 'get_user_id') && ($admin->get_user_id()!=null) ? $admin->get_user_id() : 1);
122
			$sql .= 'SET  `name` =\''.$oDb->escapeString($sDropletName).'\','
123
				 .       '`description` =\''.$oDb->escapeString($sDescription).'\','
124
				 .       '`comments` =\''.$oDb->escapeString($sComments).'\','
125
				 .       '`code` =\''.$oDb->escapeString($sCode).'\','
126
				 .       '`modified_when` = '.$iModifiedWhen.','
127
				 .       '`modified_by` = '.$iModifiedBy.','
128
				 .       '`active` = 1'
129
				 .       $extraSql;
130 1916 Luisehahne
		}
131 1948 darkviper
		if( $oDb->query($sql) ) {
132 1916 Luisehahne
			if( $msgSql!='' ) { $msg[] = $msgSql; }
133
		} else {
134 1948 darkviper
			$msg[] = $oDb->get_error();
135 1916 Luisehahne
		}
136
	}
137
	return;
138
}
139
/* -------------------------------------------------------- */
140
141
function isDropletFile($sFileName)
142
{
143
	$bRetval = false;
144
	$matches = array();
145
	if(($sFileData = file_get_contents($sFileName)) !== false)
146
	{
147
//		$sPattern = "#(?://:)+[\w]*\w?#is";
148
//		$sPattern = "#//:[\w].+#imS";
149
		$sPattern = "#//:#im";
150
		$bRetval = (bool)preg_match_all($sPattern, $sFileData, $matches, PREG_SET_ORDER);
151
	}
152
	return $bRetval;
153
}
154
155
/* -------------------------------------------------------- */
156
	function getDropletFromFiles($sBaseDir)
157
	{
158
		$aRetval = array();
159
		$oIterator = new DirectoryIterator($sBaseDir);
160
		foreach ($oIterator as $fileInfo) {
161
		// iterate the directory
162
			if($fileInfo->isDot()) continue;
163
			$sFileName = rtrim(str_replace('\\', '/', $fileInfo->getPathname()), '/');
164
			if($fileInfo->isFile()) {
165
			// only droplets are interesting
166
				if((file_exists($sFileName) && isDropletFile($sFileName))) {
167
				// if dir has no corresponding accessfile remember it
168
					$aRetval[] = $sFileName;
169
				}
170
			}
171
		}
172
		return $aRetval;
173
	}