Version 5.0.0 beta 1

This commit is contained in:
Neo committed 2025-12-19 16:27:35 -08:00
1 parent 25ddeb65d6
commit 15c7beabc5
6736 files changed
+627902 -497943

No files matched your search

+100 -173
View File
@@ -12,76 +12,94 @@
namespace IPS\cms;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use Exception;
use IPS\cms\Media\Folder;
use IPS\Db;
use IPS\File;
use IPS\Helpers\Form;
use IPS\Helpers\Form\Node;
use IPS\Helpers\Form\Upload;
use IPS\Http\Url;
use IPS\Member;
use IPS\Node\Model;
use OutOfRangeException;
use RuntimeException;
use function count;
use function defined;
use function in_array;
use function intval;
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* @brief Media Model
*/
class _Media extends \IPS\Node\Model
class Media extends Model
{
/**
* @brief [ActiveRecord] Multiton Store
*/
protected static $multitons;
protected static array $multitons;
/**
* @brief [ActiveRecord] Database Table
*/
public static $databaseTable = 'cms_media';
public static ?string $databaseTable = 'cms_media';
/**
* @brief [ActiveRecord] Database Prefix
*/
public static $databasePrefix = 'media_';
public static string $databasePrefix = 'media_';
/**
* @brief [ActiveRecord] ID Database Column
*/
public static $databaseColumnId = 'id';
public static string $databaseColumnId = 'id';
/**
* @brief [ActiveRecord] Database ID Fields
*/
protected static $databaseIdFields = array('media_full_path');
protected static array $databaseIdFields = array('media_full_path');
/**
* @brief [ActiveRecord] Multiton Map
*/
protected static $multitonMap = array();
protected static array $multitonMap = array();
/**
* @brief [Node] Parent Node ID Database Column
*/
public static $parentNodeColumnId = 'parent';
public static string $parentNodeColumnId = 'parent';
/**
* @brief [Node] Parent Node Class
*/
public static $parentNodeClass = 'IPS\cms\Media\Folder';
public static string $parentNodeClass = 'IPS\cms\Media\Folder';
/**
* @brief [Node] Parent ID Database Column
*/
public static $databaseColumnOrder = 'filename';
public static ?string $databaseColumnOrder = 'filename';
/**
* @brief [Node] Automatically set position for new nodes
*/
public static $automaticPositionDetermination = FALSE;
public static bool $automaticPositionDetermination = FALSE;
/**
* @brief [Node] Show forms modally?
*/
public static $modalForms = TRUE;
public static bool $modalForms = TRUE;
/**
* @brief [Node] Title
*/
public static $nodeTitle = 'page';
public static string $nodeTitle = 'page';
/**
* @brief [Node] ACP Restrictions
@@ -99,7 +117,7 @@ class _Media extends \IPS\Node\Model
'prefix' => 'foo_', // [Optional] Rather than specifying each key in the map, you can specify a prefix, and it will automatically look for restrictions with the key "[prefix]_add/edit/permissions/delete"
* @endcode
*/
protected static $restrictions = array(
protected static ?array $restrictions = array(
'app' => 'cms',
'module' => 'pages',
'prefix' => 'media_'
@@ -110,11 +128,20 @@ class _Media extends \IPS\Node\Model
*
* @return void
*/
public function setDefaultValues()
public function setDefaultValues() : void
{
$this->parent = 0;
$this->full_path = '';
}
/**
* @return string
*/
public function get_file_icon() : string
{
$extension = strtolower( substr( $this->file_object, strrpos( $this->file_object, '.' ) + 1 ) );
return File::$fileIconMap[ $extension ] ?? '';
}
/**
* Resets a media path
@@ -122,15 +149,15 @@ class _Media extends \IPS\Node\Model
* @param int $folderId Folder ID to reset
* @return void
*/
public static function resetPath( $folderId )
public static function resetPath( int $folderId ) : void
{
try
{
$path = \IPS\cms\Media\Folder::load( $folderId )->path;
$path = Folder::load( $folderId )->path;
}
catch ( \OutOfRangeException $ex )
catch ( OutOfRangeException $ex )
{
throw new \OutOfRangeException;
throw new OutOfRangeException;
}
$children = static::getChildren( $folderId );
@@ -147,10 +174,10 @@ class _Media extends \IPS\Node\Model
* @param INT $folderId Folder ID to fetch children from
* @return array
*/
public static function getChildren( $folderId=0 )
public static function getChildren( int $folderId=0 ) : array
{
$children = array();
foreach( \IPS\Db::i()->select( '*', static::$databaseTable, array( 'media_parent=?', \intval( $folderId ) ), 'media_filename ASC' ) as $child )
foreach( Db::i()->select( '*', static::$databaseTable, array( 'media_parent=?', intval( $folderId ) ), 'media_filename ASC' ) as $child )
{
$children[ $child[ static::$databasePrefix . static::$databaseColumnId ] ] = static::load( $child[ static::$databasePrefix . static::$databaseColumnId ] );
}
@@ -164,7 +191,7 @@ class _Media extends \IPS\Node\Model
* @param array $ids Array of IDs to remove
* @return void
*/
public static function deleteByFileIds( $ids=array() )
public static function deleteByFileIds( array $ids=array() ) : void
{
foreach( $ids as $id )
{
@@ -172,42 +199,35 @@ class _Media extends \IPS\Node\Model
{
static::load( $id )->delete();
}
catch( \Exception $ex ) { }
catch( Exception $ex ) { }
}
}
/**
* Get URL
*
* @return \IPS\Http\Url object
* @return Url|string|null object
*/
public function url()
function url(): Url|string|null
{
if ( \IPS\Theme::designersModeEnabled() )
{
return \IPS\Settings::i()->base_url . 'themes/cms/media/' . $this->full_path;
}
else
{
return (string)\IPS\File::get( 'cms_Media', $this->file_object )->url;
}
return (string)File::get( 'cms_Media', $this->file_object )->url;
}
/**
* [ActiveRecord] Delete Record
*
* @return void
* @return void
*/
public function delete()
public function delete(): void
{
try
{
if ( $this->file_object )
{
\IPS\File::get( 'cms_Media', $this->file_object )->delete();
File::get( 'cms_Media', $this->file_object )->delete();
}
}
catch( \Exception $ex ) { }
catch( Exception $ex ) { }
parent::delete();
}
@@ -215,11 +235,11 @@ class _Media extends \IPS\Node\Model
/**
* [Node] Get buttons to display in tree
*
* @param string $url Base URL
* @param bool $subnode Is this a subnode?
* @return array
* @param Url $url Base URL
* @param bool $subnode Is this a subnode?
* @return array
*/
public function getButtons( $url, $subnode=FALSE )
public function getButtons( Url $url, bool $subnode=FALSE ): array
{
$buttons = parent::getButtons( $url, $subnode );
$delete = NULL;
@@ -241,10 +261,10 @@ class _Media extends \IPS\Node\Model
}
$buttons['key'] = array(
'icon' => 'file-code-o',
'icon' => 'file-code',
'title' => 'cms_media_key',
'link' => \IPS\Http\Url::internal( 'app=cms&module=pages&controller=media&do=key&id=' . $this->id ),
'data' => array( 'ipsDialog' => '', 'ipsDialog-title' => \IPS\Member::loggedIn()->language()->addToStack('cms_media_key') )
'link' => Url::internal( 'app=cms&module=pages&controller=media&do=key&id=' . $this->id ),
'data' => array( 'ipsDialog' => '', 'ipsDialog-title' => Member::loggedIn()->language()->addToStack('cms_media_key') )
);
if ( $this->is_image )
@@ -252,8 +272,8 @@ class _Media extends \IPS\Node\Model
$buttons['preview'] = array(
'icon' => 'search',
'title' => 'cms_media_preview',
'link' => \IPS\Http\Url::internal( 'app=cms&module=pages&controller=media&do=preview&id=' . $this->id ),
'data' => array( 'ipsDialog' => '', 'ipsDialog-title' => \IPS\Member::loggedIn()->language()->addToStack('cms_media_preview') )
'link' => Url::internal( 'app=cms&module=pages&controller=media&do=preview&id=' . $this->id ),
'data' => array( 'ipsDialog' => '', 'ipsDialog-title' => Member::loggedIn()->language()->addToStack('cms_media_preview') )
);
}
@@ -269,15 +289,15 @@ class _Media extends \IPS\Node\Model
* [Node] Add/Edit Form
*
* @note This is not used currently. See \IPS\cms\modules\admin\media.php upload()
* @param \IPS\Helpers\Form $form The form
* @param Form $form The form
* @return void
*/
public function form( &$form )
public function form( Form &$form ) : void
{
/* Build form */
$form->add( new \IPS\Helpers\Form\Upload( 'media_filename', ( ( $this->filename ) ? \IPS\File::get( 'cms_Media', $this->file_object ) : NULL ), FALSE, array( 'obscure' => FALSE, 'maxFileSize' => 5, 'storageExtension' => 'cms_Media', 'storageContainer' => 'pages_media' ), NULL, NULL, NULL, 'media_filename' ) );
$form->add( new Upload( 'media_filename', ( ( $this->filename ) ? File::get( 'cms_Media', $this->file_object ) : NULL ), FALSE, array( 'obscure' => FALSE, 'maxFileSize' => 5, 'storageExtension' => 'cms_Media', 'storageContainer' => 'pages_media' ), NULL, NULL, NULL, 'media_filename' ) );
$form->add( new \IPS\Helpers\Form\Node( 'media_parent', $this->parent ? $this->parent : 0, FALSE, array(
$form->add( new Node( 'media_parent', $this->parent ?: 0, FALSE, array(
'class' => '\IPS\cms\Media\Folder',
'zeroVal' => 'node_no_parent'
) ) );
@@ -290,7 +310,7 @@ class _Media extends \IPS\Node\Model
* @param array $values Values from the form
* @return array
*/
public function formatFormValues( $values )
public function formatFormValues( array $values ): array
{
if ( isset( $values['media_parent'] ) AND ( ! empty( $values['media_parent'] ) OR $values['media_parent'] === 0 ) )
{
@@ -315,8 +335,7 @@ class _Media extends \IPS\Node\Model
$values['filename'] = $filename;
$values['filename_stored'] = $values['parent'] . '_' . $values['filename'];
$values['is_image'] = $values['media_filename']->isImage();
$values['file_object'] = (string) $values['media_filename'];
$values['file_object'] = (string) $values['media_filename'];
unset( $values['media_filename'] );
}
@@ -336,9 +355,9 @@ class _Media extends \IPS\Node\Model
* @param array $values Values from the form
* @return void
*/
public function postSaveForm( $values )
public function postSaveForm( array $values ) : void
{
$this->setFullPath( ( $this->parent ? \IPS\cms\Media\Folder::load( $this->parent )->path : '' ) );
$this->setFullPath( ( $this->parent ? Folder::load( $this->parent )->path : '' ) );
$this->save();
}
@@ -347,7 +366,7 @@ class _Media extends \IPS\Node\Model
*
* @return string
*/
public function getSortableName()
public function getSortableName() : string
{
return $this->full_path;
}
@@ -358,63 +377,21 @@ class _Media extends \IPS\Node\Model
* @param string $path Path to reset
* @return void
*/
public function setFullPath( $path )
public function setFullPath( string $path ) : void
{
$this->full_path = trim( $path . '/' . $this->filename, '/' );
$this->save();
}
/**
* Write media to disk for designer's mode
*
* @return void
*/
public static function exportDesignersModeMedia()
{
/* Make sure our media folder exists */
if ( !is_dir( \IPS\ROOT_PATH . '/themes/cms/media' ) )
{
mkdir( \IPS\ROOT_PATH . '/themes/cms/media', \IPS\IPS_FOLDER_PERMISSION );
chmod( \IPS\ROOT_PATH . '/themes/cms/media', \IPS\IPS_FOLDER_PERMISSION );
}
foreach( \IPS\Db::i()->select( '*', 'cms_media' ) as $media )
{
/* We could use recursive mode but it wouldn't correctly chmod the intermediate dirs */
$bits = explode( '/', "/themes/cms/media/" . $media['media_full_path'] );
$dir = '';
$filename = array_pop( $bits );
foreach( $bits as $part )
{
$dir .= $part . '/';
if ( ! is_dir( \IPS\ROOT_PATH . '/' . trim( $dir, '/' ) ) )
{
mkdir( \IPS\ROOT_PATH . '/' . trim( $dir, '/' ), \IPS\IPS_FOLDER_PERMISSION );
chmod( \IPS\ROOT_PATH . '/' . trim( $dir, '/' ), \IPS\IPS_FOLDER_PERMISSION );
}
}
try
{
\file_put_contents( \IPS\ROOT_PATH . '/' . trim( $dir, '/' ) . '/' . $filename, \IPS\File::get( 'cms_Media', $media['media_file_object'] )->contents() );
@chmod( \IPS\ROOT_PATH . '/' . trim( $dir, '/' ) . '/' . $filename, \IPS\IPS_FILE_PERMISSION );
}
catch( \RuntimeException $e ) { }
}
}
/**
* Removes folders that are empty
*
* @return void
*/
public static function removeEmptyFolders()
public static function removeEmptyFolders() : void
{
$folders = iterator_to_array( \IPS\Db::i()->select( 'DISTINCT(media_parent)', 'cms_media', array( 'media_parent > 0' ) ) );
$allFolders = iterator_to_array( \IPS\Db::i()->select( '*', 'cms_media_folders' )->setKeyField( 'media_folder_id' ) );
$folders = iterator_to_array( Db::i()->select( 'DISTINCT(media_parent)', 'cms_media', array( 'media_parent > 0' ) ) );
$allFolders = iterator_to_array( Db::i()->select( '*', 'cms_media_folders' )->setKeyField( 'media_folder_id' ) );
foreach( $folders as $id )
{
@@ -430,7 +407,7 @@ class _Media extends \IPS\Node\Model
break;
}
if ( ! \in_array( $currentParent, $folders ) )
if ( ! in_array( $currentParent, $folders ) )
{
$folders[] = $currentParent;
}
@@ -440,59 +417,7 @@ class _Media extends \IPS\Node\Model
}
}
\IPS\Db::i()->delete( 'cms_media_folders', array( \IPS\Db::i()->in( 'media_folder_id', array_values( $folders ), TRUE ) ) );
}
/**
* Import media from disk for designer's mode
*
* @return void
*/
public static function importDesignersModeMedia()
{
$path = \IPS\ROOT_PATH . '/themes/cms/media';
$seen = array();
if ( is_dir( $path ) )
{
static::importDesignersModeMediaRecurse( $seen, $path );
}
\IPS\Db::i()->delete( 'cms_media', array( \IPS\Db::i()->in( 'media_id', $seen, TRUE ) ) );
static::removeEmptyFolders();
}
/**
* Import media from disk for designer's mode recursive method
*
* @param array $seen Files we've seen already
* @param string $path Path to look in
* @return void
*/
public static function importDesignersModeMediaRecurse( &$seen, $path )
{
if ( is_dir( $path ) )
{
foreach ( new \DirectoryIterator( $path ) as $dir )
{
if ( $dir->isDot() || mb_substr( $dir->getFilename(), 0, 1 ) === '.' )
{
continue;
}
if ( $dir->isDir() )
{
static::importDesignersModeMediaRecurse( $seen, $path . '/' . $dir->getFilename() );
}
else
{
$contents = \file_get_contents( $dir->getRealPath() );
/* Create */
$seen[] = static::createMedia( trim( str_replace( str_replace( '\\', '/', \IPS\ROOT_PATH ) . '/themes/cms/media', '', str_replace( '\\', '/', $dir->getRealPath() ) ), '/' ), $contents );
}
}
}
Db::i()->delete( 'cms_media_folders', array( Db::i()->in( 'media_folder_id', array_values( $folders ), TRUE ) ) );
}
/**
@@ -500,49 +425,49 @@ class _Media extends \IPS\Node\Model
*
* @param string $path File path (/folder/file.txt)
* @param string $contents File contents
* @return int ID of existing media or of new media
* @return int|null ID of existing media or of new media
*/
public static function createMedia( $path, $contents )
public static function createMedia( string $path, string $contents ): ?int
{
try
{
$test = static::load( $path, 'media_full_path' );
$test->file_object = \IPS\File::create( 'cms_Media', $test->filename_stored, $contents, 'pages_media', TRUE, NULL, FALSE );
$test->file_object = File::create( 'cms_Media', $test->filename_stored, $contents, 'pages_media', TRUE, NULL, FALSE );
$test->save();
return $test->id;
}
catch( \RuntimeException $ex )
catch( RuntimeException $ex )
{
try
{
\IPS\File::get( 'cms_Media', $path );
File::get( 'cms_Media', $path );
}
catch( \Exception $x )
catch( Exception $x )
{
/* File doesn't exist already */
throw $ex;
}
}
catch( \OutOfRangeException $ex )
catch( OutOfRangeException $ex )
{
/* It doesn't exist */
$exploded = explode( '/', $path );
$filename = array_pop( $exploded );
$folderId = 0;
if ( \count( $exploded ) )
if ( count( $exploded ) )
{
$testDir = trim( implode( '/', $exploded ), '/' );
try
{
$test = \IPS\cms\Media\Folder::load( $testDir, 'media_folder_path' );
$test = Folder::load( $testDir, 'media_folder_path' );
/* Yep */
$folderId = $test->id;
}
catch( \OutOfRangeException $ex )
catch( OutOfRangeException $ex )
{
$testDir = '';
foreach( $exploded as $dir )
@@ -551,12 +476,12 @@ class _Media extends \IPS\Node\Model
try
{
$test = \IPS\cms\Media\Folder::load( $testDir, 'media_folder_path' );
$test = Folder::load( $testDir, 'media_folder_path' );
$folderId = $test->id;
}
catch( \OutOfRangeException $ex )
catch( OutOfRangeException $ex )
{
$folder = new \IPS\cms\Media\Folder;
$folder = new Folder;
$folder->parent = $folderId;
$folder->name = $dir;
$folder->path = $testDir;
@@ -567,17 +492,19 @@ class _Media extends \IPS\Node\Model
}
}
$media = new \IPS\cms\Media;
$media = new Media;
$media->parent = $folderId;
$media->filename = $filename;
$media->added = time();
$media->full_path = $path;
$media->filename_stored = $folderId . '_' . $filename;
$media->file_object = \IPS\File::create( 'cms_Media', $media->filename_stored, $contents, 'pages_media', TRUE, NULL, FALSE );
$media->file_object = File::create( 'cms_Media', $media->filename_stored, $contents, 'pages_media', TRUE, NULL, FALSE );
$media->is_image = $media->file_object->isImage();
$media->save();
return $media->id;
}
return null;
}
}