php
if( ! defined( '_HEADCAP' ) )
die( 'Direct access is not allowed!' );
// Load platform core libraries
require_once ( dirname( __FILE__ ) . '/core.php' );
// Check if is public access or beta
if( defined( 'SITE_PROTECTED' ) ) {
$siteVisible = true;
if( preg_match( "/beta([0-9]+).capsula.com.pt/", HTTP_PATH, $matches ) ) {
$siteVisible = false;
if( isset( $_COOKIE['domainkey'] ) && strcmp( $_COOKIE['domainkey'], md5( DOMAIN ) ) == 0 ) {
$siteVisible = true;
} elseif( isset( $_GET['key'] ) && $_GET['key'] == md5( DOMAIN ) ) {
$siteVisible = true;
setcookie( 'domainkey', md5( DOMAIN ) );
}
}
if( ! $siteVisible ) {
header( 'Location: http://www.capsula.pt' );
exit();
}
}
// Database connection if database name is defined and with name
if( strlen( DB_NAME ) > 0 ) {
$db = Database::instance( DB_USER, DB_PWD, DB_NAME, DB_HOST, DB_PERSISTENT, DB_DEBUG, DB_DRIVER, DB_CHARSET, DB_TRANSACTIONS );
register_shutdown_function( array(
$db,
'rollback'
) );
}
// Logging
$hcLog = new logging( LOGGING_PATH . '/hclog.log' );
// Carregar lista de idiomas dinâmicos
if( isset( $db ) )
$translations = new Translations( $db );
// Load default security data filter
if( ! defined( 'CUSTOM_FILTER' ) || ( defined( 'CUSTOM_FILTER' ) && ! CUSTOM_FILTER ) ) {
$_GET = recursiveInputFilter( $_GET, true, true );
$_POST = recursiveInputFilter( $_POST, true, true );
$_REQUEST = recursiveInputFilter( $_REQUEST, true, true );
}
// Seo robots definitions
$seoIndex = defined( 'SEO_INDEX' ) ? SEO_INDEX : false;
$seoFollow = defined( 'SEO_FOLLOW' ) ? SEO_FOLLOW : false;
// Api functions list
/**
* Obtém a lista de registos de um módulo
*
* @param string $moduleName
* @param string $condition
* @param array $fields
* @return collection
*/
function hcGetList( $moduleName, $condition = null, $fields = array() )
{
global $db;
global $hcLog;
global $translations;
try {
$modules = new modules( $db );
$modules->loadByParams( 'name', $moduleName );
$module = $modules->get( 0 );
$ar = new activerecord( $db );
call_user_func_array( array(
$ar,
'select'
), $fields );
if( isset( $module->multilang ) && $module->multilang == 1 ) {
$module->tablename .= '_' . $translations->getSelected()->abbrev;
}
$ar->fromtable( $module->tablename );
if( strlen( $condition ?? '' ) > 0 )
$ar->where( $condition );
return $ar->prepare()->execute();
} catch( Exception $ex ) {
redirect( 500 );
$hcLog->log( $ex->getMessage() . ' - Line ' . $ex->getLine() );
exit();
}
}
/**
* Obtém um registo de um módulo
*
* @param string $moduleName
* @param int $id
* @param array $fields
* @return object
*/
function hcGetDetails( $moduleName, $id, $fields = array() )
{
global $db;
global $hcLog;
global $translations;
try {
$record = new records( $db, $moduleName, $translations->getSelected()->abbrev );
$params = array_merge( array(
$id,
false
), $fields );
return call_user_func_array( array(
$record,
'details'
), $params );
} catch( Exception $ex ) {
redirect( 500 );
$hcLog->log( $ex->getMessage() . ' - Line ' . $ex->getLine() );
exit();
}
}
/**
* Obtém o primeiro registo de um módulo, de acordo com determinados parâmetros
*
* @param string $moduleName
* @param string $condition
* @param array $fields
* @return mixed
*/
function hcGetFirst( $moduleName, $condition = null, $fields = array() )
{
try {
$items = hcGetList( $moduleName, $condition, $fields );
if( $items->size() == 0 )
return false;
return $items->get( 0 );
} catch( Exception $ex ) {
redirect( 500 );
$hcLog->log( $ex->getMessage() . ' - Line ' . $ex->getLine() );
exit();
}
}
/**
* Obtém os ficheiros de um registo
*
* @param string $moduleName
* @param string $fieldName
* @param int $relatedId
* @param string $pathPrefix
* @return collection
*/
function hcGetFiles( $moduleName, $fieldName, $relatedId, $pathPrefix = HTTP_PATH )
{
global $db;
global $hcLog;
global $translations;
try {
$modules = new modules( $db );
$fields = new fields( $db );
$modules->loadByParams( 'name', $moduleName );
$module = $modules->get( 0 );
$fields->loadByParams( 'moduleId', $module->id, 'name', $fieldName );
$field = $fields->get( 0 );
$files = new files( $db );
if( isset( $module->multilang ) && $module->multilang == 1 )
$langId = $translations->getSelected()->id;
else
$langId = 0;
$files = $files->getBindFiles( $module->id, $field->id, $relatedId, $langId );
for( $i = 0; $i < $files->size(); $i ++ )
$files->get( $i )->path = $pathPrefix . $files->get( $i )->path;
return $files;
} catch( Exception $ex ) {
redirect( 400 );
$hcLog->log( $ex->getMessage() . ' - Line ' . $ex->getLine() );
exit();
}
}
/**
* Obtém as imagens de um registo
*
* @param string $moduleName
* @param string $fieldName
* @param int $relatedId
* @param string $thumb
* @return collection
*/
function hcGetImages( $moduleName, $fieldName, $relatedId, $thumb = null, $pathPrefix = HTTP_PATH )
{
global $db;
global $hcLog;
global $translations;
try {
$modules = new modules( $db );
$fields = new fields( $db );
$modules->loadByParams( 'name', $moduleName );
$module = $modules->get( 0 );
$fields->loadByParams( 'moduleId', $module->id, 'name', $fieldName );
$field = $fields->get( 0 );
$images = new images( $db );
if( isset( $module->multilang ) && $module->multilang == 1 )
$langId = $translations->getSelected()->id;
else
$langId = 0;
$images = $images->getBindImages( $module->id, $field->id, $relatedId, $langId );
$thumbsize = is_null( $thumb ) ? IMAGE_MAX_WIDTH . 'x' . IMAGE_MAX_HEIGHT : $thumb;
for( $i = 0; $i < $images->size(); $i ++ ) {
$image = $images->get( $i );
$path_info = pathinfo( $image->path );
$ext = isset( $path_info['extension'] ) ? $path_info['extension'] : 'jpg';
$image->path = $pathPrefix . IMAGES_DIR . '/' . $image->id . '_' . $thumbsize . '.' . $ext;
if( ! is_null( $thumb ) )
$image->mainpath = $pathPrefix . IMAGES_DIR . '/' . $image->id . '_' . IMAGE_MAX_WIDTH . 'x' . IMAGE_MAX_HEIGHT . '.' . $ext;
if( ! file_exists( SERVER_PATH . IMAGES_DIR . '/' . $image->id . '_' . $thumbsize . '.' . $ext ) )
$images->remove( $i -- );
}
return $images;
} catch( Exception $ex ) {
redirect( 400 );
$hcLog->log( $ex->getMessage() . ' - Line ' . $ex->getLine() );
exit();
}
}
/**
* Obtém as associações de um registo
*
* @param string $moduleName
* @param string $assocModuleName
* @param int $itemId
* @param string $condition
* @param bool $invert
* @return collection
*/
function hcGetAssociations( $moduleName, $assocModuleName, $itemId = null, $condition = null, $invert = false, $option = null, $value = null )
{
global $db;
global $hcLog;
global $translations;
try {
$modules = new modules( $db );
$modules->extend( 'name LIKE "' . $moduleName . '"' );
$module = $modules->get( 0 );
$modules->extend( 'name LIKE "' . $assocModuleName . '"' );
$assocModule = $modules->get( 0 );
$statement = '';
$option = intval( $option );
$value = intval( $value );
if( $option > 0 )
$statement .= " AND assoc.`option` = $option";
if( $value > 0 )
$statement .= " AND assoc.value = $value";
if( isset( $module->multilang ) && $module->multilang == 1 )
$module->tablename .= '_' . $translations->getSelected()->abbrev;
if( isset( $assocModule->multilang ) && $assocModule->multilang == 1 )
$assocModule->tablename .= '_' . $translations->getSelected()->abbrev;
if( is_null( $itemId ) ) {
$sql = "
SELECT t2.id as recordId, t2.*, t1.id as assocRecordId, t1.*
FROM " . tables::ASSOCIATIONS_TABLE . " assoc
JOIN $module->tablename t2 ON t2.id = assoc.recordId
JOIN $assocModule->tablename t1 ON t1.id = assoc.assocRecordId $statement";
} else {
$sql = "
SELECT DISTINCT t1.id, t1.*
FROM " . tables::ASSOCIATIONS_TABLE . " assoc";
if( ! $invert ) {
$sql .= "
JOIN $assocModule->tablename t1 ON t1.id = assoc.assocRecordId
WHERE moduleId = $module->id AND recordId = $itemId AND assocModuleId = $assocModule->id $statement";
} else {
$sql .= "
JOIN $module->tablename t1 ON t1.id = assoc.recordId
WHERE moduleId = $module->id AND assocRecordId = $itemId AND assocModuleId = $assocModule->id $statement";
}
}
$sql .= $condition;
return $db->results( $sql );
} catch( Exception $ex ) {
redirect( 400 );
$hcLog->log( $ex->getMessage() . ' - Line ' . $ex->getLine() );
exit();
}
}
/**
* Insere/Actualiza um registo de um módulo
*
* @param string $moduleName
* @param array $data
* @param int $id
* @return bool
*/
function hcSave( $moduleName, $data, $id = null )
{
global $db;
global $hcLog;
global $translations;
try {
$record = new records( $db, $moduleName, $translations->getSelected()->abbrev );
$hcLog->log( 'A gravar registo do módulo ' . $moduleName . ' com id = ' . $id );
return $record->save( $data, $id );
} catch( Exception $ex ) {
redirect( 500 );
$hcLog->log( $ex->getMessage() . ' - Line ' . $ex->getLine() );
exit();
}
}
function redirect($statusCode) {
http_response_code($statusCode);
exit();
}
/**
* Envia um e-mail
*
* @param string $from
* @param string $to
* @param string $subject
* @param string $message
* @param string $type
* @param string $cc
* @param string $bcc
* @param array $attachments
* @return bool
*/
function hcEmail( $from, $to, $subject, $message, $type = 'html', $cc = null, $bcc = '', $attachments = array(), $mailOptions = array() )
{
global $hcLog;
$result = new stdClass();
$result->status = false;
$result->message = '';
$result->object = null;
try {
$mail = new Email( SMTP_HOST, SMTP_USER, SMTP_PWD, array(
'Port' => SMTP_PORT,
'SMTPAuth' => defined( 'SMTP_AUTH_ENABLED' ) && ! SMTP_AUTH_ENABLED ? false : true,
'SMTPSecure' => defined( 'SMTP_SECURE' ) ? SMTP_SECURE : '',
'AuthType' => defined( 'SMTP_AUTH_TYPE' ) ? SMTP_AUTH_TYPE : 'LOGIN',
'Helo' => defined( 'SMTP_HELO' ) ? SMTP_HELO : '',
'CharSet' => CHARSET_ENCODING_PAGE
) );
$result->object = &$mail;
if( isset( $cc ) )
$mail->addCc( $cc );
if( isset( $bcc ) ) {
if( is_array( $bcc ) ) {
for( $i = 0; $i < count( $bcc ); $i ++ ) {
$mail->addBcc( $bcc[$i] );
}
} else $mail->addBcc( $bcc );
}
if( is_string( $attachments ) )
$attachments = array(
$attachments
);
for( $i = 0; $i < count( $attachments ); $i ++ ) {
$mail->addAttachment( $attachments[$i] );
}
global $whiteListEmails;
if( isset( $whiteListEmails ) ) {
$mail->filterPolicy = 'WHITE_LIST';
$mail->filterList = $whiteListEmails;
}
$mail->setOptions( $mailOptions );
$result->status = $mail->compose( $subject, $message, $type == 'html' ? true : false )->send( $from, $to );
if( ! $mail->getSendingStatus() )
$result->message = $mail->getErrorInfo();
return $result;
} catch( Exception $ex ) {
redirect( 500 );
$hcLog->log( $ex->getMessage() . ' - Line ' . $ex->getLine() );
exit();
}
}
/**
* Exporta dados de um módulo para XML
*
* @param string $moduleName
* @param string $condition
* @return void
*/
function hcXml( $moduleName, $condition = null, $charset = 'utf-8' )
{
global $db;
global $translations;
try {
$modules = new modules( $db );
$modules->loadByParams( 'name', $moduleName );
$module = $modules->get( 0 );
if( isset( $module->multilang ) && $module->multilang == 1 )
$module->tablename .= '_' . $translations->getSelected()->abbrev;
$ar = new activerecord( $db, null, $className );
$ar->select()->fromtable( $module->tablename );
if( strlen( $condition ) > 0 )
$ar->where( $condition );
$ar->prepare()->execute();
// preparar xml
$xmlDoc = new DOMDocument( '1.0', $charset );
$xmlDoc->formatOutput = true;
$moduleTag = $xmlDoc->createElement( 'module' );
// $moduleTag->setAttribute( 'name', $module->name );
$moduleTag = $xmlDoc->appendChild( $moduleTag );
$size = $ar->size();
for( $i = 0; $i < $size; $i ++ ) {
$record = $ar->get( $i );
$recordTag = $xmlDoc->createElement( 'record' );
$recordTag->setAttribute( 'id', $record->id );
$recordTag = $moduleTag->appendChild( $recordTag );
foreach( $record as $key => $value ) {
if( $key === 'id' )
continue;
$fieldTag = $xmlDoc->createElement( 'field' );
$fieldTag->setAttribute( 'name', $key );
$fieldValueCDATA = $xmlDoc->createCDATASection( ' ' . $value . ' ' );
$fieldTag->appendChild( $fieldValueCDATA );
$fieldTag = $recordTag->appendChild( $fieldTag );
}
}
$fp = fopen( "test1.xml", "w" );
fwrite( $fp, $xmlDoc->saveXML() );
fclose( $fp );
// return $xmlDoc->save( 'test1.xml' );
} catch( Exception $ex ) {
redirect( 500 );
$hcLog->log( $ex->getMessage() . ' - Line ' . $ex->getLine() );
exit();
}
}
/**
* Obtém as tags de um ou mais ficheiros
*
* @param int $fileId
* @return collection
*/
function hcGetFileTags( $fileId = null )
{
global $db;
global $hcLog;
try {
$ar = new activerecord( $db );
$statement = ' type = 1 ';
if( ! is_int( $fileId ) )
$statement .= ' AND fileId = ' . $fileId;
$ar->select( 'tags' )
->fromtable( tables::FILES_TABLE )
->where( $statement )
->prepare()
->execute();
return $ar;
} catch( Exception $ex ) {
redirect( 500 );
$hcLog->log( $ex->getMessage() . ' - Line ' . $ex->getLine() );
exit();
}
}
/**
* Gera a meta informação robots activando ou desactivando a
* indexação nos motores de pesquisa
*
* @param bool $seoIndex
* @param bool $seoFollow
* @return string
*/
function hcRobots( $seoIndex, $seoFollow )
{
$robots = '';
if( $seoIndex )
$robots .= 'index';
else
$robots .= 'noindex';
if( $seoFollow )
$robots .= ',follow';
else
$robots .= ',nofollow';
return $robots;
}
/**
* Retorna a data de última modificação
*
* @return string
*/
/**
* Retorna a data de última modificação
*
* @return string
*/
function hcLastModified( $moduleName = null, $id = null )
{
global $db;
global $translations;
$records = new records( $db );
if( is_null( $moduleName ) && is_null( $id ) )
return $records->getLastModified();
$modules = new modules( $db );
$modules->loadByParams( 'name', $moduleName );
$module = $modules->get( 0 );
if( isset( $module->multilang ) && $module->multilang == 1 ) {
$module->tablename .= '_' . $translations->getSelected()->abbrev;
}
return $records->getLastModified( $module->id, $id );
}
Fatal error: Uncaught Error: Undefined constant "HTTP_PATH" in /home/balancasmarques/public_html/header.php:61
Stack trace:
#0 /home/balancasmarques/public_html/index.php(2): require_once()
#1 {main}
thrown in /home/balancasmarques/public_html/header.php on line 61