File indexing completed on 2024-12-22 05:33:23

0001 <?php
0002 
0003 /*
0004  *   TRT GFX 3.0.1 (beta build) BackToSlash
0005  * 
0006  *   support: happy.snizzo@gmail.com
0007  *   website: http://trt-gfx.googlecode.com
0008  *   credits: Claudio Desideri
0009  *   
0010  *   This software is released under the MIT License.
0011  *   http://opensource.org/licenses/mit-license.php
0012  */ 
0013 
0014 
0015 class OCSContent{
0016   
0017   public $id;
0018   public $owner;
0019   public $personid;
0020   public $name;
0021   public $type;
0022   public $description;
0023   public $summary;
0024   public $version;
0025   public $changelog;
0026   public $downloadname1;
0027   public $downloadlink1;
0028   public $votes;
0029   public $score;
0030   public $license;
0031   public $preview1;
0032   public $preview2;
0033   public $preview3;
0034   
0035   private $ocs_content;
0036   private $data;
0037   
0038   
0039   /*
0040    * Enabling main to be on a global context.
0041    */
0042   public function __construct(){
0043     $this->ocs_content = new EModel("ocs_content");
0044   }
0045   
0046   /*
0047    * Load into memory a content from table ocs_content
0048    */
0049   public function load($id){
0050     if($this->ocs_content->is_there("id","id=$id")) {
0051       $r = $this->ocs_content->find("*", "where id=$id LIMIT 1");
0052       $this->id = $id;
0053       $this->owner = $r[0]["owner"];
0054       $this->personid = $r[0]["personid"];
0055       $this->name = $r[0]["name"];
0056       $this->type = $r[0]["type"];
0057       $this->description = $r[0]["description"];
0058       $this->summary = $r[0]["summary"];
0059       $this->version = $r[0]["version"];
0060       $this->changelog = $r[0]["changelog"];
0061       $this->downloadname1 = $r[0]["downloadname1"];
0062       $this->downloadlink1 = $r[0]["downloadlink1"];
0063       $this->votes = $r[0]["votes"];
0064       $this->score = $r[0]["score"];
0065       $this->preview1 = $r[0]["preview1"];
0066       $this->preview2 = $r[0]["preview2"];
0067       $this->preview3 = $r[0]["preview3"];
0068       $this->license = $r[0]["license"];
0069       return true;
0070     } else {
0071       return false;
0072     }
0073   }
0074   
0075   /*
0076    * Set a weighted score to the content.
0077    */
0078   public function set_score($score){
0079     //acquiring data
0080     $oldmedia = $this->score;
0081     $newscore = $score;
0082     
0083     $oldvotes = $this->votes;
0084     $newvotes = $this->votes + 1;
0085     //calculating new media
0086     $newmedia = ($oldmedia * $oldvotes + $newscore) / ($newvotes);
0087     
0088     //setting new infos to local memory object
0089     $this->score = $newmedia;
0090     $this->votes = $newvotes;
0091     
0092     //updating db
0093     EDatabase::q("UPDATE ocs_content SET score=".$this->score.", votes=".$this->votes." WHERE id=".$this->id." LIMIT 1");
0094     
0095   }
0096   
0097   /*
0098    * Add a download file to the current content.
0099    */
0100   public function downloadadd(){
0101     if(!is_dir("content/".$this->id)){
0102       chdir("content");
0103       if(!mkdir($this->id)){
0104         ELog::error("<b>mkdir</b> failed for some reason. Inspect.");
0105         return false;
0106       }
0107       chdir("..");
0108     }
0109     $path = "content/".$this->id."/";
0110     
0111     //clean evetual additional files
0112     EFileSystem::clean_all_files_in($path,array("1","2","3"));
0113     
0114     //if upload file failed print error. Else add link to content object.
0115     if(!EFileSystem::move_uploaded_file_in_ext($path)){
0116       ELog::error("<b>get_uploaded_file</b> failed! Path: ($path) ");
0117       return false;
0118     } else {
0119       $this->downloadlink1 = "http://".EConfig::$data["ocs"]["host"]; //retrieve website running server
0120       $this->downloadlink1 .= "/content/".$this->id."/".EFileSystem::get_uploaded_file_name();
0121       $this->downloadname1 = EFileSystem::get_uploaded_file_name();
0122       EDatabase::q("UPDATE ocs_content SET downloadlink1='".$this->downloadlink1."', downloadname1='".$this->downloadname1."' WHERE id=".$this->id." LIMIT 1");
0123       //activity update
0124       OCSActivity::add(OCSUser::id(), 3, OCSUser::login()." uploaded a new version of ".$this->name);
0125       
0126       return true;
0127     }
0128   }
0129   
0130   /*
0131    * Add a preview file to the current content.
0132    */
0133   public function previewadd($content,$localfile,$preview){
0134     if(!is_dir("content/".$this->id)){
0135       chdir("content");
0136       if(!mkdir($this->id)){
0137         ELog::error("<b>mkdir</b> failed for some reason. Inspect.");
0138         return false;
0139       }
0140       chdir("..");
0141     }
0142     $path = "content/".$this->id."/";
0143     
0144     //if upload file failed print error. Else add link to content object.
0145     //if(!EFileSystem::move_uploaded_file_in($path,$preview)){
0146     if(!EFileSystem::move_uploaded_file_in($path,$preview)){
0147       ELog::error("<b>get_uploaded_file</b> failed! Path: ($path) ");
0148       return false;
0149     } else {
0150       switch($preview){
0151         case 1:
0152           $this->preview1 = "http://".EConfig::$data["ocs"]["host"]; //retrieve website running server
0153           $this->preview1 .= "/content/".$this->id."/".$preview;
0154           $previewlink = $this->preview1;
0155           break;
0156         case 2:
0157           $this->preview2 = "http://".EConfig::$data["ocs"]["host"]; //retrieve website running server
0158           $this->preview2 .= "/content/".$this->id."/".$preview;
0159           $previewlink = $this->preview2;
0160           break;
0161         case 3:
0162           $this->preview3 = "http://".EConfig::$data["ocs"]["host"]; //retrieve website running server
0163           $this->preview3 .= "/content/".$this->id."/".$preview;
0164           $previewlink = $this->preview3;
0165           break;
0166       }
0167       EDatabase::q("UPDATE ocs_content SET preview".$preview."='".$previewlink."' WHERE id=".$this->id." LIMIT 1");
0168       return true;
0169     }
0170   }
0171   
0172   /*
0173    * Delete current download file, if setted.
0174    */
0175   public function downloaddelete(){
0176     $filename = explode("/", $this->downloadlink1);
0177     $filename = $filename[count($filename)-1];
0178     
0179     $path = "content/".$this->id."/".$filename;
0180     //if upload file failed print error. Else add link to content object.
0181     if(file_exists($path)){
0182       unlink($path);
0183       $this->downloadlink1 = "";
0184       EDatabase::q("UPDATE ocs_content SET downloadlink1='' WHERE id=".$this->id." LIMIT 1");
0185     }
0186   }
0187   
0188   /*
0189    * Delete current preview file, if setted.
0190    */
0191   public function previewdelete($content,$preview){
0192     switch($preview){
0193       case 1:
0194         $this->prelink = $this->preview1;
0195         $this->preview1 = "";
0196         break;
0197       case 2:
0198         $this->prelink = $this->preview2;
0199         $this->preview2 = "";
0200         break;
0201       case 3:
0202         $this->prelink = $this->preview3;
0203         $this->preview3 = "";
0204         break; 
0205     }
0206     $filename = explode("/", $this->prelink);
0207     $filename = $filename[count($filename)-1]; 
0208     
0209     $path = "content/".$this->id."/".$filename;
0210     //if upload file failed print error. Else add link to content object.
0211     if(file_exists($path)){
0212       unlink($path);
0213       EDatabase::q("UPDATE ocs_content SET preview$preview='' WHERE id=".$this->id." LIMIT 1");
0214     }
0215   }
0216   
0217   /*
0218    * Returns a boolean if $preview is currently available or not.
0219    */
0220   public function is_preview_available($preview){
0221     switch($preview){
0222       case 1:
0223         if(empty($this->preview1)){ return false; } else { return true; }
0224       case 2:
0225         if(empty($this->preview2)){ return false; } else { return true; }
0226       case 3:
0227         if(empty($this->preview3)){ return false; } else { return true; }
0228     }
0229   }
0230   
0231   /*
0232    * Checks if the current loaded content is owned by $id
0233    */
0234   public function is_owned($id){
0235     if($this->owner == $id){
0236       return true;
0237     } else {
0238       return false;
0239     }
0240   }
0241   
0242   /*
0243    * Return the number of people who has performed a vote action on this object.
0244    */
0245   public function votes(){
0246     return $this->votes;
0247   }
0248   
0249   /*
0250    * Returns the weighted score for this object.
0251    */
0252   public function score(){
0253     return $this->score;
0254   }
0255   
0256   public function license(){
0257     return $this->license;
0258   }
0259   
0260   /*
0261    * Saving the ram rapresentation into memory (database).
0262    */
0263   public function save(){
0264     //TODO: implement unique name.
0265     
0266     //saving
0267     EDatabase::q("INSERT INTO ocs_content (name,type,owner,personid,downloadname1,downloadlink1,description,summary,version,changelog,preview1,preview2,preview3,license) VALUES ('".$this->name."',".$this->type.",".$this->owner.",'".$this->personid."','".$this->downloadname1."','".$this->downloadlink1."','".$this->description."','".$this->summary."','".$this->version."','".$this->changelog."','".$this->preview1."','".$this->preview2."','".$this->preview3."',".$this->license.")");
0268     //updating new id, got from database
0269     $this->id = $id = EDatabase::last_insert_id();
0270   }
0271   
0272   /*
0273    * Automatic update of ocs_table.
0274    */
0275   public function update(){
0276     $this->ocs_content->update("id=".$this->id);
0277   }
0278   
0279   public function delete(){
0280     //EDatabase::q("DELETE FROM ocs_content WHERE id=".$this->id." LIMIT 1");
0281     EDatabase::q("DELETE FROM ocs_content WHERE id=".$this->id." LIMIT 1");
0282     EDatabase::q("DELETE FROM ocs_comment WHERE content=".$this->id);
0283     EFileSystem::rmdir("content/".$this->id);
0284   }
0285   
0286   /*
0287    * Setting id of the owner of the content.
0288    */
0289   public function set_owner($owner){
0290     $this->owner = $owner;
0291   }
0292   
0293   /*
0294    * Manually setting internal data.
0295    */
0296   public function set_data($data){
0297     // assuring those are not evil data to be used as SQL injections
0298     EDatabase::safe($data);
0299     //data validations
0300     if(!isset($data['type'])){ ELog::error("OCSContent: type not defined. Mandatory field."); } else { $this->type = $data['type']; }
0301     if(!isset($data['name'])){ ELog::error("OCSContent: name not defined. Mandatory field."); } else { $this->name = $data['name']; }
0302     if(!isset($data['personid'])){ ELog::error("OCSContent: personid not defined. Mandatory field."); } else { $this->personid = $data['personid']; }
0303     if(!isset($data['downloadname1'])){ $this->downloadname1 = ""; } else { $this->downloadname1 = $data['downloadname1']; }
0304     if(!isset($data['downloadlink1'])){ $this->downloadlink1 = ""; } else { $this->downloadlink1 = $data['downloadlink1']; }
0305     if(!isset($data['description'])){ $this->description = ""; } else { $this->description = $data['description']; }
0306     if(!isset($data['summary'])){ $this->summary = ""; } else { $this->summary = $data['summary']; }
0307     if(!isset($data['version'])){ $this->version = ""; } else { $this->version = $data['version']; }
0308     if(!isset($data['changelog'])){ $this->changelog = ""; } else { $this->changelog = $data['changelog']; }
0309     if(!isset($data['preview1'])){ $this->preview1 = ""; } else { $this->preview1 = $data['preview1']; }
0310     if(!isset($data['preview2'])){ $this->preview2 = ""; } else { $this->preview2 = $data['preview2']; }
0311     if(!isset($data['preview3'])){ $this->preview3 = ""; } else { $this->preview3 = $data['preview3']; }
0312     if(!isset($data['license'])){ $this->license = ""; } else { $this->license = $data['license']; }
0313   }
0314   
0315   public function updated()
0316   {
0317         OCSActivity::add(OCSUser::id(), 6, OCSUser::login()." updated ".$this->name);
0318     }
0319   
0320   /*
0321    * This function returns the associated id for the selected content
0322    */
0323   public function id(){
0324     return $this->id;
0325   }
0326   
0327 }
0328 
0329 
0330 ?>