PageRenderTime 83ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/modules/myalbum0/submit.php

https://github.com/severnaya99/Sg-2010
PHP | 371 lines | 293 code | 50 blank | 28 comment | 53 complexity | acda274d8912b1a1620c3ec3a19318dc MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-2.0
  1. <?php
  2. // ------------------------------------------------------------------------- //
  3. // myAlbum-P - XOOPS photo album //
  4. // <http://www.peak.ne.jp/> //
  5. // ------------------------------------------------------------------------- //
  6. include( 'header.php' ) ;
  7. include_once( XOOPS_ROOT_PATH . '/class/xoopstree.php' ) ;
  8. include_once( 'class/myuploader.php' ) ;
  9. include_once( 'class/myalbum.textsanitizer.php' ) ;
  10. $myts =& MyAlbumTextSanitizer::getInstance() ;
  11. $cattree = new XoopsTree( $table_cat , "cid" , "pid" ) ;
  12. // GET variables
  13. $caller = empty( $_GET['caller'] ) ? '' : $_GET['caller'] ;
  14. // POST variables
  15. $preview_name = empty( $_POST['preview_name'] ) ? '' : $_POST['preview_name'] ;
  16. // check INSERTABLE
  17. if( ! ( $global_perms & GPERM_INSERTABLE ) ) {
  18. redirect_header( XOOPS_URL."/user.php" , 2 , _ALBM_MUSTREGFIRST ) ;
  19. exit ;
  20. }
  21. // check Categories exist
  22. $result = $xoopsDB->query( "SELECT count(cid) as count FROM $table_cat" ) ;
  23. list( $count ) = $xoopsDB->fetchRow( $result ) ;
  24. if( $count < 1 ) {
  25. redirect_header( XOOPS_URL."/modules/$mydirname/" , 2 , _ALBM_MUSTADDCATFIRST ) ;
  26. exit ;
  27. }
  28. // check file_uploads = on
  29. if( ! ini_get( "file_uploads" ) ) $file_uploads_off = true ;
  30. // get flag of safe_mode
  31. $safe_mode_flag = ini_get( "safe_mode" ) ;
  32. // check or make photos_dir
  33. if( ! is_dir( $photos_dir ) ) {
  34. if( $safe_mode_flag ) {
  35. redirect_header(XOOPS_URL."/modules/$mydirname/",10,"At first create & chmod 777 '$photos_dir' by ftp or shell.");
  36. exit ;
  37. }
  38. $rs = mkdir( $photos_dir , 0777 ) ;
  39. if( ! $rs ) {
  40. redirect_header(XOOPS_URL."/modules/$mydirname/",10,"$photos_dir is not a directory");
  41. exit ;
  42. } else @chmod( $photos_dir , 0777 ) ;
  43. }
  44. // check or make thumbs_dir
  45. if( $myalbum_makethumb && ! is_dir( $thumbs_dir ) ) {
  46. if( $safe_mode_flag ) {
  47. redirect_header(XOOPS_URL."/modules/$mydirname/",10,"At first create & chmod 777 '$thumbs_dir' by ftp or shell.");
  48. exit ;
  49. }
  50. $rs = mkdir( $thumbs_dir , 0777 ) ;
  51. if( ! $rs ) {
  52. redirect_header(XOOPS_URL."/modules/$mydirname/",10,"$thumbs_dir is not a directory");
  53. exit ;
  54. } else @chmod( $thumbs_dir , 0777 ) ;
  55. }
  56. // check or set permissions of photos_dir
  57. if( ! is_writable( $photos_dir ) || ! is_readable( $photos_dir ) ) {
  58. $rs = chmod( $photos_dir , 0777 ) ;
  59. if( ! $rs ) {
  60. redirect_header(XOOPS_URL."/modules/$mydirname/",5,"chmod 0777 into $photos_dir failed");
  61. exit ;
  62. }
  63. }
  64. // check or set permissions of thumbs_dir
  65. if( $myalbum_makethumb && ! is_writable( $thumbs_dir ) ) {
  66. $rs = chmod( $thumbs_dir , 0777 ) ;
  67. if( ! $rs ) {
  68. redirect_header(XOOPS_URL."/modules/$mydirname/",5,"chmod 0777 into $thumbs_dir failed");
  69. exit ;
  70. }
  71. }
  72. if( ! empty( $_POST['submit'] ) ) {
  73. // anti-CSRF
  74. if( ! xoops_refcheck() ) die( "XOOPS_URL is not included in your REFERER" ) ;
  75. $submitter = $my_uid ;
  76. $cid = empty( $_POST['cid'] ) ? 0 : intval( $_POST['cid'] ) ;
  77. $newid = $xoopsDB->genId( $table_photos."_lid_seq" ) ;
  78. // Check if cid is valid
  79. if( $cid <= 0 ) {
  80. redirect_header( 'submit.php' , 2 , 'Category is not specified.' ) ;
  81. exit ;
  82. }
  83. // Check if upload file name specified
  84. $field = $_POST["xoops_upload_file"][0] ;
  85. if( empty( $field ) || $field == "" ) {
  86. die( "UPLOAD error: file name not specified" ) ;
  87. }
  88. $field = $_POST['xoops_upload_file'][0] ;
  89. if( $_FILES[$field]['name'] == '' ) {
  90. // No photo uploaded
  91. if( trim( $_POST["title"] ) === "" ) {
  92. $_POST['title'] = 'no title' ;
  93. }
  94. if( $preview_name != '' && is_readable( "$photos_dir/$preview_name" ) ) {
  95. $tmp_name = $preview_name ;
  96. } else {
  97. if( empty( $myalbum_allownoimage ) ) {
  98. redirect_header( 'submit.php' , 2 , _ALBM_NOIMAGESPECIFIED ) ;
  99. exit ;
  100. } else {
  101. @copy( "$mod_path/images/pixel_trans.gif" , "$photos_dir/pixel_trans.gif" ) ;
  102. $tmp_name = 'pixel_trans.gif' ;
  103. }
  104. }
  105. } else if( $_FILES[$field]['tmp_name'] == "" ) {
  106. // Fail to upload (wrong file name etc.)
  107. redirect_header( 'submit.php' , 2 , _ALBM_FILEERROR ) ;
  108. exit ;
  109. } else {
  110. if( $myalbum_canresize ) $uploader = new MyXoopsMediaUploader( $photos_dir , $array_allowed_mimetypes , $myalbum_fsize , null , null , $array_allowed_exts ) ;
  111. else $uploader = new MyXoopsMediaUploader( $photos_dir , $array_allowed_mimetypes , $myalbum_fsize , $myalbum_width , $myalbum_height , $array_allowed_exts ) ;
  112. $uploader->setPrefix( 'tmp_' ) ;
  113. if( $uploader->fetchMedia( $field ) && $uploader->upload() ) {
  114. // Succeed to upload
  115. // The original file name will be the title if title is empty
  116. if( trim( $_POST["title"] ) === "" ) {
  117. $_POST['title'] = $uploader->getMediaName() ;
  118. }
  119. $tmp_name = $uploader->getSavedFileName() ;
  120. } else {
  121. // Fail to upload (sizeover etc.)
  122. include(XOOPS_ROOT_PATH."/header.php");
  123. echo $uploader->getErrors();
  124. @unlink( $uploader->getSavedDestination() ) ;
  125. include( XOOPS_ROOT_PATH . "/footer.php" ) ;
  126. exit ;
  127. }
  128. }
  129. if( ! is_readable( "$photos_dir/$tmp_name" ) ) {
  130. redirect_header( 'submit.php' , 2 , _ALBM_FILEREADERROR ) ;
  131. exit ;
  132. }
  133. $title = $myts->stripSlashesGPC( $_POST["title"] ) ;
  134. $desc_text = $myts->stripSlashesGPC( $_POST["desc_text"] ) ;
  135. $date = time() ;
  136. $ext = substr( strrchr( $tmp_name , '.' ) , 1 ) ;
  137. $status = ( $global_perms & GPERM_SUPERINSERT ) ? 1 : 0 ;
  138. $sql = "INSERT INTO $table_photos (lid, cid, title, ext, submitter, status, date, hits, rating, votes, comments) VALUES ($newid, $cid, '".addslashes($title)."', '$ext', $submitter, $status, $date, 0, 0, 0, 0)";
  139. $xoopsDB->query( $sql ) or die( "DB error: INSERT photo table" ) ;
  140. if( $newid == 0 ) {
  141. $newid = $xoopsDB->getInsertId();
  142. }
  143. myalbum_modify_photo( "$photos_dir/$tmp_name" , "$photos_dir/$newid.$ext" ) ;
  144. $dim = GetImageSize( "$photos_dir/$newid.$ext" ) ;
  145. if( $dim ) $xoopsDB->query( "UPDATE $table_photos SET res_x='{$dim[0]}', res_y='{$dim[1]}' WHERE lid='$newid'") ;
  146. if( ! myalbum_create_thumb( "$photos_dir/$newid.$ext" , $newid , $ext ) ) {
  147. $xoopsDB->query( "DELETE FROM $table_photos WHERE lid=$newid" ) ;
  148. redirect_header( 'submit.php' , 2 , _ALBM_FILEREADERROR ) ;
  149. exit ;
  150. }
  151. $xoopsDB->query( "INSERT INTO $table_text (lid, description) VALUES ($newid, '".addslashes($desc_text)."')") or die( "DB error: INSERT text table" ) ;
  152. // Update User's Posts (Should be modified when need admission.)
  153. $xoopsDB->query( "UPDATE ".$xoopsDB->prefix('users')." SET posts=posts+'$myalbum_addposts' WHERE uid='$submitter'" ) ;
  154. // Trigger Notification
  155. if( $status ) {
  156. $notification_handler =& xoops_gethandler( 'notification' ) ;
  157. // Global Notification
  158. $notification_handler->triggerEvent( 'global' , 0 , 'new_photo' , array( 'PHOTO_TITLE' => $title , 'PHOTO_URI' => "$mod_url/photo.php?lid=$newid&cid=$cid" ) ) ;
  159. // Category Notification
  160. $rs = $xoopsDB->query( "SELECT title FROM $table_cat WHERE cid=$cid" ) ;
  161. list( $cat_title ) = $xoopsDB->fetchRow( $rs ) ;
  162. $notification_handler->triggerEvent( 'category' , $cid , 'new_photo' , array( 'PHOTO_TITLE' => $title , 'CATEGORY_TITLE' => $cat_title , 'PHOTO_URI' => "$mod_url/photo.php?lid=$newid&cid=$cid" ) ) ;
  163. }
  164. // Clear tempolary files
  165. myalbum_clear_tmp_files( $photos_dir ) ;
  166. $redirect_uri = "viewcat.php?cid=$cid&amp;orderby=dateD" ;
  167. if( $caller == 'imagemanager' ) $redirect_uri = 'close.php' ;
  168. redirect_header( $redirect_uri , 2 , _ALBM_RECEIVED ) ;
  169. exit ;
  170. }
  171. // Editing Display
  172. if( $caller == 'imagemanager' ) {
  173. echo "<html><head>
  174. <link rel='stylesheet' type='text/css' media='all' href='".XOOPS_URL."/xoops.css' />
  175. <link rel='stylesheet' type='text/css' media='all' href='".XOOPS_URL."/modules/system/style.css' />
  176. <meta http-equiv='content-type' content='text/html; charset='"._CHARSET."' />
  177. <meta http-equiv='content-language' content='"._LANGCODE."' />
  178. </head><body>\n" ;
  179. } else {
  180. include( XOOPS_ROOT_PATH . "/header.php" ) ;
  181. OpenTable() ;
  182. myalbum_header() ;
  183. }
  184. include_once( "../../class/xoopsformloader.php" ) ;
  185. include_once( "../../include/xoopscodes.php" ) ;
  186. // Preview
  187. if( $caller != 'imagemanager' && ! empty( $_POST['preview'] ) ) {
  188. $photo['description'] = $myts->stripSlashesGPC( $_POST["desc_text"] ) ;
  189. $photo['title'] = $myts->stripSlashesGPC( $_POST["title"] ) ;
  190. $photo['cid'] = empty( $_POST['cid'] ) ? 0 : intval( $_POST['cid'] ) ;
  191. $field = $_POST['xoops_upload_file'][0] ;
  192. if( is_readable( $_FILES[$field]['tmp_name'] ) ) {
  193. // new preview
  194. if( $myalbum_canresize ) $uploader = new MyXoopsMediaUploader( $photos_dir , $array_allowed_mimetypes , $myalbum_fsize , null , null , $array_allowed_exts ) ;
  195. else $uploader = new MyXoopsMediaUploader( $photos_dir , $array_allowed_mimetypes , $myalbum_fsize , $myalbum_width , $myalbum_height , $array_allowed_exts ) ;
  196. $uploader->setPrefix( 'tmp_' ) ;
  197. if( $uploader->fetchMedia( $field ) && $uploader->upload() ) {
  198. $tmp_name = $uploader->getSavedFileName() ;
  199. $preview_name = str_replace( 'tmp_' , 'tmp_prev_' , $tmp_name ) ;
  200. myalbum_modify_photo( "$photos_dir/$tmp_name" , "$photos_dir/$preview_name" ) ;
  201. list( $imgsrc , $width_spec , $ahref ) = myalbum_get_img_attribs_for_preview( $preview_name ) ;
  202. } else {
  203. @unlink( $uploader->getSavedDestination() ) ;
  204. $imgsrc = "$mod_url/images/pixel_trans.gif" ;
  205. $width_spec = "width='$myalbum_thumbsize' height='$myalbum_thumbsize'" ;
  206. $ahref = '' ;
  207. }
  208. } else if( $preview_name != '' && is_readable( "$photos_dir/$preview_name" ) ) {
  209. // old preview
  210. list( $imgsrc , $width_spec , $ahref ) = myalbum_get_img_attribs_for_preview( $preview_name ) ;
  211. } else {
  212. // preview without image
  213. $imgsrc = "$mod_url/images/pixel_trans.gif" ;
  214. $width_spec = "width='$myalbum_thumbsize' height='$myalbum_thumbsize'" ;
  215. $ahref = '' ;
  216. }
  217. // Display Preview
  218. $photo_for_tpl = array(
  219. 'description' => $myts->displayTarea( $photo['description'] , 0 , 1 , 1 , 1 , 1 , 1 ) ,
  220. 'title' => $myts->makeTboxData4Show( $photo['title'] ) ,
  221. 'width_spec' => $width_spec ,
  222. 'submitter' => $my_uid ,
  223. 'submitter_name' => myalbum_get_name_from_uid( $my_uid ) ,
  224. 'imgsrc_thumb' => $imgsrc ,
  225. 'ahref_photo' => $ahref
  226. ) ;
  227. $tpl = new XoopsTpl() ;
  228. include( 'include/assign_globals.php' ) ;
  229. $tpl->assign( $myalbum_assign_globals ) ;
  230. $tpl->assign( 'photo' , $photo_for_tpl ) ;
  231. echo "<table class='outer' style='width:100%;'>" ;
  232. $tpl->display( "db:{$mydirname}_photo_in_list.html" ) ;
  233. echo "</table>\n" ;
  234. } else {
  235. $photo = array(
  236. 'cid' => ( empty( $_GET['cid'] ) ? 0 : intval( $_GET['cid'] ) ) ,
  237. 'description' => '' ,
  238. 'title' => ''
  239. ) ;
  240. }
  241. // Show the form
  242. $form = new XoopsThemeForm( _ALBM_PHOTOUPLOAD , "uploadphoto" , "submit.php?caller=$caller" ) ;
  243. $pixels_text = "$myalbum_width x $myalbum_height" ;
  244. if( $myalbum_canresize ) $pixels_text .= " (auto resize)" ;
  245. $pixels_label = new XoopsFormLabel( _ALBM_MAXPIXEL , $pixels_text ) ;
  246. $size_label = new XoopsFormLabel( _ALBM_MAXSIZE , $myalbum_fsize . ( empty( $file_uploads_off ) ? "" : ' &nbsp; <b>"file_uploads" off</b>' ) ) ;
  247. $form->setExtra( "enctype='multipart/form-data'" ) ;
  248. $title_text = new XoopsFormText( _ALBM_PHOTOTITLE , "title" , 50 , 255 , $myts->makeTboxData4Edit( $photo['title'] ) ) ;
  249. $cat_select = new XoopsFormSelect( _ALBM_PHOTOCAT , "cid" , $photo['cid'] ) ;
  250. $cat_select->addOption( '' , '----' ) ;
  251. $tree = $cattree->getChildTreeArray( 0 , "title" ) ;
  252. foreach( $tree as $leaf ) {
  253. $leaf['prefix'] = substr( $leaf['prefix'] , 0 , -1 ) ;
  254. $leaf['prefix'] = str_replace( "." , "--" , $leaf['prefix'] ) ;
  255. $cat_select->addOption( $leaf['cid'] , $leaf['prefix'] . $leaf['title'] ) ;
  256. }
  257. /* $cat_link = new XoopsFormLabel( "<a href='javascript:location.href=\"viewcat.php?cid=\"+document.uploadphoto.cid.value;'>"._GO."</a>" ) ;
  258. $cat_tray = new XoopsFormElementTray( _ALBM_PHOTOCAT , '&nbsp;' ) ;
  259. $cat_tray->addElement( $cat_select ) ;
  260. $cat_tray->addElement( $cat_link ) ; */
  261. $desc_tarea = new XoopsFormDhtmlTextArea( _ALBM_PHOTODESC , "desc_text" , $myts->makeTareaData4Edit( $photo['description'] ) , 10 , 50 ) ;
  262. $file_form = new XoopsFormFile( _ALBM_SELECTFILE , "photofile" , $myalbum_fsize ) ;
  263. $file_form->setExtra( "size='70'" ) ;
  264. if( $myalbum_canrotate ) {
  265. $rotate_radio = new XoopsFormRadio( _ALBM_RADIO_ROTATETITLE , 'rotate' , 'rot0' ) ;
  266. $rotate_radio->addOption( 'rot0' , _ALBM_RADIO_ROTATE0." &nbsp; " ) ;
  267. $rotate_radio->addOption( 'rot90' , "<img src='images/icon_rotate90.gif' alt='"._ALBM_RADIO_ROTATE90."' title='"._ALBM_RADIO_ROTATE90."' /> &nbsp; " ) ;
  268. $rotate_radio->addOption( 'rot180' , "<img src='images/icon_rotate180.gif' alt='"._ALBM_RADIO_ROTATE180."' title='"._ALBM_RADIO_ROTATE180."' /> &nbsp; " ) ;
  269. $rotate_radio->addOption( 'rot270' , "<img src='images/icon_rotate270.gif' alt='"._ALBM_RADIO_ROTATE270."' title='"._ALBM_RADIO_ROTATE270."' /> &nbsp; " ) ;
  270. }
  271. $op_hidden = new XoopsFormHidden( "op" , "submit" ) ;
  272. $counter_hidden = new XoopsFormHidden( "fieldCounter" , 1 ) ;
  273. $preview_hidden = new XoopsFormHidden( "preview_name" , htmlspecialchars( $preview_name ) , ENT_QUOTES ) ;
  274. $submit_button = new XoopsFormButton( "" , "submit" , _SUBMIT , "submit" ) ;
  275. $preview_button = new XoopsFormButton( "" , "preview" , _PREVIEW , "submit" ) ;
  276. $reset_button = new XoopsFormButton( "" , "reset" , _CANCEL , "reset" ) ;
  277. $submit_tray = new XoopsFormElementTray( '' ) ;
  278. if( $caller != 'imagemanager' ) $submit_tray->addElement( $preview_button ) ;
  279. $submit_tray->addElement( $submit_button ) ;
  280. $submit_tray->addElement( $reset_button ) ;
  281. $form->addElement( $pixels_label ) ;
  282. $form->addElement( $size_label ) ;
  283. $form->addElement( $title_text ) ;
  284. $form->addElement( $desc_tarea ) ;
  285. $form->addElement( $cat_select ) ;
  286. $form->setRequired( $cat_select ) ;
  287. $form->addElement( $file_form ) ;
  288. if( $myalbum_canrotate ) $form->addElement( $rotate_radio ) ;
  289. $form->addElement( $preview_hidden ) ;
  290. $form->addElement( $counter_hidden ) ;
  291. $form->addElement( $op_hidden ) ;
  292. $form->addElement( $submit_tray ) ;
  293. // $form->setRequired( $file_form ) ;
  294. $form->display() ;
  295. if( $caller == 'imagemanager' ) {
  296. echo "</body></html>" ;
  297. } else {
  298. CloseTable() ;
  299. myalbum_footer() ;
  300. include( XOOPS_ROOT_PATH . "/footer.php" ) ;
  301. }
  302. ?>