PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Lampcms/Controllers/Editapp.php

http://github.com/snytkine/LampCMS
PHP | 317 lines | 132 code | 46 blank | 139 comment | 13 complexity | 4a5b5b7ea98035186eb2fee9d425930c MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /**
  3. *
  4. * License, TERMS and CONDITIONS
  5. *
  6. * This software is licensed under the GNU LESSER GENERAL PUBLIC LICENSE (LGPL) version 3
  7. * Please read the license here : http://www.gnu.org/licenses/lgpl-3.0.txt
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. The name of the author may not be used to endorse or promote products
  17. * derived from this software without specific prior written permission.
  18. *
  19. * ATTRIBUTION REQUIRED
  20. * 4. All web pages generated by the use of this software, or at least
  21. * the page that lists the recent questions (usually home page) must include
  22. * a link to the http://www.lampcms.com and text of the link must indicate that
  23. * the website\'s Questions/Answers functionality is powered by lampcms.com
  24. * An example of acceptable link would be "Powered by <a href="http://www.lampcms.com">LampCMS</a>"
  25. * The location of the link is not important, it can be in the footer of the page
  26. * but it must not be hidden by style attributes
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
  29. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  31. * IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
  32. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  33. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  35. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  36. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  37. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. * This product includes GeoLite data created by MaxMind,
  40. * available from http://www.maxmind.com/
  41. *
  42. *
  43. * @author Dmitri Snytkine <cms@lampcms.com>
  44. * @copyright 2005-2012 (or current year) Dmitri Snytkine
  45. * @license http://www.gnu.org/licenses/lgpl-3.0.txt GNU LESSER GENERAL PUBLIC LICENSE (LGPL) version 3
  46. * @link http://www.lampcms.com Lampcms.com project
  47. * @version Release: @package_version@
  48. *
  49. *
  50. */
  51. namespace Lampcms\Controllers;
  52. use \Lampcms\WebPage;
  53. use \Lampcms\Mongo\Doc as MongoDoc;
  54. use \Lampcms\Captcha\Captcha;
  55. use \Lampcms\String;
  56. use \Lampcms\Api\Clientdata;
  57. use \Lampcms\Api\IconParser;
  58. use \Lampcms\Request;
  59. use \Lampcms\Responder;
  60. /**
  61. * Controller for Adding
  62. * or Editing the API Client App
  63. *
  64. * @author Dmitri Snytkine
  65. *
  66. */
  67. class Editapp extends WebPage
  68. {
  69. /**
  70. * Pre-check to deny non-logged in user
  71. * access to this page
  72. *
  73. * @var bool
  74. */
  75. protected $membersOnly = true;
  76. /**
  77. *
  78. * Viewer must have edit_profile
  79. * permission to access this page
  80. *
  81. * @var string
  82. */
  83. protected $permission = 'edit_profile';
  84. /**
  85. * $layoutID 1 means no side-column on page
  86. *
  87. * @var int
  88. */
  89. protected $layoutID = 1;
  90. /**
  91. * Form object
  92. *
  93. * @var object of type \Lampcms\Forms\Form
  94. */
  95. protected $Form;
  96. /**
  97. * Existing API data in case this is
  98. * an edit of client
  99. *
  100. * @var object of type \Lampcms\Mongo\Doc representing
  101. * the Api client
  102. */
  103. protected $oApi;
  104. protected function main()
  105. {
  106. $this->setApi();
  107. $email = $this->Registry->Viewer->email;
  108. $this->Form = new \Lampcms\Forms\Apiclient($this->Registry);
  109. if ($this->Form->isSubmitted() && $this->Form->validate()) {
  110. d('$this->oApi: ' . print_r($this->oApi->getArrayCopy(), 1));
  111. $this->save();
  112. $this->Registry->Dispatcher->post($this->Form, 'onApiClientSave');
  113. $url = '{_WEB_ROOT_}/{_viewapp_}/' . $this->oApi['_id'];
  114. $mapper = $this->Router->getCallback();
  115. $url = $mapper($url);
  116. Responder::redirectToPage($url);
  117. } else {
  118. $this->setForm();
  119. $this->aPageVars['body'] = $this->Form->getForm();
  120. }
  121. }
  122. /**
  123. * Instantiates $this->oApi object
  124. * it will either contain empty values
  125. * in case this is new registration
  126. * or will have values from previous
  127. * application
  128. *
  129. * @return object $this
  130. */
  131. protected function setApi()
  132. {
  133. $appid = $this->Router->getSegment(1, 'i', 0);
  134. $this->oApi = Clientdata::factory($this->Registry);
  135. if (!empty($appid)) {
  136. $a = $this->Registry->Mongo->API_CLIENTS->findOne(array('_id' => $appid,
  137. 'i_uid' => $this->Registry->Viewer->getUid()));
  138. if (!empty($a)) {
  139. $this->oApi->reload($a);
  140. } else {
  141. d('APP not found by id ' . $appid.' for viewer '.$this->Registry->Viewer->getUid());
  142. }
  143. }
  144. return $this;
  145. }
  146. /**
  147. * Populate form values with
  148. * existing data from oApi object
  149. * if this is a new registration then
  150. * $this->oApi has empty array as its data
  151. * and all values will be empty (just as we expect)
  152. *
  153. * @return object $this
  154. */
  155. protected function setForm()
  156. {
  157. $c = Captcha::factory($this->Registry->Ini)->getCaptchaBlock();
  158. $id = $this->Request->get('app_id', 'i', 0);
  159. $this->aPageVars['title'] = (empty($id)) ? '@@Register an Application@@' : '@@Edit Application details@@';
  160. $this->Form->formTitle = $this->aPageVars['title'];
  161. $this->Form->setVar('captcha', $c);
  162. $this->Form->app_id = (int)$this->oApi['_id'];
  163. $this->Form->app_name = $this->oApi['app_name'];
  164. $this->Form->appsite = $this->oApi['appsite'];
  165. $this->Form->company = $this->oApi['company'];
  166. $this->Form->company = $this->oApi['company'];
  167. $this->Form->app_type = $this->oApi['app_type'];
  168. $this->Form->about = $this->oApi['about'];
  169. $this->Form->icon_image = $this->oApi->getIcon(false);
  170. return $this;
  171. }
  172. /**
  173. * Save the submitted form values
  174. * by setting the $this->oApi object
  175. * and then calling insert() or save() on it
  176. *
  177. * @throws \OutOfBoundsException
  178. * @return object $this
  179. */
  180. protected function save()
  181. {
  182. $isUpdate = false;
  183. $vals = $this->Form->getSubmittedValues();
  184. d('vals: ' . print_r($vals, 1));
  185. $appid = (int)$vals['app_id'];
  186. if ($appid > 0) {
  187. $isUpdate = true;
  188. d('has appid, editing mode');
  189. $this->validateAppIdOwnership($appid);
  190. } else {
  191. /**
  192. * Auto-generate app_id
  193. * Use USERS auto-increment value
  194. * because we can then store the image in the same
  195. * way we store avatar - in the same directory
  196. * using hex based path.
  197. *
  198. */
  199. $appid = $this->Registry->Incrementor->nextValue('USERS');
  200. }
  201. d('$appid: ' . $appid);
  202. $this->oApi['_id'] = $appid;
  203. $this->oApi['i_uid'] = $this->Registry->Viewer->getUid();
  204. $this->oApi['app_name'] = (string)$this->Request->getUTF8('app_name')->trim()->stripTags();
  205. $this->oApi['appsite'] = (string)$this->Request->getUTF8('appsite')->trim()->stripTags();
  206. $this->oApi['company'] = (string)$this->Request->getUTF8('company')->trim()->stripTags();
  207. $this->oApi['app_type'] = (string)$this->Request->getUTF8('app_type')->trim()->stripTags();
  208. $this->oApi['about'] = (string)$this->Request->getUTF8('about')->trim()->stripTags();
  209. $this->oApi['api_key'] = $appid . '.' . String::makeRandomString(12);
  210. $this->parseIcon();
  211. /**
  212. * Ensure that app is a unique field
  213. * app is the name of application
  214. */
  215. $coll = $this->Registry->Mongo->API_CLIENTS;
  216. $coll->ensureIndex(array('app_name' => 1), array('unique' => true));
  217. $coll->ensureIndex(array('api_key' => 1), array('unique' => true));
  218. $coll->ensureIndex(array('i_uid' => 1));
  219. try {
  220. if ($isUpdate) {
  221. d('cp');
  222. $this->oApi['edited_time'] = date('F j, Y g:i a T');
  223. $this->oApi['edit_ip'] = Request::getIP();
  224. $res = $this->oApi->save();
  225. } else {
  226. d('cp');
  227. $this->oApi['created_time'] = date('F j, Y g:i a T');
  228. $this->oApi['ip'] = Request::getIP();
  229. $res = $this->oApi->insert();
  230. }
  231. } catch ( \Exception $e ) {
  232. throw new \OutOfBoundsException($e->getMessage());
  233. }
  234. d('$res: ' . $res);
  235. return $this;
  236. }
  237. /**
  238. * Verify that the appid is owned by
  239. * the same user as Viewer
  240. *
  241. * @param int $appid
  242. *
  243. * @throws \Lampcms\Exception
  244. * @return \Lampcms\Controllers\Editapp
  245. */
  246. protected function validateAppIdOwnership($appid)
  247. {
  248. $a = $this->Registry->Mongo->API_CLIENTS->findOne(array('_id' => $appid));
  249. if ($a['i_uid'] !== $this->Registry->Viewer->getUid()) {
  250. throw new \Lampcms\Exception('@@You do not have permission to edit this application@@');
  251. }
  252. return $this;
  253. }
  254. /**
  255. * If use has uploaded an Icon for the app
  256. * then parse it and add to oApi object
  257. *
  258. * @return object $this
  259. */
  260. protected function parseIcon()
  261. {
  262. d('cp');
  263. if (!$this->Form->hasUploads() || (null === $tempPath = $this->Form->getUploadedFile('icon'))) {
  264. d('Icon not uploaded');
  265. return $this;
  266. }
  267. d('$tempPath: ' . $tempPath);
  268. IconParser::addIcon($this->oApi, $tempPath);
  269. return $this;
  270. }
  271. }