Пример #1
0
 private function regenerate()
 {
     $query = "  SELECT pages.slug, page_paginator.name, pagePathByCategoryId(pages.categoryid) as path \n                    FROM pages\n                    INNER JOIN page_paginator \n                        ON pages.id = page_paginator.pageId ";
     $select = $this->storage->query($query);
     $lists = array();
     do {
         $lists = $select->fetchAll();
     } while ($select->nextRowset() && $select->columnCount());
     if (empty($lists)) {
         return;
     }
     // $cachepath = $this->pageConf['cache']['path'];
     $cachepath = \Page\ModuleConfig::cachepath();
     foreach ($lists as $list) {
         $sublistdir = $cachepath . $list['path'] . '/' . $list['slug'];
         // delete main list
         unlink($sublistdir . '.html');
         // delete sublists
         if (is_dir($sublistdir)) {
             $name = $list['name'];
             $listpattern = "{$sublistdir}/{$name}-*.html";
             array_map('unlink', glob($listpattern) ? glob($listpattern) : array());
         }
     }
 }
Пример #2
0
 public function editAction(ServerRequestInterface $Request, ResponseInterface $Response, callable $Next = null)
 {
     $data = [];
     $id = $Request->getAttribute('id', false);
     if (!empty($id) && 'POST' === $Request->getMethod()) {
         $post = $Request->getParsedBody();
         // TODO [SECURITY]: validate content
         $content = $post['content'];
         unset($post['content']);
         $themepath = \Page\ModuleConfig::themepath();
         // $path = getcwd().'/templates'.$post['path'];
         $path = $themepath . $post['path'];
         if (substr($path, -1) !== '/') {
             $path .= '/';
         }
         if (!is_dir($path)) {
             // dir doesn't exist, make it
             mkdir($path, 0775, true);
         }
         $file = $path . $post['name'] . '.html.twig';
         if (file_exists($file)) {
             //backup file per day
             $bakfile = $file . '.' . date('Ymd') . '.bak';
             if (!copy($file, $bakfile)) {
                 throw new \Exception('failed to backup template: ' . $file);
             }
         }
         file_put_contents($file, $content);
         try {
             $plugin = \RpkPluginManager\PluginChain::getInstance();
             $params = $plugin->prepareArgs(['id' => $id, 'data' => $post]);
             $plugin->trigger('page/template::edit-update.pre', $params);
             $post = $params['data'];
             $this->storage->update('page_templates', $post, ['id' => $id]);
         } catch (\Exception $e) {
             // var_dump($e->getMessage());exit(__FILE__.'::'.__LINE__);
             if (!copy($bakfile, $file)) {
                 throw new \Exception('failed to update template and to restore content of the old one from: ' . $bakfile);
             } else {
                 throw new \Exception('failed to update template the old content was restored!');
             }
         }
         // delete cache of the pages that use this template
         // TODO [PERFORMANCE]: query only for slug column
         $pages = $this->storage->fetchAllPages(['templateId' => $id]);
         // $cachepath = getcwd().'/public/data/cache/html/';
         $cachepath = \Page\ModuleConfig::cachepath();
         foreach ($pages as $page) {
             // $file = $cachepath.$page['slug'].'.html';
             $file = $cachepath . $page['path'] . '/' . $page['slug'] . '.html';
             if (file_exists($file)) {
                 unlink(realpath($file));
             }
         }
         $url = $this->router->generateUri('admin.page-template', ['action' => 'edit', 'id' => $id]);
         return $Response->withStatus(302)->withHeader('Location', (string) $url);
     }
     $entity = $this->storage->fetch('page_templates', $id);
     $themepath = \Page\ModuleConfig::themepath();
     // $content = file_get_contents(getcwd().'/templates'.$entity['path'].$entity['name'].'.html.twig');
     $content = file_get_contents($themepath . $entity['path'] . $entity['name'] . '.html.twig');
     $data['page_template'] = $entity;
     $data['page_template']['content'] = $content;
     // return new HtmlResponse($this->template->render('page/template::edit', $data));
     $plugin = \RpkPluginManager\PluginChain::getInstance();
     $params = $plugin->prepareArgs(['template' => 'page/template::edit', 'data' => $data]);
     $plugin->trigger('page/template::edit-render.pre', $params);
     $htmlResponse = new HtmlResponse($this->template->render($params['template'], $params['data']));
     $response = $Next($Request, $htmlResponse);
     return $response;
 }
