mostly filebased Content Presentation System
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

909 行
35KB

  1. <?php
  2. namespace Modules;
  3. class FilesInFolders {
  4. private $folder;
  5. public $content = array();
  6. public $extras = array();
  7. private $domains = array('default'=>'default');
  8. private $keyfiles = array();
  9. public $structs = array();
  10. private $EXT=array(
  11. 'txt'=>array( 'txt', 'text', 'md' ),
  12. 'pic'=>array( 'jpg', 'jpeg', 'png', 'svg' ),
  13. 'tpl'=>array( 'html', 'htm', 'tpl' ),
  14. 'audio'=>array('mp3','wav','ogg'),
  15. 'csv'=>array( 'csv' )
  16. );
  17. public $config = array();
  18. private $state=array();
  19. function __construct($folder,$conf=array()) {
  20. debug("about to construct FIF: $folder");
  21. $f3 = \Base::instance();
  22. if(is_dir($folder)) { // are we given a valid path?
  23. $this->folder = $folder;
  24. } else { // and if not?
  25. $this->folder = $f3->get('CONTENT')."./";
  26. }
  27. if(is_array($conf)) {
  28. foreach($conf as $key=>$value) {
  29. switch($key) {
  30. case 'content':
  31. if(is_array($value)) {
  32. foreach($value as $k=>$v) {
  33. $this->domains[$k] = $v;
  34. }
  35. }
  36. break;
  37. case 'keyfiles':
  38. if(is_array($value)) {
  39. foreach($value as $k=>$v) {
  40. $this->keyfiles[$k] = $v;
  41. }
  42. }
  43. break;
  44. }
  45. }
  46. }
  47. foreach($this->domains as $domain) {
  48. $this->content[$domain] = array();
  49. }
  50. foreach($this->keyfiles as $keys=>$d) {
  51. $this->extras[$keys] = "";
  52. }
  53. debug("constructed files in folder: $folder");
  54. }
  55. /////////////////////////////
  56. // read folder into struct //
  57. /////////////////////////////
  58. function prepare_files() {
  59. foreach ($this->domains as $k=>$v) {
  60. foreach($this->EXT as $cat=>$endings) {
  61. $this->structs[$k][$cat]=array();
  62. }
  63. }
  64. $ls = scandir($this->folder);
  65. foreach ($ls as $k=>$f) {
  66. if (!strncmp($f,'.',1)) continue; // ignore hidden files
  67. $ex=explode(".", $f);
  68. $ext=strtolower(end($ex));
  69. if (array_key_exists($ex[0],$this->domains)) {
  70. $domain_key=$ex[0];
  71. $sort_key=1;
  72. } elseif (array_key_exists($ex[0], $this->keyfiles)) {
  73. if(in_array($ext,$this->EXT[$this->keyfiles[$ex[0]]['type']])) {
  74. $this->extras[$ex[0]]=$this->folder.$f;
  75. continue;
  76. }
  77. $domain_key='default';
  78. $sort_key=0;
  79. } else {
  80. $domain_key='default';
  81. $sort_key=0;
  82. }
  83. foreach ($this->EXT as $cat=>$endings) {
  84. if (in_array($ext, $endings)) {
  85. $this->structs[$domain_key][$cat][$ex[$sort_key]] = $this->folder.$f;
  86. break;
  87. }
  88. }
  89. }
  90. foreach($this->keyfiles as $key=>$param) {
  91. if(!$this->extras[$key]) {
  92. $this->extras[$key] = self::search_up(
  93. $key,
  94. array($this->folder,$param['until']),
  95. $this->EXT[$param['type']]
  96. );
  97. }
  98. if($this->extras[$key]) {
  99. if ($param['type'] == 'txt') {
  100. $this->read_textfile($this->extras[$key]);
  101. }
  102. }
  103. }
  104. }
  105. ///////////////////////////////////////
  106. // prepare content as per the struct //
  107. ///////////////////////////////////////
  108. function fill_content() {
  109. $f3 = \Base::instance();
  110. $md = new \freaParsedown();
  111. $md->deactivate_ol();
  112. //var_dump($md->get_BlockTypes());
  113. foreach($this->domains as $domain_key=>$domain) {
  114. // don't act on hidden files
  115. if ($domain == 'hidden') { continue; }
  116. $this->state['current_domain'] = $domain_key;
  117. foreach($this->structs[$domain_key]['txt'] as $key=>$file) {
  118. $str = $this->read_textfile($file);
  119. $str = self::content_element_dispatcher($str);
  120. $str = $md->text($str);
  121. //$str = sprintf("%s", $str);
  122. $this->content[$domain][$key."30text"] = sprintf(
  123. "<div class=\"item %s %s\"><a name=\"%s\"></a>%s</div>",
  124. $page,
  125. $key,
  126. $key,
  127. $str
  128. );
  129. }
  130. $includeTemplates = array_key_exists('includeTemplates', $this->config)
  131. ? $this->config['includeTemplates']
  132. : $f3->get('includeTemplates') ? : FALSE
  133. ;
  134. if ($includeTemplates) {
  135. foreach($this->structs[$domain_key]['tpl'] as $key=>$file) {
  136. $str = \Template::instance()->render(substr($file,7));
  137. $str = self::linkify($str);
  138. $this->content[$domain][$key."00tpl"] = sprintf(
  139. "<div class=\"item %s %s\">%s</div>",
  140. $page,
  141. $key,
  142. $str
  143. );
  144. }
  145. }
  146. $bulkIncludePic = array_key_exists('bulkIncludePic',$this->config)
  147. ? $this->config['bulkIncludePic']
  148. : $f3->get('bulkIncludePic') ? : FALSE
  149. ;
  150. if ($bulkIncludePic) {
  151. foreach($this->structs[$domain_key]['pic'] as $key=>$file) {
  152. $image_include_version = 2;
  153. switch ($image_include_version) {
  154. case 1:
  155. $this->content[$domain][$key."10image"] = sprintf(
  156. "<img class=\"bulkImportedImage $bulkIncludePic\" src=\"/$file\" />"
  157. );
  158. break;
  159. case 2:
  160. $module = new CEimage(['image',$file,'','gallery']);
  161. $this->content[$domain][$key."10image"] = $module->index();
  162. unset($module);
  163. break;
  164. case 3:
  165. //$this->content[$domain][] = "asdasd";
  166. break;
  167. }
  168. }
  169. }
  170. $bulkIncludeAudio = array_key_exists('bulkIncludeAudio',$this->config)
  171. ? $this->config['bulkIncludeAudio']
  172. : $f3->get('bulkIncludeAudio') ? : FALSE
  173. ;
  174. if ($bulkIncludeAudio) {
  175. foreach ($this->structs[$domain_key]['audio'] as $key=>$file) {
  176. $this->content[$domain][$key."20audio"] = sprintf(
  177. '<a href="%s" class="audio">%s</a><br>',
  178. $file, $key
  179. );
  180. }
  181. }
  182. foreach($this->structs[$domain_key]['csv'] as $key=>$file) {
  183. $csv = new \Modules\Ography($file,TRUE);
  184. $str="<table>";
  185. foreach($csv->entries as $entry) {
  186. $tmp="";
  187. foreach($entry as $key=>$value) {
  188. $tmp .= sprintf("<td class=\"%s\">%s</td>", $key, $value);
  189. }
  190. $str .= sprintf("<tr>%s</tr>",$tmp);
  191. }
  192. $str.="</table>";
  193. $this->content[$domain][$key."30csv"] = $str;
  194. }
  195. }
  196. }
  197. //////////////////////
  198. // read config data //
  199. //////////////////////
  200. function read_config($domain=false) {
  201. foreach ($this->domains as $source=>$destination) {
  202. if (is_string($domain)) {
  203. if ($source != $domain) { continue; }
  204. } elseif (is_array($domain)) {
  205. if (!in_array($source,$domain)) { continue; }
  206. }
  207. foreach ($this->structs[$source]['txt'] as $key=>$file) {
  208. $this->read_textfile($file);
  209. }
  210. }
  211. return $this->config;
  212. }
  213. ////////////////
  214. // recursions //
  215. ////////////////
  216. function search_up($key,$paths,$ext) {
  217. $return = "";
  218. if(count($paths) == 2) {
  219. $current = $paths[0];
  220. $last_try = $paths[1];
  221. $ls=scandir($current);
  222. foreach($ls as $f) {
  223. if(!strncmp($f,'.',1)) continue; // ignore hidden files
  224. $ex=explode(".", $f);
  225. if(in_array(strtolower(end($ex)),$ext)) {
  226. if($ex[0]==$key) {
  227. $return = $current.$f;
  228. break;
  229. }
  230. }
  231. }
  232. }
  233. if ($return) {
  234. return $return;
  235. } elseif($current == $last_try) {
  236. return false;
  237. } else {
  238. $p = explode('/',$current);
  239. array_pop($p);
  240. array_pop($p);
  241. return self::search_up($key,array(implode("/",$p)."/",$last_try),$ext);
  242. }
  243. }
  244. ///////////////////////
  245. // Utility functions //
  246. ///////////////////////
  247. function read_textfile($file) {
  248. debug("about to read file: $file");
  249. $str = file_get_contents($file);
  250. debug("read file: $file");
  251. $str = self::linkify($str);
  252. $str = self::strip_comments($str);
  253. $str = self::get_config_from_content($str);
  254. debug("processed file: $file");
  255. return $str;
  256. }
  257. function strip_comments($str) {
  258. $single_line_comments = "/(^;.*\R)/m";
  259. $str = preg_replace($single_line_comments,"",$str);
  260. $multi_line_comments = "/\/\*.*?\*\//s";
  261. $str = preg_replace($multi_line_comments,"",$str);
  262. return $str;
  263. }
  264. function linkify($string) {
  265. $pattern = "/\s@(\w+)[=]([\w,]+)\s/";
  266. $count = 0;
  267. $new = preg_replace_callback
  268. ($pattern,
  269. function($m){
  270. $f3 = \Base::instance();
  271. return $f3->get('SITE_URL')
  272. .$f3->alias($m[1],self::$keyword."=".$m[2])
  273. ;},
  274. $string);
  275. return $new;
  276. }
  277. function get_config_from_content($string) {
  278. $f3 = \Base::instance();
  279. $f = 0;
  280. $pattern = "/#\+(\w+):\s?(.*)/";
  281. $f = preg_match_all($pattern, $string,$matches,PREG_PATTERN_ORDER);
  282. for ($i=0;$i<$f;$i++) {
  283. $string = str_replace($matches[0][$i],"",$string);
  284. $key = $matches[1][$i];
  285. $value = trim($matches[2][$i]);
  286. if(strtolower($value) == "false") {
  287. $value = FALSE;
  288. }
  289. if(!strncmp(trim($value),'@',1)) {
  290. //var_dump($f3->get('DATA'));
  291. if (array_key_exists($key,$f3->get('DATA'))) {
  292. $DATA = $f3->get('DATA.'.$key);
  293. $conf = array($DATA['type'],$DATA['dir']);
  294. $relation = new \Modules\TOC($conf,$f3->get('CONTENT'),substr($value,1));
  295. $relation->dispatch();
  296. $this->config[$key]=array_shift($relation->entries);
  297. } else {
  298. $this->config[$key]=$value;
  299. }
  300. } else {
  301. $this->config[$key]=$value;
  302. }
  303. }
  304. $pattern = "/§>\s*(\w+):(.*?)\R[\011\040]*\R/s";
  305. $f = preg_match_all($pattern, $string,$matches,PREG_PATTERN_ORDER);
  306. for ($i=0;$i<$f;$i++) {
  307. $string = str_replace($matches[0][$i],"",$string);
  308. $key = $matches[1][$i];
  309. $value = trim($matches[2][$i]);
  310. if(strtolower($value) == "false") {
  311. $value = FALSE;
  312. }
  313. if (!strncmp($value,'@',1)) {
  314. # var_dump();
  315. if (array_key_exists($key,$f3->get('DATA'))) {
  316. $entries = explode("@",$value);
  317. array_shift($entries); // first entry is always empty
  318. $DATA = $f3->get('DATA.'.$key);
  319. $conf = array($DATA['type'],$DATA['dir']);
  320. $relation = new \Modules\TOC($conf,$f3->get('CONTENT'),$entries);
  321. $relation->dispatch();
  322. if(/*count($entries) >*/ 1) {
  323. $this->config[$key]= new CMultiple($relation->entries);
  324. } else {
  325. $this->config[$key]=array_shift($relation->entries);
  326. }
  327. } else {
  328. $this->config[$key]=$value;
  329. }
  330. } else {
  331. $this->config[$key]=$value;
  332. }
  333. }
  334. return $string;
  335. }
  336. function content_element_dispatcher($string) {
  337. $f3 = \Base::instance();
  338. $md = new \freaParsedown();
  339. $md->deactivate_ol();
  340. $f0 = 0;
  341. // find occorances of {| keyword |}
  342. $pattern = "/\{\|(.+?)\|\}/s";
  343. $f = preg_match_all($pattern, $string,$matches,PREG_PATTERN_ORDER);
  344. for ($i=0;$i<$f;$i++) {
  345. $body = preg_split("/\R/",trim($matches[1][$i]));
  346. $request = explode(":", trim(array_shift($body)));
  347. $new="";
  348. switch($request[0]) {
  349. case 'test':
  350. $new="seems to work";
  351. break;
  352. case ';':
  353. $new="";
  354. break;
  355. case 'path':
  356. $new="/".$this->folder;
  357. break;
  358. case 'space':
  359. $new=sprintf("<div style=\"height:%s;\"></div>",
  360. $request[1]
  361. );
  362. break;
  363. case 'span':
  364. $new=sprintf("<span class=\"%s\">%s</span>",
  365. $request[1],
  366. $request[2] ? : implode("\n",$body)
  367. );
  368. break;
  369. case 'small-text':
  370. if(count($body)) {
  371. $new=sprintf("<div class=\"smalltext\">%s</div>",
  372. implode("\n",$body)
  373. );
  374. } else {
  375. $new=sprintf("<span class=\"smalltext\">%s</span>",
  376. $request[1]
  377. );
  378. }
  379. break;
  380. case 'TOC':
  381. // throw away TOC part of request, we don't need it
  382. array_shift($request);
  383. $toc = new \Modules\TOC($request,$this->folder,$body);
  384. $toc->dispatch();
  385. $new=sprintf("<div class=\"TOC %s\">%s</div>",
  386. array_shift($request),
  387. $toc);
  388. break;
  389. case 'sql':
  390. //array_shift($request);
  391. switch ($request[1]) {
  392. case 'sqlite':
  393. $array = [];
  394. $db = new \DB\SQL('sqlite:' . $this->folder . $request[2]);
  395. $sql = implode(" ", $body);
  396. if ( strpos($sql,'insert')===false &&
  397. strpos($sql,'update')===false &&
  398. strpos($sql,'drop')===false) {
  399. $rows = $db->exec($sql);
  400. foreach ($rows as $res) {
  401. switch (count($res)) {
  402. case 1:
  403. $keys=array_keys($res);
  404. $array[] = $res[$keys[0]];
  405. break;
  406. case 2:
  407. $keys=array_keys($res);
  408. $array[$res[$keys[0]]] = $res[$keys[1]];
  409. break;
  410. }
  411. }
  412. }
  413. $new = sprintf("<ul><li>%s</li></ul>",
  414. implode("</li><li>",$array));
  415. unset($array);
  416. break;
  417. }
  418. break;
  419. case 'form':
  420. $token_db = $f3->get('TEMP') . "CEform/";
  421. $form_config_file = sprintf("%s%s.cfg",
  422. ROOT.$this->folder,
  423. $request[1]);
  424. $db = new \DB\Jig($token_db,\DB\Jig::FORMAT_JSON);
  425. $formcall = new \DB\Jig\Mapper($db,'form_calls');
  426. $timestamp = time();
  427. $token = md5($timestamp . $form_config_file . rand(100,999));
  428. $f3->config($form_config_file);
  429. $fields = $f3->get('fields');
  430. foreach ($fields as $k => $v) {
  431. if ($v['type'] == 'custom') {
  432. $fields[$k]['template'] = sprintf(
  433. "%s%s",
  434. substr($this->folder,8), //assumens content folder is content
  435. $v['template']
  436. );
  437. }
  438. if (array_key_exists('db',$v)) {
  439. switch ($v['db']['type']) {
  440. case 'sqlite':
  441. $array = [];
  442. $db = new \DB\SQL('sqlite:' . $this->folder . $v['db']['file']);
  443. $sql = $v['db']['sql'];
  444. if ( strpos($sql,'insert')===false &&
  445. strpos($sql,'update')===false &&
  446. strpos($sql,'drop')===false) {
  447. $rows = $db->exec($sql);
  448. foreach ($rows as $res) {
  449. switch (count($res)) {
  450. case 2:
  451. $keys=array_keys($res);
  452. $array[$res[$keys[0]]] = $res[$keys[1]];
  453. break;
  454. }
  455. }
  456. }
  457. $fields[$k]['el'] = $array;
  458. unset($array);
  459. break;
  460. }
  461. }
  462. }
  463. $f3->set('fields',
  464. array_merge(
  465. $fields,
  466. ['xss-token'=>[
  467. 'type'=>'hidden',
  468. 'value'=>$token,
  469. 'length'=>strlen($token)
  470. ]]
  471. ));
  472. $formcall->token = $token;
  473. $formcall->timestamp = $timestamp;
  474. $formcall->form = $form_config_file;
  475. $formcall->path = ROOT.$this->folder;
  476. $formcall->save();
  477. $form_view = new \Template;
  478. $new=$form_view->render('form.htm');
  479. break;
  480. case 'box':
  481. array_shift($request); //get rid of identifier
  482. $type = array_shift($request);
  483. $pics = explode(":",array_shift($body));
  484. $pic = $pics[0];
  485. $pic_hover = count($pics) > 1 ? $pics[1] : $pic;
  486. if (count($body) >= 3) {
  487. $caption = ['normal'=>['cap1' => array_shift($body),
  488. 'cap2' => array_shift($body)],
  489. 'hover'=> ['cap1' => array_shift($body),
  490. 'cap2' => array_shift($body)]];
  491. $caption_html=[];
  492. foreach ($caption as $state => $set) {
  493. if(!$set['cap2']) {
  494. $caption_html[$state] = [
  495. sprintf('<span class="first">&nbsp;</span>'),
  496. sprintf('<span class="first">%s</span>',$set['cap1'])
  497. ];
  498. } else if (!$set['cap1'] && $set['cap2']) {
  499. $caption_html[$state] = [
  500. sprintf('<span class="second">%s</span>',$set['cap2'])
  501. ];
  502. } else {
  503. $caption_html[$state] = [
  504. sprintf('<span class="first">%s</span>',$set['cap1']?:"&nbsp;"),
  505. sprintf('<span class="second">%s</span>',$set['cap2'])
  506. ];
  507. }
  508. }
  509. $has_caption = TRUE;
  510. } else {
  511. $has_caption = FALSE;
  512. }
  513. if (file_exists($this->folder.$pic)) {
  514. $pic = $this->folder.$pic;
  515. } else {
  516. $pic = $f3->get('RESOURCES')."img/default_img.png";
  517. }
  518. if (file_exists($this->folder.$pic_hover)) {
  519. $pic_hover = $this->folder.$pic_hover;
  520. } else {
  521. $pic_hover = $pic;
  522. }
  523. if(0) {
  524. $PIC = new \Image($pic);
  525. $orientation = $PIC->width() > $PIC->height()
  526. ? 'landscape'
  527. : 'portrait'
  528. ;
  529. unset($PIC);
  530. } else {
  531. list($wwidth, $hheight) = getimagesize($pic);
  532. $orientation = $wwidth > $hheight ? 'landscape' : 'portrait';
  533. }
  534. $pic = new CachedImage($pic);
  535. $pic_hover = new CachedImage($pic_hover);
  536. $class="";
  537. $add="";
  538. switch($type) {
  539. case 'plain':
  540. $link=false;
  541. break;
  542. case 'download':
  543. $file = "/".$this->folder.implode(":",$request);
  544. $link='href="'.$file.'" download ';
  545. break;
  546. case 'lightbox':
  547. $body = implode("\n",$body);
  548. if (count($request) % 2) {
  549. $class = array_pop($request);
  550. }
  551. if (count($request) >= 2) {
  552. $body=str_replace([$request[0],$request[1]],["{|","|}"],$body);
  553. $body=$this->content_element_dispatcher($body);
  554. }
  555. $hash=md5($body);
  556. $add=sprintf("<div id=\"%s\" class=\"content_elment_box_body\">\n%s\n</div>",
  557. $hash,
  558. $md->text($body));
  559. $link='href="#" data-featherlight="#'.$hash.'" ';
  560. break;
  561. case 'internal':
  562. $dest=implode(":",$request);
  563. $data = [];
  564. $base = substr($dest,0,strpos($dest,'?') ? : strlen($dest));
  565. parse_str(parse_url($dest,PHP_URL_QUERY),$data);
  566. $fragment = parse_url($dest,PHP_URL_FRAGMENT);
  567. if ($f3->get('LANG') != $f3->get('default_lang')) {
  568. if (!array_key_exists('lang',$data)) {
  569. $data['lang'] = $f3->get('LANG');
  570. }
  571. }
  572. $new_dest = $base;
  573. if (count($data) > 0) {
  574. $new_dest .= "?" . http_build_query($data);
  575. }
  576. if ($fragment) {
  577. $new_dest .= "#" . $fragment;
  578. }
  579. //if ($f3->get('LANG') != $f3->get('default_lang')) {
  580. // $dest .= "?lang=".$f3->get('LANG');
  581. //}
  582. $link=sprintf('href="%s" ',$f3->get('SITE_URL')."/".$new_dest);
  583. break;
  584. case 'external':
  585. $dest=implode(":",$request);
  586. $target = $f3->get('external_links_open_in_new_window')
  587. ? 'target="_blank"'
  588. : ''
  589. ;
  590. $link=sprintf('href="%s" %s',$dest, $target);
  591. break;
  592. default:
  593. $link='href="#"';
  594. break;
  595. }
  596. $new=sprintf("<div class=\"brick %s\">\n<a class=\"content_element_box\" %s>\n<div class=\"content_element_box\">
  597. <div class=\"image\">
  598. <img class=\"standard\" src=\"%s\" /><img class=\"hover\" src=\"%s\" />
  599. </div>
  600. %s
  601. %s
  602. \n</div>\n</a>\n%s\n</div>",
  603. implode(" ",[$orientation,$class,$type]),
  604. $link,
  605. $pic->get_src(1600),
  606. $pic_hover->get_src(1600),
  607. ($has_caption ? "<div class=\"caption standard\">".implode("<br>",$caption_html['normal'])."</div>": " "),
  608. ($has_caption ? "<div class=\"caption hover\">".implode("<br>",$caption_html['hover'])."</div>" : " " ),
  609. $add
  610. );
  611. break;
  612. case 'brick':
  613. array_shift($request);
  614. $class = array_shift($request);
  615. $new = sprintf("<div class=\"content_element brick %s\">\n%s\n</div>",
  616. $class,
  617. $md->text(implode("\n",$body))
  618. );
  619. break;
  620. case 'calendar':
  621. array_shift($request);
  622. $conf = array('path', $this->folder);
  623. $v = $this->read_config();
  624. $cal = new \Modules\CCalendar($v,$conf);
  625. $new=sprintf("<div class=\"calendar\">\n%s\n</div>",
  626. $cal);
  627. break;
  628. case 'header':
  629. array_shift($request);
  630. $conf = array('path', $this->folder);
  631. $v = $this->read_config();
  632. switch (array_shift($request)) {
  633. case 'event':
  634. $el = new CEvent($v,$conf);
  635. $el->set_layout('archive');
  636. $new = $el;
  637. break;
  638. case 'concert':
  639. $el = new CConcert($v,$conf);
  640. $el->set_layout('header');
  641. $new = $el;
  642. break;
  643. }
  644. break;
  645. case 'image':
  646. $module = new CEimage($request, $body, $this->structs, $this->content);
  647. $new = $module->index();
  648. unset($module);
  649. break;
  650. case 'devide':
  651. $contents = explode($request[1],implode("\n",$body));
  652. $c=count($contents);
  653. $str="";
  654. for ($iiii=0;$iiii<$c;$iiii++) {
  655. $str .= sprintf(" %f%%",100/$c);
  656. }
  657. $template = sprintf('grid-template-columns:%s;',
  658. $str);
  659. $counter=0;
  660. $new = sprintf('<div class="content_element_devided" style="%s">',$template);
  661. foreach($contents as $part) {
  662. $counter++;
  663. if (count($request) >= 4) {
  664. $part=str_replace([$request[2],$request[3]],["{|","|}"],$part);
  665. $part=$this->content_element_dispatcher($part);
  666. }
  667. $new .= sprintf("<div>\n%s\n</div>",
  668. $md->text($part)
  669. );
  670. }
  671. $new.="</div>";
  672. break;
  673. case 'page':
  674. array_shift($request);
  675. $target = array_shift($request);
  676. $class = array_shift($request);
  677. $folder = $this->folder.$target."/";
  678. $anchor_name=array_pop(explode("/",$target));
  679. $fff = new \Modules\FilesInFolders(
  680. $folder,
  681. array(
  682. 'content'=>array(
  683. 'secondary'=>'secondary',
  684. 'zzz'=>'hidden',
  685. 'unpublish'=>'hidden'
  686. ))
  687. );
  688. $fff->prepare_files();
  689. $fff->fill_content();
  690. $new = sprintf("<div class=\"content_element_page %s\"><a name=\"%s\"></a>\n%s\n</div>",
  691. $class,
  692. $anchor_name,
  693. implode("\n",$fff->content['default']));
  694. break;
  695. case 'youtube':
  696. $vid=array_shift($request);
  697. $pos= array_shift($request);
  698. if( in_array($pos,array('left','right','full'))) {
  699. $class = $pos;
  700. } else {
  701. $class = 'left';
  702. }
  703. $video=sprintf("<iframe width=\"700\" height=\"394\" class=\"ytvideo\" "
  704. ."src=\"https://www.youtube.com/embed/%s\"></iframe>",
  705. $vid);
  706. $thumbnail = sprintf("<div class=\"video-thumbnail\"><a href=\"%s\" data-featherlight=\"#%s\">"
  707. ."<img class=\"thumbnail\" src=\"https://img.youtube.com/vi/%s/mqdefault.jpg\" alt=\"video-preview\"/>"
  708. ."<img class=\"play-button\" src=\"%s\" alt=\"play-button\" />"
  709. ."</a></div><div class=\"lightbox\" id=\"%s\" >%s</div>",
  710. "https://www.youtube.com/watch?v=".$vid,
  711. $vid,
  712. $vid,
  713. "/rsc/img/play-button.png",
  714. $vid,
  715. $video
  716. );
  717. foreach ($body as $k=>$line) {
  718. if (strpos($line,"©") !== FALSE
  719. || strpos($line,"&copy;") !== FALSE) {
  720. $body[$k] = sprintf("<span class=\"copyright\">%s</span>",$line);
  721. }
  722. }
  723. $new=sprintf("<div class='video-container %s'>"
  724. ."<div class=\"media\">%s</div>"
  725. ."<div class=\"caption\">%s</div>"
  726. ."</div>",
  727. $class,
  728. $thumbnail,
  729. $md->text(implode("\n",$body)));
  730. break;
  731. default:
  732. $new=self::warn("Content Module \"".$request[0]."\" unknown");
  733. break;
  734. }
  735. $string = str_replace($matches[0][$i],$new,$string);
  736. }
  737. return $string;
  738. }
  739. function warn($message) {
  740. return sprintf("<div class=\"warning\">%s</div>",$message);
  741. }
  742. }
  743. class CEimage
  744. {
  745. var $r;
  746. var $b;
  747. var $structs;
  748. var $content;
  749. function __construct($request,$body=[],$structs=false,$content=false) {
  750. $this->r = $request;
  751. $this->b = $body;
  752. $this->structs = &$structs;
  753. $this->content = &$content;
  754. }
  755. function index() {
  756. $md = new \freaParsedown();
  757. $request = $this->r;
  758. $body = $this->b;
  759. $key=$request[1];
  760. $image="";
  761. $new="";
  762. // see if we can find image
  763. if ( is_file($request[1])) {
  764. $image= $request[1];
  765. } else if ($this->structs) {
  766. foreach($this->structs as $domain=>$destination) {
  767. if(array_key_exists($key, $this->structs[$domain]['pic'])) {
  768. $image = $this->structs[$domain]['pic'][$key];
  769. unset($this->structs[$domain]['pic'][$key]);
  770. unset($this->content[$this->domains[$domain]][$key]);
  771. break;
  772. }
  773. }
  774. }
  775. if ($image) {
  776. // image positioning
  777. if( in_array($request[2],array('left','right','full'))) {
  778. $class = $request[2];
  779. } else {
  780. $class = 'full';
  781. }
  782. if (isset($request[3])) {
  783. $gallery = 'gallery';
  784. } else {
  785. $gallery = 'fl';
  786. }
  787. // image orientation?
  788. $img = new \Image($image);
  789. // $ratio = "landscape";
  790. $ratio = ($img->width() >= $img->height())
  791. ? "landscape"
  792. : "portrait"
  793. ;
  794. unset($img);
  795. $cached = new CachedImage($image);
  796. // identify if caption contains a copyright note
  797. // if so, indicate with a class
  798. foreach ($body as $k=>$line) {
  799. if (strpos($line,"©") !== FALSE
  800. || strpos($line,"&copy;") !== FALSE) {
  801. $body[$k] = sprintf("<span class=\"copyright\">%s</span>",$line);
  802. }
  803. }
  804. $new=sprintf("<div class='content_element_image %s'>"
  805. ."<div class=\"media %s\"><a href=\"%s\" class=\"$gallery\"><img src=\"%s\" alt=\"user supplied image\" /></a></div>"
  806. ."<img src=\"%s\" style=\"display:none;\" alt=\"user supplied image\" />"
  807. ."<div class=\"caption\">%s</div>"
  808. ."</div>",
  809. $class,
  810. $ratio,
  811. $cached->get_src(1600),
  812. $cached->get_src(1600),
  813. $cached->get_src(1600),
  814. $md->text(implode("\n",$body))
  815. );
  816. unset($cached);
  817. } else {
  818. // image not found
  819. $new=sprintf("<div class='content_element_image %s'>\n"
  820. ."<div class=\"media %s\">\n"
  821. ."<div class=\"caption\">\n%s\n</div>\n"
  822. ."</div>\n",
  823. $class,
  824. $ratio,
  825. $md->text(implode("\n",$body))
  826. );
  827. }
  828. return $new;
  829. }
  830. }