<?php
/**
* JoomlaSEF for Joomla!
* @copyright (c) 2005 The OpenSEF Project (www.opensef.org)
* @copyright (c) 2004-2005 Xaneon Development (www.xaneon.com)
* @license GPL http://www.gnu.org/copyleft/gpl.html
*
* TODO
*
*/
/** Ensure this file is being included by a parent file */
defined( '_VALID_MOS' ) or die( 'Direct access to this location is not allowed.' );
require_once( dirname( __FILE__ ) . '/includes/sef.common.php' );
require_once( 'sef.config.php' );
require_once( 'sef.langs.php' );
xclSetIncludePath( dirname( __FILE__ ) );
/**
* Config Class
*
*/
class JosOpenSEFConfig extends xclConfig {
// What's up here?
// var $enable_security = '0';
var $enabled = '0';
var $enable_sef = '1';
var $debug = '0';
var $debug_for_ip = '';
var $backend_language = 'english';
var $backend_sidebar = '1';
var $backend_autoupdates = '0'; // Changed to '0' in beta3
var $encode_lowercase = '1';
var $encode_page_suffix = '.html';
var $encode_space_char = '-';
var $encode_strip_chars = '!,$,%,@,?,#,(,),+,*';
var $content_path_type = 'full';
var $content_title_field = 'title';
var $section_title_field = 'title';
var $category_title_field = 'title';
var $content_page_format = '%s_%d';
var $content_page_name = 'page';
var $record_unmapped_urls = '0';
var $record_invalid_urls = '0';
var $record_hits = '0'; // Hmm?
var $use_automap = '1';
var $use_automap_redirect = '1';
var $use_multisite = '0';
var $use_sef_ext = '1';
var $use_core_ext = '1';
var $use_canonical_urls = '1';
var $use_absolute_urls = '1';
var $append_itemid = '0';
var $append_date = '0';
var $append_date_format = '%Y%m%d';
var $append_contentid = '0';
var $use_session_fix = '1';
var $validate_level = '1';
var $validate_404_url = '';
var $max_recursion = '1';
var $xhtml_compliance = '0'; // for sefRelToAbs
var $cache_outgoing = '1';
var $alias_banner = 'adverse';
var $alias_links = 'links';
var $alias_newsfeeds = 'news_feeds';
var $alias_wrapper = 'external';
var $use_alias_banner = '1';
var $use_alias_links = '1';
var $use_alias_newsfeeds = '1';
var $use_alias_wrapper = '1';
var $content_blogcategory = 'blogcategory';
var $content_blogsection = 'blogsection';
var $content_archivecategory = 'archivecategory';
var $content_archivesection = 'archivesection';
var $spec_chars_d = null;
var $spec_chars = null;
var $google_filename = 'google_opensef';
var $google_path = 'components/com_sef';
var $google_modified = '1';
var $google_frequency = 'weekly';
var $google_priority = '0.8';
var $google_ping = '1';
var $google_display = '1';
var $meta_nlist = '10';
var $meta_nsep = ',';
var $meta_nsort = 'SORT_DESC';
var $meta_kreplace = '1';
var $meta_dreplace = '1';
var $meta_chars = '",.,-,?,,,.,:,!,@,#,$,%,^,&,*,(,),_,-,1,2,3,4,5,6,7,8,9,0,.,;,{,},[,],|,/,<,>,+,=,)';
var $meta_exclude_key = '{mosimage}, , ';
var $meta_gdesc = 'full';
var $meta_gdesc_opt = '1';
var $meta_gdesc_len = '400';
var $version = '2.1.1';
/**
* Load Configuration
*
* @param object $db the database connection object
* @param boole $reload
* @return JosOpenSEFConfig
*/
function JosOpenSEFConfig( &$db, $reload = true ) {
$this->xclConfig( $db, 'com_sef', $reload );
}
}
/**
* Alias Class
*
*/
class JosOpenSEFAlias extends xclDBTable {
var $id = null;
var $site_id = null;
var $external = null;
var $internal = null;
var $use_internal = 1;
var $direction = null;
var $published = null;
var $valid = null;
var $stop = null;
var $created = null;
var $created_by = null;
var $modified = null;
var $modified_by = null;
var $catid = null;
var $hits = null;
var $notes = null;
var $locked = 0;
var $link_prio = 0;
/**
* Load Aliases row
*
* @param object $db the database connection object
* @param string $id
* @return JosOpenSEFAlias
*/
function JosOpenSEFAlias( &$db, $id = null ) {
$this->xclDBTable( '#__opensef_sef', 'id', $db, $id );
}
/**
* Checkin
*
* @param string $oid
* @return boole
*/
function checkin( $oid = null ) {
return true;
}
/**
* Load Aliases
*
* @param string $oid
* @return boole or load
*/
function load( $oid = null ) {
if (!empty( $oid )) {
return parent::load( $oid );
} else {
$this->published = '1';
$this->valid = '1';
$this->stop = '0';
$this->hits = 0;
return true;
}
}
/**
* Check values
*
* @return parent check
*/
function check() {
if (!empty( $this->internal )&& sefIsJoomlaSEF( $this->internal ))
$this->internal = sefRewriteFromJoomlaSEF( $this->internal );
// Ensure empty string/number zero => null translation
if (empty( $this->site_id ))
$this->site_id = null;
if (empty( $this->direction ))
$this->direction = null;
if (empty( $this->catid ))
$this->catid = 0;
if (empty( $this->hits ))
$this->hits = 0;
$this->notes = trim( $this->notes );
if (strlen( $this->notes ) == 0)
$this->notes = null;
return parent::check();
}
/*
* TODO: do we need this anymore?
*/
function save( $source, $order_filter = null ) {
if (!$this->bind( $_POST ))
return false;
if (!$this->check())
return false;
if (!$this->store( true ))
return false;
$this->_error = '';
return true;
}
/**
* Get Siteurl
*
* @return result
*/
function getSiteURL() {
if ($this->site_id) {
$query = "SELECT CONCAT_WS('', s.host, s.base_url) FROM #__opensef_sef_site AS s" .
"\nWHERE s.id = '{$this->site_id}'";
$this->_db->setQuery( $query );
return $this->_db->loadResult();
}
return null;
}
/**
* Get Full Alias
*
* @return alias
*/
function getFullAlias() {
return str_replace( '//', '/', '/' . $this->external );
}
/**
* Check incoming URL
*
* @param string $site_id
* @param string $url
* @return result
*/
function lookupIncoming( $site_id, $url ) { // static method
$db =& $GLOBALS['database'];
if ($site_id == null) $site_id = -1;
$query = "SELECT * FROM #__opensef_sef WHERE 1" .
"\nAND (direction IS NULL OR direction = '' OR direction = 'i')" .
"\nAND (site_id IS NULL " .
($site_id ? ' OR site_id = ' . $site_id : '') . ')' .
"\nAND (external = '$url'" . (substr( $url, -1, 1 ) != '/' ?
" OR external = '$url/'" : '') . ')' .
"\nAND use_internal = '1' ".
"\nORDER BY site_id DESC, LENGTH(external) ASC, published DESC, valid DESC, LENGTH(direction) DESC, link_prio DESC LIMIT 1";
$db->setQuery( $query );
$results = $db->loadObjectList();
if (is_array( $results ) && count( $results )) {
$results = xclCastObjectList( $results, 'JosOpenSEFAlias' );
return $results[0];
}
return null;
}
/**
* Check outgoing URL
*
* @param string $site_id
* @param string $url
* @return result
*/
function lookupOutgoing( $site_id, $url ) { // static method
// ** NOTE **
// Caching is managed by the parent functions
$nullObj = null;
if (!$site_id)
$site_id = -1;
if (!$url)
return $nullObj;
if (!$this->_db)
$this->_db = $GLOBALS['database'];
$this->_db->setQuery("
SELECT * FROM #__opensef_sef AS sef
WHERE sef.site_id = '$site_id'
AND sef.internal = '$url'
AND sef.published = '1'
AND (sef.direction IS NULL OR sef.direction = 'i')
ORDER BY sef.link_prio DESC
LIMIT 1
");
$this->_db->loadObject( $aliasRow );
if( is_object($aliasRow) && $aliasRow->id ){
$aliasRow = xclCastObjectList( $aliasRow, 'JosOpenSEFAlias' );
return $aliasRow;
} else {
return $nullObj;
}
/*
// Removed v2.1.0
// - trash on next version
global $opensef_aliases;
$returnaliases = array();
if(count($opensef_aliases) > 0) {
foreach($opensef_aliases as $aliases){
if ( $aliases->internal == $url ) {
$returnaliases[] = $aliases;
}
}
}
if (is_array( $returnaliases ) && count( $returnaliases )) {
$returnaliases = xclCastObjectList( $returnaliases, 'JosOpenSEFAlias' );
return $returnaliases[0];
}
$null = null;
return $null;
*/
}
/**
* Record Hits
*
* @param string $site_id
* @param string $url
* @param boole $valid
*/
function recordHit( $site_id, $url, $valid = false ) {
$db =& $GLOBALS['database'];
// A row for this invalid URL doesn't exist yet; let's create one.
$row = new JosOpenSEFAlias( $db );
$row->site_id = $site_id;
$row->published = '0';
$row->valid = ($valid ? '1' : '0');
$row->external = (!$valid ? $url : null);
$row->internal = ($valid ? $url : null);
$row->hits = 1;
// TODO: maybe make this a configuration option?
if (!empty( $_SERVER['HTTP_REFERER'] ))
$row->notes = $_SERVER['HTTP_REFERER'];
$row->check();
if (!$row->store( false )) {
// TODO: insert failed, should not happen. Are SQL tables missing??
die( "DEBUG: row->store() failed!" );
}
}
}
/**
* Component Alias Class
*
*/
class JosOpenSEFComponent extends xclDBTable {
var $id = null;
var $site_id = null;
var $menu_id = null;
var $component = null;
var $alias = null;
var $published = null;
var $block = null;
var $ordering = null;
/**
* Load Component
*
* @param object $db the database connection object
* @param string $id
* @return JosOpenSEFComponent
*/
function JosOpenSEFComponent( &$db, $id = null ) {
$this->xclDBTable( '#__opensef_sef_component', 'id', $db, $id );
}
/**
* checkin
*
* @param string $oid
* @return boole
*/
function checkin( $oid = null ) {
return true;
}
/**
* Load
*
* @param string $oid
* @return boole or load
*/
function load( $oid = null ) {
if (!empty( $oid )) {
return parent::load( $oid );
}
else {
return true;
}
}
/**
* Check
*
* @return parent check
*/
function check() {
if (empty( $this->site_id ))
$this->site_id = null;
if (empty( $this->menu_id ))
$this->menu_id = 0;
if (empty( $this->ordering ))
$this->ordering = '0';
return parent::check();
}
/**
* Enter description here...
*
* @param string $site_id
* @param string $menu_id
* @param arry $component
* @return result
*/
function lookup( $site_id, $menu_id, $component ) { // static method
global $opensef_components;
$returncomps = array();
if ($site_id == -1) $site_id = null;
if (count($opensef_components) > 0) {
foreach($opensef_components as $components){
if ( $components->site_id == $site_id
&& $components->menu_id == $menu_id
&& $components->component == $component
) {
$returncomps[] = $components;
}
}
}
if (is_array( $returncomps ) && count( $returncomps )) {
$returncomps = xclCastObjectList( $returncomps, 'JosOpenSEFComponent' );
return $returncomps[0];
}
$null = null;
return $null;
}
}
/**
* Site class
*
*/
class JosOpenSEFSite extends xclDBTable {
var $_default = false;
var $id = null;
var $host = null;
var $base_url = null;
var $title = null;
var $published = null;
var $access = null;
var $sitename = null;
var $lang = null;
var $locale = null;
var $offset = null;
var $offline = null;
var $debug = null;
var $error_reporting = null;
var $metadesc = null;
var $metakeys = null;
//var $notes = null;
/**
* defaults
*
*/
function defaults() {
$url = parse_url( $GLOBALS['mosConfig_live_site'] );
$port = (isset($url['port']))? ':'.$url['port']:'';
$this->host = $url['host'].$port;
$this->base_url = (isset( $url['path'] ) ? $url['path'] : '');
$this->title = '';
$this->published = '1';
$this->access = '0';
}
/**
* Load site
*
* @param object $db the database connection object
* @param string $id
* @return JosOpenSEFSite
*/
function JosOpenSEFSite( &$db, $id = null ) {
$this->_globals = array('sitename', 'lang', 'locale', 'offset', 'offline',
'debug', 'error_reporting', 'MetaDesc', 'MetaKeys');
if ($id === -1) {
$this->_default = true;
foreach ($this->_globals as $prop) {
$propl = strtolower( $prop );
$this->$propl = $GLOBALS['mosConfig_' . $prop];
}
$this->lang = xclGetLangISOCode( $GLOBALS['mosConfig_lang'] );
$this->defaults();
} else {
$this->xclDBTable( '#__opensef_sef_site', 'id', $db, $id );
}
}
/**
* get Defaults
*
* @return default object
*/
function getDefault() { // static
return new JosOpenSEFSite( $GLOBALS['database'], -1 );
}
/**
* check
*
* @return parent check
*/
function check() {
// Ensure empty string/number zero => null translation
foreach ($this->_globals as $field) {
$field = strtolower( $field );
if (strlen( $this->$field ) == 0)
$this->$field = null;
}
return parent::check();
}
/**
* return defaults
*
* @return defaults
*/
function isDefault() {
return $this->_default;
}
/**
* load site
*
* @param string $oid
* @return boole or load
*/
function load( $oid = null ) {
if (!empty( $oid )) {
return parent::load( $oid );
} else {
$this->defaults();
$this->base_url .= '/newsite';
$this->title = 'New Site';
return true;
}
}
/**
* checkin
*
* @param string $oid
* @return boole
*/
function checkin( $oid = null ) {
return true;
}
/**
* has access?
*
* @return boole
*/
function hasAccess() {
if ($this->id && $this->access) {
global $my;
return ($my->gid >= $this->access);
}
return true;
}
/**
* Check Site URL
*
* @param string $host
* @param string $url
* @return NULL or object
*/
function lookup( $host, $url = null ) { // static method
$db =& $GLOBALS['database'];
$query = "SELECT * FROM #__opensef_sef_site" .
"\nWHERE (base_url = '{$url}'" .
"\nOR '{$url}' LIKE CONCAT_WS('', base_url, '/%'))" .
"\nAND host = '{$host}'" .
"\nORDER BY LENGTH(base_url) DESC, LENGTH(host) DESC LIMIT 1";
$db->setQuery( $query );
$results = $db->loadObjectList();
if (is_array( $results ) && count( $results ) > 0) {
$results = xclCastObjectList( $results, 'JosOpenSEFSite' );
return $results[0];
}
$null = null;
return $null; //new JosOpenSEFSite( $db, -1 );
}
/**
* Override GLOBALS
*
*/
function overrideGlobals() {
// TODO: setting this would be a good idea except that *ALL* URLs,
// including the ones for templates, images and stuff, are generated
// with our modified path, which means those resources won't work.
// Hmm...
//$GLOBALS['mosConfig_live_site'] = 'http://' . $this->host . $this->base_url;
foreach ($this->_globals as $prop) {
$propl = strtolower( $prop );
if (is_null( $this->$propl ) || strlen( $this->$propl ) == 0)
continue;
switch ($propl) {
case 'error_reporting':
error_reporting( intval( $this->error_reporting ) );
$GLOBALS['mosConfig_error_reporting'] =$this->error_reporting;
case 'lang':
// TODO: check Joom!Fish support, for now just this:
$_GET['lang'] = $_REQUEST['lang'] = $this->lang;
$langName = xclGetLangName( $this->lang );
$GLOBALS['mosConfig_lang'] =
($langName ? $langName : $this->lang);
break;
case 'locale':
if (!@setlocale( LC_TIME, $this->locale )) {
// Try setting it to the language value as plan B
@setlocale( LC_TIME, $this->lang );
}
// fall through
default:
$GLOBALS['mosConfig_' . $prop] = $this->$propl;
break;
}
}
}
/**
* Get Components
*
* @return unknown
*/
function getComponents() {
global $opensef_components;
$returncomps = new stdClass();
if ($this->id) {
if (count($opensef_components) > 0) {
foreach($opensef_components as $component){
if ( $component->site_id == $this->id ) {
$returncomps[] = $component;
}
}
}
return $returncomps;
}
return array();
}
/**
* Get Component Alias
*
* @param string $component
* @param string $menu_id
* @return null or object
*/
function getComponentAlias( &$component, &$menu_id ) {
$row = JosOpenSEFComponent::lookup( $this->id, $menu_id, $component );
return (is_object( $row ) ? $row->alias : null);
}
/**
* Get if Component is blocked?
*
* @param string $component
* @param string $menu_id
* @return null or object
*/
function getComponentBlock( &$component, &$menu_id ) {
$row = JosOpenSEFComponent::lookup( $this->id, $menu_id, $component );
return (is_object( $row ) ? $row->block : 0 );
}
/**
* Get SiteURL
*
* @return $string siteurl
*/
function getSiteURL() {
// TODO: need to make sure to match protocol to what the visitor is using (http/https).
return 'http://' . $this->host . $this->base_url;
}
/**
* Get Relative URL
*
* @param string $url
* @return $string url
*/
function getRelativeURL( $url ) {
if ($this->base_url != '') {
if (substr( $url, 0, strlen( $this->base_url ) ) == $this->base_url) {
$url = substr( $url, strlen( $this->base_url ) );
}
}
return ($url ? $url : '/');
}
}
/**
* Object class
*
*/
class sefObject {
var $debug = false;
var $db = null;
var $config = null;
var $site = null;
var $log = null;
/**
* Define Objects
*
* @param object $db the database connection object
* @param object $config
* @param string $site
* @return sefObject
*/
function sefObject( &$db, &$config, &$site ) {
$this->db =& $db;
$this->config =& $config;
$this->site =& $site;
}
/**
* Log Message
*
* @param string $msg
*/
function log( $msg ) {
if ($this->debug && !empty( $msg )) {
$this->log[] = $msg;
}
}
/**
* Set Debug
*
* @param boole $enabled
*/
function setDebug( $enabled = true ) {
$this->debug = $enabled;
}
/**
* Get Log
*
* @return string
*/
function getLog() {
return $this->log;
}
}
/**
* Weblink class
*
*/
class getWeblink extends mosDBTable {
/** @var int Primary key */
var $id = null;
/** @var int */
var $catid = null;
/** @var int */
var $sid = null;
/** @var string */
var $title = null;
/** @var string */
var $url = null;
/** @var string */
var $description = null;
/** @var datetime */
var $date = null;
/** @var int */
var $hits = null;
/** @var int */
var $published = null;
/** @var boolean */
var $checked_out = null;
/** @var time */
var $checked_out_time = null;
/** @var int */
var $ordering = null;
/** @var int */
var $archived = null;
/** @var int */
var $approved = null;
/** @var string */
var $params = null;
/**
* @param database A database connector object
*/
function getWeblink( &$db ) {
$this->mosDBTable( '#__weblinks', 'id', $db );
}
}
/**
* Newsfeed class
*
*/
class getNewsfeeds extends mosDBTable {
/** @var int Primary key */
var $id = null;
/** @var int */
var $catid = null;
/** @var string */
var $name = null;
/** @var string */
var $link = null;
/** @var string */
var $filename = null;
/** @var int */
var $published = null;
/** @var int */
var $numarticles = null;
/** @var int */
var $cache_time = null;
/** @var int */
var $checked_out = null;
/** @var time */
var $checked_out_time = null;
/** @var int */
var $ordering = null;
/**
* @param database A database connector object
*/
function getNewsfeeds( &$db ) {
$this->mosDBTable( '#__newsfeeds', 'id', $db );
}
}
/**
* Banner class
*
*/
class getBanner extends mosDBTable {
var $bid = null;
var $cid = null;
var $name = '';
var $contact = '';
var $email = '';
var $extrainfo = '';
var $checked_out = 0;
var $checked_out_time = 0;
var $editor = '';
/**
* @param database A database connector object
*/
function getBanner( &$db ) {
$this->mosDBTable( '#__banner', 'bid', $db );
}
}
/**
* Google Sitemap Generator - A Joomla/Mambo Component
* @version 2.0.2
* @package Emptorum.com
* @copyright (C) 2005 by rg66 adapted for OpenSEF by Marko "Predator" Schmuck
* @license Released under the terms of the GNU General Public License
**/
class google {
/**
* Generate the XML File
* @return Returns the first line of the XML header
*/
function google_header() {
$xml_content = "<?xml version =\"1.0\" encoding=\"UTF-8\"?>\n";
return $xml_content;
}
/**
* Generate the XML File
* @return Returns the starting definition for the second line
*/
function google_header2() {
$xml_content = " <urlset ";
return $xml_content;
}
/**
* Generate the XML File
* @return Returns the starting definition for the second line if more then one sitemap have to be established
*/
function google_header2a() {
$xml_content = " <sitemapindex ";
return $xml_content;
}
/**
* Generate the XML File
* @return Returns the second part for the secondline. Is the same for google_header2 and google_header2_1
*/
function google_header3() {
$xml_content = "xmlns=\"http://www.google.com/schemas/sitemap/0.84\">\n";
return $xml_content;
}
/**
* Generate the XML File
* @param $xml_url The Url which has to be shown
* @param $xml_date The date when the page has been stored/modified/created
* @param $xml_freq The frequency how often the url content is changing
* @param $xml_priority What priority to the web content does the file/url have
* @return Returns the filled out section
*/
function google_url($xml_url, $xml_date, $xml_freq, $xml_priority) {
$xml_content = " <url>\n";
$url = google :: xml_replace($xml_url);
$xml_content .= " <loc>".$url."</loc>\n";
$xml_content .= " <lastmod>".$xml_date."</lastmod>\n";
$xml_content .= " <changefreq>".$xml_freq."</changefreq>\n";
$xml_content .= " <priority>".$xml_priority."</priority>\n";
$xml_content .= " </url>\n";
return $xml_content;
}
/**
* Generate the XML File
* @return Returns the footer if google_header2 has been used
*/
function google_footer() {
$xml_content = "</urlset>";
return $xml_content;
}
/**
* Generate the XML File
* @return Returns the footer if google_header2_1 has been used
*/
function google_footer2() {
$xml_content = " </sitemapindex>";
return $xml_content;
}
/**
* Generate the XML File
* @param $xml_path If more then one XML sitemaphas been stored the path to the files is listed
* @param $xml_date When has the sitemaps been created
* @param $si Indicates how many sitemaps have been installed
* @return Returns the XML so that the user will be informed that another XML have been installed
*/
function google_sitemap($xml_path, $si) {
global $mosConfig_live_site, $google_filename, $google_location;
$slash = "";
$sefConfig =& $GLOBALS['sefConfig'];
if (strlen($sefConfig->google_path)) {
$slash = "/";
}
$google_path = $mosConfig_live_site."/".$sefConfig->google_path.$slash.$sefConfig->google_filename;
for ($i = 1; $i <= $si; $i ++) {
$xml_content .= " <sitemap>\n";
$xml_siteindex = $google_path.$i.".xml.gz";
$xml_content .= " <loc>".$xml_siteindex."</loc>\n";
$xml_content .= " <lastmod>".google :: showdate(0, 0, 1)."</lastmod>\n";
$xml_content .= " </sitemap>\n";
}
$xml_path = $xml_path."index.xml";
$makefile = google :: google_makefile($xml_path, $xml_content, false, 1);
return $makefile;
}
/**
* Generate the SQL Statement to get the XML Url entries.
* @param $table The table name within the Database
* @param $id The id number from the individual record
* @param $sectionid The sectionid if it exists
* @param $link If a link is hardcoded
* @param $published Is the recoded published
* @param $created When was the record created
* @param $modified When has it been modified
* @param $menutype Only to be used with the table #__menu
* @return Returns the sql statement
*/
function google_sql($tablename, $id, $sectionid, $title, $link, $published, $created, $modified, $menutype) {
$query = "SELECT $id, $sectionid, $title, $published, $created, $modified "."FROM #_".$tablename." WHERE $published = 1 ";
return $query;
}
/**
* Generate the XML File
* @param $xml_url Characters are searched for ans replaced within the file
* @return Returns the Url with Characters Google will accept
*/
function xml_replace($xml_url) {
$look_for = array ("&", "'", "\"", ">", "<");
$change_to = array ("&", "'", """, ">", "<");
$newurl = str_replace($look_for, $change_to, $xml_url);
return $newurl;
}
/**
* Function from php.net
* @param $source The source file which should be gzipped
* @param $level False no gzip, True gzip will be used
*/
function gzcompressfile($source, $level = false) {
$dest = $source.'.gz';
$mode = 'wb'.$level;
$error = false;
if ($fp_out = gzopen($dest, $mode)) {
if ($fp_in = fopen($source, 'rb')) {
while (!feof($fp_in))
gzwrite($fp_out, fread($fp_in, 1024 * 512));
fclose($fp_in);
} else
$error = true;
gzclose($fp_out);
} else
$error = true;
if ($error)
return false;
else
return $dest;
}
/**
* Generate the XML File
* @param $xmlfile The xml file is generated
* @param $xml_content The content which has to be included within the file
* @param $gzip True to use gzip
* @param $type either sitemap when true or urlset when false
* @return Returns the full content of the XML file
*/
function google_makefile($xmlfile, $xml_content, $gzip = true, $type) {
if ($gzip) {
$content = google :: google_header();
$content .= google :: google_header2().google :: google_header3();
$content .= $xml_content;
$content .= google :: google_footer();
if ($fp = fopen($xmlfile, "w")) {
fputs($fp, $content, strlen($content));
fclose($fp);
}
google :: gzcompressfile($xmlfile, true);
unlink($xmlfile);
} else {
if ($fp = fopen($xmlfile, "w")) {
$content = google :: google_header();
if ($type) {
$content .= google :: google_header2a().google :: google_header3();
} else {
$content .= google :: google_header2().google :: google_header3();
}
$content .= $xml_content;
if ($type) {
$content .= google :: google_footer2();
} else {
$content .= google :: google_footer();
}
fputs($fp, $content, strlen($content));
fclose($fp);
}
return $content;
}
}
function google_rename() {
}
function google_deletefile() {
}
function google_deletefolder() {
}
function google_make_sitemap_index($xmlfile, $si_counter) {
}
/**
* Generate the XML File
* param $xmlfile Change the chmod for what file
*/
function google_chmod($xmlfile) {
@ chmod($xmlfile, 0766);
}
/**
* Generate the XML File
* @param $xmlfile Is the xmlfile writable
* @param $msg Show $msg
*/
function google_iswriteable($xmlfile, $msg) {
$permission = is_writable($xmlfile);
if (!$permission) {
mosRedirect("index2.php?option=com_sef&act=google", $msg);
break;
}
}
/**
* Generate the Date with the format Google whats it
* @param $created The date in the databse when the item has been created
* @param $modified The date when an item has been modified
* @param $param If True use $created day, otherwise $modified
* return Returns the date
*/
function showdate($created, $modified, $param) {
if ($param) {
$datetoshow = mosCurrentDate($format = "%Y-%m-%dT%H:%M:%S+00:00");
return $datetoshow;
} else {
if ($modified == "0000-00-00 00:00:00") {
$datetoshow = mosFormatDate($created, $format = "%Y-%m-%dT%H:%M:%S+00:00", $offset = "");
} else {
$datetoshow = mosFormatDate($modified, $format = "%Y-%m-%dT%H:%M:%S+00:00", $offset = "");
}
return $datetoshow;
}
}
/**
* Pings to Google
* @param $count Defines if only one sitemap or more have been generated
* @return returns the msg from Google
*/
function google_ping($count=1) {
global $mosConfig_live_site;
$google_msg = "";
$sefConfig =& $GLOBALS['sefConfig'];
if ($sefConfig->google_ping) {
if ($count == 1) {
$google_xml_location = $mosConfig_live_site."/".$sefConfig->google_path.$sefConfig->google_filename.".xml";
} else {
$google_xml_location = $mosConfig_live_site."/".$sefConfig->google_path.$sefConfig->google_filename."index.xml";
}
$handle = @fopen("http://www.google.com/webmasters/sitemaps/ping?sitemap=".urlencode($google_xml_location), "r");
if ($handle) {
$string = fread($handle, 4096000);
$google_msg = _T('G_PING_OK');
$google_msg = $string;
} else {
$google_msg = _T('G_PING_ERROR');
}
@fclose($handle);
}
return $google_msg;
}
/**
* Get the urls and then call up the function xml
* param $option
* param $frontend Is the script called from the frontend =True or Backend = False
*/
function geturl($frontend) {
global $database, $mosConfig_absolute_path, $mosConfig_live_site;
$results = google :: getPluginData();
for ($i = 0, $n = count($results); $i < $n; $i ++) {
$row = & $results[$i];
$xml_check[$i] = "ON";
$link_content = $mosConfig_live_site."/".google :: sefRelToAbs($row->link);
$xml_url[$i] = $link_content;
$xml_date[$i] = $row->date;
$xml_freq[$i] = $row->changeFreq;
$xml_priority[$i] = $row->priority;
}
google :: xml($i, $xml_check, "No Title", $xml_url, $xml_date, $xml_freq, $xml_priority, $frontend);
}
/**
* Makes the XML File
* @param $option
* @param $xmlrows How many rows or urls are there
* @param $xml_check Array-Only Urls which ares et to ON will be added to the XML File
* @param $title Array-Has no meaning but we have kept it
* @param $xml_url Array-The url which has to be displayed in the XML File. Will be SEF if needed
* @param $xml_date Array-The date when the url has been created or modified
* @param $xml_freq Array-What is the change frequency of the url
* @param $xml_priority Array-What is the priority of the url towarsd the whole website
* @param $frontend Is the script run from the frontend=True or backend=False
*
*/
function xml($xml_rows, $xml_check, $xml_title, $xml_url, $xml_date, $xml_freq, $xml_priority, $frontend = false) {
global $mosConfig_live_site, $mosConfig_absolute_path;
$sefConfig =& $GLOBALS['sefConfig'];
$xml_content = google :: google_url($mosConfig_live_site, mosCurrentDate($format = "%Y-%m-%d"), $sefConfig->google_frequency, $sefConfig->google_priority);
google :: clean_gzip();
$j = 0;
$si = 1;
$initsize = strlen(google :: google_header());
$initsize += strlen(google :: google_header2());
$initsize += strlen(google :: google_header3());
$initsize += strlen(google :: google_footer());
$size = $initsize;
set_time_limit(900);
for ($i = 1; $i <= $xml_rows; $i ++) {
if ($xml_check[$i] == "ON") {
$j ++;
$xml_content .= google :: google_url($xml_url[$i], $xml_date[$i], $xml_freq[$i], $xml_priority[$i]);
$xml_content_len = google :: google_url($xml_url[$i], $xml_date[$i], $xml_freq[$i], $xml_priority[$i]);
$size += strlen($xml_content_len);
if ($j == 50000 || $size >= 2097152) {
$j = 0;
$xmlfile = $mosConfig_absolute_path.google::needslash($mosConfig_absolute_path).$sefConfig->google_path.google::needslash($sefConfig->google_path).$sefConfig->google_filename.$si.".xml";
$xml_content = google :: google_makefile($xmlfile, $xml_content, true, 0);
$si ++;
$xml_content = "";
$size = $initsize;
}
}
}
if ($si == 1) {
$xmlfile = $mosConfig_absolute_path.google::needslash($mosConfig_absolute_path).$sefConfig->google_path.google::needslash($sefConfig->google_path).$sefConfig->google_filename.".xml";
google :: google_chmod($xmlfile);
$permission = google :: google_iswriteable($xmlfile, _T('G_XML_NOWRITE'));
$xml_content = google :: google_makefile($xmlfile, $xml_content, false, 0);
} else {
if (strlen($xml_content) > 0) {
$xmlfile = $mosConfig_absolute_path.google::needslash($mosConfig_absolute_path).$sefConfig->google_path.google::needslash($sefConfig->google_path).$sefConfig->google_filename.$si.".xml";
$xml_content = google :: google_makefile($xmlfile, $xml_content, true, 0);
}
$xmlfile = $mosConfig_absolute_path.google::needslash($mosConfig_absolute_path).$sefConfig->google_path.google::needslash($sefConfig->google_path).$sefConfig->google_filename;
$xml_content = google :: google_sitemap($xmlfile, $si);
}
google :: google_ping($si);
$slash="";
if (strlen($sefConfig->google_path)) {
$slash = "/";
}
if ($si == 1) {
$google_xml_url = $mosConfig_live_site."/".$sefConfig->google_path.$slash.$sefConfig->google_filename;
} else {
$google_xml_url = $mosConfig_live_site."/".$sefConfig->google_path.$slash.$sefConfig->google_filename."index";
}
$google_url = urlencode($google_xml_url);
if (!$frontend) {
$this->tpl->assign( 'text', $xml_content );
$this->tpl->assign( 'check', $xml_check );
$this->tpl->assign( 'google_filename', $sefConfig->google_filename );
$this->tpl->assign( 'google_msg', @$google_msg );
$this->tpl->assign( 'google_url', $google_url );
$this->tpl->assign( 'xml_rows', $xml_rows );
$this->tpl->assign( 'title', @$title );
$this->tpl->assign( 'success', @$success );
$this->tpl->assign( 'log', @$log );
$this->tpl->assign( 'backLink', true );
$this->display( 'action2' );
}
}
/**
* Opens up a xml file and looks for the element
* @param $ElementName Define what element you want to retrieve from the xml file
* @param $Document Define the document, can be http as well!
* @return Element Value
*/
function getXMLElementData($ElementName, $Document) {
global $mosConfig_absolute_path;
require_once ($mosConfig_absolute_path."/includes/domit/xml_domit_include.php");
$googleCollection = & new DOMIT_Document();
$googleCollection->resolveErrors(true);
$success = $googleCollection->loadXML($Document);
if ($success) {
if ($googleCollection->documentElement->hasChildNodes()) {
$myChildNodes = & $googleCollection->documentElement->childNodes;
$numChildren = & $googleCollection->documentElement->childCount;
for ($i = 0; $i < $numChildren; $i ++) {
$currentNode = & $myChildNodes[$i]->firstChild;
$currentNodeName = & $myChildNodes[$i];
if ($currentNodeName->nodeName == $ElementName) {
return $currentNode->toString(false);
}
}
}
}
}
function checkName($name) {
$name = str_replace('../', '', $name);
$name = str_replace('./', '', $name);
return $name;
}
/**
* used from sef.php
* TODO. When the include is fixed in sef, just include the
* file and call the function
*/
/**
* Converts an absolute URL to SEF format
* @param string The URL
* @return string
*/
function sefRelToAbs( $string ) {
global $mosConfig_live_site, $mosConfig_sef, $mosConfig_mbf_content;
global $iso_client_lang, $mosConfig_multilingual_support;
if( $mosConfig_sef && ($mosConfig_mbf_content || $mosConfig_multilingual_support) && $string!='index.php' && !eregi("^(([^:/?#]+):)",$string) && !strcasecmp(substr($string,0,9),'index.php') && !eregi('lang=', $string) ) {
$string .= '&lang='. $iso_client_lang;
}
if ($mosConfig_sef && !eregi("^(([^:/?#]+):)",$string) && !strcasecmp(substr($string,0,9),'index.php')) {
$string = str_replace( '&', '&', $string );
if ($string == 'index.php') {
$string = '';
}
$url = parse_url( $string );
$fragment = '';
if ( isset($url['fragment']) ) {
if (preg_match('@^[A-Za-z][A-Za-z0-9:_.-]*$@', $url['fragment'])) {
$fragment = '#'. $url['fragment'];
}
}
if ( isset($url['query']) ) {
$url['query'] = stripslashes( str_replace( '+', '%2b', $url['query'] ) );
parse_str( $url['query'], $parts );
foreach( $parts as $key => $value) {
if ( strpos( $value, '+' ) !== false ) {
$parts[$key] = stripslashes( str_replace( '%2b', '+', $value ) );
}
}
$sefstring = '';
if ( ( $parts['option'] == 'com_content' || $parts['option'] == 'content' ) && ( $parts['task'] != 'new' ) && ( $parts['task'] != 'edit' ) ) {
$sefstring .= 'content/';
if ( isset( $parts['task'] ) ) {
$sefstring .= $parts['task'].'/';
}
if ( isset( $parts['sectionid'] ) ) {
$sefstring .= $parts['sectionid'].'/';
}
if ( isset( $parts['id'] ) ) {
$sefstring .= $parts['id'].'/';
}
if ( isset( $parts['Itemid'] ) ) {
if ( $parts['Itemid'] != 99999999 && $parts['Itemid'] != 0 ) {
$sefstring .= $parts['Itemid'].'/';
}
}
if ( isset( $parts['order'] ) ) {
$sefstring .= 'order,'. $parts['order'].'/';
}
if ( isset( $parts['filter'] ) ) {
$sefstring .= 'filter,'. $parts['filter'].'/';
}
if ( isset( $parts['limit'] ) ) {
$sefstring .= $parts['limit'].'/';
}
if ( isset( $parts['limitstart'] ) ) {
$sefstring .= $parts['limitstart'].'/';
}
if ( isset( $parts['year'] ) ) {
$sefstring .= $parts['year'].'/';
}
if ( isset( $parts['month'] ) ) {
$sefstring .= $parts['month'].'/';
}
if ( isset( $parts['module'] ) ) {
$sefstring .= $parts['module'].'/';
}
if ( isset( $parts['lang'] ) ) {
$sefstring .= 'lang,'. $parts['lang'].'/';
}
$string = $sefstring;
} else if ( ( strpos( $parts['option'], 'com_' ) !== false ) && ( @$parts['task'] != 'new' ) && ( @$parts['task'] != 'edit' ) ) {
$sefstring = 'component/';
foreach($parts as $key => $value) {
$sefstring .= $key .','. $value.'/';
}
$string = str_replace( '=', ',', $sefstring );
}
}
return $mosConfig_live_site .'/'. $string . $fragment;
} else {
if ( (strpos( $string, $mosConfig_live_site ) !== 0) ) {
if (strncmp($string, '/', 1) == 0) {
$live_site_parts = array();
eregi("^(https?:[\/]+[^\/]+)(.*$)", $mosConfig_live_site, $live_site_parts);
$string = $live_site_parts[1] . $string;
} else if ( ( strpos( $string, 'http' ) !== 0 ) && ( strpos( $string, 'https' ) !== 0 ) && ( strpos( $string, 'ftp' ) !== 0 ) && ( strpos( $string, 'file' ) !== 0 ) && ( strpos( $string, 'mailto' ) !== 0 ) && ( strpos( $string, 'javascript' ) !== 0 ) ) {
$string = $mosConfig_live_site .'/'. $string;
}
}
return $string;
}
}
/*
* removes the gzip files before they are established
* we do not want more space occupied then is required
*/
function clean_gzip() {
global $mosConfig_absolute_path;
$slash = "";
$sefConfig =& $GLOBALS['sefConfig'];
if (strlen($sefConfig->google_path)) {
$slash = "/";
}
$google_path = $mosConfig_absolute_path.$slash.$sefConfig->google_path;
$dir = dir($google_path);
while ($entry = $dir->read()) {
if (eregi(".+\.xml\.gz$", $entry)) {
unlink($google_path."/".$entry);
} //end i
}
}
/*
* Is the file or folder writeable
*/
function filedir_writeable($filedir) {
if (is_writable($filedir)) {
$color = "green";
} else {
$color = "red";
}
return $color;
}
/*
* Does the folder exist
*/
function isdir($folder) {
if (is_dir($folder)) {
$color = "green";
} else {
$color = "red";
}
return $color;
}
/*
* Does the file exist
*/
function isfile($file) {
if (is_file($file)) {
$color = "green";
} else {
$color = "red";
}
return $color;
}
/*
* Return 1 if the value is green
*/
function green($green) {
if ($green == "green") {
return 1;
} else {
return 0;
}
}
/*
* Check for trailing slash
*/
function needslash($path){
$slash = substr($path, -1);
if ($slash == "/"){
return "";
} else {
return "/";
}
}
}
class URLResource{
//url page title
var $title;
//url
var $url;
//date
var $date;
// constant from language file
var $changeFreq;
//constant from language file
var $priority;
}
/**
* Google Sitemap Generator.
* @version 0.3
* @package gsg.org
* @author Guillermo Vargas (guilleva)
* @license Released under the terms of the GNU General Public License
* @abstract This class represends the required methods of a 3PC Implementations.
**/
class GoogleOpenSEFExtension{
/**
* Get the URLS from the 3PC.
* @return An array of URLResource objects.
*/
function getURLResources( ) {
global $database, $mosConfig_live_site, $mosConfig_absolute_path;
$sefConfig =& $GLOBALS['sefConfig'];
$results = array ();
$query = "SELECT js.id, js.external,js.internal, jss.host , jss.base_url FROM #__opensef_sef as js LEFT JOIN #__opensef_sef_site as jss on js.site_id = jss.id WHERE js.published = 1 AND js.valid = 1 group by external ORDER BY external";
$database->setQuery($query);
$rows = $database->loadObjectList();
$query = "SELECT id, name, link FROM #__menu WHERE published = 1 ORDER BY id";
$database->setQuery($query);
$rows2 = $database->loadObjectList();
$search = array ('@'.$mosConfig_live_site.'@', '@^https?://@','@^/@','@/$@','@\.(php|html|htm|phtml)$@','@/@','@_@','@'.$mosConfig_live_site.'@');
$repl = array ('','','','','',' - ',' ');
for ($i = 0, $n = count($rows2); $i < $n; $i ++) {
$row = $rows2[$i];
//create a new object on the stack
$urlResource1 = new URLResource();
//assign all variables
$urlResource1->title = preg_replace ($search, $repl, $row->name);
if (eregi("index.php", $row->link) && !preg_match('#https?://#', $row->link)) {
$urlResource1->link = $row->link.'&Itemid='.$row->id;
} else {
$urlResource1->link = $row->link;
}
$urlResource1->date = google :: showdate(0, 0, 1);
$urlResource1->changeFreq = $sefConfig->google_frequency;
$urlResource1->priority = $sefConfig->google_priority;
//add the pointer to the array
$results1[] = $urlResource1;
}
$search = array ('@'.$mosConfig_live_site.'@', '@^https?://@','@^/@','@/$@','@\.(php|html|htm|phtml)$@','@/@','@_@','@'.$mosConfig_live_site.'@');
$repl = array ('','','','','',' - ',' ');
for ($i = 0, $n = count($rows); $i < $n; $i ++) {
$row = & $rows[$i];
//create a new object on the stack
$urlResource = new URLResource();
//assign all variables
$urlResource->title = preg_replace ($search, $repl, $row->external);
if (!preg_match('#https?://#',$row->external)){
if ( $row->host ) {
if (!preg_match('#^'.$row->base_url.'#',$row->external)){
$urlResource->link = 'http://' . $row->host . $row->base_url . $row->external;
} else {
$urlResource->link = 'http://' . $row->host . $row->external;
}
} else {
$urlResource->link = $mosConfig_live_site . $row->external;
}
} else {
$urlResource->link = $row->external;
}
$urlResource->date = google :: showdate(0, 0, 1);
$urlResource->changeFreq = $sefConfig->google_frequency;
$urlResource->priority = $sefConfig->google_priority;
//add the pointer to the array
$results[] = $urlResource;
}
$results = array_merge($results, $results1);
return $results;
}
}
class opensef_upgrader {
var $version_xml = null;
var $xmlDocServer = null;
var $upgradePath = null;
var $msg = null;
function opensef_upgrader() {
global $mosConfig_absolute_path;
require_once ($mosConfig_absolute_path.'/includes/domit/xml_domit_lite_include.php');
}
function set_upgrade_path($path) {
$this->upgradePath = $path;
}
function set_version_xml($file) {
$this->version_xml = $this->upgradePath."/".$file;
}
function get_version_xml() {
return $this->version_xml;
}
function get_server_version($item) {
global $mosConfig_absolute_path;
$xmlDoc = &new DOMIT_Lite_Document();
$xmlDoc->resolveErrors( true );
if ($xmlDoc->loadXML( $this->get_version_xml(), true )) {
$root =& $xmlDoc->documentElement;
$tagName = $root->getTagName();
if (!$tagName == 'update') {
return;
}
$newElement = &$xmlDoc->getElementsbyPath('/update/'.$item.'/version',1);
}
return $newElement ? $newElement->getText() : '';
}
function upgrade($item) {
global $database;
// get upgrade info from server
require_once ($this->upgradePath."/".$item->module."upd.inc");
//update files
if (isset ($updatefile)) {
foreach ($updatefile as $u) {
if ($u->upversion > $item->oldversion) {
if ($this->up_file($u->name)) {
$this->addMsg("s", "File ".$u->name." updated");
} else {
$this->addMsg("e", "File ".$u->name." update failed");
}
}
if ($u->delversion > $item->oldversion) {
if ($this->del_file($u->name)) {
$this->addMsg("s", "File ".$u->name." deleted");
} else {
$this->addMsg("e", "File ".$u->name." delete failed");
}
}
}
}
// update database
if (isset ($updatequery)) {
foreach ($updatequery as $q) {
if ($q->upversion > $item->oldversion) {
$database->setQuery($q->query);
$database->query();
if ($database->getErrorNum()) {
$this->addMsg("e", $q->text.$database->getErrorMsg());
} else {
$this->addMsg("s", $q->text." successful");
}
}
}
}
$this->addMsg("i", "Upgrade of ".$item->module." completed");
}
function addMsg($type, $text) {
$tmp = null;
$tmp->type = $type;
$tmp->text = $text;
if (!is_array($this->msg))
$this->msg = array ();
array_push($this->msg, $tmp);
}
function up_file($fname) {
global $mosConfig_absolute_path;
$b = set_magic_quotes_runtime(0);
if (!function_exists('file_get_contents')) {
function file_get_contents($file) {
$file = file($file);
return !$file ? false : implode('', $file);
}
}
if (!function_exists('file_put_contents')) {
function file_put_contents($filename, $data, $file_append = false) {
$fp = fopen($filename, (!$file_append ? 'w+' : 'a+'));
if (!$fp) {
trigger_error('file_put_contents cannot write in file.', E_USER_ERROR);
return 0;
}
$return = fwrite($fp, $data);
fclose($fp);
if ($return === false) {
$return = 0;
}
return $return;
}
}
$upfile = file_get_contents($this->upgradePath.preg_replace('/\.php$/', '.inc', $fname));
return file_put_contents($mosConfig_absolute_path.$fname, $upfile);
}
function del_file($fname) {
global $mosConfig_absolute_path;
if (file_exists($mosConfig_absolute_path."/".$fname))
unlink($mosConfig_absolute_path."/".$fname);
}
}
class opensef_upgradeItem {
var $module = null;
var $oldversion = null;
function opensef_upgradeItem() {
}
function bind($array, $ignore = "") {
if (!is_array($array)) {
$this->_error = strtolower(get_class($this))."::bind failed.";
return false;
} else {
return mosBindArrayToObject($array, $this, $ignore);
}
}
}
?>