Пример #3
0
 private function changeParent($OldParentId, $NewParentId, $OldNme, $NewName)
 {
     try {
         $oldid = intval($OldParentId);
         $newid = intval($NewParentId);
         if (empty($oldid) || empty($newid)) {
             throw \Exception('empty data!');
         }
         // $cachepath = getcwd().'/public/data/cache/html';
         $cachepath = \Page\ModuleConfig::cachepath();
         // determine old path
         $oldpath = $this->storage->getPageCateoryPathById($oldid);
         // var_dump($oldpath);exit(__FILE__.'::'.__LINE__);
         if ($oldpath) {
             // determine new path
             if ($oldid === $newid) {
                 $newpath = $oldpath;
             } else {
                 $newpath = $this->storage->getPageCateoryPathById($newid);
             }
             $oldpath = $cachepath . $oldpath['path'] . DIRECTORY_SEPARATOR . $OldNme;
             $newpath = $cachepath . $newpath['path'] . DIRECTORY_SEPARATOR . $NewName;
         } else {
             throw \Exception('could not determine old path!');
         }
         if ($newpath) {
             // var_dump("mv $oldpath $newpath");exit(__FILE__.'::'.__LINE__);
             exec("mv {$oldpath} {$newpath}");
             // $ok = file_exists($newpath);
             $ok = true;
             //fake use file_exists($newpath); for real status
         } else {
             $ok = false;
         }
         if (!$ok) {
             // if move directory failed then delete old one as
             // the new one will be generated when first file will be requested
             exec("rm -rf {$oldpath}");
             // $ok = !file_exists($oldpath);
             $ok = true;
             //fake use !file_exists($oldpath); for real status
         }
         return $ok;
     } catch (\Exception $e) {
         return false;
     }
 }
Пример #4
0
 public function __invoke(ServerRequestInterface $Request, ResponseInterface $Response, callable $Next = null)
 {
     try {
         $url = $Request->getUri();
         $path = $url->getPath();
         $query = $url->getQuery();
         // var_dump($url);exit(__FILE__.'::'.__LINE__);
         //detecting language
         // $lang = $Request->getAttribute('lang', 'en');
         //detecting requested page
         // TODO [PERFORMANCE]: check if better get from request first
         if (preg_match('#\\.(html|xml)$#', $path, $matches)) {
             $page = substr($path, 1, -strlen(reset($matches)));
         } else {
             // $page = $Request->getAttribute('page', 'index');
             if ('/' == $path) {
                 $page = 'index';
                 $path = '/index.html';
             } else {
                 $page = substr($path, 1);
                 $path .= '.html';
             }
         }
         $filename = $page;
         $pageschema = explode('/', $page);
         $page = array_pop($pageschema);
         unset($pageschema);
         // var_dump($page);exit(__FILE__.'::'.__LINE__);
         $data['page'] = $page;
         // $data['language'] = date('Y-m-d H:i:s');
         // $file = getcwd().'/data/cache/html/'.$lang.'_'.$page.'.html';
         // TODO [IMPROVEMENT]: get from config or pass responsability to a service
         // $cachepath = getcwd().'/public/data/cache/html';
         $cachepath = \Page\ModuleConfig::cachepath();
         $file = $cachepath . '/' . $filename . '.html';
         // var_dump($file);exit(__FILE__.'::'.__LINE__);
         if (!file_exists($file)) {
             // $pagedb = $this->storage->fetch('pages', $page, 'slug');
             $pagedb = $this->storage->pageBySlug($page);
             // var_dump($pagedb);exit(__FILE__.'::'.__LINE__);
             if (false == $pagedb) {
                 throw new StorageException('Page not found');
             }
             // load page extra info
             $data['page_meta'] = $this->storage->fetchAll('page_meta', ['pageId' => $pagedb['id']]);
             $data['page_meta_tags'] = $this->storage->fetchAll('page_meta_tags', ['pageId' => $pagedb['id']]);
             $pagedb = \Zend\Stdlib\ArrayUtils::merge($pagedb, $data);
             // var_dump($pagedb['content']);exit(__FILE__.'::'.__LINE__);
             // $content = file_get_contents(getcwd().'/data/cache/html/en_test.html');
             $plugin = \RpkPluginManager\PluginChain::getInstance();
             $themepath = \Page\ModuleConfig::themepath(true);
             // $params = $plugin->prepareArgs(['template'=> 'templates'.$pagedb['page_templates.path'].'::'.$pagedb['page_templates.name'], 'data' => $pagedb]);
             $params = $plugin->prepareArgs(['template' => $themepath . $pagedb['page_templates.path'] . '::' . $pagedb['page_templates.name'], 'data' => $pagedb]);
             $plugin->trigger('page::cache-render.pre', $params);
             // $content = $this->template->render('templates'.$pagedb['page_templates.path'].'::'.$pagedb['page_templates.name'], $pagedb);
             $content = $this->template->render($params['template'], $params['data']);
             // var_dump($content);exit(__FILE__.'::'.__LINE__);
             $pagename = '/' . $pagedb['slug'] . '.html';
             $pagedir = $cachepath . $pagedb['path'];
             if (!is_dir($pagedir)) {
                 // dir doesn't exist, make it
                 mkdir($pagedir, 0777, true);
             }
             $file = $pagedir . $pagename;
             if (file_put_contents($file, $content)) {
                 $path = $pagedb['path'] . $pagename;
             }
         }
         // var_dump($path);exit(__FILE__.'::'.__LINE__);
         // return $this->redirect($path.'.html', $url, $Response);
         // return $this->redirect("/data/cache/html/$filename", $url, $Response);
         return $this->redirect($path, $url, $Response);
     } catch (\Exception $e) {
         // var_dump($e->getMessage());exit(__FILE__.'::'.__LINE__);
         return $Next($Request, $Response);
     }
 }
