PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/scalr-2/tags/scalr-2.1.0/app/www/script_templates.php

http://scalr.googlecode.com/
PHP | 350 lines | 336 code | 12 blank | 2 comment | 25 complexity | 24ea7dcd214df680a4a4f2900504e074 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, GPL-3.0
  1. <?
  2. require("src/prepend.inc.php");
  3. $display['load_extjs'] = true;
  4. $display["title"] = _("Script templates");
  5. $display["help"] = _("Scalr can execute scripts on instances upon various events. Your script templates along with parameters, will appear on farm/role configuration page under Scripting tab.");
  6. $display['Scalr_Session'] = Scalr_Session::getInstance();
  7. if (isset($post_cancel))
  8. UI::Redirect("script_templates.php");
  9. if ($req_task == 'fork')
  10. {
  11. // Get template infor from database
  12. $template = $db->GetRow("SELECT * FROM scripts WHERE id=?", array($req_id));
  13. if ($template['origin'] == SCRIPT_ORIGIN_TYPE::USER_CONTRIBUTED || $template['origin'] == SCRIPT_ORIGIN_TYPE::SHARED)
  14. {
  15. $script = $db->GetRow("SELECT * FROM script_revisions WHERE scriptid=? AND approval_state=? ORDER BY revision DESC",
  16. array($template['id'], APPROVAL_STATE::APPROVED)
  17. );
  18. $chk = $db->GetOne("SELECT id FROM scripts WHERE name=? AND clientid=?",
  19. array($template['name'], Scalr_Session::getInstance()->getClientId())
  20. );
  21. if ($chk)
  22. $name = "{$template['name']} #".rand(1000, 9999);
  23. else
  24. $name = $template['name'];
  25. if ($script)
  26. {
  27. $db->Execute("INSERT INTO scripts SET
  28. name = ?,
  29. description = ?,
  30. origin = ?,
  31. dtadded = NOW(),
  32. clientid = ?,
  33. approval_state = ?
  34. ", array(
  35. $name,
  36. $template['description'],
  37. SCRIPT_ORIGIN_TYPE::CUSTOM,
  38. Scalr_Session::getInstance()->getClientId(),
  39. APPROVAL_STATE::APPROVED
  40. ));
  41. $scriptid = $db->Insert_ID();
  42. $db->Execute("INSERT INTO script_revisions SET
  43. scriptid = ?,
  44. revision = ?,
  45. script = ?,
  46. dtcreated = NOW(),
  47. approval_state = ?
  48. ", array(
  49. $scriptid,
  50. 1,
  51. $script['script'],
  52. APPROVAL_STATE::APPROVED
  53. ));
  54. $okmsg = _("Script successfully forked");
  55. }
  56. }
  57. UI::Redirect("script_templates.php");
  58. }
  59. elseif ($req_task == 'create' || $req_task == 'edit' || $req_task == 'share')
  60. {
  61. if ($req_id)
  62. {
  63. // Get template info from database
  64. $template = $db->GetRow("SELECT * FROM scripts WHERE id=?", array($req_id));
  65. if (!$template || ($template['clientid'] == 0 && Scalr_Session::getInstance()->getClientId() != 0) ||
  66. ($template['clientid'] != 0 && Scalr_Session::getInstance()->getClientId() != 0 && Scalr_Session::getInstance()->getClientId() != $template['clientid'])
  67. ) {
  68. $errmsg = _("You don't have permissions to edit this template");
  69. UI::Redirect("script_templates.php");
  70. }
  71. // Get list of all revisions
  72. $dbversions = $db->GetAll("SELECT revision FROM script_revisions WHERE scriptid=?", array($template['id']));
  73. $versions = array();
  74. foreach ($dbversions as $version)
  75. {
  76. $versions[] = $version['revision'];
  77. }
  78. // Calculate current revision
  79. $display = array_merge($display, $template);
  80. $display['versions'] = $versions;
  81. $display['latest_version'] = max($versions);
  82. $display['selected_version'] = ($req_version) ? $req_version : max($versions);
  83. // Get script for current revision
  84. $display['script'] = $db->GetOne("SELECT script FROM script_revisions WHERE scriptid=? AND revision=?",
  85. array($template['id'], $display['selected_version'])
  86. );
  87. }
  88. else
  89. {
  90. $display['versions'] = array(1);
  91. }
  92. if ($_POST)
  93. {
  94. // Validate input data
  95. $Validator = new Validator();
  96. if (!$Validator->IsNotEmpty($post_name) && !$post_id)
  97. {
  98. $err[] = _("Script name required");
  99. }
  100. else
  101. {
  102. if (isset($post_cbtn_3) && $req_id)
  103. {
  104. $display['latest_version']++;
  105. $okmsg = _("Script template successfully saved. New version created.");
  106. }
  107. else
  108. {
  109. if ($req_id)
  110. $version = $display['selected_version'];
  111. else
  112. $version = 1;
  113. $okmsg = _("Script template successfully created");
  114. }
  115. }
  116. if (!$Validator->IsNotEmpty($post_name))
  117. $err[] = _("Script name required");
  118. if (!$Validator->IsNotEmpty($post_description))
  119. $err[] = _("Script description required");
  120. if (!$Validator->IsNotEmpty($post_script))
  121. $err[] = _("Script content required");
  122. if (count($err) == 0)
  123. {
  124. if ($req_task == "edit")
  125. {
  126. $db->Execute("UPDATE scripts SET
  127. name = ?,
  128. description = ?
  129. WHERE id = ?
  130. ", array(
  131. htmlspecialchars($post_name),
  132. htmlspecialchars($post_description),
  133. $req_id
  134. ));
  135. if ($template['origin'] == SCRIPT_ORIGIN_TYPE::USER_CONTRIBUTED)
  136. $approval_state = APPROVAL_STATE::PENDING;
  137. else
  138. $approval_state = APPROVAL_STATE::APPROVED;
  139. $okmsg = _("Script template successfully updated");
  140. }
  141. if ($req_id && !isset($post_cbtn_3))
  142. {
  143. if ($req_task == "share")
  144. {
  145. $approval_state = APPROVAL_STATE::PENDING;
  146. $db->Execute("UPDATE scripts SET
  147. name = ?,
  148. description = ?,
  149. origin = ?,
  150. approval_state = ?
  151. WHERE id = ?
  152. ", array(
  153. htmlspecialchars($post_name),
  154. htmlspecialchars($post_description),
  155. SCRIPT_ORIGIN_TYPE::USER_CONTRIBUTED,
  156. $approval_state,
  157. $req_id
  158. ));
  159. $okmsg = _("Thank you for contributing your script to Scalr community! After passing moderation, it will become available to other Scalr.net users.");
  160. //
  161. $client = $db->GetRow("SELECT * FROM clients WHERE id=?", array($template['clientid']));
  162. $script = array("name" => htmlspecialchars($post_name), "version" => $display['selected_version']);
  163. $send_email_to_team = true;
  164. //
  165. }
  166. $db->Execute("UPDATE script_revisions SET
  167. script = ?, approval_state = ?
  168. WHERE scriptid = ? AND revision = ?
  169. ", array(
  170. str_replace("\r\n", "\n", $post_script), $approval_state,
  171. $req_id, $display['selected_version']
  172. ));
  173. }
  174. else
  175. {
  176. if ($template && $template['origin'] == SCRIPT_ORIGIN_TYPE::USER_CONTRIBUTED)
  177. {
  178. $origin = SCRIPT_ORIGIN_TYPE::USER_CONTRIBUTED;
  179. $approval_state = APPROVAL_STATE::PENDING;
  180. $okmsg = _("The script has been successfully updated. It will become available to other Scalr.net users after moderation.");
  181. //
  182. $client = $db->GetRow("SELECT * FROM clients WHERE id=?", array($template['clientid']));
  183. $script = array("name" => $template['name'], "version" => $display['selected_version']+1);
  184. $send_email_to_team = true;
  185. //
  186. }
  187. else
  188. {
  189. if (!$template)
  190. {
  191. if (Scalr_Session::getInstance()->getClientId() != 0)
  192. $origin = SCRIPT_ORIGIN_TYPE::CUSTOM;
  193. else
  194. $origin = SCRIPT_ORIGIN_TYPE::SHARED;
  195. $approval_state = APPROVAL_STATE::APPROVED;
  196. $clientid = Scalr_Session::getInstance()->getClientId();
  197. }
  198. else
  199. {
  200. $approval_state = $template['approval_state'];
  201. $origin = $template['origin'];
  202. $clientid = $template['clientid'];
  203. }
  204. }
  205. if ($post_id)
  206. {
  207. $post_name = htmlspecialchars_decode($template['name']);
  208. $scriptid = $template["id"];
  209. }
  210. else
  211. {
  212. $db->Execute("INSERT INTO scripts SET
  213. name = ?,
  214. description = ?,
  215. origin = ?,
  216. dtadded = NOW(),
  217. clientid = ?,
  218. approval_state = ?
  219. ", array(
  220. htmlspecialchars($post_name),
  221. htmlspecialchars($post_description),
  222. $origin,
  223. $clientid,
  224. $approval_state
  225. ));
  226. $scriptid = $db->Insert_ID();
  227. }
  228. $revision = $db->GetOne("SELECT IF(MAX(revision), MAX(revision), 0) FROM script_revisions WHERE scriptid=?",
  229. array($scriptid)
  230. );
  231. $db->Execute("INSERT INTO script_revisions SET
  232. scriptid = ?,
  233. revision = ?,
  234. script = ?,
  235. dtcreated = NOW(),
  236. approval_state = ?
  237. ", array(
  238. $scriptid,
  239. $revision+1,
  240. str_replace("\r\n", "\n", $post_script),
  241. $approval_state
  242. ));
  243. }
  244. if ($send_email_to_team)
  245. {
  246. if (strlen($post_sharing_comments) > 0)
  247. {
  248. $db->Execute("INSERT INTO comments SET
  249. clientid = ?,
  250. object_owner = ?,
  251. dtcreated = NOW(),
  252. object_type = ?,
  253. comment = ?,
  254. objectid = ?,
  255. isprivate = '1'
  256. ", array(
  257. Scalr_Session::getInstance()->getClientId(),
  258. Scalr_Session::getInstance()->getClientId(),
  259. COMMENTS_OBJECT_TYPE::SCRIPT,
  260. htmlspecialchars($post_sharing_comments),
  261. $scriptid
  262. ));
  263. }
  264. //
  265. // Send mail to admins
  266. //
  267. $count = $db->GetOne("SELECT COUNT(*) FROM script_revisions WHERE approval_state=?",
  268. array(APPROVAL_STATE::PENDING)
  269. );
  270. $link = "http://{$_SERVER['HTTP_HOST']}/contrib_script_templates.php?approval_state=Pending";
  271. $emails = explode("\n", CONFIG::$TEAM_EMAILS);
  272. if (count($emails) > 0)
  273. {
  274. foreach ($emails as $email)
  275. {
  276. $email = trim($email);
  277. $Mailer->ClearAddresses();
  278. $res = $Mailer->Send("emails/contributed_script.eml",
  279. array("client" => $client, "script" => $script, "comments" => $post_sharing_comments, "count" => $count, "link" => $link),
  280. $email,
  281. ""
  282. );
  283. $Logger->info("Sending 'emails/contributed_script.eml' email to '{$email}'. Result: {$res}");
  284. if (!$res)
  285. $Logger->error($Mailer->ErrorInfo);
  286. }
  287. }
  288. }
  289. UI::Redirect("script_templates.php");
  290. }
  291. else
  292. $display["err"] = $err;
  293. }
  294. $display["sys_vars"] = CONFIG::$SCRIPT_BUILTIN_VARIABLES;
  295. $display["script_timeout"] = CONFIG::$SYNCHRONOUS_SCRIPT_TIMEOUT;
  296. $Smarty->assign($display);
  297. if ($req_task == 'share')
  298. $Smarty->display('script_template_share.tpl');
  299. else
  300. $Smarty->display('script_template_create.tpl');
  301. exit();
  302. }
  303. require("src/append.inc.php");
  304. ?>