PageRenderTime 82ms CodeModel.GetById 13ms app.highlight 51ms RepoModel.GetById 1ms app.codeStats 1ms

/system/cp/cp.utilities.php

https://github.com/danboy/Croissierd
PHP | 5636 lines | 3811 code | 1272 blank | 553 comment | 490 complexity | 26e0bc0fa448a1550a0a0c14d0e6633d MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1<?php
   2
   3/*
   4=====================================================
   5 ExpressionEngine - by EllisLab
   6-----------------------------------------------------
   7 http://expressionengine.com/
   8-----------------------------------------------------
   9 Copyright (c) 2003 - 2010 EllisLab, Inc.
  10=====================================================
  11 THIS IS COPYRIGHTED SOFTWARE
  12 PLEASE READ THE LICENSE AGREEMENT
  13 http://expressionengine.com/docs/license.html
  14=====================================================
  15 File: cp.utilities.php
  16-----------------------------------------------------
  17 Purpose: Utilities
  18=====================================================
  19*/
  20
  21if ( ! defined('EXT'))
  22{
  23    exit('Invalid file request');
  24}
  25
  26
  27class Utilities {
  28
  29
  30    /** -------------------------------------------
  31    /**  Extensions Manager
  32    /** -------------------------------------------*/
  33
  34	function extensions_manager($message = '')
  35	{
  36        global $DSP, $IN, $PREFS, $LANG, $DB, $FNS, $EXT;
  37        
  38        $debug					= TRUE;
  39        $extension_files		= array();
  40        $extensions_installed	= array();
  41		
  42		/** ---------------------------------------
  43		/**  Extensions Available
  44		/** ---------------------------------------*/
  45		
  46		$i = 0;
  47		if ($fp = @opendir(PATH_EXT))
  48        { 
  49            while (false !== ($file = readdir($fp)))
  50            {
  51                if (substr($file, -strlen(EXT)) == EXT && substr($file, 0, 4) == 'ext.') 
  52                {
  53					$extension_files[$i] = substr($file, 4, -strlen(EXT));
  54					
  55					$i++;
  56                }
  57            }         
  58			
  59			closedir($fp); 
  60        }
  61        
  62        /** ---------------------------------------
  63		/**  Extensions Enabled
  64		/** ---------------------------------------*/
  65        
  66        $query = $DB->query("SELECT class, version FROM exp_extensions WHERE enabled = 'y'");
  67        
  68        if ($query->num_rows > 0)
  69        {
  70        	foreach($query->result as $row) $extensions_installed[strtolower($row['class'])] = $row['version'];
  71        }
  72        
  73		/** ---------------------------------------
  74		/**  Create Output
  75		/** ---------------------------------------*/
  76		
  77		$DSP->crumbline = FALSE;
  78		
  79		if ($PREFS->ini('allow_extensions') == 'y')
  80		{
  81			$DSP->right_crumb($LANG->line('disable_extensions'), BASE.AMP.'C=admin'.AMP.'M=utilities'.AMP.'P=toggle_extension_confirm'.AMP.'which=disable');
  82		}
  83		else
  84		{
  85			$DSP->right_crumb($LANG->line('enable_extensions'), BASE.AMP.'C=admin'.AMP.'M=utilities'.AMP.'P=toggle_extension_confirm'.AMP.'which=enable');
  86    	}
  87    	
  88        $r = $DSP->qdiv('tableHeading', $LANG->line('extensions_manager'));
  89    	             
  90        if ($message != '')
  91        {
  92            $r .= $DSP->qdiv('success', $message);
  93        }
  94        
  95        // List of Extensions Table
  96        
  97        $r .= $DSP->table('tableBorder', '0', '0', '100%');              
  98  
  99        if (count($extension_files) == 0)
 100        {
 101            $r .= $DSP->tr().
 102                  $DSP->td('tableCellTwo', '', '2').
 103                  '<b>'.$LANG->line('no_extensions_exist').'</b>'.
 104                  $DSP->td_c().
 105                  $DSP->tr_c();
 106        }
 107        else
 108        {
 109        	$r .=	$DSP->tr().
 110              		$DSP->td('tableHeadingAlt', '55%').
 111              		$LANG->line('extension_name').
 112              		$DSP->td_c().
 113              		$DSP->td('tableHeadingAlt', '15%').
 114              		$LANG->line('documentation').
 115              		$DSP->td_c().
 116              		$DSP->td('tableHeadingAlt', '15%').
 117              		$LANG->line('settings').
 118              		$DSP->td_c().
 119              		$DSP->td('tableHeadingAlt', '15%').
 120              		$LANG->line('status').
 121              		$DSP->td_c().
 122              		$DSP->tr_c();
 123        }
 124
 125        $i = 0;
 126        
 127        if (count($extension_files) > 0)
 128        {
 129        	$extension_meta  = array('description', 'settings_exist', 'docs_url', 'name', 'version');
 130        	$qm 			 = ($PREFS->ini('force_query_string') == 'y') ? '' : '?';
 131        	
 132            foreach ($extension_files as $extension_name)
 133            {
 134				$style = ($i++ % 2) ? 'tableCellOne' : 'tableCellTwo';
 135				
 136				/** ------------------------------------
 137				/**  Load Extension and Fetch Metadata
 138				/** ------------------------------------*/
 139				
 140				$meta 		= array();
 141				$class_name = ucfirst($extension_name);
 142				
 143				if ( ! class_exists($class_name))
 144        		{
 145        			if ($debug === TRUE)
 146        			{
 147        				include(PATH_EXT.'ext.'.$extension_name.EXT);
 148        			}
 149        			else
 150        			{
 151        				@include(PATH_EXT.'ext.'.$extension_name.EXT);
 152        			}
 153        			
 154        			if ( ! class_exists($class_name)) continue;
 155        		}
 156        			
 157        		$OBJ = new $class_name();
 158        			
 159        		foreach($extension_meta as $meta_item)
 160        		{
 161        			${$meta_item} = ( ! isset($OBJ->{$meta_item})) ? '' : $OBJ->{$meta_item};
 162        		}
 163        		
 164        		if ($name == '')
 165        		{
 166        			$name = ucwords(str_replace('_',' ',$extension_name));
 167        		}
 168        		
 169        		/** ------------------------------------
 170				/**  Different Output depending on current status
 171				/** ------------------------------------*/
 172				
 173				if ($PREFS->ini('allow_extensions') == 'y' && isset($extensions_installed[$extension_name]))
 174				{
 175					// Double check that the extension is up to date
 176					// If not, then quickly run the update script to make
 177					// sure that we are up to date before changing any settings
 178					
 179					if ($OBJ->version > $EXT->version_numbers[$class_name] && method_exists($OBJ, 'update_extension') === TRUE)
 180        			{
 181        				$update = $OBJ->update_extension($EXT->version_numbers[$class_name]);
 182        				
 183        				$EXT->version_numbers[$class_name] = $OBJ->version;
 184    	    		}
 185				
 186					$installed = $LANG->line('extension_enabled') . ' ('.$DSP->anchor(BASE.AMP.'C=admin'.
 187																		 AMP.'M=utilities'.
 188																		 AMP.'P=toggle_extension'.
 189																		 AMP.'which=disable'.
 190																		 AMP.'name='.$extension_name,
 191																		 $LANG->line('disable_extension'),
 192																		 "onclick='if(!confirm(\"".
 193																		$LANG->line('toggle_extension_confirmation').
 194																		"\")) return false;'").')';
 195																		 
 196					$link = $DSP->qspan('defaultBold', $name).' (v.'.$version.')';
 197					
 198					if ($description != '' && $description != '')
 199					{
 200						$link .= NL.$DSP->br().NL.$description;
 201					}
 202					
 203					$settings_link = $DSP->anchor(BASE.AMP.'C=admin'.
 204											  AMP.'M=utilities'.
 205											  AMP.'P=extension_settings'.
 206											  AMP.'name='.$extension_name,
 207											  $LANG->line('settings'));
 208				}
 209				else
 210				{
 211				
 212					$link = $DSP->qspan('defaultLight', $name.' (v.'.$version.')');
 213				
 214					if ($PREFS->ini('allow_extensions') == 'y')
 215					{
 216						$installed = $LANG->line('extension_disabled') . ' ('.$DSP->anchor(BASE.AMP.'C=admin'.
 217																		 AMP.'M=utilities'.
 218																		 AMP.'P=toggle_extension'.
 219																		 AMP.'which=enable'.
 220																		 AMP.'name='.$extension_name,
 221																		 $LANG->line('enable_extension'),
 222																		 "onclick='if(!confirm(\"".
 223																		$LANG->line('toggle_extension_confirmation').
 224																		"\")) return false;'").')';
 225					}
 226					else
 227					{
 228						$installed = $LANG->line('extension_disabled');
 229					}
 230					
 231					$settings_link = $DSP->qspan('defaultLight', $LANG->line('settings'));
 232				}
 233											  
 234				if ($docs_url != '')
 235				{
 236					$docs_url = $DSP->anchor($FNS->fetch_site_index().$qm.'URL='.urlencode($docs_url), $LANG->line('documentation'), '', TRUE);
 237				}
 238					
 239				$r .= 	$DSP->tr()
 240						.	$DSP->table_qcell($style, $link, '55%')
 241						.	$DSP->table_qcell($style, ($docs_url == '') ? '--' : $docs_url, '15%')
 242						.	$DSP->table_qcell($style, ($settings_exist != 'y') ? '--' : $settings_link, '15%')
 243						.	$DSP->table_qcell($style, $installed, '15%')
 244						.$DSP->tr_c();
 245						
 246				unset($OBJ);
 247            }
 248        }
 249        
 250        $r .= $DSP->table_c();
 251        
 252        $DSP->title  = $LANG->line('extensions_manager');
 253        $DSP->crumb  = $DSP->anchor(BASE.AMP.'C=admin'.AMP.'area=utilities', $LANG->line('utilities')).
 254					   $DSP->crumb_item($LANG->line('extensions_manager'));
 255        $DSP->body   = $r;  
 256	}
 257	/* END */
 258	
 259	
 260	/** -------------------------------------------
 261    /**  Extension Settings Form
 262    /** -------------------------------------------*/
 263
 264	function extension_settings($message = '')
 265	{
 266        global $DSP, $IN, $PREFS, $LANG, $DB, $FNS, $REGX;
 267        
 268		if ($PREFS->ini('allow_extensions') != 'y')
 269		{
 270			return $DSP->no_access_message();
 271		}
 272		
 273        if ($IN->GBL('name') === FALSE OR ! preg_match("/^[a-z0-9][\w.-]*$/i",$IN->GBL('name'))) return false;
 274        
 275        $class_name = ucfirst($IN->GBL('name'));
 276        $current	= array();
 277        
 278        /** ---------------------------------------
 279		/**  Extensions Enabled
 280		/** ---------------------------------------*/
 281        
 282        $query = $DB->query("SELECT settings FROM exp_extensions 
 283        					 WHERE enabled = 'y' AND class = '".$DB->escape_str($class_name)."'
 284        					 LIMIT 1");
 285        
 286        if ($query->num_rows > 0 && $query->row['settings'] != '')
 287        {
 288        	$current = $REGX->array_stripslashes(unserialize($query->row['settings']));
 289        }
 290        
 291        /** -----------------------------
 292    	/**  Call Extension File
 293    	/** -----------------------------*/
 294        
 295        if ( ! class_exists($class_name))
 296        {
 297        	@include(PATH_EXT.'ext.'.$IN->GBL('name').EXT);
 298        			
 299        	if ( ! class_exists($class_name)) return false;
 300        }
 301        			
 302        $OBJ = new $class_name();
 303        
 304        foreach(array('description', 'settings_exist', 'docs_url', 'name', 'version') as $meta_item)
 305        {
 306        	${$meta_item} = ( ! isset($OBJ->{$meta_item})) ? '' : $OBJ->{$meta_item};
 307        }
 308        		
 309        if ($name == '')
 310        {
 311        	$name = ucwords(str_replace('_',' ',$extension_name));
 312        }
 313        
 314        // -----------------------------------
 315    	//  Fetch Extension Language file
 316    	//
 317    	//  If there are settings, then there is a language file
 318    	//  because we need to know all the various variable names in the settings
 319    	//  form.  I was tempted to give these language files a prefix but I 
 320    	//  decided against it for the sake of simplicity and the fact that 
 321    	//  a module might have extension's bundled with them and it would make
 322    	//  sense to have the same language file for both.
 323    	// -----------------------------------
 324
 325		$LANG->fetch_language_file($IN->GBL('name'));
 326		
 327		/** ---------------------------------------
 328		/**  Creating Their Own Settings Form?
 329		/** ---------------------------------------*/
 330		
 331		if (method_exists($OBJ, 'settings_form') === TRUE)
 332		{
 333			return $OBJ->settings_form($current);
 334		}
 335		
 336		/** ---------------------------------------
 337		/**  Right Crumb Tab
 338		/** ---------------------------------------*/
 339		
 340		$DSP->crumbline = TRUE;
 341		
 342		$DSP->right_crumb($LANG->line('disable_extension'), BASE.AMP.'C=admin'.AMP.'M=utilities'.AMP.'P=toggle_extension_confirm'.AMP.'which=disable'.AMP.'name='.$IN->GBL('name'));
 343    	
 344    	/** -----------------------------
 345    	/**  Create Page's Content
 346    	/** -----------------------------*/
 347    	
 348       	$r = $DSP->table('', '', '', '100%')
 349             .$DSP->tr()
 350             .$DSP->td('default', '', '', '', 'top')
 351             .$DSP->heading($LANG->line('extension_settings'));
 352             
 353        $qm		= ($PREFS->ini('force_query_string') == 'y') ? '' : '?';
 354        $docs	= ($docs_url == '') ? '' : ' ['.$DSP->anchor($FNS->fetch_site_index().$qm.'URL='.urlencode($docs_url), $LANG->line('documentation'), '', TRUE).']';
 355
 356        $r .= $DSP->td_c()
 357             .$DSP->td('default', '', '', '', 'middle')
 358             .$DSP->qdiv('defaultRight', '<strong>'.$docs.'</strong>'.NBS.NBS)
 359             .$DSP->td_c()
 360             .$DSP->tr_c()
 361             .$DSP->tr()
 362             .$DSP->td('default', '100%', '2', '', 'top');
 363             
 364        $r .= Utilities::extension_settings_form($name, $IN->GBL('name'), $OBJ->settings(), $current);
 365             
 366		$r .=  $DSP->td_c()
 367			  .$DSP->tr_c()
 368			  .$DSP->table_c()
 369			  .$DSP->td_c()
 370			  .$DSP->tr_c()
 371			  .$DSP->table_c();
 372			  
 373		$DSP->title  = $LANG->line('extension_settings');
 374        $DSP->crumb  = $DSP->anchor(BASE.AMP.'C=admin'.AMP.'area=utilities', $LANG->line('utilities')).
 375					   $DSP->crumb_item($DSP->anchor(BASE.AMP.'C=admin'.AMP.'M=utilities'.AMP.'P=extensions_manager', $LANG->line('extensions_manager')));
 376        $DSP->crumb .= $DSP->crumb_item($name);
 377        $DSP->body   = $r;  
 378	}
 379	/* END */
 380	
 381	
 382	/** -----------------------------
 383    /**  Store Extension Settings
 384    /** -----------------------------*/
 385    
 386    function save_extension_settings()
 387    {
 388		global $IN, $DB, $FNS;
 389		
 390		// Basic security check
 391       	if ( ! preg_match("/^[a-z0-9][\w.-]*$/i",$IN->GBL('name'))) return false;
 392       	
 393       	if ( ! class_exists(ucfirst($IN->GBL('name'))))
 394     	{
 395     		include(PATH_EXT.'ext.'.strtolower($IN->GBL('name')).EXT);
 396		}
 397		
 398		// Ok, I admit that we should be able to simply unset the 'name' value
 399		// from the $_POST array and simply insert that into the database.
 400		// I, Paul Burdick of the Sinister House geeks, am slowly becoming
 401		// anal retentive in my young age and decided to make sure only those
 402		// settings specified by the extension are inserted AND that there is
 403		// always an empty string in the really rare chance that one is not
 404		// specified.
 405        			
 406        if (class_exists(ucfirst($IN->GBL('name'))))
 407        {
 408        	$class_name = ucfirst($IN->GBL('name'));
 409        				
 410        	$OBJ = new $class_name();
 411        	
 412        	/** ---------------------------------------
 413			/**  Processing Their Own Settings Form?
 414			/** ---------------------------------------*/
 415			
 416			if (method_exists($OBJ, 'settings_form') === TRUE)
 417			{
 418				$OBJ->save_settings();
 419				
 420				$FNS->redirect(BASE.AMP.'C=admin'.AMP.'M=utilities'.AMP.'P=extensions_manager');
 421				exit;
 422			}
 423        				
 424        	if (method_exists($OBJ, 'settings') === TRUE)
 425        	{
 426        		$settings = $OBJ->settings();
 427        	}
 428        	
 429        	$insert = array();
 430        	
 431        	foreach($settings as $key => $value)
 432        	{
 433        		if ( ! is_array($value))
 434        		{
 435        			$insert[$key] = ($IN->GBL($key, 'POST') !== FALSE) ? $IN->GBL($key, 'POST') : $value;
 436        		}
 437        		elseif (is_array($value) && isset($value['1']) && is_array($value['1']))
 438        		{
 439        			if(is_array($IN->GBL($key, 'POST')) OR $value[0] == 'ms')
 440        			{
 441        				$data = (is_array($IN->GBL($key, 'POST'))) ? $IN->GBL($key, 'POST') : array();
 442        				
 443        				$data = array_intersect($data, array_keys($value['1']));
 444        			}
 445        			else
 446        			{
 447        				if ($IN->GBL($key, 'POST') === FALSE)
 448        				{
 449        					$data = ( ! isset($value['2'])) ? '' : $value['2'];
 450        				}
 451        				else
 452        				{
 453        					$data = $IN->GBL($key, 'POST');
 454        				}
 455        			}
 456        			
 457        			$insert[$key] = $data;
 458        		}
 459        		else
 460        		{
 461        			$insert[$key] = ($IN->GBL($key, 'POST') !== FALSE) ? $IN->GBL($key, 'POST') : '';
 462        		}
 463        	}
 464        	
 465			$DB->query("UPDATE exp_extensions SET settings = '".addslashes(serialize($insert))."' WHERE class = '".$DB->escape_str($IN->GBL('name'))."'");
 466		}
 467		
 468		$FNS->redirect(BASE.AMP.'C=admin'.AMP.'M=utilities'.AMP.'P=extensions_manager');
 469		exit;
 470	}
 471	/* END */
 472	
 473	
 474	
 475	/** -----------------------------
 476    /**  Create Form Automagically
 477    /** -----------------------------*/
 478    
 479    function extension_settings_form($extension_name, $name, $fdata, $data)
 480    {
 481    	global $DSP, $LANG;				
 482
 483		$r  =	$DSP->form_open(
 484									array(
 485											'action' => 'C=admin'.AMP.'M=utilities'.AMP.'P=save_extension_settings',
 486											'name'	=> 'settings_'.$name,
 487											'id'	=> 'settings_'.$name
 488										),
 489										
 490									array('name' => $name)
 491								);
 492		
 493		
 494		$r .=	$DSP->table('tableBorder', '0', '', '100%');
 495		$r .=	$DSP->tr();
 496		$r .=   $DSP->td('tableHeadingAlt', '', '2');
 497		$r .=   $extension_name;
 498		$r .=   $DSP->td_c();
 499		$r .=	$DSP->tr_c();
 500		
 501		$i = 0;
 502		
 503		/** -----------------------------
 504		/**  Blast through the array
 505		/** -----------------------------*/
 506				
 507		foreach ($fdata as $key => $val)				
 508		{		
 509			$style = ($i % 2) ? 'tableCellOne' : 'tableCellTwo'; $i++;
 510			
 511			$default_data = (is_array($val)) ? '' : $val;
 512			
 513			$data[$key] = ( ! isset($data[$key])) ? $default_data : $data[$key];
 514			
 515			if (!is_array($val) || $val['0'] != 'sf')
 516			{
 517				$r .=	$DSP->tr();
 518				
 519				// If the form type is a textarea, we'll align the text at the top, otherwise, we'll center it
 520			
 521				if (is_array($val) && ($val['0'] == 't' OR $val['0'] == 'ms' OR ($val['0'] == 'c' && sizeof($val['1']) > 1)))
 522				{
 523					$r .= $DSP->td($style, '50%', '', '', 'top');
 524				}
 525				else
 526				{
 527					$r .= $DSP->td($style, '50%', '');
 528				}
 529				
 530				/** -----------------------------
 531				/**  Preference heading
 532				/** -----------------------------*/
 533			
 534				$r .= $DSP->div('defaultBold');
 535						
 536				$label = ( ! is_array($val)) ? $key : '';
 537				
 538				// Fix for array form variables like cat_id[]
 539				// Such names to do no work well with the
 540				// translation utility sadly.
 541				
 542				if (($LANG->line($key) === false || $LANG->line($key) == '') && strpos($key, '[]') !== false)
 543				{
 544					if ($LANG->line(str_replace('[]','',$key)) === FALSE)
 545					{
 546						$r .= '<label for="'.$key.'">'.ucwords(str_replace('_', ' ', $key))."</label>";
 547					}
 548					else
 549					{
 550						$r .= $LANG->line(str_replace('[]','',$key), $label);
 551					}
 552				}
 553				else
 554				{
 555					$r .= $LANG->line($key, $label);
 556				}
 557	
 558				$r .= $DSP->div_c();
 559			
 560			
 561				/** -----------------------------
 562				/**  Preference sub-heading
 563				/** -----------------------------*/
 564			
 565				$r .= $DSP->td_c();
 566			
 567				/** -----------------------------
 568				/**  Preference value
 569				/** -----------------------------*/
 570				
 571				$r .= $DSP->td($style, '50%', '');
 572			}
 573			
 574				if (is_array($val))
 575				{
 576					/** -----------------------------
 577					/**  Drop-down menus
 578					/** -----------------------------*/
 579								
 580					if ($val['0'] == 's' || $val['0'] == 'ms')
 581					{
 582						$multi = ($val['0'] == 'ms') ? "class='multiselect' size='8' multiple='multiple'" : "class='select'";
 583						$nkey = ($val['0'] == 'ms') ? $key.'[]' : $key;
 584					
 585						if (isset($val['2']))
 586						{
 587							$r .= "<select name='{$nkey}' $multi ".$val['2'].">\n";
 588						}
 589						else
 590						{
 591							$r .= "<select name='{$nkey}' $multi >\n";
 592						}
 593						
 594						$data[$key] = ($data[$key] == '') ? $val['2'] : $data[$key];
 595						
 596						foreach ($val['1'] as $k => $v)
 597						{
 598							if ($val['0'] == 's' || ! is_array($data[$key]))
 599							{
 600								$selected = ($k == $data[$key]) ? 1 : '';
 601							}
 602							elseif(is_array($data[$key]))
 603							{
 604								$selected = (in_array($k,$data[$key])) ? 1 : '';								
 605							}
 606						
 607							$name = ($LANG->line($v) == false OR $key == 'weblog_id') ? $v : $LANG->line($v);
 608						
 609							$r .= $DSP->input_select_option($k, $name, $selected);
 610						}
 611						
 612						$r .= $DSP->input_select_footer();
 613						
 614					} 
 615					elseif ($val['0'] == 'r')
 616					{
 617						/** -----------------------------
 618						/**  Radio buttons
 619						/** -----------------------------*/
 620						
 621						if ( ! isset($val['2']))
 622						{
 623							$val['2'] = '';
 624						}
 625						
 626						$data[$key] = ($data[$key] == '') ? $val['2'] : $data[$key];
 627					
 628						foreach ($val['1'] as $k => $v)
 629						{
 630							$selected = ($k == $data[$key]) ? 1 : '';
 631						
 632							$r .= $LANG->line($v).$DSP->nbs();
 633							$r .= $DSP->input_radio($key, $k, $selected, ( ! isset($val['3'])) ? '' : $val['3']).$DSP->nbs(3);
 634						}					
 635					}
 636					elseif ($val['0'] == 'c')
 637					{
 638						/** -----------------------------
 639						/**  Checkboxes
 640						/** -----------------------------*/
 641						
 642						if ( ! isset($val['2']))
 643						{
 644							$val['2'] = '';
 645						}
 646						
 647						$data[$key] = ($data[$key] == '') ? $val['2'] : $data[$key];
 648					
 649						foreach ($val['1'] as $k => $v)
 650						{
 651							$selected = ($k == $data[$key]) ? 1 : '';
 652							
 653							if (sizeof($val['1']) == 1)
 654							{
 655								$r .= $DSP->input_checkbox($key, $k, $selected);
 656							}
 657							else
 658							{
 659								$r .= $DSP->qdiv('publishPad', $DSP->input_checkbox($key, $k, $selected).' '.$LANG->line($v));
 660							}							
 661						}					
 662					}
 663					elseif ($val['0'] == 't')
 664					{
 665						/** -----------------------------
 666						/**  Textarea fields
 667						/** -----------------------------*/
 668						
 669						// The "kill_pipes" index instructs us to 
 670						// turn pipes into newlines
 671						
 672						$data[$key] = ($data[$key] == '') ? $val['1'] : $data[$key];
 673						
 674						if (isset($val['2']['kill_pipes']) AND $val['2']['kill_pipes'] === TRUE)
 675						{
 676							$text	= '';
 677							
 678							foreach (explode('|', $data[$key]) as $exp)
 679							{
 680								$text .= $exp.NL;
 681							}
 682						}
 683						else
 684						{
 685							$text = $data[$key];
 686						}
 687												
 688						$rows = (isset($val['2']['rows'])) ? $val['2']['rows'] : '15';
 689						
 690						$r .= $DSP->input_textarea($key, $text, $rows);
 691						
 692					}
 693					elseif ($val['0'] == 'f' || $val['0'] == 'sf')
 694					{
 695						switch($val['1'])
 696						{
 697							case 'new_table' :  
 698							
 699								$i = 0;
 700								// Close current tables
 701								$r .= $DSP->table_c();	
 702								
 703								$r .= $DSP->div_c();
 704								
 705								// Open new table
 706								$r .= $DSP->div('', '', $key);
 707																
 708								$r .= $DSP->table('tableBorder', '0', '', '100%');
 709								$r .= $DSP->tr();
 710								$r .= $DSP->td('tableHeadingAlt', '', '2');
 711								$r .= $LANG->line($val['2']);
 712								$r .= $DSP->td_c();
 713								$r .= $DSP->tr_c();	
 714						
 715							break;
 716						}
 717					}
 718				}
 719				else
 720				{
 721					/** -----------------------------
 722					/**  Text input fields
 723					/** -----------------------------*/
 724				
 725					$r .= $DSP->input_text($key, $data[$key], '20', '120', 'input', '100%');
 726				}
 727				
 728			$r .= $DSP->td_c();
 729			$r .= $DSP->tr_c();
 730		}
 731				
 732		$r .= $DSP->table_c();
 733		$r .= $DSP->div_c();
 734		$r .= $DSP->qdiv('itemWrapperTop', $DSP->input_submit($LANG->line('submit'), 'submit'));
 735		
 736		return $r;
 737    }
 738    /* END */
 739	
 740	
 741	/** -------------------------------------------
 742    /**  Plugin Manager
 743    /** -------------------------------------------*/
 744
 745	// Helper function used to sort plugins
 746	function _plugin_title_sorter($a, $b)
 747	{
 748		return strnatcasecmp($a['title'], $b['title']);
 749	}
 750
 751	function plugin_manager($message = '')
 752	{
 753        global $DSP, $IN, $PREFS, $LANG, $FNS;
 754     
 755		if ( ! @include_once(PATH_LIB.'pclzip.lib.php'))
 756		{
 757			return $DSP->no_access_message('PclZip Library does not appear to be installed.  It is required.');
 758		}     
 759     
 760		$is_writable = (is_writable(PATH_PI) && $PREFS->ini('demo_date') == FALSE) ? TRUE : FALSE;
 761        
 762        $plugins = array();
 763		$info 	= array();
 764		
 765		if ($fp = @opendir(PATH_PI))
 766        { 
 767            while (false !== ($file = readdir($fp)))
 768            {
 769            	if ( preg_match("/^pi\.[a-z\_0-9]+?".preg_quote(EXT, '/')."$/", $file))
 770            	{
 771					if ( ! @include_once(PATH_PI.$file))
 772					{
 773						continue;
 774                	}
 775                	
 776                    $name = str_replace('pi.', '', $file);
 777                	$name = str_replace(EXT, '', $name);
 778                                    
 779					$plugins[] = $name;
 780					                    
 781                    $info[$name] = $plugin_info;
 782                }
 783            }         
 784			
 785			closedir($fp); 
 786        } 	
 787
 788  		if ( in_array('magpie', $plugins) && $PREFS->ini('demo_date') == FALSE)
 789      		$r = '<div style="float: left; width: 69%; margin-right: 2%;">';
 790      	else
 791      		$r = '<div style="float: left; width: 100%;">';
 792  		        
 793		if ($is_writable)
 794		{
 795              $r .= $DSP->form_open(
 796              							array(
 797              									'action' => 'C=admin'.AMP.'M=utilities'.AMP.'P=plugin_remove_conf', 
 798              									'name'	=> 'target',
 799              									'id'	=> 'target'
 800              								)
 801              						);
 802              $r .= $DSP->toggle();
 803        }
 804        
 805        if ($message != '')
 806        {
 807        	$r .= $DSP->qdiv('itemWrapper', $DSP->qdiv('highlight', $message));
 808        }
 809              
 810        $r .= $DSP->table('tableBorder', '0', '10', '100%').
 811              $DSP->tr().
 812              $DSP->td('tableHeading', ($is_writable) ? '97%' : '100%', '').
 813              count($plugins).' '.$LANG->line('plugin_installed').
 814              $DSP->td_c();
 815              
 816		if ($is_writable)
 817		{
 818			$r .= $DSP->td('tableHeading', '3%', '').
 819				  $DSP->input_checkbox('toggleflag', '', '', "onclick=\"toggle(this);\"").
 820				  $DSP->td_c();
 821		}
 822		
 823		$r .= $DSP->tr_c();
 824  
 825        if (count($plugins) == 0)
 826        {
 827            $r .= $DSP->tr().
 828                  $DSP->td('tableCellTwo', '', '2').
 829                  '<b>'.$LANG->line('no_plugins_exist').'</b>'.
 830                  $DSP->td_c().
 831                  $DSP->tr_c();
 832        }  
 833
 834        $i = 0;
 835        
 836        if (count($plugins) > 0)
 837        {
 838            foreach ($plugins as $plugin)
 839            {
 840				$version = '(v.'.trim($info[$plugin]['pi_version']).')';
 841				$update = '';
 842				
 843				$style = ($i++ % 2) ? 'tableCellOne' : 'tableCellTwo';
 844				
 845				$name = $DSP->qspan('defaultBold', $DSP->anchor(BASE.AMP.'C=admin'.AMP.'M=utilities'.AMP.'P=plugin_info'.AMP.'name='.$plugin, $info[$plugin]['pi_name']));
 846				$description = $info[$plugin]['pi_description'];
 847				
 848				$r .= $DSP->tr();
 849				
 850				$r .= $DSP->table_qcell($style, $name.' '.$version.' '.$update.$DSP->br().$description, ($is_writable) ? '85%' : '100%');
 851		  
 852				if ($is_writable)
 853				{
 854					$r .= $DSP->table_qcell($style, $DSP->input_checkbox('toggle[]', $plugin), '15%');
 855				}
 856				
 857				$r .= $DSP->tr_c();
 858            }
 859        }
 860        
 861        $r .= $DSP->table_c();
 862             
 863		if ($is_writable)
 864		{
 865             $r .= $DSP->div('itemWrapper', 'right')
 866				 .$DSP->input_submit($LANG->line('plugin_remove'))
 867				 .$DSP->div_c()
 868				 .$DSP->form_close();
 869		}
 870		
 871		$r .= $DSP->div_c();
 872
 873             
 874        /** -------------------------------------------
 875        /**  Latest Plugin Table
 876        /** -------------------------------------------*/
 877        
 878        // Do we have the Magpie plugin so we can parse the EE plugin RSS feed?
 879        if (in_array('magpie', $plugins) && $PREFS->ini('demo_date') == FALSE)
 880        {
 881        	$request = 'http://expressionengine.com/feeds/pluginlist/';
 882    		
 883    		$target = parse_url($request);
 884			
 885			$fp = @fsockopen($target['host'], 80, $errno, $errstr, 15);
 886			
 887			$code = '';
 888			
 889			if (is_resource($fp))
 890			{
 891				fputs ($fp,"GET " . $request . " HTTP/1.0\r\n" ); 
 892				fputs ($fp,"Host: " . $target['host'] . "\r\n" ); 
 893				fputs ($fp,"User-Agent: EE/EllisLab PHP/" . phpversion() . "\r\n\r\n");
 894				
 895				$getting_headers = true;
 896			
 897				while ( ! feof($fp))
 898				{
 899					$line = fgets($fp, 4096);
 900					
 901					if ($getting_headers == false)
 902					{
 903						$code .= $line;
 904					}
 905					elseif (trim($line) == '')
 906					{
 907						$getting_headers = false;
 908					}
 909				}
 910	
 911				@fclose($fp);    	
 912			}
 913            
 914            $plugins = new MagpieRSS($code);
 915			
 916			$i = 0;
 917			
 918			if (count($plugins->items) > 0)
 919			{
 920				// Example pagination: &perpage=10&page=10&sortby=alpha
 921				$paginate = '';
 922				$extra = ''; // Will hold sort method
 923				$total_rows = count($plugins->items);
 924				$perpage = ( ! $IN->GBL('perpage')) ? 10 : $IN->GBL('perpage');
 925				$page = ( ! $IN->GBL('page')) ? 0 : $IN->GBL('page');
 926				$sortby = ( ! $IN->GBL('sortby')) ? '' : $IN->GBL('sortby');
 927				$base = BASE.AMP.'C=admin'.AMP.'M=utilities'.AMP.'P=plugin_manager';				
 928				
 929				if ($sortby == 'alpha')
 930				{
 931					usort($plugins->items, array('Utilities', '_plugin_title_sorter'));
 932					$extra = AMP.'sortby=alpha';
 933					$link = $DSP->anchor($base, $LANG->line('plugin_by_date'));
 934					$title = $LANG->line('plugins').$DSP->qspan('defaultSmall', $LANG->line('plugin_by_letter').' : '.$link);
 935				}
 936				else
 937				{
 938					$link = $DSP->anchor($base.AMP.'sortby=alpha', $LANG->line('plugin_by_letter'));
 939					$title = $LANG->line('plugins').$DSP->qspan('defaultSmall', $LANG->line('plugin_by_date').' : '.$link);
 940				}
 941
 942				$ten_plugins = array_slice($plugins->items, $page, $perpage-1);
 943				
 944				// Latest Plugins Table
 945				$r .= '<div style="float: left; width: 29%; clear: right;">';
 946				
 947				$r .= $DSP->table('tableBorder', '0', '10', '100%').
 948					$DSP->tr().
 949					$DSP->td('tableHeadingAlt', '', '').
 950					$title.
 951					$DSP->td_c().
 952					$DSP->tr_c();
 953				
 954				$curl_installed = ( ! extension_loaded('curl') || ! function_exists('curl_init')) ? FALSE : TRUE;
 955				
 956				$qm = ($PREFS->ini('force_query_string') == 'y') ? '' : '?';	
 957				
 958				foreach ($ten_plugins as $item)
 959				{
 960					$attr = explode('|', $item['dc']['subject']);
 961					$dl = $attr[0];
 962					$version = '(v.'.$attr[1].')';
 963					$require = ( ! $attr[2] ) ? '' : $DSP->br().$DSP->qspan('highlight', $LANG->line('plugin_requires').': '.$attr[2]);
 964					
 965					$name = $DSP->qspan('defaultBold', $DSP->anchor($FNS->fetch_site_index().$qm.'URL='.$item['link'], $item['title']));
 966					$description = $FNS->word_limiter($item['description'], '20');
 967					
 968					$install = ( ! class_exists('PclZip') || ! $is_writable || ! $curl_installed) ? '' : $DSP->anchor(BASE.AMP.'C=admin'.AMP.'M=utilities'.AMP.'P=plugin_install'.AMP.'file='.$dl, '<span style=\'color:#009933;\'>'.$LANG->line('plugin_install').'</span>');
 969					
 970					$style = ($i++ % 2) ? 'tableCellOne' : 'tableCellTwo';
 971					
 972					$r .= $DSP->tr();
 973					
 974					$r .= $DSP->table_qcell($style, $name.' '.$version.$DSP->nbs().$require.$DSP->qdiv('itemWrapper', $description).$install, '60%');
 975
 976					$r .= $DSP->tr_c();
 977				}
 978				
 979				$r .= $DSP->table_c();
 980					
 981				if ($total_rows > $perpage)
 982				{		 
 983					$paginate = $DSP->pager(  BASE.AMP.'C=admin'.AMP.'M=utilities'.AMP.'P=plugin_manager'.$extra.AMP.'perpage='.$perpage,
 984											  $total_rows, 
 985											  $perpage,
 986											  $page,
 987											  'page'
 988											);
 989				}
 990				
 991				$r .= $DSP->qdiv('itemWrapper', $paginate.BR.BR);
 992				$r .= $DSP->div_c();
 993			}
 994			
 995		}
 996                
 997        $DSP->title  = $LANG->line('plugin_manager');
 998        $DSP->crumb  = $DSP->anchor(BASE.AMP.'C=admin'.AMP.'area=utilities', $LANG->line('utilities')).
 999					   $DSP->crumb_item($LANG->line('plugin_manager'));
1000        $DSP->body   = $r;  
1001	}
1002	/* END */
1003
1004
1005    /** -------------------------------------------
1006    /**  Plugin Info
1007    /** -------------------------------------------*/
1008
1009    function plugin_info()
1010    {
1011		global $IN, $DSP, $LANG, $FNS, $PREFS;
1012		
1013		// Basic security check
1014		if ( ! preg_match("/^[a-z0-9][\w.-]*$/i",$IN->GBL('name'))) return false;
1015		
1016		$name = $IN->GBL('name');
1017		
1018		if ( ! @include(PATH_PI.'pi.'.$name.EXT))
1019		{
1020			return $DSP->error_message('Unable to load the following plugin: '.$name.EXT);
1021		}     
1022		
1023		$qm = ($PREFS->ini('force_query_string') == 'y') ? '' : '?';
1024			                
1025        $DSP->title  = ucwords(str_replace("_", " ", $name));
1026        $DSP->crumb  = $DSP->anchor(BASE.AMP.'C=admin'.AMP.'area=utilities', $LANG->line('utilities')).
1027					   $DSP->crumb_item($DSP->anchor(BASE.AMP.'C=admin'.AMP.'M=utilities'.AMP.'P=plugin_manager', $LANG->line('plugin_manager'))).
1028					   $DSP->crumb_item(ucwords(str_replace("_", " ", $name)));
1029
1030        $i = 0;
1031        
1032        $r  = $DSP->table('tableBorder', '0', '10', '100%').
1033              $DSP->tr().
1034              $DSP->td('tableHeading', '', '2').
1035              $LANG->line('plugin_information').
1036              $DSP->td_c().
1037              $DSP->tr_c();  
1038             		
1039		if ( ! isset($plugin_info) OR ! is_array($plugin_info))
1040		{
1041			$style = ($i++ % 2) ? 'tableCellOne' : 'tableCellTwo';
1042			
1043			$name = ucwords(str_replace("_", " ", $name));
1044
1045			$r .= $DSP->tr();
1046			$r .= $DSP->table_qcell($style, $DSP->qspan('defaultBold', $LANG->line('pi_name')), '30%');
1047			$r .= $DSP->table_qcell($style, $DSP->qspan('defaultBold', $name), '70%');
1048			$r .= $DSP->tr_c();
1049			
1050			$style = ($i++ % 2) ? 'tableCellOne' : 'tableCellTwo';
1051
1052			$r .= $DSP->tr();
1053			$r .= $DSP->td($style, '', '2').$DSP->qspan('default', $LANG->line('no_additional_info'));
1054			$r .= $DSP->td_c();
1055			$r .= $DSP->tr_c();
1056              
1057        }
1058        else
1059        {        
1060			foreach ($plugin_info as $key => $val)
1061			{ 
1062				$style = ($i++ % 2) ? 'tableCellOne' : 'tableCellTwo';
1063				
1064				$item = ($LANG->line($key) != FALSE) ? $LANG->line($key) : ucwords(str_replace("_", " ", $key));
1065				
1066				if ($key == 'pi_author_url')
1067				{
1068					if (substr($val, 0, 4) != "http") 
1069						$val = "http://".$val; 
1070						
1071						$val = $DSP->anchor($FNS->fetch_site_index().$qm.'URL='.$val, $val, '', 1);
1072				}
1073				
1074				if ($key == 'pi_usage')
1075					$val = nl2br(htmlspecialchars($val));
1076					
1077				$r .= $DSP->tr();
1078				$r .= $DSP->table_qcell($style, $DSP->qspan('defaultBold', $item), '30%', 'top');
1079				$r .= $DSP->table_qcell($style, $DSP->qspan('default', $val), '70%');
1080				$r .= $DSP->tr_c();
1081			}
1082  		}
1083
1084        $r .= $DSP->table_c();
1085        
1086		$DSP->body = $r;
1087	}
1088	/* END */
1089	
1090	/** -------------------------------------------
1091    /**  Plugin Extraction from ZIP file
1092    /** -------------------------------------------*/
1093	
1094	function plugin_install()
1095	{		
1096        global $IN, $DSP, $LANG, $PREFS;
1097        
1098		if ($PREFS->ini('demo_date') != FALSE)
1099		{
1100            return $DSP->no_access_message();
1101		}
1102		
1103		if ( ! @include_once(PATH_LIB.'pclzip.lib.php'))
1104		{
1105			return $DSP->error_message($LANG->line('plugin_zlib_missing'));
1106		}
1107		
1108		if ( ! is_writable(PATH_PI))
1109		{
1110			return $DSP->error_message($LANG->line('plugin_folder_not_writable'));
1111		}
1112		
1113		if ( ! extension_loaded('curl') || ! function_exists('curl_init'))
1114		{
1115			return $DSP->error_message($LANG->line('plugin_no_curl_support'));
1116		}
1117        
1118        $file = $IN->GBL('file');
1119                
1120        $local_name = basename($file);
1121        $local_file = PATH_PI.$local_name;
1122        
1123		// Get the remote file
1124		$c = curl_init($file);
1125		curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
1126		
1127		// prevent a PHP warning on certain servers
1128		if (! ini_get('safe_mode') && ! ini_get('open_basedir'))
1129		{
1130		    curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
1131		}
1132		
1133		$code = curl_exec($c);
1134		curl_close($c);
1135	    
1136	    $file_info = pathinfo($local_file);
1137	
1138	    if ($file_info['extension'] == 'txt' ) // Get rid of any notes/headers in the TXT file
1139        {
1140			$code = strstr($code, '<?php');
1141		}
1142	    
1143	    if ( ! $fp = fopen($local_file, 'wb'))
1144	    {
1145			return $DSP->error_message($LANG->line('plugin_problem_creating_file'));
1146	    }
1147	    
1148	    flock($fp, LOCK_EX);
1149	    fwrite($fp, $code);
1150	    flock($fp, LOCK_UN);
1151	    fclose($fp);
1152
1153	    @chmod($local_file, 0777);
1154	    
1155        // Check file information so we know what to do with it
1156        
1157		if ($file_info['extension'] == 'txt' ) // We've got a TXT file!
1158        {
1159			$new_file = basename($local_file, '.txt');
1160			if ( ! rename($local_file, PATH_PI.$new_file))
1161			{
1162				$message = $LANG->line('plugin_install_other');
1163			}
1164			else
1165			{
1166				@chmod($new_file, 0777);
1167				$message = $LANG->line('plugin_install_success');
1168			}
1169        }
1170        else if ($file_info['extension'] == 'zip' ) // We've got a ZIP file!
1171        {
1172        	// Unzip and install plugin
1173			if (class_exists('PclZip'))
1174			{
1175				$zip = new PclZip($local_file);
1176				chdir(PATH_PI);
1177				$ok = @$zip->extract('');
1178				
1179				if ($ok)
1180				{
1181					$message = $LANG->line('plugin_install_success');
1182					unlink($local_file);
1183				}
1184				else
1185				{
1186					$message = $LANG->line('plugin_error_uncompress');
1187				}
1188				
1189				chdir(PATH);
1190			}
1191			else
1192			{
1193				$message = $LANG->line('plugin_error_no_zlib');
1194			}
1195        }
1196        else
1197        {
1198        		$message = $LANG->line('plugin_install_other');
1199        }
1200		
1201		return Utilities::plugin_manager($message);
1202
1203	}
1204	/* END */
1205	
1206	
1207	/** -------------------------------------------
1208    /**  Plugin Removal Confirmation
1209    /** -------------------------------------------*/
1210    
1211    function plugin_remove_confirm()
1212    {
1213        global $IN, $DSP, $LANG, $PREFS;
1214
1215		if ($PREFS->ini('demo_date') != FALSE)
1216		{
1217            return $DSP->no_access_message();
1218		}
1219		
1220        $r  = $DSP->form_open(array('action' => 'C=admin'.AMP.'M=utilities'.AMP.'P=plugin_remove'));
1221        
1222        $i = 0;
1223        
1224        foreach ($_POST as $key => $val)
1225        {        
1226            if (strstr($key, 'toggle') AND ! is_array($val))
1227            {
1228                $r .= $DSP->input_hidden('deleted[]', $val);
1229                
1230                $i++;
1231            }        
1232        }
1233                
1234        $message = ($i == 1) ? 'plugin_single_confirm' : 'plugin_multiple_confirm';
1235                
1236		$r	.= 	 $DSP->qdiv('alertHeading', $LANG->line('plugin_delete_confirm'))
1237				.$DSP->div('box')
1238				.$DSP->qdiv('itemWrapper', '<b>'.$LANG->line($message).'</b>');
1239				
1240		$r .=	 $DSP->qdiv('itemWrapper', $DSP->qdiv('alert', $LANG->line('action_can_not_be_undone')))
1241				.$DSP->qdiv('itemWrapper', BR.$DSP->input_submit($LANG->line('deinstall')).BR)
1242				.$DSP->div_c()
1243				.$DSP->form_close();
1244				
1245		
1246        
1247        $DSP->title = $LANG->line('plugin_delete_confirm');
1248        $DSP->crumb = $DSP->anchor(BASE.AMP.'C=admin'.AMP.'area=utilities', $LANG->line('utilities')).
1249					  $DSP->crumb_item($DSP->anchor(BASE.AMP.'C=admin'.AMP.'M=utilities'.AMP.'P=plugin_manager', $LANG->line('plugin_manager'))).
1250					  $DSP->crumb_item($LANG->line('plugin_delete_confirm'));         
1251        $DSP->body  = $r;
1252    }
1253    
1254    /** -------------------------------------------
1255    /**  Plugin Removal
1256    /** -------------------------------------------*/
1257    
1258    function plugin_remove()
1259    {
1260        global $IN, $DSP, $LANG, $PREFS;
1261        
1262		if ($PREFS->ini('demo_date') != FALSE)
1263		{
1264            return $DSP->no_access_message();
1265		}
1266		
1267        $deleted = $IN->GBL('deleted');
1268        $message = '';
1269        $style = '';
1270        $i = 0;
1271        
1272        $DSP->title  = $LANG->line('plugin_removal');
1273        $DSP->crumb  = $DSP->anchor(BASE.AMP.'C=admin'.AMP.'area=utilities', $LANG->line('utilities')).
1274					   $DSP->crumb_item($DSP->anchor(BASE.AMP.'C=admin'.AMP.'M=utilities'.AMP.'P=plugin_manager', $LANG->line('plugin_manager'))).
1275					   $DSP->crumb_item($LANG->line('plugin_removal'));
1276        
1277        $r  = $DSP->table('tableBorder', '0', '10', '100%').
1278              $DSP->tr().
1279              $DSP->td('tableHeading', '', '').
1280              $LANG->line('plugin_removal_status').
1281              $DSP->td_c().
1282              $DSP->tr_c();
1283        
1284        foreach ( $deleted as $name )
1285        {
1286        		$style = ($i++ % 2) ? 'tableCellOne' : 'tableCellTwo';
1287        	
1288            if (unlink(PATH_PI.'pi.'.$name.'.php'))
1289				$message = $LANG->line('plugin_removal_success').' '.ucwords(str_replace("_", " ", $name));
1290			else
1291				$message = $LANG->line('plugin_removal_error').' '.ucwords(str_replace("_", " ", $name)).'.';
1292				
1293			$r .= $DSP->tr();
1294       		$r .= $DSP->table_qcell($style, $DSP->qdiv('itemWrapper', $DSP->qdiv('highlight', $message)), '100%');
1295        		$r .= $DSP->tr_c();
1296        }
1297        
1298        $r .= $DSP->table_c();
1299        
1300		$DSP->body = $r;
1301    }
1302    /* END */
1303    
1304    
1305	/** -------------------------------------------
1306    /**  Disable Extensions Confirmation
1307    /** -------------------------------------------*/
1308    
1309    function toggle_extension_confirm()
1310    {
1311        global $IN, $DSP, $LANG;
1312        
1313        // Basic security check
1314        if ($IN->GBL('name') !== FALSE && ! preg_match("/^[a-z0-9][\w.-]*$/i",$IN->GBL('name'))) return false;
1315
1316        $r  = $DSP->form_open(array('action' => 'C=admin'.AMP.'M=utilities'.AMP.'P=toggle_extension'));
1317        
1318       	if ($IN->GBL('which') == 'enable')
1319        {
1320        	$message = ($IN->GBL('name') !== FALSE) ? 'enable_extension_conf' : 'enable_extensions_conf';
1321        	
1322        	$r .= $DSP->input_hidden('which', 'enable');
1323        }
1324        else
1325        {
1326        	$message = ($IN->GBL('name') !== FALSE) ? 'disable_extension_conf' : 'disable_extensions_conf';
1327        	
1328        	$r .= $DSP->input_hidden('which', 'disable');
1329        }
1330        
1331        $r .= $DSP->input_hidden('name', ($IN->GBL('name') !== FALSE) ? $IN->GBL('name') : '');
1332                
1333		$r	.= 	 $DSP->qdiv('alertHeading', $LANG->line($message))
1334				.$DSP->div('box')
1335				.$DSP->qdiv('itemWrapper', '<b>'.$LANG->line('toggle_extension_confirmation').'</b>');
1336				
1337		$r .=	 $DSP->qdiv('itemWrapper', BR.$DSP->input_submit($LANG->line('submit')).BR)
1338				.$DSP->div_c()
1339				.$DSP->form_close();
1340				
1341		
1342        
1343        $DSP->title = $LANG->line('extensions_manager');
1344        $DSP->crumb = $DSP->anchor(BASE.AMP.'C=admin'.AMP.'area=utilities', $LANG->line('utilities')).
1345					  $DSP->crumb_item($DSP->anchor(BASE.AMP.'C=admin'.AMP.'M=utilities'.AMP.'P=extensions_manager', $LANG->line('extensions_manager'))).
1346					  $DSP->crumb_item($LANG->line($message));         
1347        $DSP->body  = $r;
1348    }
1349    
1350    /** -------------------------------------------
1351    /**  Toggle Extension/s
1352    /** -------------------------------------------*/
1353    
1354    function toggle_extension()
1355    {
1356        global $IN, $FNS, $DB, $PREFS;
1357
1358        $message = '';
1359        
1360        if ($IN->GBL('name') !== FALSE && $IN->GBL('name') != '')
1361        {
1362        	// Basic security check
1363       		if ( ! preg_match("/^[a-z0-9][\w.-]*$/i",$IN->GBL('name'))) return false;
1364        
1365        	// Disable/Enable Single Extension
1366        	
1367        	if ($IN->GBL('which') == 'enable')
1368        	{
1369        		// Check if the Extension is already installed and just disabled
1370        		// If so we just turn it back on.  If not, we have to activate
1371        		// the extension.  We have the enabled field so that if someone
1372        		// disables a parameter we can still have the extension's settings
1373        		// in the database and not lost to the ether.
1374        		
1375        		$query = $DB->query("SELECT COUNT(*) AS count FROM exp_extensions WHERE class = '".$DB->escape_str(ucfirst($IN->GBL('name')))."'");
1376        	
1377        		if ($query->row['count'] == 0)
1378        		{
1379        			if ( ! class_exists(ucfirst($IN->GBL('name'))))
1380        			{
1381        				include(PATH_EXT.'ext.'.$IN->GBL('name').EXT);
1382        			}
1383        			
1384        			if (class_exists(ucfirst($IN->GBL('name'))))
1385        			{
1386        				$class_name = ucfirst($IN->GBL('name'));
1387        				
1388        				$OBJ = new $class_name();
1389        				
1390        				if (method_exists($OBJ, 'activate_extension') === TRUE)
1391        				{
1392        					$activate = $OBJ->activate_extension();
1393        				}
1394        			}
1395        		}
1396        		else
1397        		{
1398        			$DB->query("UPDATE exp_extensions SET enabled = 'y' WHERE class = '".$DB->escape_str(ucfirst($IN->GBL('name')))."'");
1399        		}
1400        	}
1401        	else
1402        	{
1403        		$DB->query("UPDATE exp_extensions SET enabled = 'n' WHERE class = '".$DB->escape_str(ucfirst($IN->GBL('name')))."'");
1404        		
1405        		if ( ! class_exists(ucfirst($IN->GBL('name'))))
1406				{
1407					include(PATH_EXT.'ext.'.$IN->GBL('name').EXT);
1408				}
1409				
1410				if (class_exists(ucfirst($IN->GBL('name'))))
1411				{
1412					$class_name = ucfirst($IN->GBL('name'));
1413					
1414					$OBJ = new $class_name();
1415					
1416					if (method_exists($OBJ, 'disable_extension') === TRUE)
1417					{
1418						$disable = $OBJ->disable_extension();
1419					}
1420				}
1421        	}
1422        }
1423        else
1424        {
1425        	// Disable/Enable All Extensions
1426        	
1427        	if ($IN->GBL('which') == 'enable')
1428        	{
1429        		Admin::update_config_file(array('allow_extensions' => "y"));
1430        	}
1431        	else
1432        	{
1433        		Admin::update_config_file(array('allow_extensions' => "n"));
1434        	}
1435        }
1436        
1437        $FNS->redirect(BASE.AMP.'C=admin'.AMP.'M=utilities'.AMP.'P=extensions_manager');
1438		exit;
1439    }
1440    /* END */
1441    
1442
1443    /** -------------------------------------------
1444    /**  SQL Manager
1445    /** -------------------------------------------*/
1446
1447    function sql_info()
1448    {
1449        global $DB, $DSP, $PREFS, $LOC, $LANG;
1450        
1451		$i = 0;
1452		$style_one 	= 'tableCellOne';
1453		$style_two 	= 'tableCellTwo';
1454			
1455        
1456        $query = $DB->query("SELECT version() AS ver");
1457		
1458        $DSP->title = $LANG->line('utilities');
1459        $DSP->crumb = $DSP->anchor(BASE.AMP.'C=admin'.AMP.'area=utilities', $LANG->line('utilities')).
1460					  $DSP->crumb_item($LANG->line('sql_manager'));
1461                                
1462        $DSP->body = $DSP->qdiv('tableHeading', $LANG->line('sql_manager'));
1463              
1464		/** -----------------------------
1465    	/**  Table Header
1466    	/** -----------------------------*/
1467
1468        $DSP->body	.=	$DSP->table('tableBorder', '0', '0', '100%').
1469						$DSP->tr().
1470						$DSP->table_qcell('tableHeadingAlt', 
1471											array(
1472													$LANG->line('sql_info'),
1473													$LANG->line('value')
1474												 )
1475											).
1476						$DSP->tr_c();
1477						
1478					
1479		/** -------------------------------------------
1480		/**  Database Type
1481		/** -------------------------------------------*/
1482  		
1483		$DSP->body	.=	$DSP->table_qrow( ($i++ % 2) ? $style_one : $style_two, 
1484										array(
1485												$DSP->qspan('defaultBold', $LANG->line('database_type')),
1486												$PREFS->ini('db_type')
1487											  )
1488										);			
1489  		
1490  		
1491  		
1492		/** -------------------------------------------
1493		/**  SQL Version
1494		/** -------------------------------------------*/
1495  		
1496		$query = $DB->query("SELECT version() AS ver");
1497				
1498		$DSP->body	.=	$DSP->table_qrow( ($i++ % 2) ? $style_one : $style_two, 
1499										array(
1500												$DSP->qspan('defaultBold', $LANG->line('sql_version')),
1501												$query->row['ver']
1502											  )
1503										);	
1504										
1505										
1506
1507        $DB->fetch_fields = TRUE;
1508
1509        $query = $DB->query("SHOW TABLE STATUS FROM `".$PREFS->ini('db_name')."`");
1510
1511		$totsize = 0;
1512		$records = 0;
1513		
1514		$prelen = strlen($DB->prefix);
1515		
1516        foreach ($query->result as $val)
1517        {
1518            if (strncmp($val['Name'], $DB->prefix, $prelen) != 0)
1519            {
1520                continue;
1521            }
1522                                
1523            $totsize += ($val['Data_length'] + $val['Index_length']);
1524            $records += $val['Rows'];
1525        }
1526
1527			
1528		/** -------------------------------------------
1529		/**  Database Records
1530		/** -------------------------------------------*/
1531						
1532		$DSP->body	.=	$DSP->table_qrow( ($i++ % 2) ? $style_one : $style_two, 
1533										array(
1534												$DSP->qspan('defaultBold', $LANG->line('records')),
1535												$records
1536											  )
1537										);
1538			
1539		/** -------------------------------------------
1540		/**  Database Size
1541		/** -------------------------------------------*/
1542
1543        $size = Utilities::byte_format($totsize);
1544        
1545		$DSP->body	.=	$DSP->table_qrow( ($i++ % 2) ? $style_one : $style_two, 
1546										array(
1547												$DSP->qspan('defaultBold', $LANG->line('database_size')),
1548												$size['0'].' '.$size['1']
1549											  )
1550										);
1551
1552			
1553		/** -------------------------------------------
1554		/**  Database Uptime
1555		/** -------------------------------------------*/
1556			
1557        $query = $DB->query("SHOW STATUS");
1558		
1559		$uptime  = '';
1560		$queries = '';
1561				
1562		foreach ($query->result as $key => $val)
1563		{
1564            foreach ($val as $v)
1565            {
1566				if (preg_match("#^uptime#i", $v))
1567				{
1568					$uptime = $key;
1569				}
1570				
1571				if (preg_match("#^questions#i", $v))
1572				{
1573					$queries = $key;
1574				}
1575			}		
1576		}    
1577		
1578				                   
1579		$DSP->body	.=	$DSP->table_qrow( ($i++ % 2) ? $style_one : $style_two, 
1580										array(
1581												$DSP->qspan('defaultBold', $LANG->line('database_uptime')),
1582												$LOC->format_timespan($query->result[$uptime]['Value'])
1583											  )
1584										);			
1585       						
1586		/** -------------------------------------------
1587		/**  Total Server Queries
1588		/** -------------------------------------------*/
1589       						
1590       /*
1591		$DSP->body	.=	$DSP->table_qrow( ($i++ % 2) ? $style_one : $style_two, 
1592										array(
1593												$DSP->qspan('defaultBold', $LANG->line('total_queries')),
1594												number_format($query->result[$queries]['Value'])
1595											  )
1596										);			
1597		*/
1598		
1599		
1600        $DSP->body	.=	$DSP->table_c(); 
1601        
1602		/** -------------------------------------------
1603		/**  SQL Utilities
1604		/** -------------------------------------------*/
1605       				
1606        $DSP->body	.=	$DSP->table('tableBorder', '0', '0', '100%').
1607						$DSP->tr().
1608						$DSP->table_qcell('tableHeading', 
1609											array(
1610													$LANG->line('sql_utilities'),
1611												 )
1612											).
1613						$DSP->tr_c();
1614						
1615
1616		$DSP->body	.=	$DSP->table_qrow( ($i++ % 2) ? $style_one : $style_two, 
1617										array(
1618												$DSP->qspan('defaultBold', $DSP->anchor(BASE.AMP.'C=admin'.AMP.'M=utilities'.AMP.'P=view_database', $LANG->line('view_database')))
1619											  )
1620										);			
1621       						
1622       /*
1623		$DSP->body	.=	$DSP->table_qrow( ($i++ % 2) ? $style_one : $style_two, 
1624										array(
1625												$DSP->qspan('defaultBold', $DSP->anchor(BASE.AMP.'C=admin'.AMP.'M=utilities'.AMP.'P=sql_backup', $LANG->line('sql_backup')))
1626											  )
1627										);			
1628		*/
1629		$DSP->body	.=	$DSP->table_qrow( ($i++ % 2) ? $style_one : $style_two, 
1630										array(
1631												$DSP->qspan('defaultBold', $DSP->anchor(BASE.AMP.'C=admin'.AMP.'M=utilities'.AMP.'P=sql_query', $LANG->line('sql_query')))
1632											  )
1633										);			
1634
1635
1636		$DSP->body	.=	$DSP->table_qrow( ($i++ % 2) ? $style_one : $style_two, 
1637										array(
1638												$DSP->qspan('defaultBold', $DSP->anchor(BASE.AMP.'C=admin'.AMP.'M=utilities'.AMP.'P=sql_status', $LANG->line('sql_status')))
1639											  )
1640										);			
1641
1642
1643		$DSP->body	.=	$DSP->table_qrow( ($…

Large files files are truncated, but you can click here to view the full file