Nenesse

api.php filmotech_wordpress

Jan 24th, 2014
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.    
  3.     /*
  4.             Filmotech publishing API
  5.             (c) 2013 by Pascal PLUCHON
  6.             http://www.filmotech.fr
  7.     */
  8.    
  9.     /*
  10.             WORDPRESS
  11.     */
  12.    
  13.     require( '../wp-load.php' );
  14.     include('../wp-admin/includes/taxonomy.php');
  15.     $post_id_maj=null;
  16.     $maj = false;
  17.    
  18.     require_once("rest.inc.php");
  19.     require_once("json.inc.php");
  20.     require_once("../include/config.inc.php"); 
  21.        
  22.     class API extends REST {
  23.            
  24.         public $data = "";
  25.         private $cfg;
  26.         private $db = NULL;
  27.        
  28.         // List of allowed methods
  29.         private $services = array("check_server", "check_code", "get_config", "create_poster_directory",
  30.          "get_movie_list", "create_table", "update_publishing_date", "publish" );
  31.  
  32.         public function __construct(){
  33.             parent::__construct();              // Init parent contructor
  34.             $this->cfg = new CONFIG();          // Init database parameters
  35.             $this->dbConnect();                 // Initiate Database connection        
  36.         }
  37.        
  38.         // Database connection
  39.         private function dbConnect(){
  40.             error_reporting(0); // Disable this to see PHP errors
  41.             try
  42.             {
  43.                 if ( $this->cfg->DB_TYPE == 'sqlite' ) {
  44.                     $db_init = new PDO('sqlite:../'.$this->cfg->DB_NAME.'.sqlite3');
  45.                     // $db_init->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  // Enable this to see PDO errors
  46.                 } else {
  47.                     $db_init = new PDO('mysql:host='.$this->cfg->DB_SERVER.';dbname='.$this->cfg->DB_NAME,
  48.                     $this->cfg->DB_USER, $this->cfg->DB_PASSWORD);
  49.                     $db_init->query("SET NAMES UTF8");
  50.                 }
  51.             }
  52.             catch (Exception $e)
  53.             {
  54.                 $error = array( 'error_msg' => $e->getMessage() );
  55.                 $this->response($this->json($error), 412);
  56.             }
  57.             $this->db = $db_init;
  58.         }
  59.        
  60.         // Public method for access api.
  61.         // This method dynmically call the method based on the query string
  62.         public function processApi(){
  63.             $func = strtolower(trim(str_replace("/","",$_REQUEST['rquest'])));
  64.             // if((int)method_exists($this,$func) > 0)
  65.             if (in_array($func,$this->services))
  66.                 $this->$func();
  67.             else
  68.                 $this->response('',404);    // If the method not exist with in this class,
  69.                                             // response would be "Page not found".
  70.         }
  71.        
  72.         // Check if the service is available
  73.         private function check_server(){
  74.             $success = array('status' => "OK" );
  75.             $this->response($this->json($success),200);
  76.         }
  77.        
  78.         // Check the security code (API_ACCESS_CODE) and the access method (POST)
  79.         private function check_code(){
  80.             if($this->get_request_method() != "POST")
  81.             {
  82.                 $error = array('error_code' => "100" );
  83.                 $this->response($this->json($error),401);
  84.             }
  85.  
  86.             if (!$this->_request['code']) {
  87.                 $error = array('error_code' => "101" );
  88.                 $this->response($this->json($error),401);
  89.             }
  90.            
  91.             $code = $this->_request['code'];
  92.             if ($code!=$this->cfg->API_ACCESS_CODE) {
  93.                 $error = array('error_code' => "102" );
  94.                 $this->response($this->json($error),401);
  95.             }
  96.         }
  97.  
  98.         // Get the configuration of the API and some parameters
  99.         protected function get_config(){       
  100.             $this->check_code();
  101.            
  102.             $tableau = array('status' => 'OK' );
  103.             $tableau["API_VERSION"] = $this->cfg->API_VERSION;
  104.             $tableau["POSTERS_DIRECTORY"] = $this->cfg->POSTERS_DIRECTORY;
  105.             $tableau["DB_TABLE"] = $this->cfg->DB_TABLE;
  106.             $tableau["PHP_VERSION"] = PHP_VERSION;
  107.             $this->response($this->json($tableau),200);
  108.         }
  109.        
  110.         // Create poster directory
  111.         private function create_poster_directory(){
  112.             $this->check_code();
  113.  
  114.             $result = false;
  115.             $repertoire_affiches = '../' . $this->cfg->POSTERS_DIRECTORY;
  116.             if (!is_dir($repertoire_affiches)) {
  117.                 $result = mkdir($repertoire_affiches);
  118.                 if (!$result)
  119.                 {
  120.                     $error = array( 'error_code' => '201'  );
  121.                     $this->response($this->json($error),424);
  122.                 }
  123.             }
  124.             if (isset($_POST['forceCHMOD'])) chmod( $repertoire_affiches , 0777 );
  125.  
  126.             $success = array('status' => 'OK' );
  127.             $this->response($this->json($success),200);
  128.         }
  129.        
  130.         // Empty poster directory
  131.         private function empty_poster_directory(){
  132.             $this->check_code();
  133.  
  134.             $repertoire_affiches = '../' . $this->cfg->POSTERS_DIRECTORY;
  135.             foreach (glob($repertoire_affiches.'/Filmotech*.jpg') as $filename) {
  136.                 unlink($filename);
  137.             }
  138.         }
  139.        
  140.         // Return ID/Update date from the database
  141.         private function get_movie_list(){     
  142.             $this->check_code();
  143.             /*
  144.             $tableau = array('status' => 'OK' );       
  145.             $res = $this->db->query("SELECT ID, DateHeureMAJ FROM " . $this->cfg->DB_TABLE );
  146.             foreach ($res as $row) {
  147.                 $tableau[$row['ID']] = $row['DateHeureMAJ'];               
  148.             }
  149.             */
  150.             $filename = 'logs.txt';
  151.             if (!$handle = fopen($filename, 'w')) {
  152.                 $error = array('error_code' , '400' );
  153.                 $this->response($this->json($$error),424);
  154.             }
  155.            
  156.             $tableau = array('status' => 'OK' );
  157.             $args = array('numberposts' => -1);
  158.             $lastposts = get_posts( $args );
  159.             foreach($lastposts as $post) : setup_postdata($post);
  160.                 $ID=get_post_meta($post->ID, 'id_filmotech',true);
  161.                 $tableau[$ID] = get_post_meta($post->ID, 'dvd_date_maj',true); 
  162.                 fwrite($handle, $ID." - ".$tableau[$ID]."\n");
  163.             endforeach;
  164.             wp_reset_postdata();
  165.  
  166.             fclose($handle);
  167.            
  168.             $this->response($this->json($tableau),200);
  169.         }
  170.  
  171.         // Return ID/Update date from the WORDPRESS
  172.         private function get_movie_list_wordpress(){
  173.             $this->check_code();
  174.             $tableau = array('status' => 'OK' );
  175.             /*
  176.             $args = array('numberposts' => -1);
  177.             $lastposts = get_posts( $args );
  178.            
  179.             foreach($lastposts as $post) : setup_postdata($post);
  180.                 //$chaine=the_ID(). $separator .utf8_decode(get_the_title(get_the_ID())). $separator.utf8_decode(get_the_date());
  181.                 //$chaine=get_post_meta(get_the_ID(), 'id_filmotech',true). $separator .utf8_decode(get_the_title(get_the_ID())). $separator.utf8_decode(get_the_date("Y-m-d H:i:s"));
  182.                 //$titre_=utf8_decode(get_the_title(get_the_ID()));
  183.                 //$chaine=get_post_meta(get_the_ID(), 'id_filmotech',true). $separator .html_entity_decode($titre_,ENT_NOQUOTES,"UTF-8"). $separator.utf8_decode(get_the_date("Y-m-d H:i:s"));
  184.                 //dvd_date_maj
  185.                 //$chaine=get_post_meta(get_the_ID(), 'id_filmotech',true). $separator .html_entity_decode($titre_,ENT_NOQUOTES,"UTF-8"). $separator.get_post_meta(get_the_ID(), 'dvd_date_maj',true);
  186.                 $ID=get_post_meta(get_the_ID(), 'id_filmotech',true);
  187.                 $tableau[$ID] = get_post_meta(get_the_ID(), 'dvd_date_maj',true);  
  188.             endforeach;
  189.             */
  190.             $this->response($this->json($tableau),200);
  191.            
  192.         }
  193.        
  194.         private function create_table_sqlite() {
  195.             $sql =
  196.               "CREATE TABLE " . $this->cfg->DB_TABLE . " ("
  197.             . "ID integer NOT NULL PRIMARY KEY,"
  198.             . "DateHeureMAJ TimeStamp NOT NULL default '0000-00-00 00:00:00',"
  199.             . "TitreVF varchar(255) NOT NULL default '',"
  200.             . "TitreVO varchar(255) default '',"
  201.             . "Genre varchar(50) default '',"
  202.             . "Pays varchar(255) default '',"
  203.             . "Annee varchar(10) default '',"
  204.             . "Duree int(11) default '0',"
  205.             . "Note int(11) default '0',"
  206.             . "Synopsis text ,"
  207.             . "Acteurs text ,"
  208.             . "Realisateurs text ,"
  209.             . "Commentaires text ,"
  210.             . "Support varchar(50) default '',"
  211.             . "NombreSupport int(11) default '0',"
  212.             . "Edition varchar(255) default '',"
  213.             . "Zone varchar(10) default '',"
  214.             . "Langues varchar(255) default '',"
  215.             . "SousTitres varchar(255) default '',"
  216.             . "Audio varchar(255) default '',"
  217.             . "Bonus text ,"
  218.             . "EntreeType varchar(255) default '',"
  219.             . "EntreeSource varchar(255) default '',"
  220.             . "EntreeDate date default '0000-00-00',"
  221.             . "EntreePrix float default '0',"
  222.             . "Sortie varchar(10) default '',"
  223.             . "SortieType varchar(255) default '',"
  224.             . "SortieDestinataire varchar(255) default '',"
  225.             . "SortieDate date default '0000-00-00',"
  226.             . "SortiePrix float default '0',"
  227.             . "PretEnCours varchar(10) default '',"
  228.             . "FilmVu varchar(5) default 'NON',"
  229.             . "Reference varchar(255) default '',"
  230.             . "BAChemin varchar(255) default '',"
  231.             . "BAType varchar(10) default '',"
  232.             . "MediaChemin varchar(255) default '',"
  233.             . "MediaType varchar(10) default '');"
  234.             . "CREATE INDEX films_idx ON " . $this->cfg->DB_TABLE . " (TitreVF ASC);";
  235.  
  236.             try
  237.             {
  238.                 $this->db->query($sql);
  239.                 $success = array('status' => "OK" );
  240.                 $this->response($this->json($success),200);
  241.             }
  242.             catch (Exception $e)
  243.             {
  244.                 $this->db->query($sql);
  245.                 $success = array('status' => "KO" );
  246.                 $this->response($this->json($success),200);
  247.             }
  248.    
  249.         }      
  250.  
  251.         private function create_table_mysql() {
  252.             $sql = 'CREATE TABLE IF NOT EXISTS `' . $this->cfg->DB_TABLE . '` ('
  253.             . ' `ID` bigint(20) NOT NULL,'
  254.             . ' `DateHeureMAJ` datetime NOT NULL default \'0000-00-00 00:00:00\','
  255.             . ' `TitreVF` varchar(255) NOT NULL default \'\','
  256.             . ' `TitreVO` varchar(255) NOT NULL default \'\','
  257.             . ' `Genre` varchar(50) NOT NULL default \'\','
  258.             . ' `Pays` varchar(255) NOT NULL default \'\','
  259.             . ' `Annee` varchar(10) NOT NULL default \'\','
  260.             . ' `Duree` int(11) NOT NULL default \'0\','
  261.             . ' `Note` int(11) NOT NULL default \'0\','
  262.             . ' `Synopsis` text,'
  263.             . ' `Acteurs` text,'
  264.             . ' `Realisateurs` text,'
  265.             . ' `Commentaires` text,'
  266.             . ' `Support` varchar(50) NOT NULL default \'\','
  267.             . ' `NombreSupport` int(11) NOT NULL default \'0\','
  268.             . ' `Edition` varchar(255) NOT NULL default \'\','
  269.             . ' `Zone` varchar(10) NOT NULL default \'\','
  270.             . ' `Langues` varchar(255) NOT NULL default \'\','
  271.             . ' `SousTitres` varchar(255) NOT NULL default \'\','
  272.             . ' `Audio` varchar(255) NOT NULL default \'\','
  273.             . ' `Bonus` text,'
  274.             . ' `EntreeType` varchar(255) NOT NULL default \'\','
  275.             . ' `EntreeSource` varchar(255) NOT NULL default \'\','
  276.             . ' `EntreeDate` date NOT NULL default \'0000-00-00\','
  277.             . ' `EntreePrix` float NOT NULL default \'0\','
  278.             . ' `Sortie` varchar(10) NOT NULL default \'\','
  279.             . ' `SortieType` varchar(255) NOT NULL default \'\','
  280.             . ' `SortieDestinataire` varchar(255) NOT NULL default \'\','
  281.             . ' `SortieDate` date NOT NULL default \'0000-00-00\','
  282.             . ' `SortiePrix` float NOT NULL default \'0\','
  283.             . ' `PretEnCours` varchar(10) NOT NULL default \'\','
  284.             . ' `FilmVu` varchar(5) NOT NULL default \'NON\','
  285.             . ' `Reference` varchar(255) NOT NULL default \'\','
  286.             . ' `BAChemin` varchar(255) NOT NULL default \'\','
  287.             . ' `BAType` varchar(10) NOT NULL default \'\','
  288.             . ' `MediaChemin` varchar(255) NOT NULL default \'\','
  289.             . ' `MediaType` varchar(10) NOT NULL default \'\','
  290.             . ' PRIMARY KEY (`ID`),'
  291.             . ' KEY `TitreVF` (`TitreVF`)'
  292.             . ' ) ENGINE=MyISAM DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;';
  293.  
  294.             try
  295.             {
  296.                 $this->db->query($sql);
  297.             }
  298.             catch (Exception $e)
  299.             {
  300.                 $error = array('error_code' => "200" , 'error_msg' => $e->getMessage() );
  301.                 $this->response($this->json($error),424);
  302.             }
  303.             $success = array('status' => "OK" );
  304.             $this->response($this->json($success),200);
  305.         }
  306.        
  307.         // Create the table in the database
  308.         private function create_table() {      
  309.             $this->check_code();
  310.            
  311.             if ( $this->cfg->DB_TYPE == 'sqlite' )
  312.                 { $this->create_table_sqlite(); }
  313.             else
  314.                 { $this->create_table_mysql(); }
  315.            
  316.         }
  317.  
  318.         // Remove a record and his poster (if any)
  319.         private function del_record() {        
  320.             $this->check_code();
  321.             $sql = "DELETE FROM " . $this->cfg->DB_TABLE . " WHERE ID = " . $this->_request['ID'];
  322.             try {
  323.                 $this->db->query($sql);
  324.             } catch (Exception $e) {
  325.                 $error = array('error_code' => '500' , 'error_msg' => $e->getMessage()  );
  326.                 $this->response($this->json($error),424);
  327.             }
  328.             $repertoire_affiches = '../' . $this->cfg->POSTERS_DIRECTORY;
  329.             $filename = sprintf($repertoire_affiches.'/Filmotech_%05d.jpg' , $this->_request['ID'] );
  330.             if (file_exists($filename)) { unlink($filename); }
  331.         }
  332.        
  333.         // Remove a record and his poster (if any) WORDPRESS
  334.         private function del_record_wordpress() {          
  335.             $this->check_code();
  336.             //retrouver le post ui correspond a ID
  337.             $args = array('meta_key'=>'id_filmotech', 'meta_value'=>$this->_request['ID']);
  338.             $lastposts = get_posts( $args );
  339.             foreach($lastposts as $post) : setup_postdata($post);
  340.                 //On efface l'affiche
  341.                 $id_filmo=get_post_meta(get_the_ID(), 'id_filmotech',true);
  342.                 $repertoire_affiches = '../' . $this->cfg->POSTERS_DIRECTORY;
  343.                 $filename = sprintf($repertoire_affiches.'/Filmotech_%05d.jpg' , $id_filmo );
  344.                 if (file_exists($filename)) unlink($filename);
  345.                 $post_id_maj=get_the_ID();
  346.                 $maj=true;
  347.                 //wp_delete_post(get_the_ID());
  348.             endforeach;
  349.         }
  350.        
  351.         // Add a poster
  352.         private function add_poster() {
  353.             $this->check_code();
  354.             $repertoire_affiches = '../' . $this->cfg->POSTERS_DIRECTORY;
  355.             if (isset($this->_request['Affiche'])) {
  356.                 $affiche = base64_decode($this->_request['Affiche']);
  357.                 $filename = sprintf($repertoire_affiches.'/Filmotech_%05d.jpg' , $this->_request['ID'] );
  358.                 if (!$handle = fopen($filename, 'wb')) {
  359.                     $error = array( 'error_code' => '301' );
  360.                     $this->response($this->json($error),424);
  361.                 }
  362.                 if (fwrite($handle, $affiche) === FALSE) {
  363.                     $error = array( 'error_code' => '302' );
  364.                     $this->response($this->json($error),424);
  365.                 }
  366.                 fclose($handle);
  367.                 if (isset($this->_request['forceCHMOD'])) chmod( $filename , 0777 );
  368.             }
  369.            
  370.         }
  371.  
  372.         // Prepare SQL statement according to db type
  373.         private function sql_escape($field) {
  374.             if ( $this->cfg->DB_TYPE == 'sqlite' )
  375.                 { return str_replace('\'','\'\'',$field); }
  376.             else
  377.                 { return addslashes($field); }
  378.  
  379.         }
  380.        
  381.         // Add a record and the poster (if any)
  382.         private function add_record() {        
  383.             $this->check_code();
  384.             $champs = array( "DateHeureMAJ", "TitreVF", "TitreVO", "Genre", "Pays", "Annee", "Duree", "Note", "Synopsis", "Acteurs", "Realisateurs", "Commentaires", "Support", "NombreSupport", "Edition", "Zone", "Langues", "SousTitres", "Audio", "Bonus", "EntreeType", "EntreeSource", "EntreeDate", "EntreePrix", "Sortie", "SortieType", "SortieDestinataire", "SortieDate", "SortiePrix", "PretEnCours", "FilmVu", "Reference", "BAChemin", "BAType", "MediaChemin", "MediaType" );
  385.  
  386.  
  387.             $sql = 'INSERT INTO ' . $this->cfg->DB_TABLE . '(ID';
  388.             foreach ($champs as $value) {
  389.                 $sql .= ', ' . $value;
  390.             }
  391.             $sql .= ') VALUES(\''.$this->_request['ID'].'\'';
  392.  
  393.             foreach ($champs as $value) {
  394.                 $sql .= ', \'' . $this->sql_escape($this->_request[$value]) . '\'';
  395.             }
  396.            
  397.             $sql .= ");";
  398.  
  399.             try { $data = $this->db->query($sql);
  400.             }
  401.             catch (Exception $e)
  402.             {
  403.                 $tableau = array('error_code' => '300' , 'error_msg' => $e->getMessage() );
  404.                 $this->response($this->json($tableau),424);
  405.             }
  406.             $this->add_poster();
  407.         }
  408.        
  409.         // WORDPRESS
  410.         // Add a record and the poster (if any)
  411.         private function add_record_wordpress(){
  412.             //Gestion des prêts en cours
  413.                 $pret = $_POST['PretEnCours'];
  414.                 $resultpret= "OUI";
  415.                     if ($pret == $resultpret) {
  416.                     $pretdef = "<font color= '#FF0000' > Oui </FONT>";
  417.                     }
  418.                     else {
  419.                     $pretdef= "<font color= '#008000' > Non </FONT>";      
  420.                     }
  421.             //Gestion des films vus
  422.                 $vus = $_POST['FilmVu'];
  423.                 $resultvus= "OUI";
  424.                     if ($vus == $resultvus) {
  425.                         $vusdef = "<font color= '#008000' > Oui </FONT>";
  426.                     }
  427.                     else {
  428.                         $vusdef= "<font color= '#FF0000' > Non </FONT>";       
  429.                     }  
  430.                    
  431.             $bonus = $_POST['Bonus'];
  432.             $synopsis = $_POST['Synopsis'];
  433.             $this->check_code();
  434.             $repertoire_affiches = $this->cfg->POSTERS_DIRECTORY;
  435.             $filename = sprintf($repertoire_affiches.'/Filmotech_%05d.jpg' , $this->_request['ID']);
  436.            
  437.             //Gestion des acteurs pour ajouter un lien pour chacun
  438.                 $liens_acteurs="";
  439.                 $acteurs=str_replace(array("\r", "\r\n", "\n"),'|',$this->_request['Acteurs'], $count);
  440.                 //printf( "Count : " .$count."\n");
  441.                 $liste_acteurs = explode("|", $acteurs);
  442.                 $virgule=",";
  443.                 for($i=0;$i<count($liste_acteurs);$i++) {
  444.                     //printf( "liste_acteur : " .str_replace('\n','',$liste_acteurs[$i]));
  445.                     if ($i==count($liste_acteurs)-1) $virgule="";//si dernier éléments du tableau, pas de virgule a la fin de la chaine de caractères
  446.                     $critere_recherche=str_replace(' ','+',$liste_acteurs[$i]);//On remplace les espaces par des +
  447.                     $liens_acteurs.='<a href="?s='.$critere_recherche.'">'.$liste_acteurs[$i].'</a>'.$virgule.' ';
  448.                 }
  449.    
  450.             $id_category = wp_create_category($this->sql_escape($this->_request['Genre']));
  451.             $post_title     = stripcslashes($this->sql_escape($this->_request['TitreVF']));
  452.             $post_content=' <div id="affiche">
  453.             <div class="reflection" align="center">
  454.                 <img class="reflect" src="' . $filename . '" alt="Affiche">
  455.             </div>
  456.             </div>
  457.        
  458.             <p align="center">'.$this->sql_escape($this->_request['TitreVO']).'</p>
  459.             <table width="90%" align="center">
  460.                 <tr><td><strong>Annee :</strong> '.$_POST['Annee'].'</td><td><strong>Duree :</strong> '.$this->sql_escape($this->_request['Duree']).' mn</td></tr>
  461.                 <tr><td><strong>Pays :</strong> '.$_POST['Pays'].'</td><td><strong>Genre :</strong> '.$this->sql_escape($this->_request['Genre']).'</td></tr>
  462.                 <tr><td><strong>Support :</strong> '.$_POST['Support'].'</td><td><strong>Zone :</strong> '.$this->sql_escape($this->_request['Zone']).'</td></tr>
  463.                 <tr><td><strong>Pret en Cours:</strong> '.$pretdef.'</td><td><strong>Film Vu:</strong> '.$vusdef.'</td></tr>
  464.             </table>
  465.             <p><strong>Synopsis :</strong> '.$synopsis.'</p>
  466.             <!--more-->
  467.             <p><strong>Acteurs :</strong> '.$liens_acteurs.'</p>
  468.             <p><strong>Realisateurs :</strong> '.$this->sql_escape($this->_request['Realisateurs']).'</p>
  469.             <p><strong>Langues :</strong> '.$this->sql_escape($this->_request['langues']).'</p>
  470.             <p><strong>Sous-Titres :</strong> '.$this->sql_escape($this->_request['SousTitres']).'</p>
  471.             <p><strong>Audio :</strong> '.$this->sql_escape($this->_request['Audio']).'</p>
  472.             <p><strong>Bonus : </strong> '.$bonus.'</p>
  473.             <p><strong>Bande-annonce :</strong> </p>
  474.             '.$this->sql_escape($this->_request['BAChemin']); //pas de formatage (mise en page avec <p> retour chariot) de la ligne pour le bon fonctionnement du plugin Youtube
  475.             //str_replace('<br />',', ',nl2br($_POST['Acteurs']))
  476.  
  477.            
  478.             $my_post = array(
  479.                 'ID' => $post_id_maj,
  480.                 'post_title'    => $post_title,
  481.                 'post_content'    => $post_content, //The full text of the post.
  482.                 'post_status'    => 'publish',
  483.                 'post_author'    => 1, //The user ID number of the author.
  484.                 'post_category'    => array($id_category),  //Add some categories.
  485.                 'post_date'        => $this->sql_escape($this->_request['EntreeDate']), //The time post was made.
  486.                 'post_name'        => sanitize_file_name($this->sql_escape($this->_request['TitreVF'])), // The name (slug) for your post
  487.                 'post_type'        => 'post', // post, page, link, menu item , custom post type
  488.                 'tags_input'    => $this->sql_escape($this->_request['Genre']) // For tags
  489.             );
  490.            
  491.             // Insert the post into the database
  492.             $id_post=wp_insert_post( $my_post );
  493.            
  494.             if($maj==false){
  495.                 add_post_meta($id_post, "id_filmotech", $this->_request['ID']);
  496.                 add_post_meta($id_post, "dvd_cover", $filename);
  497.                 add_post_meta($id_post, "dvd_note",$this->_request['note']);
  498.                 add_post_meta($id_post, "dvd_ba", $this->_request['BAChemin']);
  499.                 add_post_meta($id_post, "dvd_date_maj", $this->_request['DateHeureMAJ']);//DateHeureMAJ
  500.                 add_post_meta($id_post, "dvd_pays", $this->_request['Pays']);//Pays
  501.             }
  502.             else {
  503.                 update_post_meta($id_post, "id_filmotech", $this->_request['ID']);
  504.                 update_post_meta($id_post, "dvd_cover", $filename);
  505.                 update_post_meta($id_post, "dvd_note", $this->_request['note']);
  506.                 update_post_meta($id_post, "dvd_ba", $this->_request['BAChemin']);
  507.                 update_post_meta($id_post, "dvd_date_maj", $this->_request['DateHeureMAJ']);//DateHeureMAJ
  508.                 update_post_meta($id_post, "dvd_pays", $this->_request['Pays']);//Pays
  509.             }
  510.             $this->add_poster();
  511.         }
  512.  
  513.         // Update the last publishing date (shown in the movie list page)
  514.         private function update_publishing_date(){
  515.             $this->check_code();
  516.             $filename = '../update.txt';
  517.             if (!$handle = fopen($filename, 'w')) {
  518.                 $error = array('error_code' , '400' );
  519.                 $this->response($this->json($$error),424);
  520.             }
  521.             if (fwrite($handle, $_POST['DateMAJ'] ) === FALSE) {
  522.                 $error = array('error_code' , '401' );
  523.                 $this->response($this->json($error),424);
  524.             }
  525.             fclose($handle);
  526.             $success = array('status' => 'OK' );
  527.             $this->response($this->json($success),200);
  528.         }
  529.        
  530.         // Main processs, add, update or remove records
  531.         private function publish(){
  532.             $this->check_code();
  533.            
  534.             if (isset($this->_request['ForceUpdate'])) $this->empty_poster_directory();
  535.            
  536.             if ($this->_request['ACTION']=='ADD') {
  537.                 //$this->add_record();
  538.                 /*WORDPRESS*/
  539.                 $this->add_record_wordpress();
  540.             }
  541.             if ($this->_request['ACTION']=='UPDATE') {
  542.                 //$this->del_record();
  543.                 //$this->add_record();
  544.                 /*WORDPRESS*/
  545.                 $this->del_record_wordpress();
  546.                 $this->add_record_wordpress();
  547.             }
  548.             if ($this->_request['ACTION']=='DELETE') {
  549.                 //$this->del_record();
  550.                 /*WORDPRESS*/
  551.                 $this->del_record_wordpress();
  552.             }
  553.  
  554.             $tableau = array("action" => $this->_request['ACTION'] ,
  555.                 "TitreVF" => $this->_request['TitreVF'] , "ID" => $this->_request['ID'] );
  556.             $this->response($this->json($tableau),200);
  557.         }
  558.  
  559.         // Encode array into JSON
  560.         private function json($data){
  561.         // create a new instance of Services_JSON
  562.             $json = new Services_JSON();
  563.             if(is_array($data)){
  564.                 return $json->encode($data);
  565.             }
  566.         }
  567.     }
  568.  
  569.     // Initiiate Library
  570.    
  571.     $api = new API;
  572.     $api->processApi();
  573. ?>
Add Comment
Please, Sign In to add comment