PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/api/rest/theme.php

https://gitlab.com/x33n/respond
PHP | 363 lines | 166 code | 103 blank | 94 comment | 21 complexity | ca0b40580c545e365b980870885daf50 MD5 | raw file
  1. <?php
  2. /**
  3. * This class defines an example resource that is wired into the URI /example
  4. * @uri /theme
  5. */
  6. class ThemeResource extends Tonic\Resource {
  7. /**
  8. * @method GET
  9. */
  10. function get() {
  11. $json = '{';
  12. $first = true;
  13. // open themes direcotry
  14. if($handle = opendir(APP_LOCATION.THEMES_FOLDER)){
  15. $blacklist = array('.', '..');
  16. // walk through directories
  17. while (false !== ($file = readdir($handle))) {
  18. if (!in_array($file, $blacklist)) {
  19. $dir = $file;
  20. $config = APP_LOCATION.THEMES_FOLDER.'/'.$dir.'/theme.json';
  21. if(file_exists($config)){
  22. $theme_json = file_get_contents($config);
  23. // add commas for following json objects
  24. if($first == false){
  25. $json .= ',';
  26. }
  27. // use the dir as the id
  28. $theme_json = preg_replace('/{/', '{"id":"'.$dir.'",', $theme_json, 1);
  29. $json .= '"'.$dir.'":'.$theme_json;
  30. $first = false;
  31. }
  32. }
  33. }
  34. closedir($handle);
  35. }
  36. $json .= '}';
  37. // return a json response
  38. $response = new Tonic\Response(Tonic\Response::OK);
  39. $response->contentType = 'application/json';
  40. $response->body = $json;
  41. return $response;
  42. }
  43. }
  44. /**
  45. * A protected API call to apply a themes
  46. * @uri /theme/apply
  47. */
  48. class ThemeApplyResource extends Tonic\Resource {
  49. /**
  50. * @method POST
  51. */
  52. function post() {
  53. // get token
  54. $token = Utilities::ValidateJWTToken(apache_request_headers());
  55. // check if token is not null
  56. if($token != NULL){
  57. parse_str($this->request->data, $request); // parse request
  58. $theme = $request['theme'];
  59. // convert string to a boolean
  60. $replaceContent = ($request['replaceContent'] === 'true');
  61. $site = Site::GetBySiteId($token->SiteId);
  62. // edits the theme for the site
  63. Site::EditTheme($token->SiteId, $theme);
  64. // publishes a theme for a site
  65. Publish::PublishTheme($site, $theme);
  66. // publish default content for the theme
  67. if($replaceContent == true){
  68. echo 'publish default content, $replaceContent='.$replaceContent;
  69. Publish::PublishDefaultContent($site, $theme, $token->UserId);
  70. }
  71. // republish site with the new theme
  72. Publish::PublishSite($site['SiteId']);
  73. // return a json response
  74. return new Tonic\Response(Tonic\Response::OK);
  75. }
  76. else{
  77. // return an unauthorized exception (401)
  78. return new Tonic\Response(Tonic\Response::UNAUTHORIZED);
  79. }
  80. }
  81. }
  82. /**
  83. * A protected API call to reset a theme
  84. * @uri /theme/reset
  85. */
  86. class ThemeResetResource extends Tonic\Resource {
  87. /**
  88. * @method POST
  89. */
  90. function post() {
  91. // get token
  92. $token = Utilities::ValidateJWTToken(apache_request_headers());
  93. // check if token is not null
  94. if($token != NULL){
  95. $site = Site::GetBySiteId($token->SiteId);
  96. parse_str($this->request->data, $request); // parse request
  97. $theme = $request['theme'];
  98. // publishes a theme for a site
  99. Publish::PublishTheme($site, $theme);
  100. // republish site with the new theme
  101. Publish::PublishSite($site['SiteId']);
  102. // return a json response
  103. $response = new Tonic\Response(Tonic\Response::OK);
  104. return $response;
  105. }
  106. else{
  107. // return an unauthorized exception (401)
  108. return new Tonic\Response(Tonic\Response::UNAUTHORIZED);
  109. }
  110. }
  111. }
  112. /**
  113. * A protected API call to get content from a page
  114. * @uri /theme/page/content
  115. */
  116. class ThemePageContentResource extends Tonic\Resource {
  117. /**
  118. * @method POST
  119. */
  120. function post() {
  121. // get token
  122. $token = Utilities::ValidateJWTToken(apache_request_headers());
  123. // check if token is not null
  124. if($token != NULL){
  125. $site = Site::GetBySiteId($token->SiteId);
  126. parse_str($this->request->data, $request); // parse request
  127. $location = APP_LOCATION.'/'.$request['location'];
  128. $content = '';
  129. if(file_exists($location)){
  130. $content = file_get_contents($location);
  131. // fix images
  132. $content = str_replace('{{site-dir}}', 'sites/'.$site['SiteFriendlyId'], $content);
  133. }
  134. else{
  135. return new Tonic\Response(Tonic\Response::BADREQUEST, $location.' not found');
  136. }
  137. // return a json response
  138. $response = new Tonic\Response(Tonic\Response::OK);
  139. $response->contentType = 'text/html';
  140. $response->body = $content;
  141. return $response;
  142. }
  143. else{
  144. // return an unauthorized exception (401)
  145. return new Tonic\Response(Tonic\Response::UNAUTHORIZED);
  146. }
  147. }
  148. }
  149. /**
  150. * A protected API call to retrieve a list of pages for a theme
  151. * @uri /theme/pages/list
  152. */
  153. class ThemePagesListResource extends Tonic\Resource {
  154. /**
  155. * @method GET
  156. */
  157. function get() {
  158. // get token
  159. $token = Utilities::ValidateJWTToken(apache_request_headers());
  160. // check if token is not null
  161. if($token != NULL){
  162. $arr = array();
  163. $site = Site::GetBySiteId($token->SiteId);
  164. $directory = APP_LOCATION.THEMES_FOLDER.'/'.$site['Theme'].'/pages/';
  165. //get files with a .html ext
  166. $files = glob($directory . "*.html");
  167. $arr = array();
  168. //print each file name
  169. foreach($files as $file){
  170. $f_arr = explode("/",$file);
  171. $count = count($f_arr);
  172. $filename = $f_arr[$count-1];
  173. $name = str_replace('-', ' ', $filename);
  174. $name = str_replace('.html', '', $name);
  175. $name = ucfirst($name);
  176. $file = array(
  177. 'name' => $name,
  178. 'fileName' => $filename,
  179. 'location' => THEMES_FOLDER.'/'.$site['Theme'].'/pages/'.$filename
  180. );
  181. array_push($arr, $file);
  182. }
  183. // return a json response
  184. $response = new Tonic\Response(Tonic\Response::OK);
  185. $response->contentType = 'application/json';
  186. $response->body = json_encode($arr);
  187. return $response;
  188. }
  189. else{
  190. // return an unauthorized exception (401)
  191. return new Tonic\Response(Tonic\Response::UNAUTHORIZED);
  192. }
  193. }
  194. }
  195. /**
  196. * A protected API call to retrieve a list of pages for a theme
  197. * @uri /theme/configurations/list
  198. */
  199. class ThemeConfigurationsListResource extends Tonic\Resource {
  200. /**
  201. * @method GET
  202. */
  203. function get() {
  204. // get token
  205. $token = Utilities::ValidateJWTToken(apache_request_headers());
  206. // check if token is not null
  207. if($token != NULL){
  208. $arr = array();
  209. $site = Site::GetBySiteId($token->SiteId);
  210. // get configuration
  211. $file = SITES_LOCATION.'/'.$site['FriendlyId'].'/themes/'.$site['Theme'].'/configure.json';
  212. // return a json response
  213. $response = new Tonic\Response(Tonic\Response::OK);
  214. $response->contentType = 'application/json';
  215. $response->body = file_get_contents($file);
  216. return $response;
  217. }
  218. else{
  219. // return an unauthorized exception (401)
  220. return new Tonic\Response(Tonic\Response::UNAUTHORIZED);
  221. }
  222. }
  223. }
  224. /**
  225. * A protected API call to retrieve a list of pages for a theme
  226. * @uri /theme/configurations/apply
  227. */
  228. class ThemeConfigurationsApplyResource extends Tonic\Resource {
  229. /**
  230. * @method POST
  231. */
  232. function post() {
  233. // get token
  234. $token = Utilities::ValidateJWTToken(apache_request_headers());
  235. // check if token is not null
  236. if($token != NULL){
  237. parse_str($this->request->data, $request); // parse request
  238. $configurations = $request['configurations'];
  239. $site = Site::GetBySiteId($token->SiteId);
  240. // get configuration
  241. $configure_file = SITES_LOCATION.'/'.$site['FriendlyId'].'/themes/'.$site['Theme'].'/configure.json';
  242. // put contents
  243. file_put_contents($configure_file, $configurations);
  244. // republish css
  245. Publish::PublishAllCSS($site);
  246. // get index
  247. $page = Page::GetByFriendlyId('index', '-1', $token->SiteId);
  248. // republish home page
  249. Publish::PublishPage($page['PageId']);
  250. // return a json response
  251. $response = new Tonic\Response(Tonic\Response::OK);
  252. return $response;
  253. }
  254. else{
  255. // return an unauthorized exception (401)
  256. return new Tonic\Response(Tonic\Response::UNAUTHORIZED);
  257. }
  258. }
  259. }
  260. ?>