| 1 | 4 | ryan | <?php
 | 
      
        | 2 | 1392 | Luisehahne | /**
 | 
      
        | 3 |  |  |  *
 | 
      
        | 4 |  |  |  * @category        admin
 | 
      
        | 5 |  |  |  * @package         templates
 | 
      
        | 6 | 1712 | Luisehahne |  * @author          Ryan Djurovich, WebsiteBaker Project
 | 
      
        | 7 |  |  |  * @copyright       2009-2012, WebsiteBaker Org. e.V.
 | 
      
        | 8 | 1392 | Luisehahne |  * @link			http://www.websitebaker2.org/
 | 
      
        | 9 |  |  |  * @license         http://www.gnu.org/licenses/gpl.html
 | 
      
        | 10 |  |  |  * @platform        WebsiteBaker 2.8.x
 | 
      
        | 11 |  |  |  * @requirements    PHP 5.2.2 and higher
 | 
      
        | 12 |  |  |  * @version         $Id$
 | 
      
        | 13 | 1457 | Luisehahne |  * @filesource		$HeadURL$
 | 
      
        | 14 |  |  |  * @lastmodified    $Date$
 | 
      
        | 15 | 1392 | Luisehahne |  *
 | 
      
        | 16 |  |  |  */
 | 
      
        | 17 | 4 | ryan | 
 | 
      
        | 18 | 1457 | Luisehahne | // Setup admin object
 | 
      
        | 19 |  |  | require('../../config.php');
 | 
      
        | 20 | 2098 | darkviper | $oDb = WbDatabase::getInstance();
 | 
      
        | 21 |  |  | $oTrans = Translate::getInstance();
 | 
      
        | 22 |  |  | $oTrans->enableAddon('admin\\addons');
 | 
      
        | 23 | 1457 | Luisehahne | // suppress to print the header, so no new FTAN will be set
 | 
      
        | 24 |  |  | $admin = new admin('Addons', 'templates_uninstall', false);
 | 
      
        | 25 |  |  | if( !$admin->checkFTAN() )
 | 
      
        | 26 |  |  | {
 | 
      
        | 27 | 1467 | Luisehahne | 	$admin->print_header();
 | 
      
        | 28 | 2098 | darkviper | 	$admin->print_error($oTrans->MESSAGE_GENERIC_SECURITY_ACCESS);
 | 
      
        | 29 | 1457 | Luisehahne | }
 | 
      
        | 30 |  |  | // After check print the header
 | 
      
        | 31 |  |  | $admin->print_header();
 | 
      
        | 32 | 1712 | Luisehahne | if(!isset($_POST['file']) OR $_POST['file'] == "") {
 | 
      
        | 33 | 2098 | darkviper | 	$admin->print_error($oTrans->MESSAGE_GENERIC_FORGOT_OPTIONS);
 | 
      
        | 34 | 1712 | Luisehahne | } else {
 | 
      
        | 35 |  |  | 	$file = preg_replace('/[^a-z0-9_-]/i', "", $_POST['file']);  // fix secunia 2010-92-2
 | 
      
        | 36 |  |  | }
 | 
      
        | 37 | 1457 | Luisehahne | 
 | 
      
        | 38 | 1712 | Luisehahne | // Check if the template exists
 | 
      
        | 39 |  |  | if(!is_dir(WB_PATH.'/templates/'.$file)) {
 | 
      
        | 40 | 2098 | darkviper | 	$admin->print_error($oTrans->MESSAGE_GENERIC_NOT_INSTALLED);
 | 
      
        | 41 | 1712 | Luisehahne | }
 | 
      
        | 42 |  |  | 
 | 
      
        | 43 |  |  | // Check if the template exists
 | 
      
        | 44 |  |  | if(!is_readable(WB_PATH.'/templates/'.$file)) {
 | 
      
        | 45 | 2098 | darkviper | 	$admin->print_error($oTrans->MESSAGE_ADMIN_INSUFFICIENT_PRIVELLIGES);
 | 
      
        | 46 | 1712 | Luisehahne | }
 | 
      
        | 47 |  |  | 
 | 
      
        | 48 | 4 | ryan | // Check if user selected template
 | 
      
        | 49 | 1712 | Luisehahne | /*
 | 
      
        | 50 | 4 | ryan | if(!isset($_POST['file']) OR $_POST['file'] == "") {
 | 
      
        | 51 |  |  | 	header("Location: index.php");
 | 
      
        | 52 | 286 | stefan | 	exit(0);
 | 
      
        | 53 | 4 | ryan | } else {
 | 
      
        | 54 |  |  | 	$file = $_POST['file'];
 | 
      
        | 55 |  |  | }
 | 
      
        | 56 |  |  | 
 | 
      
        | 57 | 268 | ryan | // Extra protection
 | 
      
        | 58 |  |  | if(trim($file) == '') {
 | 
      
        | 59 |  |  | 	header("Location: index.php");
 | 
      
        | 60 | 286 | stefan | 	exit(0);
 | 
      
        | 61 | 268 | ryan | }
 | 
      
        | 62 | 1712 | Luisehahne | */
 | 
      
        | 63 | 268 | ryan | 
 | 
      
        | 64 | 4 | ryan | // Include the WB functions file
 | 
      
        | 65 |  |  | require_once(WB_PATH.'/framework/functions.php');
 | 
      
        | 66 | 862 | aldus | if (!function_exists("replace_all")) {
 | 
      
        | 67 | 2098 | darkviper | 	function replace_all($aStr = "", array $aArray = null ) {
 | 
      
        | 68 |  |  | 		foreach ($aArray as $k=>$v) {
 | 
      
        | 69 |  |  |             $aStr = str_replace("{{".$k."}}", $v, $aStr);
 | 
      
        | 70 |  |  |         }
 | 
      
        | 71 | 862 | aldus | 		return $aStr;
 | 
      
        | 72 |  |  | 	}
 | 
      
        | 73 |  |  | }
 | 
      
        | 74 |  |  | 
 | 
      
        | 75 |  |  | /**
 | 
      
        | 76 |  |  | *	Check if the template is the standard-template or still in use
 | 
      
        | 77 |  |  | */
 | 
      
        | 78 | 2098 | darkviper | $sMsgTpl = (isset($oTrans->MESSAGE_GENERIC_CANNOT_UNINSTALL_IS_DEFAULT_TEMPLATE)
 | 
      
        | 79 |  |  |             ? $oTrans->MESSAGE_GENERIC_CANNOT_UNINSTALL_IS_DEFAULT_TEMPLATE
 | 
      
        | 80 |  |  |             : 'Can\'t uninstall this template <b>{{name}}</b> because it\'s the standardtemplate!');
 | 
      
        | 81 | 944 | Ruebenwurz | // check whether the template is used as default wb theme
 | 
      
        | 82 |  |  | if($file == DEFAULT_THEME) {
 | 
      
        | 83 |  |  | 	$temp = array ('name' => $file );
 | 
      
        | 84 | 2098 | darkviper | 	$msg = replace_all( $sMsgTpl, $temp );
 | 
      
        | 85 | 944 | Ruebenwurz | 	$admin->print_error( $msg );
 | 
      
        | 86 |  |  | }
 | 
      
        | 87 |  |  | 
 | 
      
        | 88 | 862 | aldus | if ($file == DEFAULT_TEMPLATE) {
 | 
      
        | 89 | 893 | aldus | 	$temp = array ('name' => $file );
 | 
      
        | 90 | 2098 | darkviper | 	$msg = replace_all( $sMsgTpl, $temp );
 | 
      
        | 91 | 893 | aldus | 	$admin->print_error( $msg );
 | 
      
        | 92 | 862 | aldus | 
 | 
      
        | 93 | 4 | ryan | } else {
 | 
      
        | 94 | 1712 | Luisehahne | 
 | 
      
        | 95 | 862 | aldus | 	/**
 | 
      
        | 96 |  |  | 	*	Check if the template is still in use by a page ...
 | 
      
        | 97 |  |  | 	*/
 | 
      
        | 98 | 2098 | darkviper | 	$sql = 'SELECT `page_id` `id`, `page_title` `title` FROM `'.$oDb->TablePrefix.'pages` '
 | 
      
        | 99 |  |  |          . 'WHERE `template`=\''.$file.'\' '
 | 
      
        | 100 |  |  |          . 'ORDER BY `page_title`';
 | 
      
        | 101 |  |  | 	if (($info = $oDb->doQuery($sql))) {
 | 
      
        | 102 |  |  |         if (($iNumberOfRows = $info->numRows())) {
 | 
      
        | 103 |  |  |             /**
 | 
      
        | 104 |  |  |             *	Template is still in use, so we're collecting the page-titles
 | 
      
        | 105 |  |  |             *	The base-message template-string for the top of the message
 | 
      
        | 106 |  |  |             */
 | 
      
        | 107 |  |  |             if (!isset($oTrans->MESSAGE_GENERIC_CANNOT_UNINSTALL_IN_USE_TMPL)) {
 | 
      
        | 108 |  |  |                 $add = ($iNumberOfRows == 1 ? 'this page' : 'these pages');
 | 
      
        | 109 |  |  |                 $msg_template_str  = '<br /><br />{{type}} <b>{{type_name}}</b> could not be uninstalled because it is still in use by {{pages}}';
 | 
      
        | 110 |  |  |                 $msg_template_str .= ':<br /><i>click for editing.</i><br /><br />';
 | 
      
        | 111 |  |  |             } else {
 | 
      
        | 112 |  |  |                 $msg_template_str = $oTrans->MESSAGE_GENERIC_CANNOT_UNINSTALL_IN_USE_TMPL;
 | 
      
        | 113 |  |  |                 $temp = explode(";",$oTrans->MESSAGE_GENERIC_CANNOT_UNINSTALL_IN_USE_TMPL_PAGES);
 | 
      
        | 114 |  |  |                 $add = $iNumberOfRows == 1 ? $temp[0] : $temp[1];
 | 
      
        | 115 |  |  |             }
 | 
      
        | 116 |  |  |             /**
 | 
      
        | 117 |  |  |             *	The template-string for displaying the Page-Titles ... in this case as a link
 | 
      
        | 118 |  |  |             */
 | 
      
        | 119 |  |  |             $page_template_str = "- <b><a href='../pages/settings.php?page_id={{id}}'>{{title}}</a></b><br />";
 | 
      
        | 120 |  |  |             $values = array ('type' => 'Template', 'type_name' => $file, 'pages' => $add);
 | 
      
        | 121 |  |  |             $msg = replace_all($msg_template_str, $values);
 | 
      
        | 122 |  |  |             $page_names = "";
 | 
      
        | 123 |  |  |             while ($page_info = $info->fetchRow(MYSQL_ASSOC) ) {
 | 
      
        | 124 |  |  | //                $page_info = array(
 | 
      
        | 125 |  |  | //                    'id'	=> $data['page_id'],
 | 
      
        | 126 |  |  | //                    'title' => $data['page_title']
 | 
      
        | 127 |  |  | //                );
 | 
      
        | 128 |  |  |                 $page_names .= replace_all($page_template_str, $page_info);
 | 
      
        | 129 |  |  |             }
 | 
      
        | 130 |  |  |             /**
 | 
      
        | 131 |  |  |             *	Printing out the error-message and die().
 | 
      
        | 132 |  |  |             */
 | 
      
        | 133 |  |  |             $admin->print_error($oTrans->MESSAGE_GENERIC_CANNOT_UNINSTALL_IN_USE.$msg.$page_names);
 | 
      
        | 134 |  |  |         }
 | 
      
        | 135 |  |  |     }
 | 
      
        | 136 | 4 | ryan | }
 | 
      
        | 137 |  |  | 
 | 
      
        | 138 |  |  | // Check if we have permissions on the directory
 | 
      
        | 139 |  |  | if(!is_writable(WB_PATH.'/templates/'.$file)) {
 | 
      
        | 140 | 2098 | darkviper | 	$admin->print_error($oTrans->MESSAGE_GENERIC_CANNOT_UNINSTALL.WB_PATH.'/templates/'.$file);
 | 
      
        | 141 | 4 | ryan | }
 | 
      
        | 142 |  |  | 
 | 
      
        | 143 |  |  | // Try to delete the template dir
 | 
      
        | 144 |  |  | if(!rm_full_dir(WB_PATH.'/templates/'.$file)) {
 | 
      
        | 145 | 2098 | darkviper | 	$admin->print_error($oTrans->MESSAGE_GENERIC_CANNOT_UNINSTALL);
 | 
      
        | 146 | 170 | ryan | } else {
 | 
      
        | 147 |  |  | 	// Remove entry from DB
 | 
      
        | 148 | 2098 | darkviper | 	$sql = 'DELETE FROM `'.$oDb->TablePrefix.'addons` '
 | 
      
        | 149 |  |  |          . 'WHERE `directory`=\''.$file.'\' AND `type`=\'template\'';
 | 
      
        | 150 |  |  | 	$oDb->doQuery($sql);
 | 
      
        | 151 | 4 | ryan | }
 | 
      
        | 152 |  |  | // Update pages that use this template with default template
 | 
      
        | 153 | 1386 | Luisehahne | // $database = new database();
 | 
      
        | 154 | 2098 | darkviper | $sql = 'UPDATE `'.$oDb->TablePrefix.'pages` '
 | 
      
        | 155 |  |  |      . 'SET `template`=\''.DEFAULT_TEMPLATE.'\' '
 | 
      
        | 156 |  |  |      . 'WHERE `template`=\''.$file.'\'';
 | 
      
        | 157 |  |  | $oDb->doQuery($sql);
 | 
      
        | 158 | 4 | ryan | 
 | 
      
        | 159 |  |  | // Print success message
 | 
      
        | 160 | 2098 | darkviper | $admin->print_success($oTrans->MESSAGE_GENERIC_UNINSTALLED);
 | 
      
        | 161 | 4 | ryan | 
 | 
      
        | 162 |  |  | // Print admin footer
 | 
      
        | 163 |  |  | $admin->print_footer();
 |