diff --git a/app/plugins/bookmarks/controllers/api/bookmarks.php b/app/plugins/bookmarks/controllers/api/bookmarks.php new file mode 100644 index 0000000..fbbfc32 --- /dev/null +++ b/app/plugins/bookmarks/controllers/api/bookmarks.php @@ -0,0 +1,59 @@ + + * @link https://TheTempusProject.com + * @license https://opensource.org/licenses/MIT [MIT LICENSE] + */ +namespace TheTempusProject\Controllers\Api; + +use TheTempusProject\Models\User; +use TheTempusProject\Classes\ApiController; +use TheTempusProject\Houdini\Classes\Views; +use TheTempusProject\Canary\Bin\Canary as Debug; +use TheTempusProject\Bedrock\Functions\Input; +use TheTempusProject\Models\Bookmarks as Bookmark; + +class Bookmarks extends ApiController { + protected static $bookmarks; + + public function __construct() { + parent::__construct(); + self::$bookmarks = new Bookmark; + } + + public function create() { + header('Access-Control-Allow-Origin: *'); + + $user = self::$authToken->createdBy; + + $payload = @file_get_contents('php://input'); + $payload = json_decode( $payload, true ); + Debug::error($payload['name']); + + $result = self::$bookmarks->create( + $payload['name'], + $payload['url'], + 0, + $payload['notes'], + 'default', + 'private', + 'external', + $user + ); + + if ( ! $result ) { + $responseType = 'error'; + $response = 'There was an error creating your bookmark.'; + } else { + $responseType = 'success'; + $response = 'success'; + } + Views::view( 'api.response', ['response' => json_encode( [ $responseType => $response ], true )]); + } +} + diff --git a/app/plugins/bookmarks/controllers/bookmarks.php b/app/plugins/bookmarks/controllers/bookmarks.php index 3641faa..0f2595c 100644 --- a/app/plugins/bookmarks/controllers/bookmarks.php +++ b/app/plugins/bookmarks/controllers/bookmarks.php @@ -108,7 +108,6 @@ class Bookmarks extends Controller { } Navigation::setCrumbComponent( 'BookmarkBreadCrumbs', 'bookmarks/bookmarks/' . $id ); - $bookmarks = self::$bookmarks->noFolder(); $panelArray = []; @@ -399,4 +398,118 @@ class Bookmarks extends Controller { Session::flash( 'success', 'Bookmark data refreshed.' ); return Redirect::to( 'bookmarks/bookmark/' . $bookmark->ID ); } + + public function import() { + // echo '
'; + + if ( ! Input::exists('submit') ) { + return Views::view( 'bookmarks.import' ); + } + + if ( ! Forms::check( 'importBookmarks' ) ) { + Issues::add( 'error', [ 'There was an error importing your bookmarks.' => Check::userErrors() ] ); + return Views::view( 'bookmarks.import' ); + } + + if (isset($_FILES['bookmark_file'])) { + $file = $_FILES['bookmark_file']; + + // Check file size + if ($file['size'] > 1024 * 1024) { // 1024 KB = 1 MB + die('The file is too large. Maximum size is 1 MB.'); + } + + // Check file extension + $fileExtension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION)); + if ($fileExtension !== 'html') { + die('Invalid file type. Only .html files are allowed.'); + } + + // Proceed with file processing + $fileContent = file_get_contents($file['tmp_name']); + } else { + die('No file was uploaded.'); + } + + $out = $this->parseBookmarks($fileContent); + $date = 'today'; + $description = 'Imported on ' . $date . ' from file: ' . $file['name']; + + $importFolder = self::$folders->create( 'New Import', 0, $description ); + // $importFolder = 1; + // echo 'make import folder: ' . PHP_EOL; + foreach ($out as $folder => $bookmarks) { + // echo 'make folder: ' . $folder . PHP_EOL; + $currentFolder = self::$folders->create( $folder, $importFolder, $description ); + foreach ($bookmarks as $index => $bookmark) { + // echo 'make folder: ' . $bookmark['url'] . PHP_EOL; + self::$bookmarks->create( $bookmark['name'], $bookmark['url'], $currentFolder); + } + } + + Session::flash( 'success', 'Your Bookmark has been created.' ); + Redirect::to( 'bookmarks/bookmarks/'. $importFolder ); + // echo ''; + // exit; + // dv ( $out ); + } + + public function parseBookmarks($htmlContent) + { + $started = false; + $out = []; + $currentFolder = []; + $folderName = ['unknown']; + $lines = explode("\n", $htmlContent); + foreach ($lines as $line) { + if ( $started == false ) { + if (preg_match("/
/i', $line, $matches)) { + continue; + } + + if (preg_match('/