Guest User

Untitled

a guest
Jan 14th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. <?php
  2. require_once($modx->getOption('core_path').'components/simplx/mygit/simplx.mygit.php');
  3.  
  4. $action = isset($action) ? $action : '';
  5. $username = isset($username) ? $username : '';
  6. $password = isset($password) ? $password : '';
  7. $object = isset($object) ? $object : '';
  8. $objectid = isset($objectid) ? $objectid : '';
  9.  
  10. switch($action){
  11. case 'mygit.getgist':
  12. if($objectid != ''){
  13.  
  14. $mygit = new simplx_mygit(new GitAPIClient());
  15.  
  16. $mygit->login($username,$password);
  17.  
  18.  
  19. $gist = $mygit->getGist($objectid);
  20.  
  21. if($gist){
  22. return json_encode($gist);
  23. }else{
  24. $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mygit.getgist() Exception: gist not found.');
  25. }
  26. }else{
  27. $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mygit.getgist() Exception: parameter "$objectid" was not sent.');
  28. }
  29. break;
  30.  
  31. case 'mygit.addgist':
  32. if($object != ''){
  33.  
  34. $object = json_decode($object,true);
  35.  
  36. if(!is_array($object)){
  37. $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mygit.addgist() Exception: parameter "$object" is not a valid php array()');
  38. return false;
  39. }
  40.  
  41. $mygit = new simplx_mygit(new GitAPIClient());
  42.  
  43. $mygit->login($username,$password);
  44.  
  45.  
  46. /*
  47. When creating a new instance of "simplx_gist", you dont yet add anything to
  48. Github. This is done by calling the "saveGist" method of the "simplx.mygit" class.
  49. */
  50.  
  51. $new_gist = new simplx_gist($object);
  52.  
  53. /*
  54. You add a file to the gist by either,
  55. - Create a new "simplx_gist_file" object
  56. - Create an array to represent the file object
  57.  
  58. Now use the "addFile" method of the gist object to actually add the file.
  59.  
  60. */
  61.  
  62. $mygit->addGist($new_gist);
  63.  
  64. return json_encode($mygit->getGist($new_gist->id));
  65. }else{
  66. $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mygit.addgist() Exception: parameter "$object" was not sent.');
  67. }
  68. break;
  69.  
  70. case 'mygit.savegist':
  71. if($object != ''){
  72.  
  73. $object = json_decode($object,true);
  74.  
  75. if(!is_array($object)){
  76. $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mygit.savegist() Exception: parameter "$object" is not a valid php array()');
  77. return false;
  78. }
  79.  
  80. $mygit = new simplx_mygit(new GitAPIClient());
  81. $mygit->login($username,$password);
  82.  
  83. $gist = new simplx_gist($object);
  84.  
  85. $mygit->saveGist($gist);
  86.  
  87. /*
  88. Alternative method:
  89.  
  90. $new_gist_file = new simplx_gist_file(array('filename'=>'test.txt','description'=>'My new gist file','content'=>'blabla'));
  91. $new_gist->addFile(array('filename'=>'test.txt','description'=>'My new gist file','content'=>'blabla'));
  92.  
  93. */
  94.  
  95. return json_encode($gist);
  96.  
  97. }else{
  98. $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mygit.savegist() Exception: parameter "$object" was not sent.');
  99. }
  100.  
  101. break;
  102.  
  103. case 'mygit.gist.addfile':
  104. if($object != ''){
  105.  
  106. if($objectid == ''){
  107. $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mygit.gist.addfile() Exception: parameter "$objectid" was not passed.');
  108. return false;
  109. }
  110.  
  111. $object = json_decode($object,true);
  112.  
  113. if(!is_array($object)){
  114. $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mygit.gist.addfile() Exception: parameter "$object" is not a valid php array()');
  115. return false;
  116. }
  117.  
  118. $mygit = new simplx_mygit(new GitAPIClient());
  119. $mygit->login($username,$password);
  120.  
  121. $gist = $mygit->getGist($objectid);
  122.  
  123. if($gist){
  124.  
  125. $gist->addFile($object);
  126. $mygit->saveGist($gist);
  127.  
  128. return json_encode($mygit->getGist($gist->id));
  129.  
  130. }else{
  131. $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mygit.gist.addfile() Exception: "$mygit->getGist()" did not return a valid object.');
  132. }
  133. }else{
  134. $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mygit.gist.addfile() Exception: parameter "$object" was not sent.');
  135. }
  136.  
  137. break;
  138.  
  139. case 'mygit.gist.savefile':
  140. if($object != ''){
  141.  
  142. if($objectid == ''){
  143. $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mygit.gist.savefile() Exception: parameter "$objectid" was not passed.');
  144. return false;
  145. }
  146.  
  147. $object = json_decode($object,true);
  148.  
  149. if(!is_array($object)){
  150. $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mygit.gist.savefile() Exception: parameter "$object" is not a valid php array()');
  151. return false;
  152. }
  153.  
  154. $mygit = new simplx_mygit(new GitAPIClient());
  155. $mygit->login($username,$password);
  156.  
  157. $gist = $mygit->getGist($objectid);
  158.  
  159. if($gist){
  160.  
  161. $gist->files[$object['filename']] = $object;
  162. $mygit->saveGist($gist);
  163.  
  164. return json_encode($gist);
  165.  
  166. }else{
  167. $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mygit.gist.savefile() Exception: "$mygit->getGist()" did not return a valid object.');
  168. }
  169. }else{
  170. $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mygit.gist.savefile() Exception: parameter "$object" was not sent.');
  171. }
  172.  
  173. break;
  174.  
  175. case 'mygit.gist.addfile':
  176. if($object != ''){
  177.  
  178. if($objectid == ''){
  179. $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mygit.gist.addfile() Exception: parameter "$objectid" was not passed.');
  180. return false;
  181. }
  182.  
  183. $object = json_decode($object,true);
  184.  
  185. if(!is_array($object)){
  186. $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mygit.gist.addfile() Exception: parameter "$object" is not a valid php array()');
  187. return false;
  188. }
  189.  
  190. $mygit = new simplx_mygit(new GitAPIClient());
  191. $mygit->login($username,$password);
  192.  
  193. $gist = $mygit->getGist($objectid);
  194.  
  195. if($gist){
  196.  
  197. $gist->addFile($object);
  198. $mygit->saveGist($gist);
  199.  
  200. return json_encode($gist);
  201.  
  202. }else{
  203. $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mygit.gist.addfile() Exception: "$mygit->getGist()" did not return a valid object.');
  204. }
  205. }else{
  206. $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mygit.gist.addfile() Exception: parameter "$object" was not sent.');
  207. }
  208.  
  209. break;
  210.  
  211. default:
  212.  
  213.  
  214. }
Add Comment
Please, Sign In to add comment