PageRenderTime 31ms CodeModel.GetById 4ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/1.0RC3/www/shared_roles_edit.php

http://scalr.googlecode.com/
PHP | 319 lines | 266 code | 42 blank | 11 comment | 33 complexity | 89313210ba573402cf13850e2c7f923d MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, GPL-3.0
  1. <?
  2. require("src/prepend.inc.php");
  3. if ($_SESSION["uid"] != 0)
  4. UI::Redirect("index.php");
  5. if ($req_ami_id)
  6. $display["title"] = _("Shared roles&nbsp;&raquo;&nbsp;Edit");
  7. else
  8. $display["title"] = _("Shared roles&nbsp;&raquo;&nbsp;Add new");
  9. $reflect = new ReflectionClass("ROLE_ALIAS");
  10. $display["aliases"] = array_values($reflect->getConstants());
  11. if ($_POST)
  12. {
  13. $display = array_merge($display, $_POST);
  14. $Validator = new Validator();
  15. if (!preg_match("/[A-Za-z0-9-]+/", $post_name))
  16. $err[] = _("Allowed chars for role name is [A-Za-z0-9-]");
  17. if (!$Validator->IsNumeric($post_default_minLA) || $post_default_minLA < 1)
  18. $err[] = _("Invalid value for minimum LA");
  19. if (!$Validator->IsNumeric($post_default_maxLA) || $post_default_maxLA > 99)
  20. $err[] = _("Invalid value for maximum LA");
  21. if ($post_ami_id)
  22. {
  23. $info = $db->GetRow("SELECT * FROM ami_roles WHERE ami_id=? AND roletype=?", array($post_ami_id, ROLE_TYPE::SHARED));
  24. if (!$info)
  25. {
  26. $AmazonEC2 = new AmazonEC2(
  27. APPPATH . "/etc/pk-".CONFIG::$AWS_KEYNAME.".pem",
  28. APPPATH . "/etc/cert-".CONFIG::$AWS_KEYNAME.".pem", true
  29. );
  30. // Generate DescribeImagesType object
  31. $DescribeImagesType = new DescribeImagesType();
  32. $DescribeImagesType->imagesSet->item = array("imageId" => $post_ami_id);
  33. // get information about shared AMIs
  34. try
  35. {
  36. $response = $AmazonEC2->DescribeImages($DescribeImagesType);
  37. if ($response && $response->imagesSet && $response->imagesSet->item)
  38. $post_arch = $response->imagesSet->item->architecture;
  39. else
  40. {
  41. $post_ami_id = false;
  42. $req_ami_id = false;
  43. $err[] = _("Cannot get information about AMI from Amazon");
  44. $display = array_merge($display, $_POST);
  45. }
  46. }
  47. catch(Exception $e)
  48. {
  49. $err[] = $e->getMessage();
  50. }
  51. }
  52. }
  53. else
  54. {
  55. $err[] = _("AMI required");
  56. }
  57. $isstable = ($post_isstable == 1) ? '1' : '0';
  58. if (count($err) == 0)
  59. {
  60. if (!$info)
  61. {
  62. $db->BeginTrans();
  63. try
  64. {
  65. // Add ne role to database
  66. $db->Execute("INSERT INTO ami_roles SET
  67. ami_id=?,
  68. roletype=?,
  69. clientid='0',
  70. name=?,
  71. default_minLA=?,
  72. default_maxLA=?,
  73. alias=?,
  74. architecture=?,
  75. isstable=?,
  76. description=?",
  77. array(
  78. $post_ami_id,
  79. ROLE_TYPE::SHARED,
  80. $post_name,
  81. $post_default_minLA,
  82. $post_default_maxLA,
  83. $post_alias,
  84. $post_arch,
  85. $isstable,
  86. $post_description
  87. )
  88. );
  89. $roleid = $db->Insert_ID();
  90. // Add security rules
  91. foreach ($post_rules as $rule)
  92. $db->Execute("INSERT INTO security_rules SET roleid=?, rule=?", array($roleid, $rule));
  93. }
  94. catch (Exception $e)
  95. {
  96. $db->RollbackTrans();
  97. throw new ApplicationException($e->getMessage(), E_ERROR);
  98. }
  99. $okmsg = _("Role successfully assigned to AMI");
  100. }
  101. else
  102. {
  103. $db->BeginTrans();
  104. try
  105. {
  106. // Add ne role to database
  107. $db->Execute("UPDATE ami_roles SET isstable=?, description=?, name=?, default_minLA=?, default_maxLA=?, alias=? WHERE ami_id=?",
  108. array($isstable, $post_description, $post_name, $post_default_minLA, $post_default_maxLA, $post_alias, $post_ami_id)
  109. );
  110. // Add security rules
  111. $roleid = $db->GetOne("SELECT id FROM ami_roles WHERE ami_id=?", $post_ami_id);
  112. $db->Execute("DELETE FROM security_rules WHERE roleid=?", $roleid);
  113. foreach ($post_rules as $rule)
  114. $db->Execute("INSERT INTO security_rules SET roleid=?, rule=?", array($roleid, $rule));
  115. if ($info['name'] != $post_name)
  116. {
  117. $db->Execute("UPDATE elastic_ips SET role_name=? WHERE role_name=?",
  118. array($post_name, $info['name'])
  119. );
  120. $db->Execute("UPDATE zones SET role_name=? WHERE role_name=?",
  121. array($post_name, $info['name'])
  122. );
  123. $db->Execute("UPDATE farm_instances SET role_name=? WHERE role_name=?",
  124. array($post_name, $info['name'])
  125. );
  126. $db->Execute("UPDATE farm_ebs SET role_name=? WHERE role_name=?",
  127. array($post_name, $info['name'])
  128. );
  129. $db->Execute("UPDATE vhosts SET role_name=? WHERE role_name=?",
  130. array($post_name, $info['name'])
  131. );
  132. }
  133. }
  134. catch (Exception $e)
  135. {
  136. $db->RollbackTrans();
  137. throw new ApplicationException($e->getMessage(), E_ERROR);
  138. }
  139. $okmsg = _("Role successfully updated");
  140. }
  141. try
  142. {
  143. // Save role options
  144. $options = json_decode($post_role_options_dataform, true);
  145. $opt_names = array("''");
  146. if (count($options) > 0)
  147. {
  148. foreach ($options as $option)
  149. {
  150. if (!$option['name'] || !$option['type'])
  151. continue;
  152. if ($option['type'] == FORM_FIELD_TYPE::SELECT)
  153. {
  154. $option['defval'] = array();
  155. foreach ($option['options'] as $opt)
  156. {
  157. if ($opt[3])
  158. array_push($option['defval'], $opt[0]);
  159. }
  160. $option['defval'] = implode(",", $option['defval']);
  161. }
  162. $db->Execute("INSERT INTO role_options SET
  163. name=?, type=?, isrequired=?, defval=?, allow_multiple_choice=?, options=?, ami_id=?, hash=?
  164. ON DUPLICATE KEY UPDATE type=?, isrequired=?, defval=?, allow_multiple_choice=?, options=?
  165. ", array(
  166. // For insertion
  167. $option['name'],
  168. $option['type'],
  169. $option['required'],
  170. $option['defval'],
  171. (int)$option['allow_multiple_choise'],
  172. json_encode($option['options']),
  173. $post_ami_id,
  174. preg_replace("/[^A-Za-z0-9]+/", "_", strtolower($option['name'])),
  175. // For update
  176. $option['type'],
  177. $option['required'],
  178. $option['defval'],
  179. (int)$option['allow_multiple_choise'],
  180. json_encode($option['options'])
  181. ));
  182. array_push($opt_names, "'{$option['name']}'");
  183. }
  184. }
  185. // Remove removed options from database
  186. $db->Execute("DELETE FROM role_options WHERE ami_id=? AND name NOT IN (".implode(",", $opt_names).")",
  187. array($post_ami_id)
  188. );
  189. }
  190. catch(Exception $e)
  191. {
  192. $db->RollbackTrans();
  193. throw new ApplicationException($e->getMessage(), E_ERROR);
  194. }
  195. // Commit transaction and redirect to shared roles view page
  196. $db->CommitTrans();
  197. UI::Redirect("shared_roles.php");
  198. }
  199. }
  200. if ($req_ami_id)
  201. {
  202. $display["ami_id"] = $req_ami_id;
  203. $info = $db->GetRow("SELECT * FROM ami_roles WHERE ami_id=? AND roletype=?", array($req_ami_id, ROLE_TYPE::SHARED));
  204. if ($info)
  205. {
  206. $display = array_merge($display, $info);
  207. $display["arch"] = $info["architecture"];
  208. $rules = $db->GetAll("SELECT * FROM security_rules WHERE roleid=?", array($info['id']));
  209. $display["rules"] = array();
  210. foreach ($rules as $rule)
  211. {
  212. $chunks = explode(":", $rule["rule"]);
  213. $display["rules"][] = array(
  214. "rule" => $rule["rule"],
  215. "id" => $rule["id"],
  216. "protocol" => $chunks[0],
  217. "portfrom" => $chunks[1],
  218. "portto" => $chunks[2],
  219. "ipranges" => $chunks[3]
  220. );
  221. }
  222. $options = $db->GetAll("SELECT * FROM role_options WHERE ami_id=?", array($info['ami_id']));
  223. $role_options = array();
  224. if (count($options) > 0)
  225. {
  226. foreach ($options as $opt)
  227. {
  228. $option = new stdClass();
  229. $option->name = $opt['name'];
  230. $option->type = $opt['type'];
  231. $option->required = (bool)$opt['isrequired'];
  232. $option->defval = $opt['defval'];
  233. $option->allow_multiple_choise = (bool)$opt['allow_multiple_choice'];
  234. $option->options = json_decode($opt['options']);
  235. $role_options[] = $option;
  236. }
  237. $display['role_options_dataform'] = json_encode($role_options);
  238. }
  239. }
  240. }
  241. else
  242. {
  243. $display["rules"] = array(
  244. array(
  245. "rule" => "udp:161:162:0.0.0.0/0",
  246. "id" => rand(0, 1000000),
  247. "protocol" => "udp",
  248. "portfrom" => 161,
  249. "portto" => 162,
  250. "ipranges" => "0.0.0.0/0"
  251. ),
  252. array(
  253. "rule" => "tcp:22:22:0.0.0.0/0",
  254. "id" => rand(0, 1000000),
  255. "protocol" => "tcp",
  256. "portfrom" => 22,
  257. "portto" => 22,
  258. "ipranges" => "0.0.0.0/0"
  259. ),
  260. array(
  261. "rule" => "icmp:-1:-1:0.0.0.0/0",
  262. "id" => rand(0, 1000000),
  263. "protocol" => "icmp",
  264. "portfrom" => -1,
  265. "portto" => -1,
  266. "ipranges" => "0.0.0.0/0"
  267. )
  268. );
  269. }
  270. $display["selected_tab"] = "general";
  271. $display["tabs_list"] = array(
  272. "general" => _("Role information"),
  273. "security" => _("Security settings"),
  274. "options" => _("Options")
  275. );
  276. require("src/append.inc.php");
  277. ?>