PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/package/app/app/alpha/web/secure_form.php

https://github.com/richhl/kalturaCE
PHP | 391 lines | 293 code | 58 blank | 40 comment | 21 complexity | bb33a80ed9f57a72bf58c850ab266aa0 MD5 | raw file
  1. <?php
  2. // Check if the user logged in
  3. if( !isset($_COOKIE['kmcks']) || empty($_COOKIE['kmcks']) ) {
  4. die('Not logged in!');
  5. }
  6. $sForm = new secForm();
  7. class secForm {
  8. var $pageTitle;
  9. var $email;
  10. var $fname;
  11. var $lname;
  12. var $partnerId;
  13. var $userId;
  14. var $Ks;
  15. var $error;
  16. var $curAction;
  17. function secForm() {
  18. //echo '<pre>'; print_r( explode(";", base64_decode($_COOKIE['kmcks'])) ); exit();
  19. // Get data from cookies
  20. $this->email = $_COOKIE['email'];
  21. $this->partnerId = $_COOKIE['pid'];
  22. $this->userId = $_COOKIE['uid'];
  23. $this->Ks = $_COOKIE['kmcks'];
  24. // Get data from url parameters
  25. $this->email = isset($_GET['email']) ? $this->clean($_GET['email']) : "";
  26. $this->fname = isset($_GET['fname']) ? $this->clean($_GET['fname']) : "";
  27. $this->lname = isset($_GET['lname']) ? $this->clean($_GET['lname']) : "";
  28. // select which action to do
  29. if( isset($_POST['do']) ) {
  30. switch($_POST['do']) {
  31. case "password":
  32. $this->doChangePassword();
  33. break;
  34. case "email":
  35. $this->doChangeEmail();
  36. break;
  37. case "name":
  38. $this->doChangeName();
  39. break;
  40. }
  41. }
  42. // select which form to show
  43. switch($_GET['action']) {
  44. case "password":
  45. $this->showChangePassword();
  46. break;
  47. case "email":
  48. $this->showChangeEmail();
  49. break;
  50. case "name":
  51. $this->showChangeName();
  52. break;
  53. }
  54. }
  55. // Return a Client with KS
  56. function getClient() {
  57. // Get kaltura configuration file
  58. require_once( realpath( dirname(__FILE__) ) . '/../config/kConf.php' );
  59. $kConf = new kConf();
  60. // Load kaltura client
  61. require_once( realpath( dirname(__FILE__) ) . '/../../clients/php5/KalturaClient.php' );
  62. try {
  63. $conf = new KalturaConfiguration( $this->partnerId );
  64. $conf->serviceUrl = 'http://' . $kConf->get('www_host');
  65. $client = new KalturaClient( $conf );
  66. $client->setKS( $this->Ks );
  67. } catch( Exception $e ){
  68. $this->error = 'Error setting KS. <a href="'.$_SERVER['SCRIPT_NAME'].'">Try again</a>';
  69. die($this->error);
  70. return false;
  71. }
  72. return $client;
  73. }
  74. function clean($str) {
  75. $str = str_replace("javascript:", "", $str);
  76. $str = str_replace("eval", "", $str);
  77. $str = str_replace("document", "", $str);
  78. $str = htmlspecialchars($str);
  79. $str = addslashes($str);
  80. return $str;
  81. }
  82. // Handle errors
  83. function errorDiv() {
  84. if( isset($this->error) && !empty($this->error) ) {
  85. //echo '<pre>'; print_r($_COOKIE); exit();
  86. //return '<div class="error">' . $this->error . '</div><br />';
  87. $error = str_replace("&lt;", "<", $this->error);
  88. $error = str_replace("&gt;", ">", $error);
  89. return '<script>alert(' . json_encode($error) . ');</script>';
  90. } else {
  91. return '';
  92. }
  93. }
  94. // Show Change Password Form
  95. function showChangePassword() {
  96. $this->pageTitle = 'Change Password';
  97. $this->showHead();
  98. echo <<<HTML
  99. <form method="post"><br />
  100. <input type="hidden" name="do" value="password" />
  101. <div class="left">Current Password:</div>
  102. <div class="right"><input id="focused" type="password" name="cur_password" /></div>
  103. <br class="clear" />
  104. <div class="left">New Password:</div>
  105. <div class="right"><input type="password" name="new_password" /></div>
  106. <br class="clear" />
  107. <div class="left">Retry New Password:</div>
  108. <div class="right"><input type="password" name="retry_new_password" /></div>
  109. <br class="clear" /><br />
  110. <div class="center"><button type="submit" id="submit"><span>Save</span></button></div><br />
  111. {$this->errorDiv()}
  112. </form>
  113. HTML;
  114. $this->showFoot();
  115. }
  116. // Do Change Password
  117. function doChangePassword() {
  118. // Set current action
  119. $this->curAction = 'password';
  120. // Checks if we have empty fields
  121. $required = array('cur_password', 'new_password', 'retry_new_password');
  122. foreach($required as $req) {
  123. if( empty($_POST[$req]) ) {
  124. $this->error = 'You must fill all the fields.';
  125. $this->showChangePassword();
  126. exit();
  127. break;
  128. }
  129. }
  130. if( $_POST['new_password'] != $_POST['retry_new_password'] ) {
  131. $this->error = "The passwords does not match!";
  132. $this->showChangePassword();
  133. exit();
  134. }
  135. $client = $this->getClient();
  136. try {
  137. //updateLoginData accepts [oldUserID, oldPassword, newUserID, newPassword, newFirstName, newLastName)
  138. $client->user->updateLoginData($this->email, $_POST['cur_password'], null, $_POST['new_password'], null, null);
  139. // Show success message
  140. $this->showSuccess();
  141. exit();
  142. } catch( Exception $e ){
  143. $this->error = $e->getMessage();
  144. $this->showChangePassword();
  145. exit();
  146. }
  147. }
  148. // Show Change Email Form
  149. function showChangeEmail() {
  150. $this->pageTitle = 'Change Email Address';
  151. $this->showHead();
  152. echo <<<HTML
  153. <form method="post">
  154. <input type="hidden" name="do" value="email" />
  155. <div class="left">Current email address:</div>
  156. <div class="right current truncated" title="{$this->email}">{$this->email}</div>
  157. <br class="clear" />
  158. <div class="left">Edit email address:</div>
  159. <div class="right"><input id="focused" type="text" name="email" value="{$this->email}" /></div>
  160. <br class="clear" />
  161. <div class="left">Password:</div>
  162. <div class="right"><input type="password" name="password" /></div>
  163. <br class="clear" />
  164. <div class="note">* Password is required for editing your email address.</div><br />
  165. <div class="center"><button type="submit" id="submit"><span>Save</span></button></div><br />
  166. {$this->errorDiv()}
  167. </form>
  168. HTML;
  169. $this->showFoot();
  170. }
  171. // Do Change Email
  172. function doChangeEmail() {
  173. // Set current action
  174. $this->curAction = 'email';
  175. // Checks if we have empty fields
  176. $required = array('email', 'password');
  177. foreach($required as $req) {
  178. if( empty($_POST[$req]) ) {
  179. $this->error = 'You must fill all the fields.';
  180. $this->showChangeEmail();
  181. exit();
  182. break;
  183. }
  184. }
  185. $client = $this->getClient();
  186. try {
  187. $client->user->updateLoginData($this->email, $_POST['password'], $_POST['email'], null, null, null);
  188. // Show success message
  189. $this->showSuccess();
  190. exit();
  191. } catch( Exception $e ){
  192. $this->error = $e->getMessage();
  193. $this->showChangeEmail();
  194. exit();
  195. }
  196. }
  197. // Show Change Name Form
  198. function showChangeName() {
  199. $this->pageTitle = 'Change Username';
  200. $this->showHead();
  201. echo <<<HTML
  202. <form method="post">
  203. <input type="hidden" name="do" value="name" />
  204. <div class="left">Current name:</div>
  205. <div class="right current truncated" title="{$this->fname} {$this->lname}">{$this->fname} {$this->lname}</div>
  206. <br class="clear" />
  207. <div class="left">Edit First Name:</div>
  208. <div class="right"><input type="text" name="fname" value="{$this->fname}" /></div>
  209. <br class="clear" />
  210. <div class="left">Edit Last Name:</div>
  211. <div class="right"><input type="text" name="lname" value="{$this->lname}" /></div>
  212. <br class="clear" />
  213. <div class="left">Password:</div>
  214. <div class="right"><input type="password" name="password" /></div>
  215. <br class="clear" />
  216. <div class="note">* Password is required for editing your name.</div><br />
  217. <div class="center"><button type="submit" id="submit"><span>Save</span></button></div><br />
  218. {$this->errorDiv()}
  219. </form>
  220. HTML;
  221. $this->showFoot();
  222. }
  223. // Do Change Name
  224. function doChangeName() {
  225. // Set current action
  226. $this->curAction = 'name';
  227. // Checks if we have empty fields
  228. $required = array('fname', 'lname', 'password');
  229. foreach($required as $req) {
  230. if( empty($_POST[$req]) ) {
  231. $this->error = 'You must fill all the fields.';
  232. $this->showChangeName();
  233. exit();
  234. break;
  235. }
  236. }
  237. $client = $this->getClient();
  238. try {
  239. // Changing name
  240. $client->user->updateLoginData($this->email, $_POST['password'], null, null, $_POST['fname'], $_POST['lname']);
  241. setcookie("screen_name", $_POST['fname'] . ' ' . $_POST['lname'] );
  242. // Show success message
  243. $this->showSuccess();
  244. exit();
  245. } catch( Exception $e ){
  246. //echo '<pre>'; print_r($e); exit();
  247. // Show error
  248. $this->error = $e->getMessage();
  249. $this->showChangeName();
  250. exit();
  251. }
  252. }
  253. // Show Success Message
  254. function showSuccess() {
  255. $parent_url = $this->clean($_GET['parent']);
  256. $this->pageTitle = 'Changes were saved!';
  257. $this->showHead();
  258. // When changing password we only closing the modal
  259. // Otherwise clode the modal & reload the page
  260. if($this->curAction == 'password') {
  261. $msg = "close";
  262. } else {
  263. $msg = "reload";
  264. }
  265. // We're using postMessage to pass data to the parent document
  266. echo <<<HTML
  267. <script type="text/javascript" src="/lib/js/postmessage.js"></script>
  268. <script type="text/javascript">
  269. var parent_url = decodeURIComponent("{$parent_url}");
  270. function send() {
  271. XD.postMessage("{$msg}", parent_url, parent);
  272. }
  273. window.onload = send;
  274. </script>
  275. HTML;
  276. $this->showFoot();
  277. }
  278. // Show Layout Header
  279. function showHead() {
  280. echo <<<HTML
  281. <html>
  282. <head>
  283. <title>{$this->pageTitle}</title>
  284. <meta charset="utf-8" />
  285. <style>
  286. html, body { margin: 0; padding: 0; width: 100%; height: 100%; }
  287. body { background:#F8F8F8; font: 13px arial,sans-serif; }
  288. #wrapper { padding: 0 10px; }
  289. .left { float: left; text-align: left; width: 140px; margin: 5px 0; padding: 4px 0 0 0; }
  290. .right { float: left; text-align: left; margin: 5px 20px 5px 0; padding: 0; }
  291. .current { padding-top: 4px; padding-left: 2px; width: 155px; color: #666666; }
  292. .note { font-size: 11px; color: #999999; }
  293. .center { text-align: center; }
  294. .clear { clear: both; }
  295. .error { color: #ff0000; font-weight: bold; font-size: 12px; margin-bottom: -10px; }
  296. input { font-size: 13px; width: 170px; }
  297. .truncated { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  298. button { margin: 0 auto 5px; padding: 0 28px 0 0; height: 24px; border: 0; font: normal 11px arial,sans-serif; color:#2B2B2B; line-height: normal; overflow: visible; background: url(lib/images/kmc/kmc_sprite.png) no-repeat -72px -152px; cursor: pointer; }
  299. button span { height:20px; padding: 4px 0 0 28px; margin: 1px 1px 0 0; float:left; white-space:nowrap; background:transparent url(lib/images/kmc/kmc_sprite.png) no-repeat scroll 0 -153px;}
  300. button:hover span { background-position: 0 -178px;}
  301. @-moz-document url-prefix() {
  302. button span { margin: 0 2px 0 -3px; }
  303. button { display: block; padding-right: 20px; }
  304. }
  305. </style>
  306. <script>
  307. function focusFirstInput() {
  308. // check all the input in the form
  309. for(i=0; i < document.forms[0].length; i++)
  310. {
  311. // check if input is not hidden & not disabled
  312. if ( (document.forms[0][i].type != "hidden") && (document.forms[0][i].disabled != true) )
  313. {
  314. document.forms[0][i].focus();
  315. break;
  316. }
  317. }
  318. }
  319. </script>
  320. </head>
  321. <body onload="focusFirstInput();">
  322. <div id="wrapper">
  323. HTML;
  324. }
  325. // Show Layout Footer
  326. function showFoot() {
  327. echo <<<HTML
  328. </div>
  329. </body></html>
  330. HTML;
  331. }
  332. }
  333. ?>