PageRenderTime 43ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/api/rest/stylesheet.php

https://gitlab.com/x33n/respond
PHP | 240 lines | 114 code | 67 blank | 59 comment | 12 complexity | 0dc29518c659c5625ed767c5a0a594a4 MD5 | raw file
  1. <?php
  2. /**
  3. * This class defines an example resource that is wired into the URI /example
  4. * @uri /stylesheet/add
  5. */
  6. class StylesheetAddResource extends Tonic\Resource {
  7. /**
  8. * @method POST
  9. */
  10. function add() {
  11. // get token
  12. $token = Utilities::ValidateJWTToken(apache_request_headers());
  13. // check if token is not null
  14. if($token != NULL){
  15. parse_str($this->request->data, $request); // parse request
  16. $name = $request['name'];
  17. $site = Site::GetBySiteId($token->SiteId);
  18. $directory = SITES_LOCATION.'/'.$site['FriendlyId'].'/themes/'.$site['Theme'].'/styles/';
  19. $file = $directory.$name.'.less';
  20. file_put_contents($file, ''); // save to file
  21. // return a text/html response
  22. $response = new Tonic\Response(Tonic\Response::OK);
  23. $response->contentType = 'text/HTML';
  24. $response->body = $name;
  25. return $response;
  26. }
  27. else{
  28. // return an unauthorized exception (401)
  29. return new Tonic\Response(Tonic\Response::UNAUTHORIZED);
  30. }
  31. }
  32. }
  33. /**
  34. * This class defines an example resource that is wired into the URI /example
  35. * @uri /stylesheet/retrieve
  36. */
  37. class StylesheetRetrieveResource extends Tonic\Resource {
  38. /**
  39. * @method POST
  40. */
  41. function get() {
  42. // get token
  43. $token = Utilities::ValidateJWTToken(apache_request_headers());
  44. // check if token is not null
  45. if($token != NULL){
  46. parse_str($this->request->data, $request); // parse request
  47. $name = $request['name'];
  48. $site = Site::GetBySiteId($token->SiteId);
  49. $directory = SITES_LOCATION.'/'.$site['FriendlyId'].'/themes/'.$site['Theme'].'/styles/';
  50. $content = html_entity_decode(file_get_contents($directory.$name.'.less'));
  51. // return a text/html response
  52. $response = new Tonic\Response(Tonic\Response::OK);
  53. $response->contentType = 'text/HTML';
  54. $response->body = $content;
  55. return $response;
  56. }
  57. else{
  58. // return an unauthorized exception (401)
  59. return new Tonic\Response(Tonic\Response::UNAUTHORIZED);
  60. }
  61. }
  62. }
  63. /**
  64. * Publishes the LESS for a stylesheet and renders the CSS to the site
  65. * @uri /stylesheet/publish
  66. */
  67. class StylesheetPublishResource extends Tonic\Resource {
  68. /**
  69. * @method POST
  70. */
  71. function update() {
  72. // get token
  73. $token = Utilities::ValidateJWTToken(apache_request_headers());
  74. // check if token is not null
  75. if($token != NULL){
  76. parse_str($this->request->data, $request); // parse request
  77. $name = $request['name'];
  78. $content = $request['content'];
  79. $site = Site::GetBySiteId($token->SiteId);
  80. $directory = SITES_LOCATION.'/'.$site['FriendlyId'].'/themes/'.$site['Theme'].'/styles/';
  81. $f = $directory.$name.'.less';
  82. file_put_contents($f, $content); // save to file
  83. $errors = Publish::GetLESSErrors($site, $name);
  84. if($errors == NULL){
  85. // publishes all css
  86. Publish::PublishAllCSS($site);
  87. // send success
  88. $response = new Tonic\Response(Tonic\Response::OK);
  89. $response->contentType = 'text/HTML';
  90. $response->body = 'yay!';
  91. }
  92. else{
  93. // send errors
  94. $response = new Tonic\Response(Tonic\Response::BADREQUEST);
  95. $response->contentType = 'text/HTML';
  96. $response->body = $errors;
  97. }
  98. return $response;
  99. }
  100. else{
  101. // return an unauthorized exception (401)
  102. return new Tonic\Response(Tonic\Response::UNAUTHORIZED);
  103. }
  104. }
  105. }
  106. /**
  107. * This class defines an example resource that is wired into the URI /example
  108. * @uri /stylesheet/remove
  109. */
  110. class StylesheetRemoveResource extends Tonic\Resource {
  111. /**
  112. * @method POST
  113. */
  114. function post() {
  115. // get token
  116. $token = Utilities::ValidateJWTToken(apache_request_headers());
  117. // check if token is not null
  118. if($token != NULL){
  119. parse_str($this->request->data, $request); // parse request
  120. $name = $request['name'];
  121. $site = Site::GetBySiteId($token->SiteId);
  122. $directory = SITES_LOCATION.'/'.$site['FriendlyId'].'/themes/'.$site['Theme'].'/styles/';
  123. $f = $directory.$name.'.less';
  124. unlink($f);
  125. // return a json response
  126. $response = new Tonic\Response(Tonic\Response::OK);
  127. return $response;
  128. }
  129. else{
  130. // return an unauthorized exception (401)
  131. return new Tonic\Response(Tonic\Response::UNAUTHORIZED);
  132. }
  133. }
  134. }
  135. /**
  136. * This class defines an example resource that is wired into the URI /example
  137. * @uri /stylesheet/list
  138. */
  139. class StylehseetListResource extends Tonic\Resource {
  140. /**
  141. * @method GET
  142. */
  143. function get() {
  144. // get token
  145. $token = Utilities::ValidateJWTToken(apache_request_headers());
  146. // check if token is not null
  147. if($token != NULL){
  148. $site = Site::GetBySiteId($token->SiteId);
  149. $directory = SITES_LOCATION.'/'.$site['FriendlyId'].'/themes/'.$site['Theme'].'/styles/';
  150. //get all image files with a .less ext
  151. $files = glob($directory . "*.less");
  152. $arr = array();
  153. //print each file name
  154. foreach($files as $file){
  155. $f_arr = explode("/",$file);
  156. $count = count($f_arr);
  157. $filename = $f_arr[$count-1];
  158. $name = str_replace('.less', '', $filename);
  159. array_push($arr, $name);
  160. }
  161. // return a json response
  162. $response = new Tonic\Response(Tonic\Response::OK);
  163. $response->contentType = 'application/json';
  164. $response->body = json_encode($arr);
  165. return $response;
  166. }
  167. else{
  168. // return an unauthorized exception (401)
  169. return new Tonic\Response(Tonic\Response::UNAUTHORIZED);
  170. }
  171. }
  172. }
  173. ?>