| 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 |  |  | 	$database=WbDatabase::getInstance();
 | 
      
        | 76 |  |  | 	foreach ($aDropletFiles as $sDropletFile) {
 | 
      
        | 77 |  |  | 		$msgSql = '';
 | 
      
        | 78 |  |  | 		$extraSql = '';
 | 
      
        | 79 |  |  | 		$sDropletName = pathinfo ($sDropletFile, PATHINFO_FILENAME);
 | 
      
        | 80 |  |  | 		$sql = 'SELECT `code` FROM `'.$database->TablePrefix.'mod_droplets` WHERE `name` LIKE "'.$sDropletName.'" ';
 | 
      
        | 81 |  |  | 		if( !($database->get_one($sql)) ) {
 | 
      
        | 82 |  |  | 			$sql = 'INSERT INTO `'.$database->TablePrefix.'mod_droplets`';
 | 
      
        | 83 |  |  | 			$msgSql = 'INSERT Droplet `'.$sDropletName.'` INTO`'.$database->TablePrefix.'mod_droplets`'." $OK";
 | 
      
        | 84 |  |  | 		} elseif ($bOverwriteDroplets) {
 | 
      
        | 85 |  |  | 			$sql = 'UPDATE `'.$database->TablePrefix.'mod_droplets` ';
 | 
      
        | 86 |  |  | 			$extraSql = 'WHERE `name` = \''.$sDropletName.'\' ';
 | 
      
        | 87 |  |  | 			$msgSql = 'UPDATE Droplet `'.$sDropletName.'` INTO`'.$database->TablePrefix.'mod_droplets`'." $OK";
 | 
      
        | 88 |  |  | 		}
 | 
      
        | 89 |  |  | // get description, comments and oode
 | 
      
        | 90 |  |  | 		$sDropletFile = preg_replace('/^\xEF\xBB\xBF/', '', $sDropletFile);
 | 
      
        | 91 |  |  | 		if( ($msgSql!='') && ($aFileData = file($sDropletFile)) ) {
 | 
      
        | 92 |  |  | 			$bDescription = false;
 | 
      
        | 93 |  |  | 			$bComments = false;
 | 
      
        | 94 |  |  | 			$bCode = false;
 | 
      
        | 95 |  |  | 			$sDescription = '';
 | 
      
        | 96 |  |  | 			$sComments = '';
 | 
      
        | 97 |  |  | 			$sCode = '';
 | 
      
        | 98 |  |  | 			$sPattern = "#//:#im";
 | 
      
        | 99 |  |  | 			while ( sizeof($aFileData) > 0 ) {
 | 
      
        | 100 |  |  | 				$sSqlLine = trim(array_shift($aFileData));
 | 
      
        | 101 |  |  | 				$isNotCode = (bool)preg_match($sPattern, $sSqlLine);
 | 
      
        | 102 |  |  | 				if( $isNotCode==true ) {
 | 
      
        | 103 |  |  | // first step line is description
 | 
      
        | 104 |  |  | 					if($bDescription==false) {
 | 
      
        | 105 |  |  | 						$sDescription .= str_replace('//:','',$sSqlLine);
 | 
      
        | 106 |  |  | 						$bDescription = true;
 | 
      
        | 107 |  |  | 					} else {
 | 
      
        | 108 |  |  | // second step fill comments
 | 
      
        | 109 |  |  | 						$sComments .= str_replace('//:','',$sSqlLine).PHP_EOL;
 | 
      
        | 110 |  |  | 					}
 | 
      
        | 111 |  |  | 				} else {
 | 
      
        | 112 |  |  | // third step fill code
 | 
      
        | 113 |  |  | 					$sCode .= str_replace('//:','',$sSqlLine).PHP_EOL;
 | 
      
        | 114 |  |  | 				}
 | 
      
        | 115 |  |  | 			}
 | 
      
        | 116 |  |  | 		$iModifiedWhen = time();
 | 
      
        | 117 |  |  | 		$iModifiedBy = (method_exists($admin, 'get_user_id') && ($admin->get_user_id()!=null) ? $admin->get_user_id() : 1);
 | 
      
        | 118 |  |  | 		$sql .= 'SET  `name` =\''.$sDropletName.'\','
 | 
      
        | 119 |  |  | 		     .       '`description` =\''.$sDescription.'\','
 | 
      
        | 120 |  |  | 		     .       '`comments` =\''.$sComments.'\','
 | 
      
        | 121 |  |  | 		     .       '`code` =\''.$database->escapeString($sCode).'\','
 | 
      
        | 122 |  |  | 		     .       '`modified_when` = '.$iModifiedWhen.','
 | 
      
        | 123 |  |  | 		     .       '`modified_by` = '.$iModifiedBy.','
 | 
      
        | 124 |  |  | 		     .       '`active` = 1'
 | 
      
        | 125 |  |  | 		     .       $extraSql;
 | 
      
        | 126 |  |  | 		}
 | 
      
        | 127 |  |  | 		if( $database->query($sql) ) {
 | 
      
        | 128 |  |  | 			if( $msgSql!='' ) { $msg[] = $msgSql; }
 | 
      
        | 129 |  |  | 		} else {
 | 
      
        | 130 |  |  | 			$msg[] = $database->get_error();
 | 
      
        | 131 |  |  | 		}
 | 
      
        | 132 |  |  | 	}
 | 
      
        | 133 |  |  | 	return;
 | 
      
        | 134 |  |  | }
 | 
      
        | 135 |  |  | /* -------------------------------------------------------- */
 | 
      
        | 136 |  |  | 
 | 
      
        | 137 |  |  | function isDropletFile($sFileName)
 | 
      
        | 138 |  |  | {
 | 
      
        | 139 |  |  | 	$bRetval = false;
 | 
      
        | 140 |  |  | 	$matches = array();
 | 
      
        | 141 |  |  | 	if(($sFileData = file_get_contents($sFileName)) !== false)
 | 
      
        | 142 |  |  | 	{
 | 
      
        | 143 |  |  | //		$sPattern = "#(?://:)+[\w]*\w?#is";
 | 
      
        | 144 |  |  | //		$sPattern = "#//:[\w].+#imS";
 | 
      
        | 145 |  |  | 		$sPattern = "#//:#im";
 | 
      
        | 146 |  |  | 		$bRetval = (bool)preg_match_all($sPattern, $sFileData, $matches, PREG_SET_ORDER);
 | 
      
        | 147 |  |  | 	}
 | 
      
        | 148 |  |  | 	return $bRetval;
 | 
      
        | 149 |  |  | }
 | 
      
        | 150 |  |  | 
 | 
      
        | 151 |  |  | /* -------------------------------------------------------- */
 | 
      
        | 152 |  |  | 	function getDropletFromFiles($sBaseDir)
 | 
      
        | 153 |  |  | 	{
 | 
      
        | 154 |  |  | 		$aRetval = array();
 | 
      
        | 155 |  |  | 		$oIterator = new DirectoryIterator($sBaseDir);
 | 
      
        | 156 |  |  | 		foreach ($oIterator as $fileInfo) {
 | 
      
        | 157 |  |  | 		// iterate the directory
 | 
      
        | 158 |  |  | 			if($fileInfo->isDot()) continue;
 | 
      
        | 159 |  |  | 			$sFileName = rtrim(str_replace('\\', '/', $fileInfo->getPathname()), '/');
 | 
      
        | 160 |  |  | 			if($fileInfo->isFile()) {
 | 
      
        | 161 |  |  | 			// only droplets are interesting
 | 
      
        | 162 |  |  | 				if((file_exists($sFileName) && isDropletFile($sFileName))) {
 | 
      
        | 163 |  |  | 				// if dir has no corresponding accessfile remember it
 | 
      
        | 164 |  |  | 					$aRetval[] = $sFileName;
 | 
      
        | 165 |  |  | 				}
 | 
      
        | 166 |  |  | 			}
 | 
      
        | 167 |  |  | 		}
 | 
      
        | 168 |  |  | 		return $aRetval;
 | 
      
        | 169 |  |  | 	}
 |