Пример #5
0
 public function editAction(ServerRequestInterface $Request, ResponseInterface $Response, callable $Next = null)
 {
     $data = [];
     $id = $Request->getAttribute('id', false);
     if (!empty($id) && 'POST' === $Request->getMethod()) {
         $post = $Request->getParsedBody();
         // var_dump($post);exit(__FILE__.'::'.__LINE__);
         unset($post['files']);
         // meta data
         // TODO [IMPROVEMENT]: maybe use a separate middleware that will handle page meta as there are different tables
         //                      use hydrator and put in request parsedBody the inserted id and return next not response
         //                      so the page_meta middleware get in action and save data using his hydrator
         if (isset($post['meta'])) {
             $meta = $post['meta'];
             unset($post['meta']);
         }
         // meta_tags data
         if (isset($post['meta_tags'])) {
             $meta_tags = $post['meta_tags'];
             unset($post['meta_tags']);
         }
         $plugin = \RpkPluginManager\PluginChain::getInstance();
         $params = $plugin->prepareArgs(['id' => $id, 'data' => $post]);
         $plugin->trigger('page::edit-update.pre', $params);
         $post = $params['data'];
         $this->storage->update('pages', $post, ['id' => $id]);
         // update page meta
         $this->storage->delete('page_meta', ['pageId' => $id]);
         // update page meta tags
         // note: we don't implement onchange flag strategy in template because is tricky for copy-pasting in textarea
         $this->storage->delete('page_meta_tags', ['pageId' => $id]);
         $authorId = !empty($post['authorId']) ? $post['authorId'] : 1;
         // persist meta
         if (!empty($meta) && is_array($meta) && !empty($meta['key']) && !empty($meta['value']) && count($meta['key']) == count($meta['value'])) {
             foreach ($meta['key'] as $idx => $key) {
                 // validate and filter key=>value
                 if (!isset($key) || empty($key) || !isset($meta['value'][$idx]) || empty($meta['value'][$idx])) {
                     continue;
                 }
                 $metadata = ['pageId' => $id, 'metaKey' => $key, 'metaValue' => $meta['value'][$idx], 'creationDate' => date('Y-m-d H:i:s'), 'authorId' => $authorId];
                 $this->storage->insert('page_meta', $metadata);
             }
         }
         // persist meta_tags
         if (!empty($meta_tags) && is_array($meta_tags) && !empty($meta_tags['value'])) {
             foreach ($meta_tags['value'] as $idx => $value) {
                 // validate and filter value
                 if (!isset($value) || empty($value)) {
                     continue;
                 }
                 $metatagsdata = ['pageId' => $id, 'value' => $value, 'creationDate' => date('Y-m-d H:i:s'), 'authorId' => $authorId];
                 $this->storage->insert('page_meta_tags', $metatagsdata);
             }
         }
         $url = $this->router->generateUri('admin.page', ['action' => 'edit', 'id' => $id]);
         // delete cached file
         // TODO [IMPROVEMENT]: get from config or pass responsability to a service
         // $cachepath = getcwd().'/public/data/cache/html';
         $cachepath = \Page\ModuleConfig::cachepath();
         $data['page'] = $this->storage->pageById($id);
         // $file = $cachepath.$post['slug'].'.html';
         $file = $cachepath . $data['page']['path'] . '/' . $data['page']['slug'] . '.html';
         if (file_exists($file)) {
             unlink(realpath($file));
         }
         return $Response->withStatus(302)->withHeader('Location', (string) $url);
     } else {
         $data['page'] = $this->storage->fetch('pages', $id);
     }
     // gather all data required for composing page
     $data['page_templates'] = $this->storage->fetchAll('page_templates');
     // $data['page_parents'] = $this->storage->fetchAll('pages',[]);
     $data['page_parents'] = $this->storage->parents('pages', $id);
     $data['page_categories'] = $this->storage->fetchAll('page_categories', []);
     $data['user_roles'] = $this->storage->fetchAll('user_roles', []);
     $data['page_statuses'] = $this->storage->fetchAll('page_statuses');
     $data['page_meta'] = $this->storage->fetchAll('page_meta', ['pageId' => $id]);
     $data['page_meta_tags'] = $this->storage->fetchAll('page_meta_tags', ['pageId' => $id]);
     $plugin = \RpkPluginManager\PluginChain::getInstance();
     $params = $plugin->prepareArgs(['template' => 'page::edit', 'data' => $data]);
     $plugin->trigger('page::edit-render.pre', $params);
     // return new HtmlResponse($this->template->render('page::edit', $data));
     $htmlResponse = new HtmlResponse($this->template->render($params['template'], $params['data']));
     $response = $Next($Request, $htmlResponse);
     return $response;
 }