diff --git a/upload/includes/classes/photos.class.php b/upload/includes/classes/photos.class.php new file mode 100644 index 00000000..e810e395 --- /dev/null +++ b/upload/includes/classes/photos.class.php @@ -0,0 +1,1912 @@ +exts = array('jpg','png','gif','jpeg'); // This should be added from Admin Area. may be some people also want to allow BMPs; + $this->embed_types = array("html","forum","email","direct"); + $this->init_photos(); + } + + /** + * Setting up Photos Section + */ + function init_photos() + { + $this->init_actions(); + $this->init_collections(); + $this->photos_admin_menu(); + } + + /** + * Initiating Actions for Photos + */ + function init_actions() + { + $this->action = new cbactions(); + $this->action->init(); // Setting up reporting excuses + $this->action->type = 'p'; + $this->action->name = 'photo'; + $this->action->obj_class = 'cbphoto'; + $this->action->check_func = 'photo_exists'; + $this->action->type_tbl = "photos"; + $this->action->type_id_field = 'photo_id'; + + + } + + /** + * Setting Email Settings + */ + function set_share_email($data) + { + $this->share_email_vars = array( + '{photo_title}' => $data['photo_title'], + '{photo_description}' => $data['photo_description'], + '{photo_link}' => $this->collection->collection_links($data,'view_item'), + '{photo_thumb}' => $this->get_image_file($data['photo_id'],'m') + ); + $this->action->share_template_name = 'photo_share_template'; + $this->action->val_array = $this->share_email_vars; + } + + + + /** + * Initiating Collections for Photos + */ + function init_collections() + { + $this->collection = new Collections; + $this->collection->objType = "p"; + $this->collection->objClass = "cbphoto"; + $this->collection->objTable = "photos"; + $this->collection->objName = "Photo"; + $this->collection->objFunction = "photo_exists"; + $this->collection->objFieldID = "photo_id"; + $this->photo_register_function('delete_collection_photos'); + } + + /** + * Create Admin Area menu for photos + */ + function photos_admin_menu() + { + global $Cbucket,$cbcollection; + $am = $Cbucket->AdminMenu; + $am['Photos'] = array( + 'Photo Manager' => 'photo_manager.php', + 'Flagged Photos' => 'flagged_photos.php', + 'Photo Settings' => 'photo_settings.php', + 'Watermark Settings' => 'photo_settings.php?mode=watermark_settings', + 'Recreate Thumbs' => 'recreate_thumbs.php?mode=mass' + ); + $Cbucket->AdminMenu = $am; + } + + /** + * Check if photo exists or not + */ + function photo_exists($id) + { + global $db; + $result = $db->select(tbl($this->p_tbl),"photo_id"," photo_id = '$id'"); + if($result) + return true; + else + return false; + } + + /** + * Register function + */ + function photo_register_function($func) + { + global $cbcollection; + $cbcollection->collection_delete_functions[] = 'delete_collection_photos'; + } + + /** + * Get Photo + */ + function get_photo($pid) + { + global $db; + if(is_numeric($pid)) + $result = $db->select(tbl($this->p_tbl),"*"," photo_id = '$pid'"); + else + $result = $db->select(tbl($this->p_tbl),"*"," photo_key = '$pid'"); + + if($db->num_rows > 0) + return $result[0]; + else + return false; + } + + /** + * Get Photos + */ + function get_photos($p) + { + global $db; + $tables = "photos,users"; + + $order = $p['order']; + $limit = $p['limit']; + $cond = ""; + + if(!has_access('admin_access',TRUE)) + { + $cond = " ".tbl('photos.broadcast')." = 'public' AND ".tbl('photos.active')." = 'yes'"; + } else { + if($p['active']) + $cond .= " ".tbl('photos.active')." = '".$p['active']."'"; + + if($p['broadcast']) + { + if($cond != "") + $cond .= " AND "; + $cond .= " ".tbl('photos.broadcast')." = '".$p['broadcast']."'"; + } + } + + if($p['pid']) + { + if($cond != "") + $cond .= " AND "; + $cond .= " ".tbl('photos.photo_id')." = '".$p['pid']."'"; + } + + if($p['key']) + { + if($cond != "") + $cond .= " AND "; + $cond .= " ".tbl('photos.photo_key')." = '".$p['key']."'"; + } + + if($p['filename']) + { + if($cond != "") + $cond .= " AND "; + $cond .= " ".tbl('photos.filename')." = '".$p['filename']."'"; + } + + if($p['extension']) + { + foreach($this->exts as $ext) + { + if(in_array($ext,$this->exts)) + { + if($cond != "") + $cond .= " AND "; + $cond .= " ".tbl('photos.ext')." = '".$p['extension']."'"; + } + } + } + + if($p['date_span']) + { + if($cond != "") + $cond .= " AND "; + $cond .= " ".cbsearch::date_margin("date_added",$p['date_span']); + } + + if($p['featured']) + { + if($cond != "") + $cond .= " AND "; + $cond .= " ".tbl('photos.featured')." = '".$p['featured']."'"; + } + + if($p['user']) + { + if($cond != "") + $cond .= " AND "; + $cond .= " ".tbl('photos.userid')." = '".$p['user']."'"; + } + + if($p['exclude']) + { + if($cond != "") + $cond .= " AND "; + $cond .= $this->exclude_query($p['exclude']); + } + + $title_tag = ''; + + if($p['title']) + { + $title_tag = " ".tbl('photos.photo_title')." LIKE '%".$p['title']."%'"; + } + + if($p['tags']) + { + $tags = explode(",",$p['tags']); + if(count($tags)>0) + { + if($title_tag != '') + $title_tag .= " OR "; + $total = count($tags); + $loop = 1; + foreach($tags as $tag) + { + $title_tag .= " ".tbl('photos.photo_tags')." LIKE '%$tag%'"; + if($loop<$total) + $title_tag .= " OR "; + $loop++; + } + } else { + if($title_tag != '') + $title_tag .= " OR "; + $title_tag .= " ".tbl('photos.photo_tags')." LIKE '%".$p['tags']."%'"; + } + } + + if($title_tag != "") + { + if($cond != '') + $cond .= " AND "; + $cond .= " ($title_tag) "; + } + + if($p['ex_user']) + { + if($cond != "") + $cond .= " AND "; + $cond .= " ".tbl('photos.userid')." <> '".$p['ex_user']."'"; + } + + if($p['extra_cond']) + { + if($cond != "") + $cond .= " AND "; + $cond .= $p['extra_cond']; + } + + if($p['collection']) + { + if($cond != "") + $cond .= " AND "; + $cond .= " ".tbl('photos.collection_id')." = '".$p['collection']."'"; + } + + if(!$p['count_only'] && !$p['show_related']) + { + if($cond != "") + $cond .= " AND "; + $result = $db->select(tbl($tables), + tbl("photos.*,users.userid,users.username"), + $cond.tbl("photos.userid")." = ".tbl("users.userid"),$limit,$order); + //echo $db->db_query; + } + + if($p['show_related']) + { + $cond = "MATCH(".tbl('photos.photo_title,photos.photo_tags').")"; + $cond .= " AGAINST ('".cbsearch::set_the_key($p['title'])."' IN BOOLEAN MODE)"; + if($p['exclude']) + { + if($cond != "") + $cond .= " AND "; + $cond .= $this->exclude_query($p['exclude']); + } + + if($p['extra_cond']) + { + if($cond != "") + $cond .= " AND "; + $cond .= $p['extra_cond']; + } + $result = $db->select(tbl($tables),tbl("photos.*,users.userid,users.username"), + $cond." AND ".tbl("photos.userid")." = ".tbl("users.userid"),$limit,$order); + //echo $db->db_query; + + // We found thing from TITLE of Photos, let's try TAGS + if($db->num_rows == 0) + { + $tags = cbsearch::set_the_key($p['tags']); + $tags = str_replace('+','',$tags); + + $cond = "MATCH(".tbl('photos.photo_title,photos.photo_tags').")"; + $cond .= " AGAINST ('".$tags."' IN BOOLEAN MODE)"; + + if($p['exclude']) + { + if($cond != "") + $cond .= " AND "; + $cond .= $this->exclude_query($p['exclude']); + } + + if($p['extra_cond']) + { + if($cond != "") + $cond .= " AND "; + $cond .= $p['extra_cond']; + } + $result = $db->select(tbl($tables),tbl("photos.*,users.userid,users.username"), + $cond." AND ".tbl("photos.userid")." = ".tbl("users.userid"),$limit,$order); + //echo $db->db_query; + } + } + + if($p['count_only']) + { + if($p['extra_cond']) + { + if($cond != "") + $cond .= " AND "; + $cond .= $p['extra_cond']; + } + $result = $db->count(tbl("photos"),"photo_id",$cond); + } + + if($p['assign']) + assign($p['assign'],$result); + else + return $result; + } + + + /** + * Used to construct Exclude Query + */ + function exclude_query($array) + { + $cond = ''; + if(!is_array($array)) + $ids = explode(',',$array); + else + $ids = $array; + + $count = 0; + + $cond .= "( "; + foreach($ids as $id) + { + $count++; + if($count > 1) + $cond .= " AND "; + $cond .= " ".tbl('photos.photo_id')." <> '".$id."'"; + } + $cond .= " )"; + + return $cond; + } + + /** + * Used to generate photo key + * Replica of video_keygen function + */ + function photo_key() + { + global $db; + + $char_list = "ABDGHKMNORSUXWY"; + $char_list .= "123456789"; + while(1) + { + $photo_key = ''; + srand((double)microtime()*1000000); + for($i = 0; $i < 12; $i++) + { + $photo_key .= substr($char_list,(rand()%(strlen($char_list))), 1); + } + + if(!$this->pkey_exists($photo_key)) + break; + } + + return $photo_key; + } + + /** + * Used to check if key exists + */ + function pkey_exists($key) + { + global $db; + $db->select(tbl("photos"),"photo_key"," photo_key = '$key'"); + if($db->num_rows > 0) + return true; + else + return false; + } + + /** + * Used to delete photo + */ + function delete_photo($id) + { + if($this->photo_exists($id)) + { + $photo = $this->get_photo($id); + + //removing from collection + $this->collection->remove_item($photo['photo_id'],$photo['collection_id']); + + //now removing photo files + $this->delete_photo_files($photo); + + //finally removing from Database + $this->delete_from_db($photo); + } else + e(lang("photo_not_exists")); + } + + /** + * Used to delete photo files + */ + function delete_photo_files($id) + { + if(!is_array($id)) + $photo = $this->get_photo($id); + else + $photo = $id; + + $pid = $photo['photo_id']; + $files = $this->get_image_file($pid,'t',TRUE,NULL,FALSE,TRUE); + if(!empty($files)) + { + foreach($files as $file) + { + $file_dir = PHOTOS_DIR."/".$file; + if(file_exists($file_dir)) + unlink($file_dir); + } + + if(file_exists(BASEDIR."/".$files[0])) + { + e(lang("unsuccess_delete_file")); + return false; + } + else + e(sprintf(lang("success_delete_file"),$photo['photo_title']),"m"); + } + } + + /** + * Used to delete photo from database + */ + function delete_from_db($id) + { + global $db; + if(is_array($id)) + $delete_id = $id['photo_id']; + else + $delete_id = $id; + + $db->execute("DELETE FROM ".tbl('photos')." WHERE photo_id = $delete_id"); + e(lang("photo_success_deleted"),"m"); + } + + + /** + * Used to get photo owner + */ + function get_photo_owner($id) + { + return $this->get_photo_field($id,'userid'); + } + + /** + * Used to get photo any field + */ + function get_photo_field($id,$field) + { + global $db; + if(!$field) + return false; + else + { + if(!is_numeric($id)) + $result = $db->select(tbl($this->p_tbl),$field,' photo_key = '.$id.''); + else + $result = $db->select(tbl($this->p_tbl),$field,' photo_id = '.$id.''); + + if($result) + return $result[0][$field]; + else + return false; + } + } + + /** + * Used filter array + */ + function remove_empty_indexes($array) + { + $newArr = array(); + if(is_array($array)) + { + foreach($array as $key=>$arr) + { + if(is_array($arr)) + { + foreach($arr as $a) + { + if(!empty($a)) + { + $newArr[$key][] = $a; + } + } + } + } + + return $newArr; + } else { + echo "No Array Provided"; + } + } + + /** + * Used to crop the image + * Image will be crop to dead-center + */ + function crop_image($input,$output,$ext,$width,$height) + { + $info = getimagesize($input); + $Swidth = $info[0]; + $Sheight = $info[1]; + + $canvas = imagecreatetruecolor($width, $height); + $left_padding = $Swidth / 2 - $width / 2; + $top_padding = $Sheight / 2 - $height / 2; + + switch($ext) + { + case "jpeg": + case "jpg": + case "JPG": + case "JPEG": + { + $image = imagecreatefromjpeg($input); + imagecopy($canvas, $image, 0, 0, $left_padding, $top_padding, $width, $height); + imagejpeg($canvas,$output,90); + } + break; + + case "png": + case "PNG": + { + $image = imagecreatefrompng($input); + imagecopy($canvas, $image, 0, 0, $left_padding, $top_padding, $width, $height); + imagepng($canvas,$output,9); + } + break; + + case "gif": + case "GIF": + { + $image = imagecreatefromgif($input); + imagecopy($canvas, $image, 0, 0, $left_padding, $top_padding, $width, $height); + imagejpeg($canvas,$output,90); + } + break; + + default: + { + return false; + } + break; + } + imagedestroy($canvas); + } + + /** + * Used to resize and watermark image + **/ + function generate_photos($array) + { + global $db; + $path = PHOTOS_DIR."/"; + + if(!is_array($array)) + $p = $this->get_photo($array); + else + $p = $array; + + $filename = $p['filename']; + $extension = $p['ext']; + + $this->createThumb($path.$filename.".".$extension,$path.$filename."_o.".$extension,$extension); + $this->createThumb($path.$filename.".".$extension,$path.$filename."_t.".$extension,$extension,$this->thumb_width,$this->thumb_height); + $this->createThumb($path.$filename.".".$extension,$path.$filename."_m.".$extension,$extension,$this->mid_width,$this->mid_height); + $this->createThumb($path.$filename.".".$extension,$path.$filename."_l.".$extension,$extension,$this->lar_width); + + $should_watermark = config('watermark_photo'); + + if(!empty($should_watermark) && $should_watermark == 1) + { + $this->watermark_image($path.$filename."_l.".$extension,$path.$filename."_l.".$extension); + $this->watermark_image($path.$filename.".".$extension,$path.$filename."_o.".$extension); + } + } + + /** + * Creating resized photo + */ + function createThumb($from,$to,$ext,$d_width=NULL,$d_height=NULL,$force_copy=false) + { + $file = $from; + $info = getimagesize($file); + $org_width = $info[0]; + $org_height = $info[1]; + + if($org_width > $d_width && !empty($d_width)) + { + $ratio = $org_width / $d_width; // We will resize it according to Width + + $width = $org_width / $ratio; + $height = $org_height / $ratio; + + $image_r = imagecreatetruecolor($width, $height); + if(!empty($d_height) && $height > $d_height && $this->cropping == 1) + { + $crop_image = TRUE; + } + + switch($ext) + { + case "jpeg": + case "jpg": + case "JPG": + case "JPEG": + { + $image = imagecreatefromjpeg($file); + imagecopyresampled($image_r, $image, 0, 0, 0, 0, $width, $height, $org_width, $org_height); + imagejpeg($image_r, $to, 90); + + if(!empty($crop_image)) + $this->crop_image($to,$to,$ext,$width,$d_height); + } + break; + + case "png": + case "PNG": + { + $image = imagecreatefrompng($file); + imagecopyresampled($image_r, $image, 0, 0, 0, 0, $width, $height, $org_width, $org_height); + imagepng($image_r,$to,9); + if(!empty($crop_image)) + $this->crop_image($to,$to,$ext,$width,$d_height); + } + break; + + case "gif": + case "GIF": + { + $image = imagecreatefromgif($file); + imagecopyresampled($image_r, $file, 0, 0, 0, 0, $width, $height, $org_width, $org_height); + imagejpeg($image_r,$to,90); + if(!empty($crop_image)) + $this->crop_image($to,$to,$ext,$width,$d_height); + } + break; + } + imagedestroy($image_r); + } else { + if(!file_exists($to) || $force_copy === true) + copy($from,$to); + } + } + + /** + * Used to get watermark file + */ + function watermark_file() + { + if(file_exists(BASEDIR."/images/photo_watermark.png")) + return BASEURL."/images/photo_watermark.png"; + else + return false; + } + + /** + * Used to set watermark position + */ + function position_watermark($file,$watermark) + { + if(empty($this->position)) + $info = array('left','top'); + else + $info = explode(":",$this->position); + + $x = $info[0]; + $y = $info[1]; + list($w,$h) = getimagesize($file); + list($ww,$wh) = getimagesize($watermark); + $padding = $this->padding; + + switch($x) + { + case "center": + { + $finalxPadding = $w / 2 - $ww / 2; + } + break; + + case "left": + default: + { + $finalxPadding = $padding; + } + break; + + case "right": + { + $finalxPadding = $w - $ww - $padding; + } + break; + } + + switch($y) + { + case "top": + default: + { + $finalyPadding = $padding; + } + break; + + case "center": + { + $finalyPadding = $h / 2 - $wh / 2; + } + break; + + case "bottom": + { + $finalyPadding = $h - $wh - $padding; + } + break; + } + + $values = array($finalxPadding,$finalyPadding); + return $values; + } + + /** + * Used to watermark image + */ + function watermark_image($input,$output) + { + $watermark_file = $this->watermark_file(); + if(!$watermark_file) + return false; + else + { + list($Swidth, $Sheight, $Stype) = getimagesize($input); + $wImage = imagecreatefrompng($watermark_file); + $ww = imagesx($wImage); + $wh = imagesy($wImage); + $paddings = $this->position_watermark($input,$watermark_file); + + switch($Stype) + { + case 1: //GIF + { + $sImage = imagecreatefromgif($input); + imagecopy($sImage,$wImage,$paddings[0],$paddings[1],0,0,$ww,$wh); + imagejpeg($sImage,$output,90); + } + break; + + case 2: //JPEG + { + $sImage = imagecreatefromjpeg($input); + imagecopy($sImage,$wImage,$paddings[0],$paddings[1],0,0,$ww,$wh); + imagejpeg($sImage,$output,90); + } + break; + + case 3: //PNG + { + $sImage = imagecreatefrompng($input); + imagecopy($sImage,$wImage,$paddings[0],$paddings[1],0,0,$ww,$wh,75); + imagepng($sImage,$output,9); + } + break; + } + } + } + + /** + * Load Required Form + */ + function load_required_forms($array=NULL) + { + + if($array == NULL) + $array = $_POST; + + $title = $array['photo_title']; + $description = $array['photo_description']; + $tags = $array['photo_tags']; + + if($array['without_user']) + $p['user'] = ""; + else + $p['user'] = userid(); + + $p['type'] = "photos"; + $collections = $this->collection->get_collections($p); + $cl_array = $this->parse_array($collections); + $collection = $array['collection_id']; + $this->unique = rand(0,9999); + $fields = + array + ( + 'name' => array( + 'title' => lang('photo_title'), + 'id' => 'photo_title', + 'name' => 'photo_title_'.$this->unique.'', + 'type' => 'textfield', + 'value' => $title, + 'db_field' => 'photo_title', + 'required' => 'yes', + 'invalid_err' => lang('photo_title_err') + ), + + 'desc' => array( + 'title' => lang('photo_caption'), + 'id' => 'photo_description', + 'name' => 'photo_description_'.$this->unique.'', + 'type' => 'textarea', + 'value' => $description, + 'db_field' => 'photo_description', + 'anchor_before' => 'before_desc_compose_box', + 'required' => 'yes', + 'invalid_err' => lang('photo_caption_err') + ), + 'tags' => array( + 'title' => lang('photo_tags'), + 'id' => 'photo_tags', + 'name' => 'photo_tags_'.$this->unique.'', + 'type' => 'textfield', + 'value' => $tags, + 'db_field' => 'photo_tags', + 'required' => 'yes', + 'invalid_err' => lang('photo_tags_err') + ), + 'collection' => array( + 'title' => lang('collection'), + 'id' => 'collection_id', + 'name' => 'collection_id_'.$this->unique.'', + 'type' => 'dropdown', + 'value' => $cl_array, + 'db_field' => 'collection_id', + 'required' => 'yes', + 'checked' => $collection, + 'invalid_err' => lang('photo_collection_err') + ) + ); + + return $fields; + } + + /** + * Update watermark file + */ + function update_watermark($file) + { + if(empty($file)) + e("Watermark file not found"); + else + { + $oldW = BASEDIR."/images/photo_watermark.png"; + if(file_exists($oldW)) + unset($oldW); + + $info = getimagesize($file['tmp_name']); + $width = $info[0]; + $type = $info[2]; + + //pr($info,TRUE); + + if($type == 3) + { + if(move_uploaded_file($file['tmp_name'],BASEDIR."/images/photo_watermark.png")) + { + $wFile = BASEDIR."/images/photo_watermark.png"; + if($width > $this->max_watermark_width) + $this->createThumb($wFile,$wFile,'png',$this->max_watermark_width); + } + e("Watermark Image Updated.","m"); + } else { + e("Please upload 24-bit PNG"); + } + + + } + } + + /** + * Load Other Form + */ + function load_other_forms($array=NULL) + { + if($array==NULL) + $array = $_POST; + + $comments = $array['allow_comments']; + $broadcast = $array['broadcast']; + $tagging = $array['allow_tagging']; + $embedding = $array['allow_embedding']; + $rating = $array['allow_rating']; + if(!empty($this->unique)) + $rand = $this->unique; + else + $rand = rand(0,9999); + + $Otherfields = array + ( + 'comments' => array( + 'title' => lang('comments'), + 'name' => 'allow_comments_'.$rand.'', + 'id' => 'allow_comments', + 'db_field' => 'allow_comments', + 'type' => 'radiobutton', + 'value' => array('yes' => lang('vdo_allow_comm'),'no' => lang('vdo_dallow_comm')), + 'required' => 'no', + 'checked' => $comments, + 'validate_function'=>'yes_or_no', + 'display_function' => 'display_sharing_opt', + 'default_value'=>'yes' + ), + /*'broadcast' => array( + 'title' => lang("vdo_br_opt"), + 'type' => 'radiobutton', + 'name' => 'broadcast_'.$array['photo_id'].'', + 'id' => 'broadcast', + 'value' => array("public"=>lang("collect_borad_pub"),"private"=>lang("collect_broad_pri")), + 'checked' => $broadcast, + 'db_field' => 'broadcast', + 'required' => 'no', + 'display_function' => 'display_sharing_opt', + 'default_value'=>'public' + ),*/ + 'tagging' => array( + 'title' => lang('tagging'), + 'type' => 'radiobutton', + 'id' => 'allow_tagging', + 'name' => 'allow_tagging_'.$rand.'', + 'db_field' => 'allow_tagging', + 'type' => 'radiobutton', + 'value' => array('yes' => lang('pic_allow_tagging'),'no' => lang('pic_dallow_tagging')), + 'checked' => $tagging, + 'validate_function'=>'yes_or_no', + 'display_function' => 'display_sharing_opt', + 'default_value'=>'yes' + ), + 'embedding' => array( + 'title' => lang('vdo_embedding'), + 'type' => 'radiobutton', + 'name' => 'allow_embedding_'.$rand.'', + 'id' => 'allow_embedding', + 'db_field' => 'allow_embedding', + 'value' => array('yes' => lang('pic_allow_embed'),'no' => lang('pic_dallow_embed')), + 'checked' => $embedding, + 'validate_function'=>'yes_or_no', + 'display_function' => 'display_sharing_opt', + 'default_value'=>'yes' + ), + 'rating' => array( + 'title' => lang('rating'), + 'id' => 'allow_rating', + 'name' => 'allow_rating_'.$rand.'', + 'type' => 'radiobutton', + 'db_field' => 'allow_rating', + 'value' => array('yes' => lang('pic_allow_rating'),'no' => lang('pic_dallow_rating')), + 'checked' => $rating, + 'validate_function'=>'yes_or_no', + 'display_function' => 'display_sharing_opt', + 'default_value'=>'yes' + ) + ); + //pr($Otherfields,TRUE); + return $Otherfields; + } + + + /** + * This will return a formatted array + * return @Array + * Array Format: Multidemsional + * Array ( [photo_id] => array( ['field_name'] => 'value' ) ) + */ + function return_formatted_post($arr) + { + $photoID = ''; + foreach($_POST as $key => $value) + { + $parts = explode('_',$key); + $total = count($parts); + $id = $parts[$total-1]; + $name = array_splice($parts,0,$total-1); + $name = implode("_",$name); + + if($photoID != $id){ + $values = array(); + $photoID = $id; + } + + if(is_numeric($id)) + { + if (strpos($key, $id) !== FALSE) + { + $values[$name] = $value; + $PhotosArray[$id] = $values; + } + } + } + + return $PhotosArray; + } + + /** + * This will be used to mutliple photos + * at once. + * Single update will be different. + */ + function update_multiple_photos($arr) + { + global $db,$cbcollection,$eh; + + foreach($arr as $id => $details) + { + if(is_array($details)) + { + $i = 0; + $query = "UPDATE ".tbl('photos')." SET "; + foreach($details as $key => $value) + { + $i++; + $query .= "$key = '$value'"; + if($iExecute($query); + $cbcollection->add_collection_item($id,$details['collection_id']); + + } + } + $eh->flush(); + } + + /** + * Used to parse collections dropdown + */ + function parse_array($array) + { + if(is_array($array)) + { + foreach($array as $key=>$v) + { + $cl_arr[$v['collection_id']] = $v['collection_name']; + } + return $cl_arr; + } else { + return false; + } + } + + /** + * Used to create filename of photo + */ + function create_filename() + { + $filename = time().RandomString(6); + return $filename; + } + + /** + * Construct extensions for SWF + */ + function extensions() + { + $exts = $this->exts; + $list = ''; + foreach($exts as $ext) + { + $list .= "*.".$ext.";"; + } + return $list; + + } + + /** + * Function used to validate form fields + */ + function validate_form_fields($array=NULL) + { + $reqFileds = $this->load_required_forms($array); + + if($array==NULL) + $array = $_POST; + + if(is_array($_FILES)) + $array = array_merge($array,$_FILES); + + + $otherFields = $this->load_other_forms($array); + + $photo_fields = array_merge($reqFileds,$otherFields); + + validate_cb_form($photo_fields,$array); + } + + /** + * Update Photo + */ + function update_photo($array=NULL) + { + global $db; + + if($array == NULL) + $array = $_POST; + $this->validate_form_fields($array); + $pid = $array['photo_id']; + $cid = $this->get_photo_field($pid,'collection_id'); + + if(!error()) + { + $reqFields = $this->load_required_forms($array); + $otherFields = $this->load_other_forms($array); + + $fields = array_merge($reqFields,$otherFields); + + foreach($fields as $field) + { + $name = formObj::rmBrackets($field['name']); + $val = $array[$name]; + + if($field['use_func_val']) + $val = $field['validate_function']($val); + + + if(!empty($field['db_field'])) + $query_field[] = $field['db_field']; + + if(is_array($val)) + { + $new_val = ''; + foreach($val as $v) + { + $new_val .= "#".$v."# "; + } + $val = $new_val; + } + if(!$field['clean_func'] || (!function_exists($field['clean_func']) && !is_array($field['clean_func']))) + $val = mysql_clean($val); + else + $val = apply_func($field['clean_func'],sql_free('|no_mc|'.$val)); + + if(!empty($field['db_field'])) + $query_val[] = $val; + } + + if(has_access('admin_access',TRUE)) + { + if(isset($array['views'])) + { + $query_field[] = 'views'; + $query_val[] = $array['views']; + } + + if(isset($array['total_comments'])) + { + $query_field[] = "total_comments"; + $query_val[] = $array['total_comments']; + } + + if(isset($array['total_favorites'])) + { + $query_field[] = "total_favorites"; + $query_val[] = $array['total_favorites']; + } + + if(isset($array['voters'])) + { + $query_field[] = "voters"; + $query_val[] = $array['voters']; + } + } + + if(!error()) + { + if(!userid()) + e(lang("you_not_logged_in")); + elseif(!$this->photo_exists($pid)) + e(lang("photo_not_exists")); + elseif($this->get_photo_owner($pid) != userid() && !has_access('admin_access',TRUE)) + e(lang("cant_edit_photo")); + else + { + if($cid != $array['collection_id_'.$pid.'']) + { + $this->collection->change_collection($array['collection_id_'.$pid.''],$pid,$cid); + } + $db->update(tbl('photos'),$query_field,$query_val," photo_id='$pid'"); + + e("photo_updated_successfully","m"); + } + } + } + + + } + + /** + * Used to get image type + * t = Thumb + * m = Medium + * l = Large + */ + function get_image_type($name) + { + if(empty($name)) + return false; + else + { + $parts = explode("_",$name); + if(is_array($parts)) + { + if(!empty($parts[1])) + return substr($parts[1],0,1); + } + } + } + + /** + * Used to get image file + */ + function get_image_file($pid,$size='t',$multi=false,$assign=NULL,$with_path=true,$with_orig=false) + { + + if(empty($pid)) + return false; + else + { + $size = $size ? $size : 't'; + if(!is_array($pid)) + $photo = $this->get_photo($pid); + else + $photo = $pid; + + if(!empty($photo)) + { + if(empty($photo['photo_id']) && empty($photo['photo_key'])) + return false; // Something is wrong, so return false; + else + { + if(!empty($photo['filename']) && !empty($photo['ext'])) + { + $files = glob(PHOTOS_DIR."/".$photo['filename']."*.".$photo['ext']); + + if(is_array($files)) + { + foreach($files as $file) + { + $file_parts = explode('/',$file); + $thumb_name = $file_parts[count($file_parts)-1]; + $type = $this->get_image_type($thumb_name); + if($with_orig) + { + if($with_path) + $thumbs[] = PHOTOS_URL."/".$thumb_name; + else + $thumbs[] = $thumb_name; + } + elseif(!empty($type)) + { + if($with_path) + $thumbs[] = PHOTOS_URL."/".$thumb_name; + else + $thumbs[] = $thumb_name; + } + } + //pr($thumbs,TRUE); + if($assign && $multi) + { + assign($assign,$thumbs); + } elseif(!$assign && $multi) { + return $thumbs; + } else { + if($size == "l" || $size == "m" || $size == "o" || $size == "t") + { + $size = "_".$size; + + $return_thumb = array_find($photo['filename'].$size,$thumbs); + if($assign != NULL) + assign($assign,$return_thumb); + else + return $return_thumb; + } + } + + } else { + return false; + } + } else { + return false; + } + } + } else + return false; + } + } + + /** + * This will become a Smarty function. + * I am writting this to eliminate the possiblitles + * of distort pictures + */ + function getFileSmarty($p) + { + + $details = $p['details']; + if(empty($details)) + { + return $this->default_thumb($size); + } else { + $size = $p['size'] ? $p['size'] : 't'; + $with_path = $p['with_path'] ? $p['with_path'] : TRUE; + + if(!is_array($details)) + $photo = $this->get_photo($details); + else + $photo = $details; + + if(empty($photo['photo_id']) || empty($photo['photo_key'])) + return $this->default_thumb($size); + else + { + if(!empty($photo['filename']) && !empty($photo['ext'])) + { + $files = glob(PHOTOS_DIR."/".$photo['filename']."*.".$photo['ext']); + if(!empty($files) && is_array($files)) + { + foreach($files as $file) + { + $file_parts = explode("/",$file); + $thumb_name = $file_parts[count($file_parts)-1]; + if($with_path) + $thumbs[] = PHOTOS_URL."/".$thumb_name; + else + $thumbs[] = $thumb_name; + } + + if(empty($p['output']) || $p['output'] == 'non_html') + { + if($p['assign'] && $p['multi']) + { + assign($p['assign'],$thumbs); + } elseif(!$p['assign'] && $p['multi']) { + return $thumbs; + } else { + $size = "_".$size; + + $return_thumb = array_find($photo['filename'].$size,$thumbs); + if($p['assign'] != NULL) + assign($p['assign'],$return_thumb); + else + echo $return_thumb; + } + } + + if($p['output'] == 'html') + { + $size = "_".$size; + + $src = array_find($photo['filename'].$size,$thumbs); + $dem = getimagesize($src); + + $img = "$p['anchor'],"data"=>$photo); + ANCHOR($anchor_p); + } + + if($p['style']) + $img .= " style = '".$p['style']."'"; + + if($p['extra']) + $img .= mysql_clean($p['extra']); + + $img .= " />"; + echo $img; + } + } else { + return $this->default_thumb($size); + } + } + } + } + } + + /** + * Will be called when collection is being deleted + * This will make photos in the collection orphan + * User will be able to access them in orphan photos + */ + function make_photo_orphan($details,$pid=NULL) + { + global $db; + if(!is_array($details) && is_numeric($details)) + { + $c = $this->collection->get_collection($details); + $cid = $c['collection_id']; + } + else + $cid = $details['collection_id']; + if(!empty($pid)) + $cond = " AND photo_id = $pid"; + + $db->update(tbl('photos'),array('collection_id'),array('0')," collection_id = $cid $cond"); + } + + /** + * Used to load upload more photos + * This button will only appear if collection type is photos + * and user logged-in is Collection Owner + */ + function upload_photo_button($arr) + { + $cid = $arr['details']; + //pr($arr,TRUE); + if($arr['text']) + $text = $arr['text']; + else + $text = "Upload More"; + + if(!is_array($cid)) + $details = $this->collection->get_collection($cid); + else + $details = $cid; + + if($details['type'] == 'photos' && $details['userid'] == user_id()) + { + $div = "
photo_links($details,'upload_more')."'"; + $div .= 'onClick = "openURL('.$link.')"'; + $div .= ">"; + $div .= $text; + $div .= "
"; + } else { + $div .= ">"; + $div .= "collection->get_collection_field($details['collection_id'],'type'); + + switch($item_type) + { + case "videos": + case "v": + { + if(SEO == "yes") + return BASEURL."/item/".$item_type."/".$details['collection_id']."/".$details['videokey']; + else + return BASEURL."/view_item.php?item=".$details['videokey']."&type=".$item_type."&collection=".$details['collection_id']; + } + break; + + case "photos": + case "p": + { + if(SEO == "yes") + return BASEURL."/item/".$item_type."/".$details['collection_id']."/".$details['photo_key']; + else + return BASEURL."/view_item.php?item=".$details['photo_key']."&type=".$item_type."&collection=".$details['collection_id']; + } + break; + } + } + break; + } + return $link; + } + } + + /** + * Used to return default thumb + */ + function default_thumb($size) + { + if($size == "o") + $size = ''; + else + $size = "_".$size; + + if(file_exists(TEMPLATEDIR."/images/thumbs/collection_thumb-small.png")) + return TEMPLATEDIR."/images/thumbs/collection_thumb-small.png"; + else + return COLLECT_THUMBS_URL."/no_thumb-small.png"; + } + + /** + * Used to check if collection can add + * photos or not + */ + function is_addable($cid) + { + $details = $this->collection->get_collection($cid); + if(empty($details)) + { + return false; + } else { + if(($details['active'] == 'yes' || $details['broadcast'] == 'public') && $details['userid'] == userid()) + return true; + elseif($details['userid'] == userid()) + return true; + else + return false; + } + } + + /** + * Used to display photo voterts details. + * User who rated, how many stars and when user rated + */ + function photo_voters($id,$return_array=FALSE,$show_all=FALSE) + { + global $json; + $p = $this->get_photo($id); + if((!empty($p) && $p['userid'] == userid()) || $show_all === TRUE) + { + global $userquery; + $voters = $p['voters']; + if(phpversion() < "5.2.0") + $voters = $json->json_decode($voters,TRUE); + else + $voters = json_decode($voters,TRUE); + + if(!empty($voters)) + { + if($return_array) + return $voters; + else + { + foreach($voters as $id=>$details) + { + $username = get_username($id); + $output = "
  • "; + $output .= "$username"; + $output .= " rated ". $details['rate']/2 ." stars ("; + $output .= niceTime($details['time']).")"; + $output .= "
  • "; + echo $output; + } + } + } + } else + return false; + } + + /** + * Used to get current rating + */ + function current_rating($id) + { + global $db; + + if(!is_numeric($id)) + $result = $db->select(tbl('photos'),'allow_rating,rating,rated_by,voters'," photo_key = ".$id.""); + else + $result = $db->select(tbl('photos'),'allow_rating,rating,rated_by,voters'," photo_id = ".$id.""); + + if($result) + return $result[0]; + else + return false; + } + + /** + * Used to rate photo + */ + function rate_photo($id,$rating) + { + global $db,$json; + + if(!is_numeric($rating) || $rating < 1) + $rating = 1; + if($rating > 10) + $rating = 10; + + $c_rating = $this->current_rating($id); + $voters = $c_rating['voters']; + + $new_rate = $c_rating['rating']; + $rated_by = $c_rating['rated_by']; + + if(phpversion < '5.2.0') + $voters = $json->json_decode($voters,TRUE); + else + $voters = json_decode($voters,TRUE); + + if(!empty($voters)) + $already_voted = array_key_exists(userid(),$voters); + + if(!userid()) + e(lang("please_login_to_rate")); + elseif(!empty($already_voted)) + e(str_replace("video","photo",lang("you_hv_already_rated_vdo"))); + elseif($c_rating['allow_rating'] == 'no') + e(str_replace('Video','Photo',lang("vid_rate_disabled"))); + else + { + $voters[userid()] = array('rate'=>$rating,'time'=>NOW()); + if(phpversion < '5.2.0') + $voters = $json->json_encode($voters); + else + $voters = json_encode($voters); + + $t = $c_rating['rated_by'] * $c_rating['rating']; + $rated_by = $c_rating['rated_by'] + 1; + $new_rate = ($t + $rating) / $rated_by; + $db->update(tbl('photos'),array('rating','rated_by','voters'),array("$new_rate","$rated_by","|no_mc|$voters")," photo_id = ".$id.""); + e(lang("thnx_for_voting"),"m"); + } + + $return = array("rating"=>$new_rate,"rated_by"=>$rated_by,'total'=>10,"id"=>$id,"type"=>"photo","disabled"=>"disabled"); + return $return; + } + + /** + * Used to generate different + * embed codes + */ + function generate_embed_codes($p) + { + $details = $p['details']; + $type = $p['type']; + $size = $p['size'] ? $p['size'] : 'm'; + if(is_array($details)) + $photo = $details; + else + $photo = $this->get_photo($detials); + + switch($type) + { + case "html": + { + if($p['with_url']) + $code .= "<a href='".$this->collection->collection_links($photo,'view_item')."' target='_blank'>"; + $code .= "<img src='".$this->get_image_file($photo,$size)."' title='".$photo['photo_title']."' alt='".$photo['photo_title']." ".TITLE."' />"; + if($p['with_url']) + $code .= "</a>"; + } + break; + + case "forum": + { + if($p['with_url']) + $code .= "[URL=".$this->collection->collection_links($photo,'view_item')."]"; + $code .= "[IMG]".$this->get_image_file($photo,$size)."[/IMG]"; + if($p['with_url']) + $code .= "[/URL]"; + } + break; + + case "email": + { + $code .= $this->collection->collection_links($photo,'view_item'); + } + break; + + case "direct": + { + $code .= $this->get_image_file($photo,"o"); + } + break; + + default: + return false; + } + + return $code; + } + + /** + * Embed Codes + */ + function photo_embed_codes($newArr) + { + $t = $newArr['type']; + if(is_array($t)) + $types = $t; + elseif($t == 'all') + $types = $this->embed_types; + else + $types = explode(',',$t); + + foreach($types as $type) + { + $type = str_replace(' ','',$type); + $newArr['type'] = $type; + $codes[] = array("name"=>ucwords($type),"type"=>$type,"code"=>$this->generate_embed_codes($newArr)); + } + + if($newArr['assign']) + assign(mysql_clean($newArr['assign']),$codes); + else + return $codes; + } + + /** + * Used to perform photo actions + */ + function photo_actions($action,$id) + { + global $db; + + switch($action) + { + case "activate": + case "activation": + case "ap": + { + $db->update(tbl($this->p_tbl),array("active"),array("yes")," photo_id = $id"); + e(lang("photo_activated"),"m"); + } + break; + + case "deactivate": + case "deactivation": + case "dap": + { + $db->update(tbl($this->p_tbl),array("active"),array("no")," photo_id = $id"); + e(lang("photo_deactivated"),"m"); + } + break; + + case "make_featured": + case "feature_photo": + case "fp": + { + $db->update(tbl($this->p_tbl),array("featured"),array("yes")," photo_id = $id"); + e(lang("photo_featured"),"m"); + } + break; + + case "make_unfeatured": + case "unfeature_photo": + case "ufp": + { + $db->update(tbl($this->p_tbl),array("featured"),array("no")," photo_id = $id"); + e(lang("photo_unfeatured"),"m"); + } + break; + } + } + +} + + ?> \ No newline at end of file