PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/phpmyfaq/admin/attachment.php

https://github.com/cyrke/phpMyFAQ
PHP | 214 lines | 148 code | 30 blank | 36 comment | 32 complexity | 6f5a9316dbc01e3c3eff9f4fdd8fe8e8 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * Select an attachment and save it
  4. *
  5. * PHP Version 5.3
  6. *
  7. * This Source Code Form is subject to the terms of the Mozilla Public License,
  8. * v. 2.0. If a copy of the MPL was not distributed with this file, You can
  9. * obtain one at http://mozilla.org/MPL/2.0/.
  10. *
  11. * @category phpMyFAQ
  12. * @package Administration
  13. * @author Thorsten Rinne <thorsten@phpmyfaq.de>
  14. * @author Anatoliy Belsky <ab@php.net>
  15. * @copyright 2002-2012 phpMyFAQ
  16. * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
  17. * @link http://www.phpmyfaq.de
  18. * @since 2002-09-17
  19. */
  20. define('PMF_ROOT_DIR', dirname(__DIR__));
  21. //
  22. // Define the named constant used as a check by any included PHP file
  23. //
  24. define('IS_VALID_PHPMYFAQ', null);
  25. //
  26. // Autoload classes, prepend and start the PHP session
  27. //
  28. require_once PMF_ROOT_DIR.'/inc/Bootstrap.php';
  29. PMF_Init::cleanRequest();
  30. session_name(PMF_Session::PMF_COOKIE_NAME_AUTH);
  31. session_start();
  32. /**
  33. * Initialize attachment factory
  34. */
  35. PMF_Attachment_Factory::init(
  36. $faqConfig->get('records.attachmentsStorageType'),
  37. $faqConfig->get('records.defaultAttachmentEncKey'),
  38. $faqConfig->get('records.enableAttachmentEncryption')
  39. );
  40. $currentSave = PMF_Filter::filterInput(INPUT_POST, 'save', FILTER_SANITIZE_STRING);
  41. $currentAction = PMF_Filter::filterInput(INPUT_GET, 'action', FILTER_SANITIZE_STRING);
  42. $currentToken = PMF_Filter::filterInput(INPUT_POST, 'csrf', FILTER_SANITIZE_STRING);
  43. $Language = new PMF_Language($faqConfig);
  44. $LANGCODE = $Language->setLanguage($faqConfig->get('main.languageDetection'), $faqConfig->get('main.language'));
  45. require_once PMF_ROOT_DIR . '/lang/language_en.php';
  46. if (isset($LANGCODE) && PMF_Language::isASupportedLanguage($LANGCODE)) {
  47. require_once PMF_ROOT_DIR . '/lang/language_'.$LANGCODE.'.php';
  48. } else {
  49. $LANGCODE = 'en';
  50. }
  51. $auth = false;
  52. $user = PMF_User_CurrentUser::getFromSession($faqConfig);
  53. if ($user) {
  54. $auth = true;
  55. } else {
  56. $error = $PMF_LANG['ad_auth_sess'];
  57. $user = null;
  58. unset($user);
  59. }
  60. //
  61. // Get current user rights
  62. //
  63. $permission = array();
  64. if ($auth === true) {
  65. // read all rights, set them FALSE
  66. $allRights = $user->perm->getAllRightsData();
  67. foreach ($allRights as $right) {
  68. $permission[$right['name']] = false;
  69. }
  70. // check user rights, set them TRUE
  71. $allUserRights = $user->perm->getAllUserRights($user->getUserId());
  72. foreach ($allRights as $right) {
  73. if (in_array($right['right_id'], $allUserRights))
  74. $permission[$right['name']] = true;
  75. }
  76. }
  77. if (is_null($currentAction) || !is_null($currentSave)) {
  78. ?>
  79. <!DOCTYPE html>
  80. <!--[if lt IE 7 ]> <html lang="<?php print $PMF_LANG['metaLanguage']; ?>" class="no-js ie6"> <![endif]-->
  81. <!--[if IE 7 ]> <html lang="<?php print $PMF_LANG['metaLanguage']; ?>" class="no-js ie7"> <![endif]-->
  82. <!--[if IE 8 ]> <html lang="<?php print $PMF_LANG['metaLanguage']; ?>" class="no-js ie8"> <![endif]-->
  83. <!--[if IE 9 ]> <html lang="<?php print $PMF_LANG['metaLanguage']; ?>" class="no-js ie9"> <![endif]-->
  84. <!--[if (gt IE 9)|!(IE)]><!--> <html lang="<?php print $PMF_LANG['metaLanguage']; ?>" class="no-js"> <!--<![endif]-->
  85. <head>
  86. <meta charset="utf-8">
  87. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  88. <title><?php print $faqConfig->get('main.titleFAQ'); ?> - powered by phpMyFAQ</title>
  89. <base href="<?php print $faqConfig->get('main.referenceURL'); ?>/admin/" />
  90. <meta name="description" content="Only Chuck Norris can divide by zero.">
  91. <meta name="author" content="phpMyFAQ Team">
  92. <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;">
  93. <meta name="application-name" content="phpMyFAQ <?php print $faqConfig->get('main.currentVersion'); ?>">
  94. <meta name="copyright" content="(c) 2001-2012 phpMyFAQ Team">
  95. <meta name="publisher" content="phpMyFAQ Team">
  96. <meta name="MSSmartTagsPreventParsing" content="true">
  97. <link rel="stylesheet" href="assets/css/style.css?v=1">
  98. <script src="../assets/js/libs/modernizr.min.js"></script>
  99. <script src="../assets/js/libs/jquery.min.js"></script>
  100. <script src="../assets/js/phpmyfaq.js"></script>
  101. <link rel="shortcut icon" href="../assets/template/<?php print PMF_Template::getTplSetName(); ?>/favicon.ico">
  102. <link rel="apple-touch-icon" href="../assets/template/<?php print PMF_Template::getTplSetName(); ?>/apple-touch-icon.png">
  103. </head>
  104. <body class="attachments">
  105. <?php
  106. }
  107. if (is_null($currentAction) && $auth && $permission['addattachment']) {
  108. $recordId = filter_input(INPUT_GET, 'record_id', FILTER_VALIDATE_INT);
  109. $recordLang = filter_input(INPUT_GET, 'record_lang', FILTER_SANITIZE_STRING);
  110. ?>
  111. <form action="attachment.php?action=save" enctype="multipart/form-data" method="post">
  112. <fieldset>
  113. <legend><?php print $PMF_LANG["ad_att_addto"]." ".$PMF_LANG["ad_att_addto_2"]; ?></legend>
  114. <input type="hidden" name="MAX_FILE_SIZE" value="<?php print $faqConfig->get('records.maxAttachmentSize'); ?>" />
  115. <input type="hidden" name="record_id" value="<?php print $recordId; ?>" />
  116. <input type="hidden" name="record_lang" value="<?php print $recordLang; ?>" />
  117. <input type="hidden" name="save" value="TRUE" />
  118. <input type="hidden" name="csrf" value="<?php print $user->getCsrfTokenFromSession(); ?>" />
  119. <?php print $PMF_LANG["ad_att_att"]; ?> <input name="userfile" type="file" />
  120. <button class="btn btn-primary" type="submit">
  121. <?php print $PMF_LANG['ad_att_butt']; ?>
  122. </button>
  123. </fieldset>
  124. </form>
  125. <?php
  126. }
  127. if (!isset($_SESSION['phpmyfaq_csrf_token']) || $_SESSION['phpmyfaq_csrf_token'] !== $currentToken) {
  128. $auth = false;
  129. }
  130. if (!is_null($currentAction) && $auth && !$permission['addattachment']) {
  131. print $PMF_LANG['err_NotAuth'];
  132. }
  133. if (!is_null($currentSave) && $currentSave == true && $auth && $permission['addattachment']) {
  134. $recordId = filter_input(INPUT_POST, 'record_id', FILTER_VALIDATE_INT);
  135. $recordLang = filter_input(INPUT_POST, 'record_lang', FILTER_SANITIZE_STRING);
  136. ?>
  137. <p><strong><?php print $PMF_LANG["ad_att_addto"]." ".$PMF_LANG["ad_att_addto_2"]; ?></strong></p>
  138. <?php
  139. if (is_uploaded_file($_FILES["userfile"]["tmp_name"]) && !(filesize($_FILES["userfile"]["tmp_name"]) > $faqConfig->get('records.maxAttachmentSize'))) {
  140. $att = PMF_Attachment_Factory::create();
  141. $att->setRecordId($recordId);
  142. $att->setRecordLang($recordLang);
  143. /**
  144. * To add user defined key
  145. * $att->setKey($somekey, false);
  146. */
  147. try {
  148. $uploaded = $att->save($_FILES["userfile"]["tmp_name"], $_FILES["userfile"]["name"]);
  149. if ($uploaded) {
  150. print "<p>".$PMF_LANG["ad_att_suc"]."</p>";
  151. } else {
  152. throw new Exception;
  153. }
  154. } catch (Exception $e) {
  155. $att->delete();
  156. print "<p>".$PMF_LANG["ad_att_fail"]."</p>";
  157. }
  158. printf(
  159. '<p align="center"><a href="javascript:;" onclick="addAttachmentLink(%d, \'%s\');">%s</a></p>',
  160. $att->getId(),
  161. $att->getFilename(),
  162. $PMF_LANG['ad_att_close']
  163. );
  164. } else {
  165. printf(
  166. '<p>%s</p>',
  167. sprintf(
  168. $PMF_LANG['ad_attach_4'],
  169. $faqConfig->get('records.maxAttachmentSize')
  170. )
  171. );
  172. printf(
  173. '<p align="center"><a href="javascript:;" onclick="closeWindow();">%s</a></p>',
  174. $PMF_LANG['ad_att_close']
  175. );
  176. }
  177. }
  178. if (!is_null($currentSave) && $currentSave == true && $auth && !$permission['addattachment']) {
  179. print $PMF_LANG["err_NotAuth"];
  180. }
  181. $faqConfig->getDb()->close();
  182. ?>
  183. </body>
  184. </html>