PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/classes/Description.php

http://github.com/thibaud-rohmer/PhotoShow
PHP | 194 lines | 72 code | 32 blank | 90 comment | 15 complexity | 7cc4e2839201ef1967ff7b7d95a9b99e MD5 | raw file
  1. <?php
  2. /**
  3. * This file implements the class Description.
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * LICENSE:
  8. *
  9. * This file is part of PhotoShow.
  10. *
  11. * PhotoShow is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation, either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * PhotoShow is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with PhotoShow. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. * @category Website
  25. * @package Photoshow
  26. * @author Thibaud Rohmer <thibaud.rohmer@gmail.com>
  27. * @copyright 2011 Thibaud Rohmer
  28. * @license http://www.gnu.org/licenses/
  29. * @link http://github.com/thibaud-rohmer/PhotoShow
  30. */
  31. /**
  32. * Description
  33. *
  34. * Implements the creating, reading, editing, and
  35. * displaying of the description, from and to an xml
  36. * file.
  37. * The file is stored in
  38. * [Thumbs]/[imagepath]/.[image]_description.xml
  39. * Description Structure:
  40. * - Login
  41. * - Date
  42. * - Content
  43. *
  44. * @category Website
  45. * @package Photoshow
  46. * @author Thibaud Rohmer <thibaud.rohmer@gmail.com>
  47. * @copyright Thibaud Rohmer
  48. * @license http://www.gnu.org/licenses/
  49. * @link http://github.com/thibaud-rohmer/PhotoShow
  50. */
  51. class Description implements HTMLObject
  52. {
  53. /// Array of the description infos
  54. private $description=array();
  55. /// Path to item
  56. private $file;
  57. /// Path to description file
  58. private $descriptionfile;
  59. /// Urlencoded version of relative path to item
  60. private $webfile;
  61. /**
  62. * Read description for item $file
  63. *
  64. * @param string $file
  65. */
  66. public function __construct($file=null){
  67. /// No item, no description !
  68. if(!isset($file) || is_array($file)) return;
  69. /// No right to view
  70. if(!Judge::view($file))
  71. return;
  72. /// Set variables
  73. $this->file = $file;
  74. $basepath = File::a2r($file);
  75. /// Urlencode basepath
  76. $this->webfile = urlencode(File::a2r($file));
  77. /// Build relative path to description file
  78. if(is_file($file)){
  79. $description = dirname($basepath)."/.".mb_basename($file)."_description.xml";
  80. }else{
  81. $description = $basepath."/.description.xml";
  82. }
  83. /// Set absolute path to description file
  84. $this->descriptionfile = File::r2a($description,Settings::$thumbs_dir);
  85. /// Check that description file exists
  86. if(file_exists($this->descriptionfile)){
  87. $this->parse_description_file();
  88. }
  89. }
  90. /**
  91. * Add a description for item $file
  92. *
  93. * @param string $file
  94. * @param string $login
  95. * @param string $description
  96. */
  97. public static function add($file,$content,$login=""){
  98. /// Just to be really sure...
  99. if( !(CurrentUser::$admin || CurrentUser::$uploader) ){
  100. return;
  101. }
  102. /// Get context
  103. $description = new Description($file);
  104. if(empty($content)){
  105. /// Description is empty, might as well delete the file
  106. if(file_exists($description->descriptionfile))
  107. unlink($description->descriptionfile);
  108. return;
  109. }
  110. /// Store the description
  111. $description->description['login'] = $login;
  112. $description->description['content'] = $content;
  113. $description->description['date'] = date('j-m-y, h-i-s');
  114. /// And save it
  115. $description->save();
  116. }
  117. public function save(){
  118. $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><description></description>');
  119. $xml->addChild("login" , $this->description['login']);
  120. $xml->addChild("date" , $this->description['date']);
  121. $xml->content = $this->description['content'];
  122. if(!file_exists(dirname($this->descriptionfile))){
  123. @mkdir(dirname($this->descriptionfile),0750,true);
  124. }
  125. /// Write xml
  126. $xml->asXML($this->descriptionfile);
  127. }
  128. /**
  129. * Read content of description file, and
  130. * store it in variables
  131. *
  132. * @return void
  133. */
  134. private function parse_description_file(){
  135. $xml = simplexml_load_file($this->descriptionfile);
  136. $this->description['login'] = $xml->login;
  137. $this->description['date'] = $xml->date;
  138. $this->description['content'] = $xml->content;
  139. }
  140. /**
  141. * Display description on website
  142. *
  143. * @return void
  144. */
  145. public function toHTML($forInfosMenu=false){
  146. if(!$this->file)
  147. return;
  148. $desc = stripslashes(htmlentities($this->description['content'], ENT_QUOTES ,'UTF-8'));
  149. if (!$forInfosMenu)
  150. echo nl2br($desc);
  151. else if(CurrentUser::$admin || CurrentUser::$uploader){
  152. // It's for the Infos menu, but only the admin or an uploader can write a description
  153. echo '<h3>'.Settings::_("description","description").'</h3>';
  154. echo "<form action='?t=Des&f=".$this->webfile."' class='pure-form pure-form-stacked' id='description_form' method='post'><fieldset class='transparent'>\n";
  155. echo "<input type='hidden' name='login' id='login' value='".htmlentities(CurrentUser::$account->login, ENT_QUOTES ,'UTF-8')."' readonly>";
  156. echo "<textarea name='content' id='content' placeholder='".Settings::_("description","description")."'>".$desc."</textarea>\n";
  157. echo "<input type='submit' class='pure-button pure-button-primary' value='".Settings::_("description","submit")."'></fieldset>\n";
  158. echo "</form>\n";
  159. }
  160. }
  161. }
  162. ?>