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
function prepareDropletToFile($aDroplet) {
28
	$retVal = '';
29 1997 Luisehahne
	$sDescription = '//:'.(($aDroplet['description']!='') ? $aDroplet['description']: 'Desription');
30 1916 Luisehahne
	$sComments = '';
31
	$aComments = explode("\n",$aDroplet['comments']);
32
	$sCode = '';
33
	$aCode = explode("\n",$aDroplet['code']);
34
	if( (sizeof($aComments)) ){
35
		foreach($aComments AS $isComment) {
36
			$sComments .= '//:'.$isComment;
37
		}
38
	} else {
39
		$sComments .= '//:use [['.$aDroplet['name'].']]';
40
	}
41
	if( (sizeof($aCode)) ){
42
		foreach($aCode AS $isCode) {
43
			$sCode .= $isCode;
44
		}
45
	}
46
47
	$retVal = $sDescription."\n".$sComments."\n".$sCode;
48
	return $retVal;
49
}
50
//
51
function backupDropletFromDatabase($sTmpDir) {
52
	$retVal = '';
53
	$database=WbDatabase::getInstance();
54
	$sql = 'SELECT `name`,`description`,`comments`,`code`  FROM `'.$database->TablePrefix.'mod_droplets` '
55
	     . 'ORDER BY `modified_when` DESC';
56
	if( $oRes = $database->query($sql) ) {
57
		while($aDroplet = $oRes->fetchRow(MYSQL_ASSOC)) {
58
			$sData = prepareDropletToFile($aDroplet);
59
			$sFileName = $sTmpDir.$aDroplet['name'].'.php';
60
			if(file_put_contents($sFileName,$sData)) {
61
				$retVal .= $sFileName.',';
62
			}
63
		}
64
	}
65
	return $retVal;
66
}
67
68
69
function insertDropletFile($aDropletFiles,&$msg,$bOverwriteDroplets,$admin)
70
{
71
	$OK  = ' <span style="color:#006400; font-weight:bold;">OK</span> ';
72
	$FAIL = ' <span style="color:#ff0000; font-weight:bold;">FAILED</span> ';
73 1948 darkviper
	$oDb = WbDatabase::getInstance();
74 1916 Luisehahne
	foreach ($aDropletFiles as $sDropletFile) {
75
		$msgSql = '';
76
		$extraSql = '';
77
		$sDropletName = pathinfo ($sDropletFile, PATHINFO_FILENAME);
78 1951 darkviper
		$sql = 'SELECT `name` FROM `'.$oDb->TablePrefix.'mod_droplets` '
79
		     . 'WHERE `name` LIKE \''.addcslashes($oDb->escapeString($sDropletName), '%_').'\' ';
80
		if( !( $sTmpName = $oDb->get_one($sql)) )
81
		{
82 1948 darkviper
			$sql = 'INSERT INTO `'.$oDb->TablePrefix.'mod_droplets`';
83 1951 darkviper
			$msgSql = 'INSERT Droplet `'.$oDb->escapeString($sDropletName).'` INTO`'.$oDb->TablePrefix.'mod_droplets`'." $OK";
84
		} elseif ($bOverwriteDroplets)
85
		{
86
			$sDropletName = $sTmpName;
87 1948 darkviper
			$sql = 'UPDATE `'.$oDb->TablePrefix.'mod_droplets` ';
88 1951 darkviper
			$extraSql = 'WHERE `name` = \''.addcslashes($oDb->escapeString($sDropletName), '%_').'\' ';
89 1948 darkviper
			$msgSql = 'UPDATE Droplet `'.$sDropletName.'` INTO`'.$oDb->TablePrefix.'mod_droplets`'." $OK";
90 1916 Luisehahne
		}
91
// get description, comments and oode
92
		$sDropletFile = preg_replace('/^\xEF\xBB\xBF/', '', $sDropletFile);
93
		if( ($msgSql!='') && ($aFileData = file($sDropletFile)) ) {
94 1951 darkviper
				$bDescription = false;
95
				$bComments = false;
96
				$bCode = false;
97
				$sDescription = '';
98
				$sComments = '';
99
				$sCode = '';
100
				$sPattern = "#//:#im";
101
				while ( sizeof($aFileData) > 0 ) {
102
					$sSqlLine = trim(array_shift($aFileData));
103
					$isNotCode = (bool)preg_match($sPattern, $sSqlLine);
104
					if( $isNotCode==true ) {
105 1916 Luisehahne
// first step line is description
106 1951 darkviper
						if($bDescription==false) {
107
							$sDescription .= str_replace('//:','',$sSqlLine);
108
							$bDescription = true;
109
						} else {
110
// second step fill comments
111
							$sComments .= str_replace('//:','',$sSqlLine).PHP_EOL;
112
						}
113 1916 Luisehahne
					} else {
114 1951 darkviper
// third step fill code
115
						$sCode .= str_replace('//:','',$sSqlLine).PHP_EOL;
116 1916 Luisehahne
					}
117
				}
118 1951 darkviper
			$iModifiedWhen = time();
119
			$iModifiedBy = (method_exists($admin, 'get_user_id') && ($admin->get_user_id()!=null) ? $admin->get_user_id() : 1);
120
			$sql .= 'SET  `name` =\''.$oDb->escapeString($sDropletName).'\','
121
				 .       '`description` =\''.$oDb->escapeString($sDescription).'\','
122
				 .       '`comments` =\''.$oDb->escapeString($sComments).'\','
123
				 .       '`code` =\''.$oDb->escapeString($sCode).'\','
124
				 .       '`modified_when` = '.$iModifiedWhen.','
125
				 .       '`modified_by` = '.$iModifiedBy.','
126
				 .       '`active` = 1'
127
				 .       $extraSql;
128 1916 Luisehahne
		}
129 1948 darkviper
		if( $oDb->query($sql) ) {
130 1916 Luisehahne
			if( $msgSql!='' ) { $msg[] = $msgSql; }
131
		} else {
132 1948 darkviper
			$msg[] = $oDb->get_error();
133 1916 Luisehahne
		}
134
	}
135
	return;
136
}
137
/* -------------------------------------------------------- */
138
139
function isDropletFile($sFileName)
140
{
141
	$bRetval = false;
142
	$matches = array();
143
	if(($sFileData = file_get_contents($sFileName)) !== false)
144
	{
145
//		$sPattern = "#(?://:)+[\w]*\w?#is";
146
//		$sPattern = "#//:[\w].+#imS";
147
		$sPattern = "#//:#im";
148
		$bRetval = (bool)preg_match_all($sPattern, $sFileData, $matches, PREG_SET_ORDER);
149
	}
150
	return $bRetval;
151
}
152
153
/* -------------------------------------------------------- */
154
	function getDropletFromFiles($sBaseDir)
155
	{
156
		$aRetval = array();
157
		$oIterator = new DirectoryIterator($sBaseDir);
158
		foreach ($oIterator as $fileInfo) {
159
		// iterate the directory
160
			if($fileInfo->isDot()) continue;
161
			$sFileName = rtrim(str_replace('\\', '/', $fileInfo->getPathname()), '/');
162
			if($fileInfo->isFile()) {
163
			// only droplets are interesting
164
				if((file_exists($sFileName) && isDropletFile($sFileName))) {
165
				// if dir has no corresponding accessfile remember it
166
					$aRetval[] = $sFileName;
167
				}
168
			}
169
		}
170
		return $aRetval;
171
	}